Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cff2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::borrow::Cow;
pub fn subset(ctx: &mut Context) -> crate::Result<()> {
let mut maxp_data = MaxpData::default();

let result = glyf::subset_with(ctx, |old_gid, ctx| {
glyf::subset_with(ctx, |old_gid, ctx| {
let data = match &ctx.interjector {
// We reject CFF2 fonts earlier if `variable-fonts` feature is not enabled.
Interjector::Dummy(_) => unreachable!(),
Expand All @@ -18,8 +18,8 @@ pub fn subset(ctx: &mut Context) -> crate::Result<()> {
};

Ok(data)
});
})?;

ctx.custom_maxp_data = Some(maxp_data);
result
Ok(())
}
14 changes: 11 additions & 3 deletions src/glyf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub fn closure(face: &Face, glyph_remapper: &mut GlyphRemapper) -> Result<()> {

pub fn subset(ctx: &mut Context) -> Result<()> {
let table = Table::new(&ctx.face).ok_or(MalformedFont)?;
let mut _maxp = MaxpData::default();

#[allow(unused_mut)]
let mut maxp_data = MaxpData::default();

subset_with(ctx, |old_gid, ctx| {
let data = match &ctx.interjector {
Expand All @@ -56,12 +58,18 @@ pub fn subset(ctx: &mut Context) -> Result<()> {
}
#[cfg(feature = "variable-fonts")]
Interjector::Skrifa(s) => {
Cow::Owned(s.glyph_data(&mut _maxp, old_gid).ok_or(MalformedFont)?)
Cow::Owned(s.glyph_data(&mut maxp_data, old_gid).ok_or(MalformedFont)?)
}
};

Ok(data)
})
})?;

if ctx.interjector.is_skrifa() {
ctx.custom_maxp_data = Some(maxp_data);
}

Ok(())
}

pub(crate) fn subset_with<'a>(
Expand Down
2 changes: 1 addition & 1 deletion src/hmtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn subset(ctx: &mut Context) -> Result<()> {

let hhea = ctx.expect_table(Tag::HHEA).ok_or(MalformedFont)?;
let mut sub_hhea = Writer::new();
sub_hhea.extend(&hhea[0..hhea.len() - 2]);
sub_hhea.extend(hhea.get(..hhea.len() - 2).ok_or(MalformedFont)?);
sub_hhea.write::<u16>(last_advance_width_index + 1);

ctx.push(Tag::HHEA, sub_hhea.finish());
Expand Down
10 changes: 10 additions & 0 deletions src/interjector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ pub(crate) enum Interjector<'a> {
Skrifa(skrifa::SkrifaInterjector<'a>),
}

impl Interjector<'_> {
pub fn is_skrifa(&self) -> bool {
match self {
Self::Dummy(_) => false,
#[cfg(feature = "variable-fonts")]
Self::Skrifa(_) => true,
}
}
}

#[cfg(feature = "variable-fonts")]
pub(crate) mod skrifa {
use crate::{MaxpData, Tag};
Expand Down
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,15 @@ fn _subset(mut ctx: Context) -> Result<Vec<u8>> {
if ctx.flavor == FontFlavor::TrueType {
// LOCA will be handled by GLYF
ctx.process(Tag::GLYF)?;
ctx.process(Tag::CVT)?; // won't be subsetted.
ctx.process(Tag::FPGM)?; // won't be subsetted.
ctx.process(Tag::PREP)?; // won't be subsetted.

// Only copy hinting tables if we don't interject because skrifa will
// discard hinting information.
// TODO: Add a test for this.
if !ctx.interjector.is_skrifa() {
ctx.process(Tag::CVT)?; // won't be subsetted.
ctx.process(Tag::FPGM)?; // won't be subsetted.
ctx.process(Tag::PREP)?; // won't be subsetted.
}
} else if ctx.flavor == FontFlavor::Cff {
ctx.process(Tag::CFF)?;
} else if ctx.flavor == FontFlavor::Cff2 {
Expand Down Expand Up @@ -303,7 +309,8 @@ fn construct(mut ctx: Context) -> Vec<u8> {
let mut offset = 12 + ctx.tables.len() * 16;
for (tag, data) in &mut ctx.tables {
if *tag == Tag::HEAD {
// Zero out checksum field in head table.
// Zero out checksum field in head table. The head table has already
// been subset and is guaranteed to have the necessary minimum size.
data.to_mut()[8..12].fill(0);
checksum_adjustment_offset = Some(offset + 8);
}
Expand Down Expand Up @@ -551,7 +558,7 @@ impl Tag {
}

impl Readable<'_> for Tag {
const SIZE: usize = u8::SIZE * 4;
const SIZE: usize = 4;

fn read(r: &mut Reader) -> Option<Self> {
r.read::<[u8; 4]>().map(Self)
Expand Down
2 changes: 1 addition & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub trait Readable<'a>: Sized {
}

impl<const N: usize> Readable<'_> for [u8; N] {
const SIZE: usize = u8::SIZE * N;
const SIZE: usize = N;

fn read(r: &mut Reader) -> Option<Self> {
Some(r.read_bytes(N)?.try_into().unwrap_or([0; N]))
Expand Down
12 changes: 6 additions & 6 deletions tests/ttx/NotoSans-Regular_var_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.013"/>
<checkSumAdjustment value="0xf8bea5c9"/>
<checkSumAdjustment value="0xf902a8a3"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -64,18 +64,18 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="0x10000"/>
<numGlyphs value="14"/>
<maxPoints value="273"/>
<maxContours value="24"/>
<maxCompositePoints value="143"/>
<maxCompositeContours value="8"/>
<maxPoints value="51"/>
<maxContours value="2"/>
<maxCompositePoints value="0"/>
<maxCompositeContours value="0"/>
<maxZones value="1"/>
<maxTwilightPoints value="0"/>
<maxStorage value="0"/>
<maxFunctionDefs value="0"/>
<maxInstructionDefs value="0"/>
<maxStackElements value="0"/>
<maxSizeOfInstructions value="0"/>
<maxComponentElements value="4"/>
<maxComponentElements value="0"/>
<maxComponentDepth value="1"/>
</maxp>

Expand Down
12 changes: 6 additions & 6 deletions tests/ttx/NotoSans-Regular_var_2.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.013"/>
<checkSumAdjustment value="0xe1c0756b"/>
<checkSumAdjustment value="0xe2047845"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -64,18 +64,18 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="0x10000"/>
<numGlyphs value="14"/>
<maxPoints value="273"/>
<maxContours value="24"/>
<maxCompositePoints value="143"/>
<maxCompositeContours value="8"/>
<maxPoints value="51"/>
<maxContours value="2"/>
<maxCompositePoints value="0"/>
<maxCompositeContours value="0"/>
<maxZones value="1"/>
<maxTwilightPoints value="0"/>
<maxStorage value="0"/>
<maxFunctionDefs value="0"/>
<maxInstructionDefs value="0"/>
<maxStackElements value="0"/>
<maxSizeOfInstructions value="0"/>
<maxComponentElements value="4"/>
<maxComponentElements value="0"/>
<maxComponentDepth value="1"/>
</maxp>

Expand Down
12 changes: 6 additions & 6 deletions tests/ttx/NotoSans-Regular_var_3.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.013"/>
<checkSumAdjustment value="0xec15e09b"/>
<checkSumAdjustment value="0xec59e375"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -64,18 +64,18 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="0x10000"/>
<numGlyphs value="14"/>
<maxPoints value="273"/>
<maxContours value="24"/>
<maxCompositePoints value="143"/>
<maxCompositeContours value="8"/>
<maxPoints value="51"/>
<maxContours value="2"/>
<maxCompositePoints value="0"/>
<maxCompositeContours value="0"/>
<maxZones value="1"/>
<maxTwilightPoints value="0"/>
<maxStorage value="0"/>
<maxFunctionDefs value="0"/>
<maxInstructionDefs value="0"/>
<maxStackElements value="0"/>
<maxSizeOfInstructions value="0"/>
<maxComponentElements value="4"/>
<maxComponentElements value="0"/>
<maxComponentDepth value="1"/>
</maxp>

Expand Down
Loading