Remove the public API for registering fonts by path or memory chunk

This is not needed anymore in the light of being able to write `import "blah.ttf"` in .60 markup
This commit is contained in:
Simon Hausmann 2021-04-14 10:01:33 +02:00
parent cb70f58990
commit afe3df6381
8 changed files with 8 additions and 60 deletions

View file

@ -687,6 +687,8 @@ inline void quit_event_loop()
cbindgen_private::sixtyfps_quit_event_loop(); cbindgen_private::sixtyfps_quit_event_loop();
} }
namespace private_api {
/// Registers a font by the specified path. The path must refer to an existing /// Registers a font by the specified path. The path must refer to an existing
/// TrueType font font. /// TrueType font font.
/// \returns an empty optional on success, otherwise an error string /// \returns an empty optional on success, otherwise an error string
@ -701,4 +703,6 @@ inline std::optional<SharedString> register_font_from_path(const SharedString &p
} }
} }
}
} // namespace sixtyfps } // namespace sixtyfps

View file

@ -234,5 +234,4 @@ module.exports = {
Timer: { Timer: {
singleShot: native.singleshot_timer, singleShot: native.singleshot_timer,
}, },
register_font_from_path: native.register_font_from_path,
}; };

View file

@ -527,22 +527,10 @@ fn singleshot_timer(mut cx: FunctionContext) -> JsResult<JsValue> {
Ok(JsUndefined::new().upcast()) Ok(JsUndefined::new().upcast())
} }
fn register_font_from_path(mut cx: FunctionContext) -> JsResult<JsValue> {
let path = cx.argument::<JsString>(0)?.value();
match sixtyfps_interpreter::register_font_from_path(&path) {
Ok(_) => Ok(JsUndefined::new().upcast()),
Err(load_err) => {
return cx.throw_error(format!("error loading font {}: {}", path, load_err));
}
}
}
register_module!(mut m, { register_module!(mut m, {
m.export_function("load", load)?; m.export_function("load", load)?;
m.export_function("mock_elapsed_time", mock_elapsed_time)?; m.export_function("mock_elapsed_time", mock_elapsed_time)?;
m.export_function("singleshot_timer", singleshot_timer)?; m.export_function("singleshot_timer", singleshot_timer)?;
m.export_function("register_font_from_path", register_font_from_path)?;
Ok(()) Ok(())
}); });

View file

@ -174,6 +174,7 @@ pub use sixtyfps_corelib::{Brush, Color, RgbaColor};
/// This function can be used to register a custom TrueType font with SixtyFPS, /// 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 /// for use with the `font-family` property. The provided slice must be a valid TrueType
/// font. /// font.
#[doc(hidden)]
pub fn register_font_from_memory(data: &'static [u8]) -> Result<(), Box<dyn std::error::Error>> { pub fn register_font_from_memory(data: &'static [u8]) -> Result<(), Box<dyn std::error::Error>> {
sixtyfps_rendering_backend_default::backend().register_font_from_memory(data) sixtyfps_rendering_backend_default::backend().register_font_from_memory(data)
} }
@ -181,6 +182,7 @@ pub fn register_font_from_memory(data: &'static [u8]) -> Result<(), Box<dyn std:
/// This function can be used to register a custom TrueType font with SixtyFPS, /// This function can be used to register a custom TrueType font with SixtyFPS,
/// for use with the `font-family` property. The provided path must refer to a valid TrueType /// for use with the `font-family` property. The provided path must refer to a valid TrueType
/// font. /// font.
#[doc(hidden)]
pub fn register_font_from_path<P: AsRef<std::path::Path>>( pub fn register_font_from_path<P: AsRef<std::path::Path>>(
path: P, path: P,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {

View file

@ -119,29 +119,3 @@ impl WrappedCompiledComp {
component.run(); component.run();
} }
} }
/// Downloads the font from the specified url and registers it as a font
/// for use in text elements.
#[wasm_bindgen]
pub async fn register_font(url: String) -> Result<(), JsValue> {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let request = Request::new_with_str_and_init(&url, &opts)?;
let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
let resp: Response = resp_value.dyn_into().unwrap();
let data = js_sys::Uint8Array::new(&JsFuture::from(resp.array_buffer()?).await?);
let data = data.to_vec();
sixtyfps_interpreter::register_font_from_memory(&data).unwrap();
Ok(())
}

View file

@ -1585,7 +1585,7 @@ fn compile_expression(
panic!("internal error: incorrect argument count to RegisterCustomFontByPath call"); panic!("internal error: incorrect argument count to RegisterCustomFontByPath call");
} }
if let Expression::StringLiteral(font_path) = &arguments[0] { if let Expression::StringLiteral(font_path) = &arguments[0] {
format!("sixtyfps::register_font_from_path(\"{}\");", font_path) format!("sixtyfps::private_api::register_font_from_path(\"{}\");", font_path)
} else { } else {
panic!("internal error: argument to RegisterCustomFontByPath must be a string literal") panic!("internal error: argument to RegisterCustomFontByPath must be a string literal")
} }

View file

@ -87,19 +87,12 @@ pub use api::*;
/// This function can be used to register a custom TrueType font with SixtyFPS, /// This function can be used to register a custom TrueType font with SixtyFPS,
/// for use with the `font-family` property. The provided path must refer to a valid TrueType /// for use with the `font-family` property. The provided path must refer to a valid TrueType
/// font. /// font.
pub fn register_font_from_path<P: AsRef<std::path::Path>>( pub(crate) fn register_font_from_path<P: AsRef<std::path::Path>>(
path: P, path: P,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
sixtyfps_rendering_backend_default::backend().register_font_from_path(path.as_ref()) sixtyfps_rendering_backend_default::backend().register_font_from_path(path.as_ref())
} }
/// 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_font_from_memory(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
sixtyfps_rendering_backend_default::backend().register_font_from_memory(data)
}
/// (Re-export from corelib.) /// (Re-export from corelib.)
#[doc(inline)] #[doc(inline)]
pub use sixtyfps_corelib::{Brush, Color, SharedString, SharedVector}; pub use sixtyfps_corelib::{Brush, Color, SharedString, SharedVector};

View file

@ -31,10 +31,6 @@ struct Cli {
#[structopt(long, name = "style name", default_value)] #[structopt(long, name = "style name", default_value)]
style: String, style: String,
/// An optionally registered font
#[structopt(long, name = "load font")]
load_font: Option<Vec<String>>,
/// Automatically watch the file system, and reload when it changes /// Automatically watch the file system, and reload when it changes
#[structopt(long)] #[structopt(long)]
auto_reload: bool, auto_reload: bool,
@ -45,14 +41,6 @@ thread_local! {static CURRENT_INSTANCE: std::cell::RefCell<Option<ComponentInsta
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Cli::from_args(); let args = Cli::from_args();
args.load_font.as_ref().map(|fonts| {
fonts.iter().for_each(|font_path| {
if let Err(app_font_err) = sixtyfps_interpreter::register_font_from_path(&font_path) {
eprintln!("Error loading app font {}: {}", font_path, app_font_err);
}
});
});
let fswatcher = if args.auto_reload { Some(start_fswatch_thread(args.clone())?) } else { None }; let fswatcher = if args.auto_reload { Some(start_fswatch_thread(args.clone())?) } else { None };
let mut compiler = init_compiler(&args, fswatcher); let mut compiler = init_compiler(&args, fswatcher);