Skip to content

Commit

Permalink
Reformat code.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Sep 28, 2022
1 parent c9e9637 commit 642af90
Show file tree
Hide file tree
Showing 63 changed files with 1,779 additions and 1,169 deletions.
33 changes: 12 additions & 21 deletions benches/methods_perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,49 @@ fn from_data_otf_cff2(bencher: &mut bencher::Bencher) {
fn outline_glyph_8_from_glyf(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansPro-Regular.ttf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(8), &mut Builder(0))
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(8), &mut Builder(0)))
}

fn outline_glyph_276_from_glyf(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansPro-Regular.ttf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
let mut b = Builder(0);
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(276), &mut b)
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(276), &mut b))
}

fn outline_glyph_8_from_cff(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansPro-Regular.otf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(8), &mut Builder(0))
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(8), &mut Builder(0)))
}

fn outline_glyph_276_from_cff(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansPro-Regular.otf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(276), &mut Builder(0))
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(276), &mut Builder(0)))
}

fn outline_glyph_8_from_cff2(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansVariable-Roman.otf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(8), &mut Builder(0))
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(8), &mut Builder(0)))
}

fn outline_glyph_276_from_cff2(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansVariable-Roman.otf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
face.outline_glyph(ttf::GlyphId(276), &mut Builder(0))
})
bencher.iter(|| face.outline_glyph(ttf::GlyphId(276), &mut Builder(0)))
}

fn family_name(bencher: &mut bencher::Bencher) {
let font_data = std::fs::read("fonts/SourceSansPro-Regular.ttf").unwrap();
let face = ttf::Face::parse(&font_data, 0).unwrap();
bencher.iter(|| {
bencher::black_box(
face.names().into_iter().find(|name| name.name_id == ttf::name_id::FULL_NAME)
.and_then(|name| name.to_string())
face.names()
.into_iter()
.find(|name| name.name_id == ttf::name_id::FULL_NAME)
.and_then(|name| name.to_string()),
);
})
}
Expand Down Expand Up @@ -154,7 +144,8 @@ impl ttf_parser::OutlineBuilder for Builder {
}
}

bencher::benchmark_group!(perf,
bencher::benchmark_group!(
perf,
from_data_ttf,
from_data_otf_cff,
from_data_otf_cff2,
Expand Down
3 changes: 2 additions & 1 deletion benches/methods_perf_x1000.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ fn glyph_hor_side_bearing(bencher: &mut bencher::Bencher) {
})
}

bencher::benchmark_group!(perf,
bencher::benchmark_group!(
perf,
units_per_em,
width,
ascender,
Expand Down
22 changes: 15 additions & 7 deletions examples/font-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ fn main() {
Err(e) => {
eprint!("Error: {}.", e);
std::process::exit(1);
},
}
};

let family_name = face.names().into_iter()
let family_name = face
.names()
.into_iter()
.find(|name| name.name_id == ttf_parser::name_id::FULL_NAME && name.is_unicode())
.and_then(|name| name.to_string());

let post_script_name = face.names().into_iter()
let post_script_name = face
.names()
.into_iter()
.find(|name| name.name_id == ttf_parser::name_id::POST_SCRIPT_NAME && name.is_unicode())
.and_then(|name| name.to_string());

Expand All @@ -46,7 +50,8 @@ fn main() {
println!("Superscript: {:?}", face.superscript_metrics());
println!("Variable: {:?}", face.is_variable());

#[cfg(feature = "opentype-layout")] {
#[cfg(feature = "opentype-layout")]
{
if let Some(ref table) = face.tables().gpos {
print_opentype_layout("positioning", table);
}
Expand All @@ -56,12 +61,15 @@ fn main() {
}
}

#[cfg(feature = "variable-fonts")] {
#[cfg(feature = "variable-fonts")]
{
if face.is_variable() {
println!("Variation axes:");
for axis in face.variation_axes() {
println!(" {} {}..{}, default {}",
axis.tag, axis.min_value, axis.max_value, axis.def_value);
println!(
" {} {}..{}, default {}",
axis.tag, axis.min_value, axis.max_value, axis.def_value
);
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions examples/font2svg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::PathBuf;
use std::io::Write;
use std::path::PathBuf;

use ttf_parser as ttf;

Expand All @@ -13,7 +13,8 @@ Usage:
";

struct Args {
#[allow(dead_code)] variations: Vec<ttf::Variation>,
#[allow(dead_code)]
variations: Vec<ttf::Variation>,
ttf_path: PathBuf,
svg_path: PathBuf,
}
Expand Down Expand Up @@ -81,7 +82,8 @@ fn process(args: Args) -> Result<(), Box<dyn std::error::Error>> {
#[allow(unused_mut)]
let mut face = ttf::Face::parse(&font_data, 0)?;
if face.is_variable() {
#[cfg(feature = "variable-fonts")] {
#[cfg(feature = "variable-fonts")]
{
for variation in args.variations {
face.set_variation(variation.axis, variation.value)
.ok_or("failed to create variation coordinates")?;
Expand All @@ -104,7 +106,13 @@ fn process(args: Args) -> Result<(), Box<dyn std::error::Error>> {
svg.write_attribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
svg.write_attribute_fmt(
"viewBox",
format_args!("{} {} {} {}", 0, 0, cell_size * COLUMNS as f64, cell_size * rows as f64),
format_args!(
"{} {} {} {}",
0,
0,
cell_size * COLUMNS as f64,
cell_size * rows as f64
),
);

draw_grid(face.number_of_glyphs(), cell_size, &mut svg);
Expand Down Expand Up @@ -179,11 +187,7 @@ fn process(args: Args) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn draw_grid(
n_glyphs: u16,
cell_size: f64,
svg: &mut xmlwriter::XmlWriter,
) {
fn draw_grid(n_glyphs: u16, cell_size: f64, svg: &mut xmlwriter::XmlWriter) {
let columns = COLUMNS;
let rows = (n_glyphs as f64 / columns as f64).ceil() as u32;

Expand Down
70 changes: 39 additions & 31 deletions src/aat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ related types.

use core::num::NonZeroU16;

use crate::parser::{FromData, LazyArray16, NumFrom, Offset, Offset16, Offset32, Stream};
use crate::GlyphId;
use crate::parser::{Stream, FromData, LazyArray16, Offset, Offset16, Offset32, NumFrom};

/// Predefined states.
pub mod state {
Expand Down Expand Up @@ -188,9 +188,9 @@ impl<'a> StateTable<'a> {
class = class::OUT_OF_BOUNDS as u8;
}

let entry_idx = self.state_array.get(
usize::from(state) * usize::from(self.number_of_classes) + usize::from(class)
)?;
let entry_idx = self
.state_array
.get(usize::from(state) * usize::from(self.number_of_classes) + usize::from(class))?;

Stream::read_at(self.entry_table, usize::from(*entry_idx) * StateEntry::SIZE)
}
Expand Down Expand Up @@ -218,7 +218,6 @@ impl core::fmt::Debug for StateTable<'_> {
}
}


/// An [Extended State Table](
/// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6Tables.html).
///
Expand Down Expand Up @@ -283,7 +282,10 @@ impl<'a, T: FromData> ExtendedStateTable<'a, T> {
usize::from(state) * usize::num_from(self.number_of_classes) + usize::from(class);

let entry_idx: u16 = Stream::read_at(self.state_array, state_idx * u16::SIZE)?;
Stream::read_at(self.entry_table, usize::from(entry_idx) * GenericStateEntry::<T>::SIZE)
Stream::read_at(
self.entry_table,
usize::from(entry_idx) * GenericStateEntry::<T>::SIZE,
)
}
}

Expand All @@ -293,7 +295,6 @@ impl<T> core::fmt::Debug for ExtendedStateTable<'_, T> {
}
}


/// A [lookup table](
/// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6Tables.html).
///
Expand Down Expand Up @@ -326,7 +327,6 @@ impl core::fmt::Debug for Lookup<'_> {
}
}


#[derive(Clone)]
enum LookupInner<'a> {
Format1(LazyArray16<'a, u16>),
Expand All @@ -335,7 +335,7 @@ enum LookupInner<'a> {
Format6(BinarySearchTable<'a, LookupSingle>),
Format8 {
first_glyph: u16,
values: LazyArray16<'a, u16>
values: LazyArray16<'a, u16>,
},
Format10 {
value_size: u16,
Expand Down Expand Up @@ -370,28 +370,30 @@ impl<'a> LookupInner<'a> {
let first_glyph = s.read::<u16>()?;
let glyph_count = s.read::<u16>()?;
let values = s.read_array16::<u16>(glyph_count)?;
Some(Self::Format8 { first_glyph, values })
Some(Self::Format8 {
first_glyph,
values,
})
}
10 => {
let value_size = s.read::<u16>()?;
let first_glyph = s.read::<u16>()?;
let glyph_count = s.read::<u16>()?;
Some(Self::Format10 { value_size, first_glyph, glyph_count, data: s.tail()? })
}
_ => {
None
Some(Self::Format10 {
value_size,
first_glyph,
glyph_count,
data: s.tail()?,
})
}
_ => None,
}
}

fn value(&self, glyph_id: GlyphId) -> Option<u16> {
match self {
Self::Format1(values) => {
values.get(glyph_id.0)
}
Self::Format2(ref bsearch) => {
bsearch.get(glyph_id).map(|v| v.value)
}
Self::Format1(values) => values.get(glyph_id.0),
Self::Format2(ref bsearch) => bsearch.get(glyph_id).map(|v| v.value),
Self::Format4(ref bsearch, data) => {
// In format 4, LookupSegment contains an offset to a list of u16 values.
// One value for each glyph in the LookupSegment range.
Expand All @@ -400,21 +402,30 @@ impl<'a> LookupInner<'a> {
let offset = usize::from(segment.value) + u16::SIZE * usize::from(index);
Stream::read_at::<u16>(data, offset)
}
Self::Format6(ref bsearch) => {
bsearch.get(glyph_id).map(|v| v.value)
}
Self::Format8 { first_glyph, values } => {
Self::Format6(ref bsearch) => bsearch.get(glyph_id).map(|v| v.value),
Self::Format8 {
first_glyph,
values,
} => {
let idx = glyph_id.0.checked_sub(*first_glyph)?;
values.get(idx)
}
Self::Format10 { value_size, first_glyph, glyph_count, data } => {
Self::Format10 {
value_size,
first_glyph,
glyph_count,
data,
} => {
let idx = glyph_id.0.checked_sub(*first_glyph)?;
let mut s = Stream::new(data);
match value_size {
1 => s.read_array16::<u8>(*glyph_count)?.get(idx).map(u16::from),
2 => s.read_array16::<u16>(*glyph_count)?.get(idx),
// TODO: we should return u32 here, but this is not supported yet
4 => s.read_array16::<u32>(*glyph_count)?.get(idx).map(|n| n as u16),
4 => s
.read_array16::<u32>(*glyph_count)?
.get(idx)
.map(|n| n as u16),
_ => None, // 8 is also supported
}
}
Expand Down Expand Up @@ -468,23 +479,21 @@ impl<'a, T: BinarySearchValue + core::fmt::Debug> BinarySearchTable<'a, T> {
let mid = (min + max) / 2;
let v = self.values.get(mid as u16)?;
match v.contains(key) {
core::cmp::Ordering::Less => max = mid - 1,
core::cmp::Ordering::Less => max = mid - 1,
core::cmp::Ordering::Greater => min = mid + 1,
core::cmp::Ordering::Equal => return Some(v),
core::cmp::Ordering::Equal => return Some(v),
}
}

None
}
}


trait BinarySearchValue: FromData {
fn is_termination(&self) -> bool;
fn contains(&self, glyph_id: GlyphId) -> core::cmp::Ordering;
}


#[derive(Clone, Copy, Debug)]
struct LookupSegment {
last_glyph: u16,
Expand Down Expand Up @@ -524,7 +533,6 @@ impl BinarySearchValue for LookupSegment {
}
}


#[derive(Clone, Copy, Debug)]
struct LookupSingle {
glyph: u16,
Expand Down
9 changes: 7 additions & 2 deletions src/ggg/chained_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parser::{FromSlice, LazyArray16, LazyOffsetArray16, Stream};
use super::{ClassDefinition, Coverage, SequenceLookupRecord};
use crate::parser::{FromSlice, LazyArray16, LazyOffsetArray16, Stream};

/// A [Chained Contextual Lookup Subtable](
/// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#chseqctxt1).
Expand Down Expand Up @@ -125,6 +125,11 @@ impl<'a> FromSlice<'a> for ChainedSequenceRule<'a> {
let lookahead = s.read_array16(lookahead_count)?;
let lookup_count = s.read::<u16>()?;
let lookups = s.read_array16(lookup_count)?;
Some(Self { backtrack, input, lookahead, lookups })
Some(Self {
backtrack,
input,
lookahead,
lookups,
})
}
}
Loading

0 comments on commit 642af90

Please sign in to comment.