aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbors-servo <infra@servo.org>2023-01-03 19:43:48 -0500
committerGitHub <noreply@github.com>2023-01-03 19:43:48 -0500
commit9769f91709c970adb9f806e3c12d328f9112efae (patch)
treeedd5775865ebcf6cf5caa0054bf559b9c02e6eab
parentb375297465a6d07561d526ed84106196a5eee72f (diff)
parent0ac2cb08dab04604c142b997c17ab3471704b667 (diff)
downloadservo-9769f91709c970adb9f806e3c12d328f9112efae.tar.gz
servo-9769f91709c970adb9f806e3c12d328f9112efae.zip
Auto merge of #29141 - IvanUkhov:truetype, r=jdm
Update truetype - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] There are tests for these changes
-rw-r--r--Cargo.lock13
-rw-r--r--components/gfx/Cargo.toml2
-rw-r--r--components/gfx/platform/windows/font.rs75
3 files changed, 32 insertions, 58 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b74f6786855..8137effeced 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6839,9 +6839,12 @@ checksum = "ce607aae8ab0ab3abf3a2723a9ab6f09bb8639ed83fdd888d857b8e556c868d8"
[[package]]
name = "truetype"
-version = "0.30.1"
+version = "0.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "063aa7cf1a7e56677e639c8f180a84d30b8fd788a684cfee2460418629925730"
+checksum = "7c628aaf2721bbf3082a712649f8735d146f8752189ad5fcbeac4b8a2d9c03cd"
+dependencies = [
+ "typeface",
+]
[[package]]
name = "try-lock"
@@ -6882,6 +6885,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae"
[[package]]
+name = "typeface"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "716f008359b511ef53d91e103efe33184a417619862736dcc9725126b792ae12"
+
+[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/components/gfx/Cargo.toml b/components/gfx/Cargo.toml
index 7c00251da59..b6c9a00df1d 100644
--- a/components/gfx/Cargo.toml
+++ b/components/gfx/Cargo.toml
@@ -59,4 +59,4 @@ xml-rs = "0.8"
[target.'cfg(target_os = "windows")'.dependencies]
dwrote = "0.11"
-truetype = "0.30"
+truetype = { version = "0.40.0", features = ["ignore-invalid-language-ids"] }
diff --git a/components/gfx/platform/windows/font.rs b/components/gfx/platform/windows/font.rs
index 8154c066714..5b41b0c21f0 100644
--- a/components/gfx/platform/windows/font.rs
+++ b/components/gfx/platform/windows/font.rs
@@ -65,39 +65,6 @@ fn make_tag(tag_bytes: &[u8]) -> FontTableTag {
macro_rules! try_lossy(($result:expr) => ($result.map_err(|_| (()))?));
-// Given a set of records, figure out the string indices for the family and face
-// names. We want name_id 1 and 2, and we need to use platform_id == 1 and
-// language_id == 0 to avoid limitations in the truetype crate. We *could* just
-// do our own parsing here, and use the offset/length data and pull the values out
-// ourselves.
-fn get_family_face_indices(records: &[truetype::naming_table::Record]) -> Option<(usize, usize)> {
- let mut family_name_index = None;
- let mut face_name_index = None;
-
- for i in 0..records.len() {
- // the truetype crate can only decode mac platform format names
- if records[i].platform_id != 1 {
- continue;
- }
-
- if records[i].language_id != 0 {
- continue;
- }
-
- if records[i].name_id == 1 {
- family_name_index = Some(i);
- } else if records[i].name_id == 2 {
- face_name_index = Some(i);
- }
- }
-
- if family_name_index.is_some() && face_name_index.is_some() {
- Some((family_name_index.unwrap(), face_name_index.unwrap()))
- } else {
- None
- }
-}
-
// We need the font (DWriteFont) in order to be able to query things like
// the family name, face name, weight, etc. On Windows 10, the
// DWriteFontFace3 interface provides this on the FontFace, but that's only
@@ -118,8 +85,10 @@ struct FontInfo {
impl FontInfo {
fn new_from_face(face: &FontFace) -> Result<FontInfo, ()> {
use std::cmp::{max, min};
+ use std::collections::HashMap;
use std::io::Cursor;
- use truetype::{NamingTable, Value, WindowsMetrics};
+ use truetype::naming_table::{NameID, NamingTable};
+ use truetype::{Value, WindowsMetrics};
let name_table_bytes = face.get_font_table(make_tag(b"name"));
let os2_table_bytes = face.get_font_table(make_tag(b"OS/2"));
@@ -129,27 +98,23 @@ impl FontInfo {
let mut name_table_cursor = Cursor::new(name_table_bytes.as_ref().unwrap());
let names = try_lossy!(NamingTable::read(&mut name_table_cursor));
- let (family, face) = match names {
- NamingTable::Format0(ref table) => {
- if let Some((family_index, face_index)) = get_family_face_indices(&table.records) {
- let strings = table.strings().unwrap();
- let family = strings[family_index].clone();
- let face = strings[face_index].clone();
- (family, face)
- } else {
- return Err(());
- }
- },
- NamingTable::Format1(ref table) => {
- if let Some((family_index, face_index)) = get_family_face_indices(&table.records) {
- let strings = table.strings().unwrap();
- let family = strings[family_index].clone();
- let face = strings[face_index].clone();
- (family, face)
- } else {
- return Err(());
- }
- },
+ let mut names: HashMap<_, _> = names
+ .iter()
+ .filter(|((_, language_tag), value)| {
+ value.is_some() &&
+ language_tag
+ .as_deref()
+ .map_or(false, |language_tag| language_tag.starts_with("en"))
+ })
+ .map(|((name_id, _), value)| (name_id, value.unwrap()))
+ .collect();
+ let family = match names.remove(&NameID::FontFamilyName) {
+ Some(family) => family,
+ _ => return Err(()),
+ };
+ let face = match names.remove(&NameID::FontSubfamilyName) {
+ Some(face) => face,
+ _ => return Err(()),
};
let mut os2_table_cursor = Cursor::new(os2_table_bytes.as_ref().unwrap());