Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ advanced-shaping = []
[dependencies]
bitflags.workspace = true
bytes.workspace = true
cosmic-text.workspace = true
glam.workspace = true
lilt.workspace = true
log.workspace = true
Expand Down
132 changes: 132 additions & 0 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ impl Font {
}
}

/// Returns the cosmic_text attributes of the given [`Font`].
impl From<Font> for cosmic_text::Attrs<'static> {
fn from(font: Font) -> cosmic_text::Attrs<'static> {
cosmic_text::Attrs::new()
.family(font.family.into())
.weight(font.weight.into())
.stretch(font.stretch.into())
.style(font.style.into())
}
}

impl From<&'static str> for Font {
fn from(name: &'static str) -> Self {
Font::new(name)
Expand Down Expand Up @@ -134,6 +145,32 @@ impl Family {
}
}

impl From<cosmic_text::Family<'static>> for Family {
fn from(family: cosmic_text::Family<'static>) -> Family {
match family {
cosmic_text::Family::Name(name) => Family::Name(name),
cosmic_text::Family::SansSerif => Family::SansSerif,
cosmic_text::Family::Serif => Family::Serif,
cosmic_text::Family::Cursive => Family::Cursive,
cosmic_text::Family::Fantasy => Family::Fantasy,
cosmic_text::Family::Monospace => Family::Monospace,
}
}
}

impl From<Family> for cosmic_text::Family<'static> {
fn from(family: Family) -> cosmic_text::Family<'static> {
match family {
Family::Name(name) => cosmic_text::Family::Name(name),
Family::SansSerif => cosmic_text::Family::SansSerif,
Family::Serif => cosmic_text::Family::Serif,
Family::Cursive => cosmic_text::Family::Cursive,
Family::Fantasy => cosmic_text::Family::Fantasy,
Family::Monospace => cosmic_text::Family::Monospace,
}
}
}

impl From<&str> for Family {
fn from(name: &str) -> Self {
Family::name(name)
Expand Down Expand Up @@ -169,6 +206,49 @@ pub enum Weight {
Black,
}

/// An error that results from trying to convert a cosmic_text::Weight into an
/// Iced Weight.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum WeightConversionError {
/// Iced only allows for certain predefined weight constants, and anything
/// other than those will result in a conversion error.
NoMatchingWeight(u16),
}

impl TryFrom<cosmic_text::Weight> for Weight {
type Error = WeightConversionError;

fn try_from(weight: cosmic_text::Weight) -> Result<Weight, WeightConversionError> {
Ok(match weight {
cosmic_text::Weight::EXTRA_BOLD => Weight::ExtraBold,
cosmic_text::Weight::BOLD => Weight::Bold,
cosmic_text::Weight::SEMIBOLD => Weight::Semibold,
cosmic_text::Weight::MEDIUM => Weight::Medium,
cosmic_text::Weight::NORMAL => Weight::Normal,
cosmic_text::Weight::LIGHT => Weight::Light,
cosmic_text::Weight::EXTRA_LIGHT => Weight::ExtraLight,
cosmic_text::Weight::THIN => Weight::Thin,
cosmic_text::Weight(w) => return Err(WeightConversionError::NoMatchingWeight(w)),
})
}
}

impl From<Weight> for cosmic_text::Weight {
fn from(weight: Weight) -> cosmic_text::Weight {
match weight {
Weight::Thin => cosmic_text::Weight::THIN,
Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
Weight::Light => cosmic_text::Weight::LIGHT,
Weight::Normal => cosmic_text::Weight::NORMAL,
Weight::Medium => cosmic_text::Weight::MEDIUM,
Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
Weight::Bold => cosmic_text::Weight::BOLD,
Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
Weight::Black => cosmic_text::Weight::BLACK,
}
}
}

/// The width of some text.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
Expand All @@ -185,6 +265,38 @@ pub enum Stretch {
UltraExpanded,
}

impl From<cosmic_text::Stretch> for Stretch {
fn from(stretch: cosmic_text::Stretch) -> Stretch {
match stretch {
cosmic_text::Stretch::UltraCondensed => Stretch::UltraCondensed,
cosmic_text::Stretch::ExtraCondensed => Stretch::ExtraCondensed,
cosmic_text::Stretch::Condensed => Stretch::Condensed,
cosmic_text::Stretch::SemiCondensed => Stretch::SemiCondensed,
cosmic_text::Stretch::Normal => Stretch::Normal,
cosmic_text::Stretch::SemiExpanded => Stretch::SemiExpanded,
cosmic_text::Stretch::Expanded => Stretch::Expanded,
cosmic_text::Stretch::ExtraExpanded => Stretch::ExtraExpanded,
cosmic_text::Stretch::UltraExpanded => Stretch::UltraExpanded,
}
}
}

impl From<Stretch> for cosmic_text::Stretch {
fn from(stretch: Stretch) -> cosmic_text::Stretch {
match stretch {
Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
Stretch::Condensed => cosmic_text::Stretch::Condensed,
Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
Stretch::Normal => cosmic_text::Stretch::Normal,
Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
Stretch::Expanded => cosmic_text::Stretch::Expanded,
Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
}
}
}

/// The style of some text.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
Expand All @@ -195,6 +307,26 @@ pub enum Style {
Oblique,
}

impl From<cosmic_text::Style> for Style {
fn from(style: cosmic_text::Style) -> Style {
match style {
cosmic_text::Style::Normal => Style::Normal,
cosmic_text::Style::Italic => Style::Italic,
cosmic_text::Style::Oblique => Style::Oblique,
}
}
}

impl From<Style> for cosmic_text::Style {
fn from(style: Style) -> cosmic_text::Style {
match style {
Style::Normal => cosmic_text::Style::Normal,
Style::Italic => cosmic_text::Style::Italic,
Style::Oblique => cosmic_text::Style::Oblique,
}
}
}

/// A font error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {}
58 changes: 1 addition & 57 deletions graphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use paragraph::Paragraph;
pub use cosmic_text;

use crate::core::alignment;
use crate::core::font::{self, Font};
use crate::core::font::Font;
use crate::core::text::{Alignment, Ellipsis, Shaping, Wrapping};
use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};

Expand Down Expand Up @@ -291,62 +291,6 @@ pub fn align(
min_bounds
}

/// Returns the attributes of the given [`Font`].
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,
Expand Down
2 changes: 1 addition & 1 deletion graphics/src/text/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Cache {

buffer.set_text(
key.content,
&text::to_attributes(key.font),
&key.font.into(),
text::to_shaping(key.shaping, key.content),
None,
);
Expand Down
8 changes: 3 additions & 5 deletions graphics/src/text/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,7 @@ impl editor::Editor for Editor {
log::trace!("Updating font of `Editor`...");

for line in buffer.lines.iter_mut() {
let _ = line.set_attrs_list(cosmic_text::AttrsList::new(&text::to_attributes(
new_font,
)));
let _ = line.set_attrs_list(cosmic_text::AttrsList::new(&new_font.into()));
}

internal.font = new_font;
Expand Down Expand Up @@ -648,7 +646,7 @@ impl editor::Editor for Editor {

let mut font_system = text::font_system().write().expect("Write font system");

let attributes = text::to_attributes(font);
let attributes = font.into();

for line in &mut buffer_mut_from_editor(&mut internal.editor).lines
[current_line..=last_visible_line]
Expand All @@ -664,7 +662,7 @@ impl editor::Editor for Editor {
&cosmic_text::Attrs {
color_opt: format.color.map(text::to_color),
..if let Some(font) = format.font {
text::to_attributes(font)
font.into()
} else {
attributes.clone()
}
Expand Down
6 changes: 3 additions & 3 deletions graphics/src/text/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl core::text::Paragraph for Paragraph {

buffer.set_text(
text.content,
&text::to_attributes(text.font),
&text.font.into(),
text::to_shaping(text.shaping, text.content),
None,
);
Expand Down Expand Up @@ -153,7 +153,7 @@ impl core::text::Paragraph for Paragraph {

buffer.set_rich_text(
text.content.iter().enumerate().map(|(i, span)| {
let attrs = text::to_attributes(span.font.unwrap_or(text.font));
let attrs: cosmic_text::Attrs<'_> = span.font.unwrap_or(text.font).into();

let attrs = match (span.size, span.line_height) {
(None, None) => attrs,
Expand All @@ -179,7 +179,7 @@ impl core::text::Paragraph for Paragraph {

(span.text.as_ref(), attrs.metadata(i))
}),
&text::to_attributes(text.font),
&text.font.into(),
cosmic_text::Shaping::Advanced,
None,
);
Expand Down
Loading