fonts: Stop using deprecated functions in dwrote (#38856)

The new version of the functions return `Result` types.

Testing: No tests because this should not change behavior.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-22 09:28:54 -07:00 committed by GitHub
parent 777373054f
commit bf5da330e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 19 deletions

View file

@ -197,28 +197,36 @@ impl PlatformFontMethods for PlatformFont {
} }
fn glyph_index(&self, codepoint: char) -> Option<GlyphId> { fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {
let glyph = self.face.get_glyph_indices(&[codepoint as u32])[0]; let Ok(glyphs) = self.face.glyph_indices(&[codepoint as u32]) else {
if glyph == 0 { return None;
};
let Some(glyph) = glyphs.first() else {
return None;
};
if *glyph == 0 {
return None; return None;
} }
Some(glyph as GlyphId) Some(*glyph as GlyphId)
} }
fn glyph_h_advance(&self, glyph: GlyphId) -> Option<FractionalPixel> { fn glyph_h_advance(&self, glyph: GlyphId) -> Option<FractionalPixel> {
if glyph == 0 { if glyph == 0 {
return None; return None;
} }
let Ok(metrics) = self.face.design_glyph_metrics(&[glyph as u16], false) else {
let gm = self.face.get_design_glyph_metrics(&[glyph as u16], false)[0]; return None;
let f = (gm.advanceWidth as f32 * self.scaled_du_to_px) as FractionalPixel; };
let Some(glyph_metric) = metrics.first() else {
Some(f) return None;
};
Some((glyph_metric.advanceWidth as f32 * self.scaled_du_to_px) as FractionalPixel)
} }
fn glyph_h_kerning(&self, first_glyph: GlyphId, second_glyph: GlyphId) -> FractionalPixel { fn glyph_h_kerning(&self, first_glyph: GlyphId, second_glyph: GlyphId) -> FractionalPixel {
let adjustment = self let adjustment = self
.face .face
.get_glyph_pair_kerning_adjustment(first_glyph as u16, second_glyph as u16); .glyph_pair_kerning_adjustment(first_glyph as u16, second_glyph as u16)
.unwrap_or_default();
(adjustment as f32 * self.scaled_du_to_px) as FractionalPixel (adjustment as f32 * self.scaled_du_to_px) as FractionalPixel
} }
@ -279,7 +287,9 @@ impl PlatformFontMethods for PlatformFont {
// tag bytes, which means that `u32::swap_bytes` must be called here in order to // tag bytes, which means that `u32::swap_bytes` must be called here in order to
// use a byte order compatible with the rest of Servo. // use a byte order compatible with the rest of Servo.
self.face self.face
.get_font_table(u32::swap_bytes(tag)) .font_table(u32::swap_bytes(tag))
.ok()
.flatten()
.map(|bytes| FontTable { data: bytes }) .map(|bytes| FontTable { data: bytes })
} }
@ -288,10 +298,12 @@ impl PlatformFontMethods for PlatformFont {
} }
fn typographic_bounds(&self, glyph_id: GlyphId) -> Rect<f32> { fn typographic_bounds(&self, glyph_id: GlyphId) -> Rect<f32> {
let metrics = self let Ok(metrics) = self.face.design_glyph_metrics(&[glyph_id as u16], false) else {
.face return Rect::zero();
.get_design_glyph_metrics(&[glyph_id as u16], false); };
let metrics = &metrics[0]; let Some(metrics) = metrics.first() else {
return Rect::zero();
};
let advance_width = metrics.advanceWidth as f32; let advance_width = metrics.advanceWidth as f32;
let advance_height = metrics.advanceHeight as f32; let advance_height = metrics.advanceHeight as f32;
let left_side_bearing = metrics.leftSideBearing as f32; let left_side_bearing = metrics.leftSideBearing as f32;

View file

@ -56,10 +56,12 @@ impl LocalFontIdentifier {
.expect("Could not create Font from FontDescriptor") .expect("Could not create Font from FontDescriptor")
.create_font_face(); .create_font_face();
let path = face let path = face
.get_files() .files()
.first() .ok()
.and_then(|files| files.first().cloned())
.expect("Could not get FontFace files") .expect("Could not get FontFace files")
.get_font_file_path() .font_file_path()
.ok()
.expect("Could not get FontFace files path"); .expect("Could not get FontFace files path");
NativeFontHandle { NativeFontHandle {
path, path,
@ -73,10 +75,10 @@ impl LocalFontIdentifier {
.ok()??; .ok()??;
let face = font.create_font_face(); let face = font.create_font_face();
let index = face.get_index(); let index = face.get_index();
let files = face.get_files(); let files = face.files().ok()?;
assert!(!files.is_empty()); assert!(!files.is_empty());
let data = files[0].get_font_file_bytes(); let data = files[0].font_file_bytes().ok()?;
let data = FontData::from_bytes(&data); let data = FontData::from_bytes(&data);
Some(FontDataAndIndex { data, index }) Some(FontDataAndIndex { data, index })