Skip to content

Commit

Permalink
use Dashmap instead of RwLock<Hashmap>
Browse files Browse the repository at this point in the history
staticintlucas committed Dec 25, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 165be05 commit c2f86e5
Showing 2 changed files with 19 additions and 39 deletions.
1 change: 1 addition & 0 deletions keyset-font/Cargo.toml
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ license.workspace = true
geom = { package = "keyset-geom", path = "../keyset-geom", version = "0.1.0" }

byte-strings = "0.3"
dashmap = "5.1"
log = "0.4"
ouroboros = "0.18"
ttf-parser = { version = "0.20", default-features = false, features = ["std"]}
57 changes: 18 additions & 39 deletions keyset-font/src/lib.rs
Original file line number Diff line number Diff line change
@@ -19,10 +19,10 @@ mod default;
mod error;
mod face;

use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::{OnceLock, RwLock};
use std::sync::OnceLock;

use dashmap::DashMap;
use geom::{BezPath, Rect, Shape};
use log::warn;
use ttf_parser::GlyphId;
@@ -103,15 +103,16 @@ impl Glyph {
}

/// A parsed font
#[derive(Clone)]
pub struct Font {
face: Face,
family: OnceLock<String>,
name: OnceLock<String>,
cap_height: OnceLock<f64>,
x_height: OnceLock<f64>,
notdef: OnceLock<Glyph>,
glyphs: RwLock<HashMap<char, Option<Glyph>>>,
kerning: RwLock<HashMap<(char, char), f64>>,
glyphs: DashMap<char, Option<Glyph>>,
kerning: DashMap<(char, char), f64>,
}

impl Debug for Font {
@@ -120,21 +121,6 @@ impl Debug for Font {
}
}

impl Clone for Font {
fn clone(&self) -> Self {
Self {
face: self.face.clone(),
family: self.family.clone(),
name: self.name.clone(),
cap_height: self.cap_height.clone(),
x_height: self.x_height.clone(),
notdef: self.notdef.clone(),
glyphs: RwLock::new(self.glyphs.read().unwrap().clone()),
kerning: RwLock::new(self.kerning.read().unwrap().clone()),
}
}
}

impl Default for Font {
fn default() -> Self {
default::font().clone()
@@ -155,8 +141,8 @@ impl Font {
cap_height: OnceLock::new(),
x_height: OnceLock::new(),
notdef: OnceLock::new(),
glyphs: RwLock::new(HashMap::new()),
kerning: RwLock::new(HashMap::new()),
glyphs: DashMap::new(),
kerning: DashMap::new(),
})
}

@@ -270,8 +256,6 @@ impl Font {
#[allow(clippy::missing_panics_doc)] // Only unwrapping a mutex
pub fn glyph(&self, char: char) -> Option<Glyph> {
self.glyphs
.write()
.unwrap()
.entry(char)
.or_insert_with(|| {
self.face
@@ -302,20 +286,15 @@ impl Font {
/// font
#[allow(clippy::missing_panics_doc)] // Only unwrapping a mutex
pub fn kerning(&self, left: char, right: char) -> f64 {
*self
.kerning
.write()
.unwrap()
.entry((left, right))
.or_insert_with(|| {
if let (Some(lhs), Some(rhs)) =
(self.face.glyph_index(left), self.face.glyph_index(right))
{
self.face.glyphs_kerning(lhs, rhs).map_or(0.0, f64::from)
} else {
0.0
}
})
*self.kerning.entry((left, right)).or_insert_with(|| {
if let (Some(lhs), Some(rhs)) =
(self.face.glyph_index(left), self.face.glyph_index(right))
{
self.face.glyphs_kerning(lhs, rhs).map_or(0.0, f64::from)
} else {
0.0
}
})
}
}

@@ -374,8 +353,8 @@ mod tests {
assert!(font.cap_height.get().is_none());
assert!(font.x_height.get().is_none());
assert!(font.notdef.get().is_none());
assert_eq!(font.glyphs.read().unwrap().len(), 0);
assert_eq!(font.kerning.read().unwrap().len(), 0);
assert_eq!(font.glyphs.len(), 0);
assert_eq!(font.kerning.len(), 0);
}

#[test]

0 comments on commit c2f86e5

Please sign in to comment.