blob: 9a2180d8c156fd32d40ac6f4457bc9dccf179db5 [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
254SkPDFArray* makeFontBBox(
255 SkIRect glyphBBox, uint16_t emSize,
256 SkPDFDevice::OriginTransform flipOrigin =
257 SkPDFDevice::kNoFlip_OriginTransform) {
258 if (flipOrigin == SkPDFDevice::kFlip_OriginTransform) {
259 int32_t temp = -glyphBBox.fTop;
260 glyphBBox.fTop = -glyphBBox.fBottom;
261 glyphBBox.fBottom = temp;
262 }
263 SkPDFArray* bbox = new SkPDFArray;
264 bbox->reserve(4);
265 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft,
266 emSize)))->unref();
267 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom,
268 emSize)))->unref();
269 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight,
270 emSize)))->unref();
271 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop,
272 emSize)))->unref();
273 return bbox;
274}
275
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000276SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
277 SkPDFArray* array) {
278 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000279 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000280}
281
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000282SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000283 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
284 uint16_t emSize, SkPDFArray* array) {
285 appendWidth(advance.fVerticalAdvance, emSize, array);
286 appendWidth(advance.fOriginXDisp, emSize, array);
287 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000288 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000289}
290
291template <typename Data>
292SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000293 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
294 uint16_t emSize,
295 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
296 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000297 Data* defaultAdvance) {
298 SkPDFArray* result = new SkPDFArray();
299 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
300 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000301 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000302 SkASSERT(advanceInfo->fAdvance.count() == 1);
303 *defaultAdvance = advanceInfo->fAdvance[0];
304 break;
305 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000306 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000307 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
308 advanceArray->unref(); // SkRefPtr and new both took a ref.
309 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000310 appendAdvance(advanceInfo->fAdvance[j], emSize,
311 advanceArray.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000312 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000313 result->append(advanceArray.get());
314 break;
315 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000316 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000317 SkASSERT(advanceInfo->fAdvance.count() == 1);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000318 result->append(new SkPDFInt(advanceInfo->fStartId))->unref();
319 result->append(new SkPDFInt(advanceInfo->fEndId))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000320 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000321 break;
322 }
323 }
324 }
325 return result;
326}
327
328} // namespace
329
330/* Font subset design: It would be nice to be able to subset fonts
331 * (particularly type 3 fonts), but it's a lot of work and not a priority.
332 *
333 * Resources are canonicalized and uniqueified by pointer so there has to be
334 * some additional state indicating which subset of the font is used. It
335 * must be maintained at the page granularity and then combined at the document
336 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
337 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
338 * page/pdf device. c) in the document, retrieve the per font glyph usage
339 * from each page and combine it and ask for a resource with that subset.
340 */
341
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000342SkPDFFont::~SkPDFFont() {
343 SkAutoMutexAcquire lock(canonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000344 int index;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000345 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000346 canonicalFonts().removeShuffle(index);
347#ifdef SK_DEBUG
348 SkASSERT(!fDescendant);
349 } else {
350 SkASSERT(fDescendant);
351#endif
352 }
353 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000354}
355
356void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
357 resourceList->setReserve(resourceList->count() + fResources.count());
358 for (int i = 0; i < fResources.count(); i++) {
359 resourceList->push(fResources[i]);
360 fResources[i]->ref();
361 fResources[i]->getResources(resourceList);
362 }
363}
364
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000365SkTypeface* SkPDFFont::typeface() {
366 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000367}
368
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000369bool SkPDFFont::hasGlyph(uint16_t id) {
370 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
371}
372
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000373bool SkPDFFont::multiByteGlyphs() {
374 return fMultiByteGlyphs;
375}
376
vandebo@chromium.org01294102011-02-28 19:52:18 +0000377size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
378 size_t numGlyphs) {
379 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000380 if (fMultiByteGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000381 return numGlyphs;
382 }
383
384 for (size_t i = 0; i < numGlyphs; i++) {
385 if (glyphIDs[i] == 0) {
386 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000387 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000388 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
389 return i;
390 }
391 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000392 }
393
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000394 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000395}
396
397// static
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000398SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000399 SkAutoMutexAcquire lock(canonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000400 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000401 int index;
402 if (find(fontID, glyphID, &index)) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000403 canonicalFonts()[index].fFont->ref();
404 return canonicalFonts()[index].fFont;
405 }
406
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000407 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000408 SkPDFDict* fontDescriptor = NULL;
409 if (index >= 0) {
410 SkPDFFont* relatedFont = canonicalFonts()[index].fFont;
411 SkASSERT(relatedFont->fFontInfo.get());
412 fontInfo = relatedFont->fFontInfo;
413 fontDescriptor = relatedFont->fDescriptor.get();
414 } else {
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000415 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, SkTBitOr(
416 SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo,
417 SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo));
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000418 SkSafeUnref(fontInfo.get()); // SkRefPtr and Get both took a reference.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000419 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000420
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000421 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000422 fontDescriptor);
423 FontRec newEntry(font, fontID, font->fFirstGlyphID);
424 index = canonicalFonts().count();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000425 canonicalFonts().push(newEntry);
426 return font; // Return the reference new SkPDFFont() created.
427}
428
429// static
430SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() {
431 // This initialization is only thread safe with gcc.
432 static SkTDArray<FontRec> gCanonicalFonts;
433 return gCanonicalFonts;
434}
435
436// static
437SkMutex& SkPDFFont::canonicalFontsMutex() {
438 // This initialization is only thread safe with gcc.
439 static SkMutex gCanonicalFontsMutex;
440 return gCanonicalFontsMutex;
441}
442
443// static
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000444bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) {
445 // TODO(vandebo) optimize this, do only one search?
446 FontRec search(NULL, fontID, glyphID);
447 *index = canonicalFonts().find(search);
448 if (*index >= 0)
449 return true;
450 search.fGlyphID = 0;
451 *index = canonicalFonts().find(search);
452 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000453}
454
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000455SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo,
456 SkTypeface* typeface,
457 uint16_t glyphID,
458 bool descendantFont,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459 SkPDFDict* fontDescriptor)
460 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000461 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000462#ifdef SK_DEBUG
463 fDescendant(descendantFont),
464#endif
465 fMultiByteGlyphs(false),
466 fFirstGlyphID(1),
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000467 fLastGlyphID(fontInfo ? fontInfo->fLastGlyphID : 0),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000468 fFontInfo(fontInfo),
469 fDescriptor(fontDescriptor) {
470
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000471 SkAdvancedTypefaceMetrics::FontType type;
472 if (fontInfo) {
473 type = fontInfo->fType;
474 } else {
475 type = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
476 }
477
478 if (fontInfo && fontInfo->fMultiMaster) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000479 SkASSERT(false); // Not supported yet.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000480 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font;
481 }
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000482 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
483 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000484 if (descendantFont) {
485 populateCIDFont();
486 } else {
487 populateType0Font();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000488 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000489 // No need to hold onto the font info for fonts types that
490 // support multibyte glyphs.
491 fFontInfo = NULL;
492 return;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000493 }
494
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000495 if (type == SkAdvancedTypefaceMetrics::kType1_Font &&
496 populateType1Font(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000497 return;
498 }
499
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000500 SkASSERT(type == SkAdvancedTypefaceMetrics::kType1_Font ||
501 type == SkAdvancedTypefaceMetrics::kCFF_Font ||
502 type == SkAdvancedTypefaceMetrics::kOther_Font ||
503 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
504 populateType3Font(glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000505}
506
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000507void SkPDFFont::populateType0Font() {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000508 // TODO(vandebo) add a ToUnicode mapping.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000509 fMultiByteGlyphs = true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000510
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000511 insert("Subtype", new SkPDFName("Type0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000512 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000513 insert("Encoding", new SkPDFName("Identity-H"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000514
515 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
516 descendantFonts->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000517
518 // Pass ref new created to fResources.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000519 fResources.push(
520 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000521 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000522 insert("DescendantFonts", descendantFonts.get());
523}
524
525void SkPDFFont::populateCIDFont() {
526 fMultiByteGlyphs = true;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000527 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000528
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000529 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000530 insert("Subtype", new SkPDFName("CIDFontType0"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000531 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000532 insert("Subtype", new SkPDFName("CIDFontType2"))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000533 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000534 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000535 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000536
537 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
538 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000539 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
540 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
541 sysInfo->insert("Supplement", new SkPDFInt(0))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000542 insert("CIDSystemInfo", sysInfo.get());
543
544 addFontDescriptor(0);
545
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000546 if (fFontInfo->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000547 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000548 SkRefPtr<SkPDFArray> widths =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000549 composeAdvanceData(fFontInfo->fGlyphWidths.get(),
550 fFontInfo->fEmSize, &appendWidth, &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000551 widths->unref(); // SkRefPtr and compose both took a reference.
552 if (widths->size())
553 insert("W", widths.get());
554 if (defaultWidth != 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000555 insert("DW", new SkPDFScalar(scaleFromFontUnits(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000556 defaultWidth, fFontInfo->fEmSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000557 }
558 }
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000559 if (fFontInfo->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000560 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000561 defaultAdvance.fVerticalAdvance = 0;
562 defaultAdvance.fOriginXDisp = 0;
563 defaultAdvance.fOriginYDisp = 0;
564 SkRefPtr<SkPDFArray> advances =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000565 composeAdvanceData(fFontInfo->fVerticalMetrics.get(),
566 fFontInfo->fEmSize, &appendVerticalAdvance,
567 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000568 advances->unref(); // SkRefPtr and compose both took a ref.
569 if (advances->size())
570 insert("W2", advances.get());
571 if (defaultAdvance.fVerticalAdvance ||
572 defaultAdvance.fOriginXDisp ||
573 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000574 insert("DW2", appendVerticalAdvance(defaultAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000575 fFontInfo->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000576 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000577 }
578 }
579}
580
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000581bool SkPDFFont::populateType1Font(int16_t glyphID) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000582 SkASSERT(!fFontInfo->fVerticalMetrics.get());
583 SkASSERT(fFontInfo->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000584
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000585 adjustGlyphRangeForSingleByteEncoding(glyphID);
586
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000587 int16_t defaultWidth = 0;
588 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
589 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000590 for (widthEntry = fFontInfo.get()->fGlyphWidths.get();
591 widthEntry != NULL;
592 widthEntry = widthEntry->fNext.get()) {
593 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000594 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000595 defaultWidth = widthEntry->fAdvance[0];
596 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000597 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000598 SkASSERT(false);
599 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000600 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601 SkASSERT(widthRangeEntry == NULL);
602 widthRangeEntry = widthEntry;
603 break;
604 }
605 }
606
607 if (!addFontDescriptor(defaultWidth))
608 return false;
609
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000610 insert("Subtype", new SkPDFName("Type1"))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000611 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000612
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000613 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000614
615 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000616 encoding->unref(); // SkRefPtr and new both took a reference.
617 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000618
619 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
620 encDiffs->unref(); // SkRefPtr and new both took a reference.
621 encoding->insert("Differences", encDiffs.get());
622
623 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000624 encDiffs->append(new SkPDFInt(1))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000625 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000626 encDiffs->append(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000627 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000628 }
629
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000630 if (fFontInfo->fLastGlyphID <= 255)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000631 fFontInfo = NULL;
632 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000633}
634
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000635void SkPDFFont::populateType3Font(int16_t glyphID) {
636 SkPaint paint;
637 paint.setTypeface(fTypeface.get());
638 paint.setTextSize(1000);
639 SkAutoGlyphCache autoCache(paint, NULL);
640 SkGlyphCache* cache = autoCache.getCache();
641 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
642 if (fLastGlyphID == 0) {
643 fLastGlyphID = cache->getGlyphCount() - 1;
644 }
645
646 adjustGlyphRangeForSingleByteEncoding(glyphID);
647
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000648 insert("Subtype", new SkPDFName("Type3"))->unref();
649 // Flip about the x-axis and scale by 1/1000.
650 SkMatrix fontMatrix;
651 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
652 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
653
654 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
655 charProcs->unref(); // SkRefPtr and new both took a reference.
656 insert("CharProcs", charProcs.get());
657
658 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
659 encoding->unref(); // SkRefPtr and new both took a reference.
660 insert("Encoding", encoding.get());
661
662 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
663 encDiffs->unref(); // SkRefPtr and new both took a reference.
664 encoding->insert("Differences", encDiffs.get());
665 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2);
666 encDiffs->append(new SkPDFInt(1))->unref();
667
668 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
669 widthArray->unref(); // SkRefPtr and new both took a ref.
670
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000671 SkIRect bbox = SkIRect::MakeEmpty();
672 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) {
673 SkString characterName;
674 characterName.printf("gid%d", gID);
675 encDiffs->append(new SkPDFName(characterName))->unref();
676
reed@google.comce11b262011-03-21 21:25:35 +0000677 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
678 widthArray->append(new SkPDFScalar(SkFixedToScalar(glyph.fAdvanceX)))->unref();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000679 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
680 glyph.fWidth, glyph.fHeight);
681 bbox.join(glyphBBox);
682
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000683 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000684 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
685 &content);
686 const SkPath* path = cache->findPath(glyph);
687 if (path) {
688 SkPDFUtils::EmitPath(*path, &content);
689 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
690 &content);
691 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000692 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000693 glyphStream->unref(); // SkRefPtr and new both took a ref.
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000694 glyphStream->setMemoryOwned(content.detach(), content.getOffset());
695
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000696 SkRefPtr<SkPDFStream> glyphDescription =
697 new SkPDFStream(glyphStream.get());
698 // SkRefPtr and new both ref()'d charProcs, pass one.
699 fResources.push(glyphDescription.get());
700 charProcs->insert(characterName.c_str(),
701 new SkPDFObjRef(glyphDescription.get()))->unref();
702 }
703
704 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
705 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref();
706 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref();
707 insert("Widths", widthArray.get());
708
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000709 if (fFontInfo && fFontInfo->fLastGlyphID <= 255)
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000710 fFontInfo = NULL;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000711}
712
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000713bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000714 if (fDescriptor.get() != NULL) {
715 fResources.push(fDescriptor.get());
716 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000717 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000718 return true;
719 }
720
721 fDescriptor = new SkPDFDict("FontDescriptor");
722 fDescriptor->unref(); // SkRefPtr and new both took a ref.
723
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000724 switch (fFontInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000725 case SkAdvancedTypefaceMetrics::kType1_Font: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000726 size_t header, data, trailer;
727 SkRefPtr<SkStream> rawFontData =
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000728 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000729 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
730 SkStream* fontData = handleType1Stream(rawFontData.get(), &header,
731 &data, &trailer);
732 if (fontData == NULL)
733 return false;
734 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
735 // SkRefPtr and new both ref()'d fontStream, pass one.
736 fResources.push(fontStream.get());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000737 fontStream->insert("Length1", new SkPDFInt(header))->unref();
738 fontStream->insert("Length2", new SkPDFInt(data))->unref();
739 fontStream->insert("Length3", new SkPDFInt(trailer))->unref();
740 fDescriptor->insert("FontFile",
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::kTrueType_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000745 SkRefPtr<SkStream> fontData =
746 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000747 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
748 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
749 // SkRefPtr and new both ref()'d fontStream, pass one.
750 fResources.push(fontStream.get());
751
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000752 fontStream->insert("Length1",
753 new SkPDFInt(fontData->getLength()))->unref();
754 fDescriptor->insert("FontFile2",
755 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000756 break;
757 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000758 case SkAdvancedTypefaceMetrics::kCFF_Font:
759 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000760 SkRefPtr<SkStream> fontData =
761 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get()));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000762 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
763 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
764 // SkRefPtr and new both ref()'d fontStream, pass one.
765 fResources.push(fontStream.get());
766
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000767 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000768 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref();
769 } else {
770 fontStream->insert("Subtype",
771 new SkPDFName("CIDFontType0c"))->unref();
772 }
773 fDescriptor->insert("FontFile3",
774 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000775 break;
776 }
777 default:
778 SkASSERT(false);
779 }
780
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000781 const uint16_t emSize = fFontInfo->fEmSize;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000782 fResources.push(fDescriptor.get());
783 fDescriptor->ref();
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000784 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000785
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000786 fDescriptor->insert("FontName", new SkPDFName(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000787 fFontInfo->fFontName))->unref();
788 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000789 fDescriptor->insert("Ascent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000790 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000791 fDescriptor->insert("Descent", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000792 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000793 fDescriptor->insert("StemV", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000794 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000795 fDescriptor->insert("CapHeight", new SkPDFScalar(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000796 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref();
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000797 fDescriptor->insert("ItalicAngle", new SkPDFInt(
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000798 fFontInfo->fItalicAngle))->unref();
799 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
800 fFontInfo->fEmSize))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000801
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000802 if (defaultWidth > 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000803 fDescriptor->insert("MissingWidth", new SkPDFScalar(
804 scaleFromFontUnits(defaultWidth, emSize)))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000805 }
806 return true;
807}
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000808void SkPDFFont::addWidthInfoFromRange(
809 int16_t defaultWidth,
810 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
811 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
812 widthArray->unref(); // SkRefPtr and new both took a ref.
813 int firstChar = 0;
814 if (widthRangeEntry) {
815 const uint16_t emSize = fFontInfo->fEmSize;
816 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId;
817 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1;
818 if (startIndex < 0)
819 startIndex = 0;
820 if (endIndex > widthRangeEntry->fAdvance.count())
821 endIndex = widthRangeEntry->fAdvance.count();
822 if (widthRangeEntry->fStartId == 0) {
823 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
824 } else {
825 firstChar = startIndex + widthRangeEntry->fStartId;
826 }
827 for (int i = startIndex; i < endIndex; i++)
828 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
829 } else {
830 appendWidth(defaultWidth, 1000, widthArray.get());
831 }
832 insert("FirstChar", new SkPDFInt(firstChar))->unref();
833 insert("LastChar",
834 new SkPDFInt(firstChar + widthArray->size() - 1))->unref();
835 insert("Widths", widthArray.get());
836}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000837
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000838void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
839 // Single byte glyph encoding supports a max of 255 glyphs.
840 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
841 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
842 fLastGlyphID = fFirstGlyphID + 255 - 1;
843 }
844}
845
846
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000847bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000848 if (fFontID != b.fFontID)
849 return false;
850 if (fFont != NULL && b.fFont != NULL) {
851 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
852 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
853 }
854 if (fGlyphID == 0 || b.fGlyphID == 0)
855 return true;
856
857 if (fFont != NULL) {
858 return fFont->fFirstGlyphID <= b.fGlyphID &&
859 b.fGlyphID <= fFont->fLastGlyphID;
860 } else if (b.fFont != NULL) {
861 return b.fFont->fFirstGlyphID <= fGlyphID &&
862 fGlyphID <= b.fFont->fLastGlyphID;
863 }
864 return fGlyphID == b.fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000865}
866
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000867SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000868 : fFont(font),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000869 fFontID(fontID),
870 fGlyphID(glyphID) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000871}