pub struct CMap { /* private fields */ }Expand description
A CMap: a byte decoder plus a charcode→CID map (ISO 32000-1 §9.7.5).
Build one with from_encoding_name, predefined or
parse_embedded.
Implementations§
Source§impl CMap
impl CMap
Sourcepub fn decode<'a>(
&'a self,
bytes: &'a [u8],
) -> impl Iterator<Item = (CharCode, Cid)> + 'a
pub fn decode<'a>( &'a self, bytes: &'a [u8], ) -> impl Iterator<Item = (CharCode, Cid)> + 'a
Decode a string into (character code, CID) pairs — the entry point
every text path shares.
Iteration ends when the string is exhausted. Damage never stops it
early: a code no codespace range accepts comes back as CharCode(0),
and a code cut off by the end of the string does too.
use pdfrum_cmap::{CharCode, Cid, from_encoding_name};
use pdfrum_common::Diagnostics;
use pdfrum_object::Name;
let mut diags = Diagnostics::default();
let cmap = from_encoding_name(&Name::from("GB-EUC-H"), &mut diags);
// 0x41 is not a lead byte, so it is a one-byte code; 0xA1 0xA1 is a pair.
let codes: Vec<CharCode> = cmap.decode(&[0x41, 0xA1, 0xA1]).map(|(c, _)| c).collect();
assert_eq!(codes, vec![CharCode(0x41), CharCode(0xA1A1)]);
assert_eq!(cmap.cid(CharCode(0xA1A1)), Cid(0x0060));Sourcepub fn next_char(&self, bytes: &[u8], offset: &mut usize) -> CharCode
pub fn next_char(&self, bytes: &[u8], offset: &mut usize) -> CharCode
Decode one character code, advancing offset.
Returns CharCode(0) rather than failing when the bytes are damaged.
offset may land past a truncated code’s start without reaching the
end of the string, which is how iteration terminates.
Sourcepub fn cid(&self, code: CharCode) -> Cid
pub fn cid(&self, code: CharCode) -> Cid
The CID a character code maps to. Unmapped codes give Cid(0),
which is .notdef.
A CMap that inherits another through usecmap or /UseCMap answers
from its own tables first and asks its parent only where it maps
nothing — the child-wins rule of ISO 32000-1 §9.7.5.3.
Sourcepub fn charcode_from_cid(&self, cid: Cid) -> CharCode
pub fn charcode_from_cid(&self, cid: Cid) -> CharCode
The character code that maps to cid, or CharCode(0)
when none does.
Defined only for a predefined CMap backed by the built-in tables; every
other kind returns 0. Even for those it is partial: the scan does not
consult the four-byte tables, so a CID that exists only above
0x1_0000 is unreachable in reverse. This is used by CID fonts to find
a code for a character they were asked to draw by Unicode.
Sourcepub fn char_size(&self, code: CharCode) -> u8
pub fn char_size(&self, code: CharCode) -> u8
How many bytes a code of this value occupies.
Derived from the value alone, not from the bytes it was decoded from
and not from the codespace ranges. That makes it disagree with
append_char in exactly one case: under a
mixed-two-byte scheme a code below 0x100 whose value is itself a
lead byte reports width 1 but is written as two bytes. Code that
needs the encoded width should measure what append_char produced.
Sourcepub fn count_chars(&self, bytes: &[u8]) -> usize
pub fn count_chars(&self, bytes: &[u8]) -> usize
How many character codes a byte string holds. Always equal to the
number of pairs decode yields for the same string.
Sourcepub fn append_char(&self, out: &mut Vec<u8>, code: CharCode)
pub fn append_char(&self, out: &mut Vec<u8>, code: CharCode)
Append a character code to a byte string in this CMap’s encoding — the
inverse of next_char.
Sourcepub fn is_vertical(&self) -> bool
pub fn is_vertical(&self) -> bool
Whether this CMap selects vertical writing mode (ISO 32000-1 §9.7.4.3).
Decided by the last byte of the name as written, before any suffix
handling, so Identity-V is vertical and so is any name ending in V.
Sourcepub fn is_loaded(&self) -> bool
pub fn is_loaded(&self) -> bool
Whether the name resolved to real tables.
false covers two different failures that behave the same way: a name
matching no row at all, and a name whose row was found but whose
built-in table was not. Either way the CMap still decodes and still
maps codes; it just maps them to themselves.
Sourcepub fn coding_scheme(&self) -> CodingScheme
pub fn coding_scheme(&self) -> CodingScheme
How its bytes split into character codes.
Sourcepub fn has_no_direct_table(&self) -> bool
pub fn has_no_direct_table(&self) -> bool
Whether this CMap has no dense charcode→CID table — true for every predefined CMap and false for every embedded one, whatever the program contained. CID fonts branch on it when choosing how to find a glyph.
Sourcepub fn has_static_map(&self) -> bool
pub fn has_static_map(&self) -> bool
Whether this CMap resolved to one of the built-in static tables.