Skip to content

Commit

Permalink
CID-keyed font parsing and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmaedje committed Jun 8, 2022
1 parent e101776 commit 3cf1e93
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 107 deletions.
56 changes: 48 additions & 8 deletions src/cff/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use crate::{Error, Reader, Result, Structure, Writer};
pub struct Dict<'a>(Vec<Pair<'a>>);

impl<'a> Dict<'a> {
pub fn new() -> Self {
Self(vec![])
}

pub fn get(&self, op: Op) -> Option<&[Operand<'a>]> {
self.0
.iter()
Expand All @@ -37,10 +33,8 @@ impl<'a> Dict<'a> {
}
}

pub fn copy(&mut self, src: &Self, op: Op) {
if let Some(operands) = src.get(op) {
self.set(op, operands.to_vec());
}
pub fn keep(&mut self, ops: &[Op]) {
self.0.retain(|pair| ops.contains(&pair.op));
}

pub fn set(&mut self, op: Op, operands: Vec<Operand<'a>>) {
Expand Down Expand Up @@ -202,6 +196,31 @@ impl<'a> Structure<'a> for Operand<'a> {
pub mod top {
use super::Op;

pub const KEEP: &[Op] = &[
ROS,
CID_FONT_VERSION,
CID_FONT_REVISION,
CID_FONT_TYPE,
CID_COUNT,
FONT_NAME,
VERSION,
NOTICE,
COPYRIGHT,
FULL_NAME,
FAMILY_NAME,
WEIGHT,
IS_FIXED_PITCH,
ITALIC_ANGLE,
UNDERLINE_POSITION,
UNDERLINE_THICKNESS,
PAINT_TYPE,
CHARSTRING_TYPE,
FONT_MATRIX,
FONT_BBOX,
STROKE_WIDTH,
POST_SCRIPT,
];

pub const VERSION: Op = Op(0, 0);
pub const NOTICE: Op = Op(1, 0);
pub const COPYRIGHT: Op = Op(12, 0);
Expand Down Expand Up @@ -237,6 +256,27 @@ pub mod top {
/// Private DICT operators.
pub mod private {
use super::Op;

pub const KEEP: &[Op] = &[
BLUE_VALUES,
OTHER_BLUES,
FAMILY_BLUES,
FAMILY_OTHER_BLUES,
BLUE_SCALE,
BLUE_SHIFT,
BLUE_FUZZ,
STD_HW,
STD_VW,
STEM_SNAP_H,
STEM_SNAP_V,
FORCE_BOLD,
LANGUAGE_GROUP,
EXPANSION_FACTOR,
INITIAL_RANDOM_SEED,
DEFAULT_WIDTH_X,
NOMINAL_WIDTH_X,
];

pub const BLUE_VALUES: Op = Op(6, 0);
pub const OTHER_BLUES: Op = Op(7, 0);
pub const FAMILY_BLUES: Op = Op(8, 0);
Expand Down
22 changes: 16 additions & 6 deletions src/cff/index.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt::{self, Debug, Formatter};
use std::ops::{Deref, DerefMut};

use crate::{Error, Reader, Result, Structure, Writer};

/// An INDEX data structure.
#[derive(Clone)]
pub struct Index<T>(Vec<T>);
pub struct Index<T>(pub Vec<T>);

impl<T> Index<T> {
/// Create a new index with a single entry.
Expand All @@ -16,11 +17,6 @@ impl<T> Index<T> {
pub fn into_one(self) -> Option<T> {
self.0.into_iter().next()
}

/// Access an item mutably.
pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
self.0.get_mut(idx)
}
}

impl<'a, T> Structure<'a> for Index<T>
Expand Down Expand Up @@ -92,6 +88,20 @@ impl<T: Debug> Debug for Index<T> {
}
}

impl<T> Deref for Index<T> {
type Target = [T];

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for Index<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// The number of bytes an offset is encoded with.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
Expand Down
Loading

0 comments on commit 3cf1e93

Please sign in to comment.