diff --git a/CMakeLists.txt b/CMakeLists.txt index a8d26a50f..bf61e639d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -258,10 +258,10 @@ set (vector_project_sources ${PROJECT_SOURCE_DIR}/src/hb-zlib.hh ${PROJECT_SOURCE_DIR}/src/hb-vector.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-draw.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-path.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-path.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-paint.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-paint-pdf.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-path.hh + ${PROJECT_SOURCE_DIR}/src/hb-vector-path.hh ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-utils.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-utils.hh ${PROJECT_SOURCE_DIR}/src/hb-static.cc diff --git a/NEWS b/NEWS index 57a39c1d4..4aabb8c7d 100644 --- a/NEWS +++ b/NEWS @@ -56,6 +56,31 @@ Overview of changes leading to NEXT (REPLACEME) - Various fuzzer fixes for `harfbuzz-raster`, `harfbuzz-vector`, and `harfbuzz-gpu` libraries. +- Paint API: + * New arbitrary-path clip: + hb_paint_push_clip_path_start / _end let callers clip to + a caller-supplied outline, not just a font glyph. + push_clip_path_start returns the draw-funcs (and matching + draw data) for the backend's path accumulator; the caller + drives hb_draw_*() into it, then calls + push_clip_path_end, followed by painted ops, then + hb_paint_pop_clip() to release the clip. Implemented for + all paint backends: vector (SVG defs + clipPath + url ref), + vector PDF (q / path / W n), raster (path rendered to an + alpha mask, intersected with the current clip), and GPU + (the path is encoded into a Slug sub-blob so the fragment + shader clips against it the same way it clips against + glyph outlines). + +- New API, Draw: + +hb_draw_line() + +hb_draw_rectangle() + +hb_draw_circle() + +hb_draw_line_cap_t + +HB_DRAW_LINE_CAP_BUTT + +HB_DRAW_LINE_CAP_ROUND + +HB_DRAW_LINE_CAP_SQUARE + - New API, Paint: +hb_paint_push_group_for() +hb_paint_funcs_set_push_group_for_func() @@ -64,6 +89,12 @@ Overview of changes leading to NEXT (REPLACEME) +hb_paint_normalize_color_line() +hb_paint_sweep_gradient_tiles() +hb_paint_sweep_gradient_tile_func_t + +hb_paint_push_clip_path_start() + +hb_paint_push_clip_path_end() + +hb_paint_funcs_set_push_clip_path_start_func() + +hb_paint_funcs_set_push_clip_path_end_func() + +hb_paint_push_clip_path_start_func_t + +hb_paint_push_clip_path_end_func_t - New API, GPU library: +hb_gpu_paint_t @@ -107,11 +138,13 @@ Overview of changes leading to NEXT (REPLACEME) +HB_VECTOR_FORMAT_PDF +hb_vector_draw_clear() +hb_vector_draw_get_precision() + +hb_vector_draw_get_format() +hb_vector_draw_glyph_or_fail() +hb_vector_draw_set_svg_prefix() +hb_vector_draw_get_svg_prefix() +hb_vector_paint_clear() +hb_vector_paint_get_precision() + +hb_vector_paint_get_format() +hb_vector_paint_get_foreground() +hb_vector_paint_get_palette() +hb_vector_paint_glyph_or_fail() @@ -163,6 +196,19 @@ Overview of changes leading to NEXT (REPLACEME) - Changed API, general: * Many read-only getters gained const on their object argument. + * Six public draw / paint funcs getters across raster, + vector, and gpu now take a const-pointer to the relevant + context instance instead of being singletons: + hb_raster_draw_get_funcs (const hb_raster_draw_t *) + hb_raster_paint_get_funcs (const hb_raster_paint_t *) + hb_vector_draw_get_funcs (const hb_vector_draw_t *) + hb_vector_paint_get_funcs (const hb_vector_paint_t *) + hb_gpu_draw_get_funcs (const hb_gpu_draw_t *) + hb_gpu_paint_get_funcs (const hb_gpu_paint_t *) + This is what lets vector_paint dispatch to SVG- vs PDF- + flavored callbacks transparently when called from outside + code; the others currently use the instance for symmetry + only. Overview of changes leading to 14.1.0 diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index c0b74a5f4..7e1e01db6 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -241,7 +241,7 @@ hb_draw_cubic_to hb_draw_close_path hb_draw_line_cap_t hb_draw_line -hb_draw_rect +hb_draw_rectangle hb_draw_circle HB_DRAW_STATE_DEFAULT hb_draw_funcs_t @@ -270,6 +270,10 @@ hb_paint_push_clip_glyph_func_t hb_paint_funcs_set_push_clip_glyph_func hb_paint_push_clip_rectangle_func_t hb_paint_funcs_set_push_clip_rectangle_func +hb_paint_push_clip_path_start_func_t +hb_paint_funcs_set_push_clip_path_start_func +hb_paint_push_clip_path_end_func_t +hb_paint_funcs_set_push_clip_path_end_func hb_paint_pop_clip_func_t hb_paint_funcs_set_pop_clip_func hb_paint_color_func_t @@ -309,6 +313,8 @@ hb_paint_pop_transform hb_paint_color_glyph hb_paint_push_clip_glyph hb_paint_push_clip_rectangle +hb_paint_push_clip_path_start +hb_paint_push_clip_path_end hb_paint_pop_clip hb_paint_color hb_paint_image @@ -1094,6 +1100,7 @@ hb_vector_draw_get_scale_factor hb_vector_draw_set_extents hb_vector_draw_get_extents hb_vector_draw_set_glyph_extents +hb_vector_draw_get_format hb_vector_draw_get_funcs hb_vector_draw_glyph hb_vector_draw_glyph_or_fail @@ -1124,6 +1131,7 @@ hb_vector_paint_set_palette hb_vector_paint_get_palette hb_vector_paint_set_custom_palette_color hb_vector_paint_clear_custom_palette_colors +hb_vector_paint_get_format hb_vector_paint_get_funcs hb_vector_paint_glyph hb_vector_paint_glyph_or_fail diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index 2def1672e..e196bb2d9 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -156,7 +156,7 @@ #include "hb-vector-draw.cc" #include "hb-vector-paint-pdf.cc" #include "hb-vector-paint.cc" -#include "hb-vector-svg-path.cc" +#include "hb-vector-path.cc" #include "hb-vector-svg-utils.cc" #include "hb-vector.cc" #include "hb-zlib.cc" diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index c93206a38..ddb5d23dc 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -227,6 +227,33 @@ hb_cairo_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, cairo_clip (cr); } +static hb_draw_funcs_t * +hb_cairo_push_clip_path_start (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void **draw_data, + void *user_data HB_UNUSED) +{ + hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data; + cairo_t *cr = c->cr; + + cairo_save (cr); + cairo_new_path (cr); + *draw_data = cr; + return hb_cairo_draw_get_funcs (); +} + +static void +hb_cairo_push_clip_path_end (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data; + cairo_t *cr = c->cr; + + cairo_close_path (cr); + cairo_clip (cr); +} + static void hb_cairo_pop_clip (hb_paint_funcs_t *pfuncs HB_UNUSED, void *paint_data, @@ -428,6 +455,8 @@ static struct hb_cairo_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t< hb_paint_funcs_set_color_glyph_func (funcs, hb_cairo_paint_color_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_cairo_push_clip_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_cairo_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_cairo_push_clip_path_start, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_cairo_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, hb_cairo_pop_clip, nullptr, nullptr); hb_paint_funcs_set_push_group_func (funcs, hb_cairo_push_group, nullptr, nullptr); hb_paint_funcs_set_pop_group_func (funcs, hb_cairo_pop_group, nullptr, nullptr); diff --git a/src/hb-draw.cc b/src/hb-draw.cc index 1dc14ba90..67972a43c 100644 --- a/src/hb-draw.cc +++ b/src/hb-draw.cc @@ -553,7 +553,7 @@ _hb_draw_rect_contour (hb_draw_funcs_t *dfuncs, void *draw_data, } /** - * hb_draw_rect: + * hb_draw_rectangle: * @dfuncs: draw functions * @draw_data: associated draw data passed by the caller * @st: current draw state @@ -579,11 +579,11 @@ _hb_draw_rect_contour (hb_draw_funcs_t *dfuncs, void *draw_data, * XSince: REPLACEME **/ void -hb_draw_rect (hb_draw_funcs_t *dfuncs, void *draw_data, - hb_draw_state_t *st, - float x, float y, - float w, float h, - float stroke_width) +hb_draw_rectangle (hb_draw_funcs_t *dfuncs, void *draw_data, + hb_draw_state_t *st, + float x, float y, + float w, float h, + float stroke_width) { if (std::isnan (stroke_width)) { @@ -596,6 +596,12 @@ hb_draw_rect (hb_draw_funcs_t *dfuncs, void *draw_data, if (stroke_width <= 0.f || !std::isfinite (stroke_width)) return; + + /* Normalize to non-negative width/height so the stroke math + * below (outer grows by sw, inner shrinks by sw) produces the + * expected outer-contains-inner ring regardless of w/h signs. */ + if (w < 0.f) { x += w; w = -w; } + if (h < 0.f) { y += h; h = -h; } /* w or h == 0 is still meaningful when stroking: a stroked * zero-height rect is a horizontal line of length w; zero * width is a vertical line. Both degenerate to a single diff --git a/src/hb-draw.h b/src/hb-draw.h index 2fbcbd5ad..1e60db66b 100644 --- a/src/hb-draw.h +++ b/src/hb-draw.h @@ -373,10 +373,10 @@ hb_draw_line (hb_draw_funcs_t *dfuncs, void *draw_data, hb_draw_line_cap_t cap); HB_EXTERN void -hb_draw_rect (hb_draw_funcs_t *dfuncs, void *draw_data, - hb_draw_state_t *st, - float x, float y, - float w, float h, +hb_draw_rectangle (hb_draw_funcs_t *dfuncs, void *draw_data, + hb_draw_state_t *st, + float x, float y, + float w, float h, float stroke_width); HB_EXTERN void diff --git a/src/hb-gpu-draw.cc b/src/hb-gpu-draw.cc index a9ff17af3..63c30ddfd 100644 --- a/src/hb-gpu-draw.cc +++ b/src/hb-gpu-draw.cc @@ -908,18 +908,19 @@ hb_gpu_draw_get_user_data (const hb_gpu_draw_t *draw, /** * hb_gpu_draw_get_funcs: + * @draw: a GPU draw context. * - * Fetches the singleton #hb_draw_funcs_t that feeds outline data - * into an #hb_gpu_draw_t. Pass the #hb_gpu_draw_t as the - * @draw_data argument when calling the draw functions. + * Fetches the #hb_draw_funcs_t that feeds outline data into + * @draw. Pass @draw as the @draw_data argument when calling + * the draw functions. * * Return value: (transfer none): * The GPU draw functions * - * Since: 14.0.0 + * XSince: REPLACEME **/ hb_draw_funcs_t * -hb_gpu_draw_get_funcs (void) +hb_gpu_draw_get_funcs (const hb_gpu_draw_t *draw HB_UNUSED) { return static_gpu_draw_funcs.get_unconst (); } @@ -991,7 +992,7 @@ hb_gpu_draw_glyph_or_fail (hb_gpu_draw_t *draw, hb_gpu_draw_set_scale (draw, x_scale, y_scale); return hb_font_draw_glyph_or_fail (font, glyph, - hb_gpu_draw_get_funcs (), + hb_gpu_draw_get_funcs (draw), draw); } diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 145dde20a..163711930 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -89,7 +89,7 @@ hb_gpu_paint_push_clip_glyph (hb_paint_funcs_t *funcs HB_UNUSED, c->clip_depth++; return; } - c->clip_stack[c->clip_depth] = { glyph, font, c->cur_transform }; + c->clip_stack[c->clip_depth] = { glyph, font, c->cur_transform, -1, 0, 0, 0, 0 }; c->clip_depth++; } @@ -102,6 +102,84 @@ hb_gpu_paint_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED, if (likely (c->clip_depth > 0)) c->clip_depth--; } +/* Arbitrary-path clip: hand the caller the same hb_gpu_draw + * funcs the encoder uses for glyph outlines, with scratch_draw + * as the accumulator. push_clip_path_end encodes the captured + * geometry into a sub-blob and commits it to clip_stack just + * like a glyph clip. No font ref needed — the sub-blob is a + * self-contained slug bundle. */ +static hb_draw_funcs_t * +hb_gpu_paint_push_clip_path_start (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data, + void **draw_data, + void *user_data HB_UNUSED) +{ + hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + + if (unlikely (c->clip_depth >= HB_GPU_PAINT_MAX_CLIP_DEPTH)) + { + c->unsupported = true; + *draw_data = nullptr; + return nullptr; + } + + if (unlikely (!c->scratch_draw)) + { + c->scratch_draw = hb_gpu_draw_create_or_fail (); + if (unlikely (!c->scratch_draw)) + { + c->unsupported = true; + *draw_data = nullptr; + return nullptr; + } + } + + hb_gpu_draw_clear (c->scratch_draw); + hb_gpu_draw_set_scale (c->scratch_draw, c->x_scale, c->y_scale); + + c->pending_clip_path_transform = c->cur_transform; + c->pending_clip_path = true; + + *draw_data = c->scratch_draw; + return hb_gpu_draw_get_funcs (c->scratch_draw); +} + +static void +hb_gpu_paint_push_clip_path_end (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + + if (unlikely (!c->pending_clip_path)) + return; + c->pending_clip_path = false; + + hb_glyph_extents_t ext; + hb_blob_t *blob = hb_gpu_draw_encode (c->scratch_draw, &ext); + if (unlikely (!blob || !c->sub_blobs.push_or_fail (blob))) + { + hb_blob_destroy (blob); + c->unsupported = true; + return; + } + int idx = (int) (c->sub_blobs.length - 1); + + int x0 = ext.x_bearing; + int x1 = ext.x_bearing + ext.width; + int y0 = ext.y_bearing; + int y1 = ext.y_bearing + ext.height; + + c->clip_stack[c->clip_depth] = { + HB_CODEPOINT_INVALID, nullptr, + c->pending_clip_path_transform, + idx, + hb_min (x0, x1), hb_min (y0, y1), + hb_max (x0, x1), hb_max (y0, y1), + }; + c->clip_depth++; +} + static void hb_gpu_paint_push_transform (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data, @@ -332,6 +410,18 @@ static int emit_clip_sub_blob (hb_gpu_paint_t *c, const hb_gpu_paint_t::pending_clip_t &clip) { + /* Path clips were already encoded into sub_blobs at + * push_clip_path_end time; just accumulate extents and hand + * back the pre-baked index. */ + if (clip.sub_blob_index >= 0) + { + c->ext_min_x = hb_min (c->ext_min_x, clip.ext_x0); + c->ext_max_x = hb_max (c->ext_max_x, clip.ext_x1); + c->ext_min_y = hb_min (c->ext_min_y, clip.ext_y0); + c->ext_max_y = hb_max (c->ext_max_y, clip.ext_y1); + return clip.sub_blob_index; + } + if (unlikely (!c->scratch_draw)) { c->scratch_draw = hb_gpu_draw_create_or_fail (); @@ -362,7 +452,7 @@ emit_clip_sub_blob (hb_gpu_paint_t *c, hb_gpu_paint_pen_t pen = {}; pen.transform = clip.transform; - pen.dfuncs = hb_gpu_draw_get_funcs (); + pen.dfuncs = hb_gpu_draw_get_funcs (c->scratch_draw); pen.data = c->scratch_draw; pen.down_st = HB_DRAW_STATE_DEFAULT; ok = hb_font_draw_glyph_or_fail (clip.font, clip.glyph, @@ -889,6 +979,8 @@ static struct hb_gpu_paint_funcs_lazy_loader_t hb_paint_funcs_set_push_transform_func (funcs, hb_gpu_paint_push_transform, nullptr, nullptr); hb_paint_funcs_set_pop_transform_func (funcs, hb_gpu_paint_pop_transform, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_gpu_paint_push_clip_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_gpu_paint_push_clip_path_start, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_gpu_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, hb_gpu_paint_pop_clip, nullptr, nullptr); hb_paint_funcs_set_push_group_func (funcs, hb_gpu_paint_push_group, nullptr, nullptr); hb_paint_funcs_set_pop_group_func (funcs, hb_gpu_paint_pop_group, nullptr, nullptr); @@ -1021,10 +1113,11 @@ hb_gpu_paint_get_user_data (const hb_gpu_paint_t *paint, /** * hb_gpu_paint_get_funcs: + * @paint: a GPU paint context. * - * Fetches the singleton #hb_paint_funcs_t that feeds paint data - * into an #hb_gpu_paint_t. Pass the #hb_gpu_paint_t as the - * @paint_data argument when calling the paint functions. + * Fetches the #hb_paint_funcs_t that feeds paint data into + * @paint. Pass @paint as the @paint_data argument when + * calling the paint functions. * * Return value: (transfer none): * The GPU paint functions @@ -1032,7 +1125,7 @@ hb_gpu_paint_get_user_data (const hb_gpu_paint_t *paint, * XSince: REPLACEME **/ hb_paint_funcs_t * -hb_gpu_paint_get_funcs (void) +hb_gpu_paint_get_funcs (const hb_gpu_paint_t *paint HB_UNUSED) { return static_gpu_paint_funcs.get_unconst (); } @@ -1181,7 +1274,7 @@ hb_gpu_paint_glyph_or_fail (hb_gpu_paint_t *paint, hb_font_get_scale (font, &x_scale, &y_scale); hb_gpu_paint_set_scale (paint, x_scale, y_scale); return hb_font_paint_glyph_or_fail (font, glyph, - hb_gpu_paint_get_funcs (), paint, + hb_gpu_paint_get_funcs (paint), paint, paint->palette, /* Foreground value is never read * from our encoded blob -- the @@ -1214,7 +1307,7 @@ hb_gpu_paint_glyph (hb_gpu_paint_t *paint, hb_font_get_scale (font, &x_scale, &y_scale); hb_gpu_paint_set_scale (paint, x_scale, y_scale); hb_font_paint_glyph (font, glyph, - hb_gpu_paint_get_funcs (), paint, + hb_gpu_paint_get_funcs (paint), paint, paint->palette, HB_COLOR (0, 0, 0, 0xff)); } @@ -1414,6 +1507,7 @@ hb_gpu_paint_clear (hb_gpu_paint_t *paint) hb_blob_destroy (b); paint->sub_blobs.reset (); paint->clip_depth = 0; + paint->pending_clip_path = false; paint->unsupported = false; paint->cur_transform = {1, 0, 0, 1, 0, 0}; paint->transform_stack.reset (); diff --git a/src/hb-gpu-paint.hh b/src/hb-gpu-paint.hh index b9a074f2f..120dc1088 100644 --- a/src/hb-gpu-paint.hh +++ b/src/hb-gpu-paint.hh @@ -283,13 +283,26 @@ struct hb_gpu_paint_t * we use for the gradient but not the clip outline. */ struct pending_clip_t { - hb_codepoint_t glyph; - hb_font_t *font; /* borrowed */ + hb_codepoint_t glyph; /* HB_CODEPOINT_INVALID for path clips */ + hb_font_t *font; /* borrowed; nullptr for path clips */ hb_transform_t transform; + /* Path clips are encoded into a sub-blob at push_clip_path_end + * time; glyph clips re-encode per consuming layer. For path + * clips, sub_blob_index is the pre-baked index and ext_* the + * captured design-unit extents. For glyph clips, sub_blob_index + * is -1 and the ext_* fields are unused. */ + int sub_blob_index; + int ext_x0, ext_y0, ext_x1, ext_y1; }; pending_clip_t clip_stack[3]; unsigned clip_depth = 0; + /* Carries the cur_transform captured at push_clip_path_start + * through to push_clip_path_end, where the encoded clip is + * committed to clip_stack. */ + hb_transform_t pending_clip_path_transform = {}; + bool pending_clip_path = false; + /* Set when the paint walk emits v1-only callbacks we do not yet * support. hb_gpu_paint_encode() returns NULL in that case. */ bool unsupported = false; diff --git a/src/hb-gpu.h b/src/hb-gpu.h index 66a8763e5..7ff18d633 100644 --- a/src/hb-gpu.h +++ b/src/hb-gpu.h @@ -120,7 +120,7 @@ hb_gpu_draw_get_scale (const hb_gpu_draw_t *draw, /* Draw */ HB_EXTERN hb_draw_funcs_t * -hb_gpu_draw_get_funcs (void); +hb_gpu_draw_get_funcs (const hb_gpu_draw_t *draw); HB_EXTERN void hb_gpu_draw_glyph (hb_gpu_draw_t *draw, @@ -137,7 +137,7 @@ hb_gpu_draw_glyph_or_fail (hb_gpu_draw_t *draw, * / hb_draw_line_to() / hb_draw_quadratic_to() / * hb_draw_cubic_to() / hb_draw_close_path() with the funcs from * hb_gpu_draw_get_funcs() and the hb_gpu_draw_t as data. The - * helpers hb_draw_line() / hb_draw_rect() / hb_draw_circle() + * helpers hb_draw_line() / hb_draw_rectangle() / hb_draw_circle() * cover common primitives (tapered lines, rectangles, circles). * * Coordinate system: the blob format quantizes coordinates to @@ -207,7 +207,7 @@ hb_gpu_paint_get_user_data (const hb_gpu_paint_t *paint, hb_user_data_key_t *key); HB_EXTERN hb_paint_funcs_t * -hb_gpu_paint_get_funcs (void); +hb_gpu_paint_get_funcs (const hb_gpu_paint_t *paint); HB_EXTERN void hb_gpu_paint_set_palette (hb_gpu_paint_t *paint, diff --git a/src/hb-paint.cc b/src/hb-paint.cc index ff7dceca8..f5ed66c6e 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -71,6 +71,15 @@ hb_paint_push_clip_rectangle_nil (hb_paint_funcs_t *funcs, void *paint_data, float xmin, float ymin, float xmax, float ymax, void *user_data) {} +static hb_draw_funcs_t * +hb_paint_push_clip_path_start_nil (hb_paint_funcs_t *funcs, void *paint_data, + void **draw_data, + void *user_data) { if (draw_data) *draw_data = nullptr; return nullptr; } + +static void +hb_paint_push_clip_path_end_nil (hb_paint_funcs_t *funcs, void *paint_data, + void *user_data) {} + static void hb_paint_pop_clip_nil (hb_paint_funcs_t *funcs, void *paint_data, void *user_data) {} @@ -581,6 +590,71 @@ hb_paint_push_clip_rectangle (hb_paint_funcs_t *funcs, void *paint_data, funcs->push_clip_rectangle (paint_data, xmin, ymin, xmax, ymax); } +/** + * hb_paint_push_clip_path_start: + * @funcs: paint functions + * @paint_data: associated data passed by the caller + * @draw_data: (out) (nullable): location to receive the draw data + * the caller should pass alongside the returned draw funcs. + * + * Begin clipping to an arbitrary path. Returns an + * #hb_draw_funcs_t owned by the backend (the caller must not + * free it) that the caller uses to emit the clip outline via + * hb_draw_*() calls, using the returned @draw_data as the + * draw data. The returned draw funcs and draw data are only + * valid until the matching hb_paint_push_clip_path_end() call; + * no other paint calls should be made between start and end + * except hb_draw_*() on the returned funcs. Finish the path + * with hb_paint_push_clip_path_end(); pop the clip later + * with hb_paint_pop_clip(). + * + * Usage: + * + * |[ + * hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pf, pd, &dd); + * hb_draw_move_to (df, dd, NULL, ...); + * hb_draw_line_to (df, dd, NULL, ...); + * ... + * hb_draw_close_path (df, dd, NULL); + * hb_paint_push_clip_path_end (pf, pd); + * /* paint ops here are clipped to the emitted path */ + * hb_paint_pop_clip (pf, pd); + * ]| + * + * Return value: (transfer none): draw funcs that accumulate + * the clip path, or `NULL` if the backend does not implement + * arbitrary-path clipping. + * + * XSince: REPLACEME + */ +hb_draw_funcs_t * +hb_paint_push_clip_path_start (hb_paint_funcs_t *funcs, + void *paint_data, + void **draw_data) +{ + void *scratch = nullptr; + if (!draw_data) draw_data = &scratch; + return funcs->push_clip_path_start (paint_data, draw_data); +} + +/** + * hb_paint_push_clip_path_end: + * @funcs: paint functions + * @paint_data: associated data passed by the caller + * + * Signal that the arbitrary-clip path started by + * hb_paint_push_clip_path_start() is fully drawn. The + * accumulated path now acts as a clip on the paint context + * until a matching hb_paint_pop_clip() call. + * + * XSince: REPLACEME + */ +void +hb_paint_push_clip_path_end (hb_paint_funcs_t *funcs, void *paint_data) +{ + funcs->push_clip_path_end (paint_data); +} + /** * hb_paint_pop_clip: * @funcs: paint functions diff --git a/src/hb-paint.h b/src/hb-paint.h index 6311418ba..b6a94d994 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -30,6 +30,7 @@ #define HB_PAINT_H #include "hb-common.h" +#include "hb-draw.h" HB_BEGIN_DECLS @@ -212,6 +213,53 @@ typedef void (*hb_paint_push_clip_rectangle_func_t) (hb_paint_funcs_t *funcs, float xmax, float ymax, void *user_data); +/** + * hb_paint_push_clip_path_start_func_t: + * @funcs: paint functions object + * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() + * @draw_data: (out): location to store the draw data the caller should + * pass alongside the returned draw funcs. + * @user_data: User data pointer passed to hb_paint_funcs_set_push_clip_path_start_func() + * + * A virtual method for the #hb_paint_funcs_t to begin clipping + * to an arbitrary path. The backend returns an + * #hb_draw_funcs_t it owns (the caller must not free it) + * that the caller feeds the clip outline to via hb_draw_*() + * calls, plus a @draw_data value to pass alongside those + * calls. Both are only valid until the matching + * #hb_paint_push_clip_path_end_func_t call; no other paint + * calls should be made in between. The clip remains + * in effect until a later #hb_paint_pop_clip_func_t call. + * + * Return value: (transfer none): draw funcs that accumulate + * the clip path, or `NULL` if arbitrary-path clipping is not + * supported. + * + * XSince: REPLACEME + */ +typedef hb_draw_funcs_t * (*hb_paint_push_clip_path_start_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + void **draw_data, + void *user_data); + +/** + * hb_paint_push_clip_path_end_func_t: + * @funcs: paint functions object + * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() + * @user_data: User data pointer passed to hb_paint_funcs_set_push_clip_path_end_func() + * + * A virtual method for the #hb_paint_funcs_t to close the + * clip path started by the #hb_paint_push_clip_path_start_func_t + * vfunc. The emitted path is now active as a clip; subsequent + * paint ops are masked by it until a matching + * #hb_paint_pop_clip_func_t call. + * + * XSince: REPLACEME + */ +typedef void (*hb_paint_push_clip_path_end_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + void *user_data); + /** * hb_paint_pop_clip_func_t: * @funcs: paint functions object @@ -219,8 +267,9 @@ typedef void (*hb_paint_push_clip_rectangle_func_t) (hb_paint_funcs_t *funcs, * @user_data: User data pointer passed to hb_paint_funcs_set_pop_clip_func() * * A virtual method for the #hb_paint_funcs_t to undo - * the effect of a prior call to the #hb_paint_funcs_push_clip_glyph_func_t - * or #hb_paint_funcs_push_clip_rectangle_func_t vfuncs. + * the effect of a prior call to the #hb_paint_funcs_push_clip_glyph_func_t, + * #hb_paint_funcs_push_clip_rectangle_func_t, or + * #hb_paint_funcs_push_clip_path_end_func_t vfuncs. * * Since: 7.0.0 */ @@ -819,6 +868,40 @@ hb_paint_funcs_set_push_clip_rectangle_func (hb_paint_funcs_t void *user_data, hb_destroy_func_t destroy); +/** + * hb_paint_funcs_set_push_clip_path_start_func: + * @funcs: A paint functions struct + * @func: (closure user_data) (destroy destroy) (scope notified): The push-clip-path-start callback + * @user_data: Data to pass to @func + * @destroy: (nullable): Function to call when @user_data is no longer needed + * + * Sets the push-clip-path-start callback on the paint functions struct. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_paint_funcs_set_push_clip_path_start_func (hb_paint_funcs_t *funcs, + hb_paint_push_clip_path_start_func_t func, + void *user_data, + hb_destroy_func_t destroy); + +/** + * hb_paint_funcs_set_push_clip_path_end_func: + * @funcs: A paint functions struct + * @func: (closure user_data) (destroy destroy) (scope notified): The push-clip-path-end callback + * @user_data: Data to pass to @func + * @destroy: (nullable): Function to call when @user_data is no longer needed + * + * Sets the push-clip-path-end callback on the paint functions struct. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_paint_funcs_set_push_clip_path_end_func (hb_paint_funcs_t *funcs, + hb_paint_push_clip_path_end_func_t func, + void *user_data, + hb_destroy_func_t destroy); + /** * hb_paint_funcs_set_pop_clip_func: * @funcs: A paint functions struct @@ -1024,6 +1107,15 @@ hb_paint_push_clip_rectangle (hb_paint_funcs_t *funcs, void *paint_data, float xmin, float ymin, float xmax, float ymax); +HB_EXTERN hb_draw_funcs_t * +hb_paint_push_clip_path_start (hb_paint_funcs_t *funcs, + void *paint_data, + void **draw_data); + +HB_EXTERN void +hb_paint_push_clip_path_end (hb_paint_funcs_t *funcs, + void *paint_data); + HB_EXTERN void hb_paint_pop_clip (hb_paint_funcs_t *funcs, void *paint_data); diff --git a/src/hb-paint.hh b/src/hb-paint.hh index bb499fc49..148bfc974 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -36,6 +36,8 @@ HB_PAINT_FUNC_IMPLEMENT (color_glyph) \ HB_PAINT_FUNC_IMPLEMENT (push_clip_glyph) \ HB_PAINT_FUNC_IMPLEMENT (push_clip_rectangle) \ + HB_PAINT_FUNC_IMPLEMENT (push_clip_path_start) \ + HB_PAINT_FUNC_IMPLEMENT (push_clip_path_end) \ HB_PAINT_FUNC_IMPLEMENT (pop_clip) \ HB_PAINT_FUNC_IMPLEMENT (color) \ HB_PAINT_FUNC_IMPLEMENT (image) \ @@ -103,6 +105,13 @@ struct hb_paint_funcs_t { func.push_clip_rectangle (this, paint_data, xmin, ymin, xmax, ymax, !user_data ? nullptr : user_data->push_clip_rectangle); } + hb_draw_funcs_t *push_clip_path_start (void *paint_data, + void **draw_data) + { return func.push_clip_path_start (this, paint_data, draw_data, + !user_data ? nullptr : user_data->push_clip_path_start); } + void push_clip_path_end (void *paint_data) + { func.push_clip_path_end (this, paint_data, + !user_data ? nullptr : user_data->push_clip_path_end); } void pop_clip (void *paint_data) { func.pop_clip (this, paint_data, !user_data ? nullptr : user_data->pop_clip); } diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index ba1873b4d..1150c99fe 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -935,18 +935,19 @@ free_static_raster_draw_funcs () /** * hb_raster_draw_get_funcs: + * @draw: a rasterizer draw context. * - * Fetches the singleton #hb_draw_funcs_t that feeds outline data - * into an #hb_raster_draw_t. Pass the #hb_raster_draw_t as the - * @draw_data argument when calling the draw functions. + * Fetches the #hb_draw_funcs_t that feeds outline data into + * @draw. Pass @draw as the @draw_data argument when calling + * the draw functions. * * Return value: (transfer none): * The rasterizer draw functions * - * Since: 13.0.0 + * XSince: REPLACEME **/ hb_draw_funcs_t * -hb_raster_draw_get_funcs (void) +hb_raster_draw_get_funcs (const hb_raster_draw_t *draw HB_UNUSED) { return static_raster_draw_funcs.get_unconst (); } @@ -987,7 +988,7 @@ hb_raster_draw_glyph_or_fail (hb_raster_draw_t *draw, dx + xx * pen_x + xy * pen_y, dy + yx * pen_x + yy * pen_y); hb_bool_t ret = hb_font_draw_glyph_or_fail (font, glyph, - hb_raster_draw_get_funcs (), draw); + hb_raster_draw_get_funcs (draw), draw); hb_raster_draw_set_transform (draw, xx, yx, xy, yy, dx, dy); return ret; } diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 46e972595..bfe6517b6 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -186,25 +186,17 @@ hb_raster_paint_push_empty_clip (hb_raster_paint_t *c, unsigned w, unsigned h) (void) c->clip_stack.push (std::move (new_clip)); } +/* Render whatever edges have been accumulated into @rdr and + * push the result as a new clip on the stack, intersecting + * with the existing clip. Used by the emitter-based clip + * helpers (push_clip_glyph) and by push_clip_path_end once + * the caller has drawn the path into @rdr. */ static void -hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c, - hb_raster_paint_clip_mask_emit_t emit, - void *emit_data) +hb_raster_paint_finalize_path_clip (hb_raster_paint_t *c, + hb_raster_draw_t *rdr, + hb_raster_image_t *surf, + unsigned w, unsigned h) { - ensure_initialized (c); - - hb_raster_image_t *surf = c->current_surface (); - if (unlikely (!surf)) return; - - unsigned w = surf->extents.width; - unsigned h = surf->extents.height; - - hb_raster_clip_t new_clip = c->acquire_clip (w, h); - - hb_raster_draw_t *rdr = c->clip_rdr; - hb_transform_t<> t = c->current_effective_transform (); - hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); - emit (rdr, emit_data); hb_raster_image_t *mask_img = hb_raster_draw_render (rdr); if (unlikely (!mask_img)) @@ -213,6 +205,8 @@ hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c, return; } + hb_raster_clip_t new_clip = c->acquire_clip (w, h); + /* Allocate alpha buffer and intersect with previous clip */ size_t clip_size = (size_t) new_clip.stride * h; if (unlikely (clip_size > HB_RASTER_MAX_BUFFER_SIZE || @@ -326,6 +320,27 @@ hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c, hb_raster_paint_push_empty_clip (c, w, h); } +static void +hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c, + hb_raster_paint_clip_mask_emit_t emit, + void *emit_data) +{ + ensure_initialized (c); + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return; + + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + + hb_raster_draw_t *rdr = c->clip_rdr; + hb_transform_t<> t = c->current_effective_transform (); + hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); + emit (rdr, emit_data); + + hb_raster_paint_finalize_path_clip (c, rdr, surf, w, h); +} + struct hb_raster_paint_glyph_clip_data_t { hb_codepoint_t glyph; @@ -337,7 +352,7 @@ hb_raster_paint_emit_clip_glyph_mask (hb_raster_draw_t *rdr, void *user_data) { hb_raster_paint_glyph_clip_data_t *data = (hb_raster_paint_glyph_clip_data_t *) user_data; /* Let draw-render choose tight glyph extents; we map by mask origin below. */ - hb_font_draw_glyph (data->font, data->glyph, hb_raster_draw_get_funcs (), rdr); + hb_font_draw_glyph (data->font, data->glyph, hb_raster_draw_get_funcs (rdr), rdr); } static void @@ -583,6 +598,50 @@ hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, hb_raster_paint_push_empty_clip (c, surf->extents.width, surf->extents.height); } +static hb_draw_funcs_t * +hb_raster_paint_push_clip_path_start (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void **draw_data, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + /* Prime clip_rdr with the current effective transform; the + * caller then drives hb_draw_*() into it, and _end renders + * the accumulated edges into a mask and intersects with the + * current clip. */ + if (unlikely (!c->surface_stack.length || !c->clip_rdr)) + { + *draw_data = nullptr; + return nullptr; + } + + hb_raster_draw_t *rdr = c->clip_rdr; + hb_transform_t<> t = c->current_effective_transform (); + hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); + + *draw_data = rdr; + return hb_raster_draw_get_funcs (rdr); +} + +static void +hb_raster_paint_push_clip_path_end (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf || !c->clip_rdr)) return; + + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + + hb_raster_paint_finalize_path_clip (c, c->clip_rdr, surf, w, h); +} + static void hb_raster_paint_pop_clip (hb_paint_funcs_t *pfuncs HB_UNUSED, void *paint_data, @@ -1626,6 +1685,8 @@ static struct hb_raster_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t hb_paint_funcs_set_color_glyph_func (funcs, hb_raster_paint_color_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_raster_paint_push_clip_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_raster_paint_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_raster_paint_push_clip_path_start, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_raster_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, hb_raster_paint_pop_clip, nullptr, nullptr); hb_paint_funcs_set_push_group_func (funcs, hb_raster_paint_push_group, nullptr, nullptr); hb_paint_funcs_set_pop_group_func (funcs, hb_raster_paint_pop_group, nullptr, nullptr); @@ -2104,18 +2165,19 @@ hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint, /** * hb_raster_paint_get_funcs: + * @paint: a rasterizer paint context. * - * Fetches the singleton #hb_paint_funcs_t that renders color glyphs - * into an #hb_raster_paint_t. Pass the #hb_raster_paint_t as the - * @paint_data argument when calling hb_font_paint_glyph(). + * Fetches the #hb_paint_funcs_t that renders color glyphs into + * @paint. Pass @paint as the @paint_data argument when calling + * hb_font_paint_glyph(). * * Return value: (transfer none): * The rasterizer paint functions * - * Since: 13.0.0 + * XSince: REPLACEME **/ hb_paint_funcs_t * -hb_raster_paint_get_funcs (void) +hb_raster_paint_get_funcs (const hb_raster_paint_t *paint HB_UNUSED) { return static_raster_paint_funcs.get_unconst (); } @@ -2152,11 +2214,11 @@ hb_raster_paint_glyph_impl (hb_raster_paint_t *paint, hb_bool_t ret = true; if (fallible) ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_raster_paint_get_funcs (), paint, + hb_raster_paint_get_funcs (paint), paint, paint->palette, paint->foreground); else hb_font_paint_glyph (font, glyph, - hb_raster_paint_get_funcs (), paint, + hb_raster_paint_get_funcs (paint), paint, paint->palette, paint->foreground); hb_raster_paint_set_transform (paint, xx, yx, xy, yy, dx, dy); return ret; diff --git a/src/hb-raster.h b/src/hb-raster.h index 6e4437be8..47caedb78 100644 --- a/src/hb-raster.h +++ b/src/hb-raster.h @@ -193,7 +193,7 @@ hb_raster_draw_set_glyph_extents (hb_raster_draw_t *draw, const hb_glyph_extents_t *glyph_extents); HB_EXTERN hb_draw_funcs_t * -hb_raster_draw_get_funcs (void); +hb_raster_draw_get_funcs (const hb_raster_draw_t *draw); HB_EXTERN void hb_raster_draw_glyph (hb_raster_draw_t *draw, @@ -314,7 +314,7 @@ hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint, hb_color_t color); HB_EXTERN hb_paint_funcs_t * -hb_raster_paint_get_funcs (void); +hb_raster_paint_get_funcs (const hb_raster_paint_t *paint); HB_EXTERN void hb_raster_paint_glyph (hb_raster_paint_t *paint, diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 66cc4fda5..36bd819a2 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -27,7 +27,7 @@ #include "hb.hh" #include "hb-vector-draw.hh" -#include "hb-vector-svg-path.hh" +#include "hb-vector-path.hh" #include #include @@ -510,17 +510,35 @@ hb_vector_draw_set_glyph_extents (hb_vector_draw_t *draw, return ret; } +/** + * hb_vector_draw_get_format: + * @draw: a vector draw context. + * + * Gets the output format @draw was created with. + * + * Return value: the output format. + * + * XSince: REPLACEME + */ +hb_vector_format_t +hb_vector_draw_get_format (const hb_vector_draw_t *draw) +{ + return draw->format; +} + /** * hb_vector_draw_get_funcs: + * @draw: a vector draw context. * - * Gets draw callbacks implemented by the vector draw backend. + * Gets draw callbacks for feeding outline data into @draw. + * Pass @draw as the @draw_data argument when calling them. * - * Return value: (transfer none): immutable #hb_draw_funcs_t singleton. + * Return value: (transfer none): immutable #hb_draw_funcs_t. * - * Since: 13.0.0 + * XSince: REPLACEME */ hb_draw_funcs_t * -hb_vector_draw_get_funcs (void) +hb_vector_draw_get_funcs (const hb_vector_draw_t *draw HB_UNUSED) { return hb_vector_draw_funcs_get (); } @@ -581,6 +599,29 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, } } + /* Flush any pending free-form path the caller accumulated + * via hb_draw_*() on hb_vector_draw_get_funcs() before this + * glyph clobbers the scratch buffer. Wrap the SVG case in + * the same scale(1,-1) Y-flip per-glyph applies, so + * the free-form coords share the glyph's coord system. */ + if (draw->path.length) + { + switch (draw->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "f\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "\"/>\n"); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } + draw->path.clear (); + } + switch (draw->format) { case HB_VECTOR_FORMAT_PDF: @@ -589,7 +630,6 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, hb_transform_t<> saved = draw->transform; draw->transform = {1, 0, 0, 1, pen_x, pen_y}; - draw->path.clear (); hb_font_draw_glyph (font, glyph, hb_vector_draw_funcs_get (), draw); draw->transform = saved; @@ -598,6 +638,7 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); hb_buf_append_str (&draw->body, "f\n"); + draw->path.clear (); return true; } @@ -606,8 +647,8 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, if (!hb_set_has (draw->defined_glyphs, glyph)) { draw->path.clear (); - hb_vector_svg_path_sink_t sink = {&draw->path, draw->precision}; - hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); + hb_vector_path_sink_t sink = {&draw->path, draw->precision, HB_VECTOR_FORMAT_SVG}; + hb_font_draw_glyph (font, glyph, hb_vector_path_draw_funcs_get (), &sink); if (!draw->path.length) return false; hb_buf_append_str (&draw->defs, "defs, draw->path.arrayZ, draw->path.length); hb_buf_append_str (&draw->defs, "\"/>\n"); hb_set_add (draw->defined_glyphs, glyph); + /* Clear so any subsequent free-form draw ops on this + * context start with an empty scratch. */ + draw->path.clear (); } float xx = draw->transform.xx; @@ -769,8 +813,11 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) if (draw->body.length) hb_buf_append_len (&stream, draw->body.arrayZ, draw->body.length); - else if (draw->path.length) + if (draw->path.length) { + /* Free-form draw ops accumulated after the last glyph + * (e.g. extents-overlay rectangles) need their own fill + * appended to the stream. */ hb_buf_append_len (&stream, draw->path.arrayZ, draw->path.length); hb_buf_append_str (&stream, "f\n"); } @@ -870,14 +917,17 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) } if (draw->body.length) - { hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); - } - else if (draw->path.length) + if (draw->path.length) { - hb_buf_append_str (&out, " elements apply scale(_, -sy) to convert, so wrap + * the leftover here in the same Y-flip. */ + hb_buf_append_str (&out, "path.arrayZ, draw->path.length); - hb_buf_append_str (&out, "\"/>\n"); + hb_buf_append_str (&out, "\"/>\n"); } hb_buf_append_str (&out, "\n"); diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index 83340fbe1..3d26d956e 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -310,7 +310,7 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, tmp.path.alloc (1024); hb_font_draw_glyph (font, glyph, - hb_vector_draw_get_funcs (), + hb_vector_draw_get_funcs (&tmp), &tmp); hb_buf_append_len (buf, tmp.path.arrayZ, tmp.path.length); @@ -398,6 +398,39 @@ hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, hb_buf_append_str (&body, " re W n\n"); } +static hb_draw_funcs_t * +hb_pdf_paint_push_clip_path_start (hb_paint_funcs_t *, + void *paint_data, + void **draw_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + { + *draw_data = nullptr; + return nullptr; + } + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + /* Stream path operators straight into the body; end() seals + * the path with "W n" to turn it into the clip region. */ + paint->clip_path_sink = {&body, paint->precision, HB_VECTOR_FORMAT_PDF}; + *draw_data = &paint->clip_path_sink; + return hb_vector_path_draw_funcs_get (); +} + +static void +hb_pdf_paint_push_clip_path_end (hb_paint_funcs_t *, + void *paint_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + hb_buf_append_str (&paint->current_body (), "W n\n"); +} + static void hb_pdf_paint_pop_clip (hb_paint_funcs_t *, void *paint_data, @@ -1217,6 +1250,8 @@ static struct hb_pdf_paint_funcs_lazy_loader_t hb_paint_funcs_set_pop_transform_func (funcs, (hb_paint_pop_transform_func_t) hb_pdf_paint_pop_transform, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, (hb_paint_push_clip_glyph_func_t) hb_pdf_paint_push_clip_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_rectangle_func (funcs, (hb_paint_push_clip_rectangle_func_t) hb_pdf_paint_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_start_func (funcs, (hb_paint_push_clip_path_start_func_t) hb_pdf_paint_push_clip_path_start, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_end_func (funcs, (hb_paint_push_clip_path_end_func_t) hb_pdf_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, (hb_paint_pop_clip_func_t) hb_pdf_paint_pop_clip, nullptr, nullptr); hb_paint_funcs_set_color_func (funcs, (hb_paint_color_func_t) hb_pdf_paint_color, nullptr, nullptr); hb_paint_funcs_set_image_func (funcs, (hb_paint_image_func_t) hb_pdf_paint_image, nullptr, nullptr); diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 09a815f5c..3435169f4 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -28,7 +28,7 @@ #include "hb-vector-paint.hh" #include "hb-paint.hh" -#include "hb-vector-svg-path.hh" +#include "hb-vector-path.hh" #include @@ -341,6 +341,8 @@ static void hb_vector_paint_push_transform (hb_paint_funcs_t *, void *, static void hb_vector_paint_pop_transform (hb_paint_funcs_t *, void *, void *); static void hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, void *, hb_codepoint_t, hb_font_t *, void *); static void hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, void *, float, float, float, float, void *); +static hb_draw_funcs_t * hb_vector_paint_push_clip_path_start (hb_paint_funcs_t *, void *, void **, void *); +static void hb_vector_paint_push_clip_path_end (hb_paint_funcs_t *, void *, void *); static void hb_vector_paint_pop_clip (hb_paint_funcs_t *, void *, void *); static void hb_vector_paint_color (hb_paint_funcs_t *, void *, hb_bool_t, hb_color_t, void *); static hb_bool_t hb_vector_paint_image (hb_paint_funcs_t *, void *, hb_blob_t *, unsigned, unsigned, hb_tag_t, float, hb_glyph_extents_t *, void *); @@ -363,6 +365,8 @@ static struct hb_vector_paint_funcs_lazy_loader_t hb_paint_funcs_set_pop_transform_func (funcs, (hb_paint_pop_transform_func_t) hb_vector_paint_pop_transform, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, (hb_paint_push_clip_glyph_func_t) hb_vector_paint_push_clip_glyph, nullptr, nullptr); hb_paint_funcs_set_push_clip_rectangle_func (funcs, (hb_paint_push_clip_rectangle_func_t) hb_vector_paint_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_start_func (funcs, (hb_paint_push_clip_path_start_func_t) hb_vector_paint_push_clip_path_start, nullptr, nullptr); + hb_paint_funcs_set_push_clip_path_end_func (funcs, (hb_paint_push_clip_path_end_func_t) hb_vector_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, (hb_paint_pop_clip_func_t) hb_vector_paint_pop_clip, nullptr, nullptr); hb_paint_funcs_set_color_func (funcs, (hb_paint_color_func_t) hb_vector_paint_color, nullptr, nullptr); hb_paint_funcs_set_image_func (funcs, (hb_paint_image_func_t) hb_vector_paint_image, nullptr, nullptr); @@ -498,8 +502,8 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, { hb_set_add (paint->defined_outlines, glyph); paint->path.clear (); - hb_vector_svg_path_sink_t sink = {&paint->path, paint->precision}; - hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); + hb_vector_path_sink_t sink = {&paint->path, paint->precision, HB_VECTOR_FORMAT_SVG}; + hb_font_draw_glyph (font, glyph, hb_vector_path_draw_funcs_get (), &sink); hb_buf_append_str (&paint->defs, "defs, pfx, pfx_len); hb_buf_append_c (&paint->defs, 'p'); @@ -565,6 +569,59 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, hb_buf_append_str (&paint->current_body (), ")\">\n"); } +static hb_draw_funcs_t * +hb_vector_paint_push_clip_path_start (hb_paint_funcs_t *, + void *paint_data, + void **draw_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + { + *draw_data = nullptr; + return nullptr; + } + + paint->path.clear (); + paint->clip_path_sink = {&paint->path, paint->precision, HB_VECTOR_FORMAT_SVG}; + *draw_data = &paint->clip_path_sink; + return hb_vector_path_draw_funcs_get (); +} + +static void +hb_vector_paint_push_clip_path_end (hb_paint_funcs_t *, + void *paint_data, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; + + const char *pfx = paint->id_prefix.arrayZ; + unsigned pfx_len = paint->id_prefix.length; + unsigned clip_id = paint->clip_path_counter++; + + /* The accumulated path is in font Y-up coords (the + * convention used inside per-glyph + * wrappers); this clip is emitted at base body level + * (free-form between glyphs), so flip its geometry inside + * the clipPath via the path's own transform attribute, + * keeping the body's at body Y-down. */ + hb_buf_append_str (&paint->defs, "defs, pfx, pfx_len); + hb_buf_append_str (&paint->defs, "cp"); + hb_buf_append_unsigned (&paint->defs, clip_id); + hb_buf_append_str (&paint->defs, "\">defs, paint->path.arrayZ, paint->path.length); + hb_buf_append_str (&paint->defs, "\"/>\n"); + + hb_buf_append_str (&paint->current_body (), "current_body (), pfx, pfx_len); + hb_buf_append_str (&paint->current_body (), "cp"); + hb_buf_append_unsigned (&paint->current_body (), clip_id); + hb_buf_append_str (&paint->current_body (), ")\">\n"); +} + static void hb_vector_paint_pop_clip (hb_paint_funcs_t *, void *paint_data, @@ -1299,19 +1356,45 @@ hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint) paint->custom_palette_colors.clear (); } +/** + * hb_vector_paint_get_format: + * @paint: a vector paint context. + * + * Gets the output format @paint was created with. + * + * Return value: the output format. + * + * XSince: REPLACEME + */ +hb_vector_format_t +hb_vector_paint_get_format (const hb_vector_paint_t *paint) +{ + return paint->format; +} + /** * hb_vector_paint_get_funcs: + * @paint: a vector paint context. * - * Gets paint callbacks implemented by the vector paint backend. + * Gets paint callbacks for emitting paint operations into @paint. + * Pass @paint as the @paint_data argument when calling them. * - * Return value: (transfer none): immutable #hb_paint_funcs_t singleton. + * Return value: (transfer none): immutable #hb_paint_funcs_t. * - * Since: 13.0.0 + * XSince: REPLACEME */ hb_paint_funcs_t * -hb_vector_paint_get_funcs (void) +hb_vector_paint_get_funcs (const hb_vector_paint_t *paint) { - return hb_vector_paint_funcs_get (); + switch (paint ? paint->format : HB_VECTOR_FORMAT_INVALID) + { + case HB_VECTOR_FORMAT_PDF: + return hb_vector_paint_pdf_funcs_get (); + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_INVALID: + default: + return hb_vector_paint_funcs_get (); + } } static hb_bool_t @@ -1443,12 +1526,12 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, hb_bool_t ret = true; if (fallible) ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (), paint, + hb_vector_paint_get_funcs (paint), paint, (unsigned) paint->palette, paint->foreground); else hb_font_paint_glyph (font, glyph, - hb_vector_paint_get_funcs (), paint, + hb_vector_paint_get_funcs (paint), paint, (unsigned) paint->palette, paint->foreground); if (unlikely (!ret)) @@ -1661,6 +1744,7 @@ hb_vector_paint_clear (hb_vector_paint_t *paint) paint->transform_group_depth = 0; paint->transform_group_overflow_depth = 0; paint->clip_rect_counter = 0; + paint->clip_path_counter = 0; paint->gradient_counter = 0; paint->color_glyph_counter = 0; paint->color_glyph_depth = 0; diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index f62809dc7..2b31d44dc 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -7,6 +7,7 @@ #include "hb-geometry.hh" #include "hb-machinery.hh" #include "hb-map.hh" +#include "hb-vector-path.hh" #include "hb-vector-svg-utils.hh" #include "hb-vector-svg.hh" @@ -61,6 +62,8 @@ struct hb_vector_paint_t unsigned transform_group_overflow_depth = 0; unsigned clip_rect_counter = 0; + unsigned clip_path_counter = 0; + hb_vector_path_sink_t clip_path_sink = {nullptr, 0, HB_VECTOR_FORMAT_INVALID}; unsigned gradient_counter = 0; unsigned color_glyph_counter = 0; unsigned color_glyph_depth = 0; diff --git a/src/hb-vector-path.cc b/src/hb-vector-path.cc new file mode 100644 index 000000000..a6447417a --- /dev/null +++ b/src/hb-vector-path.cc @@ -0,0 +1,216 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-geometry.hh" +#include "hb-machinery.hh" +#include "hb-vector-path.hh" +#include "hb-vector-svg-utils.hh" +#include "hb-vector-svg.hh" + +static inline void +hb_vector_path_append_xy (hb_vector_path_sink_t *s, float x, float y) +{ + hb_buf_append_num (s->path, x, s->precision); + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_buf_append_c (s->path, ' '); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, ','); + break; + case HB_VECTOR_FORMAT_INVALID: default: + break; + } + hb_buf_append_num (s->path, y, s->precision); +} + +static void +hb_vector_path_move_to (hb_draw_funcs_t *, + void *draw_data, + hb_draw_state_t *, + float to_x, float to_y, + void *) +{ + auto *s = (hb_vector_path_sink_t *) draw_data; + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_vector_path_append_xy (s, to_x, to_y); + hb_buf_append_str (s->path, " m\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, 'M'); + hb_vector_path_append_xy (s, to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } +} + +static void +hb_vector_path_line_to (hb_draw_funcs_t *, + void *draw_data, + hb_draw_state_t *, + float to_x, float to_y, + void *) +{ + auto *s = (hb_vector_path_sink_t *) draw_data; + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_vector_path_append_xy (s, to_x, to_y); + hb_buf_append_str (s->path, " l\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, 'L'); + hb_vector_path_append_xy (s, to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } +} + +static void +hb_vector_path_quadratic_to (hb_draw_funcs_t *, + void *draw_data, + hb_draw_state_t *st, + float cx, float cy, + float to_x, float to_y, + void *) +{ + auto *s = (hb_vector_path_sink_t *) draw_data; + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + { + /* PDF has no quadratic operator; promote to cubic using + * the current pen position from draw-state. */ + float sx = st->current_x, sy = st->current_y; + float c1x = sx + 2.f / 3.f * (cx - sx); + float c1y = sy + 2.f / 3.f * (cy - sy); + float c2x = to_x + 2.f / 3.f * (cx - to_x); + float c2y = to_y + 2.f / 3.f * (cy - to_y); + hb_vector_path_append_xy (s, c1x, c1y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, c2x, c2y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, to_x, to_y); + hb_buf_append_str (s->path, " c\n"); + break; + } + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, 'Q'); + hb_vector_path_append_xy (s, cx, cy); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } +} + +static void +hb_vector_path_cubic_to (hb_draw_funcs_t *, + void *draw_data, + hb_draw_state_t *, + float c1x, float c1y, + float c2x, float c2y, + float to_x, float to_y, + void *) +{ + auto *s = (hb_vector_path_sink_t *) draw_data; + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_vector_path_append_xy (s, c1x, c1y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, c2x, c2y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, to_x, to_y); + hb_buf_append_str (s->path, " c\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, 'C'); + hb_vector_path_append_xy (s, c1x, c1y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, c2x, c2y); + hb_buf_append_c (s->path, ' '); + hb_vector_path_append_xy (s, to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } +} + +static void +hb_vector_path_close_path (hb_draw_funcs_t *, + void *draw_data, + hb_draw_state_t *, + void *) +{ + auto *s = (hb_vector_path_sink_t *) draw_data; + switch (s->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_buf_append_str (s->path, "h\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (s->path, 'Z'); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } +} + +static inline void +free_static_vector_path_draw_funcs (); + +static struct hb_vector_path_draw_funcs_lazy_loader_t + : hb_draw_funcs_lazy_loader_t +{ + static hb_draw_funcs_t *create () + { + hb_draw_funcs_t *funcs = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) hb_vector_path_move_to, nullptr, nullptr); + hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) hb_vector_path_line_to, nullptr, nullptr); + hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) hb_vector_path_quadratic_to, nullptr, nullptr); + hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) hb_vector_path_cubic_to, nullptr, nullptr); + hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) hb_vector_path_close_path, nullptr, nullptr); + hb_draw_funcs_make_immutable (funcs); + hb_atexit (free_static_vector_path_draw_funcs); + return funcs; + } +} static_vector_path_draw_funcs; + +static inline void +free_static_vector_path_draw_funcs () +{ + static_vector_path_draw_funcs.free_instance (); +} + +hb_draw_funcs_t * +hb_vector_path_draw_funcs_get (void) +{ + return static_vector_path_draw_funcs.get_unconst (); +} diff --git a/src/hb-vector-svg-path.hh b/src/hb-vector-path.hh similarity index 71% rename from src/hb-vector-svg-path.hh rename to src/hb-vector-path.hh index bfa1573cf..9ec614958 100644 --- a/src/hb-vector-svg-path.hh +++ b/src/hb-vector-path.hh @@ -24,20 +24,27 @@ * Author(s): Behdad Esfahbod */ -#ifndef HB_VECTOR_SVG_PATH_HH -#define HB_VECTOR_SVG_PATH_HH +#ifndef HB_VECTOR_PATH_HH +#define HB_VECTOR_PATH_HH #include "hb.hh" #include "hb-vector.hh" +#include "hb-vector.h" #include "hb-draw.h" -struct hb_vector_svg_path_sink_t +/* Lightweight path sink: serializes hb_draw_* calls into an + * external char buffer as either SVG path-data or PDF path + * operators, depending on format. Used by the vector paint + * backends to accumulate arbitrary clip paths (and by the + * SVG backend to emit glyph outlines into defs). */ +struct hb_vector_path_sink_t { hb_vector_t *path; unsigned precision; + hb_vector_format_t format; }; HB_INTERNAL hb_draw_funcs_t * -hb_vector_svg_path_draw_funcs_get (void); +hb_vector_path_draw_funcs_get (void); -#endif /* HB_VECTOR_SVG_PATH_HH */ +#endif /* HB_VECTOR_PATH_HH */ diff --git a/src/hb-vector-svg-path.cc b/src/hb-vector-svg-path.cc deleted file mode 100644 index eee19d0ac..000000000 --- a/src/hb-vector-svg-path.cc +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright © 2026 Behdad Esfahbod - * - * This is part of HarfBuzz, a text shaping library. - * - * Permission is hereby granted, without written agreement and without - * license or royalty fees, to use, copy, modify, and distribute this - * software and its documentation for any purpose, provided that the - * above copyright notice and the following two paragraphs appear in - * all copies of this software. - * - * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR - * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES - * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN - * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS - * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO - * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. - * - * Author(s): Behdad Esfahbod - */ - -#include "hb.hh" - -#include "hb-machinery.hh" -#include "hb-vector-svg-path.hh" -#include "hb-vector-svg-utils.hh" - -static void -hb_vector_svg_path_move_to (hb_draw_funcs_t *, - void *draw_data, - hb_draw_state_t *, - float to_x, float to_y, - void *) -{ - auto *s = (hb_vector_svg_path_sink_t *) draw_data; - hb_buf_append_c (s->path, 'M'); - hb_buf_append_num (s->path, to_x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, to_y, s->precision); -} - -static void -hb_vector_svg_path_line_to (hb_draw_funcs_t *, - void *draw_data, - hb_draw_state_t *, - float to_x, float to_y, - void *) -{ - auto *s = (hb_vector_svg_path_sink_t *) draw_data; - hb_buf_append_c (s->path, 'L'); - hb_buf_append_num (s->path, to_x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, to_y, s->precision); -} - -static void -hb_vector_svg_path_quadratic_to (hb_draw_funcs_t *, - void *draw_data, - hb_draw_state_t *, - float cx, float cy, - float to_x, float to_y, - void *) -{ - auto *s = (hb_vector_svg_path_sink_t *) draw_data; - hb_buf_append_c (s->path, 'Q'); - hb_buf_append_num (s->path, cx, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, cy, s->precision); - hb_buf_append_c (s->path, ' '); - hb_buf_append_num (s->path, to_x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, to_y, s->precision); -} - -static void -hb_vector_svg_path_cubic_to (hb_draw_funcs_t *, - void *draw_data, - hb_draw_state_t *, - float c1x, float c1y, - float c2x, float c2y, - float to_x, float to_y, - void *) -{ - auto *s = (hb_vector_svg_path_sink_t *) draw_data; - hb_buf_append_c (s->path, 'C'); - hb_buf_append_num (s->path, c1x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, c1y, s->precision); - hb_buf_append_c (s->path, ' '); - hb_buf_append_num (s->path, c2x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, c2y, s->precision); - hb_buf_append_c (s->path, ' '); - hb_buf_append_num (s->path, to_x, s->precision); - hb_buf_append_c (s->path, ','); - hb_buf_append_num (s->path, to_y, s->precision); -} - -static void -hb_vector_svg_path_close_path (hb_draw_funcs_t *, - void *draw_data, - hb_draw_state_t *, - void *) -{ - auto *s = (hb_vector_svg_path_sink_t *) draw_data; - hb_buf_append_c (s->path, 'Z'); -} - -static inline void -free_static_svg_path_draw_funcs (); - -static struct hb_vector_svg_path_draw_funcs_lazy_loader_t - : hb_draw_funcs_lazy_loader_t -{ - static hb_draw_funcs_t *create () - { - hb_draw_funcs_t *funcs = hb_draw_funcs_create (); - hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) hb_vector_svg_path_move_to, nullptr, nullptr); - hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) hb_vector_svg_path_line_to, nullptr, nullptr); - hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) hb_vector_svg_path_quadratic_to, nullptr, nullptr); - hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) hb_vector_svg_path_cubic_to, nullptr, nullptr); - hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) hb_vector_svg_path_close_path, nullptr, nullptr); - hb_draw_funcs_make_immutable (funcs); - hb_atexit (free_static_svg_path_draw_funcs); - return funcs; - } -} static_svg_path_draw_funcs; - -static inline void -free_static_svg_path_draw_funcs () -{ - static_svg_path_draw_funcs.free_instance (); -} - -hb_draw_funcs_t * -hb_vector_svg_path_draw_funcs_get (void) -{ - return static_svg_path_draw_funcs.get_unconst (); -} diff --git a/src/hb-vector.h b/src/hb-vector.h index db95cc370..830289053 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -151,8 +151,11 @@ HB_EXTERN hb_bool_t hb_vector_draw_set_glyph_extents (hb_vector_draw_t *draw, const hb_glyph_extents_t *glyph_extents); +HB_EXTERN hb_vector_format_t +hb_vector_draw_get_format (const hb_vector_draw_t *draw); + HB_EXTERN hb_draw_funcs_t * -hb_vector_draw_get_funcs (void); +hb_vector_draw_get_funcs (const hb_vector_draw_t *draw); HB_EXTERN void hb_vector_draw_glyph (hb_vector_draw_t *draw, @@ -276,8 +279,11 @@ hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint, HB_EXTERN void hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint); +HB_EXTERN hb_vector_format_t +hb_vector_paint_get_format (const hb_vector_paint_t *paint); + HB_EXTERN hb_paint_funcs_t * -hb_vector_paint_get_funcs (void); +hb_vector_paint_get_funcs (const hb_vector_paint_t *paint); HB_EXTERN void hb_vector_paint_glyph (hb_vector_paint_t *paint, diff --git a/src/meson.build b/src/meson.build index 3f7a01c31..6d1cb22b1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -729,10 +729,10 @@ hb_vector_sources = files( 'hb-zlib.hh', 'hb-vector.cc', 'hb-vector-draw.cc', - 'hb-vector-svg-path.cc', + 'hb-vector-path.cc', 'hb-vector-paint.cc', 'hb-vector-paint-pdf.cc', - 'hb-vector-svg-path.hh', + 'hb-vector-path.hh', 'hb-vector-svg-utils.cc', 'hb-vector-svg-utils.hh', 'hb-static.cc', diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index a7d66dcd6..fd4a47ade 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -179,11 +179,11 @@ test_reset (void) static void test_draw_funcs (void) { - hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (nullptr); g_assert_nonnull (funcs); - /* Should be the same singleton each time. */ - g_assert_true (funcs == hb_gpu_draw_get_funcs ()); + /* Should be the same instance each time. */ + g_assert_true (funcs == hb_gpu_draw_get_funcs (nullptr)); } static void @@ -201,7 +201,7 @@ test_encode_quantizes_extents_outward (void) hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); g_assert_nonnull (draw); - hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (draw); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_draw_move_to (funcs, draw, &st, 0.24f, 0.24f); hb_draw_line_to (funcs, draw, &st, 0.76f, 0.24f); @@ -229,7 +229,7 @@ test_encode_preserves_touching_contours (void) hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); g_assert_nonnull (draw); - hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (draw); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_draw_move_to (funcs, draw, &st, 0.f, 0.f); @@ -256,7 +256,7 @@ test_recycle_blob (void) hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); g_assert_nonnull (draw); - hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (draw); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_draw_move_to (funcs, draw, &st, 0.f, 0.f); @@ -297,7 +297,7 @@ test_extents_saturate_overflow (void) hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); g_assert_nonnull (draw); - hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (draw); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_draw_move_to (funcs, draw, &st, -0x1p31f, 0.f); @@ -335,10 +335,10 @@ test_paint_create_destroy (void) static void test_paint_get_funcs (void) { - hb_paint_funcs_t *funcs = hb_gpu_paint_get_funcs (); + hb_paint_funcs_t *funcs = hb_gpu_paint_get_funcs (nullptr); g_assert_nonnull (funcs); - /* Singleton. */ - g_assert_true (funcs == hb_gpu_paint_get_funcs ()); + /* Same instance each call. */ + g_assert_true (funcs == hb_gpu_paint_get_funcs (nullptr)); } static void @@ -528,7 +528,7 @@ test_shapes (void) * the encoder's representable range. */ hb_gpu_draw_set_scale (draw, 1000, 1000); - hb_draw_funcs_t *dfuncs = hb_gpu_draw_get_funcs (); + hb_draw_funcs_t *dfuncs = hb_gpu_draw_get_funcs (draw); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; /* Tapered line. */ @@ -567,7 +567,7 @@ test_shapes (void) hb_blob_destroy (blob); /* Filled rect (stroke_width = NaN). */ - hb_draw_rect (dfuncs, draw, &st, + hb_draw_rectangle (dfuncs, draw, &st, 0.f, 0.f, 20.f, 10.f, (float) std::nan ("")); blob = hb_gpu_draw_encode (draw, &ext); @@ -578,7 +578,7 @@ test_shapes (void) /* Stroked rect: outer edge extends stroke_width/2 beyond the * nominal rect on each side. */ - hb_draw_rect (dfuncs, draw, &st, + hb_draw_rectangle (dfuncs, draw, &st, 0.f, 0.f, 20.f, 10.f, 2.f); blob = hb_gpu_draw_encode (draw, &ext); g_assert_nonnull (blob); diff --git a/test/api/test-paint.c b/test/api/test-paint.c index 77d283441..5a2e2b152 100644 --- a/test/api/test-paint.c +++ b/test/api/test-paint.c @@ -630,12 +630,176 @@ test_color_stops_ft (void) g_test_skip ("FreeType COLRv1 support not present"); } + +/* ── Arbitrary-path clip API (push_clip_path_start/_end) ─────────── */ + +static void +test_push_clip_path_nil_returns_null (void) +{ + /* Unset callbacks fall through to nil stubs; start must return + * NULL + clear draw_data so callers know the backend can't do + * arbitrary-path clipping. */ + hb_paint_funcs_t *funcs = hb_paint_funcs_create (); + void *draw_data = (void *) 0xdeadbeef; + hb_draw_funcs_t *df = hb_paint_push_clip_path_start (funcs, NULL, &draw_data); + g_assert_null (df); + g_assert_null (draw_data); + + /* end is a no-op on the nil stub. */ + hb_paint_push_clip_path_end (funcs, NULL); + + /* Also: NULL draw_data must be accepted; the entry point routes + * it through an internal scratch. */ + df = hb_paint_push_clip_path_start (funcs, NULL, NULL); + g_assert_null (df); + + hb_paint_funcs_destroy (funcs); +} + +typedef struct +{ + int move_to_calls; + int line_to_calls; + int close_path_calls; + float last_x, last_y; + hb_bool_t start_called; + hb_bool_t end_called; + int pop_clip_calls; + hb_draw_funcs_t *start_draw_funcs_out; +} clip_path_rec_t; + +static void +clip_path_sink_move_to (hb_draw_funcs_t *funcs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float x, float y, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) draw_data; + r->move_to_calls++; + r->last_x = x; r->last_y = y; +} + +static void +clip_path_sink_line_to (hb_draw_funcs_t *funcs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float x, float y, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) draw_data; + r->line_to_calls++; + r->last_x = x; r->last_y = y; +} + +static void +clip_path_sink_close_path (hb_draw_funcs_t *funcs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) draw_data; + r->close_path_calls++; +} + +static hb_draw_funcs_t * +clip_path_start_cb (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void **draw_data, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) paint_data; + r->start_called = TRUE; + *draw_data = r; + return r->start_draw_funcs_out; +} + +static void +clip_path_end_cb (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) paint_data; + r->end_called = TRUE; +} + +static void +clip_path_pop_clip_cb (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + clip_path_rec_t *r = (clip_path_rec_t *) paint_data; + r->pop_clip_calls++; +} + +/* Round-trip: registered start/end fire in order, the returned + * draw-funcs + draw-data propagate to the caller, and every + * hb_draw_*() call on those funcs reaches the accumulator. */ +static void +test_push_clip_path_round_trip (void) +{ + hb_draw_funcs_t *sink = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (sink, clip_path_sink_move_to, NULL, NULL); + hb_draw_funcs_set_line_to_func (sink, clip_path_sink_line_to, NULL, NULL); + hb_draw_funcs_set_close_path_func (sink, clip_path_sink_close_path, NULL, NULL); + hb_draw_funcs_make_immutable (sink); + + clip_path_rec_t r = {0}; + r.start_draw_funcs_out = sink; + + hb_paint_funcs_t *pfuncs = hb_paint_funcs_create (); + hb_paint_funcs_set_push_clip_path_start_func (pfuncs, clip_path_start_cb, NULL, NULL); + hb_paint_funcs_set_push_clip_path_end_func (pfuncs, clip_path_end_cb, NULL, NULL); + hb_paint_funcs_set_pop_clip_func (pfuncs, clip_path_pop_clip_cb, NULL, NULL); + hb_paint_funcs_make_immutable (pfuncs); + + void *draw_data = NULL; + hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pfuncs, &r, &draw_data); + + g_assert_true (r.start_called); + g_assert_nonnull (df); + g_assert_true (df == sink); + g_assert_true (draw_data == &r); + + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_draw_move_to (df, draw_data, &st, 1.f, 2.f); + hb_draw_line_to (df, draw_data, &st, 3.f, 4.f); + hb_draw_line_to (df, draw_data, &st, 5.f, 6.f); + hb_draw_close_path (df, draw_data, &st); + + g_assert_cmpint (r.move_to_calls, ==, 1); + /* hb_draw_close_path synthesizes a line_to back to the contour + * start, so our two explicit line_tos show up as three. */ + g_assert_cmpint (r.line_to_calls, ==, 3); + g_assert_cmpint (r.close_path_calls, ==, 1); + /* The synthetic closing line_to took the pen back to the + * move_to point (1, 2). */ + g_assert_cmpfloat (r.last_x, ==, 1.f); + g_assert_cmpfloat (r.last_y, ==, 2.f); + + hb_paint_push_clip_path_end (pfuncs, &r); + g_assert_true (r.end_called); + + hb_paint_pop_clip (pfuncs, &r); + g_assert_cmpint (r.pop_clip_calls, ==, 1); + + hb_paint_funcs_destroy (pfuncs); + hb_draw_funcs_destroy (sink); +} + + int main (int argc, char **argv) { int status = 0; hb_test_init (&argc, &argv); + + /* Register font-free API tests first so the missing-font bail + * below doesn't prevent them from running. */ + hb_test_add (test_push_clip_path_nil_returns_null); + hb_test_add (test_push_clip_path_round_trip); + for (unsigned int i = 0; i < G_N_ELEMENTS (paint_tests); i++) { hb_test_add_data_flavor (&paint_tests[i], paint_tests[i].output, test_hb_paint_ot); diff --git a/test/api/test-raster.cc b/test/api/test-raster.cc index 0d97c8e6b..144e84f05 100644 --- a/test/api/test-raster.cc +++ b/test/api/test-raster.cc @@ -36,7 +36,7 @@ static void draw_rect (hb_raster_draw_t *rdr, float x0, float y0, float x1, float y1) { - hb_draw_funcs_t *df = hb_raster_draw_get_funcs (); + hb_draw_funcs_t *df = hb_raster_draw_get_funcs (rdr); hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_draw_move_to (df, rdr, &st, x0, y0); diff --git a/util/gpu/demo-font.cc b/util/gpu/demo-font.cc index fd7f35f3c..879c8350b 100644 --- a/util/gpu/demo-font.cc +++ b/util/gpu/demo-font.cc @@ -4,6 +4,7 @@ #include "demo-font.h" #include "demo-atlas.h" +#include "../helper-extents-overlay.hh" #include @@ -120,34 +121,20 @@ _demo_font_upload_glyph (demo_font_t *font, { hb_glyph_extents_t hb_ext = {}; hb_blob_t *blob; + int x_scale, y_scale; + hb_font_get_scale (font->font, &x_scale, &y_scale); + float sw = (float) (x_scale * 0.01); if (font->draw_only) { hb_gpu_draw_clear (font->d); hb_gpu_draw_glyph (font->d, font->font, glyph_index); if (font->show_extents) { - /* Embed a stroked rectangle around the glyph's ink - * extents in the same blob. Pad the nominal rect - * outward by stroke_width / 2 so the stroke sits - * entirely outside the ink box (otherwise the inner - * half of the stroke would knock out against the - * glyph shape, since both fill in the same blob). */ - hb_glyph_extents_t g = {}; - if (hb_font_get_glyph_extents (font->font, glyph_index, &g) && - (g.width != 0 || g.height != 0)) - { - int x_scale, y_scale; - hb_font_get_scale (font->font, &x_scale, &y_scale); - float sw = (float) (x_scale * 0.01); - float half = 0.5f * sw; - float x = (float) g.x_bearing - half; - float y = (float) (g.y_bearing + g.height) - half; /* min_y */ - float w = (float) g.width + sw; - float h = (float) -g.height + sw; /* positive */ - hb_draw_funcs_t *dfuncs = hb_gpu_draw_get_funcs (); - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - hb_draw_rect (dfuncs, font->d, &st, x, y, w, h, sw); - } + hb_glyph_extents_t g; + if (hb_font_get_glyph_extents (font->font, glyph_index, &g)) + util_emit_extents_overlay_into_draw ( + hb_gpu_draw_get_funcs (font->d), font->d, + &g, /*pen_x*/ 0.f, /*pen_y*/ 0.f, sw); } blob = hb_gpu_draw_encode (font->d, &hb_ext); } @@ -155,6 +142,14 @@ _demo_font_upload_glyph (demo_font_t *font, { hb_gpu_paint_clear (font->p); hb_gpu_paint_glyph (font->p, font->font, glyph_index); + if (font->show_extents) + { + hb_glyph_extents_t g; + if (hb_font_get_glyph_extents (font->font, glyph_index, &g)) + util_emit_extents_overlay_into_paint ( + hb_gpu_paint_get_funcs (font->p), font->p, + &g, /*pen_x*/ 0.f, /*pen_y*/ 0.f, sw); + } blob = hb_gpu_paint_encode (font->p, &hb_ext); } unsigned int len = blob ? hb_blob_get_length (blob) : 0; @@ -164,8 +159,6 @@ _demo_font_upload_glyph (demo_font_t *font, glyph_info->extents.max_y = hb_ext.y_bearing; glyph_info->extents.min_y = hb_ext.y_bearing + hb_ext.height; glyph_info->advance = hb_font_get_glyph_h_advance (font->font, glyph_index); - int x_scale, y_scale; - hb_font_get_scale (font->font, &x_scale, &y_scale); glyph_info->upem = y_scale; glyph_info->is_empty = (len == 0); diff --git a/util/helper-extents-overlay.hh b/util/helper-extents-overlay.hh new file mode 100644 index 000000000..79eee0186 --- /dev/null +++ b/util/helper-extents-overlay.hh @@ -0,0 +1,98 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HELPER_EXTENTS_OVERLAY_HH +#define HELPER_EXTENTS_OVERLAY_HH + +#include + +#include + +/* --show-extents overlay helpers: a stroked rectangle around + * each glyph's ink box, plus a filled dot at the glyph's pen + * origin. Both render in the foreground color. + * + * In DRAW mode, the geometry goes straight into the glyph's + * draw context so it gets rendered/encoded alongside the + * outline in the foreground color. + * + * In PAINT mode, the same geometry feeds an arbitrary-path + * clip and the clipped region is filled with the paint's + * current foreground; the draw-mode and paint-mode emitters + * share all of the coord math. */ + +static inline void +util_emit_extents_overlay_into_draw (hb_draw_funcs_t *df, void *dd, + const hb_glyph_extents_t *ge, + float pen_x, float pen_y, + float stroke_width) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + + /* Stroked rectangle around the ink box. Padded outward by + * stroke_width / 2 so the stroke's inner edge sits at the + * box, instead of straddling it (and the inner half + * knocking out against glyph ink under non-zero winding). + * copysignf on the padding keeps the shift outward + * regardless of glyph-extent height-sign convention. */ + if (ge->width != 0 || ge->height != 0) + { + float half = 0.5f * stroke_width; + float sx = copysignf (half, ge->width ? (float) ge->width : 1.f); + float sy = copysignf (half, ge->height ? (float) ge->height : 1.f); + hb_draw_rectangle (df, dd, &st, + pen_x + (float) ge->x_bearing - sx, + pen_y + (float) ge->y_bearing - sy, + (float) ge->width + 2 * sx, + (float) ge->height + 2 * sy, + stroke_width); + } + + /* Filled dot at the glyph origin (pen_x, pen_y). Radius + * scales with stroke_width so the dot stays visible at the + * same render scale the stroke does. */ + hb_draw_circle (df, dd, &st, + pen_x, pen_y, + /*radius*/ 2.f * stroke_width, + /*stroke_width*/ NAN); +} + +static inline void +util_emit_extents_overlay_into_paint (hb_paint_funcs_t *pf, void *pd, + const hb_glyph_extents_t *ge, + float pen_x, float pen_y, + float stroke_width) +{ + void *dd = nullptr; + hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pf, pd, &dd); + if (!df) return; + util_emit_extents_overlay_into_draw (df, dd, ge, pen_x, pen_y, stroke_width); + hb_paint_push_clip_path_end (pf, pd); + hb_paint_color (pf, pd, /*is_foreground*/ 1, HB_COLOR (0, 0, 0, 255)); + hb_paint_pop_clip (pf, pd); +} + +#endif /* HELPER_EXTENTS_OVERLAY_HH */ diff --git a/util/raster-output.hh b/util/raster-output.hh index 380ec8647..969c883ff 100644 --- a/util/raster-output.hh +++ b/util/raster-output.hh @@ -30,6 +30,7 @@ #include "options.hh" #include "output-options.hh" #include "view-options.hh" +#include "helper-extents-overlay.hh" #include "helper-image-output.hh" #include "hb-raster.h" #include "hb-ot.h" @@ -249,6 +250,14 @@ struct raster_output_t : output_options_t, view_options_t float pen_y = g.y + off_y; hb_raster_draw_set_transform (rdr, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); hb_raster_draw_glyph (rdr, font, g.gid, pen_x, pen_y); + if (show_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (font, g.gid, &ge)) + util_emit_extents_overlay_into_draw ( + hb_raster_draw_get_funcs (rdr), rdr, + &ge, pen_x, pen_y, extents_stroke_width ()); + } } } @@ -268,13 +277,17 @@ struct raster_output_t : output_options_t, view_options_t private: struct glyph_instance_t { hb_codepoint_t gid; float x, y; }; - struct rect_i_t { int x0, y0, x1, y1; }; struct line_t { float advance_x = 0.f, advance_y = 0.f; std::vector glyphs; }; + float extents_stroke_width () const + { + return scalbnf (1.f, (int) subpixel_bits); + } + void finish_paint (float sx, float sy, float step, bool vertical, unsigned int num_iterations) { hb_raster_extents_t ext; @@ -321,6 +334,15 @@ struct raster_output_t : output_options_t, view_options_t hb_raster_paint_glyph (pnt, font, g.gid, pen_x, pen_y); + if (show_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (font, g.gid, &ge)) + util_emit_extents_overlay_into_paint ( + hb_raster_paint_get_funcs (pnt), pnt, + &ge, pen_x, pen_y, extents_stroke_width ()); + } + hb_raster_image_t *img = hb_raster_paint_render (pnt); if (img) { @@ -357,15 +379,7 @@ struct raster_output_t : output_options_t, view_options_t } if (iter + 1 == num_iterations) - { - if (show_extents) - { - std::vector rects; - collect_ink_extents_rects (sx, sy, step, vertical, ext, rects); - overlay_rects_bgra (out_buf, w, h, stride, rects); - } write_bgra_image (out_img); - } } hb_raster_image_destroy (out_img); } @@ -411,6 +425,14 @@ struct raster_output_t : output_options_t, view_options_t hb_raster_draw_set_extents (rdr, &ext); hb_raster_draw_set_transform (rdr, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); hb_raster_draw_glyph (rdr, font, g.gid, g.x + off_x, g.y + off_y); + if (show_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (font, g.gid, &ge)) + util_emit_extents_overlay_into_draw ( + hb_raster_draw_get_funcs (rdr), rdr, + &ge, g.x + off_x, g.y + off_y, extents_stroke_width ()); + } hb_raster_image_t *img = hb_raster_draw_render (rdr); if (!img) { glyph_index++; continue; } @@ -460,15 +482,7 @@ struct raster_output_t : output_options_t, view_options_t } if (iter + 1 == num_iterations) - { - if (show_extents) - { - std::vector rects; - collect_ink_extents_rects (sx, sy, step, vertical, ext, rects); - overlay_rects_bgra (out_buf, w, h, stride, rects); - } write_bgra_image (out_img); - } } hb_raster_draw_reset (rdr); @@ -606,110 +620,6 @@ struct raster_output_t : output_options_t, view_options_t return fg_color; } - void collect_ink_extents_rects (float sx, float sy, float step, bool vertical, - const hb_raster_extents_t &ext, - std::vector &rects) const - { - rects.clear (); - for (unsigned li = 0; li < lines.size (); li++) - { - float off_x = vertical ? -(step * (float) li) : 0.f; - float off_y = vertical ? 0.f : -(step * (float) li); - - for (const auto &g : lines[li].glyphs) - { - hb_glyph_extents_t gext; - if (!hb_font_get_glyph_extents (font, g.gid, &gext)) - continue; - - float gx = (g.x + off_x) * sx; - float gy = (g.y + off_y) * sy; - float x0 = gx + gext.x_bearing * sx; - float y0 = gy + (gext.y_bearing + gext.height) * sy; - float x1 = gx + (gext.x_bearing + gext.width) * sx; - float y1 = gy + gext.y_bearing * sy; - - int ix0 = (int) floorf (hb_min (x0, x1)) - ext.x_origin; - int iy0 = (int) floorf (hb_min (y0, y1)) - ext.y_origin; - int ix1 = (int) ceilf (hb_max (x0, x1)) - ext.x_origin; - int iy1 = (int) ceilf (hb_max (y0, y1)) - ext.y_origin; - rects.push_back ({ix0, iy0, ix1, iy1}); - } - } - } - - static void overlay_rects_bgra (uint8_t *buf, unsigned w, unsigned h, unsigned stride, - const std::vector &rects) - { - for (const auto &rc : rects) - { - int x0 = hb_max (0, hb_min ((int) w, rc.x0)); - int x1 = hb_max (0, hb_min ((int) w, rc.x1)); - int y0 = hb_max (0, hb_min ((int) h, rc.y0)); - int y1 = hb_max (0, hb_min ((int) h, rc.y1)); - if (x1 - x0 < 1 || y1 - y0 < 1) continue; - - auto blend_at = [&] (int x, int y) - { - uint32_t d; - hb_memcpy (&d, buf + y * stride + x * 4, 4); - uint8_t db = (uint8_t) (d & 0xFF); - uint8_t dg = (uint8_t) ((d >> 8) & 0xFF); - uint8_t dr = (uint8_t) ((d >> 16) & 0xFF); - uint8_t da = (uint8_t) (d >> 24); - uint8_t ob = (uint8_t) ((db + 255) / 2); - uint8_t og = (uint8_t) (dg / 2); - uint8_t orr = (uint8_t) ((dr + 255) / 2); - uint8_t oa = hb_max (da, (uint8_t) 128); - uint32_t o = (uint32_t) ob | ((uint32_t) og << 8) | ((uint32_t) orr << 16) | ((uint32_t) oa << 24); - hb_memcpy (buf + y * stride + x * 4, &o, 4); - }; - - for (int x = x0; x < x1; x++) - { - blend_at (x, y0); - blend_at (x, y1 - 1); - } - for (int y = y0; y < y1; y++) - { - blend_at (x0, y); - blend_at (x1 - 1, y); - } - } - } - - static void overlay_rects_rgb (std::vector &buf, unsigned w, unsigned h, - const std::vector &rects) - { - for (const auto &rc : rects) - { - int x0 = hb_max (0, hb_min ((int) w, rc.x0)); - int x1 = hb_max (0, hb_min ((int) w, rc.x1)); - int y0 = hb_max (0, hb_min ((int) h, rc.y0)); - int y1 = hb_max (0, hb_min ((int) h, rc.y1)); - if (x1 - x0 < 1 || y1 - y0 < 1) continue; - - auto blend_at = [&] (int x, int y) - { - uint8_t *p = &buf[(y * w + x) * 3]; - p[0] = (uint8_t) ((p[0] + 255) / 2); - p[1] = (uint8_t) (p[1] / 2); - p[2] = (uint8_t) ((p[2] + 255) / 2); - }; - - for (int x = x0; x < x1; x++) - { - blend_at (x, y0); - blend_at (x, y1 - 1); - } - for (int y = y0; y < y1; y++) - { - blend_at (x0, y); - blend_at (x1 - 1, y); - } - } - } - static uint8_t mul8 (uint8_t a, uint8_t b) { unsigned t = (unsigned) a * b + 128; @@ -918,13 +828,6 @@ struct raster_output_t : output_options_t, view_options_t hb_memcpy (dst + y * bgra_actual_ext.stride + x * 4, &px, 4); } - if (show_extents) - { - std::vector rects; - collect_ink_extents_rects (sx, sy, step, vertical, ext, rects); - overlay_rects_bgra (dst, ext.width, ext.height, bgra_actual_ext.stride, rects); - } - write_bgra_image (bgra); hb_raster_image_destroy (bgra); } diff --git a/util/vector-output.hh b/util/vector-output.hh index 3e8f20cbe..00488333b 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -30,6 +30,7 @@ #include #include +#include "helper-extents-overlay.hh" #include "output-options.hh" #include "view-options.hh" #include "hb-vector.h" @@ -282,12 +283,28 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, extents_mode); had_paint = true; + if (show_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (upem_font, g.gid, &ge)) + util_emit_extents_overlay_into_paint ( + hb_vector_paint_get_funcs (paint), paint, + &ge, pen_x, pen_y, (float) normalized_stroke_width); + } } else { hb_vector_draw_glyph (draw, upem_font, g.gid, pen_x, pen_y, extents_mode); had_draw = true; + if (show_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (upem_font, g.gid, &ge)) + util_emit_extents_overlay_into_draw ( + hb_vector_draw_get_funcs (draw), draw, + &ge, pen_x, pen_y, (float) normalized_stroke_width); + } } } } @@ -897,9 +914,6 @@ struct vector_output_t : output_options_t<>, view_options_t if (is_draw_blob && !(foreground_use_palette && foreground_palette && foreground_palette->len)) emit_fill_group_close (); - if (show_extents) - emit_extents_overlay (direction == HB_DIRECTION_INVALID ? HB_DIRECTION_LTR : direction, line_step); - fputs ("\n", out_fp); } @@ -949,9 +963,6 @@ struct vector_output_t : output_options_t<>, view_options_t if (paint_body.len) write_slice_resolving_palette_vars (paint_body); - if (show_extents) - emit_extents_overlay (direction == HB_DIRECTION_INVALID ? HB_DIRECTION_LTR : direction, line_step); - fputs ("\n", out_fp); } @@ -1032,54 +1043,6 @@ struct vector_output_t : output_options_t<>, view_options_t } } - void emit_extents_overlay (hb_direction_t dir, float step) - { - const bool vertical = HB_DIRECTION_IS_VERTICAL (dir); - const float dot_r = hb_max (1.f, step * 0.01f); - fputs ("\n", out_fp); - for (unsigned li = 0; li < lines.size (); li++) - { - float off_x = vertical ? -(step * (float) li) : 0.f; - float off_y = vertical ? 0.f : -(step * (float) li); - const line_t &line = lines[li]; - for (const auto &g : line.glyphs) - { - fprintf (out_fp, "\n", - precision + 4, (double) (g.x + off_x), - precision + 4, (double) (-(g.y + off_y)), - precision + 4, (double) dot_r); - } - } - fputs ("\n", out_fp); - - fprintf (out_fp, "\n", - precision + 4, normalized_stroke_width); - for (unsigned li = 0; li < lines.size (); li++) - { - float off_x = vertical ? -(step * (float) li) : 0.f; - float off_y = vertical ? 0.f : -(step * (float) li); - const line_t &line = lines[li]; - for (const auto &g : line.glyphs) - { - hb_glyph_extents_t ge; - if (!hb_font_get_glyph_extents (upem_font, g.gid, &ge)) - continue; - float x = g.x + off_x + ge.x_bearing; - float y = g.y + off_y + ge.y_bearing; - float w = ge.width; - float h = ge.height; - if (!w || !h) - continue; - fprintf (out_fp, "\n", - precision + 4, (double) x, - precision + 4, (double) (-y), - precision + 4, (double) w, - precision + 4, (double) (-h)); - } - } - fputs ("\n", out_fp); - } - void get_line_metrics (hb_direction_t dir, float *asc, float *desc, float *gap) const { if (have_font_extents) diff --git a/util/view-cairo.hh b/util/view-cairo.hh index fd407165a..7766e19b7 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -108,20 +108,16 @@ view_cairo_t::draw_lines (cairo_t *cr, hb_font_t *font, double leading, int vert { cairo_save (cr); - cairo_set_source_rgba (cr, 1., 0., 0., .5); - cairo_set_line_width (cr, 10); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - for (unsigned i = 0; i < l.num_glyphs; i++) { - cairo_move_to (cr, l.glyphs[i].x, l.glyphs[i].y); - cairo_rel_line_to (cr, 0, 0); - } - cairo_stroke (cr); - - cairo_restore (cr); - cairo_save (cr); - - cairo_set_source_rgba (cr, 1., 0., 1., .5); - cairo_set_line_width (cr, 3); + /* Foreground for parity with the other utils' --show- + * extents overlay (raster, vector, gpu). Stroked rect + * around each glyph's ink box, plus a filled dot at the + * pen origin. */ + cairo_set_source_rgba (cr, + foreground_color.r / 255., + foreground_color.g / 255., + foreground_color.b / 255., + foreground_color.a / 255.); + cairo_set_line_width (cr, 1); for (unsigned i = 0; i < l.num_glyphs; i++) { hb_glyph_extents_t hb_extents; @@ -137,6 +133,13 @@ view_cairo_t::draw_lines (cairo_t *cr, hb_font_t *font, double leading, int vert } cairo_stroke (cr); + /* Filled origin dot. */ + for (unsigned i = 0; i < l.num_glyphs; i++) + { + cairo_arc (cr, l.glyphs[i].x, l.glyphs[i].y, 2., 0., 2 * G_PI); + cairo_fill (cr); + } + cairo_restore (cr); }