diff --git a/meson.build b/meson.build index c7edf8a6f..f4806ad49 100644 --- a/meson.build +++ b/meson.build @@ -205,6 +205,12 @@ if get_option('fontations').enabled() fontations_dep_found = true endif +harfruzz_dep_found = false +if get_option('harfruzz').enabled() + add_languages(['rust'], native: false, required : true) + harfruzz_dep_found = true +endif + conf = configuration_data() incconfig = include_directories('.') @@ -250,6 +256,10 @@ if fontations_dep_found conf.set('HAVE_FONTATIONS', 1) endif +if harfruzz_dep_found + conf.set('HAVE_HARFRUZZ', 1) +endif + if wasm_dep.found() conf.set('HAVE_WASM', 1) conf.set('HB_WASM_MODULE_DIR', '"'+get_option('prefix')+'/'+get_option('libdir')+'/harfbuzz/wasm"') @@ -481,6 +491,7 @@ build_summary = { {'CoreText': conf.get('HAVE_CORETEXT', 0) == 1, 'DirectWrite (experimental)': conf.get('HAVE_DIRECTWRITE', 0) == 1, 'GDI/Uniscribe': (conf.get('HAVE_GDI', 0) == 1) and (conf.get('HAVE_UNISCRIBE', 0) == 1), + 'harfRuzz': conf.get('HAVE_HARFRUZZ', 0) == 1, }, 'Other features': {'Documentation': conf.get('HAVE_GTK_DOC', 0) == 1, diff --git a/meson_options.txt b/meson_options.txt index ba13b92a1..4008e0036 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -18,11 +18,13 @@ option('freetype', type: 'feature', value: 'auto', option('fontations', type: 'feature', value: 'disabled', description: 'Enable fontations font backend') option('gdi', type: 'feature', value: 'disabled', - description: 'Enable GDI helpers and Uniscribe shaper backend (Windows only)') + description: 'Enable GDI helpers and Uniscribe shaper backend on Windows') option('directwrite', type: 'feature', value: 'disabled', description: 'Enable DirectWrite shaper and font backend on Windows') option('coretext', type: 'feature', value: 'disabled', description: 'Enable CoreText shaper and font backend on Apple platforms') +option('harfruzz', type: 'feature', value: 'disabled', + description: 'Enable harfRuzz shaper backend') option('wasm', type: 'feature', value: 'disabled', description: 'Enable WebAssembly shaper backend (experimental)') diff --git a/src/hb-shaper-list.hh b/src/hb-shaper-list.hh index f079caf4d..dc494d236 100644 --- a/src/hb-shaper-list.hh +++ b/src/hb-shaper-list.hh @@ -57,6 +57,10 @@ HB_SHAPER_IMPLEMENT (directwrite) HB_SHAPER_IMPLEMENT (coretext) #endif +#ifdef HAVE_HARFRUZZ +HB_SHAPER_IMPLEMENT (harfruzz) +#endif + #ifndef HB_NO_FALLBACK_SHAPE HB_SHAPER_IMPLEMENT (fallback) /* <--- This should be last. */ #endif diff --git a/src/meson.build b/src/meson.build index d62b9d9d4..cbe5d9e74 100644 --- a/src/meson.build +++ b/src/meson.build @@ -338,6 +338,9 @@ hb_ft_headers = files('hb-ft.h') hb_fontations_sources = files() hb_fontations_headers = files('hb-fontations.h') +hb_harfruzz_sources = files('hb-harfruzz.cc') +hb_harfruzz_headers = files() + hb_glib_sources = files('hb-glib.cc') hb_glib_headers = files('hb-glib.h') @@ -529,13 +532,19 @@ if conf.get('HAVE_CORETEXT', 0) == 1 harfbuzz_deps += coretext_deps endif -if conf.get('HAVE_FONTATIONS', 0) == 1 - +if conf.get('HAVE_FONTATIONS', 0) == 1 or conf.get('HAVE_HARFRUZZ', 0) == 1 subdir('rust') + harfbuzz_deps += [harfbuzz_rust_dep] +endif +if conf.get('HAVE_FONTATIONS', 0) == 1 hb_sources += hb_fontations_sources hb_headers += hb_fontations_headers - harfbuzz_deps += [harfbuzz_rust_dep] +endif + +if conf.get('HAVE_HARFRUZZ', 0) == 1 + hb_sources += hb_harfruzz_sources + hb_headers += hb_harfruzz_headers endif have_icu = conf.get('HAVE_ICU', 0) == 1 diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index c16f3c21d..866965fc7 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -3,19 +3,25 @@ version = 4 [[package]] -name = "bytemuck" -version = "1.22.0" +name = "bitflags" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6b1fc10dbac614ebc03540c9dbd60e83887fda27794998c6528f1782047d540" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "bytemuck" +version = "1.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9134a6ef01ce4b366b50689c94f82c14bc72bc5d0386829828a2e2752ef7958c" dependencies = [ "bytemuck_derive", ] [[package]] name = "bytemuck_derive" -version = "1.8.1" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fa76293b4f7bb636ab88fd78228235b5248b4d05cc589aed610f954af5d7c7a" +checksum = "7ecc273b49b3205b83d648f0690daa588925572cc5063745bfe547fe7ec8e1a1" dependencies = [ "proc-macro2", "quote", @@ -23,10 +29,19 @@ dependencies = [ ] [[package]] -name = "font-types" -version = "0.8.3" +name = "core_maths" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d868ec188a98bb014c606072edd47e52e7ab7297db943b0b28503121e1d037bd" +checksum = "77745e017f5edba1a9c1d854f6f3a52dac8a12dd5af5d2f54aecf61e43d80d30" +dependencies = [ + "libm", +] + +[[package]] +name = "font-types" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa6a5e5a77b5f3f7f9e32879f484aa5b3632ddfbe568a16266c904a6f32cdaf" dependencies = [ "bytemuck", ] @@ -35,61 +50,132 @@ dependencies = [ name = "harfbuzz_rust" version = "0.0.0" dependencies = [ + "harfruzz", "read-fonts", "skrifa", ] [[package]] -name = "proc-macro2" -version = "1.0.94" +name = "harfruzz" +version = "0.0.1" +source = "git+https://github.com/harfbuzz/harfruzz#006472176ab87e3a84e799e74e0ac19fbe943dd7" +dependencies = [ + "bitflags", + "bytemuck", + "core_maths", + "log", + "read-fonts", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + +[[package]] +name = "libm" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +checksum = "a25169bd5913a4b437588a7e3d127cd6e90127b60e0ffbd834a38f1599e016b8" + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "read-fonts" -version = "0.27.5" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f14974c88fb4fd0a7203719f98020209248c9dbebaf9d10d860337797a905097" +checksum = "600e807b48ac55bad68a8cb75cc3c7739f139b9248f7e003e01e080f589b5288" dependencies = [ "bytemuck", + "core_maths", "font-types", ] [[package]] name = "skrifa" -version = "0.29.2" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c0ca53de9bb9bee1720c727606275148463cd938eb6bde249dcedeec4967747" +checksum = "6fa1e5622e4f7b98877e8a19890efddcac1230cec6198bd9de91ec0e00010dc8" dependencies = [ "bytemuck", "read-fonts", ] [[package]] -name = "syn" -version = "2.0.99" +name = "smallvec" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" + +[[package]] +name = "syn" +version = "2.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "ttf-parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" +dependencies = [ + "core_maths", +] + +[[package]] +name = "unicode-bidi-mirroring" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfa6e8c60bb66d49db113e0125ee8711b7647b5579dc7f5f19c42357ed039fe" + +[[package]] +name = "unicode-ccc" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce61d488bcdc9bc8b5d1772c404828b17fc481c0a582b5581e95fb233aef503e" + [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "unicode-properties" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" + +[[package]] +name = "unicode-script" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb421b350c9aff471779e262955939f565ec18b86c15364e6bdf0d662ca7c1f" diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index d19d8442a..d5764e2bb 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -3,8 +3,9 @@ name = "harfbuzz_rust" edition = "2021" [dependencies] -read-fonts = { version = "0.27", optional = true } -skrifa = { version = "0.29.2", optional = true } +read-fonts = { version = "0.*", optional = true } +skrifa = { version = "0.*", optional = true } +harfruzz = { git = "https://github.com/harfbuzz/harfruzz", optional = true } [lib] name = "harfbuzz_rust" @@ -13,6 +14,7 @@ crate-type = ["staticlib"] [features] font = ["dep:read-fonts", "dep:skrifa"] +shape = ["dep:harfruzz"] [profile.release] strip = true diff --git a/src/rust/font.rs b/src/rust/font.rs index 6cfa6647d..fc2d5ff9c 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -1,7 +1,4 @@ -#![allow(non_camel_case_types)] -#![allow(non_upper_case_globals)] -#![allow(dead_code)] -include!(concat!(env!("OUT_DIR"), "/hb.rs")); +use super::hb::*; use std::alloc::{GlobalAlloc, Layout}; use std::collections::HashMap; @@ -1036,10 +1033,10 @@ extern "C" fn _hb_fontations_glyph_from_name( } fn _hb_fontations_font_funcs_get() -> *mut hb_font_funcs_t { - static static_ffuncs: AtomicPtr = AtomicPtr::new(null_mut()); + static STATIC_FFUNCS: AtomicPtr = AtomicPtr::new(null_mut()); loop { - let mut ffuncs = static_ffuncs.load(Ordering::Acquire); + let mut ffuncs = STATIC_FFUNCS.load(Ordering::Acquire); if !ffuncs.is_null() { return ffuncs; @@ -1116,7 +1113,7 @@ fn _hb_fontations_font_funcs_get() -> *mut hb_font_funcs_t { ); } - if (static_ffuncs.compare_exchange(null_mut(), ffuncs, Ordering::SeqCst, Ordering::Relaxed)) + if (STATIC_FFUNCS.compare_exchange(null_mut(), ffuncs, Ordering::SeqCst, Ordering::Relaxed)) == Ok(null_mut()) { return ffuncs; diff --git a/src/rust/hb.rs b/src/rust/hb.rs new file mode 100644 index 000000000..0166694f2 --- /dev/null +++ b/src/rust/hb.rs @@ -0,0 +1,4 @@ +#![allow(non_camel_case_types)] +#![allow(non_upper_case_globals)] +#![allow(dead_code)] +include!(concat!(env!("OUT_DIR"), "/hb.rs")); diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 415da822b..7426b64b3 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -1,4 +1,11 @@ +mod hb; + #[cfg(feature = "font")] mod font; #[cfg(feature = "font")] pub use font::hb_fontations_font_set_funcs; + +#[cfg(feature = "shape")] +mod shape; +#[cfg(feature = "shape")] +pub use shape::_hb_harfruzz_shape; diff --git a/src/rust/meson.build b/src/rust/meson.build index 884b0844d..6ab16ba37 100644 --- a/src/rust/meson.build +++ b/src/rust/meson.build @@ -23,8 +23,15 @@ cargo_args = [ '-Z', 'build-std-features=panic_immediate_abort', ] +features = [] if conf.get('HAVE_FONTATIONS', 0) == 1 - cargo_args += ['--features', 'font'] + features += ['font'] +endif +if conf.get('HAVE_HARFRUZZ', 0) == 1 + features += ['shape'] +endif +if features.length() > 0 + cargo_args += ['--features', ','.join(features)] endif buildtype = get_option('buildtype') @@ -38,6 +45,7 @@ rust_flags += ' -C opt-level=' + opt_level sources = [ 'lib.rs', 'font.rs', + 'shape.rs', ] harfbuzz_rust = custom_target( diff --git a/src/rust/shape.rs b/src/rust/shape.rs new file mode 100644 index 000000000..ef0eceaf2 --- /dev/null +++ b/src/rust/shape.rs @@ -0,0 +1,106 @@ +use super::hb::*; + +use std::os::raw::c_void; +use std::ptr::null_mut; + +use harfruzz::{FontRef, ShaperFont}; + +pub struct HBHarfRuzzFaceData<'a> { + face_blob: *mut hb_blob_t, + font_ref: FontRef<'a>, + shaper_font: ShaperFont, +} + +#[no_mangle] +pub unsafe extern "C" fn _hb_harfruzz_shaper_face_data_create_rs( + face: *mut hb_face_t, +) -> *mut c_void { + let face_index = hb_face_get_index(face); + let face_blob = hb_face_reference_blob(face); + let blob_length = hb_blob_get_length(face_blob); + let blob_data = hb_blob_get_data(face_blob, null_mut()); + let face_data = std::slice::from_raw_parts(blob_data as *const u8, blob_length as usize); + + let font_ref = FontRef::from_index(face_data, face_index) + .expect("FontRef::from_index should succeed on valid HarfBuzz face data"); + let shaper_font = ShaperFont::new(&font_ref); + + let hr_face_data = HBHarfRuzzFaceData { + face_blob, + font_ref, + shaper_font, + }; + let hr_face_data_ptr = Box::into_raw(Box::new(hr_face_data)); + hr_face_data_ptr as *mut c_void +} + +#[no_mangle] +pub unsafe extern "C" fn _hb_harfruzz_shaper_face_data_destroy_rs(data: *mut c_void) { + // The data pointer is expected to be a pointer to HBHarfRuzzFaceData + let data = data as *mut HBHarfRuzzFaceData; + let hr_face_data = Box::from_raw(data); + let blob = hr_face_data.face_blob; + hb_blob_destroy(blob); + // The hr_font is automatically cleaned up when it goes out of scope +} + +#[no_mangle] +pub unsafe extern "C" fn _hb_harfruzz_shape_rs( + data: *const c_void, + _font: *mut hb_font_t, + buffer: *mut hb_buffer_t, + _features: *const hb_feature_t, + _num_features: u32, +) -> hb_bool_t { + // The data pointer is expected to be a pointer to HBHarfRuzzFaceData + let data = data as *const HBHarfRuzzFaceData; + + let mut hr_buffer = harfruzz::UnicodeBuffer::new(); + + // Set buffer properties + // XXX + + // Populate buffer + let count = hb_buffer_get_length(buffer); + let infos = hb_buffer_get_glyph_infos(buffer, null_mut()); + + for i in 0..count { + let info = infos.add(i as usize); + let unicode = (*info).codepoint; + let cluster = (*info).cluster; + hr_buffer.add(char::from_u32_unchecked(unicode), cluster); + } + + let font_ref = &(*data).font_ref; + let shaper_font = &(*data).shaper_font; + let shaper = shaper_font.shaper(font_ref, &[]); + + let glyphs = harfruzz::shape(&shaper, &[], hr_buffer); + + let count = glyphs.len(); + hb_buffer_set_content_type( + buffer, + hb_buffer_content_type_t_HB_BUFFER_CONTENT_TYPE_GLYPHS, + ); + hb_buffer_set_length(buffer, count as u32); + let infos = hb_buffer_get_glyph_infos(buffer, null_mut()); + let positions = hb_buffer_get_glyph_positions(buffer, null_mut()); + + for (i, (hr_info, hr_pos)) in glyphs + .glyph_infos() + .iter() + .zip(glyphs.glyph_positions()) + .enumerate() + { + let info = infos.add(i); + let pos = positions.add(i); + (*info).codepoint = hr_info.glyph_id; + (*info).cluster = hr_info.cluster; + (*pos).x_advance = hr_pos.x_advance; + (*pos).y_advance = hr_pos.y_advance; + (*pos).x_offset = hr_pos.x_offset; + (*pos).y_offset = hr_pos.y_offset; + } + + true as hb_bool_t +}