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
// stet-pdf-reader
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! CMap parser for Type 0 (composite) font encoding.
use std::collections::HashMap;
/// Parsed CMap: maps character codes to CIDs.
pub struct CMap {
/// Code-to-CID mapping (character code → CID).
pub code_to_cid: HashMap<u32, u32>,
/// Precomputed first-byte → code length table.
/// 0 = not in any codespace range (treat as 2-byte default).
pub code_lengths: [u8; 256],
/// Writing mode: 0 = horizontal, 1 = vertical.
pub wmode: u8,
}
impl CMap {
/// Create an Identity CMap (code == CID, all 2-byte).
pub fn identity() -> Self {
Self {
code_to_cid: HashMap::new(),
code_lengths: [2; 256],
wmode: 0,
}
}
/// Decode a character code to a CID.
pub fn decode(&self, code: u32) -> u32 {
// Identity mapping: code == CID
self.code_to_cid.get(&code).copied().unwrap_or(code)
}
/// Get the byte width of a character code starting with the given byte.
pub fn code_width(&self, first_byte: u8) -> usize {
let w = self.code_lengths[first_byte as usize];
if w == 0 { 2 } else { w as usize }
}
/// Parse a CMap from stream data.
pub fn parse(data: &[u8]) -> Self {
Self::parse_with_loader(data, None)
}
/// Parse a CMap, optionally resolving `usecmap` with a loader function.
pub fn parse_with_loader(
data: &[u8],
loader: Option<&dyn Fn(&[u8]) -> Option<Vec<u8>>>,
) -> Self {
let mut code_to_cid = HashMap::new();
let mut codespace_ranges: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
let mut wmode: u8 = 0;
let text = String::from_utf8_lossy(data);
#[allow(clippy::while_let_on_iterator)]
let mut lines = text.lines();
while let Some(line) = lines.next() {
let line = line.trim();
// Handle usecmap: inherit mappings from the referenced CMap
if line.ends_with("usecmap") {
let name = line.strip_suffix("usecmap").unwrap_or("").trim();
let name = name.strip_prefix('/').unwrap_or(name);
if !name.is_empty() {
if let Some(load_fn) = loader {
if let Some(base_data) = load_fn(name.as_bytes()) {
let base = Self::parse_with_loader(&base_data, loader);
// Inherit base mappings (current entries override)
for (k, v) in base.code_to_cid {
code_to_cid.entry(k).or_insert(v);
}
if codespace_ranges.is_empty() {
// Inherit codespace from base if not defined yet
for fb in 0..256u16 {
let w = base.code_lengths[fb as usize];
if w > 0 {
let low = if w == 1 {
vec![fb as u8]
} else {
vec![fb as u8, 0x00]
};
let high = if w == 1 {
vec![fb as u8]
} else {
vec![fb as u8, 0xFF]
};
codespace_ranges.push((low, high));
}
}
}
if wmode == 0 {
wmode = base.wmode;
}
}
}
}
}
// Parse /WMode
if let Some(rest) = line.strip_prefix("/WMode") {
let rest = rest.trim();
if let Some(rest) = rest.strip_prefix("def").or(Some(rest)) {
if let Ok(v) = rest.trim().parse::<u8>() {
wmode = v;
}
}
}
// Also handle "N /WMode def" pattern
if line.ends_with("/WMode def") {
let parts: Vec<&str> = line.split_whitespace().collect();
if let Some(v) = parts.first().and_then(|s| s.parse::<u8>().ok()) {
wmode = v;
}
}
// Parse codespace ranges
if line.ends_with("begincodespacerange") {
while let Some(range_line) = lines.next() {
let range_line = range_line.trim();
if range_line == "endcodespacerange" {
break;
}
if let Some((low, high)) = parse_codespace_range(range_line) {
codespace_ranges.push((low, high));
}
}
}
// Parse cidchar mappings: <code> cid
// Handles both multi-line format (data on subsequent lines) and
// inline format where data appears between begincidchar/endcidchar
// on the same line (e.g. "1 begincidchar <0020> 1 endcidchar").
if line.contains("begincidchar") {
// Check for inline format: both begin and end on same line
if let Some(inline) = extract_inline_data(line, "begincidchar", "endcidchar") {
if let Some((code, cid)) = parse_cidchar_line(inline) {
code_to_cid.insert(code, cid);
}
} else if line.ends_with("begincidchar") {
while let Some(char_line) = lines.next() {
let char_line = char_line.trim();
if char_line == "endcidchar" {
break;
}
if let Some((code, cid)) = parse_cidchar_line(char_line) {
code_to_cid.insert(code, cid);
}
}
}
}
// Parse cidrange mappings: <start> <end> cid_start
if line.contains("begincidrange") {
if let Some(inline) = extract_inline_data(line, "begincidrange", "endcidrange") {
if let Some((start, end, cid_start)) = parse_cidrange_line(inline) {
for code in start..=end {
code_to_cid.insert(code, cid_start + (code - start));
}
}
} else if line.ends_with("begincidrange") {
while let Some(range_line) = lines.next() {
let range_line = range_line.trim();
if range_line == "endcidrange" {
break;
}
if let Some((start, end, cid_start)) = parse_cidrange_line(range_line) {
for code in start..=end {
code_to_cid.insert(code, cid_start + (code - start));
}
}
}
}
}
// Also parse bfchar/bfrange (some CMaps use these)
if line.contains("beginbfchar") {
if let Some(inline) = extract_inline_data(line, "beginbfchar", "endbfchar") {
if let Some((code, unicode)) = parse_bfchar_line(inline) {
code_to_cid.insert(code, unicode);
}
} else if line.ends_with("beginbfchar") {
while let Some(char_line) = lines.next() {
let char_line = char_line.trim();
if char_line == "endbfchar" {
break;
}
if let Some((code, unicode)) = parse_bfchar_line(char_line) {
code_to_cid.insert(code, unicode);
}
}
}
}
if line.contains("beginbfrange") {
if let Some(inline) = extract_inline_data(line, "beginbfrange", "endbfrange") {
if let Some((start, end, cid_start)) = parse_cidrange_line(inline) {
for code in start..=end {
code_to_cid.insert(code, cid_start + (code - start));
}
}
} else if line.ends_with("beginbfrange") {
while let Some(range_line) = lines.next() {
let range_line = range_line.trim();
if range_line == "endbfrange" {
break;
}
if let Some((start, end, cid_start)) = parse_cidrange_line(range_line) {
for code in start..=end {
code_to_cid.insert(code, cid_start + (code - start));
}
}
}
}
}
}
// Build first-byte → code-length table from codespace ranges.
// For each first byte, find the shortest matching codespace range.
let mut code_lengths = [0u8; 256];
if codespace_ranges.is_empty() {
// No codespace ranges → default all to 2-byte
code_lengths = [2; 256];
} else {
for (low, high) in &codespace_ranges {
let width = low.len() as u8;
let first_lo = low[0];
let first_hi = high[0];
for byte in first_lo..=first_hi {
let cur = code_lengths[byte as usize];
// Prefer shorter (1-byte over 2-byte) or fill if unset
if cur == 0 || width < cur {
code_lengths[byte as usize] = width;
}
}
}
}
CMap {
code_to_cid,
code_lengths,
wmode,
}
}
}
/// Extract inline data between a begin/end keyword pair on the same line.
/// For example, from `"1 begincidchar <0020> 1 endcidchar"`,
/// returns `Some("<0020> 1")`.
fn extract_inline_data<'a>(line: &'a str, begin_kw: &str, end_kw: &str) -> Option<&'a str> {
let begin_pos = line.find(begin_kw)?;
let end_pos = line.find(end_kw)?;
if end_pos <= begin_pos {
return None;
}
let data_start = begin_pos + begin_kw.len();
if data_start >= end_pos {
return None;
}
let data = line[data_start..end_pos].trim();
if data.is_empty() { None } else { Some(data) }
}
/// Parse a codespace range line like `<20> <20>` or `<0000> <19FF>`.
/// Returns (low_bytes, high_bytes).
fn parse_codespace_range(line: &str) -> Option<(Vec<u8>, Vec<u8>)> {
let tokens = split_cmap_tokens(line);
if tokens.len() >= 2 {
let low = parse_hex_bytes(&tokens[0])?;
let high = parse_hex_bytes(&tokens[1])?;
if low.len() == high.len() && !low.is_empty() {
Some((low, high))
} else {
None
}
} else {
None
}
}
/// Parse a hex string like `<0041>` into raw bytes.
fn parse_hex_bytes(s: &str) -> Option<Vec<u8>> {
let s = s.trim();
if s.starts_with('<') && s.ends_with('>') {
let hex = &s[1..s.len() - 1];
let mut bytes = Vec::new();
let mut i = 0;
while i + 1 < hex.len() {
bytes.push(u8::from_str_radix(&hex[i..i + 2], 16).ok()?);
i += 2;
}
// Odd-length hex: pad last nibble
if i < hex.len() {
bytes.push(u8::from_str_radix(&format!("{}0", &hex[i..]), 16).ok()?);
}
Some(bytes)
} else {
None
}
}
/// Split a CMap line into tokens at `>` boundaries and whitespace.
/// Handles concatenated tokens like `<e0>151` or `<20><5b>1`.
fn split_cmap_tokens(line: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut i = 0;
let bytes = line.as_bytes();
while i < bytes.len() {
// Skip whitespace
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i >= bytes.len() {
break;
}
if bytes[i] == b'<' {
// Hex token: consume until '>'
let start = i;
while i < bytes.len() && bytes[i] != b'>' {
i += 1;
}
if i < bytes.len() {
i += 1; // consume '>'
}
tokens.push(line[start..i].to_string());
} else {
// Non-hex token: consume until whitespace or '<'
let start = i;
while i < bytes.len() && !bytes[i].is_ascii_whitespace() && bytes[i] != b'<' {
i += 1;
}
tokens.push(line[start..i].to_string());
}
}
tokens
}
/// Parse a hex string like `<0041>` into a u32.
fn parse_hex(s: &str) -> Option<u32> {
let s = s.trim();
if s.starts_with('<') && s.ends_with('>') {
u32::from_str_radix(&s[1..s.len() - 1], 16).ok()
} else {
None
}
}
/// Parse a cidchar line: `<code> cid` or `<code>cid` (no space).
fn parse_cidchar_line(line: &str) -> Option<(u32, u32)> {
let tokens = split_cmap_tokens(line);
if tokens.len() >= 2 {
let code = parse_hex(&tokens[0])?;
let cid = tokens[1].parse::<u32>().ok()?;
Some((code, cid))
} else {
None
}
}
/// Parse a cidrange line: `<start> <end> cid_start` or `<start><end>cid_start`.
fn parse_cidrange_line(line: &str) -> Option<(u32, u32, u32)> {
let tokens = split_cmap_tokens(line);
if tokens.len() >= 3 {
let start = parse_hex(&tokens[0])?;
let end = parse_hex(&tokens[1])?;
let cid_start = if tokens[2].starts_with('<') {
parse_hex(&tokens[2])?
} else {
tokens[2].parse::<u32>().ok()?
};
Some((start, end, cid_start))
} else {
None
}
}
/// Parse a bfchar line: `<code> <unicode>` or `<code><unicode>`.
fn parse_bfchar_line(line: &str) -> Option<(u32, u32)> {
let tokens = split_cmap_tokens(line);
if tokens.len() >= 2 {
let code = parse_hex(&tokens[0])?;
let unicode = parse_hex(&tokens[1])?;
Some((code, unicode))
} else {
None
}
}