pdfni 0.1.0

Extract tables and Markdown from text-embedded PDFs, with a built-in pure-Rust PDF reader adapted from Mozilla pdf.js.
Documentation
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
//! .bcmap バイナリデコーダ
//!
//! 準拠: pdf.js `binary_cmap.js`

use super::cmap::{CMap, MapValue};

const MAX_NUM_SIZE: usize = 16;
const MAX_ENCODED_NUM_SIZE: usize = 19;

/// デコード失敗
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BcmapError(pub String);

impl std::fmt::Display for BcmapError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::error::Error for BcmapError {}

/// .bcmap ストリーム読み取り
pub(crate) struct BinaryCMapStream<'a> {
    data: &'a [u8],
    pos: usize,
    tmp: [u8; MAX_ENCODED_NUM_SIZE],
}

impl<'a> BinaryCMapStream<'a> {
    pub fn new(data: &'a [u8]) -> Self {
        Self {
            data,
            pos: 0,
            tmp: [0; MAX_ENCODED_NUM_SIZE],
        }
    }

    fn get_byte(&mut self) -> Result<u8, BcmapError> {
        if self.pos >= self.data.len() {
            return Err(BcmapError("unexpected EOF in bcmap".into()));
        }
        let b = self.data[self.pos];
        self.pos += 1;
        Ok(b)
    }

    fn get_bytes(&mut self, n: usize) -> Result<&'a [u8], BcmapError> {
        if self.pos + n > self.data.len() {
            return Err(BcmapError("unexpected EOF in bcmap".into()));
        }
        let s = &self.data[self.pos..self.pos + n];
        self.pos += n;
        Ok(s)
    }

    /// UN: base-128、bit7 継続、bits6-0 データ
    pub fn read_number(&mut self) -> Result<u32, BcmapError> {
        let mut n: u32 = 0;
        loop {
            let b = self.get_byte()?;
            n = (n << 7) | u32::from(b & 0x7f);
            if b & 0x80 == 0 {
                return Ok(n);
            }
        }
    }

    /// SN: zigzag
    pub fn read_signed(&mut self) -> Result<i32, BcmapError> {
        let n = self.read_number()?;
        if n & 1 != 0 {
            Ok(!((n >> 1) as i32))
        } else {
            Ok((n >> 1) as i32)
        }
    }

    /// B[size]: size+1 バイトを固定配列へ
    pub fn read_hex(&mut self, num: &mut [u8], size: usize) -> Result<(), BcmapError> {
        let bytes = self.get_bytes(size + 1)?;
        num[..=size].copy_from_slice(bytes);
        Ok(())
    }

    /// UB[size]: UN 値を size+1 バイト固定幅へ展開
    pub fn read_hex_number(&mut self, num: &mut [u8], size: usize) -> Result<(), BcmapError> {
        let mut sp = 0usize;
        loop {
            let b = self.get_byte()?;
            if sp >= MAX_ENCODED_NUM_SIZE {
                return Err(BcmapError("hex number too long".into()));
            }
            self.tmp[sp] = b & 0x7f;
            sp += 1;
            if b & 0x80 == 0 {
                break;
            }
        }
        let mut buffer: u32 = 0;
        let mut buffer_size: u32 = 0;
        for i in (0..=size).rev() {
            while buffer_size < 8 && sp > 0 {
                sp -= 1;
                buffer |= u32::from(self.tmp[sp]) << buffer_size;
                buffer_size += 7;
            }
            num[i] = (buffer & 255) as u8;
            buffer >>= 8;
            buffer_size = buffer_size.saturating_sub(8);
        }
        Ok(())
    }

    /// SB[size]: 符号付き固定幅(zigzag をバイト列で)
    pub fn read_hex_signed(&mut self, num: &mut [u8], size: usize) -> Result<(), BcmapError> {
        self.read_hex_number(num, size)?;
        let sign: u8 = if num[size] & 1 != 0 { 255 } else { 0 };
        let mut c: u16 = 0;
        for i in 0..=size {
            c = ((c & 1) << 8) | u16::from(num[i]);
            num[i] = ((c >> 1) as u8) ^ sign;
        }
        Ok(())
    }

    /// S: 長さ(UN) + 各文字(UN)
    pub fn read_string(&mut self) -> Result<String, BcmapError> {
        let len = self.read_number()? as usize;
        let mut out = String::with_capacity(len);
        for _ in 0..len {
            let ch = self.read_number()?;
            if let Some(c) = char::from_u32(ch) {
                out.push(c);
            } else {
                return Err(BcmapError(format!("invalid string codepoint {ch}")));
            }
        }
        Ok(out)
    }
}

fn hex_to_int(a: &[u8], size: usize) -> u32 {
    let mut n: u32 = 0;
    for i in 0..=size {
        n = (n << 8) | u32::from(a[i]);
    }
    n
}

fn hex_to_bytes(a: &[u8], size: usize) -> Vec<u8> {
    a[..=size].to_vec()
}

fn add_hex(a: &mut [u8], b: &[u8], size: usize) {
    let mut c: u16 = 0;
    for i in (0..=size).rev() {
        c += u16::from(a[i]) + u16::from(b[i]);
        a[i] = (c & 255) as u8;
        c >>= 8;
    }
}

fn inc_hex(a: &mut [u8], size: usize) {
    let mut c: u16 = 1;
    for i in (0..=size).rev() {
        if c == 0 {
            break;
        }
        c += u16::from(a[i]);
        a[i] = (c & 255) as u8;
        c >>= 8;
    }
}

/// .bcmap をデコードして CMap に書き込む。usecmap 名があれば返す
pub(crate) fn decode_bcmap(data: &[u8], cmap: &mut CMap) -> Result<Option<String>, BcmapError> {
    let mut stream = BinaryCMapStream::new(data);
    let header = stream.get_byte()?;
    cmap.set_vertical((header & 1) != 0);

    let mut use_cmap: Option<String> = None;
    let mut start = [0u8; MAX_NUM_SIZE];
    let mut end = [0u8; MAX_NUM_SIZE];
    let mut ch = [0u8; MAX_NUM_SIZE];
    let mut char_code = [0u8; MAX_NUM_SIZE];
    let mut tmp = [0u8; MAX_NUM_SIZE];

    while let Ok(b) = stream.get_byte() {
        let type_ = b >> 5;
        if type_ == 7 {
            match b & 0x1f {
                0 => {
                    let _ = stream.read_string()?;
                }
                1 => {
                    use_cmap = Some(stream.read_string()?);
                }
                _ => {}
            }
            continue;
        }

        let sequence = (b & 0x10) != 0;
        let data_size = (b & 0x0f) as usize;
        if data_size + 1 > MAX_NUM_SIZE {
            return Err(BcmapError("invalid dataSize".into()));
        }

        let ucs2_data_size = 1usize;
        let subitems_count = stream.read_number()? as usize;

        match type_ {
            0 => {
                // codespacerange
                stream.read_hex(&mut start, data_size)?;
                stream.read_hex_number(&mut end, data_size)?;
                add_hex(&mut end, &start, data_size);
                cmap.add_codespace_range(
                    data_size + 1,
                    hex_to_int(&start, data_size),
                    hex_to_int(&end, data_size),
                );
                for _ in 1..subitems_count {
                    inc_hex(&mut end, data_size);
                    stream.read_hex_number(&mut start, data_size)?;
                    add_hex(&mut start, &end, data_size);
                    stream.read_hex_number(&mut end, data_size)?;
                    add_hex(&mut end, &start, data_size);
                    cmap.add_codespace_range(
                        data_size + 1,
                        hex_to_int(&start, data_size),
                        hex_to_int(&end, data_size),
                    );
                }
            }
            1 => {
                // notdefrange(読み飛ばし)
                stream.read_hex(&mut start, data_size)?;
                stream.read_hex_number(&mut end, data_size)?;
                add_hex(&mut end, &start, data_size);
                let _ = stream.read_number()?;
                for _ in 1..subitems_count {
                    inc_hex(&mut end, data_size);
                    stream.read_hex_number(&mut start, data_size)?;
                    add_hex(&mut start, &end, data_size);
                    stream.read_hex_number(&mut end, data_size)?;
                    add_hex(&mut end, &start, data_size);
                    let _ = stream.read_number()?;
                }
            }
            2 => {
                // cidchar
                stream.read_hex(&mut ch, data_size)?;
                let mut code = stream.read_number()?;
                cmap.map_one(hex_to_int(&ch, data_size), MapValue::Cid(code));
                for _ in 1..subitems_count {
                    inc_hex(&mut ch, data_size);
                    if !sequence {
                        stream.read_hex_number(&mut tmp, data_size)?;
                        add_hex(&mut ch, &tmp, data_size);
                    }
                    // code = signed_delta + (code + 1)
                    let delta = stream.read_signed()?;
                    code = code.wrapping_add(1).wrapping_add_signed(delta);
                    cmap.map_one(hex_to_int(&ch, data_size), MapValue::Cid(code));
                }
            }
            3 => {
                // cidrange
                stream.read_hex(&mut start, data_size)?;
                stream.read_hex_number(&mut end, data_size)?;
                add_hex(&mut end, &start, data_size);
                let mut code = stream.read_number()?;
                cmap.map_cid_range(
                    hex_to_int(&start, data_size),
                    hex_to_int(&end, data_size),
                    code,
                );
                for _ in 1..subitems_count {
                    inc_hex(&mut end, data_size);
                    if !sequence {
                        stream.read_hex_number(&mut start, data_size)?;
                        add_hex(&mut start, &end, data_size);
                    } else {
                        start[..=data_size].copy_from_slice(&end[..=data_size]);
                    }
                    stream.read_hex_number(&mut end, data_size)?;
                    add_hex(&mut end, &start, data_size);
                    code = stream.read_number()?;
                    cmap.map_cid_range(
                        hex_to_int(&start, data_size),
                        hex_to_int(&end, data_size),
                        code,
                    );
                }
            }
            4 => {
                // bfchar(src は 2 バイト固定)
                stream.read_hex(&mut ch, ucs2_data_size)?;
                stream.read_hex(&mut char_code, data_size)?;
                cmap.map_one(
                    hex_to_int(&ch, ucs2_data_size),
                    MapValue::Bytes(hex_to_bytes(&char_code, data_size)),
                );
                for _ in 1..subitems_count {
                    inc_hex(&mut ch, ucs2_data_size);
                    if !sequence {
                        stream.read_hex_number(&mut tmp, ucs2_data_size)?;
                        add_hex(&mut ch, &tmp, ucs2_data_size);
                    }
                    inc_hex(&mut char_code, data_size);
                    stream.read_hex_signed(&mut tmp, data_size)?;
                    add_hex(&mut char_code, &tmp, data_size);
                    cmap.map_one(
                        hex_to_int(&ch, ucs2_data_size),
                        MapValue::Bytes(hex_to_bytes(&char_code, data_size)),
                    );
                }
            }
            5 => {
                // bfrange
                stream.read_hex(&mut start, ucs2_data_size)?;
                stream.read_hex_number(&mut end, ucs2_data_size)?;
                add_hex(&mut end, &start, ucs2_data_size);
                stream.read_hex(&mut char_code, data_size)?;
                cmap.map_bf_range(
                    hex_to_int(&start, ucs2_data_size),
                    hex_to_int(&end, ucs2_data_size),
                    hex_to_bytes(&char_code, data_size),
                );
                for _ in 1..subitems_count {
                    inc_hex(&mut end, ucs2_data_size);
                    if !sequence {
                        stream.read_hex_number(&mut start, ucs2_data_size)?;
                        add_hex(&mut start, &end, ucs2_data_size);
                    } else {
                        start[..=ucs2_data_size].copy_from_slice(&end[..=ucs2_data_size]);
                    }
                    stream.read_hex_number(&mut end, ucs2_data_size)?;
                    add_hex(&mut end, &start, ucs2_data_size);
                    stream.read_hex(&mut char_code, data_size)?;
                    cmap.map_bf_range(
                        hex_to_int(&start, ucs2_data_size),
                        hex_to_int(&end, ucs2_data_size),
                        hex_to_bytes(&char_code, data_size),
                    );
                }
            }
            _ => {
                return Err(BcmapError(format!("unknown bcmap record type: {type_}")));
            }
        }
    }

    Ok(use_cmap)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn un_base128() {
        // 0x818407 → 16903 (0x4207) per cmapscompress README
        let mut s = BinaryCMapStream::new(&[0x81, 0x84, 0x07]);
        assert_eq!(s.read_number().unwrap(), 16903);
        // single byte
        let mut s = BinaryCMapStream::new(&[0x00]);
        assert_eq!(s.read_number().unwrap(), 0);
        let mut s = BinaryCMapStream::new(&[0x7f]);
        assert_eq!(s.read_number().unwrap(), 127);
        let mut s = BinaryCMapStream::new(&[0x81, 0x00]);
        assert_eq!(s.read_number().unwrap(), 128);
    }

    #[test]
    fn sn_zigzag() {
        // encode: n>=0 → 2n, n<0 → -2n-1
        // 0 → 0, 1 → 2, -1 → 1, 2 → 4, -2 → 3
        let mut s = BinaryCMapStream::new(&[0x00]);
        assert_eq!(s.read_signed().unwrap(), 0);
        let mut s = BinaryCMapStream::new(&[0x02]);
        assert_eq!(s.read_signed().unwrap(), 1);
        let mut s = BinaryCMapStream::new(&[0x01]);
        assert_eq!(s.read_signed().unwrap(), -1);
        let mut s = BinaryCMapStream::new(&[0x04]);
        assert_eq!(s.read_signed().unwrap(), 2);
        let mut s = BinaryCMapStream::new(&[0x03]);
        assert_eq!(s.read_signed().unwrap(), -2);
    }

    #[test]
    fn ub_fixed_width() {
        // UN value 1 expanded to 2 bytes (size=1) → [0x00, 0x01]
        // encoding of 1 as UN: 0x01
        let mut s = BinaryCMapStream::new(&[0x01]);
        let mut num = [0u8; 16];
        s.read_hex_number(&mut num, 1).unwrap();
        assert_eq!(num[0], 0x00);
        assert_eq!(num[1], 0x01);
    }

    #[test]
    fn sb_fixed_width() {
        // signed +1 as SB[1]: zigzag encode 1 → 2, as 2-byte UB
        // UN for 2: 0x02 → bytes after zigzag decode
        let mut s = BinaryCMapStream::new(&[0x02]);
        let mut num = [0u8; 16];
        s.read_hex_signed(&mut num, 1).unwrap();
        // zigzag of value whose low bit is 0 means positive; 0x0002 >> 1 = 1
        assert_eq!(hex_to_int(&num, 1), 1);
    }

    #[test]
    fn read_string_primitive() {
        // len=3, chars 'A','B','C' = 65,66,67
        let mut s = BinaryCMapStream::new(&[0x03, 65, 66, 67]);
        assert_eq!(s.read_string().unwrap(), "ABC");
    }
}