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::{Shaping, Wrapping};
15use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
16
17use once_cell::sync::OnceCell;
18use std::borrow::Cow;
19use std::sync::{Arc, 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 horizontal_alignment: alignment::Horizontal,
58 vertical_alignment: alignment::Vertical,
60 shaping: Shaping,
62 clip_bounds: Rectangle,
64 },
65 #[allow(missing_docs)]
67 Raw {
68 raw: Raw,
69 transformation: Transformation,
70 },
71}
72
73impl Text {
74 pub fn visible_bounds(&self) -> Option<Rectangle> {
76 let (bounds, horizontal_alignment, vertical_alignment) = match self {
77 Text::Paragraph {
78 position,
79 paragraph,
80 clip_bounds,
81 transformation,
82 ..
83 } => (
84 Rectangle::new(*position, paragraph.min_bounds)
85 .intersection(clip_bounds)
86 .map(|bounds| bounds * *transformation),
87 Some(paragraph.horizontal_alignment),
88 Some(paragraph.vertical_alignment),
89 ),
90 Text::Editor {
91 editor,
92 position,
93 clip_bounds,
94 transformation,
95 ..
96 } => (
97 Rectangle::new(*position, editor.bounds)
98 .intersection(clip_bounds)
99 .map(|bounds| bounds * *transformation),
100 None,
101 None,
102 ),
103 Text::Cached {
104 bounds,
105 clip_bounds,
106 horizontal_alignment,
107 vertical_alignment,
108 ..
109 } => (
110 bounds.intersection(clip_bounds),
111 Some(*horizontal_alignment),
112 Some(*vertical_alignment),
113 ),
114 Text::Raw { raw, .. } => (Some(raw.clip_bounds), None, None),
115 };
116
117 let mut bounds = bounds?;
118
119 if let Some(alignment) = horizontal_alignment {
120 match alignment {
121 alignment::Horizontal::Left => {}
122 alignment::Horizontal::Center => {
123 bounds.x -= bounds.width / 2.0;
124 }
125 alignment::Horizontal::Right => {
126 bounds.x -= bounds.width;
127 }
128 }
129 }
130
131 if let Some(alignment) = vertical_alignment {
132 match alignment {
133 alignment::Vertical::Top => {}
134 alignment::Vertical::Center => {
135 bounds.y -= bounds.height / 2.0;
136 }
137 alignment::Vertical::Bottom => {
138 bounds.y -= bounds.height;
139 }
140 }
141 }
142
143 Some(bounds)
144 }
145}
146
147#[cfg(all(target_arch = "wasm32", feature = "fira-sans"))]
153pub const FIRA_SANS_REGULAR: &'static [u8] =
154 include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice();
155
156pub fn font_system() -> &'static RwLock<FontSystem> {
158 static FONT_SYSTEM: OnceCell<RwLock<FontSystem>> = OnceCell::new();
159
160 FONT_SYSTEM.get_or_init(|| {
161 RwLock::new(FontSystem {
162 raw: cosmic_text::FontSystem::new_with_fonts([
163 cosmic_text::fontdb::Source::Binary(Arc::new(
164 include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
165 )),
166 #[cfg(all(target_arch = "wasm32", feature = "fira-sans"))]
167 cosmic_text::fontdb::Source::Binary(Arc::new(
168 include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
169 )),
170 ]),
171 version: Version::default(),
172 })
173 })
174}
175
176#[allow(missing_debug_implementations)]
178pub struct FontSystem {
179 raw: cosmic_text::FontSystem,
180 version: Version,
181}
182
183impl FontSystem {
184 pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
186 &mut self.raw
187 }
188
189 pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
191 let _ = self.raw.db_mut().load_font_source(
192 cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
193 );
194
195 self.version = Version(self.version.0 + 1);
196 }
197
198 pub fn version(&self) -> Version {
202 self.version
203 }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
208pub struct Version(u32);
209
210#[derive(Debug, Clone)]
212pub struct Raw {
213 pub buffer: Weak<cosmic_text::Buffer>,
215 pub position: Point,
217 pub color: Color,
219 pub clip_bounds: Rectangle,
221}
222
223impl PartialEq for Raw {
224 fn eq(&self, _other: &Self) -> bool {
225 false
230 }
231}
232
233pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
235 let (width, height) =
236 buffer
237 .layout_runs()
238 .fold((0.0, 0.0), |(width, height), run| {
239 (run.line_w.max(width), height + run.line_height)
240 });
241 Size::new(width, height)
242
243 }
251
252pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
254 cosmic_text::Attrs::new()
255 .family(to_family(font.family))
256 .weight(to_weight(font.weight))
257 .stretch(to_stretch(font.stretch))
258 .style(to_style(font.style))
259}
260
261fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
262 match family {
263 font::Family::Name(name) => cosmic_text::Family::Name(name),
264 font::Family::SansSerif => cosmic_text::Family::SansSerif,
265 font::Family::Serif => cosmic_text::Family::Serif,
266 font::Family::Cursive => cosmic_text::Family::Cursive,
267 font::Family::Fantasy => cosmic_text::Family::Fantasy,
268 font::Family::Monospace => cosmic_text::Family::Monospace,
269 }
270}
271
272fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
273 match weight {
274 font::Weight::Thin => cosmic_text::Weight::THIN,
275 font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
276 font::Weight::Light => cosmic_text::Weight::LIGHT,
277 font::Weight::Normal => cosmic_text::Weight::NORMAL,
278 font::Weight::Medium => cosmic_text::Weight::MEDIUM,
279 font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
280 font::Weight::Bold => cosmic_text::Weight::BOLD,
281 font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
282 font::Weight::Black => cosmic_text::Weight::BLACK,
283 }
284}
285
286fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
287 match stretch {
288 font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
289 font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
290 font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
291 font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
292 font::Stretch::Normal => cosmic_text::Stretch::Normal,
293 font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
294 font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
295 font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
296 font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
297 }
298}
299
300fn to_style(style: font::Style) -> cosmic_text::Style {
301 match style {
302 font::Style::Normal => cosmic_text::Style::Normal,
303 font::Style::Italic => cosmic_text::Style::Italic,
304 font::Style::Oblique => cosmic_text::Style::Oblique,
305 }
306}
307
308pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
310 match shaping {
311 Shaping::Basic => cosmic_text::Shaping::Basic,
312 Shaping::Advanced => cosmic_text::Shaping::Advanced,
313 }
314}
315
316pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
318 match wrapping {
319 Wrapping::None => cosmic_text::Wrap::None,
320 Wrapping::Word => cosmic_text::Wrap::Word,
321 Wrapping::Glyph => cosmic_text::Wrap::Glyph,
322 Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
323 }
324}
325
326pub fn to_color(color: Color) -> cosmic_text::Color {
328 let [r, g, b, a] = color.into_rgba8();
329
330 cosmic_text::Color::rgba(r, g, b, a)
331}