blob: 7fe8b5cb92de9d4e1e2ed6bd383857075e86a009 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000010#include <ctype.h>
11
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000013#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000014#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkPaint.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000016#include "SkPDFCatalog.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000017#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000018#include "SkPDFFont.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000019#include "SkPDFFontImpl.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000021#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000022#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000023#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000024#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkStream.h"
26#include "SkTypeface.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000027#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000028#include "SkUtils.h"
29
vandebo@chromium.org1f165892011-07-26 02:11:41 +000030#if defined (SK_SFNTLY_SUBSETTER)
31#include SK_SFNTLY_SUBSETTER
32#endif
33
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000034namespace {
35
vandebo@chromium.org98594282011-07-25 22:34:12 +000036///////////////////////////////////////////////////////////////////////////////
37// File-Local Functions
38///////////////////////////////////////////////////////////////////////////////
39
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000040bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
41 size_t* size) {
42 // PFB sections have a two or six bytes header. 0x80 and a one byte
43 // section type followed by a four byte section length. Type one is
44 // an ASCII section (includes a length), type two is a binary section
45 // (includes a length) and type three is an EOF marker with no length.
46 const uint8_t* buf = *src;
47 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
48 return false;
49 if (buf[1] == 3)
50 return true;
51 if (*len < 6)
52 return false;
53
vandebo@chromium.org73322072011-06-21 21:19:41 +000054 *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
55 ((size_t)buf[5] << 24);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000056 size_t consumed = *size + 6;
57 if (consumed > *len)
58 return false;
59 *src = *src + consumed;
60 *len = *len - consumed;
61 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000062}
63
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000064bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
65 size_t* dataLen, size_t* trailerLen) {
66 const uint8_t* srcPtr = src;
67 size_t remaining = size;
68
69 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
70 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
71 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
72 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000073}
74
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000075/* The sections of a PFA file are implicitly defined. The body starts
76 * after the line containing "eexec," and the trailer starts with 512
77 * literal 0's followed by "cleartomark" (plus arbitrary white space).
78 *
79 * This function assumes that src is NUL terminated, but the NUL
80 * termination is not included in size.
81 *
82 */
83bool parsePFA(const char* src, size_t size, size_t* headerLen,
84 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
85 const char* end = src + size;
86
87 const char* dataPos = strstr(src, "eexec");
88 if (!dataPos)
89 return false;
90 dataPos += strlen("eexec");
91 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
92 dataPos < end)
93 dataPos++;
94 *headerLen = dataPos - src;
95
96 const char* trailerPos = strstr(dataPos, "cleartomark");
97 if (!trailerPos)
98 return false;
99 int zeroCount = 0;
100 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
101 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
102 continue;
103 } else if (*trailerPos == '0') {
104 zeroCount++;
105 } else {
106 return false;
107 }
108 }
109 if (zeroCount != 512)
110 return false;
111
112 *hexDataLen = trailerPos - src - *headerLen;
113 *trailerLen = size - *headerLen - *hexDataLen;
114
115 // Verify that the data section is hex encoded and count the bytes.
116 int nibbles = 0;
117 for (; dataPos < trailerPos; dataPos++) {
118 if (isspace(*dataPos))
119 continue;
120 if (!isxdigit(*dataPos))
121 return false;
122 nibbles++;
123 }
124 *dataLen = (nibbles + 1) / 2;
125
126 return true;
127}
128
129int8_t hexToBin(uint8_t c) {
130 if (!isxdigit(c))
131 return -1;
132 if (c <= '9') return c - '0';
133 if (c <= 'F') return c - 'A' + 10;
134 if (c <= 'f') return c - 'a' + 10;
135 return -1;
136}
137
138SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
139 size_t* dataLen, size_t* trailerLen) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000140 // srcStream may be backed by a file or a unseekable fd, so we may not be
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000141 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
142 // the input only once is doable, but very ugly. Furthermore, it'd be nice
143 // if the data was NUL terminated so that we can use strstr() to search it.
144 // Make as few copies as possible given these constraints.
145 SkDynamicMemoryWStream dynamicStream;
146 SkRefPtr<SkMemoryStream> staticStream;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000147 SkData* data = NULL;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000148 const uint8_t* src;
149 size_t srcLen;
150 if ((srcLen = srcStream->getLength()) > 0) {
151 staticStream = new SkMemoryStream(srcLen + 1);
152 staticStream->unref(); // new and SkRefPtr both took a ref.
153 src = (const uint8_t*)staticStream->getMemoryBase();
154 if (srcStream->getMemoryBase() != NULL) {
155 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
156 } else {
157 size_t read = 0;
158 while (read < srcLen) {
159 size_t got = srcStream->read((void *)staticStream->getAtPos(),
160 srcLen - read);
161 if (got == 0)
162 return NULL;
163 read += got;
164 staticStream->seek(read);
165 }
166 }
167 ((uint8_t *)src)[srcLen] = 0;
168 } else {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000169 static const size_t kBufSize = 4096;
170 uint8_t buf[kBufSize];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000171 size_t amount;
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000172 while ((amount = srcStream->read(buf, kBufSize)) > 0)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000173 dynamicStream.write(buf, amount);
174 amount = 0;
175 dynamicStream.write(&amount, 1); // NULL terminator.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000176 data = dynamicStream.copyToData();
177 src = data->bytes();
178 srcLen = data->size() - 1;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000179 }
180
reed@google.com8a85d0c2011-06-24 19:12:12 +0000181 // this handles releasing the data we may have gotten from dynamicStream.
182 // if data is null, it is a no-op
183 SkAutoDataUnref aud(data);
184
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000185 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
186 SkMemoryStream* result =
187 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
188 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
189 result->seek(*headerLen);
190 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
191 result->seek(*headerLen + *dataLen);
192 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
193 *trailerLen);
194 result->rewind();
195 return result;
196 }
197
198 // A PFA has to be converted for PDF.
199 size_t hexDataLen;
200 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
201 trailerLen)) {
202 SkMemoryStream* result =
203 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
204 memcpy((char*)result->getAtPos(), src, *headerLen);
205 result->seek(*headerLen);
206
207 const uint8_t* hexData = src + *headerLen;
208 const uint8_t* trailer = hexData + hexDataLen;
209 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000210 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000211 bool highNibble = true;
212 for (; hexData < trailer; hexData++) {
213 char curNibble = hexToBin(*hexData);
214 if (curNibble < 0)
215 continue;
216 if (highNibble) {
217 dataByte = curNibble << 4;
218 highNibble = false;
219 } else {
220 dataByte |= curNibble;
221 highNibble = true;
222 ((char *)result->getAtPos())[outputOffset++] = dataByte;
223 }
224 }
225 if (!highNibble)
226 ((char *)result->getAtPos())[outputOffset++] = dataByte;
227 SkASSERT(outputOffset == *dataLen);
228 result->seek(*headerLen + outputOffset);
229
230 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
231 *trailerLen);
232 result->rewind();
233 return result;
234 }
235
236 return NULL;
237}
238
reed@google.com3f0dcf92011-03-18 21:23:45 +0000239// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000240SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000241 SkScalar scaled = SkIntToScalar(val);
242 if (emSize == 1000) {
243 return scaled;
244 } else {
245 return SkScalarMulDiv(scaled, 1000, emSize);
246 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000247}
248
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000249void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000250 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000251 // Specify width and bounding box for the glyph.
252 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000253 content->writeText(" 0 ");
254 content->writeDecAsText(box.fLeft);
255 content->writeText(" ");
256 content->writeDecAsText(box.fTop);
257 content->writeText(" ");
258 content->writeDecAsText(box.fRight);
259 content->writeText(" ");
260 content->writeDecAsText(box.fBottom);
261 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000262}
263
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000264SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000265 SkPDFArray* bbox = new SkPDFArray;
266 bbox->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000267 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
268 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
269 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
270 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000271 return bbox;
272}
273
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000274SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
275 SkPDFArray* array) {
reed@google.comc789cf12011-07-20 12:14:33 +0000276 array->appendScalar(scaleFromFontUnits(width, emSize));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000277 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000278}
279
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000280SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000281 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
282 uint16_t emSize, SkPDFArray* array) {
283 appendWidth(advance.fVerticalAdvance, emSize, array);
284 appendWidth(advance.fOriginXDisp, emSize, array);
285 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000286 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000287}
288
289template <typename Data>
290SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000291 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
292 uint16_t emSize,
293 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
294 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000295 Data* defaultAdvance) {
296 SkPDFArray* result = new SkPDFArray();
297 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
298 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000299 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000300 SkASSERT(advanceInfo->fAdvance.count() == 1);
301 *defaultAdvance = advanceInfo->fAdvance[0];
302 break;
303 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000304 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000305 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
306 advanceArray->unref(); // SkRefPtr and new both took a ref.
307 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000308 appendAdvance(advanceInfo->fAdvance[j], emSize,
309 advanceArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000310 result->appendInt(advanceInfo->fStartId);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000311 result->append(advanceArray.get());
312 break;
313 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000314 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000315 SkASSERT(advanceInfo->fAdvance.count() == 1);
reed@google.comc789cf12011-07-20 12:14:33 +0000316 result->appendInt(advanceInfo->fStartId);
317 result->appendInt(advanceInfo->fEndId);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000318 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000319 break;
320 }
321 }
322 }
323 return result;
324}
325
326} // namespace
327
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000328static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
329 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
330 // It's there to prevent old version Adobe Readers from malfunctioning.
331 const char* kHeader =
332 "/CIDInit /ProcSet findresource begin\n"
333 "12 dict begin\n"
334 "begincmap\n";
335 cmap->writeText(kHeader);
336
337 // The /CIDSystemInfo must be consistent to the one in
338 // SkPDFFont::populateCIDFont().
339 // We can not pass over the system info object here because the format is
340 // different. This is not a reference object.
341 const char* kSysInfo =
342 "/CIDSystemInfo\n"
343 "<< /Registry (Adobe)\n"
344 "/Ordering (UCS)\n"
345 "/Supplement 0\n"
346 ">> def\n";
347 cmap->writeText(kSysInfo);
348
349 // The CMapName must be consistent to /CIDSystemInfo above.
350 // /CMapType 2 means ToUnicode.
351 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
352 // code table from unsigned short (16-bits). Codespace range just tells the
353 // PDF processor the valid range. It does not matter whether a complete
354 // mapping is provided or not.
355 const char* kTypeInfo =
356 "/CMapName /Adobe-Identity-UCS def\n"
357 "/CMapType 2 def\n"
358 "1 begincodespacerange\n"
359 "<0000> <FFFF>\n"
360 "endcodespacerange\n";
361 cmap->writeText(kTypeInfo);
362}
363
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000364static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
365 const char* kFooter =
366 "endcmap\n"
367 "CMapName currentdict /CMap defineresource pop\n"
368 "end\n"
369 "end";
370 cmap->writeText(kFooter);
371}
372
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000373struct BFChar {
374 uint16_t fGlyphId;
375 SkUnichar fUnicode;
376};
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000377
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000378struct BFRange {
379 uint16_t fStart;
380 uint16_t fEnd;
381 SkUnichar fUnicode;
382};
383
384static void append_bfchar_section(const SkTDArray<BFChar>& bfchar,
385 SkDynamicMemoryWStream* cmap) {
386 // PDF spec defines that every bf* list can have at most 100 entries.
387 for (int i = 0; i < bfchar.count(); i += 100) {
388 int count = bfchar.count() - i;
389 count = SkMin32(count, 100);
390 cmap->writeDecAsText(count);
391 cmap->writeText(" beginbfchar\n");
392 for (int j = 0; j < count; ++j) {
393 cmap->writeText("<");
394 cmap->writeHexAsText(bfchar[i + j].fGlyphId, 4);
395 cmap->writeText("> <");
396 cmap->writeHexAsText(bfchar[i + j].fUnicode, 4);
397 cmap->writeText(">\n");
398 }
399 cmap->writeText("endbfchar\n");
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000400 }
401}
402
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000403static void append_bfrange_section(const SkTDArray<BFRange>& bfrange,
404 SkDynamicMemoryWStream* cmap) {
405 // PDF spec defines that every bf* list can have at most 100 entries.
406 for (int i = 0; i < bfrange.count(); i += 100) {
407 int count = bfrange.count() - i;
408 count = SkMin32(count, 100);
409 cmap->writeDecAsText(count);
410 cmap->writeText(" beginbfrange\n");
411 for (int j = 0; j < count; ++j) {
412 cmap->writeText("<");
413 cmap->writeHexAsText(bfrange[i + j].fStart, 4);
414 cmap->writeText("> <");
415 cmap->writeHexAsText(bfrange[i + j].fEnd, 4);
416 cmap->writeText("> <");
417 cmap->writeHexAsText(bfrange[i + j].fUnicode, 4);
418 cmap->writeText(">\n");
419 }
420 cmap->writeText("endbfrange\n");
421 }
422}
423
424// Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe
425// Technote 5014.
426// The function is not static so we can test it in unit tests.
427//
428// Current implementation guarantees bfchar and bfrange entries do not overlap.
429//
430// Current implementation does not attempt aggresive optimizations against
431// following case because the specification is not clear.
432//
433// 4 beginbfchar 1 beginbfchar
434// <0003> <0013> <0020> <0014>
435// <0005> <0015> to endbfchar
436// <0007> <0017> 1 beginbfrange
437// <0020> <0014> <0003> <0007> <0013>
438// endbfchar endbfrange
439//
440// Adobe Technote 5014 said: "Code mappings (unlike codespace ranges) may
441// overlap, but succeeding maps superceded preceding maps."
442//
443// In case of searching text in PDF, bfrange will have higher precedence so
444// typing char id 0x0014 in search box will get glyph id 0x0004 first. However,
445// the spec does not mention how will this kind of conflict being resolved.
446//
447// For the worst case (having 65536 continuous unicode and we use every other
448// one of them), the possible savings by aggressive optimization is 416KB
449// pre-compressed and does not provide enough motivation for implementation.
450void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
451 const SkPDFGlyphSet* subset,
452 SkDynamicMemoryWStream* cmap) {
453 if (glyphToUnicode.count() == 0) return;
454
455 SkTDArray<BFChar> bfcharEntries;
456 SkTDArray<BFRange> bfrangeEntries;
457
458 BFRange currentRangeEntry;
459 bool haveBase = false;
460 int continuousEntries = 0;
461
462 for (int i = 0; i < glyphToUnicode.count(); ++i) {
463 if (glyphToUnicode[i] && (subset == NULL || subset->has(i))) {
464 // PDF spec requires bfrange not changing the higher byte,
465 // e.g. <1035> <10FF> <2222> is ok, but
466 // <1035> <1100> <2222> is no good
467 if (haveBase) {
468 ++continuousEntries;
469 if (i == currentRangeEntry.fStart + continuousEntries &&
470 (i >> 8) == (currentRangeEntry.fStart >> 8) &&
471 glyphToUnicode[i] == (currentRangeEntry.fUnicode +
472 continuousEntries)) {
473 currentRangeEntry.fEnd = i;
474 if (i == glyphToUnicode.count() - 1) {
475 // Last entry is in a range.
476 bfrangeEntries.push(currentRangeEntry);
477 }
478 continue;
479 }
480
481 // Need to have at least 2 entries to form a bfrange.
482 if (continuousEntries >= 2) {
483 bfrangeEntries.push(currentRangeEntry);
484 } else {
485 BFChar* entry = bfcharEntries.append();
486 entry->fGlyphId = currentRangeEntry.fStart;
487 entry->fUnicode = currentRangeEntry.fUnicode;
488 }
489 continuousEntries = 0;
490 }
491
492 if (i != glyphToUnicode.count() - 1) {
493 currentRangeEntry.fStart = i;
494 currentRangeEntry.fEnd = i;
495 currentRangeEntry.fUnicode = glyphToUnicode[i];
496 haveBase = true;
497 } else {
498 BFChar* entry = bfcharEntries.append();
499 entry->fGlyphId = i;
500 entry->fUnicode = glyphToUnicode[i];
501 }
502 }
503 }
504
505 // The spec requires all bfchar entries for a font must come before bfrange
506 // entries.
507 append_bfchar_section(bfcharEntries, cmap);
508 append_bfrange_section(bfrangeEntries, cmap);
509}
510
vandebo@chromium.org98594282011-07-25 22:34:12 +0000511static SkPDFStream* generate_tounicode_cmap(
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000512 const SkTDArray<SkUnichar>& glyphToUnicode,
513 const SkPDFGlyphSet* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000514 SkDynamicMemoryWStream cmap;
515 append_tounicode_header(&cmap);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000516 append_cmap_sections(glyphToUnicode, subset, &cmap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000517 append_cmap_footer(&cmap);
518 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
519 cmapStream->unref(); // SkRefPtr and new took a reference.
520 cmapStream->setData(cmap.copyToData());
521 return new SkPDFStream(cmapStream.get());
522}
523
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000524static void sk_delete_array(const void* ptr, size_t, void*) {
525 // Use C-style cast to cast away const and cast type simultaneously.
526 delete[] (unsigned char*)ptr;
527}
528
529static int get_subset_font_stream(const char* fontName,
530 const SkTypeface* typeface,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000531 const SkTDArray<uint32_t>& subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000532 SkPDFStream** fontStream) {
533 SkRefPtr<SkStream> fontData =
534 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface));
535 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
536
537 int fontSize = fontData->getLength();
538
539#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000540 // Read font into buffer.
541 SkPDFStream* subsetFontStream = NULL;
542 SkTDArray<unsigned char> originalFont;
543 originalFont.setCount(fontSize);
544 if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) {
545 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000546 // sfntly requires unsigned int* to be passed in, as far as we know,
547 // unsigned int is equivalent to uint32_t on all platforms.
548 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
549 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000550 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
551 originalFont.begin(),
552 fontSize,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000553 subset.begin(),
554 subset.count(),
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000555 &subsetFont);
556 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000557 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
558 subsetFontSize,
559 sk_delete_array,
560 NULL));
561 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000562 fontSize = subsetFontSize;
563 }
564 }
565 if (subsetFontStream) {
566 *fontStream = subsetFontStream;
567 return fontSize;
568 }
569#endif
570
571 // Fail over: just embed the whole font.
572 *fontStream = new SkPDFStream(fontData.get());
573 return fontSize;
574}
575
vandebo@chromium.org98594282011-07-25 22:34:12 +0000576///////////////////////////////////////////////////////////////////////////////
577// class SkPDFGlyphSet
578///////////////////////////////////////////////////////////////////////////////
579
580SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
581}
582
583void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
584 for (int i = 0; i < numGlyphs; ++i) {
585 fBitSet.setBit(glyphIDs[i], true);
586 }
587}
588
589bool SkPDFGlyphSet::has(uint16_t glyphID) const {
590 return fBitSet.isBitSet(glyphID);
591}
592
593void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
594 fBitSet.orBits(usage.fBitSet);
595}
596
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000597void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
598 fBitSet.exportTo(glyphIDs);
599}
600
vandebo@chromium.org98594282011-07-25 22:34:12 +0000601///////////////////////////////////////////////////////////////////////////////
602// class SkPDFGlyphSetMap
603///////////////////////////////////////////////////////////////////////////////
604SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
605 SkPDFGlyphSet* glyphSet)
606 : fFont(font),
607 fGlyphSet(glyphSet) {
608}
609
610SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
611 reset(map);
612}
613
614SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
615 if (fIndex >= fMap->count()) {
616 return NULL;
617 }
618 return &((*fMap)[fIndex++]);
619}
620
621void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
622 fMap = &(map.fMap);
623 fIndex = 0;
624}
625
626SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
627}
628
629SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
630 reset();
631}
632
633void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
634 for (int i = 0; i < usage.fMap.count(); ++i) {
635 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
636 myUsage->merge(*(usage.fMap[i].fGlyphSet));
637 }
638}
639
640void SkPDFGlyphSetMap::reset() {
641 for (int i = 0; i < fMap.count(); ++i) {
642 delete fMap[i].fGlyphSet; // Should not be NULL.
643 }
644 fMap.reset();
645}
646
647void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
648 int numGlyphs) {
649 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
650 if (subset) {
651 subset->set(glyphIDs, numGlyphs);
652 }
653}
654
655SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
656 int index = fMap.count();
657 for (int i = 0; i < index; ++i) {
658 if (fMap[i].fFont == font) {
659 return fMap[i].fGlyphSet;
660 }
661 }
662 fMap.append();
663 index = fMap.count() - 1;
664 fMap[index].fFont = font;
665 fMap[index].fGlyphSet = new SkPDFGlyphSet();
666 return fMap[index].fGlyphSet;
667}
668
669///////////////////////////////////////////////////////////////////////////////
670// class SkPDFFont
671///////////////////////////////////////////////////////////////////////////////
672
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000673/* Font subset design: It would be nice to be able to subset fonts
674 * (particularly type 3 fonts), but it's a lot of work and not a priority.
675 *
676 * Resources are canonicalized and uniqueified by pointer so there has to be
677 * some additional state indicating which subset of the font is used. It
678 * must be maintained at the page granularity and then combined at the document
679 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
680 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
681 * page/pdf device. c) in the document, retrieve the per font glyph usage
682 * from each page and combine it and ask for a resource with that subset.
683 */
684
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000685SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000686 SkAutoMutexAcquire lock(CanonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000687 int index;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000688 if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
689 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000690 }
691 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000692}
693
694void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000695 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000696}
697
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000698SkTypeface* SkPDFFont::typeface() {
699 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000700}
701
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000702SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000703 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000704}
705
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000706bool SkPDFFont::hasGlyph(uint16_t id) {
707 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
708}
709
vandebo@chromium.org01294102011-02-28 19:52:18 +0000710size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
711 size_t numGlyphs) {
712 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000713 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000714 return numGlyphs;
715 }
716
717 for (size_t i = 0; i < numGlyphs; i++) {
718 if (glyphIDs[i] == 0) {
719 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000720 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000721 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
722 return i;
723 }
724 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000725 }
726
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000727 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000728}
729
730// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000731SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
732 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000733 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000734 int relatedFontIndex;
735 if (Find(fontID, glyphID, &relatedFontIndex)) {
736 CanonicalFonts()[relatedFontIndex].fFont->ref();
737 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000738 }
739
vandebo@chromium.org98594282011-07-25 22:34:12 +0000740 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
741 SkPDFDict* relatedFontDescriptor = NULL;
742 if (relatedFontIndex >= 0) {
743 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000744 SkASSERT(relatedFont->fFontInfo.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +0000745 fontMetrics = relatedFont->fontInfo();
746 relatedFontDescriptor = relatedFont->getFontDescriptor();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000747 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000748 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000749 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000750 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
751 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000752#if !defined (SK_SFNTLY_SUBSETTER)
753 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
754 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
755#endif
756 fontMetrics =
757 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
758#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org98594282011-07-25 22:34:12 +0000759 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref.
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000760 if (fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
761 // Font does not support subsetting, get new info with advance.
762 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
763 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
764 fontMetrics =
765 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
766 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
767 }
768#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000769 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000770
vandebo@chromium.org98594282011-07-25 22:34:12 +0000771 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
772 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000773 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000774 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000775 return font; // Return the reference new SkPDFFont() created.
776}
777
vandebo@chromium.org98594282011-07-25 22:34:12 +0000778SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) {
779 return NULL; // Default: no support.
780}
781
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000782// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000783SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000784 // This initialization is only thread safe with gcc.
785 static SkTDArray<FontRec> gCanonicalFonts;
786 return gCanonicalFonts;
787}
788
789// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000790SkMutex& SkPDFFont::CanonicalFontsMutex() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000791 // This initialization is only thread safe with gcc.
792 static SkMutex gCanonicalFontsMutex;
793 return gCanonicalFontsMutex;
794}
795
796// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000797bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000798 // TODO(vandebo) optimize this, do only one search?
799 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000800 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000801 if (*index >= 0)
802 return true;
803 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000804 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000805 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000806}
807
vandebo@chromium.org98594282011-07-25 22:34:12 +0000808SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
809 uint16_t glyphID, bool descendantFont)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000810 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000811 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000812 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000813 fLastGlyphID(info ? info->fLastGlyphID : 0),
814 fFontInfo(info) {
815 if (info == NULL) {
816 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
817 } else if (info->fMultiMaster) {
818 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
819 } else {
820 fFontType = info->fType;
821 }
822}
823
824// static
825SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
826 SkTypeface* typeface, uint16_t glyphID,
827 SkPDFDict* relatedFontDescriptor) {
828 SkAdvancedTypefaceMetrics::FontType type =
829 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
830
831 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000832 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000833 return new SkPDFType3Font(info,
834 typeface,
835 glyphID,
836 relatedFontDescriptor);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000837 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000838 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
839 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
840 SkASSERT(relatedFontDescriptor == NULL);
841 return new SkPDFType0Font(info, typeface);
842 }
843 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
844 return new SkPDFType1Font(info,
845 typeface,
846 glyphID,
847 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000848 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000849
vandebo@chromium.org98594282011-07-25 22:34:12 +0000850 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
851 type == SkAdvancedTypefaceMetrics::kOther_Font ||
852 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000853
vandebo@chromium.org98594282011-07-25 22:34:12 +0000854 return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000855}
856
vandebo@chromium.org98594282011-07-25 22:34:12 +0000857SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
858 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000859}
860
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000861void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
862 if (info == NULL || info == fFontInfo.get()) {
863 return;
864 }
865 fFontInfo = info;
866}
867
vandebo@chromium.org98594282011-07-25 22:34:12 +0000868uint16_t SkPDFFont::firstGlyphID() const {
869 return fFirstGlyphID;
870}
871
872uint16_t SkPDFFont::lastGlyphID() const {
873 return fLastGlyphID;
874}
875
876void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
877 fLastGlyphID = glyphID;
878}
879
880void SkPDFFont::addResource(SkPDFObject* object) {
881 SkASSERT(object != NULL);
882 fResources.push(object);
883}
884
885SkPDFDict* SkPDFFont::getFontDescriptor() {
886 return fDescriptor.get();
887}
888
889void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
890 fDescriptor = descriptor;
891}
892
893bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
894 if (fDescriptor.get() == NULL) {
895 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000896 }
897
vandebo@chromium.org98594282011-07-25 22:34:12 +0000898 const uint16_t emSize = fFontInfo->fEmSize;
899
900 fDescriptor->insertName("FontName", fFontInfo->fFontName);
901 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
902 fDescriptor->insertScalar("Ascent",
903 scaleFromFontUnits(fFontInfo->fAscent, emSize));
904 fDescriptor->insertScalar("Descent",
905 scaleFromFontUnits(fFontInfo->fDescent, emSize));
906 fDescriptor->insertScalar("StemV",
907 scaleFromFontUnits(fFontInfo->fStemV, emSize));
908 fDescriptor->insertScalar("CapHeight",
909 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
910 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
911 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
912 fFontInfo->fEmSize))->unref();
913
914 if (defaultWidth > 0) {
915 fDescriptor->insertScalar("MissingWidth",
916 scaleFromFontUnits(defaultWidth, emSize));
917 }
918 return true;
919}
920
921void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
922 // Single byte glyph encoding supports a max of 255 glyphs.
923 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
924 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
925 fLastGlyphID = fFirstGlyphID + 255 - 1;
926 }
927}
928
929bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
930 if (fFontID != b.fFontID)
931 return false;
932 if (fFont != NULL && b.fFont != NULL) {
933 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
934 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
935 }
936 if (fGlyphID == 0 || b.fGlyphID == 0)
937 return true;
938
939 if (fFont != NULL) {
940 return fFont->fFirstGlyphID <= b.fGlyphID &&
941 b.fGlyphID <= fFont->fLastGlyphID;
942 } else if (b.fFont != NULL) {
943 return b.fFont->fFirstGlyphID <= fGlyphID &&
944 fGlyphID <= b.fFont->fLastGlyphID;
945 }
946 return fGlyphID == b.fGlyphID;
947}
948
949SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
950 : fFont(font),
951 fFontID(fontID),
952 fGlyphID(glyphID) {
953}
954
955void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
956 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
957 return;
958 }
959 SkRefPtr<SkPDFStream> pdfCmap =
960 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset);
961 addResource(pdfCmap.get()); // Pass reference from new.
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000962 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000963}
964
vandebo@chromium.org98594282011-07-25 22:34:12 +0000965///////////////////////////////////////////////////////////////////////////////
966// class SkPDFType0Font
967///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000968
vandebo@chromium.org98594282011-07-25 22:34:12 +0000969SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
970 SkTypeface* typeface)
971 : SkPDFFont(info, typeface, 0, false) {
972 SkDEBUGCODE(fPopulated = false);
973}
974
975SkPDFType0Font::~SkPDFType0Font() {}
976
977SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
978 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
979 newSubset->populate(subset);
980 return newSubset;
981}
982
983#ifdef SK_DEBUG
984void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
985 bool indirect) {
986 SkASSERT(fPopulated);
987 return INHERITED::emitObject(stream, catalog, indirect);
988}
989#endif
990
991bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
992 insertName("Subtype", "Type0");
993 insertName("BaseFont", fontInfo()->fFontName);
994 insertName("Encoding", "Identity-H");
995
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000996 SkPDFCIDFont* newCIDFont;
997 newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset);
998
vandebo@chromium.org98594282011-07-25 22:34:12 +0000999 // Pass ref new created to fResources.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001000 addResource(newCIDFont);
1001 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
1002 descendantFonts->unref(); // SkRefPtr and new took a reference.
1003 descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref();
1004 insert("DescendantFonts", descendantFonts.get());
1005
1006 populateToUnicodeTable(subset);
1007
1008 SkDEBUGCODE(fPopulated = true);
1009 return true;
1010}
1011
1012///////////////////////////////////////////////////////////////////////////////
1013// class SkPDFCIDFont
1014///////////////////////////////////////////////////////////////////////////////
1015
1016SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1017 SkTypeface* typeface, const SkPDFGlyphSet* subset)
1018 : SkPDFFont(info, typeface, 0, true) {
1019 populate(subset);
1020}
1021
1022SkPDFCIDFont::~SkPDFCIDFont() {}
1023
1024bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001025 const SkTDArray<uint32_t>* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001026 SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor");
1027 descriptor->unref(); // SkRefPtr and new both took a ref.
1028 setFontDescriptor(descriptor.get());
1029
1030 switch (getType()) {
1031 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001032 SkASSERT(subset);
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001033 // Font subsetting
1034 SkPDFStream* rawStream = NULL;
1035 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1036 typeface(),
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001037 *subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001038 &rawStream);
1039 SkASSERT(fontSize);
1040 SkASSERT(rawStream);
1041 SkRefPtr<SkPDFStream> fontStream = rawStream;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001042 // SkRefPtr and new both ref()'d fontStream, pass one.
1043 addResource(fontStream.get());
1044
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001045 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001046 descriptor->insert("FontFile2",
1047 new SkPDFObjRef(fontStream.get()))->unref();
1048 break;
1049 }
1050 case SkAdvancedTypefaceMetrics::kCFF_Font:
1051 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1052 SkRefPtr<SkStream> fontData =
1053 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1054 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
1055 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
1056 // SkRefPtr and new both ref()'d fontStream, pass one.
1057 addResource(fontStream.get());
1058
1059 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1060 fontStream->insertName("Subtype", "Type1C");
1061 } else {
1062 fontStream->insertName("Subtype", "CIDFontType0c");
1063 }
1064 descriptor->insert("FontFile3",
1065 new SkPDFObjRef(fontStream.get()))->unref();
1066 break;
1067 }
1068 default:
1069 SkASSERT(false);
1070 }
1071
1072 addResource(descriptor.get());
1073 descriptor->ref();
1074
1075 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1076 return addCommonFontDescriptorEntries(defaultWidth);
1077}
1078
1079bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001080 // Generate new font metrics with advance info for true type fonts.
1081 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1082 // Generate glyph id array.
1083 SkTDArray<uint32_t> glyphIDs;
1084 glyphIDs.push(0); // Always include glyph 0.
1085 if (subset) {
1086 subset->exportTo(&glyphIDs);
1087 }
1088
1089 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
1090 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
1091 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
1092 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
1093 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
1094 uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin();
1095 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
1096 fontMetrics =
1097 SkFontHost::GetAdvancedTypefaceMetrics(
1098 SkTypeface::UniqueID(typeface()),
1099 info,
1100 glyphs,
1101 glyphsCount);
1102 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
1103 setFontInfo(fontMetrics.get());
1104 addFontDescriptor(0, &glyphIDs);
1105 } else {
1106 // Other CID fonts
1107 addFontDescriptor(0, NULL);
1108 }
1109
vandebo@chromium.org98594282011-07-25 22:34:12 +00001110 insertName("BaseFont", fontInfo()->fFontName);
1111
1112 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001113 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001114 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001115 insertName("Subtype", "CIDFontType2");
1116 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001117 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001118 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001119 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001120
1121 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
1122 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001123 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1124 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001125 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001126 insert("CIDSystemInfo", sysInfo.get());
1127
vandebo@chromium.org98594282011-07-25 22:34:12 +00001128 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001129 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001130 SkRefPtr<SkPDFArray> widths =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001131 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1132 fontInfo()->fEmSize, &appendWidth,
1133 &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001134 widths->unref(); // SkRefPtr and compose both took a reference.
1135 if (widths->size())
1136 insert("W", widths.get());
1137 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001138 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001139 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001140 }
1141 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001142 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001143 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001144 defaultAdvance.fVerticalAdvance = 0;
1145 defaultAdvance.fOriginXDisp = 0;
1146 defaultAdvance.fOriginYDisp = 0;
1147 SkRefPtr<SkPDFArray> advances =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001148 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1149 fontInfo()->fEmSize, &appendVerticalAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001150 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001151 advances->unref(); // SkRefPtr and compose both took a ref.
1152 if (advances->size())
1153 insert("W2", advances.get());
1154 if (defaultAdvance.fVerticalAdvance ||
1155 defaultAdvance.fOriginXDisp ||
1156 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001157 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001158 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001159 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001160 }
1161 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001162
1163 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001164}
1165
vandebo@chromium.org98594282011-07-25 22:34:12 +00001166///////////////////////////////////////////////////////////////////////////////
1167// class SkPDFType1Font
1168///////////////////////////////////////////////////////////////////////////////
1169
1170SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1171 SkTypeface* typeface,
1172 uint16_t glyphID,
1173 SkPDFDict* relatedFontDescriptor)
1174 : SkPDFFont(info, typeface, glyphID, false) {
1175 populate(glyphID);
1176}
1177
1178SkPDFType1Font::~SkPDFType1Font() {}
1179
1180bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
1181 SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
1182 if (descriptor.get() != NULL) {
1183 addResource(descriptor.get());
1184 descriptor->ref();
1185 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1186 return true;
1187 }
1188
1189 descriptor = new SkPDFDict("FontDescriptor");
1190 descriptor->unref(); // SkRefPtr and new both took a ref.
1191 setFontDescriptor(descriptor.get());
1192
1193 size_t header SK_INIT_TO_AVOID_WARNING;
1194 size_t data SK_INIT_TO_AVOID_WARNING;
1195 size_t trailer SK_INIT_TO_AVOID_WARNING;
1196 SkRefPtr<SkStream> rawFontData =
1197 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1198 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
1199 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1200 &trailer);
1201 if (fontData == NULL) {
1202 return false;
1203 }
1204 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
1205 // SkRefPtr and new both ref()'d fontStream, pass one.
1206 addResource(fontStream.get());
1207 fontStream->insertInt("Length1", header);
1208 fontStream->insertInt("Length2", data);
1209 fontStream->insertInt("Length3", trailer);
1210 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
1211
1212 addResource(descriptor.get());
1213 descriptor->ref();
1214 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1215
1216 return addCommonFontDescriptorEntries(defaultWidth);
1217}
1218
1219bool SkPDFType1Font::populate(int16_t glyphID) {
1220 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1221 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001222
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001223 adjustGlyphRangeForSingleByteEncoding(glyphID);
1224
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001225 int16_t defaultWidth = 0;
1226 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1227 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001228 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001229 widthEntry != NULL;
1230 widthEntry = widthEntry->fNext.get()) {
1231 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001232 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001233 defaultWidth = widthEntry->fAdvance[0];
1234 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001235 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001236 SkASSERT(false);
1237 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001238 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001239 SkASSERT(widthRangeEntry == NULL);
1240 widthRangeEntry = widthEntry;
1241 break;
1242 }
1243 }
1244
1245 if (!addFontDescriptor(defaultWidth))
1246 return false;
1247
reed@google.comc789cf12011-07-20 12:14:33 +00001248 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001249 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001250
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001251 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001252
1253 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001254 encoding->unref(); // SkRefPtr and new both took a reference.
1255 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001256
1257 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1258 encDiffs->unref(); // SkRefPtr and new both took a reference.
1259 encoding->insert("Differences", encDiffs.get());
1260
vandebo@chromium.org98594282011-07-25 22:34:12 +00001261 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001262 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001263 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1264 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001265 }
1266
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001267 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001268}
1269
vandebo@chromium.org98594282011-07-25 22:34:12 +00001270void SkPDFType1Font::addWidthInfoFromRange(
1271 int16_t defaultWidth,
1272 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
1273 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1274 widthArray->unref(); // SkRefPtr and new both took a ref.
1275 int firstChar = 0;
1276 if (widthRangeEntry) {
1277 const uint16_t emSize = fontInfo()->fEmSize;
1278 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1279 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1280 if (startIndex < 0)
1281 startIndex = 0;
1282 if (endIndex > widthRangeEntry->fAdvance.count())
1283 endIndex = widthRangeEntry->fAdvance.count();
1284 if (widthRangeEntry->fStartId == 0) {
1285 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1286 } else {
1287 firstChar = startIndex + widthRangeEntry->fStartId;
1288 }
1289 for (int i = startIndex; i < endIndex; i++)
1290 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
1291 } else {
1292 appendWidth(defaultWidth, 1000, widthArray.get());
1293 }
1294 insertInt("FirstChar", firstChar);
1295 insertInt("LastChar", firstChar + widthArray->size() - 1);
1296 insert("Widths", widthArray.get());
1297}
1298
1299///////////////////////////////////////////////////////////////////////////////
1300// class SkPDFType3Font
1301///////////////////////////////////////////////////////////////////////////////
1302
1303SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1304 SkTypeface* typeface,
1305 uint16_t glyphID,
1306 SkPDFDict* relatedFontDescriptor)
1307 : SkPDFFont(info, typeface, glyphID, false) {
1308 populate(glyphID);
1309}
1310
1311SkPDFType3Font::~SkPDFType3Font() {}
1312
1313bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001314 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001315 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001316 paint.setTextSize(1000);
1317 SkAutoGlyphCache autoCache(paint, NULL);
1318 SkGlyphCache* cache = autoCache.getCache();
1319 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001320 if (lastGlyphID() == 0) {
1321 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001322 }
1323
1324 adjustGlyphRangeForSingleByteEncoding(glyphID);
1325
reed@google.comc789cf12011-07-20 12:14:33 +00001326 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001327 // Flip about the x-axis and scale by 1/1000.
1328 SkMatrix fontMatrix;
1329 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1330 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1331
1332 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
1333 charProcs->unref(); // SkRefPtr and new both took a reference.
1334 insert("CharProcs", charProcs.get());
1335
1336 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
1337 encoding->unref(); // SkRefPtr and new both took a reference.
1338 insert("Encoding", encoding.get());
1339
1340 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1341 encDiffs->unref(); // SkRefPtr and new both took a reference.
1342 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001343 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001344 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001345
1346 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1347 widthArray->unref(); // SkRefPtr and new both took a ref.
1348
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001349 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001350 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001351 SkString characterName;
1352 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001353 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001354
reed@google.comce11b262011-03-21 21:25:35 +00001355 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001356 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001357 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1358 glyph.fWidth, glyph.fHeight);
1359 bbox.join(glyphBBox);
1360
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001361 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001362 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1363 &content);
1364 const SkPath* path = cache->findPath(glyph);
1365 if (path) {
1366 SkPDFUtils::EmitPath(*path, &content);
1367 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1368 &content);
1369 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001370 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001371 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +00001372 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001373
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001374 SkRefPtr<SkPDFStream> glyphDescription =
1375 new SkPDFStream(glyphStream.get());
1376 // SkRefPtr and new both ref()'d charProcs, pass one.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001377 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001378 charProcs->insert(characterName.c_str(),
1379 new SkPDFObjRef(glyphDescription.get()))->unref();
1380 }
1381
1382 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001383 insertInt("FirstChar", firstGlyphID());
1384 insertInt("LastChar", lastGlyphID());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001385 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001386 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001387
vandebo@chromium.org98594282011-07-25 22:34:12 +00001388 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001389 return true;
1390}