Skip to content

Commit

Permalink
Tidy up API surface and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmaedje committed Jun 8, 2022
1 parent 72f28af commit d3bd83e
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 212 deletions.
6 changes: 2 additions & 4 deletions src/cff/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a> Dict<'a> {
}
}

pub fn keep(&mut self, ops: &[Op]) {
pub fn retain(&mut self, ops: &[Op]) {
self.0.retain(|pair| ops.contains(&pair.op));
}

Expand Down Expand Up @@ -175,7 +175,7 @@ impl<'a> Structure<'a> for Operand<'a> {
fn write(&self, w: &mut Writer) {
match self {
Self::Int(int) => {
// TODO: More compact.
// TODO: Select most compact encoding.
w.write::<u8>(29);
w.write::<i32>(*int);
}
Expand All @@ -192,7 +192,6 @@ impl<'a> Structure<'a> for Operand<'a> {
}

/// Top DICT operators.
#[allow(unused)]
pub mod top {
use super::Op;

Expand Down Expand Up @@ -237,7 +236,6 @@ pub mod top {
pub const FONT_BBOX: Op = Op(5, 0);
pub const STROKE_WIDTH: Op = Op(12, 8);
pub const CHARSET: Op = Op(15, 0);
pub const ENCODING: Op = Op(16, 0);
pub const CHAR_STRINGS: Op = Op(17, 0);
pub const PRIVATE: Op = Op(18, 0);
pub const POST_SCRIPT: Op = Op(12, 21);
Expand Down
42 changes: 21 additions & 21 deletions src/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ struct Table<'a> {
cid: Option<CidData<'a>>,
}

/// An opaque charset.
/// A charset.
struct Charset<'a>(Opaque<'a>);

/// Data specific to Private DICTs.
struct PrivateData<'a> {
dict: Dict<'a>,
subrs: Option<Index<Opaque<'a>>>,
}

/// Data specific to CID-keyed fonts.
struct CidData<'a> {
array: Index<Dict<'a>>,
Expand All @@ -34,12 +40,6 @@ struct CidData<'a> {
/// An FD Select dat structure.
struct FdSelect<'a>(Cow<'a, [u8]>);

/// Data specific to Private DICTs.
struct PrivateData<'a> {
dict: Dict<'a>,
subrs: Option<Index<Opaque<'a>>>,
}

/// Recorded offsets that will be written into DICTs.
struct Offsets {
char_strings: usize,
Expand All @@ -48,19 +48,19 @@ struct Offsets {
cid: Option<CidOffsets>,
}

/// Offsets specific to Private DICTs.
struct PrivateOffsets {
dict: Range<usize>,
subrs: Option<usize>,
}

/// Offsets specific to CID-keyed fonts.
struct CidOffsets {
array: usize,
select: usize,
private: Vec<PrivateOffsets>,
}

/// Offsets specific to Private DICTs.
struct PrivateOffsets {
dict: Range<usize>,
subrs: Option<usize>,
}

/// Subset the CFF table by removing glyph data for unused glyphs.
pub(crate) fn subset(ctx: &mut Context) -> Result<()> {
let cff = ctx.expect_table(Tag::CFF)?;
Expand All @@ -74,27 +74,27 @@ pub(crate) fn subset(ctx: &mut Context) -> Result<()> {
}

// Parse CFF table.
let mut table = read_table(ctx, cff)?;
let mut table = read_cff_table(ctx, cff)?;

// Subset the char strings.
subset_char_strings(ctx, &mut table.char_strings)?;

// Subset Top and Private DICT.
table.top.keep(top::KEEP);
table.top.retain(top::KEEP);
if let Some(private) = &mut table.private {
private.dict.keep(private::KEEP);
private.dict.retain(private::KEEP);
}

// Subset data specific to CID-keyed fonts.
if let Some(cid) = &mut table.cid {
subset_font_dicts(ctx, cid)?;

for dict in cid.array.iter_mut() {
dict.keep(top::KEEP);
dict.retain(top::KEEP);
}

for private in &mut cid.private {
private.dict.keep(private::KEEP);
private.dict.retain(private::KEEP);
}
}

Expand All @@ -107,7 +107,7 @@ pub(crate) fn subset(ctx: &mut Context) -> Result<()> {
for _ in 0 .. 2 {
let mut w = Writer::new();
insert_offsets(&mut table, &offsets);
write_table(&mut w, &table, &mut offsets);
write_cff_table(&mut w, &table, &mut offsets);
sub_cff = w.finish();
}

Expand Down Expand Up @@ -152,7 +152,7 @@ fn subset_font_dicts(ctx: &Context, cid: &mut CidData) -> Result<()> {
}

/// Parse a CFF table.
fn read_table<'a>(ctx: &Context, cff: &'a [u8]) -> Result<Table<'a>> {
fn read_cff_table<'a>(ctx: &Context, cff: &'a [u8]) -> Result<Table<'a>> {
// Skip header.
let mut r = Reader::new(cff);
r.read::<u8>()?;
Expand Down Expand Up @@ -207,7 +207,7 @@ fn read_table<'a>(ctx: &Context, cff: &'a [u8]) -> Result<Table<'a>> {
}

/// Write the a new CFF table.
fn write_table(w: &mut Writer, table: &Table, offsets: &mut Offsets) {
fn write_cff_table(w: &mut Writer, table: &Table, offsets: &mut Offsets) {
// Write header.
w.write::<u8>(1);
w.write::<u8>(0);
Expand Down
Loading

0 comments on commit d3bd83e

Please sign in to comment.