From bf5da330e1c4521b9cd6673180ba1f22b82132de Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 22 Aug 2025 09:28:54 -0700 Subject: [PATCH] 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 --- components/fonts/platform/windows/font.rs | 40 ++++++++++++------- .../fonts/platform/windows/font_list.rs | 12 +++--- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/components/fonts/platform/windows/font.rs b/components/fonts/platform/windows/font.rs index a48fb1b8590..e31f1c4e6e0 100644 --- a/components/fonts/platform/windows/font.rs +++ b/components/fonts/platform/windows/font.rs @@ -197,28 +197,36 @@ impl PlatformFontMethods for PlatformFont { } fn glyph_index(&self, codepoint: char) -> Option { - let glyph = self.face.get_glyph_indices(&[codepoint as u32])[0]; - if glyph == 0 { + let Ok(glyphs) = self.face.glyph_indices(&[codepoint as u32]) else { + return None; + }; + let Some(glyph) = glyphs.first() else { + return None; + }; + if *glyph == 0 { return None; } - Some(glyph as GlyphId) + Some(*glyph as GlyphId) } fn glyph_h_advance(&self, glyph: GlyphId) -> Option { if glyph == 0 { return None; } - - let gm = self.face.get_design_glyph_metrics(&[glyph as u16], false)[0]; - let f = (gm.advanceWidth as f32 * self.scaled_du_to_px) as FractionalPixel; - - Some(f) + let Ok(metrics) = self.face.design_glyph_metrics(&[glyph as u16], false) else { + return None; + }; + let Some(glyph_metric) = metrics.first() else { + 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 { let adjustment = self .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 } @@ -279,7 +287,9 @@ impl PlatformFontMethods for PlatformFont { // 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. self.face - .get_font_table(u32::swap_bytes(tag)) + .font_table(u32::swap_bytes(tag)) + .ok() + .flatten() .map(|bytes| FontTable { data: bytes }) } @@ -288,10 +298,12 @@ impl PlatformFontMethods for PlatformFont { } fn typographic_bounds(&self, glyph_id: GlyphId) -> Rect { - let metrics = self - .face - .get_design_glyph_metrics(&[glyph_id as u16], false); - let metrics = &metrics[0]; + let Ok(metrics) = self.face.design_glyph_metrics(&[glyph_id as u16], false) else { + return Rect::zero(); + }; + let Some(metrics) = metrics.first() else { + return Rect::zero(); + }; let advance_width = metrics.advanceWidth as f32; let advance_height = metrics.advanceHeight as f32; let left_side_bearing = metrics.leftSideBearing as f32; diff --git a/components/fonts/platform/windows/font_list.rs b/components/fonts/platform/windows/font_list.rs index 6fdc2eac7c8..72346c618b6 100644 --- a/components/fonts/platform/windows/font_list.rs +++ b/components/fonts/platform/windows/font_list.rs @@ -56,10 +56,12 @@ impl LocalFontIdentifier { .expect("Could not create Font from FontDescriptor") .create_font_face(); let path = face - .get_files() - .first() + .files() + .ok() + .and_then(|files| files.first().cloned()) .expect("Could not get FontFace files") - .get_font_file_path() + .font_file_path() + .ok() .expect("Could not get FontFace files path"); NativeFontHandle { path, @@ -73,10 +75,10 @@ impl LocalFontIdentifier { .ok()??; let face = font.create_font_face(); let index = face.get_index(); - let files = face.get_files(); + let files = face.files().ok()?; 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); Some(FontDataAndIndex { data, index })