1 commit 362a59be47f9e187eec43df0938def661be6c972
2 Author: Jonathan Kew <jkew@mozilla.com>
3 Date: Wed Aug 30 12:55:02 2023 +0000
5 Bug 1850314 - Don't do glyph bounding-box fixup for "tricky" fonts, because it may disrupt glyph rendering on macOS. r=gfx-reviewers,lsalzman
7 Differential Revision: https://phabricator.services.mozilla.com/D187096
9 diff --git a/src/glyf.cc b/src/glyf.cc
10 index 0ed9515ef16d6..31487957bf99b 100644
20 // http://www.microsoft.com/typography/otspec/glyf.htm
21 @@ -97,7 +98,8 @@ bool OpenTypeGLYF::ParseSimpleGlyph(Buffer &glyph,
27 + bool is_tricky_font) {
28 // read the end-points array
29 uint16_t num_flags = 0;
30 for (int i = 0; i < num_contours; ++i) {
31 @@ -219,27 +221,32 @@ bool OpenTypeGLYF::ParseSimpleGlyph(Buffer &glyph,
35 - Warning("Glyph bbox was incorrect; adjusting (glyph %u)", gid);
36 - // copy the numberOfContours field
37 - this->iov.push_back(std::make_pair(glyph.buffer(), 2));
38 - // output a fixed-up version of the bounding box
39 - uint8_t* fixed_bbox = new uint8_t[8];
40 - fixed_bboxes.push_back(fixed_bbox);
41 - xmin = ots_htons(xmin);
42 - std::memcpy(fixed_bbox, &xmin, 2);
43 - ymin = ots_htons(ymin);
44 - std::memcpy(fixed_bbox + 2, &ymin, 2);
45 - xmax = ots_htons(xmax);
46 - std::memcpy(fixed_bbox + 4, &xmax, 2);
47 - ymax = ots_htons(ymax);
48 - std::memcpy(fixed_bbox + 6, &ymax, 2);
49 - this->iov.push_back(std::make_pair(fixed_bbox, 8));
50 - // copy the remainder of the glyph data
51 - this->iov.push_back(std::make_pair(glyph.buffer() + 10, glyph.offset() - 10));
53 - this->iov.push_back(std::make_pair(glyph.buffer(), glyph.offset()));
54 + if (is_tricky_font) {
55 + Warning("Glyph bbox was incorrect; NOT adjusting tricky font (glyph %u)", gid);
57 + Warning("Glyph bbox was incorrect; adjusting (glyph %u)", gid);
58 + // copy the numberOfContours field
59 + this->iov.push_back(std::make_pair(glyph.buffer(), 2));
60 + // output a fixed-up version of the bounding box
61 + uint8_t* fixed_bbox = new uint8_t[8];
62 + fixed_bboxes.push_back(fixed_bbox);
63 + xmin = ots_htons(xmin);
64 + std::memcpy(fixed_bbox, &xmin, 2);
65 + ymin = ots_htons(ymin);
66 + std::memcpy(fixed_bbox + 2, &ymin, 2);
67 + xmax = ots_htons(xmax);
68 + std::memcpy(fixed_bbox + 4, &xmax, 2);
69 + ymax = ots_htons(ymax);
70 + std::memcpy(fixed_bbox + 6, &ymax, 2);
71 + this->iov.push_back(std::make_pair(fixed_bbox, 8));
72 + // copy the remainder of the glyph data
73 + this->iov.push_back(std::make_pair(glyph.buffer() + 10, glyph.offset() - 10));
78 + this->iov.push_back(std::make_pair(glyph.buffer(), glyph.offset()));
83 @@ -342,6 +349,10 @@ bool OpenTypeGLYF::Parse(const uint8_t *data, size_t length) {
84 return Error("Missing maxp or loca or head table needed by glyf table");
87 + OpenTypeNAME *name = static_cast<OpenTypeNAME*>(
88 + GetFont()->GetTypedTable(OTS_TAG_NAME));
89 + bool is_tricky = name->IsTrickyFont();
93 const unsigned num_glyphs = maxp->num_glyphs;
94 @@ -397,7 +408,7 @@ bool OpenTypeGLYF::Parse(const uint8_t *data, size_t length) {
95 // does we will simply ignore it.
97 } else if (num_contours > 0) {
98 - if (!ParseSimpleGlyph(glyph, i, num_contours, xmin, ymin, xmax, ymax)) {
99 + if (!ParseSimpleGlyph(glyph, i, num_contours, xmin, ymin, xmax, ymax, is_tricky)) {
100 return Error("Failed to parse glyph %d", i);
103 diff --git a/src/glyf.h b/src/glyf.h
104 index 05e846f1cb6e8..f85fdc4652fcf 100644
107 @@ -51,7 +51,8 @@ class OpenTypeGLYF : public Table {
113 + bool is_tricky_font);
114 bool ParseCompositeGlyph(
116 ComponentPointCount* component_point_count);
117 diff --git a/src/name.cc b/src/name.cc
118 index fc5074b0587a3..7526e1f72b9ea 100644
121 @@ -366,4 +366,44 @@ bool OpenTypeNAME::IsValidNameId(uint16_t nameID, bool addIfMissing) {
122 return this->name_ids.count(nameID);
125 +// List of font names considered "tricky" (dependent on applying original TrueType instructions) by FreeType, see
126 +// https://gitlab.freedesktop.org/freetype/freetype/-/blob/2d9fce53d4ce89f36075168282fcdd7289e082f9/src/truetype/ttobjs.c#L170-241
127 +static const char* tricky_font_names[] = {
129 + "DFGirl-W6-WIN-BF",
143 + "Ming(for ISO10646)",
150 +bool OpenTypeNAME::IsTrickyFont() const {
151 + for (const auto& name : this->names) {
152 + const uint16_t id = name.name_id;
156 + for (const auto* p : tricky_font_names) {
157 + if (name.text.find(p) != std::string::npos) {
166 diff --git a/src/name.h b/src/name.h
167 index 68c7ac096d3f8..a241e77ee26bb 100644
170 @@ -52,6 +52,7 @@ class OpenTypeNAME : public Table {
171 bool Parse(const uint8_t *data, size_t length);
172 bool Serialize(OTSStream *out);
173 bool IsValidNameId(uint16_t nameID, bool addIfMissing = false);
174 + bool IsTrickyFont() const;
177 std::vector<NameRecord> names;