Skip to content

Commit 2a24658

Browse files
ickshonpemockersf
andauthored
Update to cosmic-text 0.15 (#21699)
# Objective Update `bevy_text`'s cosmic-text dependency to 0.15 Fixes #21766 ## Solution Very minimal changes here, nothing user facing except an error return type. * Cosmic-text no longer uses `ttf_parser`, so, to validate fonts during asset loading, a `skrifa::FontRef` is constructed instead. If the font is invalid, the font asset loader now returns a `skrifa::ReadError`, not a `ttf_parser::FaceParsingError`. * To get a font from the global `CosmicFontSystem` instance, the `get_font` function requires a weight now. The weight is looked up from the `CosmicFontSystems` font database, immediately before the `get_font` call. The changes here are a little hackish, but that's a symptom of our lack of support for font collections. Co-authored-by: François Mockers <[email protected]>
1 parent e5508b4 commit 2a24658

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

crates/bevy_text/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bevy_platform = { path = "../bevy_platform", version = "0.18.0-dev", default-fea
3030

3131
# other
3232
wgpu-types = { version = "26", default-features = false }
33-
cosmic-text = { version = "0.14", features = ["shape-run-cache"] }
33+
cosmic-text = { version = "0.15", features = ["shape-run-cache"] }
3434
thiserror = { version = "2", default-features = false }
3535
serde = { version = "1", features = ["derive"] }
3636
smallvec = { version = "1", default-features = false }

crates/bevy_text/src/font.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use alloc::sync::Arc;
22

33
use bevy_asset::Asset;
44
use bevy_reflect::TypePath;
5+
use cosmic_text::skrifa::raw::ReadError;
6+
use cosmic_text::skrifa::FontRef;
57

68
/// An [`Asset`] that contains the data for a loaded font, if loaded as an asset.
79
///
@@ -23,11 +25,8 @@ pub struct Font {
2325

2426
impl Font {
2527
/// Creates a [`Font`] from bytes
26-
pub fn try_from_bytes(
27-
font_data: Vec<u8>,
28-
) -> Result<Self, cosmic_text::ttf_parser::FaceParsingError> {
29-
use cosmic_text::ttf_parser;
30-
ttf_parser::Face::parse(&font_data, 0)?;
28+
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, ReadError> {
29+
let _ = FontRef::from_index(&font_data, 0)?;
3130
Ok(Self {
3231
data: Arc::new(font_data),
3332
})

crates/bevy_text/src/font_loader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::Font;
22
use bevy_asset::{io::Reader, AssetLoader, LoadContext};
3+
use cosmic_text::skrifa::raw::ReadError;
34
use thiserror::Error;
45

56
#[derive(Default)]
@@ -12,7 +13,7 @@ pub struct FontLoader;
1213
pub enum FontLoaderError {
1314
/// The contents that could not be parsed
1415
#[error(transparent)]
15-
Content(#[from] cosmic_text::ttf_parser::FaceParsingError),
16+
Content(#[from] ReadError),
1617
/// An [IO](std::io) Error
1718
#[error(transparent)]
1819
Io(#[from] std::io::Error),

crates/bevy_text/src/pipeline.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ impl TextPipeline {
310310
let Some((id, _)) = self.map_handle_to_font_id.get(font) else {
311311
continue;
312312
};
313-
if let Some(font) = font_system.get_font(*id) {
313+
let weight = font_system
314+
.db()
315+
.face(*id)
316+
.map(|f| f.weight)
317+
.unwrap_or(cosmic_text::Weight::NORMAL);
318+
if let Some(font) = font_system.get_font(*id, weight) {
314319
let swash = font.as_swash();
315320
let metrics = swash.metrics(&[]);
316321
let upem = metrics.units_per_em as f32;

0 commit comments

Comments
 (0)