blob: 3e363f7a90834d80b8054130185f368cc7a4ed99 [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.org325cb9a2011-03-30 18:36:29 +000031#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000032#include "SkUtils.h"
33
34namespace {
35
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000036bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
37 size_t* size) {
38 // PFB sections have a two or six bytes header. 0x80 and a one byte
39 // section type followed by a four byte section length. Type one is
40 // an ASCII section (includes a length), type two is a binary section
41 // (includes a length) and type three is an EOF marker with no length.
42 const uint8_t* buf = *src;
43 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType)
44 return false;
45 if (buf[1] == 3)
46 return true;
47 if (*len < 6)
48 return false;
49
50 *size = buf[2] | (buf[3] << 8) | (buf[4] << 16) | (buf[5] << 24);
51 size_t consumed = *size + 6;
52 if (consumed > *len)
53 return false;
54 *src = *src + consumed;
55 *len = *len - consumed;
56 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000057}
58
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000059bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
60 size_t* dataLen, size_t* trailerLen) {
61 const uint8_t* srcPtr = src;
62 size_t remaining = size;
63
64 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
65 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
66 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
67 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000068}
69
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000070/* The sections of a PFA file are implicitly defined. The body starts
71 * after the line containing "eexec," and the trailer starts with 512
72 * literal 0's followed by "cleartomark" (plus arbitrary white space).
73 *
74 * This function assumes that src is NUL terminated, but the NUL
75 * termination is not included in size.
76 *
77 */
78bool parsePFA(const char* src, size_t size, size_t* headerLen,
79 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
80 const char* end = src + size;
81
82 const char* dataPos = strstr(src, "eexec");
83 if (!dataPos)
84 return false;
85 dataPos += strlen("eexec");
86 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
87 dataPos < end)
88 dataPos++;
89 *headerLen = dataPos - src;
90
91 const char* trailerPos = strstr(dataPos, "cleartomark");
92 if (!trailerPos)
93 return false;
94 int zeroCount = 0;
95 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
96 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
97 continue;
98 } else if (*trailerPos == '0') {
99 zeroCount++;
100 } else {
101 return false;
102 }
103 }
104 if (zeroCount != 512)
105 return false;
106
107 *hexDataLen = trailerPos - src - *headerLen;
108 *trailerLen = size - *headerLen - *hexDataLen;
109
110 // Verify that the data section is hex encoded and count the bytes.
111 int nibbles = 0;
112 for (; dataPos < trailerPos; dataPos++) {
113 if (isspace(*dataPos))
114 continue;
115 if (!isxdigit(*dataPos))
116 return false;
117 nibbles++;
118 }
119 *dataLen = (nibbles + 1) / 2;
120
121 return true;
122}
123
124int8_t hexToBin(uint8_t c) {
125 if (!isxdigit(c))
126 return -1;
127 if (c <= '9') return c - '0';
128 if (c <= 'F') return c - 'A' + 10;
129 if (c <= 'f') return c - 'a' + 10;
130 return -1;
131}
132
133SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
134 size_t* dataLen, size_t* trailerLen) {
135 // srcStream may be backed by a file or a unseekable fd, so we may not be
136 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
137 // the input only once is doable, but very ugly. Furthermore, it'd be nice
138 // if the data was NUL terminated so that we can use strstr() to search it.
139 // Make as few copies as possible given these constraints.
140 SkDynamicMemoryWStream dynamicStream;
141 SkRefPtr<SkMemoryStream> staticStream;
142 const uint8_t* src;
143 size_t srcLen;
144 if ((srcLen = srcStream->getLength()) > 0) {
145 staticStream = new SkMemoryStream(srcLen + 1);
146 staticStream->unref(); // new and SkRefPtr both took a ref.
147 src = (const uint8_t*)staticStream->getMemoryBase();
148 if (srcStream->getMemoryBase() != NULL) {
149 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
150 } else {
151 size_t read = 0;
152 while (read < srcLen) {
153 size_t got = srcStream->read((void *)staticStream->getAtPos(),
154 srcLen - read);
155 if (got == 0)
156 return NULL;
157 read += got;
158 staticStream->seek(read);
159 }
160 }
161 ((uint8_t *)src)[srcLen] = 0;
162 } else {
163 static const size_t bufSize = 4096;
164 uint8_t buf[bufSize];
165 size_t amount;
166 while ((amount = srcStream->read(buf, bufSize)) > 0)
167 dynamicStream.write(buf, amount);
168 amount = 0;
169 dynamicStream.write(&amount, 1); // NULL terminator.
170 // getStream makes another copy, but we couldn't do any better.
171 src = (const uint8_t*)dynamicStream.getStream();
172 srcLen = dynamicStream.getOffset() - 1;
173 }
174
175 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
176 SkMemoryStream* result =
177 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
178 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
179 result->seek(*headerLen);
180 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
181 result->seek(*headerLen + *dataLen);
182 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
183 *trailerLen);
184 result->rewind();
185 return result;
186 }
187
188 // A PFA has to be converted for PDF.
189 size_t hexDataLen;
190 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
191 trailerLen)) {
192 SkMemoryStream* result =
193 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
194 memcpy((char*)result->getAtPos(), src, *headerLen);
195 result->seek(*headerLen);
196
197 const uint8_t* hexData = src + *headerLen;
198 const uint8_t* trailer = hexData + hexDataLen;
199 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000200 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000201 bool highNibble = true;
202 for (; hexData < trailer; hexData++) {
203 char curNibble = hexToBin(*hexData);
204 if (curNibble < 0)
205 continue;
206 if (highNibble) {
207 dataByte = curNibble << 4;
208 highNibble = false;
209 } else {
210 dataByte |= curNibble;
211 highNibble = true;
212 ((char *)result->getAtPos())[outputOffset++] = dataByte;
213 }
214 }
215 if (!highNibble)
216 ((char *)result->getAtPos())[outputOffset++] = dataByte;
217 SkASSERT(outputOffset == *dataLen);
218 result->seek(*headerLen + outputOffset);
219
220 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
221 *trailerLen);
222 result->rewind();
223 return result;
224 }
225
226 return NULL;
227}
228
reed@google.com3f0dcf92011-03-18 21:23:45 +0000229// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000230SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000231 SkScalar scaled = SkIntToScalar(val);
232 if (emSize == 1000) {
233 return scaled;
234 } else {
235 return SkScalarMulDiv(scaled, 1000, emSize);
236 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000237}
238
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000239void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000240 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000241 // Specify width and bounding box for the glyph.
242 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000243 content->writeText(" 0 ");
244 content->writeDecAsText(box.fLeft);
245 content->writeText(" ");
246 content->writeDecAsText(box.fTop);
247 content->writeText(" ");
248 content->writeDecAsText(box.fRight);
249 content->writeText(" ");
250 content->writeDecAsText(box.fBottom);
251 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000252}
253
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000254SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000255 SkPDFArray* bbox = new SkPDFArray;
256 bbox->reserve(4);
257 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
258 emSize)))->unref();
259 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
260 emSize)))->unref();
261 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
262 emSize)))->unref();
263 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
264 emSize)))->unref();
265 return bbox;
266}
267
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000268SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
269 SkPDFArray* array) {
270 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000271 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000272}
273
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000274SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000275 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
276 uint16_t emSize, SkPDFArray* array) {
277 appendWidth(advance.fVerticalAdvance, emSize, array);
278 appendWidth(advance.fOriginXDisp, emSize, array);
279 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000280 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000281}
282
283template <typename Data>
284SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000285 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
286 uint16_t emSize,
287 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
288 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000289 Data* defaultAdvance) {
290 SkPDFArray* result = new SkPDFArray();
291 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
292 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000293 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000294 SkASSERT(advanceInfo->fAdvance.count() == 1);
295 *defaultAdvance = advanceInfo->fAdvance[0];
296 break;
297 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000298 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000299 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
300 advanceArray->unref(); // SkRefPtr and new both took a ref.
301 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000302 appendAdvance(advanceInfo->fAdvance[j], emSize,
303 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000304 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000305 result->append(advanceArray.get());
306 break;
307 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000308 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000309 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000310 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
311 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000312 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000313 break;
314 }
315 }
316 }
317 return result;
318}
319
320} // namespace
321
322/* Font subset design: It would be nice to be able to subset fonts
323 * (particularly type 3 fonts), but it's a lot of work and not a priority.
324 *
325 * Resources are canonicalized and uniqueified by pointer so there has to be
326 * some additional state indicating which subset of the font is used. It
327 * must be maintained at the page granularity and then combined at the document
328 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
329 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
330 * page/pdf device. c) in the document, retrieve the per font glyph usage
331 * from each page and combine it and ask for a resource with that subset.
332 */
333
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000334SkPDFFont::~SkPDFFont() {
335 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000336 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000337 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000338 canonicalFonts().removeShuffle(index);
339#ifdef SK_DEBUG
340 SkASSERT(!fDescendant);
341 } else {
342 SkASSERT(fDescendant);
343#endif
344 }
345 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000346}
347
348void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
349 resourceList->setReserve(resourceList->count() + fResources.count());
350 for (int i = 0; i < fResources.count(); i++) {
351 resourceList->push(fResources[i]);
352 fResources[i]->ref();
353 fResources[i]->getResources(resourceList);
354 }
355}
356
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000357SkTypeface* SkPDFFont::typeface() {
358 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000359}
360
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000361bool SkPDFFont::hasGlyph(uint16_t id) {
362 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
363}
364
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000365bool SkPDFFont::multiByteGlyphs() {
366 return fMultiByteGlyphs;
367}
368
vandebo@chromium.org01294102011-02-28 19:52:18 +0000369size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
370 size_t numGlyphs) {
371 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000372 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000373 return numGlyphs;
374 }
375
376 for (size_t i = 0; i < numGlyphs; i++) {
377 if (glyphIDs[i] == 0) {
378 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000379 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000380 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
381 return i;
382 }
383 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000384 }
385
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000386 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000387}
388
389// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000390SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000391 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000392 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000393 int index;
394 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000395 canonicalFonts()[index].fFont->ref();
396 return canonicalFonts()[index].fFont;
397 }
398
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000399 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000400 SkPDFDict* fontDescriptor = NULL;
401 if (index >= 0) {
402 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
403 SkASSERT(relatedFont->fFontInfo.get());
404 fontInfo = relatedFont->fFontInfo;
405 fontDescriptor = relatedFont->fDescriptor.get();
406 } else {
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000407 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, SkTBitOr(
408 SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo,
409 SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo));
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000410 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000411 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000412
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000413 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000414 fontDescriptor);
415 FontRec newEntry(font, fontID, font->fFirstGlyphID);
416 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000417 canonicalFonts().push(newEntry);
418 return font; // Return the reference new SkPDFFont() created.
419}
420
421// static
422SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
423 // This initialization is only thread safe with gcc.
424 static SkTDArray<FontRec> gCanonicalFonts;
425 return gCanonicalFonts;
426}
427
428// static
429SkMutex& SkPDFFont::canonicalFontsMutex() {
430 // This initialization is only thread safe with gcc.
431 static SkMutex gCanonicalFontsMutex;
432 return gCanonicalFontsMutex;
433}
434
435// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000436bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
437 // TODO(vandebo) optimize this, do only one search?
438 FontRec search(NULL, fontID, glyphID);
439 *index = canonicalFonts().find(search);
440 if (*index >= 0)
441 return true;
442 search.fGlyphID = 0;
443 *index = canonicalFonts().find(search);
444 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000445}
446
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000447SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
448 SkTypeface* typeface,
449 uint16_t glyphID,
450 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000451 SkPDFDict* fontDescriptor)
452 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000453 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000454#ifdef SK_DEBUG
455 fDescendant(descendantFont),
456#endif
457 fMultiByteGlyphs(false),
458 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000459 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 fFontInfo(fontInfo),
461 fDescriptor(fontDescriptor) {
462
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000463 SkAdvancedTypefaceMetrics::FontType type;
464 if (fontInfo) {
465 type = fontInfo->fType;
466 } else {
467 type = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
468 }
469
470 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000471 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000472 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
473 }
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000474 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
475 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000476 if (descendantFont) {
477 populateCIDFont();
478 } else {
479 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000480 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000481 // No need to hold onto the font info for fonts types that
482 // support multibyte glyphs.
483 fFontInfo = NULL;
484 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000485 }
486
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000487 if (type == SkAdvancedTypefaceMetrics::kType1_Font &&
488 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000489 return;
490 }
491
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000492 SkASSERT(type == SkAdvancedTypefaceMetrics::kType1_Font ||
493 type == SkAdvancedTypefaceMetrics::kCFF_Font ||
494 type == SkAdvancedTypefaceMetrics::kOther_Font ||
495 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
496 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000497}
498
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000499void SkPDFFont::populateType0Font() {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000500 // TODO(vandebo) add a ToUnicode mapping.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000502
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000503 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000504 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000505 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000506
507 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
508 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000509
510 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000511 fResources.push(
512 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000513 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000514 insert("DescendantFonts", descendantFonts.get());
515}
516
517void SkPDFFont::populateCIDFont() {
518 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000519 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000520
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000521 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000522 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000523 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000524 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000525 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000526 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000527 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000528
529 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
530 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000531 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
532 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
533 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000534 insert("CIDSystemInfo", sysInfo.get());
535
536 addFontDescriptor(0);
537
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000538 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000539 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000540 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000541 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
542 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000543 widths->unref(); // SkRefPtr and compose both took a reference.
544 if (widths->size())
545 insert("W", widths.get());
546 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000547 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000548 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000549 }
550 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000551 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000552 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000553 defaultAdvance.fVerticalAdvance = 0;
554 defaultAdvance.fOriginXDisp = 0;
555 defaultAdvance.fOriginYDisp = 0;
556 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000557 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
558 fFontInfo->fEmSize, &appendVerticalAdvance,
559 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000560 advances->unref(); // SkRefPtr and compose both took a ref.
561 if (advances->size())
562 insert("W2", advances.get());
563 if (defaultAdvance.fVerticalAdvance ||
564 defaultAdvance.fOriginXDisp ||
565 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000566 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000567 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000568 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000569 }
570 }
571}
572
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000573bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000574 SkASSERT(!fFontInfo->fVerticalMetrics.get());
575 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000576
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000577 adjustGlyphRangeForSingleByteEncoding(glyphID);
578
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000579 int16_t defaultWidth = 0;
580 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
581 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000582 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
583 widthEntry != NULL;
584 widthEntry = widthEntry->fNext.get()) {
585 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000586 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000587 defaultWidth = widthEntry->fAdvance[0];
588 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000589 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000590 SkASSERT(false);
591 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000592 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000593 SkASSERT(widthRangeEntry == NULL);
594 widthRangeEntry = widthEntry;
595 break;
596 }
597 }
598
599 if (!addFontDescriptor(defaultWidth))
600 return false;
601
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000602 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000603 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000604
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000605 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000606
607 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000608 encoding->unref(); // SkRefPtr and new both took a reference.
609 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000610
611 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
612 encDiffs->unref(); // SkRefPtr and new both took a reference.
613 encoding->insert("Differences", encDiffs.get());
614
615 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000616 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000617 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000618 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000619 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000620 }
621
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000622 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000623 fFontInfo = NULL;
624 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000625}
626
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000627void SkPDFFont::populateType3Font(int16_t glyphID) {
628 SkPaint paint;
629 paint.setTypeface(fTypeface.get());
630 paint.setTextSize(1000);
631 SkAutoGlyphCache autoCache(paint, NULL);
632 SkGlyphCache* cache = autoCache.getCache();
633 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
634 if (fLastGlyphID == 0) {
635 fLastGlyphID = cache->getGlyphCount() - 1;
636 }
637
638 adjustGlyphRangeForSingleByteEncoding(glyphID);
639
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000640 insert("Subtype", new SkPDFName("Type3"))->unref();
641 // Flip about the x-axis and scale by 1/1000.
642 SkMatrix fontMatrix;
643 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
644 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
645
646 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
647 charProcs->unref(); // SkRefPtr and new both took a reference.
648 insert("CharProcs", charProcs.get());
649
650 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
651 encoding->unref(); // SkRefPtr and new both took a reference.
652 insert("Encoding", encoding.get());
653
654 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
655 encDiffs->unref(); // SkRefPtr and new both took a reference.
656 encoding->insert("Differences", encDiffs.get());
657 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
658 encDiffs->append(new SkPDFInt(1))->unref();
659
660 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
661 widthArray->unref(); // SkRefPtr and new both took a ref.
662
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000663 SkIRect bbox = SkIRect::MakeEmpty();
664 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
665 SkString characterName;
666 characterName.printf("gid%d", gID);
667 encDiffs->append(new SkPDFName(characterName))->unref();
668
reed@google.comce11b262011-03-21 21:25:35 +0000669 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
670 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000671 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
672 glyph.fWidth, glyph.fHeight);
673 bbox.join(glyphBBox);
674
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000675 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000676 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
677 &content);
678 const SkPath* path = cache->findPath(glyph);
679 if (path) {
680 SkPDFUtils::EmitPath(*path, &content);
681 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
682 &content);
683 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000684 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000685 glyphStream->unref(); // SkRefPtr and new both took a ref.
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000686 glyphStream->setMemoryOwned(content.detach(), content.getOffset());
687
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000688 SkRefPtr<SkPDFStream> glyphDescription =
689 new SkPDFStream(glyphStream.get());
690 // SkRefPtr and new both ref()'d charProcs, pass one.
691 fResources.push(glyphDescription.get());
692 charProcs->insert(characterName.c_str(),
693 new SkPDFObjRef(glyphDescription.get()))->unref();
694 }
695
696 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
697 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
698 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
699 insert("Widths", widthArray.get());
700
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000701 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000702 fFontInfo = NULL;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000703}
704
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000705bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000706 if (fDescriptor.get() != NULL) {
707 fResources.push(fDescriptor.get());
708 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000709 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000710 return true;
711 }
712
713 fDescriptor = new SkPDFDict("FontDescriptor");
714 fDescriptor->unref(); // SkRefPtr and new both took a ref.
715
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000716 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000717 case SkAdvancedTypefaceMetrics::kType1_Font: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000718 size_t header, data, trailer;
719 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000720 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000721 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
722 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
723 &data, &trailer);
724 if (fontData == NULL)
725 return false;
726 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
727 // SkRefPtr and new both ref()'d fontStream, pass one.
728 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000729 fontStream->insert("Length1", new SkPDFInt(header))->unref();
730 fontStream->insert("Length2", new SkPDFInt(data))->unref();
731 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
732 fDescriptor->insert("FontFile",
733 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000734 break;
735 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000736 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000737 SkRefPtr<SkStream> fontData =
738 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000739 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
740 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
741 // SkRefPtr and new both ref()'d fontStream, pass one.
742 fResources.push(fontStream.get());
743
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000744 fontStream->insert("Length1",
745 new SkPDFInt(fontData->getLength()))->unref();
746 fDescriptor->insert("FontFile2",
747 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000748 break;
749 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000750 case SkAdvancedTypefaceMetrics::kCFF_Font:
751 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000752 SkRefPtr<SkStream> fontData =
753 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000754 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
755 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
756 // SkRefPtr and new both ref()'d fontStream, pass one.
757 fResources.push(fontStream.get());
758
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000759 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000760 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
761 } else {
762 fontStream->insert("Subtype",
763 new SkPDFName("CIDFontType0c"))->unref();
764 }
765 fDescriptor->insert("FontFile3",
766 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000767 break;
768 }
769 default:
770 SkASSERT(false);
771 }
772
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000773 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000774 fResources.push(fDescriptor.get());
775 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000776 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000777
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000778 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000779 fFontInfo->fFontName))->unref();
780 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000781 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000782 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000783 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000785 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000786 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000787 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000788 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000789 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000790 fFontInfo->fItalicAngle))->unref();
791 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
792 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000793
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000794 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000795 fDescriptor->insert("MissingWidth", new SkPDFScalar(
796 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000797 }
798 return true;
799}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000800void SkPDFFont::addWidthInfoFromRange(
801 int16_t defaultWidth,
802 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
803 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
804 widthArray->unref(); // SkRefPtr and new both took a ref.
805 int firstChar = 0;
806 if (widthRangeEntry) {
807 const uint16_t emSize = fFontInfo->fEmSize;
808 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
809 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
810 if (startIndex < 0)
811 startIndex = 0;
812 if (endIndex > widthRangeEntry->fAdvance.count())
813 endIndex = widthRangeEntry->fAdvance.count();
814 if (widthRangeEntry->fStartId == 0) {
815 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
816 } else {
817 firstChar = startIndex + widthRangeEntry->fStartId;
818 }
819 for (int i = startIndex; i < endIndex; i++)
820 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
821 } else {
822 appendWidth(defaultWidth, 1000, widthArray.get());
823 }
824 insert("FirstChar", new SkPDFInt(firstChar))->unref();
825 insert("LastChar",
826 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
827 insert("Widths", widthArray.get());
828}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000829
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000830void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
831 // Single byte glyph encoding supports a max of 255 glyphs.
832 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
833 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
834 fLastGlyphID = fFirstGlyphID + 255 - 1;
835 }
836}
837
838
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000839bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000840 if (fFontID != b.fFontID)
841 return false;
842 if (fFont != NULL && b.fFont != NULL) {
843 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
844 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
845 }
846 if (fGlyphID == 0 || b.fGlyphID == 0)
847 return true;
848
849 if (fFont != NULL) {
850 return fFont->fFirstGlyphID <= b.fGlyphID &&
851 b.fGlyphID <= fFont->fLastGlyphID;
852 } else if (b.fFont != NULL) {
853 return b.fFont->fFirstGlyphID <= fGlyphID &&
854 fGlyphID <= b.fFont->fLastGlyphID;
855 }
856 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000857}
858
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000859SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000860 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000861 fFontID(fontID),
862 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000863}