blob: 83caea57092db824927a71cca0ee196fc21f4724 [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
reed@google.com3f0dcf92011-03-18 21:23:45 +0000228// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000229SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000230 SkScalar scaled = SkIntToScalar(val);
231 if (emSize == 1000) {
232 return scaled;
233 } else {
234 return SkScalarMulDiv(scaled, 1000, emSize);
235 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000236}
237
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000238void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000239 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000240 // Specify width and bounding box for the glyph.
241 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000242 content->writeText(" 0 ");
243 content->writeDecAsText(box.fLeft);
244 content->writeText(" ");
245 content->writeDecAsText(box.fTop);
246 content->writeText(" ");
247 content->writeDecAsText(box.fRight);
248 content->writeText(" ");
249 content->writeDecAsText(box.fBottom);
250 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000251}
252
253SkPDFArray* makeFontBBox(
254 SkIRect glyphBBox, uint16_t emSize,
255 SkPDFDevice::OriginTransform flipOrigin =
256 SkPDFDevice::kNoFlip_OriginTransform) {
257 if (flipOrigin == SkPDFDevice::kFlip_OriginTransform) {
258 int32_t temp = -glyphBBox.fTop;
259 glyphBBox.fTop = -glyphBBox.fBottom;
260 glyphBBox.fBottom = temp;
261 }
262 SkPDFArray* bbox = new SkPDFArray;
263 bbox->reserve(4);
264 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
265 emSize)))->unref();
266 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
267 emSize)))->unref();
268 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
269 emSize)))->unref();
270 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
271 emSize)))->unref();
272 return bbox;
273}
274
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000275SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
276 SkPDFArray* array) {
277 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000278 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000279}
280
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000281SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000282 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
283 uint16_t emSize, SkPDFArray* array) {
284 appendWidth(advance.fVerticalAdvance, emSize, array);
285 appendWidth(advance.fOriginXDisp, emSize, array);
286 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000287 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000288}
289
290template <typename Data>
291SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000292 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
293 uint16_t emSize,
294 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
295 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000296 Data* defaultAdvance) {
297 SkPDFArray* result = new SkPDFArray();
298 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
299 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000300 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000301 SkASSERT(advanceInfo->fAdvance.count() == 1);
302 *defaultAdvance = advanceInfo->fAdvance[0];
303 break;
304 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000305 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000306 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
307 advanceArray->unref(); // SkRefPtr and new both took a ref.
308 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000309 appendAdvance(advanceInfo->fAdvance[j], emSize,
310 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000311 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000312 result->append(advanceArray.get());
313 break;
314 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000315 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000316 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000317 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
318 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000319 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000320 break;
321 }
322 }
323 }
324 return result;
325}
326
327} // namespace
328
329/* Font subset design: It would be nice to be able to subset fonts
330 * (particularly type 3 fonts), but it's a lot of work and not a priority.
331 *
332 * Resources are canonicalized and uniqueified by pointer so there has to be
333 * some additional state indicating which subset of the font is used. It
334 * must be maintained at the page granularity and then combined at the document
335 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
336 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
337 * page/pdf device. c) in the document, retrieve the per font glyph usage
338 * from each page and combine it and ask for a resource with that subset.
339 */
340
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000341SkPDFFont::~SkPDFFont() {
342 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000343 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000344 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000345 canonicalFonts().removeShuffle(index);
346#ifdef SK_DEBUG
347 SkASSERT(!fDescendant);
348 } else {
349 SkASSERT(fDescendant);
350#endif
351 }
352 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000353}
354
355void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
356 resourceList->setReserve(resourceList->count() + fResources.count());
357 for (int i = 0; i < fResources.count(); i++) {
358 resourceList->push(fResources[i]);
359 fResources[i]->ref();
360 fResources[i]->getResources(resourceList);
361 }
362}
363
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000364SkTypeface* SkPDFFont::typeface() {
365 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000366}
367
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000368bool SkPDFFont::hasGlyph(uint16_t id) {
369 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
370}
371
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000372bool SkPDFFont::multiByteGlyphs() {
373 return fMultiByteGlyphs;
374}
375
vandebo@chromium.org01294102011-02-28 19:52:18 +0000376size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
377 size_t numGlyphs) {
378 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000379 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000380 return numGlyphs;
381 }
382
383 for (size_t i = 0; i < numGlyphs; i++) {
384 if (glyphIDs[i] == 0) {
385 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000386 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000387 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
388 return i;
389 }
390 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000391 }
392
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000393 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000394}
395
396// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000397SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000398 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000399 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000400 int index;
401 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000402 canonicalFonts()[index].fFont->ref();
403 return canonicalFonts()[index].fFont;
404 }
405
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000406 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000407 SkPDFDict* fontDescriptor = NULL;
408 if (index >= 0) {
409 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
410 SkASSERT(relatedFont->fFontInfo.get());
411 fontInfo = relatedFont->fFontInfo;
412 fontDescriptor = relatedFont->fDescriptor.get();
413 } else {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000414 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, true);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000415 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000416 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000417
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000418 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000419 fontDescriptor);
420 FontRec newEntry(font, fontID, font->fFirstGlyphID);
421 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000422 canonicalFonts().push(newEntry);
423 return font; // Return the reference new SkPDFFont() created.
424}
425
426// static
427SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
428 // This initialization is only thread safe with gcc.
429 static SkTDArray<FontRec> gCanonicalFonts;
430 return gCanonicalFonts;
431}
432
433// static
434SkMutex& SkPDFFont::canonicalFontsMutex() {
435 // This initialization is only thread safe with gcc.
436 static SkMutex gCanonicalFontsMutex;
437 return gCanonicalFontsMutex;
438}
439
440// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000441bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
442 // TODO(vandebo) optimize this, do only one search?
443 FontRec search(NULL, fontID, glyphID);
444 *index = canonicalFonts().find(search);
445 if (*index >= 0)
446 return true;
447 search.fGlyphID = 0;
448 *index = canonicalFonts().find(search);
449 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000450}
451
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000452SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
453 SkTypeface* typeface,
454 uint16_t glyphID,
455 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000456 SkPDFDict* fontDescriptor)
457 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000458 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459#ifdef SK_DEBUG
460 fDescendant(descendantFont),
461#endif
462 fMultiByteGlyphs(false),
463 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000464 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000465 fFontInfo(fontInfo),
466 fDescriptor(fontDescriptor) {
467
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000468 SkAdvancedTypefaceMetrics::FontType type;
469 if (fontInfo) {
470 type = fontInfo->fType;
471 } else {
472 type = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
473 }
474
475 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000476 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000477 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
478 }
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000479 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
480 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000481 if (descendantFont) {
482 populateCIDFont();
483 } else {
484 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000485 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000486 // No need to hold onto the font info for fonts types that
487 // support multibyte glyphs.
488 fFontInfo = NULL;
489 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000490 }
491
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000492 if (type == SkAdvancedTypefaceMetrics::kType1_Font &&
493 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000494 return;
495 }
496
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000497 SkASSERT(type == SkAdvancedTypefaceMetrics::kType1_Font ||
498 type == SkAdvancedTypefaceMetrics::kCFF_Font ||
499 type == SkAdvancedTypefaceMetrics::kOther_Font ||
500 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
501 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000502}
503
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000504void SkPDFFont::populateType0Font() {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000505 // TODO(vandebo) add a ToUnicode mapping.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000506 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000507
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000508 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000509 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000510 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000511
512 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
513 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000514
515 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000516 fResources.push(
517 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000518 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000519 insert("DescendantFonts", descendantFonts.get());
520}
521
522void SkPDFFont::populateCIDFont() {
523 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000524 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000525
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000526 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000527 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000528 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000529 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000530 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000531 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000532 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000533
534 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
535 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000536 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
537 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
538 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000539 insert("CIDSystemInfo", sysInfo.get());
540
541 addFontDescriptor(0);
542
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000543 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000544 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000545 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000546 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
547 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000548 widths->unref(); // SkRefPtr and compose both took a reference.
549 if (widths->size())
550 insert("W", widths.get());
551 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000552 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000553 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000554 }
555 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000556 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000557 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000558 defaultAdvance.fVerticalAdvance = 0;
559 defaultAdvance.fOriginXDisp = 0;
560 defaultAdvance.fOriginYDisp = 0;
561 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000562 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
563 fFontInfo->fEmSize, &appendVerticalAdvance,
564 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000565 advances->unref(); // SkRefPtr and compose both took a ref.
566 if (advances->size())
567 insert("W2", advances.get());
568 if (defaultAdvance.fVerticalAdvance ||
569 defaultAdvance.fOriginXDisp ||
570 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000571 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000572 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000573 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000574 }
575 }
576}
577
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000578bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000579 SkASSERT(!fFontInfo->fVerticalMetrics.get());
580 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000581
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000582 adjustGlyphRangeForSingleByteEncoding(glyphID);
583
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000584 int16_t defaultWidth = 0;
585 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
586 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000587 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
588 widthEntry != NULL;
589 widthEntry = widthEntry->fNext.get()) {
590 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000591 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000592 defaultWidth = widthEntry->fAdvance[0];
593 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000594 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000595 SkASSERT(false);
596 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000597 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000598 SkASSERT(widthRangeEntry == NULL);
599 widthRangeEntry = widthEntry;
600 break;
601 }
602 }
603
604 if (!addFontDescriptor(defaultWidth))
605 return false;
606
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000607 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000608 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000609
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000610 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000611
612 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000613 encoding->unref(); // SkRefPtr and new both took a reference.
614 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000615
616 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
617 encDiffs->unref(); // SkRefPtr and new both took a reference.
618 encoding->insert("Differences", encDiffs.get());
619
620 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000621 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000622 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000623 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000624 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000625 }
626
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000627 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000628 fFontInfo = NULL;
629 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000630}
631
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000632void SkPDFFont::populateType3Font(int16_t glyphID) {
633 SkPaint paint;
634 paint.setTypeface(fTypeface.get());
635 paint.setTextSize(1000);
636 SkAutoGlyphCache autoCache(paint, NULL);
637 SkGlyphCache* cache = autoCache.getCache();
638 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
639 if (fLastGlyphID == 0) {
640 fLastGlyphID = cache->getGlyphCount() - 1;
641 }
642
643 adjustGlyphRangeForSingleByteEncoding(glyphID);
644
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000645 insert("Subtype", new SkPDFName("Type3"))->unref();
646 // Flip about the x-axis and scale by 1/1000.
647 SkMatrix fontMatrix;
648 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
649 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
650
651 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
652 charProcs->unref(); // SkRefPtr and new both took a reference.
653 insert("CharProcs", charProcs.get());
654
655 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
656 encoding->unref(); // SkRefPtr and new both took a reference.
657 insert("Encoding", encoding.get());
658
659 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
660 encDiffs->unref(); // SkRefPtr and new both took a reference.
661 encoding->insert("Differences", encDiffs.get());
662 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
663 encDiffs->append(new SkPDFInt(1))->unref();
664
665 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
666 widthArray->unref(); // SkRefPtr and new both took a ref.
667
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000668 SkIRect bbox = SkIRect::MakeEmpty();
669 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
670 SkString characterName;
671 characterName.printf("gid%d", gID);
672 encDiffs->append(new SkPDFName(characterName))->unref();
673
reed@google.comce11b262011-03-21 21:25:35 +0000674 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
675 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000676 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
677 glyph.fWidth, glyph.fHeight);
678 bbox.join(glyphBBox);
679
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000680 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000681 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
682 &content);
683 const SkPath* path = cache->findPath(glyph);
684 if (path) {
685 SkPDFUtils::EmitPath(*path, &content);
686 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
687 &content);
688 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000689 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000690 glyphStream->unref(); // SkRefPtr and new both took a ref.
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000691 glyphStream->setMemoryOwned(content.detach(), content.getOffset());
692
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000693 SkRefPtr<SkPDFStream> glyphDescription =
694 new SkPDFStream(glyphStream.get());
695 // SkRefPtr and new both ref()'d charProcs, pass one.
696 fResources.push(glyphDescription.get());
697 charProcs->insert(characterName.c_str(),
698 new SkPDFObjRef(glyphDescription.get()))->unref();
699 }
700
701 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
702 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
703 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
704 insert("Widths", widthArray.get());
705
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000706 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000707 fFontInfo = NULL;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000708}
709
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000710bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000711 if (fDescriptor.get() != NULL) {
712 fResources.push(fDescriptor.get());
713 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000714 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000715 return true;
716 }
717
718 fDescriptor = new SkPDFDict("FontDescriptor");
719 fDescriptor->unref(); // SkRefPtr and new both took a ref.
720
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000721 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000722 case SkAdvancedTypefaceMetrics::kType1_Font: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000723 size_t header, data, trailer;
724 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000725 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000726 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
727 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
728 &data, &trailer);
729 if (fontData == NULL)
730 return false;
731 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
732 // SkRefPtr and new both ref()'d fontStream, pass one.
733 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000734 fontStream->insert("Length1", new SkPDFInt(header))->unref();
735 fontStream->insert("Length2", new SkPDFInt(data))->unref();
736 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
737 fDescriptor->insert("FontFile",
738 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000739 break;
740 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000741 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000742 SkRefPtr<SkStream> fontData =
743 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000744 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
745 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
746 // SkRefPtr and new both ref()'d fontStream, pass one.
747 fResources.push(fontStream.get());
748
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000749 fontStream->insert("Length1",
750 new SkPDFInt(fontData->getLength()))->unref();
751 fDescriptor->insert("FontFile2",
752 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000753 break;
754 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000755 case SkAdvancedTypefaceMetrics::kCFF_Font:
756 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000757 SkRefPtr<SkStream> fontData =
758 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000759 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
760 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
761 // SkRefPtr and new both ref()'d fontStream, pass one.
762 fResources.push(fontStream.get());
763
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000764 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000765 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
766 } else {
767 fontStream->insert("Subtype",
768 new SkPDFName("CIDFontType0c"))->unref();
769 }
770 fDescriptor->insert("FontFile3",
771 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000772 break;
773 }
774 default:
775 SkASSERT(false);
776 }
777
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000778 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000779 fResources.push(fDescriptor.get());
780 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000781 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000782
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000783 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784 fFontInfo->fFontName))->unref();
785 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000786 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000787 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000788 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000789 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000790 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000791 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000792 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000793 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000794 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000795 fFontInfo->fItalicAngle))->unref();
796 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
797 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000798
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000799 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000800 fDescriptor->insert("MissingWidth", new SkPDFScalar(
801 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000802 }
803 return true;
804}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000805void SkPDFFont::addWidthInfoFromRange(
806 int16_t defaultWidth,
807 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
808 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
809 widthArray->unref(); // SkRefPtr and new both took a ref.
810 int firstChar = 0;
811 if (widthRangeEntry) {
812 const uint16_t emSize = fFontInfo->fEmSize;
813 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
814 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
815 if (startIndex < 0)
816 startIndex = 0;
817 if (endIndex > widthRangeEntry->fAdvance.count())
818 endIndex = widthRangeEntry->fAdvance.count();
819 if (widthRangeEntry->fStartId == 0) {
820 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
821 } else {
822 firstChar = startIndex + widthRangeEntry->fStartId;
823 }
824 for (int i = startIndex; i < endIndex; i++)
825 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
826 } else {
827 appendWidth(defaultWidth, 1000, widthArray.get());
828 }
829 insert("FirstChar", new SkPDFInt(firstChar))->unref();
830 insert("LastChar",
831 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
832 insert("Widths", widthArray.get());
833}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000834
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000835void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
836 // Single byte glyph encoding supports a max of 255 glyphs.
837 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
838 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
839 fLastGlyphID = fFirstGlyphID + 255 - 1;
840 }
841}
842
843
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000844bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000845 if (fFontID != b.fFontID)
846 return false;
847 if (fFont != NULL && b.fFont != NULL) {
848 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
849 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
850 }
851 if (fGlyphID == 0 || b.fGlyphID == 0)
852 return true;
853
854 if (fFont != NULL) {
855 return fFont->fFirstGlyphID <= b.fGlyphID &&
856 b.fGlyphID <= fFont->fLastGlyphID;
857 } else if (b.fFont != NULL) {
858 return b.fFont->fFirstGlyphID <= fGlyphID &&
859 fGlyphID <= b.fFont->fLastGlyphID;
860 }
861 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000862}
863
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000864SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000865 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000866 fFontID(fontID),
867 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000868}