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 | |
| 8 | #include "sample/chromium/font_subsetter.h" |
| 9 | #include <vector> |
| 10 | |
| 11 | sk_sp<SkData> SkPDFSubsetFont(sk_sp<SkData> fontData, |
| 12 | const SkPDFGlyphUse& glyphUsage, |
| 13 | const char* fontName, |
| 14 | int ttcIndex) { |
| 15 | // Generate glyph id array in format needed by sfntly. |
| 16 | // TODO(halcanary): sfntly should take a more compact format. |
| 17 | std::vector<unsigned> subset; |
| 18 | if (!glyphUsage.has(0)) { |
| 19 | subset.push_back(0); // Always include glyph 0. |
| 20 | } |
| 21 | glyphUsage.getSetValues([&subset](unsigned v) { subset.push_back(v); }); |
| 22 | |
| 23 | unsigned char* subsetFont{nullptr}; |
| 24 | #if defined(SK_BUILD_FOR_GOOGLE3) |
| 25 | // TODO(halcanary): update SK_BUILD_FOR_GOOGLE3 to newest version of Sfntly. |
| 26 | (void)ttcIndex; |
| 27 | int subsetFontSize = SfntlyWrapper::SubsetFont(fontName, |
| 28 | fontData->bytes(), |
| 29 | fontData->size(), |
| 30 | subset.data(), |
| 31 | subset.size(), |
| 32 | &subsetFont); |
| 33 | #else |
| 34 | (void)fontName; |
| 35 | int subsetFontSize = SfntlyWrapper::SubsetFont(ttcIndex, |
| 36 | fontData->bytes(), |
| 37 | fontData->size(), |
| 38 | subset.data(), |
| 39 | subset.size(), |
| 40 | &subsetFont); |
| 41 | #endif |
| 42 | SkASSERT(subsetFontSize > 0 || subsetFont == nullptr); |
| 43 | if (subsetFontSize < 1 || subsetFont == nullptr) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | return SkData::MakeWithProc(subsetFont, subsetFontSize, |
| 47 | [](const void* p, void*) { delete[] (unsigned char*)p; }, |
| 48 | nullptr); |
| 49 | } |
| 50 | #endif |