[vector] Write the Porter-Duff composite modes in PDF PDF has the blend modes and none of the Porter-Duff operators, so pop_group painted PLUS as Screen, XOR as Difference, and the other ten modes as Normal. composite_CLEAR in test_glyphs-glyf_colr_1.ttf came out with both of the squares that it should have erased, and the 28 glyphs of that font that composite in DEST_OVER came out with their two layers the wrong way up. Now that groups are Form XObjects, a group can be a soft mask as well as be painted, and that is all these modes take: CLEAR, SRC, DEST, DEST_OVER neither, one, or both the other way up SRC_IN, DEST_IN one group, through the other's alpha SRC_OUT, DEST_OUT one, through the inverse of it SRC_ATOP the backdrop, then SRC_IN DEST_ATOP the source, then DEST_IN XOR DEST_OUT, then SRC_OUT in Screen The inverse of a group's alpha is the luminosity of a group that is white with black painted over it through the alpha mask. A transfer function on the alpha mask would say that in fewer objects, but poppler's cairo backend does not apply one. The last three, and PLUS, are sums of two layers, and PDF has no way to add: the upper layer hides some of the lower where both are partly transparent. As written, SRC_ATOP is exact where the backdrop is opaque or absent, DEST_ATOP where the source is, and XOR where either is. PLUS stays Screen. The API documentation now says which modes are approximate, in PDF and in SVG. As in the SVG writer, the backdrop is everything the enclosing group has painted: transforms and clips that are open in it are closed around the backdrop, repeated around the source, and opened again; pops that do not match their pushes leave the content streams balanced; and what a document refers to more than once counts against the document cap. Something painted with a blend mode in the root group would blend with the background too, so a root that has one is painted as a group of its own. render now fails if the root's content stream was cut short, as it does for a missing object. The graphics states that are kept for sizing sweep gradients are popped where a Q is written, so that they go on describing the content stream when a pop has nothing to close. Testing: hb-vector's PDF for the thirteen composite_* glyphs from CLEAR to PLUS, and for modes.ttf from How HarfBuzz Works, has the pixels hb-raster gives them in poppler (Splash and cairo), Ghostscript, pdfium and MuPDF, antialiased edges aside. Over the whole of test_glyphs-glyf_colr_1.ttf, 30 more glyphs now match hb-raster in PDF, and with the two SVG changes before this one, 41 in SVG; none fewer. Glyphs without a composite are byte for byte what they were. Replaying the same paint calls on hb-raster and hb-vector for 300 random scenes of nested groups and translucent fills, with groups popped under open transforms and clips and at the root, the nine exact modes agree in poppler, pdfium and MuPDF on every scene. Ghostscript 10.05 differs on six. The one that was reduced comes down to a group that sets a soft mask being painted through another, inside the group of a third, which Ghostscript also gets wrong in a PDF written by hand. The vector fuzzer and these scenes were also run under ASan and UBSan, with and without failing allocations. The composite test gains the PDF counterparts of its SVG checks, and the thirteen glyphs a second expected output. Full test suite passes. Fixes: https://github.com/harfbuzz/harfbuzz/issues/6829 Fixes: https://github.com/harfbuzz/harfbuzz/issues/5951 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index e5e5bf1..9c4f4fb 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc
@@ -41,7 +41,10 @@ * - Solid colors with alpha (via ExtGState) * - Glyph and rectangle clipping * - Affine transforms - * - Groups (save/restore) + * - Groups (Form XObjects that are isolated transparency groups) + * - Compositing modes: the blend modes as PDF's own, and the + * Porter-Duff modes with soft masks (SRC_ATOP, DEST_ATOP, XOR and + * PLUS only approximately) * - Linear gradients (PDF Type 2 axial shading, PAD extension only) * - Radial gradients (PDF Type 3 radial shading, PAD extension only) * - Sweep gradients (PDF Type 6 Coons-patch meshes) @@ -158,53 +161,81 @@ return id; } + /* Add an ExtGState, return resource name index. */ + unsigned add_extgstate (hb_vector_buf_t &&obj) + { + unsigned idx = extgstate_count++; + unsigned obj_id = add_object (std::move (obj)); + + extgstate_dict.append_str ("/GS"); + extgstate_dict.append_unsigned (idx); + extgstate_dict.append_c (' '); + extgstate_dict.append_unsigned (obj_id); + extgstate_dict.append_str (" 0 R "); + return idx; + } + /* Add ExtGState for fill opacity, return resource name index. */ unsigned add_extgstate_alpha (float alpha) { - unsigned idx = extgstate_count++; hb_vector_buf_t obj; obj.append_str ("<< /Type /ExtGState /ca "); obj.append_num (alpha, 4); obj.append_str (" >>"); - unsigned obj_id = add_object (std::move (obj)); - - extgstate_dict.append_str ("/GS"); - extgstate_dict.append_unsigned (idx); - extgstate_dict.append_c (' '); - extgstate_dict.append_unsigned (obj_id); - extgstate_dict.append_str (" 0 R "); - return idx; + return add_extgstate (std::move (obj)); } - /* Add ExtGState for blend mode, return resource name index. */ - unsigned add_extgstate_blend (const char *bm) + /* Add ExtGState for painting a group: with blend mode @bm, if not + * null, and through a soft mask, if @mask_id is not zero. The mask + * is the alpha of the transparency group that is object @mask_id or, + * with @inverse, one minus it. Return resource name index. */ + unsigned add_extgstate_group (const char *bm, + unsigned mask_id = 0, + bool inverse = false) { - unsigned idx = extgstate_count++; - hb_vector_buf_t obj; - obj.append_str ("<< /Type /ExtGState /BM /"); - obj.append_str (bm); - obj.append_str (" >>"); - unsigned obj_id = add_object (std::move (obj)); + if (mask_id && inverse) + { + /* A soft mask is inverted by a transfer function, but poppler's + * cairo backend does not apply one to an alpha mask. So take + * the luminosity of a group that is white, with black painted + * over it through the alpha mask. */ + unsigned alpha_idx = add_extgstate_group (nullptr, mask_id); + hb_vector_buf_t stream; + stream.append_str ("1 g\n-32767 -32767 65534 65534 re f\n/GS"); + stream.append_unsigned (alpha_idx); + stream.append_str (" gs\n0 g\n-32767 -32767 65534 65534 re f\n"); + mask_id = add_group (stream, true); + } - extgstate_dict.append_str ("/GS"); - extgstate_dict.append_unsigned (idx); - extgstate_dict.append_c (' '); - extgstate_dict.append_unsigned (obj_id); - extgstate_dict.append_str (" 0 R "); - return idx; + hb_vector_buf_t obj; + obj.append_str ("<< /Type /ExtGState"); + if (bm) + { + obj.append_str (" /BM /"); + obj.append_str (bm); + } + if (mask_id) + { + obj.append_str (inverse ? " /SMask << /Type /Mask /S /Luminosity /G " + : " /SMask << /Type /Mask /S /Alpha /G "); + obj.append_unsigned (mask_id); + obj.append_str (" 0 R >>"); + } + obj.append_str (" >>"); + return add_extgstate (std::move (obj)); } /* Add a Form XObject that is an isolated transparency group with - * @stream as its content, to be painted with `/FmN Do`. Stores - * the resource name index in @idx. */ - bool add_form_group (const hb_vector_buf_t &stream, unsigned *idx) + * @stream as its content, in DeviceRGB or, for a group whose + * luminosity is a soft mask, DeviceGray. Return object id. */ + unsigned add_group (const hb_vector_buf_t &stream, bool gray = false) { if (!resources_obj_id) { hb_vector_buf_t placeholder; resources_obj_id = add_object (std::move (placeholder)); if (unlikely (!resources_obj_id)) - return false; + return 0; } /* The form is painted where its content would have been, so it @@ -214,7 +245,8 @@ hb_vector_buf_t form; form.append_str ("<< /Type /XObject /Subtype /Form\n"); form.append_str ("/BBox [-32767 -32767 32767 32767]\n"); - form.append_str ("/Group << /S /Transparency /CS /DeviceRGB /I true >>\n"); + form.append_str (gray ? "/Group << /S /Transparency /CS /DeviceGray /I true >>\n" + : "/Group << /S /Transparency /CS /DeviceRGB /I true >>\n"); form.append_str ("/Resources "); form.append_unsigned (resources_obj_id); form.append_str (" 0 R\n/Length "); @@ -222,9 +254,20 @@ form.append_str (" >>\nstream\n"); form.append_len (stream.arrayZ, stream.length); form.append_str ("endstream"); - unsigned form_id = add_object (std::move (form)); + return add_object (std::move (form)); + } + + /* Add a group, as above, to be painted with `/FmN Do`. Stores the + * resource name index in @idx and, if not null, the object id in + * @obj_id. */ + bool add_form_group (const hb_vector_buf_t &stream, unsigned *idx, + unsigned *obj_id = nullptr) + { + unsigned form_id = add_group (stream); if (unlikely (!form_id)) return false; + if (obj_id) + *obj_id = form_id; *idx = form_count++; xobject_dict.append_str ("/Fm"); @@ -268,20 +311,12 @@ unsigned form_id = add_object (std::move (form)); /* ExtGState with luminosity soft mask. */ - unsigned idx = extgstate_count++; hb_vector_buf_t gs; gs.append_str ("<< /Type /ExtGState\n"); gs.append_str ("/SMask << /Type /Mask /S /Luminosity /G "); gs.append_unsigned (form_id); gs.append_str (" 0 R >> >>"); - unsigned gs_id = add_object (std::move (gs)); - - extgstate_dict.append_str ("/GS"); - extgstate_dict.append_unsigned (idx); - extgstate_dict.append_c (' '); - extgstate_dict.append_unsigned (gs_id); - extgstate_dict.append_str (" 0 R "); - return idx; + return add_extgstate (std::move (gs)); } /* Add a shading, return resource name index. */ @@ -493,6 +528,16 @@ /* ---- paint callbacks ---- */ +/* Closes a transform or clip that scope_closed() found open: the + * graphics state it saved ends with it. */ +static void +hb_pdf_close_scope (hb_vector_paint_t *paint) +{ + paint->current_body ().append_str ("Q\n"); + if (auto *res = hb_pdf_get_resources (paint)) + res->pop_gstate (); +} + static void hb_pdf_paint_push_transform (hb_paint_funcs_t *, void *paint_data, @@ -506,6 +551,7 @@ return; auto &body = paint->current_body (); + unsigned start = body.length; unsigned sprec = body.scale_precision (); body.append_str ("q\n"); body.append_num (xx, sprec); @@ -520,6 +566,7 @@ body.append_c (' '); body.append_num (paint->sy (dy)); body.append_str (" cm\n"); + paint->scope_opened (start); if (auto *res = hb_pdf_get_resources (paint)) { @@ -538,9 +585,8 @@ auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!paint->ensure_initialized ())) return; - paint->current_body ().append_str ("Q\n"); - if (auto *res = hb_pdf_get_resources (paint)) - res->pop_gstate (); + if (paint->scope_closed ()) + hb_pdf_close_scope (paint); } static void @@ -607,10 +653,12 @@ return; auto &body = paint->current_body (); + unsigned start = body.length; body.append_str ("q\n"); hb_extents_t<> extents; hb_pdf_emit_glyph_path (paint, font, glyph, &body, &extents); body.append_str ("W n\n"); + paint->scope_opened (start); if (auto *res = hb_pdf_get_resources (paint)) res->push_clip (extents); @@ -628,6 +676,7 @@ return; auto &body = paint->current_body (); + unsigned start = body.length; body.append_str ("q\n"); body.append_num (paint->sx (xmin)); body.append_c (' '); @@ -637,6 +686,7 @@ body.append_c (' '); body.append_num (paint->sy (ymax - ymin)); body.append_str (" re W n\n"); + paint->scope_opened (start); if (auto *res = hb_pdf_get_resources (paint)) res->push_clip ({hb_min (paint->sx (xmin), paint->sx (xmax)), @@ -659,7 +709,9 @@ } auto &body = paint->current_body (); + unsigned start = body.length; body.append_str ("q\n"); + paint->scope_opened (start); /* Stream path operators straight into the body; end() seals * the path with "W n" to turn it into the clip region. * Coordinates arrive in font-scale; the sink divides by @@ -690,6 +742,8 @@ if (unlikely (!paint->ensure_initialized ())) return; paint->current_body ().append_str ("W n\n"); + /* The path is part of what opened the clip. */ + paint->scope_extended (); auto *res = hb_pdf_get_resources (paint); if (res && paint->clip_path_sink.extents) @@ -709,9 +763,8 @@ auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!paint->ensure_initialized ())) return; - paint->current_body ().append_str ("Q\n"); - if (auto *res = hb_pdf_get_resources (paint)) - res->pop_gstate (); + if (paint->scope_closed ()) + hb_pdf_close_scope (paint); } /* Paint a solid color, including alpha via ExtGState. */ @@ -1671,11 +1724,12 @@ 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"; - /* Porter-Duff modes have no PDF blend-mode equivalent; approximate - * the two that have a plausible color-blend analog, and let the - * rest fall through to Normal (SRC_OVER). */ + /* PLUS adds the source to the backdrop, and no blend mode adds. + * Screen is the nearest: the sum, less the product. */ case HB_PAINT_COMPOSITE_MODE_PLUS: return "Screen"; - case HB_PAINT_COMPOSITE_MODE_XOR: return "Difference"; + /* The other Porter-Duff modes are not blend modes at all; + * hb_pdf_composite() writes those that are not SRC_OVER. */ + case HB_PAINT_COMPOSITE_MODE_XOR: case HB_PAINT_COMPOSITE_MODE_CLEAR: case HB_PAINT_COMPOSITE_MODE_SRC: case HB_PAINT_COMPOSITE_MODE_DEST: @@ -1708,6 +1762,198 @@ paint->push_group (); } +/* A group written as a Form XObject. */ +struct hb_pdf_form_t +{ + unsigned idx = 0; /* It is painted with `/Fm<idx> Do`. */ + unsigned obj_id = 0; /* Zero if the group was empty and was not written. */ +}; + +static bool +hb_pdf_add_form (hb_pdf_resources_t *res, + const hb_vector_buf_t &group, + hb_pdf_form_t *form) +{ + if (!group.length) + return true; + if (unlikely (group.in_error () || + !res->add_form_group (group, &form->idx, &form->obj_id))) + { + /* Make render fail rather than give a document missing a group. */ + res->overflowed = true; + return false; + } + return true; +} + +enum hb_pdf_mask_t +{ + HB_PDF_MASK_NONE, + HB_PDF_MASK_IN, /* Where the other group is, by its alpha. */ + HB_PDF_MASK_OUT, /* Where the other group is not. */ +}; + +/* Paints @form into @body, with blend mode @bm if that is not null, + * and through @mask of @other. */ +static void +hb_pdf_paint_form (hb_pdf_resources_t *res, + hb_vector_buf_t &body, + const hb_pdf_form_t &form, + const char *bm = nullptr, + hb_pdf_mask_t mask = HB_PDF_MASK_NONE, + const hb_pdf_form_t &other = hb_pdf_form_t ()) +{ + if (!form.obj_id) + return; + if (mask != HB_PDF_MASK_NONE && !other.obj_id) + { + if (mask == HB_PDF_MASK_IN) + return; /* In nothing. */ + mask = HB_PDF_MASK_NONE; /* Out of nothing. */ + } + + body.append_str ("q\n"); + if (bm || mask != HB_PDF_MASK_NONE) + { + unsigned gs_idx = res->add_extgstate_group (bm, + mask != HB_PDF_MASK_NONE ? other.obj_id : 0, + mask == HB_PDF_MASK_OUT); + body.append_str ("/GS"); + body.append_unsigned (gs_idx); + body.append_str (" gs\n"); + } + body.append_str ("/Fm"); + body.append_unsigned (form.idx); + body.append_str (" Do\nQ\n"); +} + +/* Composites @group onto the current group in a Porter-Duff mode that + * no blend mode stands for. They all come to keeping, dropping, + * reordering or masking the two groups: + * + * CLEAR nothing + * SRC the source + * DEST_OVER the source, then the backdrop + * SRC_IN the source, masked by the backdrop's alpha + * DEST_IN the backdrop, masked by the source's alpha + * SRC_OUT the source, masked by the inverse of the backdrop's alpha + * DEST_OUT the backdrop, masked by the inverse of the source's alpha + * SRC_ATOP the backdrop, then SRC_IN + * DEST_ATOP the source, then DEST_IN + * XOR DEST_OUT, then SRC_OUT + * + * The last three are sums of two of the others, and PDF has no way to + * add: painted one over the other, the upper hides some of the lower + * where both are partly transparent. Written as above, SRC_ATOP is + * exact where the backdrop is opaque or absent, DEST_ATOP where the + * source is, and XOR, painting its upper part in Screen, where either + * is. */ +static void +hb_pdf_composite (hb_vector_paint_t *paint, + hb_pdf_resources_t *res, + const hb_vector_paint_group_t &group, + hb_paint_composite_mode_t mode) +{ + auto &dst = paint->current_group (); + + hb_pdf_form_t src; + if (mode != HB_PAINT_COMPOSITE_MODE_CLEAR && + unlikely (!hb_pdf_add_form (res, group, &src))) + return; + + /* The backdrop is all that the current group has painted, and the + * result takes its place. If the group has transforms or clips + * open, the source was painted under them: close them, to make the + * backdrop a unit, put the source under copies of them, and open + * them again after the result for what is painted next. */ + hb_vector_buf_t opening; + unsigned n_scopes = paint->close_scopes ("Q\n", &opening); + if (unlikely (dst.in_error ())) + return; + if (n_scopes && src.obj_id) + { + hb_vector_buf_t under; + under.append_len (opening.arrayZ, opening.length); + under.append_str ("/Fm"); + under.append_unsigned (src.idx); + under.append_str (" Do\n"); + for (unsigned i = 0; i < n_scopes; i++) + under.append_str ("Q\n"); + src = hb_pdf_form_t (); + if (unlikely (!hb_pdf_add_form (res, under, &src))) + return; + } + + hb_vector_paint_group_t backdrop_group = std::move (dst); + dst.referred = 0; + dst.blended = false; + hb_pdf_form_t backdrop; + if (mode != HB_PAINT_COMPOSITE_MODE_CLEAR && + mode != HB_PAINT_COMPOSITE_MODE_SRC && + unlikely (!hb_pdf_add_form (res, backdrop_group, &backdrop))) + return; + + uint64_t s = group.work (), d = backdrop_group.work (); + switch ((unsigned) mode) + { + case HB_PAINT_COMPOSITE_MODE_SRC: + hb_pdf_paint_form (res, dst, src); + dst.referred = s; + break; + + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + hb_pdf_paint_form (res, dst, src); + hb_pdf_paint_form (res, dst, backdrop); + dst.referred = s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + hb_pdf_paint_form (res, dst, src, nullptr, HB_PDF_MASK_IN, backdrop); + dst.referred = s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + hb_pdf_paint_form (res, dst, backdrop, nullptr, HB_PDF_MASK_IN, src); + dst.referred = s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + hb_pdf_paint_form (res, dst, src, nullptr, HB_PDF_MASK_OUT, backdrop); + dst.referred = s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + hb_pdf_paint_form (res, dst, backdrop, nullptr, HB_PDF_MASK_OUT, src); + dst.referred = s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + hb_pdf_paint_form (res, dst, backdrop); + hb_pdf_paint_form (res, dst, src, nullptr, HB_PDF_MASK_IN, backdrop); + dst.referred = s + 2 * d; + break; + + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + hb_pdf_paint_form (res, dst, src); + hb_pdf_paint_form (res, dst, backdrop, nullptr, HB_PDF_MASK_IN, src); + dst.referred = 2 * s + d; + break; + + case HB_PAINT_COMPOSITE_MODE_XOR: + hb_pdf_paint_form (res, dst, backdrop, nullptr, HB_PDF_MASK_OUT, src); + hb_pdf_paint_form (res, dst, src, "Screen", HB_PDF_MASK_OUT, backdrop); + dst.referred = 2 * (s + d); + dst.blended = true; + break; + + case HB_PAINT_COMPOSITE_MODE_CLEAR: + default: + break; + } + + paint->reopen_scopes (opening); +} + static void hb_pdf_paint_pop_group (hb_paint_funcs_t *, void *paint_data, @@ -1720,34 +1966,52 @@ if (paint->group_stack.length < 2) return; - hb_vector_buf_t group = paint->group_stack.pop (); - if (!group.length) - return; + /* Close what the group left open, so that it is a unit. */ + while (paint->scope_closed ()) + hb_pdf_close_scope (paint); + + hb_vector_paint_group_t group = paint->group_stack.pop (); + auto &body = paint->current_group (); + + if (mode == HB_PAINT_COMPOSITE_MODE_DEST) + return; /* The source is not used. */ + + /* What is not a mode is painted source-over. */ + if (unlikely ((unsigned) mode > HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY)) + mode = HB_PAINT_COMPOSITE_MODE_SRC_OVER; + + /* Three modes are written with a group used twice. When the + * document cannot take that, write the mode nearest to them that + * uses each once. */ + if ((mode == HB_PAINT_COMPOSITE_MODE_SRC_ATOP || + mode == HB_PAINT_COMPOSITE_MODE_DEST_ATOP || + mode == HB_PAINT_COMPOSITE_MODE_XOR) && + unlikely (!paint->affords (2 * group.work () + body.work ()))) + mode = mode == HB_PAINT_COMPOSITE_MODE_DEST_ATOP + ? HB_PAINT_COMPOSITE_MODE_DEST_OVER + : HB_PAINT_COMPOSITE_MODE_SRC_OVER; auto *res = hb_pdf_get_resources (paint); if (unlikely (!res)) return; - unsigned form_idx; - if (unlikely (group.in_error () || !res->add_form_group (group, &form_idx))) + + const char *bm = hb_pdf_blend_mode_name (mode); + if (!bm && mode != HB_PAINT_COMPOSITE_MODE_SRC_OVER) { - /* Make render fail rather than give a document missing a group. */ - res->overflowed = true; + hb_pdf_composite (paint, res, group, mode); return; } - auto &body = paint->current_body (); - body.append_str ("q\n"); - const char *bm = hb_pdf_blend_mode_name (mode); + /* Source-over, or a blend mode, which blends with all that the + * current group has painted, whatever is open in it. */ + hb_pdf_form_t form; + if (unlikely (!hb_pdf_add_form (res, group, &form))) + return; + hb_pdf_paint_form (res, body, form, bm); + if (form.obj_id) + body.referred += group.work (); if (bm) - { - unsigned gs_idx = res->add_extgstate_blend (bm); - body.append_str ("/GS"); - body.append_unsigned (gs_idx); - body.append_str (" gs\n"); - } - body.append_str ("/Fm"); - body.append_unsigned (form_idx); - body.append_str (" Do\nQ\n"); + body.blended = true; } @@ -1810,11 +2074,30 @@ !paint->group_stack.arrayZ[0].length) return nullptr; - hb_vector_buf_t &content = paint->group_stack.arrayZ[0]; hb_pdf_resources_t *res = hb_pdf_get_resources (paint); /* Over the document cap: the content stream refers to dropped objects. */ if (unlikely (res && res->in_error ())) return nullptr; + /* A content stream cut short would not restore what it saved. */ + if (unlikely (paint->group_stack.in_error () || + paint->group_stack.arrayZ[0].in_error ())) + return nullptr; + + /* What blends in the root group blends with what the root group has + * painted, and the background is not part of that: when there is + * blending, paint the root as a group of its own. */ + hb_vector_buf_t &root = paint->group_stack.arrayZ[0]; + hb_vector_buf_t isolated; + if (paint->group_stack.arrayZ[0].blended) + { + hb_pdf_form_t form; + if (unlikely (!res || !hb_pdf_add_form (res, root, &form))) + return nullptr; + hb_pdf_paint_form (res, isolated, form); + if (unlikely (isolated.in_error ())) + return nullptr; + } + hb_vector_buf_t &content = isolated.length ? isolated : root; float ex = paint->extents.x; float ey = paint->extents.y;
diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 45f03d0..0d042e9 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh
@@ -154,6 +154,19 @@ hb_array_t<hb_vector_paint_scope_t> open_scopes () { return scopes.as_array ().sub_array (current_group ().scope_base); } + /* Extends the innermost transform or clip that is open in the current + * group to the end of the group's buffer, for one whose opening text + * is written in parts. */ + void scope_extended () + { + if (!open_scopes ().length) + return; + auto &group = current_group (); + auto &scope = scopes.tail (); + if (likely (scope.start <= group.length)) + scope.length = group.length - scope.start; + } + /* Closes the transforms and clips that are open in the current group * with @closer, which leaves what the group has painted so far a unit. * The text that opened them is copied to @opening, for whatever was
diff --git a/src/hb-vector.cc b/src/hb-vector.cc index 8ce53f5..fdb0b35 100644 --- a/src/hb-vector.cc +++ b/src/hb-vector.cc
@@ -67,6 +67,18 @@ * %HB_PAINT_EXTEND_PAD. %HB_PAINT_EXTEND_REPEAT and * %HB_PAINT_EXTEND_REFLECT are not supported for these gradients. * + * The blend modes of #hb_paint_composite_mode_t are written as the + * blend modes that SVG and PDF have, and the Porter-Duff modes with + * masks. SVG output relies on CSS for both, so its viewer has to + * support `mix-blend-mode`, `isolation` and `mask-type`. Four modes + * add two layers together, which only the `plus-lighter` blend mode + * of SVG does: %HB_PAINT_COMPOSITE_MODE_SRC_ATOP, + * %HB_PAINT_COMPOSITE_MODE_DEST_ATOP, %HB_PAINT_COMPOSITE_MODE_XOR and + * %HB_PAINT_COMPOSITE_MODE_PLUS. In PDF output, and in an SVG viewer + * without `plus-lighter`, these four are approximations: the first + * three where partly transparent content overlaps, and + * %HB_PAINT_COMPOSITE_MODE_PLUS wherever content overlaps. + * * 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
diff --git a/test/vector/data/expected/basic/colrv1-composite-porter-duff.pdf b/test/vector/data/expected/basic/colrv1-composite-porter-duff.pdf new file mode 100644 index 0000000..96e39f0 --- /dev/null +++ b/test/vector/data/expected/basic/colrv1-composite-porter-duff.pdf
@@ -0,0 +1,1220 @@ +%PDF-1.4 +%ÀÁÂÃ +1 0 obj +<< /Type /Catalog /Pages 2 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R] /Count 1 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [-16 -16 13016 1016] +/Contents 4 0 R +/Resources << /ExtGState << /GS0 16 0 R /GS1 20 0 R /GS2 24 0 R /GS3 26 0 R /GS4 30 0 R /GS5 32 0 R /GS6 36 0 R /GS7 40 0 R /GS8 44 0 R /GS9 46 0 R /GS10 47 0 R /GS11 49 0 R /GS12 52 0 R >> /XObject << /Fm0 6 0 R /Fm1 7 0 R /Fm2 8 0 R /Fm3 9 0 R /Fm4 10 0 R /Fm5 11 0 R /Fm6 12 0 R /Fm7 13 0 R /Fm8 14 0 R /Fm9 15 0 R /Fm10 17 0 R /Fm11 18 0 R /Fm12 19 0 R /Fm13 21 0 R /Fm14 22 0 R /Fm15 23 0 R /Fm16 27 0 R /Fm17 28 0 R /Fm18 29 0 R /Fm19 33 0 R /Fm20 34 0 R /Fm21 35 0 R /Fm22 37 0 R /Fm23 38 0 R /Fm24 39 0 R /Fm25 41 0 R /Fm26 42 0 R /Fm27 43 0 R /Fm28 50 0 R /Fm29 51 0 R /Fm30 53 0 R >> >> >> +endobj +4 0 obj +<< /Length 3383 >> +stream +1 1 1 rg +-16 -16 13032 1032 re f +q +1 0 0 1 0 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +Q +Q +Q +q +1 0 0 1 1000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm1 Do +Q +Q +Q +Q +q +1 0 0 1 2000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm2 Do +Q +Q +Q +Q +q +1 0 0 1 3000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm4 Do +Q +Q +Q +Q +q +1 0 0 1 4000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm7 Do +Q +Q +Q +Q +q +1 0 0 1 5000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm10 Do +Q +Q +Q +Q +q +1 0 0 1 6000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm13 Do +Q +Q +Q +Q +q +1 0 0 1 7000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm16 Do +Q +Q +Q +Q +q +1 0 0 1 8000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm19 Do +Q +Q +Q +Q +q +1 0 0 1 9000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm22 Do +Q +Q +Q +Q +q +1 0 0 1 10000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm25 Do +Q +Q +Q +Q +q +1 0 0 1 11000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm28 Do +Q +Q +Q +Q +q +1 0 0 1 12000 0 cm +q +64 0 0 64 0 0 cm +q +0 0 15.62 15.62 re W n +q +0.015625 0 0 0.015625 0 0 cm +0 0 0 rg +475 525 m +475 750 l +525 750 l +525 525 l +750 525 l +750 475 l +525 475 l +525 250 l +475 250 l +475 475 l +250 475 l +250 525 l +475 525 l +h +f +Q +q +/Fm30 Do +Q +Q +Q +Q +endstream +endobj +5 0 obj + << /ExtGState << /GS0 16 0 R /GS1 20 0 R /GS2 24 0 R /GS3 26 0 R /GS4 30 0 R /GS5 32 0 R /GS6 36 0 R /GS7 40 0 R /GS8 44 0 R /GS9 46 0 R /GS10 47 0 R /GS11 49 0 R /GS12 52 0 R >> /XObject << /Fm0 6 0 R /Fm1 7 0 R /Fm2 8 0 R /Fm3 9 0 R /Fm4 10 0 R /Fm5 11 0 R /Fm6 12 0 R /Fm7 13 0 R /Fm8 14 0 R /Fm9 15 0 R /Fm10 17 0 R /Fm11 18 0 R /Fm12 19 0 R /Fm13 21 0 R /Fm14 22 0 R /Fm15 23 0 R /Fm16 27 0 R /Fm17 28 0 R /Fm18 29 0 R /Fm19 33 0 R /Fm20 34 0 R /Fm21 35 0 R /Fm22 37 0 R /Fm23 38 0 R /Fm24 39 0 R /Fm25 41 0 R /Fm26 42 0 R /Fm27 43 0 R /Fm28 50 0 R /Fm29 51 0 R /Fm30 53 0 R >> >> +endobj +6 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +7 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 12 >> +stream +q +/Fm0 Do +Q +endstream +endobj +8 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +9 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +10 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 138 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +q +/Fm3 Do +Q +endstream +endobj +11 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +12 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +13 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 24 >> +stream +q +/Fm5 Do +Q +q +/Fm6 Do +Q +endstream +endobj +14 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +15 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +16 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 15 0 R >> >> +endobj +17 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 20 >> +stream +q +/GS0 gs +/Fm8 Do +Q +endstream +endobj +18 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +19 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +20 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 18 0 R >> >> +endobj +21 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 21 >> +stream +q +/GS1 gs +/Fm12 Do +Q +endstream +endobj +22 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +23 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +24 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 23 0 R >> >> +endobj +25 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceGray /I true >> +/Resources 5 0 R +/Length 78 >> +stream +1 g +-32767 -32767 65534 65534 re f +/GS2 gs +0 g +-32767 -32767 65534 65534 re f +endstream +endobj +26 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Luminosity /G 25 0 R >> >> +endobj +27 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 21 >> +stream +q +/GS3 gs +/Fm14 Do +Q +endstream +endobj +28 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +29 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +30 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 28 0 R >> >> +endobj +31 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceGray /I true >> +/Resources 5 0 R +/Length 78 >> +stream +1 g +-32767 -32767 65534 65534 re f +/GS4 gs +0 g +-32767 -32767 65534 65534 re f +endstream +endobj +32 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Luminosity /G 31 0 R >> >> +endobj +33 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 21 >> +stream +q +/GS5 gs +/Fm18 Do +Q +endstream +endobj +34 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +35 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +36 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 35 0 R >> >> +endobj +37 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 34 >> +stream +q +/Fm21 Do +Q +q +/GS6 gs +/Fm20 Do +Q +endstream +endobj +38 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +39 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +40 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 38 0 R >> >> +endobj +41 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 34 >> +stream +q +/Fm23 Do +Q +q +/GS7 gs +/Fm24 Do +Q +endstream +endobj +42 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +43 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 126 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +44 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 42 0 R >> >> +endobj +45 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceGray /I true >> +/Resources 5 0 R +/Length 78 >> +stream +1 g +-32767 -32767 65534 65534 re f +/GS8 gs +0 g +-32767 -32767 65534 65534 re f +endstream +endobj +46 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Luminosity /G 45 0 R >> >> +endobj +47 0 obj +<< /Type /ExtGState /SMask << /Type /Mask /S /Alpha /G 43 0 R >> >> +endobj +48 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceGray /I true >> +/Resources 5 0 R +/Length 79 >> +stream +1 g +-32767 -32767 65534 65534 re f +/GS10 gs +0 g +-32767 -32767 65534 65534 re f +endstream +endobj +49 0 obj +<< /Type /ExtGState /BM /Screen /SMask << /Type /Mask /S /Luminosity /G 48 0 R >> >> +endobj +50 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 43 >> +stream +q +/GS9 gs +/Fm27 Do +Q +q +/GS11 gs +/Fm26 Do +Q +endstream +endobj +51 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 131 >> +stream +q +0.5 0 0 0.5 5.21 2.6 cm +q +0.015625 0 0 0.015625 0 0 cm +0.4078 0.7804 0.9098 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +endstream +endobj +52 0 obj +<< /Type /ExtGState /BM /Screen >> +endobj +53 0 obj +<< /Type /XObject /Subtype /Form +/BBox [-32767 -32767 32767 32767] +/Group << /S /Transparency /CS /DeviceRGB /I true >> +/Resources 5 0 R +/Length 148 >> +stream +q +0.5 0 0 0.5 2.6 5.21 cm +q +0.015625 0 0 0.015625 0 0 cm +1 0.8627 0.0039 rg +0 0 m +0 1000 l +1000 1000 l +1000 0 l +0 0 l +h +f +Q +Q +q +/GS12 gs +/Fm29 Do +Q +endstream +endobj +xref +0 54 +0000000000 65535 f +0000000015 00000 n +0000000064 00000 n +0000000121 00000 n +0000000812 00000 n +0000004246 00000 n +0000004848 00000 n +0000005163 00000 n +0000005358 00000 n +0000005668 00000 n +0000005983 00000 n +0000006306 00000 n +0000006622 00000 n +0000006933 00000 n +0000007141 00000 n +0000007457 00000 n +0000007768 00000 n +0000007852 00000 n +0000008056 00000 n +0000008372 00000 n +0000008683 00000 n +0000008767 00000 n +0000008972 00000 n +0000009288 00000 n +0000009599 00000 n +0000009683 00000 n +0000009946 00000 n +0000010035 00000 n +0000010240 00000 n +0000010556 00000 n +0000010867 00000 n +0000010951 00000 n +0000011214 00000 n +0000011303 00000 n +0000011508 00000 n +0000011824 00000 n +0000012135 00000 n +0000012219 00000 n +0000012437 00000 n +0000012753 00000 n +0000013064 00000 n +0000013148 00000 n +0000013366 00000 n +0000013682 00000 n +0000013993 00000 n +0000014077 00000 n +0000014340 00000 n +0000014429 00000 n +0000014513 00000 n +0000014777 00000 n +0000014878 00000 n +0000015105 00000 n +0000015421 00000 n +0000015472 00000 n +trailer +<< /Size 54 /Root 1 0 R >> +startxref +15805 +%%EOF
diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index c73a866..d1413b1 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests
@@ -19,3 +19,4 @@ ../../../api/fonts/Roboto-Regular.abc.ttf;--paint --show-extents;U+0061,U+0062,U+0063;../expected/basic/paint-show-extents.svg ../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A17;../expected/basic/colrv1-composite-multiply.svg ../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A00,U+F0A01,U+F0A02,U+F0A03,U+F0A04,U+F0A05,U+F0A06,U+F0A07,U+F0A08,U+F0A09,U+F0A0A,U+F0A0B,U+F0A0C;../expected/basic/colrv1-composite-porter-duff.svg +../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A00,U+F0A01,U+F0A02,U+F0A03,U+F0A04,U+F0A05,U+F0A06,U+F0A07,U+F0A08,U+F0A09,U+F0A0A,U+F0A0B,U+F0A0C;../expected/basic/colrv1-composite-porter-duff.pdf
diff --git a/test/vector/test-vector-composite.c b/test/vector/test-vector-composite.c index 4d76664..482e72c 100644 --- a/test/vector/test-vector-composite.c +++ b/test/vector/test-vector-composite.c
@@ -41,9 +41,13 @@ #define RED HB_COLOR (0, 0, 255, 255) #define BLUE HB_COLOR (255, 0, 0, 255) -/* How the two colors are written in SVG. */ +#define GREEN HB_COLOR (0, 255, 0, 255) + +/* How the two colors are written in SVG and in PDF. */ #define SVG_RED "fill=\"#F00\"" #define SVG_BLUE "fill=\"#00F\"" +#define PDF_RED "\n1 0 0 rg\n" +#define PDF_BLUE "\n0 0 1 rg\n" typedef void (*scene_func_t) (hb_paint_funcs_t *funcs, void *paint, int arg); @@ -145,6 +149,63 @@ } } +/* Asserts that @doc holds together as a PDF: every content stream + * restores as many graphics states as it saves, every resource that is + * used is in the resource dictionary, and every object referred to + * exists. */ +static void +check_pdf (const char *doc) +{ + unsigned n_objects = count (doc, " 0 obj\n"); + assert (n_objects >= 4); + const char *doc_end = doc + strlen (doc); + + for (const char *p = doc; (p = find (p, doc_end, " 0 R")); p += 4) + { + const char *q = p; + while (q > doc && q[-1] >= '0' && q[-1] <= '9') + q--; + unsigned id = (unsigned) atoi (q); + assert (id >= 1 && id <= n_objects); + } + + for (const char *p = doc; (p = find (p, doc_end, "\nstream\n")); ) + { + p += 8; + const char *end = find (p, doc_end, "endstream"); + assert (end); + int depth = 0; + while (p < end) + { + const char *eol = strchr (p, '\n'); + assert (eol && eol <= end); + size_t len = (size_t) (eol - p); + if (len == 1 && p[0] == 'q') + depth++; + else if (len == 1 && p[0] == 'Q') + { + depth--; + assert (depth >= 0); + } + else if ((len > 3 && !strncmp (eol - 3, " Do", 3)) || + (len > 3 && !strncmp (eol - 3, " gs", 3))) + { + /* "/Fm3 Do" or "/GS2 gs": the name must be defined. */ + char def[32]; + size_t n = len - 3; + assert (p[0] == '/' && n < sizeof (def) - 1); + memcpy (def, p, n); + def[n] = ' '; + def[n + 1] = '\0'; + const char *found = find (doc, doc_end, def); + assert (found && found[n + 1] >= '0' && found[n + 1] <= '9'); + } + p = eol + 1; + } + assert (!depth); + } +} + static void square (hb_paint_funcs_t *funcs, void *paint, float x, float y, hb_color_t color) @@ -432,6 +493,182 @@ free (doc); } +/* A PDF with nothing painted is not rendered, so paint something else + * first for the modes that leave nothing. */ +static void +composite_scene_pdf (hb_paint_funcs_t *funcs, void *paint, int mode) +{ + square (funcs, paint, 0, 0, GREEN); + composite_scene (funcs, paint, mode); +} + +static void +test_modes_pdf (void) +{ + for (int mode = HB_PAINT_COMPOSITE_MODE_CLEAR; mode <= HB_PAINT_COMPOSITE_MODE_PLUS; mode++) + { + char *doc = render (HB_VECTOR_FORMAT_PDF, composite_scene_pdf, mode); + check_pdf (doc); + + unsigned red = count (doc, PDF_RED), blue = count (doc, PDF_BLUE); + unsigned masks = count (doc, "/SMask"), inverted = count (doc, "/S /Luminosity"); + unsigned screen = count (doc, "/BM /Screen"); + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_CLEAR: + assert (!red && !blue && !masks && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_SRC: + assert (red == 1 && !blue && !masks && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_DEST: + assert (!red && blue == 1 && !masks && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_SRC_OVER: + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + assert (red == 1 && blue == 1 && !masks && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + /* One group through the alpha of the other. */ + assert (red == 1 && blue == 1 && masks == 1 && !inverted && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + /* Through the inverse of it, which takes a second mask. */ + assert (red == 1 && blue == 1 && masks == 2 && inverted == 1 && !screen); + break; + case HB_PAINT_COMPOSITE_MODE_XOR: + assert (red == 1 && blue == 1 && masks == 4 && inverted == 2 && screen == 1); + break; + case HB_PAINT_COMPOSITE_MODE_PLUS: + assert (red == 1 && blue == 1 && !masks && screen == 1); + break; + } + + /* Which group is painted, and which is only a mask, or painted + * first: the group that the outer one paints last. */ + const char *outer = NULL; + for (const char *p = doc; (p = strstr (p, "\nstream\n")); p++) + outer = p; + assert (outer); + unsigned painted = count (outer, " Do\n"); + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + case HB_PAINT_COMPOSITE_MODE_SRC: + assert (painted == 1); + break; + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + case HB_PAINT_COMPOSITE_MODE_XOR: + assert (painted == 2); + break; + default: + break; + } + + free (doc); + } +} + +static void +test_open_scopes_pdf (void) +{ + static const char transform[] = "\n0.5 0 0 0.5 7 9 cm\n"; + for (int mode = HB_PAINT_COMPOSITE_MODE_CLEAR; mode <= HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY; mode++) + { + char *doc = render (HB_VECTOR_FORMAT_PDF, open_scopes_scene, mode); + check_pdf (doc); + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_CLEAR: + assert (!count (doc, PDF_BLUE) && count (doc, PDF_RED) == 1); + assert (count (doc, transform) == 1); + break; + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + /* In the backdrop, around the source, and open again. */ + assert (count (doc, PDF_BLUE) == 2 && count (doc, PDF_RED) == 2); + assert (count (doc, transform) == 3); + break; + case HB_PAINT_COMPOSITE_MODE_SRC_OVER: + case HB_PAINT_COMPOSITE_MODE_MULTIPLY: + /* Nothing to rearrange: a blend mode blends with all that the + * group has painted, whatever is open. */ + assert (count (doc, transform) == 1); + break; + default: + break; + } + free (doc); + } +} + +static void +test_root_pdf (void) +{ + static const struct { int mode; hb_bool_t isolated; } tests[] = { + {HB_PAINT_COMPOSITE_MODE_SRC_OVER, 0}, + {HB_PAINT_COMPOSITE_MODE_SRC_IN, 0}, + {HB_PAINT_COMPOSITE_MODE_DEST_OUT, 0}, + {HB_PAINT_COMPOSITE_MODE_XOR, 1}, + {HB_PAINT_COMPOSITE_MODE_PLUS, 1}, + {HB_PAINT_COMPOSITE_MODE_MULTIPLY, 1}, + }; + for (unsigned i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) + { + char *doc = render (HB_VECTOR_FORMAT_PDF, root_scene, tests[i].mode); + check_pdf (doc); + /* The page's own content stream is the first. When something in + * the root blends, it holds nothing but the root as one group. */ + const char *page = strstr (doc, "\nstream\n"); + assert (page); + page += 8; + assert (!strncmp (page, "q\n/Fm", 5) == !!tests[i].isolated); + if (tests[i].isolated) + { + const char *end = strstr (page, "endstream"); + assert (end && count (page, " Do\n") >= 1 && strstr (page, " Do\nQ\n") + 6 == end); + } + free (doc); + } +} + +static void +test_unbalanced_pdf (void) +{ + for (int mode = HB_PAINT_COMPOSITE_MODE_CLEAR; mode <= HB_PAINT_COMPOSITE_MODE_PLUS; mode++) + { + char *doc = render (HB_VECTOR_FORMAT_PDF, unbalanced_scene, mode); + check_pdf (doc); + free (doc); + } +} + +static void +test_nested_pdf (void) +{ + /* Each level paints what is under it, and the source through its + * alpha. */ + char *doc = render (HB_VECTOR_FORMAT_PDF, nested_scene, 1); + check_pdf (doc); + assert (count (doc, "/SMask") == NESTING); + free (doc); + + doc = render (HB_VECTOR_FORMAT_PDF, nested_scene, 12000); + check_pdf (doc); + unsigned masks = count (doc, "/SMask"); + assert (masks && masks < NESTING); + assert (count (doc, PDF_RED) == 12000 * NESTING); + free (doc); +} + /* A context gives the same document again after it has rendered one. */ static void test_reuse (hb_vector_format_t format) @@ -466,5 +703,12 @@ test_unbalanced_svg (); test_nested_svg (); test_reuse (HB_VECTOR_FORMAT_SVG); + + test_modes_pdf (); + test_open_scopes_pdf (); + test_root_pdf (); + test_unbalanced_pdf (); + test_nested_pdf (); + test_reuse (HB_VECTOR_FORMAT_PDF); return 0; }