add WguiFontSystem, remove FONT_SYSTEM singleton, custom fonts, add Light font weight
there are a few gzip-compressed ttf as for now, looks like variable fonts aren't parsed properly by cosmic_text. Not sure why. Also, we probably need to have a fallback for CJK characters in the future, or just fallback to the built-in ones in the OS.
This commit is contained in:
@@ -30,3 +30,4 @@ taffy = "0.9.1"
|
||||
vulkano = { workspace = true }
|
||||
vulkano-shaders = { workspace = true }
|
||||
rust-embed = { workspace = true }
|
||||
flate2 = "1.1.5"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use flate2::read::GzDecoder;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -74,6 +76,13 @@ impl Default for AssetPathOwned {
|
||||
|
||||
pub trait AssetProvider {
|
||||
fn load_from_path(&mut self, path: &str) -> anyhow::Result<Vec<u8>>;
|
||||
fn load_from_path_gzip(&mut self, path: &str) -> anyhow::Result<Vec<u8>> {
|
||||
let compressed = self.load_from_path(path)?;
|
||||
let mut gz = GzDecoder::new(&compressed[..]);
|
||||
let mut out = Vec::new();
|
||||
gz.read_to_end(&mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
// replace "./foo/bar/../file.txt" with "./foo/file.txt"
|
||||
|
||||
@@ -8,9 +8,9 @@ use crate::{
|
||||
animation::{Animation, AnimationEasing},
|
||||
components::{Component, ComponentBase, ComponentTrait, InitData},
|
||||
drawing::Color,
|
||||
event::{CallbackDataCommon, EventAlterables, EventListenerCollection, EventListenerID, EventListenerKind},
|
||||
event::{CallbackDataCommon, EventListenerCollection, EventListenerID, EventListenerKind},
|
||||
i18n::Translation,
|
||||
layout::{self, LayoutState, WidgetID, WidgetPair},
|
||||
layout::{self, WidgetID, WidgetPair},
|
||||
renderer_vk::text::{FontWeight, TextStyle},
|
||||
widget::{
|
||||
ConstructEssentials, EventResult,
|
||||
@@ -53,6 +53,7 @@ struct State {
|
||||
|
||||
#[allow(clippy::struct_field_names)]
|
||||
struct Data {
|
||||
#[allow(dead_code)]
|
||||
id_container: WidgetID, // Rectangle, transparent if not hovered
|
||||
|
||||
//id_outer_box: WidgetID, // Rectangle, parent of container
|
||||
|
||||
@@ -17,6 +17,7 @@ pub struct InitData<'a> {
|
||||
// common component data
|
||||
#[derive(Default)]
|
||||
pub struct ComponentBase {
|
||||
#[allow(dead_code)]
|
||||
lhandles: Vec<EventListenerID>,
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,8 @@ struct State {
|
||||
}
|
||||
|
||||
struct Data {
|
||||
body: WidgetID, // Div
|
||||
#[allow(dead_code)]
|
||||
body: WidgetID, // Div
|
||||
slider_handle_rect_id: WidgetID, // Rectangle
|
||||
slider_text_id: WidgetID, // Text
|
||||
slider_handle_node: taffy::NodeId,
|
||||
|
||||
@@ -7,6 +7,7 @@ use taffy::TraversePartialTree;
|
||||
use crate::{
|
||||
drawing,
|
||||
event::EventAlterables,
|
||||
globals::Globals,
|
||||
layout::Widget,
|
||||
renderer_vk::text::{TextShadow, custom_glyph::CustomGlyph},
|
||||
stack::{self, ScissorBoundary, ScissorStack, TransformStack},
|
||||
@@ -169,6 +170,7 @@ pub enum RenderPrimitive {
|
||||
}
|
||||
|
||||
pub struct DrawParams<'a> {
|
||||
pub globals: &'a Globals,
|
||||
pub layout: &'a mut Layout,
|
||||
pub debug_draw: bool,
|
||||
pub alpha: f32, // timestep alpha, 0.0 - 1.0, used for motion interpolation if rendering above tick rate: smoother animations or scrolling
|
||||
@@ -347,6 +349,7 @@ pub fn draw(params: &mut DrawParams) -> anyhow::Result<Vec<RenderPrimitive>> {
|
||||
let mut alterables = EventAlterables::default();
|
||||
|
||||
let mut state = DrawState {
|
||||
globals: params.globals,
|
||||
primitives: &mut primitives,
|
||||
transform_stack: &mut transform_stack,
|
||||
scissor_stack: &mut scissor_stack,
|
||||
|
||||
@@ -2,7 +2,6 @@ use std::{
|
||||
any::{Any, TypeId},
|
||||
cell::RefMut,
|
||||
collections::HashSet,
|
||||
ops::Deref,
|
||||
};
|
||||
|
||||
use glam::Vec2;
|
||||
|
||||
45
wgui/src/font_config.rs
Normal file
45
wgui/src/font_config.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use parking_lot::Mutex;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WguiFontConfig<'a> {
|
||||
pub binaries: Vec<&'a [u8]>,
|
||||
pub family_name_sans_serif: &'a str,
|
||||
pub family_name_serif: &'a str,
|
||||
pub family_name_monospace: &'a str,
|
||||
}
|
||||
|
||||
pub struct WguiFontSystem {
|
||||
pub system: Mutex<cosmic_text::FontSystem>,
|
||||
}
|
||||
|
||||
impl WguiFontSystem {
|
||||
pub fn new(config: &WguiFontConfig) -> Self {
|
||||
let mut db = cosmic_text::fontdb::Database::new();
|
||||
|
||||
let system = if config.binaries.is_empty() {
|
||||
cosmic_text::FontSystem::new()
|
||||
} else {
|
||||
for binary in &config.binaries {
|
||||
// binary data is copied and preserved here
|
||||
db.load_font_data(binary.to_vec());
|
||||
}
|
||||
|
||||
if !config.family_name_sans_serif.is_empty() {
|
||||
db.set_sans_serif_family(config.family_name_sans_serif);
|
||||
}
|
||||
|
||||
if !config.family_name_serif.is_empty() {
|
||||
db.set_serif_family(config.family_name_serif);
|
||||
}
|
||||
|
||||
// we don't require anything special, at least for now
|
||||
let locale = String::from("C");
|
||||
|
||||
cosmic_text::FontSystem::new_with_locale_and_db(locale, db)
|
||||
};
|
||||
|
||||
Self {
|
||||
system: Mutex::new(system),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use std::{
|
||||
use crate::{
|
||||
assets::{AssetPath, AssetProvider},
|
||||
assets_internal, drawing,
|
||||
font_config::{WguiFontConfig, WguiFontSystem},
|
||||
i18n::I18n,
|
||||
};
|
||||
|
||||
@@ -31,13 +32,18 @@ pub struct Globals {
|
||||
pub assets_builtin: Box<dyn AssetProvider>,
|
||||
pub i18n_builtin: I18n,
|
||||
pub defaults: Defaults,
|
||||
pub font_system: WguiFontSystem,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WguiGlobals(Rc<RefCell<Globals>>);
|
||||
|
||||
impl WguiGlobals {
|
||||
pub fn new(mut assets_builtin: Box<dyn AssetProvider>, defaults: Defaults) -> anyhow::Result<Self> {
|
||||
pub fn new(
|
||||
mut assets_builtin: Box<dyn AssetProvider>,
|
||||
defaults: Defaults,
|
||||
font_config: &WguiFontConfig,
|
||||
) -> anyhow::Result<Self> {
|
||||
let i18n_builtin = I18n::new(&mut assets_builtin)?;
|
||||
let assets_internal = Box::new(assets_internal::AssetInternal {});
|
||||
|
||||
@@ -46,6 +52,7 @@ impl WguiGlobals {
|
||||
assets_builtin,
|
||||
i18n_builtin,
|
||||
defaults,
|
||||
font_system: WguiFontSystem::new(font_config),
|
||||
}))))
|
||||
}
|
||||
|
||||
@@ -81,4 +88,8 @@ impl WguiGlobals {
|
||||
pub fn assets_builtin(&self) -> RefMut<'_, Box<dyn AssetProvider>> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.assets_builtin)
|
||||
}
|
||||
|
||||
pub fn font_system(&self) -> RefMut<'_, WguiFontSystem> {
|
||||
RefMut::map(self.0.borrow_mut(), |x| &mut x.font_system)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ impl I18n {
|
||||
log::info!("Guessed system language: {lang}");
|
||||
|
||||
match lang.as_str() {
|
||||
"en" | "pl" | "it" | "ja" | "es" => {}
|
||||
"en" | "pl" | "it" | "ja" | "es" | "de" => {}
|
||||
_ => {
|
||||
log::warn!("Unsupported language \"{}\", defaulting to \"en\".", lang.as_str());
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ fn add_child_internal(
|
||||
|
||||
pub struct LayoutCommon<'a> {
|
||||
alterables: EventAlterables,
|
||||
layout: &'a mut Layout,
|
||||
pub layout: &'a mut Layout,
|
||||
}
|
||||
|
||||
impl LayoutCommon<'_> {
|
||||
@@ -564,6 +564,8 @@ impl Layout {
|
||||
log::debug!("re-computing layout, size {}x{}", size.x, size.y);
|
||||
self.prev_size = size;
|
||||
|
||||
let globals = self.state.globals.get();
|
||||
|
||||
self.state.tree.compute_layout_with_measure(
|
||||
self.tree_root_node,
|
||||
taffy::Size {
|
||||
@@ -583,7 +585,10 @@ impl Layout {
|
||||
None => taffy::Size::ZERO,
|
||||
Some(h) => {
|
||||
if let Some(w) = self.state.widgets.get(*h) {
|
||||
w.0.borrow_mut().obj.measure(known_dimensions, available_space)
|
||||
w.0
|
||||
.borrow_mut()
|
||||
.obj
|
||||
.measure(&globals, known_dimensions, available_space)
|
||||
} else {
|
||||
taffy::Size::ZERO
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ mod assets_internal;
|
||||
pub mod components;
|
||||
pub mod drawing;
|
||||
pub mod event;
|
||||
pub mod font_config;
|
||||
pub mod gfx;
|
||||
pub mod globals;
|
||||
pub mod i18n;
|
||||
|
||||
@@ -8,7 +8,7 @@ mod widget_rectangle;
|
||||
mod widget_sprite;
|
||||
|
||||
use crate::{
|
||||
assets::{normalize_path, AssetPath, AssetPathOwned},
|
||||
assets::{AssetPath, AssetPathOwned, normalize_path},
|
||||
components::{Component, ComponentWeak},
|
||||
drawing::{self},
|
||||
globals::WguiGlobals,
|
||||
@@ -624,7 +624,7 @@ pub fn replace_vars(input: &str, vars: &HashMap<Rc<str>, Rc<str>>) -> Rc<str> {
|
||||
if let Some(replacement) = vars.get(input_var) {
|
||||
replacement.clone()
|
||||
} else {
|
||||
log::warn!("failed to replace var named \"{input_var}\" (not found)");
|
||||
// failed to find var, return an empty string
|
||||
Rc::from("")
|
||||
}
|
||||
});
|
||||
|
||||
@@ -65,6 +65,7 @@ pub fn parse_text_style(attribs: &[AttribPair]) -> TextStyle {
|
||||
}
|
||||
},
|
||||
"weight" => match value {
|
||||
"light" => style.weight = Some(FontWeight::Light),
|
||||
"normal" => style.weight = Some(FontWeight::Normal),
|
||||
"bold" => style.weight = Some(FontWeight::Bold),
|
||||
_ => {
|
||||
|
||||
@@ -7,13 +7,14 @@ use vulkano::pipeline::graphics::viewport;
|
||||
|
||||
use crate::{
|
||||
drawing::{self},
|
||||
font_config,
|
||||
gfx::{WGfx, cmd::GfxCommandBuffer},
|
||||
};
|
||||
|
||||
use super::{
|
||||
rect::{RectPipeline, RectRenderer},
|
||||
text::{
|
||||
DEFAULT_METRICS, FONT_SYSTEM, SWASH_CACHE, TextArea, TextBounds,
|
||||
DEFAULT_METRICS, SWASH_CACHE, TextArea, TextBounds,
|
||||
text_atlas::{TextAtlas, TextPipeline},
|
||||
text_renderer::TextRenderer,
|
||||
},
|
||||
@@ -51,6 +52,7 @@ impl RendererPass<'_> {
|
||||
|
||||
fn submit(
|
||||
&mut self,
|
||||
font_system: &font_config::WguiFontSystem,
|
||||
gfx: &Arc<WGfx>,
|
||||
viewport: &mut Viewport,
|
||||
cmd_buf: &mut GfxCommandBuffer,
|
||||
@@ -90,7 +92,7 @@ impl RendererPass<'_> {
|
||||
self.rect_renderer.render(gfx, viewport, &vk_scissor, cmd_buf)?;
|
||||
|
||||
{
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let mut font_system = font_system.system.lock();
|
||||
let mut swash_cache = SWASH_CACHE.lock();
|
||||
|
||||
self.text_renderer.prepare(
|
||||
@@ -217,6 +219,7 @@ impl Context {
|
||||
|
||||
pub fn draw(
|
||||
&mut self,
|
||||
font_system: &font_config::WguiFontSystem,
|
||||
shared: &mut SharedContext,
|
||||
cmd_buf: &mut GfxCommandBuffer,
|
||||
primitives: &[drawing::RenderPrimitive],
|
||||
@@ -302,7 +305,13 @@ impl Context {
|
||||
};
|
||||
|
||||
for mut pass in passes {
|
||||
pass.submit(&shared.gfx, &mut self.viewport, cmd_buf, &mut atlas.text_atlas)?;
|
||||
pass.submit(
|
||||
font_system,
|
||||
&shared.gfx,
|
||||
&mut self.viewport,
|
||||
cmd_buf,
|
||||
&mut atlas.text_atlas,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
|
||||
@@ -13,7 +13,6 @@ use parking_lot::Mutex;
|
||||
|
||||
use crate::drawing::{self};
|
||||
|
||||
pub static FONT_SYSTEM: LazyLock<Mutex<FontSystem>> = LazyLock::new(|| Mutex::new(FontSystem::new()));
|
||||
pub static SWASH_CACHE: LazyLock<Mutex<SwashCache>> = LazyLock::new(|| Mutex::new(SwashCache::new()));
|
||||
|
||||
/// Used in case no `font_size` is defined
|
||||
@@ -102,6 +101,7 @@ impl From<FontStyle> for Style {
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
pub enum FontWeight {
|
||||
Light,
|
||||
#[default]
|
||||
Normal,
|
||||
Bold,
|
||||
@@ -110,6 +110,7 @@ pub enum FontWeight {
|
||||
impl From<FontWeight> for Weight {
|
||||
fn from(value: FontWeight) -> Self {
|
||||
match value {
|
||||
FontWeight::Light => Self::LIGHT,
|
||||
FontWeight::Normal => Self::NORMAL,
|
||||
FontWeight::Bold => Self::BOLD,
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ use crate::{
|
||||
drawing::{self, Boundary, PrimitiveExtent},
|
||||
event::CallbackDataCommon,
|
||||
globals::Globals,
|
||||
i18n::{I18n, Translation},
|
||||
i18n::Translation,
|
||||
layout::WidgetID,
|
||||
renderer_vk::text::{TextStyle, FONT_SYSTEM},
|
||||
renderer_vk::text::TextStyle,
|
||||
};
|
||||
|
||||
use super::{WidgetObj, WidgetState};
|
||||
@@ -41,7 +41,7 @@ impl WidgetLabel {
|
||||
|
||||
let mut buffer = Buffer::new_empty(metrics);
|
||||
{
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let mut font_system = globals.font_system.system.lock();
|
||||
let mut buffer = buffer.borrow_with(&mut font_system);
|
||||
buffer.set_wrap(wrap);
|
||||
|
||||
@@ -63,19 +63,19 @@ impl WidgetLabel {
|
||||
|
||||
// set text without layout/re-render update.
|
||||
// Not recommended unless the widget wasn't rendered yet (first init).
|
||||
pub fn set_text_simple(&mut self, i18n: &mut I18n, translation: Translation) -> bool {
|
||||
pub fn set_text_simple(&mut self, globals: &mut Globals, translation: Translation) -> bool {
|
||||
if self.params.content == translation {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.params.content = translation;
|
||||
let attrs = Attrs::from(&self.params.style);
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let mut font_system = globals.font_system.system.lock();
|
||||
|
||||
let mut buffer = self.buffer.borrow_mut();
|
||||
buffer.set_rich_text(
|
||||
&mut font_system,
|
||||
[(self.params.content.generate(i18n).as_ref(), attrs)],
|
||||
[(self.params.content.generate(&mut globals.i18n_builtin).as_ref(), attrs)],
|
||||
&Attrs::new(),
|
||||
Shaping::Advanced,
|
||||
self.params.style.align.map(Into::into),
|
||||
@@ -93,7 +93,9 @@ impl WidgetLabel {
|
||||
|
||||
// set text and check if it needs to be re-rendered/re-layouted
|
||||
pub fn set_text(&mut self, common: &mut CallbackDataCommon, translation: Translation) {
|
||||
if self.set_text_simple(&mut common.i18n(), translation) {
|
||||
let mut globals = common.state.globals.get();
|
||||
|
||||
if self.set_text_simple(&mut globals, translation) {
|
||||
common.mark_widget_dirty(self.id);
|
||||
}
|
||||
}
|
||||
@@ -113,7 +115,7 @@ impl WidgetObj for WidgetLabel {
|
||||
|
||||
if self.last_boundary != boundary {
|
||||
self.last_boundary = boundary;
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let mut font_system = state.globals.font_system.system.lock();
|
||||
let mut buffer = self.buffer.borrow_mut();
|
||||
buffer.set_size(&mut font_system, Some(boundary.size.x), Some(boundary.size.y));
|
||||
}
|
||||
@@ -130,6 +132,7 @@ impl WidgetObj for WidgetLabel {
|
||||
|
||||
fn measure(
|
||||
&mut self,
|
||||
globals: &Globals,
|
||||
known_dimensions: taffy::Size<Option<f32>>,
|
||||
available_space: taffy::Size<taffy::AvailableSpace>,
|
||||
) -> taffy::Size<f32> {
|
||||
@@ -140,7 +143,8 @@ impl WidgetObj for WidgetLabel {
|
||||
AvailableSpace::Definite(width) => Some(width),
|
||||
});
|
||||
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let wgui_font_system = &globals.font_system;
|
||||
let mut font_system = wgui_font_system.system.lock();
|
||||
let mut buffer = self.buffer.borrow_mut();
|
||||
|
||||
buffer.set_size(&mut font_system, width_constraint, None);
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::{
|
||||
EventListenerKind::{self, InternalStateChange, MouseLeave},
|
||||
MouseWheelEvent,
|
||||
},
|
||||
globals::Globals,
|
||||
layout::{Layout, LayoutState, WidgetID},
|
||||
stack::{ScissorStack, TransformStack},
|
||||
};
|
||||
@@ -108,6 +109,7 @@ impl WidgetState {
|
||||
|
||||
// global draw params
|
||||
pub struct DrawState<'a> {
|
||||
pub globals: &'a Globals,
|
||||
pub layout: &'a Layout,
|
||||
pub primitives: &'a mut Vec<RenderPrimitive>,
|
||||
pub transform_stack: &'a mut TransformStack,
|
||||
@@ -151,6 +153,7 @@ pub trait WidgetObj: AnyTrait {
|
||||
|
||||
fn measure(
|
||||
&mut self,
|
||||
_globals: &Globals,
|
||||
_known_dimensions: taffy::Size<Option<f32>>,
|
||||
_available_space: taffy::Size<taffy::AvailableSpace>,
|
||||
) -> taffy::Size<f32> {
|
||||
@@ -181,11 +184,7 @@ impl EventResult {
|
||||
|
||||
#[must_use]
|
||||
pub fn merge(self, other: Self) -> Self {
|
||||
if self > other {
|
||||
self
|
||||
} else {
|
||||
other
|
||||
}
|
||||
if self > other { self } else { other }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@ use slotmap::Key;
|
||||
|
||||
use crate::{
|
||||
drawing::{self, PrimitiveExtent},
|
||||
globals::Globals,
|
||||
layout::WidgetID,
|
||||
renderer_vk::text::{
|
||||
DEFAULT_METRICS,
|
||||
custom_glyph::{CustomGlyph, CustomGlyphData},
|
||||
DEFAULT_METRICS, FONT_SYSTEM,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -67,7 +68,7 @@ impl WidgetObj for WidgetSprite {
|
||||
let mut buffer = Buffer::new_empty(DEFAULT_METRICS);
|
||||
|
||||
{
|
||||
let mut font_system = FONT_SYSTEM.lock();
|
||||
let mut font_system = state.globals.font_system.system.lock();
|
||||
let mut buffer = buffer.borrow_with(&mut font_system);
|
||||
let attrs = Attrs::new().color(Color::rgb(255, 0, 255)).weight(Weight::BOLD);
|
||||
|
||||
@@ -88,6 +89,7 @@ impl WidgetObj for WidgetSprite {
|
||||
|
||||
fn measure(
|
||||
&mut self,
|
||||
_globals: &Globals,
|
||||
_known_dimensions: taffy::Size<Option<f32>>,
|
||||
_available_space: taffy::Size<taffy::AvailableSpace>,
|
||||
) -> taffy::Size<f32> {
|
||||
|
||||
Reference in New Issue
Block a user