mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 07:51:10 +01:00
Merge pull request #6318 from harfbuzz/hr-new-api
[rust] Update Fontations and HarfRust integrations
This commit is contained in:
6 files changed
+998
-397
No files matched your search
+12
-8
@@ -4,11 +4,11 @@ edition = "2021"
|
||||
rust-version = "1.87.0"
|
||||
|
||||
[dependencies]
|
||||
harfrust = { version = "0.13.3", features = ["experimental_font_api"], optional = true }
|
||||
harfrust = { version = "0.14.0", optional = true }
|
||||
# harfrust = { git = "https://github.com/harfbuzz/harfrust", optional = true }
|
||||
read-fonts = "0.43.*"
|
||||
skrifa = { version = "0.46.*", optional = true }
|
||||
smallvec = "1.*"
|
||||
read-fonts = { version = "0.45.*", optional = true }
|
||||
skrifa = { version = "0.48.*", optional = true, default-features = false, features = ["std"] }
|
||||
smallvec = { version = "1.*", optional = true }
|
||||
|
||||
[lib]
|
||||
name = "harfbuzz_rust"
|
||||
@@ -17,10 +17,14 @@ crate-type = ["staticlib"]
|
||||
|
||||
[features]
|
||||
default = ["paint", "draw", "hb-allocator"]
|
||||
font = ["dep:skrifa"]
|
||||
shape = ["dep:harfrust"]
|
||||
paint = []
|
||||
draw = []
|
||||
# Core font functions require only read-fonts.
|
||||
font = ["read-fonts"]
|
||||
shape = ["dep:harfrust", "dep:smallvec"]
|
||||
read-fonts = ["dep:read-fonts"]
|
||||
# Skrifa also provides bitmap and COLR extents without drawing or painting.
|
||||
skrifa = ["read-fonts", "dep:skrifa"]
|
||||
paint = ["skrifa"]
|
||||
draw = ["skrifa"]
|
||||
hb-allocator = []
|
||||
|
||||
[profile.release]
|
||||
|
||||
+497
-308
File diff suppressed because it is too large.
Load diff
@@ -7,6 +7,36 @@ mod font;
|
||||
#[cfg(feature = "shape")]
|
||||
mod shape;
|
||||
|
||||
#[cfg(any(feature = "font", feature = "shape"))]
|
||||
struct HbBlob(*mut hb_blob_t);
|
||||
|
||||
#[cfg(any(feature = "font", feature = "shape"))]
|
||||
impl Drop for HbBlob {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
hb_blob_destroy(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "font", feature = "shape"))]
|
||||
impl AsRef<[u8]> for HbBlob {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
let mut length = 0;
|
||||
let data = unsafe { hb_blob_get_data(self.0, &mut length) };
|
||||
if data.is_null() {
|
||||
&[]
|
||||
} else {
|
||||
unsafe { std::slice::from_raw_parts(data.cast(), length as usize) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "font", feature = "shape"))]
|
||||
unsafe impl Send for HbBlob {}
|
||||
#[cfg(any(feature = "font", feature = "shape"))]
|
||||
unsafe impl Sync for HbBlob {}
|
||||
|
||||
#[cfg(feature = "hb-allocator")]
|
||||
use std::alloc::{GlobalAlloc, Layout};
|
||||
#[cfg(feature = "hb-allocator")]
|
||||
|
||||
+56
-79
@@ -2,7 +2,7 @@
|
||||
// C enum becomes i32 on some systems (eg. Windows).
|
||||
#![allow(clippy::unnecessary_cast)]
|
||||
|
||||
use super::hb::*;
|
||||
use super::{hb::*, HbBlob};
|
||||
|
||||
use std::ffi::c_void;
|
||||
use std::mem::{align_of, offset_of, size_of};
|
||||
@@ -10,12 +10,10 @@ use std::ptr::null_mut;
|
||||
use std::sync::Arc;
|
||||
|
||||
use harfrust::{
|
||||
font::{
|
||||
AdvanceWidthBatch, BuiltinFontFuncs, Font, FontBlob, FontFuncs, FontInstance,
|
||||
FontTableFunction, NominalGlyphBatch,
|
||||
},
|
||||
GlyphExtents, GlyphFlags as HRGlyphFlags, GlyphId, GlyphInfo as HRGlyphInfo,
|
||||
GlyphPosition as HRGlyphPosition, NormalizedCoord, ShapeOptions, Tag,
|
||||
font::{Blob, Font, NormalizedCoord, TableFunction},
|
||||
Advances, FontFuncs, GlyphExtents, GlyphFlags as HRGlyphFlags, GlyphId,
|
||||
GlyphInfo as HRGlyphInfo, GlyphPosition as HRGlyphPosition, NominalGlyphs, ShapeOptions,
|
||||
ShaperFont, Tag,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
@@ -72,38 +70,13 @@ impl HbFace {
|
||||
unsafe impl Send for HbFace {}
|
||||
unsafe impl Sync for HbFace {}
|
||||
|
||||
struct HbBlob(*mut hb_blob_t);
|
||||
|
||||
impl Drop for HbBlob {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
hb_blob_destroy(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for HbBlob {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
let mut length = 0;
|
||||
let data = unsafe { hb_blob_get_data(self.0, &mut length) };
|
||||
if data.is_null() {
|
||||
&[]
|
||||
} else {
|
||||
unsafe { std::slice::from_raw_parts(data.cast(), length as usize) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for HbBlob {}
|
||||
unsafe impl Sync for HbBlob {}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn _hb_harfrust_shaper_face_data_create_rs(
|
||||
face: *mut hb_face_t,
|
||||
) -> *mut c_void {
|
||||
let face_index = hb_face_get_index(face);
|
||||
let hb_face = HbFace::new(face);
|
||||
let table_fn = FontTableFunction::new(Arc::new(move |tag| {
|
||||
let table_fn = TableFunction::new(Arc::new(move |tag| {
|
||||
let blob = hb_face.reference_table(tag);
|
||||
if blob.is_null() {
|
||||
return None;
|
||||
@@ -114,12 +87,12 @@ pub unsafe extern "C" fn _hb_harfrust_shaper_face_data_create_rs(
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Some(FontBlob::Shared(Arc::new(HbBlob(blob))))
|
||||
Some(Blob::Shared(Arc::new(HbBlob(blob))))
|
||||
}));
|
||||
|
||||
let font = match Font::new(table_fn, face_index) {
|
||||
Ok(font) => font,
|
||||
Err(_) => return null_mut(),
|
||||
Some(font) => font,
|
||||
None => return null_mut(),
|
||||
};
|
||||
|
||||
let hr_face_data = Box::new(HBHarfRustFaceData { font });
|
||||
@@ -134,7 +107,7 @@ pub unsafe extern "C" fn _hb_harfrust_shaper_face_data_destroy_rs(data: *mut c_v
|
||||
}
|
||||
|
||||
pub struct HBHarfRustFontData {
|
||||
instance: FontInstance,
|
||||
instance: Font,
|
||||
x_scale: i32,
|
||||
y_scale: i32,
|
||||
ptem: Option<f32>,
|
||||
@@ -145,7 +118,7 @@ struct HBHarfBuzzFontFuncs {
|
||||
}
|
||||
|
||||
impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
fn nominal_glyph(&mut self, _: &BuiltinFontFuncs, c: u32) -> Option<GlyphId> {
|
||||
fn nominal_glyph(&self, _: &ShaperFont, c: u32) -> Option<GlyphId> {
|
||||
let mut glyph = 0;
|
||||
if unsafe { hb_font_get_nominal_glyph(self.font, c, &mut glyph) } != 0 {
|
||||
Some(GlyphId::new(glyph))
|
||||
@@ -154,7 +127,7 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
}
|
||||
}
|
||||
|
||||
fn variant_glyph(&mut self, _: &BuiltinFontFuncs, c: u32, vs: u32) -> Option<GlyphId> {
|
||||
fn variation_glyph(&self, _: &ShaperFont, c: u32, vs: u32) -> Option<GlyphId> {
|
||||
let mut glyph = 0;
|
||||
if unsafe { hb_font_get_variation_glyph(self.font, c, vs, &mut glyph) } != 0 {
|
||||
Some(GlyphId::new(glyph))
|
||||
@@ -163,11 +136,11 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
}
|
||||
}
|
||||
|
||||
fn advance_width(&mut self, _: &BuiltinFontFuncs, glyph: GlyphId) -> i32 {
|
||||
fn glyph_h_advance(&self, _: &ShaperFont, glyph: GlyphId) -> i32 {
|
||||
unsafe { hb_font_get_glyph_h_advance(self.font, glyph.to_u32()) }
|
||||
}
|
||||
|
||||
fn populate_advance_widths(&mut self, _: &BuiltinFontFuncs, batch: AdvanceWidthBatch<'_>) {
|
||||
fn glyph_h_advances(&self, _: &ShaperFont, batch: Advances<'_>) {
|
||||
let raw = batch.into_raw();
|
||||
unsafe {
|
||||
hb_font_get_glyph_h_advances(
|
||||
@@ -181,11 +154,7 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
}
|
||||
}
|
||||
|
||||
fn populate_nominal_glyphs(
|
||||
&mut self,
|
||||
_: &BuiltinFontFuncs,
|
||||
batch: NominalGlyphBatch<'_>,
|
||||
) -> usize {
|
||||
fn nominal_glyphs(&self, _: &ShaperFont, batch: NominalGlyphs<'_>) -> usize {
|
||||
let raw = batch.into_raw();
|
||||
unsafe {
|
||||
hb_font_get_nominal_glyphs(
|
||||
@@ -199,11 +168,11 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
}
|
||||
}
|
||||
|
||||
fn advance_height(&mut self, _: &BuiltinFontFuncs, glyph: GlyphId) -> i32 {
|
||||
fn glyph_v_advance(&self, _: &ShaperFont, glyph: GlyphId) -> i32 {
|
||||
unsafe { hb_font_get_glyph_v_advance(self.font, glyph.to_u32()) }
|
||||
}
|
||||
|
||||
fn vertical_origin(&mut self, _: &BuiltinFontFuncs, glyph: GlyphId) -> (i32, i32) {
|
||||
fn glyph_v_origin(&self, _: &ShaperFont, glyph: GlyphId) -> (i32, i32) {
|
||||
let mut x = 0;
|
||||
let mut y = 0;
|
||||
unsafe {
|
||||
@@ -212,7 +181,7 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
(x, y)
|
||||
}
|
||||
|
||||
fn extents(&mut self, _: &BuiltinFontFuncs, glyph: GlyphId) -> Option<GlyphExtents> {
|
||||
fn glyph_extents(&self, _: &ShaperFont, glyph: GlyphId) -> Option<GlyphExtents> {
|
||||
let mut extents = hb_glyph_extents_t {
|
||||
x_bearing: 0,
|
||||
y_bearing: 0,
|
||||
@@ -232,7 +201,7 @@ impl FontFuncs for HBHarfBuzzFontFuncs {
|
||||
}
|
||||
}
|
||||
|
||||
fn font_to_instance(font: *mut hb_font_t, font_ref: &Font) -> FontInstance {
|
||||
fn font_to_instance(font: *mut hb_font_t, font_ref: &Font) -> Font {
|
||||
let mut num_coords: u32 = 0;
|
||||
let coords = unsafe { hb_font_get_var_coords_normalized(font, &mut num_coords) };
|
||||
let coords = if coords.is_null() {
|
||||
@@ -241,7 +210,8 @@ fn font_to_instance(font: *mut hb_font_t, font_ref: &Font) -> FontInstance {
|
||||
unsafe { std::slice::from_raw_parts(coords, num_coords as usize) }
|
||||
};
|
||||
let coords = coords.iter().map(|&v| NormalizedCoord::from_bits(v as i16));
|
||||
FontInstance::builder(font_ref)
|
||||
font_ref
|
||||
.instance_builder()
|
||||
.normalized_coords(coords)
|
||||
.build()
|
||||
}
|
||||
@@ -290,13 +260,13 @@ fn hb_language_to_hr_language(language: hb_language_t) -> Option<harfrust::Langu
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn _hb_harfrust_buffer_create_rs() -> *mut c_void {
|
||||
let hr_buffer = Box::new(harfrust::UnicodeBuffer::new());
|
||||
let hr_buffer = Box::new(harfrust::Buffer::new());
|
||||
Box::into_raw(hr_buffer) as *mut c_void
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn _hb_harfrust_buffer_destroy_rs(data: *mut c_void) {
|
||||
let data = data as *mut harfrust::UnicodeBuffer;
|
||||
let data = data as *mut harfrust::Buffer;
|
||||
let _hr_buffer = Box::from_raw(data);
|
||||
}
|
||||
|
||||
@@ -377,41 +347,41 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
|
||||
num_features: u32,
|
||||
) -> hb_bool_t {
|
||||
let font_data = font_data as *const HBHarfRustFontData;
|
||||
let hr_buffer_box = hr_buffer_box as *mut harfrust::UnicodeBuffer;
|
||||
let mut hr_buffer_box = Box::from_raw(hr_buffer_box);
|
||||
let mut hr_buffer = *hr_buffer_box;
|
||||
let hr_buffer = &mut *(hr_buffer_box as *mut harfrust::Buffer);
|
||||
hr_buffer.clear();
|
||||
let shape_plan = (shape_plan as *const harfrust::ShapePlan).as_ref();
|
||||
|
||||
// Set buffer properties
|
||||
let cluster_level = hb_buffer_get_cluster_level(buffer);
|
||||
let cluster_level = match cluster_level {
|
||||
hb_buffer_cluster_level_t_HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES => {
|
||||
harfrust::BufferClusterLevel::MonotoneGraphemes
|
||||
harfrust::ClusterLevel::MonotoneGraphemes
|
||||
}
|
||||
hb_buffer_cluster_level_t_HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS => {
|
||||
harfrust::BufferClusterLevel::MonotoneCharacters
|
||||
harfrust::ClusterLevel::MonotoneCharacters
|
||||
}
|
||||
hb_buffer_cluster_level_t_HB_BUFFER_CLUSTER_LEVEL_CHARACTERS => {
|
||||
harfrust::BufferClusterLevel::Characters
|
||||
harfrust::ClusterLevel::Characters
|
||||
}
|
||||
hb_buffer_cluster_level_t_HB_BUFFER_CLUSTER_LEVEL_GRAPHEMES => {
|
||||
harfrust::BufferClusterLevel::Graphemes
|
||||
harfrust::ClusterLevel::Graphemes
|
||||
}
|
||||
_ => harfrust::BufferClusterLevel::default(),
|
||||
_ => harfrust::ClusterLevel::MonotoneGraphemes,
|
||||
};
|
||||
hr_buffer.set_cluster_level(cluster_level);
|
||||
let flags = hb_buffer_get_flags(buffer);
|
||||
hr_buffer.set_flags(harfrust::BufferFlags::from_bits_truncate(flags as u32));
|
||||
let not_found_variation_selector_glyph =
|
||||
hb_buffer_get_not_found_variation_selector_glyph(buffer);
|
||||
if not_found_variation_selector_glyph != u32::MAX {
|
||||
hr_buffer.set_not_found_variation_selector_glyph(not_found_variation_selector_glyph);
|
||||
}
|
||||
hr_buffer.set_not_found_variation_selector_glyph(
|
||||
(not_found_variation_selector_glyph != u32::MAX)
|
||||
.then_some(not_found_variation_selector_glyph),
|
||||
);
|
||||
|
||||
if let Some(plan) = shape_plan {
|
||||
// Language is only used to build the plan; shaping with an explicit
|
||||
// plan reads its cached segment properties instead of buffer language.
|
||||
hr_buffer.set_script(plan.script().unwrap_or(harfrust::script::UNKNOWN));
|
||||
hr_buffer.set_script(Some(plan.script().unwrap_or(harfrust::Script::UNKNOWN)));
|
||||
hr_buffer.set_direction(plan.direction());
|
||||
} else {
|
||||
// Convert segment properties when shaping without a cached plan.
|
||||
@@ -419,7 +389,7 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
|
||||
let language = hb_buffer_get_language(buffer);
|
||||
let direction = hb_buffer_get_direction(buffer);
|
||||
let script = harfrust::Script::from_iso15924_tag(Tag::from_u32(script as u32))
|
||||
.unwrap_or(harfrust::script::UNKNOWN);
|
||||
.unwrap_or(harfrust::Script::UNKNOWN);
|
||||
let language = hb_language_to_hr_language(language);
|
||||
let direction = match direction {
|
||||
hb_direction_t_HB_DIRECTION_LTR => harfrust::Direction::LeftToRight,
|
||||
@@ -428,9 +398,9 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
|
||||
hb_direction_t_HB_DIRECTION_BTT => harfrust::Direction::BottomToTop,
|
||||
_ => harfrust::Direction::Invalid,
|
||||
};
|
||||
hr_buffer.set_script(script);
|
||||
hr_buffer.set_script(Some(script));
|
||||
if let Some(lang) = language {
|
||||
hr_buffer.set_language(lang);
|
||||
hr_buffer.set_language(Some(lang));
|
||||
}
|
||||
hr_buffer.set_direction(direction);
|
||||
}
|
||||
@@ -449,20 +419,25 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
|
||||
hr_buffer.set_post_context_codepoints(post_context);
|
||||
|
||||
let features = hb_feature_to_hr_feature(features, num_features);
|
||||
let mut font_funcs = HBHarfBuzzFontFuncs { font };
|
||||
let font_funcs = HBHarfBuzzFontFuncs { font };
|
||||
let shaper_font = ShaperFont::new(&(*font_data).instance)
|
||||
.with_scale_separate((*font_data).x_scale, (*font_data).y_scale)
|
||||
.with_font_funcs(Some(&font_funcs));
|
||||
let options = ShapeOptions::new()
|
||||
.plan(shape_plan)
|
||||
.scale_separate(Some(((*font_data).x_scale, (*font_data).y_scale)))
|
||||
.point_size((*font_data).ptem)
|
||||
.features(&features)
|
||||
.font_funcs(Some(&mut font_funcs));
|
||||
let glyphs = harfrust::shape(&(*font_data).instance, hr_buffer, options);
|
||||
.features(&features);
|
||||
if harfrust::shape(&shaper_font, hr_buffer, options).is_err()
|
||||
|| !hr_buffer.allocation_successful()
|
||||
{
|
||||
return false as hb_bool_t;
|
||||
}
|
||||
|
||||
hb_buffer_set_content_type(
|
||||
buffer,
|
||||
hb_buffer_content_type_t_HB_BUFFER_CONTENT_TYPE_GLYPHS,
|
||||
);
|
||||
let count = glyphs.len();
|
||||
let count = hr_buffer.len();
|
||||
hb_buffer_set_length(buffer, count as u32);
|
||||
let mut count_out: u32 = 0;
|
||||
let infos = hb_buffer_get_glyph_infos(buffer, &mut count_out);
|
||||
@@ -471,12 +446,14 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
|
||||
return false as hb_bool_t;
|
||||
}
|
||||
|
||||
std::ptr::copy_nonoverlapping(glyphs.glyph_infos().as_ptr().cast(), infos, count);
|
||||
std::ptr::copy_nonoverlapping(glyphs.glyph_positions().as_ptr().cast(), positions, count);
|
||||
std::ptr::copy_nonoverlapping(hr_buffer.glyph_infos().as_ptr().cast(), infos, count);
|
||||
std::ptr::copy_nonoverlapping(
|
||||
hr_buffer.glyph_positions().as_ptr().cast(),
|
||||
positions,
|
||||
count,
|
||||
);
|
||||
|
||||
let hr_buffer = glyphs.clear();
|
||||
*hr_buffer_box = hr_buffer; // Move the buffer back into the box
|
||||
let _ = Box::into_raw(hr_buffer_box); // Prevent double free
|
||||
hr_buffer.clear();
|
||||
|
||||
true as hb_bool_t
|
||||
}
|
||||
@@ -26,6 +26,10 @@
|
||||
|
||||
#include "hb-test.h"
|
||||
|
||||
#ifdef HAVE_FONTATIONS
|
||||
#include <hb-fontations.h>
|
||||
#endif
|
||||
|
||||
/* Unit tests for hb-font.h */
|
||||
|
||||
|
||||
@@ -712,6 +716,397 @@ test_synthetic_glyph_extents_overflow (void)
|
||||
hb_font_funcs_destroy (ffuncs);
|
||||
}
|
||||
|
||||
#ifdef HAVE_FONTATIONS
|
||||
typedef struct {
|
||||
hb_font_t *font;
|
||||
GMutex mutex;
|
||||
GCond cond;
|
||||
unsigned waiting;
|
||||
hb_bool_t start;
|
||||
char name[128];
|
||||
} fontations_name_test_t;
|
||||
|
||||
static gpointer
|
||||
fontations_name_thread (gpointer user_data)
|
||||
{
|
||||
fontations_name_test_t *data = user_data;
|
||||
g_mutex_lock (&data->mutex);
|
||||
data->waiting++;
|
||||
g_cond_broadcast (&data->cond);
|
||||
while (!data->start)
|
||||
g_cond_wait (&data->cond, &data->mutex);
|
||||
g_mutex_unlock (&data->mutex);
|
||||
|
||||
for (unsigned i = 0; i < 100; i++)
|
||||
{
|
||||
hb_codepoint_t glyph = HB_CODEPOINT_INVALID;
|
||||
g_assert_true (hb_font_get_glyph_from_name (data->font, data->name, -1, &glyph));
|
||||
g_assert_cmpuint (glyph, ==, 1);
|
||||
g_assert_false (hb_font_get_glyph_from_name (data->font, "nonexistent-glyph", -1, &glyph));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_glyph_from_name_threads (void)
|
||||
{
|
||||
for (unsigned run = 0; run < 8; run++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
|
||||
fontations_name_test_t data = {0};
|
||||
data.font = hb_font_create (face);
|
||||
hb_fontations_font_set_funcs (data.font);
|
||||
hb_font_make_immutable (data.font);
|
||||
g_assert_true (hb_font_get_glyph_name (data.font, 1, data.name, sizeof (data.name)));
|
||||
g_mutex_init (&data.mutex);
|
||||
g_cond_init (&data.cond);
|
||||
|
||||
GThread *threads[8];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (threads); i++)
|
||||
threads[i] = g_thread_new ("fontations-names", fontations_name_thread, &data);
|
||||
g_mutex_lock (&data.mutex);
|
||||
while (data.waiting != G_N_ELEMENTS (threads))
|
||||
g_cond_wait (&data.cond, &data.mutex);
|
||||
data.start = true;
|
||||
g_cond_broadcast (&data.cond);
|
||||
g_mutex_unlock (&data.mutex);
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (threads); i++)
|
||||
g_thread_join (threads[i]);
|
||||
|
||||
g_cond_clear (&data.cond);
|
||||
g_mutex_clear (&data.mutex);
|
||||
hb_font_destroy (data.font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_scale_changes (void)
|
||||
{
|
||||
const char *fonts[] = {
|
||||
"fonts/Roboto-Variable.abc.ttf",
|
||||
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
|
||||
"fonts/Roboto-Regular.abc.ttf",
|
||||
};
|
||||
const struct {
|
||||
int values[3];
|
||||
unsigned count;
|
||||
} locations[] = {
|
||||
{{0, 0, 0}, 0}, {{0, 0, 0}, 3},
|
||||
{{9830, 0, 0}, 1}, {{9830, 0, 0}, 3},
|
||||
{{9830, 16384, 0}, 2}, {{9830, 16384, 16384}, 3},
|
||||
{{0, 0, 16384}, 3}, {{0, 0, 0}, 0},
|
||||
};
|
||||
const int scales[][2] = {{2048, 4096}, {-1234, -5678}, {0, 0}, {1234, 5678}};
|
||||
|
||||
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file (fonts[f]);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_font_t *reference = hb_font_create (face);
|
||||
hb_fontations_font_set_funcs (font);
|
||||
|
||||
for (unsigned c = 0; c < G_N_ELEMENTS (locations); c++)
|
||||
{
|
||||
hb_font_set_var_coords_normalized (font, locations[c].values, locations[c].count);
|
||||
hb_font_set_var_coords_normalized (reference, locations[c].values, locations[c].count);
|
||||
for (unsigned s = 0; s < G_N_ELEMENTS (scales); s++)
|
||||
{
|
||||
hb_font_set_scale (font, scales[s][0], scales[s][1]);
|
||||
hb_font_set_scale (reference, scales[s][0], scales[s][1]);
|
||||
/* Compare the reused instance against a freshly initialized one. */
|
||||
hb_fontations_font_set_funcs (reference);
|
||||
for (hb_codepoint_t glyph = 0; glyph < hb_face_get_glyph_count (face); glyph++)
|
||||
{
|
||||
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), ==,
|
||||
hb_font_get_glyph_h_advance (reference, glyph));
|
||||
g_assert_cmpint (hb_font_get_glyph_v_advance (font, glyph), ==,
|
||||
hb_font_get_glyph_v_advance (reference, glyph));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hb_font_destroy (reference);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_strides (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_fontations_font_set_funcs (font);
|
||||
const hb_codepoint_t unicodes[] = {'a', 'b', 'c'};
|
||||
hb_codepoint_t ids[3];
|
||||
uint8_t unicode_bytes[16], glyph_bytes[16], advance_bytes[16];
|
||||
memset (unicode_bytes, 0xA5, sizeof (unicode_bytes));
|
||||
memset (glyph_bytes, 0xA5, sizeof (glyph_bytes));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (unicodes); i++)
|
||||
{
|
||||
memcpy (unicode_bytes + 1 + 5 * i, &unicodes[i], sizeof (unicodes[i]));
|
||||
g_assert_true (hb_font_get_nominal_glyph (font, unicodes[i], &ids[i]));
|
||||
}
|
||||
g_assert_cmpuint (hb_font_get_nominal_glyphs (font, 3,
|
||||
(const hb_codepoint_t *) (unicode_bytes + 1), 5,
|
||||
(hb_codepoint_t *) (glyph_bytes + 1), 5), ==, 3);
|
||||
for (unsigned i = 0; i < 3; i++)
|
||||
{
|
||||
hb_codepoint_t glyph;
|
||||
memcpy (&glyph, glyph_bytes + 1 + 5 * i, sizeof (glyph));
|
||||
g_assert_cmpuint (glyph, ==, ids[i]);
|
||||
g_assert_cmpuint (glyph_bytes[5 * i + 5], ==, 0xA5);
|
||||
}
|
||||
g_assert_cmpuint (glyph_bytes[0], ==, 0xA5);
|
||||
|
||||
/* hb-ot-normalize maps codepoint to var1.u32 in the same glyph-info array. */
|
||||
hb_glyph_info_t infos[3] = {0};
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (infos); i++)
|
||||
{
|
||||
infos[i].codepoint = unicodes[i];
|
||||
infos[i].mask = 123456;
|
||||
infos[i].cluster = i;
|
||||
infos[i].var1.u32 = HB_CODEPOINT_INVALID;
|
||||
infos[i].var2.u32 = 123456;
|
||||
}
|
||||
g_assert_cmpuint (hb_font_get_nominal_glyphs (font, G_N_ELEMENTS (infos),
|
||||
&infos[0].codepoint, sizeof (infos[0]),
|
||||
&infos[0].var1.u32, sizeof (infos[0])), ==, G_N_ELEMENTS (infos));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (infos); i++)
|
||||
{
|
||||
g_assert_cmpuint (infos[i].codepoint, ==, unicodes[i]);
|
||||
g_assert_cmpuint (infos[i].var1.u32, ==, ids[i]);
|
||||
g_assert_cmpuint (infos[i].mask, ==, 123456);
|
||||
g_assert_cmpuint (infos[i].cluster, ==, i);
|
||||
g_assert_cmpuint (infos[i].var2.u32, ==, 123456);
|
||||
}
|
||||
|
||||
void (*get_advances[])(hb_font_t *, unsigned, const hb_codepoint_t *, unsigned,
|
||||
hb_position_t *, unsigned) = {
|
||||
hb_font_get_glyph_h_advances, hb_font_get_glyph_v_advances,
|
||||
};
|
||||
for (unsigned axis = 0; axis < G_N_ELEMENTS (get_advances); axis++)
|
||||
{
|
||||
hb_position_t expected[3];
|
||||
get_advances[axis] (font, 3, ids, sizeof (ids[0]), expected, sizeof (expected[0]));
|
||||
memset (advance_bytes, 0xA5, sizeof (advance_bytes));
|
||||
get_advances[axis] (font, 3, (const hb_codepoint_t *) (glyph_bytes + 1), 5,
|
||||
(hb_position_t *) (advance_bytes + 1), 5);
|
||||
for (unsigned i = 0; i < 3; i++)
|
||||
{
|
||||
hb_position_t advance;
|
||||
memcpy (&advance, advance_bytes + 1 + 5 * i, sizeof (advance));
|
||||
g_assert_cmpint (advance, ==, expected[i]);
|
||||
g_assert_cmpuint (advance_bytes[5 * i + 5], ==, 0xA5);
|
||||
}
|
||||
g_assert_cmpuint (advance_bytes[0], ==, 0xA5);
|
||||
|
||||
hb_position_t last = 0;
|
||||
get_advances[axis] (font, 3, ids, sizeof (ids[0]), &last, 0);
|
||||
g_assert_cmpint (last, ==, expected[2]);
|
||||
|
||||
hb_codepoint_t in_place[3];
|
||||
memcpy (in_place, ids, sizeof (ids));
|
||||
get_advances[axis] (font, 3, in_place, sizeof (in_place[0]),
|
||||
(hb_position_t *) in_place, sizeof (in_place[0]));
|
||||
for (unsigned i = 0; i < 3; i++)
|
||||
g_assert_cmpint ((hb_position_t) in_place[i], ==, expected[i]);
|
||||
}
|
||||
|
||||
struct {
|
||||
hb_codepoint_t glyph;
|
||||
hb_position_t h_advance, v_advance, x, y, padding;
|
||||
} records[3];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (records); i++)
|
||||
{
|
||||
records[i].glyph = ids[i];
|
||||
records[i].h_advance = records[i].v_advance = records[i].x = records[i].y = 0;
|
||||
records[i].padding = 123456;
|
||||
}
|
||||
hb_font_get_glyph_h_advances (font, G_N_ELEMENTS (records),
|
||||
&records[0].glyph, sizeof (records[0]),
|
||||
&records[0].h_advance, sizeof (records[0]));
|
||||
hb_font_get_glyph_v_advances (font, G_N_ELEMENTS (records),
|
||||
&records[0].glyph, sizeof (records[0]),
|
||||
&records[0].v_advance, sizeof (records[0]));
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (records),
|
||||
&records[0].glyph, sizeof (records[0]),
|
||||
&records[0].x, sizeof (records[0]),
|
||||
&records[0].y, sizeof (records[0])));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (records); i++)
|
||||
{
|
||||
hb_position_t x, y;
|
||||
g_assert_cmpuint (records[i].glyph, ==, ids[i]);
|
||||
g_assert_cmpint (records[i].h_advance, ==, hb_font_get_glyph_h_advance (font, ids[i]));
|
||||
g_assert_cmpint (records[i].v_advance, ==, hb_font_get_glyph_v_advance (font, ids[i]));
|
||||
g_assert_true (hb_font_get_glyph_v_origin (font, ids[i], &x, &y));
|
||||
g_assert_cmpint (records[i].x, ==, x);
|
||||
g_assert_cmpint (records[i].y, ==, y);
|
||||
g_assert_cmpint (records[i].padding, ==, 123456);
|
||||
}
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_v_origins (void)
|
||||
{
|
||||
const char *fonts[] = {
|
||||
"fonts/Roboto-Regular.abc.ttf",
|
||||
"fonts/Roboto-Variable.abc.ttf",
|
||||
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
|
||||
"fonts/AdobeVFPrototype.abc.otf",
|
||||
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.ttf",
|
||||
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.otf",
|
||||
};
|
||||
const int coords[] = {0, 16384, -16384, 0};
|
||||
const hb_codepoint_t glyphs[] = {0, 1, 2, 3, HB_CODEPOINT_INVALID};
|
||||
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file (fonts[f]);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_fontations_font_set_funcs (font);
|
||||
int upem = hb_face_get_upem (face);
|
||||
for (int sign = 1; sign >= -1; sign -= 2)
|
||||
{
|
||||
hb_font_set_scale (font, sign * upem * 2, sign * upem * 3);
|
||||
for (unsigned c = 0; c < G_N_ELEMENTS (coords); c++)
|
||||
{
|
||||
hb_font_set_var_coords_normalized (font, &coords[c], 1);
|
||||
hb_position_t expected_x[G_N_ELEMENTS (glyphs)], expected_y[G_N_ELEMENTS (glyphs)];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
g_assert_true (hb_font_get_glyph_v_origin (font, glyphs[i], &expected_x[i], &expected_y[i]));
|
||||
|
||||
hb_position_t origins[4 * G_N_ELEMENTS (glyphs)];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (origins); i++)
|
||||
origins[i] = 123456;
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]),
|
||||
origins, 4 * sizeof (origins[0]),
|
||||
origins + 1, 4 * sizeof (origins[0])));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
g_assert_cmpint (origins[4 * i], ==, expected_x[i]);
|
||||
g_assert_cmpint (origins[4 * i + 1], ==, expected_y[i]);
|
||||
g_assert_cmpint (origins[4 * i + 2], ==, 123456);
|
||||
g_assert_cmpint (origins[4 * i + 3], ==, 123456);
|
||||
}
|
||||
|
||||
hb_position_t last_x = 0, last_y = 0;
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]), &last_x, 0, &last_y, 0));
|
||||
g_assert_cmpint (last_x, ==, expected_x[G_N_ELEMENTS (glyphs) - 1]);
|
||||
g_assert_cmpint (last_y, ==, expected_y[G_N_ELEMENTS (glyphs) - 1]);
|
||||
|
||||
hb_codepoint_t in_place[G_N_ELEMENTS (glyphs)];
|
||||
memcpy (in_place, glyphs, sizeof (glyphs));
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
in_place, sizeof (in_place[0]),
|
||||
(hb_position_t *) in_place, sizeof (in_place[0]),
|
||||
origins, sizeof (origins[0])));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
g_assert_cmpint ((hb_position_t) in_place[i], ==, expected_x[i]);
|
||||
g_assert_cmpint (origins[i], ==, expected_y[i]);
|
||||
}
|
||||
|
||||
uint8_t x_bytes[1 + 5 * G_N_ELEMENTS (glyphs)], y_bytes[sizeof (x_bytes)];
|
||||
memset (x_bytes, 0xA5, sizeof (x_bytes));
|
||||
memset (y_bytes, 0xA5, sizeof (y_bytes));
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]),
|
||||
(hb_position_t *) (x_bytes + 1), 5,
|
||||
(hb_position_t *) (y_bytes + 1), 5));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
hb_position_t x, y;
|
||||
memcpy (&x, x_bytes + 1 + 5 * i, sizeof (x));
|
||||
memcpy (&y, y_bytes + 1 + 5 * i, sizeof (y));
|
||||
g_assert_cmpint (x, ==, expected_x[i]);
|
||||
g_assert_cmpint (y, ==, expected_y[i]);
|
||||
g_assert_cmpuint (x_bytes[5 * i + 5], ==, 0xA5);
|
||||
g_assert_cmpuint (y_bytes[5 * i + 5], ==, 0xA5);
|
||||
}
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, 0, NULL, 0, NULL, 0, NULL, 0));
|
||||
}
|
||||
}
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_advances (void)
|
||||
{
|
||||
const char *fonts[] = {
|
||||
"fonts/Roboto-Variable.abc.ttf",
|
||||
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
|
||||
"fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf",
|
||||
};
|
||||
const int coords[] = {0, 16384, -16384, 0};
|
||||
const struct {
|
||||
hb_codepoint_t glyph;
|
||||
hb_codepoint_t padding[2];
|
||||
} glyphs[] = {{1, {0}}, {2, {0}}, {3, {0}}, {HB_CODEPOINT_INVALID, {0}}};
|
||||
|
||||
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file (fonts[f]);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_font_t *reference = hb_font_create (face);
|
||||
int upem = hb_face_get_upem (face);
|
||||
hb_fontations_font_set_funcs (font);
|
||||
|
||||
for (int sign = 1; sign >= -1; sign -= 2)
|
||||
{
|
||||
hb_font_set_scale (font, sign * upem * 2, sign * upem * 3);
|
||||
hb_font_set_scale (reference, sign * upem * 2, sign * upem * 3);
|
||||
|
||||
for (unsigned c = 0; c < G_N_ELEMENTS (coords); c++)
|
||||
{
|
||||
hb_font_set_var_coords_normalized (font, &coords[c], 1);
|
||||
hb_font_set_var_coords_normalized (reference, &coords[c], 1);
|
||||
|
||||
hb_position_t h_advances[2 * G_N_ELEMENTS (glyphs)];
|
||||
hb_position_t v_advances[2 * G_N_ELEMENTS (glyphs)];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (h_advances); i++)
|
||||
h_advances[i] = v_advances[i] = 123456;
|
||||
|
||||
hb_font_get_glyph_h_advances (font, G_N_ELEMENTS (glyphs),
|
||||
&glyphs[0].glyph, sizeof (glyphs[0]),
|
||||
h_advances, 2 * sizeof (h_advances[0]));
|
||||
hb_font_get_glyph_v_advances (font, G_N_ELEMENTS (glyphs),
|
||||
&glyphs[0].glyph, sizeof (glyphs[0]),
|
||||
v_advances, 2 * sizeof (v_advances[0]));
|
||||
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
hb_codepoint_t glyph = glyphs[i].glyph;
|
||||
g_assert_cmpint (h_advances[2 * i], ==, hb_font_get_glyph_h_advance (font, glyph));
|
||||
g_assert_cmpint (v_advances[2 * i], ==, hb_font_get_glyph_v_advance (font, glyph));
|
||||
/* OT can apply variation deltas even to invalid glyph IDs. */
|
||||
if (glyph < hb_face_get_glyph_count (face))
|
||||
{
|
||||
g_assert_cmpint (h_advances[2 * i], ==, hb_font_get_glyph_h_advance (reference, glyph));
|
||||
g_assert_cmpint (v_advances[2 * i], ==, hb_font_get_glyph_v_advance (reference, glyph));
|
||||
}
|
||||
g_assert_cmpint (h_advances[2 * i + 1], ==, 123456);
|
||||
g_assert_cmpint (v_advances[2 * i + 1], ==, 123456);
|
||||
}
|
||||
|
||||
hb_font_get_glyph_h_advances (font, 0, NULL, 0, NULL, 0);
|
||||
hb_font_get_glyph_v_advances (font, 0, NULL, 0, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
hb_font_destroy (reference);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -730,6 +1125,13 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_font_empty);
|
||||
hb_test_add (test_font_properties);
|
||||
hb_test_add (test_synthetic_glyph_extents_overflow);
|
||||
#ifdef HAVE_FONTATIONS
|
||||
hb_test_add (test_fontations_glyph_from_name_threads);
|
||||
hb_test_add (test_fontations_scale_changes);
|
||||
hb_test_add (test_fontations_strides);
|
||||
hb_test_add (test_fontations_v_origins);
|
||||
hb_test_add (test_fontations_advances);
|
||||
#endif
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
@@ -7,8 +7,7 @@
|
||||
@font-funcs=ot,ft,fontations
|
||||
../fonts/NotoSansCJK-VF.abc.ttf;--direction=t;U+0041,U+0042;[gid1=0@-287,-880+0,-1000|gid2=1@-316,-880+0,-1000]
|
||||
../fonts/NotoSerifHK-subset.ttf;--direction=t;U+0041,U+0042;[gid1=0@-353,-880+0,-1000|gid2=1@-327,-880+0,-1000]
|
||||
# glyf+gvar+vmtx; TODO: fontations needs more work https://github.com/harfbuzz/harfbuzz/issues/5388
|
||||
@font-funcs=ot,ft
|
||||
# glyf+gvar+vmtx
|
||||
../fonts/NotoSansCJK-VF.abc.ttf;--direction=t --variations wght=700;U+0041,U+0042;[gid1=0@-320,-880+0,-1000|gid2=1@-340,-880+0,-1000]
|
||||
../fonts/NotoSerifHK-subset.ttf;--direction=t --variations wght=700;U+0041,U+0042;[gid1=0@-373,-880+0,-1000|gid2=1@-354,-880+0,-1000]
|
||||
@font-funcs=ot,ft,fontations
|
||||
|
||||
Reference in new issue
Block a user