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: RawFontDataImplementations§
Source§impl FontData
impl FontData
Sourcepub fn new(
data: Vec<u8>,
builtin: Option<BuiltinFont>,
) -> Result<FontData, Error>
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();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");Sourcepub fn load(
path: impl AsRef<Path>,
builtin: Option<BuiltinFont>,
) -> Result<FontData, Error>
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.
Sourcepub fn get_data(&self) -> Result<&[u8], Error>
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 fontsErr(Error)- If this is a built-in font (which has no raw data to extract)
Sourcepub fn has_glyph(&self, c: char) -> bool
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
trueif the font contains a glyph for this characterfalseif 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('ă'));Sourcepub fn check_coverage(&self, text: &str) -> GlyphCoverage
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§
Auto Trait Implementations§
impl Freeze for FontData
impl RefUnwindSafe for FontData
impl Send for FontData
impl Sync for FontData
impl Unpin for FontData
impl UnwindSafe for FontData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.