blob: 7a5baf633425bcd0e410b083f80c545bd7781a21 [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"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000027#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000028#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000029#include "SkStream.h"
30#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000031#include "SkUtils.h"
32
33namespace {
34
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000035bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
36 size_t* size) {
37 // PFB sections have a two or six bytes header. 0x80 and a one byte
38 // section type followed by a four byte section length. Type one is
39 // an ASCII section (includes a length), type two is a binary section
40 // (includes a length) and type three is an EOF marker with no length.
41 const uint8_t* buf = *src;
42 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
43 return false;
44 if (buf[1] == 3)
45 return true;
46 if (*len < 6)
47 return false;
48
49 *size = buf[2] | (buf[3] << 8) | (buf[4] << 16) | (buf[5] << 24);
50 size_t consumed = *size + 6;
51 if (consumed > *len)
52 return false;
53 *src = *src + consumed;
54 *len = *len - consumed;
55 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000056}
57
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000058bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
59 size_t* dataLen, size_t* trailerLen) {
60 const uint8_t* srcPtr = src;
61 size_t remaining = size;
62
63 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
64 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
65 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
66 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000067}
68
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000069/* The sections of a PFA file are implicitly defined. The body starts
70 * after the line containing "eexec," and the trailer starts with 512
71 * literal 0's followed by "cleartomark" (plus arbitrary white space).
72 *
73 * This function assumes that src is NUL terminated, but the NUL
74 * termination is not included in size.
75 *
76 */
77bool parsePFA(const char* src, size_t size, size_t* headerLen,
78 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
79 const char* end = src + size;
80
81 const char* dataPos = strstr(src, "eexec");
82 if (!dataPos)
83 return false;
84 dataPos += strlen("eexec");
85 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
86 dataPos < end)
87 dataPos++;
88 *headerLen = dataPos - src;
89
90 const char* trailerPos = strstr(dataPos, "cleartomark");
91 if (!trailerPos)
92 return false;
93 int zeroCount = 0;
94 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
95 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
96 continue;
97 } else if (*trailerPos == '0') {
98 zeroCount++;
99 } else {
100 return false;
101 }
102 }
103 if (zeroCount != 512)
104 return false;
105
106 *hexDataLen = trailerPos - src - *headerLen;
107 *trailerLen = size - *headerLen - *hexDataLen;
108
109 // Verify that the data section is hex encoded and count the bytes.
110 int nibbles = 0;
111 for (; dataPos < trailerPos; dataPos++) {
112 if (isspace(*dataPos))
113 continue;
114 if (!isxdigit(*dataPos))
115 return false;
116 nibbles++;
117 }
118 *dataLen = (nibbles + 1) / 2;
119
120 return true;
121}
122
123int8_t hexToBin(uint8_t c) {
124 if (!isxdigit(c))
125 return -1;
126 if (c <= '9') return c - '0';
127 if (c <= 'F') return c - 'A' + 10;
128 if (c <= 'f') return c - 'a' + 10;
129 return -1;
130}
131
132SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
133 size_t* dataLen, size_t* trailerLen) {
134 // srcStream may be backed by a file or a unseekable fd, so we may not be
135 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
136 // the input only once is doable, but very ugly. Furthermore, it'd be nice
137 // if the data was NUL terminated so that we can use strstr() to search it.
138 // Make as few copies as possible given these constraints.
139 SkDynamicMemoryWStream dynamicStream;
140 SkRefPtr<SkMemoryStream> staticStream;
141 const uint8_t* src;
142 size_t srcLen;
143 if ((srcLen = srcStream->getLength()) > 0) {
144 staticStream = new SkMemoryStream(srcLen + 1);
145 staticStream->unref(); // new and SkRefPtr both took a ref.
146 src = (const uint8_t*)staticStream->getMemoryBase();
147 if (srcStream->getMemoryBase() != NULL) {
148 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
149 } else {
150 size_t read = 0;
151 while (read < srcLen) {
152 size_t got = srcStream->read((void *)staticStream->getAtPos(),
153 srcLen - read);
154 if (got == 0)
155 return NULL;
156 read += got;
157 staticStream->seek(read);
158 }
159 }
160 ((uint8_t *)src)[srcLen] = 0;
161 } else {
162 static const size_t bufSize = 4096;
163 uint8_t buf[bufSize];
164 size_t amount;
165 while ((amount = srcStream->read(buf, bufSize)) > 0)
166 dynamicStream.write(buf, amount);
167 amount = 0;
168 dynamicStream.write(&amount, 1); // NULL terminator.
169 // getStream makes another copy, but we couldn't do any better.
170 src = (const uint8_t*)dynamicStream.getStream();
171 srcLen = dynamicStream.getOffset() - 1;
172 }
173
174 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
175 SkMemoryStream* result =
176 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
177 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
178 result->seek(*headerLen);
179 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
180 result->seek(*headerLen + *dataLen);
181 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
182 *trailerLen);
183 result->rewind();
184 return result;
185 }
186
187 // A PFA has to be converted for PDF.
188 size_t hexDataLen;
189 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
190 trailerLen)) {
191 SkMemoryStream* result =
192 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
193 memcpy((char*)result->getAtPos(), src, *headerLen);
194 result->seek(*headerLen);
195
196 const uint8_t* hexData = src + *headerLen;
197 const uint8_t* trailer = hexData + hexDataLen;
198 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000199 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000200 bool highNibble = true;
201 for (; hexData < trailer; hexData++) {
202 char curNibble = hexToBin(*hexData);
203 if (curNibble < 0)
204 continue;
205 if (highNibble) {
206 dataByte = curNibble << 4;
207 highNibble = false;
208 } else {
209 dataByte |= curNibble;
210 highNibble = true;
211 ((char *)result->getAtPos())[outputOffset++] = dataByte;
212 }
213 }
214 if (!highNibble)
215 ((char *)result->getAtPos())[outputOffset++] = dataByte;
216 SkASSERT(outputOffset == *dataLen);
217 result->seek(*headerLen + outputOffset);
218
219 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
220 *trailerLen);
221 result->rewind();
222 return result;
223 }
224
225 return NULL;
226}
227
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000228SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
229 if (emSize == 1000)
230 return SkIntToScalar(val);
231 int intVal = ((int)val) * 1000;
232 return SkIntToScalar(intVal) * SkScalarInvert(SkIntToScalar(emSize));
233}
234
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000235void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
236 SkString* content) {
237 // Specify width and bounding box for the glyph.
238 SkPDFScalar::Append(width, content);
239 content->appendf(" 0 %d %d %d %d d1\n", box.fLeft, box.fTop,
240 box.fRight, box.fBottom);
241}
242
243SkPDFArray* makeFontBBox(
244 SkIRect glyphBBox, uint16_t emSize,
245 SkPDFDevice::OriginTransform flipOrigin =
246 SkPDFDevice::kNoFlip_OriginTransform) {
247 if (flipOrigin == SkPDFDevice::kFlip_OriginTransform) {
248 int32_t temp = -glyphBBox.fTop;
249 glyphBBox.fTop = -glyphBBox.fBottom;
250 glyphBBox.fBottom = temp;
251 }
252 SkPDFArray* bbox = new SkPDFArray;
253 bbox->reserve(4);
254 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
255 emSize)))->unref();
256 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
257 emSize)))->unref();
258 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
259 emSize)))->unref();
260 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
261 emSize)))->unref();
262 return bbox;
263}
264
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000265SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
266 SkPDFArray* array) {
267 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000268 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000269}
270
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000271SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000272 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
273 uint16_t emSize, SkPDFArray* array) {
274 appendWidth(advance.fVerticalAdvance, emSize, array);
275 appendWidth(advance.fOriginXDisp, emSize, array);
276 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000277 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000278}
279
280template <typename Data>
281SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000282 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
283 uint16_t emSize,
284 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
285 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000286 Data* defaultAdvance) {
287 SkPDFArray* result = new SkPDFArray();
288 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
289 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000290 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000291 SkASSERT(advanceInfo->fAdvance.count() == 1);
292 *defaultAdvance = advanceInfo->fAdvance[0];
293 break;
294 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000295 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000296 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
297 advanceArray->unref(); // SkRefPtr and new both took a ref.
298 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000299 appendAdvance(advanceInfo->fAdvance[j], emSize,
300 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000301 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000302 result->append(advanceArray.get());
303 break;
304 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000305 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000306 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000307 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
308 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000309 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000310 break;
311 }
312 }
313 }
314 return result;
315}
316
317} // namespace
318
319/* Font subset design: It would be nice to be able to subset fonts
320 * (particularly type 3 fonts), but it's a lot of work and not a priority.
321 *
322 * Resources are canonicalized and uniqueified by pointer so there has to be
323 * some additional state indicating which subset of the font is used. It
324 * must be maintained at the page granularity and then combined at the document
325 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
326 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
327 * page/pdf device. c) in the document, retrieve the per font glyph usage
328 * from each page and combine it and ask for a resource with that subset.
329 */
330
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000331SkPDFFont::~SkPDFFont() {
332 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000333 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000334 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000335 canonicalFonts().removeShuffle(index);
336#ifdef SK_DEBUG
337 SkASSERT(!fDescendant);
338 } else {
339 SkASSERT(fDescendant);
340#endif
341 }
342 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000343}
344
345void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
346 resourceList->setReserve(resourceList->count() + fResources.count());
347 for (int i = 0; i < fResources.count(); i++) {
348 resourceList->push(fResources[i]);
349 fResources[i]->ref();
350 fResources[i]->getResources(resourceList);
351 }
352}
353
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000354SkTypeface* SkPDFFont::typeface() {
355 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000356}
357
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000358bool SkPDFFont::hasGlyph(uint16_t id) {
359 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
360}
361
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000362bool SkPDFFont::multiByteGlyphs() {
363 return fMultiByteGlyphs;
364}
365
vandebo@chromium.org01294102011-02-28 19:52:18 +0000366size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
367 size_t numGlyphs) {
368 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000369 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000370 return numGlyphs;
371 }
372
373 for (size_t i = 0; i < numGlyphs; i++) {
374 if (glyphIDs[i] == 0) {
375 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000376 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000377 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
378 return i;
379 }
380 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000381 }
382
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000383 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000384}
385
386// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000387SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000388 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000389 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000390 int index;
391 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000392 canonicalFonts()[index].fFont->ref();
393 return canonicalFonts()[index].fFont;
394 }
395
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000396 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000397 SkPDFDict* fontDescriptor = NULL;
398 if (index >= 0) {
399 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
400 SkASSERT(relatedFont->fFontInfo.get());
401 fontInfo = relatedFont->fFontInfo;
402 fontDescriptor = relatedFont->fDescriptor.get();
403 } else {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000404 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, true);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000405 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000406 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000407
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000408 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000409 fontDescriptor);
410 FontRec newEntry(font, fontID, font->fFirstGlyphID);
411 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000412 canonicalFonts().push(newEntry);
413 return font; // Return the reference new SkPDFFont() created.
414}
415
416// static
417SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
418 // This initialization is only thread safe with gcc.
419 static SkTDArray<FontRec> gCanonicalFonts;
420 return gCanonicalFonts;
421}
422
423// static
424SkMutex& SkPDFFont::canonicalFontsMutex() {
425 // This initialization is only thread safe with gcc.
426 static SkMutex gCanonicalFontsMutex;
427 return gCanonicalFontsMutex;
428}
429
430// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
432 // TODO(vandebo) optimize this, do only one search?
433 FontRec search(NULL, fontID, glyphID);
434 *index = canonicalFonts().find(search);
435 if (*index >= 0)
436 return true;
437 search.fGlyphID = 0;
438 *index = canonicalFonts().find(search);
439 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000440}
441
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000442SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
443 SkTypeface* typeface,
444 uint16_t glyphID,
445 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000446 SkPDFDict* fontDescriptor)
447 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000448 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000449#ifdef SK_DEBUG
450 fDescendant(descendantFont),
451#endif
452 fMultiByteGlyphs(false),
453 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000454 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000455 fFontInfo(fontInfo),
456 fDescriptor(fontDescriptor) {
457
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000458 SkAdvancedTypefaceMetrics::FontType type;
459 if (fontInfo) {
460 type = fontInfo->fType;
461 } else {
462 type = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
463 }
464
465 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000466 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000467 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
468 }
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000469 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
470 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000471 if (descendantFont) {
472 populateCIDFont();
473 } else {
474 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000475 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000476 // No need to hold onto the font info for fonts types that
477 // support multibyte glyphs.
478 fFontInfo = NULL;
479 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000480 }
481
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000482 if (type == SkAdvancedTypefaceMetrics::kType1_Font &&
483 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000484 return;
485 }
486
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000487 SkASSERT(type == SkAdvancedTypefaceMetrics::kType1_Font ||
488 type == SkAdvancedTypefaceMetrics::kCFF_Font ||
489 type == SkAdvancedTypefaceMetrics::kOther_Font ||
490 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
491 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000492}
493
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494void SkPDFFont::populateType0Font() {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000495 // TODO(vandebo) add a ToUnicode mapping.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000496 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000497
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000498 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000499 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000500 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501
502 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
503 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000504
505 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000506 fResources.push(
507 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000508 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000509 insert("DescendantFonts", descendantFonts.get());
510}
511
512void SkPDFFont::populateCIDFont() {
513 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000514 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000515
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000516 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000517 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000518 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000519 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000520 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000521 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000522 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000523
524 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
525 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000526 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
527 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
528 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000529 insert("CIDSystemInfo", sysInfo.get());
530
531 addFontDescriptor(0);
532
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000533 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000534 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000535 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000536 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
537 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000538 widths->unref(); // SkRefPtr and compose both took a reference.
539 if (widths->size())
540 insert("W", widths.get());
541 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000542 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000543 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000544 }
545 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000546 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000547 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000548 defaultAdvance.fVerticalAdvance = 0;
549 defaultAdvance.fOriginXDisp = 0;
550 defaultAdvance.fOriginYDisp = 0;
551 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000552 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
553 fFontInfo->fEmSize, &appendVerticalAdvance,
554 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000555 advances->unref(); // SkRefPtr and compose both took a ref.
556 if (advances->size())
557 insert("W2", advances.get());
558 if (defaultAdvance.fVerticalAdvance ||
559 defaultAdvance.fOriginXDisp ||
560 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000561 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000562 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000563 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000564 }
565 }
566}
567
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000568bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000569 SkASSERT(!fFontInfo->fVerticalMetrics.get());
570 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000571
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000572 adjustGlyphRangeForSingleByteEncoding(glyphID);
573
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000574 int16_t defaultWidth = 0;
575 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
576 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000577 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
578 widthEntry != NULL;
579 widthEntry = widthEntry->fNext.get()) {
580 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000581 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000582 defaultWidth = widthEntry->fAdvance[0];
583 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000584 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000585 SkASSERT(false);
586 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000587 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000588 SkASSERT(widthRangeEntry == NULL);
589 widthRangeEntry = widthEntry;
590 break;
591 }
592 }
593
594 if (!addFontDescriptor(defaultWidth))
595 return false;
596
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000597 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000598 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000599
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000600 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601
602 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000603 encoding->unref(); // SkRefPtr and new both took a reference.
604 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000605
606 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
607 encDiffs->unref(); // SkRefPtr and new both took a reference.
608 encoding->insert("Differences", encDiffs.get());
609
610 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000611 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000612 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000613 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000614 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000615 }
616
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000617 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000618 fFontInfo = NULL;
619 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000620}
621
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000622void SkPDFFont::populateType3Font(int16_t glyphID) {
623 SkPaint paint;
624 paint.setTypeface(fTypeface.get());
625 paint.setTextSize(1000);
626 SkAutoGlyphCache autoCache(paint, NULL);
627 SkGlyphCache* cache = autoCache.getCache();
628 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
629 if (fLastGlyphID == 0) {
630 fLastGlyphID = cache->getGlyphCount() - 1;
631 }
632
633 adjustGlyphRangeForSingleByteEncoding(glyphID);
634
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000635 insert("Subtype", new SkPDFName("Type3"))->unref();
636 // Flip about the x-axis and scale by 1/1000.
637 SkMatrix fontMatrix;
638 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
639 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
640
641 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
642 charProcs->unref(); // SkRefPtr and new both took a reference.
643 insert("CharProcs", charProcs.get());
644
645 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
646 encoding->unref(); // SkRefPtr and new both took a reference.
647 insert("Encoding", encoding.get());
648
649 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
650 encDiffs->unref(); // SkRefPtr and new both took a reference.
651 encoding->insert("Differences", encDiffs.get());
652 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
653 encDiffs->append(new SkPDFInt(1))->unref();
654
655 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
656 widthArray->unref(); // SkRefPtr and new both took a ref.
657
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000658 SkIRect bbox = SkIRect::MakeEmpty();
659 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
660 SkString characterName;
661 characterName.printf("gid%d", gID);
662 encDiffs->append(new SkPDFName(characterName))->unref();
663
664 const SkGlyph glyph = cache->getGlyphIDMetrics(gID);
665 appendWidth(SkFixedToFloat(glyph.fAdvanceX), 1000, widthArray.get());
666 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
667 glyph.fWidth, glyph.fHeight);
668 bbox.join(glyphBBox);
669
670 SkString content;
671 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
672 &content);
673 const SkPath* path = cache->findPath(glyph);
674 if (path) {
675 SkPDFUtils::EmitPath(*path, &content);
676 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
677 &content);
678 }
679 SkRefPtr<SkStream> glyphStream =
680 new SkMemoryStream(content.c_str(), content.size(), true);
681 glyphStream->unref(); // SkRefPtr and new both took a ref.
682 SkRefPtr<SkPDFStream> glyphDescription =
683 new SkPDFStream(glyphStream.get());
684 // SkRefPtr and new both ref()'d charProcs, pass one.
685 fResources.push(glyphDescription.get());
686 charProcs->insert(characterName.c_str(),
687 new SkPDFObjRef(glyphDescription.get()))->unref();
688 }
689
690 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
691 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
692 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
693 insert("Widths", widthArray.get());
694
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000695 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000696 fFontInfo = NULL;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000697}
698
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000699bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000700 if (fDescriptor.get() != NULL) {
701 fResources.push(fDescriptor.get());
702 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000703 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000704 return true;
705 }
706
707 fDescriptor = new SkPDFDict("FontDescriptor");
708 fDescriptor->unref(); // SkRefPtr and new both took a ref.
709
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000710 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000711 case SkAdvancedTypefaceMetrics::kType1_Font: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000712 size_t header, data, trailer;
713 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000714 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000715 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
716 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
717 &data, &trailer);
718 if (fontData == NULL)
719 return false;
720 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
721 // SkRefPtr and new both ref()'d fontStream, pass one.
722 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000723 fontStream->insert("Length1", new SkPDFInt(header))->unref();
724 fontStream->insert("Length2", new SkPDFInt(data))->unref();
725 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
726 fDescriptor->insert("FontFile",
727 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000728 break;
729 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000730 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000731 SkRefPtr<SkStream> fontData =
732 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000733 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
734 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
735 // SkRefPtr and new both ref()'d fontStream, pass one.
736 fResources.push(fontStream.get());
737
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000738 fontStream->insert("Length1",
739 new SkPDFInt(fontData->getLength()))->unref();
740 fDescriptor->insert("FontFile2",
741 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000742 break;
743 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000744 case SkAdvancedTypefaceMetrics::kCFF_Font:
745 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000746 SkRefPtr<SkStream> fontData =
747 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000748 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
749 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
750 // SkRefPtr and new both ref()'d fontStream, pass one.
751 fResources.push(fontStream.get());
752
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000753 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000754 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
755 } else {
756 fontStream->insert("Subtype",
757 new SkPDFName("CIDFontType0c"))->unref();
758 }
759 fDescriptor->insert("FontFile3",
760 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000761 break;
762 }
763 default:
764 SkASSERT(false);
765 }
766
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000767 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000768 fResources.push(fDescriptor.get());
769 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000770 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000771
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000772 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000773 fFontInfo->fFontName))->unref();
774 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000775 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000776 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000777 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000778 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000779 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000780 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000781 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000782 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000783 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784 fFontInfo->fItalicAngle))->unref();
785 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
786 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000787
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000788 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000789 fDescriptor->insert("MissingWidth", new SkPDFScalar(
790 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000791 }
792 return true;
793}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000794void SkPDFFont::addWidthInfoFromRange(
795 int16_t defaultWidth,
796 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
797 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
798 widthArray->unref(); // SkRefPtr and new both took a ref.
799 int firstChar = 0;
800 if (widthRangeEntry) {
801 const uint16_t emSize = fFontInfo->fEmSize;
802 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
803 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
804 if (startIndex < 0)
805 startIndex = 0;
806 if (endIndex > widthRangeEntry->fAdvance.count())
807 endIndex = widthRangeEntry->fAdvance.count();
808 if (widthRangeEntry->fStartId == 0) {
809 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
810 } else {
811 firstChar = startIndex + widthRangeEntry->fStartId;
812 }
813 for (int i = startIndex; i < endIndex; i++)
814 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
815 } else {
816 appendWidth(defaultWidth, 1000, widthArray.get());
817 }
818 insert("FirstChar", new SkPDFInt(firstChar))->unref();
819 insert("LastChar",
820 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
821 insert("Widths", widthArray.get());
822}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000823
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000824void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
825 // Single byte glyph encoding supports a max of 255 glyphs.
826 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
827 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
828 fLastGlyphID = fFirstGlyphID + 255 - 1;
829 }
830}
831
832
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000833bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834 if (fFontID != b.fFontID)
835 return false;
836 if (fFont != NULL && b.fFont != NULL) {
837 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
838 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
839 }
840 if (fGlyphID == 0 || b.fGlyphID == 0)
841 return true;
842
843 if (fFont != NULL) {
844 return fFont->fFirstGlyphID <= b.fGlyphID &&
845 b.fGlyphID <= fFont->fLastGlyphID;
846 } else if (b.fFont != NULL) {
847 return b.fFont->fFirstGlyphID <= fGlyphID &&
848 fGlyphID <= b.fFont->fLastGlyphID;
849 }
850 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000851}
852
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000853SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000854 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000855 fFontID(fontID),
856 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000857}