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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#![allow(dead_code, unused_variables)]
//! Tests for ISO 32000-1:2008 Section 9.3.1 Text Justification & Alignment
//!
//! The PDF specification defines text alignment (justification) through spacing parameters:
//! - Word Spacing (Tw): Adjustment added after space characters (0x20)
//! - Character Spacing (Tc): Adjustment added after every character
//! - Horizontal Scaling (Tz): Scales character widths and spacing
//!
//! Text is justified by distributing extra space across word boundaries (space characters).
//! Alignment detection requires analyzing the distribution of spacing adjustments
//! across a line of text to determine the justification mode.
//!
//! Justification Modes:
//! 1. **Left-Justified**: Consistent spacing, ragged right edge
//! 2. **Right-Justified**: Consistent spacing, ragged left edge
//! 3. **Center-Justified**: Spacing distributed symmetrically
//! 4. **Fully-Justified**: Variable spacing across word boundaries, aligned both edges
//! 5. **Unjustified**: Uniform spacing, text is not justified
//!
//! PDF spec defines text state parameters in Section 9.3:
//! - Tc (character spacing): Default 0
//! - Tw (word spacing): Default 0
//! - Tz (horizontal scaling): Default 100%
//! - Tf (font and size): Defines character width units
//! - TL (text line): Used for vertical positioning
//!
//! # Spec References
//! - ISO 32000-1:2008 Section 9.3: Text State Parameters
//! - ISO 32000-1:2008 Section 9.3.1: Text Positioning
//! - ISO 32000-1:2008 Section 9.2.4: Text Showing Operators (Tj, TJ, etc.)
/// Mock text segment with spacing information for justification analysis
#[derive(Clone, Debug)]
struct TextSegment {
text: String,
x_position: f32,
width: f32,
word_spacing: f32, // Tw parameter applied
char_spacing: f32, // Tc parameter applied
horizontal_scaling: f32, // Tz parameter (100 = 100%)
font_size: f32,
}
/// Mock line of text with all segments
#[derive(Clone, Debug)]
struct TextLine {
segments: Vec<TextSegment>,
line_width: f32, // Physical width on page
start_x: f32,
end_x: f32,
avg_word_spacing: f32,
avg_char_spacing: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum JustificationMode {
LeftJustified,
RightJustified,
CenterJustified,
FullyJustified,
Unjustified,
}
#[test]
fn test_left_justified_text_constant_spacing() {
// Left-justified: consistent word spacing, ragged right edge
let line = TextLine {
segments: vec![
TextSegment {
text: "The".to_string(),
x_position: 100.0,
width: 20.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "quick".to_string(),
x_position: 130.0,
width: 35.0,
word_spacing: 0.0, // Constant spacing
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "brown".to_string(),
x_position: 175.0,
width: 30.0,
word_spacing: 0.0, // Constant spacing
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 105.0,
start_x: 100.0,
end_x: 205.0,
avg_word_spacing: 0.0,
avg_char_spacing: 0.0,
};
// Left-justified text should have uniform word spacing (Tw = 0 or constant)
assert_eq!(line.avg_word_spacing, 0.0, "Left-justified should have constant spacing");
assert!(line.start_x < 150.0, "Should start near left margin");
// Right edge should be ragged (not at fixed position)
}
#[test]
fn test_right_justified_text_ragged_left() {
// Right-justified: text aligned to right edge, ragged left edge
let line = TextLine {
segments: vec![
TextSegment {
text: "fox".to_string(),
x_position: 320.0, // Positioned to align right
width: 15.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "jumps".to_string(),
x_position: 250.0,
width: 25.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "lazy".to_string(),
x_position: 200.0,
width: 18.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 135.0,
start_x: 200.0,
end_x: 335.0,
avg_word_spacing: 0.0,
avg_char_spacing: 0.0,
};
// Right-justified should align to right boundary
let right_edge = line.end_x;
assert!(right_edge > 300.0, "Should be positioned to right margin");
assert!(line.start_x < right_edge - 100.0, "Left edge should be ragged");
}
#[test]
fn test_center_justified_text_symmetric_margins() {
// Center-justified: symmetric margins left and right, balanced spacing
let line = TextLine {
segments: vec![
TextSegment {
text: "This".to_string(),
x_position: 150.0, // Centered position
width: 22.0,
word_spacing: 5.0, // Extra space for centering
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "is".to_string(),
x_position: 192.0,
width: 12.0,
word_spacing: 5.0, // Symmetric spacing
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "centered".to_string(),
x_position: 219.0,
width: 38.0,
word_spacing: 5.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 100.0,
start_x: 200.0, // Centered: (500 - 100) / 2 = 200
end_x: 300.0, // 200 + 100 = 300
avg_word_spacing: 5.0,
avg_char_spacing: 0.0,
};
// Center-justified should have balanced margins
let left_margin = line.start_x - 0.0; // From page edge (assume 0)
let right_margin = 500.0 - line.end_x; // Assume page width 500
assert!((left_margin - right_margin).abs() < 10.0, "Margins should be balanced");
}
#[test]
fn test_fully_justified_text_variable_spacing() {
// Fully-justified: spacing varies across word boundaries for perfect alignment
let line = TextLine {
segments: vec![
TextSegment {
text: "Lorem".to_string(),
x_position: 100.0,
width: 28.0,
word_spacing: 8.0, // First gap is wider
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "ipsum".to_string(),
x_position: 136.0,
width: 30.0,
word_spacing: 5.0, // Second gap adjusted
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "dolor".to_string(),
x_position: 171.0,
width: 28.0,
word_spacing: 6.0, // Third gap adjusted
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 300.0,
start_x: 100.0,
end_x: 400.0,
avg_word_spacing: 6.3,
avg_char_spacing: 0.0,
};
// Fully-justified should have variable word spacing
let word_spacings = [8.0, 5.0, 6.0];
let spacing_variance = word_spacings
.iter()
.map(|w| (w - line.avg_word_spacing).powi(2))
.sum::<f32>()
/ word_spacings.len() as f32;
assert!(spacing_variance > 1.0, "Fully-justified should have spacing variation");
// Should reach both margins
assert!(line.start_x < 150.0, "Should align to left");
assert!(line.end_x > 350.0, "Should align to right");
}
#[test]
fn test_unjustified_text_uniform_spacing() {
// Unjustified: uniform spacing, no attempt to fill line width
let line = TextLine {
segments: vec![
TextSegment {
text: "Regular".to_string(),
x_position: 100.0,
width: 32.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "text".to_string(),
x_position: 140.0,
width: 18.0,
word_spacing: 0.0, // No adjustment
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "here".to_string(),
x_position: 166.0,
width: 21.0,
word_spacing: 0.0, // Consistent zero
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 187.0,
start_x: 100.0,
end_x: 187.0,
avg_word_spacing: 0.0,
avg_char_spacing: 0.0,
};
// Unjustified should have zero word spacing and ragged right edge
assert_eq!(line.avg_word_spacing, 0.0, "Unjustified has no word spacing");
assert!(line.end_x < 400.0, "Unjustified should not fill line width");
}
#[test]
fn test_justification_detection_by_spacing_variance() {
// Detect justification mode by analyzing spacing parameter variance
let fully_justified_spacings = vec![8.0, 5.0, 6.0, 7.5, 5.5];
let left_justified_spacings = vec![0.0, 0.0, 0.0, 0.0, 0.0];
let center_justified_spacings = vec![5.0, 5.0, 5.0, 5.0, 5.0];
// Calculate variance for each
let calc_variance = |spacings: &[f32]| {
let avg = spacings.iter().sum::<f32>() / spacings.len() as f32;
spacings.iter().map(|w| (w - avg).powi(2)).sum::<f32>() / spacings.len() as f32
};
let fully_just_var = calc_variance(&fully_justified_spacings);
let left_just_var = calc_variance(&left_justified_spacings);
let center_just_var = calc_variance(¢er_justified_spacings);
// Fully-justified should have highest variance
assert!(fully_just_var > left_just_var, "Fully-justified variance > left-justified");
assert!(fully_just_var > center_just_var, "Fully-justified variance > center-justified");
// Left-justified should have zero variance (all zeros)
assert_eq!(left_just_var, 0.0, "Left-justified has zero variance");
}
#[test]
fn test_character_spacing_affects_justification() {
// Character spacing (Tc) is applied to every character, affects overall line width
let line_no_char_spacing = TextLine {
segments: vec![TextSegment {
text: "text".to_string(),
x_position: 100.0,
width: 20.0,
word_spacing: 0.0,
char_spacing: 0.0, // No character spacing
horizontal_scaling: 100.0,
font_size: 12.0,
}],
line_width: 20.0,
start_x: 100.0,
end_x: 120.0,
avg_word_spacing: 0.0,
avg_char_spacing: 0.0,
};
let line_with_char_spacing = TextLine {
segments: vec![TextSegment {
text: "text".to_string(),
x_position: 100.0,
width: 24.0, // Width increased by character spacing
word_spacing: 0.0,
char_spacing: 1.0, // 1 unit per character = 4 chars * 1 = +4 units
horizontal_scaling: 100.0,
font_size: 12.0,
}],
line_width: 24.0,
start_x: 100.0,
end_x: 124.0,
avg_word_spacing: 0.0,
avg_char_spacing: 1.0,
};
// Character spacing should expand line width
assert!(
line_with_char_spacing.line_width > line_no_char_spacing.line_width,
"Character spacing should expand width"
);
}
#[test]
fn test_horizontal_scaling_affects_spacing() {
// Horizontal scaling (Tz) affects both character widths and spacing parameters
let line_normal_scale = TextLine {
segments: vec![TextSegment {
text: "word".to_string(),
x_position: 100.0,
width: 20.0,
word_spacing: 5.0,
char_spacing: 0.0,
horizontal_scaling: 100.0, // Normal
font_size: 12.0,
}],
line_width: 25.0,
start_x: 100.0,
end_x: 125.0,
avg_word_spacing: 5.0,
avg_char_spacing: 0.0,
};
let line_scaled_up = TextLine {
segments: vec![TextSegment {
text: "word".to_string(),
x_position: 100.0,
width: 25.0, // 20 * 1.25
word_spacing: 6.25, // 5 * 1.25
char_spacing: 0.0,
horizontal_scaling: 125.0, // Scaled 125%
font_size: 12.0,
}],
line_width: 31.25,
start_x: 100.0,
end_x: 131.25,
avg_word_spacing: 6.25,
avg_char_spacing: 0.0,
};
// Scaling should proportionally increase widths and spacing
let scale_factor = line_scaled_up.segments[0].horizontal_scaling
/ line_normal_scale.segments[0].horizontal_scaling;
assert_eq!(
line_scaled_up.line_width / line_normal_scale.line_width,
scale_factor,
"Scaling should proportionally affect width"
);
}
#[test]
fn test_justified_vs_unjustified_line_fill() {
// Justified text stretches to fill available line width
// Unjustified text may have gaps at the right edge
let page_width = 500.0;
let margin = 50.0;
let available_width = page_width - 2.0 * margin;
let justified_end = margin + available_width; // Should reach right margin
let unjustified_end = margin + 200.0; // May not reach right margin
assert_eq!(justified_end, 450.0, "Justified should reach right margin");
assert!(unjustified_end < 450.0, "Unjustified may leave gap");
}
#[test]
fn test_mixed_justification_detection() {
// Some lines may be left-justified, others fully-justified in same paragraph
let lines = vec![
("left", 0.0, 0.0), // Left-justified line
("full", 6.0, 0.0), // Fully-justified line
("ragg", 0.0, 0.0), // Left-justified line
];
// Detect justification mode per line
for (_text, word_space, _char_space) in lines {
if word_space > 0.0 {
assert_eq!(word_space, 6.0, "Justified line has word spacing");
} else {
assert_eq!(word_space, 0.0, "Non-justified line has no spacing");
}
}
}
#[test]
fn test_spacing_parameters_iso_9_3_defaults() {
// PDF spec default values per Section 9.3
let default_char_spacing = 0.0; // Tc default
let default_word_spacing = 0.0; // Tw default
let default_horizontal_scaling = 100.0; // Tz default
assert_eq!(default_char_spacing, 0.0);
assert_eq!(default_word_spacing, 0.0);
assert_eq!(default_horizontal_scaling, 100.0);
}
#[test]
fn test_word_boundaries_required_for_justification() {
// Justification only works on word boundaries (space characters)
// Without proper word boundary detection, justification analysis is impossible
// This test documents the dependency on word boundary detection
let text_with_spaces = "This is justified";
let words: Vec<&str> = text_with_spaces.split(' ').collect();
let space_count = words.len() - 1;
assert_eq!(space_count, 2, "Text has 2 spaces for 3 words");
// Justification would distribute extra space across these 2 boundaries
}
#[test]
fn test_justification_detection_line_alignment_edges() {
// Detect justification by analyzing line edge alignment
let left_margin = 50.0;
let right_margin = 450.0;
// Left-justified: aligns left, ragged right
let left_just_start = left_margin;
let left_just_end = 380.0; // Doesn't reach right margin
// Right-justified: aligns right, ragged left
let right_just_start = 100.0; // Doesn't align to left
let right_just_end = right_margin;
// Fully-justified: aligns both edges
let fully_just_start = left_margin;
let fully_just_end = right_margin;
// Centered: balanced margins
let center_just_start: f32 = 150.0;
let center_just_end: f32 = 350.0;
let center_left_margin: f32 = center_just_start - 0.0;
let center_right_margin: f32 = 500.0 - center_just_end;
assert!(left_just_start == left_margin, "Left-justified aligns left");
assert!(right_just_end == right_margin, "Right-justified aligns right");
assert!(
fully_just_start == left_margin && fully_just_end == right_margin,
"Fully-justified aligns both"
);
assert!(
(center_left_margin - center_right_margin).abs() < 1.0f32,
"Centered has balanced margins"
);
}
#[test]
fn test_specification_reference_iso_9_3_1() {
// This test documents the spec sections that define text justification
// ISO 32000-1:2008 Section 9.3.1: Text Positioning Operators
// ISO 32000-1:2008 Section 9.3: Text State Parameters
//
// Key parameters for justification:
// - Tc (character spacing)
// - Tw (word spacing)
// - Tz (horizontal scaling)
// - Font size and metrics
let spec_elements = [
"Character Spacing (Tc)",
"Word Spacing (Tw)",
"Horizontal Scaling (Tz)",
"Font Metrics",
"Line Width Analysis",
];
assert_eq!(spec_elements.len(), 5);
assert_eq!(spec_elements[0], "Character Spacing (Tc)");
assert_eq!(spec_elements[1], "Word Spacing (Tw)");
assert_eq!(spec_elements[2], "Horizontal Scaling (Tz)");
}
#[test]
fn test_justification_with_different_font_sizes() {
// Justification spacing is font-size independent
// A 12pt font and 24pt font with same Tw should both be justified equally
let line_12pt = TextLine {
segments: vec![TextSegment {
text: "text".to_string(),
x_position: 100.0,
width: 20.0,
word_spacing: 5.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0, // Smaller font
}],
line_width: 25.0,
start_x: 100.0,
end_x: 125.0,
avg_word_spacing: 5.0,
avg_char_spacing: 0.0,
};
let line_24pt = TextLine {
segments: vec![TextSegment {
text: "text".to_string(),
x_position: 100.0,
width: 40.0, // Double width
word_spacing: 5.0, // Same Tw
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 24.0, // Larger font
}],
line_width: 45.0,
start_x: 100.0,
end_x: 145.0,
avg_word_spacing: 5.0,
avg_char_spacing: 0.0,
};
// Both have same word spacing Tw value
assert_eq!(
line_12pt.avg_word_spacing, line_24pt.avg_word_spacing,
"Justification is font-size independent"
);
}
#[test]
fn test_last_line_of_paragraph_special_handling() {
// Last line of paragraph may have different justification rules
// Often left-justified even if paragraph is fully-justified
let last_line = TextLine {
segments: vec![
TextSegment {
text: "Last".to_string(),
x_position: 100.0,
width: 18.0,
word_spacing: 0.0, // No justification on last line
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
TextSegment {
text: "line".to_string(),
x_position: 126.0,
width: 16.0,
word_spacing: 0.0,
char_spacing: 0.0,
horizontal_scaling: 100.0,
font_size: 12.0,
},
],
line_width: 42.0,
start_x: 100.0,
end_x: 142.0,
avg_word_spacing: 0.0,
avg_char_spacing: 0.0,
};
// Last line should be left-justified (no space expansion)
assert_eq!(last_line.avg_word_spacing, 0.0, "Last line typically not justified");
assert!(last_line.end_x < 450.0, "Last line may not reach right margin");
}