Skip to content

Commit

Permalink
Start to extract substitutees
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed May 23, 2024
1 parent 39fb673 commit c911481
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/formats/opentype/features/characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait Characters<'l> {
fn characters(self, _: &Mapping, _: Self::Parameter) -> Self::Target;
}

type Glyphs = BTreeSet<Vec<Glyph>>;
type Glyphs = BTreeMap<Vec<Glyph>, Vec<Glyph>>;

impl<'l> Characters<'l> for &BTreeMap<Feature, BTreeMap<Script, BTreeMap<Language, Glyphs>>> {
type Target = Features;
Expand Down Expand Up @@ -56,7 +56,7 @@ impl<'l> Characters<'l> for &Glyphs {
fn characters(self, mapping: &Mapping, _: Self::Parameter) -> Self::Target {
postcompress(
self.iter()
.filter_map(|value| value.characters(mapping, self)),
.filter_map(|(value, _)| value.characters(mapping, self)),
)
}
}
Expand Down
58 changes: 43 additions & 15 deletions src/formats/opentype/features/glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum Glyph {

pub trait Glyphs {
#[inline]
fn glyphs(&self) -> BTreeSet<Vec<Glyph>> {
fn glyphs(&self) -> BTreeMap<Vec<Glyph>, Vec<Glyph>> {
Default::default()
}
}
Expand Down Expand Up @@ -63,23 +63,43 @@ impl From<Coverage> for Glyph {
impl Glyphs for opentype::tables::glyph_positioning::Type {}

impl Glyphs for opentype::tables::glyph_substitution::Type {
fn glyphs(&self) -> BTreeSet<Vec<Glyph>> {
fn glyphs(&self) -> BTreeMap<Vec<Glyph>, Vec<Glyph>> {
use opentype::layout::{ChainedContext, Context};
use opentype::tables::glyph_substitution::{SingleSubstitution, Type};

let mut values = BTreeSet::default();
let mut values = BTreeMap::default();
match self {
Type::SingleSubstitution(SingleSubstitution::Format1(table)) => {
values.extend(uncover(&table.coverage).map(Glyph::Scalar).map(vector));
values.extend(
uncover(&table.coverage)
.map(Glyph::Scalar)
.map(vector)
.map(double),
);
}
Type::SingleSubstitution(SingleSubstitution::Format2(table)) => {
values.extend(uncover(&table.coverage).map(Glyph::Scalar).map(vector));
values.extend(
uncover(&table.coverage)
.map(Glyph::Scalar)
.map(vector)
.map(double),
);
}
Type::MultipleSubstitution(table) => {
values.extend(uncover(&table.coverage).map(Glyph::Scalar).map(vector));
values.extend(
uncover(&table.coverage)
.map(Glyph::Scalar)
.map(vector)
.map(double),
);
}
Type::AlternateSubstitution(table) => {
values.extend(uncover(&table.coverage).map(Glyph::Scalar).map(vector));
values.extend(
uncover(&table.coverage)
.map(Glyph::Scalar)
.map(vector)
.map(double),
);
}
Type::LigatureSubstitution(table) => {
values.extend(uncover(&table.coverage).zip(&table.records).flat_map(
Expand All @@ -90,7 +110,7 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
for glyph_id in record.glyph_ids.iter().cloned() {
value.push(Glyph::Scalar(glyph_id));
}
value
(value, Default::default())
})
},
));
Expand All @@ -104,7 +124,7 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
for glyph_id in record.glyph_ids.iter().cloned() {
value.push(Glyph::Scalar(glyph_id));
}
value
(value, Default::default())
})
},
));
Expand All @@ -129,13 +149,16 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
for class_index in &record.indices {
value.push(classes.get(class_index)?.clone());
}
Some(value)
Some((value, Default::default()))
})
}),
);
}
Type::ContextualSubstitution(Context::Format3(table)) => {
values.insert(table.coverages.iter().cloned().map(Glyph::from).collect());
values.insert(
table.coverages.iter().cloned().map(Glyph::from).collect(),
Default::default(),
);
}
Type::ChainedContextualSubstitution(ChainedContext::Format1(table)) => {
values.extend(uncover(&table.coverage).zip(&table.records).flat_map(
Expand All @@ -156,7 +179,7 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
for glyph_id in record.forward_glyph_ids.iter().cloned() {
value.push(Glyph::Scalar(glyph_id));
}
value
(value, Default::default())
})
},
));
Expand Down Expand Up @@ -198,7 +221,7 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
for class_index in &record.forward_indices {
value.push(forward_classes.get(class_index)?.clone());
}
Some(value)
Some((value, Default::default()))
})
}),
);
Expand All @@ -213,7 +236,7 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
.collect::<Vec<_>>();
value.extend(table.coverages.iter().cloned().map(Glyph::from));
value.extend(table.forward_coverages.iter().cloned().map(Glyph::from));
values.insert(value);
values.insert(value, Default::default());
}
Type::ReverseChainedContextualSubstibution(table) => {
let mut value = table
Expand All @@ -225,14 +248,19 @@ impl Glyphs for opentype::tables::glyph_substitution::Type {
.collect::<Vec<_>>();
value.push(table.coverage.clone().into());
value.extend(table.forward_coverages.iter().cloned().map(Glyph::from));
values.insert(value);
values.insert(value, Default::default());
}
_ => {}
}
values
}
}

#[inline]
fn double<T>(value: Vec<T>) -> (Vec<T>, Vec<T>) {
(value, Default::default())
}

#[inline]
fn vector<T>(value: T) -> Vec<T> {
vec![value]
Expand Down
11 changes: 7 additions & 4 deletions src/formats/opentype/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod glyphs;

pub use opentype::layout::{Language, Script};

use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;
use std::io::Result;

use opentype::layout::{Directory, Feature};
Expand Down Expand Up @@ -38,7 +38,10 @@ pub(crate) fn read<T: crate::Read>(cache: &mut Cache<T>) -> Result<Features> {

#[allow(clippy::type_complexity)]
fn populate<T>(
values: &mut BTreeMap<Feature, BTreeMap<Script, BTreeMap<Language, BTreeSet<Vec<Glyph>>>>>,
values: &mut BTreeMap<
Feature,
BTreeMap<Script, BTreeMap<Language, BTreeMap<Vec<Glyph>, Vec<Glyph>>>>,
>,
table: &Directory<T>,
) where
T: Glyphs,
Expand All @@ -58,7 +61,7 @@ fn populate<T>(
.cloned()
.filter_map(|index| table.lookups.records.get(index as usize))
.flat_map(|record| record.tables.iter().flat_map(|table| table.glyphs()))
.collect::<BTreeSet<_>>();
.collect::<BTreeMap<_, _>>();
values
.entry(feature)
.or_default()
Expand All @@ -83,7 +86,7 @@ fn populate<T>(
.cloned()
.filter_map(|index| table.lookups.records.get(index as usize))
.flat_map(|record| record.tables.iter().flat_map(|table| table.glyphs()))
.collect::<BTreeSet<_>>();
.collect::<BTreeMap<_, _>>();
values
.entry(feature)
.or_default()
Expand Down

0 comments on commit c911481

Please sign in to comment.