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
//! Abstraction class for images. Please use this class
//! instead of adding `ImageXObjects` yourself

use crate::{PdfLayerReference, Pt, Px, XObject, XObjectRef};
use lopdf::{Object, Stream};
use std::{error, fmt};
use usvg::TreeParsing;

/// SVG - wrapper around an `XObject` to allow for more
/// control within the library.
/// 
/// When placing multiple copies of the same SVG on the 
/// same layer, it is better to use the `into_xobject` 
/// method to get a reference, rather than a clone
#[derive(Debug, Clone)]
pub struct Svg {
    /// The PDF document, converted from SVG using svg2pdf
    svg_xobject: Stream,
    /// Width of the rendered SVG content
    pub width: Px,
    /// Height of the rendered SVG content
    pub height: Px,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SvgParseError {
    Svg2PdfConversionError(String),
    PdfParsingError,
    NoContentStream,
    // PDF returned by pdf2svg is not in the expected form
    InternalError,
}

impl fmt::Display for SvgParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Svg2PdfConversionError(inner) => write!(f, "svg2pdf conversion error: {inner}"),
            Self::PdfParsingError => write!(f, "error parsing svg2pdf pdf data"),
            Self::NoContentStream => write!(f, "svg2pdf returned no content stream"),
            Self::InternalError => write!(f, "pdf returned by pdf2svg in unexpected form"),
        }
    }
}

impl error::Error for SvgParseError {}

/// Transform that is applied immediately before the
/// image gets painted. Does not affect anything other
/// than the image.
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct SvgTransform {
    pub translate_x: Option<Pt>,
    pub translate_y: Option<Pt>,
    /// Rotate (clockwise), in degree angles
    pub rotate: Option<SvgRotation>,
    pub scale_x: Option<f32>,
    pub scale_y: Option<f32>,
    /// If set to None, will be set to 300.0 for images
    pub dpi: Option<f32>,
}

#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct SvgRotation {
    pub angle_ccw_degrees: f32,
    pub rotation_center_x: Pt,
    pub rotation_center_y: Pt,
}

fn export_svg_to_xobject_pdf(svg: &str) -> Result<Stream, String> {
    use pdf_writer::{Content, Finish, Name, Rect, Ref, Pdf};

    // Allocate the indirect reference IDs and names.
    let catalog_id = Ref::new(1);
    let page_tree_id = Ref::new(2);
    let page_id = Ref::new(3);
    let content_id = Ref::new(4);
    let svg_id = Ref::new(5);
    let svg_name = Name(b"S1");

    // Start writing a PDF.
    let mut writer = Pdf::new();
    writer.catalog(catalog_id).pages(page_tree_id);
    writer.pages(page_tree_id).kids([page_id]).count(1);

    // Set up a simple A4 page.
    let mut page = writer.page(page_id);
    page.media_box(Rect::new(0.0, 0.0, 595.0, 842.0));
    page.parent(page_tree_id);
    page.contents(content_id);

    // Add the font and, more importantly, the SVG to the resource dictionary
    // so that it can be referenced in the content stream.
    let mut resources = page.resources();
    resources.x_objects().pair(svg_name, svg_id);
    resources.finish();
    page.finish();

    // Let's add an SVG graphic to this file.
    // We need to load its source first and manually parse it into a usvg Tree.
    let tree = usvg::Tree::from_str(svg, &usvg::Options::default())
        .map_err(|err| format!("usvg parse: {err}"))?;

    // Then, we will write it to the page as the 6th indirect object.
    //
    // This call allocates some indirect object reference IDs for itself. If we
    // wanted to write some more indirect objects afterwards, we could use the
    // return value as the next unused reference ID.
    svg2pdf::convert_tree_into(&tree, svg2pdf::Options::default(), &mut writer, svg_id);

    // Write a content stream
    let content = Content::new();
    writer.stream(content_id, &content.finish());

    let bytes = writer.finish();
    let document = lopdf::Document::load_mem(&bytes)
        .map_err(|err| format!("lopdf load generated pdf: {err}"))?;
    let svg_xobject = document
        .get_object((5, 0))
        .map_err(|err| format!("grab xobject from generated pdf: {err}"))?;
    let object = svg_xobject.as_stream().unwrap();

    Ok(object.clone())
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SvgXObjectRef {
    xobject_ref: XObjectRef,
    /// Width of the rendered SVG content
    pub width: Px,
    /// Height of the rendered SVG content
    pub height: Px,
}

impl SvgXObjectRef {
    pub fn add_to_layer(self, layer: &PdfLayerReference, transform: SvgTransform) {
        use crate::CurTransMat;

        // PDF maps an image to a 1x1 square, we have to adjust the transform matrix
        // to fix the distortion

        let width = self.width;
        let height = self.height;
        let dpi = transform.dpi.unwrap_or(300.0);
        let scale_x = transform.scale_x.unwrap_or(1.0);
        let scale_y = transform.scale_y.unwrap_or(1.0);

        // Image at the given dpi should 1px = 1pt
        let image_w = width.into_pt(dpi).0 * scale_x;
        let image_h = height.into_pt(dpi).0 * scale_y;

        let mut transforms = Vec::new();

        transforms.push(CurTransMat::Scale(image_w, image_h));

        if let Some(rotate) = transform.rotate.as_ref() {
            transforms.push(CurTransMat::Translate(
                Pt(-rotate.rotation_center_x.0),
                Pt(-rotate.rotation_center_y.0),
            ));
            transforms.push(CurTransMat::Rotate(rotate.angle_ccw_degrees));
            transforms.push(CurTransMat::Translate(
                rotate.rotation_center_x,
                rotate.rotation_center_y,
            ));
        }

        if transform.translate_x.is_some() || transform.translate_y.is_some() {
            transforms.push(CurTransMat::Translate(
                transform.translate_x.unwrap_or(Pt(0.0)),
                transform.translate_y.unwrap_or(Pt(0.0)),
            ));
        }

        layer.use_xobject(self.xobject_ref, &transforms);
    }
}

impl Svg {
    /// Internally parses the SVG string, converts it to a PDF
    /// document using the svg2pdf crate, parses the resulting PDF again
    /// (using lopdf), then extracts the SVG XObject.
    ///
    /// I wish there was a more direct way, but handling SVG is very tricky.
    pub fn parse(svg_string: &str) -> Result<Self, SvgParseError> {
        // SVG -> PDF bytes
        let svg_xobject = export_svg_to_xobject_pdf(svg_string).map_err(|err| {
            SvgParseError::Svg2PdfConversionError(format!("create xobject from svg: {err}"))
        })?;

        let bbox = svg_xobject
            .dict
            .get(b"BBox")
            .map_err(|err| {
                SvgParseError::Svg2PdfConversionError(format!("extract xobject bbox: {err}"))
            })?
            .as_array()
            .map_err(|err| {
                SvgParseError::Svg2PdfConversionError(format!("xobject bbox not an array: {err}"))
            })?;

        let width_px = match bbox.get(2) {
            Some(Object::Integer(px)) => Ok(*px),
            Some(Object::Real(px)) => Ok(px.ceil() as i64),
            Some(obj) => Err(SvgParseError::Svg2PdfConversionError(format!(
                "xobject bbox width not a number: {obj:?}"
            ))),
            None => Err(SvgParseError::Svg2PdfConversionError(
                "xobject bbox missing width field".to_string(),
            )),
        }?;

        let height_px = match bbox.get(3) {
            Some(Object::Integer(px)) => Ok(*px),
            Some(Object::Real(px)) => Ok(px.ceil() as i64),
            Some(obj) => Err(SvgParseError::Svg2PdfConversionError(format!(
                "xobject bbox height not a number: {obj:?}"
            ))),
            None => Err(SvgParseError::Svg2PdfConversionError(
                "xobject bbox missing height field".to_string(),
            )),
        }?;

        Ok(Self {
            svg_xobject,
            width: Px(width_px.max(0) as usize),
            height: Px(height_px.max(0) as usize),
        })
    }

    /// Adds the SVG to the pages /Resources, returns the name of
    /// the reference to the SVG, so that one SVG can be used more
    /// than once on a page
    pub fn into_xobject(self, layer: &PdfLayerReference) -> SvgXObjectRef {
        let width = self.width;
        let height = self.height;
        let xobject_ref = layer.add_xobject(XObject::External(self.svg_xobject));

        SvgXObjectRef {
            xobject_ref,
            width,
            height,
        }
    }

    /// Adds the image to a specific layer and consumes it.
    ///
    /// This is due to a PDF weirdness - images are basically just "names"
    /// and you have to make sure that they are added to resources of the
    /// same page as they are used on.
    ///
    /// You can use the "transform.dpi" parameter to specify a scaling -
    /// the default is 300dpi
    pub fn add_to_layer(self, layer: &PdfLayerReference, transform: SvgTransform) {
        let xobject = self.into_xobject(layer);
        xobject.add_to_layer(layer, transform);
    }
}