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