1#[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#[derive(Clone, Copy, Debug, PartialOrd, Ord, Eq, Hash, PartialEq)]
16pub struct Color(pub u32);
17
18impl Color {
19 #[inline]
21 pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
22 Self::rgba(r, g, b, 0xFF)
23 }
24
25 #[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 #[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 #[inline]
39 pub const fn as_rgba(self) -> [u8; 4] {
40 [self.r(), self.g(), self.b(), self.a()]
41 }
42
43 #[inline]
45 pub const fn r(&self) -> u8 {
46 ((self.0 & 0x00_FF_00_00) >> 16) as u8
47 }
48
49 #[inline]
51 pub const fn g(&self) -> u8 {
52 ((self.0 & 0x00_00_FF_00) >> 8) as u8
53 }
54
55 #[inline]
57 pub const fn b(&self) -> u8 {
58 (self.0 & 0x00_00_00_FF) as u8
59 }
60
61 #[inline]
63 pub const fn a(&self) -> u8 {
64 ((self.0 & 0xFF_00_00_00) >> 24) as u8
65 }
66}
67
68#[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#[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#[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 pub const KERNING: Self = Self::new(b"kern");
139 pub const STANDARD_LIGATURES: Self = Self::new(b"liga");
141 pub const CONTEXTUAL_LIGATURES: Self = Self::new(b"clig");
143 pub const CONTEXTUAL_ALTERNATES: Self = Self::new(b"calt");
145 pub const DISCRETIONARY_LIGATURES: Self = Self::new(b"dlig");
147 pub const SMALL_CAPS: Self = Self::new(b"smcp");
149 pub const ALL_SMALL_CAPS: Self = Self::new(b"c2sc");
151 pub const STYLISTIC_SET_1: Self = Self::new(b"ss01");
153 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 pub fn enable(&mut self, tag: FeatureTag) -> &mut Self {
186 self.set(tag, 1)
187 }
188
189 pub fn disable(&mut self, tag: FeatureTag) -> &mut Self {
191 self.set(tag, 0)
192 }
193}
194
195#[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 (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 }
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#[derive(Clone, Copy, Debug, Default, PartialEq)]
264pub struct DecorationMetrics {
265 pub offset: f32,
267 pub thickness: f32,
269}
270
271#[derive(Clone, Debug, PartialEq)]
272pub struct GlyphDecorationData {
273 pub text_decoration: TextDecoration,
275 pub underline_metrics: DecorationMetrics,
277 pub strikethrough_metrics: DecorationMetrics,
279 pub ascent: f32,
282}
283
284#[derive(Clone, Debug, Eq, Hash, PartialEq)]
286pub struct Attrs<'a> {
287 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 pub letter_spacing_opt: Option<LetterSpacing>,
298 pub font_features: FontFeatures,
299 pub text_decoration: TextDecoration,
300}
301
302impl<'a> Attrs<'a> {
303 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 pub const fn color(mut self, color: Color) -> Self {
324 self.color_opt = Some(color);
325 self
326 }
327
328 pub const fn family(mut self, family: Family<'a>) -> Self {
330 self.family = family;
331 self
332 }
333
334 pub const fn stretch(mut self, stretch: Stretch) -> Self {
336 self.stretch = stretch;
337 self
338 }
339
340 pub const fn style(mut self, style: Style) -> Self {
342 self.style = style;
343 self
344 }
345
346 pub const fn weight(mut self, weight: Weight) -> Self {
348 self.weight = weight;
349 self
350 }
351
352 pub const fn metadata(mut self, metadata: usize) -> Self {
354 self.metadata = metadata;
355 self
356 }
357
358 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 pub fn metrics(mut self, metrics: Metrics) -> Self {
366 self.metrics_opt = Some(metrics.into());
367 self
368 }
369
370 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 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 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#[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#[derive(Clone, Debug, Eq, Hash, PartialEq)]
443pub struct AttrsOwned {
444 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 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#[derive(Debug, Clone, Eq, PartialEq)]
496pub struct AttrsList {
497 defaults: AttrsOwned,
498 pub(crate) spans: RangeMap<usize, AttrsOwned>,
499}
500
501impl AttrsList {
502 pub fn new(defaults: &Attrs) -> Self {
504 Self {
505 defaults: AttrsOwned::new(defaults),
506 spans: RangeMap::new(),
507 }
508 }
509
510 pub fn defaults(&self) -> Attrs<'_> {
512 self.defaults.as_attrs()
513 }
514
515 pub fn spans(&self) -> Vec<(&Range<usize>, &AttrsOwned)> {
517 self.spans_iter().collect()
518 }
519
520 pub fn spans_iter(&self) -> impl Iterator<Item = (&Range<usize>, &AttrsOwned)> + '_ {
522 self.spans.iter()
523 }
524
525 pub fn clear_spans(&mut self) {
527 self.spans.clear();
528 }
529
530 pub fn add_span(&mut self, range: Range<usize>, attrs: &Attrs) {
532 if range.is_empty() {
534 return;
535 }
536
537 self.spans.insert(range, AttrsOwned::new(attrs));
538 }
539
540 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 #[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 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 pub(crate) fn reset(mut self, default: &Attrs) -> Self {
590 self.defaults = AttrsOwned::new(default);
591 self.spans.clear();
592 self
593 }
594}