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

Simplify COLR API for transforms #155

Merged
merged 11 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ jobs:
toolchain: ${{ matrix.rust }}
override: true

- name: Build
run: cargo build --no-default-features
- name: Build with no-std-float
run: cargo build --no-default-features --features no-std-float

- name: Build with std
run: cargo build --no-default-features --features std

- name: Build with variable-fonts
run: cargo build --no-default-features --features variable-fonts
run: cargo build --no-default-features --features variable-fonts,no-std-float

- name: Build with all features
run: cargo build --all-features
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ readme = "README.md"
edition = "2018"
exclude = ["benches/**"]

[dependencies]
libm = { version = "0.2.8", optional = true }

[features]
default = ["std", "opentype-layout", "apple-layout", "variable-fonts", "glyph-names"]
# Enables the use of the standard library. Deactivate this and activate the no-std
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
# feature to compile for targets that don't have std.
std = []
no-std-float = ["libm"]
# Enables variable fonts support. Increases binary size almost twice.
# Includes avar, CFF2, fvar, gvar, HVAR, MVAR and VVAR tables.
variable-fonts = []
Expand Down
20 changes: 0 additions & 20 deletions examples/font2svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,26 +544,6 @@ impl<'a> ttf::colr::Painter<'a> for GlyphPainter<'a> {
self.svg.end_element(); // g
}

fn push_translate(&mut self, tx: f32, ty: f32) {
self.push_transform(ttf::Transform::new(1.0, 0.0, 0.0, 1.0, tx, ty));
}

fn push_scale(&mut self, sx: f32, sy: f32) {
self.push_transform(ttf::Transform::new(sx, 0.0, 0.0, sy, 0.0, 0.0));
}

fn push_rotate(&mut self, angle: f32) {
let cc = (angle * std::f32::consts::PI).cos();
let ss = (angle * std::f32::consts::PI).sin();
self.push_transform(ttf::Transform::new(cc, ss, -ss, cc, 0.0, 0.0));
}

fn push_skew(&mut self, skew_x: f32, skew_y: f32) {
let x = (-skew_x * std::f32::consts::PI).tan();
let y = (skew_y * std::f32::consts::PI).tan();
self.push_transform(ttf::Transform::new(1.0, y, x, 1.0, 0.0, 0.0));
}

fn push_transform(&mut self, transform: ttf::Transform) {
self.transforms_stack.push(self.transform);
self.transform = ttf::Transform::combine(self.transform, transform);
Expand Down
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ impl Transform {
Transform::new(1.0, 0.0, 0.0, 1.0, tx, ty)
}

/// Creates a new rotation transform.
#[inline]
pub fn new_rotate(angle: f32) -> Self {
let cc = (angle * core::f32::consts::PI).cos();
let ss = (angle * core::f32::consts::PI).sin();

Transform::new(cc, ss, -ss, cc, 0.0, 0.0)
}

/// Creates a new skew transform.
#[inline]
pub fn new_skew(skew_x: f32, skew_y: f32) -> Self {
let x = (skew_x * core::f32::consts::PI).tan();
let y = (skew_y * core::f32::consts::PI).tan();

Transform::new(1.0, y, -x, 1.0, 0.0, 0.0)
}

/// Creates a new scale transform.
#[inline]
pub fn new_scale(sx: f32, sy: f32) -> Self {
Transform::new(sx, 0.0, 0.0, sy, 0.0, 0.0)
}

/// Combines two transforms with each other.
#[inline]
pub fn combine(ts1: Self, ts2: Self) -> Self {
Expand Down Expand Up @@ -2347,3 +2371,24 @@ pub fn fonts_in_collection(data: &[u8]) -> Option<u32> {
s.skip::<u32>(); // version
s.read::<u32>()
}

#[allow(missing_docs)]
#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
pub(crate) trait NoStdFloat {
fn sin(self) -> Self;
fn cos(self) -> Self;
fn tan(self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
impl NoStdFloat for f32 {
fn sin(self) -> Self {
libm::sinf(self)
}
fn cos(self) -> Self {
libm::cosf(self)
}
fn tan(self) -> Self {
libm::tanf(self)
}
}
78 changes: 34 additions & 44 deletions src/tables/colr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,16 +694,6 @@ pub trait Painter<'a> {
fn push_layer(&mut self, mode: CompositeMode);
/// Pop the last layer.
fn pop_layer(&mut self);

// TODO: Unify transforms into one callback.
/// Push a translation transform.
fn push_translate(&mut self, tx: f32, ty: f32);
/// Push a scaling transform.
fn push_scale(&mut self, sx: f32, sy: f32);
/// Push a rotation transform.
fn push_rotate(&mut self, angle: f32);
/// Push a skewing transform.
fn push_skew(&mut self, skew_x: f32, skew_y: f32);
/// Push a transform.
fn push_transform(&mut self, transform: Transform);
/// Pop the last transform.
Expand Down Expand Up @@ -1331,7 +1321,7 @@ impl<'a> Table<'a> {
let tx = f32::from(s.read::<i16>()?);
let ty = f32::from(s.read::<i16>()?);

painter.push_translate(tx, ty);
painter.push_transform(Transform::new_translate(tx, ty));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1359,7 +1349,7 @@ impl<'a> Table<'a> {
let tx = f32::from(s.read::<i16>()?) + deltas[0];
let ty = f32::from(s.read::<i16>()?) + deltas[1];

painter.push_translate(tx, ty);
painter.push_transform(Transform::new_translate(tx, ty));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1376,7 +1366,7 @@ impl<'a> Table<'a> {
let sx = s.read::<F2DOT14>()?.to_f32();
let sy = s.read::<F2DOT14>()?.to_f32();

painter.push_scale(sx, sy);
painter.push_transform(Transform::new_scale(sx, sy));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1404,7 +1394,7 @@ impl<'a> Table<'a> {
let sx = s.read::<F2DOT14>()?.apply_float_delta(deltas[0]);
let sy = s.read::<F2DOT14>()?.apply_float_delta(deltas[1]);

painter.push_scale(sx, sy);
painter.push_transform(Transform::new_scale(sx, sy));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1423,9 +1413,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?);
let center_y = f32::from(s.read::<i16>()?);

painter.push_translate(center_x, center_y);
painter.push_scale(sx, sy);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_scale(sx, sy));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1457,9 +1447,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?) + deltas[2];
let center_y = f32::from(s.read::<i16>()?) + deltas[3];

painter.push_translate(center_x, center_y);
painter.push_scale(sx, sy);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_scale(sx, sy));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1477,7 +1467,7 @@ impl<'a> Table<'a> {
let paint_offset = s.read::<Offset24>()?;
let scale = s.read::<F2DOT14>()?.to_f32();

painter.push_scale(scale, scale);
painter.push_transform(Transform::new_scale(scale, scale));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1504,7 +1494,7 @@ impl<'a> Table<'a> {

let scale = s.read::<F2DOT14>()?.apply_float_delta(deltas[0]);

painter.push_scale(scale, scale);
painter.push_transform(Transform::new_scale(scale, scale));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1522,9 +1512,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?);
let center_y = f32::from(s.read::<i16>()?);

painter.push_translate(center_x, center_y);
painter.push_scale(scale, scale);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_scale(scale, scale));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1555,9 +1545,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?) + deltas[1];
let center_y = f32::from(s.read::<i16>()?) + deltas[2];

painter.push_translate(center_x, center_y);
painter.push_scale(scale, scale);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_scale(scale, scale));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1575,7 +1565,7 @@ impl<'a> Table<'a> {
let paint_offset = s.read::<Offset24>()?;
let angle = s.read::<F2DOT14>()?.to_f32();

painter.push_rotate(angle);
painter.push_transform(Transform::new_rotate(angle));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1602,7 +1592,7 @@ impl<'a> Table<'a> {

let angle = s.read::<F2DOT14>()?.apply_float_delta(deltas[0]);

painter.push_rotate(angle);
painter.push_transform(Transform::new_rotate(angle));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1620,9 +1610,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?);
let center_y = f32::from(s.read::<i16>()?);

painter.push_translate(center_x, center_y);
painter.push_rotate(angle);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_rotate(angle));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1653,9 +1643,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?) + deltas[1];
let center_y = f32::from(s.read::<i16>()?) + deltas[2];

painter.push_translate(center_x, center_y);
painter.push_rotate(angle);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_rotate(angle));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1674,7 +1664,7 @@ impl<'a> Table<'a> {
let skew_x = s.read::<F2DOT14>()?.to_f32();
let skew_y = s.read::<F2DOT14>()?.to_f32();

painter.push_skew(skew_x, skew_y);
painter.push_transform(Transform::new_skew(skew_x, skew_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1702,7 +1692,7 @@ impl<'a> Table<'a> {
let skew_x = s.read::<F2DOT14>()?.apply_float_delta(deltas[0]);
let skew_y = s.read::<F2DOT14>()?.apply_float_delta(deltas[1]);

painter.push_skew(skew_x, skew_y);
painter.push_transform(Transform::new_skew(skew_x, skew_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand All @@ -1721,9 +1711,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?);
let center_y = f32::from(s.read::<i16>()?);

painter.push_translate(center_x, center_y);
painter.push_skew(skew_x, skew_y);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_skew(skew_x, skew_y));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down Expand Up @@ -1755,9 +1745,9 @@ impl<'a> Table<'a> {
let center_x = f32::from(s.read::<i16>()?) + deltas[2];
let center_y = f32::from(s.read::<i16>()?) + deltas[3];

painter.push_translate(center_x, center_y);
painter.push_skew(skew_x, skew_y);
painter.push_translate(-center_x, -center_y);
painter.push_transform(Transform::new_translate(center_x, center_y));
painter.push_transform(Transform::new_skew(skew_x, skew_y));
painter.push_transform(Transform::new_translate(-center_x, -center_y));
self.parse_paint(
offset + paint_offset.to_usize(),
palette,
Expand Down
Loading
Loading