mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
export-w8: net change of 1 commits
- export-w8: written from its sheet
This commit is contained in:
@@ -8,7 +8,10 @@ use std::sync::Arc;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{Document, Node, NodeKind};
|
||||
use vectorcraft_pdf::{Overprint, PdfError, PdfOptions, PdfPreset, PdfSettings, Standard, THUMBNAIL_SIZE, Thumbnail};
|
||||
use vectorcraft_pdf::{
|
||||
APP, ActiveProfiles, Overprint, PdfError, PdfOptions, PdfPreset, PdfSettings, Standard, THUMBNAIL_SIZE, Thumbnail, default_preset_name, leaves_out_bleed,
|
||||
page_bleed,
|
||||
};
|
||||
|
||||
use super::super::flatten::{FlattenOptions, flatten_document, see_through_images};
|
||||
use super::super::{CommandSpec, Result, Session, always, bad, bool_or, cmd, has_doc, str_param};
|
||||
@@ -17,8 +20,6 @@ use crate::EngineError;
|
||||
|
||||
const C: &str = "document.exportPdf";
|
||||
|
||||
/// The built-in preset every PDF export starts from.
|
||||
pub use vectorcraft_pdf::DEFAULT_PRESET;
|
||||
|
||||
/// The PDF options `document.formats` lists; `document.exportPdf` documents every field.
|
||||
pub(crate) const OPTIONS: &[FormatOption] = &[
|
||||
@@ -151,13 +152,13 @@ pub fn presets(s: &Session) -> Vec<String> {
|
||||
|
||||
/// The built-in preset names, then those of `saved`.
|
||||
fn names(saved: &[PdfPreset]) -> Vec<String> {
|
||||
vectorcraft_pdf::builtin_presets().into_iter().map(|p| p.name).chain(saved.iter().map(|p| p.name.clone())).collect()
|
||||
vectorcraft_pdf::builtin_presets(APP).into_iter().map(|p| p.name).chain(saved.iter().map(|p| p.name.clone())).collect()
|
||||
}
|
||||
|
||||
/// The preset `name` names: a built-in one (any case; `default` is the app default) or one of
|
||||
/// `saved`.
|
||||
pub fn find_preset(name: &str, saved: &[PdfPreset]) -> Option<PdfPreset> {
|
||||
vectorcraft_pdf::builtin_preset(name).or_else(|| saved.iter().find(|p| p.name.eq_ignore_ascii_case(name.trim())).cloned())
|
||||
vectorcraft_pdf::builtin_preset(name, APP).or_else(|| saved.iter().find(|p| p.name.eq_ignore_ascii_case(name.trim())).cloned())
|
||||
}
|
||||
|
||||
fn preset(cmd: &str, name: &str, saved: &[PdfPreset]) -> Result<PdfSettings> {
|
||||
@@ -166,9 +167,10 @@ fn preset(cmd: &str, name: &str, saved: &[PdfPreset]) -> Result<PdfSettings> {
|
||||
.ok_or_else(|| bad(cmd, format!("unknown PDF preset `{name}` (presets: {})", names(saved).join(", "))))
|
||||
}
|
||||
|
||||
/// The settings of the preset `p` names (default: [`DEFAULT_PRESET`]), before `p`'s options.
|
||||
/// The settings of the preset `p` names (default: [`default_preset_name`]), before `p`'s options.
|
||||
pub fn preset_settings(cmd: &str, p: &Value, saved: &[PdfPreset]) -> Result<PdfSettings> {
|
||||
preset(cmd, str_param(p, "preset").unwrap_or(DEFAULT_PRESET), saved)
|
||||
let default = default_preset_name(APP);
|
||||
preset(cmd, str_param(p, "preset").unwrap_or(&default), saved)
|
||||
}
|
||||
|
||||
/// Merge `over` into `base`: objects key by key (recursively), `null` keeps the base (the
|
||||
@@ -194,7 +196,7 @@ pub(crate) fn pdf_error(cmd: &str, e: PdfError) -> EngineError {
|
||||
}
|
||||
|
||||
/// The settings `p` asks for: its preset (built-in or one of `saved`; default
|
||||
/// [`DEFAULT_PRESET`]) with `p`'s options applied over it. Choosing another standard turns
|
||||
/// [`default_preset_name`]) with `p`'s options applied over it. Choosing another standard turns
|
||||
/// Preserve Editing off, the layers off where it has none, and sets the latest compatibility it
|
||||
/// allows, unless `p` asks for those. Checked as a preset is
|
||||
/// ([`PdfSettings::check_values`]): profiles that aren't there pass. Keys that aren't PDF options
|
||||
@@ -219,14 +221,14 @@ pub fn resolve(cmd: &str, p: &Value, saved: &[PdfPreset]) -> Result<PdfSettings>
|
||||
s.compatibility = s.standard.version();
|
||||
}
|
||||
}
|
||||
s.check_values().map_err(|e| pdf_error(cmd, e))?;
|
||||
s.check_values().map_err(|e| pdf_error(cmd, e.into()))?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// [`resolve`], refusing what the writer can't honour ([`PdfSettings::check`]).
|
||||
pub fn settings_with(cmd: &str, p: &Value, saved: &[PdfPreset]) -> Result<PdfSettings> {
|
||||
let s = resolve(cmd, p, saved)?;
|
||||
s.check().map_err(|e| pdf_error(cmd, e))?;
|
||||
s.check(&ActiveProfiles::default()).map_err(|e| pdf_error(cmd, e.into()))?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
@@ -240,7 +242,7 @@ pub fn settings(cmd: &str, p: &Value) -> Result<PdfSettings> {
|
||||
/// built-in presets. Params naming none come back as they are.
|
||||
pub fn expand_preset<'a>(s: &Session, cmd: &str, p: &'a Value) -> Result<Cow<'a, Value>> {
|
||||
let expanded = match str_param(p, "preset") {
|
||||
Some(name) if vectorcraft_pdf::builtin_preset(name).is_none() => {
|
||||
Some(name) if vectorcraft_pdf::builtin_preset(name, APP).is_none() => {
|
||||
let set = settings_with(cmd, p, &s.prefs.pdf_presets)?;
|
||||
let mut q = p.clone();
|
||||
merge(&mut q, &serde_json::to_value(set).map_err(|e| EngineError::Other(e.to_string()))?);
|
||||
@@ -316,7 +318,7 @@ pub(super) fn art_page(cmd: &str, doc: &Document, p: &Value) -> Result<Option<(D
|
||||
}
|
||||
|
||||
/// The warning for a PDF/X file that leaves out the document's bleed
|
||||
/// ([`vectorcraft_pdf::PdfSettings::leaves_out_bleed`]).
|
||||
/// ([`vectorcraft_pdf::leaves_out_bleed`]).
|
||||
pub const BLEED_LEFT_OUT: &str = "the document sets a bleed that this PDF/X file leaves out: turn on Use document bleed settings";
|
||||
|
||||
/// How far, in points, an object's edge may sit inside the trim edge, or short of the bleed edge,
|
||||
@@ -579,7 +581,7 @@ fn encode_carrying_at(
|
||||
// createLayers writes non-printing layers with /PrintState /OFF, so they count only with
|
||||
// includeNonPrinting, which writes them as printing art.
|
||||
let set = &opts.settings;
|
||||
let short = if bleed_check { short_of_bleed(doc, set.bleed_of(doc), opts.artboards.as_deref(), set.include_non_printing) } else { 0 };
|
||||
let short = if bleed_check { short_of_bleed(doc, page_bleed(&set.bleed, doc), opts.artboards.as_deref(), set.include_non_printing) } else { 0 };
|
||||
// The pages' background colors, behind the art (the editing data keeps them as colors).
|
||||
let pages = opts.artboards.clone().unwrap_or_else(|| (0..doc.artboards.len()).collect());
|
||||
let filled = super::export::with_artboard_fills(doc, p, pages).map_err(|e| bad(cmd, e))?;
|
||||
@@ -592,7 +594,7 @@ fn encode_carrying_at(
|
||||
let mut notes = vec![];
|
||||
// A loss, ahead of the flattening note: the PDF/X pages leave out the bleed that Document
|
||||
// Setup gives.
|
||||
if opts.settings.leaves_out_bleed(doc) {
|
||||
if leaves_out_bleed(&opts.settings, doc) {
|
||||
notes.push(Note::loss(BLEED_LEFT_OUT));
|
||||
}
|
||||
// Art that reaches the trim edge but leaves part of the page's bleed empty.
|
||||
@@ -760,7 +762,7 @@ fn pdf_settings(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
note_texts(¬es)
|
||||
}
|
||||
_ => set.warnings(),
|
||||
_ => set.warnings(&ActiveProfiles::default()).into_iter().map(|w| w.message).collect(),
|
||||
};
|
||||
let v = serde_json::to_value(&set).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
Ok(json!({ "settings": v, "presets": presets(s), "changed": diff, "warnings": warnings }))
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::pdf::DEFAULT_PRESET;
|
||||
use vectorcraft_pdf::{APP, default_preset_name};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn session(artboards: usize) -> Session {
|
||||
@@ -310,7 +311,7 @@ fn bad_options_are_refused() {
|
||||
#[test]
|
||||
fn options_apply_over_the_preset() {
|
||||
let mut s = session(1);
|
||||
let a = b64(&export(&mut s, json!({"preset": DEFAULT_PRESET, "compression": {"compressText": false}})));
|
||||
let a = b64(&export(&mut s, json!({"preset": default_preset_name(APP), "compression": {"compressText": false}})));
|
||||
let b = b64(&export(&mut s, json!({"preset": "default"})));
|
||||
assert!(a.len() > b.len(), "content streams left uncompressed");
|
||||
assert!(String::from_utf8_lossy(&a).contains(" re") || String::from_utf8_lossy(&a).contains(" l"));
|
||||
@@ -499,7 +500,7 @@ fn pdf_settings_summarise_changes_and_warnings() {
|
||||
let mut s = Session::new();
|
||||
let v = s.execute("document.pdfSettings", &json!({})).unwrap();
|
||||
assert_eq!(v["settings"]["compatibility"], "1.7");
|
||||
assert_eq!(v["presets"][0], DEFAULT_PRESET);
|
||||
assert_eq!(v["presets"][0], default_preset_name(APP));
|
||||
assert_eq!(v["changed"], json!([]));
|
||||
assert_eq!(v["warnings"], json!([]));
|
||||
let p = json!({"compatibility": "1.5", "compression": {"compressText": false}, "createLayers": true, "security": {"copy": false, "openPassword": ""}});
|
||||
@@ -544,5 +545,5 @@ fn formats_list_the_pdf_options() {
|
||||
assert!(pdf["options"].get(k).is_some(), "{k}");
|
||||
}
|
||||
assert_eq!(pdf["options"]["compatibility"]["default"], "1.7");
|
||||
assert_eq!(pdf["options"]["preset"]["default"], DEFAULT_PRESET);
|
||||
assert_eq!(pdf["options"]["preset"]["default"], default_preset_name(APP));
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_pdf::{PdfPreset, builtin_preset, builtin_presets};
|
||||
use vectorcraft_pdf::{APP, ActiveProfiles, PdfPreset, builtin_preset, builtin_presets};
|
||||
|
||||
use super::fileio::pdf::{find_preset, resolve};
|
||||
use super::{CommandSpec, Result, Session, always, bad, bool_or, cmd, str_param};
|
||||
@@ -75,7 +75,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
impl Session {
|
||||
/// Every PDF preset: the built-in ones, then the saved ones.
|
||||
pub fn pdf_presets(&self) -> Vec<PdfPreset> {
|
||||
let mut v = builtin_presets();
|
||||
let mut v = builtin_presets(APP);
|
||||
v.extend(self.prefs.pdf_presets.iter().cloned());
|
||||
v
|
||||
}
|
||||
@@ -93,16 +93,16 @@ impl Session {
|
||||
/// Whether `name` is taken by a built-in PDF preset or a saved one other than the one at
|
||||
/// `except`.
|
||||
fn pdf_preset_taken(&self, name: &str, except: Option<usize>) -> bool {
|
||||
builtin_preset(name).is_some() || self.pdf_saved_index(name).is_some_and(|i| Some(i) != except)
|
||||
builtin_preset(name, APP).is_some() || self.pdf_saved_index(name).is_some_and(|i| Some(i) != except)
|
||||
}
|
||||
}
|
||||
|
||||
fn row(p: PdfPreset, built_in: bool) -> Value {
|
||||
json!({ "name": p.name, "builtIn": built_in, "supported": p.settings.check().is_ok(), "description": p.description, "settings": p.settings })
|
||||
json!({ "name": p.name, "builtIn": built_in, "supported": p.settings.check(&ActiveProfiles::default()).is_ok(), "description": p.description, "settings": p.settings })
|
||||
}
|
||||
|
||||
fn list(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let rows = builtin_presets().into_iter().map(|p| row(p, true)).chain(s.prefs.pdf_presets.iter().cloned().map(|p| row(p, false)));
|
||||
let rows = builtin_presets(APP).into_iter().map(|p| row(p, true)).chain(s.prefs.pdf_presets.iter().cloned().map(|p| row(p, false)));
|
||||
Ok(json!({ "presets": rows.collect::<Vec<_>>() }))
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(n) => n.to_string(),
|
||||
None => s.new_pdf_preset_name(),
|
||||
};
|
||||
if builtin_preset(&name).is_some() {
|
||||
if builtin_preset(&name, APP).is_some() {
|
||||
return Err(bad(C, format!("`{name}` is a built-in preset and is read-only: save it under another name")));
|
||||
}
|
||||
let at = s.pdf_saved_index(&name);
|
||||
@@ -145,7 +145,7 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "pdf.preset.delete";
|
||||
let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?;
|
||||
if builtin_preset(name).is_some() {
|
||||
if builtin_preset(name, APP).is_some() {
|
||||
return Err(bad(C, format!("`{name}` is a built-in preset and stays")));
|
||||
}
|
||||
let i = s.pdf_saved_index(name).ok_or_else(|| bad(C, format!("no saved PDF preset named `{name}`")))?;
|
||||
|
||||
@@ -67,7 +67,7 @@ fn drop_guides(nodes: &mut Vec<Arc<Node>>) {
|
||||
/// nothing is drawn get none.
|
||||
fn bitmap_document(cmd: &str, doc: &Document, set: &PrintSettings, rich_black: bool) -> Result<Document> {
|
||||
let regions = vectorcraft_pdf::print_regions(doc, set).map_err(|e| pdf_error(cmd, e))?;
|
||||
let bleed = set.bleed.of(doc);
|
||||
let bleed = vectorcraft_pdf::page_bleed(&set.bleed, doc);
|
||||
let mut art = doc.clone();
|
||||
drop_guides(&mut art.layers);
|
||||
let anti_alias = if doc.raster_effects.anti_alias { AntiAlias::Art } else { AntiAlias::None };
|
||||
|
||||
@@ -42,7 +42,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use vectorcraft_pdf::{PdfPreset, builtin_preset};
|
||||
use vectorcraft_pdf::{APP, PdfPreset, builtin_preset};
|
||||
use vectorcraft_plugins::permissions::GrantToken;
|
||||
use vectorcraft_plugins::safe_mode;
|
||||
use vectorcraft_plugins::storage::{self, Meter, Storage, StorageError};
|
||||
@@ -1555,7 +1555,7 @@ fn load_presets(dir: &Path) -> Vec<PdfPreset> {
|
||||
for mut preset in file.presets {
|
||||
let name = preset.name.trim().to_string();
|
||||
let usable = !name.is_empty()
|
||||
&& builtin_preset(&name).is_none()
|
||||
&& builtin_preset(&name, APP).is_none()
|
||||
&& !presets.iter().any(|q| q.name.eq_ignore_ascii_case(&name))
|
||||
&& preset.settings.check_values().is_ok()
|
||||
&& presets.len() < MAX_STORE_FILES;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
use crate::cmd::fileio::pdf::DEFAULT_PRESET;
|
||||
use vectorcraft_pdf::{APP, default_preset_name};
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
@@ -31,14 +31,15 @@ fn built_in_presets_are_listed_and_read_only() {
|
||||
let mut s = session();
|
||||
let v = s.execute("pdf.preset.list", &json!({})).unwrap();
|
||||
let rows = v["presets"].as_array().unwrap();
|
||||
assert_eq!(rows[0]["name"], DEFAULT_PRESET);
|
||||
let default = default_preset_name(APP);
|
||||
assert_eq!(rows[0]["name"], default);
|
||||
assert!(rows.iter().all(|r| r["builtIn"] == true && !r["description"].as_str().unwrap().is_empty()));
|
||||
let row = |n: &str| rows.iter().find(|r| r["name"] == n).unwrap_or_else(|| panic!("{n}"));
|
||||
for n in ["High Quality Print", "Press Quality", "Smallest File Size", "PDF/X-1a:2001", "PDF/X-3:2002", "PDF/X-4:2010"] {
|
||||
assert_eq!(row(n)["builtIn"], true);
|
||||
}
|
||||
assert!(rows.iter().all(|r| r["supported"] == true), "every built-in standard is written");
|
||||
assert_eq!(row(DEFAULT_PRESET)["settings"]["preserveEditing"], true);
|
||||
assert_eq!(row(&default)["settings"]["preserveEditing"], true);
|
||||
// Read-only: neither changed nor deleted, in any case.
|
||||
assert!(is_bad_params(s.execute("pdf.preset.save", &json!({"name": "press quality", "compatibility": "1.4"}))));
|
||||
assert!(is_bad_params(s.execute("pdf.preset.save", &json!({"name": "default"}))));
|
||||
@@ -96,7 +97,7 @@ fn presets_export_and_import_round_trip() {
|
||||
let r = s.execute("pdf.preset.import", &json!({"data": text})).unwrap();
|
||||
assert_eq!(r["imported"], json!(["Proof", "Press Quality 2"]), "a built-in name gets a number");
|
||||
assert_eq!(s.prefs.pdf_presets[0], saved[0], "the same preset comes back");
|
||||
let press = vectorcraft_pdf::builtin_preset("Press Quality").unwrap();
|
||||
let press = vectorcraft_pdf::builtin_preset("Press Quality", APP).unwrap();
|
||||
assert_eq!(s.prefs.pdf_presets[1].settings, press.settings);
|
||||
// replace: saved presets of the same names are overwritten.
|
||||
let r = s.execute("pdf.preset.import", &json!({"data": text, "replace": true})).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user