Move the entire custom application font handling into the backend

Some backends may not require a copy of the data, for example.
This commit is contained in:
Simon Hausmann 2021-01-07 11:49:34 +01:00
parent 1ed35fb628
commit edb17f996f
5 changed files with 22 additions and 20 deletions

View file

@ -152,7 +152,6 @@ struct MyStruct {
pub use sixtyfps_macros::sixtyfps;
pub use sixtyfps_corelib::graphics::register_application_font_from_memory;
pub use sixtyfps_corelib::model::{
Model, ModelHandle, ModelNotify, ModelPeer, StandardListViewItem, VecModel,
};
@ -160,6 +159,7 @@ pub use sixtyfps_corelib::sharedvector::SharedVector;
pub use sixtyfps_corelib::string::SharedString;
pub use sixtyfps_corelib::timers::{Timer, TimerMode};
pub use sixtyfps_corelib::{Color, RgbaColor};
pub use sixtyfps_rendering_backend_default::register_application_font_from_memory;
// FIXME: this should not be in this namespace
// but the name is `sixtyfps::StateInfo` in builtin.60

View file

@ -43,7 +43,6 @@ auto_enums = "0.7"
stretch = "0.3.2"
weak-table = "0.3"
femtovg = { git = "https://github.com/femtovg/femtovg", branch = "master", optional = true }
fontdb = "0.5.1"
[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = [ "wasm-bindgen", "now" ] }

View file

@ -28,7 +28,7 @@ use crate::{Callback, SharedString};
use auto_enums::auto_enum;
use const_field_offset::FieldOffsets;
use sixtyfps_corelib_macros::*;
use std::{cell::RefCell, rc::Rc};
use std::rc::Rc;
/// 2D Rectangle
pub type Rect = euclid::default::Rect<f32>;
@ -687,18 +687,3 @@ pub(crate) mod ffi {
core::ptr::write(out_coordinates as *mut crate::SharedVector<Point>, coordinates.clone());
}
}
thread_local! {
/// Database used to keep track of fonts added by the application
pub static APPLICATION_FONTS: RefCell<fontdb::Database> = RefCell::new(fontdb::Database::new())
}
/// This function can be used to register a custom TrueType font with SixtyFPS,
/// for use with the `font-family` property. The provided slice must be a valid TrueType
/// font.
pub fn register_application_font_from_memory(
data: &'static [u8],
) -> Result<(), Box<dyn std::error::Error>> {
APPLICATION_FONTS.with(|fontdb| fontdb.borrow_mut().load_font_data(data.into()));
Ok(())
}

View file

@ -45,7 +45,10 @@ pub fn create_window() -> ComponentWindow {
default_backend::create_window()
}
pub use default_backend::{native_widgets, NativeGlobals, NativeWidgets, HAS_NATIVE_STYLE};
pub use default_backend::{
native_widgets, register_application_font_from_memory, NativeGlobals, NativeWidgets,
HAS_NATIVE_STYLE,
};
#[doc(hidden)]
#[cold]

View file

@ -58,6 +58,21 @@ impl Default for FontDatabase {
}
}
thread_local! {
/// Database used to keep track of fonts added by the application
pub static APPLICATION_FONTS: RefCell<fontdb::Database> = RefCell::new(fontdb::Database::new())
}
/// This function can be used to register a custom TrueType font with SixtyFPS,
/// for use with the `font-family` property. The provided slice must be a valid TrueType
/// font.
pub fn register_application_font_from_memory(
data: &'static [u8],
) -> Result<(), Box<dyn std::error::Error>> {
APPLICATION_FONTS.with(|fontdb| fontdb.borrow_mut().load_font_data(data.into()));
Ok(())
}
fn try_load_app_font(canvas: &CanvasRc, request: &FontRequest) -> Option<GLFont> {
let family = if request.family.is_empty() {
fontdb::Family::SansSerif
@ -69,7 +84,7 @@ fn try_load_app_font(canvas: &CanvasRc, request: &FontRequest) -> Option<GLFont>
weight: fontdb::Weight(request.weight as u16),
..Default::default()
};
sixtyfps_corelib::graphics::APPLICATION_FONTS.with(|font_db| {
APPLICATION_FONTS.with(|font_db| {
let font_db = font_db.borrow();
font_db.query(&query).and_then(|id| font_db.face_source(id)).map(|(source, _index)| {
GLFont {