Files
vectorcraft/crates/text/src/kept.rs
T
5af36bae87 Fix #4568: PDF import keeps embedded fonts for text whose family isn't installed (#4569)
* PDF import: keep embedded fonts for text whose family isn't installed

Glyphs drawn in an embedded font whose family isn't available are gathered
during import and built into one small TrueType font per family and style,
kept with the document (font/ttf blob). Text objects record the fonts and a
hash of their text: layout and rendering draw them in the kept font until the
text is edited (undo restores it) or the family is installed. Lines are
spaced against the file's own widths with per-glyph placement (#3756).

* PDF import: satisfy crate layering constraints without format dev-dep

* FileFont: negate us_win_descent in i32 to prevent overflow on extreme negative descenders

* PDF import: a kept font carries the file font's licence flags, and the docs say what that means

Review of #4569: the fsType of the file's font is copied to the font built from its glyphs (a test pins it against Embedding), so exports honour it; the docs state the limits, that the file's font program is not parsed, and that a saved file carries third-party glyphs.

* PDF import: a glyph curve far outside any glyph is left out; docs say what the limits and the font program mean

CodeRabbit on #4569: coordinates beyond 1e6 font units are refused before the curve is subdivided. The docs say the 64-font limit is what draws one type object and that the file's font program is read only for its licence flags and metrics.

* PDF import: two programs of one font keep the stricter licence flags; the docs cite their sources

CodeRabbit on #4569: embedded programs of one family and style share one built font, which kept the first program's fsType; it now takes the stricter of the two, whichever came first. The docs entry cites the Acrobat Help pages and the OpenType fsType specification.

* fmt

* PDF import: embedded fonts are on by default, with an opt-out preference

Owner decision on #4568. Preferences > File Handling > Use fonts embedded in imported PDFs (pdfImportEmbeddedFonts, on by default) and the embeddedFonts param of document.open and file.place: off, a PDF's type in a font the machine lacks is set in the fallback font with the missing-font warning, as before. One test each for on and off, one for the preference reaching the options.

* fmt after merging main

* docs: restore the status lines damaged by merges (main's line plus this PR's note)

* PDF import: set embedded_fonts through a builder method (keeps read_opened under the complexity limit)

* PDF import: move the missing-font notes out of read_opened (complexity limit after the merge)

* #4569: isolate file-font resources and reclaim live faces (UNVERIFIED)

* Preserve carried-font ownership across placement and recovery

* Fix carried-font regression test dependencies and unwind probe

* Use explicit OS2 heights in owned-font reference regression

* Keep native font regression artwork inside document layers

* fix(engine): bind file fonts when reading document profiles

* test(engine): initialize file-font leases in new image fixtures

* test: complete file-font lease initialization in image fixtures

* docs: keep the #4568 entry in the file-format parity status line

* fix(pdf): keep a line with another glyph for the same character as outlines (review)

A font kept with the document has one glyph per character. A glyph of the file's font that
differs from the one already kept for its character (a stylistic alternate that maps to the same
Unicode scalar) marks its line, which is imported as outlines so each glyph draws as the file
does.

* docs: resolve the leftover status-line conflict

* Placed document blob: give the new ImageBlob literal its file_font

* Test: a PDF's two glyphs for one character survive a native save and reopen as the file draws them

* Test: move the native save and reopen half of the two-glyphs regression to the engine (layers: no format dev-dep in pdf)

* Fix embedded-font preference help and English fallback checks

* Format embedded-font preference help with the workspace edition

---------

Co-authored-by: FredApps <FredApps@users.noreply.github.com>
Co-authored-by: skylartaylor <hi@skylar.cc>
2026-11-22 18:18:18 -06:00

320 lines
11 KiB
Rust

//! The layouts of a document that is opening, kept for its first frame: opening lays out every type
//! object for its bounds and baselines, and the first frame needs the same glyphs. A kept layout is
//! found by comparing the whole text object, and only while the fonts and the hyphenation exceptions
//! are the ones it was made with. Each is taken once, the frame that takes one drops the rest, a run of
//! [`KEPT_LAYOUTS_FRAMES`] frames that take none drops them all, and together they hold at most
//! [`KEPT_LAYOUTS_BUDGET`] bytes.
use std::collections::{HashMap, VecDeque};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::mem::{size_of, size_of_val};
use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError};
use vectorcraft_doc::color::Paint;
use vectorcraft_doc::{CharStyle, ParaStyle, TextKind, TextObject, TextRun};
use vectorcraft_geom::{Anchor, PathData};
use crate::{FontDb, PositionedGlyph, TextLayout};
/// The most bytes of layouts kept at once; the oldest are dropped first.
const KEPT_LAYOUTS_BUDGET: usize = 128 << 20;
/// How many frames may end with kept layouts on the shelf: the frames of other documents, or of a view
/// that shows no type, take none.
const KEPT_LAYOUTS_FRAMES: u32 = 8;
/// The most layouts kept under one hash ([`text_hash`]): taking one compares it with those.
const KEPT_LAYOUTS_PER_HASH: usize = 32;
/// What a layout reads besides its text object, in the global font database. Take it before laying
/// out: a layout that loads a font ends with another stamp, and is not kept.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LayoutStamp {
fonts: u64,
system_fallback: bool,
hyphenation: u64,
}
impl LayoutStamp {
pub fn now() -> Self {
let db = FontDb::global();
Self { fonts: db.generation(), system_fallback: db.system_fallback_on(), hyphenation: crate::hyphen::exceptions_generation() }
}
}
struct Kept {
/// Tells this one from the others under its hash, in [`Shelf::order`].
number: u64,
text: TextObject,
layout: TextLayout,
bytes: usize,
}
#[derive(Default)]
struct Shelf {
stamp: Option<LayoutStamp>,
kept: HashMap<u64, Vec<Kept>>,
/// Hash and number of each kept layout, the oldest first. Layouts already taken stay listed
/// until they reach the front.
order: VecDeque<(u64, u64)>,
next: u64,
bytes: usize,
taken: bool,
/// Frames that have ended since the last keep.
frames: u32,
/// Frames being drawn now, on any thread.
drawing: u32,
}
impl Shelf {
fn clear(&mut self) {
self.kept.clear();
self.order.clear();
self.bytes = 0;
self.taken = false;
self.frames = 0;
}
fn remove(&mut self, hash: u64, found: impl Fn(&Kept) -> bool) -> Option<Kept> {
let list = self.kept.get_mut(&hash)?;
let kept = list.swap_remove(list.iter().position(found)?);
if list.is_empty() {
self.kept.remove(&hash);
}
self.bytes -= kept.bytes;
Some(kept)
}
/// Keep `layout` of `text`, laid out at `stamp`, when that is still `now`. Layouts of an older
/// stamp are dropped. A layout whose memory is not counted ([`counts_its_memory`]) and one past
/// [`KEPT_LAYOUTS_PER_HASH`] are not kept.
fn keep(&mut self, text: &TextObject, stamp: LayoutStamp, now: LayoutStamp, layout: TextLayout, budget: usize) {
if stamp != now {
return;
}
if self.stamp != Some(now) {
self.clear();
self.stamp = Some(now);
}
let bytes = layout_bytes(&layout) + text_bytes(text);
if bytes > budget || !counts_its_memory(text, &layout) {
return;
}
let (hash, number) = (text_hash(text), self.next);
if self.kept.get(&hash).is_some_and(|list| list.len() >= KEPT_LAYOUTS_PER_HASH) {
return;
}
while self.bytes + bytes > budget {
let Some((hash, number)) = self.order.pop_front() else { break };
self.remove(hash, |k| k.number == number);
}
self.frames = 0;
self.next += 1;
self.bytes += bytes;
self.order.push_back((hash, number));
self.kept.entry(hash).or_default().push(Kept { number, text: text.clone(), layout, bytes });
}
/// The layout kept for a text object equal to `text`, no longer kept after this.
fn take(&mut self, text: &TextObject, now: LayoutStamp) -> Option<TextLayout> {
if self.kept.is_empty() {
return None;
}
if self.stamp != Some(now) {
self.clear();
return None;
}
let kept = self.remove(text_hash(text), |k| k.text == *text)?;
if self.kept.is_empty() {
self.order.clear();
}
self.taken = true;
Some(kept.layout)
}
fn frame_begin(&mut self) {
self.drawing += 1;
}
/// A frame ended: when no other frame is being drawn and a frame took a layout, or
/// [`KEPT_LAYOUTS_FRAMES`] frames have ended since the last keep, the others are dropped.
fn frame_done(&mut self) {
self.drawing = self.drawing.saturating_sub(1);
self.frames = self.frames.saturating_add(1);
if self.drawing == 0 && (self.taken || self.frames >= KEPT_LAYOUTS_FRAMES) {
self.clear();
}
}
}
fn text_hash(t: &TextObject) -> u64 {
let mut h = DefaultHasher::new();
t.runs.iter().for_each(|r| {
r.text.hash(&mut h);
r.style.font_family.hash(&mut h);
r.style.font_style.hash(&mut h);
r.style.size.to_bits().hash(&mut h);
});
t.xf.as_coeffs().iter().for_each(|c| c.to_bits().hash(&mut h));
h.finish()
}
/// Does [`text_bytes`] and [`layout_bytes`] count everything `t` and `layout` hold? They do not count
/// owned file-font resources, a gradient's stops, or a glyph's bitmap or color art.
fn counts_its_memory(t: &TextObject, layout: &TextLayout) -> bool {
let gradient = |p: &Paint| matches!(p, Paint::Gradient(_));
t.file_fonts.is_none()
&& !t.runs.iter().any(|r| gradient(&r.style.fill) || gradient(&r.style.stroke))
&& !layout.glyphs.iter().chain(&layout.ruby).any(|g| g.image.is_some() || g.colour.is_some())
}
fn paint_bytes(p: &Paint) -> usize {
match p {
Paint::Solid { swatch: Some(name), .. } | Paint::Pattern { pattern: name, .. } => name.len(),
_ => 0,
}
}
fn path_bytes(p: &PathData) -> usize {
size_of_val(p.subpaths.as_slice()) + p.anchor_count() * size_of::<Anchor>()
}
fn style_bytes(s: &CharStyle) -> usize {
s.font_family.len()
+ s.font_style.len()
+ s.font_version.as_ref().map_or(0, String::len)
+ s.style_name.as_ref().map_or(0, String::len)
+ s.language.as_ref().map_or(0, String::len)
+ size_of_val(s.features.as_slice())
+ s.features.iter().map(String::len).sum::<usize>()
+ s.font_axes.keys().map(|k| size_of::<(String, f64)>() + k.len()).sum::<usize>()
+ paint_bytes(&s.fill)
+ paint_bytes(&s.stroke)
+ s.stroke_dash.as_ref().map_or(0, |d| size_of_val(d.pattern.as_slice()))
+ s.glyph_alternate.as_ref().map_or(0, |g| g.font.len() + g.version.len())
}
fn run_bytes(r: &TextRun) -> usize {
size_of::<TextRun>() + r.text.len() + style_bytes(&r.style) + r.inline.as_ref().map_or(0, |i| i.symbol.len())
}
fn para_bytes(p: &ParaStyle) -> usize {
size_of_val(p.tabs.as_slice()) + p.tabs.iter().map(|t| t.leader.len()).sum::<usize>() + p.style_name.as_ref().map_or(0, String::len)
}
fn text_bytes(t: &TextObject) -> usize {
let kind = match &t.kind {
TextKind::Point => 0,
TextKind::Area { frame } => path_bytes(frame),
TextKind::OnPath { path, .. } => path_bytes(path),
};
size_of::<TextObject>()
+ kind
+ t.runs.iter().map(run_bytes).sum::<usize>()
+ t.ruby.iter().map(|r| size_of_val(r) + r.text.len()).sum::<usize>()
+ para_bytes(&t.para)
+ t.paras.iter().map(|p| size_of_val(p) + para_bytes(p)).sum::<usize>()
+ size_of_val(t.cached_baselines.as_slice())
+ t.wrap.iter().map(|w| size_of_val(w) + path_bytes(&w.path)).sum::<usize>()
}
fn glyph_bytes(glyphs: &[PositionedGlyph]) -> usize {
size_of_val(glyphs) + glyphs.iter().map(|g| size_of_val(g.outline.elements()) + size_of_val(g.carets.as_slice())).sum::<usize>()
}
fn layout_bytes(l: &TextLayout) -> usize {
size_of::<TextLayout>()
+ glyph_bytes(&l.glyphs)
+ glyph_bytes(&l.ruby)
+ size_of_val(l.lines.as_slice())
+ size_of_val(l.frames.as_slice())
+ size_of_val(l.inlines.as_slice())
+ l.markers.iter().map(|m| size_of_val(m) + m.text.len()).sum::<usize>()
}
fn shelf() -> MutexGuard<'static, Shelf> {
static SHELF: LazyLock<Mutex<Shelf>> = LazyLock::new(Mutex::default);
SHELF.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Keep `layout` of `t`, made from the global font database after `stamp` was taken, for the first
/// frame ([`take_kept_layout`]). `t` is the object as the document holds it.
pub fn keep_layout(t: &TextObject, stamp: LayoutStamp, layout: TextLayout) {
let now = LayoutStamp::now();
shelf().keep(t, stamp, now, layout, KEPT_LAYOUTS_BUDGET);
}
/// The layout [`keep_layout`] kept for a text object equal to `t`, when a frame is being drawn
/// ([`KeptLayoutsFrame`]) and the fonts and the hyphenation exceptions have not changed since. Taking
/// it removes it. A thumbnail, which draws outside a frame, leaves the layouts for the first frame.
pub fn take_kept_layout(t: &TextObject) -> Option<TextLayout> {
let now = LayoutStamp::now();
let mut shelf = shelf();
if shelf.drawing == 0 {
return None;
}
shelf.take(t, now)
}
/// A frame being drawn. When the last frame being drawn ends, the kept layouts are dropped if a frame
/// took one, or after a run of frames that took none.
pub struct KeptLayoutsFrame(());
impl KeptLayoutsFrame {
#[must_use]
pub fn begin() -> Self {
shelf().frame_begin();
Self(())
}
}
impl Drop for KeptLayoutsFrame {
fn drop(&mut self) {
shelf().frame_done();
}
}
/// Drop every kept layout (tests in other crates, to lay out again what a frame would have taken).
#[cfg(any(test, feature = "test-fonts"))]
pub(crate) fn drop_kept_layouts() {
shelf().clear();
}
#[cfg(test)]
pub(crate) mod test_shelf {
use super::*;
/// A shelf of its own, so that tests do not share the process's or its font database's stamp.
#[derive(Default)]
pub(crate) struct TestShelf(Shelf);
impl TestShelf {
pub(crate) fn keep(&mut self, t: &TextObject, stamp: LayoutStamp, now: LayoutStamp, layout: TextLayout, budget: usize) {
self.0.keep(t, stamp, now, layout, budget);
}
pub(crate) fn take(&mut self, t: &TextObject, now: LayoutStamp) -> Option<TextLayout> {
self.0.take(t, now)
}
pub(crate) fn frame_begin(&mut self) {
self.0.frame_begin();
}
pub(crate) fn frame_done(&mut self) {
self.0.frame_done();
}
pub(crate) fn bytes(&self) -> usize {
self.0.bytes
}
}
pub(crate) const FRAMES: u32 = KEPT_LAYOUTS_FRAMES;
pub(crate) const PER_HASH: usize = KEPT_LAYOUTS_PER_HASH;
pub(crate) fn stamp(fonts: u64, system_fallback: bool, hyphenation: u64) -> LayoutStamp {
LayoutStamp { fonts, system_fallback, hyphenation }
}
pub(crate) fn bytes_of(t: &TextObject, layout: &TextLayout) -> usize {
layout_bytes(layout) + text_bytes(t)
}
}