From f81578fd86455eb8eba3bd586d604b83aa55ed56 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 13 Feb 2022 19:15:36 -0600 Subject: [PATCH 1/2] [ot-layout] Synthesize missing baselines Add a variation of hb_ot_layout_get_baseline that synthesizes missing baselines, using heuristics in part taken from the CSS Inline Layout Module, Level 3. Includes some new tests for synthesized baselines. The base2.ttf is a subset of Noto Sans Bengali that includes just the Bengali Ka. New API: hb_ot_layout_get_baseline_with_fallback --- docs/harfbuzz-sections.txt | 1 + src/hb-ot-layout.cc | 184 ++++++++++++++++++++++++++++++++++++- src/hb-ot-layout.h | 8 ++ test/api/fonts/base2.ttf | Bin 0 -> 5236 bytes test/api/test-baseline.c | 53 +++++++++++ 5 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 test/api/fonts/base2.ttf diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 9263994b0..f81058b53 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -537,6 +537,7 @@ hb_ot_layout_feature_with_variations_get_lookups hb_ot_layout_get_attach_points hb_ot_layout_get_horizontal_baseline_tag_for_script hb_ot_layout_get_baseline +hb_ot_layout_get_baseline_with_fallback hb_ot_layout_get_glyph_class hb_ot_layout_get_glyphs_in_class hb_ot_layout_get_ligature_carets diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index aaabc687d..0d556aa5b 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -1979,6 +1979,7 @@ hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c, hb_ot_layout_baseline_tag_t hb_ot_layout_get_horizontal_baseline_tag_for_script (hb_script_t script) { + /* Keep in sync with hb_ot_layout_get_baseline_with_fallback */ switch ((int) script) { /* Unicode-1.1 additions */ @@ -2043,7 +2044,7 @@ hb_ot_layout_get_horizontal_baseline_tag_for_script (hb_script_t script) * @direction: text direction. * @script_tag: script tag. * @language_tag: language tag, currently unused. - * @coord: (out): baseline value if found. + * @coord: (out) (nullable): baseline value if found. * * Fetches a baseline value from the face. * @@ -2066,6 +2067,187 @@ hb_ot_layout_get_baseline (hb_font_t *font, return result; } + +/** + * hb_ot_layout_get_baseline_with_fallback: + * @font: a font + * @baseline_tag: a baseline tag + * @direction: text direction. + * @script_tag: script tag. + * @language_tag: language tag, currently unused. + * @coord: (out): baseline value if found. + * + * Fetches a baseline value from the face, and synthesizes + * it if the font does not have it. + * + * Since: REPLACEME + **/ +void +hb_ot_layout_get_baseline_with_fallback (hb_font_t *font, + hb_ot_layout_baseline_tag_t baseline_tag, + hb_direction_t direction, + hb_tag_t script_tag, + hb_tag_t language_tag, + hb_position_t *coord /* OUT */) +{ + if (hb_ot_layout_get_baseline (font, + baseline_tag, + direction, + script_tag, + language_tag, + coord)) + return; + + /* Synthesize missing baselines. + * See https://www.w3.org/TR/css-inline-3/#baseline-synthesis-fonts + */ + switch (baseline_tag) + { + case HB_OT_LAYOUT_BASELINE_TAG_ROMAN: + *coord = 0; // FIXME origin ? + break; + + case HB_OT_LAYOUT_BASELINE_TAG_MATH: + { + hb_codepoint_t glyph; + hb_glyph_extents_t extents; + if (HB_DIRECTION_IS_HORIZONTAL (direction) && + (hb_font_get_nominal_glyph (font, 0x2212u, &glyph) || + hb_font_get_nominal_glyph (font, '-', &glyph)) && + hb_font_get_glyph_extents (font, glyph, &extents)) + { + *coord = extents.y_bearing + extents.height / 2; + } + else + { + hb_position_t x_height = 0; + hb_ot_metrics_get_position (font, HB_OT_METRICS_TAG_X_HEIGHT, &x_height); + *coord = x_height / 2; + } + } + break; + + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT: + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT: + { + hb_position_t embox_top, embox_bottom; + + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT, + direction, + script_tag, + language_tag, + &embox_top); + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, + direction, + script_tag, + language_tag, + &embox_bottom); + + if (baseline_tag == HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT) + *coord = embox_top + (embox_bottom - embox_top) / 10; + else + *coord = embox_bottom + (embox_top - embox_bottom) / 10; + } + break; + + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT: + if (hb_ot_layout_get_baseline (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, + direction, + script_tag, + language_tag, + coord)) + *coord += HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale; + else + { + hb_font_extents_t font_extents; + hb_font_get_extents_for_direction (font, direction, &font_extents); + *coord = font_extents.ascender; + } + break; + + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT: + if (hb_ot_layout_get_baseline (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT, + direction, + script_tag, + language_tag, + coord)) + *coord -= HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale; + else + { + hb_font_extents_t font_extents; + hb_font_get_extents_for_direction (font, direction, &font_extents); + *coord = font_extents.descender; + } + break; + + case HB_OT_LAYOUT_BASELINE_TAG_HANGING: + if (HB_DIRECTION_IS_HORIZONTAL (direction)) + { + hb_codepoint_t ch; + hb_codepoint_t glyph; + hb_glyph_extents_t extents; + + /* Keep in sync with hb_ot_layout_get_horizontal_baseline_for_script */ + switch ((int) script_tag) + { + /* Unicode-1.1 additions */ + case HB_SCRIPT_BENGALI: ch = 0x0995u; break; + case HB_SCRIPT_DEVANAGARI: ch = 0x0915u; break; + case HB_SCRIPT_GUJARATI: ch = 0x0a95u; break; + case HB_SCRIPT_GURMUKHI: ch = 0x0a15u; break; + /* Unicode-2.0 additions */ + case HB_SCRIPT_TIBETAN: ch = 0x0f40u; break; + /* Unicode-4.0 additions */ + case HB_SCRIPT_LIMBU: ch = 0x1901u; break; + /* Unicode-4.1 additions */ + case HB_SCRIPT_SYLOTI_NAGRI: ch = 0xa807u; break; + /* Unicode-5.0 additions */ + case HB_SCRIPT_PHAGS_PA: ch = 0xa840u; break; + /* Unicode-5.2 additions */ + case HB_SCRIPT_MEETEI_MAYEK: ch = 0xabc0u; break; + /* Unicode-6.1 additions */ + case HB_SCRIPT_SHARADA: ch = 0x11191u; break; + case HB_SCRIPT_TAKRI: ch = 0x1168cu; break; + /* Unicode-7.0 additions */ + case HB_SCRIPT_MODI: ch = 0x1160eu;break; + case HB_SCRIPT_SIDDHAM: ch = 0x11590u; break; + case HB_SCRIPT_TIRHUTA: ch = 0x1148fu; break; + /* Unicode-9.0 additions */ + case HB_SCRIPT_MARCHEN: ch = 0x11c72u; break; + case HB_SCRIPT_NEWA: ch = 0x1140eu; break; + /* Unicode-10.0 additions */ + case HB_SCRIPT_SOYOMBO: ch = 0x11a5cu; break; + case HB_SCRIPT_ZANABAZAR_SQUARE: ch = 0x11a0bu; break; + /* Unicode-11.0 additions */ + case HB_SCRIPT_DOGRA: ch = 0x1180au; break; + case HB_SCRIPT_GUNJALA_GONDI: ch = 0x11d6cu; break; + /* Unicode-12.0 additions */ + case HB_SCRIPT_NANDINAGARI: ch = 0x119b0u; break; + default: ch = 0; break; + } + + if (ch && + hb_font_get_nominal_glyph (font, ch, &glyph) && + hb_font_get_glyph_extents (font, glyph, &extents)) + *coord = extents.y_bearing; + else + *coord = font->y_scale * 6 / 10; // FIXME makes assumptions about origin + } + else + *coord = font->x_scale * 6 / 10; // FIXME makes assumptions about origin + break; + + case _HB_OT_LAYOUT_BASELINE_TAG_MAX_VALUE: + default: + *coord = 0; + break; + } +} + #endif diff --git a/src/hb-ot-layout.h b/src/hb-ot-layout.h index edbb7b878..22ba7c4ee 100644 --- a/src/hb-ot-layout.h +++ b/src/hb-ot-layout.h @@ -522,6 +522,14 @@ hb_ot_layout_get_baseline (hb_font_t *font, hb_tag_t language_tag, hb_position_t *coord /* OUT. May be NULL. */); +HB_EXTERN void +hb_ot_layout_get_baseline_with_fallback (hb_font_t *font, + hb_ot_layout_baseline_tag_t baseline_tag, + hb_direction_t direction, + hb_tag_t script_tag, + hb_tag_t language_tag, + hb_position_t *coord /* OUT */); + HB_END_DECLS #endif /* HB_OT_LAYOUT_H */ diff --git a/test/api/fonts/base2.ttf b/test/api/fonts/base2.ttf new file mode 100644 index 0000000000000000000000000000000000000000..5cb0d35ba5e25b6a4facdf24035e60b21bb1fede GIT binary patch literal 5236 zcmbVQTW=f36+XLMQdjF@iLz|U&d{riBQ9xKwqzxe9c7mkEh~;G$xe2i#9oTJxshbW zR$RH)v}sz0L4u&DfIOrrilTYQLoqAWsNKBeB|wX&4=Ea;K>GuVycP{wpt1X%U5b?) zt3}EMcfK>{o0)T)8A_xOQ6pX&HDuG75vnJZNEt?Nb2fK&lA=@%`55Hd?Bsd#w;z2B zvT_dcfwPl`dtdm=oJpkKgzOd;^UG9Q^;hWs1o>Fu`ih9W+wgnn(~!;C<+;Uo`geas z6nU1YE;^ULwv7G;@Y~T=&Mn-S?b`RodqknPA%8kQlb>#S{zd`gYa#c~qa$*Qu%7Zu z$nEosE34s`2(~B&k6*Y_$kU5Jq_p7`S6kcq9nQjD)9`>_wFaO3_h$>G@jQ!4=Epx= zS1;8M{hca890&U3<@bKh?Vo?~+FPHWy1NkmB(w%G;;+Dpoj>C=PFc!^-c*;=C5ndD zf|go?M^$NoznDN%?CZjRe>_@>SBblN+_^6g3rIw&N> zXu~%P=X@p<{JFJXZPe7d?%T?}SETm3r&4+&JRPAl?-&)eMxi6{p@Vwp{BU)u`(UiK zv7$m79h0p&dw4H9ii%kzRc$^KRze}oB~9C{JsE>e4fdr^*s61es~Q{kwzMCORJ0|O z=14?8*x7ZwrR8XEf8Vjru0}njc6IglAMcC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qKv$rM)WK%%2_u)A}#5hof1R1+1YwE=)|PuRLDpN zq*18KKVpI;ezEdgy_79n0ZIAT=4yWQOX+g?m(KkzZ>D${=SPa8I3o90b}oj?RSY>P zuSaRdzI}X~b|RMBrZu-|H^*+B@BR^%As5}JjEOaPa7LJ3oRfU2$wvNV{9lv-vywc?-}MPtbLYwK6;LMKDx*}A6?=# zGoZP=CWAb$$zY1tWKdwvDS&C_FqmNugIVSfchQ6gB_= literal 0 HcmV?d00001 diff --git a/test/api/test-baseline.c b/test/api/test-baseline.c index 982c68311..9f9542ea4 100644 --- a/test/api/test-baseline.c +++ b/test/api/test-baseline.c @@ -41,6 +41,58 @@ test_ot_layout_base (void) &position)); g_assert_cmpint (46, ==, position); + g_assert (!hb_ot_layout_get_baseline (font, HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT, HB_DIRECTION_TTB, + HB_TAG ('h','a','n','i'), + HB_TAG ('E','N','G',' '), + &position)); + + hb_font_destroy (font); + hb_face_destroy (face); +} + +static void +test_ot_layout_base_with_fallback (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/base.ttf"); + hb_font_t *font = hb_font_create (face); + + hb_position_t position; + hb_ot_layout_get_baseline_with_fallback (font, HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT, HB_DIRECTION_TTB, + HB_TAG ('h','a','n','i'), + HB_TAG ('E','N','G',' '), + &position); + g_assert_cmpint (46, ==, position); + + hb_ot_layout_get_baseline_with_fallback (font, HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT, HB_DIRECTION_TTB, + HB_TAG ('h','a','n','i'), + HB_TAG ('E','N','G',' '), + &position); + g_assert_cmpint (1000, ==, position); + + hb_ot_layout_get_baseline_with_fallback (font, HB_OT_LAYOUT_BASELINE_TAG_MATH, HB_DIRECTION_LTR, + HB_TAG ('h','a','n','i'), + HB_TAG ('E','N','G',' '), + &position); + g_assert_cmpint (271, ==, position); + + hb_font_destroy (font); + hb_face_destroy (face); + + face = hb_test_open_font_file ("fonts/base2.ttf"); + font = hb_font_create (face); + + hb_ot_layout_get_baseline_with_fallback (font, HB_OT_LAYOUT_BASELINE_TAG_HANGING, HB_DIRECTION_LTR, + HB_SCRIPT_BENGALI, + HB_TAG ('E','N','G',' '), + &position); + g_assert_cmpint (622, ==, position); + + hb_ot_layout_get_baseline_with_fallback (font, HB_OT_LAYOUT_BASELINE_TAG_HANGING, HB_DIRECTION_LTR, + HB_SCRIPT_TIBETAN, + HB_TAG ('E','N','G',' '), + &position); + g_assert_cmpint (600, ==, position); + hb_font_destroy (font); hb_face_destroy (face); } @@ -51,6 +103,7 @@ main (int argc, char **argv) hb_test_init (&argc, &argv); hb_test_add (test_ot_layout_base); + hb_test_add (test_ot_layout_base_with_fallback); return hb_test_run(); } From 95bb2ff7198e4811b1a712c8c6dfbae29e453d49 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 16 Feb 2022 13:10:52 -0600 Subject: [PATCH 2/2] [ot-layout] Add central baselines Add HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL and HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL which are the centers of the ideographic em-box and face box. --- src/hb-ot-layout.cc | 40 ++++++++++++++++++++++++++++++++++++++++ src/hb-ot-layout.h | 4 ++++ 2 files changed, 44 insertions(+) diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 0d556aa5b..25836d7c5 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -2241,6 +2241,46 @@ hb_ot_layout_get_baseline_with_fallback (hb_font_t *font, *coord = font->x_scale * 6 / 10; // FIXME makes assumptions about origin break; + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL: + { + hb_position_t top, bottom; + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT, + direction, + script_tag, + language_tag, + &top); + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, + direction, + script_tag, + language_tag, + &bottom); + *coord = (top + bottom) / 2; + + } + break; + + case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL: + { + hb_position_t top, bottom; + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT, + direction, + script_tag, + language_tag, + &top); + hb_ot_layout_get_baseline_with_fallback (font, + HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT, + direction, + script_tag, + language_tag, + &bottom); + *coord = (top + bottom) / 2; + + } + break; + case _HB_OT_LAYOUT_BASELINE_TAG_MAX_VALUE: default: *coord = 0; diff --git a/src/hb-ot-layout.h b/src/hb-ot-layout.h index 22ba7c4ee..ca94b5917 100644 --- a/src/hb-ot-layout.h +++ b/src/hb-ot-layout.h @@ -487,9 +487,11 @@ hb_ot_layout_feature_get_characters (hb_face_t *face, * if the direction is horizontal or vertical, respectively. * @HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT: Ideographic character face top or right edge, * if the direction is horizontal or vertical, respectively. + * @HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL: The center of the ideographic character face. Since: REPLACEME * @HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT: Ideographic em-box bottom or left edge, * if the direction is horizontal or vertical, respectively. * @HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT: Ideographic em-box top or right edge baseline, + * @HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL: The center of the ideographic em-box. Since: REPLACEME * if the direction is horizontal or vertical, respectively. * @HB_OT_LAYOUT_BASELINE_TAG_MATH: The baseline about which mathematical characters are centered. * In vertical writing mode when mathematical characters rotated 90 degrees clockwise, are centered. @@ -503,8 +505,10 @@ typedef enum { HB_OT_LAYOUT_BASELINE_TAG_HANGING = HB_TAG ('h','a','n','g'), HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT = HB_TAG ('i','c','f','b'), HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT = HB_TAG ('i','c','f','t'), + HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL = HB_TAG ('I','c','f','c'), HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT = HB_TAG ('i','d','e','o'), HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT = HB_TAG ('i','d','t','p'), + HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL = HB_TAG ('I','d','c','e'), HB_OT_LAYOUT_BASELINE_TAG_MATH = HB_TAG ('m','a','t','h'), /*< private >*/