1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use pdf_writer::types::ColorSpaceOperand;
use pdf_writer::types::ColorSpaceOperand::Pattern;
use pdf_writer::{Chunk, Content, Finish};
use usvg::tiny_skia_path::PathSegment;
use usvg::{Fill, FillRule, Node, Opacity, Paint, PaintOrder};
use usvg::{Path, Visibility};
use usvg::{Stroke, Transform};

use super::{gradient, pattern};
use crate::util::context::Context;
use crate::util::helper::{
    plain_bbox, plain_bbox_without_default, ColorExt, LineCapExt, LineJoinExt, NameExt,
    TransformExt, SRGB,
};

/// Render a path into a content stream.
pub fn render(
    node: &Node,
    path: &Path,
    chunk: &mut Chunk,
    content: &mut Content,
    ctx: &mut Context,
    accumulated_transform: Transform,
) {
    // Check if the path has a bbox at all.
    let Some(_) = plain_bbox_without_default(node, true) else {
        return;
    };

    if path.visibility != Visibility::Visible {
        return;
    }

    // In order to support different stroke and fill orders as well as "advanced" paths
    // such as pattern fills/strokes with opacities and linear gradient strokes/fills with
    // stop opacities, we always render strokes and fills separately, at the cost of slightly
    // higher file sizes depending on the SVG.
    match path.paint_order {
        PaintOrder::FillAndStroke => {
            fill(path, node, chunk, content, ctx, accumulated_transform);
            stroke(path, node, chunk, content, ctx, accumulated_transform);
        }
        PaintOrder::StrokeAndFill => {
            stroke(path, node, chunk, content, ctx, accumulated_transform);
            fill(path, node, chunk, content, ctx, accumulated_transform);
        }
    }
}

/// Draws a path into a content stream. Note that this does not perform any stroking/filling,
/// it only creates a subpath.
pub fn draw_path(path_data: impl Iterator<Item = PathSegment>, content: &mut Content) {
    // Taken from resvg
    fn calc(n1: f32, n2: f32) -> f32 {
        (n1 + n2 * 2.0) / 3.0
    }

    let mut p_prev = None;

    for operation in path_data {
        match operation {
            PathSegment::MoveTo(p) => {
                content.move_to(p.x, p.y);
                p_prev = Some(p);
            }
            PathSegment::LineTo(p) => {
                content.line_to(p.x, p.y);
                p_prev = Some(p);
            }
            PathSegment::QuadTo(p1, p2) => {
                // Since PDF doesn't support quad curves, we ned to convert them into
                // cubic.
                let prev = p_prev.unwrap();
                content.cubic_to(
                    calc(prev.x, p1.x),
                    calc(prev.y, p1.y),
                    calc(p2.x, p1.x),
                    calc(p2.y, p1.y),
                    p2.x,
                    p2.y,
                );
                p_prev = Some(p2);
            }
            PathSegment::CubicTo(p1, p2, p3) => {
                content.cubic_to(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
                p_prev = Some(p3);
            }
            PathSegment::Close => {
                content.close_path();
            }
        };
    }
}

fn stroke(
    path: &Path,
    node: &Node,
    chunk: &mut Chunk,
    content: &mut Content,
    ctx: &mut Context,
    accumulated_transform: Transform,
) {
    if let Some(stroke) = path.stroke.as_ref() {
        let paint = &stroke.paint;
        let path_bbox_with_stroke = plain_bbox(node, true);
        let path_bbox_without_stroke = plain_bbox(node, false);
        let accumulated_transform = accumulated_transform.pre_concat(path.transform);

        content.save_state();
        content.transform(path.transform.to_pdf_transform());

        match paint {
            Paint::Color(c) => {
                set_opacity_gs(chunk, content, ctx, Some(stroke.opacity), None);
                content.set_stroke_color_space(ColorSpaceOperand::Named(SRGB));
                content.set_stroke_color(c.to_pdf_color());
            }
            Paint::Pattern(p) => {
                // Instead of setting the opacity via an external graphics state, we to it
                // by passing the opacity on to the pattern. The reason is that, for example
                // if we use a pattern as a stroke and set a stroke-opacity of 0.5, when rendering
                // the pattern, the opacity would only apply to strokes in that pattern, instead of
                // the whole pattern itself. This is why we need to handle this case differently.
                let pattern_name = pattern::create(
                    p.clone(),
                    &path_bbox_without_stroke,
                    chunk,
                    ctx,
                    accumulated_transform,
                    Some(stroke.opacity),
                );
                content.set_stroke_color_space(Pattern);
                content.set_stroke_pattern(None, pattern_name.to_pdf_name());
            }
            Paint::LinearGradient(_) | Paint::RadialGradient(_) => {
                // In XPDF, the opacity will only be applied to the gradient if we also set the
                // fill opacity. Unfortunately, in muPDF it still doesn't work.
                set_opacity_gs(
                    chunk,
                    content,
                    ctx,
                    Some(stroke.opacity),
                    Some(stroke.opacity),
                );

                if let Some(soft_mask) = gradient::create_shading_soft_mask(
                    paint,
                    &path_bbox_with_stroke,
                    chunk,
                    ctx,
                ) {
                    content.set_parameters(soft_mask.to_pdf_name());
                }

                let pattern_name = gradient::create_shading_pattern(
                    paint,
                    &path_bbox_without_stroke,
                    chunk,
                    ctx,
                    &accumulated_transform,
                );
                content.set_stroke_color_space(Pattern);
                content.set_stroke_pattern(None, pattern_name.to_pdf_name());
            }
        }

        content.set_line_width(stroke.width.get());
        content.set_miter_limit(stroke.miterlimit.get());
        content.set_line_cap(stroke.linecap.to_pdf_line_cap());
        content.set_line_join(stroke.linejoin.to_pdf_line_join());

        if let Some(dasharray) = &stroke.dasharray {
            content.set_dash_pattern(dasharray.iter().cloned(), stroke.dashoffset);
        }

        draw_path(path.data.segments(), content);
        finish_path(Some(stroke), None, content);
        content.restore_state();
    }
}

fn fill(
    path: &Path,
    node: &Node,
    chunk: &mut Chunk,
    content: &mut Content,
    ctx: &mut Context,
    accumulated_transform: Transform,
) {
    if let Some(fill) = path.fill.as_ref() {
        let paint = &fill.paint;
        let path_bbox = plain_bbox(node, false);
        let accumulated_transform = accumulated_transform.pre_concat(path.transform);

        content.save_state();
        content.transform(path.transform.to_pdf_transform());

        match paint {
            Paint::Color(c) => {
                set_opacity_gs(chunk, content, ctx, None, Some(fill.opacity));
                content.set_fill_color_space(ColorSpaceOperand::Named(SRGB));
                content.set_fill_color(c.to_pdf_color());
            }
            Paint::Pattern(p) => {
                // See note in the `stroke` function.
                let pattern_name = pattern::create(
                    p.clone(),
                    &path_bbox,
                    chunk,
                    ctx,
                    accumulated_transform,
                    Some(fill.opacity),
                );
                content.set_fill_color_space(Pattern);
                content.set_fill_pattern(None, pattern_name.to_pdf_name());
            }
            Paint::LinearGradient(_) | Paint::RadialGradient(_) => {
                set_opacity_gs(chunk, content, ctx, None, Some(fill.opacity));

                if let Some(soft_mask) =
                    gradient::create_shading_soft_mask(paint, &path_bbox, chunk, ctx)
                {
                    content.set_parameters(soft_mask.to_pdf_name());
                };

                let pattern_name = gradient::create_shading_pattern(
                    paint,
                    &path_bbox,
                    chunk,
                    ctx,
                    &accumulated_transform,
                );
                content.set_fill_color_space(Pattern);
                content.set_fill_pattern(None, pattern_name.to_pdf_name());
            }
        }

        draw_path(path.data.segments(), content);
        finish_path(None, Some(fill), content);
        content.restore_state();
    }
}

fn finish_path(stroke: Option<&Stroke>, fill: Option<&Fill>, content: &mut Content) {
    match (stroke, fill.map(|f| f.rule)) {
        (Some(_), Some(FillRule::NonZero)) => content.fill_nonzero_and_stroke(),
        (Some(_), Some(FillRule::EvenOdd)) => content.fill_even_odd_and_stroke(),
        (None, Some(FillRule::NonZero)) => content.fill_nonzero(),
        (None, Some(FillRule::EvenOdd)) => content.fill_even_odd(),
        (Some(_), None) => content.stroke(),
        (None, None) => content.end_path(),
    };
}

fn set_opacity_gs(
    chunk: &mut Chunk,
    content: &mut Content,
    ctx: &mut Context,
    stroke_opacity: Option<Opacity>,
    fill_opacity: Option<Opacity>,
) {
    let fill_opacity = fill_opacity.unwrap_or(Opacity::ONE).get();
    let stroke_opacity = stroke_opacity.unwrap_or(Opacity::ONE).get();

    if fill_opacity == 1.0 && stroke_opacity == 1.0 {
        return;
    }

    let gs_ref = ctx.alloc_ref();
    let mut gs = chunk.ext_graphics(gs_ref);
    gs.non_stroking_alpha(fill_opacity)
        .stroking_alpha(stroke_opacity)
        .finish();
    content.set_parameters(ctx.deferrer.add_graphics_state(gs_ref).to_pdf_name());
}