Font mutex-locking

This commit is contained in:
marceline-cramer 2022-07-18 15:51:18 -06:00
parent 342e8b142e
commit 7e15a2f6a8
1 changed files with 58 additions and 44 deletions

View File

@ -3,7 +3,7 @@ use super::{DrawCommand, MeshIndex, MeshVertex};
use allsorts::binary::read::ReadScope;
use allsorts::cff::CFF;
use allsorts::font::{GlyphTableFlags, MatchingPresentation};
use allsorts::font_data::{DynamicFontTableProvider, FontData};
use allsorts::font_data::{DynamicFontTableProvider, FontData as AllsortsFontData};
use allsorts::glyph_position::{GlyphLayout, TextDirection};
use allsorts::outline::OutlineBuilder;
use allsorts::pathfinder_geometry::{line_segment::LineSegment2F, vector::Vector2F};
@ -13,6 +13,7 @@ use allsorts::{tag, Font as AllsortsFont};
use canary_types::GlyphPosition;
use lyon::path::Path;
use ouroboros::self_referencing;
use parking_lot::{Mutex, RwLock};
const TEXT_SCALE: f32 = 0.075;
@ -142,26 +143,9 @@ pub struct GlyphMesh {
indices: Vec<MeshIndex>,
}
#[self_referencing]
pub struct Font {
file_buffer: Vec<u8>,
face_index: u16,
script: u32,
direction: TextDirection,
vertical: bool,
data: Mutex<FontData>,
glyph_cache: GlyphCache,
#[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 {
@ -172,31 +156,62 @@ impl Font {
vertical: bool,
file_buffer: Vec<u8>,
) -> Self {
let mut built = FontBuilder {
let mut data = FontData::load(face_index, script, direction, vertical, file_buffer);
let mut glyph_cache = GlyphCache::empty();
data.with_inner_mut(|font| glyph_cache.load_paths(font));
let data = Mutex::new(data);
Self { data, glyph_cache }
}
pub fn shape(&self, text: &str) -> Vec<GlyphPosition> {
self.data.lock().shape(text)
}
}
#[self_referencing]
pub struct FontData {
file_buffer: Vec<u8>,
face_index: u16,
script: u32,
direction: TextDirection,
vertical: bool,
#[borrows(file_buffer)]
#[covariant]
read_scope: ReadScope<'this>,
#[borrows(read_scope)]
#[not_covariant]
font_data: AllsortsFontData<'this>,
#[borrows(font_data)]
#[covariant]
inner: AllsortsFont<DynamicFontTableProvider<'this>>,
}
impl FontData {
pub fn load(
face_index: u16,
script: u32,
direction: TextDirection,
vertical: bool,
file_buffer: Vec<u8>,
) -> Self {
FontDataBuilder {
file_buffer,
face_index,
script,
direction,
vertical,
glyph_cache: GlyphCache::empty(),
read_scope_builder: |buffer| ReadScope::new(buffer),
font_data_builder: |scope| scope.read::<FontData<'_>>().unwrap(),
font_data_builder: |scope| scope.read::<AllsortsFontData<'_>>().unwrap(),
inner_builder: |font_data| {
AllsortsFont::new(font_data.table_provider(0).unwrap())
.unwrap()
.unwrap()
},
}
.build();
let mut glyph_cache = GlyphCache::empty();
built.with_inner_mut(|font| {
glyph_cache.load_paths(font);
});
built.with_glyph_cache_mut(|old| std::mem::replace(old, glyph_cache));
built
.build()
}
pub fn shape(&mut self, text: &str) -> Vec<GlyphPosition> {
@ -247,7 +262,7 @@ impl FontStore {
pub fn load_font(&self, title: &str) -> Font {
use font_kit::family_name::FamilyName;
use font_kit::handle::Handle;
use font_kit::properties::{Properties, Style};
use font_kit::properties::Properties;
println!("loading font family {}", title);
@ -298,17 +313,16 @@ impl Default for DummyText {
let mut font = store.load_font(family);
let glyphs = font.shape(text);
font.with_glyph_cache_mut(|cache| {
let mut xcur = 0;
let mut ycur = 0;
for glyph in glyphs.iter() {
let xpos = xcur + glyph.xoff;
let ypos = ycur + glyph.yoff;
xcur += glyph.hori_advance;
ycur += glyph.vert_advance;
cache.draw(&mut mesh, glyph.index, xpos, ypos, line_offset);
}
});
let mut xcur = 0;
let mut ycur = 0;
for glyph in glyphs.iter() {
let xpos = xcur + glyph.xoff;
let ypos = ycur + glyph.yoff;
xcur += glyph.hori_advance;
ycur += glyph.vert_advance;
font.glyph_cache
.draw(&mut mesh, glyph.index, xpos, ypos, line_offset);
}
line_offset += line_height;
}