Skip to content

Commit c541519

Browse files
authored
Allow writing into chunks (#40)
1 parent 5a2df0d commit c541519

12 files changed

Lines changed: 130 additions & 129 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ required-features = ["cli"]
3636

3737
[dependencies]
3838
miniz_oxide = "0.7"
39-
pdf-writer = "0.8"
39+
pdf-writer = "0.9"
4040
usvg = { version = "0.35", default-features = false }
4141
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"], optional = true }
4242
termcolor = { version = "1", optional = true }

src/lib.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Among the unsupported features are currently:
4646
mod render;
4747
mod util;
4848

49-
use pdf_writer::{Content, Filter, Finish, PdfWriter, Rect, Ref, TextStr};
49+
use pdf_writer::{Chunk, Content, Filter, Finish, Pdf, Rect, Ref, TextStr};
5050
use usvg::utils::view_box_to_transform;
5151
use usvg::{Align, AspectRatio, NonZeroRect, Size, Transform, Tree, TreeParsing};
5252

@@ -161,35 +161,35 @@ pub fn convert_str(src: &str, options: Options) -> Result<Vec<u8>, usvg::Error>
161161
pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
162162
let pdf_size = pdf_size(tree, options);
163163
let mut ctx = Context::new(tree, options, None);
164-
let mut writer = PdfWriter::new();
164+
let mut pdf = Pdf::new();
165165

166166
let catalog_ref = ctx.alloc_ref();
167167
let page_tree_ref = ctx.alloc_ref();
168168
let page_ref = ctx.alloc_ref();
169169
let content_ref = ctx.alloc_ref();
170170

171-
writer.catalog(catalog_ref).pages(page_tree_ref);
172-
writer.pages(page_tree_ref).count(1).kids([page_ref]);
171+
pdf.catalog(catalog_ref).pages(page_tree_ref);
172+
pdf.pages(page_tree_ref).count(1).kids([page_ref]);
173173

174174
// Generate main content
175175
ctx.deferrer.push();
176176
let mut content = Content::new();
177177
tree_to_stream(
178178
tree,
179-
&mut writer,
179+
&mut pdf,
180180
&mut content,
181181
&mut ctx,
182182
initial_transform(options.aspect, tree, pdf_size),
183183
);
184184
let content_stream = ctx.finish_content(content);
185-
let mut stream = writer.stream(content_ref, &content_stream);
185+
let mut stream = pdf.stream(content_ref, &content_stream);
186186

187187
if ctx.options.compress {
188188
stream.filter(Filter::FlateDecode);
189189
}
190190
stream.finish();
191191

192-
let mut page = writer.page(page_ref);
192+
let mut page = pdf.page(page_ref);
193193
let mut page_resources = page.resources();
194194
ctx.deferrer.pop(&mut page_resources);
195195
page_resources.finish();
@@ -206,27 +206,27 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
206206
page.finish();
207207

208208
let document_info_id = ctx.alloc_ref();
209-
writer.document_info(document_info_id).producer(TextStr("svg2pdf"));
209+
pdf.document_info(document_info_id).producer(TextStr("svg2pdf"));
210210

211-
writer.finish()
211+
pdf.finish()
212212
}
213213

214214
/// Convert a [`usvg` tree](Tree) into a Form XObject that can be used as
215215
/// part of a larger document.
216216
///
217-
/// This method is intended for use in an existing [`PdfWriter`] workflow. It
218-
/// will always return an XObject with the width and height of one printer's
217+
/// This method is intended for use in an existing [`pdf-writer`] workflow. It
218+
/// will always produce an XObject with the width and height of one printer's
219219
/// point, just like an [`ImageXObject`](pdf_writer::writers::ImageXObject)
220220
/// would.
221221
///
222-
/// The resulting object can be used by registering a name and the `id` with a
223-
/// page's [`/XObject`](pdf_writer::writers::Resources::x_objects) resources
224-
/// dictionary and then invoking the [`Do`](Content::x_object) operator with the
225-
/// name in the page's content stream.
222+
/// The resulting object can be used by registering a name and the `start_ref`
223+
/// with a page's [`/XObject`](pdf_writer::writers::Resources::x_objects)
224+
/// resources dictionary and then invoking the [`Do`](Content::x_object)
225+
/// operator with the name in the page's content stream.
226226
///
227227
/// As the conversion process may need to create multiple indirect objects in
228-
/// the PDF, this function allocates consecutive IDs starting at `id` for its
229-
/// objects and returns the next available ID for your future writing.
228+
/// the PDF, this function allocates consecutive IDs starting at `start_ref` for
229+
/// its objects and returns the next available ID for your future writing.
230230
///
231231
/// ## Example
232232
/// Write a PDF file with some text and an SVG graphic.
@@ -248,12 +248,12 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
248248
/// let svg_name = Name(b"S1");
249249
///
250250
/// // Start writing a PDF.
251-
/// let mut writer = PdfWriter::new();
252-
/// writer.catalog(catalog_id).pages(page_tree_id);
253-
/// writer.pages(page_tree_id).kids([page_id]).count(1);
251+
/// let mut pdf = Pdf::new();
252+
/// pdf.catalog(catalog_id).pages(page_tree_id);
253+
/// pdf.pages(page_tree_id).kids([page_id]).count(1);
254254
///
255255
/// // Set up a simple A4 page.
256-
/// let mut page = writer.page(page_id);
256+
/// let mut page = pdf.page(page_id);
257257
/// page.media_box(Rect::new(0.0, 0.0, 595.0, 842.0));
258258
/// page.parent(page_tree_id);
259259
/// page.contents(content_id);
@@ -267,7 +267,7 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
267267
/// page.finish();
268268
///
269269
/// // Set a predefined font, so we do not have to load anything extra.
270-
/// writer.type1_font(font_id).base_font(Name(b"Helvetica"));
270+
/// pdf.type1_font(font_id).base_font(Name(b"Helvetica"));
271271
///
272272
/// // Let's add an SVG graphic to this file.
273273
/// // We need to load its source first and manually parse it into a usvg Tree.
@@ -280,7 +280,7 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
280280
/// // This call allocates some indirect object reference IDs for itself. If we
281281
/// // wanted to write some more indirect objects afterwards, we could use the
282282
/// // return value as the next unused reference ID.
283-
/// svg2pdf::convert_tree_into(&tree, svg2pdf::Options::default(), &mut writer, svg_id);
283+
/// svg2pdf::convert_tree_into(&tree, svg2pdf::Options::default(), &mut pdf, svg_id);
284284
///
285285
/// // Write a content stream with some text and our SVG.
286286
/// let mut content = Content::new();
@@ -297,14 +297,14 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
297297
/// .x_object(svg_name);
298298
///
299299
/// // Write the file to the disk.
300-
/// writer.stream(content_id, &content.finish());
301-
/// std::fs::write("target/embedded.pdf", writer.finish())?;
300+
/// pdf.stream(content_id, &content.finish());
301+
/// std::fs::write("target/embedded.pdf", pdf.finish())?;
302302
/// # Ok(()) }
303303
/// ```
304304
pub fn convert_tree_into(
305305
tree: &Tree,
306306
options: Options,
307-
writer: &mut PdfWriter,
307+
chunk: &mut Chunk,
308308
start_ref: Ref,
309309
) -> Ref {
310310
let pdf_size = pdf_size(tree, options);
@@ -316,14 +316,14 @@ pub fn convert_tree_into(
316316
let mut content = Content::new();
317317
tree_to_stream(
318318
tree,
319-
writer,
319+
chunk,
320320
&mut content,
321321
&mut ctx,
322322
initial_transform(options.aspect, tree, pdf_size),
323323
);
324324
let content_stream = ctx.finish_content(content);
325325

326-
let mut x_object = writer.form_xobject(x_ref, &content_stream);
326+
let mut x_object = chunk.form_xobject(x_ref, &content_stream);
327327
x_object.bbox(Rect::new(0.0, 0.0, pdf_size.width(), pdf_size.height()));
328328
x_object.matrix([
329329
1.0 / pdf_size.width(),

src/render/clip_path.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::rc::Rc;
22

33
use pdf_writer::types::MaskType;
4-
use pdf_writer::{Content, Filter, Finish, PdfWriter};
4+
use pdf_writer::{Chunk, Content, Filter, Finish};
55
use usvg::tiny_skia_path::PathSegment;
66
use usvg::{ClipPath, FillRule, Node, NodeKind, Transform, Units, Visibility};
77

@@ -14,7 +14,7 @@ use crate::util::helper::{plain_bbox, NameExt, RectExt, TransformExt};
1414
pub fn render(
1515
node: &Node,
1616
clip_path: Rc<ClipPath>,
17-
writer: &mut PdfWriter,
17+
chunk: &mut Chunk,
1818
content: &mut Content,
1919
ctx: &mut Context,
2020
) {
@@ -41,7 +41,7 @@ pub fn render(
4141
create_simple_clip_path(node, clip_path, content);
4242
} else {
4343
content.set_parameters(
44-
create_complex_clip_path(node, clip_path, writer, ctx).to_pdf_name(),
44+
create_complex_clip_path(node, clip_path, chunk, ctx).to_pdf_name(),
4545
);
4646
}
4747
}
@@ -139,7 +139,7 @@ fn extend_segments_from_node(
139139
fn create_complex_clip_path(
140140
parent: &Node,
141141
clip_path: Rc<ClipPath>,
142-
writer: &mut PdfWriter,
142+
chunk: &mut Chunk,
143143
ctx: &mut Context,
144144
) -> Rc<String> {
145145
ctx.deferrer.push();
@@ -149,7 +149,7 @@ fn create_complex_clip_path(
149149
content.save_state();
150150

151151
if let Some(recursive_clip_path) = &clip_path.clip_path {
152-
render(parent, recursive_clip_path.clone(), writer, &mut content, ctx);
152+
render(parent, recursive_clip_path.clone(), chunk, &mut content, ctx);
153153
}
154154

155155
content.transform(clip_path.transform.to_pdf_transform());
@@ -166,7 +166,7 @@ fn create_complex_clip_path(
166166
group::render(
167167
&clip_path.root,
168168
group,
169-
writer,
169+
chunk,
170170
&mut content,
171171
ctx,
172172
Transform::default(),
@@ -178,7 +178,7 @@ fn create_complex_clip_path(
178178
content.restore_state();
179179
let content_stream = ctx.finish_content(content);
180180

181-
let mut x_object = writer.form_xobject(x_object_reference, &content_stream);
181+
let mut x_object = chunk.form_xobject(x_object_reference, &content_stream);
182182

183183
if ctx.options.compress {
184184
x_object.filter(Filter::FlateDecode);
@@ -198,7 +198,7 @@ fn create_complex_clip_path(
198198
x_object.finish();
199199

200200
let gs_ref = ctx.alloc_ref();
201-
let mut gs = writer.ext_graphics(gs_ref);
201+
let mut gs = chunk.ext_graphics(gs_ref);
202202
gs.soft_mask().subtype(MaskType::Alpha).group(x_object_reference);
203203

204204
ctx.deferrer.add_graphics_state(gs_ref)

0 commit comments

Comments
 (0)