Skip to content

Commit

Permalink
Use skrifa::NormalizedCoord to represent normalized coordinates
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns committed Nov 27, 2024
1 parent f0dfc9e commit a4e3e09
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 40 deletions.
10 changes: 6 additions & 4 deletions src/internal/at.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! OpenType advanced typography tables.

use crate::NormalizedCoord;

use super::{raw_tag, Bytes, RawTag};

pub const GDEF: RawTag = raw_tag(b"GDEF");
Expand Down Expand Up @@ -116,7 +118,7 @@ impl<'a> Gdef<'a> {
self.var_store != 0
}

pub fn delta(&self, outer: u16, inner: u16, coords: &[i16]) -> f32 {
pub fn delta(&self, outer: u16, inner: u16, coords: &[NormalizedCoord]) -> f32 {
if self.var_store != 0 {
super::var::item_delta(self.data.data(), self.var_store, outer, inner, coords)
.map(|d| d.to_f32())
Expand Down Expand Up @@ -223,7 +225,7 @@ impl SubtableData {
pub struct FeatureSubsts(u32);

impl FeatureSubsts {
pub fn new(b: &Bytes, offset: u32, coords: &[i16]) -> Option<Self> {
pub fn new(b: &Bytes, offset: u32, coords: &[NormalizedCoord]) -> Option<Self> {
if offset == 0 || coords.is_empty() {
return None;
}
Expand All @@ -245,11 +247,11 @@ impl FeatureSubsts {
break;
}
let coord = coords[axis];
let min = b.read::<i16>(cond_table + 4)?;
let min = NormalizedCoord::from_bits(b.read::<i16>(cond_table + 4)?);
if coord < min {
break;
}
let max = b.read::<i16>(cond_table + 6)?;
let max = NormalizedCoord::from_bits(b.read::<i16>(cond_table + 6)?);
if coord > max {
break;
}
Expand Down
24 changes: 15 additions & 9 deletions src/internal/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use skrifa::raw::{FontData, FontRead};

use crate::NormalizedCoord;

use super::{fixed::Fixed, raw_tag, Array, Bytes, RawFont, RawTag, U24};

pub const FVAR: RawTag = raw_tag(b"fvar");
Expand Down Expand Up @@ -132,7 +134,11 @@ impl VarAxis {

/// Returns a normalized axis coordinate for the specified value in 2.14
/// fixed point format.
pub fn normalized_coord(&self, mut value: Fixed, avar: Option<(&[u8], u32)>) -> i16 {
pub fn normalized_coord(
&self,
mut value: Fixed,
avar: Option<(&[u8], u32)>,
) -> NormalizedCoord {
use core::cmp::Ordering::*;
if value < self.min {
value = self.min;
Expand All @@ -148,7 +154,7 @@ impl VarAxis {
value = avar
.and_then(|(data, avar)| adjust_axis(data, avar, self.index, value))
.unwrap_or(value);
value.to_f2dot14()
NormalizedCoord::from_bits(value.to_f2dot14())
}
}

Expand All @@ -164,14 +170,14 @@ pub struct VarInstance<'a> {
/// Metrics variation table.
pub struct Mvar<'a> {
data: Bytes<'a>,
coords: &'a [i16],
coords: &'a [NormalizedCoord],
rec_size: usize,
rec_count: usize,
store: u32,
}

impl<'a> Mvar<'a> {
pub fn new(data: &'a [u8], mvar: u32, coords: &'a [i16]) -> Option<Self> {
pub fn new(data: &'a [u8], mvar: u32, coords: &'a [NormalizedCoord]) -> Option<Self> {
let b = Bytes::with_offset(data, mvar as usize)?;
let rec_size = b.read::<u16>(6)? as usize;
let rec_count = b.read::<u16>(8)? as usize;
Expand Down Expand Up @@ -219,14 +225,14 @@ impl<'a> Mvar<'a> {
}

/// Returns the advance delta for the specified glyph.
pub fn advance_delta(data: &[u8], xvar: u32, glyph_id: u16, coords: &[i16]) -> f32 {
pub fn advance_delta(data: &[u8], xvar: u32, glyph_id: u16, coords: &[NormalizedCoord]) -> f32 {
metric_delta(data, xvar, 8, glyph_id, coords)
.map(|d| d.to_f32())
.unwrap_or(0.)
}

/// Returns the side bearing delta for the specified glyph.
pub fn sb_delta(data: &[u8], xvar: u32, glyph_id: u16, coords: &[i16]) -> f32 {
pub fn sb_delta(data: &[u8], xvar: u32, glyph_id: u16, coords: &[NormalizedCoord]) -> f32 {
metric_delta(data, xvar, 12, glyph_id, coords)
.map(|d| d.to_f32())
.unwrap_or(0.)
Expand Down Expand Up @@ -258,7 +264,7 @@ pub fn item_delta(
offset: u32,
outer: u16,
inner: u16,
coords: &[i16],
coords: &[NormalizedCoord],
) -> Option<Fixed> {
if offset == 0 {
return None;
Expand Down Expand Up @@ -302,7 +308,7 @@ pub fn item_delta(
let end = Fixed::from_f2dot14(b.read::<i16>(region_axis_base + 4)?);
let coord = coords
.get(axis)
.map(|c| Fixed::from_f2dot14(*c))
.map(|c| Fixed::from_f2dot14(c.to_bits()))
.unwrap_or(ZERO);
if start > peak || peak > end || peak == ZERO || start < ZERO && end > ZERO {
continue;
Expand Down Expand Up @@ -336,7 +342,7 @@ fn metric_delta(
base: u32,
which: usize,
glyph_id: u16,
coords: &[i16],
coords: &[NormalizedCoord],
) -> Option<Fixed> {
if base == 0 {
return None;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use proxy::BitmapStrikesProxy;
pub type GlyphId = u16;

/// Normalized variation coordinate in 2.14 fixed point format.
pub type NormalizedCoord = i16;
pub type NormalizedCoord = skrifa::instance::NormalizedCoord;

impl<'a> FontRef<'a> {
/// Returns the primary attributes for the font.
Expand Down
4 changes: 2 additions & 2 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub struct Metrics {
impl Metrics {
/// Creates a new set of metrics from the specified font and
/// normalized variation coordinates.
pub(crate) fn from_font(font: &FontRef, coords: &[i16]) -> Self {
pub(crate) fn from_font(font: &FontRef, coords: &[NormalizedCoord]) -> Self {
let meta = MetricsProxy::from_font(font);
meta.materialize_metrics(font, coords)
}
Expand Down Expand Up @@ -320,7 +320,7 @@ impl Metrics {
#[derive(Copy, Clone)]
pub struct GlyphMetrics<'a> {
data: &'a [u8],
coords: &'a [i16],
coords: &'a [NormalizedCoord],
units_per_em: u16,
glyph_count: u16,
hmtx: u32,
Expand Down
17 changes: 8 additions & 9 deletions src/scale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,8 @@ use hinting_cache::HintingCache;
use image::*;
use outline::*;
use skrifa::{
instance::{NormalizedCoord as SkrifaNormalizedCoord, Size as SkrifaSize},
outline::OutlineGlyphCollection,
GlyphId as SkrifaGlyphId, MetadataProvider,
instance::Size as SkrifaSize, outline::OutlineGlyphCollection, GlyphId as SkrifaGlyphId,
MetadataProvider,
};

use super::internal;
Expand Down Expand Up @@ -288,7 +287,7 @@ pub struct ScaleContext {
fonts: FontCache<ScalerProxy>,
state: State,
hinting_cache: HintingCache,
coords: Vec<SkrifaNormalizedCoord>,
coords: Vec<NormalizedCoord>,
}

struct State {
Expand Down Expand Up @@ -344,7 +343,7 @@ pub struct ScalerBuilder<'a> {
outlines: Option<OutlineGlyphCollection<'a>>,
proxy: &'a ScalerProxy,
id: [u64; 2],
coords: &'a mut Vec<SkrifaNormalizedCoord>,
coords: &'a mut Vec<NormalizedCoord>,
size: f32,
hint: bool,
}
Expand Down Expand Up @@ -403,7 +402,7 @@ impl<'a> ScalerBuilder<'a> {
if var.tag() == setting.tag {
let value = var.normalize(setting.value);
if let Some(c) = self.coords.get_mut(var.index()) {
*c = SkrifaNormalizedCoord::from_bits(value);
*c = value;
}
}
}
Expand All @@ -417,13 +416,13 @@ impl<'a> ScalerBuilder<'a> {
pub fn normalized_coords<I>(self, coords: I) -> Self
where
I: IntoIterator,
I::Item: Borrow<NormalizedCoord>,
I::Item: Borrow<i16>,
{
self.coords.clear();
self.coords.extend(
coords
.into_iter()
.map(|c| SkrifaNormalizedCoord::from_bits(*c.borrow())),
.map(|c| NormalizedCoord::from_bits(*c.borrow())),
);
self
}
Expand Down Expand Up @@ -470,7 +469,7 @@ pub struct Scaler<'a> {
outlines: Option<OutlineGlyphCollection<'a>>,
hinting_instance: Option<&'a skrifa::outline::HintingInstance>,
proxy: &'a ScalerProxy,
coords: &'a [SkrifaNormalizedCoord],
coords: &'a [NormalizedCoord],
size: f32,
skrifa_size: SkrifaSize,
}
Expand Down
11 changes: 6 additions & 5 deletions src/shape/at.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::internal::{at::*, *};
use super::{buffer::*, feature::*, Direction};
use crate::text::Script;
use crate::NormalizedCoord;

use alloc::vec::Vec;
use core::ops::Range;
Expand Down Expand Up @@ -287,7 +288,7 @@ impl FeatureStoreBuilder {
&mut self,
cache: &mut FeatureStore,
data: &[u8],
coords: &[i16],
coords: &[NormalizedCoord],
gdef: &Gdef,
gsub: &StageOffsets,
gpos: &StageOffsets,
Expand All @@ -309,7 +310,7 @@ impl FeatureStoreBuilder {
&mut self,
cache: &mut FeatureStore,
b: &Bytes,
coords: &[i16],
coords: &[NormalizedCoord],
gdef: &Gdef,
offsets: &StageOffsets,
stage: u8,
Expand Down Expand Up @@ -532,7 +533,7 @@ pub fn apply(
stage: u8,
data: &Bytes,
gsubgpos: u32,
coords: &[i16],
coords: &[NormalizedCoord],
gdef: &Gdef,
storage: &mut Storage,
cache: &FeatureStore,
Expand Down Expand Up @@ -590,7 +591,7 @@ struct ApplyContext<'a, 'b, 'c> {
data: &'a Bytes<'a>,
gsubgpos: u32,
defs: &'a Gdef<'a>,
coords: &'a [i16],
coords: &'a [NormalizedCoord],
enable_var: bool,
cache: &'a FeatureStore,
storage: &'b mut Storage,
Expand All @@ -608,7 +609,7 @@ impl<'a, 'b, 'c> ApplyContext<'a, 'b, 'c> {
data: &'a Bytes<'a>,
gsubgpos: u32,
defs: &'a Gdef<'a>,
coords: &'a [i16],
coords: &'a [NormalizedCoord],
cache: &'a FeatureStore,
storage: &'b mut Storage,
buffer: &'c mut Buffer,
Expand Down
8 changes: 4 additions & 4 deletions src/shape/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::at::FeatureStore;
use super::engine::EngineMetadata;
use super::internal::var::Fvar;
use crate::{charmap::CharmapProxy, metrics::MetricsProxy, FontRef};
use crate::{charmap::CharmapProxy, metrics::MetricsProxy, FontRef, NormalizedCoord};

use alloc::vec::Vec;

Expand Down Expand Up @@ -30,7 +30,7 @@ impl FontEntry {
pub struct FeatureEntry {
pub epoch: Epoch,
pub id: [u64; 2],
pub coords: Vec<i16>,
pub coords: Vec<NormalizedCoord>,
pub tags: [u32; 4],
pub store: FeatureStore,
}
Expand Down Expand Up @@ -58,7 +58,7 @@ impl FeatureCache {
pub fn entry<'a>(
&'a mut self,
id: [u64; 2],
coords: &[i16],
coords: &[NormalizedCoord],
has_feature_vars: bool,
tags: &[u32; 4],
) -> FeatureCacheEntry<'a> {
Expand All @@ -80,7 +80,7 @@ impl FeatureCache {
fn find_entry(
&mut self,
id: [u64; 2],
coords: &[i16],
coords: &[NormalizedCoord],
has_feature_vars: bool,
tags: &[u32; 4],
) -> (bool, usize) {
Expand Down
5 changes: 3 additions & 2 deletions src/shape/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::buffer::*;
use super::internal::{self, at::Gdef, raw_tag, Bytes, RawFont, RawTag};
use crate::font::FontRef;
use crate::text::{Language, Script};
use crate::NormalizedCoord;

use alloc::vec::Vec;
use core::ops::Range;
Expand All @@ -21,7 +22,7 @@ pub struct Engine<'a> {
pub ankr: u32,
pub kern: u32,
pub storage: at::Storage,
pub coords: &'a [i16],
pub coords: &'a [NormalizedCoord],
pub script: Script,
pub lang: Option<Language>,
pub tags: [RawTag; 4],
Expand All @@ -37,7 +38,7 @@ impl<'a> Engine<'a> {
pub fn new(
metadata: &EngineMetadata,
font_data: &'a [u8],
coords: &'a [i16],
coords: &'a [NormalizedCoord],
script: Script,
lang: Option<Language>,
) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions src/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub enum Direction {
pub struct ShapeContext {
font_cache: FontCache<FontEntry>,
feature_cache: FeatureCache,
coords: Vec<i16>,
coords: Vec<NormalizedCoord>,
state: State,
}

Expand Down Expand Up @@ -370,7 +370,7 @@ pub struct ShaperBuilder<'a> {
font: FontRef<'a>,
font_id: [u64; 2],
font_entry: &'a FontEntry,
coords: &'a mut Vec<i16>,
coords: &'a mut Vec<NormalizedCoord>,
charmap: Charmap<'a>,
dotted_circle: Option<u16>,
retain_ignorables: bool,
Expand Down Expand Up @@ -466,7 +466,7 @@ impl<'a> ShaperBuilder<'a> {
{
if self.font_entry.coord_count != 0 {
let vars = self.font.variations();
self.coords.resize(vars.len(), 0);
self.coords.resize(vars.len(), NormalizedCoord::ZERO);
for setting in settings {
let setting = setting.into();
for var in vars {
Expand Down
2 changes: 1 addition & 1 deletion src/variation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a> Variations<'a> {
{
let mut copy = *self;
copy.pos = 0;
let mut coords = [0i16; 32];
let mut coords = [NormalizedCoord::ZERO; 32];
let len = self.len.min(32);
for setting in settings {
let val = setting.into();
Expand Down

0 comments on commit a4e3e09

Please sign in to comment.