cosmic_text/
attrs.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#[cfg(not(feature = "std"))]
4use alloc::vec::Vec;
5use core::hash::{Hash, Hasher};
6use core::ops::Range;
7use rangemap::RangeMap;
8use smol_str::SmolStr;
9
10use crate::{CacheKeyFlags, Metrics};
11
12pub use fontdb::{Family, Stretch, Style, Weight};
13
14/// Text color
15#[derive(Clone, Copy, Debug, PartialOrd, Ord, Eq, Hash, PartialEq)]
16pub struct Color(pub u32);
17
18impl Color {
19    /// Create new color with red, green, and blue components
20    #[inline]
21    pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
22        Self::rgba(r, g, b, 0xFF)
23    }
24
25    /// Create new color with red, green, blue, and alpha components
26    #[inline]
27    pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
28        Self(((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32))
29    }
30
31    /// Get a tuple over all of the attributes, in `(r, g, b, a)` order.
32    #[inline]
33    pub const fn as_rgba_tuple(self) -> (u8, u8, u8, u8) {
34        (self.r(), self.g(), self.b(), self.a())
35    }
36
37    /// Get an array over all of the components, in `[r, g, b, a]` order.
38    #[inline]
39    pub const fn as_rgba(self) -> [u8; 4] {
40        [self.r(), self.g(), self.b(), self.a()]
41    }
42
43    /// Get the red component
44    #[inline]
45    pub const fn r(&self) -> u8 {
46        ((self.0 & 0x00_FF_00_00) >> 16) as u8
47    }
48
49    /// Get the green component
50    #[inline]
51    pub const fn g(&self) -> u8 {
52        ((self.0 & 0x00_00_FF_00) >> 8) as u8
53    }
54
55    /// Get the blue component
56    #[inline]
57    pub const fn b(&self) -> u8 {
58        (self.0 & 0x00_00_00_FF) as u8
59    }
60
61    /// Get the alpha component
62    #[inline]
63    pub const fn a(&self) -> u8 {
64        ((self.0 & 0xFF_00_00_00) >> 24) as u8
65    }
66}
67
68/// An owned version of [`Family`]
69#[derive(Clone, Debug, Eq, Hash, PartialEq)]
70pub enum FamilyOwned {
71    Name(SmolStr),
72    Serif,
73    SansSerif,
74    Cursive,
75    Fantasy,
76    Monospace,
77}
78
79impl FamilyOwned {
80    pub fn new(family: Family) -> Self {
81        match family {
82            Family::Name(name) => Self::Name(SmolStr::from(name)),
83            Family::Serif => Self::Serif,
84            Family::SansSerif => Self::SansSerif,
85            Family::Cursive => Self::Cursive,
86            Family::Fantasy => Self::Fantasy,
87            Family::Monospace => Self::Monospace,
88        }
89    }
90
91    pub fn as_family(&self) -> Family<'_> {
92        match self {
93            Self::Name(name) => Family::Name(name),
94            Self::Serif => Family::Serif,
95            Self::SansSerif => Family::SansSerif,
96            Self::Cursive => Family::Cursive,
97            Self::Fantasy => Family::Fantasy,
98            Self::Monospace => Family::Monospace,
99        }
100    }
101}
102
103/// Metrics, but implementing Eq and Hash using u32 representation of f32
104//TODO: what are the edge cases of this?
105#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
106pub struct CacheMetrics {
107    font_size_bits: u32,
108    line_height_bits: u32,
109}
110
111impl From<Metrics> for CacheMetrics {
112    fn from(metrics: Metrics) -> Self {
113        Self {
114            font_size_bits: metrics.font_size.to_bits(),
115            line_height_bits: metrics.line_height.to_bits(),
116        }
117    }
118}
119
120impl From<CacheMetrics> for Metrics {
121    fn from(metrics: CacheMetrics) -> Self {
122        Self {
123            font_size: f32::from_bits(metrics.font_size_bits),
124            line_height: f32::from_bits(metrics.line_height_bits),
125        }
126    }
127}
128/// A 4-byte `OpenType` feature tag identifier
129#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
130pub struct FeatureTag([u8; 4]);
131
132impl FeatureTag {
133    pub const fn new(tag: &[u8; 4]) -> Self {
134        Self(*tag)
135    }
136
137    /// Kerning adjusts spacing between specific character pairs
138    pub const KERNING: Self = Self::new(b"kern");
139    /// Standard ligatures (fi, fl, etc.)
140    pub const STANDARD_LIGATURES: Self = Self::new(b"liga");
141    /// Contextual ligatures (context-dependent ligatures)
142    pub const CONTEXTUAL_LIGATURES: Self = Self::new(b"clig");
143    /// Contextual alternates (glyph substitutions based on context)
144    pub const CONTEXTUAL_ALTERNATES: Self = Self::new(b"calt");
145    /// Discretionary ligatures (optional stylistic ligatures)
146    pub const DISCRETIONARY_LIGATURES: Self = Self::new(b"dlig");
147    /// Small caps (lowercase to small capitals)
148    pub const SMALL_CAPS: Self = Self::new(b"smcp");
149    /// All small caps (uppercase and lowercase to small capitals)
150    pub const ALL_SMALL_CAPS: Self = Self::new(b"c2sc");
151    /// Stylistic Set 1 (font-specific alternate glyphs)
152    pub const STYLISTIC_SET_1: Self = Self::new(b"ss01");
153    /// Stylistic Set 2 (font-specific alternate glyphs)
154    pub const STYLISTIC_SET_2: Self = Self::new(b"ss02");
155
156    pub const fn as_bytes(&self) -> &[u8; 4] {
157        &self.0
158    }
159}
160
161#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
162pub struct Feature {
163    pub tag: FeatureTag,
164    pub value: u32,
165}
166
167#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
168pub struct FontFeatures {
169    pub features: Vec<Feature>,
170}
171
172impl FontFeatures {
173    pub const fn new() -> Self {
174        Self {
175            features: Vec::new(),
176        }
177    }
178
179    pub fn set(&mut self, tag: FeatureTag, value: u32) -> &mut Self {
180        self.features.push(Feature { tag, value });
181        self
182    }
183
184    /// Enable a feature (set to 1)
185    pub fn enable(&mut self, tag: FeatureTag) -> &mut Self {
186        self.set(tag, 1)
187    }
188
189    /// Disable a feature (set to 0)
190    pub fn disable(&mut self, tag: FeatureTag) -> &mut Self {
191        self.set(tag, 0)
192    }
193}
194
195/// A wrapper for letter spacing to get around that f32 doesn't implement Eq and Hash
196#[derive(Clone, Copy, Debug)]
197pub struct LetterSpacing(pub f32);
198
199impl PartialEq for LetterSpacing {
200    fn eq(&self, other: &Self) -> bool {
201        if self.0.is_nan() {
202            other.0.is_nan()
203        } else {
204            self.0 == other.0
205        }
206    }
207}
208
209impl Eq for LetterSpacing {}
210
211impl Hash for LetterSpacing {
212    fn hash<H: Hasher>(&self, hasher: &mut H) {
213        const CANONICAL_NAN_BITS: u32 = 0x7fc0_0000;
214
215        let bits = if self.0.is_nan() {
216            CANONICAL_NAN_BITS
217        } else {
218            // Add +0.0 to canonicalize -0.0 to +0.0
219            (self.0 + 0.0).to_bits()
220        };
221
222        bits.hash(hasher);
223    }
224}
225
226/// Text attributes
227#[derive(Clone, Debug, Eq, Hash, PartialEq)]
228pub struct Attrs<'a> {
229    //TODO: should this be an option?
230    pub color_opt: Option<Color>,
231    pub family: Family<'a>,
232    pub stretch: Stretch,
233    pub style: Style,
234    pub weight: Weight,
235    pub metadata: usize,
236    pub cache_key_flags: CacheKeyFlags,
237    pub metrics_opt: Option<CacheMetrics>,
238    /// Letter spacing (tracking) in EM
239    pub letter_spacing_opt: Option<LetterSpacing>,
240    pub font_features: FontFeatures,
241}
242
243impl<'a> Attrs<'a> {
244    /// Create a new set of attributes with sane defaults
245    ///
246    /// This defaults to a regular Sans-Serif font.
247    pub const fn new() -> Self {
248        Self {
249            color_opt: None,
250            family: Family::SansSerif,
251            stretch: Stretch::Normal,
252            style: Style::Normal,
253            weight: Weight::NORMAL,
254            metadata: 0,
255            cache_key_flags: CacheKeyFlags::empty(),
256            metrics_opt: None,
257            letter_spacing_opt: None,
258            font_features: FontFeatures::new(),
259        }
260    }
261
262    /// Set [Color]
263    pub const fn color(mut self, color: Color) -> Self {
264        self.color_opt = Some(color);
265        self
266    }
267
268    /// Set [Family]
269    pub const fn family(mut self, family: Family<'a>) -> Self {
270        self.family = family;
271        self
272    }
273
274    /// Set [Stretch]
275    pub const fn stretch(mut self, stretch: Stretch) -> Self {
276        self.stretch = stretch;
277        self
278    }
279
280    /// Set [Style]
281    pub const fn style(mut self, style: Style) -> Self {
282        self.style = style;
283        self
284    }
285
286    /// Set [Weight]
287    pub const fn weight(mut self, weight: Weight) -> Self {
288        self.weight = weight;
289        self
290    }
291
292    /// Set metadata
293    pub const fn metadata(mut self, metadata: usize) -> Self {
294        self.metadata = metadata;
295        self
296    }
297
298    /// Set [`CacheKeyFlags`]
299    pub const fn cache_key_flags(mut self, cache_key_flags: CacheKeyFlags) -> Self {
300        self.cache_key_flags = cache_key_flags;
301        self
302    }
303
304    /// Set [`Metrics`], overriding values in buffer
305    pub fn metrics(mut self, metrics: Metrics) -> Self {
306        self.metrics_opt = Some(metrics.into());
307        self
308    }
309
310    /// Set letter spacing (tracking) in EM
311    pub const fn letter_spacing(mut self, letter_spacing: f32) -> Self {
312        self.letter_spacing_opt = Some(LetterSpacing(letter_spacing));
313        self
314    }
315
316    /// Set [`FontFeatures`]
317    pub fn font_features(mut self, font_features: FontFeatures) -> Self {
318        self.font_features = font_features;
319        self
320    }
321
322    /// Check if this set of attributes can be shaped with another
323    pub fn compatible(&self, other: &Self) -> bool {
324        self.family == other.family
325            && self.stretch == other.stretch
326            && self.style == other.style
327            && self.weight == other.weight
328    }
329}
330
331/// Font-specific part of [`Attrs`] to be used for matching
332#[derive(Clone, Debug, Eq, Hash, PartialEq)]
333pub struct FontMatchAttrs {
334    family: FamilyOwned,
335    stretch: Stretch,
336    style: Style,
337    weight: Weight,
338}
339
340impl<'a> From<&Attrs<'a>> for FontMatchAttrs {
341    fn from(attrs: &Attrs<'a>) -> Self {
342        Self {
343            family: FamilyOwned::new(attrs.family),
344            stretch: attrs.stretch,
345            style: attrs.style,
346            weight: attrs.weight,
347        }
348    }
349}
350
351/// An owned version of [`Attrs`]
352#[derive(Clone, Debug, Eq, Hash, PartialEq)]
353pub struct AttrsOwned {
354    //TODO: should this be an option?
355    pub color_opt: Option<Color>,
356    pub family_owned: FamilyOwned,
357    pub stretch: Stretch,
358    pub style: Style,
359    pub weight: Weight,
360    pub metadata: usize,
361    pub cache_key_flags: CacheKeyFlags,
362    pub metrics_opt: Option<CacheMetrics>,
363    /// Letter spacing (tracking) in EM
364    pub letter_spacing_opt: Option<LetterSpacing>,
365    pub font_features: FontFeatures,
366}
367
368impl AttrsOwned {
369    pub fn new(attrs: &Attrs) -> Self {
370        Self {
371            color_opt: attrs.color_opt,
372            family_owned: FamilyOwned::new(attrs.family),
373            stretch: attrs.stretch,
374            style: attrs.style,
375            weight: attrs.weight,
376            metadata: attrs.metadata,
377            cache_key_flags: attrs.cache_key_flags,
378            metrics_opt: attrs.metrics_opt,
379            letter_spacing_opt: attrs.letter_spacing_opt,
380            font_features: attrs.font_features.clone(),
381        }
382    }
383
384    pub fn as_attrs(&self) -> Attrs<'_> {
385        Attrs {
386            color_opt: self.color_opt,
387            family: self.family_owned.as_family(),
388            stretch: self.stretch,
389            style: self.style,
390            weight: self.weight,
391            metadata: self.metadata,
392            cache_key_flags: self.cache_key_flags,
393            metrics_opt: self.metrics_opt,
394            letter_spacing_opt: self.letter_spacing_opt,
395            font_features: self.font_features.clone(),
396        }
397    }
398}
399
400/// List of text attributes to apply to a line
401//TODO: have this clean up the spans when changes are made
402#[derive(Debug, Clone, Eq, PartialEq)]
403pub struct AttrsList {
404    defaults: AttrsOwned,
405    pub(crate) spans: RangeMap<usize, AttrsOwned>,
406}
407
408impl AttrsList {
409    /// Create a new attributes list with a set of default [Attrs]
410    pub fn new(defaults: &Attrs) -> Self {
411        Self {
412            defaults: AttrsOwned::new(defaults),
413            spans: RangeMap::new(),
414        }
415    }
416
417    /// Get the default [Attrs]
418    pub fn defaults(&self) -> Attrs<'_> {
419        self.defaults.as_attrs()
420    }
421
422    /// Get the current attribute spans
423    pub fn spans(&self) -> Vec<(&Range<usize>, &AttrsOwned)> {
424        self.spans_iter().collect()
425    }
426
427    /// Get an iterator over the current attribute spans
428    pub fn spans_iter(&self) -> impl Iterator<Item = (&Range<usize>, &AttrsOwned)> + '_ {
429        self.spans.iter()
430    }
431
432    /// Clear the current attribute spans
433    pub fn clear_spans(&mut self) {
434        self.spans.clear();
435    }
436
437    /// Add an attribute span, removes any previous matching parts of spans
438    pub fn add_span(&mut self, range: Range<usize>, attrs: &Attrs) {
439        //do not support 1..1 or 2..1 even if by accident.
440        if range.is_empty() {
441            return;
442        }
443
444        self.spans.insert(range, AttrsOwned::new(attrs));
445    }
446
447    /// Get the attribute span for an index
448    ///
449    /// This returns a span that contains the index
450    pub fn get_span(&self, index: usize) -> Attrs<'_> {
451        self.spans
452            .get(&index)
453            .map(|v| v.as_attrs())
454            .unwrap_or(self.defaults.as_attrs())
455    }
456
457    /// Split attributes list at an offset
458    #[allow(clippy::missing_panics_doc)]
459    pub fn split_off(&mut self, index: usize) -> Self {
460        let mut new = Self::new(&self.defaults.as_attrs());
461        let mut removes = Vec::new();
462
463        //get the keys we need to remove or fix.
464        for span in self.spans.iter() {
465            if span.0.end <= index {
466                continue;
467            }
468
469            if span.0.start >= index {
470                removes.push((span.0.clone(), false));
471            } else {
472                removes.push((span.0.clone(), true));
473            }
474        }
475
476        for (key, resize) in removes {
477            let (range, attrs) = self
478                .spans
479                .get_key_value(&key.start)
480                .map(|v| (v.0.clone(), v.1.clone()))
481                .expect("attrs span not found");
482            self.spans.remove(key);
483
484            if resize {
485                new.spans.insert(0..range.end - index, attrs.clone());
486                self.spans.insert(range.start..index, attrs);
487            } else {
488                new.spans
489                    .insert(range.start - index..range.end - index, attrs);
490            }
491        }
492        new
493    }
494
495    /// Resets the attributes with new defaults.
496    pub(crate) fn reset(mut self, default: &Attrs) -> Self {
497        self.defaults = AttrsOwned::new(default);
498        self.spans.clear();
499        self
500    }
501}