use super::{Kind, Mode, Stomach};
use crate::glue::Glue;
use crate::node::{CharNode, DiscNode, GlueNode, GlueSource, Ligature, Node, Scaled};
use crate::tfm::{store_scaled, Tfm};
const NON_CHAR: u16 = 256;
const STOP_FLAG: u8 = 128;
const KERN_FLAG: u8 = 128;
#[derive(Clone, Debug)]
pub struct Font {
pub name: String,
pub id_text: String,
pub size: Scaled,
pub design_size: Scaled,
pub checksum: u32,
pub params: Vec<Scaled>,
pub hyphen_char: i64,
pub skew_char: i64,
chars: Vec<Option<Metrics>>,
wide: rustc_hash::FxHashMap<u32, Metrics>,
tfm: Option<std::sync::Arc<Tfm>>,
bchar: u16,
false_bchar: u16,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Metrics {
pub width: Scaled,
pub height: Scaled,
pub depth: Scaled,
pub italic: Scaled,
}
impl Font {
pub fn null() -> Font {
Font {
name: "nullfont".into(),
id_text: "nullfont".into(),
size: 0,
design_size: 0,
checksum: 0,
params: vec![0; 7],
hyphen_char: '-' as i64,
skew_char: -1,
chars: Vec::new(),
wide: rustc_hash::FxHashMap::default(),
tfm: None,
bchar: NON_CHAR,
false_bchar: NON_CHAR,
}
}
pub fn load(tfm: std::sync::Arc<Tfm>, name: &str, id_text: &str, size: Scaled) -> Font {
let scale = |v: f64| store_scaled(v, size);
let chars = (0..=255u8)
.map(|c| {
tfm.char(c).map(|m| Metrics {
width: scale(m.width),
height: scale(m.height),
depth: scale(m.depth),
italic: scale(m.italic),
})
})
.collect::<Vec<_>>();
let mut params: Vec<Scaled> = tfm
.raw_params
.iter()
.enumerate()
.map(|(i, &v)| match i {
0 => (v * f64::from(1 << 16)).round() as Scaled,
_ => scale(v),
})
.collect();
if params.len() < 7 {
params.resize(7, 0);
}
let bchar = tfm.boundary_char();
let false_bchar = match bchar < NON_CHAR && chars[bchar as usize].is_some() {
true => NON_CHAR,
false => bchar,
};
Font {
name: name.to_string(),
id_text: id_text.to_string(),
size,
design_size: design_scaled(&tfm),
checksum: tfm.checksum,
params,
hyphen_char: '-' as i64,
skew_char: -1,
chars,
wide: rustc_hash::FxHashMap::default(),
tfm: Some(tfm),
bchar,
false_bchar,
}
}
pub fn from_metrics(
name: &str,
id_text: &str,
size: Scaled,
design_size: Scaled,
mut params: Vec<Scaled>,
chars: impl IntoIterator<Item = (u32, Metrics)>,
) -> Font {
if params.len() < 7 {
params.resize(7, 0);
}
let mut font = Font {
name: name.to_string(),
id_text: id_text.to_string(),
size,
design_size,
params,
..Font::null()
};
font.chars = vec![None; 256];
for (code, m) in chars {
match code < 256 {
true => font.chars[code as usize] = Some(m),
false => {
font.wide.insert(code, m);
}
}
}
font
}
pub fn metrics(&self, c: char) -> Option<Metrics> {
let code = u32::from(c);
match code < 256 {
true => self.chars.get(code as usize).copied().flatten(),
false => self.wide.get(&code).copied(),
}
}
pub fn tfm(&self) -> Option<&std::sync::Arc<Tfm>> {
self.tfm.as_ref()
}
pub fn param(&self, n: usize) -> Scaled {
n.checked_sub(1)
.and_then(|i| self.params.get(i).copied())
.unwrap_or(0)
}
fn char_node(&self, f: usize, c: char, m: Metrics) -> CharNode {
CharNode {
font: f,
character: c,
width: m.width,
height: m.height,
depth: m.depth,
}
}
pub(super) fn bchar_code(&self) -> u16 {
self.bchar
}
pub fn space_glue(&self) -> Glue {
Glue {
natural: self.param(2),
stretch: self.param(3),
shrink: self.param(4),
..Glue::default()
}
}
pub(super) fn lig_word(&self, k: usize) -> Option<[u8; 4]> {
self.tfm.as_ref()?.lig_kern_word(k)
}
pub(super) fn lig_start(&self, code: u16) -> Option<usize> {
self.tfm.as_ref()?.lig_kern_start(code)
}
pub(super) fn kern(&self, op: u8, rem: u8) -> Scaled {
match &self.tfm {
Some(t) => store_scaled(t.kern_value(op, rem), self.size),
None => 0,
}
}
}
fn design_scaled(tfm: &Tfm) -> Scaled {
((tfm.design_size * f64::from(1 << 20)).round() as Scaled) / 16
}
#[derive(Clone, Copy, Debug)]
enum LigItem {
Char(char),
Pseudo { c: u16, lig_ptr: Option<char> },
}
impl LigItem {
fn character(self) -> u16 {
match self {
LigItem::Char(c) => code_of(c),
LigItem::Pseudo { c, .. } => c,
}
}
}
fn code_of(c: char) -> u16 {
match u32::from(c) < 256 {
true => u32::from(c) as u16,
false => NON_CHAR + 1,
}
}
#[derive(Clone, Copy)]
enum Label {
Wrapup,
Move,
Move1,
Move2,
Lookahead,
LigLoop,
LigLoop1(usize),
MoveLig,
}
struct Loop {
f: usize,
cur_l: u16,
cur_r: u16,
cur_q: usize,
lig_stack: Vec<LigItem>,
bchar: u16,
ligature_present: bool,
lft_hit: bool,
rt_hit: bool,
ins_disc: bool,
}
impl Stomach {
fn adjust_space_factor(&mut self, c: char) {
let main_s = self.sf_code(c);
let cur = self.cur_mut();
if main_s == 1000 {
cur.space_factor = 1000;
} else if main_s < 1000 {
if main_s > 0 {
cur.space_factor = main_s;
}
} else if cur.space_factor < 1000 {
cur.space_factor = 1000;
} else {
cur.space_factor = main_s;
}
}
fn char_warning(&mut self, f: usize, c: char) {
if self.int_par("tracinglostchars") > 0 {
let mut text = String::from("Missing character: There is no ");
text.push(c);
text.push_str(&format!(" in font {}!", self.font(f).name));
self.diagnostic(&text);
}
}
pub fn chars(&mut self, f: usize, chars: &[char], cancel_left: bool, no_right: bool) {
let mut at = 0;
let mut cancel = cancel_left;
while at < chars.len() {
at = self.main_loop(f, chars, at, cancel, no_right);
cancel = false;
}
}
fn main_loop(
&mut self,
f: usize,
chars: &[char],
start: usize,
cancel_left: bool,
no_right: bool,
) -> usize {
let font = self.font(f).clone();
let false_bchar = font.false_bchar;
let mut j = start;
self.adjust_space_factor(chars[j]);
let mut s = Loop {
f,
cur_l: code_of(chars[j]),
cur_r: NON_CHAR,
cur_q: self.cur().list.len(),
lig_stack: vec![LigItem::Char(chars[j])],
bchar: font.bchar,
ligature_present: false,
lft_hit: false,
rt_hit: false,
ins_disc: false,
};
let main_k = match cancel_left {
true => None,
false => font.lig_start(NON_CHAR),
};
let mut at = match main_k {
None => Label::Move2,
Some(k) => {
s.cur_r = s.cur_l;
s.cur_l = NON_CHAR;
Label::LigLoop1(k)
}
};
let mut budget = 256 * (chars.len() + 4);
loop {
budget -= 1;
if budget == 0 {
return chars.len();
}
at = match at {
Label::Wrapup => {
let right = s.rt_hit;
self.wrapup(&font, &mut s, right);
Label::Move
}
Label::Move => {
let Some(&top) = s.lig_stack.last() else {
return chars.len();
};
s.cur_q = self.cur().list.len();
s.cur_l = top.character();
Label::Move1
}
Label::Move1 => match s.lig_stack.last() {
Some(LigItem::Pseudo { .. }) => Label::MoveLig,
_ => Label::Move2,
},
Label::Move2 => {
let c = chars[j];
let exists = match u32::from(c) < 256 {
true => {
s.cur_l < 256
&& font.metrics(char::from(s.cur_l as u8)).is_some()
&& font.metrics(c).is_some()
}
false => font.metrics(c).is_some(),
};
if !exists {
self.char_warning(f, c);
return j + 1;
}
s.lig_stack.pop();
let m = font.metrics(c).unwrap_or_default();
self.tail_append(Node::Char(font.char_node(f, c, m)));
Label::Lookahead
}
Label::Lookahead => {
if j + 1 < chars.len() {
j += 1;
let c = chars[j];
self.adjust_space_factor(c);
s.lig_stack.push(LigItem::Char(c));
s.cur_r = match code_of(c) == false_bchar {
true => NON_CHAR,
false => code_of(c),
};
} else {
if no_right {
s.bchar = NON_CHAR;
}
s.cur_r = s.bchar;
s.lig_stack.clear();
}
Label::LigLoop
}
Label::LigLoop => match s.cur_r == NON_CHAR {
true => Label::Wrapup,
false => match font.lig_start(s.cur_l) {
None => Label::Wrapup,
Some(k) => Label::LigLoop1(k),
},
},
Label::LigLoop1(k) => self.lig_program(&font, &mut s, k),
Label::MoveLig => {
let main_p = match s.lig_stack.pop() {
Some(LigItem::Pseudo { lig_ptr, .. }) => lig_ptr,
_ => None,
};
if let Some(c) = main_p {
let m = font.metrics(c).unwrap_or_default();
self.tail_append(Node::Char(font.char_node(f, c, m)));
}
s.ligature_present = true;
match s.lig_stack.last() {
None if main_p.is_some() => Label::Lookahead,
None => {
s.cur_r = s.bchar;
Label::LigLoop
}
Some(top) => {
s.cur_r = top.character();
Label::LigLoop
}
}
}
};
}
}
fn lig_program(&mut self, font: &Font, s: &mut Loop, mut k: usize) -> Label {
loop {
let Some([skip, next, op, rem]) = font.lig_word(k) else {
return Label::Wrapup;
};
if u16::from(next) == s.cur_r && skip <= STOP_FLAG {
return self.lig_command(font, s, op, rem);
}
if skip == 0 {
k += 1;
} else {
if skip >= STOP_FLAG {
return Label::Wrapup;
}
k += usize::from(skip) + 1;
}
}
}
fn lig_command(&mut self, font: &Font, s: &mut Loop, op: u8, rem: u8) -> Label {
if op >= KERN_FLAG {
let right = s.rt_hit;
self.wrapup(font, s, right);
self.tail_append(Node::Kern {
width: font.kern(op, rem),
explicit: false,
});
return Label::Move;
}
if s.cur_l == NON_CHAR {
s.lft_hit = true;
} else if s.lig_stack.is_empty() {
s.rt_hit = true;
}
let rem = u16::from(rem);
match op {
1 | 5 => {
s.cur_l = rem;
s.ligature_present = true;
}
2 | 6 => {
s.cur_r = rem;
match s.lig_stack.last_mut() {
None => {
s.lig_stack.push(LigItem::Pseudo {
c: rem,
lig_ptr: None,
});
s.bchar = NON_CHAR;
}
Some(top @ LigItem::Char(_)) => {
let LigItem::Char(was) = *top else {
unreachable!("just matched");
};
*top = LigItem::Pseudo {
c: rem,
lig_ptr: Some(was),
};
}
Some(LigItem::Pseudo { c, .. }) => *c = rem,
}
}
3 => {
s.cur_r = rem;
s.lig_stack.push(LigItem::Pseudo {
c: rem,
lig_ptr: None,
});
}
7 | 11 => {
self.wrapup(font, s, false);
s.cur_q = self.cur().list.len();
s.cur_l = rem;
s.ligature_present = true;
}
_ => {
s.cur_l = rem;
s.ligature_present = true;
return match s.lig_stack.is_empty() {
true => Label::Wrapup,
false => Label::Move1,
};
}
}
if op > 4 && op != 7 {
return Label::Wrapup;
}
if s.cur_l < NON_CHAR {
return Label::LigLoop;
}
match font.lig_start(NON_CHAR) {
Some(k) => Label::LigLoop1(k),
None => Label::Wrapup,
}
}
fn wrapup(&mut self, font: &Font, s: &mut Loop, right: bool) {
if s.cur_l >= NON_CHAR {
return;
}
let outer = self.mode().outer();
let list = &mut self.cur_mut().list;
if list.len() > s.cur_q {
if let Some(Node::Char(c)) = list.last() {
if i64::from(u32::from(c.character)) == font.hyphen_char {
s.ins_disc = true;
}
}
}
if s.ligature_present {
let components: Vec<char> = list
.drain(s.cur_q..)
.filter_map(|n| match n {
Node::Char(c) => Some(c.character),
_ => None,
})
.collect();
let ch = char::from(s.cur_l as u8);
let m = font.metrics(ch).unwrap_or_default();
let mut lig = Ligature {
components,
left_boundary: false,
right_boundary: false,
};
if s.lft_hit {
lig.left_boundary = true;
s.lft_hit = false;
}
if right && s.lig_stack.is_empty() {
lig.right_boundary = true;
s.rt_hit = false;
}
list.push(Node::Ligature(font.char_node(s.f, ch, m), lig));
s.ligature_present = false;
}
if s.ins_disc {
s.ins_disc = false;
if outer {
list.push(Node::Disc(DiscNode::default()));
}
}
}
pub fn app_space(&mut self, f: usize, space_skip: Glue, xspace_skip: Glue, normal: bool) {
let font = self.font(f);
let space_factor = self.cur().space_factor;
let zero = Glue::default();
let node = if normal || space_factor == 1000 {
match space_skip == zero {
true => GlueNode::new(font.space_glue()),
false => GlueNode::param(space_skip, GlueSource::SpaceSkip),
}
} else if space_factor >= 2000 && xspace_skip != zero {
GlueNode::param(xspace_skip, GlueSource::XSpaceSkip)
} else {
let mut g = match space_skip == zero {
true => font.space_glue(),
false => space_skip,
};
if space_factor >= 2000 {
g.natural += font.param(7);
}
g.stretch = crate::dimen::xn_over_d(g.stretch, space_factor, 1000);
g.shrink = crate::dimen::xn_over_d(g.shrink, 1000, space_factor);
GlueNode::new(g)
};
self.tail_append(Node::Glue(node));
}
fn new_character(&mut self, f: usize, c: char) -> Option<CharNode> {
let font = self.font(f);
match font.metrics(c) {
Some(m) => Some(font.char_node(f, c, m)),
None => {
self.char_warning(f, c);
None
}
}
}
pub fn make_accent(
&mut self,
f: usize,
a: char,
g: usize,
c: Option<char>,
tol: crate::pack::Tolerances,
) {
let Some(p) = self.new_character(f, a) else {
return;
};
let x = self.font(f).param(5);
let s = self.font(f).param(1) as f64 / 65536.0;
let a_width = p.width;
let mut p = Node::Char(p);
if let Some(q) = c.and_then(|c| self.new_character(g, c)) {
let t = self.font(g).param(1) as f64 / 65536.0;
let (w, h) = (q.width, q.height);
if h != x {
let mut b = crate::pack::hpack(vec![p], crate::pack::NATURAL, tol, None).node;
b.shift_amount = x - h;
p = Node::Box(b);
}
let delta =
((w - a_width) as f64 / 2.0 + h as f64 * t - x as f64 * s).round() as Scaled;
self.tail_append(Node::AccKern(delta));
self.tail_append(p);
self.tail_append(Node::AccKern(-a_width - delta));
p = Node::Char(q);
}
self.tail_append(p);
self.cur_mut().space_factor = 1000;
}
pub fn italic_correction(&mut self) {
if self.in_math() {
self.tail_append(Node::Kern {
width: 0,
explicit: false,
});
return;
}
let Some(last) = self.cur().list.last() else {
return;
};
let (f, c) = match last {
Node::Char(c) | Node::Ligature(c, _) => (c.font, c.character),
_ => return,
};
let italic = self.font(f).metrics(c).map_or(0, |m| m.italic);
self.tail_append(Node::Kern {
width: italic,
explicit: true,
});
let _ = Kind::H;
let _ = Mode::Horizontal;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cmr10() -> Option<Stomach> {
let path = crate::typeset::find_font("cmr10")?;
let tfm = std::sync::Arc::new(Tfm::open(path).ok()?);
let mut s = Stomach::new();
s.fonts.push(Font::load(tfm, "cmr10", "tenrm", 10 * 65536));
s.begin_box(
super::super::BoxKind::H,
crate::pack::NATURAL,
super::super::BoxContext::Shift(0),
);
Some(s)
}
#[test]
fn abc_gets_cmr10s_kern_between_b_and_c() {
let Some(mut s) = cmr10() else {
return;
};
s.chars(1, &['a', 'b', 'c'], false, false);
let shown = s.show_box_text(&s.cur().list.clone());
assert_eq!(shown, "\n\\tenrm a\n\\tenrm b\n\\kern0.27779\n\\tenrm c\n");
}
#[test]
fn ffi_is_one_ligature_of_three_letters() {
let Some(mut s) = cmr10() else {
return;
};
s.chars(1, &['f', 'f', 'i'], false, false);
let list = &s.cur().list;
assert_eq!(list.len(), 1, "{list:?}");
match &list[0] {
Node::Ligature(c, lig) => {
assert_eq!(c.character, '\u{e}');
assert_eq!(lig.components, vec!['f', 'f', 'i']);
}
other => panic!("expected a ligature, got {other:?}"),
}
}
#[test]
fn a_missing_character_breaks_the_run() {
let Some(mut s) = cmr10() else {
return;
};
s.chars(1, &['f', '\u{263a}', 'i'], false, false);
let chars: Vec<char> = s
.cur()
.list
.iter()
.filter_map(|n| match n {
Node::Char(c) => Some(c.character),
_ => None,
})
.collect();
assert_eq!(chars, vec!['f', 'i']);
}
}
#[derive(Clone, Debug)]
pub struct Table {
pub fonts: Vec<Font>,
}
impl Default for Table {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Size {
At(Scaled),
Scaled(i64),
}
impl Table {
pub fn new() -> Table {
Table {
fonts: vec![Font::null()],
}
}
pub fn get(&self, f: u16) -> &Font {
self.fonts.get(f as usize).unwrap_or(&self.fonts[0])
}
pub fn get_mut(&mut self, f: u16) -> Option<&mut Font> {
self.fonts.get_mut(f as usize)
}
pub fn add(&mut self, font: Font) -> u16 {
PROVIDED.with(|p| {
p.borrow_mut()
.insert((font.name.clone(), font.size), font.clone())
});
self.fonts.push(font);
(self.fonts.len() - 1) as u16
}
pub fn load(&mut self, name: &str, size: Size, id: &str) -> Option<(u16, bool)> {
for (f, font) in self.fonts.iter_mut().enumerate().skip(1) {
if font.name != name {
continue;
}
let same = match size {
Size::At(s) => s == font.size,
Size::Scaled(m) => font.size == crate::dimen::xn_over_d(font.design_size, m, 1000),
};
if same {
font.id_text = id.to_string();
return Some((f as u16, false));
}
}
let path = crate::typeset::find_font(name)?;
let tfm = std::sync::Arc::new(Tfm::open(path).ok()?);
let design = design_scaled(&tfm);
let at = match size {
Size::At(s) => s,
Size::Scaled(m) => crate::dimen::xn_over_d(design, m, 1000),
};
self.fonts.push(Font::load(tfm, name, id, at));
Some(((self.fonts.len() - 1) as u16, true))
}
pub fn describe(&self, f: u16) -> String {
let font = self.get(f);
match font.size != font.design_size {
true => format!(
"{} at {}pt",
font.name,
crate::dimen::print_scaled(font.size)
),
false => font.name.clone(),
}
}
}
thread_local! {
static PROVIDED: std::cell::RefCell<rustc_hash::FxHashMap<(String, Scaled), Font>> =
std::cell::RefCell::new(rustc_hash::FxHashMap::default());
}
pub fn provided(name: &str, size: Scaled, id_text: &str) -> Option<Font> {
PROVIDED.with(|p| {
p.borrow().get(&(name.to_string(), size)).map(|f| Font {
id_text: id_text.to_string(),
..f.clone()
})
})
}