Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use font_types::Tag #10

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/gen-tag-table.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def rank_delta(bcp_47, ot):

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

def hb_tag(tag):
if tag == DEFAULT_LANGUAGE_SYSTEM:
return 'Tag(0)\t '
return 'Tag::from_bytes(b\"%s%s%s%s\")' % tuple(('%-4s' % tag)[:4])
return 'Tag::new(&[0; 4])'
return 'Tag::new(b\"%s%s%s%s\")' % tuple(('%-4s' % tag)[:4])
rsheeter marked this conversation as resolved.
Show resolved Hide resolved


def hb_tag2(tag):
Expand Down
2 changes: 1 addition & 1 deletion src/hb/aat_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl hb_aat_feature_mapping_t {
selector_to_disable: u8,
) -> Self {
hb_aat_feature_mapping_t {
ot_feature_tag: hb_tag_t::from_bytes(ot_feature_tag),
ot_feature_tag: hb_tag_t::new(ot_feature_tag),
aat_feature_type,
selector_to_enable,
selector_to_disable,
Expand Down
2 changes: 1 addition & 1 deletion src/hb/aat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl hb_aat_map_builder_t {
pub fn add_feature(&mut self, face: &hb_font_t, feature: &Feature) -> Option<()> {
let feat = face.tables().feat?;

if feature.tag == hb_tag_t::from_bytes(b"aalt") {
if feature.tag == hb_tag_t::new(b"aalt") {
let exposes_feature = feat
.names
.find(HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES as u16)
Expand Down
41 changes: 30 additions & 11 deletions src/hb/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::string::String;
use core::ops::{Bound, RangeBounds};

use ttf_parser::Tag;
use skrifa::raw::types::Tag;

use super::text_parser::TextParser;

Expand Down Expand Up @@ -215,7 +215,7 @@ pub struct Script(pub(crate) Tag);
impl Script {
#[inline]
pub(crate) const fn from_bytes(bytes: &[u8; 4]) -> Self {
Script(Tag::from_bytes(bytes))
Script(Tag::new(bytes))
}

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

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

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

pub trait TagExt {
fn from_bytes_lossy(bytes: &[u8]) -> Self;
fn as_u32(self) -> u32;
fn is_null(self) -> bool;
fn default_script() -> Self;
fn default_language() -> Self;
#[cfg(test)]
Expand All @@ -711,22 +714,38 @@ pub trait TagExt {
}

impl TagExt for Tag {
fn from_bytes_lossy(bytes: &[u8]) -> Self {
let mut array = [b' '; 4];
for (src, dest) in bytes.iter().zip(&mut array) {
*dest = *src;
}
Tag::new(&array)
}

fn as_u32(self) -> u32 {
u32::from_be_bytes(self.to_be_bytes())
}

fn is_null(self) -> bool {
self.to_be_bytes() == [0, 0, 0, 0]
}

#[inline]
fn default_script() -> Self {
Tag::from_bytes(b"DFLT")
Tag::new(b"DFLT")
}

#[inline]
fn default_language() -> Self {
Tag::from_bytes(b"dflt")
Tag::new(b"dflt")
}

/// Converts tag to lowercase.
#[cfg(test)]
#[inline]
fn to_lowercase(&self) -> Self {
let b = self.to_bytes();
Tag::from_bytes(&[
let b = self.to_be_bytes();
Tag::new(&[
b[0].to_ascii_lowercase(),
b[1].to_ascii_lowercase(),
b[2].to_ascii_lowercase(),
Expand All @@ -737,8 +756,8 @@ impl TagExt for Tag {
/// Converts tag to uppercase.
#[inline]
fn to_uppercase(&self) -> Self {
let b = self.to_bytes();
Tag::from_bytes(&[
let b = self.to_be_bytes();
Tag::new(&[
b[0].to_ascii_uppercase(),
b[1].to_ascii_uppercase(),
b[2].to_ascii_uppercase(),
Expand Down
3 changes: 2 additions & 1 deletion src/hb/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ttf_parser::{GlyphId, RgbaColor};

use super::buffer::GlyphPropsFlags;
use super::fonta;
use super::common::TagExt;
use super::ot_layout::TableIndex;
use crate::Variation;

Expand Down Expand Up @@ -81,7 +82,7 @@ impl<'a> hb_font_t<'a> {
/// Sets font variations.
pub fn set_variations(&mut self, variations: &[Variation]) {
for variation in variations {
self.ttfp_face.set_variation(variation.tag, variation.value);
self.ttfp_face.set_variation(ttf_parser::Tag(variation.tag.as_u32()), variation.value);
}
self.font.set_coords(self.ttfp_face.variation_coordinates());
}
Expand Down
11 changes: 2 additions & 9 deletions src/hb/fonta/ot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ pub enum LayoutTable<'a> {
Gpos(Gpos<'a>),
}

fn conv_tag(tag: hb_tag_t) -> skrifa::raw::types::Tag {
skrifa::raw::types::Tag::from_u32(tag.0)
}

impl<'a> LayoutTable<'a> {
fn script_list(&self) -> Option<ScriptList<'a>> {
match self {
Expand Down Expand Up @@ -199,7 +195,6 @@ impl<'a> LayoutTable<'a> {

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

fn langsys_index(&self, script_index: u16, tag: hb_tag_t) -> Option<u16> {
let script = self.script(script_index)?;
let tag = conv_tag(tag);
script
.lang_sys_records()
.binary_search_by_key(&tag, |rec| rec.lang_sys_tag())
Expand All @@ -241,7 +235,7 @@ impl<'a> LayoutTable<'a> {
fn feature_tag(&self, index: u16) -> Option<hb_tag_t> {
let list = self.feature_list()?;
let record = list.feature_records().get(index as usize)?;
Some(hb_tag_t(u32::from_be_bytes(record.feature_tag().to_raw())))
Some(record.feature_tag())
}

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

pub(crate) fn feature_index(&self, tag: hb_tag_t) -> Option<u16> {
let list = self.feature_list()?;
let tag = conv_tag(tag);
for (index, feature) in list.feature_records().iter().enumerate() {
if feature.feature_tag() == tag {
return Some(index as u16);
Expand Down Expand Up @@ -351,7 +344,7 @@ impl crate::hb::ot_layout::LayoutTableExt for LayoutTable<'_> {
hb_tag_t::default_language(),
// try with 'latn'; some old fonts put their features there even though
// they're really trying to support Thai, for example :(
hb_tag_t::from_bytes(b"latn"),
hb_tag_t::new(b"latn"),
] {
if let Some(index) = self.script_index(tag) {
return Some((false, index, tag));
Expand Down
2 changes: 1 addition & 1 deletion src/hb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod text_parser;
mod unicode;
mod unicode_norm;

use ttf_parser::Tag as hb_tag_t;
use skrifa::raw::types::Tag as hb_tag_t;

use self::buffer::hb_glyph_info_t;
use self::face::hb_font_t;
Expand Down
97 changes: 0 additions & 97 deletions src/hb/ot_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,103 +125,6 @@ pub trait LayoutTableExt {
) -> Option<FeatureIndex>;
}

impl LayoutTableExt for ttf_parser::opentype_layout::LayoutTable<'_> {
// hb_ot_layout_table_select_script
/// Returns true + index and tag of the first found script tag in the given GSUB or GPOS table
/// or false + index and tag if falling back to a default script.
fn select_script(&self, script_tags: &[hb_tag_t]) -> Option<(bool, ScriptIndex, hb_tag_t)> {
for &tag in script_tags {
if let Some(index) = self.scripts.index(tag) {
return Some((true, index, tag));
}
}

for &tag in &[
// try finding 'DFLT'
hb_tag_t::default_script(),
// try with 'dflt'; MS site has had typos and many fonts use it now :(
hb_tag_t::default_language(),
// try with 'latn'; some old fonts put their features there even though
// they're really trying to support Thai, for example :(
hb_tag_t::from_bytes(b"latn"),
] {
if let Some(index) = self.scripts.index(tag) {
return Some((false, index, tag));
}
}

None
}

// hb_ot_layout_script_select_language
/// Returns the index of the first found language tag in the given GSUB or GPOS table,
/// underneath the specified script index.
fn select_script_language(
&self,
script_index: ScriptIndex,
lang_tags: &[hb_tag_t],
) -> Option<LanguageIndex> {
let script = self.scripts.get(script_index)?;

for &tag in lang_tags {
if let Some(index) = script.languages.index(tag) {
return Some(index);
}
}

// try finding 'dflt'
if let Some(index) = script.languages.index(hb_tag_t::default_language()) {
return Some(index);
}

None
}

// hb_ot_layout_language_get_required_feature
/// Returns the index and tag of a required feature in the given GSUB or GPOS table,
/// underneath the specified script and language.
fn get_required_language_feature(
&self,
script_index: ScriptIndex,
lang_index: Option<LanguageIndex>,
) -> Option<(FeatureIndex, hb_tag_t)> {
let script = self.scripts.get(script_index)?;
let sys = match lang_index {
Some(index) => script.languages.get(index)?,
None => script.default_language?,
};
let idx = sys.required_feature?;
let tag = self.features.get(idx)?.tag;
Some((idx, tag))
}

// hb_ot_layout_language_find_feature
/// Returns the index of a given feature tag in the given GSUB or GPOS table,
/// underneath the specified script and language.
fn find_language_feature(
&self,
script_index: ScriptIndex,
lang_index: Option<LanguageIndex>,
feature_tag: hb_tag_t,
) -> Option<FeatureIndex> {
let script = self.scripts.get(script_index)?;
let sys = match lang_index {
Some(index) => script.languages.get(index)?,
None => script.default_language?,
};

for i in 0..sys.feature_indices.len() {
if let Some(index) = sys.feature_indices.get(i) {
if self.features.get(index).map(|v| v.tag) == Some(feature_tag) {
return Some(index);
}
}
}

None
}
}

/// Called before substitution lookups are performed, to ensure that glyph
/// class and other properties are set on the glyphs in the buffer.
pub fn hb_ot_layout_substitute_start(face: &hb_font_t, buffer: &mut hb_buffer_t) {
Expand Down
9 changes: 6 additions & 3 deletions src/hb/ot_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use core::cmp::Ordering;
use core::ops::Range;
use ttf_parser::FromData;

use ttf_parser::opentype_layout::{
FeatureIndex, LanguageIndex, LookupIndex, ScriptIndex, VariationIndex,
};
pub type FeatureIndex = u16;
pub type LanguageIndex = u16;
pub type LookupIndex = u16;
pub type ScriptIndex = u16;
pub type VariationIndex = u32;

use super::buffer::{glyph_flag, hb_buffer_t};
use super::ot_layout::{LayoutTableExt, TableIndex};
use super::ot_shape_plan::hb_ot_shape_plan_t;
use super::common::TagExt;
use super::{hb_font_t, hb_mask_t, hb_tag_t, tag, Language, Script};

pub struct hb_ot_map_t {
Expand Down
Loading
Loading