Adapt to fontdock 🔼

This commit is contained in:
Laurenz 2020-10-12 16:59:21 +02:00
parent a2a68106c0
commit e94627721d
6 changed files with 41 additions and 37 deletions

View File

@ -2,40 +2,43 @@ use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use fontdock::fs::{FsIndex, FsProvider}; use fontdock::fs::{FsIndex, FsSource};
use futures_executor::block_on;
use typstc::eval::{eval, State}; use typstc::eval::{eval, State};
use typstc::font::FontLoader; use typstc::font::FontLoader;
use typstc::layout::layout;
use typstc::parse::parse; use typstc::parse::parse;
use typstc::typeset; use typstc::typeset;
const FONT_DIR: &str = "fonts"; const FONT_DIR: &str = "fonts";
const COMA: &str = include_str!("../tests/coma.typ"); const COMA: &str = include_str!("../tests/coma.typ");
fn parse_benchmark(c: &mut Criterion) { fn benchmarks(c: &mut Criterion) {
c.bench_function("parse-coma", |b| b.iter(|| parse(COMA)));
}
fn eval_benchmark(c: &mut Criterion) {
let tree = parse(COMA).output;
let state = State::default(); let state = State::default();
c.bench_function("eval-coma", |b| b.iter(|| eval(&tree, state.clone())));
}
fn typeset_benchmark(c: &mut Criterion) {
let mut index = FsIndex::new(); let mut index = FsIndex::new();
index.search_dir(FONT_DIR); index.search_dir(FONT_DIR);
let (descriptors, files) = index.into_vecs(); let (files, descriptors) = index.into_vecs();
let provider = FsProvider::new(files); let loader = Rc::new(RefCell::new(FontLoader::new(
let loader = FontLoader::new(Box::new(provider), descriptors); Box::new(FsSource::new(files)),
let loader = Rc::new(RefCell::new(loader)); descriptors,
)));
let state = State::default(); let tree = parse(COMA).output;
let document = eval(&tree, state.clone()).output;
let _ = block_on(layout(&document, Rc::clone(&loader)));
c.bench_function("parse-coma", |b| b.iter(|| parse(COMA)));
c.bench_function("eval-coma", |b| b.iter(|| eval(&tree, state.clone())));
c.bench_function("layout-coma", |b| {
b.iter(|| block_on(layout(&document, Rc::clone(&loader))))
});
c.bench_function("typeset-coma", |b| { c.bench_function("typeset-coma", |b| {
b.iter(|| typeset(COMA, state.clone(), Rc::clone(&loader))) b.iter(|| block_on(typeset(COMA, state.clone(), Rc::clone(&loader))))
}); });
} }
criterion_group!(benches, parse_benchmark, eval_benchmark, typeset_benchmark); criterion_group!(benches, benchmarks);
criterion_main!(benches); criterion_main!(benches);

View File

@ -4,7 +4,7 @@ use std::io::BufWriter;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
use fontdock::fs::{FsIndex, FsProvider}; use fontdock::fs::{FsIndex, FsSource};
use futures_executor::block_on; use futures_executor::block_on;
use typstc::diag::{Feedback, Pass}; use typstc::diag::{Feedback, Pass};
@ -38,10 +38,11 @@ fn main() {
index.search_dir("fonts"); index.search_dir("fonts");
index.search_os(); index.search_os();
let (descriptors, files) = index.into_vecs(); let (files, descriptors) = index.into_vecs();
let provider = FsProvider::new(files); let loader = Rc::new(RefCell::new(FontLoader::new(
let loader = FontLoader::new(Box::new(provider), descriptors); Box::new(FsSource::new(files)),
let loader = Rc::new(RefCell::new(loader)); descriptors,
)));
let state = State::default(); let state = State::default();
let Pass { let Pass {

View File

@ -3,17 +3,17 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use fontdock::{ContainsChar, FaceFromVec, FontProvider}; use fontdock::{ContainsChar, FaceFromVec, FontSource};
use ttf_parser::Face; use ttf_parser::Face;
/// A reference-counted shared font loader backed by a dynamic font provider. /// A reference-counted shared font loader backed by a dynamic font source.
pub type SharedFontLoader = Rc<RefCell<FontLoader>>; pub type SharedFontLoader = Rc<RefCell<FontLoader>>;
/// A font loader backed by a dynamic provider. /// A font loader backed by a dynamic source.
pub type FontLoader = fontdock::FontLoader<Box<DynProvider>>; pub type FontLoader = fontdock::FontLoader<Box<DynSource>>;
/// The dynamic font provider backing the font loader. /// The dynamic font source.
pub type DynProvider = dyn FontProvider<Face = OwnedFace>; pub type DynSource = dyn FontSource<Face = OwnedFace>;
/// An owned font face. /// An owned font face.
pub struct OwnedFace { pub struct OwnedFace {

View File

@ -35,8 +35,7 @@ impl Layout for Text {
self.dir, self.dir,
&self.families, &self.families,
self.variant, self.variant,
) ),
.await,
self.aligns, self.aligns,
)] )]
} }

View File

@ -61,7 +61,7 @@ impl Debug for Shaped {
/// Shape text into a box containing [`Shaped`] runs. /// Shape text into a box containing [`Shaped`] runs.
/// ///
/// [`Shaped`]: struct.Shaped.html /// [`Shaped`]: struct.Shaped.html
pub async fn shape( pub fn shape(
loader: &mut FontLoader, loader: &mut FontLoader,
text: &str, text: &str,
font_size: Length, font_size: Length,
@ -84,7 +84,7 @@ pub async fn shape(
for c in chars { for c in chars {
let query = FaceQuery { fallback: fallback.iter(), variant, c }; let query = FaceQuery { fallback: fallback.iter(), variant, c };
if let Some((id, owned_face)) = loader.query(query).await { if let Some((id, owned_face)) = loader.query(query) {
let face = owned_face.get(); let face = owned_face.get();
let (glyph, width) = match lookup_glyph(face, c, font_size) { let (glyph, width) = match lookup_glyph(face, c, font_size) {
Some(v) => v, Some(v) => v,

View File

@ -6,7 +6,7 @@ use std::io::BufWriter;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use fontdock::fs::{FsIndex, FsProvider}; use fontdock::fs::{FsIndex, FsSource};
use futures_executor::block_on; use futures_executor::block_on;
use raqote::{DrawTarget, PathBuilder, SolidSource, Source, Transform, Vector}; use raqote::{DrawTarget, PathBuilder, SolidSource, Source, Transform, Vector};
use ttf_parser::OutlineBuilder; use ttf_parser::OutlineBuilder;
@ -59,10 +59,11 @@ fn main() {
let mut index = FsIndex::new(); let mut index = FsIndex::new();
index.search_dir(FONT_DIR); index.search_dir(FONT_DIR);
let (descriptors, files) = index.into_vecs(); let (files, descriptors) = index.into_vecs();
let provider = FsProvider::new(files); let loader = Rc::new(RefCell::new(FontLoader::new(
let loader = FontLoader::new(Box::new(provider), descriptors); Box::new(FsSource::new(files)),
let loader = Rc::new(RefCell::new(loader)); descriptors,
)));
for (name, path, src) in filtered { for (name, path, src) in filtered {
test(&name, &src, &path, &loader) test(&name, &src, &path, &loader)