blob: be16c769360b8e02bb10a69bcc387c4ec4122ea7 [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000017#include <ctype.h>
18
reed@google.com8a85d0c2011-06-24 19:12:12 +000019#include "SkData.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000021#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPaint.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000023#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000024#include "SkPDFFont.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000026#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000027#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000028#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000030#include "SkStream.h"
31#include "SkTypeface.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000032#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000033#include "SkUtils.h"
34
35namespace {
36
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000037bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
38 size_t* size) {
39 // PFB sections have a two or six bytes header. 0x80 and a one byte
40 // section type followed by a four byte section length. Type one is
41 // an ASCII section (includes a length), type two is a binary section
42 // (includes a length) and type three is an EOF marker with no length.
43 const uint8_t* buf = *src;
44 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
45 return false;
46 if (buf[1] == 3)
47 return true;
48 if (*len < 6)
49 return false;
50
vandebo@chromium.org73322072011-06-21 21:19:41 +000051 *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
52 ((size_t)buf[5] << 24);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000053 size_t consumed = *size + 6;
54 if (consumed > *len)
55 return false;
56 *src = *src + consumed;
57 *len = *len - consumed;
58 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000059}
60
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000061bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
62 size_t* dataLen, size_t* trailerLen) {
63 const uint8_t* srcPtr = src;
64 size_t remaining = size;
65
66 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
67 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
68 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
69 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000070}
71
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000072/* The sections of a PFA file are implicitly defined. The body starts
73 * after the line containing "eexec," and the trailer starts with 512
74 * literal 0's followed by "cleartomark" (plus arbitrary white space).
75 *
76 * This function assumes that src is NUL terminated, but the NUL
77 * termination is not included in size.
78 *
79 */
80bool parsePFA(const char* src, size_t size, size_t* headerLen,
81 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
82 const char* end = src + size;
83
84 const char* dataPos = strstr(src, "eexec");
85 if (!dataPos)
86 return false;
87 dataPos += strlen("eexec");
88 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
89 dataPos < end)
90 dataPos++;
91 *headerLen = dataPos - src;
92
93 const char* trailerPos = strstr(dataPos, "cleartomark");
94 if (!trailerPos)
95 return false;
96 int zeroCount = 0;
97 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
98 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
99 continue;
100 } else if (*trailerPos == '0') {
101 zeroCount++;
102 } else {
103 return false;
104 }
105 }
106 if (zeroCount != 512)
107 return false;
108
109 *hexDataLen = trailerPos - src - *headerLen;
110 *trailerLen = size - *headerLen - *hexDataLen;
111
112 // Verify that the data section is hex encoded and count the bytes.
113 int nibbles = 0;
114 for (; dataPos < trailerPos; dataPos++) {
115 if (isspace(*dataPos))
116 continue;
117 if (!isxdigit(*dataPos))
118 return false;
119 nibbles++;
120 }
121 *dataLen = (nibbles + 1) / 2;
122
123 return true;
124}
125
126int8_t hexToBin(uint8_t c) {
127 if (!isxdigit(c))
128 return -1;
129 if (c <= '9') return c - '0';
130 if (c <= 'F') return c - 'A' + 10;
131 if (c <= 'f') return c - 'a' + 10;
132 return -1;
133}
134
135SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
136 size_t* dataLen, size_t* trailerLen) {
137 // srcStream may be backed by a file or a unseekable fd, so we may not be
138 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
139 // the input only once is doable, but very ugly. Furthermore, it'd be nice
140 // if the data was NUL terminated so that we can use strstr() to search it.
141 // Make as few copies as possible given these constraints.
142 SkDynamicMemoryWStream dynamicStream;
143 SkRefPtr<SkMemoryStream> staticStream;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000144 SkData* data = NULL;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000145 const uint8_t* src;
146 size_t srcLen;
147 if ((srcLen = srcStream->getLength()) > 0) {
148 staticStream = new SkMemoryStream(srcLen + 1);
149 staticStream->unref(); // new and SkRefPtr both took a ref.
150 src = (const uint8_t*)staticStream->getMemoryBase();
151 if (srcStream->getMemoryBase() != NULL) {
152 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
153 } else {
154 size_t read = 0;
155 while (read < srcLen) {
156 size_t got = srcStream->read((void *)staticStream->getAtPos(),
157 srcLen - read);
158 if (got == 0)
159 return NULL;
160 read += got;
161 staticStream->seek(read);
162 }
163 }
164 ((uint8_t *)src)[srcLen] = 0;
165 } else {
166 static const size_t bufSize = 4096;
167 uint8_t buf[bufSize];
168 size_t amount;
169 while ((amount = srcStream->read(buf, bufSize)) > 0)
170 dynamicStream.write(buf, amount);
171 amount = 0;
172 dynamicStream.write(&amount, 1); // NULL terminator.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000173 data = dynamicStream.copyToData();
174 src = data->bytes();
175 srcLen = data->size() - 1;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000176 }
177
reed@google.com8a85d0c2011-06-24 19:12:12 +0000178 // this handles releasing the data we may have gotten from dynamicStream.
179 // if data is null, it is a no-op
180 SkAutoDataUnref aud(data);
181
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000182 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
183 SkMemoryStream* result =
184 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
185 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
186 result->seek(*headerLen);
187 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
188 result->seek(*headerLen + *dataLen);
189 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
190 *trailerLen);
191 result->rewind();
192 return result;
193 }
194
195 // A PFA has to be converted for PDF.
196 size_t hexDataLen;
197 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
198 trailerLen)) {
199 SkMemoryStream* result =
200 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
201 memcpy((char*)result->getAtPos(), src, *headerLen);
202 result->seek(*headerLen);
203
204 const uint8_t* hexData = src + *headerLen;
205 const uint8_t* trailer = hexData + hexDataLen;
206 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000207 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000208 bool highNibble = true;
209 for (; hexData < trailer; hexData++) {
210 char curNibble = hexToBin(*hexData);
211 if (curNibble < 0)
212 continue;
213 if (highNibble) {
214 dataByte = curNibble << 4;
215 highNibble = false;
216 } else {
217 dataByte |= curNibble;
218 highNibble = true;
219 ((char *)result->getAtPos())[outputOffset++] = dataByte;
220 }
221 }
222 if (!highNibble)
223 ((char *)result->getAtPos())[outputOffset++] = dataByte;
224 SkASSERT(outputOffset == *dataLen);
225 result->seek(*headerLen + outputOffset);
226
227 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
228 *trailerLen);
229 result->rewind();
230 return result;
231 }
232
233 return NULL;
234}
235
reed@google.com3f0dcf92011-03-18 21:23:45 +0000236// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000237SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000238 SkScalar scaled = SkIntToScalar(val);
239 if (emSize == 1000) {
240 return scaled;
241 } else {
242 return SkScalarMulDiv(scaled, 1000, emSize);
243 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000244}
245
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000246void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000247 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000248 // Specify width and bounding box for the glyph.
249 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000250 content->writeText(" 0 ");
251 content->writeDecAsText(box.fLeft);
252 content->writeText(" ");
253 content->writeDecAsText(box.fTop);
254 content->writeText(" ");
255 content->writeDecAsText(box.fRight);
256 content->writeText(" ");
257 content->writeDecAsText(box.fBottom);
258 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000259}
260
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000261SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000262 SkPDFArray* bbox = new SkPDFArray;
263 bbox->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000264 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
265 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
266 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
267 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000268 return bbox;
269}
270
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000271SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
272 SkPDFArray* array) {
reed@google.comc789cf12011-07-20 12:14:33 +0000273 array->appendScalar(scaleFromFontUnits(width, emSize));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000274 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000275}
276
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000277SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000278 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
279 uint16_t emSize, SkPDFArray* array) {
280 appendWidth(advance.fVerticalAdvance, emSize, array);
281 appendWidth(advance.fOriginXDisp, emSize, array);
282 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000283 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000284}
285
286template <typename Data>
287SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000288 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
289 uint16_t emSize,
290 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
291 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000292 Data* defaultAdvance) {
293 SkPDFArray* result = new SkPDFArray();
294 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
295 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000296 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000297 SkASSERT(advanceInfo->fAdvance.count() == 1);
298 *defaultAdvance = advanceInfo->fAdvance[0];
299 break;
300 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000301 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000302 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
303 advanceArray->unref(); // SkRefPtr and new both took a ref.
304 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000305 appendAdvance(advanceInfo->fAdvance[j], emSize,
306 advanceArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000307 result->appendInt(advanceInfo->fStartId);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000308 result->append(advanceArray.get());
309 break;
310 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000311 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000312 SkASSERT(advanceInfo->fAdvance.count() == 1);
reed@google.comc789cf12011-07-20 12:14:33 +0000313 result->appendInt(advanceInfo->fStartId);
314 result->appendInt(advanceInfo->fEndId);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000315 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000316 break;
317 }
318 }
319 }
320 return result;
321}
322
323} // namespace
324
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000325static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
326 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
327 // It's there to prevent old version Adobe Readers from malfunctioning.
328 const char* kHeader =
329 "/CIDInit /ProcSet findresource begin\n"
330 "12 dict begin\n"
331 "begincmap\n";
332 cmap->writeText(kHeader);
333
334 // The /CIDSystemInfo must be consistent to the one in
335 // SkPDFFont::populateCIDFont().
336 // We can not pass over the system info object here because the format is
337 // different. This is not a reference object.
338 const char* kSysInfo =
339 "/CIDSystemInfo\n"
340 "<< /Registry (Adobe)\n"
341 "/Ordering (UCS)\n"
342 "/Supplement 0\n"
343 ">> def\n";
344 cmap->writeText(kSysInfo);
345
346 // The CMapName must be consistent to /CIDSystemInfo above.
347 // /CMapType 2 means ToUnicode.
348 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
349 // code table from unsigned short (16-bits). Codespace range just tells the
350 // PDF processor the valid range. It does not matter whether a complete
351 // mapping is provided or not.
352 const char* kTypeInfo =
353 "/CMapName /Adobe-Identity-UCS def\n"
354 "/CMapType 2 def\n"
355 "1 begincodespacerange\n"
356 "<0000> <FFFF>\n"
357 "endcodespacerange\n";
358 cmap->writeText(kTypeInfo);
359}
360
361static void append_cmap_bfchar_table(uint16_t* glyph_id, SkUnichar* unicode,
362 size_t count,
363 SkDynamicMemoryWStream* cmap) {
364 cmap->writeDecAsText(count);
365 cmap->writeText(" beginbfchar\n");
366 for (size_t i = 0; i < count; ++i) {
367 cmap->writeText("<");
368 cmap->writeHexAsText(glyph_id[i], 4);
369 cmap->writeText("> <");
370 cmap->writeHexAsText(unicode[i], 4);
371 cmap->writeText(">\n");
372 }
373 cmap->writeText("endbfchar\n");
374}
375
376static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
377 const char* kFooter =
378 "endcmap\n"
379 "CMapName currentdict /CMap defineresource pop\n"
380 "end\n"
381 "end";
382 cmap->writeText(kFooter);
383}
384
385// Generate <bfchar> table according to PDF spec 1.4 and Adobe Technote 5014.
386static void append_cmap_bfchar_sections(
387 const SkTDArray<SkUnichar>& glyphUnicode,
388 SkDynamicMemoryWStream* cmap) {
389 // PDF spec defines that every bf* list can have at most 100 entries.
390 const size_t kMaxEntries = 100;
391 uint16_t glyphId[kMaxEntries];
392 SkUnichar unicode[kMaxEntries];
393 size_t index = 0;
394 for (int i = 0; i < glyphUnicode.count(); i++) {
395 if (glyphUnicode[i]) {
396 glyphId[index] = i;
397 unicode[index] = glyphUnicode[i];
398 ++index;
399 }
400 if (index == kMaxEntries) {
401 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
402 index = 0;
403 }
404 }
405
406 if (index) {
407 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
408 }
409}
410
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000411/* Font subset design: It would be nice to be able to subset fonts
412 * (particularly type 3 fonts), but it's a lot of work and not a priority.
413 *
414 * Resources are canonicalized and uniqueified by pointer so there has to be
415 * some additional state indicating which subset of the font is used. It
416 * must be maintained at the page granularity and then combined at the document
417 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
418 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
419 * page/pdf device. c) in the document, retrieve the per font glyph usage
420 * from each page and combine it and ask for a resource with that subset.
421 */
422
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000423SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000424 SkAutoMutexAcquire lock(CanonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000425 int index;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000426 if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
427 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000428#ifdef SK_DEBUG
429 SkASSERT(!fDescendant);
430 } else {
431 SkASSERT(fDescendant);
432#endif
433 }
434 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000435}
436
437void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000438 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000439}
440
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000441SkTypeface* SkPDFFont::typeface() {
442 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000443}
444
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000445SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
446 return fType;
447}
448
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000449bool SkPDFFont::hasGlyph(uint16_t id) {
450 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
451}
452
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000453bool SkPDFFont::multiByteGlyphs() {
454 return fMultiByteGlyphs;
455}
456
vandebo@chromium.org01294102011-02-28 19:52:18 +0000457size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
458 size_t numGlyphs) {
459 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000461 return numGlyphs;
462 }
463
464 for (size_t i = 0; i < numGlyphs; i++) {
465 if (glyphIDs[i] == 0) {
466 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000467 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000468 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
469 return i;
470 }
471 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000472 }
473
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000474 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000475}
476
477// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000478SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
479 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000480 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000481 int index;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000482 if (Find(fontID, glyphID, &index)) {
483 CanonicalFonts()[index].fFont->ref();
484 return CanonicalFonts()[index].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000485 }
486
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000487 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000488 SkPDFDict* fontDescriptor = NULL;
489 if (index >= 0) {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000490 SkPDFFont* relatedFont = CanonicalFonts()[index].fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000491 SkASSERT(relatedFont->fFontInfo.get());
492 fontInfo = relatedFont->fFontInfo;
493 fontDescriptor = relatedFont->fDescriptor.get();
494 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000495 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
496 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
497 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
498 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
499 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
500 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
501 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000502 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000503 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000504
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000505 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000506 fontDescriptor);
507 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000508 index = CanonicalFonts().count();
509 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000510 return font; // Return the reference new SkPDFFont() created.
511}
512
513// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000514SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000515 // This initialization is only thread safe with gcc.
516 static SkTDArray<FontRec> gCanonicalFonts;
517 return gCanonicalFonts;
518}
519
520// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000521SkMutex& SkPDFFont::CanonicalFontsMutex() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000522 // This initialization is only thread safe with gcc.
523 static SkMutex gCanonicalFontsMutex;
524 return gCanonicalFontsMutex;
525}
526
527// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000528bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000529 // TODO(vandebo) optimize this, do only one search?
530 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000531 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000532 if (*index >= 0)
533 return true;
534 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000535 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000536 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000537}
538
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000539SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
540 SkTypeface* typeface,
541 uint16_t glyphID,
542 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000543 SkPDFDict* fontDescriptor)
544 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000545 fTypeface(typeface),
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000546 fType(fontInfo ? fontInfo->fType :
547 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000548#ifdef SK_DEBUG
549 fDescendant(descendantFont),
550#endif
551 fMultiByteGlyphs(false),
552 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000553 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000554 fFontInfo(fontInfo),
555 fDescriptor(fontDescriptor) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000556 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000557 NOT_IMPLEMENTED(true, true);
558 fType = SkAdvancedTypefaceMetrics::kOther_Font;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000559 }
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000560 if (fType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
561 fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000562 if (descendantFont) {
563 populateCIDFont();
564 } else {
565 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000566 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000567 // No need to hold onto the font info for fonts types that
568 // support multibyte glyphs.
569 fFontInfo = NULL;
570 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000571 }
572
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000573 if (fType == SkAdvancedTypefaceMetrics::kType1_Font &&
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000574 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000575 return;
576 }
577
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000578 SkASSERT(fType == SkAdvancedTypefaceMetrics::kType1_Font ||
579 fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
580 fType == SkAdvancedTypefaceMetrics::kOther_Font ||
581 fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000582 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000583}
584
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000585void SkPDFFont::populateType0Font() {
586 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000587
reed@google.comc789cf12011-07-20 12:14:33 +0000588 insertName("Subtype", "Type0");
589 insertName("BaseFont", fFontInfo->fFontName);
590 insertName("Encoding", "Identity-H");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000591
592 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
593 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000594
595 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000596 fResources.push(
597 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000598 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000599 insert("DescendantFonts", descendantFonts.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000600
601 populateToUnicodeTable();
602}
603
604void SkPDFFont::populateToUnicodeTable() {
605 if (fFontInfo.get() == NULL ||
606 fFontInfo->fGlyphToUnicode.begin() == NULL) {
607 return;
608 }
609
610 SkDynamicMemoryWStream cmap;
611 append_tounicode_header(&cmap);
612 append_cmap_bfchar_sections(fFontInfo->fGlyphToUnicode, &cmap);
613 append_cmap_footer(&cmap);
614 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
615 cmapStream->unref(); // SkRefPtr and new took a reference.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000616 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000617 SkRefPtr<SkPDFStream> pdfCmap = new SkPDFStream(cmapStream.get());
618 fResources.push(pdfCmap.get()); // Pass reference from new.
619 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000620}
621
622void SkPDFFont::populateCIDFont() {
623 fMultiByteGlyphs = true;
reed@google.comc789cf12011-07-20 12:14:33 +0000624 insertName("BaseFont", fFontInfo->fFontName);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000625
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000626 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000627 insertName("Subtype", "CIDFontType0");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000628 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000629 insertName("Subtype", "CIDFontType2");
630 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000631 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000632 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000633 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000634
635 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
636 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000637 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
638 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +0000639 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000640 insert("CIDSystemInfo", sysInfo.get());
641
642 addFontDescriptor(0);
643
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000644 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000645 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000646 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000647 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
648 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000649 widths->unref(); // SkRefPtr and compose both took a reference.
650 if (widths->size())
651 insert("W", widths.get());
652 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +0000653 insertScalar("DW", scaleFromFontUnits(defaultWidth,
654 fFontInfo->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000655 }
656 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000657 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000658 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000659 defaultAdvance.fVerticalAdvance = 0;
660 defaultAdvance.fOriginXDisp = 0;
661 defaultAdvance.fOriginYDisp = 0;
662 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000663 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
664 fFontInfo->fEmSize, &appendVerticalAdvance,
665 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000666 advances->unref(); // SkRefPtr and compose both took a ref.
667 if (advances->size())
668 insert("W2", advances.get());
669 if (defaultAdvance.fVerticalAdvance ||
670 defaultAdvance.fOriginXDisp ||
671 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000672 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000673 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000674 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000675 }
676 }
677}
678
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000679bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000680 SkASSERT(!fFontInfo->fVerticalMetrics.get());
681 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000682
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000683 adjustGlyphRangeForSingleByteEncoding(glyphID);
684
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000685 int16_t defaultWidth = 0;
686 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
687 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000688 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
689 widthEntry != NULL;
690 widthEntry = widthEntry->fNext.get()) {
691 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000692 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000693 defaultWidth = widthEntry->fAdvance[0];
694 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000695 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000696 SkASSERT(false);
697 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000698 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000699 SkASSERT(widthRangeEntry == NULL);
700 widthRangeEntry = widthEntry;
701 break;
702 }
703 }
704
705 if (!addFontDescriptor(defaultWidth))
706 return false;
707
reed@google.comc789cf12011-07-20 12:14:33 +0000708 insertName("Subtype", "Type1");
709 insertName("BaseFont", fFontInfo->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000710
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000711 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000712
713 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000714 encoding->unref(); // SkRefPtr and new both took a reference.
715 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000716
717 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
718 encDiffs->unref(); // SkRefPtr and new both took a reference.
719 encoding->insert("Differences", encDiffs.get());
720
721 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
reed@google.comc789cf12011-07-20 12:14:33 +0000722 encDiffs->appendInt(1);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000723 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
reed@google.comc789cf12011-07-20 12:14:33 +0000724 encDiffs->appendName(fFontInfo->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000725 }
726
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000727 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000728 fFontInfo = NULL;
729 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000730}
731
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000732void SkPDFFont::populateType3Font(int16_t glyphID) {
733 SkPaint paint;
734 paint.setTypeface(fTypeface.get());
735 paint.setTextSize(1000);
736 SkAutoGlyphCache autoCache(paint, NULL);
737 SkGlyphCache* cache = autoCache.getCache();
738 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
739 if (fLastGlyphID == 0) {
740 fLastGlyphID = cache->getGlyphCount() - 1;
741 }
742
743 adjustGlyphRangeForSingleByteEncoding(glyphID);
744
reed@google.comc789cf12011-07-20 12:14:33 +0000745 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000746 // Flip about the x-axis and scale by 1/1000.
747 SkMatrix fontMatrix;
748 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
749 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
750
751 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
752 charProcs->unref(); // SkRefPtr and new both took a reference.
753 insert("CharProcs", charProcs.get());
754
755 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
756 encoding->unref(); // SkRefPtr and new both took a reference.
757 insert("Encoding", encoding.get());
758
759 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
760 encDiffs->unref(); // SkRefPtr and new both took a reference.
761 encoding->insert("Differences", encDiffs.get());
762 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
reed@google.comc789cf12011-07-20 12:14:33 +0000763 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000764
765 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
766 widthArray->unref(); // SkRefPtr and new both took a ref.
767
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000768 SkIRect bbox = SkIRect::MakeEmpty();
769 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
770 SkString characterName;
771 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +0000772 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000773
reed@google.comce11b262011-03-21 21:25:35 +0000774 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +0000775 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000776 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
777 glyph.fWidth, glyph.fHeight);
778 bbox.join(glyphBBox);
779
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000780 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000781 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
782 &content);
783 const SkPath* path = cache->findPath(glyph);
784 if (path) {
785 SkPDFUtils::EmitPath(*path, &content);
786 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
787 &content);
788 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000789 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000790 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000791 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000792
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000793 SkRefPtr<SkPDFStream> glyphDescription =
794 new SkPDFStream(glyphStream.get());
795 // SkRefPtr and new both ref()'d charProcs, pass one.
796 fResources.push(glyphDescription.get());
797 charProcs->insert(characterName.c_str(),
798 new SkPDFObjRef(glyphDescription.get()))->unref();
799 }
800
801 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +0000802 insertInt("FirstChar", fFirstGlyphID);
803 insertInt("LastChar", fLastGlyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000804 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000805 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000806
reed@google.comc789cf12011-07-20 12:14:33 +0000807 if (fFontInfo && fFontInfo->fLastGlyphID <= 255) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000808 fFontInfo = NULL;
reed@google.comc789cf12011-07-20 12:14:33 +0000809 }
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000810 populateToUnicodeTable();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000811}
812
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000813bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000814 if (fDescriptor.get() != NULL) {
815 fResources.push(fDescriptor.get());
816 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000817 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000818 return true;
819 }
820
821 fDescriptor = new SkPDFDict("FontDescriptor");
822 fDescriptor->unref(); // SkRefPtr and new both took a ref.
823
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000824 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000825 case SkAdvancedTypefaceMetrics::kType1_Font: {
reed@google.comee5ee582011-04-28 14:12:48 +0000826 size_t header SK_INIT_TO_AVOID_WARNING;
827 size_t data SK_INIT_TO_AVOID_WARNING;
828 size_t trailer SK_INIT_TO_AVOID_WARNING;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000829 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000830 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000831 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
832 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
833 &data, &trailer);
834 if (fontData == NULL)
835 return false;
836 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
837 // SkRefPtr and new both ref()'d fontStream, pass one.
838 fResources.push(fontStream.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000839 fontStream->insertInt("Length1", header);
840 fontStream->insertInt("Length2", data);
841 fontStream->insertInt("Length3", trailer);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000842 fDescriptor->insert("FontFile",
843 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000844 break;
845 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000846 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000847 SkRefPtr<SkStream> fontData =
848 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000849 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
850 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
851 // SkRefPtr and new both ref()'d fontStream, pass one.
852 fResources.push(fontStream.get());
853
reed@google.comc789cf12011-07-20 12:14:33 +0000854 fontStream->insertInt("Length1", fontData->getLength());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000855 fDescriptor->insert("FontFile2",
856 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000857 break;
858 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000859 case SkAdvancedTypefaceMetrics::kCFF_Font:
860 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000861 SkRefPtr<SkStream> fontData =
862 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000863 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
864 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
865 // SkRefPtr and new both ref()'d fontStream, pass one.
866 fResources.push(fontStream.get());
867
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000868 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000869 fontStream->insertName("Subtype", "Type1C");
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000870 } else {
reed@google.comc789cf12011-07-20 12:14:33 +0000871 fontStream->insertName("Subtype", "CIDFontType0c");
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000872 }
873 fDescriptor->insert("FontFile3",
874 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000875 break;
876 }
877 default:
878 SkASSERT(false);
879 }
880
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000881 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000882 fResources.push(fDescriptor.get());
883 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000884 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000885
reed@google.comc789cf12011-07-20 12:14:33 +0000886 fDescriptor->insertName("FontName", fFontInfo->fFontName);
887 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
888 fDescriptor->insertScalar("Ascent",
889 scaleFromFontUnits(fFontInfo->fAscent, emSize));
890 fDescriptor->insertScalar("Descent",
891 scaleFromFontUnits(fFontInfo->fDescent, emSize));
892 fDescriptor->insertScalar("StemV",
893 scaleFromFontUnits(fFontInfo->fStemV, emSize));
894 fDescriptor->insertScalar("CapHeight",
895 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
896 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000897 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
898 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000899
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000900 if (defaultWidth > 0) {
reed@google.comc789cf12011-07-20 12:14:33 +0000901 fDescriptor->insertScalar("MissingWidth",
902 scaleFromFontUnits(defaultWidth, emSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000903 }
904 return true;
905}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000906void SkPDFFont::addWidthInfoFromRange(
907 int16_t defaultWidth,
908 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
909 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
910 widthArray->unref(); // SkRefPtr and new both took a ref.
911 int firstChar = 0;
912 if (widthRangeEntry) {
913 const uint16_t emSize = fFontInfo->fEmSize;
914 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
915 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
916 if (startIndex < 0)
917 startIndex = 0;
918 if (endIndex > widthRangeEntry->fAdvance.count())
919 endIndex = widthRangeEntry->fAdvance.count();
920 if (widthRangeEntry->fStartId == 0) {
921 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
922 } else {
923 firstChar = startIndex + widthRangeEntry->fStartId;
924 }
925 for (int i = startIndex; i < endIndex; i++)
926 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
927 } else {
928 appendWidth(defaultWidth, 1000, widthArray.get());
929 }
reed@google.comc789cf12011-07-20 12:14:33 +0000930 insertInt("FirstChar", firstChar);
931 insertInt("LastChar", firstChar + widthArray->size() - 1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000932 insert("Widths", widthArray.get());
933}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000934
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000935void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
936 // Single byte glyph encoding supports a max of 255 glyphs.
937 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
938 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
939 fLastGlyphID = fFirstGlyphID + 255 - 1;
940 }
941}
942
943
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000944bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000945 if (fFontID != b.fFontID)
946 return false;
947 if (fFont != NULL && b.fFont != NULL) {
948 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
949 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
950 }
951 if (fGlyphID == 0 || b.fGlyphID == 0)
952 return true;
953
954 if (fFont != NULL) {
955 return fFont->fFirstGlyphID <= b.fGlyphID &&
956 b.fGlyphID <= fFont->fLastGlyphID;
957 } else if (b.fFont != NULL) {
958 return b.fFont->fFirstGlyphID <= fGlyphID &&
959 fGlyphID <= b.fFont->fLastGlyphID;
960 }
961 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000962}
963
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000964SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000965 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000966 fFontID(fontID),
967 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000968}