blob: e0e18cd9335b5c31458ba41872d948521089f86e [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
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000476 BFRange currentRangeEntry = {0, 0, 0};
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000477 bool rangeEmpty = true;
478 const int count = glyphToUnicode.count();
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000479
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000480 for (int i = 0; i < count + 1; ++i) {
481 bool inSubset = i < count && (subset == NULL || subset->has(i));
482 if (!rangeEmpty) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000483 // PDF spec requires bfrange not changing the higher byte,
484 // e.g. <1035> <10FF> <2222> is ok, but
485 // <1035> <1100> <2222> is no good
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000486 bool inRange =
487 i == currentRangeEntry.fEnd + 1 &&
488 i >> 8 == currentRangeEntry.fStart >> 8 &&
489 i < count &&
490 glyphToUnicode[i] == currentRangeEntry.fUnicode + i -
491 currentRangeEntry.fStart;
492 if (!inSubset || !inRange) {
493 if (currentRangeEntry.fEnd > currentRangeEntry.fStart) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000494 bfrangeEntries.push(currentRangeEntry);
495 } else {
496 BFChar* entry = bfcharEntries.append();
497 entry->fGlyphId = currentRangeEntry.fStart;
498 entry->fUnicode = currentRangeEntry.fUnicode;
499 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000500 rangeEmpty = true;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000501 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000502 }
503 if (inSubset) {
504 currentRangeEntry.fEnd = i;
505 if (rangeEmpty) {
506 currentRangeEntry.fStart = i;
507 currentRangeEntry.fUnicode = glyphToUnicode[i];
508 rangeEmpty = false;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000509 }
510 }
511 }
512
513 // The spec requires all bfchar entries for a font must come before bfrange
514 // entries.
515 append_bfchar_section(bfcharEntries, cmap);
516 append_bfrange_section(bfrangeEntries, cmap);
517}
518
vandebo@chromium.org98594282011-07-25 22:34:12 +0000519static SkPDFStream* generate_tounicode_cmap(
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000520 const SkTDArray<SkUnichar>& glyphToUnicode,
521 const SkPDFGlyphSet* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000522 SkDynamicMemoryWStream cmap;
523 append_tounicode_header(&cmap);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000524 append_cmap_sections(glyphToUnicode, subset, &cmap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000525 append_cmap_footer(&cmap);
526 SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
527 cmapStream->unref(); // SkRefPtr and new took a reference.
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000528 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000529 return new SkPDFStream(cmapStream.get());
530}
531
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000532static void sk_delete_array(const void* ptr, size_t, void*) {
533 // Use C-style cast to cast away const and cast type simultaneously.
534 delete[] (unsigned char*)ptr;
535}
536
537static int get_subset_font_stream(const char* fontName,
538 const SkTypeface* typeface,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000539 const SkTDArray<uint32_t>& subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000540 SkPDFStream** fontStream) {
541 SkRefPtr<SkStream> fontData =
542 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface));
543 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
544
545 int fontSize = fontData->getLength();
546
547#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000548 // Read font into buffer.
549 SkPDFStream* subsetFontStream = NULL;
550 SkTDArray<unsigned char> originalFont;
551 originalFont.setCount(fontSize);
552 if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) {
553 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000554 // sfntly requires unsigned int* to be passed in, as far as we know,
555 // unsigned int is equivalent to uint32_t on all platforms.
556 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
557 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000558 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
559 originalFont.begin(),
560 fontSize,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000561 subset.begin(),
562 subset.count(),
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000563 &subsetFont);
564 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000565 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
566 subsetFontSize,
567 sk_delete_array,
568 NULL));
569 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000570 fontSize = subsetFontSize;
571 }
572 }
573 if (subsetFontStream) {
574 *fontStream = subsetFontStream;
575 return fontSize;
576 }
577#endif
578
579 // Fail over: just embed the whole font.
580 *fontStream = new SkPDFStream(fontData.get());
581 return fontSize;
582}
583
vandebo@chromium.org98594282011-07-25 22:34:12 +0000584///////////////////////////////////////////////////////////////////////////////
585// class SkPDFGlyphSet
586///////////////////////////////////////////////////////////////////////////////
587
588SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
589}
590
591void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
592 for (int i = 0; i < numGlyphs; ++i) {
593 fBitSet.setBit(glyphIDs[i], true);
594 }
595}
596
597bool SkPDFGlyphSet::has(uint16_t glyphID) const {
598 return fBitSet.isBitSet(glyphID);
599}
600
601void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
602 fBitSet.orBits(usage.fBitSet);
603}
604
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000605void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
606 fBitSet.exportTo(glyphIDs);
607}
608
vandebo@chromium.org98594282011-07-25 22:34:12 +0000609///////////////////////////////////////////////////////////////////////////////
610// class SkPDFGlyphSetMap
611///////////////////////////////////////////////////////////////////////////////
612SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
613 SkPDFGlyphSet* glyphSet)
614 : fFont(font),
615 fGlyphSet(glyphSet) {
616}
617
618SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
619 reset(map);
620}
621
622SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
623 if (fIndex >= fMap->count()) {
624 return NULL;
625 }
626 return &((*fMap)[fIndex++]);
627}
628
629void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
630 fMap = &(map.fMap);
631 fIndex = 0;
632}
633
634SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
635}
636
637SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
638 reset();
639}
640
641void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
642 for (int i = 0; i < usage.fMap.count(); ++i) {
643 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
644 myUsage->merge(*(usage.fMap[i].fGlyphSet));
645 }
646}
647
648void SkPDFGlyphSetMap::reset() {
649 for (int i = 0; i < fMap.count(); ++i) {
650 delete fMap[i].fGlyphSet; // Should not be NULL.
651 }
652 fMap.reset();
653}
654
655void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
656 int numGlyphs) {
657 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
658 if (subset) {
659 subset->set(glyphIDs, numGlyphs);
660 }
661}
662
663SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
664 int index = fMap.count();
665 for (int i = 0; i < index; ++i) {
666 if (fMap[i].fFont == font) {
667 return fMap[i].fGlyphSet;
668 }
669 }
670 fMap.append();
671 index = fMap.count() - 1;
672 fMap[index].fFont = font;
673 fMap[index].fGlyphSet = new SkPDFGlyphSet();
674 return fMap[index].fGlyphSet;
675}
676
677///////////////////////////////////////////////////////////////////////////////
678// class SkPDFFont
679///////////////////////////////////////////////////////////////////////////////
680
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000681/* Font subset design: It would be nice to be able to subset fonts
682 * (particularly type 3 fonts), but it's a lot of work and not a priority.
683 *
684 * Resources are canonicalized and uniqueified by pointer so there has to be
685 * some additional state indicating which subset of the font is used. It
686 * must be maintained at the page granularity and then combined at the document
687 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
688 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
689 * page/pdf device. c) in the document, retrieve the per font glyph usage
690 * from each page and combine it and ask for a resource with that subset.
691 */
692
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000693SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000694 SkAutoMutexAcquire lock(CanonicalFontsMutex());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000695 int index;
vandebo@chromium.org918352f2011-10-30 19:13:26 +0000696 if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index) &&
697 CanonicalFonts()[index].fFont == this) {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000698 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000699 }
700 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000701}
702
703void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000704 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000705}
706
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000707SkTypeface* SkPDFFont::typeface() {
708 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000709}
710
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000711SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000712 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000713}
714
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000715bool SkPDFFont::hasGlyph(uint16_t id) {
716 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
717}
718
vandebo@chromium.org01294102011-02-28 19:52:18 +0000719size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
720 size_t numGlyphs) {
721 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000722 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000723 return numGlyphs;
724 }
725
726 for (size_t i = 0; i < numGlyphs; i++) {
727 if (glyphIDs[i] == 0) {
728 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000729 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000730 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
731 return i;
732 }
733 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000734 }
735
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000736 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000737}
738
739// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000740SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
741 SkAutoMutexAcquire lock(CanonicalFontsMutex());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000742 const uint32_t fontID = SkTypeface::UniqueID(typeface);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000743 int relatedFontIndex;
744 if (Find(fontID, glyphID, &relatedFontIndex)) {
745 CanonicalFonts()[relatedFontIndex].fFont->ref();
746 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000747 }
748
vandebo@chromium.org98594282011-07-25 22:34:12 +0000749 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
750 SkPDFDict* relatedFontDescriptor = NULL;
751 if (relatedFontIndex >= 0) {
752 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000753 fontMetrics = relatedFont->fontInfo();
754 relatedFontDescriptor = relatedFont->getFontDescriptor();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000755 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000756 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000757 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000758 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
759 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000760#if !defined (SK_SFNTLY_SUBSETTER)
761 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
762 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
763#endif
764 fontMetrics =
765 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000766 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref.
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000767#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.orgb3b46552011-10-17 23:22:49 +0000768 if (fontMetrics &&
769 fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000770 // Font does not support subsetting, get new info with advance.
771 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
772 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
773 fontMetrics =
774 SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
775 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
776 }
777#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000778 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000779
vandebo@chromium.org98594282011-07-25 22:34:12 +0000780 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
781 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000782 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000783 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000784 return font; // Return the reference new SkPDFFont() created.
785}
786
vandebo@chromium.org98594282011-07-25 22:34:12 +0000787SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) {
788 return NULL; // Default: no support.
789}
790
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000791// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000792SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000793 // This initialization is only thread safe with gcc.
794 static SkTDArray<FontRec> gCanonicalFonts;
795 return gCanonicalFonts;
796}
797
798// static
digit@google.com1771cbf2012-01-26 21:26:40 +0000799SkBaseMutex& SkPDFFont::CanonicalFontsMutex() {
800 // This initialization is only thread safe with gcc, or when
801 // POD-style mutex initialization is used.
802 SK_DECLARE_STATIC_MUTEX(gCanonicalFontsMutex);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000803 return gCanonicalFontsMutex;
804}
805
806// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000807bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000808 // TODO(vandebo): Optimize this, do only one search?
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000809 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000810 *index = CanonicalFonts().find(search);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000811 if (*index >= 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000812 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000813 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000814 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000815 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000816 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000817}
818
vandebo@chromium.org98594282011-07-25 22:34:12 +0000819SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
820 uint16_t glyphID, bool descendantFont)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000821 : SkPDFDict("Font"),
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000822 fTypeface(typeface),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000823 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000824 fLastGlyphID(info ? info->fLastGlyphID : 0),
825 fFontInfo(info) {
826 if (info == NULL) {
827 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
828 } else if (info->fMultiMaster) {
829 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
830 } else {
831 fFontType = info->fType;
832 }
833}
834
835// static
836SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
837 SkTypeface* typeface, uint16_t glyphID,
838 SkPDFDict* relatedFontDescriptor) {
839 SkAdvancedTypefaceMetrics::FontType type =
840 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
841
842 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000843 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000844 return new SkPDFType3Font(info,
845 typeface,
846 glyphID,
847 relatedFontDescriptor);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000848 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000849 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
850 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
851 SkASSERT(relatedFontDescriptor == NULL);
852 return new SkPDFType0Font(info, typeface);
853 }
854 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
855 return new SkPDFType1Font(info,
856 typeface,
857 glyphID,
858 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000859 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000860
vandebo@chromium.org98594282011-07-25 22:34:12 +0000861 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
862 type == SkAdvancedTypefaceMetrics::kOther_Font ||
863 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000864
vandebo@chromium.org98594282011-07-25 22:34:12 +0000865 return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000866}
867
vandebo@chromium.org98594282011-07-25 22:34:12 +0000868SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
869 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000870}
871
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000872void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
873 if (info == NULL || info == fFontInfo.get()) {
874 return;
875 }
876 fFontInfo = info;
877}
878
vandebo@chromium.org98594282011-07-25 22:34:12 +0000879uint16_t SkPDFFont::firstGlyphID() const {
880 return fFirstGlyphID;
881}
882
883uint16_t SkPDFFont::lastGlyphID() const {
884 return fLastGlyphID;
885}
886
887void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
888 fLastGlyphID = glyphID;
889}
890
891void SkPDFFont::addResource(SkPDFObject* object) {
892 SkASSERT(object != NULL);
893 fResources.push(object);
894}
895
896SkPDFDict* SkPDFFont::getFontDescriptor() {
897 return fDescriptor.get();
898}
899
900void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
901 fDescriptor = descriptor;
902}
903
904bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
905 if (fDescriptor.get() == NULL) {
906 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000907 }
908
vandebo@chromium.org98594282011-07-25 22:34:12 +0000909 const uint16_t emSize = fFontInfo->fEmSize;
910
911 fDescriptor->insertName("FontName", fFontInfo->fFontName);
912 fDescriptor->insertInt("Flags", fFontInfo->fStyle);
913 fDescriptor->insertScalar("Ascent",
914 scaleFromFontUnits(fFontInfo->fAscent, emSize));
915 fDescriptor->insertScalar("Descent",
916 scaleFromFontUnits(fFontInfo->fDescent, emSize));
917 fDescriptor->insertScalar("StemV",
918 scaleFromFontUnits(fFontInfo->fStemV, emSize));
919 fDescriptor->insertScalar("CapHeight",
920 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
921 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
922 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
923 fFontInfo->fEmSize))->unref();
924
925 if (defaultWidth > 0) {
926 fDescriptor->insertScalar("MissingWidth",
927 scaleFromFontUnits(defaultWidth, emSize));
928 }
929 return true;
930}
931
932void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
933 // Single byte glyph encoding supports a max of 255 glyphs.
934 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
935 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
936 fLastGlyphID = fFirstGlyphID + 255 - 1;
937 }
938}
939
940bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000941 if (fFontID != b.fFontID) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000942 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000943 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000944 if (fFont != NULL && b.fFont != NULL) {
945 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
946 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
947 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000948 if (fGlyphID == 0 || b.fGlyphID == 0) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000949 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000950 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000951
952 if (fFont != NULL) {
953 return fFont->fFirstGlyphID <= b.fGlyphID &&
954 b.fGlyphID <= fFont->fLastGlyphID;
955 } else if (b.fFont != NULL) {
956 return b.fFont->fFirstGlyphID <= fGlyphID &&
957 fGlyphID <= b.fFont->fLastGlyphID;
958 }
959 return fGlyphID == b.fGlyphID;
960}
961
962SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
963 : fFont(font),
964 fFontID(fontID),
965 fGlyphID(glyphID) {
966}
967
968void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
969 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
970 return;
971 }
972 SkRefPtr<SkPDFStream> pdfCmap =
973 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset);
974 addResource(pdfCmap.get()); // Pass reference from new.
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000975 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000976}
977
vandebo@chromium.org98594282011-07-25 22:34:12 +0000978///////////////////////////////////////////////////////////////////////////////
979// class SkPDFType0Font
980///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000981
vandebo@chromium.org98594282011-07-25 22:34:12 +0000982SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
983 SkTypeface* typeface)
984 : SkPDFFont(info, typeface, 0, false) {
985 SkDEBUGCODE(fPopulated = false);
986}
987
988SkPDFType0Font::~SkPDFType0Font() {}
989
990SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
991 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
992 newSubset->populate(subset);
993 return newSubset;
994}
995
996#ifdef SK_DEBUG
997void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
998 bool indirect) {
999 SkASSERT(fPopulated);
1000 return INHERITED::emitObject(stream, catalog, indirect);
1001}
1002#endif
1003
1004bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
1005 insertName("Subtype", "Type0");
1006 insertName("BaseFont", fontInfo()->fFontName);
1007 insertName("Encoding", "Identity-H");
1008
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001009 SkPDFCIDFont* newCIDFont;
1010 newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset);
1011
vandebo@chromium.org98594282011-07-25 22:34:12 +00001012 // Pass ref new created to fResources.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001013 addResource(newCIDFont);
1014 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray();
1015 descendantFonts->unref(); // SkRefPtr and new took a reference.
1016 descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref();
1017 insert("DescendantFonts", descendantFonts.get());
1018
1019 populateToUnicodeTable(subset);
1020
1021 SkDEBUGCODE(fPopulated = true);
1022 return true;
1023}
1024
1025///////////////////////////////////////////////////////////////////////////////
1026// class SkPDFCIDFont
1027///////////////////////////////////////////////////////////////////////////////
1028
1029SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1030 SkTypeface* typeface, const SkPDFGlyphSet* subset)
1031 : SkPDFFont(info, typeface, 0, true) {
1032 populate(subset);
1033}
1034
1035SkPDFCIDFont::~SkPDFCIDFont() {}
1036
1037bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001038 const SkTDArray<uint32_t>* subset) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001039 SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor");
1040 descriptor->unref(); // SkRefPtr and new both took a ref.
1041 setFontDescriptor(descriptor.get());
1042
1043 switch (getType()) {
1044 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001045 SkASSERT(subset);
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001046 // Font subsetting
1047 SkPDFStream* rawStream = NULL;
1048 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1049 typeface(),
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001050 *subset,
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001051 &rawStream);
1052 SkASSERT(fontSize);
1053 SkASSERT(rawStream);
1054 SkRefPtr<SkPDFStream> fontStream = rawStream;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001055 // SkRefPtr and new both ref()'d fontStream, pass one.
1056 addResource(fontStream.get());
1057
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001058 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001059 descriptor->insert("FontFile2",
1060 new SkPDFObjRef(fontStream.get()))->unref();
1061 break;
1062 }
1063 case SkAdvancedTypefaceMetrics::kCFF_Font:
1064 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1065 SkRefPtr<SkStream> fontData =
1066 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1067 fontData->unref(); // SkRefPtr and OpenStream both took a ref.
1068 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get());
1069 // SkRefPtr and new both ref()'d fontStream, pass one.
1070 addResource(fontStream.get());
1071
1072 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1073 fontStream->insertName("Subtype", "Type1C");
1074 } else {
1075 fontStream->insertName("Subtype", "CIDFontType0c");
1076 }
1077 descriptor->insert("FontFile3",
1078 new SkPDFObjRef(fontStream.get()))->unref();
1079 break;
1080 }
1081 default:
1082 SkASSERT(false);
1083 }
1084
1085 addResource(descriptor.get());
1086 descriptor->ref();
1087
1088 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1089 return addCommonFontDescriptorEntries(defaultWidth);
1090}
1091
1092bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001093 // Generate new font metrics with advance info for true type fonts.
1094 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1095 // Generate glyph id array.
1096 SkTDArray<uint32_t> glyphIDs;
1097 glyphIDs.push(0); // Always include glyph 0.
1098 if (subset) {
1099 subset->exportTo(&glyphIDs);
1100 }
1101
1102 SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics;
1103 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
1104 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
1105 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
1106 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
1107 uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin();
1108 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
1109 fontMetrics =
1110 SkFontHost::GetAdvancedTypefaceMetrics(
1111 SkTypeface::UniqueID(typeface()),
1112 info,
1113 glyphs,
1114 glyphsCount);
1115 SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref
1116 setFontInfo(fontMetrics.get());
1117 addFontDescriptor(0, &glyphIDs);
1118 } else {
1119 // Other CID fonts
1120 addFontDescriptor(0, NULL);
1121 }
1122
vandebo@chromium.org98594282011-07-25 22:34:12 +00001123 insertName("BaseFont", fontInfo()->fFontName);
1124
1125 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001126 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001127 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001128 insertName("Subtype", "CIDFontType2");
1129 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001130 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001131 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001132 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001133
1134 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict;
1135 sysInfo->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001136 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1137 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001138 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001139 insert("CIDSystemInfo", sysInfo.get());
1140
vandebo@chromium.org98594282011-07-25 22:34:12 +00001141 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001142 int16_t defaultWidth = 0;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001143 SkRefPtr<SkPDFArray> widths =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001144 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1145 fontInfo()->fEmSize, &appendWidth,
1146 &defaultWidth);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001147 widths->unref(); // SkRefPtr and compose both took a reference.
1148 if (widths->size())
1149 insert("W", widths.get());
1150 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001151 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001152 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001153 }
1154 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001155 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001156 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001157 defaultAdvance.fVerticalAdvance = 0;
1158 defaultAdvance.fOriginXDisp = 0;
1159 defaultAdvance.fOriginYDisp = 0;
1160 SkRefPtr<SkPDFArray> advances =
vandebo@chromium.org98594282011-07-25 22:34:12 +00001161 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1162 fontInfo()->fEmSize, &appendVerticalAdvance,
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001163 &defaultAdvance);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001164 advances->unref(); // SkRefPtr and compose both took a ref.
1165 if (advances->size())
1166 insert("W2", advances.get());
1167 if (defaultAdvance.fVerticalAdvance ||
1168 defaultAdvance.fOriginXDisp ||
1169 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001170 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001171 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001172 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001173 }
1174 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001175
1176 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001177}
1178
vandebo@chromium.org98594282011-07-25 22:34:12 +00001179///////////////////////////////////////////////////////////////////////////////
1180// class SkPDFType1Font
1181///////////////////////////////////////////////////////////////////////////////
1182
1183SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1184 SkTypeface* typeface,
1185 uint16_t glyphID,
1186 SkPDFDict* relatedFontDescriptor)
1187 : SkPDFFont(info, typeface, glyphID, false) {
1188 populate(glyphID);
1189}
1190
1191SkPDFType1Font::~SkPDFType1Font() {}
1192
1193bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
1194 SkRefPtr<SkPDFDict> descriptor = getFontDescriptor();
1195 if (descriptor.get() != NULL) {
1196 addResource(descriptor.get());
1197 descriptor->ref();
1198 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1199 return true;
1200 }
1201
1202 descriptor = new SkPDFDict("FontDescriptor");
1203 descriptor->unref(); // SkRefPtr and new both took a ref.
1204 setFontDescriptor(descriptor.get());
1205
1206 size_t header SK_INIT_TO_AVOID_WARNING;
1207 size_t data SK_INIT_TO_AVOID_WARNING;
1208 size_t trailer SK_INIT_TO_AVOID_WARNING;
1209 SkRefPtr<SkStream> rawFontData =
1210 SkFontHost::OpenStream(SkTypeface::UniqueID(typeface()));
1211 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref.
1212 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1213 &trailer);
1214 if (fontData == NULL) {
1215 return false;
1216 }
1217 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData);
1218 // SkRefPtr and new both ref()'d fontStream, pass one.
1219 addResource(fontStream.get());
1220 fontStream->insertInt("Length1", header);
1221 fontStream->insertInt("Length2", data);
1222 fontStream->insertInt("Length3", trailer);
1223 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
1224
1225 addResource(descriptor.get());
1226 descriptor->ref();
1227 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1228
1229 return addCommonFontDescriptorEntries(defaultWidth);
1230}
1231
1232bool SkPDFType1Font::populate(int16_t glyphID) {
1233 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1234 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001235
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001236 adjustGlyphRangeForSingleByteEncoding(glyphID);
1237
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001238 int16_t defaultWidth = 0;
1239 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1240 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001241 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001242 widthEntry != NULL;
1243 widthEntry = widthEntry->fNext.get()) {
1244 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001245 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001246 defaultWidth = widthEntry->fAdvance[0];
1247 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001248 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001249 SkASSERT(false);
1250 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001251 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001252 SkASSERT(widthRangeEntry == NULL);
1253 widthRangeEntry = widthEntry;
1254 break;
1255 }
1256 }
1257
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001258 if (!addFontDescriptor(defaultWidth)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001259 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001260 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001261
reed@google.comc789cf12011-07-20 12:14:33 +00001262 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001263 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001264
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001265 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001266
1267 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001268 encoding->unref(); // SkRefPtr and new both took a reference.
1269 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001270
1271 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1272 encDiffs->unref(); // SkRefPtr and new both took a reference.
1273 encoding->insert("Differences", encDiffs.get());
1274
vandebo@chromium.org98594282011-07-25 22:34:12 +00001275 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001276 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001277 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1278 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001279 }
1280
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001281 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001282}
1283
vandebo@chromium.org98594282011-07-25 22:34:12 +00001284void SkPDFType1Font::addWidthInfoFromRange(
1285 int16_t defaultWidth,
1286 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
1287 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1288 widthArray->unref(); // SkRefPtr and new both took a ref.
1289 int firstChar = 0;
1290 if (widthRangeEntry) {
1291 const uint16_t emSize = fontInfo()->fEmSize;
1292 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1293 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1294 if (startIndex < 0)
1295 startIndex = 0;
1296 if (endIndex > widthRangeEntry->fAdvance.count())
1297 endIndex = widthRangeEntry->fAdvance.count();
1298 if (widthRangeEntry->fStartId == 0) {
1299 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1300 } else {
1301 firstChar = startIndex + widthRangeEntry->fStartId;
1302 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001303 for (int i = startIndex; i < endIndex; i++) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001304 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001305 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001306 } else {
1307 appendWidth(defaultWidth, 1000, widthArray.get());
1308 }
1309 insertInt("FirstChar", firstChar);
1310 insertInt("LastChar", firstChar + widthArray->size() - 1);
1311 insert("Widths", widthArray.get());
1312}
1313
1314///////////////////////////////////////////////////////////////////////////////
1315// class SkPDFType3Font
1316///////////////////////////////////////////////////////////////////////////////
1317
1318SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1319 SkTypeface* typeface,
1320 uint16_t glyphID,
1321 SkPDFDict* relatedFontDescriptor)
1322 : SkPDFFont(info, typeface, glyphID, false) {
1323 populate(glyphID);
1324}
1325
1326SkPDFType3Font::~SkPDFType3Font() {}
1327
1328bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001329 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001330 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001331 paint.setTextSize(1000);
1332 SkAutoGlyphCache autoCache(paint, NULL);
1333 SkGlyphCache* cache = autoCache.getCache();
1334 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001335 if (lastGlyphID() == 0) {
1336 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001337 }
1338
1339 adjustGlyphRangeForSingleByteEncoding(glyphID);
1340
reed@google.comc789cf12011-07-20 12:14:33 +00001341 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001342 // Flip about the x-axis and scale by 1/1000.
1343 SkMatrix fontMatrix;
1344 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1345 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1346
1347 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict;
1348 charProcs->unref(); // SkRefPtr and new both took a reference.
1349 insert("CharProcs", charProcs.get());
1350
1351 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding");
1352 encoding->unref(); // SkRefPtr and new both took a reference.
1353 insert("Encoding", encoding.get());
1354
1355 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray;
1356 encDiffs->unref(); // SkRefPtr and new both took a reference.
1357 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001358 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001359 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001360
1361 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray();
1362 widthArray->unref(); // SkRefPtr and new both took a ref.
1363
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001364 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001365 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001366 SkString characterName;
1367 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001368 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001369
reed@google.comce11b262011-03-21 21:25:35 +00001370 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001371 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001372 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1373 glyph.fWidth, glyph.fHeight);
1374 bbox.join(glyphBBox);
1375
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001376 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001377 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1378 &content);
1379 const SkPath* path = cache->findPath(glyph);
1380 if (path) {
1381 SkPDFUtils::EmitPath(*path, &content);
1382 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1383 &content);
1384 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001385 SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001386 glyphStream->unref(); // SkRefPtr and new both took a ref.
reed@google.com8a85d0c2011-06-24 19:12:12 +00001387 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001388
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001389 SkRefPtr<SkPDFStream> glyphDescription =
1390 new SkPDFStream(glyphStream.get());
1391 // SkRefPtr and new both ref()'d charProcs, pass one.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001392 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001393 charProcs->insert(characterName.c_str(),
1394 new SkPDFObjRef(glyphDescription.get()))->unref();
1395 }
1396
1397 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001398 insertInt("FirstChar", firstGlyphID());
1399 insertInt("LastChar", lastGlyphID());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001400 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001401 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001402
vandebo@chromium.org98594282011-07-25 22:34:12 +00001403 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001404 return true;
1405}