Struct allsorts::font::Font [−][src]
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, // some fields omitted }
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: GlyphTableFlags
Implementations
Construct a new instance from a type that can supply font tables.
Returns the number of glyphs in the font.
pub 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.
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.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 applyinggpos
if this argument istrue
thekern
OpenType feature is enabled for non-complex scripts. If it isfalse
then thekern
feature 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 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") .expect("unable to find suitable cmap sub-table"); // 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()), 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);
pub 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 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.
pub 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_index
is the glyph to lookup.target_ppem
is the desired size. If an exact match can’t be found the nearest one will be returned, favouring being oversize vs. undersized.max_bit_depth
is the maximum accepted bit depth of the bitmap to return. If you accept all bit depths then useBitDepth::ThirtyTwo
.
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
.
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.
Auto Trait Implementations
impl<T> !RefUnwindSafe for Font<T>
impl<T> !UnwindSafe for Font<T>