mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Adapt to fontdock 🔼
This commit is contained in:
parent
a2a68106c0
commit
e94627721d
@ -2,40 +2,43 @@ use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
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::font::FontLoader;
|
||||
use typstc::layout::layout;
|
||||
use typstc::parse::parse;
|
||||
use typstc::typeset;
|
||||
|
||||
const FONT_DIR: &str = "fonts";
|
||||
const COMA: &str = include_str!("../tests/coma.typ");
|
||||
|
||||
fn parse_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("parse-coma", |b| b.iter(|| parse(COMA)));
|
||||
}
|
||||
|
||||
fn eval_benchmark(c: &mut Criterion) {
|
||||
let tree = parse(COMA).output;
|
||||
fn benchmarks(c: &mut Criterion) {
|
||||
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();
|
||||
index.search_dir(FONT_DIR);
|
||||
|
||||
let (descriptors, files) = index.into_vecs();
|
||||
let provider = FsProvider::new(files);
|
||||
let loader = FontLoader::new(Box::new(provider), descriptors);
|
||||
let loader = Rc::new(RefCell::new(loader));
|
||||
let (files, descriptors) = index.into_vecs();
|
||||
let loader = Rc::new(RefCell::new(FontLoader::new(
|
||||
Box::new(FsSource::new(files)),
|
||||
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| {
|
||||
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);
|
||||
|
@ -4,7 +4,7 @@ use std::io::BufWriter;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
use fontdock::fs::{FsIndex, FsProvider};
|
||||
use fontdock::fs::{FsIndex, FsSource};
|
||||
use futures_executor::block_on;
|
||||
|
||||
use typstc::diag::{Feedback, Pass};
|
||||
@ -38,10 +38,11 @@ fn main() {
|
||||
index.search_dir("fonts");
|
||||
index.search_os();
|
||||
|
||||
let (descriptors, files) = index.into_vecs();
|
||||
let provider = FsProvider::new(files);
|
||||
let loader = FontLoader::new(Box::new(provider), descriptors);
|
||||
let loader = Rc::new(RefCell::new(loader));
|
||||
let (files, descriptors) = index.into_vecs();
|
||||
let loader = Rc::new(RefCell::new(FontLoader::new(
|
||||
Box::new(FsSource::new(files)),
|
||||
descriptors,
|
||||
)));
|
||||
|
||||
let state = State::default();
|
||||
let Pass {
|
||||
|
12
src/font.rs
12
src/font.rs
@ -3,17 +3,17 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use fontdock::{ContainsChar, FaceFromVec, FontProvider};
|
||||
use fontdock::{ContainsChar, FaceFromVec, FontSource};
|
||||
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>>;
|
||||
|
||||
/// A font loader backed by a dynamic provider.
|
||||
pub type FontLoader = fontdock::FontLoader<Box<DynProvider>>;
|
||||
/// A font loader backed by a dynamic source.
|
||||
pub type FontLoader = fontdock::FontLoader<Box<DynSource>>;
|
||||
|
||||
/// The dynamic font provider backing the font loader.
|
||||
pub type DynProvider = dyn FontProvider<Face = OwnedFace>;
|
||||
/// The dynamic font source.
|
||||
pub type DynSource = dyn FontSource<Face = OwnedFace>;
|
||||
|
||||
/// An owned font face.
|
||||
pub struct OwnedFace {
|
||||
|
@ -35,8 +35,7 @@ impl Layout for Text {
|
||||
self.dir,
|
||||
&self.families,
|
||||
self.variant,
|
||||
)
|
||||
.await,
|
||||
),
|
||||
self.aligns,
|
||||
)]
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ impl Debug for Shaped {
|
||||
/// Shape text into a box containing [`Shaped`] runs.
|
||||
///
|
||||
/// [`Shaped`]: struct.Shaped.html
|
||||
pub async fn shape(
|
||||
pub fn shape(
|
||||
loader: &mut FontLoader,
|
||||
text: &str,
|
||||
font_size: Length,
|
||||
@ -84,7 +84,7 @@ pub async fn shape(
|
||||
|
||||
for c in chars {
|
||||
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 (glyph, width) = match lookup_glyph(face, c, font_size) {
|
||||
Some(v) => v,
|
||||
|
@ -6,7 +6,7 @@ use std::io::BufWriter;
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
|
||||
use fontdock::fs::{FsIndex, FsProvider};
|
||||
use fontdock::fs::{FsIndex, FsSource};
|
||||
use futures_executor::block_on;
|
||||
use raqote::{DrawTarget, PathBuilder, SolidSource, Source, Transform, Vector};
|
||||
use ttf_parser::OutlineBuilder;
|
||||
@ -59,10 +59,11 @@ fn main() {
|
||||
let mut index = FsIndex::new();
|
||||
index.search_dir(FONT_DIR);
|
||||
|
||||
let (descriptors, files) = index.into_vecs();
|
||||
let provider = FsProvider::new(files);
|
||||
let loader = FontLoader::new(Box::new(provider), descriptors);
|
||||
let loader = Rc::new(RefCell::new(loader));
|
||||
let (files, descriptors) = index.into_vecs();
|
||||
let loader = Rc::new(RefCell::new(FontLoader::new(
|
||||
Box::new(FsSource::new(files)),
|
||||
descriptors,
|
||||
)));
|
||||
|
||||
for (name, path, src) in filtered {
|
||||
test(&name, &src, &path, &loader)
|
||||
|
Loading…
x
Reference in New Issue
Block a user