| 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 "src/pdf/SkPDFSubsetFont.h" |
| 5 |
|
| 6 |
#if defined(SK_PDF_USE_HARFBUZZ_SUBSET) |
| 7 |
|
| 8 |
#include "include/core/SkStream.h" |
| 9 |
#include "include/core/SkTypeface.h" |
| 10 |
#include "include/private/base/SkAssert.h" |
| 11 |
#include "include/private/base/SkMalloc.h" |
| 12 |
#include "include/private/base/SkTemplates.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/pdf/SkPDFGlyphUse.h" |
| 15 |
|
| 16 |
#include "hb.h" // NO_G3_REWRITE |
| 17 |
#include "hb-subset.h" // NO_G3_REWRITE |
| 18 |
|
| 19 |
#include <cstddef> |
| 20 |
#include <memory> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
using HBBlob = std::unique_ptr<hb_blob_t, SkFunctionObject<hb_blob_destroy>>; |
| 26 |
using HBFace = std::unique_ptr<hb_face_t, SkFunctionObject<hb_face_destroy>>; |
| 27 |
using HBSubsetInput = std::unique_ptr<hb_subset_input_t, SkFunctionObject<hb_subset_input_destroy>>; |
| 28 |
using HBSet = std::unique_ptr<hb_set_t, SkFunctionObject<hb_set_destroy>>; |
| 29 |
|
| 30 |
HBBlob stream_to_blob(std::unique_ptr<SkStreamAsset> asset) { |
| 31 |
size_t size = asset->getLength(); |
| 32 |
HBBlob blob; |
| 33 |
if (const void* base = asset->getMemoryBase()) { |
| 34 |
blob.reset(hb_blob_create(const_cast<char*>(static_cast<const char*>(base)), SkToUInt(size), |
| 35 |
HB_MEMORY_MODE_READONLY, asset.release(), |
| 36 |
[](void* p) { delete (SkStreamAsset*)p; })); |
| 37 |
} else { |
| 38 |
void* ptr = size ? sk_malloc_throw(size) : nullptr; |
| 39 |
asset->read(ptr, size); |
| 40 |
blob.reset(hb_blob_create((char*)ptr, SkToUInt(size), |
| 41 |
HB_MEMORY_MODE_READONLY, ptr, sk_free)); |
| 42 |
} |
| 43 |
SkASSERT(blob); |
| 44 |
hb_blob_make_immutable(blob.get()); |
| 45 |
return blob; |
| 46 |
} |
| 47 |
|
| 48 |
sk_sp<SkData> to_data(HBBlob blob) { |
| 49 |
if (!blob) { |
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
unsigned int length; |
| 53 |
const char* data = hb_blob_get_data(blob.get(), &length); |
| 54 |
if (!data || !length) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
return SkData::MakeWithProc(data, SkToSizeT(length), |
| 58 |
[](const void*, void* ctx) { hb_blob_destroy((hb_blob_t*)ctx); }, |
| 59 |
blob.release()); |
| 60 |
} |
| 61 |
|
| 62 |
HBFace make_subset(hb_subset_input_t* input, hb_face_t* face, bool retainZeroGlyph) { |
| 63 |
// TODO: When possible, check if a font is 'tricky' with FT_IS_TRICKY. |
| 64 |
// If it isn't known if a font is 'tricky', retain the hints. |
| 65 |
unsigned int flags = HB_SUBSET_FLAGS_RETAIN_GIDS; |
| 66 |
if (retainZeroGlyph) { |
| 67 |
flags |= HB_SUBSET_FLAGS_NOTDEF_OUTLINE; |
| 68 |
} |
| 69 |
hb_subset_input_set_flags(input, flags); |
| 70 |
return HBFace(hb_subset_or_fail(face, input)); |
| 71 |
} |
| 72 |
|
| 73 |
sk_sp<SkData> subset_harfbuzz(const SkTypeface& typeface, const SkPDFGlyphUse& glyphUsage) { |
| 74 |
int index = 0; |
| 75 |
std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openStream(&index); |
| 76 |
HBFace face; |
| 77 |
HBBlob blob(stream_to_blob(std::move(typefaceAsset))); |
| 78 |
// hb_face_create always succeeds. Check that the format is minimally recognized first. |
| 79 |
// See https://github.com/harfbuzz/harfbuzz/issues/248 |
| 80 |
unsigned int num_hb_faces = hb_face_count(blob.get()); |
| 81 |
if (0 < num_hb_faces && (unsigned)index < num_hb_faces) { |
| 82 |
face.reset(hb_face_create(blob.get(), (unsigned)index)); |
| 83 |
// Check the number of glyphs as a basic sanitization step. |
| 84 |
if (face && hb_face_get_glyph_count(face.get()) == 0) { |
| 85 |
face.reset(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
HBSubsetInput input(hb_subset_input_create_or_fail()); |
| 90 |
SkASSERT(input); |
| 91 |
if (!face || !input) { |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
hb_set_t* glyphs = hb_subset_input_glyph_set(input.get()); |
| 95 |
glyphUsage.getSetValues([&glyphs](unsigned gid) { hb_set_add(glyphs, gid);}); |
| 96 |
|
| 97 |
HBFace subset = make_subset(input.get(), face.get(), glyphUsage.has(0)); |
| 98 |
if (!subset) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
HBBlob result(hb_face_reference_blob(subset.get())); |
| 103 |
return to_data(std::move(result)); |
| 104 |
} |
| 105 |
|
| 106 |
} // namespace |
| 107 |
|
| 108 |
sk_sp<SkData> SkPDFSubsetFont(const SkTypeface& typeface, const SkPDFGlyphUse& glyphUsage) { |
| 109 |
return subset_harfbuzz(typeface, glyphUsage); |
| 110 |
} |
| 111 |
|
| 112 |
#else |
| 113 |
|
| 114 |
sk_sp<SkData> SkPDFSubsetFont(const SkTypeface&, const SkPDFGlyphUse&) { |
| 115 |
return nullptr; |
| 116 |
} |
| 117 |
|
| 118 |
#endif // defined(SK_PDF_USE_HARFBUZZ_SUBSET) |