Merge pull request #9 from typst/port-fontdock

Port font handling to fontdock and ttf-parser 🛳
This commit is contained in:
Laurenz 2020-08-01 17:42:08 +02:00 committed by GitHub
commit 4ac3aa6ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 392 additions and 374 deletions

View File

@ -24,12 +24,12 @@ jobs:
uses: actions/checkout@v2
with:
path: typstc
- name: Checkout toddle
- name: Checkout fontdock
uses: actions/checkout@v2
with:
repository: typst/toddle
repository: typst/fontdock
token: ${{ secrets.TYPSTC_ACTION_TOKEN }} # `GitHub_PAT` is a secret that contains your PAT
path: toddle
path: fontdock
- name: Checkout tide
uses: actions/checkout@v2
with:

View File

@ -5,27 +5,28 @@ authors = ["Laurenz Mädje <laurmaedje@gmail.com>"]
edition = "2018"
[dependencies]
toddle = { path = "../toddle", features = ["query"], default-features = false }
fontdock = { path = "../fontdock", features = ["serialize"] }
tide = { path = "../tide" }
byteorder = "1"
smallvec = "1"
unicode-xid = "0.2"
async-trait = "0.1"
ttf-parser = "0.8.2"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", optional = true }
futures-executor = { version = "0.3", optional = true }
[features]
default = ["fs-provider", "futures-executor", "serde_json"]
fs-provider = ["toddle/fs-provider"]
default = ["fs", "futures-executor", "serde_json"]
fs = ["fontdock/fs"]
[[bin]]
name = "typst"
path = "src/bin/main.rs"
required-features = ["fs-provider", "futures-executor"]
required-features = ["futures-executor"]
[[test]]
name = "typeset"
path = "tests/src/typeset.rs"
harness = false
required-features = ["fs-provider", "futures-executor", "serde_json"]
required-features = ["futures-executor", "serde_json"]

View File

@ -1,11 +1,15 @@
use std::cell::RefCell;
use std::error::Error;
use std::fs::{File, read_to_string};
use std::io::BufWriter;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use futures_executor::block_on;
use typstc::{Typesetter, DebugErrorProvider};
use typstc::toddle::query::fs::EagerFsProvider;
use fontdock::fs::{FsIndex, FsProvider};
use fontdock::FontLoader;
use typstc::Typesetter;
use typstc::font::DynProvider;
use typstc::export::pdf;
fn main() {
@ -18,6 +22,7 @@ fn main() {
fn run() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 {
println!("typst");
println!("usage: {} source [destination]",
args.first().map(|s| s.as_str()).unwrap_or("typst"));
std::process::exit(0);
@ -37,14 +42,22 @@ fn run() -> Result<(), Box<dyn Error>> {
let src = read_to_string(source)
.map_err(|_| "failed to read from source file")?;
let (fs, entries) = EagerFsProvider::from_index("../fonts", "index.json")?;
let provider = DebugErrorProvider::new(fs);
let typesetter = Typesetter::new((Box::new(provider), entries));
let mut index = FsIndex::new();
index.search_dir("fonts");
index.search_dir("../fonts");
index.search_os();
let (descriptors, files) = index.into_vecs();
let provider = FsProvider::new(files.clone());
let dynamic = Box::new(provider) as Box<DynProvider>;
let loader = FontLoader::new(dynamic, descriptors);
let loader = Rc::new(RefCell::new(loader));
let typesetter = Typesetter::new(loader.clone());
let layouts = block_on(typesetter.typeset(&src)).output;
let writer = BufWriter::new(File::create(&dest)?);
pdf::export(&layouts, typesetter.loader(), writer)?;
pdf::export(&layouts, &loader, writer)?;
Ok(())
}

View File

@ -1,8 +1,6 @@
//! Exporting of layouts into _PDF_ documents.
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::collections::HashMap;
use std::io::{self, Write};
use tide::{PdfWriter, Rect, Ref, Trailer, Version};
@ -13,27 +11,22 @@ use tide::font::{
CMap, CMapEncoding, FontStream, GlyphUnit, WidthRecord,
};
use toddle::{Font, OwnedFont, LoadError};
use toddle::types::Tag;
use toddle::query::FontIndex;
use toddle::tables::{
CharMap, Header, HorizontalMetrics, MacStyleFlags,
Name, NameEntry, Post, OS2,
};
use fontdock::FaceId;
use ttf_parser::{name_id, GlyphId};
use crate::GlobalFontLoader;
use crate::SharedFontLoader;
use crate::layout::{MultiLayout, Layout, LayoutAction};
use crate::length::Length;
/// Export a layouted list of boxes. The same font loader as used for
/// layouting needs to be passed in here since the layout only contains
/// indices referencing the loaded fonts. The raw PDF ist written into the
/// indices referencing the loaded faces. The raw PDF ist written into the
/// target writable, returning the number of bytes written.
pub fn export<W: Write>(
layout: &MultiLayout,
loader: &GlobalFontLoader,
loader: &SharedFontLoader,
target: W,
) -> PdfResult<usize> {
) -> io::Result<usize> {
PdfExporter::new(layout, loader, target)?.write()
}
@ -41,21 +34,15 @@ pub fn export<W: Write>(
struct PdfExporter<'a, W: Write> {
writer: PdfWriter<W>,
layouts: &'a MultiLayout,
/// Since we cross-reference pages and fonts with their IDs already in the document
/// catalog, we need to know exactly which ID is used for what from the beginning.
/// Thus, we compute a range for each category of object and stored these here.
loader: &'a SharedFontLoader,
/// Since we cross-reference pages and faces with their IDs already in the
/// document catalog, we need to know exactly which ID is used for what from
/// the beginning. Thus, we compute a range for each category of object and
/// stored these here.
offsets: Offsets,
/// Each font has got an index from the font loader. However, these may not be
/// ascending from zero. Since we want to use the indices 0 .. num_fonts we
/// go through all font usages and assign a new index for each used font.
/// This remapping is stored here because we need it when converting the
/// layout actions in `ExportProcess::write_page`.
font_remap: HashMap<FontIndex, usize>,
/// These are the fonts sorted by their *new* ids, that is, the values of `font_remap`.
fonts: Vec<OwnedFont>,
// Font remapping, see below at `remap_fonts`.
to_pdf: HashMap<FaceId, usize>,
to_fontdock: Vec<FaceId>,
}
/// Indicates which range of PDF IDs will be used for which contents.
@ -67,28 +54,31 @@ struct Offsets {
fonts: (Ref, Ref),
}
const NUM_OBJECTS_PER_FONT: u32 = 5;
impl<'a, W: Write> PdfExporter<'a, W> {
/// Prepare the export. Only once [`ExportProcess::write`] is called the
/// writing really happens.
fn new(
layouts: &'a MultiLayout,
font_loader: &GlobalFontLoader,
loader: &'a SharedFontLoader,
target: W,
) -> PdfResult<PdfExporter<'a, W>> {
let (fonts, font_remap) = subset_fonts(layouts, font_loader)?;
let offsets = calculate_offsets(layouts.len(), fonts.len());
) -> io::Result<PdfExporter<'a, W>> {
let (to_pdf, to_fontdock) = remap_fonts(layouts);
let offsets = calculate_offsets(layouts.len(), to_pdf.len());
Ok(PdfExporter {
writer: PdfWriter::new(target),
layouts,
offsets,
font_remap,
fonts,
to_pdf,
to_fontdock,
loader,
})
}
/// Write everything (writing entry point).
fn write(&mut self) -> PdfResult<usize> {
fn write(&mut self) -> io::Result<usize> {
self.writer.write_header(Version::new(1, 7))?;
self.write_preface()?;
self.write_pages()?;
@ -99,15 +89,14 @@ impl<'a, W: Write> PdfExporter<'a, W> {
}
/// Write the document catalog and page tree.
fn write_preface(&mut self) -> PdfResult<()> {
fn write_preface(&mut self) -> io::Result<()> {
// The document catalog.
self.writer.write_obj(self.offsets.catalog, &Catalog::new(self.offsets.page_tree))?;
// The font resources.
let start = self.offsets.fonts.0;
const NUM_OBJECTS_PER_FONT: usize = 5;
let fonts = (0 .. self.fonts.len()).map(|i| {
Resource::Font((i + 1) as u32, start + (NUM_OBJECTS_PER_FONT * i) as u32)
let fonts = (0 .. self.to_pdf.len() as u32).map(|i| {
Resource::Font(i + 1, start + (NUM_OBJECTS_PER_FONT * i))
});
// The root page tree.
@ -143,7 +132,7 @@ impl<'a, W: Write> PdfExporter<'a, W> {
}
/// Write the contents of all pages.
fn write_pages(&mut self) -> PdfResult<()> {
fn write_pages(&mut self) -> io::Result<()> {
for (id, page) in ids(self.offsets.contents).zip(self.layouts) {
self.write_page(id, &page)?;
}
@ -151,11 +140,12 @@ impl<'a, W: Write> PdfExporter<'a, W> {
}
/// Write the content of a page.
fn write_page(&mut self, id: u32, page: &Layout) -> PdfResult<()> {
// Moves and font switches are always cached and only flushed once
fn write_page(&mut self, id: u32, page: &Layout) -> io::Result<()> {
// Moves and face switches are always cached and only flushed once
// needed.
let mut text = Text::new();
let mut active_font = (std::usize::MAX, 0.0);
let mut face_id = FaceId::MAX;
let mut font_size = Length::ZERO;
let mut next_pos = None;
for action in &page.actions {
@ -164,19 +154,22 @@ impl<'a, W: Write> PdfExporter<'a, W> {
next_pos = Some(*pos);
},
LayoutAction::SetFont(id, size) => {
active_font = (self.font_remap[id], size.to_pt());
text.tf(active_font.0 as u32 + 1, size.to_pt() as f32);
&LayoutAction::SetFont(id, size) => {
face_id = id;
font_size = size;
text.tf(self.to_pdf[&id] as u32 + 1, font_size.to_pt() as f32);
}
LayoutAction::WriteText(string) => {
if let Some(pos) = next_pos.take() {
let x = pos.x.to_pt();
let y = (page.dimensions.y - pos.y - Length::pt(active_font.1)).to_pt();
let y = (page.dimensions.y - pos.y - font_size).to_pt();
text.tm(1.0, 0.0, 0.0, 1.0, x as f32, y as f32);
}
text.tj(self.fonts[active_font.0].encode_text(&string)?);
let loader = self.loader.borrow();
let face = loader.get_loaded(face_id);
text.tj(face.encode_text(&string));
},
LayoutAction::DebugBox(_) => {}
@ -189,20 +182,57 @@ impl<'a, W: Write> PdfExporter<'a, W> {
}
/// Write all the fonts.
fn write_fonts(&mut self) -> PdfResult<()> {
fn write_fonts(&mut self) -> io::Result<()> {
let mut id = self.offsets.fonts.0;
for font in &mut self.fonts {
// ---------------------------------------------
// Extract information from the name table.
let name = font
.read_table::<Name>()?
.get_decoded(NameEntry::PostScriptName)
for &face_id in &self.to_fontdock {
let loader = self.loader.borrow();
let face = loader.get_loaded(face_id);
let name = face
.names()
.find(|entry| {
entry.name_id() == name_id::POST_SCRIPT_NAME
&& entry.is_unicode()
})
.map(|entry| entry.to_string())
.flatten()
.unwrap_or_else(|| "unknown".to_string());
let base_font = format!("ABCDEF+{}", name);
let system_info = CIDSystemInfo::new("Adobe", "Identity", 0);
let units_per_em = face.units_per_em().unwrap_or(1000);
let ratio = 1.0 / (units_per_em as f64);
let to_length = |x| Length::pt(ratio * x as f64);
let to_glyph_unit = |font_unit| {
let length = to_length(font_unit);
(1000.0 * length.to_pt()).round() as GlyphUnit
};
let global_bbox = face.global_bounding_box();
let bbox = Rect::new(
to_glyph_unit(global_bbox.x_min as f64),
to_glyph_unit(global_bbox.y_min as f64),
to_glyph_unit(global_bbox.x_max as f64),
to_glyph_unit(global_bbox.y_max as f64),
);
let monospace = face.is_monospaced();
let italic = face.is_italic();
let italic_angle = face.italic_angle().unwrap_or(0.0);
let ascender = face.typographic_ascender().unwrap_or(0);
let descender = face.typographic_descender().unwrap_or(0);
let cap_height = face.capital_height().unwrap_or(ascender);
let stem_v = 10.0 + 0.244 * (face.weight().to_number() as f32 - 50.0);
let mut flags = FontFlags::empty();
flags.set(FontFlags::SERIF, name.contains("Serif"));
flags.set(FontFlags::FIXED_PITCH, monospace);
flags.set(FontFlags::ITALIC, italic);
flags.insert(FontFlags::SYMBOLIC);
flags.insert(FontFlags::SMALL_CAP);
// Write the base font object referencing the CID font.
self.writer.write_obj(
id,
@ -214,31 +244,10 @@ impl<'a, W: Write> PdfExporter<'a, W> {
.to_unicode(id + 3),
)?;
// ---------------------------------------------
// Extract information from the head and hmtx tables.
let head = font.read_table::<Header>()?;
let font_unit_ratio = 1.0 / (head.units_per_em as f64);
let font_unit_to_size = |x| Length::pt(font_unit_ratio * x);
let font_unit_to_glyph_unit = |fu| {
let size = font_unit_to_size(fu);
(1000.0 * size.to_pt()).round() as GlyphUnit
};
let italic = head.mac_style.contains(MacStyleFlags::ITALIC);
let bounding_box = Rect::new(
font_unit_to_glyph_unit(head.x_min as f64),
font_unit_to_glyph_unit(head.y_min as f64),
font_unit_to_glyph_unit(head.x_max as f64),
font_unit_to_glyph_unit(head.y_max as f64),
);
// Transform the width into PDF units.
let widths: Vec<_> = font
.read_table::<HorizontalMetrics>()?
.metrics
.iter()
.map(|m| font_unit_to_glyph_unit(m.advance_width as f64))
let num_glyphs = face.number_of_glyphs();
let widths: Vec<_> = (0 .. num_glyphs)
.map(|g| face.glyph_hor_advance(GlyphId(g)).unwrap_or(0))
.map(|w| to_glyph_unit(w as f64))
.collect();
// Write the CID font referencing the font descriptor.
@ -250,130 +259,71 @@ impl<'a, W: Write> PdfExporter<'a, W> {
system_info.clone(),
id + 2,
)
.widths(vec![WidthRecord::start(0, widths)]),
.widths(vec![WidthRecord::Start(0, widths)]),
)?;
// ---------------------------------------------
// Extract information from the post table.
let post = font.read_table::<Post>()?;
let fixed_pitch = post.is_fixed_pitch;
let italic_angle = post.italic_angle.to_f32();
let mut flags = FontFlags::empty();
flags.set(FontFlags::SERIF, name.contains("Serif"));
flags.set(FontFlags::FIXED_PITCH, fixed_pitch);
flags.set(FontFlags::ITALIC, italic);
flags.insert(FontFlags::SYMBOLIC);
flags.insert(FontFlags::SMALL_CAP);
// ---------------------------------------------
// Extract information from the OS/2 table.
let os2 = font.read_table::<OS2>()?;
// Write the font descriptor (contains the global information about the font).
self.writer.write_obj(id + 2, FontDescriptor::new(base_font, flags, italic_angle)
.font_bbox(bounding_box)
.ascent(font_unit_to_glyph_unit(os2.s_typo_ascender as f64))
.descent(font_unit_to_glyph_unit(os2.s_typo_descender as f64))
.cap_height(font_unit_to_glyph_unit(
os2.s_cap_height.unwrap_or(os2.s_typo_ascender) as f64,
))
.stem_v((10.0 + 0.244 * (os2.us_weight_class as f64 - 50.0)) as GlyphUnit)
.font_file_2(id + 4)
// Write the font descriptor (contains the global information about
// the font).
self.writer.write_obj(id + 2,
FontDescriptor::new(base_font, flags, italic_angle)
.font_bbox(bbox)
.ascent(to_glyph_unit(ascender as f64))
.descent(to_glyph_unit(descender as f64))
.cap_height(to_glyph_unit(cap_height as f64))
.stem_v(stem_v as GlyphUnit)
.font_file_2(id + 4)
)?;
// ---------------------------------------------
// Extract information from the cmap table.
let mut mapping = vec![];
for subtable in face.character_mapping_subtables() {
subtable.codepoints(|n| {
if let Some(c) = std::char::from_u32(n) {
if let Some(g) = face.glyph_index(c) {
mapping.push((g.0, c));
}
}
})
}
let cmap = CMap::new("Custom", system_info, font
.read_table::<CharMap>()?
.mapping
.iter()
.map(|(&c, &cid)| (cid, c))
);
// Write the CMap, which maps glyph ID's to unicode codepoints.
self.writer.write_obj(id + 3, &CMap::new(
"Custom",
system_info,
mapping,
))?;
// Write the CMap, which maps glyphs to unicode codepoints.
self.writer.write_obj(id + 3, &cmap)?;
// Finally write the subsetted font bytes.
self.writer.write_obj(id + 4, &FontStream::new(face.data()))?;
// ---------------------------------------------
// Finally write the subsetted font program.
self.writer.write_obj(id + 4, &FontStream::new(font.data().get_ref()))?;
id += 5;
id += NUM_OBJECTS_PER_FONT;
}
Ok(())
}
}
/// Subsets all fonts and assign a new PDF-internal index to each one. The
/// returned hash map maps the old indices (used by the layouts) to the new one
/// used in the PDF. The new ones index into the returned vector of owned fonts.
fn subset_fonts(
layouts: &MultiLayout,
font_loader: &GlobalFontLoader,
) -> PdfResult<(Vec<OwnedFont>, HashMap<FontIndex, usize>)> {
let mut fonts = Vec::new();
let mut font_chars: HashMap<FontIndex, HashSet<char>> = HashMap::new();
let mut old_to_new: HashMap<FontIndex, usize> = HashMap::new();
let mut new_to_old: HashMap<usize, FontIndex> = HashMap::new();
let mut active_font = FontIndex::MAX;
/// Assigns a new PDF-internal index to each used face and returns two mappings:
/// - Forwards from the old face ids to the new pdf indices (hash map)
/// - Backwards from the pdf indices to the old ids (vec)
fn remap_fonts(layouts: &MultiLayout) -> (HashMap<FaceId, usize>, Vec<FaceId>) {
let mut to_pdf = HashMap::new();
let mut to_fontdock = vec![];
// We want to find out which fonts are used at all and which chars are used
// for those. We use this information to create subsetted fonts.
// We want to find out which fonts are used at all. To do that, look at each
// text element to find out which font is uses.
for layout in layouts {
for action in &layout.actions {
match action {
LayoutAction::WriteText(text) => {
font_chars
.entry(active_font)
.or_insert_with(HashSet::new)
.extend(text.chars());
},
LayoutAction::SetFont(index, _) => {
active_font = *index;
let next_id = old_to_new.len();
let new_id = *old_to_new
.entry(active_font)
.or_insert(next_id);
new_to_old
.entry(new_id)
.or_insert(active_font);
},
_ => {}
if let &LayoutAction::SetFont(id, _) = action {
to_pdf.entry(id).or_insert_with(|| {
let next_id = to_fontdock.len();
to_fontdock.push(id);
next_id
});
}
}
}
let num_fonts = old_to_new.len();
let mut font_loader = font_loader.borrow_mut();
// All tables not listed here are dropped.
let tables: Vec<_> = [
b"name", b"OS/2", b"post", b"head", b"hhea", b"hmtx", b"maxp",
b"cmap", b"cvt ", b"fpgm", b"prep", b"loca", b"glyf",
].iter().map(|&s| Tag(*s)).collect();
// Do the subsetting.
for index in 0 .. num_fonts {
let old_index = new_to_old[&index];
let font = font_loader.get_with_index(old_index);
let chars = font_chars[&old_index].iter().cloned();
let subsetted = match font.subsetted(chars, tables.iter().copied()) {
Ok(data) => Font::from_bytes(data)?,
Err(_) => font.clone(),
};
fonts.push(subsetted);
}
Ok((fonts, old_to_new))
(to_pdf, to_fontdock)
}
/// We need to know in advance which IDs to use for which objects to
@ -398,44 +348,3 @@ fn calculate_offsets(layout_count: usize, font_count: usize) -> Offsets {
fn ids((start, end): (Ref, Ref)) -> impl Iterator<Item=Ref> {
start ..= end
}
/// The error type for _PDF_ exporting.
#[derive(Debug)]
pub enum PdfExportError {
/// An error occured while subsetting the font for the _PDF_.
Font(LoadError),
/// An I/O Error on the underlying writable.
Io(io::Error),
}
type PdfResult<T> = Result<T, PdfExportError>;
impl Error for PdfExportError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
PdfExportError::Font(err) => Some(err),
PdfExportError::Io(err) => Some(err),
}
}
}
impl Display for PdfExportError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
PdfExportError::Font(err) => err.fmt(f),
PdfExportError::Io(err) => err.fmt(f),
}
}
}
impl From<LoadError> for PdfExportError {
fn from(err: LoadError) -> PdfExportError {
PdfExportError::Font(err)
}
}
impl From<io::Error> for PdfExportError {
fn from(err: io::Error) -> PdfExportError {
PdfExportError::Io(err)
}
}

69
src/font.rs Normal file
View File

@ -0,0 +1,69 @@
//! Font handling.
use std::cell::RefCell;
use std::ops::Deref;
use std::rc::Rc;
use ttf_parser::Face;
use fontdock::{FontLoader, FontProvider, ContainsChar, FaceFromVec};
/// A referenced-count shared font loader backed by a dynamic provider.
pub type SharedFontLoader = Rc<RefCell<FontLoader<Box<DynProvider>>>>;
/// The dynamic font provider type backing the font loader.
pub type DynProvider = dyn FontProvider<Face=OwnedFace>;
/// An owned font face.
pub struct OwnedFace {
data: Vec<u8>,
face: Face<'static>,
}
impl FaceFromVec for OwnedFace {
fn from_vec(vec: Vec<u8>, i: u32) -> Option<Self> {
// The vec's location is stable in memory since we don't touch it and
// it can't be touched from outside this type.
let slice: &'static [u8] = unsafe {
std::slice::from_raw_parts(vec.as_ptr(), vec.len())
};
Some(OwnedFace {
data: vec,
face: Face::from_slice(slice, i).ok()?,
})
}
}
impl OwnedFace {
/// The raw face data.
pub fn data(&self) -> &[u8] {
&self.data
}
/// Encode the text into glyph ids and encode these into a big-endian byte
/// buffer.
pub fn encode_text(&self, text: &str) -> Vec<u8> {
const BYTES_PER_GLYPH: usize = 2;
let mut bytes = Vec::with_capacity(BYTES_PER_GLYPH * text.len());
for c in text.chars() {
if let Some(glyph) = self.glyph_index(c) {
bytes.push((glyph.0 >> 8) as u8);
bytes.push((glyph.0 & 0xff) as u8);
}
}
bytes
}
}
impl ContainsChar for OwnedFace {
fn contains_char(&self, c: char) -> bool {
self.glyph_index(c).is_some()
}
}
impl Deref for OwnedFace {
type Target = Face<'static>;
fn deref(&self) -> &Self::Target {
&self.face
}
}

View File

@ -2,7 +2,7 @@
use std::fmt::{self, Debug, Formatter};
use serde::ser::{Serialize, Serializer, SerializeTuple};
use toddle::query::FontIndex;
use fontdock::FaceId;
use crate::length::{Length, Size};
use super::Layout;
@ -15,7 +15,7 @@ pub enum LayoutAction {
/// Move to an absolute position.
MoveAbsolute(Size),
/// Set the font given the index from the font loader and font size.
SetFont(FontIndex, Length),
SetFont(FaceId, Length),
/// Write text at the current position.
WriteText(String),
/// Visualize a box for debugging purposes.
@ -31,10 +31,10 @@ impl Serialize for LayoutAction {
tup.serialize_element(&pos)?;
tup.end()
}
LayoutAction::SetFont(index, size) => {
LayoutAction::SetFont(id, size) => {
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&1u8)?;
tup.serialize_element(index)?;
tup.serialize_element(id)?;
tup.serialize_element(size)?;
tup.end()
}
@ -59,7 +59,7 @@ impl Debug for LayoutAction {
use LayoutAction::*;
match self {
MoveAbsolute(s) => write!(f, "move {} {}", s.x, s.y),
SetFont(i, s) => write!(f, "font {}-{} {}", i.id, i.variant, s),
SetFont(id, s) => write!(f, "font {}-{} {}", id.index, id.variant, s),
WriteText(s) => write!(f, "write {:?}", s),
DebugBox(s) => write!(f, "box {} {}", s.x, s.y),
}
@ -82,9 +82,9 @@ impl Debug for LayoutAction {
pub struct LayoutActions {
origin: Size,
actions: Vec<LayoutAction>,
active_font: (FontIndex, Length),
active_font: (FaceId, Length),
next_pos: Option<Size>,
next_font: Option<(FontIndex, Length)>,
next_font: Option<(FaceId, Length)>,
}
impl LayoutActions {
@ -93,7 +93,7 @@ impl LayoutActions {
LayoutActions {
actions: vec![],
origin: Size::ZERO,
active_font: (FontIndex::MAX, Length::ZERO),
active_font: (FaceId::MAX, Length::ZERO),
next_pos: None,
next_font: None,
}

View File

@ -3,7 +3,7 @@
use std::fmt::{self, Display, Formatter};
use smallvec::SmallVec;
use serde::Serialize;
use toddle::query::FontIndex;
use fontdock::FaceId;
use crate::length::{Length, Size, Margins};
use self::prelude::*;
@ -44,12 +44,12 @@ pub struct Layout {
impl Layout {
/// Returns a vector with all used font indices.
pub fn find_used_fonts(&self) -> Vec<FontIndex> {
pub fn find_used_fonts(&self) -> Vec<FaceId> {
let mut fonts = Vec::new();
for action in &self.actions {
if let LayoutAction::SetFont(index, _) = action {
if !fonts.contains(index) {
fonts.push(*index);
if let &LayoutAction::SetFont(id, _) = action {
if !fonts.contains(&id) {
fonts.push(id);
}
}
}

View File

@ -5,10 +5,9 @@
use std::future::Future;
use std::pin::Pin;
use smallvec::smallvec;
use toddle::query::FontStyle;
use crate::{Pass, Feedback};
use crate::GlobalFontLoader;
use crate::SharedFontLoader;
use crate::style::{LayoutStyle, PageStyle, TextStyle};
use crate::length::{Length, Size};
use crate::syntax::{Model, SyntaxModel, Node, Decoration};
@ -31,7 +30,7 @@ pub struct ModelLayouter<'a> {
pub struct LayoutContext<'a> {
/// The font loader to retrieve fonts from when typesetting text
/// using [`layout_text`].
pub loader: &'a GlobalFontLoader,
pub loader: &'a SharedFontLoader,
/// The style for pages and text.
pub style: &'a LayoutStyle,
/// The base unpadded dimensions of this container (for relative sizing).
@ -167,7 +166,7 @@ impl<'a> ModelLayouter<'a> {
Linebreak => self.layouter.finish_line(),
Text(text) => {
if self.style.text.variant.style == FontStyle::Italic {
if self.style.text.italic {
decorate(self, Decoration::Italic);
}
@ -179,7 +178,7 @@ impl<'a> ModelLayouter<'a> {
}
ToggleItalic => {
self.style.text.variant.style.toggle();
self.style.text.italic = !self.style.text.italic;
decorate(self, Decoration::Italic);
}
@ -191,7 +190,7 @@ impl<'a> ModelLayouter<'a> {
Raw(lines) => {
// TODO: Make this more efficient.
let fallback = self.style.text.fallback.clone();
self.style.text.fallback.list.insert(0, "monospace".to_string());
self.style.text.fallback.list_mut().insert(0, "monospace".to_string());
self.style.text.fallback.flatten();
// Layout the first line.

View File

@ -4,10 +4,8 @@
//! When the primary layouting axis horizontally inversed, the word is spelled
//! backwards. Vertical word layout is not yet supported.
use toddle::query::{FontQuery, FontIndex};
use toddle::tables::{CharMap, Header, HorizontalMetrics};
use crate::GlobalFontLoader;
use fontdock::{FaceId, FaceQuery, FontStyle};
use crate::font::SharedFontLoader;
use crate::length::{Length, Size};
use crate::style::TextStyle;
use super::*;
@ -19,7 +17,7 @@ struct TextLayouter<'a> {
text: &'a str,
actions: LayoutActions,
buffer: String,
active_font: FontIndex,
active_font: FaceId,
width: Length,
}
@ -28,7 +26,7 @@ struct TextLayouter<'a> {
pub struct TextContext<'a> {
/// The font loader to retrieve fonts from when typesetting text
/// using [`layout_text`].
pub loader: &'a GlobalFontLoader,
pub loader: &'a SharedFontLoader,
/// The style for text: Font selection with classes, weights and variants,
/// font sizes, spacing and so on.
pub style: &'a TextStyle,
@ -52,7 +50,7 @@ impl<'a> TextLayouter<'a> {
text,
actions: LayoutActions::new(),
buffer: String::new(),
active_font: FontIndex::MAX,
active_font: FaceId::MAX,
width: Length::ZERO,
}
}
@ -109,41 +107,41 @@ impl<'a> TextLayouter<'a> {
/// Select the best font for a character and return its index along with
/// the width of the char in the font.
async fn select_font(&mut self, c: char) -> Option<(FontIndex, Length)> {
async fn select_font(&mut self, c: char) -> Option<(FaceId, Length)> {
let mut loader = self.ctx.loader.borrow_mut();
let mut variant = self.ctx.style.variant;
if self.ctx.style.bolder {
variant.weight.0 += 300;
}
let query = FontQuery {
if self.ctx.style.italic {
variant.style = match variant.style {
FontStyle::Normal => FontStyle::Italic,
FontStyle::Italic => FontStyle::Normal,
FontStyle::Oblique => FontStyle::Normal,
}
}
let query = FaceQuery {
fallback: self.ctx.style.fallback.iter(),
variant,
c,
};
if let Some((font, index)) = loader.get(query).await {
if let Some((id, face)) = loader.query(query).await {
// Determine the width of the char.
let header = font.read_table::<Header>().ok()?;
let font_unit_ratio = 1.0 / (header.units_per_em as f64);
let font_unit_to_size = |x| Length::pt(font_unit_ratio * x);
let units_per_em = face.units_per_em().unwrap_or(1000);
let ratio = 1.0 / (units_per_em as f64);
let to_length = |x| Length::pt(ratio * x as f64);
let glyph = font
.read_table::<CharMap>()
.ok()?
.get(c)?;
let glyph_width = font
.read_table::<HorizontalMetrics>()
.ok()?
.get(glyph)?
.advance_width as f64;
let char_width = font_unit_to_size(glyph_width)
let glyph = face.glyph_index(c)?;
let glyph_width = face.glyph_hor_advance(glyph)?;
let char_width = to_length(glyph_width)
* self.ctx.style.font_size().to_pt();
Some((index, char_width))
Some((id, char_width))
} else {
None
}

View File

@ -16,18 +16,11 @@
//! format is [_PDF_](crate::export::pdf). Alternatively, the layout can be
//! serialized to pass it to a suitable renderer.
pub use toddle;
use std::cell::RefCell;
use std::fmt::Debug;
use async_trait::async_trait;
use smallvec::smallvec;
use toddle::{Font, OwnedData};
use toddle::query::{FontLoader, SharedFontLoader};
use toddle::query::{FontProvider, FontIndex, FontDescriptor};
use crate::diagnostic::Diagnostics;
use crate::font::SharedFontLoader;
use crate::layout::MultiLayout;
use crate::style::{LayoutStyle, PageStyle, TextStyle};
use crate::syntax::{Decorations, SyntaxModel, Scope, ParseState, parse};
@ -46,6 +39,7 @@ mod macros;
#[macro_use]
pub mod diagnostic;
pub mod export;
pub mod font;
#[macro_use]
pub mod func;
pub mod layout;
@ -60,7 +54,7 @@ pub mod syntax;
/// A typesetter can be configured through various methods.
pub struct Typesetter {
/// The font loader shared by all typesetting processes.
loader: GlobalFontLoader,
loader: SharedFontLoader,
/// The base layouting style.
style: LayoutStyle,
/// The base parser state.
@ -69,20 +63,11 @@ pub struct Typesetter {
debug: bool,
}
/// The font loader type used in the [`Typesetter`].
///
/// This font loader is ref-cell protected and backed by a dynamic font
/// provider.
pub type GlobalFontLoader = SharedFontLoader<GlobalProvider>;
/// The provider type of font loaders used in the [`Typesetter`].
pub type GlobalProvider = Box<dyn FontProvider<Data=OwnedData, Error=Box<dyn Debug>>>;
impl Typesetter {
/// Create a new typesetter.
pub fn new(provider: (GlobalProvider, Vec<FontDescriptor>)) -> Typesetter {
pub fn new(loader: SharedFontLoader) -> Typesetter {
Typesetter {
loader: RefCell::new(FontLoader::new(provider)),
loader,
style: LayoutStyle::default(),
parse_state: ParseState { scope: Scope::with_std() },
debug: false,
@ -104,11 +89,6 @@ impl Typesetter {
self.debug = debug;
}
/// A reference to the backing font loader.
pub fn loader(&self) -> &GlobalFontLoader {
&self.loader
}
/// Parse source code into a syntax tree.
pub fn parse(&self, src: &str) -> Pass<SyntaxModel> {
parse(src, Pos::ZERO, &self.parse_state)
@ -209,31 +189,3 @@ impl Feedback {
self.decorations.extend(more.decorations.offset(offset));
}
}
/// Wraps a font provider and transforms its errors into boxed [`Debug`] trait
/// objects. This enables font providers that do not return these boxed errors
/// to be used with the typesetter.
#[derive(Debug)]
pub struct DebugErrorProvider<P> {
provider: P,
}
impl<P> DebugErrorProvider<P>
where P: FontProvider, P::Error: Debug + 'static {
/// Create a new debug error provider from any provider.
pub fn new(provider: P) -> DebugErrorProvider<P> {
DebugErrorProvider { provider }
}
}
#[async_trait(?Send)]
impl<P> FontProvider for DebugErrorProvider<P>
where P: FontProvider, P::Error: Debug + 'static {
type Data = P::Data;
type Error = Box<dyn Debug>;
async fn load(&self, index: FontIndex) -> Result<Font<P::Data>, Self::Error> {
self.provider.load(index).await
.map_err(|d| Box::new(d) as Box<dyn Debug>)
}
}

View File

@ -1,4 +1,4 @@
use toddle::query::{FontWeight, FontStyle};
use fontdock::{FontStyle, FontWeight, FontWidth};
use crate::length::ScaleLength;
use super::*;
@ -40,7 +40,7 @@ function! {
styled(&self.body, ctx, Some(()),
|s, _| {
if !self.list.is_empty() {
s.fallback.list = self.list.clone();
*s.fallback.list_mut() = self.list.clone();
}
for (class, fallback) in &self.classes {
@ -105,6 +105,39 @@ function! {
}
}
function! {
/// `font.width`: Set text with a given width.
#[derive(Debug, Clone, PartialEq)]
pub struct FontWidthFunc {
body: Option<SyntaxModel>,
width: Option<FontWidth>,
}
parse(header, body, ctx, f) {
let body = body!(opt: body, ctx, f);
let width = header.args.pos.get::<Spanned<(FontWidth, bool)>>(&mut f.diagnostics)
.map(|Spanned { v: (width, is_clamped), span }| {
if is_clamped {
warning!(
@f, span,
"width should be between 1 and 9, clamped to {}",
width.to_number(),
);
}
width
})
.or_missing(&mut f.diagnostics, header.name.span, "width");
FontWidthFunc { body, width }
}
layout(self, ctx, f) {
styled(&self.body, ctx, self.width, |t, w| t.variant.width = w)
}
}
function! {
/// `font.size`: Sets the font size.
#[derive(Debug, Clone, PartialEq)]

View File

@ -19,6 +19,7 @@ pub fn std() -> Scope {
std.add::<FontFamilyFunc>("font.family");
std.add::<FontStyleFunc>("font.style");
std.add::<FontWeightFunc>("font.weight");
std.add::<FontWidthFunc>("font.width");
std.add::<FontSizeFunc>("font.size");
std.add_with_meta::<ContentSpacingFunc>("word.spacing", ContentKind::Word);

View File

@ -1,7 +1,6 @@
//! Styles for text and pages.
use toddle::fallback;
use toddle::query::{FallbackTree, FontVariant, FontStyle, FontWeight};
use fontdock::{fallback, FallbackTree, FontVariant, FontStyle, FontWeight, FontWidth};
use crate::length::{Length, Size, Margins, Value4, ScaleLength};
use crate::paper::{Paper, PaperClass, PAPER_A4};
@ -17,13 +16,16 @@ pub struct LayoutStyle {
/// Defines which fonts to use and how to space text.
#[derive(Debug, Clone, PartialEq)]
pub struct TextStyle {
/// A tree of font names and generic family names.
/// A tree of font family names and generic class names.
pub fallback: FallbackTree,
/// The selected font variant.
pub variant: FontVariant,
/// Whether the bolder toggle is active or inactive. This determines
/// whether the next `*` adds or removes font weight.
pub bolder: bool,
/// Whether the italic toggle is active or inactive. This determines
/// whether the next `_` makes italic or non-italic.
pub italic: bool,
/// The base font size.
pub base_font_size: Length,
/// The font scale to apply on the base font size.
@ -75,8 +77,10 @@ impl Default for TextStyle {
variant: FontVariant {
style: FontStyle::Normal,
weight: FontWeight(400),
width: FontWidth::Medium,
},
bolder: false,
italic: false,
base_font_size: Length::pt(11.0),
font_scale: 1.0,
word_spacing_scale: 0.25,

View File

@ -1,7 +1,7 @@
//! Value types for extracting function arguments.
use std::fmt::{self, Display, Formatter};
use toddle::query::{FontStyle, FontWeight};
use fontdock::{FontStyle, FontWeight, FontWidth};
use crate::layout::prelude::*;
use crate::length::{Length, ScaleLength};
@ -148,12 +148,11 @@ impl Value for (FontWeight, bool) {
match expr.v {
Expr::Number(weight) => {
let weight = weight.round();
if weight >= 100.0 && weight <= 900.0 {
Ok((FontWeight(weight as i16), false))
Ok((FontWeight(weight as u16), false))
} else {
let clamped = weight.min(900.0).max(100.0) as i16;
Ok((FontWeight(clamped), true))
let clamped = weight.min(900.0).max(100.0);
Ok((FontWeight(clamped as u16), true))
}
}
Expr::Ident(id) => {
@ -168,6 +167,32 @@ impl Value for (FontWeight, bool) {
}
}
/// The additional boolean specifies whether a number was clamped into the range
/// 1 - 9 to make it a valid font width.
impl Value for (FontWidth, bool) {
fn parse(expr: Spanned<Expr>) -> Result<Self, Diagnostic> {
match expr.v {
Expr::Number(width) => {
let width = width.round();
if width >= 1.0 && width <= 9.0 {
Ok((FontWidth::new(width as u16).unwrap(), false))
} else {
let clamped = width.min(9.0).max(1.0);
Ok((FontWidth::new(clamped as u16).unwrap(), true))
}
}
Expr::Ident(id) => {
FontWidth::from_name(id.as_str())
.ok_or_else(|| error!("invalid font width"))
.map(|width| (width, false))
}
other => Err(
error!("expected identifier or number, found {}", other.name())
),
}
}
}
impl Value for Paper {
fn parse(expr: Spanned<Expr>) -> Result<Self, Diagnostic> {
Paper::from_name(Ident::parse(expr)?.as_str())

View File

@ -29,10 +29,10 @@ class MultiboxRenderer:
def __init__(self, data):
self.combined = None
self.fonts = {}
for entry in data["fonts"]:
index = int(entry[0]["id"]), int(entry[0]["variant"])
self.fonts[index] = os.path.join(BASE, '../../../fonts', entry[1])
self.faces = {}
for entry in data["faces"]:
face_id = int(entry[0]["index"]), int(entry[0]["variant"])
self.faces[face_id] = os.path.join(BASE, '../../../fonts', entry[1])
self.layouts = data["layouts"]
@ -45,7 +45,7 @@ class MultiboxRenderer:
for layout in self.layouts:
size = layout["dimensions"]
renderer = BoxRenderer(self.fonts, size["x"], size["y"])
renderer = BoxRenderer(self.faces, size["x"], size["y"])
for action in layout["actions"]:
renderer.execute(action)
@ -87,8 +87,8 @@ class MultiboxRenderer:
class BoxRenderer:
def __init__(self, fonts, width, height, grid=False):
self.fonts = fonts
def __init__(self, faces, width, height, grid=False):
self.faces = faces
self.size = (pix(width), pix(height))
img = Image.new('RGBA', self.size, (255, 255, 255, 255))
@ -126,9 +126,9 @@ class BoxRenderer:
self.cursor = [pix(args[0]["x"]), pix(args[0]["y"])]
elif cmd == 1:
index = int(args[0]["id"]), int(args[0]["variant"])
face_id = int(args[0]["index"]), int(args[0]["variant"])
size = pix(args[1])
self.font = ImageFont.truetype(self.fonts[index], size)
self.font = ImageFont.truetype(self.faces[face_id], size)
elif cmd == 2:
text = args[0]

View File

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error;
use std::ffi::OsStr;
@ -5,19 +6,21 @@ use std::fs::{File, create_dir_all, read_dir, read_to_string};
use std::io::BufWriter;
use std::panic;
use std::process::Command;
use std::rc::Rc;
use std::time::{Instant, Duration};
use serde::Serialize;
use futures_executor::block_on;
use typstc::{Typesetter, DebugErrorProvider};
use typstc::Typesetter;
use typstc::font::DynProvider;
use typstc::layout::MultiLayout;
use typstc::length::{Length, Size, Value4};
use typstc::style::PageStyle;
use typstc::paper::PaperClass;
use typstc::export::pdf;
use toddle::query::FontIndex;
use toddle::query::fs::EagerFsProvider;
use fontdock::{FaceId, FontLoader};
use fontdock::fs::{FsIndex, FsProvider};
type DynResult<T> = Result<T, Box<dyn Error>>;
@ -47,12 +50,20 @@ fn main() -> DynResult<()> {
}
let len = filtered.len();
println!();
println!("Running {} test{}", len, if len > 1 { "s" } else { "" });
if len == 0 {
return Ok(());
} else if len == 1 {
println!("Running test ...");
} else {
println!("Running {} tests", len);
}
let mut index = FsIndex::new();
index.search_dir("../fonts");
for (name, src) in filtered {
panic::catch_unwind(|| {
if let Err(e) = test(&name, &src) {
if let Err(e) = test(&name, &src, &index) {
println!("error: {:?}", e);
}
}).ok();
@ -62,13 +73,15 @@ fn main() -> DynResult<()> {
}
/// Create a _PDF_ and render with a name from the source code.
fn test(name: &str, src: &str) -> DynResult<()> {
fn test(name: &str, src: &str, index: &FsIndex) -> DynResult<()> {
println!("Testing: {}.", name);
let (fs, entries) = EagerFsProvider::from_index("../fonts", "index.json")?;
let files = fs.files().to_vec();
let provider = DebugErrorProvider::new(fs);
let mut typesetter = Typesetter::new((Box::new(provider), entries));
let (descriptors, files) = index.clone().into_vecs();
let provider = FsProvider::new(files.clone());
let dynamic = Box::new(provider) as Box<DynProvider>;
let loader = FontLoader::new(dynamic, descriptors);
let loader = Rc::new(RefCell::new(loader));
let mut typesetter = Typesetter::new(loader.clone());
typesetter.set_page_style(PageStyle {
class: PaperClass::Custom,
@ -81,24 +94,25 @@ fn test(name: &str, src: &str) -> DynResult<()> {
// Write the PDF file.
let path = format!("tests/cache/{}.pdf", name);
let file = BufWriter::new(File::create(path)?);
pdf::export(&layouts, typesetter.loader(), file)?;
pdf::export(&layouts, &loader, file)?;
// Compute the font's paths.
let mut fonts = HashMap::new();
let mut faces = HashMap::new();
for layout in &layouts {
for index in layout.find_used_fonts() {
fonts.entry(index)
.or_insert_with(|| files[index.id][index.variant].as_str());
for id in layout.find_used_fonts() {
faces.entry(id).or_insert_with(|| {
files[id.index][id.variant].0.to_str().unwrap()
});
}
}
#[derive(Serialize)]
struct Document<'a> {
fonts: Vec<(FontIndex, &'a str)>,
faces: Vec<(FaceId, &'a str)>,
layouts: MultiLayout,
}
let document = Document { fonts: fonts.into_iter().collect(), layouts};
let document = Document { faces: faces.into_iter().collect(), layouts };
// Serialize the document into JSON.
let path = format!("tests/cache/{}.serde.json", name);