Hal Canary | 42d6c59 | 2018-11-15 14:01:49 -0500 | [diff] [blame] | 1 | // Copyright 2018 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | |
| 4 | #include "SkPDFSubsetFont.h" |
| 5 | |
| 6 | #if defined(SK_PDF_USE_SFNTLY) |
| 7 | |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 8 | #if defined(SK_USING_THIRD_PARTY_ICU) |
Hal Canary | 32498f0 | 2019-02-04 15:36:31 -0500 | [diff] [blame] | 9 | #include "SkLoadICU.h" |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 10 | #endif |
Hal Canary | 32498f0 | 2019-02-04 15:36:31 -0500 | [diff] [blame] | 11 | |
Hal Canary | 42d6c59 | 2018-11-15 14:01:49 -0500 | [diff] [blame] | 12 | #include "sample/chromium/font_subsetter.h" |
| 13 | #include <vector> |
| 14 | |
| 15 | sk_sp<SkData> SkPDFSubsetFont(sk_sp<SkData> fontData, |
| 16 | const SkPDFGlyphUse& glyphUsage, |
| 17 | const char* fontName, |
| 18 | int ttcIndex) { |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 19 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 20 | if (!SkLoadICU()) { |
| 21 | return nullptr; |
| 22 | } |
| 23 | #endif |
Hal Canary | 42d6c59 | 2018-11-15 14:01:49 -0500 | [diff] [blame] | 24 | // Generate glyph id array in format needed by sfntly. |
| 25 | // TODO(halcanary): sfntly should take a more compact format. |
| 26 | std::vector<unsigned> subset; |
| 27 | if (!glyphUsage.has(0)) { |
| 28 | subset.push_back(0); // Always include glyph 0. |
| 29 | } |
| 30 | glyphUsage.getSetValues([&subset](unsigned v) { subset.push_back(v); }); |
| 31 | |
| 32 | unsigned char* subsetFont{nullptr}; |
| 33 | #if defined(SK_BUILD_FOR_GOOGLE3) |
| 34 | // TODO(halcanary): update SK_BUILD_FOR_GOOGLE3 to newest version of Sfntly. |
| 35 | (void)ttcIndex; |
| 36 | int subsetFontSize = SfntlyWrapper::SubsetFont(fontName, |
| 37 | fontData->bytes(), |
| 38 | fontData->size(), |
| 39 | subset.data(), |
| 40 | subset.size(), |
| 41 | &subsetFont); |
| 42 | #else |
| 43 | (void)fontName; |
| 44 | int subsetFontSize = SfntlyWrapper::SubsetFont(ttcIndex, |
| 45 | fontData->bytes(), |
| 46 | fontData->size(), |
| 47 | subset.data(), |
| 48 | subset.size(), |
| 49 | &subsetFont); |
| 50 | #endif |
| 51 | SkASSERT(subsetFontSize > 0 || subsetFont == nullptr); |
| 52 | if (subsetFontSize < 1 || subsetFont == nullptr) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | return SkData::MakeWithProc(subsetFont, subsetFontSize, |
| 56 | [](const void* p, void*) { delete[] (unsigned char*)p; }, |
| 57 | nullptr); |
| 58 | } |
| 59 | #endif |