diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index 6bb6ec210..8b1250222 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -123,12 +123,66 @@ struct hb_pdf_resources_t return idx; } - /* Add a shading + function for a gradient, return resource name index. */ + /* Add ExtGState with an SMask (soft mask) referencing a Form XObject + * that paints a DeviceGray shading. Returns the ExtGState resource index. */ + unsigned add_extgstate_smask (unsigned alpha_shading_id, + float bbox_x, float bbox_y, + float bbox_w, float bbox_h, + unsigned precision) + { + /* Form XObject: transparency group painting the alpha shading. */ + hb_vector_t form_stream; + hb_buf_append_str (&form_stream, "/SHa sh\n"); + + hb_vector_t form; + hb_buf_append_str (&form, "<< /Type /XObject /Subtype /Form\n"); + hb_buf_append_str (&form, "/BBox ["); + hb_buf_append_num (&form, bbox_x, precision); + hb_buf_append_c (&form, ' '); + hb_buf_append_num (&form, bbox_y, precision); + hb_buf_append_c (&form, ' '); + hb_buf_append_num (&form, bbox_x + bbox_w, precision); + hb_buf_append_c (&form, ' '); + hb_buf_append_num (&form, bbox_y + bbox_h, precision); + hb_buf_append_str (&form, "]\n/Group << /S /Transparency /CS /DeviceGray >>\n"); + hb_buf_append_str (&form, "/Resources << /Shading << /SHa "); + hb_buf_append_unsigned (&form, alpha_shading_id); + hb_buf_append_str (&form, " 0 R >> >>\n"); + hb_buf_append_str (&form, "/Length "); + hb_buf_append_unsigned (&form, form_stream.length); + hb_buf_append_str (&form, " >>\nstream\n"); + hb_buf_append_len (&form, form_stream.arrayZ, form_stream.length); + hb_buf_append_str (&form, "endstream"); + unsigned form_id = add_object (std::move (form)); + + /* ExtGState with luminosity soft mask. */ + unsigned idx = extgstate_count++; + hb_vector_t gs; + hb_buf_append_str (&gs, "<< /Type /ExtGState\n"); + hb_buf_append_str (&gs, "/SMask << /Type /Mask /S /Luminosity /G "); + hb_buf_append_unsigned (&gs, form_id); + hb_buf_append_str (&gs, " 0 R >> >>"); + unsigned gs_id = add_object (std::move (gs)); + + hb_buf_append_str (&extgstate_dict, "/GS"); + hb_buf_append_unsigned (&extgstate_dict, idx); + hb_buf_append_c (&extgstate_dict, ' '); + hb_buf_append_unsigned (&extgstate_dict, gs_id); + hb_buf_append_str (&extgstate_dict, " 0 R "); + return idx; + } + + /* Add a shading, return resource name index. */ unsigned add_shading (hb_vector_t &&shading_data) { - unsigned idx = shading_count++; unsigned obj_id = add_object (std::move (shading_data)); + return add_shading_by_id (obj_id); + } + /* Register an already-allocated object as a shading resource. */ + unsigned add_shading_by_id (unsigned obj_id) + { + unsigned idx = shading_count++; hb_buf_append_str (&shading_dict, "/SH"); hb_buf_append_unsigned (&shading_dict, idx); hb_buf_append_c (&shading_dict, ' '); @@ -740,6 +794,89 @@ hb_pdf_paint_image (hb_paint_funcs_t *, /* ---- Gradient helpers ---- */ +static bool +hb_pdf_gradient_needs_alpha (const hb_color_stop_t *stops, unsigned count) +{ + for (unsigned i = 0; i < count; i++) + if (hb_color_get_alpha (stops[i].color) != 255) + return true; + return false; +} + +/* Build a PDF Type 2 (exponential interpolation) function for + * a single alpha stop pair (DeviceGray, scalar output). */ +static void +hb_pdf_build_alpha_interpolation_function (hb_vector_t *obj, + float a0, float a1) +{ + hb_buf_append_str (obj, "<< /FunctionType 2 /Domain [0 1] /N 1\n"); + hb_buf_append_str (obj, "/C0 ["); + hb_buf_append_num (obj, a0, 4); + hb_buf_append_str (obj, "]\n/C1 ["); + hb_buf_append_num (obj, a1, 4); + hb_buf_append_str (obj, "] >>"); +} + +/* Build a stitching function (Type 3) for the alpha channel of + * pre-populated paint->color_stops_scratch (already sorted+normalized). */ +static unsigned +hb_pdf_build_alpha_gradient_function_from_stops (hb_pdf_resources_t *res, + hb_vector_paint_t *paint) +{ + unsigned count = paint->color_stops_scratch.length; + + if (count < 2) + { + float a = count ? hb_color_get_alpha (paint->color_stops_scratch.arrayZ[0].color) / 255.f : 1.f; + hb_vector_t obj; + hb_pdf_build_alpha_interpolation_function (&obj, a, a); + return res->add_object (std::move (obj)); + } + + if (count == 2) + { + hb_vector_t obj; + hb_pdf_build_alpha_interpolation_function (&obj, + hb_color_get_alpha (paint->color_stops_scratch.arrayZ[0].color) / 255.f, + hb_color_get_alpha (paint->color_stops_scratch.arrayZ[1].color) / 255.f); + return res->add_object (std::move (obj)); + } + + hb_vector_t sub_func_ids; + for (unsigned i = 0; i + 1 < count; i++) + { + hb_vector_t sub; + hb_pdf_build_alpha_interpolation_function (&sub, + hb_color_get_alpha (paint->color_stops_scratch.arrayZ[i].color) / 255.f, + hb_color_get_alpha (paint->color_stops_scratch.arrayZ[i + 1].color) / 255.f); + sub_func_ids.push (res->add_object (std::move (sub))); + } + + hb_vector_t obj; + hb_buf_append_str (&obj, "<< /FunctionType 3 /Domain [0 1]\n"); + hb_buf_append_str (&obj, "/Functions ["); + for (unsigned i = 0; i < sub_func_ids.length; i++) + { + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_unsigned (&obj, sub_func_ids.arrayZ[i]); + hb_buf_append_str (&obj, " 0 R"); + } + hb_buf_append_str (&obj, "]\n/Bounds ["); + for (unsigned i = 1; i + 1 < count; i++) + { + if (i > 1) hb_buf_append_c (&obj, ' '); + hb_buf_append_num (&obj, paint->color_stops_scratch.arrayZ[i].offset, 4); + } + hb_buf_append_str (&obj, "]\n/Encode ["); + for (unsigned i = 0; i + 1 < count; i++) + { + if (i) hb_buf_append_c (&obj, ' '); + hb_buf_append_str (&obj, "0 1"); + } + hb_buf_append_str (&obj, "] >>"); + return res->add_object (std::move (obj)); +} + /* Build a PDF Type 2 (exponential interpolation) function for * a single color stop pair. */ static void @@ -874,7 +1011,11 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, unsigned func_id = hb_pdf_build_gradient_function_from_stops (res, paint); - /* Build Type 2 (axial) shading. */ + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + const char *extend_str = (extend == HB_PAINT_EXTEND_PAD) + ? "/Extend [true true]\n" : ""; + + /* Build Type 2 (axial) shading — color only. */ hb_vector_t sh; hb_buf_append_str (&sh, "<< /ShadingType 2 /ColorSpace /DeviceRGB\n"); hb_buf_append_str (&sh, "/Coords ["); @@ -888,16 +1029,44 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *, hb_buf_append_str (&sh, "]\n/Function "); hb_buf_append_unsigned (&sh, func_id); hb_buf_append_str (&sh, " 0 R\n"); - - hb_paint_extend_t extend = hb_color_line_get_extend (color_line); - if (extend == HB_PAINT_EXTEND_PAD) - hb_buf_append_str (&sh, "/Extend [true true]\n"); - + hb_buf_append_str (&sh, extend_str); hb_buf_append_str (&sh, ">>"); unsigned sh_idx = res->add_shading (std::move (sh)); auto &body = paint->current_body (); + + bool needs_alpha = hb_pdf_gradient_needs_alpha (stops.arrayZ, stops.length); + if (needs_alpha) + { + unsigned alpha_func_id = hb_pdf_build_alpha_gradient_function_from_stops (res, paint); + + hb_vector_t ash; + hb_buf_append_str (&ash, "<< /ShadingType 2 /ColorSpace /DeviceGray\n"); + hb_buf_append_str (&ash, "/Coords ["); + hb_buf_append_num (&ash, gx0, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gy0, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gx1, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gy1, paint->precision); + hb_buf_append_str (&ash, "]\n/Function "); + hb_buf_append_unsigned (&ash, alpha_func_id); + hb_buf_append_str (&ash, " 0 R\n"); + hb_buf_append_str (&ash, extend_str); + hb_buf_append_str (&ash, ">>"); + unsigned alpha_sh_id = res->add_object (std::move (ash)); + + unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, + gx0, gy0, + gx1 - gx0, gy1 - gy0, + paint->precision); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + hb_buf_append_str (&body, "/SH"); hb_buf_append_unsigned (&body, sh_idx); hb_buf_append_str (&body, " sh\n"); @@ -937,7 +1106,11 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, unsigned func_id = hb_pdf_build_gradient_function_from_stops (res, paint); - /* Build Type 3 (radial) shading. */ + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + const char *extend_str = (extend == HB_PAINT_EXTEND_PAD) + ? "/Extend [true true]\n" : ""; + + /* Build Type 3 (radial) shading — color only. */ hb_vector_t sh; hb_buf_append_str (&sh, "<< /ShadingType 3 /ColorSpace /DeviceRGB\n"); hb_buf_append_str (&sh, "/Coords ["); @@ -955,16 +1128,52 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *, hb_buf_append_str (&sh, "]\n/Function "); hb_buf_append_unsigned (&sh, func_id); hb_buf_append_str (&sh, " 0 R\n"); - - hb_paint_extend_t extend = hb_color_line_get_extend (color_line); - if (extend == HB_PAINT_EXTEND_PAD) - hb_buf_append_str (&sh, "/Extend [true true]\n"); - + hb_buf_append_str (&sh, extend_str); hb_buf_append_str (&sh, ">>"); unsigned sh_idx = res->add_shading (std::move (sh)); auto &body = paint->current_body (); + + bool needs_alpha = hb_pdf_gradient_needs_alpha (stops.arrayZ, stops.length); + if (needs_alpha) + { + unsigned alpha_func_id = hb_pdf_build_alpha_gradient_function_from_stops (res, paint); + + hb_vector_t ash; + hb_buf_append_str (&ash, "<< /ShadingType 3 /ColorSpace /DeviceGray\n"); + hb_buf_append_str (&ash, "/Coords ["); + hb_buf_append_num (&ash, gx0, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gy0, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gr0, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gx1, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gy1, paint->precision); + hb_buf_append_c (&ash, ' '); + hb_buf_append_num (&ash, gr1, paint->precision); + hb_buf_append_str (&ash, "]\n/Function "); + hb_buf_append_unsigned (&ash, alpha_func_id); + hb_buf_append_str (&ash, " 0 R\n"); + hb_buf_append_str (&ash, extend_str); + hb_buf_append_str (&ash, ">>"); + unsigned alpha_sh_id = res->add_object (std::move (ash)); + + /* BBox: enclosing square of the outer circle. */ + float cx = (gr1 >= gr0) ? gx1 : gx0; + float cy = (gr1 >= gr0) ? gy1 : gy0; + float rr = hb_max (gr0, gr1); + unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, + cx - rr, cy - rr, + 2 * rr, 2 * rr, + paint->precision); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + hb_buf_append_str (&body, "/SH"); hb_buf_append_unsigned (&body, sh_idx); hb_buf_append_str (&body, " sh\n"); @@ -999,6 +1208,14 @@ hb_pdf_encode_color_rgb (hb_vector_t *buf, hb_color_t c) hb_buf_append_len (buf, rgb, 3); } +/* Encode alpha from hb_color_t as 1 byte (gray). */ +static void +hb_pdf_encode_color_alpha (hb_vector_t *buf, hb_color_t c) +{ + char a = (char) hb_color_get_alpha (c); + hb_buf_append_len (buf, &a, 1); +} + /* Encode one Coons patch control point. */ static void hb_pdf_encode_point (hb_vector_t *buf, @@ -1010,10 +1227,13 @@ hb_pdf_encode_point (hb_vector_t *buf, hb_pdf_encode_coord (buf, y, ylo, yhi); } -/* Emit one Coons patch sector into the mesh stream. - * Splits large arcs into sub-patches of max 90°. */ +/* Emit one Coons patch sector into the mesh stream(s). + * Splits large arcs into sub-patches of max 90°. + * If alpha_mesh is non-null, emits a parallel DeviceGray + * patch with the alpha channel. */ static void hb_pdf_add_sweep_patch (hb_vector_t *mesh, + hb_vector_t *alpha_mesh, float cx, float cy, float xlo, float xhi, float ylo, float yhi, float a0, hb_color_t c0_in, @@ -1089,12 +1309,36 @@ hb_pdf_add_sweep_patch (hb_vector_t *mesh, hb_pdf_encode_color_rgb (mesh, sc0); /* outer start */ hb_pdf_encode_color_rgb (mesh, sc1); /* outer end */ hb_pdf_encode_color_rgb (mesh, sc1); /* inner end */ + + if (alpha_mesh) + { + hb_buf_append_c (alpha_mesh, '\0'); + + hb_pdf_encode_point (alpha_mesh, p0x, p0y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e1_1x, e1_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e1_2x, e1_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, p3x, p3y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e2_1x, e2_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e2_2x, e2_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, p6x, p6y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e3_1x, e3_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e3_2x, e3_2y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, p9x, p9y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e4_1x, e4_1y, xlo, xhi, ylo, yhi); + hb_pdf_encode_point (alpha_mesh, e4_2x, e4_2y, xlo, xhi, ylo, yhi); + + hb_pdf_encode_color_alpha (alpha_mesh, sc0); + hb_pdf_encode_color_alpha (alpha_mesh, sc0); + hb_pdf_encode_color_alpha (alpha_mesh, sc1); + hb_pdf_encode_color_alpha (alpha_mesh, sc1); + } } } /* Callback context + trampoline for hb_paint_sweep_gradient_tiles. */ struct hb_pdf_sweep_ctx_t { hb_vector_t *mesh; + hb_vector_t *alpha_mesh; float cx, cy, xlo, xhi, ylo, yhi; }; @@ -1104,7 +1348,8 @@ hb_pdf_sweep_emit_patch (float a0, hb_color_t c0, void *user_data) { auto *ctx = (hb_pdf_sweep_ctx_t *) user_data; - hb_pdf_add_sweep_patch (ctx->mesh, ctx->cx, ctx->cy, + hb_pdf_add_sweep_patch (ctx->mesh, ctx->alpha_mesh, + ctx->cx, ctx->cy, ctx->xlo, ctx->xhi, ctx->ylo, ctx->yhi, a0, c0, a1, c1); } @@ -1144,10 +1389,16 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, float xlo = cx - R - 1, xhi = cx + R + 1; float ylo = cy - R - 1, yhi = cy + R + 1; - hb_vector_t mesh; - mesh.alloc (256); + bool needs_alpha = hb_pdf_gradient_needs_alpha (stops, n_stops); - hb_pdf_sweep_ctx_t ctx { &mesh, cx, cy, xlo, xhi, ylo, yhi }; + hb_vector_t mesh; + hb_vector_t alpha_mesh; + mesh.alloc (256); + if (needs_alpha) + alpha_mesh.alloc (256); + + hb_pdf_sweep_ctx_t ctx { &mesh, needs_alpha ? &alpha_mesh : nullptr, + cx, cy, xlo, xhi, ylo, yhi }; hb_paint_sweep_gradient_tiles (stops, n_stops, extend, start_angle, end_angle, hb_pdf_sweep_emit_patch, &ctx); @@ -1155,28 +1406,50 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, if (!mesh.length) return; - /* Build the shading stream object. */ - hb_vector_t sh; - hb_buf_append_str (&sh, "<< /ShadingType 6 /ColorSpace /DeviceRGB\n"); - hb_buf_append_str (&sh, "/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); - hb_buf_append_str (&sh, "/Decode ["); - hb_buf_append_num (&sh, xlo, 2); - hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, xhi, 2); - hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, ylo, 2); - hb_buf_append_c (&sh, ' '); - hb_buf_append_num (&sh, yhi, 2); - hb_buf_append_str (&sh, " 0 1 0 1 0 1]\n"); - hb_buf_append_str (&sh, "/Length "); - hb_buf_append_unsigned (&sh, mesh.length); - hb_buf_append_str (&sh, " >>\nstream\n"); - hb_buf_append_len (&sh, mesh.arrayZ, mesh.length); - hb_buf_append_str (&sh, "\nendstream"); + auto hb_pdf_build_mesh_shading = [&] (hb_vector_t &m, + const char *cs, + const char *decode_suffix) -> unsigned + { + hb_vector_t sh; + hb_buf_append_str (&sh, "<< /ShadingType 6 /ColorSpace /"); + hb_buf_append_str (&sh, cs); + hb_buf_append_str (&sh, "\n/BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8\n"); + hb_buf_append_str (&sh, "/Decode ["); + hb_buf_append_num (&sh, xlo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, xhi, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, ylo, 2); + hb_buf_append_c (&sh, ' '); + hb_buf_append_num (&sh, yhi, 2); + hb_buf_append_str (&sh, decode_suffix); + hb_buf_append_str (&sh, "]\n/Length "); + hb_buf_append_unsigned (&sh, m.length); + hb_buf_append_str (&sh, " >>\nstream\n"); + hb_buf_append_len (&sh, m.arrayZ, m.length); + hb_buf_append_str (&sh, "\nendstream"); + return res->add_object (std::move (sh)); + }; - unsigned sh_idx = res->add_shading (std::move (sh)); + unsigned sh_obj_id = hb_pdf_build_mesh_shading (mesh, "DeviceRGB", + " 0 1 0 1 0 1"); + unsigned sh_idx = res->add_shading_by_id (sh_obj_id); auto &body = paint->current_body (); + + if (needs_alpha && alpha_mesh.length) + { + unsigned alpha_sh_id = hb_pdf_build_mesh_shading (alpha_mesh, "DeviceGray", + " 0 1"); + unsigned gs_idx = res->add_extgstate_smask (alpha_sh_id, + xlo, ylo, + xhi - xlo, yhi - ylo, + paint->precision); + hb_buf_append_str (&body, "/GS"); + hb_buf_append_unsigned (&body, gs_idx); + hb_buf_append_str (&body, " gs\n"); + } + hb_buf_append_str (&body, "/SH"); hb_buf_append_unsigned (&body, sh_idx); hb_buf_append_str (&body, " sh\n");