360 lines
14 KiB
Rust
360 lines
14 KiB
Rust
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 {
|
|
self.reads.insert(Dependency::Parameter(Parameter::Defaulthyphenchar));
|
|
self.reads.insert(Dependency::Parameter(Parameter::Defaultskewchar));
|
|
self.state.font_defaults()
|
|
}
|
|
|
|
pub(super) fn font_dimension_target(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(FontId, i32), Error> {
|
|
let n = self.integer(files)?;
|
|
let font = self.font_identifier(files)?;
|
|
self.state
|
|
.fonts
|
|
.fontdimen(font, n)
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
Ok((font, n))
|
|
}
|
|
pub(super) fn font_dimension(
|
|
&mut self,
|
|
p: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<i32, Error> {
|
|
if p == Primitive::Fontdimen {
|
|
let (font, n) = self.font_dimension_target(files)?;
|
|
return self
|
|
.state
|
|
.fonts
|
|
.fontdimen(font, n)
|
|
.map_err(|e| self.error(e, self.current));
|
|
}
|
|
if p == Primitive::XeTeXglyphbounds {
|
|
return self.xetex_glyph_bounds(files);
|
|
}
|
|
let font = self.font_identifier(files)?;
|
|
let slot = self.integer(files)?;
|
|
let instance = self.state.fonts.font(font).map_err(|e| self.error(e, self.current))?;
|
|
if let Some(face) = instance.opentype() {
|
|
let character = u32::try_from(slot).ok().and_then(char::from_u32)
|
|
.ok_or_else(|| self.error("invalid Unicode font character", self.current))?;
|
|
let metrics = face.glyph_metrics(character, instance.parameters())
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
return Ok(match p {
|
|
Primitive::Fontcharwd => metrics[0],
|
|
Primitive::Fontcharht => metrics[1],
|
|
Primitive::Fontchardp => metrics[2],
|
|
Primitive::Fontcharic => metrics[3],
|
|
_ => unreachable!(),
|
|
} as i32);
|
|
}
|
|
let slot = u8::try_from(slot)
|
|
.map_err(|_| self.error("font character index must be in 0..255", self.current))?;
|
|
let Some(m) = instance.metrics() else {
|
|
return Ok(0);
|
|
};
|
|
let c = char::from(slot);
|
|
Ok(match p {
|
|
Primitive::Fontcharwd => m.width(c),
|
|
Primitive::Fontcharht => m.height(c),
|
|
Primitive::Fontchardp => m.depth(c),
|
|
Primitive::Fontcharic => m.italic(c),
|
|
_ => unreachable!(),
|
|
} as i32)
|
|
}
|
|
fn xetex_glyph_bounds(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
|
|
self.reads.insert(Dependency::TextFont);
|
|
self.reads.insert(Dependency::FontResources);
|
|
let font = self.state.fonts.font(self.state.text_font())
|
|
.map_err(|error| self.error(error, self.current))?
|
|
.opentype()
|
|
.cloned()
|
|
.ok_or_else(|| self.error(r"\XeTeXglyphbounds requires a native font", self.current))?;
|
|
let edge = self.integer(files)?;
|
|
if !(1..=4).contains(&edge) {
|
|
return self.fail(format!(r"\XeTeXglyphbounds requires an edge index from 1 to 4 (got {edge})"));
|
|
}
|
|
// XeTeX passes the operand to its 16-bit native glyph interface.
|
|
let glyph = self.integer(files)? as u16;
|
|
let edges = font.glyph_bounds(glyph)
|
|
.map_err(|error| self.error(error, self.current))?;
|
|
i32::try_from(edges[edge as usize - 1])
|
|
.map_err(|_| self.error("XeTeX glyph bound exceeds the dimension range", self.current))
|
|
}
|
|
|
|
pub(super) fn xetex_glyph_name(
|
|
&mut self, files: &mut impl SourceProvider,
|
|
) -> Result<String, Error> {
|
|
let id = self.font_identifier(files)?;
|
|
let font = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?
|
|
.opentype()
|
|
.cloned()
|
|
.ok_or_else(|| self.error(r"\XeTeXglyphname requires a native font", self.current))?;
|
|
let glyph = self.integer(files)? as u16;
|
|
let face = font.face().map_err(|error| self.error(error, self.current))?;
|
|
let name = face.glyph_name(rustybuzz::ttf_parser::GlyphId(glyph));
|
|
Ok(name.unwrap_or_default().to_owned())
|
|
}
|
|
|
|
pub(super) fn protrusion_target(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(FontId, i32), Error> {
|
|
self.reads.insert(Dependency::FontResources);
|
|
if self.state.input_profile() != InputProfile::XeTeX {
|
|
let (font, slot) = self.font_character_target(files)?;
|
|
return Ok((font, i32::from(slot)));
|
|
}
|
|
|
|
let id = self.font_identifier(files)?;
|
|
let opentype = self.state.fonts.font(id)
|
|
.map_err(|error| self.error(error, self.current))?.opentype().cloned();
|
|
let slot = if let Some(font) = opentype {
|
|
if self.keyword("/", files)? {
|
|
let name = self.filename(files)?;
|
|
font.face().map_err(|error| self.error(error, self.current))?
|
|
.glyph_index_by_name(&name).map_or(0, |glyph| i32::from(glyph.0))
|
|
} else if self.keyword("u", files)? {
|
|
let slot = self.character_number("XeTeX glyph character", 65535, files)?;
|
|
let face = font.face().map_err(|error| self.error(error, self.current))?;
|
|
char::from_u32(slot).and_then(|c| face.glyph_index(c))
|
|
.map_or(0, |glyph| i32::from(glyph.0))
|
|
} else {
|
|
self.integer(files)?
|
|
}
|
|
} else {
|
|
self.character_number("XeTeX font character", 65535, files)? as i32
|
|
};
|
|
Ok((id, slot))
|
|
}
|
|
|
|
pub(super) fn xetex_font_query(
|
|
&mut self,
|
|
command: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<i32, Error> {
|
|
use Primitive::*;
|
|
use rustybuzz::ttf_parser::Tag;
|
|
|
|
if matches!(command, XeTeXcharglyph | XeTeXglyphindex) {
|
|
let id = self.state.text_font();
|
|
self.reads.insert(Dependency::TextFont);
|
|
self.reads.insert(Dependency::FontResources);
|
|
let font = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?
|
|
.opentype().cloned().ok_or_else(|| self.error(
|
|
"XeTeX glyph query requires a native OpenType font", self.current,
|
|
))?;
|
|
let face = font.face().map_err(|error| self.error(error, self.current))?;
|
|
let glyph = if command == XeTeXcharglyph {
|
|
let slot = self.integer(files)?;
|
|
u32::try_from(slot).ok().and_then(char::from_u32)
|
|
.and_then(|character| face.glyph_index(character))
|
|
} else {
|
|
let name = self.filename(files)?;
|
|
face.glyph_index_by_name(&name)
|
|
};
|
|
return Ok(glyph.map_or(0, |glyph| i32::from(glyph.0)));
|
|
}
|
|
|
|
let id = self.font_identifier(files)?;
|
|
let instance = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?;
|
|
let font = instance.opentype();
|
|
if matches!(command, XeTeXfirstfontchar | XeTeXlastfontchar) {
|
|
let range = match font {
|
|
Some(font) => font.character_range().map_err(|error| self.error(error, self.current))?,
|
|
None => instance.metrics().map_or([1, 0], crate::tfm::TfmMetrics::character_range),
|
|
};
|
|
return Ok(range[usize::from(command == XeTeXlastfontchar)] as i32);
|
|
}
|
|
if command == XeTeXfonttype {
|
|
return Ok(if font.is_some() { 2 } else { 0 });
|
|
}
|
|
let Some(font) = font.cloned() else {
|
|
return match command {
|
|
XeTeXcountglyphs | XeTeXOTcountscripts => Ok(0),
|
|
_ => self.fail("XeTeX OpenType layout query requires a native OpenType font"),
|
|
};
|
|
};
|
|
let face = font.face().map_err(|error| self.error(error, self.current))?;
|
|
let tags = crate::opentype::LayoutTags::new(face.tables());
|
|
if command == XeTeXcountglyphs {
|
|
return Ok(i32::from(face.number_of_glyphs()));
|
|
}
|
|
if command == XeTeXOTcountscripts {
|
|
return Ok(tags.script_count());
|
|
}
|
|
|
|
let first = self.integer(files)?;
|
|
if command == XeTeXOTscripttag {
|
|
return Ok(tags.script_tag(first));
|
|
}
|
|
let script = Tag(first as u32);
|
|
if command == XeTeXOTcountlanguages {
|
|
return Ok(tags.language_count(script));
|
|
}
|
|
let second = self.integer(files)?;
|
|
if command == XeTeXOTlanguagetag {
|
|
return Ok(tags.language_tag(script, second));
|
|
}
|
|
let mut features = tags.features(script, Tag(second as u32));
|
|
if command == XeTeXOTcountfeatures {
|
|
return Ok(features.count() as i32);
|
|
}
|
|
let index = self.integer(files)?;
|
|
Ok(usize::try_from(index).ok()
|
|
.and_then(|index| features.nth(index)).map_or(0, |tag| tag.0 as i32))
|
|
}
|
|
|
|
pub(super) fn font_identifier(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<FontId, Error> {
|
|
let t = self.expanded_non_space(files)?.token;
|
|
let font = match self.meaning(&t) {
|
|
Meaning::Font(f) => f,
|
|
Meaning::Primitive(Primitive::Font | Primitive::Letterspacefont) => {
|
|
self.reads.insert(Dependency::TextFont);
|
|
self.state.text_font()
|
|
}
|
|
Meaning::Primitive(
|
|
p @ (Primitive::Textfont | Primitive::Scriptfont | Primitive::Scriptscriptfont),
|
|
) => {
|
|
let family = self.scan_math_family(files)?;
|
|
let size = match p {
|
|
Primitive::Textfont => 0,
|
|
Primitive::Scriptfont => 1,
|
|
_ => 2,
|
|
};
|
|
self.reads.insert(Dependency::MathFamily { size, family });
|
|
self.state.math_font(size, family)
|
|
}
|
|
_ => return self.fail("expected native font identifier"),
|
|
};
|
|
self.reads.insert(Dependency::FontResources);
|
|
Ok(font)
|
|
}
|
|
pub(super) fn missing_glyph(
|
|
&mut self,
|
|
font: FontId,
|
|
slot: impl Into<u32>,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let slot = slot.into();
|
|
let tracing = self.read_parameter(Parameter::Tracinglostchars).width();
|
|
let online = self.read_parameter(Parameter::Tracingonline).width();
|
|
let destination = if tracing <= 0 {
|
|
None
|
|
} else if tracing > 1 || online > 0 {
|
|
Some(WriteDestination::TerminalAndLog)
|
|
} else {
|
|
Some(WriteDestination::Log)
|
|
};
|
|
// TeX print_ASCII renders nonprinting bytes in ^^ notation. This is
|
|
// the requested slot, not a Unicode-to-font substitution.
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let printed = if u32::try_from(self.state.newlinechar).ok() == Some(slot) {
|
|
"\n".into()
|
|
} else {
|
|
match slot {
|
|
0..=31 => format!("^^{}", char::from(slot as u8 + 64)),
|
|
127 => "^^?".into(),
|
|
128..=255 => format!("^^{slot:02x}"),
|
|
_ => char::from_u32(slot)
|
|
.map(|c| c.to_string())
|
|
.unwrap_or_else(|| format!("U+{slot:04X}")),
|
|
}
|
|
};
|
|
let newline = u8::try_from(self.state.newlinechar).ok().map(char::from);
|
|
let literal = |s: &str| {
|
|
s.chars()
|
|
.map(|c| if Some(c) == newline { '\n' } else { c })
|
|
.collect::<String>()
|
|
};
|
|
let message = destination.map(|_| {
|
|
// print_ASCII expands a byte's canonical ^^ string with newlinechar
|
|
// temporarily disabled. Literal diagnostic strings do not disable it.
|
|
let head = literal(if tracing > 2 {
|
|
"! Missing character: There is no "
|
|
} else {
|
|
"Missing character: There is no "
|
|
});
|
|
let tail = literal(&if tracing > 2 {
|
|
format!(
|
|
" (\"{slot:X}) in font {}.",
|
|
self.state.fonts.font(font).expect("loaded font").name()
|
|
)
|
|
} else {
|
|
format!(
|
|
" in font {}!",
|
|
self.state.fonts.font(font).expect("loaded font").name()
|
|
)
|
|
});
|
|
format!("{head}{printed}{tail}")
|
|
});
|
|
self.events.push(Event::MissingGlyph(MissingGlyph {
|
|
font,
|
|
slot,
|
|
tracing,
|
|
destination,
|
|
message: message.clone(),
|
|
span,
|
|
}));
|
|
if let (Some(destination), Some(message)) = (destination, message) {
|
|
let output = if message.ends_with('\n') {
|
|
message.clone()
|
|
} else {
|
|
format!("{message}\n")
|
|
};
|
|
files
|
|
.observe_diagnostic(destination, &output)
|
|
.map_err(|e| self.error(e, span))?;
|
|
if tracing > 2 {
|
|
let mut error = self.error(message, span);
|
|
error.message_reported = true;
|
|
return Err(error);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|