FontData

Struct FontData 

Source
pub struct FontData {
    rt_font: Font<'static>,
    raw_data: RawFontData,
}
Expand description

The data for a font that is cached by a FontCache.

Fields§

§rt_font: Font<'static>§raw_data: RawFontData

Implementations§

Source§

impl FontData

Source

pub fn new( data: Vec<u8>, builtin: Option<BuiltinFont>, ) -> Result<FontData, Error>

Loads a font from the given data.

The provided data must by readable by rusttype. If builtin is set, a built-in PDF font is used instead of embedding the font in the PDF file (see the module documentation for more information). In this case, the given font must be metrically identical to the built-in font.

§Example
use genpdfi_extended::fonts::{FontData, FontFamily, FontCache};
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let fd = FontData::new(data, None).expect("font data");
let family = FontFamily { regular: fd.clone(), bold: fd.clone(), italic: fd.clone(), bold_italic: fd.clone() };
let cache = FontCache::new(family);
let _default = cache.default_font_family();
Source

pub fn new_shared( shared_data: Arc<Vec<u8>>, builtin: Option<BuiltinFont>, ) -> Result<FontData, Error>

Creates a new FontData instance that shares the same underlying font data. This method is optimized to avoid duplicating font data when creating multiple FontData instances from the same source. Creates a new FontData instance that shares the same underlying font data. This method is optimized to avoid duplicating font data when creating multiple FontData instances from the same source.

§Examples
use std::sync::Arc;
use genpdfi_extended::fonts::FontData;
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let shared = Arc::new(data);
let fd = FontData::new_shared(shared.clone(), None).expect("font data");
Source

pub fn load( path: impl AsRef<Path>, builtin: Option<BuiltinFont>, ) -> Result<FontData, Error>

Loads the font at the given path.

The path must point to a file that can be read by rusttype. If builtin is set, a built-in PDF font is used instead of embedding the font in the PDF file (see the module documentation for more information). In this case, the given font must be metrically identical to the built-in font.

Source

pub fn get_data(&self) -> Result<&[u8], Error>

Gets the raw font data bytes (for embedded fonts only).

§Returns
  • Ok(&[u8]) - The raw font bytes for embedded fonts
  • Err(Error) - If this is a built-in font (which has no raw data to extract)
Source

pub fn has_glyph(&self, c: char) -> bool

Checks if this font has a glyph for the given character.

§Arguments
  • c - The character to check
§Returns
  • true if the font contains a glyph for this character
  • false if the character is missing (will render as .notdef/missing glyph)
§Example
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fonts/NotoSans-Regular.ttf");
let font_data = FontData::load(&p, None).unwrap();
assert!(font_data.has_glyph('ă'));
Source

pub fn check_coverage(&self, text: &str) -> GlyphCoverage

Analyzes glyph coverage for the given text.

This method checks which characters in the text are supported by this font and returns detailed coverage statistics.

§Arguments
  • text - The text to analyze
§Returns

A GlyphCoverage struct containing coverage statistics and missing characters

§Example
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fonts/NotoSans-Regular.ttf");
let font_data = FontData::load(&p, None).unwrap();
let coverage = font_data.check_coverage("Hello ăâîșț!");
assert!(coverage.coverage_percent() > 0.0);

Trait Implementations§

Source§

impl Clone for FontData

Source§

fn clone(&self) -> FontData

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FontData

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

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

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

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

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

§

fn to_owned_table(&self) -> U

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V