1use eternal_styler::{Color, ComputedStyle, Corners, FontFamily, FontStyle, FontWeight, Sides};
13
14use crate::geometry::{Point, Rect};
15
16#[derive(Clone, Debug, PartialEq)]
18pub struct FontSpec {
19 pub family: FontFamily,
20 pub size: f32,
22 pub weight: FontWeight,
23 pub style: FontStyle,
24 pub line_height: Option<f32>,
26}
27
28impl FontSpec {
29 pub fn from_style(style: &ComputedStyle) -> Self {
30 Self {
31 family: style.font_family.clone(),
32 size: style.font_size,
33 weight: style.font_weight,
34 style: style.font_style,
35 line_height: style.line_height.resolve(style.font_size),
36 }
37 }
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Default)]
42pub struct TextMetrics {
43 pub width: f32,
45 pub ascent: f32,
47 pub descent: f32,
49 pub line_height: f32,
52}
53
54pub trait Fonts {
56 fn measure(&self, text: &str, font: &FontSpec) -> TextMetrics;
57}
58
59pub trait Renderer {
66 fn fill_rect(&mut self, rect: Rect, radius: Corners<f32>, color: Color);
67
68 fn border(
70 &mut self,
71 rect: Rect,
72 widths: Sides<f32>,
73 colors: Sides<Color>,
74 radius: Corners<f32>,
75 );
76
77 fn text(&mut self, text: &str, font: &FontSpec, top_left: Point, color: Color);
81
82 fn push_clip(&mut self, rect: Rect);
84
85 fn pop_clip(&mut self);
86
87 fn push_opacity(&mut self, opacity: f32);
90
91 fn pop_opacity(&mut self);
92}
93
94#[derive(Clone, Debug)]
100pub struct MonospaceFonts {
101 pub advance: f32,
102}
103
104impl Default for MonospaceFonts {
105 fn default() -> Self {
106 Self { advance: 0.6 }
107 }
108}
109
110impl Fonts for MonospaceFonts {
111 fn measure(&self, text: &str, font: &FontSpec) -> TextMetrics {
112 let ascent = font.size * 1.0;
113 let descent = font.size * 0.25;
114 TextMetrics {
115 width: text.chars().count() as f32 * self.advance * font.size,
116 ascent,
117 descent,
118 line_height: font.line_height.unwrap_or(ascent + descent),
119 }
120 }
121}
122
123#[derive(Clone, Debug, PartialEq)]
125pub enum DrawOp {
126 FillRect {
127 rect: Rect,
128 radius: Corners<f32>,
129 color: Color,
130 },
131 Border {
132 rect: Rect,
133 widths: Sides<f32>,
134 colors: Sides<Color>,
135 radius: Corners<f32>,
136 },
137 Text {
138 text: String,
139 font: FontSpec,
140 top_left: Point,
141 color: Color,
142 },
143 PushClip(Rect),
144 PopClip,
145 PushOpacity(f32),
146 PopOpacity,
147}
148
149#[derive(Clone, Debug, Default)]
152pub struct RecordingRenderer {
153 pub ops: Vec<DrawOp>,
154}
155
156impl Renderer for RecordingRenderer {
157 fn fill_rect(&mut self, rect: Rect, radius: Corners<f32>, color: Color) {
158 self.ops.push(DrawOp::FillRect {
159 rect,
160 radius,
161 color,
162 });
163 }
164
165 fn border(
166 &mut self,
167 rect: Rect,
168 widths: Sides<f32>,
169 colors: Sides<Color>,
170 radius: Corners<f32>,
171 ) {
172 self.ops.push(DrawOp::Border {
173 rect,
174 widths,
175 colors,
176 radius,
177 });
178 }
179
180 fn text(&mut self, text: &str, font: &FontSpec, top_left: Point, color: Color) {
181 self.ops.push(DrawOp::Text {
182 text: text.to_owned(),
183 font: font.clone(),
184 top_left,
185 color,
186 });
187 }
188
189 fn push_clip(&mut self, rect: Rect) {
190 self.ops.push(DrawOp::PushClip(rect));
191 }
192
193 fn pop_clip(&mut self) {
194 self.ops.push(DrawOp::PopClip);
195 }
196
197 fn push_opacity(&mut self, opacity: f32) {
198 self.ops.push(DrawOp::PushOpacity(opacity));
199 }
200
201 fn pop_opacity(&mut self) {
202 self.ops.push(DrawOp::PopOpacity);
203 }
204}