From 3c874f7c82d2e00470d4af725632e90fffc3f628 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Mon, 3 Jul 2023 17:02:41 +0200 Subject: [PATCH 1/4] again --- experimental/harfbuzz/Cargo.toml | 5 +- experimental/harfbuzz/src/lib.rs | 96 ++++++++++++++++---------------- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/experimental/harfbuzz/Cargo.toml b/experimental/harfbuzz/Cargo.toml index c53bec1cd2e..7f8a865d7f2 100644 --- a/experimental/harfbuzz/Cargo.toml +++ b/experimental/harfbuzz/Cargo.toml @@ -42,8 +42,8 @@ denylist = ["bench", "serde", "logging"] [dependencies] harfbuzz-sys = { version = "0.5.0" } -icu_normalizer = { version = "1.0.0", path = "../../components/normalizer/" } -icu_properties = { version = "1.0.0", path = "../../components/properties" } +icu_normalizer = { version = "1.2.0", path = "../../components/normalizer/", default-features = false } +icu_properties = { version = "1.2.0", path = "../../components/properties", default-features = false } icu_provider = { version = "1.0.0", path = "../../provider/core", features = ["macros"] } displaydoc = { version = "0.2.3", default-features = false } tinystr = { version = "0.7.1", path = "../../utils/tinystr", default-features = false } @@ -54,6 +54,7 @@ harfbuzz = { version = "0.4.0" } [features] default = [] +compiled_data = ["icu_normalizer/compiled_data", "icu_properties/compiled_data"] # TODO(#3275) Make this no_std again # std = [] serde = ["icu_properties/serde", "icu_normalizer/serde", "icu_provider/serde"] diff --git a/experimental/harfbuzz/src/lib.rs b/experimental/harfbuzz/src/lib.rs index 9e0c4a0968a..de965ce7a79 100644 --- a/experimental/harfbuzz/src/lib.rs +++ b/experimental/harfbuzz/src/lib.rs @@ -280,6 +280,12 @@ unsafe extern "C" fn icu4x_hb_unicode_decompose_destroy(user_data: *mut c_void) } /// RAII holder for `*mut hb_unicode_funcs_t`. +// TODO(#2838): Document the conditions under which multithreaded lookups via the +// `*mut hb_unicode_funcs_t` are OK. HarfBuzz itself takes care of atomic +// refcounting of the `hb_unicode_funcs_t`, but if `DataPayload` is built +// without the `sync` feature, do bad things still happen if the freeing +// thread doesn't match the allocation thread? At least the trait bounds +// are violated in principle. #[derive(Debug)] pub struct UnicodeFuncs { raw: *mut hb_unicode_funcs_t, @@ -325,12 +331,20 @@ impl Drop for UnicodeFuncs { /// Sets up a `hb_unicode_funcs_t` with ICU4X as the back end as the Unicode /// Database operations that HarfBuzz needs. The `hb_unicode_funcs_t` held /// by the returned `UnicodeFuncs` is marked immutable. -// TODO(#2838): Document the conditions under which multithreaded lookups via the -// `*mut hb_unicode_funcs_t` are OK. HarfBuzz itself takes care of atomic -// refcounting of the `hb_unicode_funcs_t`, but if `DataPayload` is built -// without the `sync` feature, do bad things still happen if the freeing -// thread doesn't match the allocation thread? At least the trait bounds -// are violated in principle. +#[cfg(feature = "compiled_data")] +pub fn new_hb_unicode_funcs() -> Result { + create_ufuncs( + CanonicalCombiningClassMap::new(), + icu_properties::maps::general_category().static_to_owned(), + icu_properties::bidi_data::bidi_auxiliary_properties().static_to_owned(), + icu_properties::maps::script().static_to_owned(), + Script::enum_to_short_name_mapper().static_to_owned(), + CanonicalComposition::new(), + CanonicalDecomposition::new(), + ) +} + +#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, new_hb_unicode_funcs)] pub fn new_hb_unicode_funcs_unstable(data_provider: &D) -> Result where D: DataProvider @@ -343,25 +357,26 @@ where + DataProvider + ?Sized, { - // Let's do all the provider operations up front so that if there's - // a provider failure we can return early without having to clean up - // an already-allocated `ufuncs`. - let canonical_combining_class_map = - Box::new(CanonicalCombiningClassMap::try_new_unstable(data_provider)?); - let general_category_map = - Box::new(icu_properties::maps::load_general_category(data_provider)?); - let bidi_auxiliary_props_map = Box::new( + create_ufuncs( + CanonicalCombiningClassMap::try_new_unstable(data_provider)?, + icu_properties::maps::load_general_category(data_provider)?, icu_properties::bidi_data::load_bidi_auxiliary_properties_unstable(data_provider)?, - ); - let script_map = icu_properties::maps::load_script(data_provider)?; - let script_enum_to_short_name_lookup = Script::get_enum_to_short_name_mapper(data_provider)?; - let script_data = Box::new(ScriptDataForHarfBuzz { - script_map, - enum_to_name_mapper: script_enum_to_short_name_lookup, - }); - let canonical_composition = Box::new(CanonicalComposition::try_new_unstable(data_provider)?); - let canonical_decomposition = - Box::new(CanonicalDecomposition::try_new_unstable(data_provider)?); + icu_properties::maps::load_script(data_provider)?, + Script::get_enum_to_short_name_mapper(data_provider)?, + CanonicalComposition::try_new_unstable(data_provider)?, + CanonicalDecomposition::try_new_unstable(data_provider)?, + ) +} + +fn create_ufuncs( + canonical_combining_class_map: CanonicalCombiningClassMap, + general_category_map: CodePointMapData, + bidi_auxiliary_props_map: BidiAuxiliaryProperties, + script_map: CodePointMapData