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::values::computed::{FontVariantLigatures, FontVariantNumeric};
32
33use super::{GlyphShapingResult, ShapedGlyph, unicode_script_to_iso15924_tag};
34use crate::platform::font::FontTable;
35use crate::{
36 AFRC, BASE, CALT, CLIG, DLIG, FRAC, Font, FontBaseline, FontTableMethods, GlyphId, HLIG, KERN,
37 LIGA, LNUM, ONUM, ORDN, PNUM, ShapedText, ShapingFlags, ShapingOptions, TNUM, ZERO,
38 fixed_to_float, float_to_fixed,
39};
40
41const HB_OT_TAG_DEFAULT_SCRIPT: hb_tag_t = u32::from_be_bytes(Tag::new(b"DFLT").to_be_bytes());
42const HB_OT_TAG_DEFAULT_LANGUAGE: hb_tag_t = u32::from_be_bytes(Tag::new(b"dflt").to_be_bytes());
43
44pub(crate) struct HarfbuzzGlyphShapingResult {
45 count: usize,
46 buffer: *mut hb_buffer_t,
47 glyph_infos: *mut hb_glyph_info_t,
48 pos_infos: *mut hb_glyph_position_t,
49}
50
51impl HarfbuzzGlyphShapingResult {
52 unsafe fn new(buffer: *mut hb_buffer_t) -> HarfbuzzGlyphShapingResult {
60 let mut glyph_count = 0;
61 let glyph_infos = unsafe { hb_buffer_get_glyph_infos(buffer, &mut glyph_count) };
62 assert!(!glyph_infos.is_null());
63 let mut pos_count = 0;
64 let pos_infos = unsafe { hb_buffer_get_glyph_positions(buffer, &mut pos_count) };
65 assert!(!pos_infos.is_null());
66 assert_eq!(glyph_count, pos_count);
67
68 HarfbuzzGlyphShapingResult {
69 count: glyph_count as usize,
70 buffer,
71 glyph_infos,
72 pos_infos,
73 }
74 }
75}
76
77impl Drop for HarfbuzzGlyphShapingResult {
78 fn drop(&mut self) {
79 unsafe { hb_buffer_destroy(self.buffer) }
80 }
81}
82
83struct ShapedGlyphIterator<'a> {
84 shaped_glyph_data: &'a HarfbuzzGlyphShapingResult,
85 current_glyph_offset: usize,
86 y_position: Au,
87}
88
89impl<'a> Iterator for ShapedGlyphIterator<'a> {
90 type Item = ShapedGlyph;
91
92 fn next(&mut self) -> Option<Self::Item> {
93 if self.current_glyph_offset >= self.shaped_glyph_data.count {
94 return None;
95 }
96
97 unsafe {
98 let glyph_info_i = self
99 .shaped_glyph_data
100 .glyph_infos
101 .add(self.current_glyph_offset);
102 let pos_info_i = self
103 .shaped_glyph_data
104 .pos_infos
105 .add(self.current_glyph_offset);
106 let x_offset = Shaper::fixed_to_float((*pos_info_i).x_offset);
107 let y_offset = Shaper::fixed_to_float((*pos_info_i).y_offset);
108 let x_advance = Shaper::fixed_to_float((*pos_info_i).x_advance);
109 let y_advance = Shaper::fixed_to_float((*pos_info_i).y_advance);
110
111 let x_offset = Au::from_f64_px(x_offset);
112 let y_offset = Au::from_f64_px(y_offset);
113 let x_advance = Au::from_f64_px(x_advance);
114 let y_advance = Au::from_f64_px(y_advance);
115
116 let offset = if x_offset.is_zero() && y_offset.is_zero() && y_advance.is_zero() {
117 None
118 } else {
119 if y_advance > Au::zero() {
121 self.y_position -= y_advance;
122 }
123
124 Some(Point2D::new(x_offset, self.y_position - y_offset))
125 };
126
127 self.current_glyph_offset += 1;
128 Some(ShapedGlyph {
129 glyph_id: (*glyph_info_i).codepoint as GlyphId,
130 string_byte_offset: (*glyph_info_i).cluster as usize,
131 advance: x_advance,
132 offset,
133 })
134 }
135 }
136}
137
138impl GlyphShapingResult for HarfbuzzGlyphShapingResult {
139 #[inline]
140 fn len(&self) -> usize {
141 self.count
142 }
143
144 fn iter(&self) -> impl Iterator<Item = ShapedGlyph> {
145 ShapedGlyphIterator {
146 shaped_glyph_data: self,
147 current_glyph_offset: 0,
148 y_position: Au::zero(),
149 }
150 }
151
152 fn is_rtl(&self) -> bool {
153 if self.count == 0 {
154 return false;
155 }
156 unsafe {
160 let first_glyph_info = self.glyph_infos.add(0);
161 let last_glyph_info = self.glyph_infos.add(self.count - 1);
162 (*last_glyph_info).cluster < (*first_glyph_info).cluster
163 }
164 }
165}
166
167#[derive(Debug)]
168pub(crate) struct Shaper {
169 hb_face: *mut hb_face_t,
170 hb_font: *mut hb_font_t,
171 font: *const Font,
172}
173
174unsafe impl Sync for Shaper {}
178unsafe impl Send for Shaper {}
179
180impl Drop for Shaper {
181 fn drop(&mut self) {
182 unsafe {
183 assert!(!self.hb_face.is_null());
184 hb_face_destroy(self.hb_face);
185
186 assert!(!self.hb_font.is_null());
187 hb_font_destroy(self.hb_font);
188 }
189 }
190}
191
192impl Shaper {
193 pub(crate) fn new(font: &Font) -> Shaper {
194 unsafe {
195 let hb_face: *mut hb_face_t = hb_face_create_for_tables(
196 Some(font_table_func),
197 font as *const Font as *mut c_void,
198 None,
199 );
200 let hb_font: *mut hb_font_t = hb_font_create(hb_face);
201
202 let pt_size = font.descriptor.pt_size.to_f64_px();
204 hb_font_set_ppem(hb_font, pt_size as c_uint, pt_size as c_uint);
205
206 hb_font_set_scale(
208 hb_font,
209 Shaper::float_to_fixed(pt_size) as c_int,
210 Shaper::float_to_fixed(pt_size) as c_int,
211 );
212
213 hb_font_set_funcs(
215 hb_font,
216 HB_FONT_FUNCS.0,
217 font as *const Font as *mut c_void,
218 None,
219 );
220
221 if servo_config::pref!(layout_variable_fonts_enabled) {
222 let variations = &font.variations();
223 if !variations.is_empty() {
224 let variations: Vec<_> = variations
225 .iter()
226 .map(|variation| hb_variation_t {
227 tag: variation.tag,
228
229 value: variation.value,
230 })
231 .collect();
232
233 hb_font_set_variations(hb_font, variations.as_ptr(), variations.len() as u32);
234 }
235 }
236
237 Shaper {
238 hb_face,
239 hb_font,
240 font,
241 }
242 }
243 }
244
245 fn shaped_glyph_data(
247 &self,
248 text: &str,
249 options: &ShapingOptions,
250 ) -> HarfbuzzGlyphShapingResult {
251 unsafe {
252 let hb_buffer: *mut hb_buffer_t = hb_buffer_create();
253 hb_buffer_set_cluster_level(hb_buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
254 hb_buffer_set_direction(
255 hb_buffer,
256 if options.flags.contains(ShapingFlags::RTL_FLAG) {
257 HB_DIRECTION_RTL
258 } else {
259 HB_DIRECTION_LTR
260 },
261 );
262
263 let script =
264 hb_script_from_iso15924_tag(unicode_script_to_iso15924_tag(options.script));
265 hb_buffer_set_script(hb_buffer, script);
266
267 hb_buffer_add_utf8(
268 hb_buffer,
269 text.as_ptr() as *const c_char,
270 text.len() as c_int,
271 0,
272 text.len() as c_int,
273 );
274
275 let language = options.language;
276 let hb_language = hb_language_from_string(
277 language.as_str().as_ptr() as *const c_char,
278 language.as_str().len() as c_int,
279 );
280 hb_buffer_set_language(hb_buffer, hb_language);
281
282 let mut features = Vec::new();
283
284 let mut add_feature = |tag: Tag, value: u32| {
285 features.push(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 };
292
293 if options.ligatures == FontVariantLigatures::NORMAL {
294 add_feature(LIGA, 1);
295 add_feature(CLIG, 1);
296 } else if options.ligatures == FontVariantLigatures::NONE {
297 add_feature(LIGA, 0);
298 add_feature(CLIG, 0);
299 add_feature(DLIG, 0);
300 add_feature(HLIG, 0);
301 add_feature(CALT, 0);
302 } else {
303 if options
304 .ligatures
305 .contains(FontVariantLigatures::COMMON_LIGATURES)
306 {
307 add_feature(LIGA, 1);
308 add_feature(CLIG, 1);
309 } else if options
310 .ligatures
311 .contains(FontVariantLigatures::NO_COMMON_LIGATURES)
312 {
313 add_feature(LIGA, 0);
314 add_feature(CLIG, 0);
315 }
316
317 if options
318 .ligatures
319 .contains(FontVariantLigatures::DISCRETIONARY_LIGATURES)
320 {
321 add_feature(DLIG, 1);
322 } else if options
323 .ligatures
324 .contains(FontVariantLigatures::NO_DISCRETIONARY_LIGATURES)
325 {
326 add_feature(DLIG, 0);
327 }
328
329 if options
330 .ligatures
331 .contains(FontVariantLigatures::HISTORICAL_LIGATURES)
332 {
333 add_feature(HLIG, 1);
334 } else if options
335 .ligatures
336 .contains(FontVariantLigatures::NO_HISTORICAL_LIGATURES)
337 {
338 add_feature(HLIG, 0);
339 }
340
341 if options.ligatures.contains(FontVariantLigatures::CONTEXTUAL) {
342 add_feature(CALT, 1);
343 } else if options
344 .ligatures
345 .contains(FontVariantLigatures::NO_CONTEXTUAL)
346 {
347 add_feature(CALT, 0);
348 }
349 }
350
351 if options.numeric.contains(FontVariantNumeric::LINING_NUMS) {
352 add_feature(LNUM, 1);
353 } else if options.numeric.contains(FontVariantNumeric::OLDSTYLE_NUMS) {
354 add_feature(ONUM, 1);
355 }
356 if options
357 .numeric
358 .contains(FontVariantNumeric::PROPORTIONAL_NUMS)
359 {
360 add_feature(PNUM, 1);
361 } else if options.numeric.contains(FontVariantNumeric::TABULAR_NUMS) {
362 add_feature(TNUM, 1);
363 }
364 if options
365 .numeric
366 .contains(FontVariantNumeric::DIAGONAL_FRACTIONS)
367 {
368 add_feature(FRAC, 1);
369 } else if options
370 .numeric
371 .contains(FontVariantNumeric::STACKED_FRACTIONS)
372 {
373 add_feature(AFRC, 1);
374 }
375 if options.numeric.contains(FontVariantNumeric::ORDINAL) {
376 add_feature(ORDN, 1);
377 }
378 if options.numeric.contains(FontVariantNumeric::SLASHED_ZERO) {
379 add_feature(ZERO, 1);
380 }
381
382 if options
383 .flags
384 .contains(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
385 {
386 features.push(hb_feature_t {
387 tag: u32::from_be_bytes(KERN.to_be_bytes()),
388 value: 0,
389 start: 0,
390 end: hb_buffer_get_length(hb_buffer),
391 })
392 }
393
394 hb_shape(
395 self.hb_font,
396 hb_buffer,
397 features.as_mut_ptr(),
398 features.len() as u32,
399 );
400
401 HarfbuzzGlyphShapingResult::new(hb_buffer)
402 }
403 }
404
405 pub(crate) fn shape_text(&self, text: &str, options: &ShapingOptions) -> ShapedText {
406 ShapedText::with_shaped_glyph_data(text, options, &self.shaped_glyph_data(text, options))
407 }
408
409 pub(crate) fn baseline(&self) -> Option<FontBaseline> {
410 unsafe { (*self.font).table_for_tag(BASE)? };
411
412 let mut hanging_baseline = 0;
413 let mut alphabetic_baseline = 0;
414 let mut ideographic_baseline = 0;
415
416 unsafe {
417 hb_ot_layout_get_baseline(
418 self.hb_font,
419 HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
420 HB_DIRECTION_LTR,
421 HB_OT_TAG_DEFAULT_SCRIPT,
422 HB_OT_TAG_DEFAULT_LANGUAGE,
423 &mut alphabetic_baseline as *mut _,
424 );
425
426 hb_ot_layout_get_baseline(
427 self.hb_font,
428 HB_OT_LAYOUT_BASELINE_TAG_HANGING,
429 HB_DIRECTION_LTR,
430 HB_OT_TAG_DEFAULT_SCRIPT,
431 HB_OT_TAG_DEFAULT_LANGUAGE,
432 &mut hanging_baseline as *mut _,
433 );
434
435 hb_ot_layout_get_baseline(
436 self.hb_font,
437 HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
438 HB_DIRECTION_LTR,
439 HB_OT_TAG_DEFAULT_SCRIPT,
440 HB_OT_TAG_DEFAULT_LANGUAGE,
441 &mut ideographic_baseline as *mut _,
442 );
443 }
444
445 Some(FontBaseline {
446 ideographic_baseline: Shaper::fixed_to_float(ideographic_baseline) as f32,
447 alphabetic_baseline: Shaper::fixed_to_float(alphabetic_baseline) as f32,
448 hanging_baseline: Shaper::fixed_to_float(hanging_baseline) as f32,
449 })
450 }
451
452 fn float_to_fixed(f: f64) -> i32 {
453 float_to_fixed(16, f)
454 }
455
456 fn fixed_to_float(i: hb_position_t) -> f64 {
457 fixed_to_float(16, i)
458 }
459}
460
461struct FontFuncs(*mut hb_font_funcs_t);
463
464unsafe impl Sync for FontFuncs {}
465unsafe impl Send for FontFuncs {}
466
467static HB_FONT_FUNCS: LazyLock<FontFuncs> = LazyLock::new(|| unsafe {
468 let hb_funcs = hb_font_funcs_create();
469 hb_font_funcs_set_nominal_glyph_func(hb_funcs, Some(glyph_func), ptr::null_mut(), None);
470 hb_font_funcs_set_glyph_h_advance_func(
471 hb_funcs,
472 Some(glyph_h_advance_func),
473 ptr::null_mut(),
474 None,
475 );
476
477 FontFuncs(hb_funcs)
478});
479
480extern "C" fn glyph_func(
481 _: *mut hb_font_t,
482 font_data: *mut c_void,
483 unicode: hb_codepoint_t,
484 glyph: *mut hb_codepoint_t,
485 _: *mut c_void,
486) -> hb_bool_t {
487 let font: *const Font = font_data as *const Font;
488 assert!(!font.is_null());
489
490 match unsafe { (*font).glyph_index(char::from_u32(unicode).unwrap()) } {
491 Some(g) => {
492 unsafe { *glyph = g as hb_codepoint_t };
493 true as hb_bool_t
494 },
495 None => false as hb_bool_t,
496 }
497}
498
499extern "C" fn glyph_h_advance_func(
500 _: *mut hb_font_t,
501 font_data: *mut c_void,
502 glyph: hb_codepoint_t,
503 _: *mut c_void,
504) -> hb_position_t {
505 let font: *mut Font = font_data as *mut Font;
506 assert!(!font.is_null());
507
508 let advance = unsafe { (*font).glyph_h_advance(glyph as GlyphId) };
509 Shaper::float_to_fixed(advance)
510}
511
512extern "C" fn font_table_func(
514 _: *mut hb_face_t,
515 tag: hb_tag_t,
516 user_data: *mut c_void,
517) -> *mut hb_blob_t {
518 let font = user_data as *const Font;
520 assert!(!font.is_null());
521
522 let Some(font_table) = (unsafe { (*font).table_for_tag(Tag::from_u32(tag)) }) else {
524 return ptr::null_mut();
525 };
526
527 let font_table_ptr = Box::into_raw(Box::new(font_table));
531
532 let buf = unsafe { (*font_table_ptr).buffer() };
533 let blob = unsafe {
535 hb_blob_create(
536 buf.as_ptr() as *const c_char,
537 buf.len() as c_uint,
538 HB_MEMORY_MODE_READONLY,
539 font_table_ptr as *mut c_void,
540 Some(destroy_blob_func),
541 )
542 };
543
544 assert!(!blob.is_null());
545 blob
546}
547
548extern "C" fn destroy_blob_func(font_table_ptr: *mut c_void) {
549 unsafe {
550 drop(Box::from_raw(font_table_ptr as *mut FontTable));
551 }
552}