use crate::canvas::Canvas;
use crate::color::Color;
use crate::font::FontFile;
use crate::geom::{Point, Rect, Size, pt, rect};
use crate::path::Path;
use crate::pen::{Full, Ink, Inked, Pen, fully};
use crate::scripts::Language;
use crate::shape::{LONGEST, Laid, Plan, Script};
use crate::text::{Face, Font, Metrics, Rule, cluster};
use crate::ui::{Icon, Ui};
pub(crate) const FINE: f32 = 300.0 / 96.0;
const LOOKS: usize = 64;
#[derive(Default)]
pub struct Paper {
pub title: String,
pub language: Language,
pub(crate) pages: Vec<Sheet>,
on: usize,
pub(crate) files: Vec<FontFile>,
own: Vec<(Face, u32)>,
found: Vec<(Face, char, Option<u32>)>,
plans: Vec<(u32, Script, bool, Language, Plan)>,
pub(crate) pictures: Vec<Picture>,
pub(crate) named: Vec<(String, u32)>,
pub(crate) anchors: Vec<(String, usize, f32)>,
unpack: Option<Unpack>,
laid: Vec<Laid>,
order: crate::order::Order,
}
type Unpack = fn(&[u8]) -> Option<(usize, Vec<u32>)>;
pub(crate) struct Sheet {
pub size: Size,
pub marks: Vec<Mark>,
}
pub(crate) enum Mark {
Shape(Box<Drawn>),
Words(Box<Words>),
Picture(u32, Rect),
Clip(Rect),
Unclip,
Link(Rect, String),
}
pub(crate) struct Drawn {
pub path: Path,
pub pen: Option<Pen<'static>>,
pub dashes: Vec<f32>,
pub ink: Kept,
pub full: Option<Full>,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Kept {
Color(Color),
Along(f32, Vec<(f32, Color)>),
Around(Vec<(f32, Color)>),
}
pub(crate) struct Words {
pub font: u32,
pub size: f32,
pub color: Color,
pub glyphs: Vec<Glyph>,
pub text: String,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Glyph {
pub id: u16,
pub at: Point,
pub chars: [u32; 2],
}
pub(crate) enum Picture {
Pixels { width: usize, pixels: Vec<u32> },
Jpeg { file: Vec<u8>, size: [usize; 2], planes: u8, adobe: bool, turn: usize },
}
#[derive(Clone, Debug)]
struct Run {
bytes: std::ops::Range<usize>,
file: Option<u32>,
leftward: bool,
script: Option<Script>,
}
impl std::fmt::Debug for Paper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Paper").field("title", &self.title).field("language", &self.language).field("pages", &self.pages.len()).finish_non_exhaustive()
}
}
impl Paper {
pub const A4: Size = Size { w: 210.0 / 25.4 * 96.0, h: 297.0 / 25.4 * 96.0 };
pub const LETTER: Size = Size { w: 816.0, h: 1056.0 };
pub fn new() -> Self {
Paper { language: Language::of_the_system(), ..Paper::default() }
}
pub(crate) const LARGEST: f32 = 200.0 * 96.0;
pub fn page(&mut self, size: Size) {
self.pages.push(Sheet { size: Paper::sheet(size), marks: Vec::new() });
self.on = self.pages.len() - 1;
}
pub(crate) fn sheet(size: Size) -> Size {
let side = |side: f32| if side >= 1.0 { side.min(Paper::LARGEST) } else { 1.0 };
Size { w: side(size.w), h: side(size.h) }
}
pub fn turn_to(&mut self, page: usize) {
self.on = page.min(self.pages.len().saturating_sub(1));
}
pub fn pages(&self) -> usize {
self.pages.len()
}
pub fn size(&self, page: usize) -> Size {
self.pages.get(page).map_or(Size { w: 0.0, h: 0.0 }, |sheet| sheet.size)
}
pub(crate) fn put(&mut self, mark: Mark) {
if self.pages.is_empty() {
self.page(Paper::A4);
}
self.pages[self.on].marks.push(mark);
}
pub fn rect(&mut self, r: Rect, color: Color) {
self.rounded(r, 0.0, color);
}
pub fn rounded(&mut self, r: Rect, radius: f32, color: Color) {
let mut path = Path::new();
path.rounded_rect(r.x, r.y, r.w, r.h, radius);
self.shape(&path, color);
}
pub fn border(&mut self, r: Rect, radius: f32, width: f32, color: Color) {
let width = width.min(r.w / 2.0).min(r.h / 2.0);
if width <= 0.0 {
return;
}
let (mut path, mut inner) = (Path::new(), Path::new());
path.rounded_rect(r.x, r.y, r.w, r.h, radius);
inner.rounded_rect(r.x + width, r.y + width, r.w - 2.0 * width, r.h - 2.0 * width, (radius - width).max(0.0));
path.add(inner.reverse());
self.shape(&path, color);
}
pub fn gradient(&mut self, r: Rect, radius: f32, colors: &[(f32, Color)], turn: Option<f32>) {
let mut path = Path::new();
path.rounded_rect(r.x, r.y, r.w, r.h, radius);
self.shape(&path, turn.map_or(Ink::Around(colors), |turn| Ink::Along(turn, colors)));
}
pub fn shape<'a>(&mut self, path: &Path, ink: impl Into<Ink<'a>>) {
let ink = ink.into();
self.drawn(path, None, ink, fully(None, &ink));
}
pub fn outline<'a, 'b>(&mut self, path: &Path, pen: impl Into<Pen<'a>>, ink: impl Into<Ink<'b>>) {
let (pen, ink) = (pen.into(), ink.into());
self.drawn(path, Some(pen), ink, fully(Some(&pen), &ink));
}
fn drawn(&mut self, path: &Path, pen: Option<Pen>, ink: Ink, full: Option<Full>) {
let ink = match ink {
Ink::Color(color) if color.is_none() => return,
Ink::Color(color) => Kept::Color(color),
Ink::Along(turn, stops) => Kept::Along(turn, stops.to_vec()),
Ink::Around(stops) => Kept::Around(stops.to_vec()),
};
let kept = pen.map(|pen| Pen { width: pen.width, cap: pen.cap, join: pen.join, dashes: &[], offset: pen.offset });
let dashes = pen.map_or(Vec::new(), |pen| pen.dashes.to_vec());
self.put(Mark::Shape(Box::new(Drawn { path: path.clone(), pen: kept, dashes, ink, full })));
}
pub fn picture(&mut self, width: usize, pixels: &[u32], r: Rect) {
if let Some(at) = self.keep_picture(width, pixels) {
self.put(Mark::Picture(at, r));
}
}
fn keep_picture(&mut self, width: usize, pixels: &[u32]) -> Option<u32> {
if width == 0 || pixels.len() < width {
return None;
}
let pixels = &pixels[..pixels.len() / width * width];
let same = |kept: &Picture| matches!(kept, Picture::Pixels { width: w, pixels: p } if *w == width && p == pixels);
if let Some(at) = self.pictures.iter().position(same) {
return Some(at as u32);
}
self.pictures.push(Picture::Pixels { width, pixels: pixels.to_vec() });
Some(self.pictures.len() as u32 - 1)
}
pub fn jpeg(&mut self, file: &[u8], r: Rect) -> bool {
self.unpack = Some(crate::jpeg::decode);
if let Some(picture) = passed(file) {
self.pictures.push(picture);
self.put(Mark::Picture(self.pictures.len() as u32 - 1, r));
return true;
}
let Some((width, pixels)) = crate::jpeg::decode(file) else { return false };
self.picture(width, &pixels, r);
true
}
pub fn name_picture(&mut self, name: &str, width: usize, pixels: &[u32]) {
if let Some(at) = self.keep_picture(width, pixels) {
self.named.retain(|(known, _)| known != name);
self.named.push((name.to_string(), at));
}
}
pub fn interface(&mut self, r: Rect, describe: &mut dyn FnMut(&mut Ui)) {
let text = crate::platform::system_text();
let mut ui = Ui::new(text, Box::new(crate::platform::LocalClipboard::default()));
ui.theme = crate::theme::Theme::light();
ui.resize(Size { w: r.w.max(1.0), h: r.h.max(1.0) }, FINE);
for _ in 0..4 {
if ui.run(describe).is_empty() {
break;
}
}
let drawn = crate::surface::Surface::draw(&mut ui, 64);
let pixels: Vec<u32> = drawn.pixels.iter().map(|pixel| unmixed(*pixel)).collect();
self.picture(drawn.width, &pixels, r);
}
pub fn icon(&mut self, icon: Icon, r: Rect, color: Color) {
let by = r.w.min(r.h) / icon.view.max(1e-3);
let mut path = Path::svg(icon.path);
path.transform(crate::geom::Transform::new().scale(by, by).move_by(r.x, r.y));
match icon.stroke > 0.0 {
true => self.outline(&path, icon.stroke * by, color),
false => self.shape(&path, color),
}
}
pub fn link(&mut self, r: Rect, to: &str) {
if !to.is_empty() && !r.is_empty() {
self.put(Mark::Link(r, to.to_string()));
}
}
pub fn anchor(&mut self, name: &str, y: f32) {
if self.pages.is_empty() {
self.page(Paper::A4);
}
self.anchors.push((name.to_string(), self.on, y));
}
pub fn clip(&mut self, r: Rect) {
self.put(Mark::Clip(r));
}
pub fn unclip(&mut self) {
self.put(Mark::Unclip);
}
pub fn font(&mut self, face: Face, file: FontFile) {
self.files.push(file);
self.own.retain(|(known, _)| *known != face);
self.own.push((face, self.files.len() as u32 - 1));
}
pub fn text(&mut self, text: &str, x: f32, y: f32, font: Font, color: Color) -> f32 {
let line = y + self.metrics(font).ascent;
self.line(text, x, line, font, color)
}
pub(crate) fn line(&mut self, text: &str, x: f32, line: f32, font: Font, color: Color) -> f32 {
let mut pen = x;
for run in self.runs(text, font.face) {
pen += self.write(&text[run.bytes.clone()], &run, font.size, [pen, line], color);
}
if font.rule != Rule::None && pen > x {
let top = line - self.metrics(font).ascent;
self.rule(rect(x, top, pen - x, 0.0), font, color);
}
pen - x
}
pub fn measure(&mut self, text: &str, font: Font) -> f32 {
self.stops(text, font, &mut Vec::new())
}
pub fn metrics(&mut self, font: Font) -> Metrics {
let main = self.file_for(font.face, '\0');
main.map_or(Metrics { ascent: font.size * 0.9, descent: font.size * 0.25, cap: font.size * 0.7, small: font.size * 0.5 }, |at| self.files[at as usize].metrics(font.size))
}
pub fn paragraph(&mut self, text: &str, r: Rect, font: Font, color: Color) -> f32 {
let (mut stops, mut lines) = (Vec::new(), Vec::new());
self.stops(text, font, &mut stops);
crate::text::wrap(text, &stops, r.w, &mut lines);
let (high, height) = ((font.size * 1.5).round(), self.metrics(font).height());
for (n, line) in lines.iter().enumerate() {
let y = r.y + n as f32 * high + ((high - height) / 2.0).max(0.0);
self.text(&text[line.start..line.end], r.x, y, font, color);
}
lines.len() as f32 * high
}
fn rule(&mut self, line: Rect, font: Font, color: Color) {
let m = self.metrics(font);
let thick = (font.size / 14.0).max(0.5);
let small = if m.small > 0.0 { m.small } else { font.size / 2.0 };
let down = if font.rule == Rule::Under { m.ascent + thick } else { m.ascent - (small + thick) / 2.0 };
self.rect(rect(line.x, line.y + down, line.w, thick), color);
}
pub(crate) fn stops(&mut self, text: &str, font: Font, stops: &mut Vec<f32>) -> f32 {
stops.clear();
let (mut runs, mut ends, mut whole) = (self.runs(text, font.face), Vec::new(), 0.0);
runs.sort_by_key(|run| run.bytes.start);
for run in &runs {
let piece = &text[run.bytes.clone()];
let unit = run.file.map_or(0.0, |file| self.files[file as usize].unit(font.size));
ends.clear();
ends.resize(piece.chars().count(), 0);
for laid in self.shaped(piece, run) {
ends[laid.from as usize] += laid.advance;
}
let mut sum = 0;
for end in &ends {
sum += end;
stops.push(whole + sum as f32 * unit);
}
whole += sum as f32 * unit;
}
whole
}
fn write(&mut self, piece: &str, run: &Run, size: f32, at: [f32; 2], color: Color) -> f32 {
let Some(file) = run.file else { return 0.0 };
let unit = self.files[file as usize].unit(size);
let laid = self.shaped(piece, run);
let starts: Vec<usize> = piece.char_indices().map(|(at, _)| at).chain([piece.len()]).collect();
let mut firsts: Vec<u32> = laid.iter().map(|laid| laid.from).collect();
firsts.sort_unstable();
firsts.dedup();
let (mut pen, mut glyphs, mut taken) = (0, Vec::with_capacity(laid.len()), Vec::new());
for one in &laid {
let next = firsts.partition_point(|from| *from <= one.from);
let end = firsts.get(next).map_or(starts.len() - 1, |from| *from as usize);
let chars = if taken.contains(&one.from) { [0, 0] } else { [starts[one.from as usize] as u32, starts[end] as u32] };
taken.push(one.from);
let x = at[0] + (pen + one.offset[0]) as f32 * unit;
glyphs.push(Glyph { id: one.glyph, at: pt(x, at[1] - one.offset[1] as f32 * unit), chars });
pen += one.advance;
}
if !glyphs.is_empty() && size > 0.0 && size.is_finite() {
self.put(Mark::Words(Box::new(Words { font: file, size, color, glyphs, text: piece.to_string() })));
}
pen as f32 * unit
}
fn shaped(&mut self, piece: &str, run: &Run) -> Vec<Laid> {
let Some(file) = run.file else { return Vec::new() };
let (script, language) = (run.script.unwrap_or(Script::None), self.language);
let plan = match self.plans.iter().position(|(of, known, way, said, _)| *of == file && *known == script && *way == run.leftward && *said == language) {
Some(plan) => plan,
None => {
self.plans.push((file, script, run.leftward, language, Plan::of(&self.files[file as usize], script, run.leftward, language)));
self.plans.len() - 1
}
};
self.files[file as usize].shape(&self.plans[plan].4, piece, &mut self.laid);
std::mem::take(&mut self.laid)
}
fn runs(&mut self, text: &str, face: Face) -> Vec<Run> {
let mut stretches = Vec::new();
match crate::order::plain(text) {
true => stretches.push((0..text.len(), 0)),
false => {
self.order.of(text, None);
self.order.shown(text, &mut stretches);
}
}
let mut runs = Vec::new();
for (bytes, level) in stretches {
let first = runs.len();
self.cut(text, bytes, level % 2 == 1, face, &mut runs);
if level % 2 == 1 {
runs[first..].reverse();
}
}
runs
}
fn cut(&mut self, text: &str, bytes: std::ops::Range<usize>, leftward: bool, face: Face, runs: &mut Vec<Run>) {
let (mut at, mut count) = (bytes.start, 0);
while at < bytes.end {
let size = cluster(&text[at..bytes.end]).max(1);
let letters = &text[at..at + size];
let script = letters.chars().find_map(Script::of);
let apart = |run: &Run| matches!((run.script, script), (Some(one), Some(other)) if one != other);
let current = runs.last().filter(|run: &&Run| run.bytes.end == at && run.leftward == leftward && count < LONGEST - 64 && !apart(run));
let keeps = current.and_then(|run| run.file).is_some_and(|file| letters.chars().all(|letter| unseen(letter) || self.files[file as usize].glyph(letter) != 0));
let first = letters.chars().next().unwrap_or(' ');
let file = match (keeps, first.is_control()) {
(_, true) => None,
(true, false) => current.and_then(|run| run.file),
(false, false) => self.file_for(face, first),
};
let goes_on = runs.last().is_some_and(|run| run.bytes.end == at && run.file == file && run.leftward == leftward && count < LONGEST - 64 && !apart(run));
match runs.last_mut().filter(|_| goes_on) {
Some(run) => (run.bytes.end, run.script) = (at + size, run.script.or(script)),
None => (count, _) = (0, runs.push(Run { bytes: at..at + size, file, leftward, script })),
}
(at, count) = (at + size, count + letters.chars().count());
}
}
fn file_for(&mut self, face: Face, letter: char) -> Option<u32> {
let has = |files: &[FontFile], file: u32| letter == '\0' || unseen(letter) || files[file as usize].glyph(letter) != 0;
let own = self.own.iter().find(|(known, _)| *known == face).map(|(_, file)| *file);
if let Some(file) = own.filter(|file| has(&self.files, *file)) {
return Some(file);
}
let found = self.found.iter().filter(|(known, ..)| *known == face).find_map(|(.., file)| file.filter(|file| has(&self.files, *file)));
if found.is_some() {
return found;
}
let main = self.looked(face, '\0');
let letter = if cfg!(windows) && !cfg!(feature = "fallback-fonts") { '\0' } else { letter };
let file = self.looked(face, letter);
file.filter(|file| has(&self.files, *file)).or(own).or(main).or(file)
}
fn looked(&mut self, face: Face, letter: char) -> Option<u32> {
if let Some((.., file)) = self.found.iter().find(|(known, of, _)| *known == face && *of == letter) {
return *file;
}
if self.found.len() >= LOOKS {
return None;
}
let found = crate::fonts::file_for(face, if letter == '\0' { 'a' } else { letter });
let same = |kept: &FontFile, file: &FontFile| kept.start == file.start && kept.varied.is_none() && kept.bytes().0 == file.bytes().0;
let at = found.map(|file| match self.files.iter().position(|kept| same(kept, &file)) {
Some(at) => at as u32,
None => {
self.files.push(file);
self.files.len() as u32 - 1
}
});
self.found.push((face, letter, at));
at
}
pub fn draw(&self, page: usize, canvas: &mut Canvas) {
let Some(sheet) = self.pages.get(page) else { return };
let mut held = Vec::new();
for mark in &sheet.marks {
match mark {
Mark::Shape(drawn) => drawn.draw(canvas),
Mark::Words(words) => self.draw_words(words, canvas),
Mark::Picture(at, r) => self.draw_picture(*at, *r, canvas),
Mark::Clip(r) => held.push(canvas.clip(*r)),
Mark::Unclip => held.pop().into_iter().for_each(|before| canvas.restore_clip(before)),
Mark::Link(..) => {}
}
}
}
fn draw_words(&self, words: &Words, canvas: &mut Canvas) {
let file = &self.files[words.font as usize];
let unit = file.unit(words.size);
for glyph in &words.glyphs {
let (path, _) = file.outline_through(glyph.id, [unit, 0.0, 0.0, -unit, glyph.at.x, glyph.at.y]);
canvas.fill(&path, pt(0.0, 0.0), 1.0, words.color);
}
}
fn draw_picture(&self, at: u32, r: Rect, canvas: &mut Canvas) {
match &self.pictures[at as usize] {
Picture::Pixels { width, pixels } => canvas.picture(pixels, *width, r),
Picture::Jpeg { file, .. } => self.unpack.and_then(|unpack| unpack(file)).into_iter().for_each(|(width, pixels)| canvas.picture(&pixels, width, r)),
}
}
pub fn picture_of(&self, page: usize, scale: f32) -> (usize, Vec<u32>) {
let size = self.size(page);
let (width, height) = ((size.w * scale).round().max(1.0) as usize, (size.h * scale).round().max(1.0) as usize);
let mut pixels = vec![0xffff_ffff; width * height];
for top in (0..height).step_by(256) {
let rows = 256.min(height - top);
let block = &mut pixels[top * width..(top + rows) * width];
self.draw(page, &mut Canvas::new(block, width, rows, (0, top as i32), scale));
}
(width, pixels)
}
}
impl Drawn {
fn draw(&self, canvas: &mut Canvas) {
let pen = self.pen.map(|pen| Pen { dashes: &self.dashes, ..pen });
let ink = match &self.ink {
Kept::Color(color) => Ink::Color(*color),
Kept::Along(turn, stops) => Ink::Along(*turn, stops),
Kept::Around(stops) => Ink::Around(stops),
};
let mut inked = Inked::default();
inked.set(&self.path, pen, ink, self.full);
inked.draw(canvas, pt(0.0, 0.0));
}
}
fn unseen(letter: char) -> bool {
matches!(letter, '\u{200b}'..='\u{200f}' | '\u{2060}'..='\u{2064}' | '\u{fe00}'..='\u{fe0f}' | '\u{e0000}'..='\u{e007f}' | '\u{e0100}'..='\u{e01ef}')
}
fn unmixed(pixel: u32) -> u32 {
let a = pixel >> 24;
match a {
0 => 0,
255 => pixel,
_ => {
let part = |shift: u32| (((pixel >> shift & 0xff) * 255 + a / 2) / a).min(255) << shift;
a << 24 | part(16) | part(8) | part(0)
}
}
}
pub(crate) fn passed(file: &[u8]) -> Option<Picture> {
let mut rest = file.strip_prefix(&[0xFF, 0xD8])?;
let (mut frame, mut adobe, mut turn) = (None, false, 1);
while let Some((kind, body, after)) = part(rest) {
rest = after;
match kind {
0xC0..=0xC2 => frame = Some(body),
0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => return None,
0xDA | 0xD9 => break,
0xE1 => turn = crate::upright::turn(body).unwrap_or(turn),
0xEE => adobe |= body.starts_with(b"Adobe"),
_ => {}
}
}
let frame = frame?;
let (depth, high, wide, planes) = (*frame.first()?, u16::from_be_bytes([*frame.get(1)?, *frame.get(2)?]), u16::from_be_bytes([*frame.get(3)?, *frame.get(4)?]), *frame.get(5)?);
let fits = depth == 8 && high > 0 && wide > 0 && (planes == 1 || planes == 3 || (planes == 4 && adobe)) && (1..=8).contains(&turn);
let size = if turn >= 5 { [high as usize, wide as usize] } else { [wide as usize, high as usize] };
fits.then(|| Picture::Jpeg { file: file.to_vec(), size, planes, adobe, turn })
}
fn part(rest: &[u8]) -> Option<(u8, &[u8], &[u8])> {
let at = rest.windows(2).position(|pair| pair[0] == 0xFF && pair[1] != 0xFF && pair[1] != 0)?;
let (kind, rest) = (rest[at + 1], &rest[at + 2..]);
if matches!(kind, 0x01 | 0xD0..=0xD9) {
return Some((kind, &[], rest));
}
let long = u16::from_be_bytes([*rest.first()?, *rest.get(1)?]) as usize;
Some((kind, rest.get(2..long)?, rest.get(long..)?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pdf::tests::latin;
fn paper() -> Option<Paper> {
let mut paper = Paper::new();
paper.font(Face::Regular, latin()?);
Some(paper)
}
#[test]
fn text_is_as_wide_as_it_measures_and_stands_on_its_line() {
let Some(mut paper) = paper() else { return eprintln!("no font file here: not tested") };
let font = Font::new(20.0);
let said = "Kerning: AV To Wa, fi \u{c5}ngstr\u{f6}m";
let wide = paper.measure(said, font);
assert!(wide > 200.0, "{wide}");
assert_eq!(paper.text(said, 10.0, 30.0, font, Color::BLACK), wide);
let line = 30.0 + paper.metrics(font).ascent;
let Some(Mark::Words(words)) = paper.pages[0].marks.first() else { panic!("words were written") };
assert!(words.glyphs.iter().all(|glyph| glyph.at.y == line), "every glyph on the line");
assert!(words.glyphs.windows(2).all(|pair| pair[0].at.x < pair[1].at.x), "from left to right");
let mut stops = Vec::new();
assert_eq!(paper.stops(said, font, &mut stops), wide);
assert_eq!((stops.len(), stops.last().copied()), (said.chars().count(), Some(wide)));
}
#[test]
fn a_paragraph_breaks_into_rows_no_wider_than_its_room() {
let Some(mut paper) = paper() else { return eprintln!("no font file here: not tested") };
let font = Font::new(14.0);
let said = "Words that go on for a while break into rows, at the spaces between them, no wider than the room.\nAnd a line end starts a row of its own.";
let high = paper.paragraph(said, rect(20.0, 20.0, 200.0, 0.0), font, Color::BLACK);
let rows = (high / 21.0).round() as usize;
assert!(rows >= 4 && high == rows as f32 * 21.0, "{high}");
for mark in &paper.pages[0].marks {
let Mark::Words(words) = mark else { continue };
let last = words.glyphs.last().map_or(0.0, |glyph| glyph.at.x + paper.files[words.font as usize].width(glyph.id) * paper.files[words.font as usize].unit(14.0));
assert!(last <= 220.5 && words.glyphs[0].at.x >= 20.0, "a row from 20 to {last}");
}
}
#[test]
fn what_is_not_seen_takes_no_room_and_a_letter_a_font_lacks_comes_from_another() {
let Some(mut paper) = paper() else { return eprintln!("no font file here: not tested") };
let font = Font::new(16.0);
assert_eq!(paper.measure("a\nb", font), paper.measure("a", font) + paper.measure("b", font));
paper.text("Latin, then \u{4e2d}\u{6587}", 0.0, 0.0, font, Color::BLACK);
let fonts: Vec<u32> = paper.pages[0].marks.iter().filter_map(|mark| if let Mark::Words(words) = mark { Some(words.font) } else { None }).collect();
let written_in = |letter: char| paper.files.iter().position(|file| file.glyph(letter) != 0);
match written_in('\u{4e2d}') {
Some(_) => assert!(fonts.len() == 2 && paper.files[fonts[1] as usize].glyph('\u{4e2d}') != 0, "{fonts:?}"),
None => eprintln!("no font with Chinese letters here: written in the box of the face's own"),
}
}
#[test]
fn a_page_is_drawn_on_a_canvas_with_its_clip() {
let mut paper = Paper::new();
paper.page(Size { w: 40.0, h: 20.0 });
paper.clip(rect(0.0, 0.0, 20.0, 20.0));
paper.rect(rect(0.0, 0.0, 40.0, 20.0), Color::rgb(0x112233));
paper.unclip();
paper.rect(rect(30.0, 0.0, 10.0, 20.0), Color::rgb(0x445566));
let (width, pixels) = paper.picture_of(0, 1.0);
assert_eq!((width, pixels.len()), (40, 800));
let at = |x: usize, y: usize| pixels[y * 40 + x] & 0xff_ffff;
assert_eq!([at(5, 5), at(25, 5), at(35, 5)], [0x112233, 0xff_ffff, 0x445566]);
}
fn media_box(file: &[u8]) -> Vec<f32> {
let file = String::from_utf8_lossy(file);
let at = file.find("/MediaBox [").expect("a page") + "/MediaBox [".len();
file[at..].split(']').next().unwrap().split_whitespace().map(|number| number.parse().unwrap()).collect()
}
#[test]
fn a_page_of_endless_size_is_one_a_reader_shows_and_one_that_can_be_drawn() {
let mut paper = Paper::new();
paper.page(Size { w: f32::INFINITY, h: 1e30 });
paper.rect(rect(10.0, 10.0, 100.0, 100.0), Color::BLACK);
let sides = media_box(&paper.pdf());
assert!(sides[2] >= 3.0 && sides[3] >= 3.0 && sides[2] <= 14_400.0 && sides[3] <= 14_400.0, "{sides:?}");
assert!(paper.size(0).w.is_finite() && paper.size(0).h.is_finite(), "{:?}", paper.size(0));
let (width, pixels) = paper.picture_of(0, 0.01);
assert!(width > 0 && pixels.len() < 1 << 24, "{width} wide, {} pixels", pixels.len());
}
#[test]
fn a_page_with_a_clip_and_a_picture_of_any_size_is_drawn() {
let mut paper = Paper::new();
paper.page(Size { w: 40.0, h: 30.0 });
paper.clip(rect(-1e30, -1e30, 2e30, 2e30));
paper.picture(1, &[0xff11_2233], rect(-1e30, -1e30, 2e30, 2e30));
paper.unclip();
paper.picture(1, &[0xff44_5566], rect(f32::NAN, 0.0, 1e38, 10.0));
let drawn = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| paper.picture_of(0, 1.0)));
let (_, pixels) = drawn.expect("the page is drawn");
assert_eq!(pixels[20 * 40 + 20] & 0xff_ffff, 0x11_2233, "the picture over all of it shows");
}
}