Skip to main content

CMap

Struct CMap 

Source
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

Source

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));
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn coding(&self) -> CidCoding

The legacy encoding family this CMap’s codes belong to.

Source

pub fn charset(&self) -> CidSet

The character collection its CIDs index.

Source

pub fn coding_scheme(&self) -> CodingScheme

How its bytes split into character codes.

Source

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.

Source

pub fn has_static_map(&self) -> bool

Whether this CMap resolved to one of the built-in static tables.

Trait Implementations§

Source§

impl Clone for CMap

Source§

fn clone(&self) -> CMap

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for CMap

Source§

impl PartialEq for CMap

Source§

fn eq(&self, other: &CMap) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CMap

Auto Trait Implementations§

§

impl Freeze for CMap

§

impl RefUnwindSafe for CMap

§

impl Send for CMap

§

impl Sync for CMap

§

impl Unpin for CMap

§

impl UnsafeUnpin for CMap

§

impl UnwindSafe for CMap

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.