1use crate::{
2 color::Color,
3 font_cache::FamilyId,
4 json::{json, ToJson},
5 text_layout::RunStyle,
6 FontCache,
7};
8use anyhow::anyhow;
9pub use font_kit::{
10 metrics::Metrics,
11 properties::{Properties, Stretch, Style, Weight},
12};
13use serde::{de, Deserialize};
14use serde_json::Value;
15use std::{cell::RefCell, sync::Arc};
16
17#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
18pub struct FontId(pub usize);
19
20pub type GlyphId = u32;
21
22#[derive(Clone, Debug)]
23pub struct TextStyle {
24 pub color: Color,
25 pub font_family_name: Arc<str>,
26 pub font_family_id: FamilyId,
27 pub font_id: FontId,
28 pub font_size: f32,
29 pub font_properties: Properties,
30 pub underline: bool,
31}
32
33#[derive(Clone, Debug, Default)]
34pub struct HighlightStyle {
35 pub color: Color,
36 pub font_properties: Properties,
37 pub underline: bool,
38}
39
40#[allow(non_camel_case_types)]
41#[derive(Deserialize)]
42enum WeightJson {
43 thin,
44 extra_light,
45 light,
46 normal,
47 medium,
48 semibold,
49 bold,
50 extra_bold,
51 black,
52}
53
54thread_local! {
55 static FONT_CACHE: RefCell<Option<Arc<FontCache>>> = Default::default();
56}
57
58#[derive(Deserialize)]
59struct TextStyleJson {
60 color: Color,
61 family: String,
62 weight: Option<WeightJson>,
63 size: f32,
64 #[serde(default)]
65 italic: bool,
66 #[serde(default)]
67 underline: bool,
68}
69
70#[derive(Deserialize)]
71struct HighlightStyleJson {
72 color: Color,
73 weight: Option<WeightJson>,
74 #[serde(default)]
75 italic: bool,
76 #[serde(default)]
77 underline: bool,
78}
79
80impl TextStyle {
81 pub fn new(
82 font_family_name: impl Into<Arc<str>>,
83 font_size: f32,
84 font_properties: Properties,
85 underline: bool,
86 color: Color,
87 font_cache: &FontCache,
88 ) -> anyhow::Result<Self> {
89 let font_family_name = font_family_name.into();
90 let font_family_id = font_cache.load_family(&[&font_family_name])?;
91 let font_id = font_cache.select_font(font_family_id, &font_properties)?;
92 Ok(Self {
93 color,
94 font_family_name,
95 font_family_id,
96 font_id,
97 font_size,
98 font_properties,
99 underline,
100 })
101 }
102
103 pub fn to_run(&self) -> RunStyle {
104 RunStyle {
105 font_id: self.font_id,
106 color: self.color,
107 underline: self.underline,
108 }
109 }
110
111 fn from_json(json: TextStyleJson) -> anyhow::Result<Self> {
112 FONT_CACHE.with(|font_cache| {
113 if let Some(font_cache) = font_cache.borrow().as_ref() {
114 let font_properties = properties_from_json(json.weight, json.italic);
115 Self::new(
116 json.family,
117 json.size,
118 font_properties,
119 json.underline,
120 json.color,
121 font_cache,
122 )
123 } else {
124 Err(anyhow!(
125 "TextStyle can only be deserialized within a call to with_font_cache"
126 ))
127 }
128 })
129 }
130
131 pub fn line_height(&self, font_cache: &FontCache) -> f32 {
132 font_cache.line_height(self.font_id, self.font_size)
133 }
134
135 pub fn cap_height(&self, font_cache: &FontCache) -> f32 {
136 font_cache.cap_height(self.font_id, self.font_size)
137 }
138
139 pub fn x_height(&self, font_cache: &FontCache) -> f32 {
140 font_cache.x_height(self.font_id, self.font_size)
141 }
142
143 pub fn em_width(&self, font_cache: &FontCache) -> f32 {
144 font_cache.em_width(self.font_id, self.font_size)
145 }
146
147 pub fn descent(&self, font_cache: &FontCache) -> f32 {
148 font_cache.metric(self.font_id, |m| m.descent) * self.em_scale(font_cache)
149 }
150
151 pub fn baseline_offset(&self, font_cache: &FontCache) -> f32 {
152 font_cache.baseline_offset(self.font_id, self.font_size)
153 }
154
155 fn em_scale(&self, font_cache: &FontCache) -> f32 {
156 font_cache.em_scale(self.font_id, self.font_size)
157 }
158}
159
160impl From<TextStyle> for HighlightStyle {
161 fn from(other: TextStyle) -> Self {
162 Self {
163 color: other.color,
164 font_properties: other.font_properties,
165 underline: other.underline,
166 }
167 }
168}
169
170impl HighlightStyle {
171 fn from_json(json: HighlightStyleJson) -> Self {
172 let font_properties = properties_from_json(json.weight, json.italic);
173 Self {
174 color: json.color,
175 font_properties,
176 underline: json.underline,
177 }
178 }
179}
180
181impl From<Color> for HighlightStyle {
182 fn from(color: Color) -> Self {
183 Self {
184 color,
185 font_properties: Default::default(),
186 underline: false,
187 }
188 }
189}
190
191impl<'de> Deserialize<'de> for TextStyle {
192 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
193 where
194 D: serde::Deserializer<'de>,
195 {
196 Ok(Self::from_json(TextStyleJson::deserialize(deserializer)?)
197 .map_err(|e| de::Error::custom(e))?)
198 }
199}
200
201impl ToJson for TextStyle {
202 fn to_json(&self) -> Value {
203 json!({
204 "color": self.color.to_json(),
205 "font_family": self.font_family_name.as_ref(),
206 "font_properties": self.font_properties.to_json(),
207 })
208 }
209}
210
211impl<'de> Deserialize<'de> for HighlightStyle {
212 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
213 where
214 D: serde::Deserializer<'de>,
215 {
216 let json = serde_json::Value::deserialize(deserializer)?;
217 if json.is_object() {
218 Ok(Self::from_json(
219 serde_json::from_value(json).map_err(de::Error::custom)?,
220 ))
221 } else {
222 Ok(Self {
223 color: serde_json::from_value(json).map_err(de::Error::custom)?,
224 font_properties: Properties::new(),
225 underline: false,
226 })
227 }
228 }
229}
230
231fn properties_from_json(weight: Option<WeightJson>, italic: bool) -> Properties {
232 let weight = match weight.unwrap_or(WeightJson::normal) {
233 WeightJson::thin => Weight::THIN,
234 WeightJson::extra_light => Weight::EXTRA_LIGHT,
235 WeightJson::light => Weight::LIGHT,
236 WeightJson::normal => Weight::NORMAL,
237 WeightJson::medium => Weight::MEDIUM,
238 WeightJson::semibold => Weight::SEMIBOLD,
239 WeightJson::bold => Weight::BOLD,
240 WeightJson::extra_bold => Weight::EXTRA_BOLD,
241 WeightJson::black => Weight::BLACK,
242 };
243 let style = if italic { Style::Italic } else { Style::Normal };
244 *Properties::new().weight(weight).style(style)
245}
246
247impl ToJson for Properties {
248 fn to_json(&self) -> crate::json::Value {
249 json!({
250 "style": self.style.to_json(),
251 "weight": self.weight.to_json(),
252 "stretch": self.stretch.to_json(),
253 })
254 }
255}
256
257impl ToJson for Style {
258 fn to_json(&self) -> crate::json::Value {
259 match self {
260 Style::Normal => json!("normal"),
261 Style::Italic => json!("italic"),
262 Style::Oblique => json!("oblique"),
263 }
264 }
265}
266
267impl ToJson for Weight {
268 fn to_json(&self) -> crate::json::Value {
269 if self.0 == Weight::THIN.0 {
270 json!("thin")
271 } else if self.0 == Weight::EXTRA_LIGHT.0 {
272 json!("extra light")
273 } else if self.0 == Weight::LIGHT.0 {
274 json!("light")
275 } else if self.0 == Weight::NORMAL.0 {
276 json!("normal")
277 } else if self.0 == Weight::MEDIUM.0 {
278 json!("medium")
279 } else if self.0 == Weight::SEMIBOLD.0 {
280 json!("semibold")
281 } else if self.0 == Weight::BOLD.0 {
282 json!("bold")
283 } else if self.0 == Weight::EXTRA_BOLD.0 {
284 json!("extra bold")
285 } else if self.0 == Weight::BLACK.0 {
286 json!("black")
287 } else {
288 json!(self.0)
289 }
290 }
291}
292
293impl ToJson for Stretch {
294 fn to_json(&self) -> serde_json::Value {
295 json!(self.0)
296 }
297}
298
299pub fn with_font_cache<F, T>(font_cache: Arc<FontCache>, callback: F) -> T
300where
301 F: FnOnce() -> T,
302{
303 FONT_CACHE.with(|cache| {
304 *cache.borrow_mut() = Some(font_cache);
305 let result = callback();
306 cache.borrow_mut().take();
307 result
308 })
309}