blob: cc249ca9d3794e885e631bd2798e957bff0144f6 [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
19#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000020#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000021#include "SkPaint.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000022#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000023#include "SkPDFFont.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000024#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000026#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000027#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000028#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000029#include "SkStream.h"
30#include "SkTypeface.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000031#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000032#include "SkUtils.h"
33
34namespace {
35
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000036bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
37 size_t* size) {
38 // PFB sections have a two or six bytes header. 0x80 and a one byte
39 // section type followed by a four byte section length. Type one is
40 // an ASCII section (includes a length), type two is a binary section
41 // (includes a length) and type three is an EOF marker with no length.
42 const uint8_t* buf = *src;
43 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
44 return false;
45 if (buf[1] == 3)
46 return true;
47 if (*len < 6)
48 return false;
49
50 *size = buf[2] | (buf[3] << 8) | (buf[4] << 16) | (buf[5] << 24);
51 size_t consumed = *size + 6;
52 if (consumed > *len)
53 return false;
54 *src = *src + consumed;
55 *len = *len - consumed;
56 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000057}
58
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000059bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
60 size_t* dataLen, size_t* trailerLen) {
61 const uint8_t* srcPtr = src;
62 size_t remaining = size;
63
64 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
65 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
66 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
67 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000068}
69
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000070/* The sections of a PFA file are implicitly defined. The body starts
71 * after the line containing "eexec," and the trailer starts with 512
72 * literal 0's followed by "cleartomark" (plus arbitrary white space).
73 *
74 * This function assumes that src is NUL terminated, but the NUL
75 * termination is not included in size.
76 *
77 */
78bool parsePFA(const char* src, size_t size, size_t* headerLen,
79 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
80 const char* end = src + size;
81
82 const char* dataPos = strstr(src, "eexec");
83 if (!dataPos)
84 return false;
85 dataPos += strlen("eexec");
86 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
87 dataPos < end)
88 dataPos++;
89 *headerLen = dataPos - src;
90
91 const char* trailerPos = strstr(dataPos, "cleartomark");
92 if (!trailerPos)
93 return false;
94 int zeroCount = 0;
95 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
96 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
97 continue;
98 } else if (*trailerPos == '0') {
99 zeroCount++;
100 } else {
101 return false;
102 }
103 }
104 if (zeroCount != 512)
105 return false;
106
107 *hexDataLen = trailerPos - src - *headerLen;
108 *trailerLen = size - *headerLen - *hexDataLen;
109
110 // Verify that the data section is hex encoded and count the bytes.
111 int nibbles = 0;
112 for (; dataPos < trailerPos; dataPos++) {
113 if (isspace(*dataPos))
114 continue;
115 if (!isxdigit(*dataPos))
116 return false;
117 nibbles++;
118 }
119 *dataLen = (nibbles + 1) / 2;
120
121 return true;
122}
123
124int8_t hexToBin(uint8_t c) {
125 if (!isxdigit(c))
126 return -1;
127 if (c <= '9') return c - '0';
128 if (c <= 'F') return c - 'A' + 10;
129 if (c <= 'f') return c - 'a' + 10;
130 return -1;
131}
132
133SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
134 size_t* dataLen, size_t* trailerLen) {
135 // srcStream may be backed by a file or a unseekable fd, so we may not be
136 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
137 // the input only once is doable, but very ugly. Furthermore, it'd be nice
138 // if the data was NUL terminated so that we can use strstr() to search it.
139 // Make as few copies as possible given these constraints.
140 SkDynamicMemoryWStream dynamicStream;
141 SkRefPtr<SkMemoryStream> staticStream;
142 const uint8_t* src;
143 size_t srcLen;
144 if ((srcLen = srcStream->getLength()) > 0) {
145 staticStream = new SkMemoryStream(srcLen + 1);
146 staticStream->unref(); // new and SkRefPtr both took a ref.
147 src = (const uint8_t*)staticStream->getMemoryBase();
148 if (srcStream->getMemoryBase() != NULL) {
149 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
150 } else {
151 size_t read = 0;
152 while (read < srcLen) {
153 size_t got = srcStream->read((void *)staticStream->getAtPos(),
154 srcLen - read);
155 if (got == 0)
156 return NULL;
157 read += got;
158 staticStream->seek(read);
159 }
160 }
161 ((uint8_t *)src)[srcLen] = 0;
162 } else {
163 static const size_t bufSize = 4096;
164 uint8_t buf[bufSize];
165 size_t amount;
166 while ((amount = srcStream->read(buf, bufSize)) > 0)
167 dynamicStream.write(buf, amount);
168 amount = 0;
169 dynamicStream.write(&amount, 1); // NULL terminator.
170 // getStream makes another copy, but we couldn't do any better.
171 src = (const uint8_t*)dynamicStream.getStream();
172 srcLen = dynamicStream.getOffset() - 1;
173 }
174
175 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
176 SkMemoryStream* result =
177 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
178 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
179 result->seek(*headerLen);
180 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
181 result->seek(*headerLen + *dataLen);
182 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
183 *trailerLen);
184 result->rewind();
185 return result;
186 }
187
188 // A PFA has to be converted for PDF.
189 size_t hexDataLen;
190 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
191 trailerLen)) {
192 SkMemoryStream* result =
193 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
194 memcpy((char*)result->getAtPos(), src, *headerLen);
195 result->seek(*headerLen);
196
197 const uint8_t* hexData = src + *headerLen;
198 const uint8_t* trailer = hexData + hexDataLen;
199 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000200 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000201 bool highNibble = true;
202 for (; hexData < trailer; hexData++) {
203 char curNibble = hexToBin(*hexData);
204 if (curNibble < 0)
205 continue;
206 if (highNibble) {
207 dataByte = curNibble << 4;
208 highNibble = false;
209 } else {
210 dataByte |= curNibble;
211 highNibble = true;
212 ((char *)result->getAtPos())[outputOffset++] = dataByte;
213 }
214 }
215 if (!highNibble)
216 ((char *)result->getAtPos())[outputOffset++] = dataByte;
217 SkASSERT(outputOffset == *dataLen);
218 result->seek(*headerLen + outputOffset);
219
220 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
221 *trailerLen);
222 result->rewind();
223 return result;
224 }
225
226 return NULL;
227}
228
reed@google.com3f0dcf92011-03-18 21:23:45 +0000229// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000230SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000231 SkScalar scaled = SkIntToScalar(val);
232 if (emSize == 1000) {
233 return scaled;
234 } else {
235 return SkScalarMulDiv(scaled, 1000, emSize);
236 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000237}
238
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000239void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000240 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000241 // Specify width and bounding box for the glyph.
242 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000243 content->writeText(" 0 ");
244 content->writeDecAsText(box.fLeft);
245 content->writeText(" ");
246 content->writeDecAsText(box.fTop);
247 content->writeText(" ");
248 content->writeDecAsText(box.fRight);
249 content->writeText(" ");
250 content->writeDecAsText(box.fBottom);
251 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000252}
253
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000254SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000255 SkPDFArray* bbox = new SkPDFArray;
256 bbox->reserve(4);
257 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
258 emSize)))->unref();
259 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
260 emSize)))->unref();
261 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
262 emSize)))->unref();
263 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
264 emSize)))->unref();
265 return bbox;
266}
267
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000268SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
269 SkPDFArray* array) {
270 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000271 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000272}
273
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000274SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000275 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
276 uint16_t emSize, SkPDFArray* array) {
277 appendWidth(advance.fVerticalAdvance, emSize, array);
278 appendWidth(advance.fOriginXDisp, emSize, array);
279 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000280 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000281}
282
283template <typename Data>
284SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000285 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
286 uint16_t emSize,
287 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
288 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000289 Data* defaultAdvance) {
290 SkPDFArray* result = new SkPDFArray();
291 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
292 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000293 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000294 SkASSERT(advanceInfo->fAdvance.count() == 1);
295 *defaultAdvance = advanceInfo->fAdvance[0];
296 break;
297 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000298 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000299 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
300 advanceArray->unref(); // SkRefPtr and new both took a ref.
301 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000302 appendAdvance(advanceInfo->fAdvance[j], emSize,
303 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000304 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000305 result->append(advanceArray.get());
306 break;
307 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000308 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000309 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000310 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
311 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000312 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000313 break;
314 }
315 }
316 }
317 return result;
318}
319
320} // namespace
321
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000322static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
323 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
324 // It's there to prevent old version Adobe Readers from malfunctioning.
325 const char* kHeader =
326 "/CIDInit /ProcSet findresource begin\n"
327 "12 dict begin\n"
328 "begincmap\n";
329 cmap->writeText(kHeader);
330
331 // The /CIDSystemInfo must be consistent to the one in
332 // SkPDFFont::populateCIDFont().
333 // We can not pass over the system info object here because the format is
334 // different. This is not a reference object.
335 const char* kSysInfo =
336 "/CIDSystemInfo\n"
337 "<< /Registry (Adobe)\n"
338 "/Ordering (UCS)\n"
339 "/Supplement 0\n"
340 ">> def\n";
341 cmap->writeText(kSysInfo);
342
343 // The CMapName must be consistent to /CIDSystemInfo above.
344 // /CMapType 2 means ToUnicode.
345 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
346 // code table from unsigned short (16-bits). Codespace range just tells the
347 // PDF processor the valid range. It does not matter whether a complete
348 // mapping is provided or not.
349 const char* kTypeInfo =
350 "/CMapName /Adobe-Identity-UCS def\n"
351 "/CMapType 2 def\n"
352 "1 begincodespacerange\n"
353 "<0000> <FFFF>\n"
354 "endcodespacerange\n";
355 cmap->writeText(kTypeInfo);
356}
357
358static void append_cmap_bfchar_table(uint16_t* glyph_id, SkUnichar* unicode,
359 size_t count,
360 SkDynamicMemoryWStream* cmap) {
361 cmap->writeDecAsText(count);
362 cmap->writeText(" beginbfchar\n");
363 for (size_t i = 0; i < count; ++i) {
364 cmap->writeText("<");
365 cmap->writeHexAsText(glyph_id[i], 4);
366 cmap->writeText("> <");
367 cmap->writeHexAsText(unicode[i], 4);
368 cmap->writeText(">\n");
369 }
370 cmap->writeText("endbfchar\n");
371}
372
373static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
374 const char* kFooter =
375 "endcmap\n"
376 "CMapName currentdict /CMap defineresource pop\n"
377 "end\n"
378 "end";
379 cmap->writeText(kFooter);
380}
381
382// Generate <bfchar> table according to PDF spec 1.4 and Adobe Technote 5014.
383static void append_cmap_bfchar_sections(
384 const SkTDArray<SkUnichar>& glyphUnicode,
385 SkDynamicMemoryWStream* cmap) {
386 // PDF spec defines that every bf* list can have at most 100 entries.
387 const size_t kMaxEntries = 100;
388 uint16_t glyphId[kMaxEntries];
389 SkUnichar unicode[kMaxEntries];
390 size_t index = 0;
391 for (int i = 0; i < glyphUnicode.count(); i++) {
392 if (glyphUnicode[i]) {
393 glyphId[index] = i;
394 unicode[index] = glyphUnicode[i];
395 ++index;
396 }
397 if (index == kMaxEntries) {
398 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
399 index = 0;
400 }
401 }
402
403 if (index) {
404 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
405 }
406}
407
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000408/* Font subset design: It would be nice to be able to subset fonts
409 * (particularly type 3 fonts), but it's a lot of work and not a priority.
410 *
411 * Resources are canonicalized and uniqueified by pointer so there has to be
412 * some additional state indicating which subset of the font is used. It
413 * must be maintained at the page granularity and then combined at the document
414 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
415 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
416 * page/pdf device. c) in the document, retrieve the per font glyph usage
417 * from each page and combine it and ask for a resource with that subset.
418 */
419
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000420SkPDFFont::~SkPDFFont() {
421 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000422 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000423 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000424 canonicalFonts().removeShuffle(index);
425#ifdef SK_DEBUG
426 SkASSERT(!fDescendant);
427 } else {
428 SkASSERT(fDescendant);
429#endif
430 }
431 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000432}
433
434void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
435 resourceList->setReserve(resourceList->count() + fResources.count());
436 for (int i = 0; i < fResources.count(); i++) {
437 resourceList->push(fResources[i]);
438 fResources[i]->ref();
439 fResources[i]->getResources(resourceList);
440 }
441}
442
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000443SkTypeface* SkPDFFont::typeface() {
444 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000445}
446
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000447bool SkPDFFont::hasGlyph(uint16_t id) {
448 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
449}
450
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000451bool SkPDFFont::multiByteGlyphs() {
452 return fMultiByteGlyphs;
453}
454
vandebo@chromium.org01294102011-02-28 19:52:18 +0000455size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
456 size_t numGlyphs) {
457 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000459 return numGlyphs;
460 }
461
462 for (size_t i = 0; i < numGlyphs; i++) {
463 if (glyphIDs[i] == 0) {
464 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000465 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000466 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
467 return i;
468 }
469 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000470 }
471
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000472 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000473}
474
475// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000476SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000477 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000478 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000479 int index;
480 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000481 canonicalFonts()[index].fFont->ref();
482 return canonicalFonts()[index].fFont;
483 }
484
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000485 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000486 SkPDFDict* fontDescriptor = NULL;
487 if (index >= 0) {
488 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
489 SkASSERT(relatedFont->fFontInfo.get());
490 fontInfo = relatedFont->fFontInfo;
491 fontDescriptor = relatedFont->fDescriptor.get();
492 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000493 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
494 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
495 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
496 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
497 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
498 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
499 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000500 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000502
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000503 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000504 fontDescriptor);
505 FontRec newEntry(font, fontID, font->fFirstGlyphID);
506 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000507 canonicalFonts().push(newEntry);
508 return font; // Return the reference new SkPDFFont() created.
509}
510
511// static
512SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
513 // This initialization is only thread safe with gcc.
514 static SkTDArray<FontRec> gCanonicalFonts;
515 return gCanonicalFonts;
516}
517
518// static
519SkMutex& SkPDFFont::canonicalFontsMutex() {
520 // This initialization is only thread safe with gcc.
521 static SkMutex gCanonicalFontsMutex;
522 return gCanonicalFontsMutex;
523}
524
525// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000526bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
527 // TODO(vandebo) optimize this, do only one search?
528 FontRec search(NULL, fontID, glyphID);
529 *index = canonicalFonts().find(search);
530 if (*index >= 0)
531 return true;
532 search.fGlyphID = 0;
533 *index = canonicalFonts().find(search);
534 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000535}
536
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000537SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
538 SkTypeface* typeface,
539 uint16_t glyphID,
540 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000541 SkPDFDict* fontDescriptor)
542 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000543 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000544#ifdef SK_DEBUG
545 fDescendant(descendantFont),
546#endif
547 fMultiByteGlyphs(false),
548 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000549 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000550 fFontInfo(fontInfo),
551 fDescriptor(fontDescriptor) {
552
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000553 SkAdvancedTypefaceMetrics::FontType type;
554 if (fontInfo) {
555 type = fontInfo->fType;
556 } else {
557 type = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
558 }
559
560 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000561 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000562 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
563 }
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000564 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
565 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000566 if (descendantFont) {
567 populateCIDFont();
568 } else {
569 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000570 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000571 // No need to hold onto the font info for fonts types that
572 // support multibyte glyphs.
573 fFontInfo = NULL;
574 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000575 }
576
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000577 if (type == SkAdvancedTypefaceMetrics::kType1_Font &&
578 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000579 return;
580 }
581
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000582 SkASSERT(type == SkAdvancedTypefaceMetrics::kType1_Font ||
583 type == SkAdvancedTypefaceMetrics::kCFF_Font ||
584 type == SkAdvancedTypefaceMetrics::kOther_Font ||
585 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
586 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000587}
588
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000589void SkPDFFont::populateType0Font() {
590 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000591
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000592 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000593 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000594 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000595
596 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
597 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000598
599 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000600 fResources.push(
601 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000602 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000603 insert("DescendantFonts", descendantFonts.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000604
605 populateToUnicodeTable();
606}
607
608void SkPDFFont::populateToUnicodeTable() {
609 if (fFontInfo.get() == NULL ||
610 fFontInfo->fGlyphToUnicode.begin() == NULL) {
611 return;
612 }
613
614 SkDynamicMemoryWStream cmap;
615 append_tounicode_header(&cmap);
616 append_cmap_bfchar_sections(fFontInfo->fGlyphToUnicode, &cmap);
617 append_cmap_footer(&cmap);
618 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
619 cmapStream->unref(); // SkRefPtr and new took a reference.
620 cmapStream->setMemoryOwned(cmap.detach(), cmap.getOffset());
621 SkRefPtr<SkPDFStream> pdfCmap = new SkPDFStream(cmapStream.get());
622 fResources.push(pdfCmap.get()); // Pass reference from new.
623 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000624}
625
626void SkPDFFont::populateCIDFont() {
627 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000628 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000629
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000630 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000631 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000632 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000633 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000634 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000635 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000636 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000637 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000638
639 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
640 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000641 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
642 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
643 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000644 insert("CIDSystemInfo", sysInfo.get());
645
646 addFontDescriptor(0);
647
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000648 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000649 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000650 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000651 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
652 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000653 widths->unref(); // SkRefPtr and compose both took a reference.
654 if (widths->size())
655 insert("W", widths.get());
656 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000657 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000658 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000659 }
660 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000661 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000662 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000663 defaultAdvance.fVerticalAdvance = 0;
664 defaultAdvance.fOriginXDisp = 0;
665 defaultAdvance.fOriginYDisp = 0;
666 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000667 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
668 fFontInfo->fEmSize, &appendVerticalAdvance,
669 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000670 advances->unref(); // SkRefPtr and compose both took a ref.
671 if (advances->size())
672 insert("W2", advances.get());
673 if (defaultAdvance.fVerticalAdvance ||
674 defaultAdvance.fOriginXDisp ||
675 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000676 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000677 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000678 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000679 }
680 }
681}
682
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000683bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000684 SkASSERT(!fFontInfo->fVerticalMetrics.get());
685 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000686
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000687 adjustGlyphRangeForSingleByteEncoding(glyphID);
688
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000689 int16_t defaultWidth = 0;
690 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
691 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000692 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
693 widthEntry != NULL;
694 widthEntry = widthEntry->fNext.get()) {
695 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000696 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000697 defaultWidth = widthEntry->fAdvance[0];
698 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000699 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000700 SkASSERT(false);
701 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000702 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000703 SkASSERT(widthRangeEntry == NULL);
704 widthRangeEntry = widthEntry;
705 break;
706 }
707 }
708
709 if (!addFontDescriptor(defaultWidth))
710 return false;
711
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000712 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000713 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000714
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000715 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000716
717 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000718 encoding->unref(); // SkRefPtr and new both took a reference.
719 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000720
721 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
722 encDiffs->unref(); // SkRefPtr and new both took a reference.
723 encoding->insert("Differences", encDiffs.get());
724
725 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000726 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000727 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000728 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000729 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000730 }
731
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000732 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000733 fFontInfo = NULL;
734 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000735}
736
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000737void SkPDFFont::populateType3Font(int16_t glyphID) {
738 SkPaint paint;
739 paint.setTypeface(fTypeface.get());
740 paint.setTextSize(1000);
741 SkAutoGlyphCache autoCache(paint, NULL);
742 SkGlyphCache* cache = autoCache.getCache();
743 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
744 if (fLastGlyphID == 0) {
745 fLastGlyphID = cache->getGlyphCount() - 1;
746 }
747
748 adjustGlyphRangeForSingleByteEncoding(glyphID);
749
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000750 insert("Subtype", new SkPDFName("Type3"))->unref();
751 // Flip about the x-axis and scale by 1/1000.
752 SkMatrix fontMatrix;
753 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
754 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
755
756 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
757 charProcs->unref(); // SkRefPtr and new both took a reference.
758 insert("CharProcs", charProcs.get());
759
760 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
761 encoding->unref(); // SkRefPtr and new both took a reference.
762 insert("Encoding", encoding.get());
763
764 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
765 encDiffs->unref(); // SkRefPtr and new both took a reference.
766 encoding->insert("Differences", encDiffs.get());
767 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
768 encDiffs->append(new SkPDFInt(1))->unref();
769
770 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
771 widthArray->unref(); // SkRefPtr and new both took a ref.
772
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000773 SkIRect bbox = SkIRect::MakeEmpty();
774 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
775 SkString characterName;
776 characterName.printf("gid%d", gID);
777 encDiffs->append(new SkPDFName(characterName))->unref();
778
reed@google.comce11b262011-03-21 21:25:35 +0000779 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
780 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000781 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
782 glyph.fWidth, glyph.fHeight);
783 bbox.join(glyphBBox);
784
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000785 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000786 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
787 &content);
788 const SkPath* path = cache->findPath(glyph);
789 if (path) {
790 SkPDFUtils::EmitPath(*path, &content);
791 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
792 &content);
793 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000794 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000795 glyphStream->unref(); // SkRefPtr and new both took a ref.
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000796 glyphStream->setMemoryOwned(content.detach(), content.getOffset());
797
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000798 SkRefPtr<SkPDFStream> glyphDescription =
799 new SkPDFStream(glyphStream.get());
800 // SkRefPtr and new both ref()'d charProcs, pass one.
801 fResources.push(glyphDescription.get());
802 charProcs->insert(characterName.c_str(),
803 new SkPDFObjRef(glyphDescription.get()))->unref();
804 }
805
806 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
807 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
808 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
809 insert("Widths", widthArray.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000810 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000811
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000812 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000813 fFontInfo = NULL;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000814
815 populateToUnicodeTable();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000816}
817
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000818bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000819 if (fDescriptor.get() != NULL) {
820 fResources.push(fDescriptor.get());
821 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000822 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000823 return true;
824 }
825
826 fDescriptor = new SkPDFDict("FontDescriptor");
827 fDescriptor->unref(); // SkRefPtr and new both took a ref.
828
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000829 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000830 case SkAdvancedTypefaceMetrics::kType1_Font: {
reed@google.comee5ee582011-04-28 14:12:48 +0000831 size_t header SK_INIT_TO_AVOID_WARNING;
832 size_t data SK_INIT_TO_AVOID_WARNING;
833 size_t trailer SK_INIT_TO_AVOID_WARNING;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000835 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000836 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
837 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
838 &data, &trailer);
839 if (fontData == NULL)
840 return false;
841 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
842 // SkRefPtr and new both ref()'d fontStream, pass one.
843 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000844 fontStream->insert("Length1", new SkPDFInt(header))->unref();
845 fontStream->insert("Length2", new SkPDFInt(data))->unref();
846 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
847 fDescriptor->insert("FontFile",
848 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000849 break;
850 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000851 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000852 SkRefPtr<SkStream> fontData =
853 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000854 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
855 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
856 // SkRefPtr and new both ref()'d fontStream, pass one.
857 fResources.push(fontStream.get());
858
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000859 fontStream->insert("Length1",
860 new SkPDFInt(fontData->getLength()))->unref();
861 fDescriptor->insert("FontFile2",
862 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000863 break;
864 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000865 case SkAdvancedTypefaceMetrics::kCFF_Font:
866 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000867 SkRefPtr<SkStream> fontData =
868 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000869 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
870 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
871 // SkRefPtr and new both ref()'d fontStream, pass one.
872 fResources.push(fontStream.get());
873
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000874 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000875 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
876 } else {
877 fontStream->insert("Subtype",
878 new SkPDFName("CIDFontType0c"))->unref();
879 }
880 fDescriptor->insert("FontFile3",
881 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000882 break;
883 }
884 default:
885 SkASSERT(false);
886 }
887
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000888 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000889 fResources.push(fDescriptor.get());
890 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000891 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000892
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000893 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000894 fFontInfo->fFontName))->unref();
895 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000896 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000897 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000898 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000899 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000900 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000901 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000902 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000903 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000904 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000905 fFontInfo->fItalicAngle))->unref();
906 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
907 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000908
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000909 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000910 fDescriptor->insert("MissingWidth", new SkPDFScalar(
911 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000912 }
913 return true;
914}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000915void SkPDFFont::addWidthInfoFromRange(
916 int16_t defaultWidth,
917 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
918 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
919 widthArray->unref(); // SkRefPtr and new both took a ref.
920 int firstChar = 0;
921 if (widthRangeEntry) {
922 const uint16_t emSize = fFontInfo->fEmSize;
923 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
924 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
925 if (startIndex < 0)
926 startIndex = 0;
927 if (endIndex > widthRangeEntry->fAdvance.count())
928 endIndex = widthRangeEntry->fAdvance.count();
929 if (widthRangeEntry->fStartId == 0) {
930 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
931 } else {
932 firstChar = startIndex + widthRangeEntry->fStartId;
933 }
934 for (int i = startIndex; i < endIndex; i++)
935 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
936 } else {
937 appendWidth(defaultWidth, 1000, widthArray.get());
938 }
939 insert("FirstChar", new SkPDFInt(firstChar))->unref();
940 insert("LastChar",
941 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
942 insert("Widths", widthArray.get());
943}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000944
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000945void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
946 // Single byte glyph encoding supports a max of 255 glyphs.
947 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
948 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
949 fLastGlyphID = fFirstGlyphID + 255 - 1;
950 }
951}
952
953
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000954bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000955 if (fFontID != b.fFontID)
956 return false;
957 if (fFont != NULL && b.fFont != NULL) {
958 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
959 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
960 }
961 if (fGlyphID == 0 || b.fGlyphID == 0)
962 return true;
963
964 if (fFont != NULL) {
965 return fFont->fFirstGlyphID <= b.fGlyphID &&
966 b.fGlyphID <= fFont->fLastGlyphID;
967 } else if (b.fFont != NULL) {
968 return b.fFont->fFirstGlyphID <= fGlyphID &&
969 fGlyphID <= b.fFont->fLastGlyphID;
970 }
971 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000972}
973
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000974SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000975 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000976 fFontID(fontID),
977 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000978}