Skip to content

Commit 63b134a

Browse files
committed
feat: added support for blocks, by using reusable instances
1 parent 90e26b5 commit 63b134a

20 files changed

Lines changed: 955 additions & 5 deletions

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ A high-level PDF manipulation library built on [lopdf](https://github.com/j-f-li
1111
- **Layer Management**: High-level API for organizing content into toggleable layers
1212
- **Hatching Patterns**: Support for various fill patterns including crosshatching, dots, and custom patterns
1313
- **PDF Embedding**: Embed other PDF documents with various layout strategies
14+
- **Block System**: Reusable PDF content components with transformations and efficient rendering
1415
- **Type Safety**: Strongly typed interfaces with compile-time guarantees
1516

17+
# Showcase
18+
You can see the results of our test suit in [this](https://github.com/ducflair/hipdf/tree/main/tests/outputs) folder.
1619

1720

1821
## Quick Start
@@ -21,7 +24,7 @@ A high-level PDF manipulation library built on [lopdf](https://github.com/j-f-li
2124

2225
```rust
2326
use hipdf::ocg::{OCGManager, Layer, LayerContentBuilder, LayerOperations as Ops};
24-
use lopdf::{Document, Object};
27+
use hipdf::lopdf::{Document, Object};
2528

2629
// Create a new PDF with layers
2730
let mut doc = Document::with_version("1.5");
@@ -80,11 +83,39 @@ let options = EmbedOptions {
8083
embedder.embed_pdf(&mut target_doc, "doc1", &options)?;
8184
```
8285

86+
### Creating Reusable Blocks
87+
88+
```rust
89+
use hipdf::blocks::{BlockManager, Block, BlockInstance, Transform};
90+
use hipdf::lopdf::content::Operation;
91+
92+
// Create a block manager
93+
let mut manager = BlockManager::new();
94+
95+
// Create a reusable block
96+
let mut block = Block::new("my_shape", vec![
97+
Operation::new("re", vec![0.into(), 0.into(), 100.into(), 50.into()]),
98+
Operation::new("f", vec![]),
99+
]);
100+
manager.register(block);
101+
102+
// Create instances with different transformations
103+
let instances = vec![
104+
BlockInstance::at("my_shape", 50.0, 100.0),
105+
BlockInstance::at_scaled("my_shape", 200.0, 100.0, 0.5),
106+
BlockInstance::new("my_shape", Transform::full(350.0, 100.0, 1.5, 1.5, 45.0)),
107+
];
108+
109+
// Render all instances
110+
let operations = manager.render_instances(&instances);
111+
```
112+
83113
## Modules
84114

85115
- [`ocg`] - Optional Content Groups (layers) functionality
86116
- [`hatching`] - Hatching and pattern support for PDF documents
87117
- [`embed_pdf`] - PDF embedding and composition support
118+
- [`blocks`] - Reusable PDF content components with transformations
88119

89120
## Usage Examples
90121

@@ -141,10 +172,33 @@ layout_builder
141172
let final_doc = layout_builder.build()?;
142173
```
143174

175+
### Block System
176+
177+
```rust
178+
use hipdf::blocks::{BlockManager, Block, BlockInstance, Transform};
179+
180+
// Create block manager and register reusable content
181+
let mut manager = BlockManager::new();
182+
let star_block = Block::new("star", vec![
183+
// Operations to draw a star shape
184+
]).with_bbox(0.0, 0.0, 50.0, 50.0);
185+
manager.register(star_block);
186+
187+
// Create multiple instances with different positions and transformations
188+
let instances = vec![
189+
BlockInstance::at("star", 100.0, 200.0),
190+
BlockInstance::new("star", Transform::translate_scale(200.0, 200.0, 1.5)),
191+
BlockInstance::new("star", Transform::full(300.0, 200.0, 0.8, 0.8, 30.0)),
192+
];
193+
194+
// Render instances efficiently using Form XObjects
195+
manager.create_xobjects(&mut doc);
196+
let operations = manager.render_instances_as_xobjects(&instances, &mut resources);
197+
```
198+
144199
## Requirements
145200

146201
- Rust 1.70+
147-
- [lopdf](https://crates.io/crates/lopdf) 0.38.0
148202

149203
## License
150204

0 commit comments

Comments
 (0)