Merge pull request #5891 from harfbuzz/vector-pdf

PDF output from hb-vector
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-04-11 22:20:01 -06:00
commit 0cf2fa0929
22 files changed
+2740 -948

No files matched your search

+3 -2
View File
@@ -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
+7 -4
View File
@@ -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
</SECTION>
@@ -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
+2 -2
View File
@@ -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);
+3 -2
View File
@@ -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"
+27
View File
@@ -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
+43
View File
@@ -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);
+6
View File
@@ -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,
+2 -2
View File
@@ -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)
@@ -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 <algorithm>
#include <math.h>
#include <string.h>
#include "hb-vector-svg.hh"
HB_UNUSED static inline bool
hb_svg_buffer_contains (const hb_vector_t<char> &buf, const char *needle)
{
@@ -55,35 +50,6 @@ hb_svg_buffer_contains (const hb_vector_t<char> &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<char> defs;
hb_vector_t<char> body;
hb_vector_t<char> 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<hb_vector_draw_t> ();
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, "<path id=\"p");
hb_buf_append_unsigned (&draw->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, "<path d=\"");
hb_buf_append_len (&draw->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, "<use href=\"#p");
hb_buf_append_unsigned (&draw->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, "<path id=\"p");
hb_svg_append_unsigned (&draw->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, "<path d=\"");
hb_svg_append_len (&draw->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, "<use href=\"#p");
hb_svg_append_unsigned (&draw->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<char> 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, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"");
hb_svg_append_num (&out, draw->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, "<defs>\n");
hb_svg_append_len (&out, draw->defs.arrayZ, draw->defs.length);
hb_svg_append_str (&out, "</defs>\n");
}
hb_vector_t<char> 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 d=\"");
hb_svg_append_len (&out, draw->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, "</svg>\n");
/* Build PDF objects, tracking byte offsets for xref. */
hb_vector_t<char> 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<char> 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, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"");
hb_buf_append_num (&out, draw->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, "<defs>\n");
hb_buf_append_len (&out, draw->defs.arrayZ, draw->defs.length);
hb_buf_append_str (&out, "</defs>\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 d=\"");
hb_buf_append_len (&out, draw->path.arrayZ, draw->path.length);
hb_buf_append_str (&out, "\"/>\n");
}
hb_buf_append_str (&out, "</svg>\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.
+46
View File
@@ -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<char> defs;
hb_vector_t<char> body;
hb_vector_t<char> 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 */
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+87
View File
@@ -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<unsigned, hb_color_t> custom_palette_colors;
unsigned precision = 2;
bool flat = false;
hb_vector_t<char> defs;
hb_vector_t<char> path;
hb_vector_t<hb_vector_t<char>> 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<hb_svg_color_glyph_cache_key_t, uint64_t> defined_color_glyphs;
hb_vector_t<hb_color_stop_t> color_stops_scratch;
hb_vector_t<char> subset_body_scratch;
hb_vector_t<char> 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<char> &current_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 */
+29 -29
View File
@@ -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
+38 -38
View File
@@ -63,113 +63,113 @@ hb_svg_append_with_prefix (hb_vector_t<char> *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,
+201 -5
View File
@@ -37,7 +37,7 @@ HB_INTERNAL const char *
hb_svg_decimal_point_get (void);
static inline bool
hb_svg_append_len (hb_vector_t<char> *buf,
hb_buf_append_len (hb_vector_t<char> *buf,
const char *s,
unsigned len)
{
@@ -49,13 +49,13 @@ hb_svg_append_len (hb_vector_t<char> *buf,
}
static inline bool
hb_svg_append_c (hb_vector_t<char> *buf, char c)
hb_buf_append_c (hb_vector_t<char> *buf, char c)
{
return buf->push_or_fail (c);
}
static inline void
hb_svg_append_num (hb_vector_t<char> *buf,
hb_buf_append_num (hb_vector_t<char> *buf,
float v,
unsigned precision,
bool keep_nonzero = false)
@@ -82,7 +82,7 @@ hb_svg_append_num (hb_vector_t<char> *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<char> *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 <typename Func>
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 */
+45 -45
View File
@@ -4,13 +4,13 @@
#include "hb-vector.h"
static inline bool
hb_svg_append_str (hb_vector_t<char> *buf, const char *s)
hb_buf_append_str (hb_vector_t<char> *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<char> *buf, unsigned v)
hb_buf_append_unsigned (hb_vector_t<char> *buf, unsigned v)
{
char tmp[10];
unsigned n = 0;
@@ -29,15 +29,15 @@ hb_svg_append_unsigned (hb_vector_t<char> *buf, unsigned v)
}
static inline bool
hb_svg_append_hex_byte (hb_vector_t<char> *buf, unsigned v)
hb_buf_append_hex_byte (hb_vector_t<char> *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<char> *buf,
hb_buf_append_base64 (hb_vector_t<char> *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<char> *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<char> *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<char> *buf,
hb_buf_append_color (hb_vector_t<char> *buf,
hb_color_t color,
bool with_alpha)
{
@@ -223,25 +223,25 @@ hb_svg_append_color (hb_vector_t<char> *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<char> *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<char> *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 */
+5 -4
View File
@@ -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 *
+4 -2
View File
@@ -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',
+3 -2
View File
@@ -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) {
+4 -4
View File
@@ -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;
+61 -26
View File
@@ -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<char **> (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;