Cruddy hacked-together owning Font struct

This commit is contained in:
marceline-cramer 2022-07-16 15:13:55 -06:00
parent 9cd5aa3822
commit 2975477725
2 changed files with 55 additions and 14 deletions

View File

@ -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"

View File

@ -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<DynamicFontTableProvider>) -> Self {
pub fn new(font: &AllsortsFont<DynamicFontTableProvider>) -> 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<MeshIndex>,
}
#[self_referencing]
pub struct Font {
file_buffer: Vec<u8>,
#[borrows(file_buffer)]
#[covariant]
read_scope: ReadScope<'this>,
#[borrows(read_scope)]
#[not_covariant]
font_data: FontData<'this>,
#[borrows(font_data)]
#[covariant]
inner: AllsortsFont<DynamicFontTableProvider<'this>>,
}
impl Font {
pub fn load(file_buffer: Vec<u8>) -> Self {
FontBuilder {
file_buffer,
read_scope_builder: |buffer| ReadScope::new(buffer),
font_data_builder: |scope| scope.read::<FontData<'_>>().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<MeshVertex>,
indices: Vec<MeshIndex>,
@ -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::<FontData<'_>>().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(),