Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[read-fonts] Add scaler for CFF/CFF2 #520

Merged
merged 10 commits into from
Aug 8, 2023
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
9 changes: 8 additions & 1 deletion read-fonts/src/tables/postscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum Error {
CharstringNestingDepthLimitExceeded,
MissingSubroutines,
MissingBlendState,
MissingPrivateDict,
MissingCharstrings,
Read(ReadError),
}

Expand Down Expand Up @@ -98,7 +100,12 @@ impl fmt::Display for Error {
"encountered a blend operator but no blend state was provided"
)
}

Self::MissingPrivateDict => {
write!(f, "CFF table does not contain a private dictionary")
}
Self::MissingCharstrings => {
write!(f, "CFF table does not contain a charstrings index")
}
Self::Read(err) => write!(f, "{err}"),
}
}
Expand Down
16 changes: 8 additions & 8 deletions read-fonts/src/tables/postscript/charstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ where
P: Pen,
{
fn move_to(&mut self, x: Fixed, y: Fixed) {
self.0.move_to(x.to_f64() as f32, y.to_f64() as f32);
self.0.move_to(x.to_f32(), y.to_f32());
}

fn line_to(&mut self, x: Fixed, y: Fixed) {
self.0.line_to(x.to_f64() as f32, y.to_f64() as f32);
self.0.line_to(x.to_f32(), y.to_f32());
}

fn curve_to(&mut self, cx0: Fixed, cy0: Fixed, cx1: Fixed, cy1: Fixed, x: Fixed, y: Fixed) {
self.0.curve_to(
cx0.to_f64() as f32,
cy0.to_f64() as f32,
cx1.to_f64() as f32,
cy1.to_f64() as f32,
x.to_f64() as f32,
y.to_f64() as f32,
cx0.to_f32(),
cy0.to_f32(),
cx1.to_f32(),
cy1.to_f32(),
x.to_f32(),
y.to_f32(),
dfrg marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
49 changes: 49 additions & 0 deletions skrifa/src/scale/cff/hint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! CFF hinting.

use read_fonts::{tables::postscript::dict::Blues, types::Fixed};

/// Parameters used to generate the stem and counter zones for the hinting
/// algorithm.
#[derive(Clone)]
pub(crate) struct HintParams {
pub blues: Blues,
pub family_blues: Blues,
pub other_blues: Blues,
pub family_other_blues: Blues,
pub blue_scale: Fixed,
pub blue_shift: Fixed,
pub blue_fuzz: Fixed,
pub language_group: i32,
}

impl Default for HintParams {
fn default() -> Self {
Self {
blues: Blues::default(),
other_blues: Blues::default(),
family_blues: Blues::default(),
family_other_blues: Blues::default(),
// See <https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-16-private-dict-operators>
blue_scale: Fixed::from_f64(0.039625),
blue_shift: Fixed::from_i32(7),
blue_fuzz: Fixed::ONE,
language_group: 0,
}
}
}

/// Hinting state for a PostScript subfont.
///
/// Note that hinter states depend on the scale, subfont index and
/// variation coordinates of a glyph. They can be retained and reused
/// if those values remain the same.
#[derive(Copy, Clone)]
pub(crate) struct HintState {
// TODO
}

impl HintState {
pub fn new(_params: &HintParams, _scale: Fixed) -> Self {
Self {}
}
}
9 changes: 9 additions & 0 deletions skrifa/src/scale/cff/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Support for scaling CFF outlines.

// Temporary until new scaler API is done.
#![allow(dead_code)]

mod hint;
mod scaler;

pub(crate) use scaler::{Scaler, Subfont};
Loading