pub struct Font<T: FontTableProvider> {
pub font_table_provider: T,
pub maxp_table: MaxpTable,
pub hhea_table: HheaTable,
pub cmap_subtable_encoding: Encoding,
pub glyph_table_flags: GlyphTableFlags,
/* private fields */
}Expand description
Core type for loading a font in order to perform glyph mapping and font shaping.
Fields§
§font_table_provider: T§maxp_table: MaxpTable§hhea_table: HheaTable§cmap_subtable_encoding: Encoding§glyph_table_flags: GlyphTableFlagsImplementations§
Source§impl<T: FontTableProvider> Font<T>
impl<T: FontTableProvider> Font<T>
Sourcepub fn new(provider: T) -> Result<Font<T>, ParseError>
pub fn new(provider: T) -> Result<Font<T>, ParseError>
Construct a new instance from a type that can supply font tables.
Returns None if the font was able to be read but no supported cmap sub-table was
able to be found. The lack of such a table prevents glyph mapping.
Sourcepub fn num_glyphs(&self) -> u16
pub fn num_glyphs(&self) -> u16
Returns the number of glyphs in the font.
Sourcepub fn set_embedded_image_filter(&mut self, flags: GlyphTableFlags)
pub fn set_embedded_image_filter(&mut self, flags: GlyphTableFlags)
Set the embedded image table filter.
When determining if a font contains embedded images, as well as retrieving images this
value it used to set which tables are consulted. By default it is set to only consult
tables that can contain colour images (CBDT/CBLC, sbix, and SVG). You can change
the value to exclude certain tables or opt into tables that can only contain B&W images
(EBDT/EBLC).
Sourcepub fn lookup_glyph_index(
&mut self,
ch: char,
match_presentation: MatchingPresentation,
variation_selector: Option<VariationSelector>,
) -> (u16, VariationSelector)
pub fn lookup_glyph_index( &mut self, ch: char, match_presentation: MatchingPresentation, variation_selector: Option<VariationSelector>, ) -> (u16, VariationSelector)
Look up the glyph index for the supplied character in the font.
Sourcepub fn shape(
&mut self,
glyphs: Vec<RawGlyph<()>>,
script_tag: u32,
opt_lang_tag: Option<u32>,
features: &Features,
tuple: Option<Tuple<'_>>,
kerning: bool,
) -> Result<Vec<Info>, (ShapingError, Vec<Info>)>
pub fn shape( &mut self, glyphs: Vec<RawGlyph<()>>, script_tag: u32, opt_lang_tag: Option<u32>, features: &Features, tuple: Option<Tuple<'_>>, kerning: bool, ) -> Result<Vec<Info>, (ShapingError, Vec<Info>)>
Convenience method to shape the supplied glyphs.
The method maps applies glyph substitution (gsub) and glyph positioning (gpos). Use
map_glyphs to turn text into glyphs that can be accepted by this method.
Arguments:
glyphs: the glyphs to be shaped in logical order.script_tag: the OpenType script tag of the text.opt_lang_tag: the OpenType language tag of the text.features: the OpenType features to enable.kerning: when applyinggposif this argument istruethekernOpenType feature is enabled for non-complex scripts. If it isfalsethen thekernfeature is not enabled for non-complex scripts.
Error Handling:
This method will continue on in the face of errors, applying what it can. If no errors are
encountered this method returns Ok(Vec<Info>). If one or more errors are encountered
Err((ShapingError, Vec<Info>)) is returned. The first error encountered is returned
as the first item of the tuple. Vec<Info> is returned as the second item of the tuple,
allowing consumers of the shaping output to proceed even if errors are encountered.
However, in the error case the glyphs might not have had GPOS or GSUB applied.
§Example
use allsorts::binary::read::ReadScope;
use allsorts::font::MatchingPresentation;
use allsorts::font_data::FontData;
use allsorts::gsub::{self, Features, FeatureMask};
use allsorts::DOTTED_CIRCLE;
use allsorts::{tag, Font};
let script = tag::LATN;
let lang = tag::DFLT;
let variation_tuple = None;
let buffer = std::fs::read("tests/fonts/opentype/Klei.otf")
.expect("unable to read Klei.otf");
let scope = ReadScope::new(&buffer);
let font_file = scope.read::<FontData<'_>>().expect("unable to parse font");
// Use a different index to access other fonts in a font collection (E.g. TTC)
let provider = font_file
.table_provider(0)
.expect("unable to create table provider");
let mut font = Font::new(provider)
.expect("unable to load font tables");
// Klei ligates ff
let glyphs = font.map_glyphs("Shaping in a jiffy.", script, MatchingPresentation::NotRequired);
let glyph_infos = font
.shape(
glyphs,
script,
Some(lang),
&Features::Mask(FeatureMask::default()),
variation_tuple,
true,
)
.expect("error shaping text");
// We expect ff to be ligated so the number of glyphs (18) should be one less than the
// number of input characters (19).
assert_eq!(glyph_infos.len(), 18);Sourcepub fn map_glyphs(
&mut self,
text: &str,
script_tag: u32,
match_presentation: MatchingPresentation,
) -> Vec<RawGlyph<()>>
pub fn map_glyphs( &mut self, text: &str, script_tag: u32, match_presentation: MatchingPresentation, ) -> Vec<RawGlyph<()>>
Map text to glyphs.
This method maps text into glyphs, which can then be passed to shape. The text is
processed in logical order, there is no need to reorder text before calling this method.
The match_presentation argument controls glyph mapping in the presence of emoji/text
variation selectors. If MatchingPresentation::NotRequired is passed then glyph mapping
will succeed if the font contains a mapping for a given character, regardless of whether
it has the tables necessary to support the requested presentation. If
MatchingPresentation::Required is passed then a character with emoji presentation,
either by default or requested via variation selector will only map to a glyph if the font
has mapping for the character, and it has the necessary tables for color emoji.
Sourcepub fn is_variable(&self) -> bool
pub fn is_variable(&self) -> bool
True if the font has one or more variation axes.
pub fn variation_axes(&self) -> Result<Vec<VariationAxisRecord>, ParseError>
pub fn glyph_names<'a>(&self, ids: &[u16]) -> Vec<Cow<'a, str>>
Sourcepub fn axis_names<'a>(&self) -> Result<Vec<NamedAxis<'a>>, AxisNamesError>
pub fn axis_names<'a>(&self) -> Result<Vec<NamedAxis<'a>>, AxisNamesError>
Returns the names of the variation axes in the font.
Sourcepub fn lookup_glyph_image(
&mut self,
glyph_index: u16,
target_ppem: u16,
max_bit_depth: BitDepth,
) -> Result<Option<BitmapGlyph>, ParseError>
pub fn lookup_glyph_image( &mut self, glyph_index: u16, target_ppem: u16, max_bit_depth: BitDepth, ) -> Result<Option<BitmapGlyph>, ParseError>
Find an image matching the supplied criteria.
glyph_indexis the glyph to lookup.target_ppemis the desired size. If an exact match can’t be found the nearest one will be returned, favouring being oversize vs. undersized.max_bit_depthis the maximum accepted bit depth of the bitmap to return. If you accept all bit depths then useBitDepth::ThirtyTwo.
Sourcepub fn has_embedded_images(&mut self) -> bool
pub fn has_embedded_images(&mut self) -> bool
Returns true if the font contains embedded images in supported tables.
Allsorts supports extracting images from CBDT/CBLC, sbix, and SVG tables.
If any of these tables are present and parsable then this method returns true.
Sourcepub fn has_glyph_outlines(&self) -> bool
pub fn has_glyph_outlines(&self) -> bool
Returns true if the font contains vector glyph outlines in supported tables.
Supported tables are glyf, CFF, CFF2.
Sourcepub fn horizontal_advance(&mut self, glyph: u16) -> Option<u16>
pub fn horizontal_advance(&mut self, glyph: u16) -> Option<u16>
Returns the horizontal advance of the supplied glyph index.
Will return None if there are errors encountered reading the hmtx table or there is
no entry for the glyph index.
pub fn vertical_advance(&mut self, glyph: u16) -> Option<u16>
pub fn head_table(&self) -> Result<Option<HeadTable>, ParseError>
pub fn os2_table(&self) -> Result<Option<Os2>, ParseError>
pub fn gdef_table(&mut self) -> Result<Option<Rc<GDEFTable>>, ParseError>
pub fn gsub_cache(&mut self) -> Result<Option<LayoutCache<GSUB>>, ParseError>
pub fn gpos_cache(&mut self) -> Result<Option<LayoutCache<GPOS>>, ParseError>
pub fn vhea_table(&mut self) -> Result<Option<Rc<HheaTable>>, ParseError>
pub fn cmap_subtable_data(&self) -> &[u8] ⓘ
Auto Trait Implementations§
impl<T> Freeze for Font<T>where
T: Freeze,
impl<T> !RefUnwindSafe for Font<T>
impl<T> !Send for Font<T>
impl<T> !Sync for Font<T>
impl<T> Unpin for Font<T>where
T: Unpin,
impl<T> !UnwindSafe for Font<T>
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> 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