blob: 39712811b737e9cda7808a361ab7a0c4a956cd86 [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.
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000466
467// FIXME: this should be in a header so that it is separately testable
468// ( see caller in tests/ToUnicode.cpp )
469void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
470 const SkPDFGlyphSet* subset,
471 SkDynamicMemoryWStream* cmap);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000472
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000473void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
474 const SkPDFGlyphSet* subset,
475 SkDynamicMemoryWStream* cmap) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000476 if (glyphToUnicode.isEmpty()) {
477 return;
478 }
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000479
480 SkTDArray<BFChar> bfcharEntries;
481 SkTDArray<BFRange> bfrangeEntries;
482
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000483 BFRange currentRangeEntry = {0, 0, 0};
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000484 bool rangeEmpty = true;
485 const int count = glyphToUnicode.count();
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000486
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000487 for (int i = 0; i < count + 1; ++i) {
488 bool inSubset = i < count && (subset == NULL || subset->has(i));
489 if (!rangeEmpty) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000490 // PDF spec requires bfrange not changing the higher byte,
491 // e.g. <1035> <10FF> <2222> is ok, but
492 // <1035> <1100> <2222> is no good
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000493 bool inRange =
494 i == currentRangeEntry.fEnd + 1 &&
495 i >> 8 == currentRangeEntry.fStart >> 8 &&
496 i < count &&
497 glyphToUnicode[i] == currentRangeEntry.fUnicode + i -
498 currentRangeEntry.fStart;
499 if (!inSubset || !inRange) {
500 if (currentRangeEntry.fEnd > currentRangeEntry.fStart) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000501 bfrangeEntries.push(currentRangeEntry);
502 } else {
503 BFChar* entry = bfcharEntries.append();
504 entry->fGlyphId = currentRangeEntry.fStart;
505 entry->fUnicode = currentRangeEntry.fUnicode;
506 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000507 rangeEmpty = true;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000508 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000509 }
510 if (inSubset) {
511 currentRangeEntry.fEnd = i;
512 if (rangeEmpty) {
513 currentRangeEntry.fStart = i;
514 currentRangeEntry.fUnicode = glyphToUnicode[i];
515 rangeEmpty = false;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000516 }
517 }
518 }
519
520 // The spec requires all bfchar entries for a font must come before bfrange
521 // entries.
522 append_bfchar_section(bfcharEntries, cmap);
523 append_bfrange_section(bfrangeEntries, cmap);
524}
525
vandebo@chromium.org98594282011-07-25 22:34:12 +0000526static SkPDFStream* generate_tounicode_cmap(
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000527 const SkTDArray<SkUnichar>& glyphToUnicode,
528 const SkPDFGlyphSet* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000529 SkDynamicMemoryWStream cmap;
530 append_tounicode_header(&cmap);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000531 append_cmap_sections(glyphToUnicode, subset, &cmap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000532 append_cmap_footer(&cmap);
533 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
534 cmapStream->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000535 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000536 return new SkPDFStream(cmapStream.get());
537}
538
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000539#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000540static void sk_delete_array(const void* ptr, size_t, void*) {
541 // Use C-style cast to cast away const and cast type simultaneously.
542 delete[] (unsigned char*)ptr;
543}
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000544#endif
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000545
546static int get_subset_font_stream(const char* fontName,
547 const SkTypeface* typeface,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000548 const SkTDArray<uint32_t>& subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000549 SkPDFStream** fontStream) {
550 SkRefPtr<SkStream> fontData =
551 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface));
552 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
553
554 int fontSize = fontData->getLength();
555
556#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000557 // Read font into buffer.
558 SkPDFStream* subsetFontStream = NULL;
559 SkTDArray<unsigned char> originalFont;
560 originalFont.setCount(fontSize);
561 if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) {
562 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000563 // sfntly requires unsigned int* to be passed in, as far as we know,
564 // unsigned int is equivalent to uint32_t on all platforms.
565 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
566 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000567 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
568 originalFont.begin(),
569 fontSize,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000570 subset.begin(),
571 subset.count(),
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000572 &subsetFont);
573 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000574 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
575 subsetFontSize,
576 sk_delete_array,
577 NULL));
578 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000579 fontSize = subsetFontSize;
580 }
581 }
582 if (subsetFontStream) {
583 *fontStream = subsetFontStream;
584 return fontSize;
585 }
586#endif
587
588 // Fail over: just embed the whole font.
589 *fontStream = new SkPDFStream(fontData.get());
590 return fontSize;
591}
592
vandebo@chromium.org98594282011-07-25 22:34:12 +0000593///////////////////////////////////////////////////////////////////////////////
594// class SkPDFGlyphSet
595///////////////////////////////////////////////////////////////////////////////
596
597SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
598}
599
600void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
601 for (int i = 0; i < numGlyphs; ++i) {
602 fBitSet.setBit(glyphIDs[i], true);
603 }
604}
605
606bool SkPDFGlyphSet::has(uint16_t glyphID) const {
607 return fBitSet.isBitSet(glyphID);
608}
609
610void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
611 fBitSet.orBits(usage.fBitSet);
612}
613
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000614void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
615 fBitSet.exportTo(glyphIDs);
616}
617
vandebo@chromium.org98594282011-07-25 22:34:12 +0000618///////////////////////////////////////////////////////////////////////////////
619// class SkPDFGlyphSetMap
620///////////////////////////////////////////////////////////////////////////////
621SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
622 SkPDFGlyphSet* glyphSet)
623 : fFont(font),
624 fGlyphSet(glyphSet) {
625}
626
627SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
628 reset(map);
629}
630
631SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
632 if (fIndex >= fMap->count()) {
633 return NULL;
634 }
635 return &((*fMap)[fIndex++]);
636}
637
638void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
639 fMap = &(map.fMap);
640 fIndex = 0;
641}
642
643SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
644}
645
646SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
647 reset();
648}
649
650void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
651 for (int i = 0; i < usage.fMap.count(); ++i) {
652 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
653 myUsage->merge(*(usage.fMap[i].fGlyphSet));
654 }
655}
656
657void SkPDFGlyphSetMap::reset() {
658 for (int i = 0; i < fMap.count(); ++i) {
659 delete fMap[i].fGlyphSet; // Should not be NULL.
660 }
661 fMap.reset();
662}
663
664void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
665 int numGlyphs) {
666 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
667 if (subset) {
668 subset->set(glyphIDs, numGlyphs);
669 }
670}
671
672SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
673 int index = fMap.count();
674 for (int i = 0; i < index; ++i) {
675 if (fMap[i].fFont == font) {
676 return fMap[i].fGlyphSet;
677 }
678 }
679 fMap.append();
680 index = fMap.count() - 1;
681 fMap[index].fFont = font;
682 fMap[index].fGlyphSet = new SkPDFGlyphSet();
683 return fMap[index].fGlyphSet;
684}
685
686///////////////////////////////////////////////////////////////////////////////
687// class SkPDFFont
688///////////////////////////////////////////////////////////////////////////////
689
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000690/* Font subset design: It would be nice to be able to subset fonts
691 * (particularly type 3 fonts), but it's a lot of work and not a priority.
692 *
693 * Resources are canonicalized and uniqueified by pointer so there has to be
694 * some additional state indicating which subset of the font is used. It
695 * must be maintained at the page granularity and then combined at the document
696 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
697 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
698 * page/pdf device. c) in the document, retrieve the per font glyph usage
699 * from each page and combine it and ask for a resource with that subset.
700 */
701
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000702SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000703 SkAutoMutexAcquire lock(CanonicalFontsMutex());
edisonn@google.com022e8572012-10-23 21:32:39 +0000704 int index = -1;
705 for (int i = 0 ; i < CanonicalFonts().count() ; i++) {
706 if (CanonicalFonts()[i].fFont == this) {
707 index = i;
708 }
709 }
710
711 SkDEBUGCODE(int indexFound;)
712 SkASSERT(index == -1 ||
713 (Find(SkTypeface::UniqueID(fTypeface.get()),
714 fFirstGlyphID,
715 &indexFound) &&
716 index == indexFound));
717 if (index >= 0) {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000718 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000719 }
720 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000721}
722
723void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000724 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000725}
726
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000727SkTypeface* SkPDFFont::typeface() {
728 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000729}
730
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000731SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000732 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000733}
734
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000735bool SkPDFFont::hasGlyph(uint16_t id) {
736 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
737}
738
vandebo@chromium.org01294102011-02-28 19:52:18 +0000739size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
740 size_t numGlyphs) {
741 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000742 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000743 return numGlyphs;
744 }
745
746 for (size_t i = 0; i < numGlyphs; i++) {
747 if (glyphIDs[i] == 0) {
748 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000749 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000750 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
751 return i;
752 }
753 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000754 }
755
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000756 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000757}
758
759// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000760SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
761 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000762 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000763 int relatedFontIndex;
764 if (Find(fontID, glyphID, &relatedFontIndex)) {
765 CanonicalFonts()[relatedFontIndex].fFont->ref();
766 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000767 }
768
vandebo@chromium.org98594282011-07-25 22:34:12 +0000769 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
770 SkPDFDict* relatedFontDescriptor = NULL;
771 if (relatedFontIndex >= 0) {
772 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000773 fontMetrics = relatedFont->fontInfo();
774 relatedFontDescriptor = relatedFont->getFontDescriptor();
edisonn@google.com022e8572012-10-23 21:32:39 +0000775
776 // This only is to catch callers who pass invalid glyph ids.
777 // If glyph id is invalid, then we will create duplicate entries
778 // for True Type fonts.
779 SkAdvancedTypefaceMetrics::FontType fontType =
780 fontMetrics.get() ? fontMetrics.get()->fType :
781 SkAdvancedTypefaceMetrics::kOther_Font;
782
783 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
784 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
785 CanonicalFonts()[relatedFontIndex].fFont->ref();
786 return CanonicalFonts()[relatedFontIndex].fFont;
787 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000788 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000789 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000790 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000791 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
792 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000793#if !defined (SK_SFNTLY_SUBSETTER)
794 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
795 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
796#endif
797 fontMetrics =
798 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000799 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref.
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000800#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.orgb3b46552011-10-17 23:22:49 +0000801 if (fontMetrics &&
802 fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000803 // Font does not support subsetting, get new info with advance.
804 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
805 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
806 fontMetrics =
807 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
808 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
809 }
810#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000811 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000812
vandebo@chromium.org98594282011-07-25 22:34:12 +0000813 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
814 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000815 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000816 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000817 return font; // Return the reference new SkPDFFont() created.
818}
819
vandebo@chromium.org98594282011-07-25 22:34:12 +0000820SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) {
821 return NULL; // Default: no support.
822}
823
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000824// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000825SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000826 // This initialization is only thread safe with gcc.
827 static SkTDArray<FontRec> gCanonicalFonts;
828 return gCanonicalFonts;
829}
830
831// static
digit@google.com1771cbf2012-01-26 21:26:40 +0000832SkBaseMutex& SkPDFFont::CanonicalFontsMutex() {
833 // This initialization is only thread safe with gcc, or when
834 // POD-style mutex initialization is used.
835 SK_DECLARE_STATIC_MUTEX(gCanonicalFontsMutex);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000836 return gCanonicalFontsMutex;
837}
838
839// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000840bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000841 // TODO(vandebo): Optimize this, do only one search?
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000842 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000843 *index = CanonicalFonts().find(search);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000844 if (*index >= 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000845 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000846 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000847 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000848 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000849 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000850}
851
vandebo@chromium.org98594282011-07-25 22:34:12 +0000852SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
853 uint16_t glyphID, bool descendantFont)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000854 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000855 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000856 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000857 fLastGlyphID(info ? info->fLastGlyphID : 0),
858 fFontInfo(info) {
859 if (info == NULL) {
860 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
861 } else if (info->fMultiMaster) {
862 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
863 } else {
864 fFontType = info->fType;
865 }
866}
867
868// static
869SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
870 SkTypeface* typeface, uint16_t glyphID,
871 SkPDFDict* relatedFontDescriptor) {
872 SkAdvancedTypefaceMetrics::FontType type =
873 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
874
875 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000876 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000877 return new SkPDFType3Font(info,
878 typeface,
879 glyphID,
880 relatedFontDescriptor);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000881 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000882 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
883 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
884 SkASSERT(relatedFontDescriptor == NULL);
885 return new SkPDFType0Font(info, typeface);
886 }
887 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
888 return new SkPDFType1Font(info,
889 typeface,
890 glyphID,
891 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000892 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000893
vandebo@chromium.org98594282011-07-25 22:34:12 +0000894 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
895 type == SkAdvancedTypefaceMetrics::kOther_Font ||
896 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000897
vandebo@chromium.org98594282011-07-25 22:34:12 +0000898 return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000899}
900
vandebo@chromium.org98594282011-07-25 22:34:12 +0000901SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
902 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000903}
904
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000905void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
906 if (info == NULL || info == fFontInfo.get()) {
907 return;
908 }
909 fFontInfo = info;
910}
911
vandebo@chromium.org98594282011-07-25 22:34:12 +0000912uint16_t SkPDFFont::firstGlyphID() const {
913 return fFirstGlyphID;
914}
915
916uint16_t SkPDFFont::lastGlyphID() const {
917 return fLastGlyphID;
918}
919
920void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
921 fLastGlyphID = glyphID;
922}
923
924void SkPDFFont::addResource(SkPDFObject* object) {
925 SkASSERT(object != NULL);
926 fResources.push(object);
927}
928
929SkPDFDict* SkPDFFont::getFontDescriptor() {
930 return fDescriptor.get();
931}
932
933void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
934 fDescriptor = descriptor;
935}
936
937bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
938 if (fDescriptor.get() == NULL) {
939 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000940 }
941
vandebo@chromium.org98594282011-07-25 22:34:12 +0000942 const uint16_t emSize = fFontInfo->fEmSize;
943
944 fDescriptor->insertName("FontName", fFontInfo->fFontName);
945 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
946 fDescriptor->insertScalar("Ascent",
947 scaleFromFontUnits(fFontInfo->fAscent, emSize));
948 fDescriptor->insertScalar("Descent",
949 scaleFromFontUnits(fFontInfo->fDescent, emSize));
950 fDescriptor->insertScalar("StemV",
951 scaleFromFontUnits(fFontInfo->fStemV, emSize));
952 fDescriptor->insertScalar("CapHeight",
953 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
954 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
955 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
956 fFontInfo->fEmSize))->unref();
957
958 if (defaultWidth > 0) {
959 fDescriptor->insertScalar("MissingWidth",
960 scaleFromFontUnits(defaultWidth, emSize));
961 }
962 return true;
963}
964
965void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
966 // Single byte glyph encoding supports a max of 255 glyphs.
967 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
968 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
969 fLastGlyphID = fFirstGlyphID + 255 - 1;
970 }
971}
972
973bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000974 if (fFontID != b.fFontID) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000975 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000976 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000977 if (fFont != NULL && b.fFont != NULL) {
978 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
979 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
980 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000981 if (fGlyphID == 0 || b.fGlyphID == 0) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000982 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000983 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000984
985 if (fFont != NULL) {
986 return fFont->fFirstGlyphID <= b.fGlyphID &&
987 b.fGlyphID <= fFont->fLastGlyphID;
988 } else if (b.fFont != NULL) {
989 return b.fFont->fFirstGlyphID <= fGlyphID &&
990 fGlyphID <= b.fFont->fLastGlyphID;
991 }
992 return fGlyphID == b.fGlyphID;
993}
994
995SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
996 : fFont(font),
997 fFontID(fontID),
998 fGlyphID(glyphID) {
999}
1000
1001void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
1002 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
1003 return;
1004 }
1005 SkRefPtr<SkPDFStream> pdfCmap =
1006 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset);
1007 addResource(pdfCmap.get()); // Pass reference from new.
vandebo@chromium.org6744d492011-05-09 18:13:47 +00001008 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001009}
1010
vandebo@chromium.org98594282011-07-25 22:34:12 +00001011///////////////////////////////////////////////////////////////////////////////
1012// class SkPDFType0Font
1013///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001014
vandebo@chromium.org98594282011-07-25 22:34:12 +00001015SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
1016 SkTypeface* typeface)
1017 : SkPDFFont(info, typeface, 0, false) {
1018 SkDEBUGCODE(fPopulated = false);
1019}
1020
1021SkPDFType0Font::~SkPDFType0Font() {}
1022
1023SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
1024 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
1025 newSubset->populate(subset);
1026 return newSubset;
1027}
1028
1029#ifdef SK_DEBUG
1030void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
1031 bool indirect) {
1032 SkASSERT(fPopulated);
1033 return INHERITED::emitObject(stream, catalog, indirect);
1034}
1035#endif
1036
1037bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
1038 insertName("Subtype", "Type0");
1039 insertName("BaseFont", fontInfo()->fFontName);
1040 insertName("Encoding", "Identity-H");
1041
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001042 SkPDFCIDFont* newCIDFont;
1043 newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset);
1044
vandebo@chromium.org98594282011-07-25 22:34:12 +00001045 // Pass ref new created to fResources.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001046 addResource(newCIDFont);
1047 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
1048 descendantFonts->unref(); // SkRefPtr and new took a reference.
1049 descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref();
1050 insert("DescendantFonts", descendantFonts.get());
1051
1052 populateToUnicodeTable(subset);
1053
1054 SkDEBUGCODE(fPopulated = true);
1055 return true;
1056}
1057
1058///////////////////////////////////////////////////////////////////////////////
1059// class SkPDFCIDFont
1060///////////////////////////////////////////////////////////////////////////////
1061
1062SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1063 SkTypeface* typeface, const SkPDFGlyphSet* subset)
1064 : SkPDFFont(info, typeface, 0, true) {
1065 populate(subset);
1066}
1067
1068SkPDFCIDFont::~SkPDFCIDFont() {}
1069
1070bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001071 const SkTDArray<uint32_t>* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001072 SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor");
1073 descriptor->unref(); // SkRefPtr and new both took a ref.
1074 setFontDescriptor(descriptor.get());
1075
1076 switch (getType()) {
1077 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001078 SkASSERT(subset);
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001079 // Font subsetting
1080 SkPDFStream* rawStream = NULL;
1081 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1082 typeface(),
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001083 *subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001084 &rawStream);
1085 SkASSERT(fontSize);
1086 SkASSERT(rawStream);
1087 SkRefPtr<SkPDFStream> fontStream = rawStream;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001088 // SkRefPtr and new both ref()'d fontStream, pass one.
1089 addResource(fontStream.get());
1090
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001091 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001092 descriptor->insert("FontFile2",
1093 new SkPDFObjRef(fontStream.get()))->unref();
1094 break;
1095 }
1096 case SkAdvancedTypefaceMetrics::kCFF_Font:
1097 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1098 SkRefPtr<SkStream> fontData =
1099 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1100 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
1101 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
1102 // SkRefPtr and new both ref()'d fontStream, pass one.
1103 addResource(fontStream.get());
1104
1105 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1106 fontStream->insertName("Subtype", "Type1C");
1107 } else {
1108 fontStream->insertName("Subtype", "CIDFontType0c");
1109 }
1110 descriptor->insert("FontFile3",
1111 new SkPDFObjRef(fontStream.get()))->unref();
1112 break;
1113 }
1114 default:
1115 SkASSERT(false);
1116 }
1117
1118 addResource(descriptor.get());
1119 descriptor->ref();
1120
1121 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1122 return addCommonFontDescriptorEntries(defaultWidth);
1123}
1124
1125bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001126 // Generate new font metrics with advance info for true type fonts.
1127 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1128 // Generate glyph id array.
1129 SkTDArray<uint32_t> glyphIDs;
1130 glyphIDs.push(0); // Always include glyph 0.
1131 if (subset) {
1132 subset->exportTo(&glyphIDs);
1133 }
1134
1135 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
1136 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
1137 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
1138 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
1139 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
1140 uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin();
1141 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
1142 fontMetrics =
1143 SkFontHost::GetAdvancedTypefaceMetrics(
1144 SkTypeface::UniqueID(typeface()),
1145 info,
1146 glyphs,
1147 glyphsCount);
1148 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
1149 setFontInfo(fontMetrics.get());
1150 addFontDescriptor(0, &glyphIDs);
1151 } else {
1152 // Other CID fonts
1153 addFontDescriptor(0, NULL);
1154 }
1155
vandebo@chromium.org98594282011-07-25 22:34:12 +00001156 insertName("BaseFont", fontInfo()->fFontName);
1157
1158 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001159 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001160 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001161 insertName("Subtype", "CIDFontType2");
1162 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001163 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001164 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001165 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001166
1167 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
1168 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001169 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1170 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001171 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001172 insert("CIDSystemInfo", sysInfo.get());
1173
vandebo@chromium.org98594282011-07-25 22:34:12 +00001174 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001175 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001176 SkRefPtr<SkPDFArray> widths =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001177 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1178 fontInfo()->fEmSize, &appendWidth,
1179 &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001180 widths->unref(); // SkRefPtr and compose both took a reference.
1181 if (widths->size())
1182 insert("W", widths.get());
1183 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001184 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001185 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001186 }
1187 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001188 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001189 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001190 defaultAdvance.fVerticalAdvance = 0;
1191 defaultAdvance.fOriginXDisp = 0;
1192 defaultAdvance.fOriginYDisp = 0;
1193 SkRefPtr<SkPDFArray> advances =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001194 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1195 fontInfo()->fEmSize, &appendVerticalAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001196 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001197 advances->unref(); // SkRefPtr and compose both took a ref.
1198 if (advances->size())
1199 insert("W2", advances.get());
1200 if (defaultAdvance.fVerticalAdvance ||
1201 defaultAdvance.fOriginXDisp ||
1202 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001203 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001204 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001205 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001206 }
1207 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001208
1209 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001210}
1211
vandebo@chromium.org98594282011-07-25 22:34:12 +00001212///////////////////////////////////////////////////////////////////////////////
1213// class SkPDFType1Font
1214///////////////////////////////////////////////////////////////////////////////
1215
1216SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1217 SkTypeface* typeface,
1218 uint16_t glyphID,
1219 SkPDFDict* relatedFontDescriptor)
1220 : SkPDFFont(info, typeface, glyphID, false) {
1221 populate(glyphID);
1222}
1223
1224SkPDFType1Font::~SkPDFType1Font() {}
1225
1226bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
1227 SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
1228 if (descriptor.get() != NULL) {
1229 addResource(descriptor.get());
1230 descriptor->ref();
1231 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1232 return true;
1233 }
1234
1235 descriptor = new SkPDFDict("FontDescriptor");
1236 descriptor->unref(); // SkRefPtr and new both took a ref.
1237 setFontDescriptor(descriptor.get());
1238
1239 size_t header SK_INIT_TO_AVOID_WARNING;
1240 size_t data SK_INIT_TO_AVOID_WARNING;
1241 size_t trailer SK_INIT_TO_AVOID_WARNING;
1242 SkRefPtr<SkStream> rawFontData =
1243 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1244 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
1245 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1246 &trailer);
1247 if (fontData == NULL) {
1248 return false;
1249 }
1250 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
1251 // SkRefPtr and new both ref()'d fontStream, pass one.
1252 addResource(fontStream.get());
1253 fontStream->insertInt("Length1", header);
1254 fontStream->insertInt("Length2", data);
1255 fontStream->insertInt("Length3", trailer);
1256 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
1257
1258 addResource(descriptor.get());
1259 descriptor->ref();
1260 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1261
1262 return addCommonFontDescriptorEntries(defaultWidth);
1263}
1264
1265bool SkPDFType1Font::populate(int16_t glyphID) {
1266 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1267 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001268
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001269 adjustGlyphRangeForSingleByteEncoding(glyphID);
1270
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001271 int16_t defaultWidth = 0;
1272 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1273 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001274 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001275 widthEntry != NULL;
1276 widthEntry = widthEntry->fNext.get()) {
1277 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001278 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001279 defaultWidth = widthEntry->fAdvance[0];
1280 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001281 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001282 SkASSERT(false);
1283 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001284 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001285 SkASSERT(widthRangeEntry == NULL);
1286 widthRangeEntry = widthEntry;
1287 break;
1288 }
1289 }
1290
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001291 if (!addFontDescriptor(defaultWidth)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001292 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001293 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001294
reed@google.comc789cf12011-07-20 12:14:33 +00001295 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001296 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001297
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001298 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001299
1300 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001301 encoding->unref(); // SkRefPtr and new both took a reference.
1302 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001303
1304 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1305 encDiffs->unref(); // SkRefPtr and new both took a reference.
1306 encoding->insert("Differences", encDiffs.get());
1307
vandebo@chromium.org98594282011-07-25 22:34:12 +00001308 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001309 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001310 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1311 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001312 }
1313
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001314 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001315}
1316
vandebo@chromium.org98594282011-07-25 22:34:12 +00001317void SkPDFType1Font::addWidthInfoFromRange(
1318 int16_t defaultWidth,
1319 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
1320 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1321 widthArray->unref(); // SkRefPtr and new both took a ref.
1322 int firstChar = 0;
1323 if (widthRangeEntry) {
1324 const uint16_t emSize = fontInfo()->fEmSize;
1325 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1326 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1327 if (startIndex < 0)
1328 startIndex = 0;
1329 if (endIndex > widthRangeEntry->fAdvance.count())
1330 endIndex = widthRangeEntry->fAdvance.count();
1331 if (widthRangeEntry->fStartId == 0) {
1332 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1333 } else {
1334 firstChar = startIndex + widthRangeEntry->fStartId;
1335 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001336 for (int i = startIndex; i < endIndex; i++) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001337 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001338 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001339 } else {
1340 appendWidth(defaultWidth, 1000, widthArray.get());
1341 }
1342 insertInt("FirstChar", firstChar);
1343 insertInt("LastChar", firstChar + widthArray->size() - 1);
1344 insert("Widths", widthArray.get());
1345}
1346
1347///////////////////////////////////////////////////////////////////////////////
1348// class SkPDFType3Font
1349///////////////////////////////////////////////////////////////////////////////
1350
1351SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1352 SkTypeface* typeface,
1353 uint16_t glyphID,
1354 SkPDFDict* relatedFontDescriptor)
1355 : SkPDFFont(info, typeface, glyphID, false) {
1356 populate(glyphID);
1357}
1358
1359SkPDFType3Font::~SkPDFType3Font() {}
1360
1361bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001362 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001363 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001364 paint.setTextSize(1000);
1365 SkAutoGlyphCache autoCache(paint, NULL);
1366 SkGlyphCache* cache = autoCache.getCache();
1367 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001368 if (lastGlyphID() == 0) {
1369 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001370 }
1371
1372 adjustGlyphRangeForSingleByteEncoding(glyphID);
1373
reed@google.comc789cf12011-07-20 12:14:33 +00001374 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001375 // Flip about the x-axis and scale by 1/1000.
1376 SkMatrix fontMatrix;
1377 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1378 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1379
1380 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
1381 charProcs->unref(); // SkRefPtr and new both took a reference.
1382 insert("CharProcs", charProcs.get());
1383
1384 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
1385 encoding->unref(); // SkRefPtr and new both took a reference.
1386 insert("Encoding", encoding.get());
1387
1388 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1389 encDiffs->unref(); // SkRefPtr and new both took a reference.
1390 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001391 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001392 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001393
1394 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1395 widthArray->unref(); // SkRefPtr and new both took a ref.
1396
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001397 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001398 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001399 SkString characterName;
1400 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001401 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001402
reed@google.comce11b262011-03-21 21:25:35 +00001403 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001404 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001405 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1406 glyph.fWidth, glyph.fHeight);
1407 bbox.join(glyphBBox);
1408
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001409 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001410 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1411 &content);
1412 const SkPath* path = cache->findPath(glyph);
1413 if (path) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001414 SkPDFUtils::EmitPath(*path, paint.getStyle(), &content);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001415 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1416 &content);
1417 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001418 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001419 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +00001420 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001421
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001422 SkRefPtr<SkPDFStream> glyphDescription =
1423 new SkPDFStream(glyphStream.get());
1424 // SkRefPtr and new both ref()'d charProcs, pass one.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001425 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001426 charProcs->insert(characterName.c_str(),
1427 new SkPDFObjRef(glyphDescription.get()))->unref();
1428 }
1429
1430 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001431 insertInt("FirstChar", firstGlyphID());
1432 insertInt("LastChar", lastGlyphID());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001433 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001434 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001435
vandebo@chromium.org98594282011-07-25 22:34:12 +00001436 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001437 return true;
1438}