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 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 8 | #include "SkData.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 9 | #include "SkGlyphCache.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 11 | #include "SkPDFCanon.h" |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 12 | #include "SkPDFConvertType1FontStream.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 13 | #include "SkPDFDevice.h" |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 14 | #include "SkPDFMakeToUnicodeCmap.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 15 | #include "SkPDFFont.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 16 | #include "SkPDFUtils.h" |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 17 | #include "SkRefCnt.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 18 | #include "SkScalar.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 20 | #include "SkTypefacePriv.h" |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 21 | #include "SkTypes.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 22 | #include "SkUtils.h" |
| 23 | |
benjaminwagner | bc348f4 | 2016-01-05 17:00:04 -0800 | [diff] [blame] | 24 | #if defined (SK_SFNTLY_SUBSETTER) |
| 25 | #if defined (GOOGLE3) |
benjaminwagner | 86ea33e | 2015-10-26 10:46:25 -0700 | [diff] [blame] | 26 | // #including #defines doesn't work with this build system. |
| 27 | #include "typography/font/sfntly/src/sample/chromium/font_subsetter.h" |
benjaminwagner | bc348f4 | 2016-01-05 17:00:04 -0800 | [diff] [blame] | 28 | #else |
| 29 | #include SK_SFNTLY_SUBSETTER |
benjaminwagner | 86ea33e | 2015-10-26 10:46:25 -0700 | [diff] [blame] | 30 | #endif |
vandebo@chromium.org | 1f16589 | 2011-07-26 02:11:41 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
vandebo@chromium.org | dcf9c19 | 2013-03-13 20:01:51 +0000 | [diff] [blame] | 35 | // PDF's notion of symbolic vs non-symbolic is related to the character set, not |
| 36 | // symbols vs. characters. Rarely is a font the right character set to call it |
| 37 | // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) |
| 38 | static const int kPdfSymbolic = 4; |
| 39 | |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 40 | struct AdvanceMetric { |
| 41 | enum MetricType { |
| 42 | kDefault, // Default advance: fAdvance.count = 1 |
| 43 | kRange, // Advances for a range: fAdvance.count = fEndID-fStartID |
| 44 | kRun // fStartID-fEndID have same advance: fAdvance.count = 1 |
| 45 | }; |
| 46 | MetricType fType; |
| 47 | uint16_t fStartId; |
| 48 | uint16_t fEndId; |
| 49 | SkTDArray<int16_t> fAdvance; |
| 50 | AdvanceMetric(uint16_t startId) : fStartId(startId) {} |
| 51 | AdvanceMetric(AdvanceMetric&&) = default; |
| 52 | AdvanceMetric& operator=(AdvanceMetric&& other) = default; |
| 53 | AdvanceMetric(const AdvanceMetric&) = delete; |
| 54 | AdvanceMetric& operator=(const AdvanceMetric&) = delete; |
| 55 | }; |
| 56 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 57 | class SkPDFType0Font final : public SkPDFFont { |
| 58 | public: |
| 59 | SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
| 60 | SkTypeface* typeface); |
| 61 | virtual ~SkPDFType0Font(); |
| 62 | bool multiByteGlyphs() const override { return true; } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 63 | sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 64 | #ifdef SK_DEBUG |
| 65 | void emitObject(SkWStream*, |
| 66 | const SkPDFObjNumMap&, |
| 67 | const SkPDFSubstituteMap&) const override; |
| 68 | #endif |
| 69 | |
| 70 | private: |
| 71 | #ifdef SK_DEBUG |
| 72 | bool fPopulated; |
| 73 | #endif |
| 74 | bool populate(const SkPDFGlyphSet* subset); |
| 75 | typedef SkPDFDict INHERITED; |
| 76 | }; |
| 77 | |
| 78 | class SkPDFCIDFont final : public SkPDFFont { |
| 79 | public: |
| 80 | SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, |
| 81 | SkTypeface* typeface, |
| 82 | const SkPDFGlyphSet* subset); |
| 83 | virtual ~SkPDFCIDFont(); |
| 84 | bool multiByteGlyphs() const override { return true; } |
| 85 | |
| 86 | private: |
| 87 | bool populate(const SkPDFGlyphSet* subset); |
| 88 | bool addFontDescriptor(int16_t defaultWidth, |
| 89 | const SkTDArray<uint32_t>* subset); |
| 90 | }; |
| 91 | |
| 92 | class SkPDFType1Font final : public SkPDFFont { |
| 93 | public: |
| 94 | SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, |
| 95 | SkTypeface* typeface, |
| 96 | uint16_t glyphID, |
| 97 | SkPDFDict* relatedFontDescriptor); |
| 98 | virtual ~SkPDFType1Font(); |
| 99 | bool multiByteGlyphs() const override { return false; } |
| 100 | |
| 101 | private: |
| 102 | bool populate(int16_t glyphID); |
| 103 | bool addFontDescriptor(int16_t defaultWidth); |
| 104 | }; |
| 105 | |
| 106 | class SkPDFType3Font final : public SkPDFFont { |
| 107 | public: |
| 108 | SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
| 109 | SkTypeface* typeface, |
| 110 | uint16_t glyphID); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 111 | virtual ~SkPDFType3Font() {} |
| 112 | void emitObject(SkWStream*, |
| 113 | const SkPDFObjNumMap&, |
| 114 | const SkPDFSubstituteMap&) const override { |
| 115 | SkDEBUGFAIL("should call getFontSubset!"); |
| 116 | } |
| 117 | sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 118 | bool multiByteGlyphs() const override { return false; } |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 119 | }; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 120 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | // File-Local Functions |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 125 | const int16_t kInvalidAdvance = SK_MinS16; |
| 126 | const int16_t kDontCareAdvance = SK_MinS16 + 1; |
| 127 | |
| 128 | static void stripUninterestingTrailingAdvancesFromRange( |
| 129 | AdvanceMetric* range) { |
| 130 | SkASSERT(range); |
| 131 | |
| 132 | int expectedAdvanceCount = range->fEndId - range->fStartId + 1; |
| 133 | if (range->fAdvance.count() < expectedAdvanceCount) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | for (int i = expectedAdvanceCount - 1; i >= 0; --i) { |
| 138 | if (range->fAdvance[i] != kDontCareAdvance && |
| 139 | range->fAdvance[i] != kInvalidAdvance && |
| 140 | range->fAdvance[i] != 0) { |
| 141 | range->fEndId = range->fStartId + i; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void zeroWildcardsInRange(AdvanceMetric* range) { |
| 148 | SkASSERT(range); |
| 149 | if (range->fType != AdvanceMetric::kRange) { |
| 150 | return; |
| 151 | } |
| 152 | SkASSERT(range->fAdvance.count() == range->fEndId - range->fStartId + 1); |
| 153 | |
| 154 | // Zero out wildcards. |
| 155 | for (int i = 0; i < range->fAdvance.count(); ++i) { |
| 156 | if (range->fAdvance[i] == kDontCareAdvance) { |
| 157 | range->fAdvance[i] = 0; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void FinishRange( |
| 163 | AdvanceMetric* range, |
| 164 | int endId, |
| 165 | AdvanceMetric::MetricType type) { |
| 166 | range->fEndId = endId; |
| 167 | range->fType = type; |
| 168 | stripUninterestingTrailingAdvancesFromRange(range); |
| 169 | int newLength; |
| 170 | if (type == AdvanceMetric::kRange) { |
| 171 | newLength = range->fEndId - range->fStartId + 1; |
| 172 | } else { |
| 173 | if (range->fEndId == range->fStartId) { |
| 174 | range->fType = AdvanceMetric::kRange; |
| 175 | } |
| 176 | newLength = 1; |
| 177 | } |
| 178 | SkASSERT(range->fAdvance.count() >= newLength); |
| 179 | range->fAdvance.setCount(newLength); |
| 180 | zeroWildcardsInRange(range); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** Retrieve advance data for glyphs. Used by the PDF backend. |
| 185 | @param num_glyphs Total number of glyphs in the given font. |
| 186 | @param glyphIDs For per-glyph info, specify subset of the |
| 187 | font by giving glyph ids. Each integer |
| 188 | represents a glyph id. Passing nullptr |
| 189 | means all glyphs in the font. |
| 190 | @param glyphIDsCount Number of elements in subsetGlyphIds. |
| 191 | Ignored if glyphIDs is nullptr. |
| 192 | */ |
| 193 | // TODO(halcanary): this function is complex enough to need its logic |
| 194 | // tested with unit tests. On the other hand, I want to do another |
| 195 | // round of re-factoring before figuring out how to mock this. |
| 196 | // TODO(halcanary): this function should be combined with |
| 197 | // composeAdvanceData() so that we don't need to produce a linked list |
| 198 | // of intermediate values. Or we could make the intermediate value |
| 199 | // something other than a linked list. |
| 200 | static void get_glyph_widths(SkSinglyLinkedList<AdvanceMetric>* glyphWidths, |
| 201 | int num_glyphs, |
| 202 | const uint32_t* subsetGlyphIDs, |
| 203 | uint32_t subsetGlyphIDsLength, |
| 204 | SkGlyphCache* glyphCache) { |
| 205 | // Assuming that on average, the ASCII representation of an advance plus |
| 206 | // a space is 8 characters and the ASCII representation of a glyph id is 3 |
| 207 | // characters, then the following cut offs for using different range types |
| 208 | // apply: |
| 209 | // The cost of stopping and starting the range is 7 characers |
| 210 | // a. Removing 4 0's or don't care's is a win |
| 211 | // The cost of stopping and starting the range plus a run is 22 |
| 212 | // characters |
| 213 | // b. Removing 3 repeating advances is a win |
| 214 | // c. Removing 2 repeating advances and 3 don't cares is a win |
| 215 | // When not currently in a range the cost of a run over a range is 16 |
| 216 | // characaters, so: |
| 217 | // d. Removing a leading 0/don't cares is a win because it is omitted |
| 218 | // e. Removing 2 repeating advances is a win |
| 219 | |
| 220 | AdvanceMetric* prevRange = nullptr; |
| 221 | int16_t lastAdvance = kInvalidAdvance; |
| 222 | int repeatedAdvances = 0; |
| 223 | int wildCardsInRun = 0; |
| 224 | int trailingWildCards = 0; |
| 225 | uint32_t subsetIndex = 0; |
| 226 | |
| 227 | // Limit the loop count to glyph id ranges provided. |
| 228 | int firstIndex = 0; |
| 229 | int lastIndex = num_glyphs; |
| 230 | if (subsetGlyphIDs) { |
| 231 | firstIndex = static_cast<int>(subsetGlyphIDs[0]); |
| 232 | lastIndex = |
| 233 | static_cast<int>(subsetGlyphIDs[subsetGlyphIDsLength - 1]) + 1; |
| 234 | } |
| 235 | AdvanceMetric curRange(firstIndex); |
| 236 | |
| 237 | for (int gId = firstIndex; gId <= lastIndex; gId++) { |
| 238 | int16_t advance = kInvalidAdvance; |
| 239 | if (gId < lastIndex) { |
| 240 | // Get glyph id only when subset is nullptr, or the id is in subset. |
| 241 | SkASSERT(!subsetGlyphIDs || (subsetIndex < subsetGlyphIDsLength && |
| 242 | static_cast<uint32_t>(gId) <= subsetGlyphIDs[subsetIndex])); |
| 243 | if (!subsetGlyphIDs || |
| 244 | (subsetIndex < subsetGlyphIDsLength && |
| 245 | static_cast<uint32_t>(gId) == subsetGlyphIDs[subsetIndex])) { |
| 246 | advance = (int16_t)glyphCache->getGlyphIDAdvance(gId).fAdvanceX; |
| 247 | ++subsetIndex; |
| 248 | } else { |
| 249 | advance = kDontCareAdvance; |
| 250 | } |
| 251 | } |
| 252 | if (advance == lastAdvance) { |
| 253 | repeatedAdvances++; |
| 254 | trailingWildCards = 0; |
| 255 | } else if (advance == kDontCareAdvance) { |
| 256 | wildCardsInRun++; |
| 257 | trailingWildCards++; |
| 258 | } else if (curRange.fAdvance.count() == |
| 259 | repeatedAdvances + 1 + wildCardsInRun) { // All in run. |
| 260 | if (lastAdvance == 0) { |
| 261 | curRange.fStartId = gId; // reset |
| 262 | curRange.fAdvance.setCount(0); |
| 263 | trailingWildCards = 0; |
| 264 | } else if (repeatedAdvances + 1 >= 2 || trailingWildCards >= 4) { |
| 265 | FinishRange(&curRange, gId - 1, AdvanceMetric::kRun); |
| 266 | prevRange = glyphWidths->emplace_back(std::move(curRange)); |
| 267 | curRange = AdvanceMetric(gId); |
| 268 | trailingWildCards = 0; |
| 269 | } |
| 270 | repeatedAdvances = 0; |
| 271 | wildCardsInRun = trailingWildCards; |
| 272 | trailingWildCards = 0; |
| 273 | } else { |
| 274 | if (lastAdvance == 0 && |
| 275 | repeatedAdvances + 1 + wildCardsInRun >= 4) { |
| 276 | FinishRange(&curRange, |
| 277 | gId - repeatedAdvances - wildCardsInRun - 2, |
| 278 | AdvanceMetric::kRange); |
| 279 | prevRange = glyphWidths->emplace_back(std::move(curRange)); |
| 280 | curRange = AdvanceMetric(gId); |
| 281 | trailingWildCards = 0; |
| 282 | } else if (trailingWildCards >= 4 && repeatedAdvances + 1 < 2) { |
| 283 | FinishRange(&curRange, gId - trailingWildCards - 1, |
| 284 | AdvanceMetric::kRange); |
| 285 | prevRange = glyphWidths->emplace_back(std::move(curRange)); |
| 286 | curRange = AdvanceMetric(gId); |
| 287 | trailingWildCards = 0; |
| 288 | } else if (lastAdvance != 0 && |
| 289 | (repeatedAdvances + 1 >= 3 || |
| 290 | (repeatedAdvances + 1 >= 2 && wildCardsInRun >= 3))) { |
| 291 | FinishRange(&curRange, |
| 292 | gId - repeatedAdvances - wildCardsInRun - 2, |
| 293 | AdvanceMetric::kRange); |
| 294 | (void)glyphWidths->emplace_back(std::move(curRange)); |
| 295 | curRange = |
| 296 | AdvanceMetric(gId - repeatedAdvances - wildCardsInRun - 1); |
| 297 | curRange.fAdvance.append(1, &lastAdvance); |
| 298 | FinishRange(&curRange, gId - 1, AdvanceMetric::kRun); |
| 299 | prevRange = glyphWidths->emplace_back(std::move(curRange)); |
| 300 | curRange = AdvanceMetric(gId); |
| 301 | trailingWildCards = 0; |
| 302 | } |
| 303 | repeatedAdvances = 0; |
| 304 | wildCardsInRun = trailingWildCards; |
| 305 | trailingWildCards = 0; |
| 306 | } |
| 307 | curRange.fAdvance.append(1, &advance); |
| 308 | if (advance != kDontCareAdvance) { |
| 309 | lastAdvance = advance; |
| 310 | } |
| 311 | } |
| 312 | if (curRange.fStartId == lastIndex) { |
| 313 | if (!prevRange) { |
| 314 | glyphWidths->reset(); |
| 315 | return; // https://crbug.com/567031 |
| 316 | } |
| 317 | } else { |
| 318 | FinishRange(&curRange, lastIndex - 1, AdvanceMetric::kRange); |
| 319 | glyphWidths->emplace_back(std::move(curRange)); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | //////////////////////////////////////////////////////////////////////////////// |
| 324 | |
| 325 | |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 326 | // scale from em-units to base-1000, returning as a SkScalar |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 327 | SkScalar from_font_units(SkScalar scaled, uint16_t emSize) { |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 328 | if (emSize == 1000) { |
| 329 | return scaled; |
| 330 | } else { |
| 331 | return SkScalarMulDiv(scaled, 1000, emSize); |
| 332 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 333 | } |
| 334 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 335 | SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
| 336 | return from_font_units(SkIntToScalar(val), emSize); |
| 337 | } |
| 338 | |
| 339 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 340 | void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 341 | SkDynamicMemoryWStream* content) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 342 | // Specify width and bounding box for the glyph. |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 343 | SkPDFUtils::AppendScalar(width, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 344 | content->writeText(" 0 "); |
| 345 | content->writeDecAsText(box.fLeft); |
| 346 | content->writeText(" "); |
| 347 | content->writeDecAsText(box.fTop); |
| 348 | content->writeText(" "); |
| 349 | content->writeDecAsText(box.fRight); |
| 350 | content->writeText(" "); |
| 351 | content->writeDecAsText(box.fBottom); |
| 352 | content->writeText(" d1\n"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 353 | } |
| 354 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 355 | static sk_sp<SkPDFArray> makeFontBBox(SkIRect glyphBBox, uint16_t emSize) { |
| 356 | auto bbox = sk_make_sp<SkPDFArray>(); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 357 | bbox->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 358 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize)); |
| 359 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize)); |
| 360 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize)); |
| 361 | bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize)); |
halcanary | 51d04d3 | 2016-03-08 13:03:55 -0800 | [diff] [blame] | 362 | return bbox; |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 363 | } |
| 364 | |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 365 | sk_sp<SkPDFArray> composeAdvanceData( |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 366 | const SkSinglyLinkedList<AdvanceMetric>& advanceInfo, |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 367 | uint16_t emSize, |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 368 | int16_t* defaultAdvance) { |
| 369 | auto result = sk_make_sp<SkPDFArray>(); |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 370 | for (const AdvanceMetric& range : advanceInfo) { |
halcanary | e20a875 | 2016-05-08 18:47:16 -0700 | [diff] [blame] | 371 | switch (range.fType) { |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 372 | case AdvanceMetric::kDefault: { |
halcanary | e20a875 | 2016-05-08 18:47:16 -0700 | [diff] [blame] | 373 | SkASSERT(range.fAdvance.count() == 1); |
| 374 | *defaultAdvance = range.fAdvance[0]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 377 | case AdvanceMetric::kRange: { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 378 | auto advanceArray = sk_make_sp<SkPDFArray>(); |
halcanary | e20a875 | 2016-05-08 18:47:16 -0700 | [diff] [blame] | 379 | for (int j = 0; j < range.fAdvance.count(); j++) |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 380 | advanceArray->appendScalar( |
| 381 | scaleFromFontUnits(range.fAdvance[j], emSize)); |
halcanary | e20a875 | 2016-05-08 18:47:16 -0700 | [diff] [blame] | 382 | result->appendInt(range.fStartId); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 383 | result->appendObject(std::move(advanceArray)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 384 | break; |
| 385 | } |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 386 | case AdvanceMetric::kRun: { |
halcanary | e20a875 | 2016-05-08 18:47:16 -0700 | [diff] [blame] | 387 | SkASSERT(range.fAdvance.count() == 1); |
| 388 | result->appendInt(range.fStartId); |
| 389 | result->appendInt(range.fEndId); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 390 | result->appendScalar( |
| 391 | scaleFromFontUnits(range.fAdvance[0], emSize)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | } // namespace |
| 400 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 401 | |
| 402 | /////////////////////////////////////////////////////////////////////////////// |
| 403 | // class SkPDFGlyphSet |
| 404 | /////////////////////////////////////////////////////////////////////////////// |
| 405 | |
| 406 | SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) { |
| 407 | } |
| 408 | |
| 409 | void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) { |
| 410 | for (int i = 0; i < numGlyphs; ++i) { |
| 411 | fBitSet.setBit(glyphIDs[i], true); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | bool SkPDFGlyphSet::has(uint16_t glyphID) const { |
| 416 | return fBitSet.isBitSet(glyphID); |
| 417 | } |
| 418 | |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 419 | void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const { |
| 420 | fBitSet.exportTo(glyphIDs); |
| 421 | } |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 422 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 423 | /////////////////////////////////////////////////////////////////////////////// |
| 424 | // class SkPDFGlyphSetMap |
| 425 | /////////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 426 | |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 427 | SkPDFGlyphSetMap::SkPDFGlyphSetMap() {} |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 428 | |
| 429 | SkPDFGlyphSetMap::~SkPDFGlyphSetMap() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 430 | fMap.reset(); |
| 431 | } |
| 432 | |
| 433 | void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, |
| 434 | int numGlyphs) { |
| 435 | SkPDFGlyphSet* subset = getGlyphSetForFont(font); |
| 436 | if (subset) { |
| 437 | subset->set(glyphIDs, numGlyphs); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) { |
| 442 | int index = fMap.count(); |
| 443 | for (int i = 0; i < index; ++i) { |
| 444 | if (fMap[i].fFont == font) { |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 445 | return &fMap[i].fGlyphSet; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 448 | FontGlyphSetPair& pair = fMap.push_back(); |
| 449 | pair.fFont = font; |
| 450 | return &pair.fGlyphSet; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /////////////////////////////////////////////////////////////////////////////// |
| 454 | // class SkPDFFont |
| 455 | /////////////////////////////////////////////////////////////////////////////// |
| 456 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 457 | /* Font subset design: It would be nice to be able to subset fonts |
| 458 | * (particularly type 3 fonts), but it's a lot of work and not a priority. |
| 459 | * |
| 460 | * Resources are canonicalized and uniqueified by pointer so there has to be |
| 461 | * some additional state indicating which subset of the font is used. It |
| 462 | * must be maintained at the page granularity and then combined at the document |
| 463 | * granularity. a) change SkPDFFont to fill in its state on demand, kind of |
| 464 | * like SkPDFGraphicState. b) maintain a per font glyph usage class in each |
| 465 | * page/pdf device. c) in the document, retrieve the per font glyph usage |
| 466 | * from each page and combine it and ask for a resource with that subset. |
| 467 | */ |
| 468 | |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 469 | SkPDFFont::~SkPDFFont() {} |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 470 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 471 | SkTypeface* SkPDFFont::typeface() { |
| 472 | return fTypeface.get(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 473 | } |
| 474 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 475 | SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 476 | return fFontType; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 477 | } |
| 478 | |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 479 | bool SkPDFFont::canEmbed() const { |
| 480 | if (!fFontInfo.get()) { |
| 481 | SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); |
| 482 | return true; |
| 483 | } |
| 484 | return (fFontInfo->fFlags & |
| 485 | SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0; |
| 486 | } |
| 487 | |
| 488 | bool SkPDFFont::canSubset() const { |
| 489 | if (!fFontInfo.get()) { |
| 490 | SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); |
| 491 | return true; |
| 492 | } |
| 493 | return (fFontInfo->fFlags & |
| 494 | SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag) == 0; |
| 495 | } |
| 496 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 497 | bool SkPDFFont::hasGlyph(uint16_t id) { |
| 498 | return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; |
| 499 | } |
| 500 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame^] | 501 | int SkPDFFont::glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 502 | // 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] | 503 | if (this->multiByteGlyphs()) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 504 | return numGlyphs; |
| 505 | } |
| 506 | |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 507 | for (int i = 0; i < numGlyphs; i++) { |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 508 | if (glyphIDs[i] == 0) { |
| 509 | continue; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 510 | } |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 511 | if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) { |
| 512 | return i; |
| 513 | } |
| 514 | glyphIDs[i] -= (fFirstGlyphID - 1); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 515 | } |
| 516 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 517 | return numGlyphs; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 518 | } |
| 519 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame^] | 520 | int SkPDFFont::glyphsToPDFFontEncodingCount(const SkGlyphID* glyphIDs, |
| 521 | int numGlyphs) const { |
| 522 | if (this->multiByteGlyphs()) { // A font with multibyte glyphs will |
| 523 | return numGlyphs; // support all glyph IDs in a single font. |
| 524 | } |
| 525 | for (int i = 0; i < numGlyphs; i++) { |
| 526 | if (glyphIDs[i] != 0 && |
| 527 | (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID)) { |
| 528 | return i; |
| 529 | } |
| 530 | } |
| 531 | return numGlyphs; |
| 532 | } |
| 533 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 534 | // static |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 535 | SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, |
| 536 | SkTypeface* typeface, |
| 537 | uint16_t glyphID) { |
| 538 | SkASSERT(canon); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 539 | const uint32_t fontID = SkTypeface::UniqueID(typeface); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 540 | SkPDFFont* relatedFont; |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 541 | if (SkPDFFont* pdfFont = canon->findFont(fontID, glyphID, &relatedFont)) { |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 542 | return SkRef(pdfFont); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 543 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 544 | SkAutoResolveDefaultTypeface autoResolve(typeface); |
| 545 | typeface = autoResolve.get(); |
| 546 | SkASSERT(typeface); |
| 547 | int glyphCount = typeface->countGlyphs(); |
| 548 | if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. |
| 549 | glyphCount > 1 + SK_MaxU16 || // invalid glyphCount |
| 550 | glyphID >= glyphCount) { // invalid glyph |
| 551 | return nullptr; |
| 552 | } |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 553 | sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 554 | SkPDFDict* relatedFontDescriptor = nullptr; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 555 | if (relatedFont) { |
| 556 | fontMetrics.reset(SkSafeRef(relatedFont->fontInfo())); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 557 | relatedFontDescriptor = relatedFont->getFontDescriptor(); |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 558 | |
| 559 | // This only is to catch callers who pass invalid glyph ids. |
| 560 | // If glyph id is invalid, then we will create duplicate entries |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 561 | // for TrueType fonts. |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 562 | SkAdvancedTypefaceMetrics::FontType fontType = |
edisonn@google.com | 022e857 | 2012-10-23 21:32:39 +0000 | [diff] [blame] | 563 | fontMetrics.get() ? fontMetrics.get()->fType : |
| 564 | SkAdvancedTypefaceMetrics::kOther_Font; |
| 565 | |
| 566 | if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || |
| 567 | fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 568 | return SkRef(relatedFont); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 569 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 570 | } else { |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 571 | SkTypeface::PerGlyphInfo info = |
| 572 | SkTBitOr(SkTypeface::kGlyphNames_PerGlyphInfo, |
| 573 | SkTypeface::kToUnicode_PerGlyphInfo); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 574 | fontMetrics.reset( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 575 | typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 576 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 577 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 578 | SkPDFFont* font = SkPDFFont::Create(canon, fontMetrics.get(), typeface, |
| 579 | glyphID, relatedFontDescriptor); |
| 580 | canon->addFont(font, fontID, font->fFirstGlyphID); |
| 581 | return font; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 584 | sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 585 | return nullptr; // Default: no support. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 586 | } |
| 587 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 588 | // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 589 | SkPDFFont::SkPDFFont(const SkAdvancedTypefaceMetrics* info, |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 590 | SkTypeface* typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 591 | SkPDFDict* relatedFontDescriptor) |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 592 | : SkPDFDict("Font") |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 593 | , fTypeface(ref_or_default(typeface)) |
| 594 | , fFirstGlyphID(1) |
| 595 | , fLastGlyphID(info ? info->fLastGlyphID : 0) |
| 596 | , fFontInfo(SkSafeRef(info)) |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 597 | , fDescriptor(SkSafeRef(relatedFontDescriptor)) |
| 598 | , fFontType((!info || info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag) |
| 599 | ? SkAdvancedTypefaceMetrics::kOther_Font |
| 600 | : info->fType) { |
| 601 | SkASSERT(fTypeface); |
| 602 | if (0 == fLastGlyphID) { |
| 603 | fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | // static |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 608 | SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, |
| 609 | const SkAdvancedTypefaceMetrics* info, |
| 610 | SkTypeface* typeface, |
| 611 | uint16_t glyphID, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 612 | SkPDFDict* relatedFontDescriptor) { |
| 613 | SkAdvancedTypefaceMetrics::FontType type = |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 614 | info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 615 | SkAdvancedTypefaceMetrics::FontFlags flags = |
| 616 | info ? info->fFlags : SkAdvancedTypefaceMetrics::kEmpty_FontFlag; |
| 617 | if (SkToBool(flags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 618 | return new SkPDFType3Font(info, typeface, glyphID); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 619 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 620 | switch (type) { |
| 621 | case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 622 | case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| 623 | SkASSERT(relatedFontDescriptor == nullptr); |
| 624 | SkASSERT(info != nullptr); |
| 625 | return new SkPDFType0Font(info, typeface); |
| 626 | case SkAdvancedTypefaceMetrics::kType1_Font: |
| 627 | SkASSERT(info != nullptr); |
| 628 | return new SkPDFType1Font(info, typeface, glyphID, relatedFontDescriptor); |
| 629 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
| 630 | SkASSERT(info != nullptr); |
| 631 | // fallthrough |
| 632 | case SkAdvancedTypefaceMetrics::kOther_Font: |
| 633 | return new SkPDFType3Font(info, typeface, glyphID); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 634 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 635 | SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); |
| 636 | return nullptr; |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 637 | } |
| 638 | |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 639 | const SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 640 | return fFontInfo.get(); |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 641 | } |
| 642 | |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 643 | void SkPDFFont::setFontInfo(const SkAdvancedTypefaceMetrics* info) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 644 | if (info == nullptr || info == fFontInfo.get()) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 645 | return; |
| 646 | } |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 647 | fFontInfo.reset(info); |
| 648 | SkSafeRef(info); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 649 | } |
| 650 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 651 | uint16_t SkPDFFont::firstGlyphID() const { |
| 652 | return fFirstGlyphID; |
| 653 | } |
| 654 | |
| 655 | uint16_t SkPDFFont::lastGlyphID() const { |
| 656 | return fLastGlyphID; |
| 657 | } |
| 658 | |
| 659 | void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
| 660 | fLastGlyphID = glyphID; |
| 661 | } |
| 662 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 663 | SkPDFDict* SkPDFFont::getFontDescriptor() { |
| 664 | return fDescriptor.get(); |
| 665 | } |
| 666 | |
| 667 | void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 668 | fDescriptor.reset(descriptor); |
| 669 | SkSafeRef(descriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 673 | if (fDescriptor.get() == nullptr) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 674 | return false; |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 675 | } |
| 676 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 677 | const uint16_t emSize = fFontInfo->fEmSize; |
| 678 | |
| 679 | fDescriptor->insertName("FontName", fFontInfo->fFontName); |
vandebo@chromium.org | dcf9c19 | 2013-03-13 20:01:51 +0000 | [diff] [blame] | 680 | fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 681 | fDescriptor->insertScalar("Ascent", |
| 682 | scaleFromFontUnits(fFontInfo->fAscent, emSize)); |
| 683 | fDescriptor->insertScalar("Descent", |
| 684 | scaleFromFontUnits(fFontInfo->fDescent, emSize)); |
| 685 | fDescriptor->insertScalar("StemV", |
| 686 | scaleFromFontUnits(fFontInfo->fStemV, emSize)); |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 687 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 688 | fDescriptor->insertScalar("CapHeight", |
| 689 | scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); |
| 690 | fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); |
halcanary | bf51cfd | 2015-05-05 10:24:09 -0700 | [diff] [blame] | 691 | fDescriptor->insertObject( |
| 692 | "FontBBox", makeFontBBox(fFontInfo->fBBox, fFontInfo->fEmSize)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 693 | |
| 694 | if (defaultWidth > 0) { |
| 695 | fDescriptor->insertScalar("MissingWidth", |
| 696 | scaleFromFontUnits(defaultWidth, emSize)); |
| 697 | } |
| 698 | return true; |
| 699 | } |
| 700 | |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 701 | void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 702 | // Single byte glyph encoding supports a max of 255 glyphs. |
| 703 | fFirstGlyphID = glyphID - (glyphID - 1) % 255; |
| 704 | if (fLastGlyphID > fFirstGlyphID + 255 - 1) { |
| 705 | fLastGlyphID = fFirstGlyphID + 255 - 1; |
| 706 | } |
| 707 | } |
| 708 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 709 | void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 710 | if (fFontInfo == nullptr || fFontInfo->fGlyphToUnicode.begin() == nullptr) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
halcanary | bf51cfd | 2015-05-05 10:24:09 -0700 | [diff] [blame] | 713 | this->insertObjRef("ToUnicode", |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 714 | SkPDFMakeToUnicodeCmap(fFontInfo->fGlyphToUnicode, |
| 715 | subset, |
| 716 | multiByteGlyphs(), |
| 717 | firstGlyphID(), |
| 718 | lastGlyphID())); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 719 | } |
| 720 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 721 | /////////////////////////////////////////////////////////////////////////////// |
| 722 | // class SkPDFType0Font |
| 723 | /////////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 724 | |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 725 | SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, SkTypeface* typeface) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 726 | : SkPDFFont(info, typeface, nullptr) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 727 | SkDEBUGCODE(fPopulated = false); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 728 | if (!canSubset()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 729 | this->populate(nullptr); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 730 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | SkPDFType0Font::~SkPDFType0Font() {} |
| 734 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 735 | sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 736 | if (!canSubset()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 737 | return nullptr; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 738 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 739 | auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo(), typeface()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 740 | newSubset->populate(subset); |
| 741 | return newSubset; |
| 742 | } |
| 743 | |
| 744 | #ifdef SK_DEBUG |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 745 | void SkPDFType0Font::emitObject(SkWStream* stream, |
| 746 | const SkPDFObjNumMap& objNumMap, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 747 | const SkPDFSubstituteMap& substitutes) const { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 748 | SkASSERT(fPopulated); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 749 | return INHERITED::emitObject(stream, objNumMap, substitutes); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 750 | } |
| 751 | #endif |
| 752 | |
| 753 | bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
| 754 | insertName("Subtype", "Type0"); |
| 755 | insertName("BaseFont", fontInfo()->fFontName); |
| 756 | insertName("Encoding", "Identity-H"); |
| 757 | |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 758 | sk_sp<SkPDFCIDFont> newCIDFont( |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 759 | new SkPDFCIDFont(fontInfo(), typeface(), subset)); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 760 | auto descendantFonts = sk_make_sp<SkPDFArray>(); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 761 | descendantFonts->appendObjRef(std::move(newCIDFont)); |
| 762 | this->insertObject("DescendantFonts", std::move(descendantFonts)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 763 | |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 764 | this->populateToUnicodeTable(subset); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 765 | |
| 766 | SkDEBUGCODE(fPopulated = true); |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | /////////////////////////////////////////////////////////////////////////////// |
| 771 | // class SkPDFCIDFont |
| 772 | /////////////////////////////////////////////////////////////////////////////// |
| 773 | |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 774 | SkPDFCIDFont::SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 775 | SkTypeface* typeface, |
| 776 | const SkPDFGlyphSet* subset) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 777 | : SkPDFFont(info, typeface, nullptr) { |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 778 | this->populate(subset); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | SkPDFCIDFont::~SkPDFCIDFont() {} |
| 782 | |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 783 | #ifdef SK_SFNTLY_SUBSETTER |
| 784 | // if possible, make no copy. |
| 785 | static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { |
| 786 | SkASSERT(stream); |
| 787 | (void)stream->rewind(); |
| 788 | SkASSERT(stream->hasLength()); |
| 789 | size_t size = stream->getLength(); |
| 790 | if (const void* base = stream->getMemoryBase()) { |
| 791 | SkData::ReleaseProc proc = |
| 792 | [](const void*, void* ctx) { delete (SkStream*)ctx; }; |
| 793 | return SkData::MakeWithProc(base, size, proc, stream.release()); |
| 794 | } |
| 795 | return SkData::MakeFromStream(stream.get(), size); |
| 796 | } |
| 797 | |
| 798 | static sk_sp<SkPDFObject> get_subset_font_stream( |
| 799 | std::unique_ptr<SkStreamAsset> fontAsset, |
| 800 | const SkTDArray<uint32_t>& subset, |
| 801 | const char* fontName) { |
| 802 | // sfntly requires unsigned int* to be passed in, |
| 803 | // as far as we know, unsigned int is equivalent |
| 804 | // to uint32_t on all platforms. |
| 805 | static_assert(sizeof(unsigned) == sizeof(uint32_t), ""); |
| 806 | |
| 807 | // TODO(halcanary): Use ttcIndex, not fontName. |
| 808 | |
| 809 | unsigned char* subsetFont{nullptr}; |
| 810 | int subsetFontSize{0}; |
| 811 | { |
| 812 | sk_sp<SkData> fontData(stream_to_data(std::move(fontAsset))); |
| 813 | subsetFontSize = |
| 814 | SfntlyWrapper::SubsetFont(fontName, |
| 815 | fontData->bytes(), |
| 816 | fontData->size(), |
| 817 | subset.begin(), |
| 818 | subset.count(), |
| 819 | &subsetFont); |
| 820 | } |
| 821 | SkASSERT(subsetFontSize > 0 || subsetFont == nullptr); |
| 822 | if (subsetFontSize < 1) { |
| 823 | return nullptr; |
| 824 | } |
| 825 | SkASSERT(subsetFont != nullptr); |
| 826 | auto subsetStream = sk_make_sp<SkPDFStream>( |
| 827 | SkData::MakeWithProc( |
| 828 | subsetFont, subsetFontSize, |
| 829 | [](const void* p, void*) { delete[] (unsigned char*)p; }, |
| 830 | nullptr)); |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 831 | subsetStream->dict()->insertInt("Length1", subsetFontSize); |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 832 | return subsetStream; |
| 833 | } |
| 834 | #endif // SK_SFNTLY_SUBSETTER |
| 835 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 836 | bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 837 | const SkTDArray<uint32_t>* subset) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 838 | auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 839 | setFontDescriptor(descriptor.get()); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 840 | if (!addCommonFontDescriptorEntries(defaultWidth)) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 841 | this->insertObjRef("FontDescriptor", std::move(descriptor)); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 842 | return false; |
| 843 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 844 | SkASSERT(this->canEmbed()); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 845 | |
| 846 | switch (getType()) { |
| 847 | case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 848 | int ttcIndex; |
| 849 | std::unique_ptr<SkStreamAsset> fontAsset( |
| 850 | this->typeface()->openStream(&ttcIndex)); |
| 851 | SkASSERT(fontAsset); |
| 852 | if (!fontAsset) { |
halcanary | 3243143 | 2016-03-30 12:59:14 -0700 | [diff] [blame] | 853 | return false; |
| 854 | } |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 855 | size_t fontSize = fontAsset->getLength(); |
halcanary | 725c620 | 2015-08-20 08:09:37 -0700 | [diff] [blame] | 856 | SkASSERT(fontSize > 0); |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 857 | if (fontSize == 0) { |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | #ifdef SK_SFNTLY_SUBSETTER |
| 862 | if (this->canSubset() && subset) { |
| 863 | sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( |
| 864 | std::move(fontAsset), *subset, fontInfo()->fFontName.c_str()); |
| 865 | if (subsetStream) { |
| 866 | descriptor->insertObjRef("FontFile2", std::move(subsetStream)); |
| 867 | break; |
| 868 | } |
| 869 | // If subsetting fails, fall back to original font data. |
| 870 | fontAsset.reset(this->typeface()->openStream(&ttcIndex)); |
| 871 | } |
| 872 | #endif // SK_SFNTLY_SUBSETTER |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 873 | auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset)); |
halcanary | 725c620 | 2015-08-20 08:09:37 -0700 | [diff] [blame] | 874 | fontStream->dict()->insertInt("Length1", fontSize); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 875 | descriptor->insertObjRef("FontFile2", std::move(fontStream)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
| 879 | case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
halcanary | 3243143 | 2016-03-30 12:59:14 -0700 | [diff] [blame] | 880 | std::unique_ptr<SkStreamAsset> fontData( |
| 881 | this->typeface()->openStream(nullptr)); |
| 882 | SkASSERT(fontData); |
| 883 | SkASSERT(fontData->getLength() > 0); |
| 884 | if (!fontData || 0 == fontData->getLength()) { |
| 885 | return false; |
| 886 | } |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 887 | auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontData)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 888 | if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { |
halcanary | 725c620 | 2015-08-20 08:09:37 -0700 | [diff] [blame] | 889 | fontStream->dict()->insertName("Subtype", "Type1C"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 890 | } else { |
halcanary | 725c620 | 2015-08-20 08:09:37 -0700 | [diff] [blame] | 891 | fontStream->dict()->insertName("Subtype", "CIDFontType0c"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 892 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 893 | descriptor->insertObjRef("FontFile3", std::move(fontStream)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 894 | break; |
| 895 | } |
| 896 | default: |
| 897 | SkASSERT(false); |
| 898 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 899 | this->insertObjRef("FontDescriptor", std::move(descriptor)); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 900 | return true; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 901 | } |
| 902 | |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 903 | void set_glyph_widths(SkTypeface* tf, |
| 904 | const SkTDArray<uint32_t>* glyphIDs, |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 905 | SkSinglyLinkedList<AdvanceMetric>* dst) { |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 906 | SkPaint tmpPaint; |
| 907 | tmpPaint.setHinting(SkPaint::kNo_Hinting); |
| 908 | tmpPaint.setTypeface(sk_ref_sp(tf)); |
| 909 | tmpPaint.setTextSize((SkScalar)tf->getUnitsPerEm()); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 910 | const SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 911 | SkAutoGlyphCache autoGlyphCache(tmpPaint, &props, nullptr); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 912 | if (!glyphIDs || glyphIDs->isEmpty()) { |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 913 | get_glyph_widths(dst, tf->countGlyphs(), nullptr, 0, autoGlyphCache.get()); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 914 | } else { |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 915 | get_glyph_widths(dst, tf->countGlyphs(), glyphIDs->begin(), |
| 916 | glyphIDs->count(), autoGlyphCache.get()); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 920 | sk_sp<const SkAdvancedTypefaceMetrics> SkPDFFont::GetFontMetricsWithGlyphNames( |
| 921 | SkTypeface* typeface, uint32_t* glyphs, uint32_t glyphsCount) { |
| 922 | return sk_sp<const SkAdvancedTypefaceMetrics>( |
| 923 | typeface->getAdvancedTypefaceMetrics( |
| 924 | SkTypeface::kGlyphNames_PerGlyphInfo, glyphs, glyphsCount)); |
| 925 | } |
| 926 | |
| 927 | sk_sp<const SkAdvancedTypefaceMetrics> SkPDFFont::GetFontMetricsWithToUnicode( |
| 928 | SkTypeface* typeface, uint32_t* glyphs, uint32_t glyphsCount) { |
| 929 | return sk_sp<const SkAdvancedTypefaceMetrics>( |
| 930 | typeface->getAdvancedTypefaceMetrics( |
| 931 | SkTypeface::kToUnicode_PerGlyphInfo, glyphs, glyphsCount)); |
| 932 | } |
| 933 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 934 | bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 935 | // Generate new font metrics with advance info for true type fonts. |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 936 | // Generate glyph id array. |
| 937 | SkTDArray<uint32_t> glyphIDs; |
| 938 | if (subset) { |
| 939 | if (!subset->has(0)) { |
| 940 | glyphIDs.push(0); // Always include glyph 0. |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 941 | } |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 942 | subset->exportTo(&glyphIDs); |
| 943 | } |
| 944 | if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 945 | uint32_t* glyphs = (glyphIDs.count() == 0) ? nullptr : glyphIDs.begin(); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 946 | uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 947 | sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = |
| 948 | SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, glyphsCount); |
| 949 | this->setFontInfo(fontMetrics.get()); |
| 950 | this->addFontDescriptor(0, &glyphIDs); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 951 | } else { |
| 952 | // Other CID fonts |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 953 | addFontDescriptor(0, nullptr); |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 954 | } |
| 955 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 956 | insertName("BaseFont", fontInfo()->fFontName); |
| 957 | |
| 958 | if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 959 | insertName("Subtype", "CIDFontType0"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 960 | } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 961 | insertName("Subtype", "CIDFontType2"); |
| 962 | insertName("CIDToGIDMap", "Identity"); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 963 | } else { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 964 | SkASSERT(false); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 965 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 966 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 967 | auto sysInfo = sk_make_sp<SkPDFDict>(); |
halcanary | bf51cfd | 2015-05-05 10:24:09 -0700 | [diff] [blame] | 968 | sysInfo->insertString("Registry", "Adobe"); |
| 969 | sysInfo->insertString("Ordering", "Identity"); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 970 | sysInfo->insertInt("Supplement", 0); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 971 | this->insertObject("CIDSystemInfo", std::move(sysInfo)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 972 | |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 973 | SkSinglyLinkedList<AdvanceMetric> tmpMetrics; |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 974 | set_glyph_widths(this->typeface(), &glyphIDs, &tmpMetrics); |
| 975 | int16_t defaultWidth = 0; |
| 976 | uint16_t emSize = (uint16_t)this->fontInfo()->fEmSize; |
halcanary | 8b1d32c | 2016-08-08 09:09:59 -0700 | [diff] [blame] | 977 | sk_sp<SkPDFArray> widths = composeAdvanceData(tmpMetrics, emSize, &defaultWidth); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 978 | if (widths->size()) { |
| 979 | this->insertObject("W", std::move(widths)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 980 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 981 | |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 982 | this->insertScalar( |
| 983 | "DW", scaleFromFontUnits(defaultWidth, emSize)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 984 | return true; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 985 | } |
| 986 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 987 | /////////////////////////////////////////////////////////////////////////////// |
| 988 | // class SkPDFType1Font |
| 989 | /////////////////////////////////////////////////////////////////////////////// |
| 990 | |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 991 | SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 992 | SkTypeface* typeface, |
| 993 | uint16_t glyphID, |
| 994 | SkPDFDict* relatedFontDescriptor) |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 995 | : SkPDFFont(info, typeface, relatedFontDescriptor) { |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 996 | this->populate(glyphID); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | SkPDFType1Font::~SkPDFType1Font() {} |
| 1000 | |
| 1001 | bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
halcanary | bf51cfd | 2015-05-05 10:24:09 -0700 | [diff] [blame] | 1002 | if (SkPDFDict* descriptor = getFontDescriptor()) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1003 | this->insertObjRef("FontDescriptor", |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 1004 | sk_ref_sp(descriptor)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1005 | return true; |
| 1006 | } |
| 1007 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1008 | auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1009 | setFontDescriptor(descriptor.get()); |
| 1010 | |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1011 | int ttcIndex; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1012 | size_t header SK_INIT_TO_AVOID_WARNING; |
| 1013 | size_t data SK_INIT_TO_AVOID_WARNING; |
| 1014 | size_t trailer SK_INIT_TO_AVOID_WARNING; |
halcanary | 3243143 | 2016-03-30 12:59:14 -0700 | [diff] [blame] | 1015 | std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex)); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1016 | sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), |
| 1017 | &header, &data, &trailer); |
| 1018 | if (!fontData) { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1021 | SkASSERT(this->canEmbed()); |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 1022 | auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 1023 | fontStream->dict()->insertInt("Length1", header); |
| 1024 | fontStream->dict()->insertInt("Length2", data); |
| 1025 | fontStream->dict()->insertInt("Length3", trailer); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1026 | descriptor->insertObjRef("FontFile", std::move(fontStream)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1027 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1028 | this->insertObjRef("FontDescriptor", std::move(descriptor)); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1029 | |
| 1030 | return addCommonFontDescriptorEntries(defaultWidth); |
| 1031 | } |
| 1032 | |
| 1033 | bool SkPDFType1Font::populate(int16_t glyphID) { |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1034 | this->insertName("Subtype", "Type1"); |
| 1035 | this->insertName("BaseFont", fontInfo()->fFontName); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1036 | adjustGlyphRangeForSingleByteEncoding(glyphID); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1037 | SkGlyphID firstGlyphID = this->firstGlyphID(); |
| 1038 | SkGlyphID lastGlyphID = this->lastGlyphID(); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame] | 1039 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1040 | // glyphCount not including glyph 0 |
| 1041 | unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; |
| 1042 | SkASSERT(glyphCount > 0 && glyphCount <= 255); |
| 1043 | this->insertInt("FirstChar", (size_t)0); |
| 1044 | this->insertInt("LastChar", (size_t)glyphCount); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 1045 | { |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1046 | SkPaint tmpPaint; |
| 1047 | tmpPaint.setHinting(SkPaint::kNo_Hinting); |
| 1048 | tmpPaint.setTypeface(sk_ref_sp(this->typeface())); |
| 1049 | tmpPaint.setTextSize((SkScalar)this->typeface()->getUnitsPerEm()); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1050 | const SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 1051 | SkAutoGlyphCache glyphCache(tmpPaint, &props, nullptr); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1052 | auto widths = sk_make_sp<SkPDFArray>(); |
| 1053 | SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; |
| 1054 | const uint16_t emSize = this->fontInfo()->fEmSize; |
| 1055 | widths->appendScalar(from_font_units(advance, emSize)); |
| 1056 | for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
| 1057 | advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; |
| 1058 | widths->appendScalar(from_font_units(advance, emSize)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1059 | } |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1060 | this->insertObject("Widths", std::move(widths)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1061 | } |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1062 | if (!addFontDescriptor(0)) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1063 | return false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1064 | } |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1065 | auto encDiffs = sk_make_sp<SkPDFArray>(); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 1066 | encDiffs->reserve(lastGlyphID - firstGlyphID + 3); |
| 1067 | encDiffs->appendInt(0); |
| 1068 | const SkTArray<SkString>& glyphNames = this->fontInfo()->fGlyphNames; |
| 1069 | SkASSERT(glyphNames.count() > lastGlyphID); |
| 1070 | encDiffs->appendName(glyphNames[0].c_str()); |
| 1071 | const SkString unknown("UNKNOWN"); |
| 1072 | for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
| 1073 | const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty(); |
| 1074 | const SkString& name = valid ? glyphNames[gID] : unknown; |
| 1075 | encDiffs->appendName(name); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1078 | auto encoding = sk_make_sp<SkPDFDict>("Encoding"); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1079 | encoding->insertObject("Differences", std::move(encDiffs)); |
| 1080 | this->insertObject("Encoding", std::move(encoding)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1081 | return true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1084 | /////////////////////////////////////////////////////////////////////////////// |
| 1085 | // class SkPDFType3Font |
| 1086 | /////////////////////////////////////////////////////////////////////////////// |
| 1087 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1088 | namespace { |
| 1089 | // returns [0, first, first+1, ... last-1, last] |
| 1090 | struct SingleByteGlyphIdIterator { |
| 1091 | SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) |
| 1092 | : fFirst(first), fLast(last) { |
| 1093 | SkASSERT(fFirst > 0); |
| 1094 | SkASSERT(fLast >= first); |
| 1095 | } |
| 1096 | struct Iter { |
| 1097 | void operator++() { |
| 1098 | fCurrent = (0 == fCurrent) ? fFirst : fCurrent + 1; |
| 1099 | } |
| 1100 | // This is an input_iterator |
| 1101 | SkGlyphID operator*() const { return (SkGlyphID)fCurrent; } |
| 1102 | bool operator!=(const Iter& rhs) const { |
| 1103 | return fCurrent != rhs.fCurrent; |
| 1104 | } |
| 1105 | Iter(SkGlyphID f, int c) : fFirst(f), fCurrent(c) {} |
| 1106 | private: |
| 1107 | const SkGlyphID fFirst; |
| 1108 | int fCurrent; // must be int to make fLast+1 to fit |
| 1109 | }; |
| 1110 | Iter begin() const { return Iter(fFirst, 0); } |
| 1111 | Iter end() const { return Iter(fFirst, (int)fLast + 1); } |
| 1112 | private: |
| 1113 | const SkGlyphID fFirst; |
| 1114 | const SkGlyphID fLast; |
| 1115 | }; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1118 | static void add_type3_font_info(SkPDFDict* font, |
| 1119 | SkTypeface* typeface, |
| 1120 | SkScalar emSize, |
| 1121 | const SkPDFGlyphSet* subset, |
| 1122 | SkGlyphID firstGlyphID, |
| 1123 | SkGlyphID lastGlyphID) { |
| 1124 | SkASSERT(lastGlyphID >= firstGlyphID); |
halcanary | 5bf60ad | 2016-08-11 13:59:18 -0700 | [diff] [blame] | 1125 | SkPaint paint; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1126 | paint.setHinting(SkPaint::kNo_Hinting); |
| 1127 | paint.setTypeface(sk_ref_sp(typeface)); |
| 1128 | paint.setTextSize(emSize); |
halcanary | 5bf60ad | 2016-08-11 13:59:18 -0700 | [diff] [blame] | 1129 | const SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1130 | SkAutoGlyphCache cache(paint, &props, nullptr); |
halcanary | 5bf60ad | 2016-08-11 13:59:18 -0700 | [diff] [blame] | 1131 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1132 | font->insertName("Subtype", "Type3"); |
| 1133 | // Flip about the x-axis and scale by 1/emSize. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1134 | SkMatrix fontMatrix; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1135 | fontMatrix.setScale(SkScalarInvert(emSize), -SkScalarInvert(emSize)); |
| 1136 | font->insertObject("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix)); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1137 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1138 | auto charProcs = sk_make_sp<SkPDFDict>(); |
| 1139 | auto encoding = sk_make_sp<SkPDFDict>("Encoding"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1140 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1141 | auto encDiffs = sk_make_sp<SkPDFArray>(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1142 | // length(firstGlyphID .. lastGlyphID) == lastGlyphID - firstGlyphID + 1 |
| 1143 | // plus 1 for glyph 0; |
| 1144 | SkASSERT(firstGlyphID > 0); |
| 1145 | SkASSERT(lastGlyphID >= firstGlyphID); |
| 1146 | int glyphCount = lastGlyphID - firstGlyphID + 2; |
| 1147 | // one other entry for the index of first glyph. |
| 1148 | encDiffs->reserve(glyphCount + 1); |
| 1149 | encDiffs->appendInt(0); // index of first glyph |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1150 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1151 | auto widthArray = sk_make_sp<SkPDFArray>(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1152 | widthArray->reserve(glyphCount); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1153 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1154 | SkIRect bbox = SkIRect::MakeEmpty(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1155 | |
| 1156 | sk_sp<SkPDFStream> emptyStream; |
| 1157 | for (SkGlyphID gID : SingleByteGlyphIdIterator(firstGlyphID, lastGlyphID)) { |
| 1158 | bool skipGlyph = subset && gID != 0 && !subset->has(gID); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1159 | SkString characterName; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1160 | SkScalar advance = 0.0f; |
| 1161 | SkIRect glyphBBox; |
| 1162 | if (skipGlyph) { |
| 1163 | characterName.set("g0"); |
| 1164 | } else { |
| 1165 | characterName.printf("g%X", gID); |
| 1166 | const SkGlyph& glyph = cache->getGlyphIDMetrics(gID); |
| 1167 | advance = SkFloatToScalar(glyph.fAdvanceX); |
| 1168 | glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop, |
| 1169 | glyph.fWidth, glyph.fHeight); |
| 1170 | bbox.join(glyphBBox); |
| 1171 | const SkPath* path = cache->findPath(glyph); |
| 1172 | if (path && !path->isEmpty()) { |
| 1173 | SkDynamicMemoryWStream content; |
| 1174 | setGlyphWidthAndBoundingBox(SkFloatToScalar(glyph.fAdvanceX), glyphBBox, |
| 1175 | &content); |
| 1176 | SkPDFUtils::EmitPath(*path, SkPaint::kFill_Style, &content); |
| 1177 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, path->getFillType(), |
| 1178 | &content); |
| 1179 | charProcs->insertObjRef( |
| 1180 | characterName, sk_make_sp<SkPDFStream>( |
| 1181 | std::unique_ptr<SkStreamAsset>(content.detachAsStream()))); |
| 1182 | } else { |
| 1183 | if (!emptyStream) { |
| 1184 | emptyStream = sk_make_sp<SkPDFStream>( |
| 1185 | std::unique_ptr<SkStreamAsset>( |
| 1186 | new SkMemoryStream((size_t)0))); |
| 1187 | } |
| 1188 | charProcs->insertObjRef(characterName, emptyStream); |
| 1189 | } |
halcanary | 5bf60ad | 2016-08-11 13:59:18 -0700 | [diff] [blame] | 1190 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1191 | encDiffs->appendName(characterName.c_str()); |
| 1192 | widthArray->appendScalar(advance); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1195 | encoding->insertObject("Differences", std::move(encDiffs)); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1196 | font->insertInt("FirstChar", 0); |
| 1197 | font->insertInt("LastChar", lastGlyphID - firstGlyphID + 1); |
| 1198 | /* FontBBox: "A rectangle expressed in the glyph coordinate |
| 1199 | system, specifying the font bounding box. This is the smallest |
| 1200 | rectangle enclosing the shape that would result if all of the |
| 1201 | glyphs of the font were placed with their origins coincident and |
| 1202 | then filled." */ |
| 1203 | auto fontBBox = sk_make_sp<SkPDFArray>(); |
| 1204 | fontBBox->reserve(4); |
| 1205 | fontBBox->appendInt(bbox.left()); |
| 1206 | fontBBox->appendInt(bbox.bottom()); |
| 1207 | fontBBox->appendInt(bbox.right()); |
| 1208 | fontBBox->appendInt(bbox.top()); |
| 1209 | font->insertObject("FontBBox", std::move(fontBBox)); |
| 1210 | font->insertName("CIDToGIDMap", "Identity"); |
| 1211 | sk_sp<const SkAdvancedTypefaceMetrics> metrics; |
| 1212 | if (subset) { |
| 1213 | SkTDArray<uint32_t> subsetList; |
| 1214 | for (SkGlyphID gID : SingleByteGlyphIdIterator(firstGlyphID, lastGlyphID)) { |
| 1215 | if (gID == 0 || subset->has(gID)) { // Always include glyph 0. |
| 1216 | subsetList.push(0); |
| 1217 | } |
| 1218 | } |
| 1219 | subset->exportTo(&subsetList); |
| 1220 | metrics = SkPDFFont::GetFontMetricsWithToUnicode(typeface, subsetList.begin(), |
| 1221 | subsetList.count()); |
| 1222 | } else { |
| 1223 | metrics = SkPDFFont::GetFontMetricsWithToUnicode(typeface, nullptr, 0); |
| 1224 | } |
| 1225 | if (metrics) { |
| 1226 | font->insertObjRef("ToUnicode", |
| 1227 | SkPDFMakeToUnicodeCmap(metrics->fGlyphToUnicode, |
| 1228 | subset, |
| 1229 | false, |
| 1230 | firstGlyphID, |
| 1231 | lastGlyphID)); |
| 1232 | } |
| 1233 | font->insertObject("Widths", std::move(widthArray)); |
| 1234 | font->insertObject("Encoding", std::move(encoding)); |
| 1235 | font->insertObject("CharProcs", std::move(charProcs)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1236 | } |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 1237 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1238 | SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
| 1239 | SkTypeface* typeface, |
| 1240 | uint16_t glyphID) |
| 1241 | : SkPDFFont(info, typeface, nullptr) { |
| 1242 | // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
| 1243 | this->setLastGlyphID(SkToU16(typeface->countGlyphs() - 1)); |
| 1244 | this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
| 1245 | } |
| 1246 | |
| 1247 | sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { |
| 1248 | // All fonts are subset before serialization. |
| 1249 | // TODO(halcanary): all fonts should follow this pattern. |
| 1250 | auto font = sk_make_sp<SkPDFDict>("Font"); |
| 1251 | const SkAdvancedTypefaceMetrics* info = this->fontInfo(); |
| 1252 | uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; |
| 1253 | add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, |
| 1254 | this->firstGlyphID(), this->lastGlyphID()); |
| 1255 | return font; |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | //////////////////////////////////////////////////////////////////////////////// |
| 1260 | |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 1261 | SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, |
| 1262 | uint32_t existingFontID, |
| 1263 | uint16_t existingGlyphID, |
| 1264 | uint32_t searchFontID, |
| 1265 | uint16_t searchGlyphID) { |
| 1266 | if (existingFontID != searchFontID) { |
| 1267 | return SkPDFFont::kNot_Match; |
| 1268 | } |
| 1269 | if (existingGlyphID == 0 || searchGlyphID == 0) { |
| 1270 | return SkPDFFont::kExact_Match; |
| 1271 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1272 | if (existingFont != nullptr) { |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 1273 | return (existingFont->fFirstGlyphID <= searchGlyphID && |
| 1274 | searchGlyphID <= existingFont->fLastGlyphID) |
| 1275 | ? SkPDFFont::kExact_Match |
| 1276 | : SkPDFFont::kRelated_Match; |
| 1277 | } |
| 1278 | return (existingGlyphID == searchGlyphID) ? SkPDFFont::kExact_Match |
| 1279 | : SkPDFFont::kRelated_Match; |
| 1280 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1281 | |
| 1282 | // Since getAdvancedTypefaceMetrics is expensive, cache the result. |
| 1283 | bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame^] | 1284 | SkFontID id = SkTypeface::UniqueID(typeface); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1285 | if (bool* value = canon->fCanEmbedTypeface.find(id)) { |
| 1286 | return *value; |
| 1287 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame^] | 1288 | SkAutoResolveDefaultTypeface face(typeface); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1289 | bool canEmbed = true; |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1290 | sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics( |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1291 | face->getAdvancedTypefaceMetrics( |
| 1292 | SkTypeface::kNo_PerGlyphInfo, nullptr, 0)); |
| 1293 | if (fontMetrics) { |
| 1294 | canEmbed = !SkToBool( |
| 1295 | fontMetrics->fFlags & |
| 1296 | SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); |
| 1297 | } |
| 1298 | return *canon->fCanEmbedTypeface.set(id, canEmbed); |
| 1299 | } |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 1300 | |
| 1301 | void SkPDFFont::drop() { |
| 1302 | fTypeface = nullptr; |
| 1303 | fFontInfo = nullptr; |
| 1304 | fDescriptor = nullptr; |
| 1305 | this->SkPDFDict::drop(); |
| 1306 | } |