mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 22:39:07 +00:00
Merge pull request #7681 from dotCooCoo/feat/east-asian-glyph-forms
Type: Glyphs panel menu draws the selected characters in an East Asian glyph form
This commit is contained in:
@@ -319,6 +319,9 @@ impl OtFeatures {
|
||||
v.push(f(&t, u32::from(s.extra_values.get(i).copied().unwrap_or(0).max(1))));
|
||||
}
|
||||
}
|
||||
if let Some(tag) = st.glyph_form.feature() {
|
||||
v.push(f(tag, true));
|
||||
}
|
||||
if self.vertical {
|
||||
// Not `vrt2`: its glyphs are already turned on their side, and the layout turns Latin
|
||||
// and digits itself (they would lie upside down, and tate-chu-yoko break).
|
||||
|
||||
@@ -304,6 +304,20 @@ impl FontFace {
|
||||
self.gsub.get_or_init(|| GsubIndex::read(self))
|
||||
}
|
||||
|
||||
/// Does the face's `GSUB` table have feature `tag` with at least one lookup? At most
|
||||
/// [`MAX_FEATURES`] feature records are read; false when the face has no `GSUB` table or it
|
||||
/// cannot be read.
|
||||
pub fn has_substitution_feature(&self, tag: [u8; 4]) -> bool {
|
||||
let Some(font) = self.skrifa() else { return false };
|
||||
let Some(features) = font.gsub().ok().and_then(|g| g.feature_list().ok()) else { return false };
|
||||
features
|
||||
.feature_records()
|
||||
.iter()
|
||||
.take(MAX_FEATURES)
|
||||
.filter(|r| r.feature_tag().to_be_bytes() == tag)
|
||||
.any(|r| r.feature(features.offset_data()).is_ok_and(|f| !f.lookup_list_indices().is_empty()))
|
||||
}
|
||||
|
||||
/// What the features `aalt`, `salt`, `ss01` to `ss20`, `swsh`, `cv01` to `cv99`, `hist`,
|
||||
/// `jalt`, `nalt`, `ornm` and `titl` of the face's `GSUB` table substitute for glyph `gid`, in
|
||||
/// that order of features and within a feature in lookup order: the glyph of each single
|
||||
|
||||
@@ -86,6 +86,7 @@ pub(crate) fn paint_only(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
no_break,
|
||||
language,
|
||||
glyph_alternate,
|
||||
glyph_form: _,
|
||||
fill,
|
||||
stroke,
|
||||
stroke_width,
|
||||
|
||||
@@ -1028,6 +1028,8 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
no_break,
|
||||
language,
|
||||
glyph_alternate,
|
||||
// An OpenType feature over its own characters, like `features`.
|
||||
glyph_form: _,
|
||||
} = a;
|
||||
// Keep PartialEq's non-reflexive behavior for ignored numeric fields, without cloning styles.
|
||||
let offsets = (standard_vertical_roman, tcy_up_down, tcy_left_right);
|
||||
@@ -1096,7 +1098,7 @@ pub(crate) fn warichu_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
/// character style's name, leading, baseline shift, rotation, Character Alignment, Character
|
||||
/// Direction (text is shaped apart where its bidi level changes), Kashidas, the kerning (each glyph takes its
|
||||
/// run's, see [`shape_segment`]), the space before a line's first character ([`space_before`]),
|
||||
/// Insert Aki ([`aki`]), Tsume ([`tighten`]), the OpenType features (each run's apply to its own characters, as features
|
||||
/// Insert Aki ([`aki`]), Tsume ([`tighten`]), the OpenType features and the East Asian glyph form (each run's apply to its own characters, as features
|
||||
/// over a range of the one buffer) and the tracking, as long as the tracking keeps or drops
|
||||
/// standard ligatures alike ([`crate::features::ligatures_suppressed_by`]).
|
||||
fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
@@ -1159,6 +1161,8 @@ fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
no_break,
|
||||
language,
|
||||
glyph_alternate,
|
||||
// Applied as a feature over its run's own characters, with `features`.
|
||||
glyph_form: _,
|
||||
} = a;
|
||||
*font_family == b.font_family
|
||||
&& warichu_alike(a, b)
|
||||
@@ -1442,8 +1446,8 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
let mut ranged = Vec::new();
|
||||
for (r, run_style) in runs.get(source_runs.clone()).unwrap_or_default() {
|
||||
let (a, b) = (r.start.max(range.start), r.end.min(range.end));
|
||||
if run_style.features != base_style.features && a < b {
|
||||
let own = variant(&CharStyle { features: run_style.features.clone(), ..base_style.clone() });
|
||||
if (run_style.features != base_style.features || run_style.glyph_form != base_style.glyph_form) && a < b {
|
||||
let own = variant(&CharStyle { features: run_style.features.clone(), glyph_form: run_style.glyph_form, ..base_style.clone() });
|
||||
ranged_features(base, &own, a..b, &mut ranged);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2041,3 +2041,40 @@ fn soft_return_breaks_line_even_inside_no_break() {
|
||||
let lay = layout(db(), &t);
|
||||
assert_eq!(lay.lines.len(), 2, "soft return honors line break despite no_break");
|
||||
}
|
||||
|
||||
/// An East Asian glyph form (the Glyphs panel menu's Traditional Forms, JIS 78 Forms, …) draws
|
||||
/// the characters with the font's glyphs of that form, and Default with its default glyphs. Needs
|
||||
/// craft-fonts' BIZ UDMincho, which has traditional, expert, JIS 78, 83 and 90, full-width and
|
||||
/// half-width forms and no JIS 04 forms (skipped without it). A form on one character of a run
|
||||
/// changes that character only.
|
||||
#[test]
|
||||
fn glyph_forms_draw_the_fonts_east_asian_forms() {
|
||||
use vectorcraft_doc::GlyphForm;
|
||||
const FAMILY: &str = "BIZ UDMincho";
|
||||
if db().face(FAMILY, "Regular").is_none_or(|f| f.family != FAMILY) {
|
||||
eprintln!("skipped: built without craft-fonts (set CRAFT_FONTS_DIR to a craft-fonts checkout to run it)");
|
||||
return;
|
||||
}
|
||||
let st = |form| CharStyle { size: 20.0, font_family: FAMILY.into(), glyph_form: form, ..CharStyle::default() };
|
||||
let glyphs = |text: &str, form| layout(db(), &TextObject::point(Point::ZERO, text, st(form))).glyphs;
|
||||
let gids = |text: &str, form| glyphs(text, form).iter().map(|g| g.gid).collect::<Vec<_>>();
|
||||
for (form, text) in
|
||||
[(GlyphForm::Traditional, "亜"), (GlyphForm::Expert, "唖"), (GlyphForm::Jis78, "唖"), (GlyphForm::Jis83, "芦"), (GlyphForm::Jis90, "芦")]
|
||||
{
|
||||
assert_ne!(gids(text, form), gids(text, GlyphForm::Default), "{form:?} {text}");
|
||||
}
|
||||
// The font has no JIS 04 forms, so the characters keep their glyphs.
|
||||
assert_eq!(gids("亜唖芦", GlyphForm::Jis04), gids("亜唖芦", GlyphForm::Default));
|
||||
// Full-width Latin takes a whole em, half-width punctuation half of one.
|
||||
let advance = |text: &str, form| glyphs(text, form).first().map_or(0.0, |g| g.advance);
|
||||
assert!(advance("A", GlyphForm::Default) < 15.0, "{}", advance("A", GlyphForm::Default));
|
||||
assert!((advance("A", GlyphForm::FullWidth) - 20.0).abs() < 0.01, "{}", advance("A", GlyphForm::FullWidth));
|
||||
assert!((advance("、", GlyphForm::Default) - 20.0).abs() < 0.01, "{}", advance("、", GlyphForm::Default));
|
||||
assert!((advance("、", GlyphForm::HalfWidth) - 10.0).abs() < 0.01, "{}", advance("、", GlyphForm::HalfWidth));
|
||||
// Traditional Forms on the middle character only.
|
||||
let mut t = TextObject::point(Point::ZERO, "亜亜亜", st(GlyphForm::Default));
|
||||
edit::style_range(&mut t.runs, 3, 6, |s| s.glyph_form = GlyphForm::Traditional);
|
||||
let ids: Vec<u32> = layout(db(), &t).glyphs.iter().map(|g| g.gid).collect();
|
||||
let (plain, traditional) = (gids("亜", GlyphForm::Default), gids("亜", GlyphForm::Traditional));
|
||||
assert_eq!(ids, [plain[0], traditional[0], plain[0]]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user