pdf-font 1.0.0-beta.1

PDF font handling: CFF/Type1 parsing, CMap parsing, and PostScript scanning.
Documentation
use crate::font::OutlineError;
use alloc::format;
use alloc::string::String;

pub(crate) struct ArgumentsStack<'a> {
    pub data: &'a mut [f32],
    pub len: usize,
    pub max_len: usize,
}

impl<'a> ArgumentsStack<'a> {
    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.len
    }

    #[inline]
    pub(crate) fn is_empty(&self) -> bool {
        self.len == 0
    }

    #[inline]
    pub(crate) fn push(&mut self, n: f32) -> Result<(), OutlineError> {
        if self.len == self.max_len {
            Err(OutlineError::ArgumentsStackLimitReached)
        } else {
            self.data[self.len] = n;
            self.len += 1;
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn at(&self, index: usize) -> f32 {
        self.data[index]
    }

    #[inline]
    pub(crate) fn pop(&mut self) -> f32 {
        if self.is_empty() {
            return 0.0;
        }
        self.len -= 1;
        self.data[self.len]
    }

    pub(crate) fn dump(&self) -> String {
        format!("{:?}", &self.data[0..self.len])
    }

    #[inline]
    pub(crate) fn reverse(&mut self) {
        if self.is_empty() {
            return;
        }

        // Reverse only the actual data and not the whole stack.
        let (first, _) = self.data.split_at_mut(self.len);
        first.reverse();
    }

    #[inline]
    pub(crate) fn clear(&mut self) {
        self.len = 0;
    }

    #[inline]
    pub(crate) fn exch(&mut self) {
        let len = self.len();
        if len < 2 {
            return;
        }
        self.data.swap(len - 1, len - 2);
    }
}

impl core::fmt::Debug for ArgumentsStack<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_list().entries(&self.data[..self.len]).finish()
    }
}