From 3a01d263d642dde34acfcb57184ab100cc3e20f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 17 Apr 2026 00:19:27 -0600 Subject: [PATCH] [vector] Add hb_buf_t; move precision to the buffer Add hb_buf_t inheriting from hb_vector_t with: - precision field (default 2) - append_num, append_c, append_str, append_len, append_unsigned convenience methods Convert defs/body/path in both hb_vector_draw_t and hb_vector_paint_t from hb_vector_t to hb_buf_t. Remove the standalone precision field from both structs; set_precision/get_precision are now methods that propagate to the buffers. C API functions are thin wrappers. Callers can still use the old free functions (hb_buf_t IS-A hb_vector_t). The new methods allow gradual migration. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-draw.cc | 46 +++++++++--------- src/hb-vector-draw.hh | 25 ++++++---- src/hb-vector-paint-pdf.cc | 98 +++++++++++++++++++------------------- src/hb-vector-paint-svg.cc | 78 +++++++++++++++--------------- src/hb-vector-paint.cc | 16 +++---- src/hb-vector-paint.hh | 14 ++++-- src/hb-vector-svg-utils.hh | 24 ++++++++++ 7 files changed, 171 insertions(+), 130 deletions(-) diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 00ec28b97..abcbb0fe6 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -557,7 +557,7 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, if (!hb_set_has (draw->defined_glyphs, glyph)) { draw->path.clear (); - hb_vector_path_sink_t sink = {&draw->path, draw->precision, 1.f, 1.f}; + hb_vector_path_sink_t sink = {&draw->path, draw->get_precision (), 1.f, 1.f}; hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); if (!draw->path.length) return false; @@ -587,7 +587,7 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, hb_buf_append_unsigned (&draw->body, glyph); hb_buf_append_str (&draw->body, "\" transform=\""); hb_vector_svg_append_instance_transform (&draw->body, - draw->precision, + draw->get_precision (), draw->x_scale_factor, draw->y_scale_factor, xx, yx, xy, yy, tx, ty); @@ -706,7 +706,7 @@ void hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned precision) { - draw->precision = hb_min (precision, 12u); + draw->set_precision (precision); } /** @@ -723,7 +723,7 @@ hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned hb_vector_draw_get_precision (const hb_vector_draw_t *draw) { - return draw->precision; + return draw->get_precision (); } /** @@ -825,13 +825,13 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_buf_append_c (&stream, ' '); hb_buf_append_num (&stream, hb_color_get_blue (draw->background) / 255.f, 4); hb_buf_append_str (&stream, " rg\n"); - hb_buf_append_num (&stream, ex, draw->precision); + hb_buf_append_num (&stream, ex, draw->get_precision ()); hb_buf_append_c (&stream, ' '); - hb_buf_append_num (&stream, -(ey + eh), draw->precision); + hb_buf_append_num (&stream, -(ey + eh), draw->get_precision ()); hb_buf_append_c (&stream, ' '); - hb_buf_append_num (&stream, ew, draw->precision); + hb_buf_append_num (&stream, ew, draw->get_precision ()); hb_buf_append_c (&stream, ' '); - hb_buf_append_num (&stream, eh, draw->precision); + hb_buf_append_num (&stream, eh, draw->get_precision ()); hb_buf_append_str (&stream, " re f\n"); } @@ -860,13 +860,13 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) * Convert back: font Y range = [-(ey+eh) .. -ey]. */ offsets[2] = out.length; hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_buf_append_num (&out, ex, draw->precision); + hb_buf_append_num (&out, ex, draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, -(ey + eh), draw->precision); + hb_buf_append_num (&out, -(ey + eh), draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, ex + ew, draw->precision); + hb_buf_append_num (&out, ex + ew, draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, -ey, draw->precision); + hb_buf_append_num (&out, -ey, draw->get_precision ()); hb_buf_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); /* Object 4: Content stream */ @@ -913,17 +913,17 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) 256; out.alloc (estimated); hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_num (&out, draw->extents.x, draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_num (&out, draw->extents.y, draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_num (&out, draw->extents.width, draw->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_num (&out, draw->extents.height, draw->get_precision ()); hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_num (&out, draw->extents.width, draw->get_precision ()); hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_num (&out, draw->extents.height, draw->get_precision ()); hb_buf_append_str (&out, "\">\n"); if (draw->defs.length) @@ -937,13 +937,13 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) if (hb_color_get_alpha (draw->background)) { hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_num (&out, draw->extents.x, draw->get_precision ()); hb_buf_append_str (&out, "\" y=\""); - hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_num (&out, draw->extents.y, draw->get_precision ()); hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_num (&out, draw->extents.width, draw->get_precision ()); hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_num (&out, draw->extents.height, draw->get_precision ()); hb_buf_append_str (&out, "\" fill=\"rgb("); hb_buf_append_unsigned (&out, hb_color_get_red (draw->background)); hb_buf_append_c (&out, ','); @@ -1035,7 +1035,7 @@ hb_vector_draw_reset (hb_vector_draw_t *draw) draw->transform = {1, 0, 0, 1, 0, 0}; draw->x_scale_factor = 1.f; draw->y_scale_factor = 1.f; - draw->precision = 2; + draw->set_precision (2); hb_vector_draw_clear (draw); } diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 8490aeeac..2df8c932f 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -21,12 +21,21 @@ struct hb_vector_draw_t bool has_extents = false; hb_color_t foreground = HB_COLOR (0, 0, 0, 255); hb_color_t background = HB_COLOR (0, 0, 0, 0); - unsigned precision = 2; hb_vector_t id_prefix; - hb_vector_t defs; - hb_vector_t body; - hb_vector_t path; + hb_buf_t defs; + hb_buf_t body; + hb_buf_t path; + + void set_precision (unsigned p) + { + p = hb_min (p, 12u); + defs.precision = p; + body.precision = p; + path.precision = p; + } + + unsigned get_precision () const { return path.precision; } hb_set_t *defined_glyphs = nullptr; hb_blob_t *recycled_blob = nullptr; @@ -116,18 +125,18 @@ struct hb_vector_draw_t { float tx, ty; transform_xy (x, y, &tx, &ty); - hb_buf_append_num (&path, tx, precision); + hb_buf_append_num (&path, tx, path.precision); hb_buf_append_c (&path, ','); - hb_buf_append_num (&path, ty, precision); + hb_buf_append_num (&path, ty, path.precision); } void append_xy_pdf (float x, float y) { float tx, ty; transform_xy (x, y, &tx, &ty); - hb_buf_append_num (&path, tx, precision); + hb_buf_append_num (&path, tx, path.precision); hb_buf_append_c (&path, ' '); - hb_buf_append_num (&path, ty, precision); + hb_buf_append_num (&path, ty, path.precision); } }; diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index d02819530..b813634cc 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -359,7 +359,7 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, { hb_vector_draw_t tmp; tmp.format = HB_VECTOR_FORMAT_PDF; - tmp.precision = paint->precision; + tmp.set_precision (paint->get_precision ()); tmp.transform = {1, 0, 0, 1, 0, 0}; tmp.path.alloc (1024); @@ -387,17 +387,17 @@ hb_pdf_paint_push_transform (hb_paint_funcs_t *, auto &body = paint->current_body (); hb_buf_append_str (&body, "q\n"); - hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_num (&body, xx, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_num (&body, yx, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_num (&body, xy, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_num (&body, yy, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, dx, paint->precision); + hb_buf_append_num (&body, dx, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_num (&body, dy, paint->get_precision ()); hb_buf_append_str (&body, " cm\n"); } @@ -442,13 +442,13 @@ hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, auto &body = paint->current_body (); hb_buf_append_str (&body, "q\n"); - hb_buf_append_num (&body, xmin, paint->precision); + hb_buf_append_num (&body, xmin, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, ymin, paint->precision); + hb_buf_append_num (&body, ymin, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, xmax - xmin, paint->precision); + hb_buf_append_num (&body, xmax - xmin, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, ymax - ymin, paint->precision); + hb_buf_append_num (&body, ymax - ymin, paint->get_precision ()); hb_buf_append_str (&body, " re W n\n"); } @@ -471,7 +471,7 @@ hb_pdf_paint_push_clip_path_start (hb_paint_funcs_t *, * the path with "W n" to turn it into the clip region. * Coordinates arrive in font-scale; the sink divides by * scale_factor so they land in output space. */ - paint->clip_path_sink = {&body, paint->precision, + paint->clip_path_sink = {&body, paint->get_precision (), paint->x_scale_factor, paint->y_scale_factor}; *draw_data = &paint->clip_path_sink; @@ -520,7 +520,7 @@ hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) auto *res = hb_pdf_get_resources (paint); if (res) { - unsigned gs_idx = res->add_extgstate_alpha (a, paint->precision); + unsigned gs_idx = res->add_extgstate_alpha (a, paint->get_precision ()); hb_buf_append_str (&body, "/GS"); hb_buf_append_unsigned (&body, gs_idx); hb_buf_append_str (&body, " gs\n"); @@ -776,13 +776,13 @@ hb_pdf_paint_image (hb_paint_funcs_t *, float iw = (float) extents->width; float ih = (float) -extents->height; /* negative because image Y goes up but height is negative in extents */ - hb_buf_append_num (&body, iw, paint->precision); + hb_buf_append_num (&body, iw, paint->get_precision ()); hb_buf_append_str (&body, " 0 0 "); - hb_buf_append_num (&body, ih, paint->precision); + hb_buf_append_num (&body, ih, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, ix, paint->precision); + hb_buf_append_num (&body, ix, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, iy, paint->precision); + hb_buf_append_num (&body, iy, paint->get_precision ()); hb_buf_append_str (&body, " cm\n"); hb_buf_append_str (&body, "/Im"); @@ -1019,13 +1019,13 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, hb_vector_t sh; hb_buf_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); hb_buf_append_str (&sh, "/Coords ["); - hb_buf_append_num (&sh, gx0, paint->precision); + hb_buf_append_num (&sh, gx0, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gy0, paint->precision); + hb_buf_append_num (&sh, gy0, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gx1, paint->precision); + hb_buf_append_num (&sh, gx1, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gy1, paint->precision); + hb_buf_append_num (&sh, gy1, paint->get_precision ()); hb_buf_append_str (&sh, "]\n/Function "); hb_buf_append_unsigned (&sh, func_id); hb_buf_append_str (&sh, " 0 R\n"); @@ -1044,13 +1044,13 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, hb_vector_t ash; hb_buf_append_str (&ash, "<< /ShadingType 2 /ColorSpace /DeviceGray\n"); hb_buf_append_str (&ash, "/Coords ["); - hb_buf_append_num (&ash, gx0, paint->precision); + hb_buf_append_num (&ash, gx0, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gy0, paint->precision); + hb_buf_append_num (&ash, gy0, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gx1, paint->precision); + hb_buf_append_num (&ash, gx1, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gy1, paint->precision); + hb_buf_append_num (&ash, gy1, paint->get_precision ()); hb_buf_append_str (&ash, "]\n/Function "); hb_buf_append_unsigned (&ash, alpha_func_id); hb_buf_append_str (&ash, " 0 R\n"); @@ -1061,7 +1061,7 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, gx0, gy0, gx1 - gx0, gy1 - gy0, - paint->precision); + paint->get_precision ()); hb_buf_append_str (&body, "/GS"); hb_buf_append_unsigned (&body, gs_idx); hb_buf_append_str (&body, " gs\n"); @@ -1114,17 +1114,17 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_vector_t sh; hb_buf_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); hb_buf_append_str (&sh, "/Coords ["); - hb_buf_append_num (&sh, gx0, paint->precision); + hb_buf_append_num (&sh, gx0, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gy0, paint->precision); + hb_buf_append_num (&sh, gy0, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gr0, paint->precision); + hb_buf_append_num (&sh, gr0, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gx1, paint->precision); + hb_buf_append_num (&sh, gx1, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gy1, paint->precision); + hb_buf_append_num (&sh, gy1, paint->get_precision ()); hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, gr1, paint->precision); + hb_buf_append_num (&sh, gr1, paint->get_precision ()); hb_buf_append_str (&sh, "]\n/Function "); hb_buf_append_unsigned (&sh, func_id); hb_buf_append_str (&sh, " 0 R\n"); @@ -1143,17 +1143,17 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_vector_t ash; hb_buf_append_str (&ash, "<< /ShadingType 3 /ColorSpace /DeviceGray\n"); hb_buf_append_str (&ash, "/Coords ["); - hb_buf_append_num (&ash, gx0, paint->precision); + hb_buf_append_num (&ash, gx0, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gy0, paint->precision); + hb_buf_append_num (&ash, gy0, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gr0, paint->precision); + hb_buf_append_num (&ash, gr0, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gx1, paint->precision); + hb_buf_append_num (&ash, gx1, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gy1, paint->precision); + hb_buf_append_num (&ash, gy1, paint->get_precision ()); hb_buf_append_c (&ash, ' '); - hb_buf_append_num (&ash, gr1, paint->precision); + hb_buf_append_num (&ash, gr1, paint->get_precision ()); hb_buf_append_str (&ash, "]\n/Function "); hb_buf_append_unsigned (&ash, alpha_func_id); hb_buf_append_str (&ash, " 0 R\n"); @@ -1168,7 +1168,7 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, cx - rr, cy - rr, 2 * rr, 2 * rr, - paint->precision); + paint->get_precision ()); hb_buf_append_str (&body, "/GS"); hb_buf_append_unsigned (&body, gs_idx); hb_buf_append_str (&body, " gs\n"); @@ -1444,7 +1444,7 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, xlo, ylo, xhi - xlo, yhi - ylo, - paint->precision); + paint->get_precision ()); hb_buf_append_str (&body, "/GS"); hb_buf_append_unsigned (&body, gs_idx); hb_buf_append_str (&body, " gs\n"); @@ -1631,13 +1631,13 @@ hb_vector_paint_render_pdf (hb_vector_paint_t *paint) /* Object 3: Page */ offsets.arrayZ[2] = out.length; hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); - hb_buf_append_num (&out, ex, paint->precision); + hb_buf_append_num (&out, ex, paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, -(ey + eh), paint->precision); + hb_buf_append_num (&out, -(ey + eh), paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, ex + ew, paint->precision); + hb_buf_append_num (&out, ex + ew, paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, -ey, paint->precision); + hb_buf_append_num (&out, -ey, paint->get_precision ()); hb_buf_append_str (&out, "]\n/Contents 4 0 R"); /* Resources. */ @@ -1681,7 +1681,7 @@ hb_vector_paint_render_pdf (hb_vector_paint_t *paint) { if (res) { - unsigned gs_idx = res->add_extgstate_alpha (a, paint->precision); + unsigned gs_idx = res->add_extgstate_alpha (a, paint->get_precision ()); hb_buf_append_str (&bg_prefix, "/GS"); hb_buf_append_unsigned (&bg_prefix, gs_idx); hb_buf_append_str (&bg_prefix, " gs\n"); @@ -1693,13 +1693,13 @@ hb_vector_paint_render_pdf (hb_vector_paint_t *paint) hb_buf_append_c (&bg_prefix, ' '); hb_buf_append_num (&bg_prefix, b, 4); hb_buf_append_str (&bg_prefix, " rg\n"); - hb_buf_append_num (&bg_prefix, ex, paint->precision); + hb_buf_append_num (&bg_prefix, ex, paint->get_precision ()); hb_buf_append_c (&bg_prefix, ' '); - hb_buf_append_num (&bg_prefix, -(ey + eh), paint->precision); + hb_buf_append_num (&bg_prefix, -(ey + eh), paint->get_precision ()); hb_buf_append_c (&bg_prefix, ' '); - hb_buf_append_num (&bg_prefix, ew, paint->precision); + hb_buf_append_num (&bg_prefix, ew, paint->get_precision ()); hb_buf_append_c (&bg_prefix, ' '); - hb_buf_append_num (&bg_prefix, eh, paint->precision); + hb_buf_append_num (&bg_prefix, eh, paint->get_precision ()); hb_buf_append_str (&bg_prefix, " re f\n"); } unsigned stream_len = bg_prefix.length + content.length; diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 157d12380..5de137916 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -45,7 +45,7 @@ hb_vector_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb paint->transform.x0 == 0.f && paint->transform.y0 == 0.f) return; - unsigned sprec = hb_vector_scale_precision (paint->precision); + unsigned sprec = hb_vector_scale_precision (paint->get_precision ()); hb_buf_append_str (buf, "transform.xx, sprec, true); hb_buf_append_c (buf, ','); @@ -55,9 +55,9 @@ hb_vector_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb hb_buf_append_c (buf, ','); hb_buf_append_num (buf, paint->transform.yy, sprec, true); hb_buf_append_c (buf, ','); - hb_buf_append_num (buf, paint->transform.x0, paint->precision); + hb_buf_append_num (buf, paint->transform.x0, paint->get_precision ()); hb_buf_append_c (buf, ','); - hb_buf_append_num (buf, paint->transform.y0, paint->precision); + hb_buf_append_num (buf, paint->transform.y0, paint->get_precision ()); hb_buf_append_str (buf, ")\">\n"); } @@ -439,7 +439,7 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, return; auto &body = paint->current_body (); - unsigned sprec = hb_vector_scale_precision (paint->precision); + unsigned sprec = hb_vector_scale_precision (paint->get_precision ()); hb_buf_append_str (&body, "precision); + hb_buf_append_num (&body, dx, paint->get_precision ()); hb_buf_append_c (&body, ','); - hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_num (&body, dy, paint->get_precision ()); hb_buf_append_str (&body, ")\">\n"); } @@ -495,7 +495,7 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, { hb_set_add (paint->defined_outlines, glyph); paint->path.clear (); - hb_vector_path_sink_t sink = {&paint->path, paint->precision, 1.f, 1.f}; + hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (), 1.f, 1.f}; hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); hb_buf_append_str (&paint->defs, "defs, pfx, pfx_len); @@ -546,13 +546,13 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, hb_buf_append_c (&paint->defs, 'c'); hb_buf_append_unsigned (&paint->defs, clip_id); hb_buf_append_str (&paint->defs, "\">defs, xmin, paint->precision); + hb_buf_append_num (&paint->defs, xmin, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" y=\""); - hb_buf_append_num (&paint->defs, ymin, paint->precision); + hb_buf_append_num (&paint->defs, ymin, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" width=\""); - hb_buf_append_num (&paint->defs, xmax - xmin, paint->precision); + hb_buf_append_num (&paint->defs, xmax - xmin, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" height=\""); - hb_buf_append_num (&paint->defs, ymax - ymin, paint->precision); + hb_buf_append_num (&paint->defs, ymax - ymin, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\"/>\n"); hb_buf_append_str (&paint->current_body (), "path.clear (); - paint->clip_path_sink = {&paint->path, paint->precision, + paint->clip_path_sink = {&paint->path, paint->get_precision (), paint->x_scale_factor, paint->y_scale_factor}; *draw_data = &paint->clip_path_sink; @@ -679,21 +679,21 @@ hb_vector_paint_image (hb_paint_funcs_t *, return false; hb_buf_append_str (&body, "x_bearing, paint->precision); + hb_buf_append_num (&body, (float) extents->x_bearing, paint->get_precision ()); hb_buf_append_c (&body, ','); - hb_buf_append_num (&body, (float) extents->y_bearing, paint->precision); + hb_buf_append_num (&body, (float) extents->y_bearing, paint->get_precision ()); hb_buf_append_str (&body, ") scale("); - hb_buf_append_num (&body, (float) extents->width / width, paint->precision); + hb_buf_append_num (&body, (float) extents->width / width, paint->get_precision ()); hb_buf_append_c (&body, ','); - hb_buf_append_num (&body, (float) extents->height / height, paint->precision); + hb_buf_append_num (&body, (float) extents->height / height, paint->get_precision ()); hb_buf_append_str (&body, ")\">\n"); hb_buf_append_str (&body, "precision); + hb_buf_append_num (&body, (float) width, paint->get_precision ()); hb_buf_append_str (&body, "\" height=\""); - hb_buf_append_num (&body, (float) height, paint->precision); + hb_buf_append_num (&body, (float) height, paint->get_precision ()); hb_buf_append_str (&body, "\"/>\n\n"); return true; @@ -745,13 +745,13 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, hb_buf_append_str (&paint->defs, "gr"); hb_buf_append_unsigned (&paint->defs, grad_id); hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); - hb_buf_append_num (&paint->defs, gx0, paint->precision); + hb_buf_append_num (&paint->defs, gx0, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" y1=\""); - hb_buf_append_num (&paint->defs, gy0, paint->precision); + hb_buf_append_num (&paint->defs, gy0, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" x2=\""); - hb_buf_append_num (&paint->defs, gx1, paint->precision); + hb_buf_append_num (&paint->defs, gx1, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" y2=\""); - hb_buf_append_num (&paint->defs, gy1, paint->precision); + hb_buf_append_num (&paint->defs, gy1, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); hb_buf_append_str (&paint->defs, hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line))); hb_buf_append_str (&paint->defs, "\">\n"); @@ -803,19 +803,19 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, hb_buf_append_str (&paint->defs, "gr"); hb_buf_append_unsigned (&paint->defs, grad_id); hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\""); - hb_buf_append_num (&paint->defs, gx1, paint->precision); + hb_buf_append_num (&paint->defs, gx1, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" cy=\""); - hb_buf_append_num (&paint->defs, gy1, paint->precision); + hb_buf_append_num (&paint->defs, gy1, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" r=\""); - hb_buf_append_num (&paint->defs, gr1, paint->precision); + hb_buf_append_num (&paint->defs, gr1, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" fx=\""); - hb_buf_append_num (&paint->defs, gx0, paint->precision); + hb_buf_append_num (&paint->defs, gx0, paint->get_precision ()); hb_buf_append_str (&paint->defs, "\" fy=\""); - hb_buf_append_num (&paint->defs, gy0, paint->precision); + hb_buf_append_num (&paint->defs, gy0, paint->get_precision ()); if (gr0 > 0) { hb_buf_append_str (&paint->defs, "\" fr=\""); - hb_buf_append_num (&paint->defs, gr0, paint->precision); + hb_buf_append_num (&paint->defs, gr0, paint->get_precision ()); } hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); hb_buf_append_str (&paint->defs, hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line))); @@ -856,7 +856,7 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, float ga1 = start_angle + mx * (end_angle - start_angle); hb_vector_svg_sweep_ctx_t ctx { - &paint->current_body (), paint->precision, cx, cy, 1000000.f + &paint->current_body (), paint->get_precision (), cx, cy, 1000000.f }; hb_paint_sweep_gradient_tiles (stops.arrayZ, stops.length, hb_color_line_get_extend (color_line), @@ -952,17 +952,17 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) 320; out.alloc (estimated); hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_num (&out, paint->extents.x, paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_num (&out, paint->extents.y, paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_num (&out, paint->extents.width, paint->get_precision ()); hb_buf_append_c (&out, ' '); - hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_num (&out, paint->extents.height, paint->get_precision ()); hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_num (&out, paint->extents.width, paint->get_precision ()); hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_num (&out, paint->extents.height, paint->get_precision ()); hb_buf_append_str (&out, "\">\n"); if (paint->defs.length) @@ -975,13 +975,13 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) if (hb_color_get_alpha (paint->background)) { hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_num (&out, paint->extents.x, paint->get_precision ()); hb_buf_append_str (&out, "\" y=\""); - hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_num (&out, paint->extents.y, paint->get_precision ()); hb_buf_append_str (&out, "\" width=\""); - hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_num (&out, paint->extents.width, paint->get_precision ()); hb_buf_append_str (&out, "\" height=\""); - hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_num (&out, paint->extents.height, paint->get_precision ()); hb_buf_append_str (&out, "\" fill=\"rgb("); hb_buf_append_unsigned (&out, hb_color_get_red (paint->background)); hb_buf_append_c (&out, ','); diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index b587b5595..24f3d5379 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -585,7 +585,7 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, * operators (emitted raw in input space) land in * pixel space, matching the MediaBox. */ auto &body = paint->current_body (); - unsigned sprec = hb_vector_scale_precision (paint->precision); + unsigned sprec = hb_vector_scale_precision (paint->get_precision ()); float sx = paint->x_scale_factor; float sy = paint->y_scale_factor; hb_buf_append_str (&body, "q\n"); @@ -598,9 +598,9 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, hb_buf_append_c (&body, ' '); hb_buf_append_num (&body, yy / sy, sprec, true); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, tx / sx, paint->precision); + hb_buf_append_num (&body, tx / sx, paint->get_precision ()); hb_buf_append_c (&body, ' '); - hb_buf_append_num (&body, ty / sy, paint->precision); + hb_buf_append_num (&body, ty / sy, paint->get_precision ()); hb_buf_append_str (&body, " cm\n"); hb_bool_t ret = true; @@ -630,7 +630,7 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, hb_buf_append_str (&body, "cg"); hb_buf_append_unsigned (&body, def_id); hb_buf_append_str (&body, "\" transform=\""); - hb_vector_svg_append_instance_transform (&body, paint->precision, + hb_vector_svg_append_instance_transform (&body, paint->get_precision (), paint->x_scale_factor, paint->y_scale_factor, xx, yx, xy, yy, tx, ty); @@ -685,7 +685,7 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, hb_buf_append_str (&body, "cg"); hb_buf_append_unsigned (&body, def_id); hb_buf_append_str (&body, "\" transform=\""); - hb_vector_svg_append_instance_transform (&body, paint->precision, + hb_vector_svg_append_instance_transform (&body, paint->get_precision (), paint->x_scale_factor, paint->y_scale_factor, xx, yx, xy, yy, tx, ty); @@ -817,7 +817,7 @@ void hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned precision) { - paint->precision = hb_min (precision, 12u); + paint->set_precision (precision); } /** @@ -834,7 +834,7 @@ hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned hb_vector_paint_get_precision (const hb_vector_paint_t *paint) { - return paint->precision; + return paint->get_precision (); } /** @@ -919,7 +919,7 @@ hb_vector_paint_reset (hb_vector_paint_t *paint) paint->y_scale_factor = 1.f; paint->foreground = HB_COLOR (0, 0, 0, 255); paint->palette = 0; - paint->precision = 2; + paint->set_precision (2); hb_vector_paint_clear (paint); } diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 6fd3a023c..3ecf92193 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -27,11 +27,10 @@ struct hb_vector_paint_t hb_color_t background = HB_COLOR (0, 0, 0, 0); int palette = 0; hb_hashmap_t custom_palette_colors; - unsigned precision = 2; hb_vector_t id_prefix; - hb_vector_t defs; - hb_vector_t path; + hb_buf_t defs; + hb_buf_t path; hb_vector_t> group_stack; uint64_t transform_group_open_mask = 0; unsigned transform_group_depth = 0; @@ -87,6 +86,15 @@ struct hb_vector_paint_t hb_vector_t ¤t_body () { return group_stack.tail (); } + void set_precision (unsigned p) + { + p = hb_min (p, 12u); + defs.precision = p; + path.precision = p; + } + + unsigned get_precision () const { return path.precision; } + bool ensure_initialized () { if (group_stack.length) diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index 9b46594e2..2d6def285 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -126,4 +126,28 @@ hb_vector_scale_precision (unsigned precision) return precision < 7 ? 7 : precision; } +struct hb_buf_t : hb_vector_t +{ + unsigned precision = 2; + + void append_num (float v, bool keep_nonzero = false) + { hb_buf_append_num (this, v, precision, keep_nonzero); } + + bool append_c (char ch) + { return push_or_fail (ch); } + + bool append_str (const char *s) + { return hb_buf_append_len (this, s, (unsigned) strlen (s)); } + + bool append_len (const char *s, unsigned l) + { return hb_buf_append_len (this, s, l); } + + bool append_unsigned (unsigned v) + { + char tmp[16]; + snprintf (tmp, sizeof (tmp), "%u", v); + return hb_buf_append_len (this, tmp, (unsigned) strlen (tmp)); + } +}; + #endif /* HB_VECTOR_SVG_UTILS_HH */