blob: d0d31e3265e4a785b0f450f0d0f17540ee99a63e [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);
264 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
265 emSize)))->unref();
266 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
267 emSize)))->unref();
268 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
269 emSize)))->unref();
270 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
271 emSize)))->unref();
272 return bbox;
273}
274
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000275SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
276 SkPDFArray* array) {
277 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000278 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000279}
280
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000281SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000282 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
283 uint16_t emSize, SkPDFArray* array) {
284 appendWidth(advance.fVerticalAdvance, emSize, array);
285 appendWidth(advance.fOriginXDisp, emSize, array);
286 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000287 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000288}
289
290template <typename Data>
291SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000292 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
293 uint16_t emSize,
294 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
295 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000296 Data* defaultAdvance) {
297 SkPDFArray* result = new SkPDFArray();
298 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
299 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000300 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000301 SkASSERT(advanceInfo->fAdvance.count() == 1);
302 *defaultAdvance = advanceInfo->fAdvance[0];
303 break;
304 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000305 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000306 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
307 advanceArray->unref(); // SkRefPtr and new both took a ref.
308 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000309 appendAdvance(advanceInfo->fAdvance[j], emSize,
310 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000311 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000312 result->append(advanceArray.get());
313 break;
314 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000315 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000316 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000317 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
318 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000319 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000320 break;
321 }
322 }
323 }
324 return result;
325}
326
327} // namespace
328
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000329static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
330 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
331 // It's there to prevent old version Adobe Readers from malfunctioning.
332 const char* kHeader =
333 "/CIDInit /ProcSet findresource begin\n"
334 "12 dict begin\n"
335 "begincmap\n";
336 cmap->writeText(kHeader);
337
338 // The /CIDSystemInfo must be consistent to the one in
339 // SkPDFFont::populateCIDFont().
340 // We can not pass over the system info object here because the format is
341 // different. This is not a reference object.
342 const char* kSysInfo =
343 "/CIDSystemInfo\n"
344 "<< /Registry (Adobe)\n"
345 "/Ordering (UCS)\n"
346 "/Supplement 0\n"
347 ">> def\n";
348 cmap->writeText(kSysInfo);
349
350 // The CMapName must be consistent to /CIDSystemInfo above.
351 // /CMapType 2 means ToUnicode.
352 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
353 // code table from unsigned short (16-bits). Codespace range just tells the
354 // PDF processor the valid range. It does not matter whether a complete
355 // mapping is provided or not.
356 const char* kTypeInfo =
357 "/CMapName /Adobe-Identity-UCS def\n"
358 "/CMapType 2 def\n"
359 "1 begincodespacerange\n"
360 "<0000> <FFFF>\n"
361 "endcodespacerange\n";
362 cmap->writeText(kTypeInfo);
363}
364
365static void append_cmap_bfchar_table(uint16_t* glyph_id, SkUnichar* unicode,
366 size_t count,
367 SkDynamicMemoryWStream* cmap) {
368 cmap->writeDecAsText(count);
369 cmap->writeText(" beginbfchar\n");
370 for (size_t i = 0; i < count; ++i) {
371 cmap->writeText("<");
372 cmap->writeHexAsText(glyph_id[i], 4);
373 cmap->writeText("> <");
374 cmap->writeHexAsText(unicode[i], 4);
375 cmap->writeText(">\n");
376 }
377 cmap->writeText("endbfchar\n");
378}
379
380static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
381 const char* kFooter =
382 "endcmap\n"
383 "CMapName currentdict /CMap defineresource pop\n"
384 "end\n"
385 "end";
386 cmap->writeText(kFooter);
387}
388
389// Generate <bfchar> table according to PDF spec 1.4 and Adobe Technote 5014.
390static void append_cmap_bfchar_sections(
391 const SkTDArray<SkUnichar>& glyphUnicode,
392 SkDynamicMemoryWStream* cmap) {
393 // PDF spec defines that every bf* list can have at most 100 entries.
394 const size_t kMaxEntries = 100;
395 uint16_t glyphId[kMaxEntries];
396 SkUnichar unicode[kMaxEntries];
397 size_t index = 0;
398 for (int i = 0; i < glyphUnicode.count(); i++) {
399 if (glyphUnicode[i]) {
400 glyphId[index] = i;
401 unicode[index] = glyphUnicode[i];
402 ++index;
403 }
404 if (index == kMaxEntries) {
405 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
406 index = 0;
407 }
408 }
409
410 if (index) {
411 append_cmap_bfchar_table(glyphId, unicode, index, cmap);
412 }
413}
414
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000415/* Font subset design: It would be nice to be able to subset fonts
416 * (particularly type 3 fonts), but it's a lot of work and not a priority.
417 *
418 * Resources are canonicalized and uniqueified by pointer so there has to be
419 * some additional state indicating which subset of the font is used. It
420 * must be maintained at the page granularity and then combined at the document
421 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
422 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
423 * page/pdf device. c) in the document, retrieve the per font glyph usage
424 * from each page and combine it and ask for a resource with that subset.
425 */
426
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000427SkPDFFont::~SkPDFFont() {
428 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000429 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000430 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431 canonicalFonts().removeShuffle(index);
432#ifdef SK_DEBUG
433 SkASSERT(!fDescendant);
434 } else {
435 SkASSERT(fDescendant);
436#endif
437 }
438 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000439}
440
441void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
442 resourceList->setReserve(resourceList->count() + fResources.count());
443 for (int i = 0; i < fResources.count(); i++) {
444 resourceList->push(fResources[i]);
445 fResources[i]->ref();
446 fResources[i]->getResources(resourceList);
447 }
448}
449
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000450SkTypeface* SkPDFFont::typeface() {
451 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000452}
453
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000454SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
455 return fType;
456}
457
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458bool SkPDFFont::hasGlyph(uint16_t id) {
459 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
460}
461
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000462bool SkPDFFont::multiByteGlyphs() {
463 return fMultiByteGlyphs;
464}
465
vandebo@chromium.org01294102011-02-28 19:52:18 +0000466size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
467 size_t numGlyphs) {
468 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000469 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000470 return numGlyphs;
471 }
472
473 for (size_t i = 0; i < numGlyphs; i++) {
474 if (glyphIDs[i] == 0) {
475 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000476 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000477 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
478 return i;
479 }
480 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000481 }
482
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000484}
485
486// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000487SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000488 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000489 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000490 int index;
491 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000492 canonicalFonts()[index].fFont->ref();
493 return canonicalFonts()[index].fFont;
494 }
495
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000496 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000497 SkPDFDict* fontDescriptor = NULL;
498 if (index >= 0) {
499 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
500 SkASSERT(relatedFont->fFontInfo.get());
501 fontInfo = relatedFont->fFontInfo;
502 fontDescriptor = relatedFont->fDescriptor.get();
503 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000504 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
505 info = SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo;
506 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
507 info, SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo);
508 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
509 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
510 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, info);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000511 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000512 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000513
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000514 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000515 fontDescriptor);
516 FontRec newEntry(font, fontID, font->fFirstGlyphID);
517 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000518 canonicalFonts().push(newEntry);
519 return font; // Return the reference new SkPDFFont() created.
520}
521
522// static
523SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
524 // This initialization is only thread safe with gcc.
525 static SkTDArray<FontRec> gCanonicalFonts;
526 return gCanonicalFonts;
527}
528
529// static
530SkMutex& SkPDFFont::canonicalFontsMutex() {
531 // This initialization is only thread safe with gcc.
532 static SkMutex gCanonicalFontsMutex;
533 return gCanonicalFontsMutex;
534}
535
536// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000537bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
538 // TODO(vandebo) optimize this, do only one search?
539 FontRec search(NULL, fontID, glyphID);
540 *index = canonicalFonts().find(search);
541 if (*index >= 0)
542 return true;
543 search.fGlyphID = 0;
544 *index = canonicalFonts().find(search);
545 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000546}
547
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000548SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
549 SkTypeface* typeface,
550 uint16_t glyphID,
551 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000552 SkPDFDict* fontDescriptor)
553 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000554 fTypeface(typeface),
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000555 fType(fontInfo ? fontInfo->fType :
556 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000557#ifdef SK_DEBUG
558 fDescendant(descendantFont),
559#endif
560 fMultiByteGlyphs(false),
561 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000562 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000563 fFontInfo(fontInfo),
564 fDescriptor(fontDescriptor) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000565 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000566 NOT_IMPLEMENTED(true, true);
567 fType = SkAdvancedTypefaceMetrics::kOther_Font;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000568 }
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000569 if (fType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
570 fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000571 if (descendantFont) {
572 populateCIDFont();
573 } else {
574 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000575 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000576 // No need to hold onto the font info for fonts types that
577 // support multibyte glyphs.
578 fFontInfo = NULL;
579 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000580 }
581
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000582 if (fType == SkAdvancedTypefaceMetrics::kType1_Font &&
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000583 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000584 return;
585 }
586
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000587 SkASSERT(fType == SkAdvancedTypefaceMetrics::kType1_Font ||
588 fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
589 fType == SkAdvancedTypefaceMetrics::kOther_Font ||
590 fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000591 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000592}
593
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000594void SkPDFFont::populateType0Font() {
595 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000596
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000597 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000598 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000599 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000600
601 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
602 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000603
604 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000605 fResources.push(
606 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000607 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000608 insert("DescendantFonts", descendantFonts.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000609
610 populateToUnicodeTable();
611}
612
613void SkPDFFont::populateToUnicodeTable() {
614 if (fFontInfo.get() == NULL ||
615 fFontInfo->fGlyphToUnicode.begin() == NULL) {
616 return;
617 }
618
619 SkDynamicMemoryWStream cmap;
620 append_tounicode_header(&cmap);
621 append_cmap_bfchar_sections(fFontInfo->fGlyphToUnicode, &cmap);
622 append_cmap_footer(&cmap);
623 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
624 cmapStream->unref(); // SkRefPtr and new took a reference.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000625 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000626 SkRefPtr<SkPDFStream> pdfCmap = new SkPDFStream(cmapStream.get());
627 fResources.push(pdfCmap.get()); // Pass reference from new.
628 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000629}
630
631void SkPDFFont::populateCIDFont() {
632 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000633 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000634
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000635 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000636 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000637 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000638 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000639 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000640 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000641 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000642 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000643
644 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
645 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000646 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
647 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
648 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000649 insert("CIDSystemInfo", sysInfo.get());
650
651 addFontDescriptor(0);
652
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000653 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000654 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000655 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000656 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
657 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000658 widths->unref(); // SkRefPtr and compose both took a reference.
659 if (widths->size())
660 insert("W", widths.get());
661 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000662 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000663 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000664 }
665 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000666 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000667 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000668 defaultAdvance.fVerticalAdvance = 0;
669 defaultAdvance.fOriginXDisp = 0;
670 defaultAdvance.fOriginYDisp = 0;
671 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000672 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
673 fFontInfo->fEmSize, &appendVerticalAdvance,
674 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000675 advances->unref(); // SkRefPtr and compose both took a ref.
676 if (advances->size())
677 insert("W2", advances.get());
678 if (defaultAdvance.fVerticalAdvance ||
679 defaultAdvance.fOriginXDisp ||
680 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000681 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000682 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000683 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000684 }
685 }
686}
687
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000688bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000689 SkASSERT(!fFontInfo->fVerticalMetrics.get());
690 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000691
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000692 adjustGlyphRangeForSingleByteEncoding(glyphID);
693
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000694 int16_t defaultWidth = 0;
695 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
696 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000697 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
698 widthEntry != NULL;
699 widthEntry = widthEntry->fNext.get()) {
700 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000701 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000702 defaultWidth = widthEntry->fAdvance[0];
703 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000704 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000705 SkASSERT(false);
706 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000707 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000708 SkASSERT(widthRangeEntry == NULL);
709 widthRangeEntry = widthEntry;
710 break;
711 }
712 }
713
714 if (!addFontDescriptor(defaultWidth))
715 return false;
716
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000717 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000718 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000719
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000720 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000721
722 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000723 encoding->unref(); // SkRefPtr and new both took a reference.
724 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000725
726 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
727 encDiffs->unref(); // SkRefPtr and new both took a reference.
728 encoding->insert("Differences", encDiffs.get());
729
730 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000731 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000732 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000733 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000734 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000735 }
736
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000737 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000738 fFontInfo = NULL;
739 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000740}
741
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000742void SkPDFFont::populateType3Font(int16_t glyphID) {
743 SkPaint paint;
744 paint.setTypeface(fTypeface.get());
745 paint.setTextSize(1000);
746 SkAutoGlyphCache autoCache(paint, NULL);
747 SkGlyphCache* cache = autoCache.getCache();
748 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
749 if (fLastGlyphID == 0) {
750 fLastGlyphID = cache->getGlyphCount() - 1;
751 }
752
753 adjustGlyphRangeForSingleByteEncoding(glyphID);
754
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000755 insert("Subtype", new SkPDFName("Type3"))->unref();
756 // Flip about the x-axis and scale by 1/1000.
757 SkMatrix fontMatrix;
758 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
759 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
760
761 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
762 charProcs->unref(); // SkRefPtr and new both took a reference.
763 insert("CharProcs", charProcs.get());
764
765 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
766 encoding->unref(); // SkRefPtr and new both took a reference.
767 insert("Encoding", encoding.get());
768
769 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
770 encDiffs->unref(); // SkRefPtr and new both took a reference.
771 encoding->insert("Differences", encDiffs.get());
772 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
773 encDiffs->append(new SkPDFInt(1))->unref();
774
775 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
776 widthArray->unref(); // SkRefPtr and new both took a ref.
777
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000778 SkIRect bbox = SkIRect::MakeEmpty();
779 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
780 SkString characterName;
781 characterName.printf("gid%d", gID);
782 encDiffs->append(new SkPDFName(characterName))->unref();
783
reed@google.comce11b262011-03-21 21:25:35 +0000784 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
785 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000786 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
787 glyph.fWidth, glyph.fHeight);
788 bbox.join(glyphBBox);
789
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000790 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000791 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
792 &content);
793 const SkPath* path = cache->findPath(glyph);
794 if (path) {
795 SkPDFUtils::EmitPath(*path, &content);
796 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
797 &content);
798 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000799 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000800 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000801 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000802
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000803 SkRefPtr<SkPDFStream> glyphDescription =
804 new SkPDFStream(glyphStream.get());
805 // SkRefPtr and new both ref()'d charProcs, pass one.
806 fResources.push(glyphDescription.get());
807 charProcs->insert(characterName.c_str(),
808 new SkPDFObjRef(glyphDescription.get()))->unref();
809 }
810
811 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
812 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
813 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
814 insert("Widths", widthArray.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000815 insert("CIDToGIDMap", new SkPDFName("Identity"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000816
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000817 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000818 fFontInfo = NULL;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000819
820 populateToUnicodeTable();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000821}
822
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000823bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000824 if (fDescriptor.get() != NULL) {
825 fResources.push(fDescriptor.get());
826 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000827 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000828 return true;
829 }
830
831 fDescriptor = new SkPDFDict("FontDescriptor");
832 fDescriptor->unref(); // SkRefPtr and new both took a ref.
833
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000834 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000835 case SkAdvancedTypefaceMetrics::kType1_Font: {
reed@google.comee5ee582011-04-28 14:12:48 +0000836 size_t header SK_INIT_TO_AVOID_WARNING;
837 size_t data SK_INIT_TO_AVOID_WARNING;
838 size_t trailer SK_INIT_TO_AVOID_WARNING;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000839 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000840 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000841 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
842 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
843 &data, &trailer);
844 if (fontData == NULL)
845 return false;
846 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
847 // SkRefPtr and new both ref()'d fontStream, pass one.
848 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000849 fontStream->insert("Length1", new SkPDFInt(header))->unref();
850 fontStream->insert("Length2", new SkPDFInt(data))->unref();
851 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
852 fDescriptor->insert("FontFile",
853 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000854 break;
855 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000856 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000857 SkRefPtr<SkStream> fontData =
858 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000859 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
860 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
861 // SkRefPtr and new both ref()'d fontStream, pass one.
862 fResources.push(fontStream.get());
863
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000864 fontStream->insert("Length1",
865 new SkPDFInt(fontData->getLength()))->unref();
866 fDescriptor->insert("FontFile2",
867 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000868 break;
869 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000870 case SkAdvancedTypefaceMetrics::kCFF_Font:
871 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000872 SkRefPtr<SkStream> fontData =
873 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000874 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
875 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
876 // SkRefPtr and new both ref()'d fontStream, pass one.
877 fResources.push(fontStream.get());
878
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000879 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000880 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
881 } else {
882 fontStream->insert("Subtype",
883 new SkPDFName("CIDFontType0c"))->unref();
884 }
885 fDescriptor->insert("FontFile3",
886 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000887 break;
888 }
889 default:
890 SkASSERT(false);
891 }
892
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000893 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000894 fResources.push(fDescriptor.get());
895 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000896 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000897
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000898 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000899 fFontInfo->fFontName))->unref();
900 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000901 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000902 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000903 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000904 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000905 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000906 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000907 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000908 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000909 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000910 fFontInfo->fItalicAngle))->unref();
911 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
912 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000913
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000914 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000915 fDescriptor->insert("MissingWidth", new SkPDFScalar(
916 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000917 }
918 return true;
919}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000920void SkPDFFont::addWidthInfoFromRange(
921 int16_t defaultWidth,
922 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
923 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
924 widthArray->unref(); // SkRefPtr and new both took a ref.
925 int firstChar = 0;
926 if (widthRangeEntry) {
927 const uint16_t emSize = fFontInfo->fEmSize;
928 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
929 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
930 if (startIndex < 0)
931 startIndex = 0;
932 if (endIndex > widthRangeEntry->fAdvance.count())
933 endIndex = widthRangeEntry->fAdvance.count();
934 if (widthRangeEntry->fStartId == 0) {
935 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
936 } else {
937 firstChar = startIndex + widthRangeEntry->fStartId;
938 }
939 for (int i = startIndex; i < endIndex; i++)
940 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
941 } else {
942 appendWidth(defaultWidth, 1000, widthArray.get());
943 }
944 insert("FirstChar", new SkPDFInt(firstChar))->unref();
945 insert("LastChar",
946 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
947 insert("Widths", widthArray.get());
948}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000949
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000950void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
951 // Single byte glyph encoding supports a max of 255 glyphs.
952 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
953 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
954 fLastGlyphID = fFirstGlyphID + 255 - 1;
955 }
956}
957
958
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000959bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000960 if (fFontID != b.fFontID)
961 return false;
962 if (fFont != NULL && b.fFont != NULL) {
963 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
964 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
965 }
966 if (fGlyphID == 0 || b.fGlyphID == 0)
967 return true;
968
969 if (fFont != NULL) {
970 return fFont->fFirstGlyphID <= b.fGlyphID &&
971 b.fGlyphID <= fFont->fLastGlyphID;
972 } else if (b.fFont != NULL) {
973 return b.fFont->fFirstGlyphID <= fGlyphID &&
974 fGlyphID <= b.fFont->fLastGlyphID;
975 }
976 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000977}
978
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000979SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000980 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000981 fFontID(fontID),
982 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000983}