From d60ffa956ff014d472f4eb6350b5d21a9196b992 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 15 Apr 2026 17:29:48 -0600 Subject: [PATCH] [util/vector] Emit SVG/PDF in pixel units, not upem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vector-output.hh fed upem-space coordinates to the draw/paint context and then set scale_factor=1, so the resulting SVG's viewBox / width / height were in upem (e.g. ~2500×1400 for a 48 px "Hello" in a 1000-upem font). Browsers sized the inline SVG at 1 upem-unit-per-CSS-pixel, rendering it ~20× too large. Fix: compute the upem→pixel ratio from x_scale / y_scale (already available) and * scale the explicit extents by that ratio before calling set_extents (so viewBox and width/height come out in pixels), and * pass its inverse as set_scale_factor so upem-space input coordinates (pen position, glyph outline points) get divided to pixels internally. No test expected-output changes -- the vector tests render at FONT_SIZE_UPEM where upem_to_pixel = 1 and the patch is a no-op. Assisted-by: Claude Opus 4.6 (1M context) --- util/vector-output.hh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 543f23139..d910b392a 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -203,14 +203,31 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (fmt); hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (fmt); - hb_vector_draw_set_scale_factor (draw, 1.f, 1.f); - hb_vector_draw_set_extents (draw, &extents); + /* We feed upem-space coordinates to the draw/paint context + * (glyph outlines and pen positions), but downstream consumers + * expect pixel-sized SVG/PDF output. Divide inputs by + * (upem / font_size) -- equivalently, multiply by + * font_size / upem -- so the output viewBox / width / height + * come out in pixels. */ + float upem_to_pixel_x = (float) x_scale * scalbnf (1.f, -(int) subpixel_bits) / (float) upem; + float upem_to_pixel_y = (float) y_scale * scalbnf (1.f, -(int) subpixel_bits) / (float) upem; + float upem_per_pixel_x = 1.f / upem_to_pixel_x; + float upem_per_pixel_y = 1.f / upem_to_pixel_y; + hb_vector_extents_t pixel_extents = { + extents.x * upem_to_pixel_x, + extents.y * upem_to_pixel_y, + extents.width * upem_to_pixel_x, + extents.height * upem_to_pixel_y, + }; + + hb_vector_draw_set_scale_factor (draw, upem_per_pixel_x, upem_per_pixel_y); + hb_vector_draw_set_extents (draw, &pixel_extents); hb_vector_draw_set_precision (draw, precision); if (paint) { - hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); - hb_vector_paint_set_extents (paint, &extents); + hb_vector_paint_set_scale_factor (paint, upem_per_pixel_x, upem_per_pixel_y); + hb_vector_paint_set_extents (paint, &pixel_extents); hb_vector_paint_set_foreground (paint, foreground); hb_vector_paint_set_palette (paint, this->palette); apply_custom_palette (paint);