Implement native letterspaced fonts and virtual glyph placement
This commit is contained in:
1 parent
6675ab639a
commit
f66a5a7e20
13 files changed
+337
-35
No files matched your search
@@ -1096,7 +1096,9 @@ impl Executor {
|
||||
token.kind = TokenKind::Command(CommandName::Font(font));
|
||||
return Ok(TheValue::Tokens(vec![token]));
|
||||
}
|
||||
Meaning::Primitive(Font | Textfont | Scriptfont | Scriptscriptfont) => {
|
||||
Meaning::Primitive(
|
||||
Font | Letterspacefont | Textfont | Scriptfont | Scriptscriptfont,
|
||||
) => {
|
||||
let mut token = next.token.clone();
|
||||
self.unread(next)?;
|
||||
let font = self.font_identifier(files)?;
|
||||
@@ -4042,6 +4044,7 @@ impl Executor {
|
||||
.map_err(|e| self.error(e, self.current))?;
|
||||
}
|
||||
Some(Font) => self.define_font(global, files)?,
|
||||
Some(Letterspacefont) => self.define_letterspaced_font(global, files)?,
|
||||
Some(Textfont | Scriptfont | Scriptscriptfont) => {
|
||||
self.math_font_assignment(primitive.unwrap(), global, files)?
|
||||
}
|
||||
|
||||
@@ -401,6 +401,7 @@ impl Executor {
|
||||
| Readline
|
||||
| Font
|
||||
| Fontdimen
|
||||
| Letterspacefont
|
||||
| Hyphenchar
|
||||
| Lpcode
|
||||
| Rpcode
|
||||
|
||||
@@ -1,5 +1,49 @@
|
||||
use super::*;
|
||||
impl Executor {
|
||||
pub(super) fn define_letterspaced_font(
|
||||
&mut self,
|
||||
global: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
let name = self.command(files)?;
|
||||
self.define_traced(name.clone(), Meaning::Font(FontId::NULL), global, files)?;
|
||||
self.optional_equals(files)?;
|
||||
let base = self.font_identifier(files)?;
|
||||
let amount = self.integer(files)?.clamp(-1000, 1000);
|
||||
let no_ligatures = self.keyword("nolig", files)?;
|
||||
let defaults = self.font_defaults();
|
||||
let from = self.sources[self.current.source].name.clone();
|
||||
let (font, zero_quad) = self
|
||||
.state
|
||||
.fonts
|
||||
.letterspace(
|
||||
files,
|
||||
&from,
|
||||
base,
|
||||
amount,
|
||||
no_ligatures,
|
||||
name.clone(),
|
||||
defaults,
|
||||
)
|
||||
.map_err(|e| self.error(e, self.current))?;
|
||||
if zero_quad {
|
||||
files
|
||||
.observe_diagnostic(
|
||||
WriteDestination::TerminalAndLog,
|
||||
"\npdfTeX warning (\\letterspacefont): font has zero em size (\\fontdimen6)\n",
|
||||
)
|
||||
.map_err(|e| self.error(e, self.current))?;
|
||||
}
|
||||
self.define_traced(name, Meaning::Font(font), global, files)
|
||||
}
|
||||
|
||||
pub(super) fn font_defaults(&mut self) -> FontIntegers {
|
||||
FontIntegers {
|
||||
hyphen: self.read_parameter(Parameter::Defaulthyphenchar).width(),
|
||||
skew: self.read_parameter(Parameter::Defaultskewchar).width(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn font_dimension_target(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
@@ -54,7 +98,7 @@ impl Executor {
|
||||
let t = self.expanded_non_space(files)?.token;
|
||||
let font = match self.meaning(&t) {
|
||||
Meaning::Font(f) => f,
|
||||
Meaning::Primitive(Primitive::Font) => {
|
||||
Meaning::Primitive(Primitive::Font | Primitive::Letterspacefont) => {
|
||||
self.reads.insert(Dependency::TextFont);
|
||||
self.state.text_font()
|
||||
}
|
||||
|
||||
@@ -783,7 +783,7 @@ impl Executor {
|
||||
| Delcode | Count | Countdef | Dimen | Dimendef | Skip | Skipdef | Muskip
|
||||
| Muskipdef | Toks | Toksdef | Advance | Multiply | Divide | Read | Readline
|
||||
| Font | Fontdimen | Hyphenchar | Skewchar | Lpcode | Rpcode | Efcode | Pdffontexpand
|
||||
| Textfont | Scriptfont
|
||||
| Letterspacefont | Textfont | Scriptfont
|
||||
| Scriptscriptfont | Endlinechar | Newlinechar | Escapechar | Globaldefs | Time
|
||||
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
|
||||
| Endinput | Showbox | Show | Showthe | Discretionary | DiscretionaryHyphen
|
||||
|
||||
@@ -635,10 +635,7 @@ impl Executor {
|
||||
crate::tfm::FontSize::Design
|
||||
};
|
||||
self.reads.insert(Dependency::FontResources);
|
||||
let defaults = FontIntegers {
|
||||
hyphen: self.read_parameter(Parameter::Defaulthyphenchar).width(),
|
||||
skew: self.read_parameter(Parameter::Defaultskewchar).width(),
|
||||
};
|
||||
let defaults = self.font_defaults();
|
||||
let from = self.sources[self.current.source].name.clone();
|
||||
let font = self
|
||||
.state
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
//! This layer has no master-font names, outline aliases or bundled fallbacks.
|
||||
use super::{CommandName, Flex, FontIntegers, Glue, SourceProvider};
|
||||
use crate::{
|
||||
resources::{ResourceKind, ResourceVersion},
|
||||
native_font::{LetterspaceProgram, NativeFont},
|
||||
resources::{LoadedResource, ResourceKind, ResourceVersion},
|
||||
tfm::{FontSize, TfmMetrics},
|
||||
};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -260,18 +261,82 @@ impl FontCatalog {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.check_font_capacity()?;
|
||||
let (loaded, metrics) = Self::read_tfm(files, &format!("{area}{name}.tfm"), from, size)?;
|
||||
self.insert_font((name, area), loaded, metrics, selector, defaults, None)
|
||||
}
|
||||
|
||||
pub(super) fn letterspace(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
from: &str,
|
||||
base: FontId,
|
||||
amount: i32,
|
||||
no_ligatures: bool,
|
||||
selector: CommandName,
|
||||
defaults: FontIntegers,
|
||||
) -> Result<(FontId, bool), String> {
|
||||
self.check_font_capacity()?;
|
||||
let font = self.font(base)?;
|
||||
let base_font = self.glyph_font(base)?.clone();
|
||||
let base_quad = i64::from(font.quad());
|
||||
let size = FontSize::At(font.size_sp());
|
||||
let (loaded, mut metrics) =
|
||||
Self::read_tfm(files, &format!("{}.tfm", font.name), from, size)?;
|
||||
let mut quad = metrics.param(5);
|
||||
if quad == 0 && base_quad > 0 {
|
||||
quad = base_quad;
|
||||
}
|
||||
let width_delta = round_ratio(quad, amount, 1000);
|
||||
metrics.set_letter_spacing(width_delta, quad, no_ligatures);
|
||||
let shift = round_ratio(base_quad, amount, 2000);
|
||||
let right = TfmMetrics::encode_vf_move(shift, font.size_sp())?;
|
||||
let sign = if amount > 0 { "+" } else { "" };
|
||||
let name = format!("{}{sign}{amount}ls", font.name);
|
||||
let font = self.insert_font(
|
||||
(&name, ""),
|
||||
loaded,
|
||||
metrics,
|
||||
selector,
|
||||
defaults,
|
||||
Some(LetterspaceProgram { base: base_font, right }),
|
||||
)?;
|
||||
Ok((font, quad == 0))
|
||||
}
|
||||
|
||||
fn check_font_capacity(&self) -> Result<(), String> {
|
||||
if self.fonts.len() >= self.limits.fonts as usize {
|
||||
return Err("native font capacity exceeded".into());
|
||||
}
|
||||
let loaded = files.resource(ResourceKind::Tfm, &format!("{area}{name}.tfm"), from)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_tfm(
|
||||
files: &mut impl SourceProvider,
|
||||
name: &str,
|
||||
from: &str,
|
||||
size: FontSize,
|
||||
) -> Result<(LoadedResource, TfmMetrics), String> {
|
||||
let loaded = files.resource(ResourceKind::Tfm, name, from)?;
|
||||
if loaded.version.bytes != loaded.data.len() as u64
|
||||
|| loaded.version.sha256 != format!("{:x}", Sha256::digest(&loaded.data))
|
||||
{
|
||||
return Err("font resource version does not match its actual TFM bytes".into());
|
||||
}
|
||||
// Do not parse at design size first: zero entries are validated after
|
||||
// scaling, and a file can be legal only at the requested size.
|
||||
// Zero entries are validated after scaling at the requested size.
|
||||
let metrics = TfmMetrics::parse_at(&loaded.data, size)?;
|
||||
Ok((loaded, metrics))
|
||||
}
|
||||
|
||||
fn insert_font(
|
||||
&mut self,
|
||||
(name, area): (&str, &str),
|
||||
loaded: LoadedResource,
|
||||
metrics: TfmMetrics,
|
||||
selector: CommandName,
|
||||
defaults: FontIntegers,
|
||||
letter_spacing: Option<LetterspaceProgram>,
|
||||
) -> Result<FontId, String> {
|
||||
let count = metrics.parameter_count();
|
||||
if count > self.limits.parameter_words - self.parameter_words {
|
||||
return Err("native font parameter capacity exceeded".into());
|
||||
@@ -293,12 +358,13 @@ impl FontCatalog {
|
||||
}
|
||||
};
|
||||
let id = FontId(self.fonts.len() as u32);
|
||||
let glyph_font = Arc::new(crate::native_font::NativeFont {
|
||||
let glyph_font = Arc::new(NativeFont {
|
||||
id,
|
||||
name: name.into(),
|
||||
metrics: Arc::new(metrics),
|
||||
resource: self.resources[resource].clone(),
|
||||
expansion: 0,
|
||||
letter_spacing,
|
||||
});
|
||||
self.fonts.push(FontInstance {
|
||||
name: name.into(),
|
||||
@@ -437,3 +503,8 @@ impl FontCatalog {
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn round_ratio(value: i64, numerator: i32, denominator: i64) -> i64 {
|
||||
let product = value * i64::from(numerator);
|
||||
product.signum() * ((product.abs() + denominator / 2) / denominator)
|
||||
}
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 48;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 17] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, SCHEMA,
|
||||
const SCHEMA: u32 = 49;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 18] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -203,6 +203,7 @@ pub enum Primitive {
|
||||
Setlanguage,
|
||||
Font,
|
||||
Fontname,
|
||||
Letterspacefont,
|
||||
Fontdimen,
|
||||
Fontcharwd,
|
||||
Fontcharht,
|
||||
@@ -797,6 +798,7 @@ impl Default for State {
|
||||
("efcode", Efcode),
|
||||
("pdffontexpand", Pdffontexpand),
|
||||
("fontname", Fontname),
|
||||
("letterspacefont", Letterspacefont),
|
||||
("fontdimen", Fontdimen),
|
||||
("skewchar", Skewchar),
|
||||
("mathchar", Mathchar),
|
||||
|
||||
Reference in new issue
Block a user