mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 22:34:09 +00:00
VectorCraft's UI moves onto the shared craft UI framework in storytold/craft-libs (`next`; the framework side is storytold/craft-libs#2809), as PhotoCraft's did. The app keeps its engine, document model, file formats, commands and automation; its windows, panels, bars, dialogs and canvas are now built and driven by the framework: shell, docking and panels, hosted dialogs, keyboard, focus, IME and pointer routing, and theme tokens. About 264,000 lines of VectorCraft's own egui UI code are removed and about 154,000 added. craft-libs is pinned at bf62d509f (in `next`), the egui fork at f5025284. Tests that drove the old UI were rewritten against the port where only their premise changed, with their assertions kept. The UI tests still failing at merge, and their follow-ups, are listed in the pull request.
192 lines
9.5 KiB
Rust
192 lines
9.5 KiB
Rust
//! Document → PDF (krilla). Mirrors the tree walk of `vectorcraft-render`.
|
|
|
|
use std::borrow::Cow;
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::sync::Arc;
|
|
|
|
use craft::pdf::write::backend as krilla;
|
|
use craft::pdf::write::{self, Metadata};
|
|
use krilla::color::separation::{Color as SepColor, SeparationColorant, SeparationSpace};
|
|
use krilla::color::{cmyk, luma, rgb};
|
|
use krilla::configure::{Archival, PdfVersion};
|
|
use krilla::geom::{Path, PathBuilder, Size};
|
|
use krilla::image::Image;
|
|
use krilla::num::NormalizedF32;
|
|
use krilla::page::PageSettings;
|
|
use krilla::paint::{Fill, LinearGradient, RadialGradient, SpreadMethod, Stop, Stroke, StrokeDash, SweepGradient};
|
|
use krilla::text::{Glyph, GlyphId, KrillaGlyph};
|
|
use kurbo::{PathEl, Shape, Vec2};
|
|
use vectorcraft_color::{BlendMode, Color, GradientKind, Paint};
|
|
use vectorcraft_doc::{AppearanceItem, Document, LineCap, LineJoin, Node, NodeKind, StrokeAlign, StrokeLayer, SymbolBoxes, TextObject};
|
|
use vectorcraft_effects::stroke::{self, WrittenShape};
|
|
use vectorcraft_geom::{Affine, BezPath, FillRule, Rect};
|
|
use vectorcraft_text::embed::Embedding;
|
|
use vectorcraft_text::{ListMarker, TextLayout};
|
|
|
|
use crate::forms::Mark;
|
|
use crate::lab_spot::{find, rfind};
|
|
use crate::marks::PageBoxes;
|
|
use crate::output::ColorOut;
|
|
use crate::surface::Surface;
|
|
use crate::{Compatibility, ExportReport, PdfError, PdfOptions, Standard};
|
|
|
|
mod entry;
|
|
mod exporter;
|
|
mod images;
|
|
mod links;
|
|
mod nodes;
|
|
mod paint;
|
|
mod text;
|
|
mod writer;
|
|
|
|
pub(crate) use entry::{RgbInCmyk, leaving_out_undecodable};
|
|
pub use entry::{check_page_sizes, export, export_with_report, page_areas};
|
|
#[cfg(test)]
|
|
use exporter::{MAX_COORD, writable, xf};
|
|
#[cfg(test)]
|
|
use images::{png_8_bit, png_16_bit};
|
|
#[cfg(test)]
|
|
pub(crate) use nodes::{KNOCKOUT_BRUSHES, KNOCKOUT_SHAPES, MAX_KNOCKOUT_MASK_OBJECTS, MEASURES};
|
|
pub(crate) use writer::{Sheet, Writer};
|
|
|
|
pub(crate) struct Exporter<'a> {
|
|
doc: &'a Document,
|
|
/// The art boxes of `doc`'s symbols, which the walk measures symbol instances by when it
|
|
/// leaves out objects outside the page ([`SymbolBoxes::quick`]). The box of an instance whose
|
|
/// art holds brushed strokes grows by as far as their brush art can paint
|
|
/// ([`SymbolBoxes::with_brush_art`]).
|
|
symbols: SymbolBoxes<'a>,
|
|
warnings: Vec<String>,
|
|
images: HashMap<String, Option<Image>>,
|
|
/// `Some` while opacity mask art is written: whether an image in it was left out.
|
|
image_left_out: Option<bool>,
|
|
/// While the pieces of a distorted image whose opacity mask is its own pixel grid are written:
|
|
/// its image key and the image written with the mask as its alpha, which each piece draws.
|
|
piece_image: Option<(String, Image)>,
|
|
/// While the pieces of a mesh-warped image are written: the size its pixels are resampled for
|
|
/// ([`Exporter::warp_image_size`]).
|
|
warp_size: Option<images::SharedSize>,
|
|
/// The brush library, parsed when the first brushed stroke is written.
|
|
brushes: Option<Vec<vectorcraft_brush::Brush>>,
|
|
/// Whether the group being written is a knockout group (what its neutral children inherit).
|
|
knockout: bool,
|
|
/// Spot colours written with a Lab alternate ([`crate::lab_spot`]): colorant name, Lab values.
|
|
lab_spots: Vec<(String, vectorcraft_color::cms::Lab)>,
|
|
/// Images may ask viewers to smooth them (not in PDF/A).
|
|
interpolate: bool,
|
|
/// Conic gradients may be written as sweep shadings, whose PostScript calculator functions
|
|
/// PDF/A forbids; otherwise they are drawn as images ([`Self::area_paint`]).
|
|
sweeps: bool,
|
|
/// Images may have 16 bits a sample (PDF 1.5 and later); in earlier files they are written at
|
|
/// 8 bits ([`embed`]).
|
|
sixteen_bit: bool,
|
|
/// How images are resampled and compressed.
|
|
compression: &'a crate::CompressionSettings,
|
|
/// Layers whose Print option is off are written too.
|
|
pub non_printing: bool,
|
|
/// How colours are separated into CMYK (the colour settings' intent unless print sets one).
|
|
pub intent: vectorcraft_color::cms::Intent,
|
|
/// Whether a layer shows transparency (found out on the first page that asks).
|
|
transparent: Option<bool>,
|
|
/// How colours are written (the Output settings; print writes them as they are).
|
|
pub out: Arc<ColorOut>,
|
|
/// Colours are converted to the destination (printer's marks aren't).
|
|
convert: bool,
|
|
/// RGB colours and images of a CMYK document drawn so far.
|
|
rgb_in_cmyk: RgbInCmyk,
|
|
/// Text as glyph outlines; else the characters' fills are real text in embedded fonts.
|
|
outline_text: bool,
|
|
/// The fonts real text is written in, by face id, with their units per em; `None` for faces
|
|
/// that can't be embedded.
|
|
fonts: HashMap<u32, Option<(krilla::text::Font, f64)>>,
|
|
/// Layers and sublayers are drawn as forms marked for their optional content groups
|
|
/// ([`crate::forms`]).
|
|
pub layers: bool,
|
|
/// Each layer's index in [`crate::forms::pdf_layers`], by address.
|
|
layer_index: HashMap<usize, usize>,
|
|
/// The address of the layer whose form is being drawn.
|
|
in_layer: usize,
|
|
/// The paper of the page being drawn, in its drawing space: the box of each layer's form
|
|
/// covers it ([`crate::forms::layer_form`]).
|
|
paper: Rect,
|
|
/// Overprinting fills and strokes are drawn as forms marked to overprint.
|
|
pub overprint: bool,
|
|
/// Overprinting characters' fills and strokes are drawn with Darken, as a separation's plate
|
|
/// draws its overprinting fills and strokes ([`vectorcraft_doc::overprint::darken_overprints`]).
|
|
pub darken_overprints: bool,
|
|
/// An overprinting fill or stroke was drawn so.
|
|
pub overprinted: bool,
|
|
/// Characters with neither fill nor stroke, or whose fill is drawn as art (outlines filled
|
|
/// with an image or pattern), were written as text in forms marked invisible.
|
|
pub invisible: bool,
|
|
/// Inline graphics being drawn inside inline graphics (text in a symbol shown inline in
|
|
/// text…): deeper ones are left out.
|
|
inline_depth: u32,
|
|
/// Tiling patterns being drawn inside tiling patterns ([`Self::tiling`]).
|
|
pattern_depth: u32,
|
|
/// Keys of the images left out because the writer couldn't decode them
|
|
/// ([`leaving_out_undecodable`]).
|
|
pub left_out: HashSet<String>,
|
|
/// Objects the symbol instances written held, together
|
|
/// ([`vectorcraft_doc::MAX_SYMBOL_NODES`]).
|
|
symbol_nodes: u64,
|
|
/// The most [`Self::symbol_nodes`] may reach: [`vectorcraft_doc::MAX_SYMBOL_NODES`], or less
|
|
/// while one of the copies on a sheet is drawn ([`Writer::page`]).
|
|
pub(crate) symbol_limit: u64,
|
|
/// Objects the symbol instances in the copies drawn in knockout masks held, together: counted
|
|
/// apart from [`Self::symbol_nodes`], within [`vectorcraft_doc::MAX_SYMBOL_NODES`] of their own.
|
|
mask_symbol_nodes: u64,
|
|
/// Whether a knockout mask is being drawn: [`Self::symbol_nodes`] then counts
|
|
/// [`Self::mask_symbol_nodes`].
|
|
in_knockout_mask: bool,
|
|
/// Whether a knockout mask left out symbol art because the objects its copies may hold ran out.
|
|
mask_art_left_out: bool,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn writable_bounds_values_the_transform_and_values_through_it() {
|
|
let mut pdf = krilla::Document::new();
|
|
let mut page = pdf.start_page_with(PageSettings::new(Size::from_wh(100.0, 100.0).unwrap()));
|
|
let mut s = Surface::page(page.surface(), 100.0);
|
|
let r = Rect::new(0.0, 0.0, 10.0, 10.0);
|
|
assert!(writable(&s, Affine::IDENTITY, r));
|
|
assert!(!writable(&s, Affine::IDENTITY, Rect::new(-2_147_483_649.0, 0.0, 10.0, 10.0)));
|
|
assert!(!writable(&s, Affine::IDENTITY, Rect::new(f64::NAN, 0.0, 10.0, 10.0)));
|
|
assert!(!writable(&s, Affine::translate((0.0, 2e9)), r));
|
|
assert!(!writable(&s, Affine::scale(2e9), Rect::ZERO));
|
|
// `writable` also checks the surface's own transform: with the surface moved to
|
|
// x = MAX_COORD, a point at the origin fits and a 10 by 10 box does not.
|
|
s.push_transform(&xf(Affine::translate((MAX_COORD, 0.0))));
|
|
assert!(writable(&s, Affine::IDENTITY, Rect::ZERO));
|
|
assert!(!writable(&s, Affine::IDENTITY, r));
|
|
s.pop();
|
|
s.finish();
|
|
page.finish();
|
|
}
|
|
|
|
#[test]
|
|
fn a_16_bit_png_at_8_bits_keeps_its_color_type_and_icc_profile() {
|
|
use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image as CodecImage, SampleType};
|
|
let icc = vectorcraft_color::cms::icc_bytes(vectorcraft_color::cms::GRAY).unwrap().to_vec();
|
|
let u16_samples: Vec<u16> = (0..2).flat_map(|y| (0..4).map(move |x| x as u16 * 20000 + y as u16)).collect();
|
|
let raw_u16: Vec<u8> = u16_samples.iter().flat_map(|v| v.to_ne_bytes()).collect();
|
|
let mut src = CodecImage::from_raw(4, 2, ChannelLayout::Gray, SampleType::U16, raw_u16).unwrap();
|
|
src.icc = Some(icc.clone());
|
|
let png = craft_image_codecs::encode(&src, Format::Png, &EncodeOptions::default()).unwrap();
|
|
assert!(png_16_bit(&png));
|
|
let out = png_8_bit(&png).unwrap();
|
|
assert!(!png_16_bit(&out));
|
|
let decoded = craft_image_codecs::decode_as_with(Format::Png, &out, &craft_image_codecs::DecodeOptions::default()).unwrap();
|
|
assert_eq!(decoded.icc, Some(icc));
|
|
assert_eq!(decoded.layout(), ChannelLayout::Gray);
|
|
assert_eq!(decoded.sample_type(), SampleType::U8);
|
|
// Rounded to the nearest 8-bit level, as the 8-bit copy is made.
|
|
let want: Vec<u8> = u16_samples.into_iter().map(|v| ((u32::from(v) * 255 + 32_767) / 65_535) as u8).collect();
|
|
assert_eq!(decoded.data(), want);
|
|
}
|
|
}
|