-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* implement cosmic_text as the text renderer and editor * fix text related tests * fix text sizing being incorrect on certain fonts * Re-add TextAlignment so that we don't break more code * Restore textbox API * Fix overflow detection; left debug prints in while I diagnose other problems * Make PaintLimits optional because they aren't initialized right away * Fix textbox state machine * Working textbox state machine * Fix up modifier keys and some warnings * Fix alignment (which works in more cases now! O_o) * Remove debug prints, fix cursor when buffer is empty * Use ((x, y), (w, h)) order instead of ((x, w), (y, h)) order for coordinates * Improve variable names since there are two 'text changed' variables now * Re-export cosmic_text from yakui crate * Fix tests, restore old YakuiWgpu constructor, add button alignment example * Remove Yakui argument from boostrap, app, and demo * Always import cosmic_text from yakui instead of yakui_widgets if available * Restore examples and text API to use (potentially flawed) Into<String> generic * Fix clear_textbox example * Remove yakui_widgets references from yakui_app and bootstrap * Clean up examples * Borrow and initialize Option instead of taking it to prevent possible mistakes * Triple whammy to fix button alignment — I think this is good enough * Accept snapshots, for better or worse --------- Co-authored-by: Madeline Sparkles <[email protected]>
- Loading branch information
Showing
35 changed files
with
1,691 additions
and
807 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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
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
Binary file not shown.
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 |
---|---|---|
@@ -1,118 +1,89 @@ | ||
use std::cell::Ref; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::fmt; | ||
use std::rc::Rc; | ||
|
||
use smol_str::SmolStr; | ||
use thunderdome::{Arena, Index}; | ||
|
||
pub use fontdue::{Font, FontSettings}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub struct Fonts { | ||
inner: Rc<RefCell<FontsInner>>, | ||
} | ||
|
||
struct FontsInner { | ||
storage: Arena<Font>, | ||
by_name: HashMap<FontName, FontId>, | ||
font_system: cosmic_text::FontSystem, | ||
} | ||
|
||
impl Fonts { | ||
#[allow(unused_mut, unused_assignments)] | ||
fn new() -> Self { | ||
let mut storage = Arena::new(); | ||
let mut by_name = HashMap::new(); | ||
let mut font_system = cosmic_text::FontSystem::new_with_locale_and_db( | ||
sys_locale::get_locale().unwrap_or(String::from("en-US")), | ||
{ | ||
let mut database = cosmic_text::fontdb::Database::default(); | ||
database.set_serif_family(""); | ||
database.set_sans_serif_family(""); | ||
database.set_cursive_family(""); | ||
database.set_fantasy_family(""); | ||
database.set_monospace_family(""); | ||
database | ||
}, | ||
); | ||
|
||
#[cfg(feature = "default-fonts")] | ||
{ | ||
static DEFAULT_BYTES: &[u8] = include_bytes!("../assets/Roboto-Regular.ttf"); | ||
|
||
let font = Font::from_bytes(DEFAULT_BYTES, FontSettings::default()) | ||
.expect("failed to load built-in font"); | ||
let id = FontId::new(storage.insert(font)); | ||
by_name.insert(FontName::new("default"), id); | ||
font_system | ||
.db_mut() | ||
.load_font_source(cosmic_text::fontdb::Source::Binary(Arc::from( | ||
&DEFAULT_BYTES, | ||
))); | ||
} | ||
|
||
let inner = Rc::new(RefCell::new(FontsInner { storage, by_name })); | ||
let inner = Rc::new(RefCell::new(FontsInner { font_system })); | ||
Self { inner } | ||
} | ||
|
||
pub fn add<S: Into<FontName>>(&self, font: Font, name: Option<S>) -> FontId { | ||
let mut inner = self.inner.borrow_mut(); | ||
|
||
let id = FontId::new(inner.storage.insert(font)); | ||
if let Some(name) = name { | ||
inner.by_name.insert(name.into(), id); | ||
} | ||
|
||
id | ||
} | ||
|
||
pub fn get(&self, name: &FontName) -> Option<Ref<'_, Font>> { | ||
let inner = self.inner.borrow(); | ||
|
||
let &id = inner.by_name.get(name)?; | ||
if inner.storage.contains(id.0) { | ||
Some(Ref::map(inner, |inner| inner.storage.get(id.0).unwrap())) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
pub fn with_system<T>(&self, f: impl FnOnce(&mut cosmic_text::FontSystem) -> T) -> T { | ||
let mut inner = (*self.inner).borrow_mut(); | ||
|
||
impl Default for Fonts { | ||
fn default() -> Self { | ||
Self::new() | ||
f(&mut inner.font_system) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct FontName(SmolStr); | ||
|
||
impl FontName { | ||
pub fn new<S: AsRef<str>>(name: S) -> Self { | ||
Self(name.as_ref().into()) | ||
pub fn load_font_source( | ||
&self, | ||
source: cosmic_text::fontdb::Source, | ||
) -> Vec<cosmic_text::fontdb::ID> { | ||
self.with_system(|font_system| font_system.db_mut().load_font_source(source)) | ||
.to_vec() | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.0.as_str() | ||
/// Sets the family that will be used by `Family::Serif`. | ||
pub fn set_serif_family<S: Into<String>>(&self, family: S) { | ||
self.with_system(|font_system| font_system.db_mut().set_serif_family(family)); | ||
} | ||
} | ||
|
||
impl From<&str> for FontName { | ||
fn from(value: &str) -> Self { | ||
Self(value.into()) | ||
/// Sets the family that will be used by `Family::SansSerif`. | ||
pub fn set_sans_serif_family<S: Into<String>>(&self, family: S) { | ||
self.with_system(|font_system| font_system.db_mut().set_sans_serif_family(family)); | ||
} | ||
} | ||
|
||
impl From<&String> for FontName { | ||
fn from(value: &String) -> Self { | ||
Self(value.into()) | ||
/// Sets the family that will be used by `Family::Cursive`. | ||
pub fn set_cursive_family<S: Into<String>>(&self, family: S) { | ||
self.with_system(|font_system| font_system.db_mut().set_cursive_family(family)); | ||
} | ||
} | ||
|
||
impl fmt::Display for FontName { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
/// Sets the family that will be used by `Family::Fantasy`. | ||
pub fn set_fantasy_family<S: Into<String>>(&self, family: S) { | ||
self.with_system(|font_system| font_system.db_mut().set_fantasy_family(family)); | ||
} | ||
} | ||
|
||
/// Identifies a font that has been loaded and can be used by yakui. | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[repr(transparent)] | ||
pub struct FontId(Index); | ||
|
||
impl FontId { | ||
#[inline] | ||
pub(crate) fn new(index: Index) -> Self { | ||
Self(index) | ||
/// Sets the family that will be used by `Family::Monospace`. | ||
pub fn set_monospace_family<S: Into<String>>(&self, family: S) { | ||
self.with_system(|font_system| font_system.db_mut().set_monospace_family(family)); | ||
} | ||
} | ||
|
||
impl fmt::Debug for FontId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "FontId({}, {})", self.0.slot(), self.0.generation()) | ||
impl Default for Fonts { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
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
Oops, something went wrong.