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 #[cfg(target_os = "macos")]
138 {
139 #[cfg(not(feature = "fira-sans"))]
140 raw.db_mut().set_sans_serif_family(".SF NS");
141 raw.db_mut().set_serif_family("Times New Roman");
142 raw.db_mut().set_monospace_family("Menlo");
143 }
144
145 #[cfg(target_os = "windows")]
146 {
147 #[cfg(not(feature = "fira-sans"))]
148 raw.db_mut().set_sans_serif_family("Segoe UI");
149 raw.db_mut().set_serif_family("Times New Roman");
150 raw.db_mut().set_monospace_family("Consolas");
151 }
152
153 RwLock::new(FontSystem {
154 raw,
155 loaded_fonts: HashSet::new(),
156 version: Version::default(),
157 })
158 })
159}
160
161pub struct FontSystem {
163 raw: cosmic_text::FontSystem,
164 loaded_fonts: HashSet<usize>,
165 version: Version,
166}
167
168impl FontSystem {
169 pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
171 &mut self.raw
172 }
173
174 pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
176 if let Cow::Borrowed(bytes) = bytes {
177 let address = bytes.as_ptr() as usize;
178
179 if !self.loaded_fonts.insert(address) {
180 return;
181 }
182 }
183
184 let _ = self
185 .raw
186 .db_mut()
187 .load_font_source(cosmic_text::fontdb::Source::Binary(Arc::new(
188 bytes.into_owned(),
189 )));
190
191 self.version = Version(self.version.0 + 1);
192 }
193
194 pub fn families(&self) -> impl Iterator<Item = &str> {
197 self.raw
198 .db()
199 .faces()
200 .filter_map(|face| face.families.first())
201 .map(|(name, _)| name.as_str())
202 }
203
204 pub fn version(&self) -> Version {
208 self.version
209 }
210}
211
212#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
214pub struct Version(u32);
215
216#[derive(Debug, Clone)]
218pub struct Raw {
219 pub buffer: Weak<cosmic_text::Buffer>,
221 pub position: Point,
223 pub color: Color,
225 pub clip_bounds: Rectangle,
227}
228
229impl PartialEq for Raw {
230 fn eq(&self, _other: &Self) -> bool {
231 false
236 }
237}
238
239pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
241 let (width, height, has_rtl) =
242 buffer
243 .layout_runs()
244 .fold((0.0, 0.0, false), |(width, height, has_rtl), run| {
245 (
246 run.line_w.max(width),
247 height + run.line_height,
248 has_rtl || run.rtl,
249 )
250 });
251
252 (Size::new(width, height), has_rtl)
253}
254
255pub fn align(
258 buffer: &mut cosmic_text::Buffer,
259 font_system: &mut cosmic_text::FontSystem,
260 alignment: Alignment,
261) -> Size {
262 let (min_bounds, has_rtl) = measure(buffer);
263 let mut needs_relayout = has_rtl;
264
265 if let Some(align) = to_align(alignment) {
266 let has_multiple_lines = buffer.lines.len() > 1
267 || buffer
268 .lines
269 .first()
270 .is_some_and(|line| line.layout_opt().is_some_and(|layout| layout.len() > 1));
271
272 if has_multiple_lines {
273 for line in &mut buffer.lines {
274 let _ = line.set_align(Some(align));
275 }
276
277 needs_relayout = true;
278 } else if let Some(line) = buffer.lines.first_mut() {
279 needs_relayout |= line.set_align(None);
280 }
281 }
282
283 if needs_relayout {
285 log::trace!("Relayouting paragraph...");
286
287 buffer.set_size(Some(min_bounds.width), Some(min_bounds.height));
288 buffer.shape_until_scroll(font_system, false);
289 }
290
291 min_bounds
292}
293
294pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
296 cosmic_text::Attrs::new()
297 .family(to_family(font.family))
298 .weight(to_weight(font.weight))
299 .stretch(to_stretch(font.stretch))
300 .style(to_style(font.style))
301}
302
303fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
304 match family {
305 font::Family::Name(name) => cosmic_text::Family::Name(name),
306 font::Family::SansSerif => cosmic_text::Family::SansSerif,
307 font::Family::Serif => cosmic_text::Family::Serif,
308 font::Family::Cursive => cosmic_text::Family::Cursive,
309 font::Family::Fantasy => cosmic_text::Family::Fantasy,
310 font::Family::Monospace => cosmic_text::Family::Monospace,
311 }
312}
313
314fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
315 match weight {
316 font::Weight::Thin => cosmic_text::Weight::THIN,
317 font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
318 font::Weight::Light => cosmic_text::Weight::LIGHT,
319 font::Weight::Normal => cosmic_text::Weight::NORMAL,
320 font::Weight::Medium => cosmic_text::Weight::MEDIUM,
321 font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
322 font::Weight::Bold => cosmic_text::Weight::BOLD,
323 font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
324 font::Weight::Black => cosmic_text::Weight::BLACK,
325 }
326}
327
328fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
329 match stretch {
330 font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
331 font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
332 font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
333 font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
334 font::Stretch::Normal => cosmic_text::Stretch::Normal,
335 font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
336 font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
337 font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
338 font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
339 }
340}
341
342fn to_style(style: font::Style) -> cosmic_text::Style {
343 match style {
344 font::Style::Normal => cosmic_text::Style::Normal,
345 font::Style::Italic => cosmic_text::Style::Italic,
346 font::Style::Oblique => cosmic_text::Style::Oblique,
347 }
348}
349
350fn to_align(alignment: Alignment) -> Option<cosmic_text::Align> {
351 match alignment {
352 Alignment::Default => None,
353 Alignment::Left => Some(cosmic_text::Align::Left),
354 Alignment::Center => Some(cosmic_text::Align::Center),
355 Alignment::Right => Some(cosmic_text::Align::Right),
356 Alignment::Justified => Some(cosmic_text::Align::Justified),
357 }
358}
359
360pub fn to_shaping(shaping: Shaping, text: &str) -> cosmic_text::Shaping {
362 match shaping {
363 Shaping::Auto => {
364 if text.is_ascii() {
365 cosmic_text::Shaping::Basic
366 } else {
367 cosmic_text::Shaping::Advanced
368 }
369 }
370 Shaping::Basic => cosmic_text::Shaping::Basic,
371 Shaping::Advanced => cosmic_text::Shaping::Advanced,
372 }
373}
374
375pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
377 match wrapping {
378 Wrapping::None => cosmic_text::Wrap::None,
379 Wrapping::Word => cosmic_text::Wrap::Word,
380 Wrapping::Glyph => cosmic_text::Wrap::Glyph,
381 Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
382 }
383}
384
385pub fn to_ellipsize(ellipsis: Ellipsis, max_height: f32) -> cosmic_text::Ellipsize {
387 let limit = cosmic_text::EllipsizeHeightLimit::Height(max_height);
388
389 match ellipsis {
390 Ellipsis::None => cosmic_text::Ellipsize::None,
391 Ellipsis::Start => cosmic_text::Ellipsize::Start(limit),
392 Ellipsis::Middle => cosmic_text::Ellipsize::Middle(limit),
393 Ellipsis::End => cosmic_text::Ellipsize::End(limit),
394 }
395}
396
397pub fn to_color(color: Color) -> cosmic_text::Color {
399 let [r, g, b, a] = color.into_rgba8();
400
401 cosmic_text::Color::rgba(r, g, b, a)
402}
403
404pub fn hint_factor(_size: Pixels, _scale_factor: Option<f32>) -> Option<f32> {
406 None }
419
420pub trait Renderer {
422 fn fill_raw(&mut self, raw: Raw);
424}