Skip to content

Commit

Permalink
Use owned-ttf-parser 0.13.2 PreParsedSubtables
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Nov 6, 2021
1 parent a46153c commit 7cbdec7
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 229 deletions.
22 changes: 0 additions & 22 deletions dev/benches/font_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const OPENS_SANS_ITALIC: &[u8] = include_bytes!("../fonts/OpenSans-Italic.ttf");
fn bench_font_glyph_id(c: &mut Criterion) {
let font = FontRef::try_from_slice(OPENS_SANS_ITALIC).unwrap();

#[allow(deprecated)]
c.bench_function("method:Font::glyph_id", |b| {
let mut glyph = GlyphId(0);

Expand All @@ -20,15 +19,6 @@ fn bench_font_glyph_id(c: &mut Criterion) {
assert_eq!(glyph, GlyphId(91));
});

c.bench_function("method:GlyphIdentifier::glyph_id", |b| {
let c2g = font.glyph_identifier();
let mut glyph = GlyphId(0);

b.iter(|| glyph = c2g.glyph_id('x'));

assert_eq!(glyph, GlyphId(91));
});

c.bench_function("method:Font::h_advance", |b| {
let glyph = GlyphId(91);
let mut h_advance = 0.0;
Expand All @@ -38,7 +28,6 @@ fn bench_font_glyph_id(c: &mut Criterion) {
assert_relative_eq!(h_advance, 979.0);
});

#[allow(deprecated)]
c.bench_function("method:Font::kern_unscaled", |b| {
let glyph = GlyphId(91);
let glyph2 = GlyphId(92);
Expand All @@ -48,17 +37,6 @@ fn bench_font_glyph_id(c: &mut Criterion) {

assert_relative_eq!(kern, 0.0);
});

c.bench_function("method:Kerner::kern_unscaled", |b| {
let glyph = GlyphId(91);
let glyph2 = GlyphId(92);
let kerner = font.kerner();
let mut kern = 0.0;

b.iter(|| kern = kerner.kern_unscaled(glyph, glyph2));

assert_relative_eq!(kern, 0.0);
});
}

criterion_group!(
Expand Down
6 changes: 2 additions & 4 deletions dev/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub fn layout_paragraph<F, SF>(
F: Font,
SF: ScaleFont<F>,
{
let c2g = font.glyph_identifier();
let kerner = font.kerner();
let v_advance = font.height() + font.line_gap();
let mut caret = position + point(0.0, font.ascent());
let mut last_glyph: Option<Glyph> = None;
Expand All @@ -26,9 +24,9 @@ pub fn layout_paragraph<F, SF>(
}
continue;
}
let mut glyph = c2g.scaled_glyph(c);
let mut glyph = font.scaled_glyph(c);
if let Some(previous) = last_glyph.take() {
caret.x += kerner.kern(previous.id, glyph.id);
caret.x += font.kern(previous.id, glyph.id);
}
glyph.position = caret;

Expand Down
4 changes: 1 addition & 3 deletions dev/tests/render_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ fn reference_outline_draw_ttf_tailed_e() {
fn outline_draw<F: Font>(font: F, c: char, scale: f32) -> image::GrayAlphaImage {
let font = font.into_scaled(scale);

let glyph = font
.outline_glyph(font.glyph_identifier().scaled_glyph(c))
.unwrap();
let glyph = font.outline_glyph(font.scaled_glyph(c)).unwrap();
let bounds = glyph.px_bounds();

let mut glyph_image =
Expand Down
8 changes: 4 additions & 4 deletions glyph/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Unreleased
* Add `Font::glyph_identifier`, `Font::kerner` as re-usable better performing replacements
to `Font::glyph_id`, `Font::kern_unscaled` respectively, which are now deprecated.
Similar deprecations have happened, and equivalents are available, in `ScaleFont`.
* Update _ttf-parser_ to `0.13.1`.
* Update _owned-ttf-parser_ to `0.13.2`.
* Pre-parse cmap & kern subtables on all `Font` variants at initialization. This provides
much faster `glyph_id` & `kern` method performance, which are used heavily when positioning
glyphs into layouts.

# 0.2.11
* `Font::outline` will return `None` for rare invalid/empty glyph bounds instead of panicking.
Expand Down
2 changes: 1 addition & 1 deletion glyph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "Apache-2.0"
readme="README.md"

[dependencies]
owned_ttf_parser = { version = "0.13.1", default-features = false }
owned_ttf_parser = { version = "0.13.2", default-features = false }
ab_glyph_rasterizer = { version = "0.1.2", path = "../rasterizer", default-features = false }
# no_std float stuff
# renamed to enable a "libm" feature
Expand Down
23 changes: 2 additions & 21 deletions glyph/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
point,
ttfp::{GlyphIdentifier, Kerner},
Glyph, GlyphId, GlyphImage, Outline, OutlinedGlyph, PxScale, PxScaleFont, Rect, ScaleFont,
point, Glyph, GlyphId, GlyphImage, Outline, OutlinedGlyph, PxScale, PxScaleFont, Rect,
ScaleFont,
};

/// Functionality required from font data.
Expand Down Expand Up @@ -75,7 +74,6 @@ pub trait Font {
/// Lookup a `GlyphId` matching a given `char`.
///
/// Scaling can be done with [as_scaled](trait.Font.html#method.as_scaled).
#[deprecated(note = "Use & re-use glyph_identifier() for better performance")]
fn glyph_id(&self, c: char) -> GlyphId;

/// Unscaled horizontal advance for a given glyph id.
Expand All @@ -101,7 +99,6 @@ pub trait Font {
/// Returns additional unscaled kerning to apply for a particular pair of glyph ids.
///
/// Scaling can be done with [as_scaled](trait.Font.html#method.as_scaled).
#[deprecated(note = "Use & re-use kerner() for better performance")]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32;

/// Compute unscaled glyph outline curves & bounding box.
Expand Down Expand Up @@ -209,10 +206,6 @@ pub trait Font {
scale: scale.into(),
}
}

fn glyph_identifier(&self) -> GlyphIdentifier<'_>;

fn kerner(&self) -> Kerner<'_>;
}

impl<F: Font> Font for &F {
Expand All @@ -237,7 +230,6 @@ impl<F: Font> Font for &F {
}

#[inline]
#[allow(deprecated)]
fn glyph_id(&self, c: char) -> GlyphId {
(*self).glyph_id(c)
}
Expand All @@ -263,7 +255,6 @@ impl<F: Font> Font for &F {
}

#[inline]
#[allow(deprecated)]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32 {
(*self).kern_unscaled(first, second)
}
Expand All @@ -287,14 +278,4 @@ impl<F: Font> Font for &F {
fn glyph_raster_image(&self, id: GlyphId, size: u16) -> Option<GlyphImage> {
(*self).glyph_raster_image(id, size)
}

#[inline]
fn glyph_identifier(&self) -> GlyphIdentifier<'_> {
(*self).glyph_identifier()
}

#[inline]
fn kerner(&self) -> Kerner<'_> {
(*self).kerner()
}
}
17 changes: 1 addition & 16 deletions glyph/src/font_arc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
ttfp::{GlyphIdentifier, Kerner},
Font, FontRef, FontVec, GlyphId, GlyphImage, InvalidFont, Outline,
};
use crate::{Font, FontRef, FontVec, GlyphId, GlyphImage, InvalidFont, Outline};
use alloc::sync::Arc;
use core::fmt;

Expand Down Expand Up @@ -95,7 +92,6 @@ impl Font for FontArc {
}

#[inline]
#[allow(deprecated)]
fn glyph_id(&self, c: char) -> GlyphId {
self.0.glyph_id(c)
}
Expand All @@ -121,7 +117,6 @@ impl Font for FontArc {
}

#[inline]
#[allow(deprecated)]
fn kern_unscaled(&self, first: GlyphId, second: GlyphId) -> f32 {
self.0.kern_unscaled(first, second)
}
Expand All @@ -145,16 +140,6 @@ impl Font for FontArc {
fn glyph_raster_image(&self, id: GlyphId, size: u16) -> Option<GlyphImage> {
self.0.glyph_raster_image(id, size)
}

#[inline]
fn glyph_identifier(&self) -> GlyphIdentifier<'_> {
self.0.glyph_identifier()
}

#[inline]
fn kerner(&self) -> Kerner<'_> {
self.0.kerner()
}
}

impl From<FontVec> for FontArc {
Expand Down
2 changes: 1 addition & 1 deletion glyph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ pub use crate::{
glyph::*,
outlined::*,
scale::*,
ttfp::{FontRef, FontVec, GlyphIdentifier, GlyphImage, GlyphImageFormat, Kerner},
ttfp::{FontRef, FontVec, GlyphImage, GlyphImageFormat},
};
69 changes: 1 addition & 68 deletions glyph/src/scale.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Font, Glyph, GlyphId, GlyphIdentifier, OutlinedGlyph, Rect};
use crate::{Font, Glyph, GlyphId, OutlinedGlyph, Rect};

/// Pixel scale.
///
Expand Down Expand Up @@ -116,8 +116,6 @@ pub trait ScaleFont<F: Font> {

/// Lookup a `GlyphId` matching a given `char`.
#[inline]
#[allow(deprecated)]
#[deprecated(note = "Use & re-use glyph_identifier() for better performance")]
fn glyph_id(&self, c: char) -> GlyphId {
self.font().glyph_id(c)
}
Expand All @@ -139,8 +137,6 @@ pub trait ScaleFont<F: Font> {
/// assert_eq!(a1.position, point(0.0, 0.0));
/// ```
#[inline]
#[allow(deprecated)]
#[deprecated(note = "Use & re-use glyph_identifier() for better performance")]
fn scaled_glyph(&self, c: char) -> Glyph {
self.font().glyph_id(c).with_scale(self.scale())
}
Expand Down Expand Up @@ -171,8 +167,6 @@ pub trait ScaleFont<F: Font> {

/// Returns additional pixel scaled kerning to apply for a particular pair of glyphs.
#[inline]
#[allow(deprecated)]
#[deprecated(note = "Use & re-use kerner() for better performance")]
fn kern(&self, first: GlyphId, second: GlyphId) -> f32 {
self.h_scale_factor() * self.font().kern_unscaled(first, second)
}
Expand Down Expand Up @@ -209,10 +203,6 @@ pub trait ScaleFont<F: Font> {
fn outline_glyph(&self, glyph: Glyph) -> Option<OutlinedGlyph> {
self.font().outline_glyph(glyph)
}

fn glyph_identifier(&self) -> PxScaleGlyphIdentifier<'_>;

fn kerner(&self) -> PxScaleKerner<'_>;
}

impl<F: Font, SF: ScaleFont<F>> ScaleFont<F> for &SF {
Expand All @@ -230,16 +220,6 @@ impl<F: Font, SF: ScaleFont<F>> ScaleFont<F> for &SF {
fn codepoint_ids(&self) -> crate::CodepointIdIter<'_> {
(*self).codepoint_ids()
}

#[inline]
fn glyph_identifier(&self) -> PxScaleGlyphIdentifier<'_> {
(*self).glyph_identifier()
}

#[inline]
fn kerner(&self) -> PxScaleKerner<'_> {
(*self).kerner()
}
}

/// A [`Font`](trait.Font.html) and an associated pixel scale.
Expand Down Expand Up @@ -272,51 +252,4 @@ impl<F: Font> ScaleFont<F> for PxScaleFont<F> {
fn codepoint_ids(&self) -> crate::CodepointIdIter<'_> {
self.font.codepoint_ids()
}

#[inline]
fn glyph_identifier(&self) -> PxScaleGlyphIdentifier<'_> {
PxScaleGlyphIdentifier {
gider: self.font.glyph_identifier(),
scale: self.scale,
}
}

#[inline]
fn kerner(&self) -> PxScaleKerner<'_> {
PxScaleKerner {
kerner: self.font.kerner(),
h_scale_factor: self.h_scale_factor(),
}
}
}

#[derive(Debug)]
pub struct PxScaleKerner<'a> {
kerner: crate::Kerner<'a>,
h_scale_factor: f32,
}

impl PxScaleKerner<'_> {
#[inline]
pub fn kern(&self, first: GlyphId, second: GlyphId) -> f32 {
self.kerner.kern_unscaled(first, second) * self.h_scale_factor
}
}

#[derive(Debug)]
pub struct PxScaleGlyphIdentifier<'a> {
gider: GlyphIdentifier<'a>,
scale: PxScale,
}

impl PxScaleGlyphIdentifier<'_> {
#[inline]
pub fn glyph_id(&self, c: char) -> GlyphId {
self.gider.glyph_id(c)
}

#[inline]
pub fn scaled_glyph(&self, c: char) -> Glyph {
self.glyph_id(c).with_scale(self.scale)
}
}
Loading

0 comments on commit 7cbdec7

Please sign in to comment.