1#![expect(unsafe_code)]
6
7use std::os::raw::{c_char, c_int, c_uint, c_void};
8use std::sync::LazyLock;
9use std::{char, ptr};
10
11use app_units::Au;
12use euclid::default::Point2D;
13use harfbuzz_sys::{
16 HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS, HB_DIRECTION_LTR, HB_DIRECTION_RTL,
17 HB_MEMORY_MODE_READONLY, HB_OT_LAYOUT_BASELINE_TAG_HANGING,
18 HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
19 hb_blob_create, hb_blob_t, hb_bool_t, hb_buffer_add_utf8, hb_buffer_create, hb_buffer_destroy,
20 hb_buffer_get_glyph_infos, hb_buffer_get_glyph_positions, hb_buffer_get_length,
21 hb_buffer_set_cluster_level, hb_buffer_set_direction, hb_buffer_set_language,
22 hb_buffer_set_script, hb_buffer_t, hb_codepoint_t, hb_face_create_for_tables, hb_face_destroy,
23 hb_face_t, hb_feature_t, hb_font_create, hb_font_destroy, hb_font_funcs_create,
24 hb_font_funcs_set_glyph_h_advance_func, hb_font_funcs_set_nominal_glyph_func, hb_font_funcs_t,
25 hb_font_set_funcs, hb_font_set_ppem, hb_font_set_scale, hb_font_set_variations, hb_font_t,
26 hb_glyph_info_t, hb_glyph_position_t, hb_language_from_string, hb_ot_layout_get_baseline,
27 hb_position_t, hb_script_from_iso15924_tag, hb_shape, hb_tag_t, hb_variation_t,
28};
29use num_traits::Zero;
30use read_fonts::types::Tag;
31use style::font_face::FontFaceRule;
32
33use super::{
34 GlyphShapingResult, ShapedGlyph, compute_used_font_features, unicode_script_to_iso15924_tag,
35};
36use crate::platform::font::FontTable;
37use crate::{
38 BASE, Font, FontBaseline, FontTableMethods, GlyphId, ShapedText, ShapingFlags, ShapingOptions,
39 fixed_to_float, float_to_fixed,
40};
41
42const HB_OT_TAG_DEFAULT_SCRIPT: hb_tag_t = u32::from_be_bytes(Tag::new(b"DFLT").to_be_bytes());
43const HB_OT_TAG_DEFAULT_LANGUAGE: hb_tag_t = u32::from_be_bytes(Tag::new(b"dflt").to_be_bytes());
44
45pub(crate) struct HarfbuzzGlyphShapingResult {
46 count: usize,
47 buffer: *mut hb_buffer_t,
48 glyph_infos: *mut hb_glyph_info_t,
49 pos_infos: *mut hb_glyph_position_t,
50}
51
52impl HarfbuzzGlyphShapingResult {
53 unsafe fn new(buffer: *mut hb_buffer_t) -> HarfbuzzGlyphShapingResult {
61 let mut glyph_count = 0;
62 let glyph_infos = unsafe { hb_buffer_get_glyph_infos(buffer, &mut glyph_count) };
63 assert!(!glyph_infos.is_null());
64 let mut pos_count = 0;
65 let pos_infos = unsafe { hb_buffer_get_glyph_positions(buffer, &mut pos_count) };
66 assert!(!pos_infos.is_null());
67 assert_eq!(glyph_count, pos_count);
68
69 HarfbuzzGlyphShapingResult {
70 count: glyph_count as usize,
71 buffer,
72 glyph_infos,
73 pos_infos,
74 }
75 }
76}
77
78impl Drop for HarfbuzzGlyphShapingResult {
79 fn drop(&mut self) {
80 unsafe { hb_buffer_destroy(self.buffer) }
81 }
82}
83
84struct ShapedGlyphIterator<'a> {
85 shaped_glyph_data: &'a HarfbuzzGlyphShapingResult,
86 current_glyph_offset: usize,
87 y_position: Au,
88}
89
90impl<'a> Iterator for ShapedGlyphIterator<'a> {
91 type Item = ShapedGlyph;
92
93 fn next(&mut self) -> Option<Self::Item> {
94 if self.current_glyph_offset >= self.shaped_glyph_data.count {
95 return None;
96 }
97
98 unsafe {
99 let glyph_info_i = self
100 .shaped_glyph_data
101 .glyph_infos
102 .add(self.current_glyph_offset);
103 let pos_info_i = self
104 .shaped_glyph_data
105 .pos_infos
106 .add(self.current_glyph_offset);
107 let x_offset = Shaper::fixed_to_float((*pos_info_i).x_offset);
108 let y_offset = Shaper::fixed_to_float((*pos_info_i).y_offset);
109 let x_advance = Shaper::fixed_to_float((*pos_info_i).x_advance);
110 let y_advance = Shaper::fixed_to_float((*pos_info_i).y_advance);
111
112 let x_offset = Au::from_f64_px(x_offset);
113 let y_offset = Au::from_f64_px(y_offset);
114 let x_advance = Au::from_f64_px(x_advance);
115 let y_advance = Au::from_f64_px(y_advance);
116
117 let offset = if x_offset.is_zero() && y_offset.is_zero() && y_advance.is_zero() {
118 None
119 } else {
120 if y_advance > Au::zero() {
122 self.y_position -= y_advance;
123 }
124
125 Some(Point2D::new(x_offset, self.y_position - y_offset))
126 };
127
128 self.current_glyph_offset += 1;
129 Some(ShapedGlyph {
130 glyph_id: (*glyph_info_i).codepoint as GlyphId,
131 string_byte_offset: (*glyph_info_i).cluster as usize,
132 advance: x_advance,
133 offset,
134 })
135 }
136 }
137}
138
139impl GlyphShapingResult for HarfbuzzGlyphShapingResult {
140 #[inline]
141 fn len(&self) -> usize {
142 self.count
143 }
144
145 fn iter(&self) -> impl Iterator<Item = ShapedGlyph> {
146 ShapedGlyphIterator {
147 shaped_glyph_data: self,
148 current_glyph_offset: 0,
149 y_position: Au::zero(),
150 }
151 }
152
153 fn is_rtl(&self) -> bool {
154 if self.count == 0 {
155 return false;
156 }
157 unsafe {
161 let first_glyph_info = self.glyph_infos.add(0);
162 let last_glyph_info = self.glyph_infos.add(self.count - 1);
163 (*last_glyph_info).cluster < (*first_glyph_info).cluster
164 }
165 }
166}
167
168#[derive(Debug)]
169pub(crate) struct Shaper {
170 hb_face: *mut hb_face_t,
171 hb_font: *mut hb_font_t,
172 font: *const Font,
173}
174
175unsafe impl Sync for Shaper {}
179unsafe impl Send for Shaper {}
180
181impl Drop for Shaper {
182 fn drop(&mut self) {
183 unsafe {
184 assert!(!self.hb_face.is_null());
185 hb_face_destroy(self.hb_face);
186
187 assert!(!self.hb_font.is_null());
188 hb_font_destroy(self.hb_font);
189 }
190 }
191}
192
193impl Shaper {
194 pub(crate) fn new(font: &Font) -> Shaper {
195 unsafe {
196 let hb_face: *mut hb_face_t = hb_face_create_for_tables(
197 Some(font_table_func),
198 font as *const Font as *mut c_void,
199 None,
200 );
201 let hb_font: *mut hb_font_t = hb_font_create(hb_face);
202
203 let pt_size = font.descriptor.pt_size.to_f64_px();
205 hb_font_set_ppem(hb_font, pt_size as c_uint, pt_size as c_uint);
206
207 hb_font_set_scale(
209 hb_font,
210 Shaper::float_to_fixed(pt_size) as c_int,
211 Shaper::float_to_fixed(pt_size) as c_int,
212 );
213
214 hb_font_set_funcs(
216 hb_font,
217 HB_FONT_FUNCS.0,
218 font as *const Font as *mut c_void,
219 None,
220 );
221
222 if servo_config::pref!(layout_variable_fonts_enabled) {
223 let variations = &font.variations();
224 if !variations.is_empty() {
225 let variations: Vec<_> = variations
226 .iter()
227 .map(|variation| hb_variation_t {
228 tag: variation.tag,
229
230 value: variation.value,
231 })
232 .collect();
233
234 hb_font_set_variations(hb_font, variations.as_ptr(), variations.len() as u32);
235 }
236 }
237
238 Shaper {
239 hb_face,
240 hb_font,
241 font,
242 }
243 }
244 }
245
246 fn shaped_glyph_data(
248 &self,
249 text: &str,
250 options: &ShapingOptions,
251 font_face_rule: Option<&FontFaceRule>,
252 ) -> HarfbuzzGlyphShapingResult {
253 unsafe {
254 let hb_buffer: *mut hb_buffer_t = hb_buffer_create();
255 hb_buffer_set_cluster_level(hb_buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
256 hb_buffer_set_direction(
257 hb_buffer,
258 if options.flags.contains(ShapingFlags::RTL_FLAG) {
259 HB_DIRECTION_RTL
260 } else {
261 HB_DIRECTION_LTR
262 },
263 );
264
265 let script =
266 hb_script_from_iso15924_tag(unicode_script_to_iso15924_tag(options.script));
267 hb_buffer_set_script(hb_buffer, script);
268
269 hb_buffer_add_utf8(
270 hb_buffer,
271 text.as_ptr() as *const c_char,
272 text.len() as c_int,
273 0,
274 text.len() as c_int,
275 );
276
277 let language = options.language;
278 let hb_language = hb_language_from_string(
279 language.as_str().as_ptr() as *const c_char,
280 language.as_str().len() as c_int,
281 );
282 hb_buffer_set_language(hb_buffer, hb_language);
283
284 let mut features: Vec<_> = compute_used_font_features(options, font_face_rule)
285 .map(|(tag, value)| hb_feature_t {
286 tag: u32::from_be_bytes(tag.to_be_bytes()),
287 value,
288 start: 0,
289 end: hb_buffer_get_length(hb_buffer),
290 })
291 .collect();
292 hb_shape(
293 self.hb_font,
294 hb_buffer,
295 features.as_mut_ptr(),
296 features.len() as u32,
297 );
298
299 HarfbuzzGlyphShapingResult::new(hb_buffer)
300 }
301 }
302
303 pub(crate) fn shape_text(
304 &self,
305 text: &str,
306 options: &ShapingOptions,
307 font_face_rule: Option<&FontFaceRule>,
308 ) -> ShapedText {
309 ShapedText::with_shaped_glyph_data(
310 text,
311 options,
312 &self.shaped_glyph_data(text, options, font_face_rule),
313 )
314 }
315
316 pub(crate) fn baseline(&self) -> Option<FontBaseline> {
317 unsafe { (*self.font).table_for_tag(BASE)? };
318
319 let mut hanging_baseline = 0;
320 let mut alphabetic_baseline = 0;
321 let mut ideographic_baseline = 0;
322
323 unsafe {
324 hb_ot_layout_get_baseline(
325 self.hb_font,
326 HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
327 HB_DIRECTION_LTR,
328 HB_OT_TAG_DEFAULT_SCRIPT,
329 HB_OT_TAG_DEFAULT_LANGUAGE,
330 &mut alphabetic_baseline as *mut _,
331 );
332
333 hb_ot_layout_get_baseline(
334 self.hb_font,
335 HB_OT_LAYOUT_BASELINE_TAG_HANGING,
336 HB_DIRECTION_LTR,
337 HB_OT_TAG_DEFAULT_SCRIPT,
338 HB_OT_TAG_DEFAULT_LANGUAGE,
339 &mut hanging_baseline as *mut _,
340 );
341
342 hb_ot_layout_get_baseline(
343 self.hb_font,
344 HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
345 HB_DIRECTION_LTR,
346 HB_OT_TAG_DEFAULT_SCRIPT,
347 HB_OT_TAG_DEFAULT_LANGUAGE,
348 &mut ideographic_baseline as *mut _,
349 );
350 }
351
352 Some(FontBaseline {
353 ideographic_baseline: Shaper::fixed_to_float(ideographic_baseline) as f32,
354 alphabetic_baseline: Shaper::fixed_to_float(alphabetic_baseline) as f32,
355 hanging_baseline: Shaper::fixed_to_float(hanging_baseline) as f32,
356 })
357 }
358
359 fn float_to_fixed(f: f64) -> i32 {
360 float_to_fixed(16, f)
361 }
362
363 fn fixed_to_float(i: hb_position_t) -> f64 {
364 fixed_to_float(16, i)
365 }
366}
367
368struct FontFuncs(*mut hb_font_funcs_t);
370
371unsafe impl Sync for FontFuncs {}
372unsafe impl Send for FontFuncs {}
373
374static HB_FONT_FUNCS: LazyLock<FontFuncs> = LazyLock::new(|| unsafe {
375 let hb_funcs = hb_font_funcs_create();
376 hb_font_funcs_set_nominal_glyph_func(hb_funcs, Some(glyph_func), ptr::null_mut(), None);
377 hb_font_funcs_set_glyph_h_advance_func(
378 hb_funcs,
379 Some(glyph_h_advance_func),
380 ptr::null_mut(),
381 None,
382 );
383
384 FontFuncs(hb_funcs)
385});
386
387extern "C" fn glyph_func(
388 _: *mut hb_font_t,
389 font_data: *mut c_void,
390 unicode: hb_codepoint_t,
391 glyph: *mut hb_codepoint_t,
392 _: *mut c_void,
393) -> hb_bool_t {
394 let font: *const Font = font_data as *const Font;
395 assert!(!font.is_null());
396
397 match unsafe { (*font).glyph_index(char::from_u32(unicode).unwrap()) } {
398 Some(g) => {
399 unsafe { *glyph = g as hb_codepoint_t };
400 true as hb_bool_t
401 },
402 None => false as hb_bool_t,
403 }
404}
405
406extern "C" fn glyph_h_advance_func(
407 _: *mut hb_font_t,
408 font_data: *mut c_void,
409 glyph: hb_codepoint_t,
410 _: *mut c_void,
411) -> hb_position_t {
412 let font: *mut Font = font_data as *mut Font;
413 assert!(!font.is_null());
414
415 let advance = unsafe { (*font).glyph_h_advance(glyph as GlyphId) };
416 Shaper::float_to_fixed(advance)
417}
418
419extern "C" fn font_table_func(
421 _: *mut hb_face_t,
422 tag: hb_tag_t,
423 user_data: *mut c_void,
424) -> *mut hb_blob_t {
425 let font = user_data as *const Font;
427 assert!(!font.is_null());
428
429 let Some(font_table) = (unsafe { (*font).table_for_tag(Tag::from_u32(tag)) }) else {
431 return ptr::null_mut();
432 };
433
434 let font_table_ptr = Box::into_raw(Box::new(font_table));
438
439 let buf = unsafe { (*font_table_ptr).buffer() };
440 let blob = unsafe {
442 hb_blob_create(
443 buf.as_ptr() as *const c_char,
444 buf.len() as c_uint,
445 HB_MEMORY_MODE_READONLY,
446 font_table_ptr as *mut c_void,
447 Some(destroy_blob_func),
448 )
449 };
450
451 assert!(!blob.is_null());
452 blob
453}
454
455extern "C" fn destroy_blob_func(font_table_ptr: *mut c_void) {
456 unsafe {
457 drop(Box::from_raw(font_table_ptr as *mut FontTable));
458 }
459}