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#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
227pub enum UnderlineStyle {
228    #[default]
229    None,
230    Single,
231    Double,
232    // TODO: Wavy
233}
234
235#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
236pub struct TextDecoration {
237    pub underline: UnderlineStyle,
238    pub underline_color_opt: Option<Color>,
239    pub strikethrough: bool,
240    pub strikethrough_color_opt: Option<Color>,
241    pub overline: bool,
242    pub overline_color_opt: Option<Color>,
243}
244
245impl TextDecoration {
246    pub const fn new() -> Self {
247        Self {
248            underline: UnderlineStyle::None,
249            underline_color_opt: None,
250            strikethrough: false,
251            strikethrough_color_opt: None,
252            overline: false,
253            overline_color_opt: None,
254        }
255    }
256
257    pub const fn has_decoration(&self) -> bool {
258        !matches!(self.underline, UnderlineStyle::None) || self.strikethrough || self.overline
259    }
260}
261
262/// Offset and thickness for a text decoration line, in EM units.
263#[derive(Clone, Copy, Debug, Default, PartialEq)]
264pub struct DecorationMetrics {
265    /// Offset from baseline in EM units
266    pub offset: f32,
267    /// Thickness in EM units
268    pub thickness: f32,
269}
270
271#[derive(Clone, Debug, PartialEq)]
272pub struct GlyphDecorationData {
273    /// The text decoration configuration from the user
274    pub text_decoration: TextDecoration,
275    /// Underline offset and thickness from the font
276    pub underline_metrics: DecorationMetrics,
277    /// Strikethrough offset and thickness from the font
278    pub strikethrough_metrics: DecorationMetrics,
279    /// Font ascent in EM units (ascent / upem).
280    /// Used for overline positioning
281    pub ascent: f32,
282}
283
284/// Text attributes
285#[derive(Clone, Debug, Eq, Hash, PartialEq)]
286pub struct Attrs<'a> {
287    //TODO: should this be an option?
288    pub color_opt: Option<Color>,
289    pub family: Family<'a>,
290    pub stretch: Stretch,
291    pub style: Style,
292    pub weight: Weight,
293    pub metadata: usize,
294    pub cache_key_flags: CacheKeyFlags,
295    pub metrics_opt: Option<CacheMetrics>,
296    /// Letter spacing (tracking) in EM
297    pub letter_spacing_opt: Option<LetterSpacing>,
298    pub font_features: FontFeatures,
299    pub text_decoration: TextDecoration,
300}
301
302impl<'a> Attrs<'a> {
303    /// Create a new set of attributes with sane defaults
304    ///
305    /// This defaults to a regular Sans-Serif font.
306    pub const fn new() -> Self {
307        Self {
308            color_opt: None,
309            family: Family::SansSerif,
310            stretch: Stretch::Normal,
311            style: Style::Normal,
312            weight: Weight::NORMAL,
313            metadata: 0,
314            cache_key_flags: CacheKeyFlags::empty(),
315            metrics_opt: None,
316            letter_spacing_opt: None,
317            font_features: FontFeatures::new(),
318            text_decoration: TextDecoration::new(),
319        }
320    }
321
322    /// Set [Color]
323    pub const fn color(mut self, color: Color) -> Self {
324        self.color_opt = Some(color);
325        self
326    }
327
328    /// Set [Family]
329    pub const fn family(mut self, family: Family<'a>) -> Self {
330        self.family = family;
331        self
332    }
333
334    /// Set [Stretch]
335    pub const fn stretch(mut self, stretch: Stretch) -> Self {
336        self.stretch = stretch;
337        self
338    }
339
340    /// Set [Style]
341    pub const fn style(mut self, style: Style) -> Self {
342        self.style = style;
343        self
344    }
345
346    /// Set [Weight]
347    pub const fn weight(mut self, weight: Weight) -> Self {
348        self.weight = weight;
349        self
350    }
351
352    /// Set metadata
353    pub const fn metadata(mut self, metadata: usize) -> Self {
354        self.metadata = metadata;
355        self
356    }
357
358    /// Set [`CacheKeyFlags`]
359    pub const fn cache_key_flags(mut self, cache_key_flags: CacheKeyFlags) -> Self {
360        self.cache_key_flags = cache_key_flags;
361        self
362    }
363
364    /// Set [`Metrics`], overriding values in buffer
365    pub fn metrics(mut self, metrics: Metrics) -> Self {
366        self.metrics_opt = Some(metrics.into());
367        self
368    }
369
370    /// Set letter spacing (tracking) in EM
371    pub const fn letter_spacing(mut self, letter_spacing: f32) -> Self {
372        self.letter_spacing_opt = Some(LetterSpacing(letter_spacing));
373        self
374    }
375
376    /// Set [`FontFeatures`]
377    pub fn font_features(mut self, font_features: FontFeatures) -> Self {
378        self.font_features = font_features;
379        self
380    }
381
382    pub const fn underline(mut self, style: UnderlineStyle) -> Self {
383        self.text_decoration.underline = style;
384        self
385    }
386
387    pub const fn underline_color(mut self, color: Color) -> Self {
388        self.text_decoration.underline_color_opt = Some(color);
389        self
390    }
391
392    pub const fn strikethrough(mut self) -> Self {
393        self.text_decoration.strikethrough = true;
394        self
395    }
396
397    pub const fn strikethrough_color(mut self, color: Color) -> Self {
398        self.text_decoration.strikethrough_color_opt = Some(color);
399        self
400    }
401
402    pub const fn overline(mut self) -> Self {
403        self.text_decoration.overline = true;
404        self
405    }
406
407    pub const fn overline_color(mut self, color: Color) -> Self {
408        self.text_decoration.overline_color_opt = Some(color);
409        self
410    }
411
412    /// Check if this set of attributes can be shaped with another
413    pub fn compatible(&self, other: &Self) -> bool {
414        self.family == other.family
415            && self.stretch == other.stretch
416            && self.style == other.style
417            && self.weight == other.weight
418    }
419}
420
421/// Font-specific part of [`Attrs`] to be used for matching
422#[derive(Clone, Debug, Eq, Hash, PartialEq)]
423pub struct FontMatchAttrs {
424    family: FamilyOwned,
425    stretch: Stretch,
426    style: Style,
427    weight: Weight,
428}
429
430impl<'a> From<&Attrs<'a>> for FontMatchAttrs {
431    fn from(attrs: &Attrs<'a>) -> Self {
432        Self {
433            family: FamilyOwned::new(attrs.family),
434            stretch: attrs.stretch,
435            style: attrs.style,
436            weight: attrs.weight,
437        }
438    }
439}
440
441/// An owned version of [`Attrs`]
442#[derive(Clone, Debug, Eq, Hash, PartialEq)]
443pub struct AttrsOwned {
444    //TODO: should this be an option?
445    pub color_opt: Option<Color>,
446    pub family_owned: FamilyOwned,
447    pub stretch: Stretch,
448    pub style: Style,
449    pub weight: Weight,
450    pub metadata: usize,
451    pub cache_key_flags: CacheKeyFlags,
452    pub metrics_opt: Option<CacheMetrics>,
453    /// Letter spacing (tracking) in EM
454    pub letter_spacing_opt: Option<LetterSpacing>,
455    pub font_features: FontFeatures,
456    pub text_decoration: TextDecoration,
457}
458
459impl AttrsOwned {
460    pub fn new(attrs: &Attrs) -> Self {
461        Self {
462            color_opt: attrs.color_opt,
463            family_owned: FamilyOwned::new(attrs.family),
464            stretch: attrs.stretch,
465            style: attrs.style,
466            weight: attrs.weight,
467            metadata: attrs.metadata,
468            cache_key_flags: attrs.cache_key_flags,
469            metrics_opt: attrs.metrics_opt,
470            letter_spacing_opt: attrs.letter_spacing_opt,
471            font_features: attrs.font_features.clone(),
472            text_decoration: attrs.text_decoration,
473        }
474    }
475
476    pub fn as_attrs(&self) -> Attrs<'_> {
477        Attrs {
478            color_opt: self.color_opt,
479            family: self.family_owned.as_family(),
480            stretch: self.stretch,
481            style: self.style,
482            weight: self.weight,
483            metadata: self.metadata,
484            cache_key_flags: self.cache_key_flags,
485            metrics_opt: self.metrics_opt,
486            letter_spacing_opt: self.letter_spacing_opt,
487            font_features: self.font_features.clone(),
488            text_decoration: self.text_decoration,
489        }
490    }
491}
492
493/// List of text attributes to apply to a line
494//TODO: have this clean up the spans when changes are made
495#[derive(Debug, Clone, Eq, PartialEq)]
496pub struct AttrsList {
497    defaults: AttrsOwned,
498    pub(crate) spans: RangeMap<usize, AttrsOwned>,
499}
500
501impl AttrsList {
502    /// Create a new attributes list with a set of default [Attrs]
503    pub fn new(defaults: &Attrs) -> Self {
504        Self {
505            defaults: AttrsOwned::new(defaults),
506            spans: RangeMap::new(),
507        }
508    }
509
510    /// Get the default [Attrs]
511    pub fn defaults(&self) -> Attrs<'_> {
512        self.defaults.as_attrs()
513    }
514
515    /// Get the current attribute spans
516    pub fn spans(&self) -> Vec<(&Range<usize>, &AttrsOwned)> {
517        self.spans_iter().collect()
518    }
519
520    /// Get an iterator over the current attribute spans
521    pub fn spans_iter(&self) -> impl Iterator<Item = (&Range<usize>, &AttrsOwned)> + '_ {
522        self.spans.iter()
523    }
524
525    /// Clear the current attribute spans
526    pub fn clear_spans(&mut self) {
527        self.spans.clear();
528    }
529
530    /// Add an attribute span, removes any previous matching parts of spans
531    pub fn add_span(&mut self, range: Range<usize>, attrs: &Attrs) {
532        //do not support 1..1 or 2..1 even if by accident.
533        if range.is_empty() {
534            return;
535        }
536
537        self.spans.insert(range, AttrsOwned::new(attrs));
538    }
539
540    /// Get the attribute span for an index
541    ///
542    /// This returns a span that contains the index
543    pub fn get_span(&self, index: usize) -> Attrs<'_> {
544        self.spans
545            .get(&index)
546            .map(|v| v.as_attrs())
547            .unwrap_or(self.defaults.as_attrs())
548    }
549
550    /// Split attributes list at an offset
551    #[allow(clippy::missing_panics_doc)]
552    pub fn split_off(&mut self, index: usize) -> Self {
553        let mut new = Self::new(&self.defaults.as_attrs());
554        let mut removes = Vec::new();
555
556        //get the keys we need to remove or fix.
557        for span in self.spans.iter() {
558            if span.0.end <= index {
559                continue;
560            }
561
562            if span.0.start >= index {
563                removes.push((span.0.clone(), false));
564            } else {
565                removes.push((span.0.clone(), true));
566            }
567        }
568
569        for (key, resize) in removes {
570            let (range, attrs) = self
571                .spans
572                .get_key_value(&key.start)
573                .map(|v| (v.0.clone(), v.1.clone()))
574                .expect("attrs span not found");
575            self.spans.remove(key);
576
577            if resize {
578                new.spans.insert(0..range.end - index, attrs.clone());
579                self.spans.insert(range.start..index, attrs);
580            } else {
581                new.spans
582                    .insert(range.start - index..range.end - index, attrs);
583            }
584        }
585        new
586    }
587
588    /// Resets the attributes with new defaults.
589    pub(crate) fn reset(mut self, default: &Attrs) -> Self {
590        self.defaults = AttrsOwned::new(default);
591        self.spans.clear();
592        self
593    }
594}