1use std::ops::Range;
2
3use crate::core::{TextAlign, UiRect};
4
5#[derive(Clone, Copy, Debug)]
6pub struct TextMeasureRequest<'a> {
7 pub text: &'a str,
8 pub bounds: UiRect,
9 pub font_height: f32,
10 pub font_weight: i32,
11}
12
13#[derive(Clone, Copy, Debug, Default, PartialEq)]
14pub struct TextMetrics {
15 pub width: f32,
16}
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
19pub enum TextDirection {
20 #[default]
21 Auto,
22 LeftToRight,
23 RightToLeft,
24}
25
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
27pub enum TextAffinity {
28 Upstream,
29 #[default]
30 Downstream,
31}
32
33#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
34pub enum TextFontSlant {
35 #[default]
36 Upright,
37 Italic,
38 Oblique,
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
42pub struct TextFontWidth(pub i32);
43
44impl Default for TextFontWidth {
45 fn default() -> Self {
46 Self(5)
47 }
48}
49
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
51pub enum TextVerticalAlign {
52 #[default]
53 Top,
54 Center,
55 Bottom,
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
59pub struct TextFeature<'a> {
60 pub name: &'a str,
61 pub value: i32,
62}
63
64#[derive(Clone, Debug, Default)]
65pub struct TextSpan<'a> {
66 pub range: Range<usize>,
67 pub font_families: &'a [&'a str],
68 pub font_height: Option<f32>,
69 pub font_weight: Option<i32>,
70 pub font_width: Option<TextFontWidth>,
71 pub font_slant: Option<TextFontSlant>,
72 pub locale: Option<&'a str>,
73 pub tracking: Option<f32>,
74 pub features: &'a [TextFeature<'a>],
75}
76
77#[derive(Clone, Debug)]
78pub struct TextLayoutRequest<'a> {
79 pub text: &'a str,
80 pub bounds: UiRect,
81 pub font_height: f32,
82 pub font_weight: i32,
83 pub font_width: TextFontWidth,
84 pub font_slant: TextFontSlant,
85 pub font_families: &'a [&'a str],
86 pub locale: &'a str,
87 pub tracking: f32,
88 pub align: TextAlign,
89 pub direction: TextDirection,
90 pub max_lines: Option<usize>,
91 pub line_height: Option<f32>,
92 pub vertical_align: TextVerticalAlign,
93 pub features: &'a [TextFeature<'a>],
94 pub spans: &'a [TextSpan<'a>],
95}
96
97impl<'a> TextLayoutRequest<'a> {
98 pub fn single_line(text: &'a str, bounds: UiRect, font_height: f32, font_weight: i32) -> Self {
99 Self {
100 text,
101 bounds,
102 font_height,
103 font_weight,
104 font_width: TextFontWidth::default(),
105 font_slant: TextFontSlant::Upright,
106 font_families: &[],
107 locale: "",
108 tracking: 0.0,
109 align: TextAlign::Left,
110 direction: TextDirection::Auto,
111 max_lines: Some(1),
112 line_height: None,
113 vertical_align: TextVerticalAlign::Center,
114 features: &[],
115 spans: &[],
116 }
117 }
118}
119
120#[derive(Clone, Debug, Default, PartialEq)]
121pub struct TextLineMetrics {
122 pub range: Range<usize>,
123 pub bounds: UiRect,
124 pub baseline: f32,
125 pub hard_break: bool,
126}
127
128#[derive(Clone, Debug, Default, PartialEq)]
129pub struct TextCluster {
130 pub range: Range<usize>,
131 pub bounds: UiRect,
132 pub direction: TextDirection,
133}
134
135#[derive(Clone, Copy, Debug, Default, PartialEq)]
136pub struct TextHit {
137 pub index: usize,
138 pub affinity: TextAffinity,
139 pub inside: bool,
140}