Skip to content

Commit 2e82372

Browse files
committed
Add charstring offsets when needed in IftTable::AddToFont.
1 parent 8085f9b commit 2e82372

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

ift/proto/format_2_patch_map_test.cc

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,29 @@ class Format2PatchMapTest : public ::testing::Test {
1818
Format2PatchMapTest() {}
1919
};
2020

21-
static std::string HeaderSimple(uint8_t entry_count = 1, uint8_t offset_delta = 0) {
22-
std::string part1 {
23-
0x02, // format
24-
0x00, 0x00, 0x00,
25-
0x00, // reserved
26-
0x00, 0x00, 0x00,
27-
0x01, 0x00, 0x00,
28-
0x00, 0x02, 0x00,
29-
0x00, 0x00, 0x03,
30-
0x00, 0x00, 0x00,
31-
0x04, // compat id
32-
0x01, // default format = Table Keyed Full
33-
0x00, 0x00, (char)entry_count, // entry count
34-
0x00, 0x00, 0x00};
35-
36-
std::string part2 {
37-
0x00, 0x00, 0x00,
38-
0x00, // entry id string data offset
39-
0x00, 0x06, // uri template length
40-
0x66, 0x6f, 0x6f,
41-
0x2f, 0x24, 0x31,
21+
static std::string HeaderSimple(uint8_t entry_count = 1,
22+
uint8_t offset_delta = 0) {
23+
std::string part1{0x02, // format
24+
0x00, 0x00, 0x00,
25+
0x00, // reserved
26+
0x00, 0x00, 0x00,
27+
0x01, 0x00, 0x00,
28+
0x00, 0x02, 0x00,
29+
0x00, 0x00, 0x03,
30+
0x00, 0x00, 0x00,
31+
0x04, // compat id
32+
0x01, // default format = Table Keyed Full
33+
0x00, 0x00, (char)entry_count, // entry count
34+
0x00, 0x00, 0x00};
35+
36+
std::string part2{
37+
0x00, 0x00, 0x00,
38+
0x00, // entry id string data offset
39+
0x00, 0x06, // uri template length
40+
0x66, 0x6f, 0x6f, 0x2f, 0x24, 0x31,
4241
};
4342

44-
part1 += (char) ((uint8_t) 0x29 + offset_delta);
43+
part1 += (char)((uint8_t)0x29 + offset_delta);
4544
part1 += part2;
4645
return part1;
4746
}
@@ -80,9 +79,7 @@ TEST_F(Format2PatchMapTest, Simple_WithCffOffset) {
8079
auto encoded = Format2PatchMap::Serialize(table, 0x7856, std::nullopt);
8180
ASSERT_TRUE(encoded.ok()) << encoded.status();
8281

83-
std::string cff_offset = {
84-
0x00, 0x00, 0x78, 0x56
85-
};
82+
std::string cff_offset = {0x00, 0x00, 0x78, 0x56};
8683

8784
std::string entry_0 = {
8885
// entry 0
@@ -105,9 +102,7 @@ TEST_F(Format2PatchMapTest, Simple_WithCff2Offset) {
105102
auto encoded = Format2PatchMap::Serialize(table, std::nullopt, 0x127856);
106103
ASSERT_TRUE(encoded.ok()) << encoded.status();
107104

108-
std::string cff2_offset = {
109-
0x00, 0x12, 0x78, 0x56
110-
};
105+
std::string cff2_offset = {0x00, 0x12, 0x78, 0x56};
111106

112107
std::string entry_0 = {
113108
// entry 0
@@ -131,8 +126,7 @@ TEST_F(Format2PatchMapTest, Simple_WithCffAndCff2Offset) {
131126
ASSERT_TRUE(encoded.ok()) << encoded.status();
132127

133128
std::string offsets = {
134-
0x00, 0x00, 0x01, 0x23,
135-
0x00, 0x00, 0x04, 0x56,
129+
0x00, 0x00, 0x01, 0x23, 0x00, 0x00, 0x04, 0x56,
136130
};
137131

138132
std::string entry_0 = {

ift/proto/ift_table.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "common/font_helper.h"
1515
#include "common/hb_set_unique_ptr.h"
1616
#include "common/sparse_bit_set.h"
17+
#include "common/try.h"
1718
#include "hb.h"
1819
#include "ift/proto/format_2_patch_map.h"
1920

@@ -100,10 +101,11 @@ StatusOr<FontData> IFTTable::AddToFont(
100101
absl::StatusOr<common::FontData> IFTTable::AddToFont(
101102
hb_face_t* face, const IFTTable& main,
102103
std::optional<const IFTTable*> extension) {
103-
// TODO XXXXX check for CFF/CFF2 and set offset as needed. Only set on the
104-
// main table.
105-
auto main_bytes =
106-
Format2PatchMap::Serialize(main, std::nullopt, std::nullopt);
104+
auto cff_charstrings_offset = TRY(FontHelper::CffCharStringsOffset(face));
105+
auto cff2_charstrings_offset = TRY(FontHelper::Cff2CharStringsOffset(face));
106+
107+
auto main_bytes = Format2PatchMap::Serialize(main, cff_charstrings_offset,
108+
cff2_charstrings_offset);
107109
if (!main_bytes.ok()) {
108110
return main_bytes.status();
109111
}

ift/proto/ift_table_test.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace ift::proto {
3636

3737
class IFTTableTest : public ::testing::Test {
3838
protected:
39-
IFTTableTest() : roboto_ab(make_hb_face(nullptr)) {
39+
IFTTableTest()
40+
: roboto_ab(make_hb_face(nullptr)), noto_sans_jp(make_hb_face(nullptr)) {
4041
sample.SetUrlTemplate("fonts/go/here");
4142
sample.SetId({1, 2, 3, 4});
4243
auto sc = sample.GetPatchMap().AddEntry({30, 32}, 1, TABLE_KEYED_PARTIAL);
@@ -63,9 +64,14 @@ class IFTTableTest : public ::testing::Test {
6364
hb_blob_unique_ptr blob = make_hb_blob(
6465
hb_blob_create_from_file("common/testdata/Roboto-Regular.ab.ttf"));
6566
roboto_ab = make_hb_face(hb_face_create(blob.get(), 0));
67+
68+
blob = make_hb_blob(
69+
hb_blob_create_from_file("common/testdata/NotoSansJP-Regular.otf"));
70+
noto_sans_jp = make_hb_face(hb_face_create(blob.get(), 0));
6671
}
6772

6873
hb_face_unique_ptr roboto_ab;
74+
hb_face_unique_ptr noto_sans_jp;
6975
IFTTable empty;
7076
IFTTable sample;
7177
IFTTable sample_with_extensions;
@@ -131,6 +137,25 @@ TEST_F(IFTTableTest, AddToFont_WithExtension) {
131137
EXPECT_EQ(original_tag_order, new_tag_order);
132138
}
133139

140+
TEST_F(IFTTableTest, AddToFont_WithExtensionAndCharStringsOffset) {
141+
auto font =
142+
IFTTable::AddToFont(noto_sans_jp.get(), sample, &sample_with_extensions);
143+
ASSERT_TRUE(font.ok()) << font.status();
144+
hb_face_unique_ptr face = font->face();
145+
146+
FontData ift_table =
147+
FontHelper::TableData(face.get(), HB_TAG('I', 'F', 'T', ' '));
148+
FontData iftx_table =
149+
FontHelper::TableData(face.get(), HB_TAG('I', 'F', 'T', 'X'));
150+
151+
FontData expected_ift(
152+
*Format2PatchMap::Serialize(sample, 0xa7ed, std::nullopt));
153+
FontData expected_iftx(*Format2PatchMap::Serialize(
154+
sample_with_extensions, std::nullopt, std::nullopt));
155+
ASSERT_EQ(ift_table, expected_ift);
156+
ASSERT_EQ(iftx_table, expected_iftx);
157+
}
158+
134159
TEST_F(IFTTableTest, GetId) { ASSERT_EQ(sample.GetId(), CompatId(1, 2, 3, 4)); }
135160

136161
TEST_F(IFTTableTest, GetId_None) { ASSERT_EQ(empty.GetId(), CompatId()); }

0 commit comments

Comments
 (0)