Skip to content

Commit 353f65f

Browse files
committed
Use font_types::Tag
Replace `ttf_parser::Tag` with `font_types::Tag` throughout the code.
1 parent 9f1bf03 commit 353f65f

21 files changed

+2032
-2113
lines changed

scripts/gen-tag-table.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ def rank_delta(bcp_47, ot):
869869

870870
print('// WARNING: this file was generated by ../scripts/gen-tag-table.py')
871871
print()
872-
print('use ttf_parser::Tag;')
872+
print('use skrifa::raw::types::Tag;')
873873
print()
874874
print('pub struct LangTag {')
875875
print(' pub language: &\'static str,')
@@ -882,8 +882,8 @@ def rank_delta(bcp_47, ot):
882882

883883
def hb_tag(tag):
884884
if tag == DEFAULT_LANGUAGE_SYSTEM:
885-
return 'Tag(0)\t '
886-
return 'Tag::from_bytes(b\"%s%s%s%s\")' % tuple(('%-4s' % tag)[:4])
885+
return 'Tag::new(&[0; 4])\t '
886+
return 'Tag::new(b\"%s%s%s%s\")' % tuple(('%-4s' % tag)[:4])
887887

888888

889889
def hb_tag2(tag):

src/hb/aat_layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl hb_aat_feature_mapping_t {
393393
selector_to_disable: u8,
394394
) -> Self {
395395
hb_aat_feature_mapping_t {
396-
ot_feature_tag: hb_tag_t::from_bytes(ot_feature_tag),
396+
ot_feature_tag: hb_tag_t::new(ot_feature_tag),
397397
aat_feature_type,
398398
selector_to_enable,
399399
selector_to_disable,

src/hb/aat_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl hb_aat_map_builder_t {
9898
pub fn add_feature(&mut self, face: &hb_font_t, feature: &Feature) -> Option<()> {
9999
let feat = face.tables().feat?;
100100

101-
if feature.tag == hb_tag_t::from_bytes(b"aalt") {
101+
if feature.tag == hb_tag_t::new(b"aalt") {
102102
let exposes_feature = feat
103103
.names
104104
.find(HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES as u16)

src/hb/common.rs

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::string::String;
22
use core::ops::{Bound, RangeBounds};
33

4-
use ttf_parser::Tag;
4+
use skrifa::raw::types::Tag;
55

66
use super::text_parser::TextParser;
77

@@ -215,7 +215,7 @@ pub struct Script(pub(crate) Tag);
215215
impl Script {
216216
#[inline]
217217
pub(crate) const fn from_bytes(bytes: &[u8; 4]) -> Self {
218-
Script(Tag::from_bytes(bytes))
218+
Script(Tag::new(bytes))
219219
}
220220

221221
/// Converts an ISO 15924 script tag to a corresponding `Script`.
@@ -225,9 +225,9 @@ impl Script {
225225
}
226226

227227
// Be lenient, adjust case (one capital letter followed by three small letters).
228-
let tag = Tag((tag.as_u32() & 0xDFDFDFDF) | 0x00202020);
228+
let tag = Tag::from_u32((tag.as_u32() & 0xDFDFDFDF) | 0x00202020);
229229

230-
match &tag.to_bytes() {
230+
match &tag.to_be_bytes() {
231231
// These graduated from the 'Q' private-area codes, but
232232
// the old code is still aliased by Unicode, and the Qaai
233233
// one in use by ICU.
@@ -628,7 +628,7 @@ mod tests_features {
628628
fn $name() {
629629
assert_eq!(
630630
Feature::from_str($text).unwrap(),
631-
Feature::new(Tag::from_bytes($tag), $value, $range)
631+
Feature::new(Tag::new($tag), $value, $range)
632632
);
633633
}
634634
};
@@ -703,6 +703,9 @@ impl core::str::FromStr for Variation {
703703
}
704704

705705
pub trait TagExt {
706+
fn from_bytes_lossy(bytes: &[u8]) -> Self;
707+
fn as_u32(self) -> u32;
708+
fn is_null(self) -> bool;
706709
fn default_script() -> Self;
707710
fn default_language() -> Self;
708711
#[cfg(test)]
@@ -711,22 +714,38 @@ pub trait TagExt {
711714
}
712715

713716
impl TagExt for Tag {
717+
fn from_bytes_lossy(bytes: &[u8]) -> Self {
718+
let mut array = [b' '; 4];
719+
for (src, dest) in bytes.iter().zip(&mut array) {
720+
*dest = *src;
721+
}
722+
Tag::new(&array)
723+
}
724+
725+
fn as_u32(self) -> u32 {
726+
u32::from_be_bytes(self.to_be_bytes())
727+
}
728+
729+
fn is_null(self) -> bool {
730+
self.to_be_bytes() == [0, 0, 0, 0]
731+
}
732+
714733
#[inline]
715734
fn default_script() -> Self {
716-
Tag::from_bytes(b"DFLT")
735+
Tag::new(b"DFLT")
717736
}
718737

719738
#[inline]
720739
fn default_language() -> Self {
721-
Tag::from_bytes(b"dflt")
740+
Tag::new(b"dflt")
722741
}
723742

724743
/// Converts tag to lowercase.
725744
#[cfg(test)]
726745
#[inline]
727746
fn to_lowercase(&self) -> Self {
728-
let b = self.to_bytes();
729-
Tag::from_bytes(&[
747+
let b = self.to_be_bytes();
748+
Tag::new(&[
730749
b[0].to_ascii_lowercase(),
731750
b[1].to_ascii_lowercase(),
732751
b[2].to_ascii_lowercase(),
@@ -737,8 +756,8 @@ impl TagExt for Tag {
737756
/// Converts tag to uppercase.
738757
#[inline]
739758
fn to_uppercase(&self) -> Self {
740-
let b = self.to_bytes();
741-
Tag::from_bytes(&[
759+
let b = self.to_be_bytes();
760+
Tag::new(&[
742761
b[0].to_ascii_uppercase(),
743762
b[1].to_ascii_uppercase(),
744763
b[2].to_ascii_uppercase(),

src/hb/face.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ttf_parser::{GlyphId, RgbaColor};
88

99
use super::buffer::GlyphPropsFlags;
1010
use super::fonta;
11+
use super::common::TagExt;
1112
use super::ot_layout::TableIndex;
1213
use crate::Variation;
1314

@@ -81,7 +82,7 @@ impl<'a> hb_font_t<'a> {
8182
/// Sets font variations.
8283
pub fn set_variations(&mut self, variations: &[Variation]) {
8384
for variation in variations {
84-
self.ttfp_face.set_variation(variation.tag, variation.value);
85+
self.ttfp_face.set_variation(ttf_parser::Tag(variation.tag.as_u32()), variation.value);
8586
}
8687
self.font.set_coords(self.ttfp_face.variation_coordinates());
8788
}

src/hb/fonta/ot/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ pub enum LayoutTable<'a> {
168168
Gpos(Gpos<'a>),
169169
}
170170

171-
fn conv_tag(tag: hb_tag_t) -> skrifa::raw::types::Tag {
172-
skrifa::raw::types::Tag::from_u32(tag.0)
173-
}
174-
175171
impl<'a> LayoutTable<'a> {
176172
fn script_list(&self) -> Option<ScriptList<'a>> {
177173
match self {
@@ -199,7 +195,6 @@ impl<'a> LayoutTable<'a> {
199195

200196
fn script_index(&self, tag: hb_tag_t) -> Option<u16> {
201197
let list = self.script_list()?;
202-
let tag = conv_tag(tag);
203198
list.script_records()
204199
.binary_search_by_key(&tag, |rec| rec.script_tag())
205200
.map(|index| index as u16)
@@ -214,7 +209,6 @@ impl<'a> LayoutTable<'a> {
214209

215210
fn langsys_index(&self, script_index: u16, tag: hb_tag_t) -> Option<u16> {
216211
let script = self.script(script_index)?;
217-
let tag = conv_tag(tag);
218212
script
219213
.lang_sys_records()
220214
.binary_search_by_key(&tag, |rec| rec.lang_sys_tag())
@@ -241,7 +235,7 @@ impl<'a> LayoutTable<'a> {
241235
fn feature_tag(&self, index: u16) -> Option<hb_tag_t> {
242236
let list = self.feature_list()?;
243237
let record = list.feature_records().get(index as usize)?;
244-
Some(hb_tag_t(u32::from_be_bytes(record.feature_tag().to_raw())))
238+
Some(record.feature_tag())
245239
}
246240

247241
pub(crate) fn feature_variation_index(&self, coords: &[F2Dot14]) -> Option<u32> {
@@ -310,7 +304,6 @@ impl<'a> LayoutTable<'a> {
310304

311305
pub(crate) fn feature_index(&self, tag: hb_tag_t) -> Option<u16> {
312306
let list = self.feature_list()?;
313-
let tag = conv_tag(tag);
314307
for (index, feature) in list.feature_records().iter().enumerate() {
315308
if feature.feature_tag() == tag {
316309
return Some(index as u16);
@@ -351,7 +344,7 @@ impl crate::hb::ot_layout::LayoutTableExt for LayoutTable<'_> {
351344
hb_tag_t::default_language(),
352345
// try with 'latn'; some old fonts put their features there even though
353346
// they're really trying to support Thai, for example :(
354-
hb_tag_t::from_bytes(b"latn"),
347+
hb_tag_t::new(b"latn"),
355348
] {
356349
if let Some(index) = self.script_index(tag) {
357350
return Some((false, index, tag));

src/hb/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod text_parser;
5858
mod unicode;
5959
mod unicode_norm;
6060

61-
use ttf_parser::Tag as hb_tag_t;
61+
use skrifa::raw::types::Tag as hb_tag_t;
6262

6363
use self::buffer::hb_glyph_info_t;
6464
use self::face::hb_font_t;

src/hb/ot_layout.rs

-97
Original file line numberDiff line numberDiff line change
@@ -125,103 +125,6 @@ pub trait LayoutTableExt {
125125
) -> Option<FeatureIndex>;
126126
}
127127

128-
impl LayoutTableExt for ttf_parser::opentype_layout::LayoutTable<'_> {
129-
// hb_ot_layout_table_select_script
130-
/// Returns true + index and tag of the first found script tag in the given GSUB or GPOS table
131-
/// or false + index and tag if falling back to a default script.
132-
fn select_script(&self, script_tags: &[hb_tag_t]) -> Option<(bool, ScriptIndex, hb_tag_t)> {
133-
for &tag in script_tags {
134-
if let Some(index) = self.scripts.index(tag) {
135-
return Some((true, index, tag));
136-
}
137-
}
138-
139-
for &tag in &[
140-
// try finding 'DFLT'
141-
hb_tag_t::default_script(),
142-
// try with 'dflt'; MS site has had typos and many fonts use it now :(
143-
hb_tag_t::default_language(),
144-
// try with 'latn'; some old fonts put their features there even though
145-
// they're really trying to support Thai, for example :(
146-
hb_tag_t::from_bytes(b"latn"),
147-
] {
148-
if let Some(index) = self.scripts.index(tag) {
149-
return Some((false, index, tag));
150-
}
151-
}
152-
153-
None
154-
}
155-
156-
// hb_ot_layout_script_select_language
157-
/// Returns the index of the first found language tag in the given GSUB or GPOS table,
158-
/// underneath the specified script index.
159-
fn select_script_language(
160-
&self,
161-
script_index: ScriptIndex,
162-
lang_tags: &[hb_tag_t],
163-
) -> Option<LanguageIndex> {
164-
let script = self.scripts.get(script_index)?;
165-
166-
for &tag in lang_tags {
167-
if let Some(index) = script.languages.index(tag) {
168-
return Some(index);
169-
}
170-
}
171-
172-
// try finding 'dflt'
173-
if let Some(index) = script.languages.index(hb_tag_t::default_language()) {
174-
return Some(index);
175-
}
176-
177-
None
178-
}
179-
180-
// hb_ot_layout_language_get_required_feature
181-
/// Returns the index and tag of a required feature in the given GSUB or GPOS table,
182-
/// underneath the specified script and language.
183-
fn get_required_language_feature(
184-
&self,
185-
script_index: ScriptIndex,
186-
lang_index: Option<LanguageIndex>,
187-
) -> Option<(FeatureIndex, hb_tag_t)> {
188-
let script = self.scripts.get(script_index)?;
189-
let sys = match lang_index {
190-
Some(index) => script.languages.get(index)?,
191-
None => script.default_language?,
192-
};
193-
let idx = sys.required_feature?;
194-
let tag = self.features.get(idx)?.tag;
195-
Some((idx, tag))
196-
}
197-
198-
// hb_ot_layout_language_find_feature
199-
/// Returns the index of a given feature tag in the given GSUB or GPOS table,
200-
/// underneath the specified script and language.
201-
fn find_language_feature(
202-
&self,
203-
script_index: ScriptIndex,
204-
lang_index: Option<LanguageIndex>,
205-
feature_tag: hb_tag_t,
206-
) -> Option<FeatureIndex> {
207-
let script = self.scripts.get(script_index)?;
208-
let sys = match lang_index {
209-
Some(index) => script.languages.get(index)?,
210-
None => script.default_language?,
211-
};
212-
213-
for i in 0..sys.feature_indices.len() {
214-
if let Some(index) = sys.feature_indices.get(i) {
215-
if self.features.get(index).map(|v| v.tag) == Some(feature_tag) {
216-
return Some(index);
217-
}
218-
}
219-
}
220-
221-
None
222-
}
223-
}
224-
225128
/// Called before substitution lookups are performed, to ensure that glyph
226129
/// class and other properties are set on the glyphs in the buffer.
227130
pub fn hb_ot_layout_substitute_start(face: &hb_font_t, buffer: &mut hb_buffer_t) {

src/hb/ot_map.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ use core::cmp::Ordering;
33
use core::ops::Range;
44
use ttf_parser::FromData;
55

6-
use ttf_parser::opentype_layout::{
7-
FeatureIndex, LanguageIndex, LookupIndex, ScriptIndex, VariationIndex,
8-
};
6+
pub type FeatureIndex = u16;
7+
pub type LanguageIndex = u16;
8+
pub type LookupIndex = u16;
9+
pub type ScriptIndex = u16;
10+
pub type VariationIndex = u32;
911

1012
use super::buffer::{glyph_flag, hb_buffer_t};
1113
use super::ot_layout::{LayoutTableExt, TableIndex};
1214
use super::ot_shape_plan::hb_ot_shape_plan_t;
15+
use super::common::TagExt;
1316
use super::{hb_font_t, hb_mask_t, hb_tag_t, tag, Language, Script};
1417

1518
pub struct hb_ot_map_t {

0 commit comments

Comments
 (0)