Skip to content

Commit

Permalink
0.12.1: Make collection of monospace fallback information optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Jul 31, 2024
1 parent 4f31665 commit 58c2ccd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.12.1] - 2024-06-31

### Changed

- Make collection of monospace fallback information optional

## [0.12.0] - 2024-06-18

### Added
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cosmic-text"
description = "Pure Rust multi-line text handling"
version = "0.12.0"
version = "0.12.1"
authors = ["Jeremy Soller <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -38,6 +38,7 @@ features = ["hardcoded-data"]
[features]
default = ["std", "swash", "fontconfig"]
fontconfig = ["fontdb/fontconfig", "std"]
monospace_fallback = []
no_std = ["rustybuzz/libm", "hashbrown", "dep:libm"]
shape-run-cache = []
std = [
Expand Down
36 changes: 24 additions & 12 deletions src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ self_cell!(
}
);

struct FontMonospaceFallback {
monospace_em_width: Option<f32>,
scripts: Vec<[u8; 4]>,
unicode_codepoints: Vec<u32>,
}

/// A font
pub struct Font {
#[cfg(feature = "swash")]
swash: (u32, swash::CacheKey),
rustybuzz: OwnedFace,
data: Arc<dyn AsRef<[u8]> + Send + Sync>,
id: fontdb::ID,
monospace_em_width: Option<f32>,
scripts: Vec<[u8; 4]>,
unicode_codepoints: Vec<u32>,
monospace_fallback: Option<FontMonospaceFallback>,
}

impl fmt::Debug for Font {
Expand All @@ -51,15 +55,19 @@ impl Font {
}

pub fn monospace_em_width(&self) -> Option<f32> {
self.monospace_em_width
self.monospace_fallback
.as_ref()
.and_then(|x| x.monospace_em_width)
}

pub fn scripts(&self) -> &[[u8; 4]] {
&self.scripts
self.monospace_fallback.as_ref().map_or(&[], |x| &x.scripts)
}

pub fn unicode_codepoints(&self) -> &[u32] {
&self.unicode_codepoints
self.monospace_fallback
.as_ref()
.map_or(&[], |x| &x.unicode_codepoints)
}

pub fn data(&self) -> &[u8] {
Expand All @@ -85,7 +93,7 @@ impl Font {
pub fn new(db: &fontdb::Database, id: fontdb::ID) -> Option<Self> {
let info = db.face(id)?;

let (monospace_em_width, scripts, unicode_codepoints) = {
let monospace_fallback = if cfg!(feature = "monospace_fallback") {
db.with_face_data(id, |font_data, face_index| {
let face = ttf_parser::Face::parse(font_data, face_index).ok()?;
let monospace_em_width = info
Expand Down Expand Up @@ -128,9 +136,15 @@ impl Font {

unicode_codepoints.shrink_to_fit();

Some((monospace_em_width, scripts, unicode_codepoints))
Some(FontMonospaceFallback {
monospace_em_width,
scripts,
unicode_codepoints,
})
})?
}?;
} else {
None
};

let data = match &info.source {
fontdb::Source::Binary(data) => Arc::clone(data),
Expand All @@ -145,9 +159,7 @@ impl Font {

Some(Self {
id: info.id,
monospace_em_width,
scripts,
unicode_codepoints,
monospace_fallback,
#[cfg(feature = "swash")]
swash: {
let swash = swash::FontRef::from_index((*data).as_ref(), info.index as usize)?;
Expand Down

0 comments on commit 58c2ccd

Please sign in to comment.