1pub mod cache;
3pub mod editor;
4pub mod paragraph;
5
6pub use cache::Cache;
7pub use editor::Editor;
8pub use paragraph::Paragraph;
9
10pub use cosmic_text;
11
12use crate::core::alignment;
13use crate::core::font::{self, Font};
14use crate::core::text::{Alignment, Ellipsis, Shaping, Wrapping};
15use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
16
17use std::borrow::Cow;
18use std::collections::HashSet;
19use std::sync::{Arc, OnceLock, RwLock, Weak};
20
21#[derive(Debug, Clone, PartialEq)]
23pub enum Text {
24 #[allow(missing_docs)]
26 Paragraph {
27 paragraph: paragraph::Weak,
28 position: Point,
29 color: Color,
30 clip_bounds: Rectangle,
31 transformation: Transformation,
32 },
33 #[allow(missing_docs)]
35 Editor {
36 editor: editor::Weak,
37 position: Point,
38 color: Color,
39 clip_bounds: Rectangle,
40 transformation: Transformation,
41 },
42 Cached {
44 content: String,
46 bounds: Rectangle,
48 color: Color,
50 size: Pixels,
52 line_height: Pixels,
54 font: Font,
56 align_x: Alignment,
58 align_y: alignment::Vertical,
60 shaping: Shaping,
62 wrapping: Wrapping,
64 ellipsis: Ellipsis,
66 clip_bounds: Rectangle,
68 },
69 #[allow(missing_docs)]
71 Raw {
72 raw: Raw,
73 transformation: Transformation,
74 },
75}
76
77impl Text {
78 pub fn visible_bounds(&self) -> Option<Rectangle> {
80 match self {
81 Text::Paragraph {
82 position,
83 paragraph,
84 clip_bounds,
85 transformation,
86 ..
87 } => Rectangle::new(*position, paragraph.min_bounds)
88 .intersection(clip_bounds)
89 .map(|bounds| bounds * *transformation),
90 Text::Editor {
91 editor,
92 position,
93 clip_bounds,
94 transformation,
95 ..
96 } => Rectangle::new(*position, editor.bounds)
97 .intersection(clip_bounds)
98 .map(|bounds| bounds * *transformation),
99 Text::Cached {
100 bounds,
101 clip_bounds,
102 ..
103 } => bounds.intersection(clip_bounds),
104 Text::Raw { raw, .. } => Some(raw.clip_bounds),
105 }
106 }
107}
108
109#[cfg(feature = "fira-sans")]
116pub const FIRA_SANS_REGULAR: &[u8] = include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice();
117
118pub fn font_system() -> &'static RwLock<FontSystem> {
120 static FONT_SYSTEM: OnceLock<RwLock<FontSystem>> = OnceLock::new();
121
122 FONT_SYSTEM.get_or_init(|| {
123 #[allow(unused_mut)]
124 let mut raw = cosmic_text::FontSystem::new_with_fonts([
125 cosmic_text::fontdb::Source::Binary(Arc::new(
126 include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
127 )),
128 #[cfg(feature = "fira-sans")]
129 cosmic_text::fontdb::Source::Binary(Arc::new(
130 include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
131 )),
132 ]);
133
134 #[cfg(feature = "fira-sans")]
135 raw.db_mut().set_sans_serif_family("Fira Sans");
136
137 RwLock::new(FontSystem {
138 raw,
139 loaded_fonts: HashSet::new(),
140 version: Version::default(),
141 })
142 })
143}
144
145pub struct FontSystem {
147 raw: cosmic_text::FontSystem,
148 loaded_fonts: HashSet<usize>,
149 version: Version,
150}
151
152impl FontSystem {
153 pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
155 &mut self.raw
156 }
157
158 pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
160 if let Cow::Borrowed(bytes) = bytes {
161 let address = bytes.as_ptr() as usize;
162
163 if !self.loaded_fonts.insert(address) {
164 return;
165 }
166 }
167
168 let _ = self
169 .raw
170 .db_mut()
171 .load_font_source(cosmic_text::fontdb::Source::Binary(Arc::new(
172 bytes.into_owned(),
173 )));
174
175 self.version = Version(self.version.0 + 1);
176 }
177
178 pub fn families(&self) -> impl Iterator<Item = &str> {
181 self.raw
182 .db()
183 .faces()
184 .filter_map(|face| face.families.first())
185 .map(|(name, _)| name.as_str())
186 }
187
188 pub fn version(&self) -> Version {
192 self.version
193 }
194}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
198pub struct Version(u32);
199
200#[derive(Debug, Clone)]
202pub struct Raw {
203 pub buffer: Weak<cosmic_text::Buffer>,
205 pub position: Point,
207 pub color: Color,
209 pub clip_bounds: Rectangle,
211}
212
213impl PartialEq for Raw {
214 fn eq(&self, _other: &Self) -> bool {
215 false
220 }
221}
222
223pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
225 let (width, height, has_rtl) =
226 buffer
227 .layout_runs()
228 .fold((0.0, 0.0, false), |(width, height, has_rtl), run| {
229 (
230 run.line_w.max(width),
231 height + run.line_height,
232 has_rtl || run.rtl,
233 )
234 });
235
236 (Size::new(width, height), has_rtl)
237}
238
239pub fn align(
242 buffer: &mut cosmic_text::Buffer,
243 font_system: &mut cosmic_text::FontSystem,
244 alignment: Alignment,
245) -> Size {
246 let (min_bounds, has_rtl) = measure(buffer);
247 let mut needs_relayout = has_rtl;
248
249 if let Some(align) = to_align(alignment) {
250 let has_multiple_lines = buffer.lines.len() > 1
251 || buffer
252 .lines
253 .first()
254 .is_some_and(|line| line.layout_opt().is_some_and(|layout| layout.len() > 1));
255
256 if has_multiple_lines {
257 for line in &mut buffer.lines {
258 let _ = line.set_align(Some(align));
259 }
260
261 needs_relayout = true;
262 } else if let Some(line) = buffer.lines.first_mut() {
263 needs_relayout = line.set_align(None);
264 }
265 }
266
267 if needs_relayout {
269 log::trace!("Relayouting paragraph...");
270
271 buffer.set_size(font_system, Some(min_bounds.width), Some(min_bounds.height));
272 }
273
274 min_bounds
275}
276
277pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
279 cosmic_text::Attrs::new()
280 .family(to_family(font.family))
281 .weight(to_weight(font.weight))
282 .stretch(to_stretch(font.stretch))
283 .style(to_style(font.style))
284}
285
286fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
287 match family {
288 font::Family::Name(name) => cosmic_text::Family::Name(name),
289 font::Family::SansSerif => cosmic_text::Family::SansSerif,
290 font::Family::Serif => cosmic_text::Family::Serif,
291 font::Family::Cursive => cosmic_text::Family::Cursive,
292 font::Family::Fantasy => cosmic_text::Family::Fantasy,
293 font::Family::Monospace => cosmic_text::Family::Monospace,
294 }
295}
296
297fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
298 match weight {
299 font::Weight::Thin => cosmic_text::Weight::THIN,
300 font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
301 font::Weight::Light => cosmic_text::Weight::LIGHT,
302 font::Weight::Normal => cosmic_text::Weight::NORMAL,
303 font::Weight::Medium => cosmic_text::Weight::MEDIUM,
304 font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
305 font::Weight::Bold => cosmic_text::Weight::BOLD,
306 font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
307 font::Weight::Black => cosmic_text::Weight::BLACK,
308 }
309}
310
311fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
312 match stretch {
313 font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
314 font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
315 font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
316 font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
317 font::Stretch::Normal => cosmic_text::Stretch::Normal,
318 font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
319 font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
320 font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
321 font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
322 }
323}
324
325fn to_style(style: font::Style) -> cosmic_text::Style {
326 match style {
327 font::Style::Normal => cosmic_text::Style::Normal,
328 font::Style::Italic => cosmic_text::Style::Italic,
329 font::Style::Oblique => cosmic_text::Style::Oblique,
330 }
331}
332
333fn to_align(alignment: Alignment) -> Option<cosmic_text::Align> {
334 match alignment {
335 Alignment::Default => None,
336 Alignment::Left => Some(cosmic_text::Align::Left),
337 Alignment::Center => Some(cosmic_text::Align::Center),
338 Alignment::Right => Some(cosmic_text::Align::Right),
339 Alignment::Justified => Some(cosmic_text::Align::Justified),
340 }
341}
342
343pub fn to_shaping(shaping: Shaping, text: &str) -> cosmic_text::Shaping {
345 match shaping {
346 Shaping::Auto => {
347 if text.is_ascii() {
348 cosmic_text::Shaping::Basic
349 } else {
350 cosmic_text::Shaping::Advanced
351 }
352 }
353 Shaping::Basic => cosmic_text::Shaping::Basic,
354 Shaping::Advanced => cosmic_text::Shaping::Advanced,
355 }
356}
357
358pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
360 match wrapping {
361 Wrapping::None => cosmic_text::Wrap::None,
362 Wrapping::Word => cosmic_text::Wrap::Word,
363 Wrapping::Glyph => cosmic_text::Wrap::Glyph,
364 Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
365 }
366}
367
368pub fn to_ellipsize(ellipsis: Ellipsis, max_height: f32) -> cosmic_text::Ellipsize {
370 let limit = cosmic_text::EllipsizeHeightLimit::Height(max_height);
371
372 match ellipsis {
373 Ellipsis::None => cosmic_text::Ellipsize::None,
374 Ellipsis::Start => cosmic_text::Ellipsize::Start(limit),
375 Ellipsis::Middle => cosmic_text::Ellipsize::Middle(limit),
376 Ellipsis::End => cosmic_text::Ellipsize::End(limit),
377 }
378}
379
380pub fn to_color(color: Color) -> cosmic_text::Color {
382 let [r, g, b, a] = color.into_rgba8();
383
384 cosmic_text::Color::rgba(r, g, b, a)
385}
386
387pub fn hint_factor(_size: Pixels, _scale_factor: Option<f32>) -> Option<f32> {
389 None }
402
403pub trait Renderer {
405 fn fill_raw(&mut self, raw: Raw);
407}