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
@@ -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(_)
|
||||
|
||||
Reference in new issue
Block a user