-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fff0a30
commit c8d770a
Showing
9 changed files
with
190 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
pub use cosmic_text::{Style, Weight}; | ||
use std::{borrow::Cow, fmt::Display}; | ||
|
||
#[derive(Debug, Clone)] | ||
// Inspired by: https://github.com/pop-os/cosmic-text | ||
pub enum FontFamily { | ||
Named(Cow<'static, str>), | ||
|
||
/// Serif fonts represent the formal text style for a script. | ||
Serif, | ||
|
||
/// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low contrast | ||
/// and have stroke endings that are plain — without any flaring, cross stroke, | ||
/// or other ornamentation. | ||
SansSerif, | ||
|
||
/// Glyphs in cursive fonts generally use a more informal script style, | ||
/// and the result looks more like handwritten pen or brush writing than printed letterwork. | ||
Cursive, | ||
|
||
/// Fantasy fonts are primarily decorative or expressive fonts that | ||
/// contain decorative or expressive representations of characters. | ||
Fantasy, | ||
|
||
/// The sole criterion of a monospace font is that all glyphs have the same fixed width. | ||
Monospace, | ||
} | ||
|
||
impl Display for FontFamily { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
FontFamily::Named(name) => write!(f, "{name}"), | ||
FontFamily::Serif => write!(f, "serif"), | ||
FontFamily::SansSerif => write!(f, "sans-serif"), | ||
FontFamily::Cursive => write!(f, "cursive"), | ||
FontFamily::Fantasy => write!(f, "fantasy"), | ||
FontFamily::Monospace => write!(f, "monospace"), | ||
} | ||
} | ||
} | ||
|
||
impl FontFamily { | ||
pub fn named(name: impl Into<Cow<'static, str>>) -> Self { | ||
Self::Named(name.into()) | ||
} | ||
} | ||
|
||
impl<T> From<T> for FontFamily | ||
where | ||
T: Into<Cow<'static, str>>, | ||
{ | ||
fn from(value: T) -> Self { | ||
Self::named(value) | ||
} | ||
} | ||
|
||
/// A segment of rich text | ||
pub struct TextSegment { | ||
pub text: String, | ||
pub family: FontFamily, | ||
pub style: Style, | ||
pub weight: Weight, | ||
} | ||
|
||
impl TextSegment { | ||
pub fn new(text: impl Into<String>) -> Self { | ||
Self { | ||
text: text.into(), | ||
family: FontFamily::Serif, | ||
style: Style::Normal, | ||
weight: Weight::NORMAL, | ||
} | ||
} | ||
|
||
pub fn with_family(mut self, family: impl Into<FontFamily>) -> Self { | ||
self.family = family.into(); | ||
self | ||
} | ||
|
||
pub fn with_style(mut self, style: Style) -> Self { | ||
self.style = style; | ||
self | ||
} | ||
|
||
pub fn with_weight(mut self, weight: Weight) -> Self { | ||
self.weight = weight; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.