blob: d96afa8238bb34c789f9579f4d90803a93bdc88a [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,
531 const SkPDFGlyphSet* subset,
532 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)
540 // Generate glyph id array.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000541 SkTDArray<uint32_t> glyphIDs;
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000542 glyphIDs.push(0); // Always include glyph 0.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000543 subset->exportTo(&glyphIDs);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000544
545 // Read font into buffer.
546 SkPDFStream* subsetFontStream = NULL;
547 SkTDArray<unsigned char> originalFont;
548 originalFont.setCount(fontSize);
549 if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) {
550 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000551 // sfntly requires unsigned int* to be passed in, as far as we know,
552 // unsigned int is equivalent to uint32_t on all platforms.
553 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
554 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000555 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
556 originalFont.begin(),
557 fontSize,
558 glyphIDs.begin(),
559 glyphIDs.count(),
560 &subsetFont);
561 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000562 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
563 subsetFontSize,
564 sk_delete_array,
565 NULL));
566 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000567 fontSize = subsetFontSize;
568 }
569 }
570 if (subsetFontStream) {
571 *fontStream = subsetFontStream;
572 return fontSize;
573 }
574#endif
575
576 // Fail over: just embed the whole font.
577 *fontStream = new SkPDFStream(fontData.get());
578 return fontSize;
579}
580
vandebo@chromium.org98594282011-07-25 22:34:12 +0000581///////////////////////////////////////////////////////////////////////////////
582// class SkPDFGlyphSet
583///////////////////////////////////////////////////////////////////////////////
584
585SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
586}
587
588void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
589 for (int i = 0; i < numGlyphs; ++i) {
590 fBitSet.setBit(glyphIDs[i], true);
591 }
592}
593
594bool SkPDFGlyphSet::has(uint16_t glyphID) const {
595 return fBitSet.isBitSet(glyphID);
596}
597
598void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
599 fBitSet.orBits(usage.fBitSet);
600}
601
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000602void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
603 fBitSet.exportTo(glyphIDs);
604}
605
vandebo@chromium.org98594282011-07-25 22:34:12 +0000606///////////////////////////////////////////////////////////////////////////////
607// class SkPDFGlyphSetMap
608///////////////////////////////////////////////////////////////////////////////
609SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
610 SkPDFGlyphSet* glyphSet)
611 : fFont(font),
612 fGlyphSet(glyphSet) {
613}
614
615SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
616 reset(map);
617}
618
619SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
620 if (fIndex >= fMap->count()) {
621 return NULL;
622 }
623 return &((*fMap)[fIndex++]);
624}
625
626void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
627 fMap = &(map.fMap);
628 fIndex = 0;
629}
630
631SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
632}
633
634SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
635 reset();
636}
637
638void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
639 for (int i = 0; i < usage.fMap.count(); ++i) {
640 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
641 myUsage->merge(*(usage.fMap[i].fGlyphSet));
642 }
643}
644
645void SkPDFGlyphSetMap::reset() {
646 for (int i = 0; i < fMap.count(); ++i) {
647 delete fMap[i].fGlyphSet; // Should not be NULL.
648 }
649 fMap.reset();
650}
651
652void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
653 int numGlyphs) {
654 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
655 if (subset) {
656 subset->set(glyphIDs, numGlyphs);
657 }
658}
659
660SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
661 int index = fMap.count();
662 for (int i = 0; i < index; ++i) {
663 if (fMap[i].fFont == font) {
664 return fMap[i].fGlyphSet;
665 }
666 }
667 fMap.append();
668 index = fMap.count() - 1;
669 fMap[index].fFont = font;
670 fMap[index].fGlyphSet = new SkPDFGlyphSet();
671 return fMap[index].fGlyphSet;
672}
673
674///////////////////////////////////////////////////////////////////////////////
675// class SkPDFFont
676///////////////////////////////////////////////////////////////////////////////
677
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000678/* Font subset design: It would be nice to be able to subset fonts
679 * (particularly type 3 fonts), but it's a lot of work and not a priority.
680 *
681 * Resources are canonicalized and uniqueified by pointer so there has to be
682 * some additional state indicating which subset of the font is used. It
683 * must be maintained at the page granularity and then combined at the document
684 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
685 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
686 * page/pdf device. c) in the document, retrieve the per font glyph usage
687 * from each page and combine it and ask for a resource with that subset.
688 */
689
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000690SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000691 SkAutoMutexAcquire lock(CanonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000692 int index;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000693 if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
694 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000695 }
696 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000697}
698
699void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000700 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000701}
702
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000703SkTypeface* SkPDFFont::typeface() {
704 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000705}
706
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000707SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000708 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000709}
710
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000711bool SkPDFFont::hasGlyph(uint16_t id) {
712 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
713}
714
vandebo@chromium.org01294102011-02-28 19:52:18 +0000715size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
716 size_t numGlyphs) {
717 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000718 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000719 return numGlyphs;
720 }
721
722 for (size_t i = 0; i < numGlyphs; i++) {
723 if (glyphIDs[i] == 0) {
724 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000725 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000726 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
727 return i;
728 }
729 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000730 }
731
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000732 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000733}
734
735// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000736SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
737 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000738 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000739 int relatedFontIndex;
740 if (Find(fontID, glyphID, &relatedFontIndex)) {
741 CanonicalFonts()[relatedFontIndex].fFont->ref();
742 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000743 }
744
vandebo@chromium.org98594282011-07-25 22:34:12 +0000745 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
746 SkPDFDict* relatedFontDescriptor = NULL;
747 if (relatedFontIndex >= 0) {
748 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000749 SkASSERT(relatedFont->fFontInfo.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +0000750 fontMetrics = relatedFont->fontInfo();
751 relatedFontDescriptor = relatedFont->getFontDescriptor();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000752 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000753 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
754 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
755 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
756 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
757 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
758 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000759 fontMetrics = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
760 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000761 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000762
vandebo@chromium.org98594282011-07-25 22:34:12 +0000763 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
764 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000765 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000766 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000767 return font; // Return the reference new SkPDFFont() created.
768}
769
vandebo@chromium.org98594282011-07-25 22:34:12 +0000770SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) {
771 return NULL; // Default: no support.
772}
773
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000774// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000775SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000776 // This initialization is only thread safe with gcc.
777 static SkTDArray<FontRec> gCanonicalFonts;
778 return gCanonicalFonts;
779}
780
781// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000782SkMutex& SkPDFFont::CanonicalFontsMutex() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000783 // This initialization is only thread safe with gcc.
784 static SkMutex gCanonicalFontsMutex;
785 return gCanonicalFontsMutex;
786}
787
788// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000789bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000790 // TODO(vandebo) optimize this, do only one search?
791 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000792 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000793 if (*index >= 0)
794 return true;
795 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000796 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000797 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000798}
799
vandebo@chromium.org98594282011-07-25 22:34:12 +0000800SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
801 uint16_t glyphID, bool descendantFont)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000802 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000803 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000804 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000805 fLastGlyphID(info ? info->fLastGlyphID : 0),
806 fFontInfo(info) {
807 if (info == NULL) {
808 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
809 } else if (info->fMultiMaster) {
810 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
811 } else {
812 fFontType = info->fType;
813 }
814}
815
816// static
817SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
818 SkTypeface* typeface, uint16_t glyphID,
819 SkPDFDict* relatedFontDescriptor) {
820 SkAdvancedTypefaceMetrics::FontType type =
821 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
822
823 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000824 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000825 return new SkPDFType3Font(info,
826 typeface,
827 glyphID,
828 relatedFontDescriptor);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000829 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000830 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
831 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
832 SkASSERT(relatedFontDescriptor == NULL);
833 return new SkPDFType0Font(info, typeface);
834 }
835 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
836 return new SkPDFType1Font(info,
837 typeface,
838 glyphID,
839 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000840 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000841
vandebo@chromium.org98594282011-07-25 22:34:12 +0000842 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
843 type == SkAdvancedTypefaceMetrics::kOther_Font ||
844 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000845
vandebo@chromium.org98594282011-07-25 22:34:12 +0000846 return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000847}
848
vandebo@chromium.org98594282011-07-25 22:34:12 +0000849SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
850 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000851}
852
vandebo@chromium.org98594282011-07-25 22:34:12 +0000853uint16_t SkPDFFont::firstGlyphID() const {
854 return fFirstGlyphID;
855}
856
857uint16_t SkPDFFont::lastGlyphID() const {
858 return fLastGlyphID;
859}
860
861void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
862 fLastGlyphID = glyphID;
863}
864
865void SkPDFFont::addResource(SkPDFObject* object) {
866 SkASSERT(object != NULL);
867 fResources.push(object);
868}
869
870SkPDFDict* SkPDFFont::getFontDescriptor() {
871 return fDescriptor.get();
872}
873
874void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
875 fDescriptor = descriptor;
876}
877
878bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
879 if (fDescriptor.get() == NULL) {
880 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000881 }
882
vandebo@chromium.org98594282011-07-25 22:34:12 +0000883 const uint16_t emSize = fFontInfo->fEmSize;
884
885 fDescriptor->insertName("FontName", fFontInfo->fFontName);
886 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
887 fDescriptor->insertScalar("Ascent",
888 scaleFromFontUnits(fFontInfo->fAscent, emSize));
889 fDescriptor->insertScalar("Descent",
890 scaleFromFontUnits(fFontInfo->fDescent, emSize));
891 fDescriptor->insertScalar("StemV",
892 scaleFromFontUnits(fFontInfo->fStemV, emSize));
893 fDescriptor->insertScalar("CapHeight",
894 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
895 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
896 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
897 fFontInfo->fEmSize))->unref();
898
899 if (defaultWidth > 0) {
900 fDescriptor->insertScalar("MissingWidth",
901 scaleFromFontUnits(defaultWidth, emSize));
902 }
903 return true;
904}
905
906void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
907 // Single byte glyph encoding supports a max of 255 glyphs.
908 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
909 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
910 fLastGlyphID = fFirstGlyphID + 255 - 1;
911 }
912}
913
914bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
915 if (fFontID != b.fFontID)
916 return false;
917 if (fFont != NULL && b.fFont != NULL) {
918 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
919 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
920 }
921 if (fGlyphID == 0 || b.fGlyphID == 0)
922 return true;
923
924 if (fFont != NULL) {
925 return fFont->fFirstGlyphID <= b.fGlyphID &&
926 b.fGlyphID <= fFont->fLastGlyphID;
927 } else if (b.fFont != NULL) {
928 return b.fFont->fFirstGlyphID <= fGlyphID &&
929 fGlyphID <= b.fFont->fLastGlyphID;
930 }
931 return fGlyphID == b.fGlyphID;
932}
933
934SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
935 : fFont(font),
936 fFontID(fontID),
937 fGlyphID(glyphID) {
938}
939
940void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
941 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
942 return;
943 }
944 SkRefPtr<SkPDFStream> pdfCmap =
945 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset);
946 addResource(pdfCmap.get()); // Pass reference from new.
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000947 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000948}
949
vandebo@chromium.org98594282011-07-25 22:34:12 +0000950///////////////////////////////////////////////////////////////////////////////
951// class SkPDFType0Font
952///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000953
vandebo@chromium.org98594282011-07-25 22:34:12 +0000954SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
955 SkTypeface* typeface)
956 : SkPDFFont(info, typeface, 0, false) {
957 SkDEBUGCODE(fPopulated = false);
958}
959
960SkPDFType0Font::~SkPDFType0Font() {}
961
962SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
963 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
964 newSubset->populate(subset);
965 return newSubset;
966}
967
968#ifdef SK_DEBUG
969void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
970 bool indirect) {
971 SkASSERT(fPopulated);
972 return INHERITED::emitObject(stream, catalog, indirect);
973}
974#endif
975
976bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
977 insertName("Subtype", "Type0");
978 insertName("BaseFont", fontInfo()->fFontName);
979 insertName("Encoding", "Identity-H");
980
981 // Pass ref new created to fResources.
982 SkPDFCIDFont* newCIDFont =
983 new SkPDFCIDFont(fontInfo(), typeface(), subset);
984 addResource(newCIDFont);
985 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
986 descendantFonts->unref(); // SkRefPtr and new took a reference.
987 descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref();
988 insert("DescendantFonts", descendantFonts.get());
989
990 populateToUnicodeTable(subset);
991
992 SkDEBUGCODE(fPopulated = true);
993 return true;
994}
995
996///////////////////////////////////////////////////////////////////////////////
997// class SkPDFCIDFont
998///////////////////////////////////////////////////////////////////////////////
999
1000SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1001 SkTypeface* typeface, const SkPDFGlyphSet* subset)
1002 : SkPDFFont(info, typeface, 0, true) {
1003 populate(subset);
1004}
1005
1006SkPDFCIDFont::~SkPDFCIDFont() {}
1007
1008bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
1009 const SkPDFGlyphSet* subset) {
1010 SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor");
1011 descriptor->unref(); // SkRefPtr and new both took a ref.
1012 setFontDescriptor(descriptor.get());
1013
1014 switch (getType()) {
1015 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001016 // Font subsetting
1017 SkPDFStream* rawStream = NULL;
1018 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1019 typeface(),
1020 subset,
1021 &rawStream);
1022 SkASSERT(fontSize);
1023 SkASSERT(rawStream);
1024 SkRefPtr<SkPDFStream> fontStream = rawStream;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001025 // SkRefPtr and new both ref()'d fontStream, pass one.
1026 addResource(fontStream.get());
1027
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001028 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001029 descriptor->insert("FontFile2",
1030 new SkPDFObjRef(fontStream.get()))->unref();
1031 break;
1032 }
1033 case SkAdvancedTypefaceMetrics::kCFF_Font:
1034 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1035 SkRefPtr<SkStream> fontData =
1036 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1037 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
1038 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
1039 // SkRefPtr and new both ref()'d fontStream, pass one.
1040 addResource(fontStream.get());
1041
1042 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1043 fontStream->insertName("Subtype", "Type1C");
1044 } else {
1045 fontStream->insertName("Subtype", "CIDFontType0c");
1046 }
1047 descriptor->insert("FontFile3",
1048 new SkPDFObjRef(fontStream.get()))->unref();
1049 break;
1050 }
1051 default:
1052 SkASSERT(false);
1053 }
1054
1055 addResource(descriptor.get());
1056 descriptor->ref();
1057
1058 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1059 return addCommonFontDescriptorEntries(defaultWidth);
1060}
1061
1062bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
1063 insertName("BaseFont", fontInfo()->fFontName);
1064
1065 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001066 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001067 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001068 insertName("Subtype", "CIDFontType2");
1069 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001070 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001071 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001072 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001073
1074 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
1075 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001076 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1077 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001078 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001079 insert("CIDSystemInfo", sysInfo.get());
1080
vandebo@chromium.org98594282011-07-25 22:34:12 +00001081 addFontDescriptor(0, subset);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001082
vandebo@chromium.org98594282011-07-25 22:34:12 +00001083 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001084 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001085 SkRefPtr<SkPDFArray> widths =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001086 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1087 fontInfo()->fEmSize, &appendWidth,
1088 &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001089 widths->unref(); // SkRefPtr and compose both took a reference.
1090 if (widths->size())
1091 insert("W", widths.get());
1092 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001093 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001094 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001095 }
1096 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001097 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001098 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001099 defaultAdvance.fVerticalAdvance = 0;
1100 defaultAdvance.fOriginXDisp = 0;
1101 defaultAdvance.fOriginYDisp = 0;
1102 SkRefPtr<SkPDFArray> advances =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001103 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1104 fontInfo()->fEmSize, &appendVerticalAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001105 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001106 advances->unref(); // SkRefPtr and compose both took a ref.
1107 if (advances->size())
1108 insert("W2", advances.get());
1109 if (defaultAdvance.fVerticalAdvance ||
1110 defaultAdvance.fOriginXDisp ||
1111 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001112 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001113 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001114 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001115 }
1116 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001117
1118 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001119}
1120
vandebo@chromium.org98594282011-07-25 22:34:12 +00001121///////////////////////////////////////////////////////////////////////////////
1122// class SkPDFType1Font
1123///////////////////////////////////////////////////////////////////////////////
1124
1125SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1126 SkTypeface* typeface,
1127 uint16_t glyphID,
1128 SkPDFDict* relatedFontDescriptor)
1129 : SkPDFFont(info, typeface, glyphID, false) {
1130 populate(glyphID);
1131}
1132
1133SkPDFType1Font::~SkPDFType1Font() {}
1134
1135bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
1136 SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
1137 if (descriptor.get() != NULL) {
1138 addResource(descriptor.get());
1139 descriptor->ref();
1140 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1141 return true;
1142 }
1143
1144 descriptor = new SkPDFDict("FontDescriptor");
1145 descriptor->unref(); // SkRefPtr and new both took a ref.
1146 setFontDescriptor(descriptor.get());
1147
1148 size_t header SK_INIT_TO_AVOID_WARNING;
1149 size_t data SK_INIT_TO_AVOID_WARNING;
1150 size_t trailer SK_INIT_TO_AVOID_WARNING;
1151 SkRefPtr<SkStream> rawFontData =
1152 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1153 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
1154 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1155 &trailer);
1156 if (fontData == NULL) {
1157 return false;
1158 }
1159 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
1160 // SkRefPtr and new both ref()'d fontStream, pass one.
1161 addResource(fontStream.get());
1162 fontStream->insertInt("Length1", header);
1163 fontStream->insertInt("Length2", data);
1164 fontStream->insertInt("Length3", trailer);
1165 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
1166
1167 addResource(descriptor.get());
1168 descriptor->ref();
1169 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1170
1171 return addCommonFontDescriptorEntries(defaultWidth);
1172}
1173
1174bool SkPDFType1Font::populate(int16_t glyphID) {
1175 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1176 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001177
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001178 adjustGlyphRangeForSingleByteEncoding(glyphID);
1179
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001180 int16_t defaultWidth = 0;
1181 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1182 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001183 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001184 widthEntry != NULL;
1185 widthEntry = widthEntry->fNext.get()) {
1186 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001187 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001188 defaultWidth = widthEntry->fAdvance[0];
1189 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001190 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001191 SkASSERT(false);
1192 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001193 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001194 SkASSERT(widthRangeEntry == NULL);
1195 widthRangeEntry = widthEntry;
1196 break;
1197 }
1198 }
1199
1200 if (!addFontDescriptor(defaultWidth))
1201 return false;
1202
reed@google.comc789cf12011-07-20 12:14:33 +00001203 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001204 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001205
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001206 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001207
1208 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001209 encoding->unref(); // SkRefPtr and new both took a reference.
1210 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001211
1212 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1213 encDiffs->unref(); // SkRefPtr and new both took a reference.
1214 encoding->insert("Differences", encDiffs.get());
1215
vandebo@chromium.org98594282011-07-25 22:34:12 +00001216 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001217 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001218 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1219 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001220 }
1221
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001222 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001223}
1224
vandebo@chromium.org98594282011-07-25 22:34:12 +00001225void SkPDFType1Font::addWidthInfoFromRange(
1226 int16_t defaultWidth,
1227 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
1228 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1229 widthArray->unref(); // SkRefPtr and new both took a ref.
1230 int firstChar = 0;
1231 if (widthRangeEntry) {
1232 const uint16_t emSize = fontInfo()->fEmSize;
1233 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1234 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1235 if (startIndex < 0)
1236 startIndex = 0;
1237 if (endIndex > widthRangeEntry->fAdvance.count())
1238 endIndex = widthRangeEntry->fAdvance.count();
1239 if (widthRangeEntry->fStartId == 0) {
1240 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1241 } else {
1242 firstChar = startIndex + widthRangeEntry->fStartId;
1243 }
1244 for (int i = startIndex; i < endIndex; i++)
1245 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
1246 } else {
1247 appendWidth(defaultWidth, 1000, widthArray.get());
1248 }
1249 insertInt("FirstChar", firstChar);
1250 insertInt("LastChar", firstChar + widthArray->size() - 1);
1251 insert("Widths", widthArray.get());
1252}
1253
1254///////////////////////////////////////////////////////////////////////////////
1255// class SkPDFType3Font
1256///////////////////////////////////////////////////////////////////////////////
1257
1258SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1259 SkTypeface* typeface,
1260 uint16_t glyphID,
1261 SkPDFDict* relatedFontDescriptor)
1262 : SkPDFFont(info, typeface, glyphID, false) {
1263 populate(glyphID);
1264}
1265
1266SkPDFType3Font::~SkPDFType3Font() {}
1267
1268bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001269 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001270 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001271 paint.setTextSize(1000);
1272 SkAutoGlyphCache autoCache(paint, NULL);
1273 SkGlyphCache* cache = autoCache.getCache();
1274 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001275 if (lastGlyphID() == 0) {
1276 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001277 }
1278
1279 adjustGlyphRangeForSingleByteEncoding(glyphID);
1280
reed@google.comc789cf12011-07-20 12:14:33 +00001281 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001282 // Flip about the x-axis and scale by 1/1000.
1283 SkMatrix fontMatrix;
1284 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1285 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1286
1287 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
1288 charProcs->unref(); // SkRefPtr and new both took a reference.
1289 insert("CharProcs", charProcs.get());
1290
1291 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
1292 encoding->unref(); // SkRefPtr and new both took a reference.
1293 insert("Encoding", encoding.get());
1294
1295 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1296 encDiffs->unref(); // SkRefPtr and new both took a reference.
1297 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001298 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001299 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001300
1301 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1302 widthArray->unref(); // SkRefPtr and new both took a ref.
1303
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001304 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001305 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001306 SkString characterName;
1307 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001308 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001309
reed@google.comce11b262011-03-21 21:25:35 +00001310 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001311 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001312 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1313 glyph.fWidth, glyph.fHeight);
1314 bbox.join(glyphBBox);
1315
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001316 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001317 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1318 &content);
1319 const SkPath* path = cache->findPath(glyph);
1320 if (path) {
1321 SkPDFUtils::EmitPath(*path, &content);
1322 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1323 &content);
1324 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001325 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001326 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +00001327 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001328
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001329 SkRefPtr<SkPDFStream> glyphDescription =
1330 new SkPDFStream(glyphStream.get());
1331 // SkRefPtr and new both ref()'d charProcs, pass one.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001332 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001333 charProcs->insert(characterName.c_str(),
1334 new SkPDFObjRef(glyphDescription.get()))->unref();
1335 }
1336
1337 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001338 insertInt("FirstChar", firstGlyphID());
1339 insertInt("LastChar", lastGlyphID());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001340 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001341 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001342
vandebo@chromium.org98594282011-07-25 22:34:12 +00001343 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001344 return true;
1345}