Simplify FontHandle and rename it to PlatformFont (#32101)

* Simplify `FontHandle` and rename it to `PlatformFont`

Rename it to `PlatformFont` and move the `FontTemplate` member to
`Font`, because it's shared by all platforms.

* Update components/gfx/platform/freetype/font.rs

Co-authored-by: Mukilan Thiyagarajan <mukilanthiagarajan@gmail.com>

* Fix build for MacOS and Windows

---------

Co-authored-by: Mukilan Thiyagarajan <mukilanthiagarajan@gmail.com>
This commit is contained in:
Martin Robinson 2024-04-17 19:44:34 +02:00 committed by GitHub
parent e9e46f4c0b
commit 5393d30a8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 71 additions and 80 deletions

View file

@ -8,6 +8,7 @@
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use app_units::Au;
use dwrote::{Font, FontFace, FontFile, FontStretch, FontStyle};
@ -18,7 +19,7 @@ use style::values::computed::font::FontStyle as StyleFontStyle;
use style::values::specified::font::FontStretchKeyword;
use crate::font::{
FontHandleMethods, FontMetrics, FontTableMethods, FontTableTag, FractionalPixel,
FontMetrics, FontTableMethods, FontTableTag, FractionalPixel, PlatformFontMethods,
};
use crate::font_cache_thread::FontIdentifier;
use crate::font_template::{FontTemplateRef, FontTemplateRefMethods};
@ -198,9 +199,11 @@ impl FontInfo {
}
#[derive(Debug)]
pub struct FontHandle {
font_template: FontTemplateRef,
pub struct PlatformFont {
face: Nondebug<FontFace>,
/// A reference to this data used to create this [`PlatformFont`], ensuring the
/// data stays alive of the lifetime of this struct.
data: Option<Arc<Vec<u8>>>,
info: FontInfo,
em_size: f32,
du_to_px: f32,
@ -222,9 +225,7 @@ impl<T> Deref for Nondebug<T> {
}
}
impl FontHandle {}
impl FontHandleMethods for FontHandle {
impl PlatformFontMethods for PlatformFont {
fn new_from_template(
font_template: FontTemplateRef,
pt_size: Option<Au>,
@ -234,8 +235,12 @@ impl FontHandleMethods for FontHandle {
FontIdentifier::Web(_) => None,
};
let (face, info) = match direct_write_font {
Some(font) => (font.create_font_face(), FontInfo::new_from_font(&font)?),
let (face, info, data) = match direct_write_font {
Some(font) => (
font.create_font_face(),
FontInfo::new_from_font(&font)?,
None,
),
None => {
let bytes = font_template.data();
let font_file =
@ -244,7 +249,7 @@ impl FontHandleMethods for FontHandle {
.create_face(0, dwrote::DWRITE_FONT_SIMULATIONS_NONE)
.map_err(|_| "Could not create FontFace")?;
let info = FontInfo::new_from_face(&face)?;
(face, info)
(face, info, font_template.borrow().data_if_in_memory())
},
};
@ -257,9 +262,9 @@ impl FontHandleMethods for FontHandle {
let design_units_to_pixels = 1. / design_units_per_pixel;
let scaled_design_units_to_pixels = em_size / design_units_per_pixel;
Ok(FontHandle {
font_template,
Ok(PlatformFont {
face: Nondebug(face),
data,
info,
em_size,
du_to_px: design_units_to_pixels,
@ -267,10 +272,6 @@ impl FontHandleMethods for FontHandle {
})
}
fn template(&self) -> FontTemplateRef {
self.font_template.clone()
}
fn family_name(&self) -> Option<String> {
Some(self.info.family_name.clone())
}