blob: 7b952fced0dabc060ca8af23a9405dd3bf8753a3 [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000017#include <ctype.h>
18
19#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000020#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000021#include "SkPaint.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000022#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000023#include "SkPDFFont.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000024#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000026#include "SkPDFUtils.h"
27#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000028#include "SkStream.h"
29#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000030#include "SkUtils.h"
31
32namespace {
33
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000034bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
35 size_t* size) {
36 // PFB sections have a two or six bytes header. 0x80 and a one byte
37 // section type followed by a four byte section length. Type one is
38 // an ASCII section (includes a length), type two is a binary section
39 // (includes a length) and type three is an EOF marker with no length.
40 const uint8_t* buf = *src;
41 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
42 return false;
43 if (buf[1] == 3)
44 return true;
45 if (*len < 6)
46 return false;
47
48 *size = buf[2] | (buf[3] << 8) | (buf[4] << 16) | (buf[5] << 24);
49 size_t consumed = *size + 6;
50 if (consumed > *len)
51 return false;
52 *src = *src + consumed;
53 *len = *len - consumed;
54 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000055}
56
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000057bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
58 size_t* dataLen, size_t* trailerLen) {
59 const uint8_t* srcPtr = src;
60 size_t remaining = size;
61
62 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
63 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
64 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
65 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000066}
67
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000068/* The sections of a PFA file are implicitly defined. The body starts
69 * after the line containing "eexec," and the trailer starts with 512
70 * literal 0's followed by "cleartomark" (plus arbitrary white space).
71 *
72 * This function assumes that src is NUL terminated, but the NUL
73 * termination is not included in size.
74 *
75 */
76bool parsePFA(const char* src, size_t size, size_t* headerLen,
77 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
78 const char* end = src + size;
79
80 const char* dataPos = strstr(src, "eexec");
81 if (!dataPos)
82 return false;
83 dataPos += strlen("eexec");
84 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
85 dataPos < end)
86 dataPos++;
87 *headerLen = dataPos - src;
88
89 const char* trailerPos = strstr(dataPos, "cleartomark");
90 if (!trailerPos)
91 return false;
92 int zeroCount = 0;
93 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
94 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
95 continue;
96 } else if (*trailerPos == '0') {
97 zeroCount++;
98 } else {
99 return false;
100 }
101 }
102 if (zeroCount != 512)
103 return false;
104
105 *hexDataLen = trailerPos - src - *headerLen;
106 *trailerLen = size - *headerLen - *hexDataLen;
107
108 // Verify that the data section is hex encoded and count the bytes.
109 int nibbles = 0;
110 for (; dataPos < trailerPos; dataPos++) {
111 if (isspace(*dataPos))
112 continue;
113 if (!isxdigit(*dataPos))
114 return false;
115 nibbles++;
116 }
117 *dataLen = (nibbles + 1) / 2;
118
119 return true;
120}
121
122int8_t hexToBin(uint8_t c) {
123 if (!isxdigit(c))
124 return -1;
125 if (c <= '9') return c - '0';
126 if (c <= 'F') return c - 'A' + 10;
127 if (c <= 'f') return c - 'a' + 10;
128 return -1;
129}
130
131SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
132 size_t* dataLen, size_t* trailerLen) {
133 // srcStream may be backed by a file or a unseekable fd, so we may not be
134 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
135 // the input only once is doable, but very ugly. Furthermore, it'd be nice
136 // if the data was NUL terminated so that we can use strstr() to search it.
137 // Make as few copies as possible given these constraints.
138 SkDynamicMemoryWStream dynamicStream;
139 SkRefPtr<SkMemoryStream> staticStream;
140 const uint8_t* src;
141 size_t srcLen;
142 if ((srcLen = srcStream->getLength()) > 0) {
143 staticStream = new SkMemoryStream(srcLen + 1);
144 staticStream->unref(); // new and SkRefPtr both took a ref.
145 src = (const uint8_t*)staticStream->getMemoryBase();
146 if (srcStream->getMemoryBase() != NULL) {
147 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
148 } else {
149 size_t read = 0;
150 while (read < srcLen) {
151 size_t got = srcStream->read((void *)staticStream->getAtPos(),
152 srcLen - read);
153 if (got == 0)
154 return NULL;
155 read += got;
156 staticStream->seek(read);
157 }
158 }
159 ((uint8_t *)src)[srcLen] = 0;
160 } else {
161 static const size_t bufSize = 4096;
162 uint8_t buf[bufSize];
163 size_t amount;
164 while ((amount = srcStream->read(buf, bufSize)) > 0)
165 dynamicStream.write(buf, amount);
166 amount = 0;
167 dynamicStream.write(&amount, 1); // NULL terminator.
168 // getStream makes another copy, but we couldn't do any better.
169 src = (const uint8_t*)dynamicStream.getStream();
170 srcLen = dynamicStream.getOffset() - 1;
171 }
172
173 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
174 SkMemoryStream* result =
175 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
176 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
177 result->seek(*headerLen);
178 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
179 result->seek(*headerLen + *dataLen);
180 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
181 *trailerLen);
182 result->rewind();
183 return result;
184 }
185
186 // A PFA has to be converted for PDF.
187 size_t hexDataLen;
188 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
189 trailerLen)) {
190 SkMemoryStream* result =
191 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
192 memcpy((char*)result->getAtPos(), src, *headerLen);
193 result->seek(*headerLen);
194
195 const uint8_t* hexData = src + *headerLen;
196 const uint8_t* trailer = hexData + hexDataLen;
197 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000198 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000199 bool highNibble = true;
200 for (; hexData < trailer; hexData++) {
201 char curNibble = hexToBin(*hexData);
202 if (curNibble < 0)
203 continue;
204 if (highNibble) {
205 dataByte = curNibble << 4;
206 highNibble = false;
207 } else {
208 dataByte |= curNibble;
209 highNibble = true;
210 ((char *)result->getAtPos())[outputOffset++] = dataByte;
211 }
212 }
213 if (!highNibble)
214 ((char *)result->getAtPos())[outputOffset++] = dataByte;
215 SkASSERT(outputOffset == *dataLen);
216 result->seek(*headerLen + outputOffset);
217
218 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
219 *trailerLen);
220 result->rewind();
221 return result;
222 }
223
224 return NULL;
225}
226
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000227SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
228 if (emSize == 1000)
229 return SkIntToScalar(val);
230 int intVal = ((int)val) * 1000;
231 return SkIntToScalar(intVal) * SkScalarInvert(SkIntToScalar(emSize));
232}
233
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000234void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
235 SkString* content) {
236 // Specify width and bounding box for the glyph.
237 SkPDFScalar::Append(width, content);
238 content->appendf(" 0 %d %d %d %d d1\n", box.fLeft, box.fTop,
239 box.fRight, box.fBottom);
240}
241
242SkPDFArray* makeFontBBox(
243 SkIRect glyphBBox, uint16_t emSize,
244 SkPDFDevice::OriginTransform flipOrigin =
245 SkPDFDevice::kNoFlip_OriginTransform) {
246 if (flipOrigin == SkPDFDevice::kFlip_OriginTransform) {
247 int32_t temp = -glyphBBox.fTop;
248 glyphBBox.fTop = -glyphBBox.fBottom;
249 glyphBBox.fBottom = temp;
250 }
251 SkPDFArray* bbox = new SkPDFArray;
252 bbox->reserve(4);
253 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
254 emSize)))->unref();
255 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
256 emSize)))->unref();
257 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
258 emSize)))->unref();
259 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
260 emSize)))->unref();
261 return bbox;
262}
263
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000264SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
265 SkPDFArray* array) {
266 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000267 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000268}
269
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000270SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000271 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
272 uint16_t emSize, SkPDFArray* array) {
273 appendWidth(advance.fVerticalAdvance, emSize, array);
274 appendWidth(advance.fOriginXDisp, emSize, array);
275 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000276 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000277}
278
279template <typename Data>
280SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000281 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
282 uint16_t emSize,
283 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
284 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000285 Data* defaultAdvance) {
286 SkPDFArray* result = new SkPDFArray();
287 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
288 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000289 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000290 SkASSERT(advanceInfo->fAdvance.count() == 1);
291 *defaultAdvance = advanceInfo->fAdvance[0];
292 break;
293 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000294 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000295 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
296 advanceArray->unref(); // SkRefPtr and new both took a ref.
297 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000298 appendAdvance(advanceInfo->fAdvance[j], emSize,
299 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000300 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000301 result->append(advanceArray.get());
302 break;
303 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000304 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000305 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000306 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
307 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000308 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000309 break;
310 }
311 }
312 }
313 return result;
314}
315
316} // namespace
317
318/* Font subset design: It would be nice to be able to subset fonts
319 * (particularly type 3 fonts), but it's a lot of work and not a priority.
320 *
321 * Resources are canonicalized and uniqueified by pointer so there has to be
322 * some additional state indicating which subset of the font is used. It
323 * must be maintained at the page granularity and then combined at the document
324 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
325 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
326 * page/pdf device. c) in the document, retrieve the per font glyph usage
327 * from each page and combine it and ask for a resource with that subset.
328 */
329
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000330SkPDFFont::~SkPDFFont() {
331 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000332 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000333 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000334 canonicalFonts().removeShuffle(index);
335#ifdef SK_DEBUG
336 SkASSERT(!fDescendant);
337 } else {
338 SkASSERT(fDescendant);
339#endif
340 }
341 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000342}
343
344void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
345 resourceList->setReserve(resourceList->count() + fResources.count());
346 for (int i = 0; i < fResources.count(); i++) {
347 resourceList->push(fResources[i]);
348 fResources[i]->ref();
349 fResources[i]->getResources(resourceList);
350 }
351}
352
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000353SkTypeface* SkPDFFont::typeface() {
354 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000355}
356
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000357bool SkPDFFont::hasGlyph(uint16_t id) {
358 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
359}
360
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000361bool SkPDFFont::multiByteGlyphs() {
362 return fMultiByteGlyphs;
363}
364
vandebo@chromium.org01294102011-02-28 19:52:18 +0000365size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
366 size_t numGlyphs) {
367 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000368 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000369 return numGlyphs;
370 }
371
372 for (size_t i = 0; i < numGlyphs; i++) {
373 if (glyphIDs[i] == 0) {
374 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000375 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000376 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
377 return i;
378 }
379 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000380 }
381
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000382 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000383}
384
385// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000386SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000387 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000388 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000389 int index;
390 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000391 canonicalFonts()[index].fFont->ref();
392 return canonicalFonts()[index].fFont;
393 }
394
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000395 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000396 SkPDFDict* fontDescriptor = NULL;
397 if (index >= 0) {
398 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
399 SkASSERT(relatedFont->fFontInfo.get());
400 fontInfo = relatedFont->fFontInfo;
401 fontDescriptor = relatedFont->fDescriptor.get();
402 } else {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000403 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, true);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000404 fontInfo->unref(); // SkRefPtr and get info both took a reference.
405 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000406
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000407 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000408 fontDescriptor);
409 FontRec newEntry(font, fontID, font->fFirstGlyphID);
410 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000411 canonicalFonts().push(newEntry);
412 return font; // Return the reference new SkPDFFont() created.
413}
414
415// static
416SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
417 // This initialization is only thread safe with gcc.
418 static SkTDArray<FontRec> gCanonicalFonts;
419 return gCanonicalFonts;
420}
421
422// static
423SkMutex& SkPDFFont::canonicalFontsMutex() {
424 // This initialization is only thread safe with gcc.
425 static SkMutex gCanonicalFontsMutex;
426 return gCanonicalFontsMutex;
427}
428
429// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000430bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
431 // TODO(vandebo) optimize this, do only one search?
432 FontRec search(NULL, fontID, glyphID);
433 *index = canonicalFonts().find(search);
434 if (*index >= 0)
435 return true;
436 search.fGlyphID = 0;
437 *index = canonicalFonts().find(search);
438 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000439}
440
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000441SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
442 SkTypeface* typeface,
443 uint16_t glyphID,
444 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000445 SkPDFDict* fontDescriptor)
446 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000447 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448#ifdef SK_DEBUG
449 fDescendant(descendantFont),
450#endif
451 fMultiByteGlyphs(false),
452 fFirstGlyphID(1),
453 fLastGlyphID(fontInfo->fLastGlyphID),
454 fFontInfo(fontInfo),
455 fDescriptor(fontDescriptor) {
456
457 if (fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000458 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000459 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
460 }
461 if (fontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
462 fontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
463 if (descendantFont) {
464 populateCIDFont();
465 } else {
466 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000467 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000468 // No need to hold onto the font info for fonts types that
469 // support multibyte glyphs.
470 fFontInfo = NULL;
471 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000472 }
473
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000474 // Single byte glyph encoding supports a max of 255 glyphs.
475 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
476 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
477 fLastGlyphID = fFirstGlyphID + 255 - 1;
478 }
479
480 if (fontInfo->fType == SkAdvancedTypefaceMetrics::kType1_Font &&
481 populateType1Font()) {
482 return;
483 }
484
485 SkASSERT(fontInfo->fType == SkAdvancedTypefaceMetrics::kType1_Font ||
486 fontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
487 fontInfo->fType == SkAdvancedTypefaceMetrics::kOther_Font ||
488 fontInfo->fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
489 populateType3Font();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000490}
491
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000492void SkPDFFont::populateType0Font() {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000493 // TODO(vandebo) add a ToUnicode mapping.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000495
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000496 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000497 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000498 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000499
500 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
501 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000502
503 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000504 fResources.push(
505 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000506 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000507 insert("DescendantFonts", descendantFonts.get());
508}
509
510void SkPDFFont::populateCIDFont() {
511 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000512 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000513
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000514 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000515 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000516 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000517 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000518 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000519 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000520 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000521
522 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
523 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000524 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
525 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
526 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000527 insert("CIDSystemInfo", sysInfo.get());
528
529 addFontDescriptor(0);
530
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000531 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000532 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000533 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000534 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
535 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000536 widths->unref(); // SkRefPtr and compose both took a reference.
537 if (widths->size())
538 insert("W", widths.get());
539 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000540 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000541 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000542 }
543 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000544 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000545 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000546 defaultAdvance.fVerticalAdvance = 0;
547 defaultAdvance.fOriginXDisp = 0;
548 defaultAdvance.fOriginYDisp = 0;
549 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000550 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
551 fFontInfo->fEmSize, &appendVerticalAdvance,
552 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000553 advances->unref(); // SkRefPtr and compose both took a ref.
554 if (advances->size())
555 insert("W2", advances.get());
556 if (defaultAdvance.fVerticalAdvance ||
557 defaultAdvance.fOriginXDisp ||
558 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000559 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000560 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000561 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000562 }
563 }
564}
565
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000566bool SkPDFFont::populateType1Font() {
567 SkASSERT(!fFontInfo->fVerticalMetrics.get());
568 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000569
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000570 int16_t defaultWidth = 0;
571 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
572 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000573 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
574 widthEntry != NULL;
575 widthEntry = widthEntry->fNext.get()) {
576 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000577 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000578 defaultWidth = widthEntry->fAdvance[0];
579 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000580 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000581 SkASSERT(false);
582 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000583 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000584 SkASSERT(widthRangeEntry == NULL);
585 widthRangeEntry = widthEntry;
586 break;
587 }
588 }
589
590 if (!addFontDescriptor(defaultWidth))
591 return false;
592
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000593 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000594 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000595
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000596 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000597
598 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000599 encoding->unref(); // SkRefPtr and new both took a reference.
600 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601
602 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
603 encDiffs->unref(); // SkRefPtr and new both took a reference.
604 encoding->insert("Differences", encDiffs.get());
605
606 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000607 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000608 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000609 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000610 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000611 }
612
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000613 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000614 fFontInfo = NULL;
615 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000616}
617
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000618void SkPDFFont::populateType3Font() {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000619 insert("Subtype", new SkPDFName("Type3"))->unref();
620 // Flip about the x-axis and scale by 1/1000.
621 SkMatrix fontMatrix;
622 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
623 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
624
625 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
626 charProcs->unref(); // SkRefPtr and new both took a reference.
627 insert("CharProcs", charProcs.get());
628
629 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
630 encoding->unref(); // SkRefPtr and new both took a reference.
631 insert("Encoding", encoding.get());
632
633 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
634 encDiffs->unref(); // SkRefPtr and new both took a reference.
635 encoding->insert("Differences", encDiffs.get());
636 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
637 encDiffs->append(new SkPDFInt(1))->unref();
638
639 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
640 widthArray->unref(); // SkRefPtr and new both took a ref.
641
642 SkPaint paint;
643 paint.setTypeface(fTypeface.get());
644 paint.setTextSize(1000);
645 SkAutoGlyphCache autoCache(paint, NULL);
646 SkGlyphCache* cache = autoCache.getCache();
647
648 SkIRect bbox = SkIRect::MakeEmpty();
649 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
650 SkString characterName;
651 characterName.printf("gid%d", gID);
652 encDiffs->append(new SkPDFName(characterName))->unref();
653
654 const SkGlyph glyph = cache->getGlyphIDMetrics(gID);
655 appendWidth(SkFixedToFloat(glyph.fAdvanceX), 1000, widthArray.get());
656 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
657 glyph.fWidth, glyph.fHeight);
658 bbox.join(glyphBBox);
659
660 SkString content;
661 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
662 &content);
663 const SkPath* path = cache->findPath(glyph);
664 if (path) {
665 SkPDFUtils::EmitPath(*path, &content);
666 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
667 &content);
668 }
669 SkRefPtr<SkStream> glyphStream =
670 new SkMemoryStream(content.c_str(), content.size(), true);
671 glyphStream->unref(); // SkRefPtr and new both took a ref.
672 SkRefPtr<SkPDFStream> glyphDescription =
673 new SkPDFStream(glyphStream.get());
674 // SkRefPtr and new both ref()'d charProcs, pass one.
675 fResources.push(glyphDescription.get());
676 charProcs->insert(characterName.c_str(),
677 new SkPDFObjRef(glyphDescription.get()))->unref();
678 }
679
680 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
681 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
682 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
683 insert("Widths", widthArray.get());
684
685 if (fFontInfo->fLastGlyphID <= 255)
686 fFontInfo = NULL;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000687}
688
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000689bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000690 if (fDescriptor.get() != NULL) {
691 fResources.push(fDescriptor.get());
692 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000693 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000694 return true;
695 }
696
697 fDescriptor = new SkPDFDict("FontDescriptor");
698 fDescriptor->unref(); // SkRefPtr and new both took a ref.
699
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000700 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000701 case SkAdvancedTypefaceMetrics::kType1_Font: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000702 size_t header, data, trailer;
703 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000704 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000705 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
706 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
707 &data, &trailer);
708 if (fontData == NULL)
709 return false;
710 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
711 // SkRefPtr and new both ref()'d fontStream, pass one.
712 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000713 fontStream->insert("Length1", new SkPDFInt(header))->unref();
714 fontStream->insert("Length2", new SkPDFInt(data))->unref();
715 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
716 fDescriptor->insert("FontFile",
717 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000718 break;
719 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000720 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000721 SkRefPtr<SkStream> fontData =
722 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000723 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
724 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
725 // SkRefPtr and new both ref()'d fontStream, pass one.
726 fResources.push(fontStream.get());
727
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000728 fontStream->insert("Length1",
729 new SkPDFInt(fontData->getLength()))->unref();
730 fDescriptor->insert("FontFile2",
731 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000732 break;
733 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000734 case SkAdvancedTypefaceMetrics::kCFF_Font:
735 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000736 SkRefPtr<SkStream> fontData =
737 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000738 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
739 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
740 // SkRefPtr and new both ref()'d fontStream, pass one.
741 fResources.push(fontStream.get());
742
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000743 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000744 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
745 } else {
746 fontStream->insert("Subtype",
747 new SkPDFName("CIDFontType0c"))->unref();
748 }
749 fDescriptor->insert("FontFile3",
750 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000751 break;
752 }
753 default:
754 SkASSERT(false);
755 }
756
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000757 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000758 fResources.push(fDescriptor.get());
759 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000760 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000761
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000762 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000763 fFontInfo->fFontName))->unref();
764 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000765 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000766 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000767 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000768 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000769 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000770 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000771 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000772 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000773 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000774 fFontInfo->fItalicAngle))->unref();
775 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
776 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000777
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000778 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000779 fDescriptor->insert("MissingWidth", new SkPDFScalar(
780 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000781 }
782 return true;
783}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784void SkPDFFont::addWidthInfoFromRange(
785 int16_t defaultWidth,
786 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
787 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
788 widthArray->unref(); // SkRefPtr and new both took a ref.
789 int firstChar = 0;
790 if (widthRangeEntry) {
791 const uint16_t emSize = fFontInfo->fEmSize;
792 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
793 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
794 if (startIndex < 0)
795 startIndex = 0;
796 if (endIndex > widthRangeEntry->fAdvance.count())
797 endIndex = widthRangeEntry->fAdvance.count();
798 if (widthRangeEntry->fStartId == 0) {
799 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
800 } else {
801 firstChar = startIndex + widthRangeEntry->fStartId;
802 }
803 for (int i = startIndex; i < endIndex; i++)
804 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
805 } else {
806 appendWidth(defaultWidth, 1000, widthArray.get());
807 }
808 insert("FirstChar", new SkPDFInt(firstChar))->unref();
809 insert("LastChar",
810 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
811 insert("Widths", widthArray.get());
812}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000813
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000814bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000815 if (fFontID != b.fFontID)
816 return false;
817 if (fFont != NULL && b.fFont != NULL) {
818 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
819 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
820 }
821 if (fGlyphID == 0 || b.fGlyphID == 0)
822 return true;
823
824 if (fFont != NULL) {
825 return fFont->fFirstGlyphID <= b.fGlyphID &&
826 b.fGlyphID <= fFont->fLastGlyphID;
827 } else if (b.fFont != NULL) {
828 return b.fFont->fFirstGlyphID <= fGlyphID &&
829 fGlyphID <= b.fFont->fLastGlyphID;
830 }
831 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000832}
833
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000835 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000836 fFontID(fontID),
837 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000838}