diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ae6aa06e..d9063fc85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,9 +282,10 @@ set (vector_project_sources ${PROJECT_SOURCE_DIR}/src/hb-zlib.cc ${PROJECT_SOURCE_DIR}/src/hb-zlib.hh ${PROJECT_SOURCE_DIR}/src/hb-vector.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-draw.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-draw.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-path.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-paint.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-svg-subset.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-subset.hh diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 26f8d3af9..66477f7dd 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -291,6 +291,8 @@ hb_paint_funcs_set_sweep_gradient_func hb_paint_composite_mode_t hb_paint_push_group_func_t hb_paint_funcs_set_push_group_func +hb_paint_push_group_for_func_t +hb_paint_funcs_set_push_group_for_func hb_paint_pop_group_func_t hb_paint_funcs_set_pop_group_func hb_paint_custom_palette_color_func_t @@ -310,6 +312,7 @@ hb_paint_linear_gradient hb_paint_radial_gradient hb_paint_sweep_gradient hb_paint_push_group +hb_paint_push_group_for hb_paint_pop_group hb_paint_custom_palette_color @@ -1078,8 +1081,8 @@ hb_vector_draw_get_extents hb_vector_draw_set_glyph_extents hb_vector_draw_get_funcs hb_vector_draw_glyph -hb_vector_svg_set_flat -hb_vector_svg_set_precision +hb_vector_draw_set_flat +hb_vector_draw_set_precision hb_vector_draw_render hb_vector_draw_reset hb_vector_draw_recycle_blob @@ -1102,8 +1105,8 @@ hb_vector_paint_set_custom_palette_color hb_vector_paint_clear_custom_palette_colors hb_vector_paint_get_funcs hb_vector_paint_glyph -hb_vector_svg_paint_set_flat -hb_vector_svg_paint_set_precision +hb_vector_paint_set_flat +hb_vector_paint_set_precision hb_vector_paint_render hb_vector_paint_reset hb_vector_paint_recycle_blob diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index c3b5808eb..62d3a3ce1 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -1526,9 +1526,9 @@ struct PaintComposite void paint_glyph (hb_paint_context_t *c) const { TRACE_PAINT (this); - c->funcs->push_group (c->data); + c->funcs->push_group_for (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER); c->recurse (this+backdrop); - c->funcs->push_group (c->data); + c->funcs->push_group_for (c->data, (hb_paint_composite_mode_t) (int) mode); c->recurse (this+src); c->funcs->pop_group (c->data, (hb_paint_composite_mode_t) (int) mode); c->funcs->pop_group (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER); diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index fb730f86b..156231773 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -165,8 +165,9 @@ #ifdef HB_HAS_VECTOR #include "hb-static.cc" -#include "hb-vector-svg-draw.cc" -#include "hb-vector-svg-paint.cc" +#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-svg-subset.cc" #include "hb-vector-svg-utils.cc" diff --git a/src/hb-paint.cc b/src/hb-paint.cc index ffdd3c3a5..68fc0cade 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -118,6 +118,14 @@ static void hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data, void *user_data) {} +static void +hb_paint_push_group_for_nil (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode, + void *user_data) +{ + hb_paint_push_group (funcs, paint_data); +} + static void hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode, @@ -723,6 +731,25 @@ hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data) funcs->push_group (paint_data); } +/** + * hb_paint_push_group_for: + * @funcs: paint functions + * @paint_data: associated data passed by the caller + * @mode: the compositing mode that will be used when the group is popped + * + * Perform a "push-group" paint operation, with the compositing + * mode known in advance. By default, this calls + * hb_paint_push_group(). + * + * XSince: REPLACEME + */ +void +hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode) +{ + funcs->push_group_for (paint_data, mode); +} + /** * hb_paint_pop_group: * @funcs: paint functions diff --git a/src/hb-paint.h b/src/hb-paint.h index 787118c46..4fd6c6fbc 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -660,6 +660,28 @@ typedef void (*hb_paint_push_group_func_t) (hb_paint_funcs_t *funcs, void *paint_data, void *user_data); +/** + * hb_paint_push_group_for_func_t: + * @funcs: paint functions object + * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() + * @mode: the compositing mode that will be used when the group is popped + * @user_data: User data pointer passed to hb_paint_funcs_set_push_group_for_func() + * + * A virtual method for the #hb_paint_funcs_t to use + * an intermediate surface for subsequent paint calls, + * with the compositing mode known in advance. + * + * This is like #hb_paint_push_group_func_t, but the + * compositing mode is provided at push time. By default + * this calls #hb_paint_push_group_func_t. + * + * XSince: REPLACEME + */ +typedef void (*hb_paint_push_group_for_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + hb_paint_composite_mode_t mode, + void *user_data); + /** * hb_paint_pop_group_func_t: * @funcs: paint functions object @@ -916,6 +938,23 @@ hb_paint_funcs_set_push_group_func (hb_paint_funcs_t *funcs, void *user_data, hb_destroy_func_t destroy); +/** + * hb_paint_funcs_set_push_group_for_func: + * @funcs: A paint functions struct + * @func: (closure user_data) (destroy destroy) (scope notified): The push-group-for callback + * @user_data: Data to pass to @func + * @destroy: (nullable): Function to call when @user_data is no longer needed + * + * Sets the push-group-for callback on the paint functions struct. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_paint_funcs_set_push_group_for_func (hb_paint_funcs_t *funcs, + hb_paint_push_group_for_func_t func, + void *user_data, + hb_destroy_func_t destroy); + /** * hb_paint_funcs_set_pop_group_func: * @funcs: A paint functions struct @@ -1026,6 +1065,10 @@ hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data, HB_EXTERN void hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data); +HB_EXTERN void +hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data, + hb_paint_composite_mode_t mode); + HB_EXTERN void hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode); diff --git a/src/hb-paint.hh b/src/hb-paint.hh index bbeeb0319..be4655b7b 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -43,6 +43,7 @@ HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \ HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \ HB_PAINT_FUNC_IMPLEMENT (push_group) \ + HB_PAINT_FUNC_IMPLEMENT (push_group_for) \ HB_PAINT_FUNC_IMPLEMENT (pop_group) \ HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \ /* ^--- Add new callbacks here */ @@ -146,6 +147,11 @@ struct hb_paint_funcs_t void push_group (void *paint_data) { func.push_group (this, paint_data, !user_data ? nullptr : user_data->push_group); } + void push_group_for (void *paint_data, + hb_paint_composite_mode_t mode) + { func.push_group_for (this, paint_data, + mode, + !user_data ? nullptr : user_data->push_group_for); } void pop_group (void *paint_data, hb_paint_composite_mode_t mode) { func.pop_group (this, paint_data, diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh index 5589f1d4c..6ed02a0b2 100644 --- a/src/hb-raster-svg-context.hh +++ b/src/hb-raster-svg-context.hh @@ -85,10 +85,10 @@ struct hb_svg_render_context_t { hb_paint_pop_transform (pfuncs, paint); } - bool push_group () + bool push_group (hb_paint_composite_mode_t mode = HB_PAINT_COMPOSITE_MODE_SRC_OVER) { unsigned depth = paint->surface_stack.length; - hb_paint_push_group (pfuncs, paint); + hb_paint_push_group_for (pfuncs, paint, mode); return paint->surface_stack.length > depth; } void pop_group (hb_paint_composite_mode_t mode) diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-draw.cc similarity index 59% rename from src/hb-vector-svg-draw.cc rename to src/hb-vector-draw.cc index 82f31b4f9..9103afcb2 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-draw.cc @@ -26,21 +26,16 @@ #include "hb.hh" -#include "hb-vector.h" +#include "hb-vector-draw.hh" #include "hb-blob.hh" -#include "hb-geometry.hh" -#include "hb-machinery.hh" #include "hb-map.hh" #include "hb-vector-svg-path.hh" #include "hb-vector-svg-subset.hh" -#include "hb-vector-svg-utils.hh" #include #include #include -#include "hb-vector-svg.hh" - HB_UNUSED static inline bool hb_svg_buffer_contains (const hb_vector_t &buf, const char *needle) { @@ -55,35 +50,6 @@ hb_svg_buffer_contains (const hb_vector_t &buf, const char *needle) return false; } -struct hb_vector_draw_t -{ - hb_object_header_t header; - - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; - hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; - float x_scale_factor = 1.f; - float y_scale_factor = 1.f; - hb_vector_extents_t extents = {0, 0, 0, 0}; - bool has_extents = false; - unsigned precision = 2; - bool flat = false; - - hb_vector_t defs; - hb_vector_t body; - hb_vector_t path; - hb_set_t *defined_glyphs = nullptr; - hb_blob_t *recycled_blob = nullptr; - - void append_xy (float x, float y) - { - float tx, ty; - hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); - hb_svg_append_num (&path, tx, precision); - hb_svg_append_c (&path, ','); - hb_svg_append_num (&path, ty, precision); - } -}; - static void hb_vector_draw_move_to (hb_draw_funcs_t *, void *draw_data, @@ -92,8 +58,18 @@ hb_vector_draw_move_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'M'); - d->append_xy (to_x, to_y); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " m\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'M'); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static void @@ -104,23 +80,55 @@ hb_vector_draw_line_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'L'); - d->append_xy (to_x, to_y); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " l\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'L'); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static void hb_vector_draw_quadratic_to (hb_draw_funcs_t *, void *draw_data, - hb_draw_state_t *, + hb_draw_state_t *st, float cx, float cy, float to_x, float to_y, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'Q'); - d->append_xy (cx, cy); - hb_svg_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + { + /* PDF has no quadratic operator; promote to cubic. */ + 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); + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " c\n"); + break; + } + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'Q'); + d->append_xy (cx, cy); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static void @@ -133,12 +141,26 @@ hb_vector_draw_cubic_to (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'C'); - d->append_xy (c1x, c1y); - hb_svg_append_c (&d->path, ' '); - d->append_xy (c2x, c2y); - hb_svg_append_c (&d->path, ' '); - d->append_xy (to_x, to_y); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + hb_buf_append_str (&d->path, " c\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'C'); + d->append_xy (c1x, c1y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (c2x, c2y); + hb_buf_append_c (&d->path, ' '); + d->append_xy (to_x, to_y); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static void @@ -148,7 +170,16 @@ hb_vector_draw_close_path (hb_draw_funcs_t *, void *) { auto *d = (hb_vector_draw_t *) draw_data; - hb_svg_append_c (&d->path, 'Z'); + switch (d->format) + { + case HB_VECTOR_FORMAT_PDF: + hb_buf_append_str (&d->path, "h\n"); + break; + case HB_VECTOR_FORMAT_SVG: + hb_buf_append_c (&d->path, 'Z'); + break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } } static inline void free_static_vector_draw_funcs (); @@ -195,8 +226,14 @@ hb_vector_draw_funcs_get () hb_vector_draw_t * hb_vector_draw_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG) - return nullptr; + switch (format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; + } hb_vector_draw_t *draw = hb_object_create (); if (unlikely (!draw)) @@ -503,8 +540,14 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, float pen_y, hb_vector_extents_mode_t extents_mode) { - if (draw->format != HB_VECTOR_FORMAT_SVG) - return false; + switch (draw->format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return false; + } if (extents_mode == HB_VECTOR_EXTENTS_MODE_EXPAND) { @@ -530,160 +573,210 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, } } - bool needs_def = !draw->flat && !hb_set_has (draw->defined_glyphs, glyph); - if (needs_def) + switch (draw->format) { - draw->path.clear (); - hb_svg_path_sink_t sink = {&draw->path, draw->precision}; - hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - if (!draw->path.length) + case HB_VECTOR_FORMAT_PDF: + { + /* PDF: always inline. Pen and font coords are both Y-up. */ + 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; + + if (!draw->path.length) + return false; + + hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "f\n"); + return true; + } + + case HB_VECTOR_FORMAT_SVG: + { + bool needs_def = !draw->flat && !hb_set_has (draw->defined_glyphs, glyph); + if (needs_def) + { + draw->path.clear (); + hb_svg_path_sink_t sink = {&draw->path, draw->precision}; + hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); + if (!draw->path.length) + return false; + hb_buf_append_str (&draw->defs, "defs, glyph); + hb_buf_append_str (&draw->defs, "\" d=\""); + hb_buf_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->defs, "\"/>\n"); + hb_set_add (draw->defined_glyphs, glyph); + } + + if (draw->flat) + { + draw->path.clear (); + hb_svg_path_sink_t sink = {&draw->path, draw->precision}; + hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); + + if (!draw->path.length) + return false; + + float xx = draw->transform.xx; + float yx = draw->transform.yx; + float xy = draw->transform.xy; + float yy = draw->transform.yy; + float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; + float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; + + hb_buf_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&draw->body, "\" transform=\""); + hb_svg_append_instance_transform (&draw->body, + draw->precision, + draw->x_scale_factor, + draw->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&draw->body, "\"/>\n"); + return true; + } + + float xx = draw->transform.xx; + float yx = draw->transform.yx; + float xy = draw->transform.xy; + float yy = draw->transform.yy; + float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; + float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; + + hb_buf_append_str (&draw->body, "body, glyph); + hb_buf_append_str (&draw->body, "\" transform=\""); + hb_svg_append_instance_transform (&draw->body, + draw->precision, + draw->x_scale_factor, + draw->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&draw->body, "\"/>\n"); + return true; + } + + case HB_VECTOR_FORMAT_INVALID: default: return false; - hb_svg_append_str (&draw->defs, "defs, glyph); - hb_svg_append_str (&draw->defs, "\" d=\""); - hb_svg_append_len (&draw->defs, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&draw->defs, "\"/>\n"); - hb_set_add (draw->defined_glyphs, glyph); } - - if (draw->flat) - { - draw->path.clear (); - hb_svg_path_sink_t sink = {&draw->path, draw->precision}; - hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - - if (!draw->path.length) - return false; - - float xx = draw->transform.xx; - float yx = draw->transform.yx; - float xy = draw->transform.xy; - float yy = draw->transform.yy; - float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; - float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; - - hb_svg_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); - hb_svg_append_str (&draw->body, "\" transform=\""); - hb_svg_append_instance_transform (&draw->body, - draw->precision, - draw->x_scale_factor, - draw->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&draw->body, "\"/>\n"); - return true; - } - - float xx = draw->transform.xx; - float yx = draw->transform.yx; - float xy = draw->transform.xy; - float yy = draw->transform.yy; - float tx = draw->transform.x0 + xx * pen_x + xy * pen_y; - float ty = draw->transform.y0 + yx * pen_x + yy * pen_y; - - hb_svg_append_str (&draw->body, "body, glyph); - hb_svg_append_str (&draw->body, "\" transform=\""); - hb_svg_append_instance_transform (&draw->body, - draw->precision, - draw->x_scale_factor, - draw->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&draw->body, "\"/>\n"); - return true; } /** - * hb_vector_svg_set_flat: + * hb_vector_draw_set_flat: * @draw: a draw context. * @flat: whether to flatten geometry and disable reuse. * - * Enables or disables SVG draw flattening. + * Enables or disables draw flattening. * - * Since: 13.0.0 + * XSince: REPLACEME */ void -hb_vector_svg_set_flat (hb_vector_draw_t *draw, +hb_vector_draw_set_flat (hb_vector_draw_t *draw, hb_bool_t flat) { draw->flat = !!flat; } /** - * hb_vector_svg_set_precision: + * hb_vector_draw_set_precision: * @draw: a draw context. * @precision: decimal precision. * - * Sets numeric output precision for SVG draw output. + * Sets numeric output precision for draw output. * - * Since: 13.0.0 + * XSince: REPLACEME */ void -hb_vector_svg_set_precision (hb_vector_draw_t *draw, +hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned precision) { draw->precision = hb_min (precision, 12u); } -/** - * hb_vector_draw_render: - * @draw: a draw context. - * - * Renders accumulated draw content to an SVG blob. - * - * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. - * - * Since: 13.0.0 - */ -hb_blob_t * -hb_vector_draw_render (hb_vector_draw_t *draw) +static hb_blob_t * +hb_vector_draw_render_pdf (hb_vector_draw_t *draw) { - if (draw->format != HB_VECTOR_FORMAT_SVG) - return nullptr; if (!draw->has_extents) return nullptr; - hb_vector_t out; - hb_svg_recover_recycled_buffer (draw->recycled_blob, &out); - unsigned estimated = draw->defs.length + - (draw->body.length ? draw->body.length : draw->path.length) + - 256; - out.alloc (estimated); - hb_svg_append_str (&out, "extents.x, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.y, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.width, draw->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, draw->extents.height, draw->precision); - hb_svg_append_str (&out, "\" width=\""); - hb_svg_append_num (&out, draw->extents.width, draw->precision); - hb_svg_append_str (&out, "\" height=\""); - hb_svg_append_num (&out, draw->extents.height, draw->precision); - hb_svg_append_str (&out, "\">\n"); + /* Collect the content stream. The path coordinates are in + * SVG space (Y-down). Prepend a CTM that flips Y so the + * PDF page (Y-up) renders correctly. */ + float ex = draw->extents.x; + float ey = draw->extents.y; + float ew = draw->extents.width; + float eh = draw->extents.height; - if (draw->defs.length) - { - hb_svg_append_str (&out, "\n"); - hb_svg_append_len (&out, draw->defs.arrayZ, draw->defs.length); - hb_svg_append_str (&out, "\n"); - } + hb_vector_t stream; + stream.alloc (draw->body.length + draw->path.length + 128); + + /* Path coords are in font space (Y-up); no CTM needed. */ if (draw->body.length) - { - hb_svg_append_len (&out, draw->body.arrayZ, draw->body.length); - } + hb_buf_append_len (&stream, draw->body.arrayZ, draw->body.length); else if (draw->path.length) { - hb_svg_append_str (&out, "path.arrayZ, draw->path.length); - hb_svg_append_str (&out, "\"/>\n"); + hb_buf_append_len (&stream, draw->path.arrayZ, draw->path.length); + hb_buf_append_str (&stream, "f\n"); } - hb_svg_append_str (&out, "\n"); + /* Build PDF objects, tracking byte offsets for xref. */ + hb_vector_t out; + hb_buf_recover_recycled (draw->recycled_blob, &out); + out.alloc (stream.length + 512); - hb_blob_t *blob = hb_svg_blob_from_buffer (&draw->recycled_blob, &out); + unsigned offsets[5]; /* objects 1-4, plus end */ + + hb_buf_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + + /* Object 1: Catalog */ + offsets[0] = out.length; + hb_buf_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + /* Object 2: Pages */ + offsets[1] = out.length; + hb_buf_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + /* Object 3: Page. Extents are in SVG space (y = -font_y). + * Convert back: font Y range = [-(ey+eh) .. -ey]. */ + offsets[2] = out.length; + hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_buf_append_num (&out, ex, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -(ey + eh), draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, ex + ew, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -ey, draw->precision); + hb_buf_append_str (&out, "] /Contents 4 0 R >>\nendobj\n"); + + /* Object 4: Content stream */ + offsets[3] = out.length; + hb_buf_append_str (&out, "4 0 obj\n<< /Length "); + hb_buf_append_unsigned (&out, stream.length); + hb_buf_append_str (&out, " >>\nstream\n"); + hb_buf_append_len (&out, stream.arrayZ, stream.length); + hb_buf_append_str (&out, "endstream\nendobj\n"); + + /* Cross-reference table */ + unsigned xref_offset = out.length; + hb_buf_append_str (&out, "xref\n0 5\n"); + hb_buf_append_str (&out, "0000000000 65535 f \n"); + for (unsigned i = 0; i < 4; i++) + { + char tmp[21]; + snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets[i]); + hb_buf_append_len (&out, tmp, 20); + } + + /* Trailer */ + hb_buf_append_str (&out, "trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n"); + hb_buf_append_unsigned (&out, xref_offset); + hb_buf_append_str (&out, "\n%%EOF\n"); + + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); draw->path.clear (); draw->defs.clear (); @@ -695,6 +788,90 @@ hb_vector_draw_render (hb_vector_draw_t *draw) return blob; } +static hb_blob_t * +hb_vector_draw_render_svg (hb_vector_draw_t *draw) +{ + if (!draw->has_extents) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (draw->recycled_blob, &out); + unsigned estimated = draw->defs.length + + (draw->body.length ? draw->body.length : draw->path.length) + + 256; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.y, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, draw->extents.width, draw->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, draw->extents.height, draw->precision); + hb_buf_append_str (&out, "\">\n"); + + if (draw->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length); + hb_buf_append_str (&out, "\n"); + } + + if (draw->body.length) + { + hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); + } + else if (draw->path.length) + { + hb_buf_append_str (&out, "path.arrayZ, draw->path.length); + hb_buf_append_str (&out, "\"/>\n"); + } + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); + + draw->path.clear (); + draw->defs.clear (); + draw->body.clear (); + hb_set_clear (draw->defined_glyphs); + draw->has_extents = false; + draw->extents = {0, 0, 0, 0}; + + return blob; +} + +/** + * hb_vector_draw_render: + * @draw: a draw context. + * + * Renders accumulated draw content to an output blob. + * + * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. + * + * Since: 13.0.0 + */ +hb_blob_t * +hb_vector_draw_render (hb_vector_draw_t *draw) +{ + switch (draw->format) + { + case HB_VECTOR_FORMAT_SVG: + return hb_vector_draw_render_svg (draw); + + case HB_VECTOR_FORMAT_PDF: + return hb_vector_draw_render_pdf (draw); + + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; + } +} + /** * hb_vector_draw_reset: * @draw: a draw context. diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh new file mode 100644 index 000000000..6f7603c33 --- /dev/null +++ b/src/hb-vector-draw.hh @@ -0,0 +1,46 @@ +#ifndef HB_VECTOR_DRAW_HH +#define HB_VECTOR_DRAW_HH + +#include "hb.hh" + +#include "hb-vector.h" +#include "hb-geometry.hh" +#include "hb-machinery.hh" +#include "hb-vector-svg-utils.hh" +#include "hb-vector-svg.hh" + +struct hb_vector_draw_t +{ + hb_object_header_t header; + + hb_vector_format_t format = HB_VECTOR_FORMAT_INVALID; + hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; + float x_scale_factor = 1.f; + float y_scale_factor = 1.f; + hb_vector_extents_t extents = {0, 0, 0, 0}; + bool has_extents = false; + unsigned precision = 2; + bool flat = false; + + hb_vector_t defs; + hb_vector_t body; + hb_vector_t path; + hb_set_t *defined_glyphs = nullptr; + hb_blob_t *recycled_blob = nullptr; + + void append_xy (float x, float y) + { + float tx, ty; + hb_svg_transform_point (transform, x_scale_factor, y_scale_factor, x, y, &tx, &ty); + hb_buf_append_num (&path, tx, precision); + switch (format) + { + case HB_VECTOR_FORMAT_PDF: hb_buf_append_c (&path, ' '); break; + case HB_VECTOR_FORMAT_SVG: hb_buf_append_c (&path, ','); break; + case HB_VECTOR_FORMAT_INVALID: default: break; + } + hb_buf_append_num (&path, ty, precision); + } +}; + +#endif /* HB_VECTOR_DRAW_HH */ diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc new file mode 100644 index 000000000..9b7923573 --- /dev/null +++ b/src/hb-vector-paint-pdf.cc @@ -0,0 +1,1368 @@ +/* + * 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-vector-paint.hh" +#include "hb-vector-draw.hh" + +#include +#include + +#ifdef HAVE_ZLIB +#include +#endif + +/* PDF paint backend for COLRv0/v1 color font rendering. + * + * Supports: + * - Solid colors with alpha (via ExtGState) + * - Glyph and rectangle clipping + * - Affine transforms + * - Groups (save/restore) + * - Linear gradients (PDF Type 2 axial shading) + * - Radial gradients (PDF Type 3 radial shading) + * - Sweep gradients (approximated via solid fallback) + */ + + +/* ---- PDF object collector ---- */ + +struct hb_pdf_obj_t +{ + hb_vector_t data; +}; + +/* Collects extra PDF objects (shadings, functions, ExtGState) + * during painting. Referenced from the content stream by name + * (e.g. /SH0, /GS0) and emitted at render time. */ +static bool +hb_pdf_build_indexed_smask (hb_vector_t *out, + const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + const uint8_t *trns, unsigned trns_len); + +struct hb_pdf_resources_t +{ + hb_vector_t objects; /* extra objects, starting at id 5 */ + hb_vector_t extgstate_dict; /* /GS0 5 0 R /GS1 6 0 R ... */ + hb_vector_t shading_dict; /* /SH0 7 0 R ... */ + hb_vector_t xobject_dict; /* /Im0 8 0 R ... */ + unsigned extgstate_count = 0; + unsigned shading_count = 0; + unsigned xobject_count = 0; + + unsigned add_object (hb_vector_t &&obj_data) + { + unsigned id = 5 + objects.length; /* objects 1-4 are fixed */ + hb_pdf_obj_t obj; + obj.data = std::move (obj_data); + objects.push (std::move (obj)); + return id; + } + + /* Add ExtGState for fill opacity, return resource name index. */ + unsigned add_extgstate_alpha (float alpha, unsigned precision) + { + unsigned idx = extgstate_count++; + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /Type /ExtGState /ca "); + hb_buf_append_num (&obj, alpha, 4); + hb_buf_append_str (&obj, " >>"); + unsigned obj_id = add_object (std::move (obj)); + + /* Add to resource dict. */ + hb_buf_append_str (&extgstate_dict, "/GS"); + hb_buf_append_unsigned (&extgstate_dict, idx); + hb_buf_append_c (&extgstate_dict, ' '); + hb_buf_append_unsigned (&extgstate_dict, obj_id); + hb_buf_append_str (&extgstate_dict, " 0 R "); + (void) precision; + return idx; + } + + /* Add ExtGState for blend mode, return resource name index. */ + unsigned add_extgstate_blend (const char *bm) + { + unsigned idx = extgstate_count++; + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /Type /ExtGState /BM /"); + hb_buf_append_str (&obj, bm); + hb_buf_append_str (&obj, " >>"); + unsigned obj_id = add_object (std::move (obj)); + + hb_buf_append_str (&extgstate_dict, "/GS"); + hb_buf_append_unsigned (&extgstate_dict, idx); + hb_buf_append_c (&extgstate_dict, ' '); + hb_buf_append_unsigned (&extgstate_dict, obj_id); + hb_buf_append_str (&extgstate_dict, " 0 R "); + return idx; + } + + /* Add a shading + function for a gradient, return resource name index. */ + unsigned add_shading (hb_vector_t &&shading_data) + { + unsigned idx = shading_count++; + unsigned obj_id = add_object (std::move (shading_data)); + + hb_buf_append_str (&shading_dict, "/SH"); + hb_buf_append_unsigned (&shading_dict, idx); + hb_buf_append_c (&shading_dict, ' '); + hb_buf_append_unsigned (&shading_dict, obj_id); + hb_buf_append_str (&shading_dict, " 0 R "); + return idx; + } + + /* Add an XObject image, return resource name index. + * idat_data/idat_len is the concatenated PNG IDAT payload (zlib data). + * colors is 1 (gray), 3 (RGB), or 4 (RGBA); for indexed, pass colors=1. + * plte/plte_len is the PLTE chunk data for indexed images (may be null). */ + unsigned add_xobject_png_image (const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + unsigned colors, bool has_alpha, + const uint8_t *plte = nullptr, + unsigned plte_len = 0, + const uint8_t *trns = nullptr, + unsigned trns_len = 0) + { + unsigned idx = xobject_count++; + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /Type /XObject /Subtype /Image\n"); + hb_buf_append_str (&obj, "/Width "); + hb_buf_append_unsigned (&obj, width); + hb_buf_append_str (&obj, " /Height "); + hb_buf_append_unsigned (&obj, height); + hb_buf_append_str (&obj, "\n/BitsPerComponent 8\n"); + + if (plte && plte_len >= 3) + { + /* Indexed color: /ColorSpace [/Indexed /DeviceRGB N ] */ + unsigned n_entries = plte_len / 3; + hb_buf_append_str (&obj, "/ColorSpace [/Indexed /DeviceRGB "); + hb_buf_append_unsigned (&obj, n_entries - 1); + hb_buf_append_str (&obj, " <"); + for (unsigned i = 0; i < n_entries * 3; i++) + { + hb_buf_append_c (&obj, "0123456789ABCDEF"[plte[i] >> 4]); + hb_buf_append_c (&obj, "0123456789ABCDEF"[plte[i] & 0xF]); + } + hb_buf_append_str (&obj, ">]\n"); + } + else + { + hb_buf_append_str (&obj, "/ColorSpace "); + unsigned color_channels = has_alpha ? colors - 1 : colors; + hb_buf_append_str (&obj, color_channels == 1 ? "/DeviceGray" : "/DeviceRGB"); + hb_buf_append_c (&obj, '\n'); + } + + /* Build SMask for indexed images with tRNS transparency. */ + unsigned smask_id = 0; + if (plte && trns && trns_len) + { + hb_vector_t smask_stream; + if (hb_pdf_build_indexed_smask (&smask_stream, idat_data, idat_len, + width, height, trns, trns_len)) + { + hb_vector_t smask_obj; + hb_buf_append_str (&smask_obj, "<< /Type /XObject /Subtype /Image\n"); + hb_buf_append_str (&smask_obj, "/Width "); + hb_buf_append_unsigned (&smask_obj, width); + hb_buf_append_str (&smask_obj, " /Height "); + hb_buf_append_unsigned (&smask_obj, height); + hb_buf_append_str (&smask_obj, "\n/ColorSpace /DeviceGray /BitsPerComponent 8\n"); + hb_buf_append_str (&smask_obj, "/Length "); + hb_buf_append_unsigned (&smask_obj, smask_stream.length); + hb_buf_append_str (&smask_obj, " >>\nstream\n"); + hb_buf_append_len (&smask_obj, smask_stream.arrayZ, smask_stream.length); + hb_buf_append_str (&smask_obj, "\nendstream"); + smask_id = add_object (std::move (smask_obj)); + } + } + + if (smask_id) + { + hb_buf_append_str (&obj, "/SMask "); + hb_buf_append_unsigned (&obj, smask_id); + hb_buf_append_str (&obj, " 0 R\n"); + } + + hb_buf_append_str (&obj, "/Filter /FlateDecode\n"); + hb_buf_append_str (&obj, "/DecodeParms << /Predictor 15 /Colors "); + hb_buf_append_unsigned (&obj, colors); + hb_buf_append_str (&obj, " /BitsPerComponent 8 /Columns "); + hb_buf_append_unsigned (&obj, width); + hb_buf_append_str (&obj, " >>\n"); + hb_buf_append_str (&obj, "/Length "); + hb_buf_append_unsigned (&obj, idat_len); + hb_buf_append_str (&obj, " >>\nstream\n"); + hb_buf_append_len (&obj, idat_data, idat_len); + hb_buf_append_str (&obj, "\nendstream"); + + (void) has_alpha; + + unsigned obj_id = add_object (std::move (obj)); + + hb_buf_append_str (&xobject_dict, "/Im"); + hb_buf_append_unsigned (&xobject_dict, idx); + hb_buf_append_c (&xobject_dict, ' '); + hb_buf_append_unsigned (&xobject_dict, obj_id); + hb_buf_append_str (&xobject_dict, " 0 R "); + return idx; + } +}; + +/* Store resources pointer in the paint struct's defs buffer + * (repurposed — defs is unused for PDF). */ +static hb_pdf_resources_t * +hb_pdf_get_resources (hb_vector_paint_t *paint) +{ + if (!paint->defs.length) + { + /* First call: allocate and store. */ + auto *res = (hb_pdf_resources_t *) hb_calloc (1, sizeof (hb_pdf_resources_t)); + if (unlikely (!res)) return nullptr; + new (res) hb_pdf_resources_t (); + paint->defs.resize (sizeof (void *)); + memcpy (paint->defs.arrayZ, &res, sizeof (void *)); + } + hb_pdf_resources_t *res; + memcpy (&res, paint->defs.arrayZ, sizeof (void *)); + return res; +} + +static void +hb_pdf_free_resources (hb_vector_paint_t *paint) +{ + if (paint->defs.length >= sizeof (void *)) + { + hb_pdf_resources_t *res; + memcpy (&res, paint->defs.arrayZ, sizeof (void *)); + if (res) + { + res->~hb_pdf_resources_t (); + hb_free (res); + } + } + paint->defs.clear (); +} + + +/* ---- helpers ---- */ + +static hb_bool_t +hb_pdf_paint_ensure_initialized (hb_vector_paint_t *paint) +{ + if (paint->group_stack.length) + return !paint->group_stack.in_error () && + !paint->group_stack.tail ().in_error (); + if (unlikely (!paint->group_stack.push_or_fail ())) + return false; + paint->group_stack.tail ().alloc (4096); + if (unlikely (paint->group_stack.tail ().in_error ())) + { + paint->group_stack.pop (); + return false; + } + return !paint->group_stack.in_error (); +} + +/* Emit a glyph outline as PDF path operators into buf. */ +static void +hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, + hb_font_t *font, + hb_codepoint_t glyph, + hb_vector_t *buf) +{ + hb_vector_draw_t tmp; + tmp.format = HB_VECTOR_FORMAT_PDF; + tmp.precision = paint->precision; + tmp.transform = {1, 0, 0, 1, 0, 0}; + tmp.path.alloc (1024); + + hb_font_draw_glyph (font, glyph, + hb_vector_draw_get_funcs (), + &tmp); + + hb_buf_append_len (buf, tmp.path.arrayZ, tmp.path.length); +} + + +/* ---- paint callbacks ---- */ + +static void +hb_pdf_paint_push_transform (hb_paint_funcs_t *, + void *paint_data, + float xx, float yx, + float xy, float yy, + float dx, float dy, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, dx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_str (&body, " cm\n"); +} + +static void +hb_pdf_paint_pop_transform (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 (), "Q\n"); +} + +static void +hb_pdf_paint_push_clip_glyph (hb_paint_funcs_t *, + void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + hb_pdf_emit_glyph_path (paint, font, glyph, &body); + hb_buf_append_str (&body, "W n\n"); +} + +static void +hb_pdf_paint_push_clip_rectangle (hb_paint_funcs_t *, + void *paint_data, + float xmin, float ymin, + float xmax, float ymax, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + hb_buf_append_num (&body, xmin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ymin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xmax - xmin, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ymax - ymin, paint->precision); + hb_buf_append_str (&body, " re W n\n"); +} + +static void +hb_pdf_paint_pop_clip (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 (), "Q\n"); +} + +/* Paint a solid color, including alpha via ExtGState. */ +static void +hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) +{ + auto &body = paint->current_body (); + + float r = hb_color_get_red (c) / 255.f; + float g = hb_color_get_green (c) / 255.f; + float b = hb_color_get_blue (c) / 255.f; + float a = hb_color_get_alpha (c) / 255.f; + + if (a < 1.f / 255.f) + return; + + /* Set alpha via ExtGState if needed. */ + if (a < 1.f - 1.f / 512.f) + { + auto *res = hb_pdf_get_resources (paint); + if (res) + { + unsigned gs_idx = res->add_extgstate_alpha (a, paint->precision); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + } + + /* Set fill color. */ + hb_buf_append_num (&body, r, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, g, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, b, 4); + hb_buf_append_str (&body, " rg\n"); + + /* Paint a huge rect (will be clipped). */ + hb_buf_append_str (&body, "-32767 -32767 65534 65534 re f\n"); +} + +static void +hb_pdf_paint_color (hb_paint_funcs_t *, + void *paint_data, + hb_bool_t is_foreground, + hb_color_t color, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + hb_color_t c = color; + if (is_foreground) + c = HB_COLOR (hb_color_get_blue (paint->foreground), + hb_color_get_green (paint->foreground), + hb_color_get_red (paint->foreground), + (unsigned) hb_color_get_alpha (paint->foreground) * + hb_color_get_alpha (color) / 255); + + hb_pdf_paint_solid_color (paint, c); +} + +/* Build an uncompressed alpha mask from indexed PNG IDAT data + tRNS. + * Decompresses IDAT, un-filters PNG rows, maps palette indices to alpha. */ +static bool +hb_pdf_build_indexed_smask (hb_vector_t *out, + const char *idat_data, unsigned idat_len, + unsigned width, unsigned height, + const uint8_t *trns, unsigned trns_len) +{ +#ifndef HAVE_ZLIB + (void) out; (void) idat_data; (void) idat_len; + (void) width; (void) height; (void) trns; (void) trns_len; + return false; +#else + /* Decompress IDAT (zlib). */ + unsigned raw_len = (width + 1) * height; /* 1 filter byte per row + width bytes */ + uint8_t *raw = (uint8_t *) hb_malloc (raw_len); + if (!raw) return false; + + z_stream stream = {}; + stream.next_in = (Bytef *) idat_data; + stream.avail_in = idat_len; + stream.next_out = (Bytef *) raw; + stream.avail_out = raw_len; + + if (inflateInit (&stream) != Z_OK) + { + hb_free (raw); + return false; + } + int status = inflate (&stream, Z_FINISH); + inflateEnd (&stream); + if (status != Z_STREAM_END) + { + hb_free (raw); + return false; + } + + /* Un-filter and map to alpha. */ + if (!out->resize (width * height)) + { + hb_free (raw); + return false; + } + + uint8_t *unfiltered = (uint8_t *) hb_malloc (width); + if (!unfiltered) + { + hb_free (raw); + return false; + } + uint8_t *prev_unfiltered = (uint8_t *) hb_calloc (width, 1); + if (!prev_unfiltered) + { + hb_free (unfiltered); + hb_free (raw); + return false; + } + + for (unsigned y = 0; y < height; y++) + { + uint8_t *row = raw + y * (width + 1); + uint8_t filter = row[0]; + uint8_t *pixels = row + 1; + + for (unsigned x = 0; x < width; x++) + { + uint8_t a = (x > 0) ? unfiltered[x - 1] : 0; + uint8_t b = prev_unfiltered[x]; + uint8_t c = (x > 0) ? prev_unfiltered[x - 1] : 0; + uint8_t val = pixels[x]; + + switch (filter) + { + case 0: unfiltered[x] = val; break; /* None */ + case 1: unfiltered[x] = val + a; break; /* Sub */ + case 2: unfiltered[x] = val + b; break; /* Up */ + case 3: unfiltered[x] = val + (uint8_t) (((unsigned) a + b) / 2); break; /* Average */ + case 4: /* Paeth */ + { + int p = (int) a + (int) b - (int) c; + int pa = abs (p - (int) a); + int pb = abs (p - (int) b); + int pc = abs (p - (int) c); + uint8_t pr = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; + unfiltered[x] = val + pr; + break; + } + default: unfiltered[x] = val; break; + } + + /* Map palette index to alpha. */ + uint8_t idx = unfiltered[x]; + out->arrayZ[y * width + x] = (idx < trns_len) ? trns[idx] : 0xFF; + } + + /* Swap buffers. */ + uint8_t *tmp = prev_unfiltered; + prev_unfiltered = unfiltered; + unfiltered = tmp; + } + + hb_free (unfiltered); + hb_free (prev_unfiltered); + hb_free (raw); + return true; +#endif +} + +/* Read a big-endian uint32 from a byte pointer. */ +static inline uint32_t +hb_pdf_png_u32 (const uint8_t *p) +{ + return ((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16) | + ((uint32_t) p[2] << 8) | (uint32_t) p[3]; +} + +static hb_bool_t +hb_pdf_paint_image (hb_paint_funcs_t *, + void *paint_data, + hb_blob_t *image, + unsigned width, + unsigned height, + hb_tag_t format, + float slant HB_UNUSED, + hb_glyph_extents_t *extents, + void *) +{ + if (format != HB_TAG ('p','n','g',' ')) + return false; + if (!extents || !width || !height) + return false; + + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return false; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return false; + + unsigned len = 0; + const uint8_t *data = (const uint8_t *) hb_blob_get_data (image, &len); + if (!data || len < 8) + return false; + + /* Verify PNG signature. */ + static const uint8_t png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; + if (hb_memcmp (data, png_sig, 8) != 0) + return false; + + /* Parse PNG chunks: extract IHDR and concatenate IDAT payloads. */ + unsigned png_w = 0, png_h = 0; + unsigned colors = 3; /* default RGB */ + uint8_t color_type = 2; + bool has_alpha = false; + const uint8_t *plte_data = nullptr; + unsigned plte_len = 0; + const uint8_t *trns_data = nullptr; + unsigned trns_len = 0; + hb_vector_t idat; + + unsigned pos = 8; + while (pos + 12 <= len) + { + uint32_t chunk_len = hb_pdf_png_u32 (data + pos); + uint32_t chunk_type = hb_pdf_png_u32 (data + pos + 4); + if (pos + 12 + chunk_len > len) + break; + const uint8_t *chunk_data = data + pos + 8; + + if (chunk_type == 0x49484452u) /* IHDR */ + { + if (chunk_len < 13) return false; + png_w = hb_pdf_png_u32 (chunk_data); + png_h = hb_pdf_png_u32 (chunk_data + 4); + uint8_t bit_depth = chunk_data[8]; + color_type = chunk_data[9]; + if (bit_depth != 8) return false; /* only 8-bit supported */ + + switch (color_type) + { + case 0: colors = 1; has_alpha = false; break; /* Grayscale */ + case 2: colors = 3; has_alpha = false; break; /* RGB */ + case 3: colors = 1; has_alpha = false; break; /* Indexed */ + case 4: colors = 2; has_alpha = true; break; /* Gray+Alpha */ + case 6: colors = 4; has_alpha = true; break; /* RGBA */ + default: return false; + } + } + else if (chunk_type == 0x504C5445u) /* PLTE */ + { + plte_data = chunk_data; + plte_len = chunk_len; + } + else if (chunk_type == 0x74524E53u) /* tRNS */ + { + trns_data = chunk_data; + trns_len = chunk_len; + } + else if (chunk_type == 0x49444154u) /* IDAT */ + { + hb_buf_append_len (&idat, (const char *) chunk_data, chunk_len); + } + else if (chunk_type == 0x49454E44u) /* IEND */ + break; + + pos += 12 + chunk_len; + } + + if (!png_w || !png_h || !idat.length) + return false; + + unsigned im_idx = res->add_xobject_png_image (idat.arrayZ, idat.length, + png_w, png_h, + colors, has_alpha, + plte_data, plte_len, + trns_data, trns_len); + + /* Emit: save state, set CTM to map image (0,0)-(1,1) to extents, paint. */ + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + + /* Image space: (0,0) at bottom-left, (1,1) at top-right. + * We need to map to extents (x_bearing, y_bearing+height) .. (x_bearing+width, y_bearing). + * Since font coords are Y-up like PDF, y_bearing is the top. */ + float ix = (float) extents->x_bearing; + float iy = (float) extents->y_bearing + (float) extents->height; + float iw = (float) extents->width; + float ih = (float) -extents->height; /* negative because image Y goes up but height is negative in extents */ + + hb_buf_append_num (&body, iw, paint->precision); + hb_buf_append_str (&body, " 0 0 "); + hb_buf_append_num (&body, ih, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ix, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, iy, paint->precision); + hb_buf_append_str (&body, " cm\n"); + + hb_buf_append_str (&body, "/Im"); + hb_buf_append_unsigned (&body, im_idx); + hb_buf_append_str (&body, " Do\nQ\n"); + + return true; +} + +/* ---- Gradient helpers ---- */ + +/* Build a PDF Type 2 (exponential interpolation) function for + * a single color stop pair. */ +static void +hb_pdf_build_interpolation_function (hb_vector_t *obj, + hb_color_t c0, hb_color_t c1) +{ + hb_buf_append_str (obj, "<< /FunctionType 2 /Domain [0 1] /N 1\n"); + hb_buf_append_str (obj, "/C0 ["); + hb_buf_append_num (obj, hb_color_get_red (c0) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_green (c0) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_blue (c0) / 255.f, 4); + hb_buf_append_str (obj, "]\n/C1 ["); + hb_buf_append_num (obj, hb_color_get_red (c1) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_green (c1) / 255.f, 4); + hb_buf_append_c (obj, ' '); + hb_buf_append_num (obj, hb_color_get_blue (c1) / 255.f, 4); + hb_buf_append_str (obj, "] >>"); +} + +/* Build a stitching function (Type 3) for multiple color stops. */ +static unsigned +hb_pdf_build_gradient_function (hb_pdf_resources_t *res, + hb_vector_paint_t *paint, + hb_color_line_t *color_line) +{ + unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + paint->color_stops_scratch.resize (count); + hb_color_line_get_color_stops (color_line, 0, &count, paint->color_stops_scratch.arrayZ); + + if (count < 2) + { + /* Single stop: constant function. */ + hb_color_t c = count ? paint->color_stops_scratch.arrayZ[0].color + : HB_COLOR (0, 0, 0, 255); + hb_vector_t obj; + hb_pdf_build_interpolation_function (&obj, c, c); + return res->add_object (std::move (obj)); + } + + /* Sort by offset. */ + paint->color_stops_scratch.as_array ().qsort ( + [] (const hb_color_stop_t &a, const hb_color_stop_t &b) + { return a.offset < b.offset; }); + + if (count == 2) + { + /* Two stops: single interpolation function. */ + hb_vector_t obj; + hb_pdf_build_interpolation_function (&obj, + paint->color_stops_scratch.arrayZ[0].color, + paint->color_stops_scratch.arrayZ[1].color); + return res->add_object (std::move (obj)); + } + + /* Multiple stops: create sub-functions and stitch. */ + hb_vector_t sub_func_ids; + for (unsigned i = 0; i + 1 < count; i++) + { + hb_vector_t sub; + hb_pdf_build_interpolation_function (&sub, + paint->color_stops_scratch.arrayZ[i].color, + paint->color_stops_scratch.arrayZ[i + 1].color); + sub_func_ids.push (res->add_object (std::move (sub))); + } + + /* Stitching function (Type 3). */ + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /FunctionType 3 /Domain [0 1]\n"); + + /* Functions array. */ + hb_buf_append_str (&obj, "/Functions ["); + for (unsigned i = 0; i < sub_func_ids.length; i++) + { + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_unsigned (&obj, sub_func_ids.arrayZ[i]); + hb_buf_append_str (&obj, " 0 R"); + } + hb_buf_append_str (&obj, "]\n"); + + /* Bounds. */ + hb_buf_append_str (&obj, "/Bounds ["); + for (unsigned i = 1; i + 1 < count; i++) + { + if (i > 1) hb_buf_append_c (&obj, ' '); + hb_buf_append_num (&obj, paint->color_stops_scratch.arrayZ[i].offset, 4); + } + hb_buf_append_str (&obj, "]\n"); + + /* Encode array. */ + hb_buf_append_str (&obj, "/Encode ["); + for (unsigned i = 0; i + 1 < count; i++) + { + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_str (&obj, "0 1"); + } + hb_buf_append_str (&obj, "] >>"); + + return res->add_object (std::move (obj)); +} + +static void +hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float x0, float y0, + float x1, float y1, + float x2 HB_UNUSED, float y2 HB_UNUSED, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; + + unsigned func_id = hb_pdf_build_gradient_function (res, paint, color_line); + + /* Build Type 2 (axial) shading. */ + hb_vector_t sh; + hb_buf_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/Coords ["); + hb_buf_append_num (&sh, x0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, x1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y1, paint->precision); + hb_buf_append_str (&sh, "]\n/Function "); + hb_buf_append_unsigned (&sh, func_id); + hb_buf_append_str (&sh, " 0 R\n"); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + if (extend == HB_PAINT_EXTEND_PAD) + hb_buf_append_str (&sh, "/Extend [true true]\n"); + + hb_buf_append_str (&sh, ">>"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); +} + +static void +hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float x0, float y0, float r0, + float x1, float y1, float r1, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; + + unsigned func_id = hb_pdf_build_gradient_function (res, paint, color_line); + + /* Build Type 3 (radial) shading. */ + hb_vector_t sh; + hb_buf_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/Coords ["); + hb_buf_append_num (&sh, x0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, r0, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, x1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, y1, paint->precision); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, r1, paint->precision); + hb_buf_append_str (&sh, "]\n/Function "); + hb_buf_append_unsigned (&sh, func_id); + hb_buf_append_str (&sh, " 0 R\n"); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + if (extend == HB_PAINT_EXTEND_PAD) + hb_buf_append_str (&sh, "/Extend [true true]\n"); + + hb_buf_append_str (&sh, ">>"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); +} + + +/* Encode a 16-bit big-endian unsigned value into buf. */ +static void +hb_pdf_encode_u16 (hb_vector_t *buf, uint16_t v) +{ + char bytes[2] = {(char) (v >> 8), (char) (v & 0xFF)}; + hb_buf_append_len (buf, bytes, 2); +} + +/* Encode a coordinate as 16-bit value relative to Decode range. */ +static void +hb_pdf_encode_coord (hb_vector_t *buf, + float val, float lo, float hi) +{ + float t = (val - lo) / (hi - lo); + t = hb_clamp (t, 0.f, 1.f); + hb_pdf_encode_u16 (buf, (uint16_t) (t * 65535.f + 0.5f)); +} + +/* Encode RGB from hb_color_t as 3 bytes. */ +static void +hb_pdf_encode_color_rgb (hb_vector_t *buf, hb_color_t c) +{ + char rgb[3] = {(char) hb_color_get_red (c), + (char) hb_color_get_green (c), + (char) hb_color_get_blue (c)}; + hb_buf_append_len (buf, rgb, 3); +} + +/* Encode one Coons patch control point. */ +static void +hb_pdf_encode_point (hb_vector_t *buf, + float x, float y, + float xlo, float xhi, + float ylo, float yhi) +{ + hb_pdf_encode_coord (buf, x, xlo, xhi); + hb_pdf_encode_coord (buf, y, ylo, yhi); +} + +/* Emit one Coons patch sector into the mesh stream. + * Splits large arcs into sub-patches of max 90°. */ +static void +hb_pdf_add_sweep_patch (hb_vector_t *mesh, + float cx, float cy, + float xlo, float xhi, float ylo, float yhi, + float a0, hb_color_t c0_in, + float a1, hb_color_t c1_in) +{ + const float R = 32767.f; + const float eps = 0.5f; + const float MAX_SECTOR = (float) M_PI / 2.f; + + int num_splits = (int) ceilf (fabsf (a1 - a0) / MAX_SECTOR); + if (num_splits < 1) num_splits = 1; + + for (int s = 0; s < num_splits; s++) + { + float k0 = (float) s / num_splits; + float k1 = (float) (s + 1) / num_splits; + float sa0 = a0 + k0 * (a1 - a0); + float sa1 = a0 + k1 * (a1 - a0); + hb_color_t sc0 = hb_color_lerp (c0_in, c1_in, k0); + hb_color_t sc1 = hb_color_lerp (c0_in, c1_in, k1); + + float da = sa1 - sa0; + float kappa = (4.f / 3.f) * tanf (da / 4.f); + + float cos0 = cosf (sa0), sin0 = sinf (sa0); + float cos1 = cosf (sa1), sin1 = sinf (sa1); + + float p0x = cx + eps * cos0, p0y = cy + eps * sin0; + float p3x = cx + R * cos0, p3y = cy + R * sin0; + float p6x = cx + R * cos1, p6y = cy + R * sin1; + float p9x = cx + eps * cos1, p9y = cy + eps * sin1; + + /* Edge 1: p0→p3, radial straight line. */ + float e1_1x = p0x + (p3x - p0x) / 3.f; + float e1_1y = p0y + (p3y - p0y) / 3.f; + float e1_2x = p0x + 2.f * (p3x - p0x) / 3.f; + float e1_2y = p0y + 2.f * (p3y - p0y) / 3.f; + + /* Edge 2: p3→p6, outer arc. */ + float e2_1x = p3x + kappa * R * (-sin0); + float e2_1y = p3y + kappa * R * ( cos0); + float e2_2x = p6x - kappa * R * (-sin1); + float e2_2y = p6y - kappa * R * ( cos1); + + /* Edge 3: p6→p9, radial straight line. */ + float e3_1x = p6x + (p9x - p6x) / 3.f; + float e3_1y = p6y + (p9y - p6y) / 3.f; + float e3_2x = p6x + 2.f * (p9x - p6x) / 3.f; + float e3_2y = p6y + 2.f * (p9y - p6y) / 3.f; + + /* Edge 4: p9→p0, inner arc. */ + float e4_1x = p9x + kappa * eps * (-sin1); + float e4_1y = p9y + kappa * eps * ( cos1); + float e4_2x = p0x - kappa * eps * (-sin0); + float e4_2y = p0y - kappa * eps * ( cos0); + + hb_buf_append_c (mesh, '\0'); /* flag = 0, new patch */ + + hb_pdf_encode_point (mesh, p0x, p0y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e1_1x, e1_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e1_2x, e1_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p3x, p3y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e2_1x, e2_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e2_2x, e2_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p6x, p6y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e3_1x, e3_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e3_2x, e3_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, p9x, p9y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e4_1x, e4_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (mesh, e4_2x, e4_2y, xlo, xhi, ylo, yhi); + + hb_pdf_encode_color_rgb (mesh, sc0); /* inner start */ + hb_pdf_encode_color_rgb (mesh, sc0); /* outer start */ + hb_pdf_encode_color_rgb (mesh, sc1); /* outer end */ + hb_pdf_encode_color_rgb (mesh, sc1); /* inner end */ + } +} + +static void +hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, + void *paint_data, + hb_color_line_t *color_line, + float cx, float cy, + float start_angle, + float end_angle, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + auto *res = hb_pdf_get_resources (paint); + if (unlikely (!res)) + return; + + /* Get and sort color stops. */ + unsigned n_stops = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + paint->color_stops_scratch.resize (n_stops); + hb_color_line_get_color_stops (color_line, 0, &n_stops, paint->color_stops_scratch.arrayZ); + paint->color_stops_scratch.as_array ().qsort ( + [] (const hb_color_stop_t &a, const hb_color_stop_t &b) + { return a.offset < b.offset; }); + hb_color_stop_t *stops = paint->color_stops_scratch.arrayZ; + + if (!n_stops) + return; + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + const float R = 32767.f; + float xlo = cx - R - 1, xhi = cx + R + 1; + float ylo = cy - R - 1, yhi = cy + R + 1; + + hb_vector_t mesh; + mesh.alloc (256); + + hb_sweep_gradient_tiles (stops, n_stops, extend, + start_angle, end_angle, + [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) + { hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, + a0, c0, a1, c1); }); + + if (!mesh.length) + return; + + /* Build the shading stream object. */ + hb_vector_t sh; + hb_buf_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); + hb_buf_append_str (&sh, "/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); + hb_buf_append_str (&sh, "/Decode ["); + hb_buf_append_num (&sh, xlo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, xhi, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, ylo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, yhi, 2); + hb_buf_append_str (&sh, " 0 1 0 1 0 1]\n"); + hb_buf_append_str (&sh, "/Length "); + hb_buf_append_unsigned (&sh, mesh.length); + hb_buf_append_str (&sh, " >>\nstream\n"); + hb_buf_append_len (&sh, mesh.arrayZ, mesh.length); + hb_buf_append_str (&sh, "\nendstream"); + + unsigned sh_idx = res->add_shading (std::move (sh)); + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "/SH"); + hb_buf_append_unsigned (&body, sh_idx); + hb_buf_append_str (&body, " sh\n"); +} + +static const char * +hb_pdf_blend_mode_name (hb_paint_composite_mode_t mode) +{ + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_MULTIPLY: return "Multiply"; + case HB_PAINT_COMPOSITE_MODE_SCREEN: return "Screen"; + case HB_PAINT_COMPOSITE_MODE_OVERLAY: return "Overlay"; + case HB_PAINT_COMPOSITE_MODE_DARKEN: return "Darken"; + case HB_PAINT_COMPOSITE_MODE_LIGHTEN: return "Lighten"; + case HB_PAINT_COMPOSITE_MODE_COLOR_DODGE: return "ColorDodge"; + case HB_PAINT_COMPOSITE_MODE_COLOR_BURN: return "ColorBurn"; + case HB_PAINT_COMPOSITE_MODE_HARD_LIGHT: return "HardLight"; + case HB_PAINT_COMPOSITE_MODE_SOFT_LIGHT: return "SoftLight"; + case HB_PAINT_COMPOSITE_MODE_DIFFERENCE: return "Difference"; + case HB_PAINT_COMPOSITE_MODE_EXCLUSION: return "Exclusion"; + case HB_PAINT_COMPOSITE_MODE_HSL_HUE: return "Hue"; + case HB_PAINT_COMPOSITE_MODE_HSL_SATURATION: return "Saturation"; + case HB_PAINT_COMPOSITE_MODE_HSL_COLOR: return "Color"; + case HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY: return "Luminosity"; + case HB_PAINT_COMPOSITE_MODE_CLEAR: + case HB_PAINT_COMPOSITE_MODE_SRC: + case HB_PAINT_COMPOSITE_MODE_DEST: + case HB_PAINT_COMPOSITE_MODE_SRC_OVER: + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + case HB_PAINT_COMPOSITE_MODE_XOR: + case HB_PAINT_COMPOSITE_MODE_PLUS: + default: return nullptr; /* Normal / no PDF equivalent */ + } +} + +static void +hb_pdf_paint_push_group (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 (), "q\n"); +} + +static void +hb_pdf_paint_push_group_for (hb_paint_funcs_t *, + void *paint_data, + hb_paint_composite_mode_t mode, + void *) +{ + auto *paint = (hb_vector_paint_t *) paint_data; + if (unlikely (!hb_pdf_paint_ensure_initialized (paint))) + return; + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "q\n"); + + const char *bm = hb_pdf_blend_mode_name (mode); + if (bm) + { + auto *res = hb_pdf_get_resources (paint); + if (likely (res)) + { + unsigned gs_idx = res->add_extgstate_blend (bm); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + } +} + +static void +hb_pdf_paint_pop_group (hb_paint_funcs_t *, + void *paint_data, + hb_paint_composite_mode_t mode HB_UNUSED, + 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 (), "Q\n"); +} + + +/* ---- lazy loader for paint funcs ---- */ + +static inline void free_static_pdf_paint_funcs (); + +static struct hb_pdf_paint_funcs_lazy_loader_t + : hb_paint_funcs_lazy_loader_t +{ + static hb_paint_funcs_t *create () + { + hb_paint_funcs_t *funcs = hb_paint_funcs_create (); + hb_paint_funcs_set_push_transform_func (funcs, (hb_paint_push_transform_func_t) hb_pdf_paint_push_transform, nullptr, nullptr); + 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_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); + hb_paint_funcs_set_linear_gradient_func (funcs, (hb_paint_linear_gradient_func_t) hb_pdf_paint_linear_gradient, nullptr, nullptr); + hb_paint_funcs_set_radial_gradient_func (funcs, (hb_paint_radial_gradient_func_t) hb_pdf_paint_radial_gradient, nullptr, nullptr); + hb_paint_funcs_set_sweep_gradient_func (funcs, (hb_paint_sweep_gradient_func_t) hb_pdf_paint_sweep_gradient, nullptr, nullptr); + hb_paint_funcs_set_push_group_func (funcs, (hb_paint_push_group_func_t) hb_pdf_paint_push_group, nullptr, nullptr); + hb_paint_funcs_set_push_group_for_func (funcs, (hb_paint_push_group_for_func_t) hb_pdf_paint_push_group_for, nullptr, nullptr); + hb_paint_funcs_set_pop_group_func (funcs, (hb_paint_pop_group_func_t) hb_pdf_paint_pop_group, nullptr, nullptr); + hb_paint_funcs_make_immutable (funcs); + hb_atexit (free_static_pdf_paint_funcs); + return funcs; + } +} static_pdf_paint_funcs; + +static inline void +free_static_pdf_paint_funcs () +{ + static_pdf_paint_funcs.free_instance (); +} + +hb_paint_funcs_t * +hb_vector_paint_pdf_funcs_get () +{ + return static_pdf_paint_funcs.get_unconst (); +} + + +/* ---- render ---- */ + +hb_blob_t * +hb_vector_paint_render_pdf (hb_vector_paint_t *paint) +{ + if (!paint->has_extents) + return nullptr; + if (!paint->group_stack.length || + !paint->group_stack.arrayZ[0].length) + return nullptr; + + hb_vector_t &content = paint->group_stack.arrayZ[0]; + hb_pdf_resources_t *res = hb_pdf_get_resources (paint); + + float ex = paint->extents.x; + float ey = paint->extents.y; + float ew = paint->extents.width; + float eh = paint->extents.height; + + unsigned num_extra = res ? res->objects.length : 0; + unsigned total_objects = 4 + num_extra; /* 1-based: 1..total_objects */ + + /* Build PDF. */ + hb_vector_t out; + hb_buf_recover_recycled (paint->recycled_blob, &out); + out.alloc (content.length + num_extra * 128 + 1024); + + hb_vector_t offsets; + offsets.resize (total_objects); + + hb_buf_append_str (&out, "%PDF-1.4\n%\xC0\xC1\xC2\xC3\n"); + + /* Object 1: Catalog */ + offsets.arrayZ[0] = out.length; + hb_buf_append_str (&out, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + + /* Object 2: Pages */ + offsets.arrayZ[1] = out.length; + hb_buf_append_str (&out, "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + + /* Object 3: Page */ + offsets.arrayZ[2] = out.length; + hb_buf_append_str (&out, "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox ["); + hb_buf_append_num (&out, ex, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -(ey + eh), paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, ex + ew, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, -ey, paint->precision); + hb_buf_append_str (&out, "]\n/Contents 4 0 R"); + + /* Resources. */ + bool has_resources = res && + (res->extgstate_dict.length || res->shading_dict.length || res->xobject_dict.length); + if (has_resources) + { + hb_buf_append_str (&out, "\n/Resources <<"); + if (res->extgstate_dict.length) + { + hb_buf_append_str (&out, " /ExtGState << "); + hb_buf_append_len (&out, res->extgstate_dict.arrayZ, res->extgstate_dict.length); + hb_buf_append_str (&out, ">>"); + } + if (res->shading_dict.length) + { + hb_buf_append_str (&out, " /Shading << "); + hb_buf_append_len (&out, res->shading_dict.arrayZ, res->shading_dict.length); + hb_buf_append_str (&out, ">>"); + } + if (res->xobject_dict.length) + { + hb_buf_append_str (&out, " /XObject << "); + hb_buf_append_len (&out, res->xobject_dict.arrayZ, res->xobject_dict.length); + hb_buf_append_str (&out, ">>"); + } + hb_buf_append_str (&out, " >>"); + } + + hb_buf_append_str (&out, " >>\nendobj\n"); + + /* Object 4: Content stream */ + offsets.arrayZ[3] = out.length; + hb_buf_append_str (&out, "4 0 obj\n<< /Length "); + hb_buf_append_unsigned (&out, content.length); + hb_buf_append_str (&out, " >>\nstream\n"); + hb_buf_append_len (&out, content.arrayZ, content.length); + hb_buf_append_str (&out, "endstream\nendobj\n"); + + /* Extra objects (functions, shadings, ExtGState). */ + for (unsigned i = 0; i < num_extra; i++) + { + offsets.arrayZ[4 + i] = out.length; + hb_buf_append_unsigned (&out, 5 + i); + hb_buf_append_str (&out, " 0 obj\n"); + auto &obj = res->objects.arrayZ[i]; + hb_buf_append_len (&out, obj.data.arrayZ, obj.data.length); + hb_buf_append_str (&out, "\nendobj\n"); + } + + /* Cross-reference table */ + unsigned xref_offset = out.length; + hb_buf_append_str (&out, "xref\n0 "); + hb_buf_append_unsigned (&out, total_objects + 1); + hb_buf_append_str (&out, "\n0000000000 65535 f \n"); + for (unsigned i = 0; i < total_objects; i++) + { + char tmp[21]; + snprintf (tmp, sizeof (tmp), "%010u 00000 n \n", offsets.arrayZ[i]); + hb_buf_append_len (&out, tmp, 20); + } + + /* Trailer */ + hb_buf_append_str (&out, "trailer\n<< /Size "); + hb_buf_append_unsigned (&out, total_objects + 1); + hb_buf_append_str (&out, " /Root 1 0 R >>\nstartxref\n"); + hb_buf_append_unsigned (&out, xref_offset); + hb_buf_append_str (&out, "\n%%EOF\n"); + + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); + + /* Reset state. */ + hb_pdf_free_resources (paint); + paint->group_stack.clear (); + paint->path.clear (); + paint->has_extents = false; + paint->extents = {0, 0, 0, 0}; + + return blob; +} diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-paint.cc similarity index 62% rename from src/hb-vector-svg-paint.cc rename to src/hb-vector-paint.cc index 4d8a3b09d..09dc11a7b 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-paint.cc @@ -26,124 +26,49 @@ #include "hb.hh" -#include "hb-vector.h" +#include "hb-vector-paint.hh" #include "hb-blob.hh" -#include "hb-geometry.hh" -#include "hb-machinery.hh" -#include "hb-map.hh" #include "hb-vector-svg-path.hh" #include "hb-vector-svg-subset.hh" -#include "hb-vector-svg-utils.hh" -#include #include #include -#include "hb-vector-svg.hh" - -struct hb_svg_color_glyph_cache_key_t +static void +hb_svg_paint_append_global_transform_prefix (hb_vector_paint_t *paint, hb_vector_t *buf) { - hb_codepoint_t glyph = HB_CODEPOINT_INVALID; - unsigned palette = 0; - hb_color_t foreground = 0; + if (paint->transform.xx == 1.f && paint->transform.yx == 0.f && + paint->transform.xy == 0.f && paint->transform.yy == 1.f && + paint->transform.x0 == 0.f && paint->transform.y0 == 0.f && + paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f) + return; - hb_svg_color_glyph_cache_key_t () = default; - hb_svg_color_glyph_cache_key_t (hb_codepoint_t g, unsigned p, hb_color_t f) - : glyph (g), palette (p), foreground (f) {} + unsigned sprec = hb_svg_scale_precision (paint->precision); + hb_buf_append_str (buf, "transform.xx / paint->x_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.yx / paint->y_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.xy / paint->x_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.yy / paint->y_scale_factor, sprec, true); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.x0 / paint->x_scale_factor, paint->precision); + hb_buf_append_c (buf, ','); + hb_buf_append_num (buf, paint->transform.y0 / paint->y_scale_factor, paint->precision); + hb_buf_append_str (buf, ")\">\n"); +} - bool operator == (const hb_svg_color_glyph_cache_key_t &o) const - { - return glyph == o.glyph && - palette == o.palette && - foreground == o.foreground; - } - - uint32_t hash () const - { - uint32_t h = hb_hash (glyph); - h = h * 31u + hb_hash (palette); - h = h * 31u + hb_hash (foreground); - return h; - } -}; - -struct hb_vector_paint_t +static void +hb_svg_paint_append_global_transform_suffix (hb_vector_paint_t *paint, hb_vector_t *buf) { - hb_object_header_t header; - - hb_vector_format_t format = HB_VECTOR_FORMAT_SVG; - hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; - float x_scale_factor = 1.f; - float y_scale_factor = 1.f; - hb_vector_extents_t extents = {0, 0, 0, 0}; - bool has_extents = false; - - hb_color_t foreground = HB_COLOR (0, 0, 0, 255); - int palette = 0; - hb_hashmap_t custom_palette_colors; - unsigned precision = 2; - bool flat = false; - - hb_vector_t defs; - hb_vector_t path; - hb_vector_t> group_stack; - uint64_t transform_group_open_mask = 0; - unsigned transform_group_depth = 0; - unsigned transform_group_overflow_depth = 0; - - unsigned clip_rect_counter = 0; - unsigned gradient_counter = 0; - unsigned color_glyph_counter = 0; - unsigned color_glyph_depth = 0; - hb_set_t *defined_outlines = nullptr; - hb_set_t *defined_clips = nullptr; - hb_set_t *active_color_glyphs = nullptr; - hb_hashmap_t defined_color_glyphs; - hb_vector_t color_stops_scratch; - hb_vector_t subset_body_scratch; - hb_vector_t captured_scratch; - hb_blob_t *recycled_blob = nullptr; - bool current_color_glyph_has_svg_image = false; - hb_codepoint_t current_svg_image_glyph = HB_CODEPOINT_INVALID; - hb_face_t *current_face = nullptr; - unsigned svg_image_counter = 0; - - hb_vector_t ¤t_body () { return group_stack.tail (); } - - void append_global_transform_prefix (hb_vector_t *buf) - { - if (transform.xx == 1.f && transform.yx == 0.f && - transform.xy == 0.f && transform.yy == 1.f && - transform.x0 == 0.f && transform.y0 == 0.f && - x_scale_factor == 1.f && y_scale_factor == 1.f) - return; - - unsigned sprec = hb_svg_scale_precision (precision); - hb_svg_append_str (buf, "\n"); - } - - void append_global_transform_suffix (hb_vector_t *buf) - { - if (transform.xx == 1.f && transform.yx == 0.f && - transform.xy == 0.f && transform.yy == 1.f && - transform.x0 == 0.f && transform.y0 == 0.f && - x_scale_factor == 1.f && y_scale_factor == 1.f) - return; - hb_svg_append_str (buf, "\n"); - } -}; + if (paint->transform.xx == 1.f && paint->transform.yx == 0.f && + paint->transform.xy == 0.f && paint->transform.yy == 1.f && + paint->transform.x0 == 0.f && paint->transform.y0 == 0.f && + paint->x_scale_factor == 1.f && paint->y_scale_factor == 1.f) + return; + hb_buf_append_str (buf, "\n"); +} static inline uint64_t hb_svg_pack_color_glyph_cache_entry (unsigned def_id, @@ -230,22 +155,22 @@ hb_svg_emit_color_stops (hb_vector_paint_t *paint, for (unsigned i = 0; i < stops->length; i++) { hb_color_t c = stops->arrayZ[i].color; - hb_svg_append_str (buf, "arrayZ[i].offset, 4); - hb_svg_append_str (buf, "\" stop-color=\"rgb("); - hb_svg_append_unsigned (buf, hb_color_get_red (c)); - hb_svg_append_c (buf, ','); - hb_svg_append_unsigned (buf, hb_color_get_green (c)); - hb_svg_append_c (buf, ','); - hb_svg_append_unsigned (buf, hb_color_get_blue (c)); - hb_svg_append_str (buf, ")\""); + hb_buf_append_str (buf, "arrayZ[i].offset, 4); + hb_buf_append_str (buf, "\" stop-color=\"rgb("); + hb_buf_append_unsigned (buf, hb_color_get_red (c)); + hb_buf_append_c (buf, ','); + hb_buf_append_unsigned (buf, hb_color_get_green (c)); + hb_buf_append_c (buf, ','); + hb_buf_append_unsigned (buf, hb_color_get_blue (c)); + hb_buf_append_str (buf, ")\""); if (hb_color_get_alpha (c) != 255) { - hb_svg_append_str (buf, " stop-opacity=\""); - hb_svg_append_num (buf, hb_color_get_alpha (c) / 255.f, 4); - hb_svg_append_c (buf, '"'); + hb_buf_append_str (buf, " stop-opacity=\""); + hb_buf_append_num (buf, hb_color_get_alpha (c) / 255.f, 4); + hb_buf_append_c (buf, '"'); } - hb_svg_append_str (buf, "/>\n"); + hb_buf_append_str (buf, "/>\n"); } } @@ -389,220 +314,35 @@ hb_svg_add_sweep_patch (hb_vector_t *body, hb_svg_rgba_t mid_color = hb_svg_lerp_rgba (color0, color1, 0.5f); hb_color_t mid = hb_svg_hb_color_from_rgba (mid_color); - hb_svg_append_str (body, "\n"); + hb_buf_append_str (body, "\n"); p0 = p1; color0 = color1; } } -static void -hb_svg_add_sweep_gradient_patches (hb_vector_t *body, - unsigned precision, - hb_color_stop_t *stops, - unsigned n_stops, - hb_paint_extend_t extend, - float cx, float cy, float radius, - float start_angle, float end_angle) -{ - if (!n_stops) return; - - hb_svg_rgba_t colors_buf[16]; - float angles_buf[16]; - hb_svg_rgba_t *colors = colors_buf; - float *angles = angles_buf; - bool dynamic = false; - - if (start_angle == end_angle) - { - if (extend == HB_PAINT_EXTEND_PAD) - { - if (start_angle > 0.f) - { - hb_svg_rgba_t c = hb_svg_rgba_from_hb_color (stops[0].color); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, c, start_angle, c); - } - if (end_angle < HB_2_PI) - { - hb_svg_rgba_t c = hb_svg_rgba_from_hb_color (stops[n_stops - 1].color); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, end_angle, c, HB_2_PI, c); - } - } - return; - } - - if (end_angle < start_angle) - { - float tmp = start_angle; start_angle = end_angle; end_angle = tmp; - for (unsigned i = 0; i < n_stops - 1 - i; i++) - { - hb_color_stop_t t = stops[i]; - stops[i] = stops[n_stops - 1 - i]; - stops[n_stops - 1 - i] = t; - } - for (unsigned i = 0; i < n_stops; i++) - stops[i].offset = 1.f - stops[i].offset; - } - - if (n_stops > 16) - { - angles = (float *) hb_malloc (sizeof (float) * n_stops); - colors = (hb_svg_rgba_t *) hb_malloc (sizeof (hb_svg_rgba_t) * n_stops); - if (!angles || !colors) - { - hb_free (angles); - hb_free (colors); - return; - } - dynamic = true; - } - - for (unsigned i = 0; i < n_stops; i++) - { - angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); - colors[i] = hb_svg_rgba_from_hb_color (stops[i].color); - } - - if (extend == HB_PAINT_EXTEND_PAD) - { - unsigned pos; - hb_svg_rgba_t color0 = colors[0]; - for (pos = 0; pos < n_stops; pos++) - { - if (angles[pos] >= 0) - { - if (pos > 0) - { - float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - color0 = hb_svg_lerp_rgba (colors[pos - 1], colors[pos], f); - } - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, color0, HB_2_PI, color0); - goto done; - } - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, color0, angles[pos], colors[pos]); - for (pos++; pos < n_stops; pos++) - { - if (angles[pos] <= HB_2_PI) - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); - else - { - float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - hb_svg_rgba_t color1 = hb_svg_lerp_rgba (colors[pos - 1], colors[pos], f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[pos - 1], colors[pos - 1], HB_2_PI, color1); - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, angles[n_stops - 1], color0, HB_2_PI, color0); - goto done; - } - } - else - { - float span = angles[n_stops - 1] - angles[0]; - if (fabsf (span) < 1e-6f) - goto done; - - int k = 0; - if (angles[0] >= 0) - { - float ss = angles[0]; - while (ss > 0) - { - if (span > 0) { ss -= span; k--; } - else { ss += span; k++; } - } - } - else - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) { ee += span; k++; } - else { ee -= span; k--; } - } - } - - span = fabsf (span); - for (int l = k; l < 1000; l++) - { - for (unsigned i = 1; i < n_stops; i++) - { - float a0_l, a1_l; - const hb_svg_rgba_t *col0, *col1; - if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) - { - a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; - a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; - col0 = &colors[n_stops - i]; - col1 = &colors[n_stops - 1 - i]; - } - else - { - a0_l = angles[i - 1] + l * span; - a1_l = angles[i] + l * span; - col0 = &colors[i - 1]; - col1 = &colors[i]; - } - - if (a1_l < 0.f) continue; - if (a0_l < 0.f) - { - float f = (0.f - a0_l) / (a1_l - a0_l); - hb_svg_rgba_t c = hb_svg_lerp_rgba (*col0, *col1, f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, 0.f, c, a1_l, *col1); - } - else if (a1_l >= HB_2_PI) - { - float f = (HB_2_PI - a0_l) / (a1_l - a0_l); - hb_svg_rgba_t c = hb_svg_lerp_rgba (*col0, *col1, f); - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, a0_l, *col0, HB_2_PI, c); - goto done; - } - else - hb_svg_add_sweep_patch (body, precision, cx, cy, radius, a0_l, *col0, a1_l, *col1); - } - } - } - -done: - if (dynamic) - { - hb_free (angles); - hb_free (colors); - } -} - static void hb_vector_paint_push_transform (hb_paint_funcs_t *, void *, float, float, float, float, float, float, @@ -712,19 +452,19 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, auto &body = paint->current_body (); unsigned sprec = hb_svg_scale_precision (paint->precision); - hb_svg_append_str (&body, "precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, dy, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, dy, paint->precision); + hb_buf_append_str (&body, ")\">\n"); } static void @@ -746,7 +486,7 @@ hb_vector_paint_pop_transform (hb_paint_funcs_t *, hb_bool_t opened = !!(paint->transform_group_open_mask & 1ull); paint->transform_group_open_mask >>= 1; if (opened) - hb_svg_append_str (&paint->current_body (), "\n"); + hb_buf_append_str (&paint->current_body (), "\n"); } static void @@ -766,26 +506,26 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, paint->path.clear (); hb_svg_path_sink_t sink = {&paint->path, paint->precision}; hb_font_draw_glyph (font, glyph, hb_svg_path_draw_funcs_get (), &sink); - hb_svg_append_str (&paint->defs, "defs, glyph); - hb_svg_append_str (&paint->defs, "\" d=\""); - hb_svg_append_len (&paint->defs, paint->path.arrayZ, paint->path.length); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, glyph); + hb_buf_append_str (&paint->defs, "\" d=\""); + hb_buf_append_len (&paint->defs, paint->path.arrayZ, paint->path.length); + hb_buf_append_str (&paint->defs, "\"/>\n"); } if (!hb_set_has (paint->defined_clips, glyph)) { hb_set_add (paint->defined_clips, glyph); - hb_svg_append_str (&paint->defs, "defs, glyph); - hb_svg_append_str (&paint->defs, "\">defs, glyph); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, glyph); + hb_buf_append_str (&paint->defs, "\">defs, glyph); + hb_buf_append_str (&paint->defs, "\"/>\n"); } - hb_svg_append_str (&paint->current_body (), "current_body (), glyph); - hb_svg_append_str (&paint->current_body (), ")\">\n"); + hb_buf_append_str (&paint->current_body (), "current_body (), glyph); + hb_buf_append_str (&paint->current_body (), ")\">\n"); } static void @@ -800,21 +540,21 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, return; unsigned clip_id = paint->clip_rect_counter++; - hb_svg_append_str (&paint->defs, "defs, clip_id); - hb_svg_append_str (&paint->defs, "\">defs, xmin, paint->precision); - hb_svg_append_str (&paint->defs, "\" y=\""); - hb_svg_append_num (&paint->defs, ymin, paint->precision); - hb_svg_append_str (&paint->defs, "\" width=\""); - hb_svg_append_num (&paint->defs, xmax - xmin, paint->precision); - hb_svg_append_str (&paint->defs, "\" height=\""); - hb_svg_append_num (&paint->defs, ymax - ymin, paint->precision); - hb_svg_append_str (&paint->defs, "\"/>\n"); + hb_buf_append_str (&paint->defs, "defs, clip_id); + hb_buf_append_str (&paint->defs, "\">defs, xmin, paint->precision); + hb_buf_append_str (&paint->defs, "\" y=\""); + hb_buf_append_num (&paint->defs, ymin, paint->precision); + hb_buf_append_str (&paint->defs, "\" width=\""); + hb_buf_append_num (&paint->defs, xmax - xmin, paint->precision); + hb_buf_append_str (&paint->defs, "\" height=\""); + hb_buf_append_num (&paint->defs, ymax - ymin, paint->precision); + hb_buf_append_str (&paint->defs, "\"/>\n"); - hb_svg_append_str (&paint->current_body (), "current_body (), clip_id); - hb_svg_append_str (&paint->current_body (), ")\">\n"); + hb_buf_append_str (&paint->current_body (), "current_body (), clip_id); + hb_buf_append_str (&paint->current_body (), ")\">\n"); } static void @@ -825,7 +565,7 @@ hb_vector_paint_pop_clip (hb_paint_funcs_t *, auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return; - hb_svg_append_str (&paint->current_body (), "\n"); + hb_buf_append_str (&paint->current_body (), "\n"); } static void @@ -847,9 +587,9 @@ hb_vector_paint_color (hb_paint_funcs_t *, (unsigned) hb_color_get_alpha (paint->foreground) * hb_color_get_alpha (color) / 255); auto &body = paint->current_body (); - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); } static hb_bool_t @@ -884,24 +624,24 @@ hb_vector_paint_image (hb_paint_funcs_t *, if (extents) { - hb_svg_append_str (&body, "x_bearing, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->y_bearing, paint->precision); - hb_svg_append_str (&body, ") scale("); - hb_svg_append_num (&body, (float) extents->width / width, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->height / height, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "x_bearing, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->y_bearing, paint->precision); + hb_buf_append_str (&body, ") scale("); + hb_buf_append_num (&body, (float) extents->width / width, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->height / height, paint->precision); + hb_buf_append_str (&body, ")\">\n"); } - hb_svg_append_len (&body, + hb_buf_append_len (&body, paint->subset_body_scratch.arrayZ, paint->subset_body_scratch.length); - hb_svg_append_c (&body, '\n'); + hb_buf_append_c (&body, '\n'); if (extents) - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); return true; } @@ -916,23 +656,23 @@ hb_vector_paint_image (hb_paint_funcs_t *, if (!png_data || !len) return false; - hb_svg_append_str (&body, "x_bearing, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->y_bearing, paint->precision); - hb_svg_append_str (&body, ") scale("); - hb_svg_append_num (&body, (float) extents->width / width, paint->precision); - hb_svg_append_c (&body, ','); - hb_svg_append_num (&body, (float) extents->height / height, paint->precision); - hb_svg_append_str (&body, ")\">\n"); + hb_buf_append_str (&body, "x_bearing, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->y_bearing, paint->precision); + hb_buf_append_str (&body, ") scale("); + hb_buf_append_num (&body, (float) extents->width / width, paint->precision); + hb_buf_append_c (&body, ','); + hb_buf_append_num (&body, (float) extents->height / height, paint->precision); + hb_buf_append_str (&body, ")\">\n"); - hb_svg_append_str (&body, "precision); - hb_svg_append_str (&body, "\" height=\""); - hb_svg_append_num (&body, (float) height, paint->precision); - hb_svg_append_str (&body, "\"/>\n\n"); + hb_buf_append_str (&body, "precision); + hb_buf_append_str (&body, "\" height=\""); + hb_buf_append_num (&body, (float) height, paint->precision); + hb_buf_append_str (&body, "\"/>\n\n"); return true; } @@ -961,26 +701,26 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, unsigned grad_id = paint->gradient_counter++; - hb_svg_append_str (&paint->defs, "defs, grad_id); - hb_svg_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); - hb_svg_append_num (&paint->defs, x0, paint->precision); - hb_svg_append_str (&paint->defs, "\" y1=\""); - hb_svg_append_num (&paint->defs, y0, paint->precision); - hb_svg_append_str (&paint->defs, "\" x2=\""); - hb_svg_append_num (&paint->defs, x1 + (x1 - x2), paint->precision); - hb_svg_append_str (&paint->defs, "\" y2=\""); - hb_svg_append_num (&paint->defs, y1 + (y1 - y2), paint->precision); - hb_svg_append_str (&paint->defs, "\" spreadMethod=\""); - hb_svg_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); - hb_svg_append_str (&paint->defs, "\">\n"); + hb_buf_append_str (&paint->defs, "defs, grad_id); + hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); + hb_buf_append_num (&paint->defs, x0, paint->precision); + hb_buf_append_str (&paint->defs, "\" y1=\""); + hb_buf_append_num (&paint->defs, y0, paint->precision); + hb_buf_append_str (&paint->defs, "\" x2=\""); + hb_buf_append_num (&paint->defs, x1 + (x1 - x2), paint->precision); + hb_buf_append_str (&paint->defs, "\" y2=\""); + hb_buf_append_num (&paint->defs, y1 + (y1 - y2), paint->precision); + hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); + hb_buf_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); + hb_buf_append_str (&paint->defs, "\">\n"); hb_svg_emit_color_stops (paint, &paint->defs, &stops); - hb_svg_append_str (&paint->defs, "\n"); + hb_buf_append_str (&paint->defs, "\n"); - hb_svg_append_str (&paint->current_body (), + hb_buf_append_str (&paint->current_body (), "current_body (), grad_id); - hb_svg_append_str (&paint->current_body (), ")\"/>\n"); + hb_buf_append_unsigned (&paint->current_body (), grad_id); + hb_buf_append_str (&paint->current_body (), ")\"/>\n"); } static void @@ -1003,33 +743,33 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, unsigned grad_id = paint->gradient_counter++; - hb_svg_append_str (&paint->defs, "defs, grad_id); - hb_svg_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\""); - hb_svg_append_num (&paint->defs, x1, paint->precision); - hb_svg_append_str (&paint->defs, "\" cy=\""); - hb_svg_append_num (&paint->defs, y1, paint->precision); - hb_svg_append_str (&paint->defs, "\" r=\""); - hb_svg_append_num (&paint->defs, r1, paint->precision); - hb_svg_append_str (&paint->defs, "\" fx=\""); - hb_svg_append_num (&paint->defs, x0, paint->precision); - hb_svg_append_str (&paint->defs, "\" fy=\""); - hb_svg_append_num (&paint->defs, y0, paint->precision); + hb_buf_append_str (&paint->defs, "defs, grad_id); + hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" cx=\""); + hb_buf_append_num (&paint->defs, x1, paint->precision); + hb_buf_append_str (&paint->defs, "\" cy=\""); + hb_buf_append_num (&paint->defs, y1, paint->precision); + hb_buf_append_str (&paint->defs, "\" r=\""); + hb_buf_append_num (&paint->defs, r1, paint->precision); + hb_buf_append_str (&paint->defs, "\" fx=\""); + hb_buf_append_num (&paint->defs, x0, paint->precision); + hb_buf_append_str (&paint->defs, "\" fy=\""); + hb_buf_append_num (&paint->defs, y0, paint->precision); if (r0 > 0) { - hb_svg_append_str (&paint->defs, "\" fr=\""); - hb_svg_append_num (&paint->defs, r0, paint->precision); + hb_buf_append_str (&paint->defs, "\" fr=\""); + hb_buf_append_num (&paint->defs, r0, paint->precision); } - hb_svg_append_str (&paint->defs, "\" spreadMethod=\""); - hb_svg_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); - hb_svg_append_str (&paint->defs, "\">\n"); + hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); + hb_buf_append_str (&paint->defs, hb_svg_extend_mode_str (hb_color_line_get_extend (color_line))); + hb_buf_append_str (&paint->defs, "\">\n"); hb_svg_emit_color_stops (paint, &paint->defs, &stops); - hb_svg_append_str (&paint->defs, "\n"); + hb_buf_append_str (&paint->defs, "\n"); - hb_svg_append_str (&paint->current_body (), + hb_buf_append_str (&paint->current_body (), "current_body (), grad_id); - hb_svg_append_str (&paint->current_body (), ")\"/>\n"); + hb_buf_append_unsigned (&paint->current_body (), grad_id); + hb_buf_append_str (&paint->current_body (), ")\"/>\n"); } static void @@ -1049,15 +789,17 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, return; qsort (stops.arrayZ, stops.length, sizeof (hb_color_stop_t), hb_svg_color_stop_cmp); - hb_svg_add_sweep_gradient_patches (&paint->current_body (), - paint->precision, - stops.arrayZ, - stops.length, - hb_color_line_get_extend (color_line), - cx, cy, - 32767.f, - start_angle, - end_angle); + + auto *body = &paint->current_body (); + unsigned precision = paint->precision; + float radius = 32767.f; + hb_sweep_gradient_tiles (stops.arrayZ, stops.length, + hb_color_line_get_extend (color_line), + start_angle, end_angle, + [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) + { hb_svg_add_sweep_patch (body, precision, cx, cy, radius, + a0, hb_svg_rgba_from_hb_color (c0), + a1, hb_svg_rgba_from_hb_color (c1)); }); } static void @@ -1090,14 +832,14 @@ hb_vector_paint_pop_group (hb_paint_funcs_t *, const char *blend = hb_svg_composite_mode_str (mode); if (blend) { - hb_svg_append_str (&body, "\n"); - hb_svg_append_len (&body, group.arrayZ, group.length); - hb_svg_append_str (&body, "\n"); + hb_buf_append_str (&body, "\n"); + hb_buf_append_len (&body, group.arrayZ, group.length); + hb_buf_append_str (&body, "\n"); } else - hb_svg_append_len (&body, group.arrayZ, group.length); + hb_buf_append_len (&body, group.arrayZ, group.length); } static hb_bool_t @@ -1149,8 +891,14 @@ hb_vector_paint_color_glyph (hb_paint_funcs_t *, hb_vector_paint_t * hb_vector_paint_create_or_fail (hb_vector_format_t format) { - if (format != HB_VECTOR_FORMAT_SVG) - return nullptr; + switch (format) + { + case HB_VECTOR_FORMAT_SVG: + case HB_VECTOR_FORMAT_PDF: + break; + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; + } hb_vector_paint_t *paint = hb_object_create (); if (unlikely (!paint)) @@ -1583,149 +1331,185 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, if (unlikely (!hb_vector_paint_ensure_initialized (paint))) return false; - bool can_cache = !paint->flat; - hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, - (unsigned) paint->palette, - paint->foreground); - if (can_cache) + switch (paint->format) { - if (paint->defined_color_glyphs.has (cache_key)) + case HB_VECTOR_FORMAT_PDF: { - uint64_t entry = paint->defined_color_glyphs.get (cache_key); - unsigned def_id = hb_svg_cache_entry_def_id (entry); - bool image_like = hb_svg_cache_entry_image_like (entry); + /* PDF: emit transform + paint directly, no caching. */ auto &body = paint->current_body (); - hb_svg_append_str (&body, "precision, - paint->x_scale_factor, - paint->y_scale_factor, - tx, ty); - else - hb_svg_append_instance_transform (&body, paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&body, "\"/>\n"); - return !body.in_error (); + hb_buf_append_str (&body, "q\n"); + /* Font and PDF coords are both Y-up; no negation needed. */ + hb_buf_append_num (&body, xx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, xy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, yy, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, tx, paint->precision); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, ty, paint->precision); + hb_buf_append_str (&body, " cm\n"); + + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_pdf_funcs_get (), paint, + (unsigned) paint->palette, + paint->foreground); + hb_buf_append_str (&body, "Q\n"); + return ret; } - } - bool has_svg_image = false; - if (can_cache) - { - if (unlikely (!paint->group_stack.push_or_fail (hb_vector_t {}))) - return false; - paint->current_color_glyph_has_svg_image = false; - } - - if (can_cache) - { - hb_codepoint_t old_gid = paint->current_svg_image_glyph; - hb_face_t *old_face = paint->current_face; - paint->current_svg_image_glyph = glyph; - paint->current_face = hb_font_get_face (font); - hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (), paint, - (unsigned) paint->palette, - paint->foreground); - paint->current_svg_image_glyph = old_gid; - paint->current_face = old_face; - if (unlikely (!ret)) + case HB_VECTOR_FORMAT_SVG: { - paint->group_stack.pop (); - return false; + bool can_cache = !paint->flat; + hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, + (unsigned) paint->palette, + paint->foreground); + if (can_cache) + { + if (paint->defined_color_glyphs.has (cache_key)) + { + uint64_t entry = paint->defined_color_glyphs.get (cache_key); + unsigned def_id = hb_svg_cache_entry_def_id (entry); + bool image_like = hb_svg_cache_entry_image_like (entry); + auto &body = paint->current_body (); + hb_buf_append_str (&body, "precision, + paint->x_scale_factor, + paint->y_scale_factor, + tx, ty); + else + hb_svg_append_instance_transform (&body, paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&body, "\"/>\n"); + return !body.in_error (); + } + } + + bool has_svg_image = false; + if (can_cache) + { + if (unlikely (!paint->group_stack.push_or_fail (hb_vector_t {}))) + return false; + paint->current_color_glyph_has_svg_image = false; + } + + if (can_cache) + { + hb_codepoint_t old_gid = paint->current_svg_image_glyph; + hb_face_t *old_face = paint->current_face; + paint->current_svg_image_glyph = glyph; + paint->current_face = hb_font_get_face (font); + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_get_funcs (), paint, + (unsigned) paint->palette, + paint->foreground); + paint->current_svg_image_glyph = old_gid; + paint->current_face = old_face; + if (unlikely (!ret)) + { + paint->group_stack.pop (); + return false; + } + + paint->captured_scratch = paint->group_stack.pop (); + has_svg_image = paint->current_color_glyph_has_svg_image; + if (unlikely (paint->captured_scratch.in_error () || + !paint->captured_scratch.length)) + return false; + + unsigned def_id = paint->color_glyph_counter++; + if (unlikely (!paint->defined_color_glyphs.set (cache_key, + hb_svg_pack_color_glyph_cache_entry (def_id, + has_svg_image)))) + return false; + + hb_buf_append_str (&paint->defs, "defs, def_id); + hb_buf_append_str (&paint->defs, "\">\n"); + hb_buf_append_len (&paint->defs, + paint->captured_scratch.arrayZ, + paint->captured_scratch.length); + hb_buf_append_str (&paint->defs, "\n"); + + auto &body = paint->current_body (); + hb_buf_append_str (&body, "precision, + paint->x_scale_factor, + paint->y_scale_factor, + tx, ty); + else + hb_svg_append_instance_transform (&body, paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&body, "\"/>\n"); + return !paint->defs.in_error () && !body.in_error (); + } + + hb_buf_append_str (&paint->current_body (), "current_body (), paint->precision, + paint->x_scale_factor, + paint->y_scale_factor, + xx, yx, xy, yy, tx, ty); + hb_buf_append_str (&paint->current_body (), "\">\n"); + hb_codepoint_t old_gid = paint->current_svg_image_glyph; + hb_face_t *old_face = paint->current_face; + paint->current_svg_image_glyph = glyph; + paint->current_face = hb_font_get_face (font); + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_get_funcs (), paint, + (unsigned) paint->palette, + paint->foreground); + paint->current_svg_image_glyph = old_gid; + paint->current_face = old_face; + hb_buf_append_str (&paint->current_body (), "\n"); + return ret && + !paint->defs.in_error () && + !paint->current_body ().in_error (); } - paint->captured_scratch = paint->group_stack.pop (); - has_svg_image = paint->current_color_glyph_has_svg_image; - if (unlikely (paint->captured_scratch.in_error () || - !paint->captured_scratch.length)) + case HB_VECTOR_FORMAT_INVALID: default: return false; - - unsigned def_id = paint->color_glyph_counter++; - if (unlikely (!paint->defined_color_glyphs.set (cache_key, - hb_svg_pack_color_glyph_cache_entry (def_id, - has_svg_image)))) - return false; - - hb_svg_append_str (&paint->defs, "defs, def_id); - hb_svg_append_str (&paint->defs, "\">\n"); - hb_svg_append_len (&paint->defs, - paint->captured_scratch.arrayZ, - paint->captured_scratch.length); - hb_svg_append_str (&paint->defs, "\n"); - - auto &body = paint->current_body (); - hb_svg_append_str (&body, "precision, - paint->x_scale_factor, - paint->y_scale_factor, - tx, ty); - else - hb_svg_append_instance_transform (&body, paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&body, "\"/>\n"); - return !paint->defs.in_error () && !body.in_error (); } - - hb_svg_append_str (&paint->current_body (), "current_body (), paint->precision, - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - hb_svg_append_str (&paint->current_body (), "\">\n"); - hb_codepoint_t old_gid = paint->current_svg_image_glyph; - hb_face_t *old_face = paint->current_face; - paint->current_svg_image_glyph = glyph; - paint->current_face = hb_font_get_face (font); - hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (), paint, - (unsigned) paint->palette, - paint->foreground); - paint->current_svg_image_glyph = old_gid; - paint->current_face = old_face; - hb_svg_append_str (&paint->current_body (), "\n"); - return ret && - !paint->defs.in_error () && - !paint->current_body ().in_error (); } /** - * hb_vector_svg_paint_set_flat: + * hb_vector_paint_set_flat: * @paint: a paint context. * @flat: whether to flatten paint output and disable glyph-group reuse. * - * Enables or disables SVG paint flattening. + * Enables or disables paint flattening. * - * Since: 13.0.0 + * XSince: REPLACEME */ void -hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, +hb_vector_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat) { paint->flat = !!flat; } /** - * hb_vector_svg_paint_set_precision: + * hb_vector_paint_set_precision: * @paint: a paint context. * @precision: decimal precision. * - * Sets numeric output precision for SVG paint output. + * Sets numeric output precision for paint output. * - * Since: 13.0.0 + * XSince: REPLACEME */ void -hb_vector_svg_paint_set_precision (hb_vector_paint_t *paint, +hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned precision) { paint->precision = hb_min (precision, 12u); @@ -1760,11 +1544,60 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) paint->captured_scratch.clear (); } +static hb_blob_t * +hb_vector_paint_render_svg (hb_vector_paint_t *paint) +{ + if (!paint->has_extents) + return nullptr; + + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return nullptr; + + hb_vector_t out; + hb_buf_recover_recycled (paint->recycled_blob, &out); + unsigned estimated = paint->defs.length + + paint->group_stack.arrayZ[0].length + + 320; + out.alloc (estimated); + hb_buf_append_str (&out, "extents.x, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.y, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_c (&out, ' '); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\" width=\""); + hb_buf_append_num (&out, paint->extents.width, paint->precision); + hb_buf_append_str (&out, "\" height=\""); + hb_buf_append_num (&out, paint->extents.height, paint->precision); + hb_buf_append_str (&out, "\">\n"); + + if (paint->defs.length) + { + hb_buf_append_str (&out, "\n"); + hb_buf_append_len (&out, paint->defs.arrayZ, paint->defs.length); + hb_buf_append_str (&out, "\n"); + } + + hb_svg_paint_append_global_transform_prefix (paint, &out); + hb_buf_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); + hb_svg_paint_append_global_transform_suffix (paint, &out); + + hb_buf_append_str (&out, "\n"); + + hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); + + hb_vector_paint_clear_render_state (paint); + + return blob; +} + /** * hb_vector_paint_render: * @paint: a paint context. * - * Renders accumulated paint content to an SVG blob. + * Renders accumulated paint content to an output blob. * * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. * @@ -1773,52 +1606,17 @@ hb_vector_paint_clear_render_state (hb_vector_paint_t *paint) hb_blob_t * hb_vector_paint_render (hb_vector_paint_t *paint) { - if (paint->format != HB_VECTOR_FORMAT_SVG) - return nullptr; - if (!paint->has_extents) - return nullptr; - - if (unlikely (!hb_vector_paint_ensure_initialized (paint))) - return nullptr; - - hb_vector_t out; - hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); - unsigned estimated = paint->defs.length + - paint->group_stack.arrayZ[0].length + - 320; - out.alloc (estimated); - hb_svg_append_str (&out, "extents.x, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.y, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.width, paint->precision); - hb_svg_append_c (&out, ' '); - hb_svg_append_num (&out, paint->extents.height, paint->precision); - hb_svg_append_str (&out, "\" width=\""); - hb_svg_append_num (&out, paint->extents.width, paint->precision); - hb_svg_append_str (&out, "\" height=\""); - hb_svg_append_num (&out, paint->extents.height, paint->precision); - hb_svg_append_str (&out, "\">\n"); - - if (paint->defs.length) + switch (paint->format) { - hb_svg_append_str (&out, "\n"); - hb_svg_append_len (&out, paint->defs.arrayZ, paint->defs.length); - hb_svg_append_str (&out, "\n"); + case HB_VECTOR_FORMAT_SVG: + return hb_vector_paint_render_svg (paint); + + case HB_VECTOR_FORMAT_PDF: + return hb_vector_paint_render_pdf (paint); + + case HB_VECTOR_FORMAT_INVALID: default: + return nullptr; } - - paint->append_global_transform_prefix (&out); - hb_svg_append_len (&out, paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); - paint->append_global_transform_suffix (&out); - - hb_svg_append_str (&out, "\n"); - - hb_blob_t *blob = hb_svg_blob_from_buffer (&paint->recycled_blob, &out); - - hb_vector_paint_clear_render_state (paint); - - return blob; } /** diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh new file mode 100644 index 000000000..befd88d8f --- /dev/null +++ b/src/hb-vector-paint.hh @@ -0,0 +1,87 @@ +#ifndef HB_VECTOR_PAINT_HH +#define HB_VECTOR_PAINT_HH + +#include "hb.hh" + +#include "hb-vector.h" +#include "hb-geometry.hh" +#include "hb-machinery.hh" +#include "hb-map.hh" +#include "hb-vector-svg-utils.hh" +#include "hb-vector-svg.hh" + +struct hb_svg_color_glyph_cache_key_t +{ + hb_codepoint_t glyph = HB_CODEPOINT_INVALID; + unsigned palette = 0; + hb_color_t foreground = 0; + + hb_svg_color_glyph_cache_key_t () = default; + hb_svg_color_glyph_cache_key_t (hb_codepoint_t g, unsigned p, hb_color_t f) + : glyph (g), palette (p), foreground (f) {} + + bool operator == (const hb_svg_color_glyph_cache_key_t &o) const + { + return glyph == o.glyph && + palette == o.palette && + foreground == o.foreground; + } + + uint32_t hash () const + { + uint32_t h = hb_hash (glyph); + h = h * 31u + hb_hash (palette); + h = h * 31u + hb_hash (foreground); + return h; + } +}; + +struct hb_vector_paint_t +{ + hb_object_header_t header; + + hb_vector_format_t format = HB_VECTOR_FORMAT_INVALID; + hb_transform_t<> transform = {1, 0, 0, 1, 0, 0}; + float x_scale_factor = 1.f; + float y_scale_factor = 1.f; + hb_vector_extents_t extents = {0, 0, 0, 0}; + bool has_extents = false; + + hb_color_t foreground = HB_COLOR (0, 0, 0, 255); + int palette = 0; + hb_hashmap_t custom_palette_colors; + unsigned precision = 2; + bool flat = false; + + hb_vector_t defs; + hb_vector_t path; + hb_vector_t> group_stack; + uint64_t transform_group_open_mask = 0; + unsigned transform_group_depth = 0; + unsigned transform_group_overflow_depth = 0; + + unsigned clip_rect_counter = 0; + unsigned gradient_counter = 0; + unsigned color_glyph_counter = 0; + unsigned color_glyph_depth = 0; + hb_set_t *defined_outlines = nullptr; + hb_set_t *defined_clips = nullptr; + hb_set_t *active_color_glyphs = nullptr; + hb_hashmap_t defined_color_glyphs; + hb_vector_t color_stops_scratch; + hb_vector_t subset_body_scratch; + hb_vector_t captured_scratch; + hb_blob_t *recycled_blob = nullptr; + bool current_color_glyph_has_svg_image = false; + hb_codepoint_t current_svg_image_glyph = HB_CODEPOINT_INVALID; + hb_face_t *current_face = nullptr; + unsigned svg_image_counter = 0; + + hb_vector_t ¤t_body () { return group_stack.tail (); } +}; + +/* Implemented in hb-vector-paint-pdf.cc */ +HB_INTERNAL hb_paint_funcs_t * hb_vector_paint_pdf_funcs_get (); +HB_INTERNAL hb_blob_t * hb_vector_paint_render_pdf (hb_vector_paint_t *paint); + +#endif /* HB_VECTOR_PAINT_HH */ diff --git a/src/hb-vector-svg-path.cc b/src/hb-vector-svg-path.cc index 286bc31a6..26acefb04 100644 --- a/src/hb-vector-svg-path.cc +++ b/src/hb-vector-svg-path.cc @@ -38,10 +38,10 @@ hb_svg_path_move_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'M'); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + 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 @@ -52,10 +52,10 @@ hb_svg_path_line_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'L'); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + 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 @@ -67,14 +67,14 @@ hb_svg_path_quadratic_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'Q'); - hb_svg_append_num (s->path, cx, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, cy, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + 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 @@ -87,18 +87,18 @@ hb_svg_path_cubic_to (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'C'); - hb_svg_append_num (s->path, c1x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, c1y, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, c2x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, c2y, s->precision); - hb_svg_append_c (s->path, ' '); - hb_svg_append_num (s->path, to_x, s->precision); - hb_svg_append_c (s->path, ','); - hb_svg_append_num (s->path, to_y, s->precision); + 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 @@ -108,7 +108,7 @@ hb_svg_path_close_path (hb_draw_funcs_t *, void *) { auto *s = (hb_svg_path_sink_t *) draw_data; - hb_svg_append_c (s->path, 'Z'); + hb_buf_append_c (s->path, 'Z'); } static inline void diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 57209bc65..2c3e0aaea 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -63,113 +63,113 @@ hb_svg_append_with_prefix (hb_vector_t *out, { if (i + 4 <= n && !memcmp (s + i, "id=\"", 4)) { - if (!hb_svg_append_len (out, s + i, 4)) return false; + if (!hb_buf_append_len (out, s + i, 4)) return false; i += 4; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 4 <= n && !memcmp (s + i, "id='", 4)) { - if (!hb_svg_append_len (out, s + i, 4)) return false; + if (!hb_buf_append_len (out, s + i, 4)) return false; i += 4; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 7 <= n && !memcmp (s + i, "href=\"#", 7)) { - if (!hb_svg_append_len (out, s + i, 7)) return false; + if (!hb_buf_append_len (out, s + i, 7)) return false; i += 7; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 7 <= n && !memcmp (s + i, "href='#", 7)) { - if (!hb_svg_append_len (out, s + i, 7)) return false; + if (!hb_buf_append_len (out, s + i, 7)) return false; i += 7; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 13 <= n && !memcmp (s + i, "xlink:href=\"#", 13)) { - if (!hb_svg_append_len (out, s + i, 13)) return false; + if (!hb_buf_append_len (out, s + i, 13)) return false; i += 13; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 13 <= n && !memcmp (s + i, "xlink:href='#", 13)) { - if (!hb_svg_append_len (out, s + i, 13)) return false; + if (!hb_buf_append_len (out, s + i, 13)) return false; i += 13; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 5 <= n && !memcmp (s + i, "url(#", 5)) { - if (!hb_svg_append_len (out, s + i, 5)) return false; + if (!hb_buf_append_len (out, s + i, 5)) return false; i += 5; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != ')') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 6 <= n && !memcmp (s + i, "url(\"#", 6)) { - if (!hb_svg_append_len (out, s + i, 6)) return false; + if (!hb_buf_append_len (out, s + i, 6)) return false; i += 6; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '"') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } if (i + 6 <= n && !memcmp (s + i, "url('#", 6)) { - if (!hb_svg_append_len (out, s + i, 6)) return false; + if (!hb_buf_append_len (out, s + i, 6)) return false; i += 6; - if (!hb_svg_append_len (out, prefix, prefix_len)) return false; + if (!hb_buf_append_len (out, prefix, prefix_len)) return false; while (i < n && s[i] != '\'') { - if (!hb_svg_append_c (out, s[i])) return false; + if (!hb_buf_append_c (out, s[i])) return false; i++; } continue; } - if (!hb_svg_append_c (out, s[i])) + if (!hb_buf_append_c (out, s[i])) return false; i++; } @@ -392,7 +392,7 @@ hb_svg_subset_glyph_image (hb_face_t *face, goto done; if (!hb_svg_append_with_prefix (defs_dst, svg + e.start, e.end - e.start, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_c (defs_dst, '\n')) + if (!hb_buf_append_c (defs_dst, '\n')) goto done; } @@ -407,19 +407,19 @@ hb_svg_subset_glyph_image (hb_face_t *face, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_str (body_dst, " width=\"")) + if (!hb_buf_append_str (body_dst, " width=\"")) goto done; - if (!hb_svg_append_unsigned (body_dst, upem)) + if (!hb_buf_append_unsigned (body_dst, upem)) goto done; - if (!hb_svg_append_str (body_dst, "\" height=\"")) + if (!hb_buf_append_str (body_dst, "\" height=\"")) goto done; - if (!hb_svg_append_unsigned (body_dst, upem)) + if (!hb_buf_append_unsigned (body_dst, upem)) goto done; - if (!hb_svg_append_str (body_dst, "\" overflow=\"visible")) + if (!hb_buf_append_str (body_dst, "\" overflow=\"visible")) goto done; - if (!hb_svg_append_c (body_dst, '"')) + if (!hb_buf_append_c (body_dst, '"')) goto done; - if (!hb_svg_append_c (body_dst, '>')) + if (!hb_buf_append_c (body_dst, '>')) goto done; ret = hb_svg_append_with_prefix (body_dst, svg + glyph_start + root_open_len, @@ -437,9 +437,9 @@ hb_svg_subset_glyph_image (hb_face_t *face, prefix, (unsigned) prefix_len)) goto done; - if (!hb_svg_append_str (body_dst, " overflow=\"visible\"")) + if (!hb_buf_append_str (body_dst, " overflow=\"visible\"")) goto done; - if (!hb_svg_append_c (body_dst, '>')) + if (!hb_buf_append_c (body_dst, '>')) goto done; ret = hb_svg_append_with_prefix (body_dst, svg + glyph_start + root_open_len, diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index 138b51b66..c8fd9e76c 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -37,7 +37,7 @@ HB_INTERNAL const char * hb_svg_decimal_point_get (void); static inline bool -hb_svg_append_len (hb_vector_t *buf, +hb_buf_append_len (hb_vector_t *buf, const char *s, unsigned len) { @@ -49,13 +49,13 @@ hb_svg_append_len (hb_vector_t *buf, } static inline bool -hb_svg_append_c (hb_vector_t *buf, char c) +hb_buf_append_c (hb_vector_t *buf, char c) { return buf->push_or_fail (c); } static inline void -hb_svg_append_num (hb_vector_t *buf, +hb_buf_append_num (hb_vector_t *buf, float v, unsigned precision, bool keep_nonzero = false) @@ -82,7 +82,7 @@ hb_svg_append_num (hb_vector_t *buf, if (!(v == v) || !std::isfinite (v)) { - hb_svg_append_c (buf, '0'); + hb_buf_append_c (buf, '0'); return; } @@ -117,7 +117,7 @@ hb_svg_append_num (hb_vector_t *buf, *end = '\0'; } - hb_svg_append_len (buf, out, (unsigned) strlen (out)); + hb_buf_append_len (buf, out, (unsigned) strlen (out)); } static inline unsigned @@ -126,4 +126,200 @@ hb_svg_scale_precision (unsigned precision) return precision < 7 ? 7 : precision; } +static inline hb_color_t +hb_color_lerp (hb_color_t c0, hb_color_t c1, float t) +{ + auto lerp = [&] (unsigned shift) -> unsigned { + unsigned v0 = (c0 >> shift) & 0xFF; + unsigned v1 = (c1 >> shift) & 0xFF; + return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); + }; + return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24)); +} + +/* Iterate sweep gradient color stop tiling across the full 0..2π range, + * calling emit_patch (a0, c0, a1, c1) for each sector. + * + * Handles pad, repeat, and reflect extend modes. Stops must be + * pre-sorted by offset. */ +template +static inline void +hb_sweep_gradient_tiles (hb_color_stop_t *stops, + unsigned n_stops, + hb_paint_extend_t extend, + float start_angle, + float end_angle, + Func emit_patch) +{ + if (!n_stops) return; + + if (start_angle == end_angle) + { + if (extend == HB_PAINT_EXTEND_PAD) + { + if (start_angle > 0.f) + emit_patch (0.f, stops[0].color, start_angle, stops[0].color); + if (end_angle < HB_2_PI) + emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); + } + return; + } + + if (end_angle < start_angle) + { + float tmp = start_angle; start_angle = end_angle; end_angle = tmp; + for (unsigned i = 0; i < n_stops - 1 - i; i++) + { + hb_color_stop_t t = stops[i]; + stops[i] = stops[n_stops - 1 - i]; + stops[n_stops - 1 - i] = t; + } + for (unsigned i = 0; i < n_stops; i++) + stops[i].offset = 1.f - stops[i].offset; + } + + /* Map stop offsets to angles. */ + float angles_buf[16]; + hb_color_t colors_buf[16]; + float *angles = angles_buf; + hb_color_t *colors = colors_buf; + bool dynamic = false; + + if (n_stops > 16) + { + angles = (float *) hb_malloc (sizeof (float) * n_stops); + colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); + if (!angles || !colors) + { + hb_free (angles); + hb_free (colors); + return; + } + dynamic = true; + } + + for (unsigned i = 0; i < n_stops; i++) + { + angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); + colors[i] = stops[i].color; + } + + if (extend == HB_PAINT_EXTEND_PAD) + { + unsigned pos; + hb_color_t color0 = colors[0]; + for (pos = 0; pos < n_stops; pos++) + { + if (angles[pos] >= 0) + { + if (pos > 0) + { + float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + color0 = hb_color_lerp (colors[pos - 1], colors[pos], f); + } + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (0.f, color0, HB_2_PI, color0); + goto done; + } + emit_patch (0.f, color0, angles[pos], colors[pos]); + for (pos++; pos < n_stops; pos++) + { + if (angles[pos] <= HB_2_PI) + emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); + else + { + float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f); + emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1); + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0); + goto done; + } + } + else + { + float span = angles[n_stops - 1] - angles[0]; + if (fabsf (span) < 1e-6f) + goto done; + + int k = 0; + if (angles[0] >= 0) + { + float ss = angles[0]; + while (ss > 0) + { + if (span > 0) { ss -= span; k--; } + else { ss += span; k++; } + } + } + else + { + float ee = angles[n_stops - 1]; + while (ee < 0) + { + if (span > 0) { ee += span; k++; } + else { ee -= span; k--; } + } + } + + span = fabsf (span); + for (int l = k; l < 1000; l++) + { + for (unsigned i = 1; i < n_stops; i++) + { + float a0_l, a1_l; + hb_color_t col0, col1; + if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) + { + a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; + a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; + col0 = colors[n_stops - i]; + col1 = colors[n_stops - 1 - i]; + } + else + { + a0_l = angles[i - 1] + l * span; + a1_l = angles[i] + l * span; + col0 = colors[i - 1]; + col1 = colors[i]; + } + + if (a1_l < 0.f) continue; + if (a0_l < 0.f) + { + float f = (0.f - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (0.f, c, a1_l, col1); + } + else if (a1_l >= HB_2_PI) + { + float f = (HB_2_PI - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (a0_l, col0, HB_2_PI, c); + goto done; + } + else + emit_patch (a0_l, col0, a1_l, col1); + } + } + } + +done: + if (dynamic) + { + hb_free (angles); + hb_free (colors); + } +} + #endif /* HB_VECTOR_SVG_UTILS_HH */ diff --git a/src/hb-vector-svg.hh b/src/hb-vector-svg.hh index 418d5bc46..5cab81e64 100644 --- a/src/hb-vector-svg.hh +++ b/src/hb-vector-svg.hh @@ -4,13 +4,13 @@ #include "hb-vector.h" static inline bool -hb_svg_append_str (hb_vector_t *buf, const char *s) +hb_buf_append_str (hb_vector_t *buf, const char *s) { - return hb_svg_append_len (buf, s, (unsigned) strlen (s)); + return hb_buf_append_len (buf, s, (unsigned) strlen (s)); } static inline bool -hb_svg_append_unsigned (hb_vector_t *buf, unsigned v) +hb_buf_append_unsigned (hb_vector_t *buf, unsigned v) { char tmp[10]; unsigned n = 0; @@ -29,15 +29,15 @@ hb_svg_append_unsigned (hb_vector_t *buf, unsigned v) } static inline bool -hb_svg_append_hex_byte (hb_vector_t *buf, unsigned v) +hb_buf_append_hex_byte (hb_vector_t *buf, unsigned v) { static const char hex[] = "0123456789ABCDEF"; char tmp[2] = {hex[(v >> 4) & 15], hex[v & 15]}; - return hb_svg_append_len (buf, tmp, 2); + return hb_buf_append_len (buf, tmp, 2); } static inline bool -hb_svg_append_base64 (hb_vector_t *buf, +hb_buf_append_base64 (hb_vector_t *buf, const uint8_t *data, unsigned len) { @@ -124,7 +124,7 @@ hb_svg_blob_meta_destroy (void *data) } static inline hb_blob_t * -hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, +hb_buf_blob_from (hb_blob_t **recycled_blob, hb_vector_t *buf) { unsigned len = 0; @@ -197,7 +197,7 @@ hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, } static inline void -hb_svg_recover_recycled_buffer (hb_blob_t *blob, +hb_buf_recover_recycled (hb_blob_t *blob, hb_vector_t *buf) { if (!blob) @@ -214,7 +214,7 @@ hb_svg_recover_recycled_buffer (hb_blob_t *blob, } static inline void -hb_svg_append_color (hb_vector_t *buf, +hb_buf_append_color (hb_vector_t *buf, hb_color_t color, bool with_alpha) { @@ -223,25 +223,25 @@ hb_svg_append_color (hb_vector_t *buf, unsigned g = hb_color_get_green (color); unsigned b = hb_color_get_blue (color); unsigned a = hb_color_get_alpha (color); - hb_svg_append_c (buf, '#'); + hb_buf_append_c (buf, '#'); if (((r >> 4) == (r & 0xF)) && ((g >> 4) == (g & 0xF)) && ((b >> 4) == (b & 0xF))) { - hb_svg_append_c (buf, hex[r & 0xF]); - hb_svg_append_c (buf, hex[g & 0xF]); - hb_svg_append_c (buf, hex[b & 0xF]); + hb_buf_append_c (buf, hex[r & 0xF]); + hb_buf_append_c (buf, hex[g & 0xF]); + hb_buf_append_c (buf, hex[b & 0xF]); } else { - hb_svg_append_hex_byte (buf, r); - hb_svg_append_hex_byte (buf, g); - hb_svg_append_hex_byte (buf, b); + hb_buf_append_hex_byte (buf, r); + hb_buf_append_hex_byte (buf, g); + hb_buf_append_hex_byte (buf, b); } if (with_alpha && a != 255) { - hb_svg_append_str (buf, "\" fill-opacity=\""); - hb_svg_append_num (buf, a / 255.f, 4); + hb_buf_append_str (buf, "\" fill-opacity=\""); + hb_buf_append_num (buf, a / 255.f, 4); } } @@ -321,31 +321,31 @@ hb_svg_append_instance_transform (hb_vector_t *out, { float sx = 1.f / x_scale_factor; float sy = 1.f / y_scale_factor; - hb_svg_append_str (out, "translate("); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_str (out, ") scale("); - hb_svg_append_num (out, sx, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -sy, sprec, true); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "translate("); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_str (out, ") scale("); + hb_buf_append_num (out, sx, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -sy, sprec, true); + hb_buf_append_c (out, ')'); } else { - hb_svg_append_str (out, "matrix("); - hb_svg_append_num (out, xx / x_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, yx / y_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -xy / x_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -yy / y_scale_factor, sprec, true); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "matrix("); + hb_buf_append_num (out, xx / x_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, yx / y_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -xy / x_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -yy / y_scale_factor, sprec, true); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_c (out, ')'); } } @@ -356,11 +356,11 @@ hb_svg_append_image_instance_translate (hb_vector_t *out, float y_scale_factor, float tx, float ty) { - hb_svg_append_str (out, "translate("); - hb_svg_append_num (out, tx / x_scale_factor, precision); - hb_svg_append_c (out, ','); - hb_svg_append_num (out, -ty / y_scale_factor, precision); - hb_svg_append_c (out, ')'); + hb_buf_append_str (out, "translate("); + hb_buf_append_num (out, tx / x_scale_factor, precision); + hb_buf_append_c (out, ','); + hb_buf_append_num (out, -ty / y_scale_factor, precision); + hb_buf_append_c (out, ')'); } #endif /* HB_VECTOR_SVG_HH */ diff --git a/src/hb-vector.h b/src/hb-vector.h index dd32f2ec8..ad7e77560 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -43,6 +43,7 @@ HB_BEGIN_DECLS typedef enum { HB_VECTOR_FORMAT_INVALID = HB_TAG_NONE, HB_VECTOR_FORMAT_SVG = HB_TAG ('s','v','g',' '), + HB_VECTOR_FORMAT_PDF = HB_TAG ('p','d','f',' '), } hb_vector_format_t; /** @@ -161,11 +162,11 @@ hb_vector_draw_glyph (hb_vector_draw_t *draw, hb_vector_extents_mode_t extents_mode); HB_EXTERN void -hb_vector_svg_set_flat (hb_vector_draw_t *draw, +hb_vector_draw_set_flat (hb_vector_draw_t *draw, hb_bool_t flat); HB_EXTERN void -hb_vector_svg_set_precision (hb_vector_draw_t *draw, +hb_vector_draw_set_precision (hb_vector_draw_t *draw, unsigned precision); HB_EXTERN hb_blob_t * @@ -263,11 +264,11 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, hb_vector_extents_mode_t extents_mode); HB_EXTERN void -hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, +hb_vector_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat); HB_EXTERN void -hb_vector_svg_paint_set_precision (hb_vector_paint_t *paint, +hb_vector_paint_set_precision (hb_vector_paint_t *paint, unsigned precision); HB_EXTERN hb_blob_t * diff --git a/src/meson.build b/src/meson.build index 5451f0dcc..adb680354 100644 --- a/src/meson.build +++ b/src/meson.build @@ -273,6 +273,7 @@ hb_base_sources = files( 'hb-unicode.hh', 'hb-utf.hh', 'hb-vector.hh', + 'hb-vector-draw.hh', 'hb.hh', ) @@ -752,9 +753,10 @@ hb_vector_sources = files( 'hb-zlib.cc', 'hb-zlib.hh', 'hb-vector.cc', - 'hb-vector-svg-draw.cc', + 'hb-vector-draw.cc', 'hb-vector-svg-path.cc', - 'hb-vector-svg-paint.cc', + 'hb-vector-paint.cc', + 'hb-vector-paint-pdf.cc', 'hb-vector-svg-path.hh', 'hb-vector-svg-subset.cc', 'hb-vector-svg-subset.hh', diff --git a/src/rust/font.rs b/src/rust/font.rs index 08f44e145..8e6b526b0 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -879,9 +879,10 @@ impl ColorPainter for HbColorPainter<'_> { } } } - fn push_layer(&mut self, _mode: CompositeMode) { + fn push_layer(&mut self, mode: CompositeMode) { + let mode = mode as hb_paint_composite_mode_t; unsafe { - hb_paint_push_group(self.paint_funcs, self.paint_data); + hb_paint_push_group_for(self.paint_funcs, self.paint_data, mode); } } fn pop_layer_with_mode(&mut self, mode: CompositeMode) { diff --git a/test/fuzzing/hb-vector-fuzzer.cc b/test/fuzzing/hb-vector-fuzzer.cc index fc096a739..e58400a9b 100644 --- a/test/fuzzing/hb-vector-fuzzer.cc +++ b/test/fuzzing/hb-vector-fuzzer.cc @@ -21,10 +21,10 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); unsigned precision = size ? data[size - 1] % 5 : 0; - hb_vector_svg_set_precision (draw, precision); - hb_vector_svg_paint_set_precision (paint, precision); - hb_vector_svg_set_flat (draw, size > 1 ? !!(data[size - 2] & 1) : false); - hb_vector_svg_paint_set_flat (paint, size > 2 ? !!(data[size - 3] & 1) : false); + hb_vector_draw_set_precision (draw, precision); + hb_vector_paint_set_precision (paint, precision); + hb_vector_draw_set_flat (draw, size > 1 ? !!(data[size - 2] & 1) : false); + hb_vector_paint_set_flat (paint, size > 2 ? !!(data[size - 3] & 1) : false); unsigned glyph_count = hb_face_get_glyph_count (input.face); unsigned limit = glyph_count > 16 ? 16 : glyph_count; diff --git a/util/vector-output.hh b/util/vector-output.hh index ef11ffa6f..c1d22bf0c 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -36,6 +36,12 @@ #include "hb-ot.h" +static const char *vector_supported_formats[] = { + "svg", + "pdf", + nullptr +}; + struct vector_output_t : output_options_t<>, view_options_t { static const bool repeat_shape = false; @@ -49,9 +55,9 @@ struct vector_output_t : output_options_t<>, view_options_t void add_options (option_parser_t *parser) { parser->set_summary ("Draw text with given font."); - parser->set_description ("Shows shaped glyph outlines as SVG."); + parser->set_description ("Shows shaped glyph outlines as SVG or PDF."); - output_options_t::add_options (parser); + output_options_t::add_options (parser, vector_supported_formats); view_options_t::add_options (parser); GOptionEntry entries[] = @@ -80,6 +86,18 @@ struct vector_output_t : output_options_t<>, view_options_t return; } + if (output_format && + g_ascii_strcasecmp (output_format, "svg") != 0 && + g_ascii_strcasecmp (output_format, "pdf") != 0) + { + char *items = g_strjoinv ("/", const_cast (vector_supported_formats)); + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + "Unknown output format `%s'; supported formats are: %s", + output_format, items); + g_free (items); + return; + } + foreground = HB_COLOR (foreground_color.b, foreground_color.g, foreground_color.r, @@ -179,22 +197,29 @@ struct vector_output_t : output_options_t<>, view_options_t return; final_extents = extents; - hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (HB_VECTOR_FORMAT_SVG); - hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG); + hb_vector_format_t fmt = HB_VECTOR_FORMAT_SVG; + if (output_format && g_ascii_strcasecmp (output_format, "pdf") == 0) + fmt = HB_VECTOR_FORMAT_PDF; + + hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (fmt); + hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (fmt); hb_vector_draw_set_scale_factor (draw, 1.f, 1.f); hb_vector_draw_set_extents (draw, &extents); - hb_vector_svg_set_precision (draw, precision); - hb_vector_svg_set_flat (draw, flat); + hb_vector_draw_set_precision (draw, precision); + hb_vector_draw_set_flat (draw, flat); - hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); - hb_vector_paint_set_extents (paint, &extents); - hb_vector_paint_set_foreground (paint, foreground); - hb_vector_paint_set_palette (paint, this->palette); - apply_custom_palette (paint); - init_palette_color_cache (); - hb_vector_svg_paint_set_precision (paint, precision); - hb_vector_svg_paint_set_flat (paint, flat); + if (paint) + { + hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); + hb_vector_paint_set_extents (paint, &extents); + hb_vector_paint_set_foreground (paint, foreground); + hb_vector_paint_set_palette (paint, this->palette); + apply_custom_palette (paint); + init_palette_color_cache (); + hb_vector_paint_set_precision (paint, precision); + hb_vector_paint_set_flat (paint, flat); + } bool had_draw = false; bool had_paint = false; @@ -225,20 +250,23 @@ struct vector_output_t : output_options_t<>, view_options_t float pen_x = g.x + off_x; float pen_y = g.y + off_y; - if (use_foreground_palette) + if (paint) { - const rgba_color_t &c = - g_array_index (foreground_palette, rgba_color_t, - palette_glyph_index++ % foreground_palette->len); - hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a)); - } + if (use_foreground_palette) + { + const rgba_color_t &c = + g_array_index (foreground_palette, rgba_color_t, + palette_glyph_index++ % foreground_palette->len); + hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a)); + } - hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); - if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, - extents_mode)) - { - had_paint = true; - continue; + hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); + if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, + extents_mode)) + { + had_paint = true; + continue; + } } if (hb_vector_draw_glyph (draw, upem_font, g.gid, pen_x, pen_y, @@ -810,6 +838,13 @@ struct vector_output_t : output_options_t<>, view_options_t if (!in_data || !in_len) return; + /* PDF blobs are self-contained; write directly. */ + if (in_len > 5 && memcmp (in_data, "%PDF-", 5) == 0) + { + fwrite (in_data, 1, in_len, out_fp); + return; + } + const char *hdr_end = (const char *) memchr (in_data, '>', in_len); if (!hdr_end) return;