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: GlyphTableFlags
Implementations
sourceimpl<T: FontTableProvider> Font<T>
impl<T: FontTableProvider> Font<T>
sourcepub fn new(provider: T) -> Result<Option<Font<T>>, ParseError>
pub fn new(provider: T) -> Result<Option<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,
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,
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 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);
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.
pub fn glyph_names<'a>(&self, ids: &[u16]) -> Vec<Cow<'a, str>>
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_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
.
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 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.