Add documentation and improve API

This commit is contained in:
Jeremy Soller 2022-10-18 17:13:48 -06:00
parent de572b1645
commit a242d817e9
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
10 changed files with 76 additions and 71 deletions

27
src/font/font.rs Normal file
View file

@ -0,0 +1,27 @@
use std::{collections::HashMap, sync::Mutex};
use super::{CacheKey, CacheItem};
pub struct Font<'a> {
pub info: &'a fontdb::FaceInfo,
pub data: &'a [u8],
pub index: u32,
pub rustybuzz: rustybuzz::Face<'a>,
pub swash: swash::FontRef<'a>,
pub scale_context: Mutex<swash::scale::ScaleContext>,
pub cache: Mutex<HashMap<CacheKey, CacheItem>>,
}
impl<'a> Font<'a> {
pub fn new(info: &'a fontdb::FaceInfo, data: &'a [u8], index: u32) -> Option<Self> {
Some(Self {
info,
data,
index,
rustybuzz: rustybuzz::Face::from_slice(data, index)?,
swash: swash::FontRef::from_index(data, index as usize)?,
scale_context: Mutex::new(swash::scale::ScaleContext::new()),
cache: Mutex::new(HashMap::new()),
})
}
}

View file

@ -1,4 +1,5 @@
use super::{CacheKey, Font, FontLineIndex};
use crate::TextLineIndex;
use super::{CacheKey, Font};
pub struct FontLayoutGlyph<'a> {
pub start: usize,
@ -10,7 +11,7 @@ pub struct FontLayoutGlyph<'a> {
}
pub struct FontLayoutLine<'a> {
pub line_i: FontLineIndex,
pub line_i: TextLineIndex,
pub glyphs: Vec<FontLayoutGlyph<'a>>,
}

View file

@ -1,8 +1,10 @@
use unicode_script::{Script, UnicodeScript};
use super::{Font, FontLineIndex, FontShapeGlyph, FontShapeLine, FontShapeSpan, FontShapeWord};
use crate::TextLineIndex;
use super::{Font, FontShapeGlyph, FontShapeLine, FontShapeSpan, FontShapeWord};
use super::fallback::{FontFallbackIter};
/// Fonts that match a pattern
pub struct FontMatches<'a> {
pub locale: &'a str,
pub fonts: Vec<Font<'a>>,
@ -285,7 +287,7 @@ impl<'a> FontMatches<'a> {
}
}
pub fn shape_line(&self, line_i: FontLineIndex, line: &str) -> FontShapeLine {
pub fn shape_line(&self, line_i: TextLineIndex, line: &str) -> FontShapeLine {
let mut spans = Vec::new();
let bidi = unicode_bidi::BidiInfo::new(line, None);

View file

@ -1,60 +1,19 @@
use std::{collections::HashMap, sync::Mutex};
pub(crate) mod fallback;
pub mod fallback;
pub use self::cache::*;
pub(crate) use self::cache::*;
mod cache;
pub use self::layout::*;
pub(crate) use self::font::*;
mod font;
pub(crate) use self::layout::*;
mod layout;
pub use self::matches::*;
mod matches;
pub use self::shape::*;
pub(crate) use self::shape::*;
mod shape;
pub use self::system::*;
mod system;
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FontCacheKey {
glyph_id: u16,
}
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct FontLineIndex(usize);
impl FontLineIndex {
pub fn new(index: usize) -> Self {
Self(index)
}
pub fn get(&self) -> usize {
self.0
}
}
pub struct Font<'a> {
pub info: &'a fontdb::FaceInfo,
pub data: &'a [u8],
pub index: u32,
pub rustybuzz: rustybuzz::Face<'a>,
pub swash: swash::FontRef<'a>,
pub scale_context: Mutex<swash::scale::ScaleContext>,
pub cache: Mutex<HashMap<CacheKey, CacheItem>>,
}
impl<'a> Font<'a> {
pub fn new(info: &'a fontdb::FaceInfo, data: &'a [u8], index: u32) -> Option<Self> {
Some(Self {
info,
data,
index,
rustybuzz: rustybuzz::Face::from_slice(data, index)?,
swash: swash::FontRef::from_index(data, index as usize)?,
scale_context: Mutex::new(swash::scale::ScaleContext::new()),
cache: Mutex::new(HashMap::new()),
})
}
}

View file

@ -1,4 +1,5 @@
use super::{CacheKey, Font, FontLayoutGlyph, FontLayoutLine, FontLineIndex};
use crate::TextLineIndex;
use super::{CacheKey, Font, FontLayoutGlyph, FontLayoutLine};
pub struct FontShapeGlyph<'a> {
pub start: usize,
@ -40,7 +41,7 @@ pub struct FontShapeSpan<'a> {
}
pub struct FontShapeLine<'a> {
pub line_i: FontLineIndex,
pub line_i: TextLineIndex,
pub rtl: bool,
pub spans: Vec<FontShapeSpan<'a>>,
}

View file

@ -2,6 +2,7 @@ use std::ops::Deref;
use super::{Font, FontMatches};
/// Access system fonts
pub struct FontSystem {
pub locale: String,
db: fontdb::Database,