From 008402dccfe6835cff8e04ddf5f2ae5d491d2f50 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 9 Feb 2026 00:29:32 -0700 Subject: [PATCH] [hb-draw] Default to upem font size --- .../expected/basic/reuse-svg-upem-default.svg | 10 ++++ test/draw/data/tests/basic.tests | 1 + util/draw-output.hh | 48 ++++++++++++++----- util/hb-draw.cc | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 test/draw/data/expected/basic/reuse-svg-upem-default.svg diff --git a/test/draw/data/expected/basic/reuse-svg-upem-default.svg b/test/draw/data/expected/basic/reuse-svg-upem-default.svg new file mode 100644 index 000000000..58058dfe5 --- /dev/null +++ b/test/draw/data/expected/basic/reuse-svg-upem-default.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/draw/data/tests/basic.tests b/test/draw/data/tests/basic.tests index 15e4029c4..f67a47841 100644 --- a/test/draw/data/tests/basic.tests +++ b/test/draw/data/tests/basic.tests @@ -2,3 +2,4 @@ ../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=64;U+0061,U+0062,U+0063;../expected/basic/reuse-svg.svg ../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=64 --ned;U+0061,U+0062,U+0063;../expected/basic/flat-svg.svg ../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=64 -q;U+0061,U+0020,U+0061;../expected/basic/quiet-paths-space.txt +../../../api/fonts/Roboto-Regular.abc.ttf;;U+0061,U+0062,U+0063;../expected/basic/reuse-svg-upem-default.svg diff --git a/util/draw-output.hh b/util/draw-output.hh index d1eb3ea06..f7f74538e 100644 --- a/util/draw-output.hh +++ b/util/draw-output.hh @@ -522,6 +522,11 @@ struct draw_output_t : output_options_t<> bbox_t box = compute_bbox (offsets); emit_svg_header (box); + float font_scale_x = scalbnf ((float) x_scale, -(int) subpixel_bits); + float font_scale_y = scalbnf ((float) y_scale, -(int) subpixel_bits); + bool upem_scale = font_scale_x == (float) upem && + font_scale_y == (float) upem; + std::vector unique_gids; unique_gids.reserve (128); std::unordered_set seen; @@ -536,22 +541,34 @@ struct draw_output_t : output_options_t<> for (hb_codepoint_t gid : unique_gids) { if (!draw_glyph (upem_font, gid, defs_data)) - fprintf (out_fp, "\n", gid); + fprintf (out_fp, + upem_scale + ? "\n" + : "\n", + gid); else - fprintf (out_fp, "\n", gid, path->str); + fprintf (out_fp, + upem_scale + ? "\n" + : "\n", + gid, + path->str); } fprintf (out_fp, "\n"); - g_string_set_size (path, 0); - append_num (scalbnf ((float) x_scale, -(int) subpixel_bits), precision, true); - g_string_append_c (path, ' '); - append_num (-scalbnf ((float) y_scale, -(int) subpixel_bits), precision, true); - fprintf (out_fp, "\n", path->str); + if (!upem_scale) + { + g_string_set_size (path, 0); + append_num (font_scale_x, precision, true); + g_string_append_c (path, ' '); + append_num (-font_scale_y, precision, true); + fprintf (out_fp, "\n", path->str); - g_string_set_size (path, 0); - int upem_scale_precision = precision < 9 ? 9 : precision; - append_num (1.f / upem, upem_scale_precision, true); - fprintf (out_fp, "\n", path->str); + g_string_set_size (path, 0); + int upem_scale_precision = precision < 9 ? 9 : precision; + append_num (1.f / upem, upem_scale_precision, true); + fprintf (out_fp, "\n", path->str); + } for (unsigned li = 0; li < lines.size (); li++) { @@ -562,7 +579,10 @@ struct draw_output_t : output_options_t<> g_string_set_size (path, 0); append_num (offset.first + glyph.x, precision, false); g_string_append_c (path, ' '); - append_num (offset.second + glyph.y, precision, false); + append_num (upem_scale ? -(offset.second + glyph.y) + : (offset.second + glyph.y), + precision, + false); fprintf (out_fp, "\n", glyph.gid, @@ -570,7 +590,9 @@ struct draw_output_t : output_options_t<> } } - fprintf (out_fp, "\n\n\n"); + if (!upem_scale) + fprintf (out_fp, "\n\n"); + fprintf (out_fp, "\n"); } void write_flat_svg () diff --git a/util/hb-draw.cc b/util/hb-draw.cc index 6e5fa86ad..9097ca347 100644 --- a/util/hb-draw.cc +++ b/util/hb-draw.cc @@ -29,7 +29,7 @@ #include "shape-consumer.hh" #include "text-options.hh" -const unsigned DEFAULT_FONT_SIZE = 256; +const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; const unsigned SUBPIXEL_BITS = 6; int