diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 21ae06b78..86a9eb12b 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -833,8 +833,6 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); - hb_vector_draw_clear (draw); - return blob; } @@ -918,8 +916,6 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) hb_blob_t *blob = hb_buf_blob_from (&draw->recycled_blob, &out); - hb_vector_draw_clear (draw); - return blob; } @@ -928,6 +924,8 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) * @draw: a draw context. * * Renders accumulated draw content to an output blob. + * Accumulated content and extents are cleared on return, including + * when rendering fails. User configuration is preserved. * * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. * @@ -936,6 +934,7 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) hb_blob_t * hb_vector_draw_render (hb_vector_draw_t *draw) { + HB_SCOPE_GUARD (hb_vector_draw_clear (draw)); switch (draw->format) { case HB_VECTOR_FORMAT_SVG: diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index bf13eb8f0..439b93f53 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -1794,7 +1794,5 @@ hb_vector_paint_render_pdf (hb_vector_paint_t *paint) hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); - hb_vector_paint_clear (paint); - return blob; } diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 67fc396ba..07eecf62a 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -983,7 +983,5 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) hb_blob_t *blob = hb_buf_blob_from (&paint->recycled_blob, &out); - hb_vector_paint_clear (paint); - return blob; } diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index fc5e3c6cc..2b0cb503b 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -759,6 +759,8 @@ hb_vector_paint_clear (hb_vector_paint_t *paint) * @paint: a paint context. * * Renders accumulated paint content to an output blob. + * Accumulated content and extents are cleared on return, including + * when rendering fails. User configuration is preserved. * * Return value: (transfer full) (nullable): output blob, or `NULL` if rendering cannot proceed. * @@ -767,6 +769,7 @@ hb_vector_paint_clear (hb_vector_paint_t *paint) hb_blob_t * hb_vector_paint_render (hb_vector_paint_t *paint) { + HB_SCOPE_GUARD (hb_vector_paint_clear (paint)); switch (paint->format) { case HB_VECTOR_FORMAT_SVG: diff --git a/src/hb-vector.cc b/src/hb-vector.cc index 2d98059f9..68a55844b 100644 --- a/src/hb-vector.cc +++ b/src/hb-vector.cc @@ -65,9 +65,9 @@ * Both contexts accumulate multiple glyphs into a single document. * Call hb_vector_draw_render() / hb_vector_paint_render() to * retrieve the final blob. Rendering clears all accumulated - * content (including extents), so retrieve any needed extents - * via hb_vector_draw_get_extents() / hb_vector_paint_get_extents() - * before rendering. + * content (including extents), even on failure, so retrieve any + * needed extents via hb_vector_draw_get_extents() / + * hb_vector_paint_get_extents() before rendering. * * Each glyph is emitted as an independent element. If glyphs * overlap and the foreground color is semi-transparent, the diff --git a/test/vector/test-vector-reset.c b/test/vector/test-vector-reset.c index 5605d8b11..d1f8037f4 100644 --- a/test/vector/test-vector-reset.c +++ b/test/vector/test-vector-reset.c @@ -109,6 +109,87 @@ draw_triangle (hb_vector_draw_t *draw) hb_vector_draw_new_path (draw); } +static void +assert_blob_equal (hb_blob_t *output, hb_blob_t *expected) +{ + assert (output && expected); + unsigned int length, expected_length; + const char *data = hb_blob_get_data (output, &length); + const char *expected_data = hb_blob_get_data (expected, &expected_length); + assert (length && length == expected_length); + assert (!memcmp (data, expected_data, length)); + hb_blob_destroy (output); + hb_blob_destroy (expected); +} + +static void +test_draw_render_failure (hb_vector_format_t format) +{ + hb_vector_draw_t *fresh = hb_vector_draw_create_or_fail (format); + hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (format); + assert (fresh && draw); + hb_color_t foreground = HB_COLOR (0, 0, 255, 128); + hb_vector_draw_set_foreground (fresh, foreground); + hb_vector_draw_set_foreground (draw, foreground); + + /* Discard content, including PDF opacity resources, on failure. */ + draw_triangle (draw); + hb_vector_draw_set_extents (draw, NULL); + assert (!hb_vector_draw_render (draw)); + assert (hb_vector_draw_get_foreground (draw) == foreground); + + draw_triangle (draw); + draw_triangle (fresh); + assert_blob_equal (hb_vector_draw_render (draw), hb_vector_draw_render (fresh)); + assert (!hb_vector_draw_get_extents (draw, NULL)); + hb_vector_draw_destroy (draw); + hb_vector_draw_destroy (fresh); +} + +static void +paint_rectangle (hb_vector_paint_t *paint, hb_color_t color) +{ + hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint); + hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 10, 10); + hb_paint_color (funcs, paint, 0, color); + hb_paint_pop_clip (funcs, paint); +} + +static void +test_paint_render_failure (hb_vector_format_t format) +{ + hb_vector_paint_t *fresh = hb_vector_paint_create_or_fail (format); + hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (format); + assert (fresh && paint); + hb_vector_extents_t extents = {0, 0, 10, 10}; + hb_vector_paint_set_svg_prefix (fresh, "kept-"); + hb_vector_paint_set_svg_prefix (paint, "kept-"); + + /* An empty PDF render fails even when extents are available. */ + hb_vector_paint_set_extents (paint, &extents); + hb_blob_t *empty = hb_vector_paint_render (paint); + if (format == HB_VECTOR_FORMAT_PDF) + assert (!empty); + else + assert (empty); + hb_blob_destroy (empty); + assert (!hb_vector_paint_get_extents (paint, NULL)); + + /* Missing extents must discard the old content and resources too. */ + paint_rectangle (paint, HB_COLOR (255, 0, 0, 128)); + assert (!hb_vector_paint_render (paint)); + assert (!strcmp (hb_vector_paint_get_svg_prefix (paint), "kept-")); + + hb_vector_paint_set_extents (paint, &extents); + hb_vector_paint_set_extents (fresh, &extents); + paint_rectangle (paint, HB_COLOR (0, 0, 255, 128)); + paint_rectangle (fresh, HB_COLOR (0, 0, 255, 128)); + assert_blob_equal (hb_vector_paint_render (paint), hb_vector_paint_render (fresh)); + assert (!hb_vector_paint_get_extents (paint, NULL)); + hb_vector_paint_destroy (paint); + hb_vector_paint_destroy (fresh); +} + static void test_pdf_clear (void) { @@ -166,5 +247,9 @@ main (void) test_paint_reset (HB_VECTOR_FORMAT_SVG); test_paint_reset (HB_VECTOR_FORMAT_PDF); test_pdf_clear (); + test_draw_render_failure (HB_VECTOR_FORMAT_SVG); + test_draw_render_failure (HB_VECTOR_FORMAT_PDF); + test_paint_render_failure (HB_VECTOR_FORMAT_SVG); + test_paint_render_failure (HB_VECTOR_FORMAT_PDF); return 0; }