blob: e9f8450509458af90031cba9f35975226e53d5a9 [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000017#include <ctype.h>
18
reed@google.com8a85d0c2011-06-24 19:12:12 +000019#include "SkData.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000021#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPaint.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000023#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000024#include "SkPDFFont.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000026#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000027#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000028#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000030#include "SkStream.h"
31#include "SkTypeface.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000032#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000033#include "SkUtils.h"
34
35namespace {
36
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000037bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
38 size_t* size) {
39 // PFB sections have a two or six bytes header. 0x80 and a one byte
40 // section type followed by a four byte section length. Type one is
41 // an ASCII section (includes a length), type two is a binary section
42 // (includes a length) and type three is an EOF marker with no length.
43 const uint8_t* buf = *src;
44 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
45 return false;
46 if (buf[1] == 3)
47 return true;
48 if (*len < 6)
49 return false;
50
vandebo@chromium.org73322072011-06-21 21:19:41 +000051 *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
52 ((size_t)buf[5] << 24);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000053 size_t consumed = *size + 6;
54 if (consumed > *len)
55 return false;
56 *src = *src + consumed;
57 *len = *len - consumed;
58 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000059}
60
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000061bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
62 size_t* dataLen, size_t* trailerLen) {
63 const uint8_t* srcPtr = src;
64 size_t remaining = size;
65
66 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
67 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
68 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
69 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000070}
71
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000072/* The sections of a PFA file are implicitly defined. The body starts
73 * after the line containing "eexec," and the trailer starts with 512
74 * literal 0's followed by "cleartomark" (plus arbitrary white space).
75 *
76 * This function assumes that src is NUL terminated, but the NUL
77 * termination is not included in size.
78 *
79 */
80bool parsePFA(const char* src, size_t size, size_t* headerLen,
81 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
82 const char* end = src + size;
83
84 const char* dataPos = strstr(src, "eexec");
85 if (!dataPos)
86 return false;
87 dataPos += strlen("eexec");
88 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
89 dataPos < end)
90 dataPos++;
91 *headerLen = dataPos - src;
92
93 const char* trailerPos = strstr(dataPos, "cleartomark");
94 if (!trailerPos)
95 return false;
96 int zeroCount = 0;
97 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
98 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
99 continue;
100 } else if (*trailerPos == '0') {
101 zeroCount++;
102 } else {
103 return false;
104 }
105 }
106 if (zeroCount != 512)
107 return false;
108
109 *hexDataLen = trailerPos - src - *headerLen;
110 *trailerLen = size - *headerLen - *hexDataLen;
111
112 // Verify that the data section is hex encoded and count the bytes.
113 int nibbles = 0;
114 for (; dataPos < trailerPos; dataPos++) {
115 if (isspace(*dataPos))
116 continue;
117 if (!isxdigit(*dataPos))
118 return false;
119 nibbles++;
120 }
121 *dataLen = (nibbles + 1) / 2;
122
123 return true;
124}
125
126int8_t hexToBin(uint8_t c) {
127 if (!isxdigit(c))
128 return -1;
129 if (c <= '9') return c - '0';
130 if (c <= 'F') return c - 'A' + 10;
131 if (c <= 'f') return c - 'a' + 10;
132 return -1;
133}
134
135SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
136 size_t* dataLen, size_t* trailerLen) {
137 // srcStream may be backed by a file or a unseekable fd, so we may not be
138 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
139 // the input only once is doable, but very ugly. Furthermore, it'd be nice
140 // if the data was NUL terminated so that we can use strstr() to search it.
141 // Make as few copies as possible given these constraints.
142 SkDynamicMemoryWStream dynamicStream;
143 SkRefPtr<SkMemoryStream> staticStream;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000144 SkData* data = NULL;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000145 const uint8_t* src;
146 size_t srcLen;
147 if ((srcLen = srcStream->getLength()) > 0) {
148 staticStream = new SkMemoryStream(srcLen + 1);
149 staticStream->unref(); // new and SkRefPtr both took a ref.
150 src = (const uint8_t*)staticStream->getMemoryBase();
151 if (srcStream->getMemoryBase() != NULL) {
152 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
153 } else {
154 size_t read = 0;
155 while (read < srcLen) {
156 size_t got = srcStream->read((void *)staticStream->getAtPos(),
157 srcLen - read);
158 if (got == 0)
159 return NULL;
160 read += got;
161 staticStream->seek(read);
162 }
163 }
164 ((uint8_t *)src)[srcLen] = 0;
165 } else {
166 static const size_t bufSize = 4096;
167 uint8_t buf[bufSize];
168 size_t amount;
169 while ((amount = srcStream->read(buf, bufSize)) > 0)
170 dynamicStream.write(buf, amount);
171 amount = 0;
172 dynamicStream.write(&amount, 1); // NULL terminator.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000173 data = dynamicStream.copyToData();
174 src = data->bytes();
175 srcLen = data->size() - 1;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000176 }
177
reed@google.com8a85d0c2011-06-24 19:12:12 +0000178 // this handles releasing the data we may have gotten from dynamicStream.
179 // if data is null, it is a no-op
180 SkAutoDataUnref aud(data);
181
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000182 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
183 SkMemoryStream* result =
184 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
185 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
186 result->seek(*headerLen);
187 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
188 result->seek(*headerLen + *dataLen);
189 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
190 *trailerLen);
191 result->rewind();
192 return result;
193 }
194
195 // A PFA has to be converted for PDF.
196 size_t hexDataLen;
197 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
198 trailerLen)) {
199 SkMemoryStream* result =
200 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
201 memcpy((char*)result->getAtPos(), src, *headerLen);
202 result->seek(*headerLen);
203
204 const uint8_t* hexData = src + *headerLen;
205 const uint8_t* trailer = hexData + hexDataLen;
206 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000207 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000208 bool highNibble = true;
209 for (; hexData < trailer; hexData++) {
210 char curNibble = hexToBin(*hexData);
211 if (curNibble < 0)
212 continue;
213 if (highNibble) {
214 dataByte = curNibble << 4;
215 highNibble = false;
216 } else {
217 dataByte |= curNibble;
218 highNibble = true;
219 ((char *)result->getAtPos())[outputOffset++] = dataByte;
220 }
221 }
222 if (!highNibble)
223 ((char *)result->getAtPos())[outputOffset++] = dataByte;
224 SkASSERT(outputOffset == *dataLen);
225 result->seek(*headerLen + outputOffset);
226
227 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
228 *trailerLen);
229 result->rewind();
230 return result;
231 }
232
233 return NULL;
234}
235
reed@google.com3f0dcf92011-03-18 21:23:45 +0000236// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000237SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000238 SkScalar scaled = SkIntToScalar(val);
239 if (emSize == 1000) {
240 return scaled;
241 } else {
242 return SkScalarMulDiv(scaled, 1000, emSize);
243 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000244}
245
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000246void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000247 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000248 // Specify width and bounding box for the glyph.
249 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000250 content->writeText(" 0 ");
251 content->writeDecAsText(box.fLeft);
252 content->writeText(" ");
253 content->writeDecAsText(box.fTop);
254 content->writeText(" ");
255 content->writeDecAsText(box.fRight);
256 content->writeText(" ");
257 content->writeDecAsText(box.fBottom);
258 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000259}
260
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000261SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000262 SkPDFArray* bbox = new SkPDFArray;
263 bbox->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000264 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
265 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
266 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
267 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000268 return bbox;
269}
270
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000271SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
272 SkPDFArray* array) {
reed@google.comc789cf12011-07-20 12:14:33 +0000273 array->appendScalar(scaleFromFontUnits(width, emSize));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000274 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000275}
276
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000277SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000278 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
279 uint16_t emSize, SkPDFArray* array) {
280 appendWidth(advance.fVerticalAdvance, emSize, array);
281 appendWidth(advance.fOriginXDisp, emSize, array);
282 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000283 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000284}
285
286template <typename Data>
287SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000288 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
289 uint16_t emSize,
290 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
291 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000292 Data* defaultAdvance) {
293 SkPDFArray* result = new SkPDFArray();
294 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
295 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000296 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000297 SkASSERT(advanceInfo->fAdvance.count() == 1);
298 *defaultAdvance = advanceInfo->fAdvance[0];
299 break;
300 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000301 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000302 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
303 advanceArray->unref(); // SkRefPtr and new both took a ref.
304 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000305 appendAdvance(advanceInfo->fAdvance[j], emSize,
306 advanceArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000307 result->appendInt(advanceInfo->fStartId);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000308 result->append(advanceArray.get());
309 break;
310 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000311 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000312 SkASSERT(advanceInfo->fAdvance.count() == 1);
reed@google.comc789cf12011-07-20 12:14:33 +0000313 result->appendInt(advanceInfo->fStartId);
314 result->appendInt(advanceInfo->fEndId);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000315 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000316 break;
317 }
318 }
319 }
320 return result;
321}
322
323} // namespace
324
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000325static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
326 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
327 // It's there to prevent old version Adobe Readers from malfunctioning.
328 const char* kHeader =
329 "/CIDInit /ProcSet findresource begin\n"
330 "12 dict begin\n"
331 "begincmap\n";
332 cmap->writeText(kHeader);
333
334 // The /CIDSystemInfo must be consistent to the one in
335 // SkPDFFont::populateCIDFont().
336 // We can not pass over the system info object here because the format is
337 // different. This is not a reference object.
338 const char* kSysInfo =
339 "/CIDSystemInfo\n"
340 "<< /Registry (Adobe)\n"
341 "/Ordering (UCS)\n"
342 "/Supplement 0\n"
343 ">> def\n";
344 cmap->writeText(kSysInfo);
345
346 // The CMapName must be consistent to /CIDSystemInfo above.
347 // /CMapType 2 means ToUnicode.
348 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
349 // code table from unsigned short (16-bits). Codespace range just tells the
350 // PDF processor the valid range. It does not matter whether a complete
351 // mapping is provided or not.
352 const char* kTypeInfo =
353 "/CMapName /Adobe-Identity-UCS def\n"
354 "/CMapType 2 def\n"
355 "1 begincodespacerange\n"
356 "<0000> <FFFF>\n"
357 "endcodespacerange\n";
358 cmap->writeText(kTypeInfo);
359}
360
361static void append_cmap_bfchar_table(uint16_t* glyph_id, SkUnichar* unicode,
362 size_t count,
363 SkDynamicMemoryWStream* cmap) {
364 cmap->writeDecAsText(count);
365 cmap->writeText(" beginbfchar\n");
366 for (size_t i = 0; i < count; ++i) {
367 cmap->writeText("<");
368 cmap->writeHexAsText(glyph_id[i], 4);
369 cmap->writeText("> <");
370 cmap->writeHexAsText(unicode[i], 4);
371 cmap->writeText(">\n");
372 }
373 cmap->writeText("endbfchar\n");
374}
375
376static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
377 const char* kFooter =
378 "endcmap\n"
379 "CMapName currentdict /CMap defineresource pop\n"
380 "end\n"
381 "end";
382 cmap->writeText(kFooter);
383}
384
385// Generate <bfchar> table according to PDF spec 1.4 and Adobe Technote 5014.
386static void append_cmap_bfchar_sections(
387 const SkTDArray<SkUnichar>& glyphUnicode,
388 SkDynamicMemoryWStream* cmap) {
389 // PDF spec defines that every bf* list can have at most 100 entries.
390 const size_t kMaxEntries = 100;
391 uint16_t glyphId[kMaxEntries];
392 SkUnichar unicode[kMaxEntries];
393 size_t index = 0;
394 for (int i = 0; i < glyphUnicode.count(); i++) {
395 if (glyphUnicode[i]) {
396 glyphId[index] = i;
397 unicode[index] = glyphUnicode[i];
398 ++index;
399 }
400 if (index == kMaxEntries) {
401 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
402 index = 0;
403 }
404 }
405
406 if (index) {
407 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
408 }
409}
410
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000411/* Font subset design: It would be nice to be able to subset fonts
412 * (particularly type 3 fonts), but it's a lot of work and not a priority.
413 *
414 * Resources are canonicalized and uniqueified by pointer so there has to be
415 * some additional state indicating which subset of the font is used. It
416 * must be maintained at the page granularity and then combined at the document
417 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
418 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
419 * page/pdf device. c) in the document, retrieve the per font glyph usage
420 * from each page and combine it and ask for a resource with that subset.
421 */
422
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000423SkPDFFont::~SkPDFFont() {
424 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000425 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000426 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000427 canonicalFonts().removeShuffle(index);
428#ifdef SK_DEBUG
429 SkASSERT(!fDescendant);
430 } else {
431 SkASSERT(fDescendant);
432#endif
433 }
434 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000435}
436
437void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
438 resourceList->setReserve(resourceList->count() + fResources.count());
439 for (int i = 0; i < fResources.count(); i++) {
440 resourceList->push(fResources[i]);
441 fResources[i]->ref();
442 fResources[i]->getResources(resourceList);
443 }
444}
445
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000446SkTypeface* SkPDFFont::typeface() {
447 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000448}
449
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000450SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
451 return fType;
452}
453
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000454bool SkPDFFont::hasGlyph(uint16_t id) {
455 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
456}
457
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000458bool SkPDFFont::multiByteGlyphs() {
459 return fMultiByteGlyphs;
460}
461
vandebo@chromium.org01294102011-02-28 19:52:18 +0000462size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
463 size_t numGlyphs) {
464 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000465 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000466 return numGlyphs;
467 }
468
469 for (size_t i = 0; i < numGlyphs; i++) {
470 if (glyphIDs[i] == 0) {
471 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000472 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000473 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
474 return i;
475 }
476 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000477 }
478
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000479 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000480}
481
482// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000483SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000484 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000485 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000486 int index;
487 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000488 canonicalFonts()[index].fFont->ref();
489 return canonicalFonts()[index].fFont;
490 }
491
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000492 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000493 SkPDFDict* fontDescriptor = NULL;
494 if (index >= 0) {
495 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
496 SkASSERT(relatedFont->fFontInfo.get());
497 fontInfo = relatedFont->fFontInfo;
498 fontDescriptor = relatedFont->fDescriptor.get();
499 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000500 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
501 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
502 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
503 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
504 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
505 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
506 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000507 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000509
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000510 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000511 fontDescriptor);
512 FontRec newEntry(font, fontID, font->fFirstGlyphID);
513 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000514 canonicalFonts().push(newEntry);
515 return font; // Return the reference new SkPDFFont() created.
516}
517
518// static
519SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
520 // This initialization is only thread safe with gcc.
521 static SkTDArray<FontRec> gCanonicalFonts;
522 return gCanonicalFonts;
523}
524
525// static
526SkMutex& SkPDFFont::canonicalFontsMutex() {
527 // This initialization is only thread safe with gcc.
528 static SkMutex gCanonicalFontsMutex;
529 return gCanonicalFontsMutex;
530}
531
532// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000533bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
534 // TODO(vandebo) optimize this, do only one search?
535 FontRec search(NULL, fontID, glyphID);
536 *index = canonicalFonts().find(search);
537 if (*index >= 0)
538 return true;
539 search.fGlyphID = 0;
540 *index = canonicalFonts().find(search);
541 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000542}
543
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000544SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
545 SkTypeface* typeface,
546 uint16_t glyphID,
547 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000548 SkPDFDict* fontDescriptor)
549 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000550 fTypeface(typeface),
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000551 fType(fontInfo ? fontInfo->fType :
552 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000553#ifdef SK_DEBUG
554 fDescendant(descendantFont),
555#endif
556 fMultiByteGlyphs(false),
557 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000558 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000559 fFontInfo(fontInfo),
560 fDescriptor(fontDescriptor) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000561 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000562 NOT_IMPLEMENTED(true, true);
563 fType = SkAdvancedTypefaceMetrics::kOther_Font;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000564 }
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000565 if (fType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
566 fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000567 if (descendantFont) {
568 populateCIDFont();
569 } else {
570 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000571 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000572 // No need to hold onto the font info for fonts types that
573 // support multibyte glyphs.
574 fFontInfo = NULL;
575 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000576 }
577
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000578 if (fType == SkAdvancedTypefaceMetrics::kType1_Font &&
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000579 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000580 return;
581 }
582
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000583 SkASSERT(fType == SkAdvancedTypefaceMetrics::kType1_Font ||
584 fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
585 fType == SkAdvancedTypefaceMetrics::kOther_Font ||
586 fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000587 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000588}
589
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000590void SkPDFFont::populateType0Font() {
591 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000592
reed@google.comc789cf12011-07-20 12:14:33 +0000593 insertName("Subtype", "Type0");
594 insertName("BaseFont", fFontInfo->fFontName);
595 insertName("Encoding", "Identity-H");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000596
597 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
598 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000599
600 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000601 fResources.push(
602 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000603 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000604 insert("DescendantFonts", descendantFonts.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000605
606 populateToUnicodeTable();
607}
608
609void SkPDFFont::populateToUnicodeTable() {
610 if (fFontInfo.get() == NULL ||
611 fFontInfo->fGlyphToUnicode.begin() == NULL) {
612 return;
613 }
614
615 SkDynamicMemoryWStream cmap;
616 append_tounicode_header(&cmap);
617 append_cmap_bfchar_sections(fFontInfo->fGlyphToUnicode, &cmap);
618 append_cmap_footer(&cmap);
619 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
620 cmapStream->unref(); // SkRefPtr and new took a reference.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000621 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000622 SkRefPtr<SkPDFStream> pdfCmap = new SkPDFStream(cmapStream.get());
623 fResources.push(pdfCmap.get()); // Pass reference from new.
624 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000625}
626
627void SkPDFFont::populateCIDFont() {
628 fMultiByteGlyphs = true;
reed@google.comc789cf12011-07-20 12:14:33 +0000629 insertName("BaseFont", fFontInfo->fFontName);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000630
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000631 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000632 insertName("Subtype", "CIDFontType0");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000633 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000634 insertName("Subtype", "CIDFontType2");
635 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000636 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000637 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000638 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000639
640 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
641 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000642 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
643 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +0000644 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000645 insert("CIDSystemInfo", sysInfo.get());
646
647 addFontDescriptor(0);
648
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000649 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000650 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000651 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000652 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
653 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000654 widths->unref(); // SkRefPtr and compose both took a reference.
655 if (widths->size())
656 insert("W", widths.get());
657 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +0000658 insertScalar("DW", scaleFromFontUnits(defaultWidth,
659 fFontInfo->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000660 }
661 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000662 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000663 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000664 defaultAdvance.fVerticalAdvance = 0;
665 defaultAdvance.fOriginXDisp = 0;
666 defaultAdvance.fOriginYDisp = 0;
667 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000668 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
669 fFontInfo->fEmSize, &appendVerticalAdvance,
670 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000671 advances->unref(); // SkRefPtr and compose both took a ref.
672 if (advances->size())
673 insert("W2", advances.get());
674 if (defaultAdvance.fVerticalAdvance ||
675 defaultAdvance.fOriginXDisp ||
676 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000677 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000678 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000679 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000680 }
681 }
682}
683
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000684bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000685 SkASSERT(!fFontInfo->fVerticalMetrics.get());
686 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000687
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000688 adjustGlyphRangeForSingleByteEncoding(glyphID);
689
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000690 int16_t defaultWidth = 0;
691 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
692 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000693 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
694 widthEntry != NULL;
695 widthEntry = widthEntry->fNext.get()) {
696 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000697 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000698 defaultWidth = widthEntry->fAdvance[0];
699 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000700 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000701 SkASSERT(false);
702 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000703 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000704 SkASSERT(widthRangeEntry == NULL);
705 widthRangeEntry = widthEntry;
706 break;
707 }
708 }
709
710 if (!addFontDescriptor(defaultWidth))
711 return false;
712
reed@google.comc789cf12011-07-20 12:14:33 +0000713 insertName("Subtype", "Type1");
714 insertName("BaseFont", fFontInfo->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000715
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000716 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000717
718 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000719 encoding->unref(); // SkRefPtr and new both took a reference.
720 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000721
722 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
723 encDiffs->unref(); // SkRefPtr and new both took a reference.
724 encoding->insert("Differences", encDiffs.get());
725
726 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
reed@google.comc789cf12011-07-20 12:14:33 +0000727 encDiffs->appendInt(1);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000728 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
reed@google.comc789cf12011-07-20 12:14:33 +0000729 encDiffs->appendName(fFontInfo->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000730 }
731
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000732 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000733 fFontInfo = NULL;
734 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000735}
736
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000737void SkPDFFont::populateType3Font(int16_t glyphID) {
738 SkPaint paint;
739 paint.setTypeface(fTypeface.get());
740 paint.setTextSize(1000);
741 SkAutoGlyphCache autoCache(paint, NULL);
742 SkGlyphCache* cache = autoCache.getCache();
743 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
744 if (fLastGlyphID == 0) {
745 fLastGlyphID = cache->getGlyphCount() - 1;
746 }
747
748 adjustGlyphRangeForSingleByteEncoding(glyphID);
749
reed@google.comc789cf12011-07-20 12:14:33 +0000750 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000751 // Flip about the x-axis and scale by 1/1000.
752 SkMatrix fontMatrix;
753 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
754 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
755
756 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
757 charProcs->unref(); // SkRefPtr and new both took a reference.
758 insert("CharProcs", charProcs.get());
759
760 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
761 encoding->unref(); // SkRefPtr and new both took a reference.
762 insert("Encoding", encoding.get());
763
764 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
765 encDiffs->unref(); // SkRefPtr and new both took a reference.
766 encoding->insert("Differences", encDiffs.get());
767 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
reed@google.comc789cf12011-07-20 12:14:33 +0000768 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000769
770 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
771 widthArray->unref(); // SkRefPtr and new both took a ref.
772
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000773 SkIRect bbox = SkIRect::MakeEmpty();
774 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
775 SkString characterName;
776 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +0000777 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000778
reed@google.comce11b262011-03-21 21:25:35 +0000779 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +0000780 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000781 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
782 glyph.fWidth, glyph.fHeight);
783 bbox.join(glyphBBox);
784
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000785 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000786 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
787 &content);
788 const SkPath* path = cache->findPath(glyph);
789 if (path) {
790 SkPDFUtils::EmitPath(*path, &content);
791 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
792 &content);
793 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000794 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000795 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000796 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000797
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000798 SkRefPtr<SkPDFStream> glyphDescription =
799 new SkPDFStream(glyphStream.get());
800 // SkRefPtr and new both ref()'d charProcs, pass one.
801 fResources.push(glyphDescription.get());
802 charProcs->insert(characterName.c_str(),
803 new SkPDFObjRef(glyphDescription.get()))->unref();
804 }
805
806 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +0000807 insertInt("FirstChar", fFirstGlyphID);
808 insertInt("LastChar", fLastGlyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000809 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000810 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000811
reed@google.comc789cf12011-07-20 12:14:33 +0000812 if (fFontInfo && fFontInfo->fLastGlyphID <= 255) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000813 fFontInfo = NULL;
reed@google.comc789cf12011-07-20 12:14:33 +0000814 }
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000815 populateToUnicodeTable();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000816}
817
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000818bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000819 if (fDescriptor.get() != NULL) {
820 fResources.push(fDescriptor.get());
821 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000822 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000823 return true;
824 }
825
826 fDescriptor = new SkPDFDict("FontDescriptor");
827 fDescriptor->unref(); // SkRefPtr and new both took a ref.
828
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000829 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000830 case SkAdvancedTypefaceMetrics::kType1_Font: {
reed@google.comee5ee582011-04-28 14:12:48 +0000831 size_t header SK_INIT_TO_AVOID_WARNING;
832 size_t data SK_INIT_TO_AVOID_WARNING;
833 size_t trailer SK_INIT_TO_AVOID_WARNING;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000835 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000836 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
837 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
838 &data, &trailer);
839 if (fontData == NULL)
840 return false;
841 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
842 // SkRefPtr and new both ref()'d fontStream, pass one.
843 fResources.push(fontStream.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000844 fontStream->insertInt("Length1", header);
845 fontStream->insertInt("Length2", data);
846 fontStream->insertInt("Length3", trailer);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000847 fDescriptor->insert("FontFile",
848 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000849 break;
850 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000851 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000852 SkRefPtr<SkStream> fontData =
853 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000854 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
855 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
856 // SkRefPtr and new both ref()'d fontStream, pass one.
857 fResources.push(fontStream.get());
858
reed@google.comc789cf12011-07-20 12:14:33 +0000859 fontStream->insertInt("Length1", fontData->getLength());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000860 fDescriptor->insert("FontFile2",
861 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000862 break;
863 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000864 case SkAdvancedTypefaceMetrics::kCFF_Font:
865 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000866 SkRefPtr<SkStream> fontData =
867 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000868 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
869 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
870 // SkRefPtr and new both ref()'d fontStream, pass one.
871 fResources.push(fontStream.get());
872
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000873 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +0000874 fontStream->insertName("Subtype", "Type1C");
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000875 } else {
reed@google.comc789cf12011-07-20 12:14:33 +0000876 fontStream->insertName("Subtype", "CIDFontType0c");
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000877 }
878 fDescriptor->insert("FontFile3",
879 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000880 break;
881 }
882 default:
883 SkASSERT(false);
884 }
885
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000886 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000887 fResources.push(fDescriptor.get());
888 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000889 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000890
reed@google.comc789cf12011-07-20 12:14:33 +0000891 fDescriptor->insertName("FontName", fFontInfo->fFontName);
892 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
893 fDescriptor->insertScalar("Ascent",
894 scaleFromFontUnits(fFontInfo->fAscent, emSize));
895 fDescriptor->insertScalar("Descent",
896 scaleFromFontUnits(fFontInfo->fDescent, emSize));
897 fDescriptor->insertScalar("StemV",
898 scaleFromFontUnits(fFontInfo->fStemV, emSize));
899 fDescriptor->insertScalar("CapHeight",
900 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
901 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000902 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
903 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000904
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000905 if (defaultWidth > 0) {
reed@google.comc789cf12011-07-20 12:14:33 +0000906 fDescriptor->insertScalar("MissingWidth",
907 scaleFromFontUnits(defaultWidth, emSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000908 }
909 return true;
910}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000911void SkPDFFont::addWidthInfoFromRange(
912 int16_t defaultWidth,
913 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
914 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
915 widthArray->unref(); // SkRefPtr and new both took a ref.
916 int firstChar = 0;
917 if (widthRangeEntry) {
918 const uint16_t emSize = fFontInfo->fEmSize;
919 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
920 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
921 if (startIndex < 0)
922 startIndex = 0;
923 if (endIndex > widthRangeEntry->fAdvance.count())
924 endIndex = widthRangeEntry->fAdvance.count();
925 if (widthRangeEntry->fStartId == 0) {
926 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
927 } else {
928 firstChar = startIndex + widthRangeEntry->fStartId;
929 }
930 for (int i = startIndex; i < endIndex; i++)
931 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
932 } else {
933 appendWidth(defaultWidth, 1000, widthArray.get());
934 }
reed@google.comc789cf12011-07-20 12:14:33 +0000935 insertInt("FirstChar", firstChar);
936 insertInt("LastChar", firstChar + widthArray->size() - 1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000937 insert("Widths", widthArray.get());
938}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000939
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000940void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
941 // Single byte glyph encoding supports a max of 255 glyphs.
942 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
943 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
944 fLastGlyphID = fFirstGlyphID + 255 - 1;
945 }
946}
947
948
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000949bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000950 if (fFontID != b.fFontID)
951 return false;
952 if (fFont != NULL && b.fFont != NULL) {
953 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
954 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
955 }
956 if (fGlyphID == 0 || b.fGlyphID == 0)
957 return true;
958
959 if (fFont != NULL) {
960 return fFont->fFirstGlyphID <= b.fGlyphID &&
961 b.fGlyphID <= fFont->fLastGlyphID;
962 } else if (b.fFont != NULL) {
963 return b.fFont->fFirstGlyphID <= fGlyphID &&
964 fGlyphID <= b.fFont->fLastGlyphID;
965 }
966 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000967}
968
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000969SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000970 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000971 fFontID(fontID),
972 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000973}