Rebind native format TFM resources after project relocation

This commit is contained in:
gloamxun committed 2026-11-28 01:46:37 +08:00
1 parent 1ad9c4db82
commit 65204a7dae
9 files changed
+304 -35

No files matched your search

@@ -5,7 +5,7 @@ mod interword;
mod tail;
pub(super) use tail::TailNodeRef;
mod leaders;
mod legacy_fonts;
mod format_fonts;
mod native_words;
pub use leaders::{LeaderGlue, LeaderMode, LeaderPattern};
@@ -1,64 +1,66 @@
//! Format schema 53 stored device faces in boxes. Rebind them to the original
//! logical font slots before a restored job changes any active expansion links.
//! Visit the font instances retained by packed glyphs in a format.
use super::*;
use crate::{math_engine_tex::MathItem, native_font::NativeFont};
use std::{collections::BTreeMap, sync::Arc};
type Replacements = BTreeMap<(FontId, i32), Arc<NativeFont>>;
use std::sync::Arc;
impl NativeBox {
pub(in crate::execution) fn rebind_legacy_expansion_fonts(&mut self, fonts: &Replacements) {
pub(in crate::execution) fn visit_format_fonts_mut(
&mut self,
visit: &mut impl FnMut(&mut Arc<NativeFont>),
) {
match &mut self.content {
BoxContent::Horizontal(nodes) => rebind_horizontal(nodes, fonts),
BoxContent::Vertical(nodes) => rebind_vertical(nodes, fonts),
BoxContent::Horizontal(nodes) => rebind_horizontal(nodes, visit),
BoxContent::Vertical(nodes) => rebind_vertical(nodes, visit),
}
}
}
fn rebind_horizontal(nodes: &mut [TextNode], fonts: &Replacements) {
fn rebind_horizontal(nodes: &mut [TextNode], visit: &mut impl FnMut(&mut Arc<NativeFont>)) {
for node in nodes {
match node {
TextNode::Glyph { glyph, .. } | TextNode::Box(glyph) | TextNode::MathChar(glyph) => {
if let Some(b) = &mut glyph.native_box {
b.rebind_legacy_expansion_fonts(fonts);
b.visit_format_fonts_mut(visit);
}
if let Some(geometry) = &mut glyph.math_geometry {
for item in &mut geometry.items {
if let MathItem::NativeGlyph { font, .. } = &mut item.item
&& let Some(logical) = fonts.get(&(font.id, font.expansion))
{
*font = logical.clone();
if let MathItem::NativeGlyph { font, .. } = &mut item.item {
visit(font);
}
}
}
}
TextNode::Discretionary { pre_break, post_break, .. } => {
rebind_horizontal(pre_break, fonts);
rebind_horizontal(post_break, fonts);
TextNode::Discretionary {
pre_break,
post_break,
..
} => {
rebind_horizontal(pre_break, visit);
rebind_horizontal(post_break, visit);
}
TextNode::Adjustment(value) => rebind_vertical(&mut value.nodes, fonts),
TextNode::Insertion(value) => rebind_vertical(&mut value.nodes, fonts),
TextNode::Leaders(leader) => rebind_leader(leader, fonts),
TextNode::Adjustment(value) => rebind_vertical(&mut value.nodes, visit),
TextNode::Insertion(value) => rebind_vertical(&mut value.nodes, visit),
TextNode::Leaders(leader) => rebind_leader(leader, visit),
_ => {}
}
}
}
fn rebind_vertical(nodes: &mut [NativeVerticalNode], fonts: &Replacements) {
fn rebind_vertical(nodes: &mut [NativeVerticalNode], visit: &mut impl FnMut(&mut Arc<NativeFont>)) {
for node in nodes {
match node {
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
b.rebind_legacy_expansion_fonts(fonts);
b.visit_format_fonts_mut(visit);
}
NativeVerticalNode::Insertion(value) => rebind_vertical(&mut value.nodes, fonts),
NativeVerticalNode::Leaders(leader) => rebind_leader(leader, fonts),
NativeVerticalNode::Insertion(value) => rebind_vertical(&mut value.nodes, visit),
NativeVerticalNode::Leaders(leader) => rebind_leader(leader, visit),
_ => {}
}
}
}
fn rebind_leader(leader: &mut LeaderGlue, fonts: &Replacements) {
fn rebind_leader(leader: &mut LeaderGlue, visit: &mut impl FnMut(&mut Arc<NativeFont>)) {
if let LeaderPattern::Box(b) = &mut leader.pattern {
b.rebind_legacy_expansion_fonts(fonts);
b.visit_format_fonts_mut(visit);
}
}
@@ -1025,6 +1025,27 @@ impl FontCatalog {
.collect()
}
pub(super) fn rebind_format_resources(
&mut self,
bindings: &BTreeMap<std::path::PathBuf, std::path::PathBuf>,
) -> bool {
let mut relocated = false;
for resource in &mut self.resources {
if let Some(path) = bindings.get(&resource.version.path) {
resource.version.path.clone_from(path);
relocated = true;
}
}
if relocated {
for instance in &mut self.fonts {
if let Some(font) = &mut instance.glyph_font {
NativeFont::rebind_resource_path(font, bindings);
}
}
}
relocated
}
pub(super) fn reset_format_font_state(&mut self) {
for font in &mut self.fonts {
font.expansion_origin = None;
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 93;
const COMPATIBLE_SCHEMAS: [u32; 59] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, SCHEMA,
const SCHEMA: u32 = 94;
const COMPATIBLE_SCHEMAS: [u32; 60] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -44,8 +44,10 @@ impl FormatImage {
}
match &lookup.lookup_path {
Some(spelling)
if matches!(lookup.kind, ResourceKind::Tex | ResourceKind::PdfImage)
&& relative_tree_path(spelling)
if matches!(
lookup.kind,
ResourceKind::Tex | ResourceKind::PdfImage | ResourceKind::Tfm
) && relative_tree_path(spelling)
&& !spelling.as_os_str().is_empty() =>
{
inputs
@@ -167,6 +169,7 @@ impl FormatImage {
.clone_from(path);
}
}
self.state.rebind_format_font_resources(bindings);
self.input_checks.take();
}
}
+30 -3
View File
@@ -1716,12 +1716,39 @@ impl State {
})
}
fn visit_format_fonts_mut(
&mut self,
mut visit: impl FnMut(&mut std::sync::Arc<crate::native_font::NativeFont>),
) {
for b in self.boxes.values_mut() {
b.visit_format_fonts_mut(&mut visit);
}
for form in self.pdf_forms.values_mut() {
if let crate::pdf_device::forms::FormContents::Pending(body) = &mut form.contents {
std::sync::Arc::make_mut(body).box_node.visit_format_fonts_mut(&mut visit);
}
}
}
pub(super) fn rebind_format_font_resources(
&mut self,
bindings: &BTreeMap<std::path::PathBuf, std::path::PathBuf>,
) {
if self.fonts.rebind_format_resources(bindings) {
self.visit_format_fonts_mut(|font| {
crate::native_font::NativeFont::rebind_resource_path(font, bindings);
});
}
}
pub(super) fn restore_legacy_format_fonts(&mut self) -> Result<(), String> {
let replacements = self.fonts.restore_legacy_synthesized_fonts()?;
if !replacements.is_empty() {
for b in self.boxes.values_mut() {
b.rebind_legacy_expansion_fonts(&replacements);
}
self.visit_format_fonts_mut(|font| {
if let Some(logical) = replacements.get(&(font.id, font.expansion)) {
*font = logical.clone();
}
});
}
self.fonts.reset_format_font_state();
Ok(())