From 5ce8f73badb17eee1620a2880cb595f319a0d4cb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 16 Apr 2026 14:35:58 -0600 Subject: [PATCH] [util/vector] Apply margin in pixel units MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The other utils (hb-view, hb-raster) treat --margin as pixels, but hb-vector was applying it directly to the upem-space extents. For a font with upem=1000 rendered at font-size=72, the default 16-pixel margin came out as 16/1000*72 = ~1.15 pixels per side, much smaller than expected. Convert margin from pixels to upem before adding it to the extents, matching the per-axis x_scale/y_scale used elsewhere in the file. Now hb-vector and hb-raster produce matching surface dimensions for the same input (e.g. NotoNastaliqUrdu "اردو" at 72px: 137x213 in both). Assisted-by: Claude Opus 4.6 (1M context) --- util/vector-output.hh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 3a7b0d95d..3ea65b6ba 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -428,10 +428,14 @@ struct vector_output_t : output_options_t<>, view_options_t if (y2 <= y1) y2 = y1 + 1; - x1 -= (float) margin.l; - y1 -= (float) margin.t; - x2 += (float) margin.r; - y2 += (float) margin.b; + /* Margin is specified in pixels (matching the other utils); + * the surrounding extents are in upem units, so convert. */ + 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; + x1 -= (float) margin.l * upem_per_pixel_x; + y1 -= (float) margin.t * upem_per_pixel_y; + x2 += (float) margin.r * upem_per_pixel_x; + y2 += (float) margin.b * upem_per_pixel_y; extents->x = x1; extents->y = y1;