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

Initial glyph loading/scaling crate #191

Merged
merged 29 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
70d40a8
initial pared down glyph loader
dfrg Dec 20, 2022
675b274
oh, clippy
dfrg Dec 20, 2022
e9a0c0c
simplify high level glyph representation
dfrg Dec 21, 2022
0385640
add C API mockup doc
dfrg Dec 21, 2022
051427e
C requires parens around if conditions
dfrg Dec 21, 2022
6d694d0
Merge branch 'googlefonts:main' into load-glyphs
dfrg Dec 21, 2022
3b2654a
Update punchcut/README.md
dfrg Jan 11, 2023
17dd7ab
Update punchcut/src/scaler.rs
dfrg Jan 11, 2023
0b44e5d
Update punchcut/src/source/glyf/scaler.rs
dfrg Jan 11, 2023
aa55c79
Update punchcut/src/scaler.rs comment formatting
dfrg Jan 11, 2023
675d406
Merge branch 'googlefonts:main' into load-glyphs
dfrg Jan 11, 2023
6b374a6
update read-fonts version
dfrg Jan 11, 2023
737e0e9
break GlyphScaler::load() into separate functions
dfrg Jan 11, 2023
7c28f2c
rename hinting modes
dfrg Jan 11, 2023
3671a00
change CacheSlot::Cached to a struct variant with named fields
dfrg Jan 11, 2023
f5a4fa7
remove currently unused math functions
dfrg Jan 11, 2023
e5fa151
Remove Outline type and add PathSink trait
dfrg Jan 11, 2023
a3342dd
derive Default for Context
dfrg Jan 11, 2023
e276383
update README source matrix
dfrg Jan 13, 2023
ecba6f3
feature gate hinting code
dfrg Jan 13, 2023
96d007d
Remove peniko/kurbo dependency and...
dfrg Jan 13, 2023
582a590
Improve single letter variable names and rename CONIC -> QUAD
dfrg Jan 13, 2023
3ad6a4e
derive debug and clone for more types
dfrg Jan 13, 2023
3008574
move variation setup in ScalerBuilder::build() to separate function
dfrg Jan 13, 2023
3e10a07
Merge branch 'googlefonts:main' into load-glyphs
dfrg Jan 15, 2023
5483eee
Address more review feedback
dfrg Jan 15, 2023
dd7134d
fmt and fix mismatched args in feature gated code
dfrg Jan 15, 2023
c81c504
fmt --all
dfrg Jan 15, 2023
40d4cdc
fix weird clippy lint
dfrg Jan 15, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ members = [
"font-codegen",
"write-fonts",
"otexplorer",
"punchcut",
]
179 changes: 94 additions & 85 deletions font-types/src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,96 @@ macro_rules! fixed_impl {
};
}

/// Implements multiplication and division operators for fixed types.
macro_rules! fixed_mul_div {
($ty:ty) => {
impl $ty {
/// Multiplies `self` by `a` and divides the product by `b`.
pub const fn mul_div(&self, a: Self, b: Self) -> Self {
let mut sign = 1;
let mut su = self.0 as u64;
let mut au = a.0 as u64;
let mut bu = b.0 as u64;
if self.0 < 0 {
su = 0u64.wrapping_sub(su);
sign = -1;
}
if a.0 < 0 {
au = 0u64.wrapping_sub(au);
sign = -sign;
}
if b.0 < 0 {
bu = 0u64.wrapping_sub(bu);
sign = -sign;
}
let result = if bu > 0 {
su.wrapping_mul(au).wrapping_add(bu >> 1) / bu
} else {
0x7FFFFFFF
};
Self(if sign < 0 {
-(result as i32)
} else {
result as i32
})
}
}

impl Mul for $ty {
type Output = Self;
#[inline(always)]
fn mul(self, other: Self) -> Self::Output {
let ab = self.0 as i64 * other.0 as i64;
Self(((ab + 0x8000 - i64::from(ab < 0)) >> 16) as i32)
}
}

impl MulAssign for $ty {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl Div for $ty {
type Output = Self;
#[inline(always)]
fn div(self, other: Self) -> Self::Output {
let mut sign = 1;
let mut a = self.0;
let mut b = other.0;
if a < 0 {
a = -a;
sign = -1;
}
if b < 0 {
b = -b;
sign = -sign;
}
let q = if b == 0 {
0x7FFFFFFF
} else {
((((a as u64) << 16) + ((b as u64) >> 1)) / (b as u64)) as u32
};
Self(if sign < 0 { -(q as i32) } else { q as i32 })
}
}

impl DivAssign for $ty {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}

impl Neg for $ty {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
Self(-self.0)
}
}
};
}

/// impl float conversion methods.
///
/// We convert to different float types in order to ensure we can roundtrip
Expand Down Expand Up @@ -167,8 +257,12 @@ macro_rules! float_conv {

fixed_impl!(F2Dot14, 16, 14, i16);
fixed_impl!(Fixed, 32, 16, i32);
fixed_impl!(F26Dot6, 32, 6, i32);
fixed_mul_div!(Fixed);
fixed_mul_div!(F26Dot6);
float_conv!(F2Dot14, to_f32, from_f32, f32);
float_conv!(Fixed, to_f64, from_f64, f64);
float_conv!(F26Dot6, to_f64, from_f64, f64);
crate::newtype_scalar!(F2Dot14, [u8; 2]);
crate::newtype_scalar!(Fixed, [u8; 4]);

Expand All @@ -192,91 +286,6 @@ impl F2Dot14 {
}
}

impl Fixed {
/// Multiplies `self` by `a` and divides the product by `b`.
pub const fn mul_div(&self, a: Fixed, b: Fixed) -> Fixed {
let mut sign = 1;
let mut su = self.0 as u64;
let mut au = a.0 as u64;
let mut bu = b.0 as u64;
if self.0 < 0 {
su = 0u64.wrapping_sub(su);
sign = -1;
}
if a.0 < 0 {
au = 0u64.wrapping_sub(au);
sign = -sign;
}
if b.0 < 0 {
bu = 0u64.wrapping_sub(bu);
sign = -sign;
}
let result = if bu > 0 {
su.wrapping_mul(au).wrapping_add(bu >> 1) / bu
} else {
0x7FFFFFFF
};
Fixed(if sign < 0 {
-(result as i32)
} else {
result as i32
})
}
}

impl Mul for Fixed {
type Output = Self;
#[inline(always)]
fn mul(self, other: Self) -> Self::Output {
let ab = self.0 as i64 * other.0 as i64;
Self(((ab + 0x8000 - i64::from(ab < 0)) >> 16) as i32)
}
}

impl MulAssign for Fixed {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl Div for Fixed {
type Output = Self;
#[inline(always)]
fn div(self, other: Self) -> Self::Output {
let mut sign = 1;
let mut a = self.0;
let mut b = other.0;
if a < 0 {
a = -a;
sign = -1;
}
if b < 0 {
b = -b;
sign = -sign;
}
let q = if b == 0 {
0x7FFFFFFF
} else {
((((a as u64) << 16) + ((b as u64) >> 1)) / (b as u64)) as u32
};
Self(if sign < 0 { -(q as i32) } else { q as i32 })
}
}

impl DivAssign for Fixed {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}

impl Neg for Fixed {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
Self(-self.0)
}
}

#[cfg(test)]
mod tests {
#![allow(overflowing_literals)] // we want to specify byte values directly
Expand Down
2 changes: 1 addition & 1 deletion font-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod tag;
mod uint24;
mod version;

pub use fixed::{F2Dot14, Fixed};
pub use fixed::{F26Dot6, F2Dot14, Fixed};
pub use fword::{FWord, UfWord};
pub use glyph_id::GlyphId;
pub use longdatetime::LongDateTime;
Expand Down
18 changes: 18 additions & 0 deletions punchcut/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "punchcut"
version = "0.1.0"
edition = "2021"
license = "MIT/Apache-2.0"
description = "Loading glyphs from OpenType font files."
repository = "https://github.com/googlefonts/fontations"
readme = "README.md"
categories = ["text-processing", "parsing", "graphics"]

[features]
hinting = []

[dependencies]
read-fonts = { version = "0.0.5", path = "../read-fonts" }

[dev-dependencies]
read-fonts = { version = "0.0.5", path = "../read-fonts", features = ["test_data"] }
37 changes: 37 additions & 0 deletions punchcut/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# punchcut

This is a library for high level loading of glyph outlines (and eventually color outlines and bitmaps)
from font files. The intention is fully featured (e.g. variations and hinting) support for all glyph sources
except for the SVG table.

This is part of the [oxidize](https://github.com/googlefonts/oxidize) project.

## Features

Current (✔️), near term (🔜) and planned (⌛) feature matrix:

| Source | Loading | Variations | Hinting |
|--------|---------|------------|---------|
| glyf | ✔️ | 🔜 | ⌛* |
| CFF | ⌛ | ⌛ | ⌛ |
| CFF2 | ⌛ | ⌛ | ⌛ |
| COLRv0 | 🔜 | 🔜 | * |
| COLRv1 | 🔜 | 🔜 | * |
| EBDT | 🔜 | - | - |
| CBDT | 🔜 | - | - |
| sbix | 🔜 | - | - |

\* A working implementation exists for hinting but is not yet merged.

\*\* This will be supported but is probably not desirable due the general affine transforms
present in the paint graph.

## The name?

Wikipedia says "[punchcutting](https://en.wikipedia.org/wiki/Punchcutting) is a craft used in traditional
typography to cut letter punches in steel as the first stage of making metal type." The punches carry the
outline of the desired letter which can be used to create a mold to transfer the design to various
surfaces.

The primary purpose of this crate is the generation of outlines from font data, so the name seemed
appropriate.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide (a link could suffice) a basic example of use, say loading a font from a file and getting the outline of something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some example code. Inline is fairly common in Rust READMEs

55 changes: 55 additions & 0 deletions punchcut/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use read_fonts::{types::GlyphId, ReadError};
dfrg marked this conversation as resolved.
Show resolved Hide resolved

use std::fmt;

/// Errors that may occur when loading glyphs.
#[derive(Clone, Debug)]
pub enum Error {
/// No viable sources were available.
NoSources,
/// The requested glyph was not present in the font.
GlyphNotFound(GlyphId),
/// Exceeded a recursion limit when loading a glyph.
RecursionLimitExceeded(GlyphId),
/// Error occured during hinting.
#[cfg(feature = "hinting")]
HintingFailed(GlyphId),
/// An anchor point had invalid indices.
InvalidAnchorPoint(GlyphId, u16),
/// Error occured when reading font data.
Read(ReadError),
}

impl From<ReadError> for Error {
fn from(e: ReadError) -> Self {
Self::Read(e)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NoSources => write!(f, "No glyph sources are available for the given font"),
Self::GlyphNotFound(gid) => write!(f, "Glyph {} was not found in the given font", gid),
Self::RecursionLimitExceeded(gid) => write!(
dfrg marked this conversation as resolved.
Show resolved Hide resolved
f,
"Recursion limit ({}) exceeded when loading composite component {}",
crate::GLYF_COMPOSITE_RECURSION_LIMIT,
gid
),
#[cfg(feature = "hinting")]
Self::HintingFailed(gid) => write!(f, "Bad hinting bytecode for glyph {}", gid),
dfrg marked this conversation as resolved.
Show resolved Hide resolved
Self::InvalidAnchorPoint(gid, index) => write!(
f,
"Invalid anchor point index ({}) for composite glyph {}",
index, gid
),
Self::Read(e) => write!(f, "{}", e),
}
}
}

impl std::error::Error for Error {}

/// Result type for errors that may occur when loading glyphs.
pub type Result<T> = core::result::Result<T, Error>;
Loading