Skip to main content

LazyCMap

Struct LazyCMap 

Source
pub struct LazyCMap { /* private fields */ }
Expand description

Lazy-loaded ToUnicode CMap wrapper.

Defers parsing of ToUnicode CMap streams until first character lookup, improving performance during initial font loading. After first access, the parsed CMap is cached and reused for subsequent lookups.

§Two-Level Caching

  • Local cache (parsed): Caches result in this LazyCMap instance
  • Global cache: Deduplicates identical CMaps across fonts (Phase 5.2)

§Design

  • raw_stream: Stores unparsed CMap stream bytes
  • cache_key: Hash of stream bytes for global cache lookup
  • parsed: Mutex-protected optional Arc of parsed CMap
    • Arc: Thread-safe sharing of the parsed result
    • Mutex: Thread-safe mutable access to the Option
    • Option: Tracks whether parsing has occurred

§Thread Safety

Multiple threads can safely call get() concurrently:

  • Parse happens once, even with concurrent access
  • Cached result is shared via Arc<CMap> globally
  • Mutex ensures atomic updates to cached state

§Performance Impact

  • Font creation: 30-40% faster (skips CMap parsing)
  • First lookup: Slightly slower (parse + store cost, amortized across fonts)
  • Subsequent lookups: Same speed (cached result)
  • Multi-font documents: Significant improvement (50-70% for repeated fonts)
  • Global cache: Deduplicates identical CMaps across fonts

Implementations§

Source§

impl LazyCMap

Source

pub fn new(raw_stream: Vec<u8>) -> Self

Create a new lazy CMap from raw stream bytes.

§Arguments
  • raw_stream - Unparsed CMap stream bytes
§Returns

A new LazyCMap that will parse on first access via get()

§Performance

This is O(n) where n is the size of raw_stream (for hashing). Parsing is deferred until first call to get().

Source

pub fn raw_data(&self) -> &[u8]

Get a reference to the parsed CMap.

On first call, checks global cache, then parses if needed. On subsequent calls, returns the cached Arc<CMap>.

§Caching Strategy
  1. Check local parsed cache (fastest, no lock contention)
  2. Check global CMAP_CACHE (fast, shared across fonts)
  3. Parse and populate both caches on miss
§Returns

Some(Arc<CMap>) if parsing succeeded, None if parsing failed or stream was empty Get the raw CMap stream bytes.

Source

pub fn code_width(&self) -> u8

Return the character code width (1 or 2) declared by begincodespacerange.

Parses and caches the CMap if not already done. Returns 1 when the CMap is missing or unparseable (safe default for simple fonts). Returns 2 when the codespace declares 2-byte codes, indicating a CJK composite font whose content stream must be read two bytes at a time.

Source

pub fn wmode(&self) -> u8

Return the writing mode declared by the underlying CMap stream.

Parses and caches the CMap if not already done. Returns 0 (horizontal) when the CMap is missing, unparseable, or does not contain an explicit /WMode directive — matching the spec default. Returns 1 when the CMap declares /WMode 1 def (vertical writing).

Source

pub fn get(&self) -> Option<Arc<CMap>>

Returns the parsed CMap, loading and caching it on first access.

Trait Implementations§

Source§

impl Clone for LazyCMap

Source§

fn clone(&self) -> LazyCMap

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 LazyCMap

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

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.
Source§

impl<T> Ungil for T
where T: Send,