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 3 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])\t '
khaledhosny marked this conversation as resolved.
Show resolved Hide resolved
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
63 changes: 14 additions & 49 deletions src/hb/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use core_maths::CoreFloat;

use crate::hb::paint_extents::hb_paint_extents_context_t;
use ttf_parser::gdef::GlyphClass;
use ttf_parser::opentype_layout::LayoutTable;
use ttf_parser::{GlyphId, RgbaColor};

use super::buffer::GlyphPropsFlags;
use super::fonta;
use super::common::TagExt;
use super::ot_layout::TableIndex;
use super::ot_layout_common::{PositioningTable, SubstitutionTable};
use crate::Variation;

/// A font face handle.
Expand All @@ -21,22 +20,6 @@ pub struct hb_font_t<'a> {
pub(crate) units_per_em: u16,
pixels_per_em: Option<(u16, u16)>,
pub(crate) points_per_em: Option<f32>,
pub(crate) gsub: Option<SubstitutionTable<'a>>,
pub(crate) gpos: Option<PositioningTable<'a>>,
}

impl<'a> AsRef<ttf_parser::Face<'a>> for hb_font_t<'a> {
#[inline]
fn as_ref(&self) -> &ttf_parser::Face<'a> {
&self.ttfp_face
}
}

impl<'a> AsMut<ttf_parser::Face<'a>> for hb_font_t<'a> {
#[inline]
fn as_mut(&mut self) -> &mut ttf_parser::Face<'a> {
&mut self.ttfp_face
}
}

impl<'a> core::ops::Deref for hb_font_t<'a> {
Expand All @@ -48,13 +31,6 @@ impl<'a> core::ops::Deref for hb_font_t<'a> {
}
}

impl<'a> core::ops::DerefMut for hb_font_t<'a> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ttfp_face
}
}

impl<'a> hb_font_t<'a> {
/// Creates a new `Face` from data.
///
Expand All @@ -67,28 +43,10 @@ impl<'a> hb_font_t<'a> {
units_per_em: face.units_per_em(),
pixels_per_em: None,
points_per_em: None,
gsub: face.tables().gsub.map(SubstitutionTable::new),
gpos: face.tables().gpos.map(PositioningTable::new),
ttfp_face: face,
})
}

/// Creates a new [`Face`] from [`ttf_parser::Face`].
///
/// Data will be referenced, not owned.
pub fn from_face(face: ttf_parser::Face<'a>) -> Self {
let font = fonta::Font::new(face.raw_face().data, 0).unwrap();
hb_font_t {
font,
units_per_em: face.units_per_em(),
pixels_per_em: None,
points_per_em: None,
gsub: face.tables().gsub.map(SubstitutionTable::new),
gpos: face.tables().gpos.map(PositioningTable::new),
ttfp_face: face,
}
}

// TODO: remove
/// Returns face’s units per EM.
#[inline]
Expand Down Expand Up @@ -124,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.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 Expand Up @@ -338,17 +296,24 @@ impl<'a> hb_font_t<'a> {
}
}

pub(crate) fn layout_table(&self, table_index: TableIndex) -> Option<&LayoutTable<'a>> {
pub(crate) fn layout_table(
&self,
table_index: TableIndex,
) -> Option<fonta::ot::LayoutTable<'a>> {
match table_index {
TableIndex::GSUB => self.gsub.as_ref().map(|table| &table.inner),
TableIndex::GPOS => self.gpos.as_ref().map(|table| &table.inner),
TableIndex::GSUB => Some(fonta::ot::LayoutTable::Gsub(
self.font.ot.gsub.as_ref()?.table.clone(),
)),
TableIndex::GPOS => Some(fonta::ot::LayoutTable::Gpos(
self.font.ot.gpos.as_ref()?.table.clone(),
)),
}
}

pub(crate) fn layout_tables(
&self,
) -> impl Iterator<Item = (TableIndex, &LayoutTable<'a>)> + '_ {
TableIndex::iter().filter_map(move |idx| self.layout_table(idx).map(|table| (idx, table)))
) -> impl Iterator<Item = (TableIndex, fonta::ot::LayoutTable<'a>)> + '_ {
TableIndex::iter().filter_map(move |idx| Some((idx, self.layout_table(idx)?)))
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/hb/fonta/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ impl<'a> Font<'a> {
pub(crate) fn set_coords(&mut self, coords: &[NormalizedCoordinate]) {
self.coords.clear();
if !coords.is_empty() && !coords.iter().all(|coord| coord.get() == 0) {
self.coords.extend(
coords
.iter()
.map(|coord| NormalizedCoord::from_bits(coord.get())),
);
let ivs = self.ivs.take().or_else(|| self.ot.item_variation_store());
if ivs.is_some() {
self.coords.extend(
coords
.iter()
.map(|coord| NormalizedCoord::from_bits(coord.get())),
);
}
self.ivs = ivs;
} else {
self.ivs = None;
Expand Down
Loading
Loading