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

Implement a subset of FontTools Pens for write-fonts, leaning on kurbo for things like transform #237

Merged
merged 6 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion font-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use fword::{FWord, UfWord};
pub use glyph_id::GlyphId;
pub use longdatetime::LongDateTime;
pub use offset::{Nullable, Offset16, Offset24, Offset32};
pub use pen::Pen;
pub use pen::{Pen, PenCommand};
pub use point::Point;
pub use raw::{BigEndian, FixedSize, ReadScalar, Scalar};
pub use tag::{InvalidTag, Tag};
Expand Down
51 changes: 51 additions & 0 deletions font-types/src/pen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
///
/// This is a general abstraction to unify ouput for processes that decode and/or
/// transform outlines.
///
/// /// AbstractPen in Python terms.
/// <https://github.com/fonttools/fonttools/blob/78e10d8b42095b709cd4125e592d914d3ed1558e/Lib/fontTools/pens/basePen.py#L54>.
/// Implementations
pub trait Pen {
/// Emit a command to begin a new subpath at (x, y).
fn move_to(&mut self, x: f32, y: f32);
Expand All @@ -20,3 +24,50 @@ pub trait Pen {
/// Emit a command to close the current subpath.
fn close(&mut self);
}

/// Captures commands to [Pen] to facilitate implementations that buffer commands.
#[derive(Debug, Copy, Clone)]
pub enum PenCommand {
MoveTo {
x: f32,
y: f32,
},
LineTo {
x: f32,
y: f32,
},
QuadTo {
cx0: f32,
cy0: f32,
x: f32,
y: f32,
},
CurveTo {
cx0: f32,
cy0: f32,
cx1: f32,
cy1: f32,
x: f32,
y: f32,
},
Close,
}

impl PenCommand {
pub fn apply_to<T: Pen>(&self, pen: &mut T) {
match *self {
PenCommand::MoveTo { x, y } => pen.move_to(x, y),
PenCommand::LineTo { x, y } => pen.line_to(x, y),
PenCommand::QuadTo { cx0, cy0, x, y } => pen.quad_to(cx0, cy0, x, y),
PenCommand::CurveTo {
cx0,
cy0,
cx1,
cy1,
x,
y,
} => pen.curve_to(cx0, cy0, cx1, cy1, x, y),
PenCommand::Close => pen.close(),
}
}
}
1 change: 1 addition & 0 deletions write-fonts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod font_builder;
pub mod from_obj;
mod graph;
mod offsets;
pub mod pens;
pub mod tables;
pub mod validate;
mod write;
Expand Down
Loading