[util/vector] Emit SVG/PDF in pixel units, not upem

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) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-15 17:31:37 -06:00
1 parent f53d20c425
commit d60ffa956f
1 file changed
+21 -4
+21 -4
View File
@@ -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);