blob: fe55c8d046d08a906246678ea1a74863d4f70710 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000010#include <ctype.h>
11
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000013#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000014#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkPaint.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000016#include "SkPDFCatalog.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000017#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000018#include "SkPDFFont.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000019#include "SkPDFFontImpl.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000021#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000022#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000023#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000024#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkStream.h"
26#include "SkTypeface.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000027#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000028#include "SkUtils.h"
29
vandebo@chromium.org1f165892011-07-26 02:11:41 +000030#if defined (SK_SFNTLY_SUBSETTER)
31#include SK_SFNTLY_SUBSETTER
32#endif
33
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000034namespace {
35
vandebo@chromium.org98594282011-07-25 22:34:12 +000036///////////////////////////////////////////////////////////////////////////////
37// File-Local Functions
38///////////////////////////////////////////////////////////////////////////////
39
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000040bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
41 size_t* size) {
42 // PFB sections have a two or six bytes header. 0x80 and a one byte
43 // section type followed by a four byte section length. Type one is
44 // an ASCII section (includes a length), type two is a binary section
45 // (includes a length) and type three is an EOF marker with no length.
46 const uint8_t* buf = *src;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000047 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000048 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000049 } else if (buf[1] == 3) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000050 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000051 } else if (*len < 6) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000052 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000053 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000054
vandebo@chromium.org73322072011-06-21 21:19:41 +000055 *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
56 ((size_t)buf[5] << 24);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000057 size_t consumed = *size + 6;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000058 if (consumed > *len) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000059 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000060 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000061 *src = *src + consumed;
62 *len = *len - consumed;
63 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000064}
65
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000066bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
67 size_t* dataLen, size_t* trailerLen) {
68 const uint8_t* srcPtr = src;
69 size_t remaining = size;
70
71 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
72 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
73 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
74 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075}
76
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000077/* The sections of a PFA file are implicitly defined. The body starts
78 * after the line containing "eexec," and the trailer starts with 512
79 * literal 0's followed by "cleartomark" (plus arbitrary white space).
80 *
81 * This function assumes that src is NUL terminated, but the NUL
82 * termination is not included in size.
83 *
84 */
85bool parsePFA(const char* src, size_t size, size_t* headerLen,
86 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
87 const char* end = src + size;
88
89 const char* dataPos = strstr(src, "eexec");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000090 if (!dataPos) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000091 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000092 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000093 dataPos += strlen("eexec");
94 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000095 dataPos < end) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000096 dataPos++;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000097 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000098 *headerLen = dataPos - src;
99
100 const char* trailerPos = strstr(dataPos, "cleartomark");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000101 if (!trailerPos) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000102 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000103 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000104 int zeroCount = 0;
105 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
106 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
107 continue;
108 } else if (*trailerPos == '0') {
109 zeroCount++;
110 } else {
111 return false;
112 }
113 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000114 if (zeroCount != 512) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000115 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000116 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000117
118 *hexDataLen = trailerPos - src - *headerLen;
119 *trailerLen = size - *headerLen - *hexDataLen;
120
121 // Verify that the data section is hex encoded and count the bytes.
122 int nibbles = 0;
123 for (; dataPos < trailerPos; dataPos++) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000124 if (isspace(*dataPos)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000125 continue;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000126 }
127 if (!isxdigit(*dataPos)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000128 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000129 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000130 nibbles++;
131 }
132 *dataLen = (nibbles + 1) / 2;
133
134 return true;
135}
136
137int8_t hexToBin(uint8_t c) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000138 if (!isxdigit(c)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000139 return -1;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000140 } else if (c <= '9') {
141 return c - '0';
142 } else if (c <= 'F') {
143 return c - 'A' + 10;
144 } else if (c <= 'f') {
145 return c - 'a' + 10;
146 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000147 return -1;
148}
149
150SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
151 size_t* dataLen, size_t* trailerLen) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000152 // srcStream may be backed by a file or a unseekable fd, so we may not be
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000153 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
154 // the input only once is doable, but very ugly. Furthermore, it'd be nice
155 // if the data was NUL terminated so that we can use strstr() to search it.
156 // Make as few copies as possible given these constraints.
157 SkDynamicMemoryWStream dynamicStream;
158 SkRefPtr<SkMemoryStream> staticStream;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000159 SkData* data = NULL;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000160 const uint8_t* src;
161 size_t srcLen;
162 if ((srcLen = srcStream->getLength()) > 0) {
163 staticStream = new SkMemoryStream(srcLen + 1);
164 staticStream->unref(); // new and SkRefPtr both took a ref.
165 src = (const uint8_t*)staticStream->getMemoryBase();
166 if (srcStream->getMemoryBase() != NULL) {
167 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
168 } else {
169 size_t read = 0;
170 while (read < srcLen) {
171 size_t got = srcStream->read((void *)staticStream->getAtPos(),
172 srcLen - read);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000173 if (got == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000174 return NULL;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000175 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000176 read += got;
177 staticStream->seek(read);
178 }
179 }
180 ((uint8_t *)src)[srcLen] = 0;
181 } else {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000182 static const size_t kBufSize = 4096;
183 uint8_t buf[kBufSize];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000184 size_t amount;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000185 while ((amount = srcStream->read(buf, kBufSize)) > 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000186 dynamicStream.write(buf, amount);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000187 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000188 amount = 0;
189 dynamicStream.write(&amount, 1); // NULL terminator.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000190 data = dynamicStream.copyToData();
191 src = data->bytes();
192 srcLen = data->size() - 1;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000193 }
194
reed@google.com8a85d0c2011-06-24 19:12:12 +0000195 // this handles releasing the data we may have gotten from dynamicStream.
196 // if data is null, it is a no-op
197 SkAutoDataUnref aud(data);
198
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000199 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
200 SkMemoryStream* result =
201 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
202 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
203 result->seek(*headerLen);
204 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
205 result->seek(*headerLen + *dataLen);
206 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
207 *trailerLen);
208 result->rewind();
209 return result;
210 }
211
212 // A PFA has to be converted for PDF.
213 size_t hexDataLen;
214 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
215 trailerLen)) {
216 SkMemoryStream* result =
217 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
218 memcpy((char*)result->getAtPos(), src, *headerLen);
219 result->seek(*headerLen);
220
221 const uint8_t* hexData = src + *headerLen;
222 const uint8_t* trailer = hexData + hexDataLen;
223 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000224 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000225 bool highNibble = true;
226 for (; hexData < trailer; hexData++) {
227 char curNibble = hexToBin(*hexData);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000228 if (curNibble < 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000229 continue;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000230 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000231 if (highNibble) {
232 dataByte = curNibble << 4;
233 highNibble = false;
234 } else {
235 dataByte |= curNibble;
236 highNibble = true;
237 ((char *)result->getAtPos())[outputOffset++] = dataByte;
238 }
239 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000240 if (!highNibble) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000241 ((char *)result->getAtPos())[outputOffset++] = dataByte;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000242 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000243 SkASSERT(outputOffset == *dataLen);
244 result->seek(*headerLen + outputOffset);
245
246 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
247 *trailerLen);
248 result->rewind();
249 return result;
250 }
251
252 return NULL;
253}
254
reed@google.com3f0dcf92011-03-18 21:23:45 +0000255// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000256SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000257 SkScalar scaled = SkIntToScalar(val);
258 if (emSize == 1000) {
259 return scaled;
260 } else {
261 return SkScalarMulDiv(scaled, 1000, emSize);
262 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000263}
264
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000265void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000266 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000267 // Specify width and bounding box for the glyph.
268 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000269 content->writeText(" 0 ");
270 content->writeDecAsText(box.fLeft);
271 content->writeText(" ");
272 content->writeDecAsText(box.fTop);
273 content->writeText(" ");
274 content->writeDecAsText(box.fRight);
275 content->writeText(" ");
276 content->writeDecAsText(box.fBottom);
277 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000278}
279
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000280SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000281 SkPDFArray* bbox = new SkPDFArray;
282 bbox->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000283 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
284 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
285 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
286 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000287 return bbox;
288}
289
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000290SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
291 SkPDFArray* array) {
reed@google.comc789cf12011-07-20 12:14:33 +0000292 array->appendScalar(scaleFromFontUnits(width, emSize));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000293 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000294}
295
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000296SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000297 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
298 uint16_t emSize, SkPDFArray* array) {
299 appendWidth(advance.fVerticalAdvance, emSize, array);
300 appendWidth(advance.fOriginXDisp, emSize, array);
301 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000302 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000303}
304
305template <typename Data>
306SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000307 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
308 uint16_t emSize,
309 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
310 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000311 Data* defaultAdvance) {
312 SkPDFArray* result = new SkPDFArray();
313 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
314 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000315 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000316 SkASSERT(advanceInfo->fAdvance.count() == 1);
317 *defaultAdvance = advanceInfo->fAdvance[0];
318 break;
319 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000320 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000321 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray();
322 advanceArray->unref(); // SkRefPtr and new both took a ref.
323 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000324 appendAdvance(advanceInfo->fAdvance[j], emSize,
325 advanceArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000326 result->appendInt(advanceInfo->fStartId);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000327 result->append(advanceArray.get());
328 break;
329 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000330 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000331 SkASSERT(advanceInfo->fAdvance.count() == 1);
reed@google.comc789cf12011-07-20 12:14:33 +0000332 result->appendInt(advanceInfo->fStartId);
333 result->appendInt(advanceInfo->fEndId);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000334 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000335 break;
336 }
337 }
338 }
339 return result;
340}
341
342} // namespace
343
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000344static void append_tounicode_header(SkDynamicMemoryWStream* cmap) {
345 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
346 // It's there to prevent old version Adobe Readers from malfunctioning.
347 const char* kHeader =
348 "/CIDInit /ProcSet findresource begin\n"
349 "12 dict begin\n"
350 "begincmap\n";
351 cmap->writeText(kHeader);
352
353 // The /CIDSystemInfo must be consistent to the one in
354 // SkPDFFont::populateCIDFont().
355 // We can not pass over the system info object here because the format is
356 // different. This is not a reference object.
357 const char* kSysInfo =
358 "/CIDSystemInfo\n"
359 "<< /Registry (Adobe)\n"
360 "/Ordering (UCS)\n"
361 "/Supplement 0\n"
362 ">> def\n";
363 cmap->writeText(kSysInfo);
364
365 // The CMapName must be consistent to /CIDSystemInfo above.
366 // /CMapType 2 means ToUnicode.
367 // We specify codespacerange from 0x0000 to 0xFFFF because we convert our
368 // code table from unsigned short (16-bits). Codespace range just tells the
369 // PDF processor the valid range. It does not matter whether a complete
370 // mapping is provided or not.
371 const char* kTypeInfo =
372 "/CMapName /Adobe-Identity-UCS def\n"
373 "/CMapType 2 def\n"
374 "1 begincodespacerange\n"
375 "<0000> <FFFF>\n"
376 "endcodespacerange\n";
377 cmap->writeText(kTypeInfo);
378}
379
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000380static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
381 const char* kFooter =
382 "endcmap\n"
383 "CMapName currentdict /CMap defineresource pop\n"
384 "end\n"
385 "end";
386 cmap->writeText(kFooter);
387}
388
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000389struct BFChar {
390 uint16_t fGlyphId;
391 SkUnichar fUnicode;
392};
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000393
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000394struct BFRange {
395 uint16_t fStart;
396 uint16_t fEnd;
397 SkUnichar fUnicode;
398};
399
400static void append_bfchar_section(const SkTDArray<BFChar>& bfchar,
401 SkDynamicMemoryWStream* cmap) {
402 // PDF spec defines that every bf* list can have at most 100 entries.
403 for (int i = 0; i < bfchar.count(); i += 100) {
404 int count = bfchar.count() - i;
405 count = SkMin32(count, 100);
406 cmap->writeDecAsText(count);
407 cmap->writeText(" beginbfchar\n");
408 for (int j = 0; j < count; ++j) {
409 cmap->writeText("<");
410 cmap->writeHexAsText(bfchar[i + j].fGlyphId, 4);
411 cmap->writeText("> <");
412 cmap->writeHexAsText(bfchar[i + j].fUnicode, 4);
413 cmap->writeText(">\n");
414 }
415 cmap->writeText("endbfchar\n");
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000416 }
417}
418
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000419static void append_bfrange_section(const SkTDArray<BFRange>& bfrange,
420 SkDynamicMemoryWStream* cmap) {
421 // PDF spec defines that every bf* list can have at most 100 entries.
422 for (int i = 0; i < bfrange.count(); i += 100) {
423 int count = bfrange.count() - i;
424 count = SkMin32(count, 100);
425 cmap->writeDecAsText(count);
426 cmap->writeText(" beginbfrange\n");
427 for (int j = 0; j < count; ++j) {
428 cmap->writeText("<");
429 cmap->writeHexAsText(bfrange[i + j].fStart, 4);
430 cmap->writeText("> <");
431 cmap->writeHexAsText(bfrange[i + j].fEnd, 4);
432 cmap->writeText("> <");
433 cmap->writeHexAsText(bfrange[i + j].fUnicode, 4);
434 cmap->writeText(">\n");
435 }
436 cmap->writeText("endbfrange\n");
437 }
438}
439
440// Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe
441// Technote 5014.
442// The function is not static so we can test it in unit tests.
443//
444// Current implementation guarantees bfchar and bfrange entries do not overlap.
445//
446// Current implementation does not attempt aggresive optimizations against
447// following case because the specification is not clear.
448//
449// 4 beginbfchar 1 beginbfchar
450// <0003> <0013> <0020> <0014>
451// <0005> <0015> to endbfchar
452// <0007> <0017> 1 beginbfrange
453// <0020> <0014> <0003> <0007> <0013>
454// endbfchar endbfrange
455//
456// Adobe Technote 5014 said: "Code mappings (unlike codespace ranges) may
457// overlap, but succeeding maps superceded preceding maps."
458//
459// In case of searching text in PDF, bfrange will have higher precedence so
460// typing char id 0x0014 in search box will get glyph id 0x0004 first. However,
461// the spec does not mention how will this kind of conflict being resolved.
462//
463// For the worst case (having 65536 continuous unicode and we use every other
464// one of them), the possible savings by aggressive optimization is 416KB
465// pre-compressed and does not provide enough motivation for implementation.
466void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
467 const SkPDFGlyphSet* subset,
468 SkDynamicMemoryWStream* cmap) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000469 if (glyphToUnicode.isEmpty()) {
470 return;
471 }
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000472
473 SkTDArray<BFChar> bfcharEntries;
474 SkTDArray<BFRange> bfrangeEntries;
475
476 BFRange currentRangeEntry;
477 bool haveBase = false;
478 int continuousEntries = 0;
479
480 for (int i = 0; i < glyphToUnicode.count(); ++i) {
481 if (glyphToUnicode[i] && (subset == NULL || subset->has(i))) {
482 // PDF spec requires bfrange not changing the higher byte,
483 // e.g. <1035> <10FF> <2222> is ok, but
484 // <1035> <1100> <2222> is no good
485 if (haveBase) {
486 ++continuousEntries;
487 if (i == currentRangeEntry.fStart + continuousEntries &&
488 (i >> 8) == (currentRangeEntry.fStart >> 8) &&
489 glyphToUnicode[i] == (currentRangeEntry.fUnicode +
490 continuousEntries)) {
epoger@google.com17b78942011-08-26 14:40:38 +0000491 currentRangeEntry.fEnd = i;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000492 if (i == glyphToUnicode.count() - 1) {
493 // Last entry is in a range.
494 bfrangeEntries.push(currentRangeEntry);
495 }
496 continue;
497 }
498
499 // Need to have at least 2 entries to form a bfrange.
500 if (continuousEntries >= 2) {
501 bfrangeEntries.push(currentRangeEntry);
502 } else {
503 BFChar* entry = bfcharEntries.append();
504 entry->fGlyphId = currentRangeEntry.fStart;
505 entry->fUnicode = currentRangeEntry.fUnicode;
506 }
507 continuousEntries = 0;
508 }
509
510 if (i != glyphToUnicode.count() - 1) {
511 currentRangeEntry.fStart = i;
512 currentRangeEntry.fEnd = i;
513 currentRangeEntry.fUnicode = glyphToUnicode[i];
514 haveBase = true;
515 } else {
516 BFChar* entry = bfcharEntries.append();
517 entry->fGlyphId = i;
518 entry->fUnicode = glyphToUnicode[i];
519 }
520 }
521 }
522
523 // The spec requires all bfchar entries for a font must come before bfrange
524 // entries.
525 append_bfchar_section(bfcharEntries, cmap);
526 append_bfrange_section(bfrangeEntries, cmap);
527}
528
vandebo@chromium.org98594282011-07-25 22:34:12 +0000529static SkPDFStream* generate_tounicode_cmap(
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000530 const SkTDArray<SkUnichar>& glyphToUnicode,
531 const SkPDFGlyphSet* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000532 SkDynamicMemoryWStream cmap;
533 append_tounicode_header(&cmap);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000534 append_cmap_sections(glyphToUnicode, subset, &cmap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000535 append_cmap_footer(&cmap);
536 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
537 cmapStream->unref(); // SkRefPtr and new took a reference.
538 cmapStream->setData(cmap.copyToData());
539 return new SkPDFStream(cmapStream.get());
540}
541
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000542static void sk_delete_array(const void* ptr, size_t, void*) {
543 // Use C-style cast to cast away const and cast type simultaneously.
544 delete[] (unsigned char*)ptr;
545}
546
547static int get_subset_font_stream(const char* fontName,
548 const SkTypeface* typeface,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000549 const SkTDArray<uint32_t>& subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000550 SkPDFStream** fontStream) {
551 SkRefPtr<SkStream> fontData =
552 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface));
553 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
554
555 int fontSize = fontData->getLength();
556
557#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000558 // Read font into buffer.
559 SkPDFStream* subsetFontStream = NULL;
560 SkTDArray<unsigned char> originalFont;
561 originalFont.setCount(fontSize);
562 if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) {
563 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000564 // sfntly requires unsigned int* to be passed in, as far as we know,
565 // unsigned int is equivalent to uint32_t on all platforms.
566 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
567 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000568 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
569 originalFont.begin(),
570 fontSize,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000571 subset.begin(),
572 subset.count(),
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000573 &subsetFont);
574 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000575 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
576 subsetFontSize,
577 sk_delete_array,
578 NULL));
579 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000580 fontSize = subsetFontSize;
581 }
582 }
583 if (subsetFontStream) {
584 *fontStream = subsetFontStream;
585 return fontSize;
586 }
587#endif
588
589 // Fail over: just embed the whole font.
590 *fontStream = new SkPDFStream(fontData.get());
591 return fontSize;
592}
593
vandebo@chromium.org98594282011-07-25 22:34:12 +0000594///////////////////////////////////////////////////////////////////////////////
595// class SkPDFGlyphSet
596///////////////////////////////////////////////////////////////////////////////
597
598SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
599}
600
601void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
602 for (int i = 0; i < numGlyphs; ++i) {
603 fBitSet.setBit(glyphIDs[i], true);
604 }
605}
606
607bool SkPDFGlyphSet::has(uint16_t glyphID) const {
608 return fBitSet.isBitSet(glyphID);
609}
610
611void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
612 fBitSet.orBits(usage.fBitSet);
613}
614
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000615void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
616 fBitSet.exportTo(glyphIDs);
617}
618
vandebo@chromium.org98594282011-07-25 22:34:12 +0000619///////////////////////////////////////////////////////////////////////////////
620// class SkPDFGlyphSetMap
621///////////////////////////////////////////////////////////////////////////////
622SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
623 SkPDFGlyphSet* glyphSet)
624 : fFont(font),
625 fGlyphSet(glyphSet) {
626}
627
628SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
629 reset(map);
630}
631
632SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
633 if (fIndex >= fMap->count()) {
634 return NULL;
635 }
636 return &((*fMap)[fIndex++]);
637}
638
639void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
640 fMap = &(map.fMap);
641 fIndex = 0;
642}
643
644SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
645}
646
647SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
648 reset();
649}
650
651void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
652 for (int i = 0; i < usage.fMap.count(); ++i) {
653 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
654 myUsage->merge(*(usage.fMap[i].fGlyphSet));
655 }
656}
657
658void SkPDFGlyphSetMap::reset() {
659 for (int i = 0; i < fMap.count(); ++i) {
660 delete fMap[i].fGlyphSet; // Should not be NULL.
661 }
662 fMap.reset();
663}
664
665void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
666 int numGlyphs) {
667 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
668 if (subset) {
669 subset->set(glyphIDs, numGlyphs);
670 }
671}
672
673SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
674 int index = fMap.count();
675 for (int i = 0; i < index; ++i) {
676 if (fMap[i].fFont == font) {
677 return fMap[i].fGlyphSet;
678 }
679 }
680 fMap.append();
681 index = fMap.count() - 1;
682 fMap[index].fFont = font;
683 fMap[index].fGlyphSet = new SkPDFGlyphSet();
684 return fMap[index].fGlyphSet;
685}
686
687///////////////////////////////////////////////////////////////////////////////
688// class SkPDFFont
689///////////////////////////////////////////////////////////////////////////////
690
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000691/* Font subset design: It would be nice to be able to subset fonts
692 * (particularly type 3 fonts), but it's a lot of work and not a priority.
693 *
694 * Resources are canonicalized and uniqueified by pointer so there has to be
695 * some additional state indicating which subset of the font is used. It
696 * must be maintained at the page granularity and then combined at the document
697 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
698 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
699 * page/pdf device. c) in the document, retrieve the per font glyph usage
700 * from each page and combine it and ask for a resource with that subset.
701 */
702
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000703SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000704 SkAutoMutexAcquire lock(CanonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000705 int index;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000706 if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) {
707 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000708 }
709 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000710}
711
712void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000713 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000714}
715
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000716SkTypeface* SkPDFFont::typeface() {
717 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000718}
719
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000720SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000721 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000722}
723
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000724bool SkPDFFont::hasGlyph(uint16_t id) {
725 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
726}
727
vandebo@chromium.org01294102011-02-28 19:52:18 +0000728size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
729 size_t numGlyphs) {
730 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000731 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000732 return numGlyphs;
733 }
734
735 for (size_t i = 0; i < numGlyphs; i++) {
736 if (glyphIDs[i] == 0) {
737 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000738 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000739 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
740 return i;
741 }
742 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000743 }
744
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000745 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000746}
747
748// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000749SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
750 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000751 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000752 int relatedFontIndex;
753 if (Find(fontID, glyphID, &relatedFontIndex)) {
754 CanonicalFonts()[relatedFontIndex].fFont->ref();
755 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000756 }
757
vandebo@chromium.org98594282011-07-25 22:34:12 +0000758 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
759 SkPDFDict* relatedFontDescriptor = NULL;
760 if (relatedFontIndex >= 0) {
761 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000762 SkASSERT(relatedFont->fFontInfo.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +0000763 fontMetrics = relatedFont->fontInfo();
764 relatedFontDescriptor = relatedFont->getFontDescriptor();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000765 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000766 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000767 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000768 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
769 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000770#if !defined (SK_SFNTLY_SUBSETTER)
771 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
772 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
773#endif
774 fontMetrics =
775 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
776#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org98594282011-07-25 22:34:12 +0000777 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref.
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000778 if (fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
779 // Font does not support subsetting, get new info with advance.
780 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
781 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
782 fontMetrics =
783 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
784 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
785 }
786#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000787 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000788
vandebo@chromium.org98594282011-07-25 22:34:12 +0000789 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
790 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000791 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000792 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000793 return font; // Return the reference new SkPDFFont() created.
794}
795
vandebo@chromium.org98594282011-07-25 22:34:12 +0000796SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) {
797 return NULL; // Default: no support.
798}
799
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000800// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000801SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000802 // This initialization is only thread safe with gcc.
803 static SkTDArray<FontRec> gCanonicalFonts;
804 return gCanonicalFonts;
805}
806
807// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000808SkMutex& SkPDFFont::CanonicalFontsMutex() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000809 // This initialization is only thread safe with gcc.
810 static SkMutex gCanonicalFontsMutex;
811 return gCanonicalFontsMutex;
812}
813
814// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000815bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000816 // TODO(vandebo): Optimize this, do only one search?
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000817 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000818 *index = CanonicalFonts().find(search);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000819 if (*index >= 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000820 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000821 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000822 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000823 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000824 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000825}
826
vandebo@chromium.org98594282011-07-25 22:34:12 +0000827SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
828 uint16_t glyphID, bool descendantFont)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000829 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000830 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000831 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000832 fLastGlyphID(info ? info->fLastGlyphID : 0),
833 fFontInfo(info) {
834 if (info == NULL) {
835 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
836 } else if (info->fMultiMaster) {
837 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
838 } else {
839 fFontType = info->fType;
840 }
841}
842
843// static
844SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
845 SkTypeface* typeface, uint16_t glyphID,
846 SkPDFDict* relatedFontDescriptor) {
847 SkAdvancedTypefaceMetrics::FontType type =
848 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
849
850 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000851 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000852 return new SkPDFType3Font(info,
853 typeface,
854 glyphID,
855 relatedFontDescriptor);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000856 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000857 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
858 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
859 SkASSERT(relatedFontDescriptor == NULL);
860 return new SkPDFType0Font(info, typeface);
861 }
862 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
863 return new SkPDFType1Font(info,
864 typeface,
865 glyphID,
866 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000867 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000868
vandebo@chromium.org98594282011-07-25 22:34:12 +0000869 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
870 type == SkAdvancedTypefaceMetrics::kOther_Font ||
871 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000872
vandebo@chromium.org98594282011-07-25 22:34:12 +0000873 return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000874}
875
vandebo@chromium.org98594282011-07-25 22:34:12 +0000876SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
877 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000878}
879
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000880void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
881 if (info == NULL || info == fFontInfo.get()) {
882 return;
883 }
884 fFontInfo = info;
885}
886
vandebo@chromium.org98594282011-07-25 22:34:12 +0000887uint16_t SkPDFFont::firstGlyphID() const {
888 return fFirstGlyphID;
889}
890
891uint16_t SkPDFFont::lastGlyphID() const {
892 return fLastGlyphID;
893}
894
895void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
896 fLastGlyphID = glyphID;
897}
898
899void SkPDFFont::addResource(SkPDFObject* object) {
900 SkASSERT(object != NULL);
901 fResources.push(object);
902}
903
904SkPDFDict* SkPDFFont::getFontDescriptor() {
905 return fDescriptor.get();
906}
907
908void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
909 fDescriptor = descriptor;
910}
911
912bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
913 if (fDescriptor.get() == NULL) {
914 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000915 }
916
vandebo@chromium.org98594282011-07-25 22:34:12 +0000917 const uint16_t emSize = fFontInfo->fEmSize;
918
919 fDescriptor->insertName("FontName", fFontInfo->fFontName);
920 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
921 fDescriptor->insertScalar("Ascent",
922 scaleFromFontUnits(fFontInfo->fAscent, emSize));
923 fDescriptor->insertScalar("Descent",
924 scaleFromFontUnits(fFontInfo->fDescent, emSize));
925 fDescriptor->insertScalar("StemV",
926 scaleFromFontUnits(fFontInfo->fStemV, emSize));
927 fDescriptor->insertScalar("CapHeight",
928 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
929 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
930 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
931 fFontInfo->fEmSize))->unref();
932
933 if (defaultWidth > 0) {
934 fDescriptor->insertScalar("MissingWidth",
935 scaleFromFontUnits(defaultWidth, emSize));
936 }
937 return true;
938}
939
940void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
941 // Single byte glyph encoding supports a max of 255 glyphs.
942 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
943 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
944 fLastGlyphID = fFirstGlyphID + 255 - 1;
945 }
946}
947
948bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000949 if (fFontID != b.fFontID) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000950 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000951 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000952 if (fFont != NULL && b.fFont != NULL) {
953 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
954 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
955 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000956 if (fGlyphID == 0 || b.fGlyphID == 0) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000957 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000958 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000959
960 if (fFont != NULL) {
961 return fFont->fFirstGlyphID <= b.fGlyphID &&
962 b.fGlyphID <= fFont->fLastGlyphID;
963 } else if (b.fFont != NULL) {
964 return b.fFont->fFirstGlyphID <= fGlyphID &&
965 fGlyphID <= b.fFont->fLastGlyphID;
966 }
967 return fGlyphID == b.fGlyphID;
968}
969
970SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
971 : fFont(font),
972 fFontID(fontID),
973 fGlyphID(glyphID) {
974}
975
976void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
977 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
978 return;
979 }
980 SkRefPtr<SkPDFStream> pdfCmap =
981 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset);
982 addResource(pdfCmap.get()); // Pass reference from new.
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000983 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000984}
985
vandebo@chromium.org98594282011-07-25 22:34:12 +0000986///////////////////////////////////////////////////////////////////////////////
987// class SkPDFType0Font
988///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000989
vandebo@chromium.org98594282011-07-25 22:34:12 +0000990SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
991 SkTypeface* typeface)
992 : SkPDFFont(info, typeface, 0, false) {
993 SkDEBUGCODE(fPopulated = false);
994}
995
996SkPDFType0Font::~SkPDFType0Font() {}
997
998SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
999 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
1000 newSubset->populate(subset);
1001 return newSubset;
1002}
1003
1004#ifdef SK_DEBUG
1005void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
1006 bool indirect) {
1007 SkASSERT(fPopulated);
1008 return INHERITED::emitObject(stream, catalog, indirect);
1009}
1010#endif
1011
1012bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
1013 insertName("Subtype", "Type0");
1014 insertName("BaseFont", fontInfo()->fFontName);
1015 insertName("Encoding", "Identity-H");
1016
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001017 SkPDFCIDFont* newCIDFont;
1018 newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset);
1019
vandebo@chromium.org98594282011-07-25 22:34:12 +00001020 // Pass ref new created to fResources.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001021 addResource(newCIDFont);
1022 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
1023 descendantFonts->unref(); // SkRefPtr and new took a reference.
1024 descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref();
1025 insert("DescendantFonts", descendantFonts.get());
1026
1027 populateToUnicodeTable(subset);
1028
1029 SkDEBUGCODE(fPopulated = true);
1030 return true;
1031}
1032
1033///////////////////////////////////////////////////////////////////////////////
1034// class SkPDFCIDFont
1035///////////////////////////////////////////////////////////////////////////////
1036
1037SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1038 SkTypeface* typeface, const SkPDFGlyphSet* subset)
1039 : SkPDFFont(info, typeface, 0, true) {
1040 populate(subset);
1041}
1042
1043SkPDFCIDFont::~SkPDFCIDFont() {}
1044
1045bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001046 const SkTDArray<uint32_t>* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001047 SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor");
1048 descriptor->unref(); // SkRefPtr and new both took a ref.
1049 setFontDescriptor(descriptor.get());
1050
1051 switch (getType()) {
1052 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001053 SkASSERT(subset);
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001054 // Font subsetting
1055 SkPDFStream* rawStream = NULL;
1056 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1057 typeface(),
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001058 *subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001059 &rawStream);
1060 SkASSERT(fontSize);
1061 SkASSERT(rawStream);
1062 SkRefPtr<SkPDFStream> fontStream = rawStream;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001063 // SkRefPtr and new both ref()'d fontStream, pass one.
1064 addResource(fontStream.get());
1065
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001066 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001067 descriptor->insert("FontFile2",
1068 new SkPDFObjRef(fontStream.get()))->unref();
1069 break;
1070 }
1071 case SkAdvancedTypefaceMetrics::kCFF_Font:
1072 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1073 SkRefPtr<SkStream> fontData =
1074 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1075 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
1076 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
1077 // SkRefPtr and new both ref()'d fontStream, pass one.
1078 addResource(fontStream.get());
1079
1080 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1081 fontStream->insertName("Subtype", "Type1C");
1082 } else {
1083 fontStream->insertName("Subtype", "CIDFontType0c");
1084 }
1085 descriptor->insert("FontFile3",
1086 new SkPDFObjRef(fontStream.get()))->unref();
1087 break;
1088 }
1089 default:
1090 SkASSERT(false);
1091 }
1092
1093 addResource(descriptor.get());
1094 descriptor->ref();
1095
1096 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1097 return addCommonFontDescriptorEntries(defaultWidth);
1098}
1099
1100bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001101 // Generate new font metrics with advance info for true type fonts.
1102 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1103 // Generate glyph id array.
1104 SkTDArray<uint32_t> glyphIDs;
1105 glyphIDs.push(0); // Always include glyph 0.
1106 if (subset) {
1107 subset->exportTo(&glyphIDs);
1108 }
1109
1110 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
1111 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
1112 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
1113 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
1114 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
1115 uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin();
1116 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
1117 fontMetrics =
1118 SkFontHost::GetAdvancedTypefaceMetrics(
1119 SkTypeface::UniqueID(typeface()),
1120 info,
1121 glyphs,
1122 glyphsCount);
1123 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
1124 setFontInfo(fontMetrics.get());
1125 addFontDescriptor(0, &glyphIDs);
1126 } else {
1127 // Other CID fonts
1128 addFontDescriptor(0, NULL);
1129 }
1130
vandebo@chromium.org98594282011-07-25 22:34:12 +00001131 insertName("BaseFont", fontInfo()->fFontName);
1132
1133 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001134 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001135 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001136 insertName("Subtype", "CIDFontType2");
1137 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001138 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001139 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001140 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001141
1142 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
1143 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001144 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1145 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001146 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001147 insert("CIDSystemInfo", sysInfo.get());
1148
vandebo@chromium.org98594282011-07-25 22:34:12 +00001149 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001150 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001151 SkRefPtr<SkPDFArray> widths =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001152 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1153 fontInfo()->fEmSize, &appendWidth,
1154 &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001155 widths->unref(); // SkRefPtr and compose both took a reference.
1156 if (widths->size())
1157 insert("W", widths.get());
1158 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001159 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001160 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001161 }
1162 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001163 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001164 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001165 defaultAdvance.fVerticalAdvance = 0;
1166 defaultAdvance.fOriginXDisp = 0;
1167 defaultAdvance.fOriginYDisp = 0;
1168 SkRefPtr<SkPDFArray> advances =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001169 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1170 fontInfo()->fEmSize, &appendVerticalAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001171 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001172 advances->unref(); // SkRefPtr and compose both took a ref.
1173 if (advances->size())
1174 insert("W2", advances.get());
1175 if (defaultAdvance.fVerticalAdvance ||
1176 defaultAdvance.fOriginXDisp ||
1177 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001178 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001179 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001180 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001181 }
1182 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001183
1184 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001185}
1186
vandebo@chromium.org98594282011-07-25 22:34:12 +00001187///////////////////////////////////////////////////////////////////////////////
1188// class SkPDFType1Font
1189///////////////////////////////////////////////////////////////////////////////
1190
1191SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1192 SkTypeface* typeface,
1193 uint16_t glyphID,
1194 SkPDFDict* relatedFontDescriptor)
1195 : SkPDFFont(info, typeface, glyphID, false) {
1196 populate(glyphID);
1197}
1198
1199SkPDFType1Font::~SkPDFType1Font() {}
1200
1201bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
1202 SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
1203 if (descriptor.get() != NULL) {
1204 addResource(descriptor.get());
1205 descriptor->ref();
1206 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1207 return true;
1208 }
1209
1210 descriptor = new SkPDFDict("FontDescriptor");
1211 descriptor->unref(); // SkRefPtr and new both took a ref.
1212 setFontDescriptor(descriptor.get());
1213
1214 size_t header SK_INIT_TO_AVOID_WARNING;
1215 size_t data SK_INIT_TO_AVOID_WARNING;
1216 size_t trailer SK_INIT_TO_AVOID_WARNING;
1217 SkRefPtr<SkStream> rawFontData =
1218 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1219 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
1220 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1221 &trailer);
1222 if (fontData == NULL) {
1223 return false;
1224 }
1225 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
1226 // SkRefPtr and new both ref()'d fontStream, pass one.
1227 addResource(fontStream.get());
1228 fontStream->insertInt("Length1", header);
1229 fontStream->insertInt("Length2", data);
1230 fontStream->insertInt("Length3", trailer);
1231 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
1232
1233 addResource(descriptor.get());
1234 descriptor->ref();
1235 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1236
1237 return addCommonFontDescriptorEntries(defaultWidth);
1238}
1239
1240bool SkPDFType1Font::populate(int16_t glyphID) {
1241 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1242 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001243
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001244 adjustGlyphRangeForSingleByteEncoding(glyphID);
1245
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001246 int16_t defaultWidth = 0;
1247 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1248 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001249 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001250 widthEntry != NULL;
1251 widthEntry = widthEntry->fNext.get()) {
1252 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001253 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001254 defaultWidth = widthEntry->fAdvance[0];
1255 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001256 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001257 SkASSERT(false);
1258 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001259 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001260 SkASSERT(widthRangeEntry == NULL);
1261 widthRangeEntry = widthEntry;
1262 break;
1263 }
1264 }
1265
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001266 if (!addFontDescriptor(defaultWidth)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001267 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001268 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001269
reed@google.comc789cf12011-07-20 12:14:33 +00001270 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001271 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001272
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001273 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001274
1275 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001276 encoding->unref(); // SkRefPtr and new both took a reference.
1277 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001278
1279 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1280 encDiffs->unref(); // SkRefPtr and new both took a reference.
1281 encoding->insert("Differences", encDiffs.get());
1282
vandebo@chromium.org98594282011-07-25 22:34:12 +00001283 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001284 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001285 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1286 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001287 }
1288
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001289 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001290}
1291
vandebo@chromium.org98594282011-07-25 22:34:12 +00001292void SkPDFType1Font::addWidthInfoFromRange(
1293 int16_t defaultWidth,
1294 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
1295 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1296 widthArray->unref(); // SkRefPtr and new both took a ref.
1297 int firstChar = 0;
1298 if (widthRangeEntry) {
1299 const uint16_t emSize = fontInfo()->fEmSize;
1300 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1301 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1302 if (startIndex < 0)
1303 startIndex = 0;
1304 if (endIndex > widthRangeEntry->fAdvance.count())
1305 endIndex = widthRangeEntry->fAdvance.count();
1306 if (widthRangeEntry->fStartId == 0) {
1307 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1308 } else {
1309 firstChar = startIndex + widthRangeEntry->fStartId;
1310 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001311 for (int i = startIndex; i < endIndex; i++) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001312 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001313 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001314 } else {
1315 appendWidth(defaultWidth, 1000, widthArray.get());
1316 }
1317 insertInt("FirstChar", firstChar);
1318 insertInt("LastChar", firstChar + widthArray->size() - 1);
1319 insert("Widths", widthArray.get());
1320}
1321
1322///////////////////////////////////////////////////////////////////////////////
1323// class SkPDFType3Font
1324///////////////////////////////////////////////////////////////////////////////
1325
1326SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1327 SkTypeface* typeface,
1328 uint16_t glyphID,
1329 SkPDFDict* relatedFontDescriptor)
1330 : SkPDFFont(info, typeface, glyphID, false) {
1331 populate(glyphID);
1332}
1333
1334SkPDFType3Font::~SkPDFType3Font() {}
1335
1336bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001337 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001338 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001339 paint.setTextSize(1000);
1340 SkAutoGlyphCache autoCache(paint, NULL);
1341 SkGlyphCache* cache = autoCache.getCache();
1342 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001343 if (lastGlyphID() == 0) {
1344 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001345 }
1346
1347 adjustGlyphRangeForSingleByteEncoding(glyphID);
1348
reed@google.comc789cf12011-07-20 12:14:33 +00001349 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001350 // Flip about the x-axis and scale by 1/1000.
1351 SkMatrix fontMatrix;
1352 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1353 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1354
1355 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
1356 charProcs->unref(); // SkRefPtr and new both took a reference.
1357 insert("CharProcs", charProcs.get());
1358
1359 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
1360 encoding->unref(); // SkRefPtr and new both took a reference.
1361 insert("Encoding", encoding.get());
1362
1363 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1364 encDiffs->unref(); // SkRefPtr and new both took a reference.
1365 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001366 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001367 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001368
1369 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1370 widthArray->unref(); // SkRefPtr and new both took a ref.
1371
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001372 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001373 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001374 SkString characterName;
1375 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001376 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001377
reed@google.comce11b262011-03-21 21:25:35 +00001378 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001379 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001380 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1381 glyph.fWidth, glyph.fHeight);
1382 bbox.join(glyphBBox);
1383
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001384 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001385 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1386 &content);
1387 const SkPath* path = cache->findPath(glyph);
1388 if (path) {
1389 SkPDFUtils::EmitPath(*path, &content);
1390 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1391 &content);
1392 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001393 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001394 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +00001395 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001396
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001397 SkRefPtr<SkPDFStream> glyphDescription =
1398 new SkPDFStream(glyphStream.get());
1399 // SkRefPtr and new both ref()'d charProcs, pass one.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001400 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001401 charProcs->insert(characterName.c_str(),
1402 new SkPDFObjRef(glyphDescription.get()))->unref();
1403 }
1404
1405 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001406 insertInt("FirstChar", firstGlyphID());
1407 insertInt("LastChar", lastGlyphID());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001408 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001409 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001410
vandebo@chromium.org98594282011-07-25 22:34:12 +00001411 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001412 return true;
1413}