mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 14:06:09 +00:00
Merge main and retain qualified changes for #6593
This commit is contained in:
@@ -728,6 +728,51 @@ fn images_are_written_in_the_form_the_specification_shows() {
|
||||
assert!(rgb.iter().zip([247.0, 148.0, 29.0]).all(|(c, want)| (c * 255.0 - want).abs() < 0.1), "{rgb:?}");
|
||||
}
|
||||
|
||||
/// An image's samples are written as upper-case hex on `%` comment lines (§8.1, §8.4) of 60
|
||||
/// digits, the last line shorter, each line ending with a newline: no samples write no line, and
|
||||
/// samples that fill their last line exactly write no empty `%` line after it.
|
||||
#[test]
|
||||
fn image_samples_are_written_as_hex_lines() {
|
||||
let hex = |samples: &[u8]| {
|
||||
let mut s = String::new();
|
||||
super::writer::Writer::push_hex(&mut s, samples);
|
||||
s
|
||||
};
|
||||
let mut samples = vec![0x00, 0x09, 0x0A, 0xAF, 0xFF];
|
||||
samples.extend(0x10..=0x29);
|
||||
assert_eq!(hex(&samples), "%00090AAFFF101112131415161718191A1B1C1D1E1F202122232425262728\n%29\n");
|
||||
assert_eq!(hex(&samples[..30]), "%00090AAFFF101112131415161718191A1B1C1D1E1F202122232425262728\n");
|
||||
assert_eq!(hex(&samples[..29]), "%00090AAFFF101112131415161718191A1B1C1D1E1F2021222324252627\n");
|
||||
assert_eq!(hex(&[0xAB]), "%AB\n");
|
||||
assert_eq!(hex(&[]), "");
|
||||
// Appended after what is already written.
|
||||
let mut s = String::from("0 0 1 1 1 1 8 3 0 0 0 0 XI\n");
|
||||
super::writer::Writer::push_hex(&mut s, &[0xC0, 0xFF, 0xEE]);
|
||||
assert_eq!(s, "0 0 1 1 1 1 8 3 0 0 0 0 XI\n%C0FFEE\n");
|
||||
}
|
||||
|
||||
/// The hex lines are the same as formatting each sample with `{:02X}` and breaking the digits
|
||||
/// into lines of [`HEX_LINE`], for every length up to three lines and one more sample, and for
|
||||
/// every byte value.
|
||||
#[test]
|
||||
fn image_hex_matches_formatted_samples() {
|
||||
let formatted = |samples: &[u8]| {
|
||||
let digits: String = samples.iter().map(|c| format!("{c:02X}")).collect();
|
||||
let lines: Vec<String> = digits.as_bytes().chunks(HEX_LINE).map(|l| format!("%{}\n", std::str::from_utf8(l).unwrap())).collect();
|
||||
lines.concat()
|
||||
};
|
||||
let all: Vec<u8> = (0..=255u8).cycle().skip(7).take(3 * HEX_LINE / 2 + 1).collect();
|
||||
for n in 0..=all.len() {
|
||||
let mut s = String::new();
|
||||
super::writer::Writer::push_hex(&mut s, &all[..n]);
|
||||
assert_eq!(s, formatted(&all[..n]), "{n} samples");
|
||||
}
|
||||
let every: Vec<u8> = (0..=255u8).collect();
|
||||
let mut s = String::new();
|
||||
super::writer::Writer::push_hex(&mut s, &every);
|
||||
assert_eq!(s, formatted(&every));
|
||||
}
|
||||
|
||||
/// The pieces of an image cut into a raster mesh warp (as Object › Expand leaves a Warp on an
|
||||
/// image) are written as the image whole once, then each as the part of it that its triangle shows,
|
||||
/// and the file draws as the document does ([`write`] runs it).
|
||||
|
||||
@@ -1213,32 +1213,35 @@ impl<'a> Writer<'a> {
|
||||
coverage
|
||||
}
|
||||
|
||||
/// Appends `samples` to `s` as upper-case hex digits on comment lines: each line starts with `%`,
|
||||
/// holds [`HEX_LINE`] digits (the last line may hold fewer) and ends with a newline. An empty
|
||||
/// `samples` appends no line.
|
||||
pub(super) fn push_hex(s: &mut String, samples: &[u8]) {
|
||||
const DIGITS: &[u8; 16] = b"0123456789ABCDEF";
|
||||
const PER_LINE: usize = HEX_LINE / 2;
|
||||
const _: () = assert!(HEX_LINE > 0 && HEX_LINE.is_multiple_of(2));
|
||||
let lines = samples.len().div_ceil(PER_LINE);
|
||||
s.reserve(samples.len().saturating_mul(2).saturating_add(lines.saturating_mul(2)));
|
||||
for line in samples.chunks(PER_LINE) {
|
||||
s.push('%');
|
||||
for &c in line {
|
||||
s.push(char::from(DIGITS[usize::from(c >> 4)]));
|
||||
s.push(char::from(DIGITS[usize::from(c & 0x0F)]));
|
||||
}
|
||||
s.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
/// An image's lines (§8.4): `%AI5_File:`, `%AI5_BeginRaster`, matrix `m` as `[ a b c d tx ty ]`
|
||||
/// and the numbers `operands` with `XI`, `samples` in hex on comment lines of [`HEX_LINE`]
|
||||
/// digits, `%AI5_EndRaster`, then `F`.
|
||||
fn raster(&mut self, m: Affine, operands: &str, samples: &[u8]) {
|
||||
let mut hex = String::with_capacity(samples.len() * 2 + samples.len() / HEX_LINE * 2 + 2);
|
||||
hex.push('%');
|
||||
let mut on_line = 0;
|
||||
for c in samples {
|
||||
let _ = write!(hex, "{c:02X}");
|
||||
on_line += 2;
|
||||
if on_line >= HEX_LINE {
|
||||
hex.push_str("\n%");
|
||||
on_line = 0;
|
||||
}
|
||||
}
|
||||
if hex.ends_with('%') {
|
||||
hex.pop();
|
||||
} else {
|
||||
hex.push('\n');
|
||||
}
|
||||
let [a, b, c, d, e, f] = m.as_coeffs();
|
||||
let s = &mut self.body;
|
||||
s.push_str("%AI5_File:\n%AI5_BeginRaster\n[ ");
|
||||
ps::push_nums(s, &[a, b, c, d, e, f]);
|
||||
let _ = writeln!(s, "] {operands} XI");
|
||||
s.push_str(&hex);
|
||||
Self::push_hex(s, samples);
|
||||
s.push_str("%AI5_EndRaster\nF\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,16 +111,21 @@ pub(crate) struct Look {
|
||||
}
|
||||
|
||||
impl Look {
|
||||
/// Can a glyph drawn with `other` join a run drawn with this? A fill joins a run whose
|
||||
/// glyphs were also stroked over (fill and stroke rendering draws each glyph twice); invisible
|
||||
/// text (neither fill nor stroke) joins only invisible text.
|
||||
/// Can a glyph drawn with `other` join a run drawn with this? Only a run drawn alike: fill,
|
||||
/// stroke and overprints. A glyph filled only never joins a run whose glyphs were also stroked
|
||||
/// over: it takes a stroke only when one is drawn over it later
|
||||
/// ([`TextLine::stroke_filled`]), which then joins it to the run before if that is stroked
|
||||
/// alike (fill and stroke rendering draws each glyph twice), and a glyph filled only after
|
||||
/// one filled and stroked stays unstroked (#8300). Invisible text (neither fill nor stroke)
|
||||
/// joins only invisible text.
|
||||
fn takes(&self, other: &Look) -> bool {
|
||||
self.font == other.font
|
||||
&& (self.size - other.size).abs() <= self.size * 0.01
|
||||
&& (self.h_scale - other.h_scale).abs() < 0.5
|
||||
&& self.fill == other.fill
|
||||
&& self.overprint_fill == other.overprint_fill
|
||||
&& ((other.stroke.is_none() && other.fill.is_some()) || (self.stroke == other.stroke && self.overprint_stroke == other.overprint_stroke))
|
||||
&& self.stroke == other.stroke
|
||||
&& self.overprint_stroke == other.overprint_stroke
|
||||
}
|
||||
|
||||
fn style(&self) -> CharStyle {
|
||||
@@ -658,8 +663,9 @@ impl TextLine {
|
||||
|
||||
/// Restyle the glyphs gathered for a stroke ([`stroke_filled`](Self::stroke_filled)): their
|
||||
/// text becomes a run of its own with the stroke, unless the run is stroked so already, and the
|
||||
/// text before and after it in the run keeps its stroke. A run they make up whole is stroked
|
||||
/// whole; text that comes right after a run drawn alike joins it.
|
||||
/// text before and after it in the run keeps its stroke. Text that comes right after a run
|
||||
/// drawn alike joins it, a run made up of it whole too: fill and stroke rendering glyph by
|
||||
/// glyph fills each glyph as a run of its own ([`Look::takes`]) before its stroke (#8300).
|
||||
fn flush_stroke(&mut self) {
|
||||
let Some(PendingStroke { run, start, end, stroke, overprint }) = self.pending.take() else { return };
|
||||
let last = run.saturating_add(1) == self.runs.len();
|
||||
@@ -671,10 +677,6 @@ impl TextLine {
|
||||
if !(start < end && end <= text.len() && text.is_char_boundary(start) && text.is_char_boundary(end)) {
|
||||
return;
|
||||
}
|
||||
if start == 0 && end == text.len() {
|
||||
(look.stroke, look.overprint_stroke) = (Some(stroke), overprint);
|
||||
return;
|
||||
}
|
||||
let tail = text.split_off(end);
|
||||
let glyphs = text.split_off(start);
|
||||
let head = std::mem::take(text);
|
||||
@@ -738,11 +740,10 @@ impl TextLine {
|
||||
/// A stroke over glyph `glyph` just drawn at `at` in `font`, which `overprint`s or not: that
|
||||
/// glyph is stroked. `false`: it isn't that glyph (another place, font or text).
|
||||
///
|
||||
/// A filled glyph joins a run of the same fill whatever its stroke ([`Look::takes`]), so the
|
||||
/// run can hold glyphs before it that are stroked otherwise or not at all: then the glyph's
|
||||
/// text becomes a run of its own with this stroke, and the glyphs before it keep theirs
|
||||
/// (#7741). The line's text is the same either way, so the characters' slots, gaps and
|
||||
/// authored flags, kept along the whole line, still line up.
|
||||
/// The glyph's run can hold glyphs before it, filled alike and not stroked
|
||||
/// ([`Look::takes`]): then the glyph's text becomes a run of its own with this stroke, and
|
||||
/// the glyphs before it stay unstroked (#7741). The line's text is the same either way, so
|
||||
/// the characters' slots, gaps and authored flags, kept along the whole line, still line up.
|
||||
fn stroke_last(&mut self, font: u128, at: Placement, glyph: &str, stroke: TypeStroke, overprint: bool) -> bool {
|
||||
self.flush_stroke();
|
||||
let start = self.last_start;
|
||||
|
||||
@@ -297,3 +297,52 @@ fn a_stroke_of_another_glyph_where_one_was_filled_stays_its_own_type() {
|
||||
let want = [("A".to_string(), 60.0, true, true), ("B".to_string(), 60.0, true, false), ("X".to_string(), 60.0, false, true)];
|
||||
assert_eq!(word_runs(content), want, "{content}");
|
||||
}
|
||||
|
||||
/// Helvetica PDF page drawing `content` (fill cream, stroke black, 3 points wide), imported: each
|
||||
/// type object's runs in drawing order, as their text and whether they are filled and stroked.
|
||||
fn type_runs(content: &str) -> Vec<Vec<(String, bool, bool)>> {
|
||||
use vectorcraft_testkit::pdf::{PdfPage, pdf};
|
||||
let resources = "/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >>";
|
||||
let content = format!("0 0 0 RG 1 1 0.8 rg 3 w {content}");
|
||||
let bytes = pdf(&[PdfPage { resources: resources.into(), ..PdfPage::new(300.0, 200.0, &content) }], None);
|
||||
let d = import(&bytes).unwrap();
|
||||
let mut types = vec![];
|
||||
d.walk(|n| {
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
types.push(t.runs.iter().map(|r| (r.text.clone(), !r.style.fill.is_none(), !r.style.stroke.is_none())).collect());
|
||||
}
|
||||
});
|
||||
types
|
||||
}
|
||||
|
||||
/// A glyph filled only (mode 0) right after one filled and stroked (mode 2) is filled only: it
|
||||
/// joins the same type object, but not the stroked run, whether the glyphs before were filled
|
||||
/// only, filled and stroked, or both, and a glyph filled and stroked after it is stroked again
|
||||
/// (#8300).
|
||||
#[test]
|
||||
fn a_glyph_filled_only_after_one_filled_and_stroked_is_not_stroked() {
|
||||
let run = |text: &str, stroked: bool| (text.to_string(), true, stroked);
|
||||
let cases = [
|
||||
("2 Tr (B) Tj 0 Tr (C) Tj", vec![run("B", true), run("C", false)]),
|
||||
("0 Tr (A) Tj 2 Tr (B) Tj 0 Tr (C) Tj", vec![run("A", false), run("B", true), run("C", false)]),
|
||||
("2 Tr (B) Tj 0 Tr (C) Tj 2 Tr (D) Tj", vec![run("B", true), run("C", false), run("D", true)]),
|
||||
("2 Tr (AB) Tj 0 Tr (CD) Tj", vec![run("AB", true), run("CD", false)]),
|
||||
("0 Tr (A) Tj 2 Tr (BC) Tj", vec![run("A", false), run("BC", true)]),
|
||||
];
|
||||
for (show, want) in cases {
|
||||
let content = format!("BT /F1 48 Tf 20 80 Td {show} 0 Tr ET");
|
||||
assert_eq!(type_runs(&content), [want], "{content}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill and stroke rendering glyph by glyph, and a word filled whole then stroked whole at the
|
||||
/// same place, still come in as one run filled and stroked (#8300).
|
||||
#[test]
|
||||
fn glyphs_filled_and_stroked_one_by_one_stay_one_run() {
|
||||
for content in [
|
||||
"BT /F1 48 Tf 20 80 Td 2 Tr (A) Tj (B) Tj (C) Tj 0 Tr ET",
|
||||
"BT /F1 48 Tf 1 0 0 1 20 80 Tm 0 Tr (ABC) Tj 1 Tr 1 0 0 1 20 80 Tm (ABC) Tj 0 Tr ET",
|
||||
] {
|
||||
assert_eq!(type_runs(content), [vec![("ABC".to_string(), true, true)]], "{content}");
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
+3
-2
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user