Skip to content

Commit d8d5b80

Browse files
committed
Add FontHelper::Cff2GetCharstrings.
1 parent 9495896 commit d8d5b80

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ http_archive(
3737
http_archive(
3838
name = "harfbuzz",
3939
build_file = "//third_party:harfbuzz.BUILD",
40-
integrity = "sha256-7+uXin3Ov76LIARwi104FtmjW3ZIB0DlFiHnliAGVJ8=",
41-
strip_prefix = "harfbuzz-0752e5852be6f98f2a9e8d0f8020db3654739860",
42-
urls = ["https://github.com/harfbuzz/harfbuzz/archive/0752e5852be6f98f2a9e8d0f8020db3654739860.zip"],
40+
integrity = "sha256-yNAzHL3p++Giyd3IKGYH/8Gn5YGbh2j2h/uyeWD/GtE=",
41+
strip_prefix = "harfbuzz-a1e587b75acfa91b8756c21e5305af53fb2114af",
42+
urls = ["https://github.com/harfbuzz/harfbuzz/archive/a1e587b75acfa91b8756c21e5305af53fb2114af.zip"],
4343
)
4444

4545
# URI Templates

common/font_helper.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using absl::btree_set;
1717
using absl::flat_hash_map;
18+
using absl::Status;
1819
using absl::StatusOr;
1920
using absl::StrCat;
2021
using absl::string_view;
@@ -122,6 +123,33 @@ FontData FontHelper::Cff2Data(hb_face_t* face, uint32_t gid) {
122123
return data;
123124
}
124125

126+
Status FontHelper::Cff2GetCharstrings(hb_face_t* face,
127+
FontData& non_charstrings,
128+
FontData& charstrings) {
129+
FontData cff2_data = FontHelper::TableData(face, FontHelper::kCFF2);
130+
hb_blob_unique_ptr cff2_data_blob = cff2_data.blob();
131+
132+
hb_blob_unique_ptr charstrings_index_blob =
133+
make_hb_blob(hb_subset_cff2_get_charstrings_index(face));
134+
charstrings.set(charstrings_index_blob.get());
135+
136+
const char* cff2_start = cff2_data.data();
137+
const char* charstrings_start = charstrings.data();
138+
if (charstrings_start < cff2_start) {
139+
return absl::InternalError("CharStrings is not after CFF2 start.");
140+
}
141+
uint64_t non_charstrings_length = (uint64_t)(charstrings_start - cff2_start);
142+
if (non_charstrings_length > cff2_data.size()) {
143+
return absl::InternalError("Non CharStrings data is too large.");
144+
}
145+
146+
hb_blob_unique_ptr non_charstrings_blob = make_hb_blob(
147+
hb_blob_create_sub_blob(cff2_data_blob.get(), 0, non_charstrings_length));
148+
non_charstrings.set(non_charstrings_blob.get());
149+
150+
return absl::OkStatus();
151+
}
152+
125153
StatusOr<uint32_t> FontHelper::GvarSharedTupleCount(const hb_face_t* face) {
126154
auto gvar = TableData(face, kGvar);
127155
if (gvar.empty()) {

common/font_helper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class FontHelper {
144144

145145
static FontData Cff2Data(hb_face_t* face, uint32_t gid);
146146

147+
static absl::Status Cff2GetCharstrings(hb_face_t* face,
148+
FontData& non_charstrings,
149+
FontData& charstrings);
150+
147151
static absl::StatusOr<uint32_t> GvarSharedTupleCount(const hb_face_t* face);
148152

149153
static absl::StatusOr<absl::string_view> Loca(const hb_face_t* face) {

common/font_helper_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,23 @@ TEST_F(FontHelperTest, Cff2Data) {
404404
ASSERT_EQ(data.size(), 0);
405405
}
406406

407+
TEST_F(FontHelperTest, Cff2GetCharstrings) {
408+
FontData noncharstrings;
409+
FontData charstrings;
410+
auto status = FontHelper::Cff2GetCharstrings(noto_sans_vf_jp_otf.get(),
411+
noncharstrings, charstrings);
412+
ASSERT_TRUE(status.ok()) << status;
413+
414+
uint32_t charstrings_offset = 0x8f; // pulled manually from the font file.
415+
auto cff2 =
416+
FontHelper::TableData(noto_sans_vf_jp_otf.get(), FontHelper::kCFF2);
417+
auto expected_noncharstrings = cff2.str(0, charstrings_offset);
418+
auto expected_charstrings = cff2.str(charstrings_offset);
419+
420+
ASSERT_EQ(expected_noncharstrings, noncharstrings.str());
421+
ASSERT_EQ(expected_charstrings, charstrings.str());
422+
}
423+
407424
TEST_F(FontHelperTest, GvarSharedTupleCount) {
408425
auto count = FontHelper::GvarSharedTupleCount(roboto_vf.get());
409426
ASSERT_TRUE(count.ok()) << count.status();

0 commit comments

Comments
 (0)