Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

412
vendor/swash/src/shape/aat.rs vendored Normal file
View File

@@ -0,0 +1,412 @@
use super::buffer::*;
use super::internal::aat::*;
pub fn apply_morx(
data: &[u8],
morx: u32,
buffer: &mut Buffer,
selectors: &[(u16, u16)],
) -> Option<()> {
use morx::*;
let max_ops = buffer.glyphs.len() * 16;
for chain in chains(data, morx) {
let mut ops = 0;
let mut flags = chain.default_flags();
if !selectors.is_empty() {
for feature in chain.features() {
let key = (feature.selector, feature.setting_selector);
if selectors.binary_search(&key).is_ok() {
flags = flags & feature.disable_flags | feature.enable_flags;
}
}
}
for (_i, subtable) in chain.subtables().enumerate() {
if subtable.flags() & flags == 0 {
// if TRACE {
// println!(" <SKIP chain subtable {}>", i);
// }
continue;
} else {
// if TRACE {
// println!(" <chain subtable {} order: {:?}>", i, subtable.order());
// }
}
let reverse = subtable.should_reverse(buffer.is_rtl);
buffer.ensure_order(reverse);
let kind = match subtable.kind() {
Some(kind) => kind,
_ => continue,
};
match kind {
SubtableKind::Rearrangement(t) => {
//println!(".. rearrangement");
let mut i = 0;
let mut state = RearrangementState::new();
while i < buffer.glyphs.len() && ops < max_ops {
let g = buffer.glyphs[i].id;
match t.next(&mut state, i, g, false, |r| {
// if TRACE {
// println!("Rearrange!");
// }
r.apply(&mut buffer.glyphs);
Some(())
}) {
Some(advance) => i += advance,
None => break,
}
ops += 1;
}
// Apply END_OF_TEXT state
t.next(&mut state, i, 0, true, |r| {
r.apply(&mut buffer.glyphs);
Some(())
});
}
SubtableKind::Contextual(t) => {
//println!(".. contextual");
let mut state = ContextualState::new();
for i in 0..buffer.glyphs.len() {
let g = buffer.glyphs[i].id;
t.next(&mut state, i, g, false, |i, g| {
buffer.substitute(i, g);
Some(())
});
}
// Apply END_OF_TEXT state
if let Some(last_id) = buffer.glyphs.last().map(|g| g.id) {
t.next(
&mut state,
buffer.glyphs.len() - 1,
last_id,
true,
|i, g| {
buffer.substitute(i, g);
Some(())
},
);
}
}
SubtableKind::NonContextual(t) => {
//println!(".. non-contextual");
for (_i, g) in buffer.glyphs.iter_mut().enumerate() {
if let Some(s) = t.substitute(g.id) {
// if TRACE {
// println!("NonContextual[{}] {} -> {}", i, g.id, s);
// }
g.id = s;
}
}
}
SubtableKind::Ligature(t) => {
//println!(".. ligature");
let mut i = 0;
let mut state = LigatureState::new();
while i < buffer.glyphs.len() && ops < max_ops {
let g = buffer.glyphs[i].id;
let f = |i, g, comps: &[usize]| {
buffer.substitute_ligature(i, g, comps);
Some(())
};
if t.next(&mut state, i, g, false, f).is_none() {
break;
}
i += 1;
ops += 1;
}
// Apply END_OF_TEXT state
t.next(
&mut state,
buffer.glyphs.len().saturating_sub(1),
0,
true,
|i, g, comps| {
buffer.substitute_ligature(i, g, comps);
Some(())
},
);
}
SubtableKind::Insertion(t) => {
//println!(".. insertion");
let mut i = 0;
let mut state = InsertionState::new();
while i < buffer.glyphs.len() && ops < max_ops {
let g = buffer.glyphs[i].id;
match t.next(&mut state, i, g, false, |i, array| {
// if TRACE {
// let rep = array.iter().collect::<Vec<_>>();
// println!("Insert[{}] {:?}", i, &rep);
// }
buffer.multiply(i, array.len());
let start = i;
let end = start + array.len();
for (g, s) in buffer.glyphs[start..end].iter_mut().zip(array.iter()) {
g.id = s;
g.flags = 0;
}
Some(())
}) {
Some(advance) => i += advance,
None => break,
}
ops += 1;
}
// Apply END_OF_TEXT state
t.next(
&mut state,
buffer.glyphs.len().saturating_sub(1),
0,
true,
|i, array| {
buffer.multiply(i, array.len());
let start = i;
let end = start + array.len();
for (g, s) in buffer.glyphs[start..end].iter_mut().zip(array.iter()) {
g.id = s;
g.flags = 0;
}
Some(())
},
);
}
}
}
}
buffer.ensure_order(false);
Some(())
}
pub fn apply_kerx(
data: &[u8],
kerx: u32,
ankr: u32,
buffer: &mut Buffer,
disable_kern: bool,
) -> Option<()> {
use kerx::*;
for (_i, subtable) in subtables(data, kerx, ankr).enumerate() {
// if TRACE {
// println!(" <kerx subtable {}>", i);
// }
let reverse = subtable.should_reverse(buffer.is_rtl);
buffer.ensure_order(reverse);
let kind = match subtable.kind() {
Some(kind) => kind,
_ => continue,
};
if subtable.is_vertical() || subtable.is_cross_stream() {
continue;
}
match kind {
SubtableKind::Format0(t) => {
if disable_kern {
continue;
}
let len = buffer.len();
let mut left_index = if let Some((index, _)) = buffer
.glyphs
.iter()
.enumerate()
.find(|(_, g)| g.joining_type != 6)
{
index
} else {
continue;
};
let mut left = buffer.glyphs[left_index].id;
for i in left_index + 1..len {
if buffer.glyphs[i].joining_type == 6 {
continue;
}
let right = buffer.glyphs[i].id;
if let Some(kerning) = t.get(left, right) {
if kerning != 0 {
// if TRACE {
// println!("KERN [{} & {}] {}", left_index, i, kerning);
// }
buffer.positions[left_index].advance += kerning as f32;
}
}
left_index = i;
left = right;
}
}
SubtableKind::Format1(t) => {
if disable_kern {
continue;
}
let mut i = 0;
let len = buffer.glyphs.len();
let mut state = ContextualState::new();
while i < len {
match t.next(&mut state, i, buffer.glyphs[i].id, |i, kerning| {
buffer.positions[i].advance += kerning as f32;
Some(())
}) {
Some(advance) => i += advance,
None => break,
}
}
}
SubtableKind::Format2(t) => {
if disable_kern {
continue;
}
//println!("142/116 = {:?}", t.get(142, 116));
let len = buffer.len();
let mut left_index = if let Some((index, _)) = buffer
.glyphs
.iter()
.enumerate()
.find(|(_, g)| g.joining_type != 6)
{
index
} else {
continue;
};
let mut left = buffer.glyphs[left_index].id;
for i in left_index + 1..len {
if buffer.glyphs[i].joining_type == 6 {
continue;
}
let right = buffer.glyphs[i].id;
if let Some(kerning) = t.get(left, right) {
if kerning != 0 {
// if TRACE {
// println!("KERN [{} & {}] {}", left_index, i, kerning);
// }
buffer.positions[left_index].advance += kerning as f32;
}
}
left_index = i;
left = right;
}
}
SubtableKind::Format4(t) => {
let mut i = 0;
let len = buffer.glyphs.len();
let mut state = Format4State::new();
while i < len {
match t.next(&mut state, i, buffer.glyphs[i].id, |i, base, x, y| {
buffer.position_mark(i, base, x, y);
Some(())
}) {
Some(advance) => i += advance,
None => break,
}
}
}
}
}
buffer.ensure_order(false);
Some(())
}
pub fn apply_kern(data: &[u8], kern: u32, buffer: &mut Buffer) -> Option<()> {
use kern::*;
for (_i, subtable) in subtables(data, kern).enumerate() {
let kind = match subtable.kind() {
Some(kind) => kind,
_ => continue,
};
if !subtable.is_horizontal() {
continue;
}
buffer.ensure_order(buffer.is_rtl);
let cross_stream = subtable.cross_stream();
match kind {
SubtableKind::Format0(t) => {
buffer.ensure_order(false);
let len = buffer.len();
let mut left_index = if let Some((index, _)) = buffer
.glyphs
.iter()
.enumerate()
.find(|(_, g)| g.joining_type != 6)
{
index
} else {
continue;
};
let mut left = buffer.glyphs[left_index].id;
for i in left_index + 1..len {
if buffer.glyphs[i].joining_type == 6 {
continue;
}
let right = buffer.glyphs[i].id;
if let Some(kerning) = t.get(left, right) {
if kerning != 0 {
// if TRACE {
// println!("KERN [{} & {}] {}", left_index, i, kerning);
// }
buffer.positions[left_index].advance += kerning as f32;
}
}
left_index = i;
left = right;
}
}
SubtableKind::Format1(t) => {
let mut i = 0;
let len = buffer.glyphs.len();
let mut state = Format1State::new();
while i < len {
match t.next(&mut state, i, buffer.glyphs[i].id, |i, kerning| {
let g = &buffer.glyphs[i];
if g.joining_type == 6 {
if cross_stream {
let pos = &mut buffer.positions[i];
if pos.y == 0. {
pos.y = kerning as f32;
}
} else if let Some(base) = find_base(buffer, buffer.is_rtl, i) {
let diff = if base >= i { base - i } else { i - base };
if diff < 255 {
let pos = &mut buffer.positions[i];
if pos.base == 0 {
pos.flags |= MARK_ATTACH;
pos.base = diff as u8;
pos.x = kerning as f32;
buffer.has_marks = true;
}
}
}
}
Some(())
}) {
Some(advance) => i += advance,
None => break,
}
}
}
}
}
buffer.ensure_order(false);
Some(())
}
fn find_base(buffer: &Buffer, reverse: bool, index: usize) -> Option<usize> {
use crate::text::cluster::ShapeClass;
let cluster = buffer.glyphs[index].cluster;
if reverse {
for i in index + 1..buffer.len() {
let g = &buffer.glyphs[i];
if g.cluster != cluster {
return None;
}
if g.char_class == ShapeClass::Base {
return Some(i);
}
}
} else if index > 0 {
for i in (0..index).rev() {
let g = &buffer.glyphs[i];
if g.cluster != cluster {
return None;
}
if g.char_class == ShapeClass::Base {
return Some(i);
}
}
}
None
}

1826
vendor/swash/src/shape/at.rs vendored Normal file

File diff suppressed because it is too large Load Diff

708
vendor/swash/src/shape/buffer.rs vendored Normal file
View File

@@ -0,0 +1,708 @@
use super::cluster::{Glyph, GlyphInfo};
use super::feature::*;
use crate::text::{
cluster::{Char, CharCluster, ClusterInfo, ShapeClass, SourceRange, MAX_CLUSTER_SIZE},
JoiningType,
};
use alloc::vec::Vec;
use core::ops::Range;
// Glyph flags.
pub const SUBSTITUTED: u16 = 1;
pub const LIGATED: u16 = 2;
pub const COMPONENT: u16 = 4;
pub const MARK_ATTACH: u16 = 8;
pub const CURSIVE_ATTACH: u16 = 16;
pub const IGNORABLE: u16 = 64;
/// Per glyph shaping data.
#[derive(Copy, Clone, Default, Debug)]
pub struct GlyphData {
pub id: u16,
pub flags: u16,
pub class: u8,
pub char_class: ShapeClass,
pub mark_type: u8,
pub joining_type: u8,
pub mask: u8,
pub skip: bool,
pub component: u8,
pub cluster: u32,
pub data: u32,
}
impl GlyphData {
pub fn is_component(&self) -> bool {
self.flags & COMPONENT != 0
}
}
/// Per glyph shaping position data.
#[derive(Copy, Clone, Default, Debug)]
pub struct PositionData {
pub base: u8,
pub flags: u16,
pub x: f32,
pub y: f32,
pub advance: f32,
}
impl Glyph {
pub(super) fn new(g: &GlyphData, p: &PositionData) -> Self {
Self {
id: g.id,
info: GlyphInfo(p.flags),
x: p.x,
y: p.y,
advance: p.advance,
data: g.data,
}
}
}
#[derive(Clone, Default)]
pub struct Buffer {
pub glyphs: Vec<GlyphData>,
pub positions: Vec<PositionData>,
pub infos: Vec<(ClusterInfo, bool, u32)>,
pub ranges: Vec<SourceRange>,
pub shaped_glyphs: Vec<Glyph>,
pub is_rtl: bool,
pub dotted_circle: Option<u16>,
pub has_cursive: bool,
pub has_marks: bool,
pub reversed: bool,
pub next_cluster: u32,
pub skip_state: SkipState,
pub sub_args: Vec<u16>,
pub pos_args: Vec<u16>,
}
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct SkipState {
pub flags: u8,
pub mask: u8,
pub mark_check: u8,
pub mark_class: u8,
pub mark_set: u32,
}
impl Buffer {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.glyphs.len()
}
pub fn push(&mut self, cluster: &CharCluster) -> Range<usize> {
let start = self.glyphs.len();
let chars = cluster.mapped_chars();
if cluster.info().is_broken() {
if let Some(id) = self.dotted_circle {
let first = &chars[0];
self.push_char(&Char {
ch: '\u{25cc}',
shape_class: ShapeClass::Base,
joining_type: JoiningType::U,
ignorable: false,
contributes_to_shaping: true,
glyph_id: id,
offset: first.offset,
data: first.data,
});
}
}
for ch in chars {
self.push_char(ch);
}
self.next_cluster += 1;
self.push_cluster(cluster);
start..self.glyphs.len()
}
pub fn push_order(&mut self, cluster: &CharCluster, order: &[usize]) -> Range<usize> {
let start = self.glyphs.len();
let chars = cluster.mapped_chars();
if cluster.info().is_broken() {
if let Some(id) = self.dotted_circle {
let first = &chars[order[0]];
self.push_char(&Char {
ch: '\u{25cc}',
shape_class: ShapeClass::Base,
joining_type: JoiningType::U,
ignorable: false,
contributes_to_shaping: true,
glyph_id: id,
offset: first.offset,
data: first.data,
});
}
}
for ch in order[..chars.len()].iter().map(|i| &chars[*i]) {
self.push_char(ch);
}
self.next_cluster += 1;
self.push_cluster(cluster);
start..self.glyphs.len()
}
pub fn _push_hangul(&mut self, cluster: &CharCluster) -> Range<usize> {
let start = self.glyphs.len();
let chars = cluster.mapped_chars();
if cluster.info().is_broken() {
if let Some(id) = self.dotted_circle {
let first = &chars[0];
self.push_char(&Char {
ch: '\u{25cc}',
shape_class: ShapeClass::Base,
joining_type: JoiningType::U,
ignorable: false,
contributes_to_shaping: true,
glyph_id: id,
offset: first.offset,
data: first.data,
});
}
}
for ch in chars {
self._push_hangul_char(ch);
}
self.next_cluster += 1;
self.push_cluster(cluster);
start..self.glyphs.len()
}
#[inline(always)]
fn push_char(&mut self, ch: &Char) {
let cluster = self.next_cluster;
self.glyphs.push(GlyphData {
id: ch.glyph_id,
flags: (ch.ignorable as u16) << 6,
class: 0,
char_class: ch.shape_class,
joining_type: ch.joining_type as u8,
mark_type: 0,
mask: 0,
skip: false,
component: !0,
cluster,
data: ch.data,
});
}
fn _push_hangul_char(&mut self, ch: &Char) {
let cluster = self.next_cluster;
let c = ch.ch as u32;
let mask = if (0x1100..=0x115F).contains(&c) || (0xA960..=0xA97C).contains(&c) {
1
} else if (0x1160..=0x11A7).contains(&c) || (0xD7B0..=0xD7C6).contains(&c) {
2
} else if (0x11A8..=0x11FF).contains(&c) || (0xD7CB..=0xD7FB).contains(&c) {
4
} else {
1 | 2 | 4
};
self.glyphs.push(GlyphData {
id: ch.glyph_id,
flags: (ch.ignorable as u16) << 6,
class: 0,
char_class: ch.shape_class,
joining_type: ch.joining_type as u8,
mark_type: 0,
mask,
skip: false,
component: !0,
cluster,
data: ch.data,
});
}
fn push_cluster(&mut self, cluster: &CharCluster) {
self.infos
.push((cluster.info(), false, cluster.user_data()));
self.ranges.push(cluster.range());
}
pub fn clear(&mut self) {
self.glyphs.clear();
self.positions.clear();
self.infos.clear();
self.ranges.clear();
self.is_rtl = false;
self.reversed = false;
self.has_cursive = false;
self.has_marks = false;
self.dotted_circle = None;
self.next_cluster = 0;
self.skip_state = SkipState::default();
}
pub fn ensure_order(&mut self, reversed: bool) {
if reversed != self.reversed {
self.glyphs.reverse();
if !self.positions.is_empty() {
self.positions.reverse();
}
self.reversed = reversed;
}
}
pub fn clear_flags(&mut self, flags: u16, range: Option<Range<usize>>) {
if let Some(range) = range {
for g in &mut self.glyphs[range] {
g.flags &= !flags;
}
} else {
for g in &mut self.glyphs {
g.flags &= !flags;
}
}
}
pub fn setup_positions(&mut self, was_morx: bool) {
if was_morx {
self.glyphs
.retain(|g| g.flags & COMPONENT == 0 && g.id != 0xFFFF);
} else {
self.glyphs.retain(|g| g.flags & COMPONENT == 0);
}
self.positions.clear();
self.positions
.resize(self.glyphs.len(), PositionData::default());
}
pub fn substitute(&mut self, index: usize, id: u16) {
let g = &mut self.glyphs[index];
// if TRACE {
// println!("!subst[{}] {} -> {}", index, g.id, id);
// }
g.id = id;
g.flags |= SUBSTITUTED;
}
pub fn substitute_ligature(&mut self, index: usize, id: u16, components: &[usize]) {
// if TRACE {
// print!("!subst[{}] {}", index, self.glyphs[index].id);
// for c in components {
// print!(" {}", self.glyphs[*c].id)
// }
// println!(" -> {}", id);
// }
if components.is_empty() {
return;
}
let g = &mut self.glyphs[index];
g.id = id;
g.flags |= SUBSTITUTED | LIGATED;
let cluster = g.cluster;
let mut last_index = index;
for (i, &index) in components.iter().enumerate() {
let g = &mut self.glyphs[index];
self.infos[g.cluster as usize].1 = true;
g.id = 0xFFFF;
g.flags |= COMPONENT;
g.class = 5;
g.cluster = cluster;
g.skip = true;
if (index - last_index) > 1 {
let component = i as u8;
for g in &mut self.glyphs[last_index + 1..index] {
if g.mark_type != 0 || g.class == 3 {
g.component = component;
g.cluster = cluster;
}
}
}
last_index = index;
}
if (last_index + 1) < self.glyphs.len() {
let last_component = components.len() as u8;
for g in &mut self.glyphs[last_index + 1..] {
if g.mark_type != 0 || g.class == 3 {
g.component = last_component;
g.cluster = cluster;
} else {
break;
}
}
}
}
pub fn substitute_multiple(&mut self, index: usize, ids: &[u16]) {
let count = ids.len();
if count == 0 {
self.glyphs.remove(index);
return;
} else if count == 1 {
self.substitute(index, ids[0]);
return;
}
// if TRACE {
// println!("!subst[{}] {} -> {:?}", index, self.glyphs[index].id, ids);
// }
let g = self.glyphs[index];
self.glyphs
.splice(index..index + 1, SubstIter { ids, g, cur: 0 });
}
pub fn multiply(&mut self, index: usize, count: usize) {
let g = self
.glyphs
.get(index)
.copied()
.unwrap_or_else(GlyphData::default);
self.glyphs.splice(index..index, (0..count).map(|_| g));
}
pub fn position(&mut self, index: usize, x: f32, y: f32, xadvance: f32, _yadvance: f32) {
let p = &mut self.positions[index];
p.x += x;
p.y += y;
p.advance += xadvance;
}
pub fn position_cursive(&mut self, index: usize, next: usize, x: f32, y: f32) {
let p = &mut self.positions[index];
self.has_cursive = true;
p.flags = CURSIVE_ATTACH;
if true {
//self.dir.is_horizontal() {
p.y = y;
//p.advance -= x;
} else {
p.x = x;
}
p.base = (next - index) as u8;
}
pub fn position_mark(&mut self, index: usize, base: usize, dx: f32, dy: f32) {
let p = &mut self.positions[index];
self.has_marks = true;
p.flags = MARK_ATTACH;
p.base = (index - base) as u8;
p.x = dx;
p.y = dy;
}
pub fn set_join_masks(&mut self) {
let mut prev: Option<usize> = None;
let mut state = 0;
let glyphs = &mut self.glyphs;
let len = glyphs.len();
// Transparent joining type.
const JOIN_T: u8 = 6;
for i in 0..len {
let ty = glyphs[i].joining_type;
if ty == JOIN_T {
continue;
}
let entry = JOIN_STATES[state][ty as usize];
if let Some(j) = prev {
if entry.0 != NONE_MASK {
glyphs[j].mask = entry.0;
}
}
glyphs[i].mask = entry.1;
prev = Some(i);
state = entry.2 as usize;
}
}
}
struct SubstIter<'a> {
ids: &'a [u16],
g: GlyphData,
cur: usize,
}
impl<'a> Iterator for SubstIter<'a> {
type Item = GlyphData;
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.ids.len() - self.cur;
(remaining, Some(remaining))
}
fn next(&mut self) -> Option<Self::Item> {
if self.cur >= self.ids.len() {
return None;
}
let g = GlyphData {
id: self.ids[self.cur],
flags: SUBSTITUTED,
..self.g
};
self.cur += 1;
Some(g)
}
}
pub fn reorder_myanmar(chars: &[Char], order: &mut Vec<usize>) {
use ShapeClass::*;
let mut ignored = [false; MAX_CLUSTER_SIZE];
let mut base = None;
let mut kinzi: Option<Range<usize>> = None;
let mut medial_ra = None;
let mut vpre: Option<Range<usize>> = None;
let mut vblw: Option<usize> = None;
let mut anus: Option<Range<usize>> = None;
let mut i = 0;
let mut last_vblw = false;
let len = chars.len();
if len == 0 {
return;
}
if order.len() < len {
order.resize(chars.len(), 0);
}
if chars[0].shape_class == Kinzi {
kinzi = Some(0..3);
ignored[0] = true;
ignored[1] = true;
ignored[2] = true;
i = 3;
}
while i < len {
let ch = chars[i];
let k = ch.shape_class;
if last_vblw && k == Anusvara {
anus = match anus {
Some(r) => Some(r.start..i - r.start + 1),
None => Some(i..i + 1),
};
ignored[i] = true;
i += 1;
continue;
}
last_vblw = false;
if k == VBlw {
if vblw.is_none() {
vblw = Some(i);
}
last_vblw = true;
}
if k == Base && base.is_none() {
base = Some(i);
ignored[i] = true;
} else if k == MedialRa {
medial_ra = Some(i);
ignored[i] = true;
} else if k == VPre {
vpre = match vpre {
Some(r) => Some(r.start..i - r.start + 1),
None => Some(i..i + 1),
};
ignored[i] = true;
}
i += 1;
}
i = 0;
if let Some(r) = vpre {
for j in r {
order[i] = j;
i += 1;
}
}
if let Some(j) = medial_ra {
order[i] = j;
i += 1;
}
if let Some(j) = base {
order[i] = j;
i += 1;
}
if let Some(r) = kinzi {
for j in r {
order[i] = j;
i += 1;
}
}
let mut j = 0;
while j < len {
if ignored[j] {
j += 1;
continue;
}
if Some(j) == vblw && anus.is_some() {
for k in anus.take().unwrap() {
order[i] = k;
i += 1;
}
}
order[i] = j;
i += 1;
j += 1;
}
}
#[allow(clippy::needless_range_loop)]
pub fn reorder_complex(glyphs: &mut [GlyphData], buf: &mut Vec<GlyphData>, order: &mut Vec<usize>) {
use ShapeClass::*;
let mut first_base = None;
let mut last_base = None;
let mut last_halant = None;
let mut reph = None;
let mut pref = None;
let mut vpre: Option<Range<usize>> = None;
let mut vmpre: Option<Range<usize>> = None;
let mut ignored = [false; 64];
let len = glyphs.len();
if buf.len() < glyphs.len() {
buf.resize(len, GlyphData::default());
}
let buf = &mut buf[..len];
if order.len() < len {
order.resize(len, 0);
}
let order = &mut order[..len];
for (i, g) in glyphs.iter().enumerate() {
if g.is_component() {
continue;
}
match g.char_class {
Base => {
if first_base.is_none() {
first_base = Some(i);
ignored[i] = true;
}
if last_halant.is_none() {
last_base = Some(i);
}
}
Halant => {
last_halant = Some(i);
}
Reph => {
if reph.is_none() {
reph = Some(i);
ignored[i] = true;
}
}
Pref => {
if pref.is_none() {
pref = Some(i);
ignored[i] = true;
}
}
VPre => {
vpre = match vpre {
Some(r) => Some(r.start..i - r.start + 1),
None => Some(i..i + 1),
};
ignored[i] = true;
}
VMPre => {
vmpre = match vmpre {
Some(r) => Some(r.start..i - r.start + 1),
None => Some(i..i + 1),
};
ignored[i] = true;
}
_ => {}
}
}
let mut j = 0;
// No explicit virama; insert vmpre, vpre, pref
if last_halant.is_none() {
if let Some(r) = vmpre.clone() {
for i in r {
order[j] = i;
j += 1;
}
}
if let Some(r) = vpre.clone() {
for i in r {
order[j] = i;
j += 1;
}
}
if let Some(i) = pref {
order[j] = i;
j += 1;
}
}
// Insert the base...
if let Some(i) = first_base {
order[j] = i;
j += 1;
}
if last_base.is_none() {
// ... and the reph
if let Some(i) = reph {
order[j] = i;
j += 1;
}
}
// Now the rest
let len = glyphs.len();
for i in 0..len {
if ignored[i] {
continue;
}
// println!(" -> i = {}, j = {}", i, j);
// println!("order: {:?}", order);
// println!("ignored: {:?}", &ignored[0..order.len()]);
order[j] = i;
j += 1;
// Insert reph after final base
if Some(i) == last_base {
if let Some(i) = reph {
order[j] = i;
j += 1;
}
}
// Move vmpre, vpre and pref after the final virama
if Some(i) == last_halant {
if let Some(r) = vmpre.clone() {
for i in r {
order[j] = i;
j += 1;
}
}
if let Some(r) = vpre.clone() {
for i in r {
order[j] = i;
j += 1;
}
}
if let Some(i) = pref {
order[j] = i;
j += 1;
}
}
}
// Reorder glyphs
buf.copy_from_slice(glyphs);
for (i, j) in order.iter().enumerate() {
glyphs[i] = buf[*j];
}
// println!("order: {:?}", order);
// let new = glyphs
// .iter()
// .map(|g| (g.class, char_kind(g.subclass)))
// .collect::<Vec<_>>();
// println!("new: {:?}", &new[..]);
}
// Matches the Arabic joining table from Harfbuzz.
#[rustfmt::skip]
const JOIN_STATES: [[(u8, u8, u8); 6]; 7] = [
// U, L, R, D, ALAPH, DALATH_RISH
// State 0: prev was U, not willing to join.
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,ISOL_MASK,1), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,ISOL_MASK,1), (NONE_MASK,ISOL_MASK,6), ],
// State 1: prev was R or ISOL_MASK/ALAPH, not willing to join.
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,ISOL_MASK,1), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,FIN2_MASK,5), (NONE_MASK,ISOL_MASK,6), ],
// State 2: prev was D/L in ISOL_MASK form, willing to join.
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (INIT_MASK,FINA_MASK,1), (INIT_MASK,FINA_MASK,3), (INIT_MASK,FINA_MASK,4), (INIT_MASK,FINA_MASK,6), ],
// State 3: prev was D in FINA_MASK form, willing to join. */
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (MEDI_MASK,FINA_MASK,1), (MEDI_MASK,FINA_MASK,3), (MEDI_MASK,FINA_MASK,4), (MEDI_MASK,FINA_MASK,6), ],
// State 4: prev was FINA_MASK ALAPH, not willing to join. */
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (MED2_MASK,ISOL_MASK,1), (MED2_MASK,ISOL_MASK,2), (MED2_MASK,FIN2_MASK,5), (MED2_MASK,ISOL_MASK,6), ],
// State 5: prev was FIN2_MASK/FIN3_MASK ALAPH, not willing to join. */
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (ISOL_MASK,ISOL_MASK,1), (ISOL_MASK,ISOL_MASK,2), (ISOL_MASK,FIN2_MASK,5), (ISOL_MASK,ISOL_MASK,6), ],
// State 6: prev was DALATH/RISH, not willing to join. */
[ (NONE_MASK,NONE_MASK,0), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,ISOL_MASK,1), (NONE_MASK,ISOL_MASK,2), (NONE_MASK,FIN3_MASK,5), (NONE_MASK,ISOL_MASK,6), ]
];

122
vendor/swash/src/shape/cache.rs vendored Normal file
View File

@@ -0,0 +1,122 @@
use super::at::FeatureStore;
use super::engine::EngineMetadata;
use super::internal::var::Fvar;
use crate::{charmap::CharmapProxy, metrics::MetricsProxy, FontRef};
use alloc::vec::Vec;
pub type Epoch = u64;
pub struct FontEntry {
pub metrics: MetricsProxy,
pub charmap: CharmapProxy,
pub coord_count: u16,
pub metadata: EngineMetadata,
}
impl FontEntry {
pub fn new(font: &FontRef) -> Self {
Self {
metrics: MetricsProxy::from_font(font),
charmap: CharmapProxy::from_font(font),
coord_count: Fvar::from_font(font)
.map(|fvar| fvar.axis_count())
.unwrap_or(0),
metadata: EngineMetadata::from_font(font),
}
}
}
pub struct FeatureEntry {
pub epoch: Epoch,
pub id: [u64; 2],
pub coords: Vec<i16>,
pub tags: [u32; 4],
pub store: FeatureStore,
}
pub struct FeatureCache {
entries: Vec<FeatureEntry>,
epoch: Epoch,
max_entries: usize,
}
pub enum FeatureCacheEntry<'a> {
New(&'a mut FeatureStore),
Present(&'a mut FeatureStore),
}
impl FeatureCache {
pub fn new(max_entries: usize) -> Self {
Self {
entries: Default::default(),
epoch: 0,
max_entries,
}
}
pub fn entry<'a>(
&'a mut self,
id: [u64; 2],
coords: &[i16],
has_feature_vars: bool,
tags: &[u32; 4],
) -> FeatureCacheEntry<'a> {
match self.find_entry(id, coords, has_feature_vars, tags) {
(true, index) => {
let entry = &mut self.entries[index];
entry.epoch = self.epoch;
FeatureCacheEntry::Present(&mut entry.store)
}
(false, index) => {
self.epoch += 1;
let entry = &mut self.entries[index];
entry.epoch = self.epoch;
FeatureCacheEntry::New(&mut entry.store)
}
}
}
fn find_entry(
&mut self,
id: [u64; 2],
coords: &[i16],
has_feature_vars: bool,
tags: &[u32; 4],
) -> (bool, usize) {
let epoch = self.epoch;
let mut lowest_serial = epoch;
let mut lowest_index = 0;
for (i, entry) in self.entries.iter().enumerate() {
if entry.id == id && &entry.tags == tags {
if has_feature_vars && coords != &entry.coords[..] {
continue;
}
return (true, i);
}
if entry.epoch < lowest_serial {
lowest_serial = entry.epoch;
lowest_index = i;
}
}
if self.entries.len() < self.max_entries {
lowest_index = self.entries.len();
self.entries.push(FeatureEntry {
epoch,
id,
coords: Vec::from(coords),
store: FeatureStore::default(),
tags: *tags,
});
} else {
let entry = &mut self.entries[lowest_index];
entry.epoch = epoch;
entry.id = id;
entry.coords.clear();
entry.coords.extend_from_slice(coords);
entry.store.clear();
entry.tags = *tags;
}
(false, lowest_index)
}
}

93
vendor/swash/src/shape/cluster.rs vendored Normal file
View File

@@ -0,0 +1,93 @@
/*!
Glyph cluster modeling-- output from the shaper.
*/
use super::buffer::MARK_ATTACH;
use crate::text::cluster::{ClusterInfo, SourceRange, UserData};
use crate::GlyphId;
/// Information for a glyph.
#[derive(Copy, Clone, Default, Debug)]
pub struct GlyphInfo(pub u16);
impl GlyphInfo {
/// Returns true if the glyph is an attached mark.
pub fn is_mark(self) -> bool {
self.0 & MARK_ATTACH != 0
}
}
/// Glyph identifier and positioning information as a result of shaping.
#[derive(Copy, Clone, Default, Debug)]
pub struct Glyph {
/// Glyph identifier.
pub id: GlyphId,
/// Glyph flags.
pub info: GlyphInfo,
/// Horizontal offset.
pub x: f32,
/// Vertical offset.
pub y: f32,
/// Advance width or height.
pub advance: f32,
/// Arbitrary user data.
pub data: UserData,
}
/// Collection of glyphs and associated metadata corresponding to one or
/// more source clusters.
#[derive(Copy, Clone, Debug)]
pub struct GlyphCluster<'a> {
/// Full source range of the cluster in original units supplied to the
/// shaper.
pub source: SourceRange,
/// Information about the textual content of the cluster.
pub info: ClusterInfo,
/// Sequence of glyphs for the cluster. May be empty for clusters whose
/// source consisted entirely of control characters.
pub glyphs: &'a [Glyph],
/// If the cluster is a ligature, this contains the source range
/// of each ligature component. Empty otherwise.
pub components: &'a [SourceRange],
/// Arbitrary user data-- taken from the initial character of the cluster.
pub data: UserData,
}
impl<'a> GlyphCluster<'a> {
/// Returns true if the cluster is empty. Empty clusters still represent
/// characters in the source text, but contain no glyphs. This will be
/// true, for example, with newline sequences (\n or \r\n) as well as other
/// control characters.
pub fn is_empty(&self) -> bool {
self.glyphs.is_empty()
}
/// Returns true if the cluster contains a single glyph. Note that a simple
/// cluster can also be a ligature.
pub fn is_simple(&self) -> bool {
self.glyphs.len() == 1
}
/// Returns true if the cluster corresponds to multiple source clusters.
/// Note that a ligature cluster can also be complex.
pub fn is_ligature(&self) -> bool {
!self.components.is_empty()
}
/// Returns true if the cluster is complex-- that is if it contains more
/// than one glyph. This will be true for clusters containing marks and is
/// also commonly true for syllabic languages such as those in the Indic
/// family.
pub fn is_complex(&self) -> bool {
self.glyphs.len() > 1
}
/// Computes the full advance width or height of the cluster.
pub fn advance(&self) -> f32 {
let mut advance = 0.;
for g in self.glyphs {
advance += g.advance;
}
advance
}
}

300
vendor/swash/src/shape/engine.rs vendored Normal file
View File

@@ -0,0 +1,300 @@
use super::{aat, at};
use super::buffer::*;
use super::internal::{self, at::Gdef, raw_tag, Bytes, RawFont, RawTag};
use crate::font::FontRef;
use crate::text::{Language, Script};
use alloc::vec::Vec;
use core::ops::Range;
/// Shaping engine that handles the various methods available in
/// an OpenType font.
pub struct Engine<'a> {
pub data: Bytes<'a>,
pub gdef: Gdef<'a>,
pub gsub: at::StageOffsets,
pub gpos: at::StageOffsets,
pub morx: u32,
pub kerx: u32,
pub ankr: u32,
pub kern: u32,
pub storage: at::Storage,
pub coords: &'a [i16],
pub script: Script,
pub tags: [RawTag; 4],
pub sub_mode: SubMode,
pub pos_mode: PosMode,
pub use_ot: bool,
pub mode: EngineMode,
}
impl<'a> Engine<'a> {
/// Creates a new shaping engine from precreated metadata.
pub fn new(
metadata: &EngineMetadata,
font_data: &'a [u8],
coords: &'a [i16],
script: Script,
lang: Option<Language>,
) -> Self {
let data = Bytes::new(font_data);
let gdef = Gdef::from_offset(font_data, metadata.gdef).unwrap_or_else(Gdef::empty);
let script_tag = script.to_opentype();
let lang_tag = lang.and_then(|l| l.to_opentype());
let (gsub, stags) = if metadata.sub_mode == SubMode::Gsub {
at::StageOffsets::new(&data, metadata.gsub, script_tag, lang_tag).unwrap_or_default()
} else {
(at::StageOffsets::default(), [0, 0])
};
let (gpos, ptags) = if metadata.pos_mode == PosMode::Gpos {
at::StageOffsets::new(&data, metadata.gpos, script_tag, lang_tag).unwrap_or_default()
} else {
(at::StageOffsets::default(), [0, 0])
};
let tags = [stags[0], stags[1], ptags[0], ptags[1]];
let use_ot = gsub.lang != 0 || gpos.lang != 0;
let mode = if gsub.lang != 0 && script.is_complex() {
if script == Script::Myanmar {
EngineMode::Myanmar
} else {
EngineMode::Complex
}
} else {
EngineMode::Simple
};
let mut sub_mode = metadata.sub_mode;
let mut pos_mode = metadata.pos_mode;
if sub_mode == SubMode::Gsub && gsub.lang == 0 {
sub_mode = SubMode::None;
}
if pos_mode == PosMode::Gpos && gpos.lang == 0 {
pos_mode = PosMode::None;
}
Self {
data,
gdef,
gsub,
gpos,
morx: metadata.morx,
kerx: metadata.kerx,
ankr: metadata.ankr,
kern: metadata.kern,
storage: at::Storage::default(),
coords,
script,
tags,
sub_mode,
pos_mode,
use_ot,
mode,
}
}
}
/// OpenType shaping.
impl<'a> Engine<'a> {
/// Returns the script and language tags that have been selected for
/// the GSUB and GPOS tables.
pub fn tags(&self) -> &[RawTag; 4] {
&self.tags
}
/// Builds a feature store for the current engine configuration.
pub fn collect_features(
&self,
builder: &mut at::FeatureStoreBuilder,
store: &mut at::FeatureStore,
) {
builder.build(
store,
self.data.data(),
self.coords,
&self.gdef,
&self.gsub,
&self.gpos,
);
store.groups = store.groups(self.script);
}
/// Returns true if feature variations are supported.
pub fn has_feature_vars(&self) -> bool {
self.gsub.var != 0 || self.gpos.var != 0
}
/// Sets glyph and mark classes for the specified range of the buffer.
pub fn set_classes(&self, buffer: &mut Buffer, range: Option<Range<usize>>) {
if !self.gdef.ok() {
return;
}
let slice = if let Some(range) = range {
&mut buffer.glyphs[range]
} else {
&mut buffer.glyphs[..]
};
let gdef = &self.gdef;
if gdef.has_mark_classes() {
for g in slice.iter_mut() {
g.class = gdef.class(g.id) as u8;
g.mark_type = gdef.mark_class(g.id) as u8;
}
} else {
for g in slice.iter_mut() {
g.class = gdef.class(g.id) as u8;
}
}
}
/// Applies the GSUB features to the specified range of the buffer.
pub fn gsub(
&mut self,
store: &at::FeatureStore,
feature_mask: impl Into<at::FeatureMask>,
buffer: &mut Buffer,
buffer_range: Option<Range<usize>>,
) -> bool {
at::apply(
0,
&self.data,
self.gsub.base,
self.coords,
&self.gdef,
&mut self.storage,
store,
feature_mask.into(),
buffer,
buffer_range,
) == Some(true)
}
/// Applies the GPOS features to the specified range of the buffer.
pub fn gpos(
&mut self,
store: &at::FeatureStore,
feature_mask: impl Into<at::FeatureMask>,
buffer: &mut Buffer,
buffer_range: Option<Range<usize>>,
) -> bool {
at::apply(
1,
&self.data,
self.gpos.base,
self.coords,
&self.gdef,
&mut self.storage,
store,
feature_mask.into(),
buffer,
buffer_range,
) == Some(true)
}
}
/// Apple shaping.
impl<'a> Engine<'a> {
/// Converts a feature list into a sorted collection of AAT selectors.
pub fn collect_selectors(&self, features: &[(RawTag, u16)], selectors: &mut Vec<(u16, u16)>) {
use internal::aat::morx::feature_from_tag;
selectors.clear();
for (tag, value) in features {
if let Some((selector, [on, off])) = feature_from_tag(*tag) {
let setting = if *value == 0 { off } else { on };
selectors.push((selector, setting));
}
}
selectors.sort_unstable();
}
/// Applies the extended metamorphosis table.
pub fn morx(&self, buffer: &mut Buffer, selectors: &[(u16, u16)]) {
if self.morx != 0 {
aat::apply_morx(self.data.data(), self.morx, buffer, selectors);
buffer.ensure_order(false);
}
}
/// Applies the extended kerning table.
pub fn kerx(&self, buffer: &mut Buffer, disable_kern: bool) {
if self.kerx != 0 {
aat::apply_kerx(self.data.data(), self.kerx, self.ankr, buffer, disable_kern);
buffer.ensure_order(false);
}
}
/// Applies the kerning table.
pub fn kern(&self, buffer: &mut Buffer) {
if self.kern != 0 {
aat::apply_kern(self.data.data(), self.kern, buffer);
}
}
}
/// The overall mode of the engine based on a combination of the
/// supported tables and the selected script.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum EngineMode {
Simple,
Myanmar,
Complex,
}
/// The substitution mode supported by the engine.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum SubMode {
None,
Gsub,
Morx,
}
/// The positioning mode supported by the engine.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum PosMode {
None,
Gpos,
Kerx,
Kern,
}
/// Metadata for creating a shaping engine.
#[derive(Copy, Clone)]
pub struct EngineMetadata {
pub gdef: u32,
pub gsub: u32,
pub gpos: u32,
pub morx: u32,
pub kerx: u32,
pub ankr: u32,
pub kern: u32,
pub sub_mode: SubMode,
pub pos_mode: PosMode,
}
impl EngineMetadata {
pub fn from_font(font: &FontRef) -> Self {
let mut this = Self {
gdef: font.table_offset(raw_tag(b"GDEF")),
gsub: font.table_offset(raw_tag(b"GSUB")),
gpos: font.table_offset(raw_tag(b"GPOS")),
morx: font.table_offset(raw_tag(b"morx")),
// ltag: font.table_offset(raw_tag(b"ltag")),
kerx: font.table_offset(raw_tag(b"kerx")),
ankr: font.table_offset(raw_tag(b"ankr")),
kern: font.table_offset(raw_tag(b"kern")),
sub_mode: SubMode::None,
pos_mode: PosMode::None,
};
if this.gsub != 0 {
this.sub_mode = SubMode::Gsub;
} else if this.morx != 0 {
this.sub_mode = SubMode::Morx;
}
if this.gpos != 0 {
this.pos_mode = PosMode::Gpos;
} else if this.kerx != 0 {
this.pos_mode = PosMode::Kerx;
} else if this.kern != 0 {
this.pos_mode = PosMode::Kern;
}
this
}
}

71
vendor/swash/src/shape/feature.rs vendored Normal file
View File

@@ -0,0 +1,71 @@
//! Feature constants.
use super::internal::{raw_tag, RawTag};
// Default tag used in various places.
pub const _DFLT: RawTag = raw_tag(b"DFLT");
// Substitution features.
pub const CCMP: RawTag = raw_tag(b"ccmp");
pub const LOCL: RawTag = raw_tag(b"locl");
pub const RVRN: RawTag = raw_tag(b"rvrn");
pub const LIGA: RawTag = raw_tag(b"liga");
pub const CLIG: RawTag = raw_tag(b"clig");
pub const RLIG: RawTag = raw_tag(b"rlig");
pub const _DLIG: RawTag = raw_tag(b"dlig");
pub const CALT: RawTag = raw_tag(b"calt");
pub const LJMO: RawTag = raw_tag(b"ljmo");
pub const VJMO: RawTag = raw_tag(b"vjmo");
pub const TJMO: RawTag = raw_tag(b"tjmo");
pub const NUKT: RawTag = raw_tag(b"nukt");
pub const AKHN: RawTag = raw_tag(b"akhn");
pub const RKRF: RawTag = raw_tag(b"rkrf");
pub const HALF: RawTag = raw_tag(b"half");
pub const HALN: RawTag = raw_tag(b"haln");
pub const VATU: RawTag = raw_tag(b"vatu");
pub const CJCT: RawTag = raw_tag(b"cjct");
pub const ISOL: RawTag = raw_tag(b"isol");
pub const INIT: RawTag = raw_tag(b"init");
pub const MEDI: RawTag = raw_tag(b"medi");
pub const MED2: RawTag = raw_tag(b"med2");
pub const FINA: RawTag = raw_tag(b"fina");
pub const FIN2: RawTag = raw_tag(b"fin2");
pub const FIN3: RawTag = raw_tag(b"fin3");
pub const MSET: RawTag = raw_tag(b"mset");
pub const RPHF: RawTag = raw_tag(b"rphf");
pub const PREF: RawTag = raw_tag(b"pref");
pub const ABVF: RawTag = raw_tag(b"abvf");
pub const BLWF: RawTag = raw_tag(b"blwf");
pub const PSTF: RawTag = raw_tag(b"pstf");
pub const PRES: RawTag = raw_tag(b"pres");
pub const ABVS: RawTag = raw_tag(b"abvs");
pub const BLWS: RawTag = raw_tag(b"blws");
pub const PSTS: RawTag = raw_tag(b"psts");
pub const RCLT: RawTag = raw_tag(b"rclt");
pub const VERT: RawTag = raw_tag(b"vert");
pub const VRT2: RawTag = raw_tag(b"vrt2");
pub const RTLM: RawTag = raw_tag(b"rtlm");
// Positioning features.
pub const KERN: RawTag = raw_tag(b"kern");
pub const DIST: RawTag = raw_tag(b"dist");
pub const ABVM: RawTag = raw_tag(b"abvm");
pub const BLWM: RawTag = raw_tag(b"blwm");
pub const CURS: RawTag = raw_tag(b"curs");
pub const MARK: RawTag = raw_tag(b"mark");
pub const MKMK: RawTag = raw_tag(b"mkmk");
// Arabic joining masks.
pub const ISOL_MASK: u8 = 1;
pub const INIT_MASK: u8 = 2;
pub const MEDI_MASK: u8 = 4;
pub const FINA_MASK: u8 = 8;
pub const MED2_MASK: u8 = 16;
pub const FIN2_MASK: u8 = 32;
pub const FIN3_MASK: u8 = 64;
pub const NONE_MASK: u8 = 0;
// Hangul jamo masks.
pub const LJMO_MASK: u8 = 1;
pub const VJMO_MASK: u8 = 2;
pub const TJMO_MASK: u8 = 4;

936
vendor/swash/src/shape/mod.rs vendored Normal file
View File

@@ -0,0 +1,936 @@
/*!
Mapping complex text to a sequence of positioned glyphs.
Shaping is the process of converting a sequence of
[character clusters](CharCluster) into a sequence of
[glyph clusters](GlyphCluster) with respect to the rules of a particular
writing system and the typographic features available in a font. The shaper
operates on one _item_ at a time where an item is a run of text with
a single script, language, direction, font, font size, and set of variation/feature
settings. The process of producing these runs is called _itemization_
and is out of scope for this crate.
# Building the shaper
All shaping in this crate takes place within the purview of a
[`ShapeContext`]. This opaque struct manages internal LRU caches and scratch
buffers that are necessary for the shaping process. Generally, you'll
want to keep an instance that persists for more than one layout pass as
this amortizes the cost of allocations, reduces contention for the global
heap and increases the hit rate for the internal acceleration structures. If
you're doing multithreaded layout, you should keep a context per thread.
The only method available on the context is [`builder`](ShapeContext::builder)
which takes a type that can be converted into a [`FontRef`] as an argument
and produces a [`ShaperBuilder`] that provides options for configuring and
building a [`Shaper`].
Here, we'll create a context and build a shaper for Arabic text at 16px:
```
# use swash::{FontRef, CacheKey, shape::*, text::Script};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ShapeContext::new();
let mut shaper = context.builder(font)
.script(Script::Arabic)
.direction(Direction::RightToLeft)
.size(16.)
.build();
```
You can specify feature settings by calling the [`features`](ShaperBuilder::features)
method with an iterator that yields a sequence of values that are convertible
to [`Setting<u16>`]. Tuples of (&str, u16) will work in a pinch. For example,
you can enable discretionary ligatures like this:
```
# use swash::{FontRef, CacheKey, shape::*, text::Script, tag_from_bytes};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ShapeContext::new();
let mut shaper = context.builder(font)
.script(Script::Latin)
.size(14.)
.features(&[("dlig", 1)])
.build();
```
A value of `0` will disable a feature while a non-zero value will enable it.
Some features use non-zero values as an argument. The stylistic alternates
feature, for example, often offers a collection of choices per glyph. The argument
is used as an index to select among them. If a requested feature is not present
in a font, the setting is ignored.
Font variation settings are specified in a similar manner with the
[`variations`](ShaperBuilder::variations) method but take an `f32`
to define the value within the variation space for the requested axis:
```
# use swash::{FontRef, CacheKey, shape::*, text::Script, tag_from_bytes};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ShapeContext::new();
let mut shaper = context.builder(font)
.script(Script::Latin)
.size(14.)
.variations(&[("wght", 520.5)])
.build();
```
See [`ShaperBuilder`] for available options and default values.
# Feeding the shaper
Once we have a properly configured shaper, we need to feed it some
clusters. The simplest approach is to call the [`add_str`](Shaper::add_str)
method with a string:
```
# use swash::{FontRef, CacheKey, shape::*, text::Script, tag_from_bytes};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
# let mut context = ShapeContext::new();
# let mut shaper = context.builder(font).build();
shaper.add_str("a quick brown fox?");
```
You can call [`add_str`](Shaper::add_str) multiple times to add a sequence
of text fragments to the shaper.
This simple approach is certainly reasonable when dealing with text consisting
of a single run on one line with a font that is known to contain all the
necessary glyphs. A small text label in a UI is a good example.
For more complex scenarios, the shaper can be fed a single cluster at a time.
This method allows you to provide:
- accurate source ranges per character even if your runs
and items span multiple non-contiguous fragments
- user data per character (a single `u32`) that can be used, for
example, to associate each resulting glyph with a style span
- boundary analysis per character, carrying word boundaries and
line break opportunities through the shaper.
This also provides a junction point for inserting a font fallback
mechanism.
All of this is served by the functionality in the
[`text::cluster`](crate::text::cluster) module.
Let's see a somewhat contrived example that demonstrates the process:
```
use swash::text::cluster::{CharCluster, CharInfo, Parser, Token};
# use swash::{FontRef, CacheKey, shape::*, text::Script, tag_from_bytes};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
# let mut context = ShapeContext::new();
let mut shaper = context.builder(font)
.script(Script::Latin)
.build();
// We'll need the character map for our font
let charmap = font.charmap();
// And some storage for the cluster we're working with
let mut cluster = CharCluster::new();
// Now we build a cluster parser which takes a script and
// an iterator that yields a Token per character
let mut parser = Parser::new(
Script::Latin,
"a quick brown fox?".char_indices().map(|(i, ch)| Token {
// The character
ch,
// Offset of the character in code units
offset: i as u32,
// Length of the character in code units
len: ch.len_utf8() as u8,
// Character information
info: ch.into(),
// Pass through user data
data: 0,
})
);
// Loop over all of the clusters
while parser.next(&mut cluster) {
// Map all of the characters in the cluster
// to nominal glyph identifiers
cluster.map(|ch| charmap.map(ch));
// Add the cluster to the shaper
shaper.add_cluster(&cluster);
}
```
Phew! That's quite a lot of work. It also happens to be exactly what
[`add_str`](Shaper::add_str) does internally.
So why bother? As mentioned earlier, this method allows you to customize
the per-character data that passes through the shaper. Is your source text in
UTF-16 instead of UTF-8? No problem. Set the [`offset`](Token::offset) and
[`len`](Token::len) fields of your [`Token`]s to appropriate values. Are you shaping
across style spans? Set the [`data`](Token::data) field to the index of your span so
it can be recovered. Have you used the
[`Analyze`](crate::text::Analyze) iterator to generate
[`CharInfo`](crate::text::cluster::CharInfo)s containing boundary analysis? This
is where you apply them to the [`info`](Token::info) fields of your [`Token`]s.
That last one deserves a quick example, showing how you might build a cluster
parser with boundary analysis:
```
use swash::text::{analyze, Script};
use swash::text::cluster::{CharInfo, Parser, Token};
let text = "a quick brown fox?";
let mut parser = Parser::new(
Script::Latin,
text.char_indices()
// Call analyze passing the same text and zip
// the results
.zip(analyze(text.chars()))
// Analyze yields the tuple (Properties, Boundary)
.map(|((i, ch), (props, boundary))| Token {
ch,
offset: i as u32,
len: ch.len_utf8() as u8,
// Create character information from properties and boundary
info: CharInfo::new(props, boundary),
data: 0,
}),
);
```
That leaves us with font fallback. This crate does not provide the infrastructure
for such, but a small example can demonstrate the idea. The key is in
the return value of the [`CharCluster::map`] method which describes the
[`Status`](crate::text::cluster::Status) of the mapping operation. This function
will return the index of the best matching font:
```
use swash::FontRef;
use swash::text::cluster::{CharCluster, Status};
fn select_font<'a>(fonts: &[FontRef<'a>], cluster: &mut CharCluster) -> Option<usize> {
let mut best = None;
for (i, font) in fonts.iter().enumerate() {
let charmap = font.charmap();
match cluster.map(|ch| charmap.map(ch)) {
// This font provided a glyph for every character
Status::Complete => return Some(i),
// This font provided the most complete mapping so far
Status::Keep => best = Some(i),
// A previous mapping was more complete
Status::Discard => {}
}
}
best
}
```
Note that [`CharCluster`] maintains internal composed and decomposed sequences
of the characters in the cluster so that it can select the best form for each
candidate font.
Since this process is done during shaping, upon return we compare the selected
font with our current font and if they're different, we complete shaping for the
clusters submitted so far and continue the process by building a new shaper with
the selected font. By doing manual cluster parsing and nominal glyph mapping
_outside_ the shaper, we can implement per-cluster font fallback without the costly
technique of heuristically shaping runs.
# Collecting the prize
Finish up shaping by calling [`Shaper::shape_with`] with a closure that will be
invoked with each resulting [`GlyphCluster`]. This structure contains borrowed data
and thus cannot be stored directly. The data you extract from each cluster and the
method in which you store it will depend entirely on the design of your text layout
system.
Please note that, unlike HarfBuzz, this shaper does _not_ reverse runs that are in
right-to-left order. The reasoning is that, for correctness, line breaking must be
done in logical order and reversing runs should occur during bidi reordering.
Also pertinent to right-to-left runs: you'll need to ensure that you reverse
_clusters_ and not _glyphs_. Intra-cluster glyphs must remain in logical order
for proper mark placement.
*/
pub mod cluster;
#[doc(hidden)]
pub mod partition;
mod aat;
mod at;
mod buffer;
mod cache;
mod engine;
mod feature;
use cluster::*;
use super::{
cache::FontCache, charmap::Charmap, internal, metrics::Metrics, setting::Setting, FontRef,
NormalizedCoord,
};
use crate::text::{
cluster::{CharCluster, Parser, ShapeClass, Token},
Language, Script,
};
use alloc::vec::Vec;
use at::{FeatureMask, FeatureStore, FeatureStoreBuilder};
use buffer::*;
use cache::{FeatureCache, FontEntry};
use core::borrow::Borrow;
use engine::{Engine, EngineMode};
const DEFAULT_SIZE: usize = 16;
/// Text direction.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Direction {
LeftToRight,
RightToLeft,
}
/// Context that manages caches and transient buffers for shaping.
///
/// See the module level [documentation](index.html#building-the-shaper) for detail.
pub struct ShapeContext {
font_cache: FontCache<FontEntry>,
feature_cache: FeatureCache,
coords: Vec<i16>,
state: State,
}
impl ShapeContext {
/// Creates a new shaping context.
pub fn new() -> Self {
Self::with_max_entries(DEFAULT_SIZE)
}
/// Creates a new shaping context with the specified maximum number of
/// cache entries.
pub fn with_max_entries(max_entries: usize) -> Self {
let max_entries = max_entries.clamp(1, 64);
Self {
font_cache: FontCache::new(max_entries),
feature_cache: FeatureCache::new(max_entries),
coords: Vec::new(),
state: State::new(),
}
}
/// Creates a new builder for constructing a shaper with this context
/// and the specified font.
pub fn builder<'a>(&'a mut self, font: impl Into<FontRef<'a>>) -> ShaperBuilder<'a> {
ShaperBuilder::new(self, font)
}
/// Creates a new builder for constructing a shaper with this context
/// and the specified font.
pub fn builder_with_id<'a>(
&'a mut self,
font: impl Into<FontRef<'a>>,
id: [u64; 2],
) -> ShaperBuilder<'a> {
ShaperBuilder::new_with_id(self, font, id)
}
}
impl Default for ShapeContext {
fn default() -> Self {
Self::new()
}
}
struct State {
buffer: Buffer,
store_builder: FeatureStoreBuilder,
order: Vec<usize>,
glyphs: Vec<GlyphData>,
disable_kern: bool,
features: Vec<(u32, u16)>,
selectors: Vec<(u16, u16)>,
}
impl State {
pub fn new() -> Self {
Self {
buffer: Buffer::new(),
store_builder: FeatureStoreBuilder::default(),
order: Vec::new(),
glyphs: Vec::new(),
disable_kern: false,
features: Vec::new(),
selectors: Vec::new(),
}
}
pub fn reset(&mut self) {
self.buffer.clear();
self.features.clear();
self.disable_kern = false;
}
}
/// Builder for configuring a shaper.
///
/// See the module level [documentation](index.html#building-the-shaper) for more detail.
pub struct ShaperBuilder<'a> {
state: &'a mut State,
feature_cache: &'a mut FeatureCache,
font: FontRef<'a>,
font_id: [u64; 2],
font_entry: &'a FontEntry,
coords: &'a mut Vec<i16>,
charmap: Charmap<'a>,
dotted_circle: Option<u16>,
retain_ignorables: bool,
size: f32,
script: Script,
lang: Option<Language>,
dir: Direction,
}
impl<'a> ShaperBuilder<'a> {
/// Creates a new builder for configuring a shaper with the specified
/// context and font.
fn new(context: &'a mut ShapeContext, font: impl Into<FontRef<'a>>) -> Self {
let font = font.into();
let id = [font.key.value(), u64::MAX];
Self::new_with_id(context, font, id)
}
/// Creates a new builder for configuring a shaper with the specified
/// context and font.
fn new_with_id(
context: &'a mut ShapeContext,
font: impl Into<FontRef<'a>>,
id: [u64; 2],
) -> Self {
let font = font.into();
let (font_id, font_entry) = context.font_cache.get(&font, Some(id), FontEntry::new);
context.state.reset();
context.coords.clear();
Self {
state: &mut context.state,
feature_cache: &mut context.feature_cache,
font,
font_id,
font_entry,
coords: &mut context.coords,
charmap: font_entry.charmap.materialize(&font),
dotted_circle: None,
retain_ignorables: false,
size: 0.,
script: Script::Latin,
lang: None,
dir: Direction::LeftToRight,
}
}
/// Specifies the script. The default value is [`Script::Latin`].
pub fn script(mut self, script: Script) -> Self {
self.script = script;
self
}
/// Specifies the language. The default value is `None`.
pub fn language(mut self, language: Option<Language>) -> Self {
self.lang = language;
self
}
/// Specifies the text direction. The default value is [`Direction::LeftToRight`].
pub fn direction(mut self, direction: Direction) -> Self {
self.dir = direction;
self
}
/// Specifies the font size in pixels per em. The default value is `0`
/// which will produce glyphs with offsets and advances in font units.
pub fn size(mut self, ppem: f32) -> Self {
self.size = ppem.max(0.);
self
}
/// Adds feature settings to the shaper.
pub fn features<I>(self, settings: I) -> Self
where
I: IntoIterator,
I::Item: Into<Setting<u16>>,
{
for feature in settings {
let feature = feature.into();
if feature.tag == feature::KERN {
self.state.disable_kern = feature.value == 0;
}
self.state.features.push((feature.tag, feature.value));
}
self
}
/// Adds variation settings to the shaper.
pub fn variations<I>(self, settings: I) -> Self
where
I: IntoIterator,
I::Item: Into<Setting<f32>>,
{
if self.font_entry.coord_count != 0 {
let vars = self.font.variations();
self.coords.resize(vars.len(), 0);
for setting in settings {
let setting = setting.into();
for var in vars {
if var.tag() == setting.tag {
let value = var.normalize(setting.value);
if let Some(c) = self.coords.get_mut(var.index()) {
*c = value;
}
}
}
}
}
self
}
/// Specifies the variation settings in terms of normalized coordinates.
pub fn normalized_coords<I>(self, coords: I) -> Self
where
I: IntoIterator,
I::Item: Borrow<NormalizedCoord>,
{
self.coords.clear();
self.coords.extend(coords.into_iter().map(|c| *c.borrow()));
self
}
/// Specifies whether to insert dotted circles for broken clusters. The
/// default value is `false`.
pub fn insert_dotted_circles(mut self, yes: bool) -> Self {
if yes {
let gid = self.charmap.map('\u{25cc}');
if gid != 0 {
self.dotted_circle = Some(gid);
}
} else {
self.dotted_circle = None;
}
self
}
/// Specifies whether characters defined as default ignorable should be
/// retained by the shaper. The default is `false`.
pub fn retain_ignorables(mut self, yes: bool) -> Self {
self.retain_ignorables = yes;
self
}
/// Builds a shaper for the current configuration.
pub fn build(self) -> Shaper<'a> {
let engine = Engine::new(
&self.font_entry.metadata,
self.font.data,
&self.coords[..],
self.script,
self.lang,
);
self.state.buffer.dotted_circle = self.dotted_circle;
let rtl = self.dir == Direction::RightToLeft;
self.state.buffer.is_rtl = rtl;
let (store, sub_mask, pos_mask) = if engine.use_ot {
use cache::FeatureCacheEntry;
let store = match self.feature_cache.entry(
self.font_id,
&self.coords[..],
engine.has_feature_vars(),
engine.tags(),
) {
FeatureCacheEntry::Present(store) => store,
FeatureCacheEntry::New(store) => {
engine.collect_features(&mut self.state.store_builder, store);
store
}
};
let buf = &mut self.state.buffer;
let (sub, pos) = store.custom_masks(
&self.state.features[..],
&mut buf.sub_args,
&mut buf.pos_args,
self.dir,
);
(Some(store as _), sub, pos)
} else {
(None, FeatureMask::default(), FeatureMask::default())
};
Shaper {
state: self.state,
font: self.font,
font_entry: self.font_entry,
charmap: self.charmap,
retain_ignorables: self.retain_ignorables,
size: self.size,
script: self.script,
joined: engine.use_ot && self.script.is_joined(),
dir: self.dir,
engine,
store,
sub_mask,
pos_mask,
}
}
}
/// Maps character clusters to positioned glyph clusters according to
/// typographic rules and features.
///
/// See the module level [documentation](index.html#feeding-the-shaper) for detail.
pub struct Shaper<'a> {
state: &'a mut State,
font: FontRef<'a>,
font_entry: &'a FontEntry,
charmap: Charmap<'a>,
retain_ignorables: bool,
size: f32,
script: Script,
joined: bool,
dir: Direction,
engine: Engine<'a>,
store: Option<&'a FeatureStore>,
sub_mask: FeatureMask,
pos_mask: FeatureMask,
}
impl<'a> Shaper<'a> {
/// Adds a character cluster to the shaper.
pub fn add_cluster(&mut self, cluster: &CharCluster) {
let buf = &mut self.state.buffer;
match self.engine.mode {
EngineMode::Simple => {
buf.push(cluster);
}
EngineMode::Myanmar => {
let e = &mut self.engine;
let s = self.store.unwrap();
let chars = cluster.mapped_chars();
reorder_myanmar(chars, &mut self.state.order);
let range = buf.push_order(cluster, &self.state.order);
e.set_classes(buf, Some(range.clone()));
let start = range.start;
e.gsub(s, s.groups.default, buf, Some(range));
let end = buf.len();
e.gsub(s, s.groups.reph, buf, Some(start..end));
let end = buf.len();
e.gsub(s, s.groups.pref, buf, Some(start..end));
let end = buf.len();
e.gsub(s, s.groups.stage1, buf, Some(start..end));
let end = buf.len();
e.gsub(s, s.groups.stage2, buf, Some(start..end));
}
EngineMode::Complex => {
let e = &mut self.engine;
let s = self.store.unwrap();
let range = buf.push(cluster);
e.set_classes(buf, Some(range.clone()));
let start = range.start;
// Default group
e.gsub(s, s.groups.default, buf, Some(range.clone()));
for g in &mut buf.glyphs[range] {
if g.char_class == ShapeClass::Halant && g.flags & SUBSTITUTED != 0 {
// Don't prevent reordering across a virama that has been substituted
g.char_class = ShapeClass::Other;
}
}
// Reph identification
let len = 3.min(buf.glyphs.len() - start);
let end = start + len;
buf.clear_flags(buffer::SUBSTITUTED, Some(start..end));
e.gsub(s, s.groups.reph, buf, Some(start..end));
for g in &mut buf.glyphs[start..end] {
if g.flags & buffer::SUBSTITUTED != 0 {
g.char_class = ShapeClass::Reph;
break;
}
}
// Pref identification
let end = buf.len();
buf.clear_flags(buffer::SUBSTITUTED, Some(start..end));
e.gsub(s, s.groups.pref, buf, Some(start..end));
for g in &mut buf.glyphs[start..end] {
if g.flags & buffer::SUBSTITUTED != 0 {
g.char_class = ShapeClass::Pref;
break;
}
}
// Orthographic group
let end = buf.len();
e.gsub(s, s.groups.stage1, buf, Some(start..end));
// Reordering
let len = (buf.len() - start).min(64);
let end = start + len;
reorder_complex(
&mut buf.glyphs[start..end],
&mut self.state.glyphs,
&mut self.state.order,
);
}
}
}
/// Adds a string to the shaper.
pub fn add_str(&mut self, s: &str) {
use crate::text::Codepoint;
let mut cluster = CharCluster::new();
let mut parser = Parser::new(
self.script,
s.char_indices().map(|(i, ch)| Token {
ch,
offset: i as u32,
len: ch.len_utf8() as u8,
info: ch.properties().into(),
data: 0,
}),
);
let charmap = self.charmap;
while parser.next(&mut cluster) {
cluster.map(|ch| charmap.map(ch));
self.add_cluster(&cluster);
}
}
/// Returns the current normalized variation coordinates in use by the
/// shaper.
pub fn normalized_coords(&self) -> &[NormalizedCoord] {
self.engine.coords
}
/// Returns the current font metrics in use by the shaper.
pub fn metrics(&self) -> Metrics {
let scale = if self.size != 0. { self.size } else { 1. };
self.font_entry
.metrics
.materialize_metrics(&self.font, self.engine.coords)
.scale(scale)
}
/// Shapes the text and invokes the specified closure with each
/// resulting glyph cluster.
pub fn shape_with(mut self, mut f: impl FnMut(&GlyphCluster)) {
self.finish();
let buf = &mut self.state.buffer;
buf.shaped_glyphs.clear();
let mut sentinel = (
buffer::GlyphData::default(),
buffer::PositionData::default(),
);
sentinel.0.cluster = buf.ranges.len() as u32;
let mut last_cluster = 0;
for (g, p) in buf
.glyphs
.iter()
.zip(&buf.positions)
.chain(core::iter::once((&sentinel.0, &sentinel.1)))
{
if g.cluster != last_cluster {
// Simple and common case: no ligatures and no empty clusters.
if last_cluster > g.cluster || g.cluster - last_cluster == 1 {
let index = last_cluster as usize;
let info = &buf.infos[index];
let cluster = GlyphCluster {
source: buf.ranges[index],
info: info.0,
glyphs: &buf.shaped_glyphs,
components: &[],
data: info.2,
};
f(&cluster);
buf.shaped_glyphs.clear();
} else {
// Collect the range for the non-empty cluster.
let end = g.cluster as usize;
let start = last_cluster as usize;
let mut group_end = start + 1;
while group_end < end && buf.infos[group_end].1 {
group_end += 1;
}
if !buf.shaped_glyphs.is_empty() {
// We have some glyphs. Emit the cluster.
let mut source = buf.ranges[start];
source.end = buf.ranges[group_end - 1].end;
// If the range spans more than one cluster, we have a ligature.
let components = if group_end > start + 1 {
&buf.ranges[start..group_end]
} else {
&[]
};
let info = &buf.infos[start];
let cluster = GlyphCluster {
source,
info: info.0,
glyphs: &buf.shaped_glyphs,
components,
data: info.2,
};
f(&cluster);
buf.shaped_glyphs.clear();
}
if end > group_end {
// We have a trailing sequence of empty clusters. Emit
// them one by one.
for (info, source) in buf.infos[group_end..end]
.iter()
.zip(&buf.ranges[group_end..end])
{
let cluster = GlyphCluster {
source: *source,
info: info.0,
glyphs: &[],
components: &[],
data: info.2,
};
f(&cluster);
}
}
}
}
last_cluster = g.cluster;
if self.retain_ignorables || g.flags & IGNORABLE == 0 {
buf.shaped_glyphs.push(Glyph::new(g, p));
}
}
}
// FIXME: when writing docs, I realized that it's impossible
// to use the result of this function correctly with RTL runs
// that contain marks.
// /// Shapes the text and invokes the specified closure with each
// /// resulting glyph.
// pub fn shape_glyphs_with(mut self, mut f: impl FnMut(&Glyph)) {
// self.finish();
// let buf = &self.state.buffer;
// for (g, p) in buf.glyphs.iter().zip(&buf.positions) {
// if g.flags & IGNORABLE == 0 {
// f(&Glyph::new(g, p))
// }
// }
// }
fn finish(&mut self) {
use engine::{PosMode, SubMode};
if self.state.buffer.glyphs.is_empty() {
return;
}
let e = &mut self.engine;
let buf = &mut self.state.buffer;
match e.mode {
EngineMode::Simple => match e.sub_mode {
SubMode::Gsub => {
let s = self.store.unwrap();
e.set_classes(buf, None);
if self.joined {
buf.set_join_masks();
}
e.gsub(s, self.sub_mask, buf, None);
}
SubMode::Morx => {
e.collect_selectors(&self.state.features, &mut self.state.selectors);
e.morx(buf, &self.state.selectors);
}
_ => {}
},
EngineMode::Myanmar => {
let s = self.store.unwrap();
e.gsub(s, self.sub_mask | s.groups.stage2, buf, None);
}
EngineMode::Complex => {
let s = self.store.unwrap();
if self.joined {
buf.set_join_masks();
e.gsub(s, s.groups.stage2 | self.sub_mask, buf, None);
} else {
e.gsub(s, self.sub_mask, buf, None);
}
}
}
buf.setup_positions(e.sub_mode == SubMode::Morx);
match e.pos_mode {
PosMode::Gpos => {
let s = self.store.unwrap();
e.gpos(s, self.pos_mask, buf, None);
}
PosMode::Kerx => {
e.kerx(buf, self.state.disable_kern);
}
PosMode::Kern => {
if !self.state.disable_kern {
e.kern(buf);
}
}
_ => {}
}
// let metrics = self
// .font_entry
// .metrics
// .materialize_metrics(self.font.data, self.engine.coords);
let glyph_metrics = self
.font_entry
.metrics
.materialize_glyph_metrics(&self.font, self.engine.coords);
for (g, p) in buf.glyphs.iter_mut().zip(buf.positions.iter_mut()) {
if g.flags & MARK_ATTACH == 0 {
p.advance += glyph_metrics.advance_width(g.id);
}
g.flags |= p.flags;
}
if buf.has_cursive {
if self.dir == Direction::RightToLeft {
for (i, g) in buf.glyphs.iter().enumerate().rev() {
if g.flags & buffer::CURSIVE_ATTACH != 0 {
let base_offset = buf.positions[i].base as usize;
if base_offset != 0 {
let (x, y) = {
let base = &buf.positions[i + base_offset];
(base.x, base.y)
};
let pos = &mut buf.positions[i];
pos.x += x;
pos.y += y;
}
}
}
} else {
for (i, g) in buf.glyphs.iter().enumerate() {
if g.flags & buffer::CURSIVE_ATTACH != 0 {
let base_offset = buf.positions[i].base as usize;
if base_offset != 0 {
let (x, y) = {
let base = &buf.positions[i + base_offset];
(base.x, base.y)
};
let pos = &mut buf.positions[i];
pos.x += x;
pos.y += y;
}
}
}
}
}
if buf.has_marks {
fn round_f32(f: f32) -> f32 {
f
}
for (i, g) in buf.glyphs.iter().enumerate() {
if g.flags & buffer::MARK_ATTACH != 0 {
let base_offset = buf.positions[i].base as usize;
if base_offset != 0 {
let (x, y) = {
let base = &buf.positions[i - base_offset];
(base.x - round_f32(base.advance), base.y)
};
let pos = &mut buf.positions[i];
pos.x += x;
pos.y += y;
}
}
}
}
let upem = glyph_metrics.units_per_em();
if self.size != 0. && upem != 0 {
let s = self.size / upem as f32;
for p in buf.positions.iter_mut() {
p.x *= s;
p.y *= s;
p.advance *= s;
}
}
}
}

253
vendor/swash/src/shape/partition.rs vendored Normal file
View File

@@ -0,0 +1,253 @@
/*!
Shaper that partitions by font using trait based per-cluster selection.
*/
use super::{Direction, ShapeContext, Shaper};
use crate::text::cluster::{CharCluster, Parser, Token};
use crate::text::{Codepoint as _, Language, Script};
use crate::{FontRef, Setting, Synthesis};
use core::iter::Copied;
use core::slice;
/// Trait for types that can select appropriate fonts for character clusters.
pub trait Selector {
/// Type of font returned by the provider.
type SelectedFont: SelectedFont;
/// Selects a font for the specified character cluster.
fn select_font(&mut self, cluster: &mut CharCluster) -> Option<Self::SelectedFont>;
}
/// Trait for a font provided by a font selector.
pub trait SelectedFont: PartialEq {
/// Returns a reference to the underlying font.
fn font(&self) -> FontRef;
fn id_override(&self) -> Option<[u64; 2]> {
None
}
/// Returns an optional synthesis state for the font.
fn synthesis(&self) -> Option<Synthesis> {
None
}
}
/// Trait for types that specify shaping options.
pub trait ShapeOptions {
/// Iterator over the feature settings for a fragment.
type Features: Iterator<Item = Setting<u16>>;
/// Iterator over the variation settings for a fragment.
type Variations: Iterator<Item = Setting<f32>>;
/// Returns the script of the text in the fragment.
fn script(&self) -> Script {
Script::Latin
}
/// Returns the language of the text in the fragment.
fn language(&self) -> Option<Language> {
None
}
/// Returns the direction for the fragment.
fn direction(&self) -> Direction {
Direction::LeftToRight
}
/// Returns the font size for the fragment.
fn size(&self) -> f32 {
0.
}
/// Returns an iterator over the feature settings for the fragment.
fn features(&self) -> Self::Features;
/// Returns an iterator over the variation settings for the fragment.
fn variations(&self) -> Self::Variations;
/// Returns true if the shaper should insert dotted circles for broken
/// clusters in the fragment.
fn insert_dotted_circles(&self) -> bool {
false
}
}
/// Simple implementation of the shape options trait.
#[derive(Copy, Clone)]
pub struct SimpleShapeOptions<'a> {
/// Script for the fragment.
pub script: Script,
/// Language for the fragment.
pub language: Option<Language>,
/// Text direction of the fragment.
pub direction: Direction,
/// Font size for the fragment.
pub size: f32,
/// Feature settings for the fragment.
pub features: &'a [Setting<u16>],
/// Variation settings for the fragment.
pub variations: &'a [Setting<f32>],
/// True if the shaper should insert dotted circles for broken clusters.
pub insert_dotted_circles: bool,
}
impl Default for SimpleShapeOptions<'_> {
fn default() -> Self {
Self {
script: Script::Latin,
language: None,
direction: Direction::LeftToRight,
size: 0.,
features: &[],
variations: &[],
insert_dotted_circles: false,
}
}
}
impl<'a> ShapeOptions for SimpleShapeOptions<'a> {
type Features = Copied<slice::Iter<'a, Setting<u16>>>;
type Variations = Copied<slice::Iter<'a, Setting<f32>>>;
fn script(&self) -> Script {
self.script
}
fn language(&self) -> Option<Language> {
self.language
}
fn direction(&self) -> Direction {
self.direction
}
fn size(&self) -> f32 {
self.size
}
fn features(&self) -> Self::Features {
self.features.iter().copied()
}
fn variations(&self) -> Self::Variations {
self.variations.iter().copied()
}
fn insert_dotted_circles(&self) -> bool {
self.insert_dotted_circles
}
}
/// Shapes a run of text (provided as a [`Token`] iterator) using the specified [`Selector`] to assign
/// per-cluster fonts. Invokes the specified closure `f` for each contiguous fragment with the chosen font
/// and a prepared [`Shaper`] that contains the cluster content of the fragment.
pub fn shape<S, T>(
context: &mut ShapeContext,
selector: &mut S,
options: &impl ShapeOptions,
tokens: T,
mut f: impl FnMut(&S::SelectedFont, Shaper),
) where
S: Selector,
T: IntoIterator<Item = Token>,
T::IntoIter: Clone,
{
let mut cluster = CharCluster::new();
if options.direction() == Direction::RightToLeft {
let mut parser = Parser::new(
options.script(),
tokens.into_iter().map(|mut t| {
t.ch = t.ch.mirror().unwrap_or(t.ch);
t
}),
);
if !parser.next(&mut cluster) {
return;
}
let mut font = selector.select_font(&mut cluster);
while shape_clusters(
context,
selector,
options,
&mut parser,
&mut cluster,
&mut font,
&mut f,
) {}
} else {
let mut parser = Parser::new(options.script(), tokens.into_iter());
if !parser.next(&mut cluster) {
return;
}
let mut font = selector.select_font(&mut cluster);
while shape_clusters(
context,
selector,
options,
&mut parser,
&mut cluster,
&mut font,
&mut f,
) {}
}
}
fn shape_clusters<S, T>(
context: &mut ShapeContext,
selector: &mut S,
options: &impl ShapeOptions,
parser: &mut Parser<T>,
cluster: &mut CharCluster,
current_font: &mut Option<S::SelectedFont>,
f: &mut impl FnMut(&S::SelectedFont, Shaper),
) -> bool
where
S: Selector,
T: Iterator<Item = Token> + Clone,
{
if current_font.is_none() {
return false;
}
let font = current_font.as_ref().unwrap();
let font_ref = font.font();
let id = font
.id_override()
.unwrap_or([font_ref.key.value(), u64::MAX]);
let mut shaper = context
.builder_with_id(font.font(), id)
.script(options.script())
.language(options.language())
.direction(options.direction())
.size(options.size())
.features(options.features())
.variations(
font.synthesis()
.as_ref()
.map(|s| s.variations())
.unwrap_or(&[])
.iter()
.copied(),
)
.variations(options.variations())
.insert_dotted_circles(options.insert_dotted_circles())
.build();
loop {
shaper.add_cluster(cluster);
if !parser.next(cluster) {
f(font, shaper);
return false;
}
if let Some(next_font) = selector.select_font(cluster) {
if next_font != *font {
f(font, shaper);
*current_font = Some(next_font);
return true;
}
} else {
return false;
}
}
}