Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fontheight-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ ordered-float = "4.6"
rustybuzz = "0.20.1"
skrifa = "0.26.5"
thiserror = "2"
unicode-script = "0.5.7"
31 changes: 31 additions & 0 deletions fontheight-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use skrifa::{
MetadataProvider,
};
use thiserror::Error;
use unicode_script::UnicodeScript;
pub use unicode_script::{Script, ScriptExtension};

use crate::{locations::interesting_locations, pens::BezierPen};

Expand Down Expand Up @@ -166,3 +168,32 @@ pub enum FontHeightError {
#[error(transparent)]
Drawing(#[from] SkrifaDrawError),
}

pub fn discover_font_scripts(
font: &skrifa::FontRef,
) -> Result<ScriptExtension, ScriptDiscoveryError> {
let mut empty_cmap = true;
let scripts = font.charmap().mappings().try_fold(
ScriptExtension::default(),
|acc, (codepoint, _)| {
empty_cmap = false;
let script_extension = char::from_u32(codepoint)
.ok_or(ScriptDiscoveryError::InvalidCodepoint(codepoint))?
.script_extension();
Ok(acc.union(script_extension))
},
)?;
if empty_cmap {
Err(ScriptDiscoveryError::EmptyCmap)
} else {
Ok(scripts)
}
}

#[derive(Debug, Error)]
pub enum ScriptDiscoveryError {
#[error("invalid codepoint in cmap: {0:#x}")]
InvalidCodepoint(u32),
#[error("empty cmap")]
EmptyCmap,
}
32 changes: 30 additions & 2 deletions fontheight-core/src/word_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ use std::{

use thiserror::Error;

use crate::{Script, ScriptExtension};

#[derive(Debug)]
pub struct WordList {
name: String,
words: Vec<String>,
scripts: ScriptExtension,
}

impl WordList {
pub fn load(
name: impl Into<String>,
path: impl AsRef<Path>,
scripts: ScriptExtension,
) -> Result<Self, WordListError> {
let path = path.as_ref();
let file_content = fs::read_to_string(path).map_err(|io_err| {
Expand All @@ -29,29 +33,43 @@ impl WordList {
.filter(|word| !word.is_empty())
.map(String::from)
.collect(),
scripts,
})
}

pub fn define(
name: impl Into<String>,
words: impl IntoIterator<Item = impl Into<String>>,
scripts: ScriptExtension,
) -> Self {
WordList {
name: name.into(),
words: words.into_iter().map(Into::into).collect(),
scripts,
}
}

// Private API used by static-lang-word-lists
#[doc(hidden)]
pub fn new(name: String, words: Vec<String>) -> Self {
WordList { name, words }
#[inline]
pub fn new(
name: String,
words: Vec<String>,
scripts: ScriptExtension,
) -> Self {
WordList {
name,
words,
scripts,
}
}

#[inline]
pub fn name(&self) -> &str {
&self.name
}

#[inline]
pub fn iter(&self) -> WordListIter {
WordListIter(self.words.iter())
}
Expand All @@ -70,6 +88,16 @@ impl WordList {
pub fn get(&self, index: usize) -> Option<&str> {
self.words.get(index).map(|word| word.as_str())
}

#[inline]
pub fn covers(&self, script: Script) -> bool {
self.scripts.contains_script(script)
}

#[inline]
pub fn covers_all(&self, scripts: ScriptExtension) -> bool {
self.scripts.intersection(scripts) == self.scripts
}
}

impl Index<usize> for WordList {
Expand Down
Loading