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