Skip to content

Commit

Permalink
Isolate AAT tables
Browse files Browse the repository at this point in the history
Captures lookup of all the AAT types into a new type and builds them directly from table data rather than going through `ttf_parser::Face`.

This will allow us to remove `ttf_parser::Face` from `hb_font_t` before AAT support in `read-fonts` is done and will enable us to replace those tables one at a time.
  • Loading branch information
dfrg committed Jul 28, 2024
1 parent 353f65f commit 913495e
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/hb/aat_layout_kerx_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn apply(
buffer.unsafe_to_concat(None, None);

let mut seen_cross_stream = false;
for subtable in face.tables().kerx?.subtables {
for subtable in face.aat_tables.kerx.as_ref()?.subtables {
if subtable.variable {
continue;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ pub(crate) fn apply(
let mut driver = Driver4 {
mark_set: false,
mark: 0,
ankr_table: face.tables().ankr.clone(),
ankr_table: face.aat_tables.ankr.clone(),
};

apply_state_machine_kerning(&subtable, sub, &mut driver, plan, buffer);
Expand Down
4 changes: 2 additions & 2 deletions src/hb/aat_layout_morx_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn compile_flags(
.is_ok()
};

let chains = face.tables().morx.as_ref()?.chains;
let chains = face.aat_tables.morx.as_ref()?.chains;
let chain_len = chains.clone().into_iter().count();
map.chain_flags.resize(chain_len, vec![]);

Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn compile_flags(
pub fn apply<'a>(c: &mut hb_aat_apply_context_t<'a>, map: &'a mut hb_aat_map_t) -> Option<()> {
c.buffer.unsafe_to_concat(None, None);

let chains = c.face.tables().morx.as_ref()?.chains;
let chains = c.face.aat_tables.morx.as_ref()?.chains;
let chain_len = chains.clone().into_iter().count();
map.chain_flags.resize(chain_len, vec![]);

Expand Down
2 changes: 1 addition & 1 deletion src/hb/aat_layout_trak_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn apply(plan: &hb_ot_shape_plan_t, face: &hb_font_t, buffer: &mut hb_buffer
return None;
}

let trak = face.tables().trak?;
let trak = face.aat_tables.trak.as_ref()?;

if !buffer.have_positions {
buffer.clear_positions();
Expand Down
2 changes: 1 addition & 1 deletion src/hb/aat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Default for hb_aat_map_builder_t {

impl hb_aat_map_builder_t {
pub fn add_feature(&mut self, face: &hb_font_t, feature: &Feature) -> Option<()> {
let feat = face.tables().feat?;
let feat = face.aat_tables.feat.as_ref()?;

if feature.tag == hb_tag_t::new(b"aalt") {
let exposes_feature = feat
Expand Down
48 changes: 48 additions & 0 deletions src/hb/aat_tables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use core::num::NonZeroU16;
use skrifa::raw::{types::Tag, FontRef, TableProvider};
use ttf_parser::{
ankr::Table as Ankr, feat::Table as Feat, kern::Table as Kern, kerx::Table as Kerx,
morx::Table as Morx, trak::Table as Trak,
};

#[derive(Clone, Default)]
pub struct AatTables<'a> {
pub morx: Option<Morx<'a>>,
pub ankr: Option<Ankr<'a>>,
pub kern: Option<Kern<'a>>,
pub kerx: Option<Kerx<'a>>,
pub trak: Option<Trak<'a>>,
pub feat: Option<Feat<'a>>,
}

impl<'a> AatTables<'a> {
pub fn new(font: &FontRef<'a>) -> Self {
let Some(num_glyphs) = NonZeroU16::new(
font.maxp()
.map(|maxp| maxp.num_glyphs())
.unwrap_or_default(),
) else {
return Self::default();
};
Self {
morx: font
.table_data(Tag::new(b"morx"))
.and_then(|data| Morx::parse(num_glyphs, data.as_bytes())),
ankr: font
.table_data(Tag::new(b"ankr"))
.and_then(|data| Ankr::parse(num_glyphs, data.as_bytes())),
kern: font
.table_data(Tag::new(b"kern"))
.and_then(|data| Kern::parse(data.as_bytes())),
kerx: font
.table_data(Tag::new(b"kerx"))
.and_then(|data| Kerx::parse(num_glyphs, data.as_bytes())),
trak: font
.table_data(Tag::new(b"trak"))
.and_then(|data| Trak::parse(data.as_bytes())),
feat: font
.table_data(Tag::new(b"feat"))
.and_then(|data| Feat::parse(data.as_bytes())),
}
}
}
18 changes: 15 additions & 3 deletions src/hb/face.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use bytemuck::{Pod, Zeroable};
#[cfg(not(feature = "std"))]
use core_maths::CoreFloat;
use skrifa::raw::TableProvider;
use skrifa::FontRef;

use crate::hb::paint_extents::hb_paint_extents_context_t;
use ttf_parser::gdef::GlyphClass;
use ttf_parser::{GlyphId, RgbaColor};

use super::aat_tables::AatTables;
use super::buffer::GlyphPropsFlags;
use super::fonta;
use super::common::TagExt;
use super::fonta;
use super::ot_layout::TableIndex;
use crate::Variation;

Expand All @@ -17,6 +20,7 @@ use crate::Variation;
pub struct hb_font_t<'a> {
pub(crate) ttfp_face: ttf_parser::Face<'a>,
pub(crate) font: fonta::Font<'a>,
pub(crate) aat_tables: AatTables<'a>,
pub(crate) units_per_em: u16,
pixels_per_em: Option<(u16, u16)>,
pub(crate) points_per_em: Option<f32>,
Expand All @@ -37,10 +41,17 @@ impl<'a> hb_font_t<'a> {
/// Data will be referenced, not owned.
pub fn from_slice(data: &'a [u8], face_index: u32) -> Option<Self> {
let face = ttf_parser::Face::parse(data, face_index).ok()?;
let font_ref = FontRef::from_index(data, face_index).ok()?;
let units_per_em = font_ref
.head()
.map(|head| head.units_per_em())
.unwrap_or_default();
let font = fonta::Font::new(data, face_index)?;
let aat_tables = AatTables::new(&font_ref);
Some(hb_font_t {
font,
units_per_em: face.units_per_em(),
aat_tables,
units_per_em,
pixels_per_em: None,
points_per_em: None,
ttfp_face: face,
Expand Down Expand Up @@ -82,7 +93,8 @@ impl<'a> hb_font_t<'a> {
/// Sets font variations.
pub fn set_variations(&mut self, variations: &[Variation]) {
for variation in variations {
self.ttfp_face.set_variation(ttf_parser::Tag(variation.tag.as_u32()), variation.value);
self.ttfp_face
.set_variation(ttf_parser::Tag(variation.tag.as_u32()), variation.value);
}
self.font.set_coords(self.ttfp_face.variation_coordinates());
}
Expand Down
2 changes: 1 addition & 1 deletion src/hb/kerning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::ot_shape_plan::hb_ot_shape_plan_t;
use super::{hb_font_t, hb_mask_t};

pub fn hb_ot_layout_kern(plan: &hb_ot_shape_plan_t, face: &hb_font_t, buffer: &mut hb_buffer_t) {
let subtables = match face.tables().kern {
let subtables = match &face.aat_tables.kern {
Some(table) => table.subtables,
None => return,
};
Expand Down
1 change: 1 addition & 0 deletions src/hb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]

mod aat_tables;
mod algs;
mod fonta;
#[macro_use]
Expand Down

0 comments on commit 913495e

Please sign in to comment.