[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
This commit is contained in:
Matthias Clasen 2022-02-13 19:15:36 -06:00
parent 98079109e6
commit f81578fd86
5 changed files with 245 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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 */

BIN
test/api/fonts/base2.ttf Normal file

Binary file not shown.

View File

@ -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();
}