pub mod cache;
pub mod editor;
pub mod paragraph;
pub use cache::Cache;
pub use editor::Editor;
pub use paragraph::Paragraph;
pub use cosmic_text;
use crate::core::alignment;
use crate::core::font::{self, Font};
use crate::core::text::{Alignment, Ellipsis, Shaping, Wrapping};
use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
use std::borrow::Cow;
use std::collections::HashSet;
use std::sync::{Arc, OnceLock, RwLock, Weak};
#[derive(Debug, Clone, PartialEq)]
pub enum Text {
#[allow(missing_docs)]
Paragraph {
paragraph: paragraph::Weak,
position: Point,
color: Color,
clip_bounds: Rectangle,
transformation: Transformation,
},
#[allow(missing_docs)]
Editor {
editor: editor::Weak,
position: Point,
color: Color,
clip_bounds: Rectangle,
transformation: Transformation,
},
Cached {
content: String,
bounds: Rectangle,
color: Color,
size: Pixels,
line_height: Pixels,
font: Font,
align_x: Alignment,
align_y: alignment::Vertical,
shaping: Shaping,
wrapping: Wrapping,
ellipsis: Ellipsis,
clip_bounds: Rectangle,
},
#[allow(missing_docs)]
Raw {
raw: Raw,
transformation: Transformation,
},
}
impl Text {
pub fn visible_bounds(&self) -> Option<Rectangle> {
match self {
Text::Paragraph {
position,
paragraph,
clip_bounds,
transformation,
..
} => Rectangle::new(*position, paragraph.min_bounds)
.intersection(clip_bounds)
.map(|bounds| bounds * *transformation),
Text::Editor {
editor,
position,
clip_bounds,
transformation,
..
} => Rectangle::new(*position, editor.bounds)
.intersection(clip_bounds)
.map(|bounds| bounds * *transformation),
Text::Cached {
bounds,
clip_bounds,
..
} => bounds.intersection(clip_bounds),
Text::Raw { raw, .. } => Some(raw.clip_bounds),
}
}
}
#[cfg(feature = "fira-sans")]
pub const FIRA_SANS_REGULAR: &[u8] = include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice();
pub fn font_system() -> &'static RwLock<FontSystem> {
static FONT_SYSTEM: OnceLock<RwLock<FontSystem>> = OnceLock::new();
FONT_SYSTEM.get_or_init(|| {
#[allow(unused_mut)]
let mut raw = cosmic_text::FontSystem::new_with_fonts([
cosmic_text::fontdb::Source::Binary(Arc::new(
include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
)),
#[cfg(feature = "fira-sans")]
cosmic_text::fontdb::Source::Binary(Arc::new(
include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
)),
]);
#[cfg(feature = "fira-sans")]
raw.db_mut().set_sans_serif_family("Fira Sans");
RwLock::new(FontSystem {
raw,
loaded_fonts: HashSet::new(),
version: Version::default(),
})
})
}
pub struct FontSystem {
raw: cosmic_text::FontSystem,
loaded_fonts: HashSet<usize>,
version: Version,
}
impl FontSystem {
pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
&mut self.raw
}
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
if let Cow::Borrowed(bytes) = bytes {
let address = bytes.as_ptr() as usize;
if !self.loaded_fonts.insert(address) {
return;
}
}
let _ = self
.raw
.db_mut()
.load_font_source(cosmic_text::fontdb::Source::Binary(Arc::new(
bytes.into_owned(),
)));
self.version = Version(self.version.0 + 1);
}
pub fn families(&self) -> impl Iterator<Item = &str> {
self.raw
.db()
.faces()
.filter_map(|face| face.families.first())
.map(|(name, _)| name.as_str())
}
pub fn version(&self) -> Version {
self.version
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Version(u32);
#[derive(Debug, Clone)]
pub struct Raw {
pub buffer: Weak<cosmic_text::Buffer>,
pub position: Point,
pub color: Color,
pub clip_bounds: Rectangle,
}
impl PartialEq for Raw {
fn eq(&self, _other: &Self) -> bool {
false
}
}
pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
let (width, height, has_rtl) =
buffer
.layout_runs()
.fold((0.0, 0.0, false), |(width, height, has_rtl), run| {
(
run.line_w.max(width),
height + run.line_height,
has_rtl || run.rtl,
)
});
(Size::new(width, height), has_rtl)
}
pub fn align(
buffer: &mut cosmic_text::Buffer,
font_system: &mut cosmic_text::FontSystem,
alignment: Alignment,
) -> Size {
let (min_bounds, has_rtl) = measure(buffer);
let mut needs_relayout = has_rtl;
if let Some(align) = to_align(alignment) {
let has_multiple_lines = buffer.lines.len() > 1
|| buffer
.lines
.first()
.is_some_and(|line| line.layout_opt().is_some_and(|layout| layout.len() > 1));
if has_multiple_lines {
for line in &mut buffer.lines {
let _ = line.set_align(Some(align));
}
needs_relayout = true;
} else if let Some(line) = buffer.lines.first_mut() {
needs_relayout |= line.set_align(None);
}
}
if needs_relayout {
log::trace!("Relayouting paragraph...");
buffer.set_size(font_system, Some(min_bounds.width), Some(min_bounds.height));
}
min_bounds
}
pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
cosmic_text::Attrs::new()
.family(to_family(font.family))
.weight(to_weight(font.weight))
.stretch(to_stretch(font.stretch))
.style(to_style(font.style))
}
fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
match family {
font::Family::Name(name) => cosmic_text::Family::Name(name),
font::Family::SansSerif => cosmic_text::Family::SansSerif,
font::Family::Serif => cosmic_text::Family::Serif,
font::Family::Cursive => cosmic_text::Family::Cursive,
font::Family::Fantasy => cosmic_text::Family::Fantasy,
font::Family::Monospace => cosmic_text::Family::Monospace,
}
}
fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
match weight {
font::Weight::Thin => cosmic_text::Weight::THIN,
font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
font::Weight::Light => cosmic_text::Weight::LIGHT,
font::Weight::Normal => cosmic_text::Weight::NORMAL,
font::Weight::Medium => cosmic_text::Weight::MEDIUM,
font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
font::Weight::Bold => cosmic_text::Weight::BOLD,
font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
font::Weight::Black => cosmic_text::Weight::BLACK,
}
}
fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
match stretch {
font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
font::Stretch::Normal => cosmic_text::Stretch::Normal,
font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
}
}
fn to_style(style: font::Style) -> cosmic_text::Style {
match style {
font::Style::Normal => cosmic_text::Style::Normal,
font::Style::Italic => cosmic_text::Style::Italic,
font::Style::Oblique => cosmic_text::Style::Oblique,
}
}
fn to_align(alignment: Alignment) -> Option<cosmic_text::Align> {
match alignment {
Alignment::Default => None,
Alignment::Left => Some(cosmic_text::Align::Left),
Alignment::Center => Some(cosmic_text::Align::Center),
Alignment::Right => Some(cosmic_text::Align::Right),
Alignment::Justified => Some(cosmic_text::Align::Justified),
}
}
pub fn to_shaping(shaping: Shaping, text: &str) -> cosmic_text::Shaping {
match shaping {
Shaping::Auto => {
if text.is_ascii() {
cosmic_text::Shaping::Basic
} else {
cosmic_text::Shaping::Advanced
}
}
Shaping::Basic => cosmic_text::Shaping::Basic,
Shaping::Advanced => cosmic_text::Shaping::Advanced,
}
}
pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
match wrapping {
Wrapping::None => cosmic_text::Wrap::None,
Wrapping::Word => cosmic_text::Wrap::Word,
Wrapping::Glyph => cosmic_text::Wrap::Glyph,
Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
}
}
pub fn to_ellipsize(ellipsis: Ellipsis, max_height: f32) -> cosmic_text::Ellipsize {
let limit = cosmic_text::EllipsizeHeightLimit::Height(max_height);
match ellipsis {
Ellipsis::None => cosmic_text::Ellipsize::None,
Ellipsis::Start => cosmic_text::Ellipsize::Start(limit),
Ellipsis::Middle => cosmic_text::Ellipsize::Middle(limit),
Ellipsis::End => cosmic_text::Ellipsize::End(limit),
}
}
pub fn to_color(color: Color) -> cosmic_text::Color {
let [r, g, b, a] = color.into_rgba8();
cosmic_text::Color::rgba(r, g, b, a)
}
pub fn hint_factor(_size: Pixels, _scale_factor: Option<f32>) -> Option<f32> {
None }
pub trait Renderer {
fn fill_raw(&mut self, raw: Raw);
}