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 RwLock::new(FontSystem {
124 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 loaded_fonts: HashSet::new(),
134 version: Version::default(),
135 })
136 })
137}
138
139pub struct FontSystem {
141 raw: cosmic_text::FontSystem,
142 loaded_fonts: HashSet<usize>,
143 version: Version,
144}
145
146impl FontSystem {
147 pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
149 &mut self.raw
150 }
151
152 pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
154 if let Cow::Borrowed(bytes) = bytes {
155 let address = bytes.as_ptr() as usize;
156
157 if !self.loaded_fonts.insert(address) {
158 return;
159 }
160 }
161
162 let _ = self
163 .raw
164 .db_mut()
165 .load_font_source(cosmic_text::fontdb::Source::Binary(Arc::new(
166 bytes.into_owned(),
167 )));
168
169 self.version = Version(self.version.0 + 1);
170 }
171
172 pub fn version(&self) -> Version {
176 self.version
177 }
178}
179
180#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
182pub struct Version(u32);
183
184#[derive(Debug, Clone)]
186pub struct Raw {
187 pub buffer: Weak<cosmic_text::Buffer>,
189 pub position: Point,
191 pub color: Color,
193 pub clip_bounds: Rectangle,
195}
196
197impl PartialEq for Raw {
198 fn eq(&self, _other: &Self) -> bool {
199 false
204 }
205}
206
207pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
209 let (width, height, has_rtl) =
210 buffer
211 .layout_runs()
212 .fold((0.0, 0.0, false), |(width, height, has_rtl), run| {
213 (
214 run.line_w.max(width),
215 height + run.line_height,
216 has_rtl || run.rtl,
217 )
218 });
219
220 (Size::new(width, height), has_rtl)
221}
222
223pub fn align(
226 buffer: &mut cosmic_text::Buffer,
227 font_system: &mut cosmic_text::FontSystem,
228 alignment: Alignment,
229) -> Size {
230 let (min_bounds, has_rtl) = measure(buffer);
231 let mut needs_relayout = has_rtl;
232
233 if let Some(align) = to_align(alignment) {
234 let has_multiple_lines = buffer.lines.len() > 1
235 || buffer
236 .lines
237 .first()
238 .is_some_and(|line| line.layout_opt().is_some_and(|layout| layout.len() > 1));
239
240 if has_multiple_lines {
241 for line in &mut buffer.lines {
242 let _ = line.set_align(Some(align));
243 }
244
245 needs_relayout = true;
246 } else if let Some(line) = buffer.lines.first_mut() {
247 needs_relayout = line.set_align(None);
248 }
249 }
250
251 if needs_relayout {
253 log::trace!("Relayouting paragraph...");
254
255 buffer.set_size(font_system, Some(min_bounds.width), Some(min_bounds.height));
256 }
257
258 min_bounds
259}
260
261pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
263 cosmic_text::Attrs::new()
264 .family(to_family(font.family))
265 .weight(to_weight(font.weight))
266 .stretch(to_stretch(font.stretch))
267 .style(to_style(font.style))
268}
269
270fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
271 match family {
272 font::Family::Name(name) => cosmic_text::Family::Name(name),
273 font::Family::SansSerif => cosmic_text::Family::SansSerif,
274 font::Family::Serif => cosmic_text::Family::Serif,
275 font::Family::Cursive => cosmic_text::Family::Cursive,
276 font::Family::Fantasy => cosmic_text::Family::Fantasy,
277 font::Family::Monospace => cosmic_text::Family::Monospace,
278 }
279}
280
281fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
282 match weight {
283 font::Weight::Thin => cosmic_text::Weight::THIN,
284 font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
285 font::Weight::Light => cosmic_text::Weight::LIGHT,
286 font::Weight::Normal => cosmic_text::Weight::NORMAL,
287 font::Weight::Medium => cosmic_text::Weight::MEDIUM,
288 font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
289 font::Weight::Bold => cosmic_text::Weight::BOLD,
290 font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
291 font::Weight::Black => cosmic_text::Weight::BLACK,
292 }
293}
294
295fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
296 match stretch {
297 font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
298 font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
299 font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
300 font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
301 font::Stretch::Normal => cosmic_text::Stretch::Normal,
302 font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
303 font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
304 font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
305 font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
306 }
307}
308
309fn to_style(style: font::Style) -> cosmic_text::Style {
310 match style {
311 font::Style::Normal => cosmic_text::Style::Normal,
312 font::Style::Italic => cosmic_text::Style::Italic,
313 font::Style::Oblique => cosmic_text::Style::Oblique,
314 }
315}
316
317fn to_align(alignment: Alignment) -> Option<cosmic_text::Align> {
318 match alignment {
319 Alignment::Default => None,
320 Alignment::Left => Some(cosmic_text::Align::Left),
321 Alignment::Center => Some(cosmic_text::Align::Center),
322 Alignment::Right => Some(cosmic_text::Align::Right),
323 Alignment::Justified => Some(cosmic_text::Align::Justified),
324 }
325}
326
327pub fn to_shaping(shaping: Shaping, text: &str) -> cosmic_text::Shaping {
329 match shaping {
330 Shaping::Auto => {
331 if text.is_ascii() {
332 cosmic_text::Shaping::Basic
333 } else {
334 cosmic_text::Shaping::Advanced
335 }
336 }
337 Shaping::Basic => cosmic_text::Shaping::Basic,
338 Shaping::Advanced => cosmic_text::Shaping::Advanced,
339 }
340}
341
342pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
344 match wrapping {
345 Wrapping::None => cosmic_text::Wrap::None,
346 Wrapping::Word => cosmic_text::Wrap::Word,
347 Wrapping::Glyph => cosmic_text::Wrap::Glyph,
348 Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
349 }
350}
351
352pub fn to_ellipsize(ellipsis: Ellipsis, max_height: f32) -> cosmic_text::Ellipsize {
354 let limit = cosmic_text::EllipsizeHeightLimit::Height(max_height);
355
356 match ellipsis {
357 Ellipsis::None => cosmic_text::Ellipsize::None,
358 Ellipsis::Start => cosmic_text::Ellipsize::Start(limit),
359 Ellipsis::Middle => cosmic_text::Ellipsize::Middle(limit),
360 Ellipsis::End => cosmic_text::Ellipsize::End(limit),
361 }
362}
363
364pub fn to_color(color: Color) -> cosmic_text::Color {
366 let [r, g, b, a] = color.into_rgba8();
367
368 cosmic_text::Color::rgba(r, g, b, a)
369}
370
371pub fn hint_factor(_size: Pixels, _scale_factor: Option<f32>) -> Option<f32> {
373 None }
386
387pub trait Renderer {
389 fn fill_raw(&mut self, raw: Raw);
391}