vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 8 | #include <ctype.h> |
| 9 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 11 | #include "SkFontHost.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 12 | #include "SkGlyphCache.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 13 | #include "SkPaint.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 14 | #include "SkPDFCatalog.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 15 | #include "SkPDFDevice.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 16 | #include "SkPDFFont.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 17 | #include "SkPDFFontImpl.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 18 | #include "SkPDFStream.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 19 | #include "SkPDFTypes.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 20 | #include "SkPDFUtils.h" |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 21 | #include "SkRefCnt.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 22 | #include "SkScalar.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 23 | #include "SkStream.h" |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 24 | #include "SkTypefacePriv.h" |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 25 | #include "SkTypes.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 26 | #include "SkUtils.h" |
| 27 | |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 28 | #if defined (SK_SFNTLY_SUBSETTER) |
| 29 | #include SK_SFNTLY_SUBSETTER |
| 30 | #endif |
| 31 | |
vandebo@chromium.org | dcf9c19 | 2013-03-13 20:01:51 +0000 | [diff] [blame] | 32 | // 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) |
| 35 | static const int kPdfSymbolic = 4; |
| 36 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | // File-Local Functions |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 43 | bool 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 50 | if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 51 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 52 | } else if (buf[1] == 3) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 53 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 54 | } else if (*len < 6) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 55 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 56 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 57 | |
vandebo@chromium.org | 7332207 | 2011-06-21 21:19:41 +0000 | [diff] [blame] | 58 | *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) | |
| 59 | ((size_t)buf[5] << 24); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 60 | size_t consumed = *size + 6; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 61 | if (consumed > *len) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 62 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 63 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 64 | *src = *src + consumed; |
| 65 | *len = *len - consumed; |
| 66 | return true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 67 | } |
| 68 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 69 | bool 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.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 78 | } |
| 79 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 80 | /* 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 | */ |
| 88 | bool 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 93 | if (!dataPos) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 94 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 95 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 96 | dataPos += strlen("eexec"); |
| 97 | while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') && |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 98 | dataPos < end) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 99 | dataPos++; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 100 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 101 | *headerLen = dataPos - src; |
| 102 | |
| 103 | const char* trailerPos = strstr(dataPos, "cleartomark"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 104 | if (!trailerPos) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 105 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 106 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 107 | 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 117 | if (zeroCount != 512) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 118 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 119 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 120 | |
| 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 127 | if (isspace(*dataPos)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 128 | continue; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 129 | } |
| 130 | if (!isxdigit(*dataPos)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 131 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 132 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 133 | nibbles++; |
| 134 | } |
| 135 | *dataLen = (nibbles + 1) / 2; |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | int8_t hexToBin(uint8_t c) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 141 | if (!isxdigit(c)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 142 | return -1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 143 | } 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, |
| 154 | size_t* dataLen, size_t* trailerLen) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 155 | // srcStream may be backed by a file or a unseekable fd, so we may not be |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 156 | // 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.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 161 | SkAutoTUnref<SkMemoryStream> staticStream; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 162 | SkData* data = NULL; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 163 | const uint8_t* src; |
| 164 | size_t srcLen; |
| 165 | if ((srcLen = srcStream->getLength()) > 0) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 166 | staticStream.reset(new SkMemoryStream(srcLen + 1)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 167 | 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 175 | if (got == 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 176 | return NULL; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 177 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 178 | read += got; |
| 179 | staticStream->seek(read); |
| 180 | } |
| 181 | } |
| 182 | ((uint8_t *)src)[srcLen] = 0; |
| 183 | } else { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 184 | static const size_t kBufSize = 4096; |
| 185 | uint8_t buf[kBufSize]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 186 | size_t amount; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 187 | while ((amount = srcStream->read(buf, kBufSize)) > 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 188 | dynamicStream.write(buf, amount); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 189 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 190 | amount = 0; |
| 191 | dynamicStream.write(&amount, 1); // NULL terminator. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 192 | data = dynamicStream.copyToData(); |
| 193 | src = data->bytes(); |
| 194 | srcLen = data->size() - 1; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 195 | } |
| 196 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 197 | // 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 201 | 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.org | 5b07368 | 2011-03-08 18:33:31 +0000 | [diff] [blame] | 226 | uint8_t dataByte = 0; // To hush compiler. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 227 | bool highNibble = true; |
| 228 | for (; hexData < trailer; hexData++) { |
commit-bot@chromium.org | 3720fda | 2013-07-10 15:03:52 +0000 | [diff] [blame] | 229 | int8_t curNibble = hexToBin(*hexData); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 230 | if (curNibble < 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 231 | continue; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 232 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 233 | 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 242 | if (!highNibble) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 243 | ((char *)result->getAtPos())[outputOffset++] = dataByte; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 244 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 245 | 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.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 257 | // scale from em-units to base-1000, returning as a SkScalar |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 258 | SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 259 | SkScalar scaled = SkIntToScalar(val); |
| 260 | if (emSize == 1000) { |
| 261 | return scaled; |
| 262 | } else { |
| 263 | return SkScalarMulDiv(scaled, 1000, emSize); |
| 264 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 265 | } |
| 266 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 267 | void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 268 | SkWStream* content) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 269 | // Specify width and bounding box for the glyph. |
| 270 | SkPDFScalar::Append(width, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 271 | 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.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 280 | } |
| 281 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 282 | SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 283 | SkPDFArray* bbox = new SkPDFArray; |
| 284 | bbox->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 285 | 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.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 289 | return bbox; |
| 290 | } |
| 291 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 292 | SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize, |
| 293 | SkPDFArray* array) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 294 | array->appendScalar(scaleFromFontUnits(width, emSize)); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 295 | return array; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 296 | } |
| 297 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 298 | SkPDFArray* appendVerticalAdvance( |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 299 | 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.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 304 | return array; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | template <typename Data> |
| 308 | SkPDFArray* composeAdvanceData( |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 309 | SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo, |
| 310 | uint16_t emSize, |
| 311 | SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize, |
| 312 | SkPDFArray* array), |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 313 | Data* defaultAdvance) { |
| 314 | SkPDFArray* result = new SkPDFArray(); |
| 315 | for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) { |
| 316 | switch (advanceInfo->fType) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 317 | case SkAdvancedTypefaceMetrics::WidthRange::kDefault: { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 318 | SkASSERT(advanceInfo->fAdvance.count() == 1); |
| 319 | *defaultAdvance = advanceInfo->fAdvance[0]; |
| 320 | break; |
| 321 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 322 | case SkAdvancedTypefaceMetrics::WidthRange::kRange: { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 323 | SkAutoTUnref<SkPDFArray> advanceArray(new SkPDFArray()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 324 | for (int j = 0; j < advanceInfo->fAdvance.count(); j++) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 325 | appendAdvance(advanceInfo->fAdvance[j], emSize, |
| 326 | advanceArray.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 327 | result->appendInt(advanceInfo->fStartId); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 328 | result->append(advanceArray.get()); |
| 329 | break; |
| 330 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 331 | case SkAdvancedTypefaceMetrics::WidthRange::kRun: { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 332 | SkASSERT(advanceInfo->fAdvance.count() == 1); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 333 | result->appendInt(advanceInfo->fStartId); |
| 334 | result->appendInt(advanceInfo->fEndId); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 335 | appendAdvance(advanceInfo->fAdvance[0], emSize, result); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | } // namespace |
| 344 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 345 | static void append_tounicode_header(SkDynamicMemoryWStream* cmap, |
| 346 | uint16_t firstGlyphID, |
| 347 | uint16_t lastGlyphID) { |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 348 | // 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.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 370 | // Codespace range just tells the PDF processor the valid range. |
| 371 | const char* kTypeInfoHeader = |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 372 | "/CMapName /Adobe-Identity-UCS def\n" |
| 373 | "/CMapType 2 def\n" |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 374 | "1 begincodespacerange\n"; |
| 375 | cmap->writeText(kTypeInfoHeader); |
skia.committer@gmail.com | 2262c58 | 2013-09-19 07:25:55 +0000 | [diff] [blame] | 376 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 377 | // 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.com | 2262c58 | 2013-09-19 07:25:55 +0000 | [diff] [blame] | 381 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 382 | const char* kTypeInfoFooter = "endcodespacerange\n"; |
| 383 | cmap->writeText(kTypeInfoFooter); |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 384 | } |
| 385 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 386 | static 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.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 395 | struct BFChar { |
| 396 | uint16_t fGlyphId; |
| 397 | SkUnichar fUnicode; |
| 398 | }; |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 399 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 400 | struct BFRange { |
| 401 | uint16_t fStart; |
| 402 | uint16_t fEnd; |
| 403 | SkUnichar fUnicode; |
| 404 | }; |
| 405 | |
| 406 | static 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.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 425 | static 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.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 463 | // overlap, but succeeding maps supersede preceding maps." |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 464 | // |
| 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.com | 1445a0d | 2012-06-06 12:04:24 +0000 | [diff] [blame] | 472 | |
| 473 | // FIXME: this should be in a header so that it is separately testable |
| 474 | // ( see caller in tests/ToUnicode.cpp ) |
| 475 | void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, |
| 476 | const SkPDFGlyphSet* subset, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 477 | SkDynamicMemoryWStream* cmap, |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 478 | bool multiByteGlyphs, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 479 | uint16_t firstGlyphID, |
| 480 | uint16_t lastGlyphID); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 481 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 482 | void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, |
| 483 | const SkPDFGlyphSet* subset, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 484 | SkDynamicMemoryWStream* cmap, |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 485 | bool multiByteGlyphs, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 486 | uint16_t firstGlyphID, |
| 487 | uint16_t lastGlyphID) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 488 | if (glyphToUnicode.isEmpty()) { |
| 489 | return; |
| 490 | } |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 491 | int glyphOffset = 0; |
| 492 | if (!multiByteGlyphs) { |
| 493 | glyphOffset = firstGlyphID - 1; |
| 494 | } |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 495 | |
| 496 | SkTDArray<BFChar> bfcharEntries; |
| 497 | SkTDArray<BFRange> bfrangeEntries; |
| 498 | |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 499 | BFRange currentRangeEntry = {0, 0, 0}; |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 500 | bool rangeEmpty = true; |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 501 | const int limit = |
| 502 | SkMin32(lastGlyphID + 1, glyphToUnicode.count()) - glyphOffset; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 503 | |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 504 | for (int i = firstGlyphID - glyphOffset; i < limit + 1; ++i) { |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 505 | bool inSubset = i < limit && |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 506 | (subset == NULL || subset->has(i + glyphOffset)); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 507 | if (!rangeEmpty) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 508 | // 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.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 511 | bool inRange = |
| 512 | i == currentRangeEntry.fEnd + 1 && |
| 513 | i >> 8 == currentRangeEntry.fStart >> 8 && |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 514 | i < limit && |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 515 | glyphToUnicode[i + glyphOffset] == |
| 516 | currentRangeEntry.fUnicode + i - currentRangeEntry.fStart; |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 517 | if (!inSubset || !inRange) { |
| 518 | if (currentRangeEntry.fEnd > currentRangeEntry.fStart) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 519 | bfrangeEntries.push(currentRangeEntry); |
| 520 | } else { |
| 521 | BFChar* entry = bfcharEntries.append(); |
| 522 | entry->fGlyphId = currentRangeEntry.fStart; |
| 523 | entry->fUnicode = currentRangeEntry.fUnicode; |
| 524 | } |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 525 | rangeEmpty = true; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 526 | } |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 527 | } |
| 528 | if (inSubset) { |
| 529 | currentRangeEntry.fEnd = i; |
| 530 | if (rangeEmpty) { |
| 531 | currentRangeEntry.fStart = i; |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 532 | currentRangeEntry.fUnicode = glyphToUnicode[i + glyphOffset]; |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 533 | rangeEmpty = false; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 534 | } |
| 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.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 544 | static SkPDFStream* generate_tounicode_cmap( |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 545 | const SkTDArray<SkUnichar>& glyphToUnicode, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 546 | const SkPDFGlyphSet* subset, |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 547 | bool multiByteGlyphs, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 548 | uint16_t firstGlyphID, |
| 549 | uint16_t lastGlyphID) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 550 | SkDynamicMemoryWStream cmap; |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 551 | 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.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 557 | firstGlyphID, lastGlyphID); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 558 | append_cmap_footer(&cmap); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 559 | SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream()); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 560 | cmapStream->setData(cmap.copyToData())->unref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 561 | return new SkPDFStream(cmapStream.get()); |
| 562 | } |
| 563 | |
caryclark@google.com | 1445a0d | 2012-06-06 12:04:24 +0000 | [diff] [blame] | 564 | #if defined (SK_SFNTLY_SUBSETTER) |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 565 | static 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.com | 1445a0d | 2012-06-06 12:04:24 +0000 | [diff] [blame] | 569 | #endif |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 570 | |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 571 | static size_t get_subset_font_stream(const char* fontName, |
| 572 | const SkTypeface* typeface, |
| 573 | const SkTDArray<uint32_t>& subset, |
| 574 | SkPDFStream** fontStream) { |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 575 | int ttcIndex; |
| 576 | SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 577 | |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 578 | size_t fontSize = fontData->getLength(); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 579 | |
| 580 | #if defined (SK_SFNTLY_SUBSETTER) |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 581 | // Read font into buffer. |
| 582 | SkPDFStream* subsetFontStream = NULL; |
| 583 | SkTDArray<unsigned char> originalFont; |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 584 | originalFont.setCount(SkToInt(fontSize)); |
| 585 | if (fontData->read(originalFont.begin(), fontSize) == fontSize) { |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 586 | unsigned char* subsetFont = NULL; |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 587 | // 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.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 591 | int subsetFontSize = SfntlyWrapper::SubsetFont(fontName, |
| 592 | originalFont.begin(), |
| 593 | fontSize, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 594 | subset.begin(), |
| 595 | subset.count(), |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 596 | &subsetFont); |
| 597 | if (subsetFontSize > 0 && subsetFont != NULL) { |
vandebo@chromium.org | 93225ff | 2011-07-27 18:38:11 +0000 | [diff] [blame] | 598 | SkAutoDataUnref data(SkData::NewWithProc(subsetFont, |
| 599 | subsetFontSize, |
| 600 | sk_delete_array, |
| 601 | NULL)); |
| 602 | subsetFontStream = new SkPDFStream(data.get()); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 603 | fontSize = subsetFontSize; |
| 604 | } |
| 605 | } |
| 606 | if (subsetFontStream) { |
| 607 | *fontStream = subsetFontStream; |
| 608 | return fontSize; |
| 609 | } |
vandebo@chromium.org | 938ea62 | 2013-09-05 19:44:44 +0000 | [diff] [blame] | 610 | fontData->rewind(); |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 611 | #else |
| 612 | sk_ignore_unused_variable(fontName); |
| 613 | sk_ignore_unused_variable(subset); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 614 | #endif |
| 615 | |
| 616 | // Fail over: just embed the whole font. |
| 617 | *fontStream = new SkPDFStream(fontData.get()); |
| 618 | return fontSize; |
| 619 | } |
| 620 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 621 | /////////////////////////////////////////////////////////////////////////////// |
| 622 | // class SkPDFGlyphSet |
| 623 | /////////////////////////////////////////////////////////////////////////////// |
| 624 | |
| 625 | SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) { |
| 626 | } |
| 627 | |
| 628 | void 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 | |
| 634 | bool SkPDFGlyphSet::has(uint16_t glyphID) const { |
| 635 | return fBitSet.isBitSet(glyphID); |
| 636 | } |
| 637 | |
| 638 | void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) { |
| 639 | fBitSet.orBits(usage.fBitSet); |
| 640 | } |
| 641 | |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 642 | void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const { |
| 643 | fBitSet.exportTo(glyphIDs); |
| 644 | } |
| 645 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 646 | /////////////////////////////////////////////////////////////////////////////// |
| 647 | // class SkPDFGlyphSetMap |
| 648 | /////////////////////////////////////////////////////////////////////////////// |
| 649 | SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font, |
| 650 | SkPDFGlyphSet* glyphSet) |
| 651 | : fFont(font), |
| 652 | fGlyphSet(glyphSet) { |
| 653 | } |
| 654 | |
| 655 | SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) { |
| 656 | reset(map); |
| 657 | } |
| 658 | |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 659 | const SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 660 | if (fIndex >= fMap->count()) { |
| 661 | return NULL; |
| 662 | } |
| 663 | return &((*fMap)[fIndex++]); |
| 664 | } |
| 665 | |
| 666 | void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) { |
| 667 | fMap = &(map.fMap); |
| 668 | fIndex = 0; |
| 669 | } |
| 670 | |
| 671 | SkPDFGlyphSetMap::SkPDFGlyphSetMap() { |
| 672 | } |
| 673 | |
| 674 | SkPDFGlyphSetMap::~SkPDFGlyphSetMap() { |
| 675 | reset(); |
| 676 | } |
| 677 | |
| 678 | void 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 | |
| 685 | void 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 | |
| 692 | void 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 | |
| 700 | SkPDFGlyphSet* 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 718 | /* 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.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 730 | SkPDFFont::~SkPDFFont() { |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 731 | SkAutoMutexAcquire lock(CanonicalFontsMutex()); |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 732 | 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.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 738 | |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 739 | SkDEBUGCODE(int indexFound;) |
| 740 | SkASSERT(index == -1 || |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 741 | (Find(fTypeface->uniqueID(), |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 742 | fFirstGlyphID, |
| 743 | &indexFound) && |
| 744 | index == indexFound)); |
| 745 | if (index >= 0) { |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 746 | CanonicalFonts().removeShuffle(index); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 747 | } |
| 748 | fResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 749 | } |
| 750 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 751 | void SkPDFFont::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
| 752 | SkTSet<SkPDFObject*>* newResourceObjects) { |
| 753 | GetResourcesHelper(&fResources, knownResourceObjects, newResourceObjects); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 754 | } |
| 755 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 756 | SkTypeface* SkPDFFont::typeface() { |
| 757 | return fTypeface.get(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 758 | } |
| 759 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 760 | SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 761 | return fFontType; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 762 | } |
| 763 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 764 | bool SkPDFFont::hasGlyph(uint16_t id) { |
| 765 | return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; |
| 766 | } |
| 767 | |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 768 | int SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 769 | // A font with multibyte glyphs will support all glyph IDs in a single font. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 770 | if (this->multiByteGlyphs()) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 771 | return numGlyphs; |
| 772 | } |
| 773 | |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 774 | for (int i = 0; i < numGlyphs; i++) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 775 | if (glyphIDs[i] == 0) { |
| 776 | continue; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 777 | } |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 778 | if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) { |
| 779 | return i; |
| 780 | } |
| 781 | glyphIDs[i] -= (fFirstGlyphID - 1); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 782 | } |
| 783 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 784 | return numGlyphs; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 788 | SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) { |
| 789 | SkAutoMutexAcquire lock(CanonicalFontsMutex()); |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 790 | |
| 791 | SkAutoResolveDefaultTypeface autoResolve(typeface); |
| 792 | typeface = autoResolve.get(); |
| 793 | |
| 794 | const uint32_t fontID = typeface->uniqueID(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 795 | int relatedFontIndex; |
| 796 | if (Find(fontID, glyphID, &relatedFontIndex)) { |
| 797 | CanonicalFonts()[relatedFontIndex].fFont->ref(); |
| 798 | return CanonicalFonts()[relatedFontIndex].fFont; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 799 | } |
| 800 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 801 | SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 802 | SkPDFDict* relatedFontDescriptor = NULL; |
| 803 | if (relatedFontIndex >= 0) { |
| 804 | SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 805 | fontMetrics.reset(relatedFont->fontInfo()); |
| 806 | SkSafeRef(fontMetrics.get()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 807 | relatedFontDescriptor = relatedFont->getFontDescriptor(); |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 808 | |
| 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.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 811 | // for True Type fonts. |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 812 | SkAdvancedTypefaceMetrics::FontType fontType = |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 813 | 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.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 820 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 821 | } else { |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 822 | SkAdvancedTypefaceMetrics::PerGlyphInfo info; |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 823 | info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo; |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 824 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 825 | info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 826 | #if !defined (SK_SFNTLY_SUBSETTER) |
| 827 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 828 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
| 829 | #endif |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 830 | fontMetrics.reset( |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 831 | typeface->getAdvancedTypefaceMetrics(info, NULL, 0)); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 832 | #if defined (SK_SFNTLY_SUBSETTER) |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 833 | if (fontMetrics.get() && |
vandebo@chromium.org | b3b4655 | 2011-10-17 23:22:49 +0000 | [diff] [blame] | 834 | fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 835 | // Font does not support subsetting, get new info with advance. |
| 836 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 837 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 838 | fontMetrics.reset( |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 839 | typeface->getAdvancedTypefaceMetrics(info, NULL, 0)); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 840 | } |
| 841 | #endif |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 842 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 843 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 844 | SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID, |
| 845 | relatedFontDescriptor); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 846 | FontRec newEntry(font, fontID, font->fFirstGlyphID); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 847 | CanonicalFonts().push(newEntry); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 848 | return font; // Return the reference new SkPDFFont() created. |
| 849 | } |
| 850 | |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 851 | SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 852 | return NULL; // Default: no support. |
| 853 | } |
| 854 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 855 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 856 | SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 857 | // This initialization is only thread safe with gcc. |
| 858 | static SkTDArray<FontRec> gCanonicalFonts; |
| 859 | return gCanonicalFonts; |
| 860 | } |
| 861 | |
| 862 | // static |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 863 | SkBaseMutex& 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.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 867 | return gCanonicalFontsMutex; |
| 868 | } |
| 869 | |
| 870 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 871 | bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 872 | // TODO(vandebo): Optimize this, do only one search? |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 873 | FontRec search(NULL, fontID, glyphID); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 874 | *index = CanonicalFonts().find(search); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 875 | if (*index >= 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 876 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 877 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 878 | search.fGlyphID = 0; |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 879 | *index = CanonicalFonts().find(search); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 880 | return false; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 881 | } |
| 882 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 883 | SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 884 | SkPDFDict* relatedFontDescriptor) |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 885 | : SkPDFDict("Font"), |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 886 | fTypeface(ref_or_default(typeface)), |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 887 | fFirstGlyphID(1), |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 888 | fLastGlyphID(info ? info->fLastGlyphID : 0), |
commit-bot@chromium.org | 4566107 | 2013-12-05 21:14:02 +0000 | [diff] [blame] | 889 | fFontInfo(SkSafeRef(info)), |
| 890 | fDescriptor(SkSafeRef(relatedFontDescriptor)) { |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 891 | if (info == NULL) { |
| 892 | fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 893 | } else if (info->fMultiMaster) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 894 | fFontType = SkAdvancedTypefaceMetrics::kOther_Font; |
| 895 | } else { |
| 896 | fFontType = info->fType; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // static |
| 901 | SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info, |
| 902 | SkTypeface* typeface, uint16_t glyphID, |
| 903 | SkPDFDict* relatedFontDescriptor) { |
| 904 | SkAdvancedTypefaceMetrics::FontType type = |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 905 | info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 906 | |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 907 | if (info && info->fMultiMaster) { |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 908 | NOT_IMPLEMENTED(true, true); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 909 | return new SkPDFType3Font(info, |
| 910 | typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 911 | glyphID); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 912 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 913 | 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.org | 6504cfd | 2011-07-23 20:22:53 +0000 | [diff] [blame] | 923 | } |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 924 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 925 | SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 926 | type == SkAdvancedTypefaceMetrics::kOther_Font || |
| 927 | type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 928 | |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 929 | return new SkPDFType3Font(info, typeface, glyphID); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 930 | } |
| 931 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 932 | SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { |
| 933 | return fFontInfo.get(); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 934 | } |
| 935 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 936 | void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { |
| 937 | if (info == NULL || info == fFontInfo.get()) { |
| 938 | return; |
| 939 | } |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 940 | fFontInfo.reset(info); |
| 941 | SkSafeRef(info); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 942 | } |
| 943 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 944 | uint16_t SkPDFFont::firstGlyphID() const { |
| 945 | return fFirstGlyphID; |
| 946 | } |
| 947 | |
| 948 | uint16_t SkPDFFont::lastGlyphID() const { |
| 949 | return fLastGlyphID; |
| 950 | } |
| 951 | |
| 952 | void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
| 953 | fLastGlyphID = glyphID; |
| 954 | } |
| 955 | |
| 956 | void SkPDFFont::addResource(SkPDFObject* object) { |
| 957 | SkASSERT(object != NULL); |
| 958 | fResources.push(object); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 959 | object->ref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | SkPDFDict* SkPDFFont::getFontDescriptor() { |
| 963 | return fDescriptor.get(); |
| 964 | } |
| 965 | |
| 966 | void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 967 | fDescriptor.reset(descriptor); |
| 968 | SkSafeRef(descriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
| 972 | if (fDescriptor.get() == NULL) { |
| 973 | return false; |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 974 | } |
| 975 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 976 | const uint16_t emSize = fFontInfo->fEmSize; |
| 977 | |
| 978 | fDescriptor->insertName("FontName", fFontInfo->fFontName); |
vandebo@chromium.org | dcf9c19 | 2013-03-13 20:01:51 +0000 | [diff] [blame] | 979 | fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 980 | 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 | |
| 999 | void 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 | |
| 1007 | bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1008 | if (fFontID != b.fFontID) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1009 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1010 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1011 | if (fFont != NULL && b.fFont != NULL) { |
| 1012 | return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID && |
| 1013 | fFont->fLastGlyphID == b.fFont->fLastGlyphID; |
| 1014 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1015 | if (fGlyphID == 0 || b.fGlyphID == 0) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1016 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1017 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1018 | |
| 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 | |
| 1029 | SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID) |
| 1030 | : fFont(font), |
| 1031 | fFontID(fontID), |
| 1032 | fGlyphID(glyphID) { |
| 1033 | } |
| 1034 | |
| 1035 | void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { |
| 1036 | if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) { |
| 1037 | return; |
| 1038 | } |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1039 | SkAutoTUnref<SkPDFStream> pdfCmap( |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 1040 | generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset, |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 1041 | multiByteGlyphs(), firstGlyphID(), |
| 1042 | lastGlyphID())); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1043 | addResource(pdfCmap.get()); |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 1044 | insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1047 | /////////////////////////////////////////////////////////////////////////////// |
| 1048 | // class SkPDFType0Font |
| 1049 | /////////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1050 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1051 | SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, |
| 1052 | SkTypeface* typeface) |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 1053 | : SkPDFFont(info, typeface, NULL) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1054 | SkDEBUGCODE(fPopulated = false); |
| 1055 | } |
| 1056 | |
| 1057 | SkPDFType0Font::~SkPDFType0Font() {} |
| 1058 | |
| 1059 | SkPDFFont* 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 |
| 1066 | void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
| 1067 | bool indirect) { |
| 1068 | SkASSERT(fPopulated); |
| 1069 | return INHERITED::emitObject(stream, catalog, indirect); |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
| 1073 | bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
| 1074 | insertName("Subtype", "Type0"); |
| 1075 | insertName("BaseFont", fontInfo()->fFontName); |
| 1076 | insertName("Encoding", "Identity-H"); |
| 1077 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1078 | 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.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1083 | insert("DescendantFonts", descendantFonts.get()); |
| 1084 | |
| 1085 | populateToUnicodeTable(subset); |
| 1086 | |
| 1087 | SkDEBUGCODE(fPopulated = true); |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| 1091 | /////////////////////////////////////////////////////////////////////////////// |
| 1092 | // class SkPDFCIDFont |
| 1093 | /////////////////////////////////////////////////////////////////////////////// |
| 1094 | |
| 1095 | SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info, |
| 1096 | SkTypeface* typeface, const SkPDFGlyphSet* subset) |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 1097 | : SkPDFFont(info, typeface, NULL) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1098 | populate(subset); |
| 1099 | } |
| 1100 | |
| 1101 | SkPDFCIDFont::~SkPDFCIDFont() {} |
| 1102 | |
| 1103 | bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1104 | const SkTDArray<uint32_t>* subset) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1105 | SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1106 | setFontDescriptor(descriptor.get()); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1107 | addResource(descriptor.get()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1108 | |
| 1109 | switch (getType()) { |
| 1110 | case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 1111 | SkASSERT(subset); |
| 1112 | // Font subsetting |
| 1113 | SkPDFStream* rawStream = NULL; |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 1114 | size_t fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(), |
| 1115 | typeface(), |
| 1116 | *subset, |
| 1117 | &rawStream); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 1118 | SkASSERT(fontSize); |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 1119 | SkASSERT(rawStream); |
| 1120 | SkAutoTUnref<SkPDFStream> fontStream(rawStream); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1121 | addResource(fontStream.get()); |
| 1122 | |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 1123 | fontStream->insertInt("Length1", fontSize); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1124 | descriptor->insert("FontFile2", |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1125 | new SkPDFObjRef(fontStream.get()))->unref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | } |
| 1128 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
| 1129 | case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1130 | int ttcIndex; |
| 1131 | SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex)); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1132 | SkAutoTUnref<SkPDFStream> fontStream( |
| 1133 | new SkPDFStream(fontData.get())); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1134 | 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.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 1148 | |
| 1149 | insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1150 | return addCommonFontDescriptorEntries(defaultWidth); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1154 | // 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.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1158 | if (subset) { |
commit-bot@chromium.org | 4da3bfc | 2013-12-11 23:54:31 +0000 | [diff] [blame] | 1159 | // Always include glyph 0. |
| 1160 | if (!subset->has(0)) { |
| 1161 | glyphIDs.push(0); |
| 1162 | } |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1163 | subset->exportTo(&glyphIDs); |
| 1164 | } |
| 1165 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1166 | SkAdvancedTypefaceMetrics::PerGlyphInfo info; |
| 1167 | info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo; |
| 1168 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 1169 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
commit-bot@chromium.org | 4da3bfc | 2013-12-11 23:54:31 +0000 | [diff] [blame] | 1170 | uint32_t* glyphs = (glyphIDs.count() == 0) ? NULL : glyphIDs.begin(); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1171 | uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1172 | SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics( |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1173 | typeface()->getAdvancedTypefaceMetrics(info, glyphs, glyphsCount)); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1174 | setFontInfo(fontMetrics.get()); |
| 1175 | addFontDescriptor(0, &glyphIDs); |
| 1176 | } else { |
| 1177 | // Other CID fonts |
| 1178 | addFontDescriptor(0, NULL); |
| 1179 | } |
| 1180 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1181 | insertName("BaseFont", fontInfo()->fFontName); |
| 1182 | |
| 1183 | if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1184 | insertName("Subtype", "CIDFontType0"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1185 | } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1186 | insertName("Subtype", "CIDFontType2"); |
| 1187 | insertName("CIDToGIDMap", "Identity"); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1188 | } else { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1189 | SkASSERT(false); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1190 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1191 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1192 | SkAutoTUnref<SkPDFDict> sysInfo(new SkPDFDict); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1193 | sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref(); |
| 1194 | sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref(); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1195 | sysInfo->insertInt("Supplement", 0); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1196 | insert("CIDSystemInfo", sysInfo.get()); |
| 1197 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1198 | if (fontInfo()->fGlyphWidths.get()) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1199 | int16_t defaultWidth = 0; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1200 | SkAutoTUnref<SkPDFArray> widths( |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1201 | composeAdvanceData(fontInfo()->fGlyphWidths.get(), |
| 1202 | fontInfo()->fEmSize, &appendWidth, |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1203 | &defaultWidth)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1204 | if (widths->size()) |
| 1205 | insert("W", widths.get()); |
| 1206 | if (defaultWidth != 0) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1207 | insertScalar("DW", scaleFromFontUnits(defaultWidth, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1208 | fontInfo()->fEmSize)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1211 | if (fontInfo()->fVerticalMetrics.get()) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1212 | struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1213 | defaultAdvance.fVerticalAdvance = 0; |
| 1214 | defaultAdvance.fOriginXDisp = 0; |
| 1215 | defaultAdvance.fOriginYDisp = 0; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1216 | SkAutoTUnref<SkPDFArray> advances( |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1217 | composeAdvanceData(fontInfo()->fVerticalMetrics.get(), |
| 1218 | fontInfo()->fEmSize, &appendVerticalAdvance, |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1219 | &defaultAdvance)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1220 | if (advances->size()) |
| 1221 | insert("W2", advances.get()); |
| 1222 | if (defaultAdvance.fVerticalAdvance || |
| 1223 | defaultAdvance.fOriginXDisp || |
| 1224 | defaultAdvance.fOriginYDisp) { |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1225 | insert("DW2", appendVerticalAdvance(defaultAdvance, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1226 | fontInfo()->fEmSize, |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1227 | new SkPDFArray))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1230 | |
| 1231 | return true; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1234 | /////////////////////////////////////////////////////////////////////////////// |
| 1235 | // class SkPDFType1Font |
| 1236 | /////////////////////////////////////////////////////////////////////////////// |
| 1237 | |
| 1238 | SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info, |
| 1239 | SkTypeface* typeface, |
| 1240 | uint16_t glyphID, |
| 1241 | SkPDFDict* relatedFontDescriptor) |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 1242 | : SkPDFFont(info, typeface, relatedFontDescriptor) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1243 | populate(glyphID); |
| 1244 | } |
| 1245 | |
| 1246 | SkPDFType1Font::~SkPDFType1Font() {} |
| 1247 | |
| 1248 | bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1249 | if (getFontDescriptor() != NULL) { |
| 1250 | SkPDFDict* descriptor = getFontDescriptor(); |
| 1251 | addResource(descriptor); |
| 1252 | insert("FontDescriptor", new SkPDFObjRef(descriptor))->unref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1253 | return true; |
| 1254 | } |
| 1255 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1256 | SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1257 | setFontDescriptor(descriptor.get()); |
| 1258 | |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1259 | int ttcIndex; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1260 | 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.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1263 | SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1264 | SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, |
| 1265 | &trailer); |
| 1266 | if (fontData == NULL) { |
| 1267 | return false; |
| 1268 | } |
vandebo@chromium.org | 5f209e6 | 2013-12-10 17:22:41 +0000 | [diff] [blame] | 1269 | 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.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1275 | |
| 1276 | addResource(descriptor.get()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1277 | insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1278 | |
| 1279 | return addCommonFontDescriptorEntries(defaultWidth); |
| 1280 | } |
| 1281 | |
| 1282 | bool SkPDFType1Font::populate(int16_t glyphID) { |
| 1283 | SkASSERT(!fontInfo()->fVerticalMetrics.get()); |
| 1284 | SkASSERT(fontInfo()->fGlyphWidths.get()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1285 | |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1286 | adjustGlyphRangeForSingleByteEncoding(glyphID); |
| 1287 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1288 | int16_t defaultWidth = 0; |
| 1289 | const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL; |
| 1290 | const SkAdvancedTypefaceMetrics::WidthRange* widthEntry; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1291 | for (widthEntry = fontInfo()->fGlyphWidths.get(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1292 | widthEntry != NULL; |
| 1293 | widthEntry = widthEntry->fNext.get()) { |
| 1294 | switch (widthEntry->fType) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1295 | case SkAdvancedTypefaceMetrics::WidthRange::kDefault: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1296 | defaultWidth = widthEntry->fAdvance[0]; |
| 1297 | break; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1298 | case SkAdvancedTypefaceMetrics::WidthRange::kRun: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1299 | SkASSERT(false); |
| 1300 | break; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1301 | case SkAdvancedTypefaceMetrics::WidthRange::kRange: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1302 | SkASSERT(widthRangeEntry == NULL); |
| 1303 | widthRangeEntry = widthEntry; |
| 1304 | break; |
| 1305 | } |
| 1306 | } |
| 1307 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1308 | if (!addFontDescriptor(defaultWidth)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1309 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1310 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1311 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1312 | insertName("Subtype", "Type1"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1313 | insertName("BaseFont", fontInfo()->fFontName); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1314 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1315 | addWidthInfoFromRange(defaultWidth, widthRangeEntry); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1316 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1317 | SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding")); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1318 | insert("Encoding", encoding.get()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1319 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1320 | SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1321 | encoding->insert("Differences", encDiffs.get()); |
| 1322 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1323 | encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1324 | encDiffs->appendInt(1); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1325 | for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) { |
| 1326 | encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1329 | return true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1332 | void SkPDFType1Font::addWidthInfoFromRange( |
| 1333 | int16_t defaultWidth, |
| 1334 | const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1335 | SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1336 | 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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1350 | for (int i = startIndex; i < endIndex; i++) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1351 | appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get()); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1352 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1353 | } 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 | |
| 1365 | SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info, |
| 1366 | SkTypeface* typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 1367 | uint16_t glyphID) |
| 1368 | : SkPDFFont(info, typeface, NULL) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1369 | populate(glyphID); |
| 1370 | } |
| 1371 | |
| 1372 | SkPDFType3Font::~SkPDFType3Font() {} |
| 1373 | |
| 1374 | bool SkPDFType3Font::populate(int16_t glyphID) { |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1375 | SkPaint paint; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1376 | paint.setTypeface(typeface()); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1377 | paint.setTextSize(1000); |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1378 | SkAutoGlyphCache autoCache(paint, NULL, NULL); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1379 | SkGlyphCache* cache = autoCache.getCache(); |
| 1380 | // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1381 | if (lastGlyphID() == 0) { |
| 1382 | setLastGlyphID(cache->getGlyphCount() - 1); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | adjustGlyphRangeForSingleByteEncoding(glyphID); |
| 1386 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1387 | insertName("Subtype", "Type3"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1388 | // 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.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1393 | SkAutoTUnref<SkPDFDict> charProcs(new SkPDFDict); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1394 | insert("CharProcs", charProcs.get()); |
| 1395 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1396 | SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding")); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1397 | insert("Encoding", encoding.get()); |
| 1398 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1399 | SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1400 | encoding->insert("Differences", encDiffs.get()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1401 | encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1402 | encDiffs->appendInt(1); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1403 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1404 | SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1405 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1406 | SkIRect bbox = SkIRect::MakeEmpty(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1407 | for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1408 | SkString characterName; |
| 1409 | characterName.printf("gid%d", gID); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1410 | encDiffs->appendName(characterName.c_str()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1411 | |
reed@google.com | ce11b26 | 2011-03-21 21:25:35 +0000 | [diff] [blame] | 1412 | const SkGlyph& glyph = cache->getGlyphIDMetrics(gID); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1413 | widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX)); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1414 | SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop, |
| 1415 | glyph.fWidth, glyph.fHeight); |
| 1416 | bbox.join(glyphBBox); |
| 1417 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1418 | SkDynamicMemoryWStream content; |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1419 | setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox, |
| 1420 | &content); |
| 1421 | const SkPath* path = cache->findPath(glyph); |
| 1422 | if (path) { |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 1423 | SkPDFUtils::EmitPath(*path, paint.getStyle(), &content); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1424 | SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(), |
| 1425 | &content); |
| 1426 | } |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1427 | SkAutoTUnref<SkMemoryStream> glyphStream(new SkMemoryStream()); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 1428 | glyphStream->setData(content.copyToData())->unref(); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1429 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1430 | SkAutoTUnref<SkPDFStream> glyphDescription( |
| 1431 | new SkPDFStream(glyphStream.get())); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1432 | addResource(glyphDescription.get()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1433 | charProcs->insert(characterName.c_str(), |
| 1434 | new SkPDFObjRef(glyphDescription.get()))->unref(); |
| 1435 | } |
| 1436 | |
| 1437 | insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 1438 | insertInt("FirstChar", 1); |
| 1439 | insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1440 | insert("Widths", widthArray.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1441 | insertName("CIDToGIDMap", "Identity"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1442 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1443 | populateToUnicodeTable(NULL); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1444 | return true; |
| 1445 | } |