Shape and render explicit OpenType fonts in native Unicode jobs
This commit is contained in:
1 parent
3caf1eced2
commit
70822cbd94
33 files changed
+1255
-80
No files matched your search
@@ -332,6 +332,7 @@ impl NativeBox {
|
||||
TextNode::Glyph { glyph, .. }
|
||||
| TextNode::Box(glyph)
|
||||
| TextNode::MathChar(glyph) => HorizontalItem::Box(glyph.clone()),
|
||||
TextNode::OpenType(run) => HorizontalItem::Box(crate::opentype::horizontal_box(run)),
|
||||
TextNode::Rule(r) => {
|
||||
HorizontalItem::Box(r.horizontal_item(self.width, self.height, self.depth))
|
||||
}
|
||||
@@ -605,6 +606,11 @@ pub(super) fn hpack(
|
||||
height = height.max(glyph.height - shift);
|
||||
depth = depth.max(glyph.depth + shift);
|
||||
}
|
||||
TextNode::OpenType(run) => {
|
||||
width += run.width;
|
||||
height = height.max(run.font.ascent);
|
||||
depth = depth.max(run.font.descent);
|
||||
}
|
||||
TextNode::Rule(r) => {
|
||||
let (w, h, d) = r.dimensions();
|
||||
width += w;
|
||||
|
||||
@@ -133,6 +133,13 @@ impl NativeShipout {
|
||||
}
|
||||
let x = self.device.horizontal_origin + p.x;
|
||||
let y = self.device.vertical_origin + self.box_node.height + p.y;
|
||||
if let MathItem::OpenType(run) = p.item {
|
||||
run.paint(x, y, |value| value, |item, source| {
|
||||
page.sources.insert(page.items.len(), source);
|
||||
page.items.push(item);
|
||||
});
|
||||
return;
|
||||
}
|
||||
page.items.push(match p.item {
|
||||
MathItem::NativeGlyph {
|
||||
font,
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum DiagnosticNode {
|
||||
expansion: i32,
|
||||
ligature: Option<String>,
|
||||
},
|
||||
OpenType { font: FontId, text: String, glyphs: Vec<u16> },
|
||||
Box(DiagnosticBox),
|
||||
EmptyBox(i32),
|
||||
Glue {
|
||||
@@ -181,6 +182,10 @@ pub(super) fn text_nodes(nodes: &[TextNode]) -> Vec<DiagnosticNode> {
|
||||
)
|
||||
}),
|
||||
},
|
||||
TextNode::OpenType(run) => DiagnosticNode::OpenType {
|
||||
font: run.font.id, text: run.text.clone(),
|
||||
glyphs: run.glyphs.iter().map(|glyph| glyph.id).collect(),
|
||||
},
|
||||
TextNode::Box(b) if b.native_box.is_some() => {
|
||||
DiagnosticNode::Box(box_view(b.native_box.as_ref().unwrap()))
|
||||
}
|
||||
@@ -474,6 +479,12 @@ impl Printer<'_> {
|
||||
self.literal(")");
|
||||
}
|
||||
}
|
||||
DiagnosticNode::OpenType { font, text, .. } => {
|
||||
let name = self.state.fonts.font(*font)?.identifier_text();
|
||||
self.command(&name);
|
||||
self.literal(" ");
|
||||
self.literal(text);
|
||||
}
|
||||
DiagnosticNode::Box(b) => self.box_(b, prefix)?,
|
||||
DiagnosticNode::EmptyBox(w) => {
|
||||
self.command("hbox");
|
||||
|
||||
@@ -1314,10 +1314,17 @@ impl Executor {
|
||||
Iffontchar => {
|
||||
let font = self.font_identifier(files)?;
|
||||
let slot = self.integer(files)?;
|
||||
let slot = u8::try_from(slot).map_err(|_| {
|
||||
self.error("font character code must be in 0..255", self.current)
|
||||
})?;
|
||||
self.state.font_has_slot(font, slot)
|
||||
let instance = self.state.fonts.font(font).map_err(|e| self.error(e, self.current))?;
|
||||
if let Some(font) = instance.opentype() {
|
||||
let character = u32::try_from(slot).ok().and_then(char::from_u32)
|
||||
.ok_or_else(|| self.error("font character code requires a Unicode scalar", self.current))?;
|
||||
font.face().map_err(|e| self.error(e, self.current))?.glyph_index(character).is_some()
|
||||
} else {
|
||||
let slot = u8::try_from(slot).map_err(|_| {
|
||||
self.error("font character code must be in 0..255", self.current)
|
||||
})?;
|
||||
self.state.font_has_slot(font, slot)
|
||||
}
|
||||
}
|
||||
Ifodd => self.integer(files)? % 2 != 0,
|
||||
Ifvmode | Ifhmode | Ifmmode | Ifinner => {
|
||||
@@ -3705,6 +3712,10 @@ impl Executor {
|
||||
return self
|
||||
.fail("PDF image objects require a job-local PDF device");
|
||||
}
|
||||
self.reads.insert(Dependency::FontResources);
|
||||
if self.state.fonts.fonts().iter().any(|font| font.opentype().is_some()) {
|
||||
return self.fail("cannot dump a format with native OpenType fonts");
|
||||
}
|
||||
self.format_dumped = true;
|
||||
}
|
||||
if self.native_page.is_some() {
|
||||
@@ -3782,11 +3793,16 @@ impl Executor {
|
||||
Setlanguage => self.set_language(files)?,
|
||||
Char => {
|
||||
let n = self.integer(files)?;
|
||||
let c = u8::try_from(n)
|
||||
.map_err(|_| self.error("char must be in 0..255", token.span))?;
|
||||
self.text_character(c as char, CatCode::Other, &token, files)?;
|
||||
let c = if self.state.input_profile() == super::InputProfile::UnicodeScalar {
|
||||
u32::try_from(n).ok().and_then(char::from_u32)
|
||||
.ok_or_else(|| self.error("char requires a Unicode scalar", token.span))?
|
||||
} else {
|
||||
char::from(u8::try_from(n)
|
||||
.map_err(|_| self.error("char must be in 0..255", token.span))?)
|
||||
};
|
||||
self.text_character(c, CatCode::Other, &token, files)?;
|
||||
self.events.push(Event::Character(Token {
|
||||
kind: TokenKind::Character(c as char, CatCode::Other),
|
||||
kind: TokenKind::Character(c, CatCode::Other),
|
||||
..token
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -70,6 +70,19 @@ impl Executor {
|
||||
}
|
||||
let font = self.font_identifier(files)?;
|
||||
let slot = self.integer(files)?;
|
||||
if let Some(face) = self.state.fonts.font(font).map_err(|e| self.error(e, self.current))?.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)
|
||||
.map_err(|e| self.error(e, self.current))?.unwrap_or([0; 4]);
|
||||
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) = self
|
||||
|
||||
@@ -25,6 +25,7 @@ impl Tail {
|
||||
}
|
||||
TextNode::Penalty(n) => Self::Penalty(*n),
|
||||
TextNode::MarginKern { .. } => Self::Other(15),
|
||||
TextNode::OpenType(_) => Self::Other(9),
|
||||
TextNode::Glyph { ligature, .. } => Self::Other(if *ligature { 7 } else { 0 }),
|
||||
TextNode::MathChar(_) => Self::Other(0),
|
||||
TextNode::Box(b) => Self::Other(b.native_box.as_ref().map_or(1, |b| {
|
||||
|
||||
@@ -284,13 +284,18 @@ impl Executor {
|
||||
i64::from(self.read_parameter(Parameter::Pdfvorigin).width()) + v_offset;
|
||||
let mut page_width = i64::from(self.read_parameter(Parameter::Pdfpagewidth).width());
|
||||
let requested_page_height = i64::from(self.read_parameter(Parameter::Pdfpageheight).width());
|
||||
let unicode_pages = self.state.input_profile() == super::super::InputProfile::UnicodeScalar;
|
||||
// The XeTeX PDF device uses the default letter canvas unless a page size was supplied.
|
||||
let default_canvas = |bp: i64| bp * 803 * 65536 / 800;
|
||||
if page_width == 0 {
|
||||
page_width = b.width + 2 * horizontal_origin;
|
||||
page_width = if unicode_pages { default_canvas(612) } else { b.width + 2 * horizontal_origin };
|
||||
}
|
||||
let page_height = if requested_page_height == 0 {
|
||||
b.height + b.depth + 2 * vertical_origin
|
||||
} else {
|
||||
let page_height = if requested_page_height != 0 {
|
||||
requested_page_height
|
||||
} else if unicode_pages {
|
||||
default_canvas(792)
|
||||
} else {
|
||||
b.height + b.depth + 2 * vertical_origin
|
||||
};
|
||||
let device = ShipoutDevice {
|
||||
magnification,
|
||||
|
||||
@@ -350,15 +350,26 @@ impl Executor {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let slot=u8::try_from(c as u32).map_err(|_|self.error("native text font character requires an 8-bit slot; Unicode shaping is not implemented",token.span))?;
|
||||
let font = self.state.text_font();
|
||||
let opentype = if self.state.input_profile() == super::super::InputProfile::UnicodeScalar {
|
||||
self.state.fonts.font(font).map_err(|e| self.error(e, token.span))?.opentype().cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let slot = u8::try_from(c as u32).ok();
|
||||
if slot.is_none() && opentype.is_none() {
|
||||
return Err(self.error("native TFM text font requires an 8-bit character slot", token.span));
|
||||
}
|
||||
if self.paragraph.is_none() {
|
||||
self.begin_paragraph(token.invocation.unwrap_or(token.span), true)?;
|
||||
}
|
||||
self.reads.insert(Dependency::SfCode(slot));
|
||||
self.paragraph
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.adjust_space_factor(self.state.sfcode(slot));
|
||||
let space_factor = if let Some(slot) = slot {
|
||||
self.reads.insert(Dependency::SfCode(slot));
|
||||
self.state.sfcode(slot)
|
||||
} else {
|
||||
1000
|
||||
};
|
||||
self.paragraph.as_mut().unwrap().adjust_space_factor(space_factor);
|
||||
if !self.restricted() {
|
||||
let language = HyphenationLanguage::from_state(&self.state);
|
||||
let b = self.paragraph.as_mut().unwrap();
|
||||
@@ -375,8 +386,12 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
self.reads.insert(Dependency::FontResources);
|
||||
let font = self.state.text_font();
|
||||
self.reads.insert(Dependency::TextFont);
|
||||
if let Some(font) = opentype {
|
||||
return self.paragraph.as_mut().unwrap()
|
||||
.opentype_character(c, TokenOrigin::from(token), &self.state, font)
|
||||
.map_err(|e| self.error(e, token.invocation.unwrap_or(token.span)));
|
||||
}
|
||||
let missing = self
|
||||
.paragraph
|
||||
.as_mut()
|
||||
@@ -649,11 +664,16 @@ impl Executor {
|
||||
self.reads.insert(Dependency::FontResources);
|
||||
let defaults = self.font_defaults();
|
||||
let from = self.sources[self.current.source].name.clone();
|
||||
let font = self
|
||||
.state
|
||||
.fonts
|
||||
.load(files, &file, &from, size, name.clone(), defaults)
|
||||
.map_err(|e| self.error(e, self.current))?;
|
||||
let request = if self.state.input_profile() == super::super::InputProfile::UnicodeScalar {
|
||||
crate::opentype::Request::parse(&file).map_err(|e| self.error(e, self.current))?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let font = if let Some(request) = request {
|
||||
self.state.fonts.load_opentype(files, &file, request, &from, size, name.clone(), defaults)
|
||||
} else {
|
||||
self.state.fonts.load(files, &file, &from, size, name.clone(), defaults)
|
||||
}.map_err(|e| self.error(e, self.current))?;
|
||||
self.define_traced(name, Meaning::Font(font), global, files)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use super::{CommandName, Flex, FontIntegers, Glue, Parameter, SourceProvider, State};
|
||||
use crate::{
|
||||
native_font::{LetterspaceProgram, NativeFont},
|
||||
opentype::{OpenTypeFont, Request},
|
||||
resources::{LoadedResource, ResourceKind, ResourceVersion},
|
||||
tfm::{FontSize, TfmMetrics},
|
||||
};
|
||||
@@ -26,7 +27,7 @@ impl FontId {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FontResource {
|
||||
pub version: ResourceVersion,
|
||||
/// Exact TFM bytes; retained for format/dependency validation, not recreated
|
||||
/// Exact font bytes; retained for format/dependency validation, not recreated
|
||||
/// from a font name or the parsed metrics.
|
||||
pub data: Arc<Vec<u8>>,
|
||||
}
|
||||
@@ -37,6 +38,8 @@ pub struct FontInstance {
|
||||
area: String,
|
||||
resource: Option<usize>,
|
||||
glyph_font: Option<Arc<crate::native_font::NativeFont>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
opentype: Option<Arc<OpenTypeFont>>,
|
||||
parameters: Arc<Vec<i32>>,
|
||||
integers: FontIntegers,
|
||||
selector: CommandName,
|
||||
@@ -53,7 +56,10 @@ impl FontInstance {
|
||||
&self.area
|
||||
}
|
||||
pub fn size_sp(&self) -> i64 {
|
||||
self.metrics().map_or(0, TfmMetrics::size_sp)
|
||||
self.opentype.as_ref().map_or_else(|| self.metrics().map_or(0, TfmMetrics::size_sp), |font| font.size)
|
||||
}
|
||||
pub fn opentype(&self) -> Option<&Arc<OpenTypeFont>> {
|
||||
self.opentype.as_ref()
|
||||
}
|
||||
pub fn metrics(&self) -> Option<&TfmMetrics> {
|
||||
self.glyph_font.as_ref().map(|f| f.metrics())
|
||||
@@ -78,13 +84,13 @@ impl FontInstance {
|
||||
}
|
||||
}
|
||||
pub fn fontname(&self) -> String {
|
||||
match self.metrics() {
|
||||
Some(m) if m.size_sp() != m.design_size_sp() => format!(
|
||||
"{} at {}pt",
|
||||
self.name,
|
||||
super::registers::print_scaled(m.size_sp() as i32)
|
||||
),
|
||||
_ => self.name.clone(),
|
||||
let size = self.size_sp();
|
||||
let design = self.opentype.as_ref().map_or_else(
|
||||
|| self.metrics().map_or(size, TfmMetrics::design_size_sp), |font| font.design_size);
|
||||
if size != design {
|
||||
format!("{} at {}pt", self.name, super::registers::print_scaled(size as i32))
|
||||
} else {
|
||||
self.name.clone()
|
||||
}
|
||||
}
|
||||
/// Fresh glue takes the live parameters. Previously returned glue remains
|
||||
@@ -228,6 +234,7 @@ impl FontCatalog {
|
||||
area: String::new(),
|
||||
resource: None,
|
||||
glyph_font: None,
|
||||
opentype: None,
|
||||
parameters: Arc::new(vec![0; 7]),
|
||||
integers: FontIntegers {
|
||||
hyphen: 45,
|
||||
@@ -307,12 +314,11 @@ impl FontCatalog {
|
||||
for (index, f) in self.fonts.iter_mut().enumerate().skip(1) {
|
||||
// A font attached to an expansion family has an internal
|
||||
// identity. A later explicit definition loads a separate font.
|
||||
if f.name == name && f.area == area && f.expansion_origin.is_none() {
|
||||
let m = f.metrics().unwrap();
|
||||
if m.requested_size(size).is_ok_and(|n| n == m.size_sp()) {
|
||||
f.selector = selector;
|
||||
return Ok(FontId(index as u32));
|
||||
}
|
||||
if f.name == name && f.area == area && f.expansion_origin.is_none()
|
||||
&& f.metrics().is_some_and(|m| m.requested_size(size).is_ok_and(|n| n == m.size_sp()))
|
||||
{
|
||||
f.selector = selector;
|
||||
return Ok(FontId(index as u32));
|
||||
}
|
||||
}
|
||||
self.check_font_capacity()?;
|
||||
@@ -320,6 +326,68 @@ impl FontCatalog {
|
||||
self.insert_font((name, area), loaded, metrics, selector, defaults, None)
|
||||
}
|
||||
|
||||
pub(super) fn load_opentype(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
spec: &str,
|
||||
request: Request,
|
||||
from: &str,
|
||||
size: FontSize,
|
||||
selector: CommandName,
|
||||
defaults: FontIntegers,
|
||||
) -> Result<FontId, String> {
|
||||
size.validate()?;
|
||||
for (index, instance) in self.fonts.iter_mut().enumerate() {
|
||||
if let Some(font) = &instance.opentype
|
||||
&& instance.name == spec
|
||||
&& font.requested_size(size) == font.size
|
||||
{
|
||||
instance.selector = selector;
|
||||
return Ok(FontId(index as u32));
|
||||
}
|
||||
}
|
||||
self.check_font_capacity()?;
|
||||
if self.limits.parameter_words - self.parameter_words < 8 {
|
||||
return Err("native font parameter capacity exceeded".into());
|
||||
}
|
||||
let names = request.resource_names();
|
||||
let loaded = if let [(kind, name)] = &names[..] {
|
||||
files.resource(*kind, name, from)?
|
||||
} else {
|
||||
let mut selected = None;
|
||||
for (kind, name) in &names {
|
||||
if let Some(resource) = files.optional_font_file(*kind, name, from)? {
|
||||
selected = Some(resource);
|
||||
break;
|
||||
}
|
||||
}
|
||||
selected.ok_or_else(|| format!("OpenType font file not found: {}", request.file))?
|
||||
};
|
||||
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 OpenType bytes".into());
|
||||
}
|
||||
let resource = FontResource { version: loaded.version, data: Arc::new(loaded.data) };
|
||||
let id = FontId(self.fonts.len() as u32);
|
||||
let (font, parameters) = OpenTypeFont::new(id, resource, request, size)?;
|
||||
let resource = self.intern_resource(font.resource.clone());
|
||||
self.fonts.push(FontInstance {
|
||||
name: spec.to_owned(),
|
||||
area: String::new(),
|
||||
resource: Some(resource),
|
||||
glyph_font: None,
|
||||
opentype: Some(Arc::new(font)),
|
||||
parameters: Arc::new(parameters.to_vec()),
|
||||
integers: defaults,
|
||||
selector,
|
||||
expansion_origin: None,
|
||||
automatic_expansion: false,
|
||||
});
|
||||
self.parameter_words += parameters.len();
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// The PDF device reads a new design-size font directly, as opposed to
|
||||
/// an explicit font definition which may reuse an existing instance.
|
||||
pub(super) fn load_pdf_space(
|
||||
@@ -411,6 +479,15 @@ impl FontCatalog {
|
||||
Ok((loaded, metrics))
|
||||
}
|
||||
|
||||
fn intern_resource(&mut self, resource: FontResource) -> usize {
|
||||
if let Some(index) = self.resources.iter().position(|current| current.version == resource.version) {
|
||||
return index;
|
||||
}
|
||||
let index = self.resources.len();
|
||||
self.resources.push(resource);
|
||||
index
|
||||
}
|
||||
|
||||
fn insert_font(
|
||||
&mut self,
|
||||
(name, area): (&str, &str),
|
||||
@@ -425,21 +502,10 @@ impl FontCatalog {
|
||||
return Err("native font parameter capacity exceeded".into());
|
||||
}
|
||||
let parameters = (0..count).map(|i| metrics.param(i) as i32).collect();
|
||||
let resource = match self
|
||||
.resources
|
||||
.iter()
|
||||
.position(|r| r.version == loaded.version)
|
||||
{
|
||||
Some(i) => i,
|
||||
None => {
|
||||
let i = self.resources.len();
|
||||
self.resources.push(FontResource {
|
||||
version: loaded.version,
|
||||
data: Arc::new(loaded.data),
|
||||
});
|
||||
i
|
||||
}
|
||||
};
|
||||
let resource = self.intern_resource(FontResource {
|
||||
version: loaded.version,
|
||||
data: Arc::new(loaded.data),
|
||||
});
|
||||
let id = FontId(self.fonts.len() as u32);
|
||||
let glyph_font = Arc::new(NativeFont {
|
||||
id,
|
||||
@@ -456,6 +522,7 @@ impl FontCatalog {
|
||||
area: area.into(),
|
||||
resource: Some(resource),
|
||||
glyph_font: Some(glyph_font),
|
||||
opentype: None,
|
||||
parameters: Arc::new(parameters),
|
||||
integers: defaults,
|
||||
selector,
|
||||
@@ -705,6 +772,7 @@ impl FontCatalog {
|
||||
self.fonts.push(FontInstance {
|
||||
name,
|
||||
glyph_font: Some(glyph_font),
|
||||
opentype: None,
|
||||
expansion_origin: Some((base, amount)),
|
||||
automatic_expansion: true,
|
||||
..font.clone()
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 55;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 24] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, SCHEMA,
|
||||
const SCHEMA: u32 = 56;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 25] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
@@ -132,8 +132,8 @@ impl FormatImage {
|
||||
}
|
||||
if image.schema < 55 {
|
||||
image.state.restore_legacy_font_map_commands();
|
||||
image.schema = SCHEMA;
|
||||
}
|
||||
image.schema = SCHEMA;
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
|
||||
@@ -853,7 +853,12 @@ pub(crate) fn paint_geometry(
|
||||
use super::boxes::bp;
|
||||
use crate::render::DisplayItem;
|
||||
for p in &g.items {
|
||||
if let MathItem::OpenType(run) = &p.item {
|
||||
run.paint(ox + bp(x + p.x), oy + bp(y + p.y), bp, |item, _| out.push(item));
|
||||
continue;
|
||||
}
|
||||
out.push(match &p.item {
|
||||
MathItem::OpenType(_) => unreachable!("OpenType run handled before scalar paint"),
|
||||
MathItem::NativeGlyph {
|
||||
font,
|
||||
slot,
|
||||
|
||||
@@ -250,6 +250,12 @@ pub trait SourceProvider {
|
||||
) -> Result<Option<crate::resources::LoadedResource>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// An exact lookup that can report a missing font file before trying another format.
|
||||
fn optional_font_file(
|
||||
&mut self, kind: crate::resources::ResourceKind, name: &str, from: &str,
|
||||
) -> Result<Option<crate::resources::LoadedResource>, String> {
|
||||
self.resource(kind, name, from).map(Some)
|
||||
}
|
||||
/// Read an actual PDF font map. A host that can distinguish missing
|
||||
/// files from other failures can return None for TeX's warning path.
|
||||
fn optional_font_map(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Native horizontal material and paragraph breaking. No command/string parser.
|
||||
use super::*;
|
||||
use crate::{HorizontalBox, Paragraph, ParagraphFont};
|
||||
use std::collections::BTreeMap;
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
use crate::opentype::{OpenTypeFont, OpenTypeRun};
|
||||
|
||||
mod break_cache;
|
||||
mod expansion;
|
||||
@@ -42,6 +43,7 @@ pub enum TextNode {
|
||||
component_sources: Vec<crate::provenance::PaintSource>,
|
||||
hyphen_char: Option<char>,
|
||||
},
|
||||
OpenType(Arc<OpenTypeRun>),
|
||||
Box(HorizontalBox),
|
||||
Rule(NativeRule),
|
||||
Mark(NativeMark),
|
||||
@@ -118,6 +120,7 @@ pub(super) struct Builder {
|
||||
pub space_factor: i32,
|
||||
pub hyphen_min: (usize, usize),
|
||||
run: Option<FontRun>,
|
||||
opentype: Option<PendingOpenType>,
|
||||
pub cancel_boundary: bool,
|
||||
pub font_steps: usize,
|
||||
pub font_step_limit: usize,
|
||||
@@ -131,14 +134,21 @@ struct FontRun {
|
||||
machine: super::ligature::Machine,
|
||||
hyphen_char: Option<char>,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
struct PendingOpenType {
|
||||
font: Arc<OpenTypeFont>,
|
||||
text: String,
|
||||
utf16_units: usize,
|
||||
sources: Vec<(usize, crate::provenance::PaintSource)>,
|
||||
}
|
||||
impl Builder {
|
||||
pub(super) fn has_unpacked_text(&self) -> bool {
|
||||
self.run
|
||||
.as_ref()
|
||||
self.opentype.is_some() || self.run.as_ref()
|
||||
.is_some_and(|run| run.machine.has_unpacked_components())
|
||||
}
|
||||
pub(super) fn ligature_work_items(&self) -> usize {
|
||||
self.run.as_ref().map_or(0, |r| r.machine.resource_items())
|
||||
+ self.opentype.as_ref().map_or(0, |run| run.sources.len())
|
||||
}
|
||||
pub fn start(&mut self, state: &State, span: Span, indent: bool) {
|
||||
self.span = span;
|
||||
@@ -227,6 +237,13 @@ impl Builder {
|
||||
missing
|
||||
}
|
||||
pub fn finish_text(&mut self, right_boundary: bool, state: &State) -> Result<(), String> {
|
||||
if let Some(run) = self.opentype.take() {
|
||||
let shaped = run.font.shape(run.text, run.sources)?;
|
||||
self.font_steps = self.font_steps.checked_add(shaped.glyphs.len())
|
||||
.filter(|steps| *steps <= self.font_step_limit)
|
||||
.ok_or("native OpenType shaping step limit exceeded")?;
|
||||
self.nodes.push(TextNode::OpenType(Arc::new(shaped)));
|
||||
}
|
||||
if let Some(mut run) = self.run.take() {
|
||||
run.machine
|
||||
.put_carry(std::mem::take(&mut self.ligature_carry));
|
||||
@@ -245,6 +262,25 @@ impl Builder {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn opentype_character(
|
||||
&mut self, c: char, origin: TokenOrigin, state: &State, font: Arc<OpenTypeFont>,
|
||||
) -> Result<(), String> {
|
||||
if self.run.is_some() || self.opentype.as_ref().is_some_and(|r| r.font.id != font.id) {
|
||||
self.finish_text(true, state)?;
|
||||
}
|
||||
self.cancel_boundary = false;
|
||||
let run = self.opentype.get_or_insert_with(|| PendingOpenType {
|
||||
font, text: String::new(), utf16_units: 0, sources: Vec::new(),
|
||||
});
|
||||
// Native words carry a 16-bit character count in XeTeX.
|
||||
run.utf16_units += c.len_utf16();
|
||||
if run.utf16_units > u16::MAX as usize {
|
||||
return Err("native OpenType word capacity exceeded".into());
|
||||
}
|
||||
run.sources.push((run.text.len(), crate::provenance::PaintSource::token(origin)));
|
||||
run.text.push(c);
|
||||
Ok(())
|
||||
}
|
||||
pub fn character(
|
||||
&mut self,
|
||||
c: char,
|
||||
@@ -253,6 +289,9 @@ impl Builder {
|
||||
font: FontId,
|
||||
hyphen_char: Option<char>,
|
||||
) -> Result<Vec<super::ligature::Component>, String> {
|
||||
if self.opentype.is_some() {
|
||||
self.finish_text(true, state)?;
|
||||
}
|
||||
let c = super::ligature::Component {
|
||||
slot: c as u8,
|
||||
origin: origin.span,
|
||||
@@ -396,6 +435,7 @@ fn width(n: &TextNode) -> i64 {
|
||||
TextNode::Glyph { glyph, .. } | TextNode::Box(glyph) | TextNode::MathChar(glyph) => {
|
||||
glyph.width
|
||||
}
|
||||
TextNode::OpenType(run) => run.width,
|
||||
TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => g.width as i64,
|
||||
@@ -1042,6 +1082,7 @@ fn pre_display_line_size(
|
||||
n,
|
||||
TextNode::Glyph { .. }
|
||||
| TextNode::Box(_)
|
||||
| TextNode::OpenType(_)
|
||||
| TextNode::MathChar(_)
|
||||
| TextNode::Indent(_)
|
||||
| TextNode::Rule(_)
|
||||
|
||||
@@ -142,6 +142,7 @@ impl BreakNode {
|
||||
TextNode::Language(_)
|
||||
| TextNode::Glyph { .. }
|
||||
| TextNode::Box(_)
|
||||
| TextNode::OpenType(_)
|
||||
| TextNode::Rule(_)
|
||||
| TextNode::Mark(_)
|
||||
| TextNode::Adjustment(_)
|
||||
|
||||
@@ -187,6 +187,7 @@ impl Tracer {
|
||||
let character = (*slot as char).to_string();
|
||||
self.character(state, *font, if *ligature { original } else { &character }, out)?;
|
||||
}
|
||||
TextNode::OpenType(run) => self.character(state, run.font.id, &run.text, out)?,
|
||||
TextNode::MathChar(b) => {
|
||||
if let Some(g) = &b.math_geometry
|
||||
&& g.items.len() == 1
|
||||
|
||||
Reference in new issue
Block a user