aboutsummaryrefslogtreecommitdiffstats
path: root/components/fonts/shaper.rs
diff options
context:
space:
mode:
authorChocolate Pie <106949016+chocolate-pie@users.noreply.github.com>2024-07-18 04:20:18 +0900
committerGitHub <noreply@github.com>2024-07-17 19:20:18 +0000
commit122333554768d69789a08df25c0bcde3ddd1aa4c (patch)
tree7fc6eeead1345171f7d62f39ae8cd2f36695ff44 /components/fonts/shaper.rs
parentd82232d549a880aaa1b5613e22ca4f7ec9593d74 (diff)
downloadservo-122333554768d69789a08df25c0bcde3ddd1aa4c.tar.gz
servo-122333554768d69789a08df25c0bcde3ddd1aa4c.zip
enhance: Implement `CanvasRenderingContext2D.measureText` (#32704)
Signed-off-by: Chocolate Pie <106949016+chocolate-pie@users.noreply.github.com> Co-authored-by: Martin Robinson <mrobinson@igalia.com>
Diffstat (limited to 'components/fonts/shaper.rs')
-rw-r--r--components/fonts/shaper.rs55
1 files changed, 51 insertions, 4 deletions
diff --git a/components/fonts/shaper.rs b/components/fonts/shaper.rs
index 72d47de28fb..f5dbb5b0c74 100644
--- a/components/fonts/shaper.rs
+++ b/components/fonts/shaper.rs
@@ -19,20 +19,24 @@ use harfbuzz_sys::{
hb_face_create_for_tables, hb_face_destroy, hb_face_t, hb_feature_t, hb_font_create,
hb_font_destroy, hb_font_funcs_create, hb_font_funcs_set_glyph_h_advance_func,
hb_font_funcs_set_nominal_glyph_func, hb_font_funcs_t, hb_font_set_funcs, hb_font_set_ppem,
- hb_font_set_scale, hb_font_t, hb_glyph_info_t, hb_glyph_position_t, hb_position_t, hb_shape,
- hb_tag_t, HB_DIRECTION_LTR, HB_DIRECTION_RTL, HB_MEMORY_MODE_READONLY,
+ hb_font_set_scale, hb_font_t, hb_glyph_info_t, hb_glyph_position_t, hb_ot_layout_get_baseline,
+ hb_position_t, hb_shape, hb_tag_t, HB_DIRECTION_LTR, HB_DIRECTION_RTL, HB_MEMORY_MODE_READONLY,
+ HB_OT_LAYOUT_BASELINE_TAG_HANGING, HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
+ HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
};
use lazy_static::lazy_static;
use log::debug;
use crate::platform::font::FontTable;
use crate::{
- fixed_to_float, float_to_fixed, ot_tag, ByteIndex, Font, FontTableMethods, FontTableTag,
- GlyphData, GlyphId, GlyphStore, ShapingFlags, ShapingOptions, KERN,
+ fixed_to_float, float_to_fixed, ot_tag, ByteIndex, Font, FontBaseline, FontTableMethods,
+ FontTableTag, GlyphData, GlyphId, GlyphStore, ShapingFlags, ShapingOptions, BASE, KERN,
};
const NO_GLYPH: i32 = -1;
const LIGA: u32 = ot_tag!('l', 'i', 'g', 'a');
+const HB_OT_TAG_DEFAULT_SCRIPT: u32 = ot_tag!('D', 'F', 'L', 'T');
+const HB_OT_TAG_DEFAULT_LANGUAGE: u32 = ot_tag!('d', 'f', 'l', 't');
pub struct ShapedGlyphData {
count: usize,
@@ -606,6 +610,49 @@ impl Shaper {
advance
}
+
+ pub unsafe fn get_baseline(&self) -> Option<FontBaseline> {
+ if (*self.font).table_for_tag(BASE).is_none() {
+ return None;
+ }
+
+ let mut hanging_baseline = 0;
+ let mut alphabetic_baseline = 0;
+ let mut ideographic_baseline = 0;
+
+ hb_ot_layout_get_baseline(
+ self.hb_font,
+ HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
+ HB_DIRECTION_LTR,
+ HB_OT_TAG_DEFAULT_SCRIPT,
+ HB_OT_TAG_DEFAULT_LANGUAGE,
+ &mut alphabetic_baseline as *mut _,
+ );
+
+ hb_ot_layout_get_baseline(
+ self.hb_font,
+ HB_OT_LAYOUT_BASELINE_TAG_HANGING,
+ HB_DIRECTION_LTR,
+ HB_OT_TAG_DEFAULT_SCRIPT,
+ HB_OT_TAG_DEFAULT_LANGUAGE,
+ &mut hanging_baseline as *mut _,
+ );
+
+ hb_ot_layout_get_baseline(
+ self.hb_font,
+ HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
+ HB_DIRECTION_LTR,
+ HB_OT_TAG_DEFAULT_SCRIPT,
+ HB_OT_TAG_DEFAULT_LANGUAGE,
+ &mut ideographic_baseline as *mut _,
+ );
+
+ Some(FontBaseline {
+ ideographic_baseline: Shaper::fixed_to_float(ideographic_baseline) as f32,
+ alphabetic_baseline: Shaper::fixed_to_float(alphabetic_baseline) as f32,
+ hanging_baseline: Shaper::fixed_to_float(hanging_baseline) as f32,
+ })
+ }
}
/// Callbacks from Harfbuzz when font map and glyph advance lookup needed.