blob: 277ed12ac3c3f788b038f0604bc54409b6bfb963 [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.orgf0ec2662011-05-29 05:55:42 +0000447SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
448 return fType;
449}
450
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000451bool SkPDFFont::hasGlyph(uint16_t id) {
452 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
453}
454
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000455bool SkPDFFont::multiByteGlyphs() {
456 return fMultiByteGlyphs;
457}
458
vandebo@chromium.org01294102011-02-28 19:52:18 +0000459size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
460 size_t numGlyphs) {
461 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000462 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000463 return numGlyphs;
464 }
465
466 for (size_t i = 0; i < numGlyphs; i++) {
467 if (glyphIDs[i] == 0) {
468 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000469 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000470 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
471 return i;
472 }
473 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000474 }
475
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000476 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000477}
478
479// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000480SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000481 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000482 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483 int index;
484 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000485 canonicalFonts()[index].fFont->ref();
486 return canonicalFonts()[index].fFont;
487 }
488
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000489 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000490 SkPDFDict* fontDescriptor = NULL;
491 if (index >= 0) {
492 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
493 SkASSERT(relatedFont->fFontInfo.get());
494 fontInfo = relatedFont->fFontInfo;
495 fontDescriptor = relatedFont->fDescriptor.get();
496 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000497 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
498 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
499 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
500 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
501 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
502 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
503 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000504 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000505 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000506
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000507 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 fontDescriptor);
509 FontRec newEntry(font, fontID, font->fFirstGlyphID);
510 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000511 canonicalFonts().push(newEntry);
512 return font; // Return the reference new SkPDFFont() created.
513}
514
515// static
516SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
517 // This initialization is only thread safe with gcc.
518 static SkTDArray<FontRec> gCanonicalFonts;
519 return gCanonicalFonts;
520}
521
522// static
523SkMutex& SkPDFFont::canonicalFontsMutex() {
524 // This initialization is only thread safe with gcc.
525 static SkMutex gCanonicalFontsMutex;
526 return gCanonicalFontsMutex;
527}
528
529// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000530bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
531 // TODO(vandebo) optimize this, do only one search?
532 FontRec search(NULL, fontID, glyphID);
533 *index = canonicalFonts().find(search);
534 if (*index >= 0)
535 return true;
536 search.fGlyphID = 0;
537 *index = canonicalFonts().find(search);
538 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000539}
540
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000541SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
542 SkTypeface* typeface,
543 uint16_t glyphID,
544 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000545 SkPDFDict* fontDescriptor)
546 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000547 fTypeface(typeface),
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000548 fType(fontInfo ? fontInfo->fType :
549 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000550#ifdef SK_DEBUG
551 fDescendant(descendantFont),
552#endif
553 fMultiByteGlyphs(false),
554 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000555 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000556 fFontInfo(fontInfo),
557 fDescriptor(fontDescriptor) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000558 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000559 NOT_IMPLEMENTED(true, true);
560 fType = SkAdvancedTypefaceMetrics::kOther_Font;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000561 }
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000562 if (fType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
563 fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000564 if (descendantFont) {
565 populateCIDFont();
566 } else {
567 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000568 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000569 // No need to hold onto the font info for fonts types that
570 // support multibyte glyphs.
571 fFontInfo = NULL;
572 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000573 }
574
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000575 if (fType == SkAdvancedTypefaceMetrics::kType1_Font &&
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000576 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000577 return;
578 }
579
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000580 SkASSERT(fType == SkAdvancedTypefaceMetrics::kType1_Font ||
581 fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
582 fType == SkAdvancedTypefaceMetrics::kOther_Font ||
583 fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000584 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000585}
586
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000587void SkPDFFont::populateType0Font() {
588 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000589
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000590 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000591 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000592 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000593
594 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
595 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000596
597 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000598 fResources.push(
599 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000600 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601 insert("DescendantFonts", descendantFonts.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000602
603 populateToUnicodeTable();
604}
605
606void SkPDFFont::populateToUnicodeTable() {
607 if (fFontInfo.get() == NULL ||
608 fFontInfo->fGlyphToUnicode.begin() == NULL) {
609 return;
610 }
611
612 SkDynamicMemoryWStream cmap;
613 append_tounicode_header(&cmap);
614 append_cmap_bfchar_sections(fFontInfo->fGlyphToUnicode, &cmap);
615 append_cmap_footer(&cmap);
616 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
617 cmapStream->unref(); // SkRefPtr and new took a reference.
618 cmapStream->setMemoryOwned(cmap.detach(), cmap.getOffset());
619 SkRefPtr<SkPDFStream> pdfCmap = new SkPDFStream(cmapStream.get());
620 fResources.push(pdfCmap.get()); // Pass reference from new.
621 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000622}
623
624void SkPDFFont::populateCIDFont() {
625 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000626 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000627
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000628 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000629 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000630 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000631 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000632 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000633 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000634 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000635 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000636
637 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
638 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000639 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
640 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
641 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000642 insert("CIDSystemInfo", sysInfo.get());
643
644 addFontDescriptor(0);
645
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000646 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000647 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000648 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000649 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
650 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000651 widths->unref(); // SkRefPtr and compose both took a reference.
652 if (widths->size())
653 insert("W", widths.get());
654 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000655 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000656 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000657 }
658 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000659 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000660 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000661 defaultAdvance.fVerticalAdvance = 0;
662 defaultAdvance.fOriginXDisp = 0;
663 defaultAdvance.fOriginYDisp = 0;
664 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000665 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
666 fFontInfo->fEmSize, &appendVerticalAdvance,
667 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000668 advances->unref(); // SkRefPtr and compose both took a ref.
669 if (advances->size())
670 insert("W2", advances.get());
671 if (defaultAdvance.fVerticalAdvance ||
672 defaultAdvance.fOriginXDisp ||
673 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000674 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000675 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000676 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000677 }
678 }
679}
680
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000681bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000682 SkASSERT(!fFontInfo->fVerticalMetrics.get());
683 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000684
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000685 adjustGlyphRangeForSingleByteEncoding(glyphID);
686
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000687 int16_t defaultWidth = 0;
688 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
689 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000690 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
691 widthEntry != NULL;
692 widthEntry = widthEntry->fNext.get()) {
693 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000694 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000695 defaultWidth = widthEntry->fAdvance[0];
696 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000697 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000698 SkASSERT(false);
699 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000700 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000701 SkASSERT(widthRangeEntry == NULL);
702 widthRangeEntry = widthEntry;
703 break;
704 }
705 }
706
707 if (!addFontDescriptor(defaultWidth))
708 return false;
709
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000710 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000711 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000712
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000713 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000714
715 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000716 encoding->unref(); // SkRefPtr and new both took a reference.
717 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000718
719 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
720 encDiffs->unref(); // SkRefPtr and new both took a reference.
721 encoding->insert("Differences", encDiffs.get());
722
723 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000724 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000725 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000726 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000727 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000728 }
729
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000730 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000731 fFontInfo = NULL;
732 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000733}
734
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000735void SkPDFFont::populateType3Font(int16_t glyphID) {
736 SkPaint paint;
737 paint.setTypeface(fTypeface.get());
738 paint.setTextSize(1000);
739 SkAutoGlyphCache autoCache(paint, NULL);
740 SkGlyphCache* cache = autoCache.getCache();
741 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
742 if (fLastGlyphID == 0) {
743 fLastGlyphID = cache->getGlyphCount() - 1;
744 }
745
746 adjustGlyphRangeForSingleByteEncoding(glyphID);
747
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000748 insert("Subtype", new SkPDFName("Type3"))->unref();
749 // Flip about the x-axis and scale by 1/1000.
750 SkMatrix fontMatrix;
751 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
752 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
753
754 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
755 charProcs->unref(); // SkRefPtr and new both took a reference.
756 insert("CharProcs", charProcs.get());
757
758 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
759 encoding->unref(); // SkRefPtr and new both took a reference.
760 insert("Encoding", encoding.get());
761
762 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
763 encDiffs->unref(); // SkRefPtr and new both took a reference.
764 encoding->insert("Differences", encDiffs.get());
765 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
766 encDiffs->append(new SkPDFInt(1))->unref();
767
768 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
769 widthArray->unref(); // SkRefPtr and new both took a ref.
770
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000771 SkIRect bbox = SkIRect::MakeEmpty();
772 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
773 SkString characterName;
774 characterName.printf("gid%d", gID);
775 encDiffs->append(new SkPDFName(characterName))->unref();
776
reed@google.comce11b262011-03-21 21:25:35 +0000777 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
778 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000779 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
780 glyph.fWidth, glyph.fHeight);
781 bbox.join(glyphBBox);
782
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000783 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
785 &content);
786 const SkPath* path = cache->findPath(glyph);
787 if (path) {
788 SkPDFUtils::EmitPath(*path, &content);
789 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
790 &content);
791 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000792 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000793 glyphStream->unref(); // SkRefPtr and new both took a ref.
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000794 glyphStream->setMemoryOwned(content.detach(), content.getOffset());
795
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000796 SkRefPtr<SkPDFStream> glyphDescription =
797 new SkPDFStream(glyphStream.get());
798 // SkRefPtr and new both ref()'d charProcs, pass one.
799 fResources.push(glyphDescription.get());
800 charProcs->insert(characterName.c_str(),
801 new SkPDFObjRef(glyphDescription.get()))->unref();
802 }
803
804 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
805 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
806 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
807 insert("Widths", widthArray.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000808 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000809
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000810 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000811 fFontInfo = NULL;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000812
813 populateToUnicodeTable();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000814}
815
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000816bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000817 if (fDescriptor.get() != NULL) {
818 fResources.push(fDescriptor.get());
819 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000820 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000821 return true;
822 }
823
824 fDescriptor = new SkPDFDict("FontDescriptor");
825 fDescriptor->unref(); // SkRefPtr and new both took a ref.
826
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000827 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000828 case SkAdvancedTypefaceMetrics::kType1_Font: {
reed@google.comee5ee582011-04-28 14:12:48 +0000829 size_t header SK_INIT_TO_AVOID_WARNING;
830 size_t data SK_INIT_TO_AVOID_WARNING;
831 size_t trailer SK_INIT_TO_AVOID_WARNING;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000832 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000833 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
835 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
836 &data, &trailer);
837 if (fontData == NULL)
838 return false;
839 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
840 // SkRefPtr and new both ref()'d fontStream, pass one.
841 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000842 fontStream->insert("Length1", new SkPDFInt(header))->unref();
843 fontStream->insert("Length2", new SkPDFInt(data))->unref();
844 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
845 fDescriptor->insert("FontFile",
846 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000847 break;
848 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000849 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000850 SkRefPtr<SkStream> fontData =
851 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000852 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
853 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
854 // SkRefPtr and new both ref()'d fontStream, pass one.
855 fResources.push(fontStream.get());
856
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000857 fontStream->insert("Length1",
858 new SkPDFInt(fontData->getLength()))->unref();
859 fDescriptor->insert("FontFile2",
860 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000861 break;
862 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000863 case SkAdvancedTypefaceMetrics::kCFF_Font:
864 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000865 SkRefPtr<SkStream> fontData =
866 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000867 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
868 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
869 // SkRefPtr and new both ref()'d fontStream, pass one.
870 fResources.push(fontStream.get());
871
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000872 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000873 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
874 } else {
875 fontStream->insert("Subtype",
876 new SkPDFName("CIDFontType0c"))->unref();
877 }
878 fDescriptor->insert("FontFile3",
879 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000880 break;
881 }
882 default:
883 SkASSERT(false);
884 }
885
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000886 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000887 fResources.push(fDescriptor.get());
888 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000889 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000890
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000891 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000892 fFontInfo->fFontName))->unref();
893 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000894 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000895 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000896 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000897 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000898 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000899 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000900 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000901 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000902 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000903 fFontInfo->fItalicAngle))->unref();
904 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
905 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000906
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000907 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000908 fDescriptor->insert("MissingWidth", new SkPDFScalar(
909 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000910 }
911 return true;
912}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000913void SkPDFFont::addWidthInfoFromRange(
914 int16_t defaultWidth,
915 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
916 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
917 widthArray->unref(); // SkRefPtr and new both took a ref.
918 int firstChar = 0;
919 if (widthRangeEntry) {
920 const uint16_t emSize = fFontInfo->fEmSize;
921 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
922 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
923 if (startIndex < 0)
924 startIndex = 0;
925 if (endIndex > widthRangeEntry->fAdvance.count())
926 endIndex = widthRangeEntry->fAdvance.count();
927 if (widthRangeEntry->fStartId == 0) {
928 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
929 } else {
930 firstChar = startIndex + widthRangeEntry->fStartId;
931 }
932 for (int i = startIndex; i < endIndex; i++)
933 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
934 } else {
935 appendWidth(defaultWidth, 1000, widthArray.get());
936 }
937 insert("FirstChar", new SkPDFInt(firstChar))->unref();
938 insert("LastChar",
939 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
940 insert("Widths", widthArray.get());
941}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000942
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000943void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
944 // Single byte glyph encoding supports a max of 255 glyphs.
945 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
946 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
947 fLastGlyphID = fFirstGlyphID + 255 - 1;
948 }
949}
950
951
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000952bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000953 if (fFontID != b.fFontID)
954 return false;
955 if (fFont != NULL && b.fFont != NULL) {
956 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
957 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
958 }
959 if (fGlyphID == 0 || b.fGlyphID == 0)
960 return true;
961
962 if (fFont != NULL) {
963 return fFont->fFirstGlyphID <= b.fGlyphID &&
964 b.fGlyphID <= fFont->fLastGlyphID;
965 } else if (b.fFont != NULL) {
966 return b.fFont->fFirstGlyphID <= fGlyphID &&
967 fGlyphID <= b.fFont->fLastGlyphID;
968 }
969 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000970}
971
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000972SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000973 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000974 fFontID(fontID),
975 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000976}