From 29754777256aad83780c8e27780b733e2230ae5e Mon Sep 17 00:00:00 2001 From: marceline-cramer Date: Sat, 16 Jul 2022 15:13:55 -0600 Subject: [PATCH] Cruddy hacked-together owning Font struct --- Cargo.toml | 1 + src/text.rs | 68 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4673391..63a7fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,6 @@ anyhow = "1" bytemuck = "1" canary_types = { path = "crates/types" } lyon = "1" +ouroboros = "^0.15" parking_lot = "0.12" wasmtime = "0.38" diff --git a/src/text.rs b/src/text.rs index 86494d5..d0912e5 100644 --- a/src/text.rs +++ b/src/text.rs @@ -9,8 +9,9 @@ use allsorts::outline::OutlineBuilder; use allsorts::pathfinder_geometry::{line_segment::LineSegment2F, vector::Vector2F}; use allsorts::tables::{glyf::GlyfTable, loca::LocaTable}; use allsorts::tables::{FontTableProvider, SfntVersion}; -use allsorts::{tag, Font}; +use allsorts::{tag, Font as AllsortsFont}; use lyon::path::Path; +use ouroboros::self_referencing; const TEXT_SCALE: f32 = 0.0001; @@ -19,7 +20,7 @@ pub struct GlyphCache { } impl GlyphCache { - pub fn new(font: &mut Font) -> Self { + pub fn new(font: &AllsortsFont) -> Self { let glyph_num = font.num_glyphs(); let mut glyphs = Vec::with_capacity(glyph_num as usize); @@ -127,6 +128,43 @@ pub struct GlyphMesh { indices: Vec, } +#[self_referencing] +pub struct Font { + file_buffer: Vec, + + #[borrows(file_buffer)] + #[covariant] + read_scope: ReadScope<'this>, + + #[borrows(read_scope)] + #[not_covariant] + font_data: FontData<'this>, + + #[borrows(font_data)] + #[covariant] + inner: AllsortsFont>, +} + +impl Font { + pub fn load(file_buffer: Vec) -> Self { + FontBuilder { + file_buffer, + read_scope_builder: |buffer| ReadScope::new(buffer), + font_data_builder: |scope| scope.read::>().unwrap(), + inner_builder: |font_data| { + AllsortsFont::new(font_data.table_provider(0).unwrap()) + .unwrap() + .unwrap() + }, + } + .build() + } + + pub fn create_glyph_cache(&self) -> GlyphCache { + GlyphCache::new(self.borrow_inner()) + } +} + pub struct DummyText { vertices: Vec, indices: Vec, @@ -136,26 +174,28 @@ impl Default for DummyText { fn default() -> Self { let script = tag::LATN; let buffer = include_bytes!("linja-pona-4.9.otf"); - let buffer = buffer.as_slice(); - let scope = ReadScope::new(&buffer); - let font_file = scope.read::>().unwrap(); - let provider = font_file.table_provider(0).unwrap(); - let mut font = Font::new(provider).unwrap().unwrap(); - let mut cache = GlyphCache::new(&mut font); let text = "toki o! nimi mi li [_mun_alasa_sona]. toki+pona li pona."; - let glyphs = font.map_glyphs(&text, script, MatchingPresentation::Required); + let mut font = Font::load(buffer.to_vec()); + let mut cache = font.create_glyph_cache(); + + let glyphs = font + .with_inner_mut(|font| font.map_glyphs(&text, script, MatchingPresentation::Required)); let lang_tag = None; let features = allsorts::gsub::Features::default(); let kerning = true; - let infos = font - .shape(glyphs, script, lang_tag, &features, kerning) - .unwrap(); + let infos = font.with_inner_mut(|font| { + font.shape(glyphs, script, lang_tag, &features, kerning) + .unwrap() + }); let direction = TextDirection::LeftToRight; let vertical = false; - let mut layout = GlyphLayout::new(&mut font, &infos, direction, vertical); - let positions = layout.glyph_positions().unwrap(); + + let positions = font.with_inner_mut(|font| { + let mut layout = GlyphLayout::new(font, &infos, direction, vertical); + layout.glyph_positions().unwrap() + }); let mut mesh = Self { vertices: Vec::new(),