blitz-pdf 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +266 -12
- package/THIRD_PARTY_NOTICES.md +4 -2
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/document-options.d.ts +36 -0
- package/dist/document-options.d.ts.map +1 -0
- package/dist/document-options.js +236 -0
- package/dist/document-options.js.map +1 -0
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/options.d.ts +35 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +21 -14
- package/dist/options.js.map +1 -1
- package/dist/types.d.ts +71 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +7 -0
- package/dist/validation.js.map +1 -0
- package/examples/zugferd.mjs +41 -0
- package/licenses/RUST_DEPENDENCIES.txt +27 -0
- package/package.json +3 -2
- package/src/browser.ts +1 -1
- package/src/document-options.ts +218 -0
- package/src/node.ts +1 -1
- package/src/options.ts +18 -13
- package/src/types.ts +76 -1
- package/src/validation.ts +6 -0
- package/vendor/README.md +27 -2
- package/vendor/krilla/.cargo-ok +1 -0
- package/vendor/krilla/.cargo_vcs_info.json +6 -0
- package/vendor/krilla/Cargo.lock +1357 -0
- package/vendor/krilla/Cargo.toml +165 -0
- package/vendor/krilla/LICENSE_APACHE +202 -0
- package/vendor/krilla/LICENSE_MIT +21 -0
- package/vendor/krilla/README.md +156 -0
- package/vendor/krilla/examples/basic.rs +117 -0
- package/vendor/krilla/examples/empty_document.rs +25 -0
- package/vendor/krilla/examples/parley.rs +174 -0
- package/vendor/krilla/examples/simple_text.rs +118 -0
- package/vendor/krilla/examples/stream_builder.rs +93 -0
- package/vendor/krilla/icc/LICENSE +116 -0
- package/vendor/krilla/icc/README.md +1 -0
- package/vendor/krilla/icc/sGrey-v2-magic.icc +0 -0
- package/vendor/krilla/icc/sGrey-v4.icc +0 -0
- package/vendor/krilla/icc/sRGB-v2-magic.icc +0 -0
- package/vendor/krilla/icc/sRGB-v4.icc +0 -0
- package/vendor/krilla/src/chunk_container.rs +503 -0
- package/vendor/krilla/src/configure/PDF_A1.md +227 -0
- package/vendor/krilla/src/configure/PDF_A2.md +269 -0
- package/vendor/krilla/src/configure/PDF_A3.md +31 -0
- package/vendor/krilla/src/configure/PDF_A4.md +211 -0
- package/vendor/krilla/src/configure/PDF_UA1.md +202 -0
- package/vendor/krilla/src/configure/README.md +23 -0
- package/vendor/krilla/src/configure/mod.rs +197 -0
- package/vendor/krilla/src/configure/validate.rs +1244 -0
- package/vendor/krilla/src/configure/version.rs +134 -0
- package/vendor/krilla/src/content.rs +1352 -0
- package/vendor/krilla/src/data.rs +49 -0
- package/vendor/krilla/src/document.rs +156 -0
- package/vendor/krilla/src/error.rs +170 -0
- package/vendor/krilla/src/geom.rs +341 -0
- package/vendor/krilla/src/graphics/blend.rs +46 -0
- package/vendor/krilla/src/graphics/color.rs +552 -0
- package/vendor/krilla/src/graphics/graphic.rs +30 -0
- package/vendor/krilla/src/graphics/graphics_state.rs +251 -0
- package/vendor/krilla/src/graphics/icc.rs +218 -0
- package/vendor/krilla/src/graphics/image.rs +835 -0
- package/vendor/krilla/src/graphics/mask.rs +128 -0
- package/vendor/krilla/src/graphics/mod.rs +14 -0
- package/vendor/krilla/src/graphics/paint.rs +434 -0
- package/vendor/krilla/src/graphics/separation.rs +86 -0
- package/vendor/krilla/src/graphics/shading_function.rs +865 -0
- package/vendor/krilla/src/graphics/shading_pattern.rs +60 -0
- package/vendor/krilla/src/graphics/tiling_pattern.rs +119 -0
- package/vendor/krilla/src/graphics/xobject.rs +150 -0
- package/vendor/krilla/src/interactive/action.rs +68 -0
- package/vendor/krilla/src/interactive/annotation.rs +283 -0
- package/vendor/krilla/src/interactive/destination.rs +289 -0
- package/vendor/krilla/src/interactive/mod.rs +3 -0
- package/vendor/krilla/src/interchange/embed.rs +240 -0
- package/vendor/krilla/src/interchange/metadata.rs +564 -0
- package/vendor/krilla/src/interchange/mod.rs +4 -0
- package/vendor/krilla/src/interchange/outline.rs +263 -0
- package/vendor/krilla/src/interchange/tagging/fmt.rs +733 -0
- package/vendor/krilla/src/interchange/tagging/generate.toml +474 -0
- package/vendor/krilla/src/interchange/tagging/generated.rs +4754 -0
- package/vendor/krilla/src/interchange/tagging/mod.rs +1324 -0
- package/vendor/krilla/src/interchange/tagging/tag.rs +686 -0
- package/vendor/krilla/src/lib.rs +179 -0
- package/vendor/krilla/src/num.rs +26 -0
- package/vendor/krilla/src/page.rs +612 -0
- package/vendor/krilla/src/pdf.rs +313 -0
- package/vendor/krilla/src/resource.rs +436 -0
- package/vendor/krilla/src/serialize.rs +1101 -0
- package/vendor/krilla/src/stream.rs +368 -0
- package/vendor/krilla/src/surface.rs +664 -0
- package/vendor/krilla/src/text/cid.rs +488 -0
- package/vendor/krilla/src/text/font.rs +351 -0
- package/vendor/krilla/src/text/glyph/bitmap.rs +81 -0
- package/vendor/krilla/src/text/glyph/colr.rs +581 -0
- package/vendor/krilla/src/text/glyph/mod.rs +205 -0
- package/vendor/krilla/src/text/glyph/outline.rs +72 -0
- package/vendor/krilla/src/text/glyph/svg.rs +34 -0
- package/vendor/krilla/src/text/group.rs +463 -0
- package/vendor/krilla/src/text/mod.rs +232 -0
- package/vendor/krilla/src/text/shape.rs +102 -0
- package/vendor/krilla/src/text/type3.rs +514 -0
- package/vendor/krilla/src/util.rs +286 -0
- package/vendor/stylo/properties/longhands.toml +0 -1
- package/vendor/stylo/stylesheets/rule_parser.rs +1 -1
- package/wasm/blitz_pdf.js +10 -5
- package/wasm/blitz_pdf_bg.wasm +0 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
//! Drawing onto a surface.
|
|
2
|
+
//!
|
|
3
|
+
//! This module contains most core part of krilla: The [`Surface`] struct. A surface
|
|
4
|
+
//! represents a drawing area on which you can define the contents of your page. This includes
|
|
5
|
+
//! operations such as applying linear transformations, showing text or images and drawing paths.
|
|
6
|
+
|
|
7
|
+
use std::num::NonZeroU64;
|
|
8
|
+
|
|
9
|
+
use crate::chunk_container::ChunkContainer;
|
|
10
|
+
use crate::color::rgb;
|
|
11
|
+
use crate::configure::validate::VersionedFeature;
|
|
12
|
+
use crate::configure::ValidationError;
|
|
13
|
+
use crate::content::ContentBuilder;
|
|
14
|
+
use crate::geom::Path;
|
|
15
|
+
#[cfg(any(feature = "raster-images", feature = "pdf"))]
|
|
16
|
+
use crate::geom::Size;
|
|
17
|
+
use crate::geom::{Point, Transform};
|
|
18
|
+
use crate::graphic::Graphic;
|
|
19
|
+
use crate::graphics::blend::BlendMode;
|
|
20
|
+
use crate::graphics::graphics_state::ExtGState;
|
|
21
|
+
#[cfg(feature = "raster-images")]
|
|
22
|
+
use crate::graphics::image::Image;
|
|
23
|
+
use crate::graphics::mask::Mask;
|
|
24
|
+
use crate::graphics::paint::{Fill, FillRule, Stroke};
|
|
25
|
+
use crate::graphics::shading_function::ShadingFunction;
|
|
26
|
+
use crate::interchange::tagging::{ContentTag, Identifier, PageTagIdentifier};
|
|
27
|
+
use crate::num::NormalizedF32;
|
|
28
|
+
use crate::paint::{InnerPaint, Paint};
|
|
29
|
+
#[cfg(feature = "pdf")]
|
|
30
|
+
use crate::pdf::PdfDocument;
|
|
31
|
+
use crate::serialize::SerializeContext;
|
|
32
|
+
use crate::stream::{Stream, StreamBuilder};
|
|
33
|
+
use crate::tagging::ArtifactType;
|
|
34
|
+
use crate::text::Font;
|
|
35
|
+
use crate::text::{draw_glyph, Glyph};
|
|
36
|
+
#[cfg(feature = "simple-text")]
|
|
37
|
+
use crate::text::{shape::naive_shape, TextDirection};
|
|
38
|
+
|
|
39
|
+
/// Can be used to associate render operations with a unique identifier.
|
|
40
|
+
/// This is useful if you want to backtrack a validation error to a specific
|
|
41
|
+
/// identifier chosen by you.
|
|
42
|
+
pub type Location = NonZeroU64;
|
|
43
|
+
|
|
44
|
+
/// A surface.
|
|
45
|
+
///
|
|
46
|
+
/// Represents a drawing area for defining graphical content. The origin of the
|
|
47
|
+
/// coordinate axis is in the top-left corner.
|
|
48
|
+
///
|
|
49
|
+
/// You cannot directly create an instance of a [`Surface`] yourself.
|
|
50
|
+
/// Instead, there are two ways of getting access to a surface, which you can then use to draw on:
|
|
51
|
+
///
|
|
52
|
+
/// - The first way, and also the most common one you will use, is by creating a new document,
|
|
53
|
+
/// adding a page to it and then invoking the [`Page::surface`] method. The surface returned as part of
|
|
54
|
+
/// that represents the drawing area of the page.
|
|
55
|
+
/// - The second way is by calling the [`Surface::stream_builder`] method on the surface, to create a sub-drawing
|
|
56
|
+
/// context. See the documentation of the [`stream`] module for more information.
|
|
57
|
+
///
|
|
58
|
+
/// The surface uses a `push` and `pop` based mechanism for applying certain actions. For example,
|
|
59
|
+
/// there is a [`Surface::push_transform`] method which allows you to concatenate a new transform to the
|
|
60
|
+
/// current transform matrix. There is also a [`Surface::push_clip_path`] method, which allows you to
|
|
61
|
+
/// intersect the current drawing area with a clip path. Once you call such a `push` method,
|
|
62
|
+
/// the action that it invokes will be in place until you call the `pop` method, which will then
|
|
63
|
+
/// revert the last `push` operation. This allows you to, for example, define a clipping path area
|
|
64
|
+
/// or a mask to use only for certain objects.
|
|
65
|
+
///
|
|
66
|
+
/// It is important that, for each `push` method you invoke, there must be a corresponding `pop`
|
|
67
|
+
/// invocation that reverts it. If, at the end of the using the surface, the number of pushes/pops is
|
|
68
|
+
/// uneven, the program will panic.
|
|
69
|
+
///
|
|
70
|
+
/// [`stream`]: crate::stream
|
|
71
|
+
/// [`Page::surface`]: crate::page::Page::surface
|
|
72
|
+
pub struct Surface<'a> {
|
|
73
|
+
pub(crate) sc: &'a mut SerializeContext,
|
|
74
|
+
pub(crate) chunk_container: &'a mut ChunkContainer,
|
|
75
|
+
fill: Option<Fill>,
|
|
76
|
+
stroke: Option<Stroke>,
|
|
77
|
+
bd: Builders,
|
|
78
|
+
push_instructions: Vec<PushInstruction>,
|
|
79
|
+
page_identifier: Option<PageTagIdentifier>,
|
|
80
|
+
finish_fn: Box<dyn FnMut(Stream, i32) + 'a>,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
impl<'a> Surface<'a> {
|
|
84
|
+
pub(crate) fn new(
|
|
85
|
+
sc: &'a mut SerializeContext,
|
|
86
|
+
chunk_container: &'a mut ChunkContainer,
|
|
87
|
+
root_builder: ContentBuilder,
|
|
88
|
+
page_identifier: Option<PageTagIdentifier>,
|
|
89
|
+
finish_fn: Box<dyn FnMut(Stream, i32) + 'a>,
|
|
90
|
+
) -> Surface<'a> {
|
|
91
|
+
Self {
|
|
92
|
+
sc,
|
|
93
|
+
chunk_container,
|
|
94
|
+
bd: Builders::new(root_builder),
|
|
95
|
+
page_identifier,
|
|
96
|
+
fill: None,
|
|
97
|
+
stroke: None,
|
|
98
|
+
push_instructions: vec![],
|
|
99
|
+
finish_fn,
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Return a `StreamBuilder` to allow drawing on a sub-context.
|
|
104
|
+
pub fn stream_builder(&mut self) -> StreamBuilder<'_> {
|
|
105
|
+
StreamBuilder::new(self.sc, self.chunk_container)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// Set the fill that should be used for the next drawing operation.
|
|
109
|
+
///
|
|
110
|
+
/// You can set it to `None` if you want to disable filling (though
|
|
111
|
+
/// if there is no active stroke, than krilla will choose to fill
|
|
112
|
+
/// with black by default).
|
|
113
|
+
pub fn set_fill(&mut self, fill: Option<Fill>) {
|
|
114
|
+
self.fill = fill;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Get the currently active fill.
|
|
118
|
+
pub fn get_fill(&mut self) -> Option<&Fill> {
|
|
119
|
+
self.fill.as_ref()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Set the stroke that should be used for drawing operations.
|
|
123
|
+
///
|
|
124
|
+
/// You can set it to `None` if you want to disable stroking.
|
|
125
|
+
pub fn set_stroke(&mut self, stroke: Option<Stroke>) {
|
|
126
|
+
self.stroke = stroke;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// Get the currently active stroke.
|
|
130
|
+
pub fn get_stroke(&mut self) -> Option<&Stroke> {
|
|
131
|
+
self.stroke.as_ref()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// Draw a path using the currently active fill and/or stroke.
|
|
135
|
+
pub fn draw_path(&mut self, path: &Path) {
|
|
136
|
+
if self.fill.is_some() || self.stroke.is_some() {
|
|
137
|
+
if self.has_complex_fill_or_stroke() {
|
|
138
|
+
self.bd.get_mut().draw_path(
|
|
139
|
+
&path.0,
|
|
140
|
+
self.fill.as_ref(),
|
|
141
|
+
None,
|
|
142
|
+
self.sc,
|
|
143
|
+
self.chunk_container,
|
|
144
|
+
);
|
|
145
|
+
self.bd.get_mut().draw_path(
|
|
146
|
+
&path.0,
|
|
147
|
+
None,
|
|
148
|
+
self.stroke.as_ref(),
|
|
149
|
+
self.sc,
|
|
150
|
+
self.chunk_container,
|
|
151
|
+
);
|
|
152
|
+
} else {
|
|
153
|
+
self.bd.get_mut().draw_path(
|
|
154
|
+
&path.0,
|
|
155
|
+
self.fill.as_ref(),
|
|
156
|
+
self.stroke.as_ref(),
|
|
157
|
+
self.sc,
|
|
158
|
+
self.chunk_container,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
// Draw with black by default.
|
|
163
|
+
self.bd.get_mut().draw_path(
|
|
164
|
+
&path.0,
|
|
165
|
+
Some(&Fill::default()),
|
|
166
|
+
None,
|
|
167
|
+
self.sc,
|
|
168
|
+
self.chunk_container,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Start a new tagged content section.
|
|
174
|
+
///
|
|
175
|
+
/// # Panics
|
|
176
|
+
/// Panics if a tagged section has already been started.
|
|
177
|
+
/// Panics if an artifact with a required bbox has none.
|
|
178
|
+
pub fn start_tagged(&mut self, tag: ContentTag) -> Identifier {
|
|
179
|
+
if let Some(id) = &mut self.page_identifier {
|
|
180
|
+
match tag {
|
|
181
|
+
// An artifact is actually not really part of tagged PDF and doesn't have
|
|
182
|
+
// a marked content identifier, so we need to return a dummy one here. It's just
|
|
183
|
+
// the API of krilla that conflates artifacts with tagged content,
|
|
184
|
+
// for the sake of simplicity. But the user of the library does not need to know
|
|
185
|
+
// about this.
|
|
186
|
+
ContentTag::Artifact(artifact) => {
|
|
187
|
+
if matches!(artifact.kind, ArtifactType::Header | ArtifactType::Footer)
|
|
188
|
+
&& self.sc.serialize_settings().pdf_version()
|
|
189
|
+
< VersionedFeature::HeaderFooterArtifactSubtypes.minimum_pdf_version()
|
|
190
|
+
{
|
|
191
|
+
self.sc.register_validation_error(
|
|
192
|
+
ValidationError::RequiresNewerPdfVersion(
|
|
193
|
+
VersionedFeature::HeaderFooterArtifactSubtypes,
|
|
194
|
+
self.sc.location,
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if artifact.requires_properties(self.sc.serialize_settings().pdf_version()) {
|
|
200
|
+
self.bd
|
|
201
|
+
.get_mut()
|
|
202
|
+
.start_marked_content_with_properties(self.sc, None, tag);
|
|
203
|
+
} else {
|
|
204
|
+
self.bd.get_mut().start_marked_content(tag.name());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if artifact.requires_bbox(self.sc.serialize_settings().pdf_version())
|
|
208
|
+
&& artifact.bbox.is_none()
|
|
209
|
+
{
|
|
210
|
+
panic!("Background artifact must have a bounding box in PDF 1.7");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
Identifier::dummy()
|
|
214
|
+
}
|
|
215
|
+
ContentTag::Span(_) | ContentTag::Other => {
|
|
216
|
+
self.bd.get_mut().start_marked_content_with_properties(
|
|
217
|
+
self.sc,
|
|
218
|
+
Some(id.mcid),
|
|
219
|
+
tag,
|
|
220
|
+
);
|
|
221
|
+
id.bump().into()
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
Identifier::dummy()
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/// End the current tagged section.
|
|
230
|
+
///
|
|
231
|
+
/// # Panics
|
|
232
|
+
/// Panics if no tagged section has been started.
|
|
233
|
+
pub fn end_tagged(&mut self) {
|
|
234
|
+
if self.page_identifier.is_some() {
|
|
235
|
+
self.bd.get_mut().end_marked_content();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fn outline_glyphs(
|
|
240
|
+
&mut self,
|
|
241
|
+
glyphs: &[impl Glyph],
|
|
242
|
+
context_color: rgb::Color,
|
|
243
|
+
start: Point,
|
|
244
|
+
font: Font,
|
|
245
|
+
font_size: f32,
|
|
246
|
+
) {
|
|
247
|
+
let (mut cur_x, y) = (start.x, start.y);
|
|
248
|
+
|
|
249
|
+
for glyph in glyphs {
|
|
250
|
+
let mut base_transform = tiny_skia_path::Transform::from_translate(
|
|
251
|
+
cur_x + glyph.x_offset(font_size),
|
|
252
|
+
y - glyph.y_offset(font_size),
|
|
253
|
+
);
|
|
254
|
+
base_transform = base_transform.pre_concat(tiny_skia_path::Transform::from_scale(
|
|
255
|
+
font_size / font.units_per_em(),
|
|
256
|
+
-font_size / font.units_per_em(),
|
|
257
|
+
));
|
|
258
|
+
draw_glyph(
|
|
259
|
+
font.clone(),
|
|
260
|
+
context_color,
|
|
261
|
+
glyph.glyph_id(),
|
|
262
|
+
Transform::from_tsp(base_transform),
|
|
263
|
+
self,
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
cur_x += glyph.x_advance(font_size);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// Draw a sequence of glyphs using the currently active fill and/or stroke.
|
|
271
|
+
///
|
|
272
|
+
/// This is a very low-level method, which gives you full control over how to place
|
|
273
|
+
/// the glyphs that make up the text. This means that you must have your own text processing
|
|
274
|
+
/// logic for dealing with bidirectional text, font fallback, text layouting, etc.
|
|
275
|
+
pub fn draw_glyphs(
|
|
276
|
+
&mut self,
|
|
277
|
+
start: Point,
|
|
278
|
+
glyphs: &[impl Glyph],
|
|
279
|
+
font: Font,
|
|
280
|
+
text: &str,
|
|
281
|
+
font_size: f32,
|
|
282
|
+
outlined: bool,
|
|
283
|
+
) {
|
|
284
|
+
let context_color = self.context_color();
|
|
285
|
+
if outlined {
|
|
286
|
+
self.outline_glyphs(glyphs, context_color, start, font, font_size);
|
|
287
|
+
} else {
|
|
288
|
+
match (self.fill.as_ref(), self.stroke.as_ref()) {
|
|
289
|
+
(Some(f), Some(s)) => {
|
|
290
|
+
if self.has_complex_fill_or_stroke() {
|
|
291
|
+
// We need to draw fill and stroke separately. In order to avoid embedding
|
|
292
|
+
// text twice, we fill the text as normal text and then stroke it as a path.
|
|
293
|
+
self.bd.get_mut().draw_glyphs(
|
|
294
|
+
start,
|
|
295
|
+
self.sc,
|
|
296
|
+
self.chunk_container,
|
|
297
|
+
Some(f),
|
|
298
|
+
None,
|
|
299
|
+
context_color,
|
|
300
|
+
glyphs,
|
|
301
|
+
font.clone(),
|
|
302
|
+
text,
|
|
303
|
+
font_size,
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
self.outline_glyphs(glyphs, context_color, start, font, font_size);
|
|
307
|
+
} else {
|
|
308
|
+
self.bd.get_mut().draw_glyphs(
|
|
309
|
+
start,
|
|
310
|
+
self.sc,
|
|
311
|
+
self.chunk_container,
|
|
312
|
+
Some(f),
|
|
313
|
+
Some(s),
|
|
314
|
+
context_color,
|
|
315
|
+
glyphs,
|
|
316
|
+
font,
|
|
317
|
+
text,
|
|
318
|
+
font_size,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
(None, Some(s)) => {
|
|
323
|
+
self.bd.get_mut().draw_glyphs(
|
|
324
|
+
start,
|
|
325
|
+
self.sc,
|
|
326
|
+
self.chunk_container,
|
|
327
|
+
None,
|
|
328
|
+
Some(s),
|
|
329
|
+
context_color,
|
|
330
|
+
glyphs,
|
|
331
|
+
font,
|
|
332
|
+
text,
|
|
333
|
+
font_size,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
(Some(f), None) => {
|
|
337
|
+
self.bd.get_mut().draw_glyphs(
|
|
338
|
+
start,
|
|
339
|
+
self.sc,
|
|
340
|
+
self.chunk_container,
|
|
341
|
+
Some(f),
|
|
342
|
+
None,
|
|
343
|
+
context_color,
|
|
344
|
+
glyphs,
|
|
345
|
+
font,
|
|
346
|
+
text,
|
|
347
|
+
font_size,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
(None, None) => {
|
|
351
|
+
// Draw with black by default.
|
|
352
|
+
self.bd.get_mut().draw_glyphs(
|
|
353
|
+
start,
|
|
354
|
+
self.sc,
|
|
355
|
+
self.chunk_container,
|
|
356
|
+
Some(&Fill::default()),
|
|
357
|
+
None,
|
|
358
|
+
context_color,
|
|
359
|
+
glyphs,
|
|
360
|
+
font,
|
|
361
|
+
text,
|
|
362
|
+
font_size,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/// Draw some text using the currently active fill and/or stroke.
|
|
370
|
+
///
|
|
371
|
+
/// This is a high-level method which allows you to just provide some text, which will
|
|
372
|
+
/// then be rendered into a single line. However, this approach has restrictions:
|
|
373
|
+
///
|
|
374
|
+
/// - It will not perform BIDI resolution and only supports a single script, meaning that you
|
|
375
|
+
/// must ensure that your text does not contain multiple scripts.
|
|
376
|
+
/// - It will only use the single font you provided to draw the text, no font fallback will
|
|
377
|
+
/// be performed.
|
|
378
|
+
///
|
|
379
|
+
/// If you need more advanced control over how your text looks,
|
|
380
|
+
/// you can use the `fill_glyphs` method.
|
|
381
|
+
#[cfg(feature = "simple-text")]
|
|
382
|
+
pub fn draw_text(
|
|
383
|
+
&mut self,
|
|
384
|
+
start: Point,
|
|
385
|
+
font: Font,
|
|
386
|
+
font_size: f32,
|
|
387
|
+
text: &str,
|
|
388
|
+
outlined: bool,
|
|
389
|
+
direction: TextDirection,
|
|
390
|
+
) {
|
|
391
|
+
let glyphs = naive_shape(text, font.clone(), direction);
|
|
392
|
+
|
|
393
|
+
self.draw_glyphs(start, &glyphs, font, text, font_size, outlined);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/// Set the location that should be assumed for subsequent operations.
|
|
397
|
+
pub fn set_location(&mut self, location: Location) {
|
|
398
|
+
self.sc.set_location(location);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/// Reset the location that should be assumed for subsequent operations.
|
|
402
|
+
pub fn reset_location(&mut self) {
|
|
403
|
+
self.sc.reset_location();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/// Return the current transformation matrix.
|
|
407
|
+
pub fn ctm(&self) -> Transform {
|
|
408
|
+
self.bd.get().cur_transform()
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/// Concatenate a new transform to the current transformation matrix.
|
|
412
|
+
pub fn push_transform(&mut self, transform: &Transform) {
|
|
413
|
+
self.push_instructions.push(PushInstruction::Transform);
|
|
414
|
+
self.bd.get_mut().save_graphics_state();
|
|
415
|
+
self.bd.get_mut().concat_transform(transform);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/// Push a new blend mode.
|
|
419
|
+
pub fn push_blend_mode(&mut self, blend_mode: BlendMode) {
|
|
420
|
+
self.push_instructions.push(PushInstruction::BlendMode);
|
|
421
|
+
self.bd.get_mut().save_graphics_state();
|
|
422
|
+
self.bd.get_mut().set_blend_mode(blend_mode.to_pdf());
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/// Push a new clip path.
|
|
426
|
+
pub fn push_clip_path(&mut self, path: &Path, clip_rule: &FillRule) {
|
|
427
|
+
self.push_instructions.push(PushInstruction::ClipPath);
|
|
428
|
+
self.bd.get_mut().push_clip_path(&path.0, clip_rule);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/// Push a new mask.
|
|
432
|
+
pub fn push_mask(&mut self, mask: Mask) {
|
|
433
|
+
self.push_instructions
|
|
434
|
+
.push(PushInstruction::Mask(Box::new(mask)));
|
|
435
|
+
self.bd
|
|
436
|
+
.sub_builders
|
|
437
|
+
.push(ContentBuilder::new(Transform::identity(), true, self.sc));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
#[cfg(feature = "pdf")]
|
|
441
|
+
/// Embed a single PDF page with the given dimensions.
|
|
442
|
+
pub fn draw_pdf_page(&mut self, pdf: &PdfDocument, size: Size, page_idx: usize) {
|
|
443
|
+
use crate::geom::Rect;
|
|
444
|
+
|
|
445
|
+
let obj_ref = self.sc.embed_pdf_page_as_xobject(pdf, page_idx);
|
|
446
|
+
// If the user provided an invalid page index, we will detect this later on anyway, so
|
|
447
|
+
// just use dummy dimensions here.
|
|
448
|
+
let (page_width, page_height) = pdf
|
|
449
|
+
.pages()
|
|
450
|
+
.get(page_idx)
|
|
451
|
+
.map(|p| p.render_dimensions())
|
|
452
|
+
.unwrap_or((1.0, 1.0));
|
|
453
|
+
let transform =
|
|
454
|
+
Transform::from_scale(size.width() / page_width, size.height() / page_height);
|
|
455
|
+
// We applied a transform to invert the y-axis initially, but since the embedded PDF also uses
|
|
456
|
+
// a y-up coordinate system, we need to "invert" this.
|
|
457
|
+
self.push_transform(&transform);
|
|
458
|
+
self.push_transform(&Transform::from_row(1.0, 0.0, 0.0, -1.0, 0.0, page_height));
|
|
459
|
+
|
|
460
|
+
self.bd.get_mut().draw_xobject_by_reference(
|
|
461
|
+
self.sc,
|
|
462
|
+
self.chunk_container,
|
|
463
|
+
Rect::from_xywh(0.0, 0.0, page_width, page_height).unwrap(),
|
|
464
|
+
obj_ref,
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
self.pop();
|
|
468
|
+
self.pop();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/// Push a new opacity, meaning that each subsequent graphics object will be
|
|
472
|
+
/// rendered with a base opacity.
|
|
473
|
+
///
|
|
474
|
+
/// This stacks, meaning that if you do `push_opacity(0.5)` twice, the resulting
|
|
475
|
+
/// base opacity will be 0.25.
|
|
476
|
+
pub fn push_opacity(&mut self, opacity: NormalizedF32) {
|
|
477
|
+
self.push_instructions
|
|
478
|
+
.push(PushInstruction::Opacity(opacity));
|
|
479
|
+
|
|
480
|
+
if opacity != NormalizedF32::ONE {
|
|
481
|
+
self.bd
|
|
482
|
+
.sub_builders
|
|
483
|
+
.push(ContentBuilder::new(Transform::identity(), true, self.sc));
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/// Push a new isolated layer.
|
|
488
|
+
pub fn push_isolated(&mut self) {
|
|
489
|
+
self.push_instructions.push(PushInstruction::Isolated);
|
|
490
|
+
self.bd
|
|
491
|
+
.sub_builders
|
|
492
|
+
.push(ContentBuilder::new(Transform::identity(), true, self.sc));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/// Pop the last `push` instruction.
|
|
496
|
+
///
|
|
497
|
+
/// # Panics
|
|
498
|
+
/// Panics if the there wasn't a corresponding `push` to the `pop`.
|
|
499
|
+
pub fn pop(&mut self) {
|
|
500
|
+
match self.push_instructions.pop().unwrap() {
|
|
501
|
+
PushInstruction::Transform => self.bd.get_mut().restore_graphics_state(),
|
|
502
|
+
PushInstruction::Opacity(o) => {
|
|
503
|
+
if o != NormalizedF32::ONE {
|
|
504
|
+
let stream = self.bd.sub_builders.pop().unwrap().finish(self.sc);
|
|
505
|
+
self.bd
|
|
506
|
+
.get_mut()
|
|
507
|
+
.draw_opacified(self.sc, self.chunk_container, o, stream);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
PushInstruction::ClipPath => self.bd.get_mut().pop_clip_path(),
|
|
511
|
+
PushInstruction::BlendMode => self.bd.get_mut().restore_graphics_state(),
|
|
512
|
+
PushInstruction::Mask(mask) => {
|
|
513
|
+
let stream = self.bd.sub_builders.pop().unwrap().finish(self.sc);
|
|
514
|
+
self.bd
|
|
515
|
+
.get_mut()
|
|
516
|
+
.draw_masked(self.sc, self.chunk_container, *mask, stream)
|
|
517
|
+
}
|
|
518
|
+
PushInstruction::Isolated => {
|
|
519
|
+
let stream = self.bd.sub_builders.pop().unwrap().finish(self.sc);
|
|
520
|
+
self.bd
|
|
521
|
+
.get_mut()
|
|
522
|
+
.draw_isolated(self.sc, self.chunk_container, stream);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
#[cfg(feature = "raster-images")]
|
|
528
|
+
/// Draw a new bitmap image.
|
|
529
|
+
pub fn draw_image(&mut self, image: Image, size: Size) {
|
|
530
|
+
self.bd
|
|
531
|
+
.get_mut()
|
|
532
|
+
.draw_image(image, size, self.sc, self.chunk_container);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/// Draw a new graphic.
|
|
536
|
+
///
|
|
537
|
+
/// Drawing the same graphic multiple times is very cheap in terms of
|
|
538
|
+
/// file size.
|
|
539
|
+
pub fn draw_graphic(&mut self, graphic: Graphic) {
|
|
540
|
+
self.bd.get_mut().draw_xobject(
|
|
541
|
+
self.sc,
|
|
542
|
+
self.chunk_container,
|
|
543
|
+
graphic.x_object,
|
|
544
|
+
&ExtGState::new(),
|
|
545
|
+
)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
pub(crate) fn draw_shading(&mut self, shading: &ShadingFunction) {
|
|
549
|
+
self.bd
|
|
550
|
+
.get_mut()
|
|
551
|
+
.draw_shading(shading, self.sc, self.chunk_container);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/// A convenience method for `std::mem::drop`.
|
|
555
|
+
///
|
|
556
|
+
/// # Panics
|
|
557
|
+
/// Panics if the push/pop difference is not 0.
|
|
558
|
+
pub fn finish(self) {}
|
|
559
|
+
|
|
560
|
+
pub(crate) fn draw_opacified_stream(&mut self, opacity: NormalizedF32, stream: Stream) {
|
|
561
|
+
self.bd
|
|
562
|
+
.get_mut()
|
|
563
|
+
.draw_opacified(self.sc, self.chunk_container, opacity, stream)
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/// Return the current transformation matrix of the surface.
|
|
567
|
+
pub fn cur_transform(&self) -> Transform {
|
|
568
|
+
self.bd.get().cur_transform()
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
fn context_color(&self) -> rgb::Color {
|
|
572
|
+
self.fill
|
|
573
|
+
.as_ref()
|
|
574
|
+
.and_then(|f| f.paint.as_rgb())
|
|
575
|
+
.or_else(|| self.stroke.as_ref().and_then(|s| s.paint.as_rgb()))
|
|
576
|
+
.unwrap_or_default()
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
fn has_complex_fill_or_stroke(&self) -> bool {
|
|
580
|
+
// Things can go wrong and yield inconsistent results in different PDF viewers
|
|
581
|
+
// for some fill/stroke combinations. So in some cases, we need to fill and stroke
|
|
582
|
+
// separately.
|
|
583
|
+
// 1) We have an opacity on the stroke, because they render inconsistently across different
|
|
584
|
+
// viewers.
|
|
585
|
+
// 2) We have a linear gradient with opacities in at least one stop, because then we need
|
|
586
|
+
// to invoke a soft mask, in which case the fill/stroke would both be affected by this.
|
|
587
|
+
|
|
588
|
+
let check_paint = |paint: &Paint| match &paint.0 {
|
|
589
|
+
InnerPaint::Color(_) => false,
|
|
590
|
+
InnerPaint::LinearGradient(l) => {
|
|
591
|
+
l.stops.iter().any(|s| s.opacity != NormalizedF32::ONE)
|
|
592
|
+
}
|
|
593
|
+
InnerPaint::RadialGradient(r) => {
|
|
594
|
+
r.stops.iter().any(|s| s.opacity != NormalizedF32::ONE)
|
|
595
|
+
}
|
|
596
|
+
InnerPaint::SweepGradient(r) => r.stops.iter().any(|s| s.opacity != NormalizedF32::ONE),
|
|
597
|
+
InnerPaint::Pattern(_) => false,
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
let complex_stroke = self
|
|
601
|
+
.stroke
|
|
602
|
+
.as_ref()
|
|
603
|
+
.is_some_and(|s| s.opacity != NormalizedF32::ONE || check_paint(&s.paint));
|
|
604
|
+
|
|
605
|
+
let complex_fill = self.fill.as_ref().is_some_and(|f| check_paint(&f.paint));
|
|
606
|
+
|
|
607
|
+
complex_fill || complex_stroke
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
impl Drop for Surface<'_> {
|
|
612
|
+
fn drop(&mut self) {
|
|
613
|
+
let root_builder = std::mem::replace(
|
|
614
|
+
&mut self.bd.root_builder,
|
|
615
|
+
ContentBuilder::new(Transform::identity(), false, self.sc),
|
|
616
|
+
);
|
|
617
|
+
let num_mcids = match self.page_identifier {
|
|
618
|
+
Some(pi) => pi.mcid,
|
|
619
|
+
None => 0,
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
assert!(self.bd.sub_builders.is_empty());
|
|
623
|
+
assert!(self.push_instructions.is_empty());
|
|
624
|
+
assert!(!root_builder.active_marked_content);
|
|
625
|
+
|
|
626
|
+
(self.finish_fn)(root_builder.finish(self.sc), num_mcids)
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/// Holds the different content streams we are currently building. In the usual case,
|
|
631
|
+
/// this only contains the current page stream as the root builder, but the sub builders
|
|
632
|
+
/// will be used if we are for example creating a mask/pattern, or an XObject.
|
|
633
|
+
struct Builders {
|
|
634
|
+
pub(crate) root_builder: ContentBuilder,
|
|
635
|
+
pub(crate) sub_builders: Vec<ContentBuilder>,
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
impl Builders {
|
|
639
|
+
fn new(root_builder: ContentBuilder) -> Self {
|
|
640
|
+
Self {
|
|
641
|
+
root_builder,
|
|
642
|
+
sub_builders: vec![],
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
fn get_mut(&mut self) -> &mut ContentBuilder {
|
|
647
|
+
self.sub_builders
|
|
648
|
+
.last_mut()
|
|
649
|
+
.unwrap_or(&mut self.root_builder)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
fn get(&self) -> &ContentBuilder {
|
|
653
|
+
self.sub_builders.last().unwrap_or(&self.root_builder)
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
pub(crate) enum PushInstruction {
|
|
658
|
+
Transform,
|
|
659
|
+
Opacity(NormalizedF32),
|
|
660
|
+
ClipPath,
|
|
661
|
+
BlendMode,
|
|
662
|
+
Mask(Box<Mask>),
|
|
663
|
+
Isolated,
|
|
664
|
+
}
|