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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! PDF layer management. Layers can contain referenced or real content.

use lopdf;

use indices::{PdfPageIndex, PdfLayerIndex};
use std::rc::Weak;
use std::cell::RefCell;
use lopdf::content::Operation;
use glob_defines::OP_PATH_STATE_SET_LINE_WIDTH;
use {
    Font, XObject, PdfColor,  PdfDocument, ExtendedGraphicsStateBuilder, Line, ImageXObject, XObjectRef, Color, IndirectFontRef, BlendMode,
    LineJoinStyle, LineCapStyle, LineDashPattern, CurTransMat, TextMatrix, TextRenderingMode, Mm, Pt
};

/// One layer of PDF data
#[derive(Debug, Clone)]
pub struct PdfLayer {
    /// Name of the layer. Must be present for the optional content group
    pub(crate) name: String,
    /// Stream objects in this layer. Usually, one layer == one stream
    pub(super) operations: Vec<Operation>,
}

/// A "reference" to the current layer, allows for inner mutability
/// but only inside this library
#[derive(Debug, Clone)]
pub struct PdfLayerReference {
    /// A weak reference to the document, for inner mutability
    pub document: Weak<RefCell<PdfDocument>>,
    /// The index of the page this layer is on
    pub page: PdfPageIndex,
    /// The index of the layer this layer has (inside the page)
    pub layer: PdfLayerIndex,
}

impl PdfLayer {

    /// Create a new layer, with a name and what index the layer has in the page
    #[inline]
    pub fn new<S>(name: S)
    -> Self where S: Into<String>
    {
        Self {
            name: name.into(),
            operations: Vec::new(),
        }
    }
}

impl Into<lopdf::Stream> for PdfLayer {
    fn into(self)
    -> lopdf::Stream
    {
        use lopdf::{Stream, Dictionary};
        let stream_content = lopdf::content::Content { operations: self.operations };

        // page contents may not be compressed (todo: is this valid for XObjects?)
        Stream::new(Dictionary::new(), stream_content.encode().unwrap()).with_compression(false)
    }
}

impl PdfLayerReference {

    /// Add a shape to the layer. Use `closed` to indicate whether the line is a closed line
    /// Use has_fill to determine if the line should be filled.
    pub fn add_shape(&self, line: Line)
    {
        let line_ops = line.into_stream_op();
        for op in line_ops {
            self.internal_add_operation(op);
        }
    }

    /// Add an image to the layer
    /// To be called from the `image.add_to_layer()` class (see `use_xobject` documentation)
    pub(crate) fn add_image<T>(&self, image: T)
    -> XObjectRef where T: Into<ImageXObject>
    {
        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];

        page_mut.add_xobject(XObject::Image(image.into()))
    }
/*
    /// Add an svg element to the layer
    /// To be called from the `svg.add_to_layer()` class (see `use_xobject` documentation)
    pub(crate) fn add_svg(&self, svg: Svg)
    -> std::result::Result<XObjectRef, ::std::io::Error>
    {
        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];
        let form_data = svg.try_into()?;
        Ok(page_mut.add_xobject(XObject::Form(Box::new(form_data))))
    }
*/
    /// Begins a new text section
    /// You have to make sure to call `end_text_section` afterwards
    #[inline]
    pub fn begin_text_section(&self)
    -> ()
    {
        self.internal_add_operation(Operation::new("BT", vec![] ));
    }

    /// Ends a new text section
    /// Only valid if `begin_text_section` has been called
    #[inline]
    pub fn end_text_section(&self)
    -> ()
    {
        self.internal_add_operation(Operation::new("ET", vec![] ));
    }

    /// Set the current fill color for the layer
    #[inline]
    pub fn set_fill_color(&self, fill_color: Color)
    -> ()
    {
        self.internal_add_operation(PdfColor::FillColor(fill_color));
    }

    /// Set the current font, only valid in a `begin_text_section` to
    /// `end_text_section` block
    #[inline]
    pub fn set_font(&self, font: &IndirectFontRef, font_size: i64)
    -> ()
    {
        self.internal_add_operation(Operation::new("Tf",
            vec![font.name.clone().into(), (font_size).into()]
        ));
    }

    /// Set the current line / outline color for the layer
    #[inline]
    pub fn set_outline_color(&self, color: Color)
    {
        self.internal_add_operation(PdfColor::OutlineColor(color));
    }
    /// Instantiate layers, forms and postscript items on the page
    /// __WARNING__: Object must be added to the same page, since the XObjectRef is just a
    /// String, essentially, it can't be checked that this is the case. The caller is
    /// responsible for ensuring this. However, you can use the `Image` struct
    /// and use `image.add_to(layer)`, which will essentially do the same thing, but ensures
    /// that the image is referenced correctly
    ///
    /// Function is limited to this library to ensure that outside code cannot call it
    pub(crate) fn use_xobject(&self, xobj: XObjectRef,
                        translate_x: Option<Mm>, translate_y: Option<Mm>,
                        rotate_cw: Option<f64>,
                        scale_x: Option<f64>, scale_y: Option<f64>)
    {
        // save graphics state
        self.save_graphics_state();

        // apply ctm if any
        let (mut t_x, mut t_y) = (Mm(0.0), Mm(0.0));
        let (mut s_x, mut s_y) = (0.0, 0.0);

        if let Some(tr_x) = translate_x { t_x = tr_x; }
        if let Some(tr_y) = translate_y { t_y = tr_y; }
        if let Some(sc_x) = scale_x { s_x = sc_x; }
        if let Some(sc_y) = scale_y { s_y = sc_y; }

        // translate, rotate, scale - order does not matter

        if t_x != Mm(0.0) || t_y != Mm(0.0) {
            let translate_ctm = CurTransMat::Translate(t_x, t_y);
            self.internal_add_operation(translate_ctm);
        }

        if let Some(rot) = rotate_cw {
            let rotate_ctm = CurTransMat::Rotate(rot);
            self.internal_add_operation(rotate_ctm);
        }

        if s_x != 0.0 || s_y != 0.0 {
            let scale_ctm = CurTransMat::Scale(s_x, s_y);
            self.internal_add_operation(scale_ctm);
        }

        // invoke object
        self.internal_invoke_xobject(xobj.name);

        // restore graphics state
        self.restore_graphics_state();
    }

    /// Set the overprint mode of the stroke color to true (overprint) or false (no overprint)
    pub fn set_overprint_fill(&self, overprint: bool)
    {
        let new_overprint_state = ExtendedGraphicsStateBuilder::new()
                                      .with_overprint_fill(overprint)
                                      .build();

        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];

        let new_ref = page_mut.add_graphics_state(new_overprint_state);

        // add gs operator to stream
        page_mut.layers[self.layer.0]
            .operations.push(lopdf::content::Operation::new(
                "gs", vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())]
        ));
    }

    /// Set the overprint mode of the fill color to true (overprint) or false (no overprint)
    /// This changes the graphics state of the current page, don't do it too often or you'll bloat the file size
    pub fn set_overprint_stroke(&self, overprint: bool)
    {
        // this is technically an operation on the page level
        let new_overprint_state = ExtendedGraphicsStateBuilder::new()
                                      .with_overprint_stroke(overprint)
                                      .build();

        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];

        let new_ref = page_mut.add_graphics_state(new_overprint_state);
        page_mut.layers[self.layer.0]
            .operations.push(lopdf::content::Operation::new(
                "gs", vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())]
        ));
    }

    /// Set the overprint mode of the fill color to true (overprint) or false (no overprint)
    /// This changes the graphics state of the current page, don't do it too often or you'll bloat the file size
    pub fn set_blend_mode(&self, blend_mode: BlendMode)
    {
        // this is technically an operation on the page level
        let new_blend_mode_state = ExtendedGraphicsStateBuilder::new()
                                      .with_blend_mode(blend_mode)
                                      .build();

        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];

        let new_ref = page_mut.add_graphics_state(new_blend_mode_state);

        page_mut.layers[self.layer.0]
            .operations.push(lopdf::content::Operation::new(
                "gs", vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())]
        ));
    }

    /// Set the current line thickness, in points
    ///
    /// __NOTE__: 0.0 is a special value, it does not make the line disappear, but rather
    /// makes it appear 1px wide across all devices
    #[inline]
    pub fn set_outline_thickness(&self, outline_thickness: f64)
    {
        use lopdf::Object::*;
        self.internal_add_operation(Operation::new(OP_PATH_STATE_SET_LINE_WIDTH, vec![Real(outline_thickness)]));
    }

    /// Set the current line join style for outlines
    #[inline]
    pub fn set_line_join_style(&self, line_join: LineJoinStyle) {
        self.internal_add_operation(line_join);
    }

    /// Set the current line join style for outlines
    #[inline]
    pub fn set_line_cap_style(&self, line_cap: LineCapStyle) {
        self.internal_add_operation(line_cap);
    }

    /// Set the current line join style for outlines
    #[inline]
    pub fn set_line_dash_pattern(&self, dash_pattern: LineDashPattern) {
        self.internal_add_operation(dash_pattern);
    }

    /// Sets (adds to) the current transformation matrix
    /// Use `save_graphics_state()` and `restore_graphics_state()`
    /// to "scope" the transformation matrix to a specific function
    #[inline]
    pub fn set_ctm(&self, ctm: CurTransMat) {
        self.internal_add_operation(ctm);
    }

    /// Sets (replaces) the current text matrix
    /// This does not have to be scoped, since the matrix is replaced
    /// instead of concatenated to the current matrix. However,
    /// you should only call this function with in a block scoped by
    /// `begin_text_section()` and `end_text_section()`
    #[inline]
    pub fn set_text_matrix(&self, tm: TextMatrix) {
        self.internal_add_operation(tm);
    }

    /// Sets the position where the text should appear
    #[inline]
    pub fn set_text_cursor(&self, x:Mm, y:Mm) {
        let x_in_pt: Pt = x.into();
        let y_in_pt: Pt = y.into();
        self.internal_add_operation(Operation::new("Td",
                vec![x_in_pt.into(), y_in_pt.into()]
        ));
    }

    /// If called inside a text block scoped by `begin_text_section` and
    /// `end_text_section`, moves the cursor to a new line. PDF does not have
    /// any concept of "alignment" except left-aligned text
    /// __Note:__ Use `set_line_height` earlier to set the line height first
    #[inline]
    pub fn add_line_break(&self) {
        self.internal_add_operation(Operation::new("T*", Vec::new()));
    }

    /// Sets the text line height inside a text block
    /// (must be called within `begin_text_block` and `end_text_block`)
    #[inline]
    pub fn set_line_height(&self, height: i64) {
        self.internal_add_operation(Operation::new("TL",
            vec![lopdf::Object::Integer(height)]
        ));
    }

    /// Sets the character spacing inside a text block
    /// Values are given in points. A value of 3 (pt) will increase
    /// the spacing inside a word by 3pt.
    #[inline]
    pub fn set_character_spacing(&self, spacing: i64) {
        self.internal_add_operation(Operation::new("Tc",
            vec![lopdf::Object::Integer(spacing)]
        ));
    }

    /// Sets the word spacing inside a text block.
    /// Same as `set_character_spacing`, just for words.
    /// __Note:__ This currently does not work for external
    /// fonts. External fonts are encoded with Unicode, and
    /// PDF does not recognize unicode fonts. It only
    /// recognizes builtin fonts done with PDFDoc encoding.
    /// However, the function itself is valid and _will work_
    /// with builtin fonts.
    #[inline]
    pub fn set_word_spacing(&self, spacing: i64) {
        self.internal_add_operation(Operation::new("Tw",
            vec![lopdf::Object::Integer(spacing)]
        ));
    }

    /// Sets the horizontal scaling (like a "condensed" font)
    /// Default value is 100 (regular scaling). Setting it to
    /// 50 will reduce the width of the written text by half,
    /// but stretch the text
    #[inline]
    pub fn set_text_scaling(&self, scaling: i64) {
        self.internal_add_operation(Operation::new("Tz",
            vec![lopdf::Object::Integer(scaling)]
        ));
    }

    /// Offsets the current text positon (used for superscript
    /// and subscript). To reset the superscript / subscript, call this
    /// function with 0 as the offset. For superscript, use a positive
    /// number, for subscript, use a negative number. This does not
    /// change the size of the font
    #[inline]
    pub fn set_line_offset(&self, offset: i64) {
        self.internal_add_operation(Operation::new("Ts",
            vec![lopdf::Object::Integer(offset)]
        ));
    }

    #[inline]
    pub fn set_text_rendering_mode(&self, mode: TextRenderingMode) {
        self.internal_add_operation(Operation::new("Tr",
            vec![lopdf::Object::Integer(mode.into())]
        ));
    }

    /// Sets the position where the text should appear (in mm)
    #[inline]
    pub fn write_text<S>(&self, text: S, font: &IndirectFontRef)
    -> () where S: Into<String>
    {
        // NOTE: The unwrap() calls in this function are safe, since
        // we've already checked the font for validity when it was added to the document

        use lopdf::Object::*;
        use lopdf::StringFormat::Hexadecimal;
        use lopdf::content::Operation;

        let text = text.into();

        // we need to transform the characters into glyph ids and then add them to the layer
        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();

        // glyph IDs that make up this string

        // kerning for each glyph id. If no kerning is present, will be 0
        // must be the same length as list_gid
        // let mut kerning_data = Vec::<freetype::Vector>::new();

        let bytes: Vec<u8> = {
            use rusttype::FontCollection;
            use rusttype::Codepoint as Cp;

            if let Font::ExternalFont(face_direct_ref) = doc.fonts.get_font(font).unwrap().data {

                let mut list_gid = Vec::<u16>::new();
                let collection = FontCollection::from_bytes(&*face_direct_ref.font_bytes).unwrap();
                let font = collection.clone().into_font().unwrap_or(collection.font_at(0).unwrap());

                // convert into list of glyph ids - unicode magic
                let char_iter = text.chars();

                for ch in char_iter {
                    // note: font.glyph will panic if the character is \0
                    // since that can't happen in Rust, I think we're safe here
                    let glyph = font.glyph(Cp(ch as u32));
                    list_gid.push(glyph.id().0 as u16);

                    // todo - kerning !!
                    // font.pair_kerning(scale, id, base_glyph.id());
                }

                list_gid.iter()
                    .flat_map(|x| vec!((x >> 8) as u8, (x & 255) as u8))
                    .collect::<Vec<u8>>()
            } else {
                text.as_bytes().to_vec()
            }
        };

        doc.pages[self.page.0]
            .layers[self.layer.0]
                .operations.push(Operation::new("Tj",
                    vec![String(bytes, Hexadecimal)]
        ));
    }

    /// Saves the current graphic state
    #[inline]
    pub fn save_graphics_state(&self) {
        self.internal_add_operation(Operation::new("q", Vec::new()));
    }

    /// Restores the previous graphic state
    #[inline]
    pub fn restore_graphics_state(&self) {
        self.internal_add_operation(Operation::new("Q", Vec::new()));
    }

    /// Add text to the file, x and y are measure in millimeter from the bottom left corner
    #[inline]
    pub fn use_text<S>(&self, text: S, font_size: i64,
                       x: Mm, y: Mm, font: &IndirectFontRef)
    -> () where S: Into<String>
    {
            self.begin_text_section();
            self.set_font(font, font_size);
            self.set_text_cursor(x, y);
            self.write_text(text, font);
            self.end_text_section();
    }

/*
    /// Instantiate SVG data
    #[inline]
    pub fn use_svg(&self, width_mm: f64, height_mm: f64,
                   x_mm: f64, y_mm: f64, svg_data_index: SvgIndex)
    {
        let svg_element_ref = {
            let doc = self.document.upgrade().unwrap();
            let doc = doc.borrow_mut();
            let element = doc.contents.get((svg_data_index.0).0).expect("invalid svg reference");
            (*element).clone()
        };

        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();

        // todo: what about width / height?
        doc.pages.get_mut(self.page.0).unwrap()
            .layers.get_mut(self.layer.0).unwrap()
                .layer.push(PdfResource::ReferencedResource(svg_data_index.0.clone()));
    }
*/

    // internal function to invoke an xobject
    fn internal_invoke_xobject(&self, name: String)
    {
        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let page_mut = &mut doc.pages[self.page.0];

        page_mut.layers[self.layer.0]
          .operations.push(lopdf::content::Operation::new(
              "Do", vec![lopdf::Object::Name(name.as_bytes().to_vec())]
        ));
    }

    // internal function to add an operation (prevents locking)
    fn internal_add_operation<T>(&self, op: T)
    -> () where T: Into<Operation>
    {
        let doc = self.document.upgrade().unwrap();
        let mut doc = doc.borrow_mut();
        let layer = &mut doc.pages[self.page.0].layers[self.layer.0];
        layer.operations.push(op.into());
    }
}