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
//! TXTa/TXTz text layer encoder.
//!
//! Serializes a [`TextLayer`] back into the DjVu binary text chunk format.
//! The encoded data can be embedded as a TXTa chunk (uncompressed) or
//! compressed with BZZ and written as TXTz.
use crate::text::{TextLayer, TextZone, TextZoneKind};
/// Encode a [`TextLayer`] to TXTa binary format (uncompressed).
///
/// The binary format is:
/// - u24be: text length
/// - UTF-8 text bytes
/// - u8: version (0)
/// - zone tree (recursive)
///
/// Coordinates are converted from top-left origin (as stored in `TextZone`)
/// back to DjVu bottom-left origin using `page_height`.
pub fn encode_text_layer(layer: &TextLayer, page_height: u32) -> Vec<u8> {
let text_bytes = layer.text.as_bytes();
let text_len = text_bytes.len();
// Estimate capacity: header + text + version + zones
let mut buf = Vec::with_capacity(text_len + 128);
// u24be text length
write_u24(&mut buf, text_len as u32);
// UTF-8 text
buf.extend_from_slice(text_bytes);
// Version byte
buf.push(0);
// Encode zone tree
if let Some(root) = layer.zones.first() {
encode_zone(&mut buf, root, None, None, &layer.text, page_height);
}
buf
}
/// Encode a zone and its children recursively.
fn encode_zone(
buf: &mut Vec<u8>,
zone: &TextZone,
parent: Option<&ZoneCtx>,
prev: Option<&ZoneCtx>,
full_text: &str,
page_height: u32,
) {
// Type byte
let type_byte = match zone.kind {
TextZoneKind::Page => 1u8,
TextZoneKind::Column => 2,
TextZoneKind::Region => 3,
TextZoneKind::Para => 4,
TextZoneKind::Line => 5,
TextZoneKind::Word => 6,
TextZoneKind::Character => 7,
};
buf.push(type_byte);
// Convert from top-left to bottom-left coordinates
// bl_y = page_height - (tl_y + height)
let abs_x = zone.rect.x as i32;
let abs_y = (page_height as i32).saturating_sub(zone.rect.y as i32 + zone.rect.height as i32);
let width = zone.rect.width as i32;
let height = zone.rect.height as i32;
// Find text_start: byte offset of zone.text within full_text
let text_start = full_text.find(&zone.text).unwrap_or(0) as i32;
let text_len = zone.text.len() as i32;
// Apply inverse delta encoding to match the decoder in text.rs parse_zone.
// Note: the decoder stores parent's text_start/text_len in prev, not the
// sibling's. So dts for siblings = text_start - (parent_ts + parent_tl).
let (dx, dy, dts) = if let Some(prev) = prev {
match type_byte {
1 | 4 | 5 => {
// PAGE, PARAGRAPH, LINE
let dx = abs_x - prev.x;
let dy = prev.y - (abs_y + height);
(dx, dy, text_start - (prev.text_start + prev.text_len))
}
_ => {
// COLUMN, REGION, WORD, CHARACTER
let dx = abs_x - (prev.x + prev.width);
let dy = abs_y - prev.y;
(dx, dy, text_start - (prev.text_start + prev.text_len))
}
}
} else if let Some(parent) = parent {
let dx = abs_x - parent.x;
let dy = parent.y + parent.height - (abs_y + height);
(dx, dy, text_start - parent.text_start)
} else {
(abs_x, abs_y, text_start)
};
// Write 5 biased i16 fields + i24 text_len
write_i16_biased(buf, dx);
write_i16_biased(buf, dy);
write_i16_biased(buf, width);
write_i16_biased(buf, height);
write_i16_biased(buf, dts);
write_i24(buf, text_len);
// Children count (i24)
write_i24(buf, zone.children.len() as i32);
// Context for children
let ctx = ZoneCtx {
x: abs_x,
y: abs_y,
width,
height,
text_start,
text_len,
};
let mut prev_child: Option<ZoneCtx> = None;
for child in &zone.children {
encode_zone(
buf,
child,
Some(&ctx),
prev_child.as_ref(),
full_text,
page_height,
);
let child_bl_y =
(page_height as i32).saturating_sub(child.rect.y as i32 + child.rect.height as i32);
// Match the decoder: prev_child stores the PARENT's text_start/text_len
// (see text.rs parse_zone), not the child's.
prev_child = Some(ZoneCtx {
x: child.rect.x as i32,
y: child_bl_y,
width: child.rect.width as i32,
height: child.rect.height as i32,
text_start,
text_len,
});
}
}
#[derive(Clone)]
struct ZoneCtx {
x: i32,
y: i32,
width: i32,
height: i32,
text_start: i32,
text_len: i32,
}
fn write_u24(buf: &mut Vec<u8>, val: u32) {
buf.push((val >> 16) as u8);
buf.push((val >> 8) as u8);
buf.push(val as u8);
}
fn write_i16_biased(buf: &mut Vec<u8>, val: i32) {
let biased = (val + 0x8000) as u16;
buf.push((biased >> 8) as u8);
buf.push(biased as u8);
}
fn write_i24(buf: &mut Vec<u8>, val: i32) {
let v = val as u32;
buf.push((v >> 16) as u8);
buf.push((v >> 8) as u8);
buf.push(v as u8);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::text::{self, Rect};
#[test]
fn encode_decode_roundtrip() {
let layer = TextLayer {
text: "Hello World".into(),
zones: vec![TextZone {
kind: TextZoneKind::Page,
rect: Rect {
x: 0,
y: 0,
width: 100,
height: 200,
},
text: "Hello World".into(),
children: vec![
TextZone {
kind: TextZoneKind::Word,
rect: Rect {
x: 10,
y: 20,
width: 30,
height: 15,
},
text: "Hello".into(),
children: Vec::new(),
},
TextZone {
kind: TextZoneKind::Word,
rect: Rect {
x: 50,
y: 20,
width: 40,
height: 15,
},
text: "World".into(),
children: Vec::new(),
},
],
}],
};
let page_height = 200;
let encoded = encode_text_layer(&layer, page_height);
// Decode it back
let decoded = text::parse_text_layer(&encoded, page_height).expect("roundtrip decode");
assert_eq!(decoded.text, layer.text);
assert_eq!(decoded.zones.len(), 1);
assert_eq!(decoded.zones[0].children.len(), 2);
assert_eq!(decoded.zones[0].children[0].text, "Hello");
assert_eq!(decoded.zones[0].children[1].text, "World");
}
#[test]
fn encode_all_zone_kinds_roundtrip() {
// Covers Column(2), Region(3), Para(4), Line(5), Character(7) in encode_zone
let layer = TextLayer {
text: "hi".into(),
zones: vec![TextZone {
kind: TextZoneKind::Column,
rect: Rect {
x: 0,
y: 0,
width: 200,
height: 300,
},
text: "hi".into(),
children: vec![TextZone {
kind: TextZoneKind::Region,
rect: Rect {
x: 5,
y: 5,
width: 180,
height: 280,
},
text: "hi".into(),
children: vec![TextZone {
kind: TextZoneKind::Para,
rect: Rect {
x: 5,
y: 5,
width: 180,
height: 30,
},
text: "hi".into(),
children: vec![TextZone {
kind: TextZoneKind::Line,
rect: Rect {
x: 5,
y: 5,
width: 170,
height: 20,
},
text: "hi".into(),
children: vec![TextZone {
kind: TextZoneKind::Character,
rect: Rect {
x: 5,
y: 5,
width: 8,
height: 16,
},
text: "h".into(),
children: vec![],
}],
}],
}],
}],
}],
};
let page_height = 300u32;
let encoded = encode_text_layer(&layer, page_height);
assert!(!encoded.is_empty());
let decoded = crate::text::parse_text_layer(&encoded, page_height)
.expect("all-kinds roundtrip must decode");
assert_eq!(decoded.zones[0].kind, TextZoneKind::Column);
}
#[test]
fn encode_sibling_lines_use_page_para_line_delta_branch() {
// Two sibling Line zones: the second has prev=Some and type_byte=5,
// which hits the `1|4|5` delta branch (lines 82-84).
let layer = TextLayer {
text: "hello world".into(),
zones: vec![TextZone {
kind: TextZoneKind::Page,
rect: Rect {
x: 0,
y: 0,
width: 200,
height: 300,
},
text: "hello world".into(),
children: vec![
TextZone {
kind: TextZoneKind::Line,
rect: Rect {
x: 0,
y: 0,
width: 200,
height: 20,
},
text: "hello".into(),
children: vec![],
},
TextZone {
kind: TextZoneKind::Line,
rect: Rect {
x: 0,
y: 30,
width: 200,
height: 20,
},
text: "world".into(),
children: vec![],
},
],
}],
};
let page_height = 300u32;
let encoded = encode_text_layer(&layer, page_height);
let decoded = crate::text::parse_text_layer(&encoded, page_height)
.expect("sibling lines must roundtrip");
assert_eq!(decoded.zones[0].children.len(), 2);
}
}