From fb852d3ab5ef25dca26d87a98f7a0dd3b871f328 Mon Sep 17 00:00:00 2001 From: Stewart Connor Date: Wed, 26 Mar 2025 14:48:54 +1100 Subject: [PATCH] changes before ref --- src/attrs.rs | 26 ++++++++++++++------------ src/shape.rs | 23 ++++++++++++----------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/attrs.rs b/src/attrs.rs index 33faf91..4a08b3e 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -5,8 +5,6 @@ use alloc::vec::Vec; use core::ops::Range; use rangemap::RangeMap; use smol_str::SmolStr; -use std::collections::HashMap; -use std::rc::Rc; use crate::{CacheKeyFlags, Metrics}; @@ -148,6 +146,10 @@ impl FeatureTag { pub const STYLISTIC_SET_1: Self = Self::new(b"ss01"); pub const STYLISTIC_SET_2: Self = Self::new(b"ss02"); // Add more common features as needed + + pub fn as_bytes(&self) -> &[u8; 4] { + &self.0 + } } /// Font feature with tag and value @@ -194,7 +196,7 @@ impl FontFeatures { } /// Text attributes -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct Attrs<'a> { //TODO: should this be an option? pub color_opt: Option, @@ -205,7 +207,7 @@ pub struct Attrs<'a> { pub metadata: usize, pub cache_key_flags: CacheKeyFlags, pub metrics_opt: Option, - pub font_features: Option>, + pub font_features: Option, } impl<'a> Attrs<'a> { @@ -276,7 +278,7 @@ impl<'a> Attrs<'a> { /// Set [FontFeatures] pub fn font_features(mut self, font_features: FontFeatures) -> Self { - self.font_features = Some(Rc::new(font_features)); + self.font_features = Some(font_features); self } @@ -328,7 +330,7 @@ pub struct AttrsOwned { pub metadata: usize, pub cache_key_flags: CacheKeyFlags, pub metrics_opt: Option, - pub font_features: Option>, + pub font_features: Option, } impl AttrsOwned { @@ -371,9 +373,9 @@ pub struct AttrsList { impl AttrsList { /// Create a new attributes list with a set of default [Attrs] - pub fn new(defaults: Attrs) -> Self { + pub fn new(defaults: &Attrs) -> Self { Self { - defaults: AttrsOwned::new(defaults), + defaults: AttrsOwned::new(defaults.clone()), spans: RangeMap::new(), } } @@ -399,13 +401,13 @@ impl AttrsList { } /// Add an attribute span, removes any previous matching parts of spans - pub fn add_span(&mut self, range: Range, attrs: Attrs) { + pub fn add_span(&mut self, range: Range, attrs: &Attrs) { //do not support 1..1 or 2..1 even if by accident. if range.is_empty() { return; } - self.spans.insert(range, AttrsOwned::new(attrs)); + self.spans.insert(range, AttrsOwned::new(attrs.clone())); } /// Get the attribute span for an index @@ -455,8 +457,8 @@ impl AttrsList { } /// Resets the attributes with new defaults. - pub(crate) fn reset(mut self, default: Attrs) -> Self { - self.defaults = AttrsOwned::new(default); + pub(crate) fn reset(mut self, default: &Attrs) -> Self { + self.defaults = AttrsOwned::new(default.clone()); self.spans.clear(); self } diff --git a/src/shape.rs b/src/shape.rs index 2718aa0..1edcc5e 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -141,17 +141,18 @@ fn shape_fallback( assert_eq!(rtl, span_rtl); let attrs = attrs_list.get_span(start_run); + let mut rb_features = Vec::new(); // Convert attrs::Feature to rustybuzz::Feature - let font_features = attrs.get_font_features(); - let mut rb_features = Vec::with_capacity(font_features.features.len()); - for feature in &font_features.features { - rb_features.push(rustybuzz::Feature::new( - rustybuzz::ttf_parser::Tag::from_bytes(&feature.tag.as_bytes()), - feature.value, - 0..usize::MAX, - )); - } + if let Some(font_features) = attrs.font_features { + for feature in font_features.features { + rb_features.push(rustybuzz::Feature::new( + rustybuzz::ttf_parser::Tag::from_bytes(&feature.tag.as_bytes()), + feature.value, + 0..usize::MAX, + )); + } + }; let shape_plan = rustybuzz::ShapePlan::new( font.rustybuzz(), @@ -261,7 +262,7 @@ fn shape_run( let attrs = attrs_list.get_span(start_run); - let fonts = font_system.get_font_matches(attrs); + let fonts = font_system.get_font_matches(&attrs); let default_families = [&attrs.family]; let mut font_iter = FontFallbackIter::new( @@ -444,7 +445,7 @@ fn shape_skip( end_run: usize, ) { let attrs = attrs_list.get_span(start_run); - let fonts = font_system.get_font_matches(attrs); + let fonts = font_system.get_font_matches(&attrs); let default_families = [&attrs.family]; let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[], "");