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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! PDF page operations
//!
//! Implements rendering operations on individual PDF pages.
use fop_types::Length;
use crate::pdf::font::FontManager;
use super::types::{LinkAnnotation, LinkDestination, PdfPage};
/// Build PDF character spacing (Tc) and word spacing (Tw) operator strings.
///
/// Returns a string with the appropriate operators for inclusion in a BT...ET block.
/// Non-zero spacing values produce "Tc" or "Tw" lines; zero values are omitted.
pub(super) fn build_spacing_ops(
letter_spacing: Option<Length>,
word_spacing: Option<Length>,
) -> String {
let mut ops = String::new();
if let Some(ls) = letter_spacing {
let pt = ls.to_pt();
if pt.abs() > 0.0001 {
ops.push_str(&format!(
"{:.4} Tc
",
pt
));
}
}
if let Some(ws) = word_spacing {
let pt = ws.to_pt();
if pt.abs() > 0.0001 {
ops.push_str(&format!(
"{:.4} Tw
",
pt
));
}
}
ops
}
impl PdfPage {
/// Create a new PDF page
pub fn new(width: Length, height: Length) -> Self {
Self {
width,
height,
content: Vec::new(),
link_annotations: Vec::new(),
}
}
/// Add a link annotation to the page
///
/// # Arguments
/// * `x` - X position (PDF coordinates: bottom-left origin)
/// * `y` - Y position (PDF coordinates: bottom-left origin)
/// * `width` - Width of the clickable area
/// * `height` - Height of the clickable area
/// * `destination` - Link destination (external URL or internal ID)
pub fn add_link_annotation(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
destination: LinkDestination,
) {
let rect = [
x.to_pt(),
y.to_pt(),
(x + width).to_pt(),
(y + height).to_pt(),
];
self.link_annotations
.push(LinkAnnotation { rect, destination });
}
/// Encode text for PDF output using UTF-16BE for CID fonts
/// Uses UTF-16BE hex strings WITHOUT BOM (matches Java FOP StandardCharsets.UTF_16BE)
fn encode_pdf_text(text: &str) -> String {
// For CID fonts (Type 0), we use UTF-16BE encoding
// Java FOP: text.getBytes(StandardCharsets.UTF_16BE) - NO BOM!
// BOM should only be in ToUnicode CMap, not in content streams
let mut result = String::from("<");
// Encode each character as UTF-16BE (without BOM)
for c in text.chars() {
let code = c as u32;
if code <= 0xFFFF {
// BMP character (Basic Multilingual Plane)
result.push_str(&format!("{:04X}", code));
} else {
// Surrogate pair for non-BMP characters (above U+FFFF)
let code = code - 0x10000;
let high = 0xD800 + (code >> 10);
let low = 0xDC00 + (code & 0x3FF);
result.push_str(&format!("{:04X}{:04X}", high, low));
}
}
result.push('>');
result
}
/// Add text to the page using the default Helvetica font (F1)
pub fn add_text(&mut self, text: &str, x: Length, y: Length, font_size: Length) {
self.add_text_with_spacing(text, x, y, font_size, None, None);
}
/// Add text to the page using the default Helvetica font (F1) with optional letter/word spacing
///
/// # Arguments
/// * `letter_spacing` - Optional character spacing in points (Tc operator)
/// * `word_spacing` - Optional word spacing in points (Tw operator)
pub fn add_text_with_spacing(
&mut self,
text: &str,
x: Length,
y: Length,
font_size: Length,
letter_spacing: Option<Length>,
word_spacing: Option<Length>,
) {
// Build optional Tc/Tw spacing operators
let spacing_ops = build_spacing_ops(letter_spacing, word_spacing);
let ops = format!(
"BT\n/F1 {} Tf\n{}{} {} Td\n({}) Tj\nET\n",
font_size.to_pt(),
spacing_ops,
x.to_pt(),
y.to_pt(),
text
);
self.content.extend_from_slice(ops.as_bytes());
}
/// Add text to the page using a custom embedded font
///
/// # Arguments
/// * `text` - The text to display
/// * `x` - X position
/// * `y` - Y position
/// * `font_size` - Font size
/// * `font_index` - Index of the embedded font (from `embed_font`)
///
/// Note: Character usage tracking must be done separately via FontManager::record_text
pub fn add_text_with_font(
&mut self,
text: &str,
x: Length,
y: Length,
font_size: Length,
font_index: usize,
) {
self.add_text_with_font_and_spacing(text, x, y, font_size, font_index, None, None);
}
/// Add text to the page using a custom embedded font with optional letter/word spacing
///
/// # Arguments
/// * `text` - The text to display
/// * `x` - X position
/// * `y` - Y position
/// * `font_size` - Font size
/// * `font_index` - Index of the embedded font (from `embed_font`)
/// * `letter_spacing` - Optional character spacing in points (Tc operator)
/// * `word_spacing` - Optional word spacing in points (Tw operator)
#[allow(clippy::too_many_arguments)]
pub fn add_text_with_font_and_spacing(
&mut self,
text: &str,
x: Length,
y: Length,
font_size: Length,
font_index: usize,
letter_spacing: Option<Length>,
word_spacing: Option<Length>,
) {
// Custom fonts are F2, F3, F4, etc. (F1 is reserved for Helvetica)
let font_name = format!("F{}", font_index + 2);
// Encode text for PDF - use hex strings for Unicode characters
let encoded_text = Self::encode_pdf_text(text);
// Build optional Tc/Tw spacing operators
let spacing_ops = build_spacing_ops(letter_spacing, word_spacing);
let ops = format!(
"BT\n/{} {} Tf\n{}{} {} Td\n{} Tj\nET\n",
font_name,
font_size.to_pt(),
spacing_ops,
x.to_pt(),
y.to_pt(),
encoded_text
);
self.content.extend_from_slice(ops.as_bytes());
}
/// Add text to the page using a custom embedded font and track character usage
///
/// This is a convenience method that both adds the text and records character usage
/// for subsetting.
///
/// # Arguments
/// * `text` - The text to display
/// * `x` - X position
/// * `y` - Y position
/// * `font_size` - Font size
/// * `font_index` - Index of the embedded font (from `embed_font`)
/// * `font_manager` - FontManager to record character usage
pub fn add_text_with_font_tracked(
&mut self,
text: &str,
x: Length,
y: Length,
font_size: Length,
font_index: usize,
font_manager: &mut FontManager,
) {
// Record character usage for subsetting
font_manager.record_text(font_index, text);
// Add the text to the page
self.add_text_with_font(text, x, y, font_size, font_index);
}
/// Add background color to an area
pub fn add_background(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
color: fop_types::Color,
) {
self.add_background_with_radius(x, y, width, height, color, None);
}
/// Add background color to an area with optional rounded corners
pub fn add_background_with_radius(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
color: fop_types::Color,
border_radius: Option<[Length; 4]>,
) {
use crate::pdf::graphics::PdfGraphics;
let mut graphics = PdfGraphics::new();
let _ = graphics.set_fill_color(color);
let _ = graphics.fill_rectangle_with_radius(x, y, width, height, border_radius);
self.content
.extend_from_slice(graphics.content().as_bytes());
}
/// Add background color with opacity to an area with optional rounded corners
///
/// # Arguments
/// * `x, y` - Bottom-left corner of the area (PDF coordinates)
/// * `width, height` - Dimensions of the area
/// * `color` - Fill color
/// * `border_radius` - Optional corner radii
/// * `gs_index` - Index of the ExtGState resource for opacity
#[allow(clippy::too_many_arguments)]
pub fn add_background_with_opacity(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
color: fop_types::Color,
border_radius: Option<[Length; 4]>,
gs_index: usize,
) {
use crate::pdf::graphics::PdfGraphics;
let mut graphics = PdfGraphics::new();
let _ = graphics.set_opacity(&format!("GS{}", gs_index));
let _ = graphics.set_fill_color(color);
let _ = graphics.fill_rectangle_with_radius(x, y, width, height, border_radius);
self.content
.extend_from_slice(graphics.content().as_bytes());
}
/// Add gradient background to an area
///
/// # Arguments
/// * `x, y` - Bottom-left corner of the area (PDF coordinates)
/// * `width, height` - Dimensions of the area
/// * `gradient_index` - Index of the gradient in the document's gradient list
pub fn add_gradient_background(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
gradient_index: usize,
) {
self.add_gradient_background_with_radius(x, y, width, height, gradient_index, None);
}
/// Add gradient background to an area with optional rounded corners
///
/// # Arguments
/// * `x, y` - Bottom-left corner of the area (PDF coordinates)
/// * `width, height` - Dimensions of the area
/// * `gradient_index` - Index of the gradient in the document's gradient list
/// * `border_radius` - Optional corner radii [top-left, top-right, bottom-right, bottom-left]
pub fn add_gradient_background_with_radius(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
gradient_index: usize,
border_radius: Option<[Length; 4]>,
) {
use crate::pdf::graphics::PdfGraphics;
let mut graphics = PdfGraphics::new();
let _ =
graphics.fill_gradient_with_radius(x, y, width, height, gradient_index, border_radius);
self.content
.extend_from_slice(graphics.content().as_bytes());
}
/// Add borders to an area
#[allow(clippy::too_many_arguments)]
pub fn add_borders(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
border_widths: [Length; 4],
border_colors: [fop_types::Color; 4],
border_styles: [fop_layout::area::BorderStyle; 4],
) {
self.add_borders_with_radius(
x,
y,
width,
height,
border_widths,
border_colors,
border_styles,
None,
);
}
/// Add borders to an area with optional rounded corners
#[allow(clippy::too_many_arguments)]
pub fn add_borders_with_radius(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
border_widths: [Length; 4],
border_colors: [fop_types::Color; 4],
border_styles: [fop_layout::area::BorderStyle; 4],
border_radius: Option<[Length; 4]>,
) {
use crate::pdf::graphics::PdfGraphics;
let mut graphics = PdfGraphics::new();
let _ = graphics.draw_borders_with_radius(
x,
y,
width,
height,
border_widths,
border_colors,
border_styles,
border_radius,
);
self.content
.extend_from_slice(graphics.content().as_bytes());
}
/// Add borders with opacity to an area with optional rounded corners
#[allow(clippy::too_many_arguments)]
pub fn add_borders_with_opacity(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
border_widths: [Length; 4],
border_colors: [fop_types::Color; 4],
border_styles: [fop_layout::area::BorderStyle; 4],
border_radius: Option<[Length; 4]>,
gs_index: usize,
) {
use crate::pdf::graphics::PdfGraphics;
let mut graphics = PdfGraphics::new();
let _ = graphics.set_stroke_opacity(&format!("GS{}", gs_index));
let _ = graphics.draw_borders_with_radius(
x,
y,
width,
height,
border_widths,
border_colors,
border_styles,
border_radius,
);
self.content
.extend_from_slice(graphics.content().as_bytes());
}
/// Add an image to the page
///
/// # Arguments
/// * `image_index` - The index of the image XObject in the document's image_xobjects list
/// * `x` - X position in PDF coordinates (bottom-left origin)
/// * `y` - Y position in PDF coordinates (bottom-left origin)
/// * `width` - Display width
/// * `height` - Display height
pub fn add_image(
&mut self,
image_index: usize,
x: Length,
y: Length,
width: Length,
height: Length,
) {
use std::fmt::Write;
let mut ops = String::new();
// Save graphics state
let _ = writeln!(&mut ops, "q");
// Set up transformation matrix: translate, then scale
// PDF images are 1x1 unit square by default, so we scale to width/height
let _ = writeln!(
&mut ops,
"{:.3} 0 0 {:.3} {:.3} {:.3} cm",
width.to_pt(),
height.to_pt(),
x.to_pt(),
y.to_pt()
);
// Draw the image
let _ = writeln!(&mut ops, "/Im{} Do", image_index);
// Restore graphics state
let _ = writeln!(&mut ops, "Q");
self.content.extend_from_slice(ops.as_bytes());
}
/// Add a horizontal rule (line) to the page
///
/// # Arguments
/// * `x` - Left edge x-coordinate
/// * `y` - Bottom edge y-coordinate (PDF coordinate system)
/// * `width` - Rule width
/// * `thickness` - Line thickness
/// * `color` - Line color
/// * `style` - Line style (solid, dashed, dotted)
pub fn add_rule(
&mut self,
x: Length,
y: Length,
width: Length,
thickness: Length,
color: fop_types::Color,
style: &str,
) {
use std::fmt::Write;
let mut ops = String::new();
// Set stroke color
let _ = writeln!(
&mut ops,
"{:.3} {:.3} {:.3} RG",
color.r_f32(),
color.g_f32(),
color.b_f32()
);
// Set line width
let _ = writeln!(&mut ops, "{:.3} w", thickness.to_pt());
// Set dash pattern based on style
match style {
"dashed" => {
let _ = writeln!(&mut ops, "[6 3] 0 d");
}
"dotted" => {
let _ = writeln!(&mut ops, "[1 2] 0 d");
}
_ => {
// solid or unknown - use solid line
let _ = writeln!(&mut ops, "[] 0 d");
}
}
// Draw the line (move to start, line to end, stroke)
let _ = writeln!(
&mut ops,
"{:.3} {:.3} m {:.3} {:.3} l S",
x.to_pt(),
y.to_pt(),
(x + width).to_pt(),
y.to_pt()
);
self.content.extend_from_slice(ops.as_bytes());
}
/// Save graphics state and set clipping path
///
/// This method saves the current graphics state and establishes a rectangular
/// clipping path. Content drawn after this call will be clipped to the specified
/// rectangle until restore_clip_state() is called.
///
/// PDF operators used:
/// - q: Save graphics state
/// - re: Rectangle path
/// - W: Set clipping path (intersect with current path)
/// - n: End path without stroking or filling
///
/// # Arguments
/// * `x, y` - Bottom-left corner of clipping rectangle (PDF coordinates)
/// * `width, height` - Dimensions of clipping rectangle
///
/// # PDF Reference
/// See PDF specification section 8.5 for clipping path details.
pub fn save_clip_state(
&mut self,
x: Length,
y: Length,
width: Length,
height: Length,
) -> fop_types::Result<()> {
use std::fmt::Write;
let mut ops = String::new();
// Save graphics state
writeln!(&mut ops, "q").map_err(|e| fop_types::FopError::Generic(e.to_string()))?;
// Define rectangle path and set as clipping path
writeln!(
&mut ops,
"{:.3} {:.3} {:.3} {:.3} re W n",
x.to_pt(),
y.to_pt(),
width.to_pt(),
height.to_pt()
)
.map_err(|e| fop_types::FopError::Generic(e.to_string()))?;
self.content.extend_from_slice(ops.as_bytes());
Ok(())
}
/// Restore graphics state after clipping
///
/// This restores the graphics state that was saved by save_clip_state(),
/// removing the clipping path.
///
/// PDF operator used:
/// - Q: Restore graphics state
pub fn restore_clip_state(&mut self) -> fop_types::Result<()> {
use std::fmt::Write;
let mut ops = String::new();
writeln!(&mut ops, "Q").map_err(|e| fop_types::FopError::Generic(e.to_string()))?;
self.content.extend_from_slice(ops.as_bytes());
Ok(())
}
}