blob: 0d411769827156ced4ddfe2bb2a1068880c4a156 [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00006 */
7
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00008#include <ctype.h>
9
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000011#include "SkFontHost.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000012#include "SkGlyphCache.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000013#include "SkPaint.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000014#include "SkPDFCatalog.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000015#include "SkPDFDevice.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000016#include "SkPDFFont.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000017#include "SkPDFFontImpl.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000018#include "SkPDFStream.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000019#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000020#include "SkPDFUtils.h"
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +000021#include "SkRefCnt.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000022#include "SkScalar.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000023#include "SkStream.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000024#include "SkTypefacePriv.h"
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +000025#include "SkTypes.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000026#include "SkUtils.h"
27
vandebo@chromium.org1f165892011-07-26 02:11:41 +000028#if defined (SK_SFNTLY_SUBSETTER)
29#include SK_SFNTLY_SUBSETTER
30#endif
31
vandebo@chromium.orgdcf9c192013-03-13 20:01:51 +000032// PDF's notion of symbolic vs non-symbolic is related to the character set, not
33// symbols vs. characters. Rarely is a font the right character set to call it
34// non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1)
35static const int kPdfSymbolic = 4;
36
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000037namespace {
38
vandebo@chromium.org98594282011-07-25 22:34:12 +000039///////////////////////////////////////////////////////////////////////////////
40// File-Local Functions
41///////////////////////////////////////////////////////////////////////////////
42
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000043bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
44 size_t* size) {
45 // PFB sections have a two or six bytes header. 0x80 and a one byte
46 // section type followed by a four byte section length. Type one is
47 // an ASCII section (includes a length), type two is a binary section
48 // (includes a length) and type three is an EOF marker with no length.
49 const uint8_t* buf = *src;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000050 if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000051 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000052 } else if (buf[1] == 3) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000053 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000054 } else if (*len < 6) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000055 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000056 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000057
vandebo@chromium.org73322072011-06-21 21:19:41 +000058 *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
59 ((size_t)buf[5] << 24);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000060 size_t consumed = *size + 6;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000061 if (consumed > *len) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000062 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000063 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000064 *src = *src + consumed;
65 *len = *len - consumed;
66 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000067}
68
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000069bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
70 size_t* dataLen, size_t* trailerLen) {
71 const uint8_t* srcPtr = src;
72 size_t remaining = size;
73
74 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
75 parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
76 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
77 parsePFBSection(&srcPtr, &remaining, 3, NULL);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000078}
79
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000080/* The sections of a PFA file are implicitly defined. The body starts
81 * after the line containing "eexec," and the trailer starts with 512
82 * literal 0's followed by "cleartomark" (plus arbitrary white space).
83 *
84 * This function assumes that src is NUL terminated, but the NUL
85 * termination is not included in size.
86 *
87 */
88bool parsePFA(const char* src, size_t size, size_t* headerLen,
89 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
90 const char* end = src + size;
91
92 const char* dataPos = strstr(src, "eexec");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000093 if (!dataPos) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000094 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000095 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000096 dataPos += strlen("eexec");
97 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000098 dataPos < end) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000099 dataPos++;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000100 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000101 *headerLen = dataPos - src;
102
103 const char* trailerPos = strstr(dataPos, "cleartomark");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000104 if (!trailerPos) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000105 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000106 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000107 int zeroCount = 0;
108 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
109 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
110 continue;
111 } else if (*trailerPos == '0') {
112 zeroCount++;
113 } else {
114 return false;
115 }
116 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000117 if (zeroCount != 512) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000118 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000119 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000120
121 *hexDataLen = trailerPos - src - *headerLen;
122 *trailerLen = size - *headerLen - *hexDataLen;
123
124 // Verify that the data section is hex encoded and count the bytes.
125 int nibbles = 0;
126 for (; dataPos < trailerPos; dataPos++) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000127 if (isspace(*dataPos)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000128 continue;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000129 }
130 if (!isxdigit(*dataPos)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000131 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000132 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000133 nibbles++;
134 }
135 *dataLen = (nibbles + 1) / 2;
136
137 return true;
138}
139
140int8_t hexToBin(uint8_t c) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000141 if (!isxdigit(c)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000142 return -1;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000143 } else if (c <= '9') {
144 return c - '0';
145 } else if (c <= 'F') {
146 return c - 'A' + 10;
147 } else if (c <= 'f') {
148 return c - 'a' + 10;
149 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000150 return -1;
151}
152
153SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
154 size_t* dataLen, size_t* trailerLen) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000155 // srcStream may be backed by a file or a unseekable fd, so we may not be
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000156 // able to use skip(), rewind(), or getMemoryBase(). read()ing through
157 // the input only once is doable, but very ugly. Furthermore, it'd be nice
158 // if the data was NUL terminated so that we can use strstr() to search it.
159 // Make as few copies as possible given these constraints.
160 SkDynamicMemoryWStream dynamicStream;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000161 SkAutoTUnref<SkMemoryStream> staticStream;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000162 SkData* data = NULL;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000163 const uint8_t* src;
164 size_t srcLen;
165 if ((srcLen = srcStream->getLength()) > 0) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000166 staticStream.reset(new SkMemoryStream(srcLen + 1));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000167 src = (const uint8_t*)staticStream->getMemoryBase();
168 if (srcStream->getMemoryBase() != NULL) {
169 memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
170 } else {
171 size_t read = 0;
172 while (read < srcLen) {
173 size_t got = srcStream->read((void *)staticStream->getAtPos(),
174 srcLen - read);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000175 if (got == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000176 return NULL;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000177 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000178 read += got;
179 staticStream->seek(read);
180 }
181 }
182 ((uint8_t *)src)[srcLen] = 0;
183 } else {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000184 static const size_t kBufSize = 4096;
185 uint8_t buf[kBufSize];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000186 size_t amount;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000187 while ((amount = srcStream->read(buf, kBufSize)) > 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000188 dynamicStream.write(buf, amount);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000189 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000190 amount = 0;
191 dynamicStream.write(&amount, 1); // NULL terminator.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000192 data = dynamicStream.copyToData();
193 src = data->bytes();
194 srcLen = data->size() - 1;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000195 }
196
reed@google.com8a85d0c2011-06-24 19:12:12 +0000197 // this handles releasing the data we may have gotten from dynamicStream.
198 // if data is null, it is a no-op
199 SkAutoDataUnref aud(data);
200
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000201 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
202 SkMemoryStream* result =
203 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
204 memcpy((char*)result->getAtPos(), src + 6, *headerLen);
205 result->seek(*headerLen);
206 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
207 result->seek(*headerLen + *dataLen);
208 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
209 *trailerLen);
210 result->rewind();
211 return result;
212 }
213
214 // A PFA has to be converted for PDF.
215 size_t hexDataLen;
216 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
217 trailerLen)) {
218 SkMemoryStream* result =
219 new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
220 memcpy((char*)result->getAtPos(), src, *headerLen);
221 result->seek(*headerLen);
222
223 const uint8_t* hexData = src + *headerLen;
224 const uint8_t* trailer = hexData + hexDataLen;
225 size_t outputOffset = 0;
vandebo@chromium.org5b073682011-03-08 18:33:31 +0000226 uint8_t dataByte = 0; // To hush compiler.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000227 bool highNibble = true;
228 for (; hexData < trailer; hexData++) {
commit-bot@chromium.org3720fda2013-07-10 15:03:52 +0000229 int8_t curNibble = hexToBin(*hexData);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000230 if (curNibble < 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000231 continue;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000232 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000233 if (highNibble) {
234 dataByte = curNibble << 4;
235 highNibble = false;
236 } else {
237 dataByte |= curNibble;
238 highNibble = true;
239 ((char *)result->getAtPos())[outputOffset++] = dataByte;
240 }
241 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000242 if (!highNibble) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000243 ((char *)result->getAtPos())[outputOffset++] = dataByte;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000244 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000245 SkASSERT(outputOffset == *dataLen);
246 result->seek(*headerLen + outputOffset);
247
248 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
249 *trailerLen);
250 result->rewind();
251 return result;
252 }
253
254 return NULL;
255}
256
reed@google.com3f0dcf92011-03-18 21:23:45 +0000257// scale from em-units to base-1000, returning as a SkScalar
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000258SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
reed@google.com3f0dcf92011-03-18 21:23:45 +0000259 SkScalar scaled = SkIntToScalar(val);
260 if (emSize == 1000) {
261 return scaled;
262 } else {
263 return SkScalarMulDiv(scaled, 1000, emSize);
264 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000265}
266
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000267void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000268 SkWStream* content) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000269 // Specify width and bounding box for the glyph.
270 SkPDFScalar::Append(width, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000271 content->writeText(" 0 ");
272 content->writeDecAsText(box.fLeft);
273 content->writeText(" ");
274 content->writeDecAsText(box.fTop);
275 content->writeText(" ");
276 content->writeDecAsText(box.fRight);
277 content->writeText(" ");
278 content->writeDecAsText(box.fBottom);
279 content->writeText(" d1\n");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000280}
281
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000282SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000283 SkPDFArray* bbox = new SkPDFArray;
284 bbox->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000285 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
286 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
287 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
288 bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000289 return bbox;
290}
291
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000292SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
293 SkPDFArray* array) {
reed@google.comc789cf12011-07-20 12:14:33 +0000294 array->appendScalar(scaleFromFontUnits(width, emSize));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000295 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000296}
297
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000298SkPDFArray* appendVerticalAdvance(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000299 const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
300 uint16_t emSize, SkPDFArray* array) {
301 appendWidth(advance.fVerticalAdvance, emSize, array);
302 appendWidth(advance.fOriginXDisp, emSize, array);
303 appendWidth(advance.fOriginYDisp, emSize, array);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000304 return array;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000305}
306
307template <typename Data>
308SkPDFArray* composeAdvanceData(
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000309 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
310 uint16_t emSize,
311 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
312 SkPDFArray* array),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000313 Data* defaultAdvance) {
314 SkPDFArray* result = new SkPDFArray();
315 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
316 switch (advanceInfo->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000317 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000318 SkASSERT(advanceInfo->fAdvance.count() == 1);
319 *defaultAdvance = advanceInfo->fAdvance[0];
320 break;
321 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000322 case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000323 SkAutoTUnref<SkPDFArray> advanceArray(new SkPDFArray());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000324 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000325 appendAdvance(advanceInfo->fAdvance[j], emSize,
326 advanceArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000327 result->appendInt(advanceInfo->fStartId);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000328 result->append(advanceArray.get());
329 break;
330 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000331 case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000332 SkASSERT(advanceInfo->fAdvance.count() == 1);
reed@google.comc789cf12011-07-20 12:14:33 +0000333 result->appendInt(advanceInfo->fStartId);
334 result->appendInt(advanceInfo->fEndId);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000335 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000336 break;
337 }
338 }
339 }
340 return result;
341}
342
343} // namespace
344
edisonn@google.com26d2e042013-09-18 19:29:08 +0000345static void append_tounicode_header(SkDynamicMemoryWStream* cmap,
346 uint16_t firstGlyphID,
347 uint16_t lastGlyphID) {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000348 // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
349 // It's there to prevent old version Adobe Readers from malfunctioning.
350 const char* kHeader =
351 "/CIDInit /ProcSet findresource begin\n"
352 "12 dict begin\n"
353 "begincmap\n";
354 cmap->writeText(kHeader);
355
356 // The /CIDSystemInfo must be consistent to the one in
357 // SkPDFFont::populateCIDFont().
358 // We can not pass over the system info object here because the format is
359 // different. This is not a reference object.
360 const char* kSysInfo =
361 "/CIDSystemInfo\n"
362 "<< /Registry (Adobe)\n"
363 "/Ordering (UCS)\n"
364 "/Supplement 0\n"
365 ">> def\n";
366 cmap->writeText(kSysInfo);
367
368 // The CMapName must be consistent to /CIDSystemInfo above.
369 // /CMapType 2 means ToUnicode.
edisonn@google.com26d2e042013-09-18 19:29:08 +0000370 // Codespace range just tells the PDF processor the valid range.
371 const char* kTypeInfoHeader =
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000372 "/CMapName /Adobe-Identity-UCS def\n"
373 "/CMapType 2 def\n"
edisonn@google.com26d2e042013-09-18 19:29:08 +0000374 "1 begincodespacerange\n";
375 cmap->writeText(kTypeInfoHeader);
skia.committer@gmail.com2262c582013-09-19 07:25:55 +0000376
edisonn@google.com26d2e042013-09-18 19:29:08 +0000377 // e.g. "<0000> <FFFF>\n"
378 SkString range;
379 range.appendf("<%04X> <%04X>\n", firstGlyphID, lastGlyphID);
380 cmap->writeText(range.c_str());
skia.committer@gmail.com2262c582013-09-19 07:25:55 +0000381
edisonn@google.com26d2e042013-09-18 19:29:08 +0000382 const char* kTypeInfoFooter = "endcodespacerange\n";
383 cmap->writeText(kTypeInfoFooter);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000384}
385
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000386static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
387 const char* kFooter =
388 "endcmap\n"
389 "CMapName currentdict /CMap defineresource pop\n"
390 "end\n"
391 "end";
392 cmap->writeText(kFooter);
393}
394
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000395struct BFChar {
396 uint16_t fGlyphId;
397 SkUnichar fUnicode;
398};
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000399
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000400struct BFRange {
401 uint16_t fStart;
402 uint16_t fEnd;
403 SkUnichar fUnicode;
404};
405
406static void append_bfchar_section(const SkTDArray<BFChar>& bfchar,
407 SkDynamicMemoryWStream* cmap) {
408 // PDF spec defines that every bf* list can have at most 100 entries.
409 for (int i = 0; i < bfchar.count(); i += 100) {
410 int count = bfchar.count() - i;
411 count = SkMin32(count, 100);
412 cmap->writeDecAsText(count);
413 cmap->writeText(" beginbfchar\n");
414 for (int j = 0; j < count; ++j) {
415 cmap->writeText("<");
416 cmap->writeHexAsText(bfchar[i + j].fGlyphId, 4);
417 cmap->writeText("> <");
418 cmap->writeHexAsText(bfchar[i + j].fUnicode, 4);
419 cmap->writeText(">\n");
420 }
421 cmap->writeText("endbfchar\n");
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000422 }
423}
424
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000425static void append_bfrange_section(const SkTDArray<BFRange>& bfrange,
426 SkDynamicMemoryWStream* cmap) {
427 // PDF spec defines that every bf* list can have at most 100 entries.
428 for (int i = 0; i < bfrange.count(); i += 100) {
429 int count = bfrange.count() - i;
430 count = SkMin32(count, 100);
431 cmap->writeDecAsText(count);
432 cmap->writeText(" beginbfrange\n");
433 for (int j = 0; j < count; ++j) {
434 cmap->writeText("<");
435 cmap->writeHexAsText(bfrange[i + j].fStart, 4);
436 cmap->writeText("> <");
437 cmap->writeHexAsText(bfrange[i + j].fEnd, 4);
438 cmap->writeText("> <");
439 cmap->writeHexAsText(bfrange[i + j].fUnicode, 4);
440 cmap->writeText(">\n");
441 }
442 cmap->writeText("endbfrange\n");
443 }
444}
445
446// Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe
447// Technote 5014.
448// The function is not static so we can test it in unit tests.
449//
450// Current implementation guarantees bfchar and bfrange entries do not overlap.
451//
452// Current implementation does not attempt aggresive optimizations against
453// following case because the specification is not clear.
454//
455// 4 beginbfchar 1 beginbfchar
456// <0003> <0013> <0020> <0014>
457// <0005> <0015> to endbfchar
458// <0007> <0017> 1 beginbfrange
459// <0020> <0014> <0003> <0007> <0013>
460// endbfchar endbfrange
461//
462// Adobe Technote 5014 said: "Code mappings (unlike codespace ranges) may
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000463// overlap, but succeeding maps supersede preceding maps."
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000464//
465// In case of searching text in PDF, bfrange will have higher precedence so
466// typing char id 0x0014 in search box will get glyph id 0x0004 first. However,
467// the spec does not mention how will this kind of conflict being resolved.
468//
469// For the worst case (having 65536 continuous unicode and we use every other
470// one of them), the possible savings by aggressive optimization is 416KB
471// pre-compressed and does not provide enough motivation for implementation.
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000472
473// FIXME: this should be in a header so that it is separately testable
474// ( see caller in tests/ToUnicode.cpp )
475void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
476 const SkPDFGlyphSet* subset,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000477 SkDynamicMemoryWStream* cmap,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000478 bool multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000479 uint16_t firstGlyphID,
480 uint16_t lastGlyphID);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000481
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000482void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
483 const SkPDFGlyphSet* subset,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000484 SkDynamicMemoryWStream* cmap,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000485 bool multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000486 uint16_t firstGlyphID,
487 uint16_t lastGlyphID) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000488 if (glyphToUnicode.isEmpty()) {
489 return;
490 }
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000491 int glyphOffset = 0;
492 if (!multiByteGlyphs) {
493 glyphOffset = firstGlyphID - 1;
494 }
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000495
496 SkTDArray<BFChar> bfcharEntries;
497 SkTDArray<BFRange> bfrangeEntries;
498
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000499 BFRange currentRangeEntry = {0, 0, 0};
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000500 bool rangeEmpty = true;
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000501 const int limit =
502 SkMin32(lastGlyphID + 1, glyphToUnicode.count()) - glyphOffset;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000503
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000504 for (int i = firstGlyphID - glyphOffset; i < limit + 1; ++i) {
edisonn@google.com26d2e042013-09-18 19:29:08 +0000505 bool inSubset = i < limit &&
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000506 (subset == NULL || subset->has(i + glyphOffset));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000507 if (!rangeEmpty) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000508 // PDF spec requires bfrange not changing the higher byte,
509 // e.g. <1035> <10FF> <2222> is ok, but
510 // <1035> <1100> <2222> is no good
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000511 bool inRange =
512 i == currentRangeEntry.fEnd + 1 &&
513 i >> 8 == currentRangeEntry.fStart >> 8 &&
edisonn@google.com26d2e042013-09-18 19:29:08 +0000514 i < limit &&
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000515 glyphToUnicode[i + glyphOffset] ==
516 currentRangeEntry.fUnicode + i - currentRangeEntry.fStart;
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000517 if (!inSubset || !inRange) {
518 if (currentRangeEntry.fEnd > currentRangeEntry.fStart) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000519 bfrangeEntries.push(currentRangeEntry);
520 } else {
521 BFChar* entry = bfcharEntries.append();
522 entry->fGlyphId = currentRangeEntry.fStart;
523 entry->fUnicode = currentRangeEntry.fUnicode;
524 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000525 rangeEmpty = true;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000526 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000527 }
528 if (inSubset) {
529 currentRangeEntry.fEnd = i;
530 if (rangeEmpty) {
531 currentRangeEntry.fStart = i;
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000532 currentRangeEntry.fUnicode = glyphToUnicode[i + glyphOffset];
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000533 rangeEmpty = false;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000534 }
535 }
536 }
537
538 // The spec requires all bfchar entries for a font must come before bfrange
539 // entries.
540 append_bfchar_section(bfcharEntries, cmap);
541 append_bfrange_section(bfrangeEntries, cmap);
542}
543
vandebo@chromium.org98594282011-07-25 22:34:12 +0000544static SkPDFStream* generate_tounicode_cmap(
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000545 const SkTDArray<SkUnichar>& glyphToUnicode,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000546 const SkPDFGlyphSet* subset,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000547 bool multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000548 uint16_t firstGlyphID,
549 uint16_t lastGlyphID) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000550 SkDynamicMemoryWStream cmap;
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000551 if (multiByteGlyphs) {
552 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID);
553 } else {
554 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1);
555 }
556 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +0000557 firstGlyphID, lastGlyphID);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000558 append_cmap_footer(&cmap);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000559 SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream());
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000560 cmapStream->setData(cmap.copyToData())->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000561 return new SkPDFStream(cmapStream.get());
562}
563
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000564#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000565static void sk_delete_array(const void* ptr, size_t, void*) {
566 // Use C-style cast to cast away const and cast type simultaneously.
567 delete[] (unsigned char*)ptr;
568}
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000569#endif
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000570
reed@google.comaec40662014-04-18 19:29:07 +0000571static size_t get_subset_font_stream(const char* fontName,
572 const SkTypeface* typeface,
573 const SkTDArray<uint32_t>& subset,
574 SkPDFStream** fontStream) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000575 int ttcIndex;
576 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex));
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000577
reed@google.comaec40662014-04-18 19:29:07 +0000578 size_t fontSize = fontData->getLength();
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000579
580#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000581 // Read font into buffer.
582 SkPDFStream* subsetFontStream = NULL;
583 SkTDArray<unsigned char> originalFont;
reed@google.comaec40662014-04-18 19:29:07 +0000584 originalFont.setCount(SkToInt(fontSize));
585 if (fontData->read(originalFont.begin(), fontSize) == fontSize) {
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000586 unsigned char* subsetFont = NULL;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000587 // sfntly requires unsigned int* to be passed in, as far as we know,
588 // unsigned int is equivalent to uint32_t on all platforms.
589 SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
590 unsigned_int_not_32_bits);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000591 int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
592 originalFont.begin(),
593 fontSize,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000594 subset.begin(),
595 subset.count(),
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000596 &subsetFont);
597 if (subsetFontSize > 0 && subsetFont != NULL) {
vandebo@chromium.org93225ff2011-07-27 18:38:11 +0000598 SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
599 subsetFontSize,
600 sk_delete_array,
601 NULL));
602 subsetFontStream = new SkPDFStream(data.get());
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000603 fontSize = subsetFontSize;
604 }
605 }
606 if (subsetFontStream) {
607 *fontStream = subsetFontStream;
608 return fontSize;
609 }
vandebo@chromium.org938ea622013-09-05 19:44:44 +0000610 fontData->rewind();
sugoi@google.come2e81132013-03-05 18:35:55 +0000611#else
612 sk_ignore_unused_variable(fontName);
613 sk_ignore_unused_variable(subset);
vandebo@chromium.org1f165892011-07-26 02:11:41 +0000614#endif
615
616 // Fail over: just embed the whole font.
617 *fontStream = new SkPDFStream(fontData.get());
618 return fontSize;
619}
620
vandebo@chromium.org98594282011-07-25 22:34:12 +0000621///////////////////////////////////////////////////////////////////////////////
622// class SkPDFGlyphSet
623///////////////////////////////////////////////////////////////////////////////
624
625SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
626}
627
628void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
629 for (int i = 0; i < numGlyphs; ++i) {
630 fBitSet.setBit(glyphIDs[i], true);
631 }
632}
633
634bool SkPDFGlyphSet::has(uint16_t glyphID) const {
635 return fBitSet.isBitSet(glyphID);
636}
637
638void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
639 fBitSet.orBits(usage.fBitSet);
640}
641
vandebo@chromium.org17e66e22011-07-27 20:59:55 +0000642void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
643 fBitSet.exportTo(glyphIDs);
644}
645
vandebo@chromium.org98594282011-07-25 22:34:12 +0000646///////////////////////////////////////////////////////////////////////////////
647// class SkPDFGlyphSetMap
648///////////////////////////////////////////////////////////////////////////////
649SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
650 SkPDFGlyphSet* glyphSet)
651 : fFont(font),
652 fGlyphSet(glyphSet) {
653}
654
655SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
656 reset(map);
657}
658
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000659const SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000660 if (fIndex >= fMap->count()) {
661 return NULL;
662 }
663 return &((*fMap)[fIndex++]);
664}
665
666void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
667 fMap = &(map.fMap);
668 fIndex = 0;
669}
670
671SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
672}
673
674SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
675 reset();
676}
677
678void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
679 for (int i = 0; i < usage.fMap.count(); ++i) {
680 SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
681 myUsage->merge(*(usage.fMap[i].fGlyphSet));
682 }
683}
684
685void SkPDFGlyphSetMap::reset() {
686 for (int i = 0; i < fMap.count(); ++i) {
687 delete fMap[i].fGlyphSet; // Should not be NULL.
688 }
689 fMap.reset();
690}
691
692void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
693 int numGlyphs) {
694 SkPDFGlyphSet* subset = getGlyphSetForFont(font);
695 if (subset) {
696 subset->set(glyphIDs, numGlyphs);
697 }
698}
699
700SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
701 int index = fMap.count();
702 for (int i = 0; i < index; ++i) {
703 if (fMap[i].fFont == font) {
704 return fMap[i].fGlyphSet;
705 }
706 }
707 fMap.append();
708 index = fMap.count() - 1;
709 fMap[index].fFont = font;
710 fMap[index].fGlyphSet = new SkPDFGlyphSet();
711 return fMap[index].fGlyphSet;
712}
713
714///////////////////////////////////////////////////////////////////////////////
715// class SkPDFFont
716///////////////////////////////////////////////////////////////////////////////
717
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000718/* Font subset design: It would be nice to be able to subset fonts
719 * (particularly type 3 fonts), but it's a lot of work and not a priority.
720 *
721 * Resources are canonicalized and uniqueified by pointer so there has to be
722 * some additional state indicating which subset of the font is used. It
723 * must be maintained at the page granularity and then combined at the document
724 * granularity. a) change SkPDFFont to fill in its state on demand, kind of
725 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each
726 * page/pdf device. c) in the document, retrieve the per font glyph usage
727 * from each page and combine it and ask for a resource with that subset.
728 */
729
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000730SkPDFFont::~SkPDFFont() {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000731 SkAutoMutexAcquire lock(CanonicalFontsMutex());
edisonn@google.com022e8572012-10-23 21:32:39 +0000732 int index = -1;
733 for (int i = 0 ; i < CanonicalFonts().count() ; i++) {
734 if (CanonicalFonts()[i].fFont == this) {
735 index = i;
736 }
737 }
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000738
edisonn@google.com022e8572012-10-23 21:32:39 +0000739 SkDEBUGCODE(int indexFound;)
740 SkASSERT(index == -1 ||
reed@google.comfed86bd2013-03-14 15:04:57 +0000741 (Find(fTypeface->uniqueID(),
edisonn@google.com022e8572012-10-23 21:32:39 +0000742 fFirstGlyphID,
743 &indexFound) &&
744 index == indexFound));
745 if (index >= 0) {
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000746 CanonicalFonts().removeShuffle(index);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000747 }
748 fResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000749}
750
edisonn@google.com6addb192013-04-02 15:33:08 +0000751void SkPDFFont::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
752 SkTSet<SkPDFObject*>* newResourceObjects) {
753 GetResourcesHelper(&fResources, knownResourceObjects, newResourceObjects);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000754}
755
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000756SkTypeface* SkPDFFont::typeface() {
757 return fTypeface.get();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000758}
759
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000760SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000761 return fFontType;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000762}
763
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000764bool SkPDFFont::hasGlyph(uint16_t id) {
765 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
766}
767
reed@google.comaec40662014-04-18 19:29:07 +0000768int SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000769 // A font with multibyte glyphs will support all glyph IDs in a single font.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000770 if (this->multiByteGlyphs()) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000771 return numGlyphs;
772 }
773
reed@google.comaec40662014-04-18 19:29:07 +0000774 for (int i = 0; i < numGlyphs; i++) {
vandebo@chromium.org01294102011-02-28 19:52:18 +0000775 if (glyphIDs[i] == 0) {
776 continue;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000777 }
vandebo@chromium.org01294102011-02-28 19:52:18 +0000778 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
779 return i;
780 }
781 glyphIDs[i] -= (fFirstGlyphID - 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000782 }
783
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000784 return numGlyphs;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000785}
786
787// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000788SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
789 SkAutoMutexAcquire lock(CanonicalFontsMutex());
reed@google.comfed86bd2013-03-14 15:04:57 +0000790
791 SkAutoResolveDefaultTypeface autoResolve(typeface);
792 typeface = autoResolve.get();
793
794 const uint32_t fontID = typeface->uniqueID();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000795 int relatedFontIndex;
796 if (Find(fontID, glyphID, &relatedFontIndex)) {
797 CanonicalFonts()[relatedFontIndex].fFont->ref();
798 return CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000799 }
800
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000801 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000802 SkPDFDict* relatedFontDescriptor = NULL;
803 if (relatedFontIndex >= 0) {
804 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000805 fontMetrics.reset(relatedFont->fontInfo());
806 SkSafeRef(fontMetrics.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +0000807 relatedFontDescriptor = relatedFont->getFontDescriptor();
edisonn@google.com022e8572012-10-23 21:32:39 +0000808
809 // This only is to catch callers who pass invalid glyph ids.
810 // If glyph id is invalid, then we will create duplicate entries
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000811 // for True Type fonts.
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000812 SkAdvancedTypefaceMetrics::FontType fontType =
edisonn@google.com022e8572012-10-23 21:32:39 +0000813 fontMetrics.get() ? fontMetrics.get()->fType :
814 SkAdvancedTypefaceMetrics::kOther_Font;
815
816 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
817 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
818 CanonicalFonts()[relatedFontIndex].fFont->ref();
819 return CanonicalFonts()[relatedFontIndex].fFont;
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000820 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000821 } else {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000822 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000823 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000824 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
825 info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000826#if !defined (SK_SFNTLY_SUBSETTER)
827 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
828 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
829#endif
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000830 fontMetrics.reset(
reed@google.comfed86bd2013-03-14 15:04:57 +0000831 typeface->getAdvancedTypefaceMetrics(info, NULL, 0));
vandebo@chromium.org610f7162012-03-14 18:34:15 +0000832#if defined (SK_SFNTLY_SUBSETTER)
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000833 if (fontMetrics.get() &&
vandebo@chromium.orgb3b46552011-10-17 23:22:49 +0000834 fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000835 // Font does not support subsetting, get new info with advance.
836 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
837 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000838 fontMetrics.reset(
reed@google.comfed86bd2013-03-14 15:04:57 +0000839 typeface->getAdvancedTypefaceMetrics(info, NULL, 0));
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000840 }
841#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000842 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000843
vandebo@chromium.org98594282011-07-25 22:34:12 +0000844 SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
845 relatedFontDescriptor);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000846 FontRec newEntry(font, fontID, font->fFirstGlyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000847 CanonicalFonts().push(newEntry);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000848 return font; // Return the reference new SkPDFFont() created.
849}
850
sugoi@google.come2e81132013-03-05 18:35:55 +0000851SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet*) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000852 return NULL; // Default: no support.
853}
854
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000855// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000856SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000857 // This initialization is only thread safe with gcc.
858 static SkTDArray<FontRec> gCanonicalFonts;
859 return gCanonicalFonts;
860}
861
862// static
digit@google.com1771cbf2012-01-26 21:26:40 +0000863SkBaseMutex& SkPDFFont::CanonicalFontsMutex() {
864 // This initialization is only thread safe with gcc, or when
865 // POD-style mutex initialization is used.
866 SK_DECLARE_STATIC_MUTEX(gCanonicalFontsMutex);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000867 return gCanonicalFontsMutex;
868}
869
870// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000871bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000872 // TODO(vandebo): Optimize this, do only one search?
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000873 FontRec search(NULL, fontID, glyphID);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000874 *index = CanonicalFonts().find(search);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000875 if (*index >= 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000876 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000877 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000878 search.fGlyphID = 0;
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000879 *index = CanonicalFonts().find(search);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000880 return false;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000881}
882
vandebo@chromium.org98594282011-07-25 22:34:12 +0000883SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
sugoi@google.come2e81132013-03-05 18:35:55 +0000884 SkPDFDict* relatedFontDescriptor)
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000885 : SkPDFDict("Font"),
reed@google.comfed86bd2013-03-14 15:04:57 +0000886 fTypeface(ref_or_default(typeface)),
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000887 fFirstGlyphID(1),
vandebo@chromium.org98594282011-07-25 22:34:12 +0000888 fLastGlyphID(info ? info->fLastGlyphID : 0),
commit-bot@chromium.org45661072013-12-05 21:14:02 +0000889 fFontInfo(SkSafeRef(info)),
890 fDescriptor(SkSafeRef(relatedFontDescriptor)) {
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000891 if (info == NULL) {
892 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
893 } else if (info->fMultiMaster) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000894 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
895 } else {
896 fFontType = info->fType;
897 }
898}
899
900// static
901SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
902 SkTypeface* typeface, uint16_t glyphID,
903 SkPDFDict* relatedFontDescriptor) {
904 SkAdvancedTypefaceMetrics::FontType type =
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000905 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000906
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000907 if (info && info->fMultiMaster) {
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000908 NOT_IMPLEMENTED(true, true);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000909 return new SkPDFType3Font(info,
910 typeface,
sugoi@google.come2e81132013-03-05 18:35:55 +0000911 glyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000912 }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000913 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
914 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
915 SkASSERT(relatedFontDescriptor == NULL);
916 return new SkPDFType0Font(info, typeface);
917 }
918 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
919 return new SkPDFType1Font(info,
920 typeface,
921 glyphID,
922 relatedFontDescriptor);
vandebo@chromium.org6504cfd2011-07-23 20:22:53 +0000923 }
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000924
vandebo@chromium.org98594282011-07-25 22:34:12 +0000925 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000926 type == SkAdvancedTypefaceMetrics::kOther_Font ||
927 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000928
sugoi@google.come2e81132013-03-05 18:35:55 +0000929 return new SkPDFType3Font(info, typeface, glyphID);
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000930}
931
vandebo@chromium.org98594282011-07-25 22:34:12 +0000932SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
933 return fFontInfo.get();
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000934}
935
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000936void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
937 if (info == NULL || info == fFontInfo.get()) {
938 return;
939 }
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000940 fFontInfo.reset(info);
941 SkSafeRef(info);
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000942}
943
vandebo@chromium.org98594282011-07-25 22:34:12 +0000944uint16_t SkPDFFont::firstGlyphID() const {
945 return fFirstGlyphID;
946}
947
948uint16_t SkPDFFont::lastGlyphID() const {
949 return fLastGlyphID;
950}
951
952void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
953 fLastGlyphID = glyphID;
954}
955
956void SkPDFFont::addResource(SkPDFObject* object) {
957 SkASSERT(object != NULL);
958 fResources.push(object);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000959 object->ref();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000960}
961
962SkPDFDict* SkPDFFont::getFontDescriptor() {
963 return fDescriptor.get();
964}
965
966void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000967 fDescriptor.reset(descriptor);
968 SkSafeRef(descriptor);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000969}
970
971bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
972 if (fDescriptor.get() == NULL) {
973 return false;
vandebo@chromium.org31dcee72011-07-23 21:13:30 +0000974 }
975
vandebo@chromium.org98594282011-07-25 22:34:12 +0000976 const uint16_t emSize = fFontInfo->fEmSize;
977
978 fDescriptor->insertName("FontName", fFontInfo->fFontName);
vandebo@chromium.orgdcf9c192013-03-13 20:01:51 +0000979 fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000980 fDescriptor->insertScalar("Ascent",
981 scaleFromFontUnits(fFontInfo->fAscent, emSize));
982 fDescriptor->insertScalar("Descent",
983 scaleFromFontUnits(fFontInfo->fDescent, emSize));
984 fDescriptor->insertScalar("StemV",
985 scaleFromFontUnits(fFontInfo->fStemV, emSize));
986 fDescriptor->insertScalar("CapHeight",
987 scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
988 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
989 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
990 fFontInfo->fEmSize))->unref();
991
992 if (defaultWidth > 0) {
993 fDescriptor->insertScalar("MissingWidth",
994 scaleFromFontUnits(defaultWidth, emSize));
995 }
996 return true;
997}
998
999void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
1000 // Single byte glyph encoding supports a max of 255 glyphs.
1001 fFirstGlyphID = glyphID - (glyphID - 1) % 255;
1002 if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
1003 fLastGlyphID = fFirstGlyphID + 255 - 1;
1004 }
1005}
1006
1007bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001008 if (fFontID != b.fFontID) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001009 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001010 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001011 if (fFont != NULL && b.fFont != NULL) {
1012 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
1013 fFont->fLastGlyphID == b.fFont->fLastGlyphID;
1014 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001015 if (fGlyphID == 0 || b.fGlyphID == 0) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001016 return true;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001017 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001018
1019 if (fFont != NULL) {
1020 return fFont->fFirstGlyphID <= b.fGlyphID &&
1021 b.fGlyphID <= fFont->fLastGlyphID;
1022 } else if (b.fFont != NULL) {
1023 return b.fFont->fFirstGlyphID <= fGlyphID &&
1024 fGlyphID <= b.fFont->fLastGlyphID;
1025 }
1026 return fGlyphID == b.fGlyphID;
1027}
1028
1029SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
1030 : fFont(font),
1031 fFontID(fontID),
1032 fGlyphID(glyphID) {
1033}
1034
1035void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
1036 if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
1037 return;
1038 }
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001039 SkAutoTUnref<SkPDFStream> pdfCmap(
edisonn@google.com26d2e042013-09-18 19:29:08 +00001040 generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +00001041 multiByteGlyphs(), firstGlyphID(),
1042 lastGlyphID()));
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001043 addResource(pdfCmap.get());
vandebo@chromium.org6744d492011-05-09 18:13:47 +00001044 insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001045}
1046
vandebo@chromium.org98594282011-07-25 22:34:12 +00001047///////////////////////////////////////////////////////////////////////////////
1048// class SkPDFType0Font
1049///////////////////////////////////////////////////////////////////////////////
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001050
vandebo@chromium.org98594282011-07-25 22:34:12 +00001051SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
1052 SkTypeface* typeface)
sugoi@google.come2e81132013-03-05 18:35:55 +00001053 : SkPDFFont(info, typeface, NULL) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001054 SkDEBUGCODE(fPopulated = false);
1055}
1056
1057SkPDFType0Font::~SkPDFType0Font() {}
1058
1059SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
1060 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
1061 newSubset->populate(subset);
1062 return newSubset;
1063}
1064
1065#ifdef SK_DEBUG
1066void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
1067 bool indirect) {
1068 SkASSERT(fPopulated);
1069 return INHERITED::emitObject(stream, catalog, indirect);
1070}
1071#endif
1072
1073bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
1074 insertName("Subtype", "Type0");
1075 insertName("BaseFont", fontInfo()->fFontName);
1076 insertName("Encoding", "Identity-H");
1077
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001078 SkAutoTUnref<SkPDFCIDFont> newCIDFont(
1079 new SkPDFCIDFont(fontInfo(), typeface(), subset));
1080 addResource(newCIDFont.get());
1081 SkAutoTUnref<SkPDFArray> descendantFonts(new SkPDFArray());
1082 descendantFonts->append(new SkPDFObjRef(newCIDFont.get()))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001083 insert("DescendantFonts", descendantFonts.get());
1084
1085 populateToUnicodeTable(subset);
1086
1087 SkDEBUGCODE(fPopulated = true);
1088 return true;
1089}
1090
1091///////////////////////////////////////////////////////////////////////////////
1092// class SkPDFCIDFont
1093///////////////////////////////////////////////////////////////////////////////
1094
1095SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
1096 SkTypeface* typeface, const SkPDFGlyphSet* subset)
sugoi@google.come2e81132013-03-05 18:35:55 +00001097 : SkPDFFont(info, typeface, NULL) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001098 populate(subset);
1099}
1100
1101SkPDFCIDFont::~SkPDFCIDFont() {}
1102
1103bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001104 const SkTDArray<uint32_t>* subset) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001105 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001106 setFontDescriptor(descriptor.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001107 addResource(descriptor.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001108
1109 switch (getType()) {
1110 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
vandebo@chromium.org5f209e62013-12-10 17:22:41 +00001111 SkASSERT(subset);
1112 // Font subsetting
1113 SkPDFStream* rawStream = NULL;
reed@google.comaec40662014-04-18 19:29:07 +00001114 size_t fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1115 typeface(),
1116 *subset,
1117 &rawStream);
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001118 SkASSERT(fontSize);
vandebo@chromium.org5f209e62013-12-10 17:22:41 +00001119 SkASSERT(rawStream);
1120 SkAutoTUnref<SkPDFStream> fontStream(rawStream);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001121 addResource(fontStream.get());
1122
vandebo@chromium.org1f165892011-07-26 02:11:41 +00001123 fontStream->insertInt("Length1", fontSize);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001124 descriptor->insert("FontFile2",
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001125 new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001126 break;
1127 }
1128 case SkAdvancedTypefaceMetrics::kCFF_Font:
1129 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
reed@google.comfed86bd2013-03-14 15:04:57 +00001130 int ttcIndex;
1131 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex));
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001132 SkAutoTUnref<SkPDFStream> fontStream(
1133 new SkPDFStream(fontData.get()));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001134 addResource(fontStream.get());
1135
1136 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1137 fontStream->insertName("Subtype", "Type1C");
1138 } else {
1139 fontStream->insertName("Subtype", "CIDFontType0c");
1140 }
1141 descriptor->insert("FontFile3",
1142 new SkPDFObjRef(fontStream.get()))->unref();
1143 break;
1144 }
1145 default:
1146 SkASSERT(false);
1147 }
vandebo@chromium.org5f209e62013-12-10 17:22:41 +00001148
1149 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1150 return addCommonFontDescriptorEntries(defaultWidth);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001151}
1152
1153bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001154 // Generate new font metrics with advance info for true type fonts.
1155 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1156 // Generate glyph id array.
1157 SkTDArray<uint32_t> glyphIDs;
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001158 if (subset) {
commit-bot@chromium.org4da3bfc2013-12-11 23:54:31 +00001159 // Always include glyph 0.
1160 if (!subset->has(0)) {
1161 glyphIDs.push(0);
1162 }
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001163 subset->exportTo(&glyphIDs);
1164 }
1165
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001166 SkAdvancedTypefaceMetrics::PerGlyphInfo info;
1167 info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
1168 info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
1169 info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
commit-bot@chromium.org4da3bfc2013-12-11 23:54:31 +00001170 uint32_t* glyphs = (glyphIDs.count() == 0) ? NULL : glyphIDs.begin();
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001171 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001172 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics(
reed@google.comfed86bd2013-03-14 15:04:57 +00001173 typeface()->getAdvancedTypefaceMetrics(info, glyphs, glyphsCount));
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +00001174 setFontInfo(fontMetrics.get());
1175 addFontDescriptor(0, &glyphIDs);
1176 } else {
1177 // Other CID fonts
1178 addFontDescriptor(0, NULL);
1179 }
1180
vandebo@chromium.org98594282011-07-25 22:34:12 +00001181 insertName("BaseFont", fontInfo()->fFontName);
1182
1183 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001184 insertName("Subtype", "CIDFontType0");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001185 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
reed@google.comc789cf12011-07-20 12:14:33 +00001186 insertName("Subtype", "CIDFontType2");
1187 insertName("CIDToGIDMap", "Identity");
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001188 } else {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001189 SkASSERT(false);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001190 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001191
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001192 SkAutoTUnref<SkPDFDict> sysInfo(new SkPDFDict);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001193 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
1194 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
reed@google.comc789cf12011-07-20 12:14:33 +00001195 sysInfo->insertInt("Supplement", 0);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001196 insert("CIDSystemInfo", sysInfo.get());
1197
vandebo@chromium.org98594282011-07-25 22:34:12 +00001198 if (fontInfo()->fGlyphWidths.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001199 int16_t defaultWidth = 0;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001200 SkAutoTUnref<SkPDFArray> widths(
vandebo@chromium.org98594282011-07-25 22:34:12 +00001201 composeAdvanceData(fontInfo()->fGlyphWidths.get(),
1202 fontInfo()->fEmSize, &appendWidth,
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001203 &defaultWidth));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001204 if (widths->size())
1205 insert("W", widths.get());
1206 if (defaultWidth != 0) {
reed@google.comc789cf12011-07-20 12:14:33 +00001207 insertScalar("DW", scaleFromFontUnits(defaultWidth,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001208 fontInfo()->fEmSize));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001209 }
1210 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001211 if (fontInfo()->fVerticalMetrics.get()) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001212 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001213 defaultAdvance.fVerticalAdvance = 0;
1214 defaultAdvance.fOriginXDisp = 0;
1215 defaultAdvance.fOriginYDisp = 0;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001216 SkAutoTUnref<SkPDFArray> advances(
vandebo@chromium.org98594282011-07-25 22:34:12 +00001217 composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
1218 fontInfo()->fEmSize, &appendVerticalAdvance,
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001219 &defaultAdvance));
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001220 if (advances->size())
1221 insert("W2", advances.get());
1222 if (defaultAdvance.fVerticalAdvance ||
1223 defaultAdvance.fOriginXDisp ||
1224 defaultAdvance.fOriginYDisp) {
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001225 insert("DW2", appendVerticalAdvance(defaultAdvance,
vandebo@chromium.org98594282011-07-25 22:34:12 +00001226 fontInfo()->fEmSize,
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001227 new SkPDFArray))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001228 }
1229 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001230
1231 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001232}
1233
vandebo@chromium.org98594282011-07-25 22:34:12 +00001234///////////////////////////////////////////////////////////////////////////////
1235// class SkPDFType1Font
1236///////////////////////////////////////////////////////////////////////////////
1237
1238SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
1239 SkTypeface* typeface,
1240 uint16_t glyphID,
1241 SkPDFDict* relatedFontDescriptor)
sugoi@google.come2e81132013-03-05 18:35:55 +00001242 : SkPDFFont(info, typeface, relatedFontDescriptor) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001243 populate(glyphID);
1244}
1245
1246SkPDFType1Font::~SkPDFType1Font() {}
1247
1248bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001249 if (getFontDescriptor() != NULL) {
1250 SkPDFDict* descriptor = getFontDescriptor();
1251 addResource(descriptor);
1252 insert("FontDescriptor", new SkPDFObjRef(descriptor))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001253 return true;
1254 }
1255
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001256 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001257 setFontDescriptor(descriptor.get());
1258
reed@google.comfed86bd2013-03-14 15:04:57 +00001259 int ttcIndex;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001260 size_t header SK_INIT_TO_AVOID_WARNING;
1261 size_t data SK_INIT_TO_AVOID_WARNING;
1262 size_t trailer SK_INIT_TO_AVOID_WARNING;
reed@google.comfed86bd2013-03-14 15:04:57 +00001263 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001264 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1265 &trailer);
1266 if (fontData == NULL) {
1267 return false;
1268 }
vandebo@chromium.org5f209e62013-12-10 17:22:41 +00001269 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData));
1270 addResource(fontStream.get());
1271 fontStream->insertInt("Length1", header);
1272 fontStream->insertInt("Length2", data);
1273 fontStream->insertInt("Length3", trailer);
1274 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001275
1276 addResource(descriptor.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001277 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1278
1279 return addCommonFontDescriptorEntries(defaultWidth);
1280}
1281
1282bool SkPDFType1Font::populate(int16_t glyphID) {
1283 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1284 SkASSERT(fontInfo()->fGlyphWidths.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001285
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001286 adjustGlyphRangeForSingleByteEncoding(glyphID);
1287
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001288 int16_t defaultWidth = 0;
1289 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
1290 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001291 for (widthEntry = fontInfo()->fGlyphWidths.get();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001292 widthEntry != NULL;
1293 widthEntry = widthEntry->fNext.get()) {
1294 switch (widthEntry->fType) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001295 case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001296 defaultWidth = widthEntry->fAdvance[0];
1297 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001298 case SkAdvancedTypefaceMetrics::WidthRange::kRun:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001299 SkASSERT(false);
1300 break;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +00001301 case SkAdvancedTypefaceMetrics::WidthRange::kRange:
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001302 SkASSERT(widthRangeEntry == NULL);
1303 widthRangeEntry = widthEntry;
1304 break;
1305 }
1306 }
1307
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001308 if (!addFontDescriptor(defaultWidth)) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001309 return false;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001310 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001311
reed@google.comc789cf12011-07-20 12:14:33 +00001312 insertName("Subtype", "Type1");
vandebo@chromium.org98594282011-07-25 22:34:12 +00001313 insertName("BaseFont", fontInfo()->fFontName);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001314
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001315 addWidthInfoFromRange(defaultWidth, widthRangeEntry);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001316
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001317 SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001318 insert("Encoding", encoding.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001319
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001320 SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001321 encoding->insert("Differences", encDiffs.get());
1322
vandebo@chromium.org98594282011-07-25 22:34:12 +00001323 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001324 encDiffs->appendInt(1);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001325 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
1326 encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001327 }
1328
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001329 return true;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001330}
1331
vandebo@chromium.org98594282011-07-25 22:34:12 +00001332void SkPDFType1Font::addWidthInfoFromRange(
1333 int16_t defaultWidth,
1334 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001335 SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001336 int firstChar = 0;
1337 if (widthRangeEntry) {
1338 const uint16_t emSize = fontInfo()->fEmSize;
1339 int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
1340 int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
1341 if (startIndex < 0)
1342 startIndex = 0;
1343 if (endIndex > widthRangeEntry->fAdvance.count())
1344 endIndex = widthRangeEntry->fAdvance.count();
1345 if (widthRangeEntry->fStartId == 0) {
1346 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
1347 } else {
1348 firstChar = startIndex + widthRangeEntry->fStartId;
1349 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001350 for (int i = startIndex; i < endIndex; i++) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001351 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001352 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001353 } else {
1354 appendWidth(defaultWidth, 1000, widthArray.get());
1355 }
1356 insertInt("FirstChar", firstChar);
1357 insertInt("LastChar", firstChar + widthArray->size() - 1);
1358 insert("Widths", widthArray.get());
1359}
1360
1361///////////////////////////////////////////////////////////////////////////////
1362// class SkPDFType3Font
1363///////////////////////////////////////////////////////////////////////////////
1364
1365SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
1366 SkTypeface* typeface,
sugoi@google.come2e81132013-03-05 18:35:55 +00001367 uint16_t glyphID)
1368 : SkPDFFont(info, typeface, NULL) {
vandebo@chromium.org98594282011-07-25 22:34:12 +00001369 populate(glyphID);
1370}
1371
1372SkPDFType3Font::~SkPDFType3Font() {}
1373
1374bool SkPDFType3Font::populate(int16_t glyphID) {
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001375 SkPaint paint;
vandebo@chromium.org98594282011-07-25 22:34:12 +00001376 paint.setTypeface(typeface());
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001377 paint.setTextSize(1000);
bungeman@google.com532470f2013-01-22 19:25:14 +00001378 SkAutoGlyphCache autoCache(paint, NULL, NULL);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001379 SkGlyphCache* cache = autoCache.getCache();
1380 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
vandebo@chromium.org98594282011-07-25 22:34:12 +00001381 if (lastGlyphID() == 0) {
1382 setLastGlyphID(cache->getGlyphCount() - 1);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +00001383 }
1384
1385 adjustGlyphRangeForSingleByteEncoding(glyphID);
1386
reed@google.comc789cf12011-07-20 12:14:33 +00001387 insertName("Subtype", "Type3");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001388 // Flip about the x-axis and scale by 1/1000.
1389 SkMatrix fontMatrix;
1390 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
1391 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
1392
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001393 SkAutoTUnref<SkPDFDict> charProcs(new SkPDFDict);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001394 insert("CharProcs", charProcs.get());
1395
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001396 SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001397 insert("Encoding", encoding.get());
1398
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001399 SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001400 encoding->insert("Differences", encDiffs.get());
vandebo@chromium.org98594282011-07-25 22:34:12 +00001401 encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
reed@google.comc789cf12011-07-20 12:14:33 +00001402 encDiffs->appendInt(1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001403
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001404 SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001405
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001406 SkIRect bbox = SkIRect::MakeEmpty();
vandebo@chromium.org98594282011-07-25 22:34:12 +00001407 for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001408 SkString characterName;
1409 characterName.printf("gid%d", gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001410 encDiffs->appendName(characterName.c_str());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001411
reed@google.comce11b262011-03-21 21:25:35 +00001412 const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
reed@google.comc789cf12011-07-20 12:14:33 +00001413 widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001414 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
1415 glyph.fWidth, glyph.fHeight);
1416 bbox.join(glyphBBox);
1417
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001418 SkDynamicMemoryWStream content;
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001419 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
1420 &content);
1421 const SkPath* path = cache->findPath(glyph);
1422 if (path) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001423 SkPDFUtils::EmitPath(*path, paint.getStyle(), &content);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001424 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
1425 &content);
1426 }
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001427 SkAutoTUnref<SkMemoryStream> glyphStream(new SkMemoryStream());
reed@google.com8a85d0c2011-06-24 19:12:12 +00001428 glyphStream->setData(content.copyToData())->unref();
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001429
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001430 SkAutoTUnref<SkPDFStream> glyphDescription(
1431 new SkPDFStream(glyphStream.get()));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001432 addResource(glyphDescription.get());
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001433 charProcs->insert(characterName.c_str(),
1434 new SkPDFObjRef(glyphDescription.get()))->unref();
1435 }
1436
1437 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +00001438 insertInt("FirstChar", 1);
1439 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001440 insert("Widths", widthArray.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001441 insertName("CIDToGIDMap", "Identity");
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001442
vandebo@chromium.org98594282011-07-25 22:34:12 +00001443 populateToUnicodeTable(NULL);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001444 return true;
1445}