mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-20 01:14:23 +01:00
Numbers, strings, matrices, path data, fill/clip operators and Flate come from craft_ps::write. The VCdict prolog, ASCII85 and PackBits stay in the app.
This commit is contained in:
1 parent
7452b6f383
commit
8263ec5e41
4 files changed
+36
-169
No files matched your search
Generated
+10
@@ -1193,6 +1193,15 @@ dependencies = [
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ps"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a80c0f25f1c4767374a27a444fbf95e3e8162e14#a80c0f25f1c4767374a27a444fbf95e3e8162e14"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"kurbo 0.13.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
@@ -6790,6 +6799,7 @@ dependencies = [
|
||||
"craft-eps",
|
||||
"craft-image-codecs",
|
||||
"craft-pdf",
|
||||
"craft-ps",
|
||||
"craft-stream-codecs",
|
||||
"encoding_rs",
|
||||
"fax",
|
||||
|
||||
@@ -30,6 +30,8 @@ craft-affinity = { git = "https://github.com/storytold/craft-libs", rev = "a4997
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["tree"] }
|
||||
# EPS container, previews, ASCII85 and DSC comments (craft-libs#778).
|
||||
craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# PostScript write tokens (craft-libs#778, #790).
|
||||
craft-ps = { git = "https://github.com/storytold/craft-libs", rev = "a80c0f25f1c4767374a27a444fbf95e3e8162e14", features = ["write"] }
|
||||
# DXF tags, reader and writer (craft-libs#778, #926, #927).
|
||||
craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" }
|
||||
# WMF, EMF and EMF+ reader (craft-libs#778).
|
||||
|
||||
@@ -10,6 +10,8 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-eps = { workspace = true }
|
||||
# PostScript token writer (numbers, strings, paths, Flate).
|
||||
craft-ps = { workspace = true }
|
||||
vectorcraft-geom = { workspace = true }
|
||||
vectorcraft-color = { workspace = true }
|
||||
vectorcraft-doc = { workspace = true }
|
||||
|
||||
+22
-169
@@ -1,11 +1,9 @@
|
||||
//! PostScript writing primitives: numbers, strings, paths, the prolog's short operator names, and
|
||||
//! the data encodings images and embedded data use (ASCII85, run-length, Flate).
|
||||
//!
|
||||
//! Token formatting comes from [`craft_ps::write`]. The `VCdict` prolog and PackBits stay here.
|
||||
|
||||
use std::fmt::Write;
|
||||
use std::io::{Read, Write as _};
|
||||
|
||||
use kurbo::PathEl;
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule};
|
||||
use vectorcraft_geom::{BezPath, FillRule};
|
||||
|
||||
/// The procedures the page uses, in `VCdict`.
|
||||
pub(crate) const PROLOG: &str = "/VCdict 64 dict def
|
||||
@@ -21,159 +19,7 @@ VCdict begin
|
||||
end
|
||||
";
|
||||
|
||||
/// `v` as PostScript writes it: at most four decimals, no trailing zeros (0 when not finite).
|
||||
pub(crate) fn num(v: f64) -> String {
|
||||
let mut s = String::new();
|
||||
push_num(&mut s, v);
|
||||
s
|
||||
}
|
||||
|
||||
/// Append [`num`]`(v)` to `s`.
|
||||
pub(crate) fn push_num(s: &mut String, v: f64) {
|
||||
push_num_places(s, v, 4);
|
||||
}
|
||||
|
||||
/// The value [`num`]`(v)` stands for, as a reader parses it back.
|
||||
pub(crate) fn written(v: f64) -> f64 {
|
||||
num(v).parse().unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Append `v` to `s` with at most `places` decimals (up to 9), no trailing zeros (0 when not
|
||||
/// finite).
|
||||
pub(crate) fn push_num_places(s: &mut String, v: f64, places: u32) {
|
||||
let places = places.min(9);
|
||||
let scale = 10f64.powi(places as i32);
|
||||
// Past where the scaling overflows, values have no fractional digits to round.
|
||||
let v = if !v.is_finite() {
|
||||
0.0
|
||||
} else if (v * scale).is_finite() {
|
||||
(v * scale).round() / scale
|
||||
} else {
|
||||
v
|
||||
};
|
||||
if v == v.trunc() && v.abs() < 1e15 {
|
||||
let _ = write!(s, "{}", v as i64);
|
||||
return;
|
||||
}
|
||||
let start = s.len();
|
||||
let _ = write!(s, "{v:.*}", places as usize);
|
||||
let keep = s[start..].trim_end_matches('0').trim_end_matches('.').len();
|
||||
s.truncate(start + keep);
|
||||
}
|
||||
|
||||
/// `vs` as numbers separated by spaces, then a space.
|
||||
pub(crate) fn push_nums(s: &mut String, vs: &[f64]) {
|
||||
for v in vs {
|
||||
push_num(s, *v);
|
||||
s.push(' ');
|
||||
}
|
||||
}
|
||||
|
||||
/// `text` as a PostScript string: `(` `)` `\` escaped, anything outside printable ASCII as octal
|
||||
/// escapes of its UTF-8 bytes.
|
||||
pub(crate) fn string(text: &str) -> String {
|
||||
let mut s = String::with_capacity(text.len() + 2);
|
||||
s.push('(');
|
||||
for b in text.bytes() {
|
||||
match b {
|
||||
b'(' | b')' | b'\\' => {
|
||||
s.push('\\');
|
||||
s.push(char::from(b));
|
||||
}
|
||||
0x20..=0x7e => s.push(char::from(b)),
|
||||
_ => {
|
||||
let _ = write!(s, "\\{b:03o}");
|
||||
}
|
||||
}
|
||||
}
|
||||
s.push(')');
|
||||
s
|
||||
}
|
||||
|
||||
/// `bytes` as a PostScript string: `(` `)` `\` escaped, carriage returns, line feeds and tabs as
|
||||
/// `\r` `\n` `\t`, other bytes outside printable ASCII as octal escapes.
|
||||
pub(crate) fn bytes(bytes: &[u8]) -> String {
|
||||
let mut s = String::with_capacity(bytes.len() + 2);
|
||||
s.push('(');
|
||||
for &b in bytes {
|
||||
match b {
|
||||
b'(' | b')' | b'\\' => {
|
||||
s.push('\\');
|
||||
s.push(char::from(b));
|
||||
}
|
||||
b'\r' => s.push_str("\\r"),
|
||||
b'\n' => s.push_str("\\n"),
|
||||
b'\t' => s.push_str("\\t"),
|
||||
0x20..=0x7e => s.push(char::from(b)),
|
||||
_ => {
|
||||
let _ = write!(s, "\\{b:03o}");
|
||||
}
|
||||
}
|
||||
}
|
||||
s.push(')');
|
||||
s
|
||||
}
|
||||
|
||||
/// A matrix operand `[a b c d e f]`.
|
||||
pub(crate) fn matrix(a: Affine) -> String {
|
||||
let mut s = String::from("[");
|
||||
push_nums(&mut s, &a.as_coeffs());
|
||||
s.pop();
|
||||
s.push(']');
|
||||
s
|
||||
}
|
||||
|
||||
/// Append path `bp` (`m`, `l`, `c`, `h`; quadratics raised to cubics), one segment a line.
|
||||
pub(crate) fn push_path(s: &mut String, bp: &BezPath) {
|
||||
let mut last = kurbo::Point::ZERO;
|
||||
let mut start = kurbo::Point::ZERO;
|
||||
for el in bp.elements() {
|
||||
match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
push_nums(s, &[p.x, p.y]);
|
||||
s.push_str("m\n");
|
||||
(last, start) = (p, p);
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
push_nums(s, &[p.x, p.y]);
|
||||
s.push_str("l\n");
|
||||
last = p;
|
||||
}
|
||||
PathEl::QuadTo(a, p) => {
|
||||
let c1 = last + (a - last) * (2.0 / 3.0);
|
||||
let c2 = p + (a - p) * (2.0 / 3.0);
|
||||
push_nums(s, &[c1.x, c1.y, c2.x, c2.y, p.x, p.y]);
|
||||
s.push_str("c\n");
|
||||
last = p;
|
||||
}
|
||||
PathEl::CurveTo(a, b, p) => {
|
||||
push_nums(s, &[a.x, a.y, b.x, b.y, p.x, p.y]);
|
||||
s.push_str("c\n");
|
||||
last = p;
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
s.push_str("h\n");
|
||||
last = start;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The fill operator for `rule`.
|
||||
pub(crate) fn fill_op(rule: FillRule) -> &'static str {
|
||||
match rule {
|
||||
FillRule::NonZero => "f\n",
|
||||
FillRule::EvenOdd => "f*\n",
|
||||
}
|
||||
}
|
||||
|
||||
/// The clip operator for `rule` (it also ends the path).
|
||||
pub(crate) fn clip_op(rule: FillRule) -> &'static str {
|
||||
match rule {
|
||||
FillRule::NonZero => "W\n",
|
||||
FillRule::EvenOdd => "W*\n",
|
||||
}
|
||||
}
|
||||
pub(crate) use craft_ps::write::{bytes, deflate, inflate, matrix, num, push_num, push_num_places, push_nums, string, written};
|
||||
|
||||
/// `bytes` in ASCII85 with its `~>` end, in lines of about [`LINE`] characters, none starting
|
||||
/// with `%` (DSC readers would take it for a comment).
|
||||
@@ -197,19 +43,26 @@ pub(crate) fn ascii85_decode_within(text: &str, max: usize) -> Option<Vec<u8>> {
|
||||
/// Run-length encoding (PackBits: what PostScript's `RunLengthDecode` reads), shared with TIFF.
|
||||
pub(crate) use vectorcraft_render::encode::tiff::packbits;
|
||||
|
||||
/// `data` compressed for `FlateDecode` (zlib).
|
||||
pub(crate) fn deflate(data: &[u8]) -> Vec<u8> {
|
||||
let mut e = flate2::write::ZlibEncoder::new(Vec::with_capacity(data.len() / 2), flate2::Compression::default());
|
||||
// Writing to a Vec doesn't fail.
|
||||
let _ = e.write_all(data);
|
||||
e.finish().unwrap_or_default()
|
||||
/// Append path `bp` (`m`, `l`, `c`, `h`; quadratics raised to cubics), one segment a line.
|
||||
pub(crate) fn push_path(s: &mut String, bp: &BezPath) {
|
||||
craft_ps::write::push_path(s, bp, craft_ps::write::PathOps::SHORT);
|
||||
}
|
||||
|
||||
/// zlib `data` inflated, when it is valid and at most `max` bytes.
|
||||
pub(crate) fn inflate(data: &[u8], max: u64) -> Option<Vec<u8>> {
|
||||
let mut out = vec![];
|
||||
flate2::read::ZlibDecoder::new(data).take(max + 1).read_to_end(&mut out).ok()?;
|
||||
(out.len() as u64 <= max).then_some(out)
|
||||
/// The fill operator for `rule`.
|
||||
pub(crate) fn fill_op(rule: FillRule) -> &'static str {
|
||||
craft_ps::write::fill_op(paint_rule(rule), craft_ps::write::PaintOps::SHORT)
|
||||
}
|
||||
|
||||
/// The clip operator for `rule` (it also ends the path).
|
||||
pub(crate) fn clip_op(rule: FillRule) -> &'static str {
|
||||
craft_ps::write::clip_op(paint_rule(rule), craft_ps::write::PaintOps::SHORT)
|
||||
}
|
||||
|
||||
fn paint_rule(rule: FillRule) -> craft_ps::write::FillRule {
|
||||
match rule {
|
||||
FillRule::NonZero => craft_ps::write::FillRule::NonZero,
|
||||
FillRule::EvenOdd => craft_ps::write::FillRule::EvenOdd,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user