epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
| 11 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 13 | #include "SkFontHost.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 14 | #include "SkGlyphCache.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 16 | #include "SkPDFCatalog.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 17 | #include "SkPDFDevice.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 18 | #include "SkPDFFont.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 19 | #include "SkPDFFontImpl.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 20 | #include "SkPDFStream.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 21 | #include "SkPDFTypes.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 22 | #include "SkPDFUtils.h" |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 23 | #include "SkRefCnt.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 24 | #include "SkScalar.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 25 | #include "SkStream.h" |
| 26 | #include "SkTypeface.h" |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 27 | #include "SkTypes.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 28 | #include "SkUtils.h" |
| 29 | |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 30 | #if defined (SK_SFNTLY_SUBSETTER) |
| 31 | #include SK_SFNTLY_SUBSETTER |
| 32 | #endif |
| 33 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // File-Local Functions |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 40 | bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType, |
| 41 | size_t* size) { |
| 42 | // PFB sections have a two or six bytes header. 0x80 and a one byte |
| 43 | // section type followed by a four byte section length. Type one is |
| 44 | // an ASCII section (includes a length), type two is a binary section |
| 45 | // (includes a length) and type three is an EOF marker with no length. |
| 46 | const uint8_t* buf = *src; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 47 | if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 48 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 49 | } else if (buf[1] == 3) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 50 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 51 | } else if (*len < 6) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 52 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 53 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 54 | |
vandebo@chromium.org | 7332207 | 2011-06-21 21:19:41 +0000 | [diff] [blame] | 55 | *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) | |
| 56 | ((size_t)buf[5] << 24); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 57 | size_t consumed = *size + 6; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 58 | if (consumed > *len) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 59 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 60 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 61 | *src = *src + consumed; |
| 62 | *len = *len - consumed; |
| 63 | return true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 64 | } |
| 65 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 66 | bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen, |
| 67 | size_t* dataLen, size_t* trailerLen) { |
| 68 | const uint8_t* srcPtr = src; |
| 69 | size_t remaining = size; |
| 70 | |
| 71 | return parsePFBSection(&srcPtr, &remaining, 1, headerLen) && |
| 72 | parsePFBSection(&srcPtr, &remaining, 2, dataLen) && |
| 73 | parsePFBSection(&srcPtr, &remaining, 1, trailerLen) && |
| 74 | parsePFBSection(&srcPtr, &remaining, 3, NULL); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 77 | /* The sections of a PFA file are implicitly defined. The body starts |
| 78 | * after the line containing "eexec," and the trailer starts with 512 |
| 79 | * literal 0's followed by "cleartomark" (plus arbitrary white space). |
| 80 | * |
| 81 | * This function assumes that src is NUL terminated, but the NUL |
| 82 | * termination is not included in size. |
| 83 | * |
| 84 | */ |
| 85 | bool parsePFA(const char* src, size_t size, size_t* headerLen, |
| 86 | size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) { |
| 87 | const char* end = src + size; |
| 88 | |
| 89 | const char* dataPos = strstr(src, "eexec"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 90 | if (!dataPos) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 91 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 92 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 93 | dataPos += strlen("eexec"); |
| 94 | while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') && |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 95 | dataPos < end) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 96 | dataPos++; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 97 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 98 | *headerLen = dataPos - src; |
| 99 | |
| 100 | const char* trailerPos = strstr(dataPos, "cleartomark"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 101 | if (!trailerPos) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 102 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 103 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 104 | int zeroCount = 0; |
| 105 | for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) { |
| 106 | if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') { |
| 107 | continue; |
| 108 | } else if (*trailerPos == '0') { |
| 109 | zeroCount++; |
| 110 | } else { |
| 111 | return false; |
| 112 | } |
| 113 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 114 | if (zeroCount != 512) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 115 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 116 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 117 | |
| 118 | *hexDataLen = trailerPos - src - *headerLen; |
| 119 | *trailerLen = size - *headerLen - *hexDataLen; |
| 120 | |
| 121 | // Verify that the data section is hex encoded and count the bytes. |
| 122 | int nibbles = 0; |
| 123 | for (; dataPos < trailerPos; dataPos++) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 124 | if (isspace(*dataPos)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 125 | continue; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 126 | } |
| 127 | if (!isxdigit(*dataPos)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 128 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 129 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 130 | nibbles++; |
| 131 | } |
| 132 | *dataLen = (nibbles + 1) / 2; |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | int8_t hexToBin(uint8_t c) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 138 | if (!isxdigit(c)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 139 | return -1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 140 | } else if (c <= '9') { |
| 141 | return c - '0'; |
| 142 | } else if (c <= 'F') { |
| 143 | return c - 'A' + 10; |
| 144 | } else if (c <= 'f') { |
| 145 | return c - 'a' + 10; |
| 146 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, |
| 151 | size_t* dataLen, size_t* trailerLen) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 152 | // 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] | 153 | // able to use skip(), rewind(), or getMemoryBase(). read()ing through |
| 154 | // the input only once is doable, but very ugly. Furthermore, it'd be nice |
| 155 | // if the data was NUL terminated so that we can use strstr() to search it. |
| 156 | // Make as few copies as possible given these constraints. |
| 157 | SkDynamicMemoryWStream dynamicStream; |
| 158 | SkRefPtr<SkMemoryStream> staticStream; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 159 | SkData* data = NULL; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 160 | const uint8_t* src; |
| 161 | size_t srcLen; |
| 162 | if ((srcLen = srcStream->getLength()) > 0) { |
| 163 | staticStream = new SkMemoryStream(srcLen + 1); |
| 164 | staticStream->unref(); // new and SkRefPtr both took a ref. |
| 165 | src = (const uint8_t*)staticStream->getMemoryBase(); |
| 166 | if (srcStream->getMemoryBase() != NULL) { |
| 167 | memcpy((void *)src, srcStream->getMemoryBase(), srcLen); |
| 168 | } else { |
| 169 | size_t read = 0; |
| 170 | while (read < srcLen) { |
| 171 | size_t got = srcStream->read((void *)staticStream->getAtPos(), |
| 172 | srcLen - read); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 173 | if (got == 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 174 | return NULL; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 175 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 176 | read += got; |
| 177 | staticStream->seek(read); |
| 178 | } |
| 179 | } |
| 180 | ((uint8_t *)src)[srcLen] = 0; |
| 181 | } else { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 182 | static const size_t kBufSize = 4096; |
| 183 | uint8_t buf[kBufSize]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 184 | size_t amount; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 185 | while ((amount = srcStream->read(buf, kBufSize)) > 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 186 | dynamicStream.write(buf, amount); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 187 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 188 | amount = 0; |
| 189 | dynamicStream.write(&amount, 1); // NULL terminator. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 190 | data = dynamicStream.copyToData(); |
| 191 | src = data->bytes(); |
| 192 | srcLen = data->size() - 1; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 193 | } |
| 194 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 195 | // this handles releasing the data we may have gotten from dynamicStream. |
| 196 | // if data is null, it is a no-op |
| 197 | SkAutoDataUnref aud(data); |
| 198 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 199 | if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { |
| 200 | SkMemoryStream* result = |
| 201 | new SkMemoryStream(*headerLen + *dataLen + *trailerLen); |
| 202 | memcpy((char*)result->getAtPos(), src + 6, *headerLen); |
| 203 | result->seek(*headerLen); |
| 204 | memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen); |
| 205 | result->seek(*headerLen + *dataLen); |
| 206 | memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen, |
| 207 | *trailerLen); |
| 208 | result->rewind(); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | // A PFA has to be converted for PDF. |
| 213 | size_t hexDataLen; |
| 214 | if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, |
| 215 | trailerLen)) { |
| 216 | SkMemoryStream* result = |
| 217 | new SkMemoryStream(*headerLen + *dataLen + *trailerLen); |
| 218 | memcpy((char*)result->getAtPos(), src, *headerLen); |
| 219 | result->seek(*headerLen); |
| 220 | |
| 221 | const uint8_t* hexData = src + *headerLen; |
| 222 | const uint8_t* trailer = hexData + hexDataLen; |
| 223 | size_t outputOffset = 0; |
vandebo@chromium.org | 5b07368 | 2011-03-08 18:33:31 +0000 | [diff] [blame] | 224 | uint8_t dataByte = 0; // To hush compiler. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 225 | bool highNibble = true; |
| 226 | for (; hexData < trailer; hexData++) { |
| 227 | char curNibble = hexToBin(*hexData); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 228 | if (curNibble < 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 229 | continue; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 230 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 231 | if (highNibble) { |
| 232 | dataByte = curNibble << 4; |
| 233 | highNibble = false; |
| 234 | } else { |
| 235 | dataByte |= curNibble; |
| 236 | highNibble = true; |
| 237 | ((char *)result->getAtPos())[outputOffset++] = dataByte; |
| 238 | } |
| 239 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 240 | if (!highNibble) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 241 | ((char *)result->getAtPos())[outputOffset++] = dataByte; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 242 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 243 | SkASSERT(outputOffset == *dataLen); |
| 244 | result->seek(*headerLen + outputOffset); |
| 245 | |
| 246 | memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen, |
| 247 | *trailerLen); |
| 248 | result->rewind(); |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | return NULL; |
| 253 | } |
| 254 | |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 255 | // scale from em-units to base-1000, returning as a SkScalar |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 256 | SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 257 | SkScalar scaled = SkIntToScalar(val); |
| 258 | if (emSize == 1000) { |
| 259 | return scaled; |
| 260 | } else { |
| 261 | return SkScalarMulDiv(scaled, 1000, emSize); |
| 262 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 263 | } |
| 264 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 265 | void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 266 | SkWStream* content) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 267 | // Specify width and bounding box for the glyph. |
| 268 | SkPDFScalar::Append(width, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 269 | content->writeText(" 0 "); |
| 270 | content->writeDecAsText(box.fLeft); |
| 271 | content->writeText(" "); |
| 272 | content->writeDecAsText(box.fTop); |
| 273 | content->writeText(" "); |
| 274 | content->writeDecAsText(box.fRight); |
| 275 | content->writeText(" "); |
| 276 | content->writeDecAsText(box.fBottom); |
| 277 | content->writeText(" d1\n"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 278 | } |
| 279 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 280 | SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 281 | SkPDFArray* bbox = new SkPDFArray; |
| 282 | bbox->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 283 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize)); |
| 284 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize)); |
| 285 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize)); |
| 286 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize)); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 287 | return bbox; |
| 288 | } |
| 289 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 290 | SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize, |
| 291 | SkPDFArray* array) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 292 | array->appendScalar(scaleFromFontUnits(width, emSize)); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 293 | return array; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 294 | } |
| 295 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 296 | SkPDFArray* appendVerticalAdvance( |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 297 | const SkAdvancedTypefaceMetrics::VerticalMetric& advance, |
| 298 | uint16_t emSize, SkPDFArray* array) { |
| 299 | appendWidth(advance.fVerticalAdvance, emSize, array); |
| 300 | appendWidth(advance.fOriginXDisp, emSize, array); |
| 301 | appendWidth(advance.fOriginYDisp, emSize, array); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 302 | return array; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | template <typename Data> |
| 306 | SkPDFArray* composeAdvanceData( |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 307 | SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo, |
| 308 | uint16_t emSize, |
| 309 | SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize, |
| 310 | SkPDFArray* array), |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 311 | Data* defaultAdvance) { |
| 312 | SkPDFArray* result = new SkPDFArray(); |
| 313 | for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) { |
| 314 | switch (advanceInfo->fType) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 315 | case SkAdvancedTypefaceMetrics::WidthRange::kDefault: { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 316 | SkASSERT(advanceInfo->fAdvance.count() == 1); |
| 317 | *defaultAdvance = advanceInfo->fAdvance[0]; |
| 318 | break; |
| 319 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 320 | case SkAdvancedTypefaceMetrics::WidthRange::kRange: { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 321 | SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray(); |
| 322 | advanceArray->unref(); // SkRefPtr and new both took a ref. |
| 323 | for (int j = 0; j < advanceInfo->fAdvance.count(); j++) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 324 | appendAdvance(advanceInfo->fAdvance[j], emSize, |
| 325 | advanceArray.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 326 | result->appendInt(advanceInfo->fStartId); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 327 | result->append(advanceArray.get()); |
| 328 | break; |
| 329 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 330 | case SkAdvancedTypefaceMetrics::WidthRange::kRun: { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 331 | SkASSERT(advanceInfo->fAdvance.count() == 1); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 332 | result->appendInt(advanceInfo->fStartId); |
| 333 | result->appendInt(advanceInfo->fEndId); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 334 | appendAdvance(advanceInfo->fAdvance[0], emSize, result); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | } // namespace |
| 343 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 344 | static void append_tounicode_header(SkDynamicMemoryWStream* cmap) { |
| 345 | // 12 dict begin: 12 is an Adobe-suggested value. Shall not change. |
| 346 | // It's there to prevent old version Adobe Readers from malfunctioning. |
| 347 | const char* kHeader = |
| 348 | "/CIDInit /ProcSet findresource begin\n" |
| 349 | "12 dict begin\n" |
| 350 | "begincmap\n"; |
| 351 | cmap->writeText(kHeader); |
| 352 | |
| 353 | // The /CIDSystemInfo must be consistent to the one in |
| 354 | // SkPDFFont::populateCIDFont(). |
| 355 | // We can not pass over the system info object here because the format is |
| 356 | // different. This is not a reference object. |
| 357 | const char* kSysInfo = |
| 358 | "/CIDSystemInfo\n" |
| 359 | "<< /Registry (Adobe)\n" |
| 360 | "/Ordering (UCS)\n" |
| 361 | "/Supplement 0\n" |
| 362 | ">> def\n"; |
| 363 | cmap->writeText(kSysInfo); |
| 364 | |
| 365 | // The CMapName must be consistent to /CIDSystemInfo above. |
| 366 | // /CMapType 2 means ToUnicode. |
| 367 | // We specify codespacerange from 0x0000 to 0xFFFF because we convert our |
| 368 | // code table from unsigned short (16-bits). Codespace range just tells the |
| 369 | // PDF processor the valid range. It does not matter whether a complete |
| 370 | // mapping is provided or not. |
| 371 | const char* kTypeInfo = |
| 372 | "/CMapName /Adobe-Identity-UCS def\n" |
| 373 | "/CMapType 2 def\n" |
| 374 | "1 begincodespacerange\n" |
| 375 | "<0000> <FFFF>\n" |
| 376 | "endcodespacerange\n"; |
| 377 | cmap->writeText(kTypeInfo); |
| 378 | } |
| 379 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 380 | static void append_cmap_footer(SkDynamicMemoryWStream* cmap) { |
| 381 | const char* kFooter = |
| 382 | "endcmap\n" |
| 383 | "CMapName currentdict /CMap defineresource pop\n" |
| 384 | "end\n" |
| 385 | "end"; |
| 386 | cmap->writeText(kFooter); |
| 387 | } |
| 388 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 389 | struct BFChar { |
| 390 | uint16_t fGlyphId; |
| 391 | SkUnichar fUnicode; |
| 392 | }; |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 393 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 394 | struct BFRange { |
| 395 | uint16_t fStart; |
| 396 | uint16_t fEnd; |
| 397 | SkUnichar fUnicode; |
| 398 | }; |
| 399 | |
| 400 | static void append_bfchar_section(const SkTDArray<BFChar>& bfchar, |
| 401 | SkDynamicMemoryWStream* cmap) { |
| 402 | // PDF spec defines that every bf* list can have at most 100 entries. |
| 403 | for (int i = 0; i < bfchar.count(); i += 100) { |
| 404 | int count = bfchar.count() - i; |
| 405 | count = SkMin32(count, 100); |
| 406 | cmap->writeDecAsText(count); |
| 407 | cmap->writeText(" beginbfchar\n"); |
| 408 | for (int j = 0; j < count; ++j) { |
| 409 | cmap->writeText("<"); |
| 410 | cmap->writeHexAsText(bfchar[i + j].fGlyphId, 4); |
| 411 | cmap->writeText("> <"); |
| 412 | cmap->writeHexAsText(bfchar[i + j].fUnicode, 4); |
| 413 | cmap->writeText(">\n"); |
| 414 | } |
| 415 | cmap->writeText("endbfchar\n"); |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 419 | static void append_bfrange_section(const SkTDArray<BFRange>& bfrange, |
| 420 | SkDynamicMemoryWStream* cmap) { |
| 421 | // PDF spec defines that every bf* list can have at most 100 entries. |
| 422 | for (int i = 0; i < bfrange.count(); i += 100) { |
| 423 | int count = bfrange.count() - i; |
| 424 | count = SkMin32(count, 100); |
| 425 | cmap->writeDecAsText(count); |
| 426 | cmap->writeText(" beginbfrange\n"); |
| 427 | for (int j = 0; j < count; ++j) { |
| 428 | cmap->writeText("<"); |
| 429 | cmap->writeHexAsText(bfrange[i + j].fStart, 4); |
| 430 | cmap->writeText("> <"); |
| 431 | cmap->writeHexAsText(bfrange[i + j].fEnd, 4); |
| 432 | cmap->writeText("> <"); |
| 433 | cmap->writeHexAsText(bfrange[i + j].fUnicode, 4); |
| 434 | cmap->writeText(">\n"); |
| 435 | } |
| 436 | cmap->writeText("endbfrange\n"); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe |
| 441 | // Technote 5014. |
| 442 | // The function is not static so we can test it in unit tests. |
| 443 | // |
| 444 | // Current implementation guarantees bfchar and bfrange entries do not overlap. |
| 445 | // |
| 446 | // Current implementation does not attempt aggresive optimizations against |
| 447 | // following case because the specification is not clear. |
| 448 | // |
| 449 | // 4 beginbfchar 1 beginbfchar |
| 450 | // <0003> <0013> <0020> <0014> |
| 451 | // <0005> <0015> to endbfchar |
| 452 | // <0007> <0017> 1 beginbfrange |
| 453 | // <0020> <0014> <0003> <0007> <0013> |
| 454 | // endbfchar endbfrange |
| 455 | // |
| 456 | // Adobe Technote 5014 said: "Code mappings (unlike codespace ranges) may |
| 457 | // overlap, but succeeding maps superceded preceding maps." |
| 458 | // |
| 459 | // In case of searching text in PDF, bfrange will have higher precedence so |
| 460 | // typing char id 0x0014 in search box will get glyph id 0x0004 first. However, |
| 461 | // the spec does not mention how will this kind of conflict being resolved. |
| 462 | // |
| 463 | // For the worst case (having 65536 continuous unicode and we use every other |
| 464 | // one of them), the possible savings by aggressive optimization is 416KB |
| 465 | // pre-compressed and does not provide enough motivation for implementation. |
| 466 | void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, |
| 467 | const SkPDFGlyphSet* subset, |
| 468 | SkDynamicMemoryWStream* cmap) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 469 | if (glyphToUnicode.isEmpty()) { |
| 470 | return; |
| 471 | } |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 472 | |
| 473 | SkTDArray<BFChar> bfcharEntries; |
| 474 | SkTDArray<BFRange> bfrangeEntries; |
| 475 | |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 476 | BFRange currentRangeEntry = {0, 0, 0}; |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 477 | bool rangeEmpty = true; |
| 478 | const int count = glyphToUnicode.count(); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 479 | |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 480 | for (int i = 0; i < count + 1; ++i) { |
| 481 | bool inSubset = i < count && (subset == NULL || subset->has(i)); |
| 482 | if (!rangeEmpty) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 483 | // PDF spec requires bfrange not changing the higher byte, |
| 484 | // e.g. <1035> <10FF> <2222> is ok, but |
| 485 | // <1035> <1100> <2222> is no good |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 486 | bool inRange = |
| 487 | i == currentRangeEntry.fEnd + 1 && |
| 488 | i >> 8 == currentRangeEntry.fStart >> 8 && |
| 489 | i < count && |
| 490 | glyphToUnicode[i] == currentRangeEntry.fUnicode + i - |
| 491 | currentRangeEntry.fStart; |
| 492 | if (!inSubset || !inRange) { |
| 493 | if (currentRangeEntry.fEnd > currentRangeEntry.fStart) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 494 | bfrangeEntries.push(currentRangeEntry); |
| 495 | } else { |
| 496 | BFChar* entry = bfcharEntries.append(); |
| 497 | entry->fGlyphId = currentRangeEntry.fStart; |
| 498 | entry->fUnicode = currentRangeEntry.fUnicode; |
| 499 | } |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 500 | rangeEmpty = true; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 501 | } |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 502 | } |
| 503 | if (inSubset) { |
| 504 | currentRangeEntry.fEnd = i; |
| 505 | if (rangeEmpty) { |
| 506 | currentRangeEntry.fStart = i; |
| 507 | currentRangeEntry.fUnicode = glyphToUnicode[i]; |
| 508 | rangeEmpty = false; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // The spec requires all bfchar entries for a font must come before bfrange |
| 514 | // entries. |
| 515 | append_bfchar_section(bfcharEntries, cmap); |
| 516 | append_bfrange_section(bfrangeEntries, cmap); |
| 517 | } |
| 518 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 519 | static SkPDFStream* generate_tounicode_cmap( |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 520 | const SkTDArray<SkUnichar>& glyphToUnicode, |
| 521 | const SkPDFGlyphSet* subset) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 522 | SkDynamicMemoryWStream cmap; |
| 523 | append_tounicode_header(&cmap); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 524 | append_cmap_sections(glyphToUnicode, subset, &cmap); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 525 | append_cmap_footer(&cmap); |
| 526 | SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream(); |
| 527 | cmapStream->unref(); // SkRefPtr and new took a reference. |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame^] | 528 | cmapStream->setData(cmap.copyToData())->unref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 529 | return new SkPDFStream(cmapStream.get()); |
| 530 | } |
| 531 | |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 532 | static void sk_delete_array(const void* ptr, size_t, void*) { |
| 533 | // Use C-style cast to cast away const and cast type simultaneously. |
| 534 | delete[] (unsigned char*)ptr; |
| 535 | } |
| 536 | |
| 537 | static int get_subset_font_stream(const char* fontName, |
| 538 | const SkTypeface* typeface, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 539 | const SkTDArray<uint32_t>& subset, |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 540 | SkPDFStream** fontStream) { |
| 541 | SkRefPtr<SkStream> fontData = |
| 542 | SkFontHost::OpenStream(SkTypeface::UniqueID(typeface)); |
| 543 | fontData->unref(); // SkRefPtr and OpenStream both took a ref. |
| 544 | |
| 545 | int fontSize = fontData->getLength(); |
| 546 | |
| 547 | #if defined (SK_SFNTLY_SUBSETTER) |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 548 | // Read font into buffer. |
| 549 | SkPDFStream* subsetFontStream = NULL; |
| 550 | SkTDArray<unsigned char> originalFont; |
| 551 | originalFont.setCount(fontSize); |
| 552 | if (fontData->read(originalFont.begin(), fontSize) == (size_t)fontSize) { |
| 553 | unsigned char* subsetFont = NULL; |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 554 | // sfntly requires unsigned int* to be passed in, as far as we know, |
| 555 | // unsigned int is equivalent to uint32_t on all platforms. |
| 556 | SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t), |
| 557 | unsigned_int_not_32_bits); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 558 | int subsetFontSize = SfntlyWrapper::SubsetFont(fontName, |
| 559 | originalFont.begin(), |
| 560 | fontSize, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 561 | subset.begin(), |
| 562 | subset.count(), |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 563 | &subsetFont); |
| 564 | if (subsetFontSize > 0 && subsetFont != NULL) { |
vandebo@chromium.org | 93225ff | 2011-07-27 18:38:11 +0000 | [diff] [blame] | 565 | SkAutoDataUnref data(SkData::NewWithProc(subsetFont, |
| 566 | subsetFontSize, |
| 567 | sk_delete_array, |
| 568 | NULL)); |
| 569 | subsetFontStream = new SkPDFStream(data.get()); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 570 | fontSize = subsetFontSize; |
| 571 | } |
| 572 | } |
| 573 | if (subsetFontStream) { |
| 574 | *fontStream = subsetFontStream; |
| 575 | return fontSize; |
| 576 | } |
| 577 | #endif |
| 578 | |
| 579 | // Fail over: just embed the whole font. |
| 580 | *fontStream = new SkPDFStream(fontData.get()); |
| 581 | return fontSize; |
| 582 | } |
| 583 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 584 | /////////////////////////////////////////////////////////////////////////////// |
| 585 | // class SkPDFGlyphSet |
| 586 | /////////////////////////////////////////////////////////////////////////////// |
| 587 | |
| 588 | SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) { |
| 589 | } |
| 590 | |
| 591 | void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) { |
| 592 | for (int i = 0; i < numGlyphs; ++i) { |
| 593 | fBitSet.setBit(glyphIDs[i], true); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | bool SkPDFGlyphSet::has(uint16_t glyphID) const { |
| 598 | return fBitSet.isBitSet(glyphID); |
| 599 | } |
| 600 | |
| 601 | void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) { |
| 602 | fBitSet.orBits(usage.fBitSet); |
| 603 | } |
| 604 | |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 605 | void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const { |
| 606 | fBitSet.exportTo(glyphIDs); |
| 607 | } |
| 608 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 609 | /////////////////////////////////////////////////////////////////////////////// |
| 610 | // class SkPDFGlyphSetMap |
| 611 | /////////////////////////////////////////////////////////////////////////////// |
| 612 | SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font, |
| 613 | SkPDFGlyphSet* glyphSet) |
| 614 | : fFont(font), |
| 615 | fGlyphSet(glyphSet) { |
| 616 | } |
| 617 | |
| 618 | SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) { |
| 619 | reset(map); |
| 620 | } |
| 621 | |
| 622 | SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const { |
| 623 | if (fIndex >= fMap->count()) { |
| 624 | return NULL; |
| 625 | } |
| 626 | return &((*fMap)[fIndex++]); |
| 627 | } |
| 628 | |
| 629 | void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) { |
| 630 | fMap = &(map.fMap); |
| 631 | fIndex = 0; |
| 632 | } |
| 633 | |
| 634 | SkPDFGlyphSetMap::SkPDFGlyphSetMap() { |
| 635 | } |
| 636 | |
| 637 | SkPDFGlyphSetMap::~SkPDFGlyphSetMap() { |
| 638 | reset(); |
| 639 | } |
| 640 | |
| 641 | void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) { |
| 642 | for (int i = 0; i < usage.fMap.count(); ++i) { |
| 643 | SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont); |
| 644 | myUsage->merge(*(usage.fMap[i].fGlyphSet)); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | void SkPDFGlyphSetMap::reset() { |
| 649 | for (int i = 0; i < fMap.count(); ++i) { |
| 650 | delete fMap[i].fGlyphSet; // Should not be NULL. |
| 651 | } |
| 652 | fMap.reset(); |
| 653 | } |
| 654 | |
| 655 | void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, |
| 656 | int numGlyphs) { |
| 657 | SkPDFGlyphSet* subset = getGlyphSetForFont(font); |
| 658 | if (subset) { |
| 659 | subset->set(glyphIDs, numGlyphs); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) { |
| 664 | int index = fMap.count(); |
| 665 | for (int i = 0; i < index; ++i) { |
| 666 | if (fMap[i].fFont == font) { |
| 667 | return fMap[i].fGlyphSet; |
| 668 | } |
| 669 | } |
| 670 | fMap.append(); |
| 671 | index = fMap.count() - 1; |
| 672 | fMap[index].fFont = font; |
| 673 | fMap[index].fGlyphSet = new SkPDFGlyphSet(); |
| 674 | return fMap[index].fGlyphSet; |
| 675 | } |
| 676 | |
| 677 | /////////////////////////////////////////////////////////////////////////////// |
| 678 | // class SkPDFFont |
| 679 | /////////////////////////////////////////////////////////////////////////////// |
| 680 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 681 | /* Font subset design: It would be nice to be able to subset fonts |
| 682 | * (particularly type 3 fonts), but it's a lot of work and not a priority. |
| 683 | * |
| 684 | * Resources are canonicalized and uniqueified by pointer so there has to be |
| 685 | * some additional state indicating which subset of the font is used. It |
| 686 | * must be maintained at the page granularity and then combined at the document |
| 687 | * granularity. a) change SkPDFFont to fill in its state on demand, kind of |
| 688 | * like SkPDFGraphicState. b) maintain a per font glyph usage class in each |
| 689 | * page/pdf device. c) in the document, retrieve the per font glyph usage |
| 690 | * from each page and combine it and ask for a resource with that subset. |
| 691 | */ |
| 692 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 693 | SkPDFFont::~SkPDFFont() { |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 694 | SkAutoMutexAcquire lock(CanonicalFontsMutex()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 695 | int index; |
vandebo@chromium.org | 918352f | 2011-10-30 19:13:26 +0000 | [diff] [blame] | 696 | if (Find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index) && |
| 697 | CanonicalFonts()[index].fFont == this) { |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 698 | CanonicalFonts().removeShuffle(index); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 699 | } |
| 700 | fResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 704 | GetResourcesHelper(&fResources, resourceList); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 705 | } |
| 706 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 707 | SkTypeface* SkPDFFont::typeface() { |
| 708 | return fTypeface.get(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 709 | } |
| 710 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 711 | SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 712 | return fFontType; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 713 | } |
| 714 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 715 | bool SkPDFFont::hasGlyph(uint16_t id) { |
| 716 | return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; |
| 717 | } |
| 718 | |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 719 | size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, |
| 720 | size_t numGlyphs) { |
| 721 | // A font with multibyte glyphs will support all glyph IDs in a single font. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 722 | if (this->multiByteGlyphs()) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 723 | return numGlyphs; |
| 724 | } |
| 725 | |
| 726 | for (size_t i = 0; i < numGlyphs; i++) { |
| 727 | if (glyphIDs[i] == 0) { |
| 728 | continue; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 729 | } |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 730 | if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) { |
| 731 | return i; |
| 732 | } |
| 733 | glyphIDs[i] -= (fFirstGlyphID - 1); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 734 | } |
| 735 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 736 | return numGlyphs; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 740 | SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) { |
| 741 | SkAutoMutexAcquire lock(CanonicalFontsMutex()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 742 | const uint32_t fontID = SkTypeface::UniqueID(typeface); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 743 | int relatedFontIndex; |
| 744 | if (Find(fontID, glyphID, &relatedFontIndex)) { |
| 745 | CanonicalFonts()[relatedFontIndex].fFont->ref(); |
| 746 | return CanonicalFonts()[relatedFontIndex].fFont; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 747 | } |
| 748 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 749 | SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics; |
| 750 | SkPDFDict* relatedFontDescriptor = NULL; |
| 751 | if (relatedFontIndex >= 0) { |
| 752 | SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 753 | fontMetrics = relatedFont->fontInfo(); |
| 754 | relatedFontDescriptor = relatedFont->getFontDescriptor(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 755 | } else { |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 756 | SkAdvancedTypefaceMetrics::PerGlyphInfo info; |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 757 | info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo; |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 758 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 759 | info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 760 | #if !defined (SK_SFNTLY_SUBSETTER) |
| 761 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 762 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
| 763 | #endif |
| 764 | fontMetrics = |
| 765 | SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 766 | SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref. |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame^] | 767 | #if defined (SK_SFNTLY_SUBSETTER) |
vandebo@chromium.org | b3b4655 | 2011-10-17 23:22:49 +0000 | [diff] [blame] | 768 | if (fontMetrics && |
| 769 | fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 770 | // Font does not support subsetting, get new info with advance. |
| 771 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 772 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
| 773 | fontMetrics = |
| 774 | SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0); |
| 775 | SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref |
| 776 | } |
| 777 | #endif |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 778 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 779 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 780 | SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID, |
| 781 | relatedFontDescriptor); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 782 | FontRec newEntry(font, fontID, font->fFirstGlyphID); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 783 | CanonicalFonts().push(newEntry); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 784 | return font; // Return the reference new SkPDFFont() created. |
| 785 | } |
| 786 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 787 | SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet* usage) { |
| 788 | return NULL; // Default: no support. |
| 789 | } |
| 790 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 791 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 792 | SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 793 | // This initialization is only thread safe with gcc. |
| 794 | static SkTDArray<FontRec> gCanonicalFonts; |
| 795 | return gCanonicalFonts; |
| 796 | } |
| 797 | |
| 798 | // static |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 799 | SkBaseMutex& SkPDFFont::CanonicalFontsMutex() { |
| 800 | // This initialization is only thread safe with gcc, or when |
| 801 | // POD-style mutex initialization is used. |
| 802 | SK_DECLARE_STATIC_MUTEX(gCanonicalFontsMutex); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 803 | return gCanonicalFontsMutex; |
| 804 | } |
| 805 | |
| 806 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 807 | bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 808 | // TODO(vandebo): Optimize this, do only one search? |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 809 | FontRec search(NULL, fontID, glyphID); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 810 | *index = CanonicalFonts().find(search); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 811 | if (*index >= 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 812 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 813 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 814 | search.fGlyphID = 0; |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 815 | *index = CanonicalFonts().find(search); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 816 | return false; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 817 | } |
| 818 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 819 | SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface, |
| 820 | uint16_t glyphID, bool descendantFont) |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 821 | : SkPDFDict("Font"), |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 822 | fTypeface(typeface), |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 823 | fFirstGlyphID(1), |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 824 | fLastGlyphID(info ? info->fLastGlyphID : 0), |
| 825 | fFontInfo(info) { |
| 826 | if (info == NULL) { |
| 827 | fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 828 | } else if (info->fMultiMaster) { |
| 829 | fFontType = SkAdvancedTypefaceMetrics::kOther_Font; |
| 830 | } else { |
| 831 | fFontType = info->fType; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // static |
| 836 | SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info, |
| 837 | SkTypeface* typeface, uint16_t glyphID, |
| 838 | SkPDFDict* relatedFontDescriptor) { |
| 839 | SkAdvancedTypefaceMetrics::FontType type = |
| 840 | info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 841 | |
| 842 | if (info && info->fMultiMaster) { |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 843 | NOT_IMPLEMENTED(true, true); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 844 | return new SkPDFType3Font(info, |
| 845 | typeface, |
| 846 | glyphID, |
| 847 | relatedFontDescriptor); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 848 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 849 | if (type == SkAdvancedTypefaceMetrics::kType1CID_Font || |
| 850 | type == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
| 851 | SkASSERT(relatedFontDescriptor == NULL); |
| 852 | return new SkPDFType0Font(info, typeface); |
| 853 | } |
| 854 | if (type == SkAdvancedTypefaceMetrics::kType1_Font) { |
| 855 | return new SkPDFType1Font(info, |
| 856 | typeface, |
| 857 | glyphID, |
| 858 | relatedFontDescriptor); |
vandebo@chromium.org | 6504cfd | 2011-07-23 20:22:53 +0000 | [diff] [blame] | 859 | } |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 860 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 861 | SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || |
| 862 | type == SkAdvancedTypefaceMetrics::kOther_Font || |
| 863 | type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 864 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 865 | return new SkPDFType3Font(info, typeface, glyphID, relatedFontDescriptor); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 866 | } |
| 867 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 868 | SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { |
| 869 | return fFontInfo.get(); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 870 | } |
| 871 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 872 | void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { |
| 873 | if (info == NULL || info == fFontInfo.get()) { |
| 874 | return; |
| 875 | } |
| 876 | fFontInfo = info; |
| 877 | } |
| 878 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 879 | uint16_t SkPDFFont::firstGlyphID() const { |
| 880 | return fFirstGlyphID; |
| 881 | } |
| 882 | |
| 883 | uint16_t SkPDFFont::lastGlyphID() const { |
| 884 | return fLastGlyphID; |
| 885 | } |
| 886 | |
| 887 | void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
| 888 | fLastGlyphID = glyphID; |
| 889 | } |
| 890 | |
| 891 | void SkPDFFont::addResource(SkPDFObject* object) { |
| 892 | SkASSERT(object != NULL); |
| 893 | fResources.push(object); |
| 894 | } |
| 895 | |
| 896 | SkPDFDict* SkPDFFont::getFontDescriptor() { |
| 897 | return fDescriptor.get(); |
| 898 | } |
| 899 | |
| 900 | void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { |
| 901 | fDescriptor = descriptor; |
| 902 | } |
| 903 | |
| 904 | bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
| 905 | if (fDescriptor.get() == NULL) { |
| 906 | return false; |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 907 | } |
| 908 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 909 | const uint16_t emSize = fFontInfo->fEmSize; |
| 910 | |
| 911 | fDescriptor->insertName("FontName", fFontInfo->fFontName); |
| 912 | fDescriptor->insertInt("Flags", fFontInfo->fStyle); |
| 913 | fDescriptor->insertScalar("Ascent", |
| 914 | scaleFromFontUnits(fFontInfo->fAscent, emSize)); |
| 915 | fDescriptor->insertScalar("Descent", |
| 916 | scaleFromFontUnits(fFontInfo->fDescent, emSize)); |
| 917 | fDescriptor->insertScalar("StemV", |
| 918 | scaleFromFontUnits(fFontInfo->fStemV, emSize)); |
| 919 | fDescriptor->insertScalar("CapHeight", |
| 920 | scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); |
| 921 | fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); |
| 922 | fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox, |
| 923 | fFontInfo->fEmSize))->unref(); |
| 924 | |
| 925 | if (defaultWidth > 0) { |
| 926 | fDescriptor->insertScalar("MissingWidth", |
| 927 | scaleFromFontUnits(defaultWidth, emSize)); |
| 928 | } |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) { |
| 933 | // Single byte glyph encoding supports a max of 255 glyphs. |
| 934 | fFirstGlyphID = glyphID - (glyphID - 1) % 255; |
| 935 | if (fLastGlyphID > fFirstGlyphID + 255 - 1) { |
| 936 | fLastGlyphID = fFirstGlyphID + 255 - 1; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 941 | if (fFontID != b.fFontID) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 942 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 943 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 944 | if (fFont != NULL && b.fFont != NULL) { |
| 945 | return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID && |
| 946 | fFont->fLastGlyphID == b.fFont->fLastGlyphID; |
| 947 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 948 | if (fGlyphID == 0 || b.fGlyphID == 0) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 949 | return true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 950 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 951 | |
| 952 | if (fFont != NULL) { |
| 953 | return fFont->fFirstGlyphID <= b.fGlyphID && |
| 954 | b.fGlyphID <= fFont->fLastGlyphID; |
| 955 | } else if (b.fFont != NULL) { |
| 956 | return b.fFont->fFirstGlyphID <= fGlyphID && |
| 957 | fGlyphID <= b.fFont->fLastGlyphID; |
| 958 | } |
| 959 | return fGlyphID == b.fGlyphID; |
| 960 | } |
| 961 | |
| 962 | SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID) |
| 963 | : fFont(font), |
| 964 | fFontID(fontID), |
| 965 | fGlyphID(glyphID) { |
| 966 | } |
| 967 | |
| 968 | void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { |
| 969 | if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) { |
| 970 | return; |
| 971 | } |
| 972 | SkRefPtr<SkPDFStream> pdfCmap = |
| 973 | generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset); |
| 974 | addResource(pdfCmap.get()); // Pass reference from new. |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 975 | insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 976 | } |
| 977 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 978 | /////////////////////////////////////////////////////////////////////////////// |
| 979 | // class SkPDFType0Font |
| 980 | /////////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 981 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 982 | SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, |
| 983 | SkTypeface* typeface) |
| 984 | : SkPDFFont(info, typeface, 0, false) { |
| 985 | SkDEBUGCODE(fPopulated = false); |
| 986 | } |
| 987 | |
| 988 | SkPDFType0Font::~SkPDFType0Font() {} |
| 989 | |
| 990 | SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
| 991 | SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface()); |
| 992 | newSubset->populate(subset); |
| 993 | return newSubset; |
| 994 | } |
| 995 | |
| 996 | #ifdef SK_DEBUG |
| 997 | void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
| 998 | bool indirect) { |
| 999 | SkASSERT(fPopulated); |
| 1000 | return INHERITED::emitObject(stream, catalog, indirect); |
| 1001 | } |
| 1002 | #endif |
| 1003 | |
| 1004 | bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
| 1005 | insertName("Subtype", "Type0"); |
| 1006 | insertName("BaseFont", fontInfo()->fFontName); |
| 1007 | insertName("Encoding", "Identity-H"); |
| 1008 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1009 | SkPDFCIDFont* newCIDFont; |
| 1010 | newCIDFont = new SkPDFCIDFont(fontInfo(), typeface(), subset); |
| 1011 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1012 | // Pass ref new created to fResources. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1013 | addResource(newCIDFont); |
| 1014 | SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray(); |
| 1015 | descendantFonts->unref(); // SkRefPtr and new took a reference. |
| 1016 | descendantFonts->append(new SkPDFObjRef(newCIDFont))->unref(); |
| 1017 | insert("DescendantFonts", descendantFonts.get()); |
| 1018 | |
| 1019 | populateToUnicodeTable(subset); |
| 1020 | |
| 1021 | SkDEBUGCODE(fPopulated = true); |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
| 1025 | /////////////////////////////////////////////////////////////////////////////// |
| 1026 | // class SkPDFCIDFont |
| 1027 | /////////////////////////////////////////////////////////////////////////////// |
| 1028 | |
| 1029 | SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info, |
| 1030 | SkTypeface* typeface, const SkPDFGlyphSet* subset) |
| 1031 | : SkPDFFont(info, typeface, 0, true) { |
| 1032 | populate(subset); |
| 1033 | } |
| 1034 | |
| 1035 | SkPDFCIDFont::~SkPDFCIDFont() {} |
| 1036 | |
| 1037 | bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1038 | const SkTDArray<uint32_t>* subset) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1039 | SkRefPtr<SkPDFDict> descriptor = new SkPDFDict("FontDescriptor"); |
| 1040 | descriptor->unref(); // SkRefPtr and new both took a ref. |
| 1041 | setFontDescriptor(descriptor.get()); |
| 1042 | |
| 1043 | switch (getType()) { |
| 1044 | case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1045 | SkASSERT(subset); |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 1046 | // Font subsetting |
| 1047 | SkPDFStream* rawStream = NULL; |
| 1048 | int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(), |
| 1049 | typeface(), |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1050 | *subset, |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 1051 | &rawStream); |
| 1052 | SkASSERT(fontSize); |
| 1053 | SkASSERT(rawStream); |
| 1054 | SkRefPtr<SkPDFStream> fontStream = rawStream; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1055 | // SkRefPtr and new both ref()'d fontStream, pass one. |
| 1056 | addResource(fontStream.get()); |
| 1057 | |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 1058 | fontStream->insertInt("Length1", fontSize); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1059 | descriptor->insert("FontFile2", |
| 1060 | new SkPDFObjRef(fontStream.get()))->unref(); |
| 1061 | break; |
| 1062 | } |
| 1063 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
| 1064 | case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
| 1065 | SkRefPtr<SkStream> fontData = |
| 1066 | SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())); |
| 1067 | fontData->unref(); // SkRefPtr and OpenStream both took a ref. |
| 1068 | SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get()); |
| 1069 | // SkRefPtr and new both ref()'d fontStream, pass one. |
| 1070 | addResource(fontStream.get()); |
| 1071 | |
| 1072 | if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { |
| 1073 | fontStream->insertName("Subtype", "Type1C"); |
| 1074 | } else { |
| 1075 | fontStream->insertName("Subtype", "CIDFontType0c"); |
| 1076 | } |
| 1077 | descriptor->insert("FontFile3", |
| 1078 | new SkPDFObjRef(fontStream.get()))->unref(); |
| 1079 | break; |
| 1080 | } |
| 1081 | default: |
| 1082 | SkASSERT(false); |
| 1083 | } |
| 1084 | |
| 1085 | addResource(descriptor.get()); |
| 1086 | descriptor->ref(); |
| 1087 | |
| 1088 | insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1089 | return addCommonFontDescriptorEntries(defaultWidth); |
| 1090 | } |
| 1091 | |
| 1092 | bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1093 | // Generate new font metrics with advance info for true type fonts. |
| 1094 | if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
| 1095 | // Generate glyph id array. |
| 1096 | SkTDArray<uint32_t> glyphIDs; |
| 1097 | glyphIDs.push(0); // Always include glyph 0. |
| 1098 | if (subset) { |
| 1099 | subset->exportTo(&glyphIDs); |
| 1100 | } |
| 1101 | |
| 1102 | SkRefPtr<SkAdvancedTypefaceMetrics> fontMetrics; |
| 1103 | SkAdvancedTypefaceMetrics::PerGlyphInfo info; |
| 1104 | info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo; |
| 1105 | info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>( |
| 1106 | info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo); |
| 1107 | uint32_t* glyphs = (glyphIDs.count() == 1) ? NULL : glyphIDs.begin(); |
| 1108 | uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
| 1109 | fontMetrics = |
| 1110 | SkFontHost::GetAdvancedTypefaceMetrics( |
| 1111 | SkTypeface::UniqueID(typeface()), |
| 1112 | info, |
| 1113 | glyphs, |
| 1114 | glyphsCount); |
| 1115 | SkSafeUnref(fontMetrics.get()); // SkRefPtr and Get both took a ref |
| 1116 | setFontInfo(fontMetrics.get()); |
| 1117 | addFontDescriptor(0, &glyphIDs); |
| 1118 | } else { |
| 1119 | // Other CID fonts |
| 1120 | addFontDescriptor(0, NULL); |
| 1121 | } |
| 1122 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1123 | insertName("BaseFont", fontInfo()->fFontName); |
| 1124 | |
| 1125 | if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1126 | insertName("Subtype", "CIDFontType0"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1127 | } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1128 | insertName("Subtype", "CIDFontType2"); |
| 1129 | insertName("CIDToGIDMap", "Identity"); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1130 | } else { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1131 | SkASSERT(false); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1132 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1133 | |
| 1134 | SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict; |
| 1135 | sysInfo->unref(); // SkRefPtr and new both took a reference. |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1136 | sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref(); |
| 1137 | sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref(); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1138 | sysInfo->insertInt("Supplement", 0); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1139 | insert("CIDSystemInfo", sysInfo.get()); |
| 1140 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1141 | if (fontInfo()->fGlyphWidths.get()) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1142 | int16_t defaultWidth = 0; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1143 | SkRefPtr<SkPDFArray> widths = |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1144 | composeAdvanceData(fontInfo()->fGlyphWidths.get(), |
| 1145 | fontInfo()->fEmSize, &appendWidth, |
| 1146 | &defaultWidth); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1147 | widths->unref(); // SkRefPtr and compose both took a reference. |
| 1148 | if (widths->size()) |
| 1149 | insert("W", widths.get()); |
| 1150 | if (defaultWidth != 0) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1151 | insertScalar("DW", scaleFromFontUnits(defaultWidth, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1152 | fontInfo()->fEmSize)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1155 | if (fontInfo()->fVerticalMetrics.get()) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1156 | struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1157 | defaultAdvance.fVerticalAdvance = 0; |
| 1158 | defaultAdvance.fOriginXDisp = 0; |
| 1159 | defaultAdvance.fOriginYDisp = 0; |
| 1160 | SkRefPtr<SkPDFArray> advances = |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1161 | composeAdvanceData(fontInfo()->fVerticalMetrics.get(), |
| 1162 | fontInfo()->fEmSize, &appendVerticalAdvance, |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1163 | &defaultAdvance); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1164 | advances->unref(); // SkRefPtr and compose both took a ref. |
| 1165 | if (advances->size()) |
| 1166 | insert("W2", advances.get()); |
| 1167 | if (defaultAdvance.fVerticalAdvance || |
| 1168 | defaultAdvance.fOriginXDisp || |
| 1169 | defaultAdvance.fOriginYDisp) { |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1170 | insert("DW2", appendVerticalAdvance(defaultAdvance, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1171 | fontInfo()->fEmSize, |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 1172 | new SkPDFArray))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1175 | |
| 1176 | return true; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1179 | /////////////////////////////////////////////////////////////////////////////// |
| 1180 | // class SkPDFType1Font |
| 1181 | /////////////////////////////////////////////////////////////////////////////// |
| 1182 | |
| 1183 | SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info, |
| 1184 | SkTypeface* typeface, |
| 1185 | uint16_t glyphID, |
| 1186 | SkPDFDict* relatedFontDescriptor) |
| 1187 | : SkPDFFont(info, typeface, glyphID, false) { |
| 1188 | populate(glyphID); |
| 1189 | } |
| 1190 | |
| 1191 | SkPDFType1Font::~SkPDFType1Font() {} |
| 1192 | |
| 1193 | bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
| 1194 | SkRefPtr<SkPDFDict> descriptor = getFontDescriptor(); |
| 1195 | if (descriptor.get() != NULL) { |
| 1196 | addResource(descriptor.get()); |
| 1197 | descriptor->ref(); |
| 1198 | insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1199 | return true; |
| 1200 | } |
| 1201 | |
| 1202 | descriptor = new SkPDFDict("FontDescriptor"); |
| 1203 | descriptor->unref(); // SkRefPtr and new both took a ref. |
| 1204 | setFontDescriptor(descriptor.get()); |
| 1205 | |
| 1206 | size_t header SK_INIT_TO_AVOID_WARNING; |
| 1207 | size_t data SK_INIT_TO_AVOID_WARNING; |
| 1208 | size_t trailer SK_INIT_TO_AVOID_WARNING; |
| 1209 | SkRefPtr<SkStream> rawFontData = |
| 1210 | SkFontHost::OpenStream(SkTypeface::UniqueID(typeface())); |
| 1211 | rawFontData->unref(); // SkRefPtr and OpenStream both took a ref. |
| 1212 | SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, |
| 1213 | &trailer); |
| 1214 | if (fontData == NULL) { |
| 1215 | return false; |
| 1216 | } |
| 1217 | SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData); |
| 1218 | // SkRefPtr and new both ref()'d fontStream, pass one. |
| 1219 | addResource(fontStream.get()); |
| 1220 | fontStream->insertInt("Length1", header); |
| 1221 | fontStream->insertInt("Length2", data); |
| 1222 | fontStream->insertInt("Length3", trailer); |
| 1223 | descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref(); |
| 1224 | |
| 1225 | addResource(descriptor.get()); |
| 1226 | descriptor->ref(); |
| 1227 | insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1228 | |
| 1229 | return addCommonFontDescriptorEntries(defaultWidth); |
| 1230 | } |
| 1231 | |
| 1232 | bool SkPDFType1Font::populate(int16_t glyphID) { |
| 1233 | SkASSERT(!fontInfo()->fVerticalMetrics.get()); |
| 1234 | SkASSERT(fontInfo()->fGlyphWidths.get()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1235 | |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1236 | adjustGlyphRangeForSingleByteEncoding(glyphID); |
| 1237 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1238 | int16_t defaultWidth = 0; |
| 1239 | const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL; |
| 1240 | const SkAdvancedTypefaceMetrics::WidthRange* widthEntry; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1241 | for (widthEntry = fontInfo()->fGlyphWidths.get(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1242 | widthEntry != NULL; |
| 1243 | widthEntry = widthEntry->fNext.get()) { |
| 1244 | switch (widthEntry->fType) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1245 | case SkAdvancedTypefaceMetrics::WidthRange::kDefault: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1246 | defaultWidth = widthEntry->fAdvance[0]; |
| 1247 | break; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1248 | case SkAdvancedTypefaceMetrics::WidthRange::kRun: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1249 | SkASSERT(false); |
| 1250 | break; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 1251 | case SkAdvancedTypefaceMetrics::WidthRange::kRange: |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1252 | SkASSERT(widthRangeEntry == NULL); |
| 1253 | widthRangeEntry = widthEntry; |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1258 | if (!addFontDescriptor(defaultWidth)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1259 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1260 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1261 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1262 | insertName("Subtype", "Type1"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1263 | insertName("BaseFont", fontInfo()->fFontName); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1264 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1265 | addWidthInfoFromRange(defaultWidth, widthRangeEntry); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1266 | |
| 1267 | SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1268 | encoding->unref(); // SkRefPtr and new both took a reference. |
| 1269 | insert("Encoding", encoding.get()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1270 | |
| 1271 | SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray; |
| 1272 | encDiffs->unref(); // SkRefPtr and new both took a reference. |
| 1273 | encoding->insert("Differences", encDiffs.get()); |
| 1274 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1275 | encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1276 | encDiffs->appendInt(1); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1277 | for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) { |
| 1278 | encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1281 | return true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1284 | void SkPDFType1Font::addWidthInfoFromRange( |
| 1285 | int16_t defaultWidth, |
| 1286 | const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) { |
| 1287 | SkRefPtr<SkPDFArray> widthArray = new SkPDFArray(); |
| 1288 | widthArray->unref(); // SkRefPtr and new both took a ref. |
| 1289 | int firstChar = 0; |
| 1290 | if (widthRangeEntry) { |
| 1291 | const uint16_t emSize = fontInfo()->fEmSize; |
| 1292 | int startIndex = firstGlyphID() - widthRangeEntry->fStartId; |
| 1293 | int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1; |
| 1294 | if (startIndex < 0) |
| 1295 | startIndex = 0; |
| 1296 | if (endIndex > widthRangeEntry->fAdvance.count()) |
| 1297 | endIndex = widthRangeEntry->fAdvance.count(); |
| 1298 | if (widthRangeEntry->fStartId == 0) { |
| 1299 | appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get()); |
| 1300 | } else { |
| 1301 | firstChar = startIndex + widthRangeEntry->fStartId; |
| 1302 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1303 | for (int i = startIndex; i < endIndex; i++) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1304 | appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get()); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1305 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1306 | } else { |
| 1307 | appendWidth(defaultWidth, 1000, widthArray.get()); |
| 1308 | } |
| 1309 | insertInt("FirstChar", firstChar); |
| 1310 | insertInt("LastChar", firstChar + widthArray->size() - 1); |
| 1311 | insert("Widths", widthArray.get()); |
| 1312 | } |
| 1313 | |
| 1314 | /////////////////////////////////////////////////////////////////////////////// |
| 1315 | // class SkPDFType3Font |
| 1316 | /////////////////////////////////////////////////////////////////////////////// |
| 1317 | |
| 1318 | SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info, |
| 1319 | SkTypeface* typeface, |
| 1320 | uint16_t glyphID, |
| 1321 | SkPDFDict* relatedFontDescriptor) |
| 1322 | : SkPDFFont(info, typeface, glyphID, false) { |
| 1323 | populate(glyphID); |
| 1324 | } |
| 1325 | |
| 1326 | SkPDFType3Font::~SkPDFType3Font() {} |
| 1327 | |
| 1328 | bool SkPDFType3Font::populate(int16_t glyphID) { |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1329 | SkPaint paint; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1330 | paint.setTypeface(typeface()); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1331 | paint.setTextSize(1000); |
| 1332 | SkAutoGlyphCache autoCache(paint, NULL); |
| 1333 | SkGlyphCache* cache = autoCache.getCache(); |
| 1334 | // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1335 | if (lastGlyphID() == 0) { |
| 1336 | setLastGlyphID(cache->getGlyphCount() - 1); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | adjustGlyphRangeForSingleByteEncoding(glyphID); |
| 1340 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1341 | insertName("Subtype", "Type3"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1342 | // Flip about the x-axis and scale by 1/1000. |
| 1343 | SkMatrix fontMatrix; |
| 1344 | fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000)); |
| 1345 | insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref(); |
| 1346 | |
| 1347 | SkRefPtr<SkPDFDict> charProcs = new SkPDFDict; |
| 1348 | charProcs->unref(); // SkRefPtr and new both took a reference. |
| 1349 | insert("CharProcs", charProcs.get()); |
| 1350 | |
| 1351 | SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding"); |
| 1352 | encoding->unref(); // SkRefPtr and new both took a reference. |
| 1353 | insert("Encoding", encoding.get()); |
| 1354 | |
| 1355 | SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray; |
| 1356 | encDiffs->unref(); // SkRefPtr and new both took a reference. |
| 1357 | encoding->insert("Differences", encDiffs.get()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1358 | encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1359 | encDiffs->appendInt(1); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1360 | |
| 1361 | SkRefPtr<SkPDFArray> widthArray = new SkPDFArray(); |
| 1362 | widthArray->unref(); // SkRefPtr and new both took a ref. |
| 1363 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1364 | SkIRect bbox = SkIRect::MakeEmpty(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1365 | for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1366 | SkString characterName; |
| 1367 | characterName.printf("gid%d", gID); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1368 | encDiffs->appendName(characterName.c_str()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1369 | |
reed@google.com | ce11b26 | 2011-03-21 21:25:35 +0000 | [diff] [blame] | 1370 | const SkGlyph& glyph = cache->getGlyphIDMetrics(gID); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1371 | widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX)); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1372 | SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop, |
| 1373 | glyph.fWidth, glyph.fHeight); |
| 1374 | bbox.join(glyphBBox); |
| 1375 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1376 | SkDynamicMemoryWStream content; |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1377 | setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox, |
| 1378 | &content); |
| 1379 | const SkPath* path = cache->findPath(glyph); |
| 1380 | if (path) { |
| 1381 | SkPDFUtils::EmitPath(*path, &content); |
| 1382 | SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(), |
| 1383 | &content); |
| 1384 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1385 | SkRefPtr<SkMemoryStream> glyphStream = new SkMemoryStream(); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1386 | glyphStream->unref(); // SkRefPtr and new both took a ref. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 1387 | glyphStream->setData(content.copyToData())->unref(); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1388 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1389 | SkRefPtr<SkPDFStream> glyphDescription = |
| 1390 | new SkPDFStream(glyphStream.get()); |
| 1391 | // SkRefPtr and new both ref()'d charProcs, pass one. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1392 | addResource(glyphDescription.get()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1393 | charProcs->insert(characterName.c_str(), |
| 1394 | new SkPDFObjRef(glyphDescription.get()))->unref(); |
| 1395 | } |
| 1396 | |
| 1397 | insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1398 | insertInt("FirstChar", firstGlyphID()); |
| 1399 | insertInt("LastChar", lastGlyphID()); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1400 | insert("Widths", widthArray.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 1401 | insertName("CIDToGIDMap", "Identity"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1402 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1403 | populateToUnicodeTable(NULL); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1404 | return true; |
| 1405 | } |