blob: a52cbbb47009791fa9d0c5761fcd9b7a6893b451 [file] [log] [blame]
Hal Canary42d6c592018-11-15 14:01:49 -05001// 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 Canary61021922019-02-06 12:29:11 -05008#if defined(SK_USING_THIRD_PARTY_ICU)
Hal Canary32498f02019-02-04 15:36:31 -05009#include "SkLoadICU.h"
Hal Canary61021922019-02-06 12:29:11 -050010#endif
Hal Canary32498f02019-02-04 15:36:31 -050011
Hal Canary42d6c592018-11-15 14:01:49 -050012#include "sample/chromium/font_subsetter.h"
13#include <vector>
14
15sk_sp<SkData> SkPDFSubsetFont(sk_sp<SkData> fontData,
16 const SkPDFGlyphUse& glyphUsage,
17 const char* fontName,
18 int ttcIndex) {
Hal Canary61021922019-02-06 12:29:11 -050019#if defined(SK_USING_THIRD_PARTY_ICU)
20 if (!SkLoadICU()) {
21 return nullptr;
22 }
23#endif
Hal Canary42d6c592018-11-15 14:01:49 -050024 // 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