mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
* 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>
197 lines
7.4 KiB
Rust
197 lines
7.4 KiB
Rust
use super::{Affine, Arc, Deserialize, Node, NodeId, Point, Serialize};
|
||
|
||
/// New art on a locked artboard ([`Document::new_art`]). Setting it changes the document in
|
||
/// place, so a selection change that sets it doesn't replace the document.
|
||
#[derive(Default)]
|
||
pub struct NewArtIds(pub(crate) std::sync::RwLock<Arc<std::collections::HashSet<NodeId>>>);
|
||
|
||
impl NewArtIds {
|
||
/// The objects.
|
||
pub fn get(&self) -> Arc<std::collections::HashSet<NodeId>> {
|
||
self.0.read().map(|g| g.clone()).unwrap_or_default()
|
||
}
|
||
/// Make `ids` the objects.
|
||
pub fn set(&self, ids: Arc<std::collections::HashSet<NodeId>>) {
|
||
if let Ok(mut g) = self.0.write() {
|
||
*g = ids;
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Clone for NewArtIds {
|
||
fn clone(&self) -> Self {
|
||
NewArtIds(std::sync::RwLock::new(self.get()))
|
||
}
|
||
}
|
||
|
||
/// Editing state, not document content: two documents are equal whatever new art they name.
|
||
impl PartialEq for NewArtIds {
|
||
fn eq(&self, _: &Self) -> bool {
|
||
true
|
||
}
|
||
}
|
||
|
||
impl std::fmt::Debug for NewArtIds {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
f.debug_tuple("NewArtIds").field(&self.get()).finish()
|
||
}
|
||
}
|
||
|
||
/// Opacity-mask editing mode: `object`'s mask art lives on the temporary `layer` while editing.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct MaskEdit {
|
||
pub object: NodeId,
|
||
pub layer: NodeId,
|
||
}
|
||
|
||
/// Symbol editing mode: the art of symbol `symbol` lives on the temporary `layer` while editing,
|
||
/// placed by `xf` (from the symbol's coordinates to the document's), and goes back into the symbol
|
||
/// after each edit, so every instance follows it.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct SymbolEdit {
|
||
pub symbol: String,
|
||
pub layer: NodeId,
|
||
/// The instance editing was entered from (a double-click on it), selected again on leaving after an edit;
|
||
/// `None` when it was entered from the Symbols panel.
|
||
pub instance: Option<NodeId>,
|
||
pub xf: Affine,
|
||
/// The symbol's art is a group whose members are on the layer: the group takes them back.
|
||
pub group: bool,
|
||
/// An edit changed the art since editing was entered.
|
||
pub changed: bool,
|
||
/// The isolated object from before editing was entered, isolated again on leaving.
|
||
#[serde(default)]
|
||
pub back_isolation: Option<NodeId>,
|
||
/// The current layer from before editing was entered, current again on leaving.
|
||
#[serde(default)]
|
||
pub back_layer: Option<NodeId>,
|
||
}
|
||
|
||
/// A saved view (View → New View…): zoom, centre and rotation, listed at the bottom of the View menu.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct SavedView {
|
||
pub name: String,
|
||
pub center: Point,
|
||
pub zoom: f64,
|
||
#[serde(default)]
|
||
pub rotation: f64,
|
||
}
|
||
|
||
impl SavedView {
|
||
/// Most views a document keeps (the View menu lists every one).
|
||
pub const MAX: usize = 25;
|
||
/// Longest name, in characters.
|
||
pub const MAX_NAME: usize = 255;
|
||
|
||
/// `name` trimmed and cut to [`Self::MAX_NAME`] characters (`None` when blank).
|
||
pub fn clean_name(name: &str) -> Option<String> {
|
||
let name: String = name.trim().chars().take(Self::MAX_NAME).collect();
|
||
(!name.is_empty()).then_some(name)
|
||
}
|
||
|
||
/// The first "View N" none of `saved` is named.
|
||
pub fn default_name(saved: &[SavedView]) -> String {
|
||
(1..=saved.len() + 1).map(|i| format!("View {i}")).find(|n| !saved.iter().any(|x| &x.name == n)).unwrap_or_default()
|
||
}
|
||
|
||
/// Can the canvas show this view: a finite centre and a finite, positive zoom.
|
||
pub fn is_usable(&self) -> bool {
|
||
self.center.x.is_finite() && self.center.y.is_finite() && self.zoom.is_finite() && self.zoom > 0.0
|
||
}
|
||
}
|
||
|
||
/// A saved selection (Select → Save Selection…): the objects that were selected, listed by name at
|
||
/// the bottom of the Select menu.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct SavedSelection {
|
||
pub name: String,
|
||
pub objects: Vec<NodeId>,
|
||
}
|
||
|
||
impl SavedSelection {
|
||
/// Most saved selections a document keeps (the Select menu lists every one).
|
||
pub const MAX: usize = 25;
|
||
/// Longest name, in characters.
|
||
pub const MAX_NAME: usize = 255;
|
||
|
||
/// `name` trimmed and cut to [`Self::MAX_NAME`] characters (`None` when blank).
|
||
pub fn clean_name(name: &str) -> Option<String> {
|
||
let name: String = name.trim().chars().take(Self::MAX_NAME).collect();
|
||
(!name.is_empty()).then_some(name)
|
||
}
|
||
|
||
/// The first "Selection N" none of `saved` is named.
|
||
pub fn default_name(saved: &[SavedSelection]) -> String {
|
||
(1..=saved.len() + 1).map(|i| format!("Selection {i}")).find(|n| !saved.iter().any(|x| &x.name == n)).unwrap_or_default()
|
||
}
|
||
}
|
||
|
||
/// An SVG filter the document holds (Effect › SVG Filters): a `<filter>` element named by its
|
||
/// `id`, listed in the SVG Filters submenu and in Apply SVG Filter.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct SvgFilter {
|
||
/// The filter element's `id`.
|
||
pub name: String,
|
||
/// The `<filter>` element's XML, as it was written.
|
||
pub xml: String,
|
||
}
|
||
|
||
impl SvgFilter {
|
||
/// Most SVG filters a document holds.
|
||
pub const MAX: usize = 64;
|
||
/// Longest filter element, in bytes.
|
||
pub const MAX_XML: usize = 64 * 1024;
|
||
}
|
||
|
||
/// A symbol definition.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct Symbol {
|
||
pub name: String,
|
||
pub art: Arc<Node>,
|
||
/// Symbol Options › Symbol Type › Dynamic Symbol: each instance may change the appearance of
|
||
/// objects in the art and stay linked (see [`crate::symbol_overrides`]). Static by default.
|
||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||
pub dynamic: bool,
|
||
}
|
||
|
||
/// Encoded image bytes (PNG/JPEG/WebP…) shared by image objects, and the fonts files carried for
|
||
/// type ([`crate::FileFonts`], MIME [`crate::FILE_FONT_MIME`]), which save and travel the same way.
|
||
#[derive(Serialize, Deserialize)]
|
||
pub struct ImageBlob {
|
||
pub mime: String,
|
||
#[serde(skip)]
|
||
pub bytes: Arc<Vec<u8>>,
|
||
/// A linked image's low-resolution preview (PNG, see [`links`]): what a save writes when only
|
||
/// linked images show this blob, and what `bytes` hold while the linked file can't be read.
|
||
#[serde(skip)]
|
||
pub proxy: Option<Arc<Vec<u8>>>,
|
||
/// Runtime ownership of an admitted font face. Image resources (including undo and clipboard
|
||
/// copies) own it; cached text nodes carry only weak references.
|
||
#[serde(skip)]
|
||
pub file_font: std::sync::Mutex<Option<Arc<dyn crate::FileFontFace>>>,
|
||
}
|
||
|
||
impl std::fmt::Debug for ImageBlob {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
f.debug_struct("ImageBlob").field("mime", &self.mime).field("bytes", &self.bytes).field("proxy", &self.proxy).finish()
|
||
}
|
||
}
|
||
|
||
impl Clone for ImageBlob {
|
||
fn clone(&self) -> Self {
|
||
let mut owner = self.file_font.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||
if owner.as_ref().is_some_and(|face| {
|
||
self.mime != crate::FILE_FONT_MIME || (!std::ptr::eq(face.bytes(), self.bytes.as_slice()) && face.bytes() != self.bytes.as_slice())
|
||
}) {
|
||
*owner = None;
|
||
}
|
||
Self { mime: self.mime.clone(), bytes: self.bytes.clone(), proxy: self.proxy.clone(), file_font: std::sync::Mutex::new(owner.clone()) }
|
||
}
|
||
}
|
||
|
||
impl PartialEq for ImageBlob {
|
||
fn eq(&self, other: &Self) -> bool {
|
||
self.mime == other.mime && self.bytes == other.bytes && self.proxy == other.proxy
|
||
}
|
||
}
|