[vector] Divide explicit set_extents by scale_factor

hb_vector_{paint,draw}_set_extents stored caller-supplied
extents as-is, while per-glyph extents accumulated via
hb_vector_set_glyph_extents_common were divided by
scale_factor on the way in.  When callers passed extents in
input-space (the natural unit alongside the glyph geometry
they're feeding) and a non-1 scale_factor, the resulting
viewBox didn't match what the glyphs drew.

Make set_extents divide by scale_factor too, matching the
accumulate path; callers can now pass input-space extents
uniformly.

Also:

* hb-vector-paint's global transform wrapper dropped the
  redundant /scale_factor division.  Each per-glyph
  <use transform="... scale(1/scale_factor, ...)"> already
  applies scale_factor; wrapping the body in another
  /scale_factor matrix double-scaled the output, shrinking
  color-glyph output to 1/scale_factor^2 of its intended
  size.  The wrapper now only fires when the user set a
  non-identity paint->transform, and emits that transform
  as-is.

* util/vector-output.hh simplification: now that set_extents
  handles the divide, drop the local pre-multiply of extents
  by upem_to_pixel -- pass the upem-space extents through.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-15 22:04:26 -06:00
1 parent d8980e94e0
commit 152aa0fd8c
3 files changed
+57 -38

No files matched your search

+17 -5
View File
@@ -432,19 +432,31 @@ hb_vector_draw_set_extents (hb_vector_draw_t *draw,
if (!(extents->width > 0.f && extents->height > 0.f))
return;
/* Caller-supplied extents are in input-space; divide by
* scale_factor so they end up in output-space, matching
* the per-glyph extents accumulated via
* hb_vector_set_glyph_extents_common (which applies the
* same divide). */
hb_vector_extents_t e = {
extents->x / draw->x_scale_factor,
extents->y / draw->y_scale_factor,
extents->width / draw->x_scale_factor,
extents->height / draw->y_scale_factor,
};
if (draw->has_extents)
{
float x0 = hb_min (draw->extents.x, extents->x);
float y0 = hb_min (draw->extents.y, extents->y);
float x0 = hb_min (draw->extents.x, e.x);
float y0 = hb_min (draw->extents.y, e.y);
float x1 = hb_max (draw->extents.x + draw->extents.width,
extents->x + extents->width);
e.x + e.width);
float y1 = hb_max (draw->extents.y + draw->extents.height,
extents->y + extents->height);
e.y + e.height);
draw->extents = {x0, y0, x1 - x0, y1 - y0};
}
else
{
draw->extents = *extents;
draw->extents = e;
draw->has_extents = true;
}
}
+30 -15
View File
@@ -35,25 +35,29 @@
static void
hb_vector_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb_vector_t<char> *buf)
{
/* Skip when the paint's own transform is identity -- each
* instance's <use> transform already divides by scale_factor,
* so wrapping the body in another /scale_factor matrix would
* double-scale. Only emit when the user has set a non-
* identity paint-level transform. */
if (paint->transform.xx == 1.f && paint->transform.yx == 0.f &&
paint->transform.xy == 0.f && paint->transform.yy == 1.f &&
paint->transform.x0 == 0.f && paint->transform.y0 == 0.f &&
paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f)
paint->transform.x0 == 0.f && paint->transform.y0 == 0.f)
return;
unsigned sprec = hb_vector_scale_precision (paint->precision);
hb_buf_append_str (buf, "<g transform=\"matrix(");
hb_buf_append_num (buf, paint->transform.xx / paint->x_scale_factor, sprec, true);
hb_buf_append_num (buf, paint->transform.xx, sprec, true);
hb_buf_append_c (buf, ',');
hb_buf_append_num (buf, paint->transform.yx / paint->y_scale_factor, sprec, true);
hb_buf_append_num (buf, paint->transform.yx, sprec, true);
hb_buf_append_c (buf, ',');
hb_buf_append_num (buf, paint->transform.xy / paint->x_scale_factor, sprec, true);
hb_buf_append_num (buf, paint->transform.xy, sprec, true);
hb_buf_append_c (buf, ',');
hb_buf_append_num (buf, paint->transform.yy / paint->y_scale_factor, sprec, true);
hb_buf_append_num (buf, paint->transform.yy, sprec, true);
hb_buf_append_c (buf, ',');
hb_buf_append_num (buf, paint->transform.x0 / paint->x_scale_factor, paint->precision);
hb_buf_append_num (buf, paint->transform.x0, paint->precision);
hb_buf_append_c (buf, ',');
hb_buf_append_num (buf, paint->transform.y0 / paint->y_scale_factor, paint->precision);
hb_buf_append_num (buf, paint->transform.y0, paint->precision);
hb_buf_append_str (buf, ")\">\n");
}
@@ -62,8 +66,7 @@ hb_vector_svg_paint_append_global_transform_suffix (hb_vector_paint_t *paint, hb
{
if (paint->transform.xx == 1.f && paint->transform.yx == 0.f &&
paint->transform.xy == 0.f && paint->transform.yy == 1.f &&
paint->transform.x0 == 0.f && paint->transform.y0 == 0.f &&
paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f)
paint->transform.x0 == 0.f && paint->transform.y0 == 0.f)
return;
hb_buf_append_str (buf, "</g>\n");
}
@@ -1095,19 +1098,31 @@ hb_vector_paint_set_extents (hb_vector_paint_t *paint,
if (!(extents->width > 0.f && extents->height > 0.f))
return;
/* Caller-supplied extents are in input-space; divide by
* scale_factor so they end up in output-space, matching
* the per-glyph extents accumulated via
* hb_vector_set_glyph_extents_common (which applies the
* same divide). */
hb_vector_extents_t e = {
extents->x / paint->x_scale_factor,
extents->y / paint->y_scale_factor,
extents->width / paint->x_scale_factor,
extents->height / paint->y_scale_factor,
};
if (paint->has_extents)
{
float x0 = hb_min (paint->extents.x, extents->x);
float y0 = hb_min (paint->extents.y, extents->y);
float x0 = hb_min (paint->extents.x, e.x);
float y0 = hb_min (paint->extents.y, e.y);
float x1 = hb_max (paint->extents.x + paint->extents.width,
extents->x + extents->width);
e.x + e.width);
float y1 = hb_max (paint->extents.y + paint->extents.height,
extents->y + extents->height);
e.y + e.height);
paint->extents = {x0, y0, x1 - x0, y1 - y0};
}
else
{
paint->extents = *extents;
paint->extents = e;
paint->has_extents = true;
}
}
+10 -18
View File
@@ -203,31 +203,23 @@ 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);
/* 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,
};
/* We feed upem-space coordinates (glyph outlines + pen
* positions) to the draw/paint context; scale_factor
* converts to pixel-space on emit. set_extents now
* divides the caller-supplied extents by the same
* scale_factor, so we can hand it the upem-space
* extents directly. */
float upem_per_pixel_x = (float) upem * scalbnf (1.f, (int) subpixel_bits) / (float) x_scale;
float upem_per_pixel_y = (float) upem * scalbnf (1.f, (int) subpixel_bits) / (float) y_scale;
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_extents (draw, &extents);
hb_vector_draw_set_precision (draw, precision);
if (paint)
{
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_extents (paint, &extents);
hb_vector_paint_set_foreground (paint, foreground);
hb_vector_paint_set_palette (paint, this->palette);
apply_custom_palette (paint);