Move the fonts out of the Backend trait (#1438)

and remove the `'static`
This commit is contained in:
Olivier Goffart 2022-07-26 16:45:54 +02:00 committed by GitHub
parent 793974ce9f
commit 8c70cd7f57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 167 additions and 251 deletions

View file

@ -194,6 +194,36 @@ public:
cbindgen_private::slint_windowrc_set_size(&inner, &size);
}
/// Registers a font by the specified path. The path must refer to an existing
/// TrueType font.
/// \returns an empty optional on success, otherwise an error string
inline std::optional<SharedString> register_font_from_path(const SharedString &path)
{
SharedString maybe_err;
cbindgen_private::slint_register_font_from_path(&inner, &path, &maybe_err);
if (!maybe_err.empty()) {
return maybe_err;
} else {
return {};
}
}
/// Registers a font by the data. The data must be valid TrueType font data.
/// \returns an empty optional on success, otherwise an error string
inline std::optional<SharedString> register_font_from_data(const uint8_t *data, std::size_t len)
{
SharedString maybe_err;
cbindgen_private::slint_register_font_from_data(&inner, { const_cast<uint8_t *>(data), len },
&maybe_err);
if (!maybe_err.empty()) {
return maybe_err;
} else {
return {};
}
}
private:
cbindgen_private::WindowRcOpaque inner;
};
@ -1106,36 +1136,4 @@ auto blocking_invoke_from_event_loop(Functor f) -> void
cv.wait(lock, [&] { return ok; });
}
namespace private_api {
/// Registers a font by the specified path. The path must refer to an existing
/// TrueType font.
/// \returns an empty optional on success, otherwise an error string
inline std::optional<SharedString> register_font_from_path(const SharedString &path)
{
SharedString maybe_err;
cbindgen_private::slint_register_font_from_path(&path, &maybe_err);
if (!maybe_err.empty()) {
return maybe_err;
} else {
return {};
}
}
/// Registers a font by the data. The data must be valid TrueType font data.
/// \returns an empty optional on success, otherwise an error string
inline std::optional<SharedString> register_font_from_data(const uint8_t *data, std::size_t len)
{
SharedString maybe_err;
cbindgen_private::slint_register_font_from_data({ const_cast<uint8_t *>(data), len },
&maybe_err);
if (!maybe_err.empty()) {
return maybe_err;
} else {
return {};
}
}
}
} // namespace slint

View file

@ -6,7 +6,7 @@
use core::ffi::c_void;
use i_slint_backend_selector::backend;
use i_slint_core::api::Window;
use i_slint_core::window::ffi::WindowRcOpaque;
use i_slint_core::window::{ffi::WindowRcOpaque, WindowRc};
#[doc(hidden)]
#[cold]
@ -63,12 +63,14 @@ pub unsafe extern "C" fn slint_quit_event_loop() {
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_path(
win: *const WindowRcOpaque,
path: &i_slint_core::SharedString,
error_str: *mut i_slint_core::SharedString,
) {
let window = &*(win as *const WindowRc);
core::ptr::write(
error_str,
match crate::backend().register_font_from_path(std::path::Path::new(path.as_str())) {
match window.renderer().register_font_from_path(std::path::Path::new(path.as_str())) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
@ -77,12 +79,14 @@ pub unsafe extern "C" fn slint_register_font_from_path(
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_data(
win: *const WindowRcOpaque,
data: i_slint_core::slice::Slice<'static, u8>,
error_str: *mut i_slint_core::SharedString,
) {
let window = &*(win as *const WindowRc);
core::ptr::write(
error_str,
match crate::backend().register_font_from_memory(data.as_slice()) {
match window.renderer().register_font_from_memory(data.as_slice()) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},