use super::super::display::{Item, Page};
use super::super::fonts::FontBook;
use super::super::para::{self, Ctx};
use super::super::pdf;
use super::super::shape::Shaper;
use super::WorkbookPdfConfig;
use super::header;
use crate::chart::{Rgb, Theme};
use crate::excel::drawing::{DEFAULT_INSETS_PT, Drawn, Shape, ShapeRun, TextShape};
use crate::excel::grid::{CellKind, DEFAULT_COL_PT, Grid, GridCell, Marker, Merge, OwnFont, Sizes};
use crate::excel::styles::{Color, Line, Look, Styles, VAlign};
use crate::excel::view::{alignment, col_width, column_name, look_index, object_box, shape_svg, text_insets};
use crate::excel::{DrawnSheet, SheetBook};
use crate::model::style::{BOLD, ITALIC, SUB, SUP};
use crate::word::fmt::model::{
Align, BreakKind, FDoc, FImage, Inline, LineSpacing, Para, ParaProps, RunFmt, Underline, VertAlign,
};
use anyhow::Result;
use std::collections::{HashMap, HashSet};
const WIDE_LETTERS_FROM: u32 = 0x2E80;
const LETTER_PT: (f32, f32) = (612.0, 792.0);
const DEFAULT_FONT: &str = "Calibri";
const DEFAULT_SIZE_PT: f32 = 11.0;
const HALF_EM: f32 = 0.5;
const HALF_EM_DIGIT_FONTS: &[&str] = &[
"ms ゴシック",
"ms pゴシック",
"ms 明朝",
"ms gothic",
"ms pgothic",
"ms mincho",
];
const MIN_SCALE: f32 = 0.1;
const PT_PER_PX: f32 = 0.75;
const CELL_PADDING_PT: f32 = 1.5;
const CELL_PADDING_TOP_PT: f32 = 1.0;
const INDENT_STEP_PT: f32 = 7.5;
const HAIR_LINE_PT: f32 = 0.25;
const GRID_LINE_COLOR: Rgb = [0xBF, 0xBF, 0xBF];
const MIN_HEADING_DIGITS: usize = 3;
const HEADING_PAD_PT: f32 = 4.0;
const MAX_OVERFLOW_COLS: usize = 40;
const PAGE_EDGE_SLACK_PT: f32 = 0.01;
const LINK_SCHEMES: [&str; 4] = ["http://", "https://", "mailto:", "ftp://"];
const CJK_FAMILIES: &[&str] = &[
"Noto Sans CJK JP",
"Source Han Sans JP",
"IPAexGothic",
"IPAGothic",
"Yu Gothic",
"MS Gothic",
"Noto Serif CJK JP",
];
pub(crate) fn to_pdf(book: &SheetBook, config: &WorkbookPdfConfig, file: Option<&str>) -> Result<Vec<u8>> {
let mut fonts = FontBook::new(&[], &HashMap::new());
fonts.require_font()?;
let cjk = CJK_FAMILIES
.iter()
.find(|name| fonts.has_family(name))
.map_or_else(|| CJK_FAMILIES[0].to_string(), ToString::to_string);
let mut images: Vec<FImage> = book
.pictures
.iter()
.map(|picture| FImage {
data: picture.data.clone(),
format: picture.format,
bare: None,
texts: Vec::new(),
})
.collect();
let mut shaper = Shaper::default();
let mut coverage = HashMap::new();
let doc = FDoc::default();
let notes = HashMap::new();
let mut ctx = Ctx {
book: &mut fonts,
shaper: &mut shaper,
doc: &doc,
notes: ¬es,
own_note: None,
page: None,
page_count: None,
section_pages: None,
coverage: &mut coverage,
page_refs: None,
origin: 0.0,
};
let mut pages = Vec::new();
for sheet in &book.sheets {
let left = config.print_max_pages.saturating_sub(pages.len());
if left == 0 {
break;
}
let mut printer = Printer {
sheet,
grid: &sheet.grid,
styles: &book.styles,
theme: &book.theme,
cjk: &cjk,
images: &mut images,
ctx: &mut ctx,
file: file.unwrap_or_default(),
sizes: sheet.grid.sizes.clone(),
placing: None,
page_right: f32::INFINITY,
repeats_cols: false,
};
printer.printed_columns();
pages.extend(printer.pages(left));
}
if pages.is_empty() {
pages.push(Page {
width: LETTER_PT.0,
height: LETTER_PT.1,
items: Vec::new(),
});
}
drop(ctx);
pdf::write(pages, &[], &fonts, &images, book.title.as_deref())
}
struct Printer<'a, 'c, 'f> {
sheet: &'a DrawnSheet,
grid: &'a Grid,
styles: &'a Styles,
theme: &'a Theme,
cjk: &'a str,
images: &'a mut Vec<FImage>,
ctx: &'c mut Ctx<'f>,
file: &'a str,
sizes: Sizes,
placing: Option<Grid>,
page_right: f32,
repeats_cols: bool,
}
struct Area {
range: Merge,
xs: Vec<f32>,
ys: Vec<f32>,
rows: Vec<u32>,
cols: Vec<u32>,
}
impl Area {
fn x(&self, col: u32) -> f32 {
self.xs[(col - self.range.left) as usize]
}
fn width(&self, col: u32) -> f32 {
self.x(col + 1) - self.x(col)
}
fn y(&self, row: u32) -> f32 {
self.ys[(row - self.range.top) as usize]
}
fn height(&self, row: u32) -> f32 {
self.y(row + 1) - self.y(row)
}
}
struct Layout {
paper: (f32, f32),
origin: (f32, f32),
room: (f32, f32),
scale: f32,
titles: Vec<u32>,
titles_h: f32,
title_cols: Vec<u32>,
titles_w: f32,
headings: (f32, f32),
}
fn scale(print: &crate::excel::grid::PrintSetup, area: &Area, room: (f32, f32), headings: (f32, f32)) -> f32 {
if !print.fit_to_page {
return print.scale_percent / 100.0;
}
let total_w: f32 = area.cols.iter().map(|&col| area.width(col)).sum::<f32>() + headings.0;
let total_h: f32 = area.rows.iter().map(|&row| area.height(row)).sum::<f32>() + headings.1;
let mut scale: f32 = 1.0;
if print.fit_width > 0 && total_w > 0.0 {
scale = scale.min(print.fit_width as f32 * room.0 / total_w);
}
if print.fit_height > 0 && total_h > 0.0 {
scale = scale.min(print.fit_height as f32 * room.1 / total_h);
}
scale.max(MIN_SCALE)
}
struct Band {
rows: Vec<u32>,
sheet_top: f32,
sheet_bottom: f32,
page_top: f32,
}
struct Columns {
cols: Vec<u32>,
sheet_left: f32,
sheet_right: f32,
page_left: f32,
}
impl Printer<'_, '_, '_> {
fn printed_columns(&mut self) {
let screen_digit = self.sizes.screen_digit_pt;
let font = &self.styles.look(0).font;
let size = font.size_pt.unwrap_or(DEFAULT_SIZE_PT);
let name = font.name.as_deref().map(crate::layout::fonts::table_name);
let digit = if name.as_deref().is_some_and(|name| HALF_EM_DIGIT_FONTS.contains(&name)) {
HALF_EM * size
} else if self.styles.east_asian_default() {
return;
} else if font.name.as_deref().is_some_and(|name| self.ctx.book.has_family(name)) {
let base = self.run(self.styles.look(0), 1.0);
para::text_items(self.ctx, "0", &base, 0.0, 0.0).1
} else {
return;
};
if screen_digit <= 0.0 || digit <= 0.0 {
return;
}
let ratio = digit / screen_digit;
for (_, _, width) in &mut self.sizes.cols {
*width *= ratio;
}
self.sizes.default_col_pt = Some(self.sizes.default_col_pt.unwrap_or(DEFAULT_COL_PT) * ratio);
}
fn pages(&mut self, max: usize) -> Vec<Page> {
let Some(area) = self.area() else {
return Vec::new();
};
let print = &self.grid.print;
let (short, long) = {
let (width, height) = self.grid.paper_pt.unwrap_or(LETTER_PT);
(width.min(height), width.max(height))
};
let paper = if print.landscape { (long, short) } else { (short, long) };
let [margin_l, margin_r, margin_t, margin_b] = print.margins_pt;
let room = (
(paper.0 - margin_l - margin_r).max(paper.0 / 4.0),
(paper.1 - margin_t - margin_b).max(paper.1 / 4.0),
);
let (titles, title_cols) = self.titles(&area);
let titles_h: f32 = titles.iter().map(|&row| area.height(row)).sum();
let titles_w: f32 = title_cols.iter().map(|&col| area.width(col)).sum();
let headings = if print.headings {
self.headings(&area)
} else {
(0.0, 0.0)
};
let scale = scale(print, &area, room, headings);
let keep = |fitted: u32, breaks: &[u32]| -> HashSet<u32> {
if print.fit_to_page && fitted > 0 {
HashSet::new()
} else {
breaks.iter().copied().collect()
}
};
let col_groups = groups(
&area.cols,
|col| area.width(col),
room.0 / scale - headings.0,
&keep(print.fit_width, &print.col_breaks),
titles_w,
);
let row_groups = groups(
&area.rows,
|row| area.height(row),
room.1 / scale - headings.1,
&keep(print.fit_height, &print.row_breaks),
titles_h,
);
let layout = Layout {
paper,
origin: (margin_l, margin_t),
room,
scale,
titles,
titles_h,
title_cols,
titles_w,
headings,
};
let mut order = Vec::new();
if print.over_then_down {
for rows in &row_groups {
order.extend(col_groups.iter().map(|cols| (rows, cols)));
}
} else {
for cols in &col_groups {
order.extend(row_groups.iter().map(|rows| (rows, cols)));
}
}
let mut pages: Vec<Page> = order
.into_iter()
.map(|(rows, cols)| self.page(&area, &layout, rows, cols))
.filter(|page| !page.items.is_empty())
.take(max)
.collect();
self.headers_and_footers(&mut pages, &layout);
pages
}
fn titles(&self, area: &Area) -> (Vec<u32>, Vec<u32>) {
let within = |range: Option<(u32, u32)>, items: &[u32]| -> Vec<u32> {
range
.map(|(first, last)| {
items
.iter()
.copied()
.filter(|item| (first..=last).contains(item))
.collect()
})
.unwrap_or_default()
};
let declared = &self.grid.declared;
(
within(declared.print_title_rows, &area.rows),
within(declared.print_title_cols, &area.cols),
)
}
fn headers_and_footers(&mut self, pages: &mut [Page], layout: &Layout) {
let (date, time) = header::now();
let count = pages.len();
for (index, page) in pages.iter_mut().enumerate() {
let fields = header::Fields {
page: index + 1,
pages: count,
sheet: &self.sheet.name,
file: self.file,
date: &date,
time: &time,
};
self.header_footer(&mut page.items, layout.paper, layout.scale, &fields);
}
}
fn header_footer(&mut self, items: &mut Vec<Item>, paper: (f32, f32), scale: f32, fields: &header::Fields<'_>) {
let print = &self.grid.print;
let setup = &print.header_footer;
let (head, foot) = setup.of_page(fields.page);
let scale = if setup.scale_with_doc { scale } else { 1.0 };
let [margin_l, margin_r, ..] = print.margins_pt;
let (left, right) = if setup.align_with_margins {
(margin_l, paper.0 - margin_r)
} else {
(print.header_pt, paper.0 - print.header_pt)
};
let (header_pt, footer_pt) = (print.header_pt, print.footer_pt);
for (code, top_down) in [(head.to_string(), true), (foot.to_string(), false)] {
if code.is_empty() {
continue;
}
for (index, lines) in header::sections(&code, fields).iter().enumerate() {
let mut placed = Vec::new();
for line in lines {
placed.push(self.header_line(line, scale));
}
let total: f32 = placed.iter().map(|(_, _, height)| height).sum();
let mut top = if top_down {
header_pt
} else {
paper.1 - footer_pt - total
};
for (mut line_items, width, height) in placed {
let x = match index {
0 => left,
1 => (paper.0 - width) / 2.0,
_ => right - width,
};
translate(&mut line_items, x, top);
items.append(&mut line_items);
top += height;
}
}
}
}
fn header_line(&mut self, line: &[header::Piece], scale: f32) -> (Vec<Item>, f32, f32) {
let fmts: Vec<RunFmt> = line
.iter()
.map(|piece| {
let mut look = self.styles.look(0).clone();
let font = &mut look.font;
font.name = piece.fmt.font.clone().or(font.name.take());
font.size_pt = piece.fmt.size_pt.or(font.size_pt);
font.bold = piece.fmt.bold;
font.italic = piece.fmt.italic;
font.underline = piece.fmt.underline;
font.strike = piece.fmt.strike;
let mut fmt = self.run(&look, scale);
if piece.fmt.color.is_some() {
fmt.color = piece.fmt.color.filter(|rgb| *rgb != [0; 3]);
}
fmt
})
.collect();
let (mut ascent, mut descent) = (0.0_f32, 0.0_f32);
for fmt in &fmts {
let (above, below, _) = self.ctx.line_metrics(fmt);
ascent = ascent.max(above);
descent = descent.max(below);
}
let mut items = Vec::new();
let mut pen = 0.0;
for (piece, fmt) in line.iter().zip(&fmts) {
let (mut run, run_w) = para::text_items(self.ctx, &piece.text, fmt, pen, ascent);
items.append(&mut run);
pen += run_w;
}
(items, pen, ascent + descent)
}
fn page(&mut self, area: &Area, layout: &Layout, rows: &[u32], cols: &[u32]) -> Page {
let print = &self.grid.print;
let scale = layout.scale;
let repeat = layout
.titles
.last()
.is_some_and(|last| rows.first().is_some_and(|first| first > last));
let repeat_cols = layout
.title_cols
.last()
.is_some_and(|last| cols.first().is_some_and(|first| first > last));
let (heading_w, heading_h) = layout.headings;
let repeated_w = if repeat_cols { layout.titles_w } else { 0.0 } + heading_w;
let content_w: f32 = (cols.iter().map(|&col| area.width(col)).sum::<f32>() + repeated_w) * scale;
let repeated_h = if repeat { layout.titles_h } else { 0.0 } + heading_h;
let content_h: f32 = (rows.iter().map(|&row| area.height(row)).sum::<f32>() + repeated_h) * scale;
let centred = |on: bool, room: f32, content: f32| if on { (room - content).max(0.0) / 2.0 } else { 0.0 };
let heading_left = layout.origin.0 + centred(print.h_centered, layout.room.0, content_w);
let heading_top = layout.origin.1 + centred(print.v_centered, layout.room.1, content_h);
let left = heading_left + heading_w * scale;
let top = heading_top + heading_h * scale;
let columns_of = |cols: &[u32], page_left: f32| Columns {
cols: cols.to_vec(),
sheet_left: area.x(cols[0]),
sheet_right: area.x(*cols.last().unwrap_or(&cols[0]) + 1),
page_left,
};
let columns = columns_of(cols, left + (repeated_w - heading_w) * scale);
self.repeats_cols = repeat_cols;
let mut bands = Vec::new();
let mut page_top = top;
if repeat {
bands.push(band(area, &layout.titles, page_top));
page_top += layout.titles_h * scale;
}
bands.push(band(area, rows, page_top));
let mut page = Page {
width: layout.paper.0,
height: layout.paper.1,
items: Vec::new(),
};
let mut all_columns = Vec::new();
if repeat_cols {
let titles = columns_of(&layout.title_cols, left);
self.draw_page(&mut page.items, area, &titles, &bands, scale, repeat);
all_columns.push(titles);
}
self.draw_page(&mut page.items, area, &columns, &bands, scale, repeat);
all_columns.push(columns);
if heading_w > 0.0 {
self.draw_headings(
&mut page.items,
area,
(&all_columns, &bands),
(heading_left, heading_top),
scale,
);
}
page
}
fn headings(&self, area: &Area) -> (f32, f32) {
let last = area.rows.last().copied().unwrap_or(0) + 1;
let digits = last.to_string().len().max(MIN_HEADING_DIGITS);
let digit = if self.sizes.screen_digit_pt > 0.0 {
self.sizes.screen_digit_pt
} else {
HALF_EM * DEFAULT_SIZE_PT
};
(
digits as f32 * digit + HEADING_PAD_PT,
self.sizes.default_row_pt.unwrap_or(DEFAULT_ROW_PT),
)
}
fn draw_headings(
&mut self,
items: &mut Vec<Item>,
area: &Area,
(columns, bands): (&[Columns], &[Band]),
(left, top): (f32, f32),
scale: f32,
) {
let Some(first) = columns.first() else { return };
let width = first.page_left - left;
let height = bands.first().map_or(0.0, |band| band.page_top) - top;
let base = self.run(self.styles.look(0), scale);
let (ascent, descent, _) = self.ctx.line_metrics(&base);
let mut boxes = vec![([left, top, width, height], String::new())];
for group in columns {
for &col in &group.cols {
let x = group.page_left + (area.x(col) - group.sheet_left) * scale;
boxes.push(([x, top, area.width(col) * scale, height], column_name(col)));
}
}
for band in bands {
for &row in &band.rows {
let y = band.page_top + (area.y(row) - band.sheet_top) * scale;
boxes.push(([left, y, width, area.height(row) * scale], (row + 1).to_string()));
}
}
for ([x, y, box_w, box_h], label) in boxes {
if box_w <= 0.0 || box_h <= 0.0 {
continue;
}
outline(items, [x, y, box_w, box_h], scale);
let (mut run, run_w) = para::text_items(self.ctx, &label, &base, 0.0, ascent);
translate(
&mut run,
x + (box_w - run_w) / 2.0,
y + (box_h - ascent - descent) / 2.0,
);
items.append(&mut run);
}
}
fn area(&mut self) -> Option<Area> {
let grid = self.grid;
let range = match self.sheet.print_area {
Some(range) => range,
None => {
let used = self.used_range()?;
Merge {
right: self.spill_right(used.right),
..used
}
}
};
let bottom = range.bottom.min(range.top.saturating_add(MAX_PRINT_ROWS));
let right = range.right.min(range.left.saturating_add(MAX_PRINT_COLS));
let range = Merge { bottom, right, ..range };
let origin = Marker {
row: range.top,
row_off_pt: 0.0,
col: range.left,
col_off_pt: 0.0,
};
let start = self.sizes.span_pt(
Marker {
row: 0,
col: 0,
..origin
},
origin,
&grid.hidden_rows,
&grid.hidden_cols,
);
let mut xs = vec![start.0];
for col in range.left..=range.right {
let width = if grid.hidden_cols.contains(&col) {
0.0
} else {
col_width(&self.sizes, col)
};
xs.push(xs[xs.len() - 1] + width);
}
let ys = self.row_edges(range, &xs, start.1);
let rows: Vec<u32> = (range.top..=range.bottom)
.filter(|row| !grid.hidden_rows.contains(row))
.collect();
let cols: Vec<u32> = (range.left..=range.right)
.filter(|col| !grid.hidden_cols.contains(col))
.collect();
(!rows.is_empty() && !cols.is_empty()).then_some(Area {
range,
xs,
ys,
rows,
cols,
})
}
fn row_edges(&mut self, range: Merge, xs: &[f32], top: f32) -> Vec<f32> {
let grid = self.grid;
let default_row = grid.sizes.default_row_pt.unwrap_or(DEFAULT_ROW_PT);
let mut ys = vec![top];
let mut fitted_rows = Vec::new();
for row in range.top..=range.bottom {
let height = if grid.hidden_rows.contains(&row) {
0.0
} else {
let height = grid.sizes.rows.get(&row).copied().unwrap_or(default_row);
if grid.looks.custom_rows.contains(&row) {
height
} else {
let fitted = self.fitted_height(row, range, xs, height);
if fitted > height {
fitted_rows.push((row, fitted));
}
fitted
}
};
ys.push(ys[ys.len() - 1] + height);
}
if !fitted_rows.is_empty() || self.sizes != grid.sizes {
let mut sizes = self.sizes.clone();
sizes.rows.extend(fitted_rows);
self.placing = Some(Grid {
sizes,
hidden_rows: grid.hidden_rows.clone(),
hidden_cols: grid.hidden_cols.clone(),
..Grid::default()
});
}
ys
}
fn fitted_height(&mut self, row: u32, range: Merge, xs: &[f32], height: f32) -> f32 {
let grid = self.grid;
let base_size = self.styles.look(0).font.size_pt.unwrap_or(DEFAULT_SIZE_PT);
let base_line = grid.sizes.default_row_pt.unwrap_or(DEFAULT_ROW_PT) / base_size;
let mut fitted = height;
for (&(_, col), cell) in grid.cells.range((row, range.left)..=(row, range.right)) {
let look = self.styles.look(look_index(grid, (row, col)) as usize);
if !(look.wraps())
|| look.stacked
|| look.rotation.is_some()
|| grid
.merges
.iter()
.any(|merge| (merge.top..=merge.bottom).contains(&row) && (merge.left..=merge.right).contains(&col))
{
continue;
}
let index = (col - range.left) as usize;
let width = xs[index + 1] - xs[index];
let base = self.run(look, 1.0);
let indent = f32::from(look.indent) * INDENT_STEP_PT;
let inner_w = (width - 2.0 * CELL_PADDING_PT - indent).max(1.0);
let align = alignment(look.h_align, Some(cell.kind));
let lines = para::layout(self.ctx, &self.wrapped_para(cell, &base, align), inner_w)
.lines
.len();
let line = base_line * look.font.size_pt.unwrap_or(base_size);
#[expect(clippy::cast_precision_loss, reason = "a cell's lines are few")]
let lacking = lines as f32 - (height / line).round().max(1.0);
if lacking > 0.0 {
fitted = fitted.max(height + lacking * line);
}
}
fitted
}
fn spill_right(&mut self, right: u32) -> u32 {
let mut last: HashMap<u32, (u32, &GridCell)> = HashMap::new();
for (&(row, col), cell) in &self.grid.cells {
last.insert(row, (col, cell));
}
let mut spill = right;
for (row, (col, cell)) in last {
let look = self.styles.look(look_index(self.grid, (row, col)) as usize);
let in_merge =
self.grid.merges.iter().any(|merge| {
(merge.top..=merge.bottom).contains(&row) && (merge.left..=merge.right).contains(&col)
});
if cell.kind != CellKind::Text
|| look.wrap
|| in_merge
|| !matches!(alignment(look.h_align, Some(cell.kind)), "left" | "center")
{
continue;
}
let base = self.run(look, 1.0);
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let (_, text_w) = para::text_items(self.ctx, &text.replace(['\n', '\r', '\t'], " "), &base, 0.0, 0.0);
let indent = f32::from(look.indent) * INDENT_STEP_PT;
let mut room = col_width(&self.sizes, col) - 2.0 * CELL_PADDING_PT - indent;
let mut next = col;
while room < text_w && next < col.saturating_add(MAX_OVERFLOW_COLS as u32) {
next += 1;
if !self.grid.hidden_cols.contains(&next) {
room += col_width(&self.sizes, next);
}
}
spill = spill.max(next);
}
spill
}
fn used_range(&self) -> Option<Merge> {
let grid = self.grid;
let mut last: Option<(u32, u32)> = None;
let mut extend = |(row, col): (u32, u32)| {
let (last_row, last_col) = last.unwrap_or((0, 0));
last = Some((last_row.max(row), last_col.max(col)));
};
grid.cells.keys().copied().for_each(&mut extend);
for (&at, &style) in &grid.looks.cells {
let look = self.styles.look(style as usize);
if look.fill.is_some() || look.borders.iter().any(Option::is_some) {
extend(at);
}
}
for merge in &grid.merges {
extend((merge.bottom, merge.right));
}
for shape in &self.sheet.shapes {
if let Some(marker) = shape.frame.to.or(shape.frame.from) {
extend((marker.row, marker.col));
}
}
let (bottom, right) = last?;
Some(Merge {
top: 0,
left: 0,
bottom,
right,
})
}
fn draw_page(
&mut self,
items: &mut Vec<Item>,
area: &Area,
columns: &Columns,
bands: &[Band],
scale: f32,
repeat: bool,
) {
crate::common::cancel::check();
let mut texts = Vec::new();
let mut lines = Vec::new();
self.page_right = columns.page_left + (columns.sheet_right - columns.sheet_left) * scale;
for band in bands {
let cells = self.band_cells(area, columns, band);
for (at, rect, span) in &cells {
let own = self.styles.look(look_index(self.grid, *at) as usize);
let edged;
let look = match span {
Some(merge) => {
edged = Look {
borders: [
own.borders[0],
self.styles
.look(look_index(self.grid, (merge.top, merge.right)) as usize)
.borders[1],
own.borders[2],
self.styles
.look(look_index(self.grid, (merge.bottom, merge.left)) as usize)
.borders[3],
],
..own.clone()
};
&edged
}
None => own,
};
let rect = self.to_page(*rect, columns, band, scale);
let region = span.map(|_| page_region(columns, band, scale));
let keep = |item: Item| match region {
Some(region) => cut(item, region),
None => Some(item),
};
if let Some(fill) = look.fill.and_then(|color| self.cell_rgb(color)) {
items.extend(keep(Item::Rect {
x: rect[0],
y: rect[1],
width: rect[2],
height: rect[3],
color: fill,
}));
}
items.extend(self.data_bar(*at, rect, scale).and_then(keep));
let mut edges = Vec::new();
if self.grid.print.grid_lines {
grid_lines(&mut edges, rect, scale);
}
self.borders(&mut edges, look, rect, scale);
lines.extend(edges.into_iter().filter_map(keep));
let position = columns.cols.iter().position(|col| *col == at.1).unwrap_or(0);
let mut text_rect = rect;
text_rect[2] += self
.grid
.selection_width(*at, &columns.cols[position + 1..], |col| area.width(col))
* scale;
let clip = self.clip(area, columns, band, *at, *span, rect, scale, look);
texts.extend(self.cell_text(*at, look, text_rect, clip, scale));
}
texts.extend(self.spilled_in(area, columns, band, scale));
}
items.append(&mut lines);
items.append(&mut texts);
let Some(band) = bands.get(usize::from(repeat)) else {
return;
};
let region = [
columns.page_left,
band.page_top,
columns.page_left + (columns.sheet_right - columns.sheet_left) * scale,
band.page_top + (band.sheet_bottom - band.sheet_top) * scale,
];
for shape in &self.sheet.shapes {
self.shape(items, shape, columns, band, scale, region);
}
}
fn spilled_in(&mut self, area: &Area, columns: &Columns, band: &Band, scale: f32) -> Vec<Item> {
let Some(&first) = columns.cols.first() else {
return Vec::new();
};
let before = area.cols.iter().position(|&col| col == first).unwrap_or(0);
let page_left = columns.page_left;
let titles = self.grid.declared.print_title_cols.filter(|_| self.repeats_cols);
let mut items = Vec::new();
for &row in &band.rows {
let free = |col: u32| self.grid.is_blank((row, col)) && !self.grid.merged((row, col));
let Some(&from) = area.cols[..before]
.iter()
.rev()
.take(MAX_OVERFLOW_COLS)
.find(|&&col| !free(col))
else {
continue;
};
if titles.is_some_and(|(first, last)| (first..=last).contains(&from)) {
continue;
}
let at = (row, from);
let look = self.styles.look(look_index(self.grid, at) as usize);
let cell = self.grid.cells.get(&at);
let left_aligned = alignment(look.h_align, cell.map(|cell| cell.kind)) == "left";
let runs_over = left_aligned
&& !look.wrap
&& !self.grid.merged(at)
&& !cell.is_some_and(|cell| cell.center_continuous)
&& cell.is_some_and(|cell| cell.kind == CellKind::Text);
if !runs_over || !free(first) {
continue;
}
let reach: f32 = columns
.cols
.iter()
.take_while(|&&col| free(col))
.take(MAX_OVERFLOW_COLS)
.map(|&col| area.width(col))
.sum();
let rect = self.to_page(
[area.x(from), area.y(row), area.width(from), area.height(row)],
columns,
band,
scale,
);
let clip = [page_left, rect[1], page_left + reach * scale, rect[1] + rect[3]];
let look = look.clone();
items.extend(self.cell_text(at, &look, rect, clip, scale));
}
items
}
#[allow(clippy::type_complexity)]
fn band_cells(&self, area: &Area, columns: &Columns, band: &Band) -> Vec<((u32, u32), [f32; 4], Option<Merge>)> {
let first_col = columns.cols[0];
let last_col = *columns.cols.last().unwrap_or(&first_col);
let (Some(&first_row), Some(&last_row)) = (band.rows.first(), band.rows.last()) else {
return Vec::new();
};
let page = Merge {
top: first_row,
left: first_col,
bottom: last_row,
right: last_col,
};
let mut covered = HashSet::new();
let mut cells = Vec::new();
for merge in self.grid.merges.iter().filter(|merge| merge.overlaps(&page)) {
let merge = Merge {
bottom: merge.bottom.min(area.range.bottom),
right: merge.right.min(area.range.right),
..*merge
};
if merge.top < area.range.top || merge.left < area.range.left {
continue;
}
for row in merge.top.max(first_row)..=merge.bottom.min(last_row) {
for col in merge.left.max(first_col)..=merge.right.min(last_col) {
covered.insert((row, col));
}
}
let rect = [
area.x(merge.left),
area.y(merge.top),
area.x(merge.right + 1) - area.x(merge.left),
area.y(merge.bottom + 1) - area.y(merge.top),
];
cells.push(((merge.top, merge.left), rect, Some(merge)));
}
for &row in &band.rows {
for &col in &columns.cols {
if covered.contains(&(row, col)) {
continue;
}
let rect = [area.x(col), area.y(row), area.width(col), area.height(row)];
cells.push(((row, col), rect, None));
}
}
cells
}
fn to_page(&self, [x, y, width, height]: [f32; 4], columns: &Columns, band: &Band, scale: f32) -> [f32; 4] {
[
columns.page_left + (x - columns.sheet_left) * scale,
band.page_top + (y - band.sheet_top) * scale,
width * scale,
height * scale,
]
}
#[allow(clippy::too_many_arguments)]
fn clip(
&self,
area: &Area,
columns: &Columns,
band: &Band,
at: (u32, u32),
span: Option<Merge>,
rect: [f32; 4],
scale: f32,
look: &Look,
) -> [f32; 4] {
let band_top = band.page_top;
let band_bottom = band.page_top + (band.sheet_bottom - band.sheet_top) * scale;
let page_left = columns.page_left;
let page_right = columns.page_left + (columns.sheet_right - columns.sheet_left) * scale;
let mut left = rect[0];
let mut right = rect[0] + rect[2];
let cell = self.grid.cells.get(&at);
let position = columns.cols.iter().position(|&col| col == at.1).unwrap_or(0);
let selection = self
.grid
.selection_width(at, &columns.cols[position + 1..], |col| area.width(col));
right += selection * scale;
let runs_over =
span.is_none() && selection == 0.0 && !look.wrap && cell.is_some_and(|cell| cell.kind == CellKind::Text);
if runs_over {
let free = |col: u32| self.grid.is_blank((at.0, col)) && !self.grid.merged((at.0, col));
let align = alignment(look.h_align, cell.map(|cell| cell.kind));
if matches!(align, "left" | "center") {
for &col in columns.cols[position + 1..].iter().take(MAX_OVERFLOW_COLS) {
if !free(col) {
break;
}
right += area.width(col) * scale;
}
}
if matches!(align, "right" | "center") {
for &col in columns.cols[..position].iter().rev().take(MAX_OVERFLOW_COLS) {
if !free(col) {
break;
}
left -= area.width(col) * scale;
}
}
}
let top = rect[1].max(band_top);
let bottom = (rect[1] + rect[3]).min(band_bottom);
[left.max(page_left), top, right.min(page_right), bottom]
}
fn data_bar(&self, at: (u32, u32), rect: [f32; 4], scale: f32) -> Option<Item> {
let bar = self.grid.looks.bars.get(&at)?;
let color = self.cell_rgb(bar.color)?;
let inset = DATA_BAR_INSET_PT * scale;
Some(Item::Rect {
x: rect[0] + inset / 2.0,
y: rect[1] + inset,
width: ((rect[2] - inset) * bar.length).max(0.0),
height: (rect[3] - 2.0 * inset).max(0.0),
color: color.map(|channel| channel + (u8::MAX - channel) / 4),
})
}
fn borders(&self, lines: &mut Vec<Item>, look: &Look, [x, y, width, height]: [f32; 4], scale: f32) {
let sides = [
(x, y, x, y + height),
(x + width, y, x + width, y + height),
(x, y, x + width, y),
(x, y + height, x + width, y + height),
];
for ((x1, y1, x2, y2), edge) in sides.into_iter().zip(look.borders) {
let Some(edge) = edge else { continue };
let color = edge.color.and_then(|color| self.cell_rgb(color)).unwrap_or([0; 3]);
let line_w = if edge.line == Line::Hair {
HAIR_LINE_PT
} else {
(edge.width_px * PT_PER_PX * scale.max(0.5)).max(HAIR_LINE_PT)
};
let dash = match edge.line {
Line::Dashed => Some(line_w * 3.0),
Line::Dotted => Some(line_w),
Line::Solid | Line::Hair | Line::Double => None,
};
if edge.line == Line::Double {
let thin = line_w / 3.0;
let (dx, dy) = if x1 == x2 { (thin, 0.0) } else { (0.0, thin) };
for sign in [-1.0, 1.0] {
lines.push(Item::Line {
x1: x1 + sign * dx,
y1: y1 + sign * dy,
x2: x2 + sign * dx,
y2: y2 + sign * dy,
width: thin,
color,
dash: None,
});
}
continue;
}
lines.push(Item::Line {
x1,
y1,
x2,
y2,
width: line_w,
color,
dash,
});
}
}
fn cell_text(&mut self, at: (u32, u32), look: &Look, rect: [f32; 4], clip: [f32; 4], scale: f32) -> Vec<Item> {
let Some(cell) = self.grid.cells.get(&at) else {
return Vec::new();
};
if cell.spans.iter().all(|span| span.text.trim().is_empty()) {
return Vec::new();
}
let base = self.run(look, scale);
let pad = CELL_PADDING_PT * scale;
let pad_top = CELL_PADDING_TOP_PT * scale;
let indent = f32::from(look.indent) * INDENT_STEP_PT * scale;
let align = alignment(look.h_align, Some(cell.kind));
let [x, y, width, height] = rect;
let inner_w = (width - 2.0 * pad - indent).max(1.0);
let (ascent, descent, _) = self.ctx.line_metrics(&base);
let (mut items, text_h) = if look.stacked || look.rotation.is_some() {
let room = if look.stacked && look.wrap {
height - 2.0 * pad_top
} else {
f32::INFINITY
};
self.stacked_text(cell, &base, (width, room), (ascent, descent))
} else if let Some(filled) = self.filled_text(at, cell, &base, (pad + indent, width - pad - indent), ascent) {
(filled, ascent + descent)
} else if align == "distributed" {
self.distributed_text(cell, &base, (pad + indent, inner_w - indent), (ascent, descent))
} else if look.wraps() {
self.wrapped_text(cell, &base, align, pad, indent, inner_w)
} else {
let mut items = Vec::new();
let mut pen = 0.0;
for (index, span) in cell.spans.iter().enumerate() {
let fmt = self.span_run(&base, span, cell.own_font(index));
let text = span.text.replace(['\n', '\r', '\t'], " ");
let (mut run, run_w) = para::text_items(self.ctx, &text, &fmt, pen, ascent);
items.append(&mut run);
pen += run_w;
}
let start = match align {
"right" => width - pad - indent - pen,
"center" => (width - pen) / 2.0,
_ => pad + indent,
};
translate(&mut items, start, 0.0);
(items, ascent + descent)
};
let top = match look.v_align {
VAlign::Top => pad_top,
VAlign::Middle => (height - text_h) / 2.0,
VAlign::Bottom => height - pad_top - text_h,
};
translate(&mut items, x, y + top);
let at_page_edge = (clip[2] - self.page_right).abs() < PAGE_EDGE_SLACK_PT;
clip_items(items, clip, at_page_edge)
}
fn filled_text(
&mut self,
at: (u32, u32),
cell: &GridCell,
base: &RunFmt,
(left, right): (f32, f32),
ascent: f32,
) -> Option<Vec<Item>> {
let fill = *self.grid.looks.fills.get(&at)?;
let [span] = cell.spans.as_slice() else {
return None;
};
let (before, after) = (span.text.get(..fill)?, span.text.get(fill..)?.trim_start());
let fmt = self.span_run(base, span, cell.own_font(0));
let (mut items, _) = para::text_items(self.ctx, before, &fmt, 0.0, ascent);
let (mut tail, tail_w) = para::text_items(self.ctx, after, &fmt, 0.0, ascent);
translate(&mut items, left, 0.0);
translate(&mut tail, right - tail_w, 0.0);
items.append(&mut tail);
Some(items)
}
fn stacked_text(
&mut self,
cell: &GridCell,
base: &RunFmt,
(width, room): (f32, f32),
metrics: (f32, f32),
) -> (Vec<Item>, f32) {
let (ascent, descent) = metrics;
let pitch = ascent + descent;
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let columns = if room.is_finite() {
stacked_columns(&text, ((room / pitch).floor() as usize).max(1))
} else {
vec![text.chars().filter(|ch| !ch.is_whitespace()).collect()]
};
let mut items = Vec::new();
let mut text_h: f32 = 0.0;
let first_x = (width + (columns.len() as f32 - 1.0) * pitch) / 2.0;
for (number, column) in columns.iter().enumerate() {
let middle = first_x - number as f32 * pitch;
let mut line_y = ascent;
for ch in column {
if !ch.is_whitespace() {
let (mut run, run_w) = para::text_items(self.ctx, &ch.to_string(), base, 0.0, line_y);
translate(&mut run, middle - run_w / 2.0, 0.0);
items.append(&mut run);
}
line_y += pitch;
}
text_h = text_h.max(line_y - ascent);
}
(items, text_h)
}
fn distributed_text(
&mut self,
cell: &GridCell,
base: &RunFmt,
(left, room): (f32, f32),
(ascent, descent): (f32, f32),
) -> (Vec<Item>, f32) {
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let mut items = Vec::new();
let mut line_top = 0.0;
for paragraph in text.trim().split('\n') {
let runs: Vec<(Vec<Item>, f32, bool)> = distributed_units(paragraph)
.iter()
.map(|unit| {
let (run, run_w) = para::text_items(self.ctx, unit, base, 0.0, ascent);
(run, run_w, unit.trim().is_empty())
})
.collect();
for line in fill_lines(runs, room) {
let used: f32 = line.iter().map(|(_, run_w, _)| run_w).sum();
let gap = if line.len() > 1 {
(room - used) / (line.len() - 1) as f32
} else {
0.0
};
let mut pen = if line.len() > 1 {
left
} else {
left + (room - used) / 2.0
};
for (mut run, run_w, _) in line {
translate(&mut run, pen, line_top);
items.append(&mut run);
pen += run_w + gap;
}
line_top += ascent + descent;
}
}
(items, line_top.max(ascent + descent))
}
fn wrapped_text(
&mut self,
cell: &GridCell,
base: &RunFmt,
align: &str,
pad: f32,
indent: f32,
inner_w: f32,
) -> (Vec<Item>, f32) {
let para = self.wrapped_para(cell, base, align);
let laid = para::layout(self.ctx, ¶, inner_w);
let mut items = Vec::new();
let mut line_top = 0.0;
let left = pad + if align == "right" { 0.0 } else { indent };
for mut line in laid.lines {
translate(&mut line.items, left, line_top);
items.append(&mut line.items);
line_top += line.height;
}
(items, line_top)
}
fn wrapped_para(&self, cell: &GridCell, base: &RunFmt, align: &str) -> Para {
Para {
props: ParaProps {
align: match align {
"center" => Align::Center,
"right" => Align::Right,
"justify" => Align::Both,
"distributed" => Align::Distribute,
_ => Align::Left,
},
line: LineSpacing::Auto(1.0),
..Default::default()
},
mark: base.clone(),
inlines: self.inlines(cell, base),
..Default::default()
}
}
fn inlines(&self, cell: &GridCell, base: &RunFmt) -> Vec<Inline> {
let mut inlines = Vec::new();
for (index, span) in cell.spans.iter().enumerate() {
let fmt = self.span_run(base, span, cell.own_font(index));
for (index, line) in span.text.split('\n').enumerate() {
if index > 0 {
inlines.push(Inline::Break {
kind: BreakKind::Line,
fmt: fmt.clone(),
});
}
let line = line.replace(['\t', '\r'], " ");
if !line.is_empty() {
inlines.push(Inline::Text {
text: line,
fmt: fmt.clone(),
});
}
}
}
inlines
}
fn run(&self, look: &Look, scale: f32) -> RunFmt {
let base = &self.styles.look(0).font;
let font = &look.font;
let name = font
.name
.clone()
.or_else(|| base.name.clone())
.unwrap_or_else(|| DEFAULT_FONT.to_string());
let latin = self.ctx.book.latin_stand_in(&name);
let east_asia = if name.is_ascii() && latin.is_none() {
self.cjk.to_string()
} else {
name.clone()
};
let western = latin.map_or_else(|| name.clone(), ToString::to_string);
let size = font.size_pt.or(base.size_pt).unwrap_or(DEFAULT_SIZE_PT) * scale;
RunFmt {
font: western.clone(),
font_hansi: western.clone(),
font_east_asia: east_asia,
font_cs: western,
size,
size_cs: size,
bold: font.bold,
italic: font.italic,
underline: font.underline.then_some(Underline::Single),
strike: font.strike,
color: font
.color
.and_then(|color| self.cell_rgb(color))
.filter(|rgb| *rgb != [0; 3]),
..RunFmt::default()
}
}
fn span_run(&self, base: &RunFmt, span: &crate::model::Span, own: OwnFont) -> RunFmt {
let mut fmt = base.clone();
if own.style {
fmt.bold = span.style & BOLD != 0;
fmt.italic = span.style & ITALIC != 0;
fmt.underline = own.underline.then_some(Underline::Single);
} else {
fmt.bold |= span.style & BOLD != 0;
fmt.italic |= span.style & ITALIC != 0;
}
if span.style & SUP != 0 {
fmt.vert = VertAlign::Super;
} else if span.style & SUB != 0 {
fmt.vert = VertAlign::Sub;
}
if self.grid.print.black_and_white {
fmt.color = None;
} else if span.color != [0; 3] {
fmt.color = Some(span.color);
} else if own.color {
fmt.color = None;
}
fmt.href = span
.href
.clone()
.filter(|href| LINK_SCHEMES.iter().any(|scheme| href.starts_with(scheme)));
fmt
}
fn shape(
&mut self,
items: &mut Vec<Item>,
shape: &Shape,
columns: &Columns,
band: &Band,
scale: f32,
region: [f32; 4],
) {
let Some(sheet_box) = object_box(self.placing.as_ref().unwrap_or(self.grid), shape) else {
return;
};
let [x, y, width, height] = self.to_page(sheet_box, columns, band, scale);
if width < 0.0 || height < 0.0 {
return;
}
match &shape.drawn {
Drawn::Picture(picture) => {
if let Some(index) = picture.index {
items.extend(image(index, [x, y, width, height], region));
}
}
Drawn::CellPicture(picture) => {
let Some(index) = picture.index else { return };
let (own_w, own_h) = picture.size_pt.unwrap_or((width, height));
let fit = (width / own_w.max(1.0)).min(height / own_h.max(1.0));
let (fit_w, fit_h) = (own_w * fit, own_h * fit);
let placed = [x + (width - fit_w) / 2.0, y + (height - fit_h) / 2.0, fit_w, fit_h];
items.extend(image(index, placed, region));
}
Drawn::Chart(chart) => {
if let Some((_, Some(index))) = chart.placed.as_deref() {
items.extend(image(*index, [x, y, width, height], region));
}
}
Drawn::Text(text) => {
if let Some((svg, [dx, dy, svg_w, svg_h])) = shape_svg(text, width / scale, height / scale) {
let index = self.images.len();
self.images.push(FImage {
data: svg.into_bytes(),
format: crate::model::PictureFormat::Svg,
bare: None,
texts: Vec::new(),
});
let placed = [x + dx * scale, y + dy * scale, svg_w * scale, svg_h * scale];
items.extend(image(index, placed, region));
}
items.extend(self.shape_text(text, [x, y, width, height], scale, region));
}
}
}
fn shape_text(&mut self, shape: &TextShape, rect: [f32; 4], scale: f32, region: [f32; 4]) -> Vec<Item> {
if shape.text.is_empty() {
return Vec::new();
}
let mut fmt = self.run(self.styles.look(0), scale);
if let Some(size) = shape.size_pt {
fmt.size = size * scale;
fmt.size_cs = fmt.size;
}
fmt.bold = shape.bold;
fmt.color = shape.color.filter(|rgb| *rgb != [0; 3]);
let [x, y, width, height] = rect;
let [left, top_inset, right, bottom_inset] = text_insets(shape, width / scale, height / scale)
.unwrap_or(DEFAULT_INSETS_PT)
.map(|inset| inset * scale);
let inlines = shape_inlines(shape, &fmt, scale);
let align = match shape.align.as_deref() {
Some("ctr") => Align::Center,
Some("r") => Align::Right,
Some("just" | "dist") => Align::Both,
_ => Align::Left,
};
let para = Para {
props: ParaProps {
align: if shape.nowrap { Align::Left } else { align },
line: LineSpacing::Auto(1.0),
..Default::default()
},
mark: fmt,
inlines,
..Default::default()
};
let inner = (width - left - right).max(1.0);
let laid = para::layout(self.ctx, ¶, if shape.nowrap { NOWRAP_WIDTH_PT } else { inner });
let mut items = Vec::new();
let mut line_top = 0.0;
for mut line in laid.lines {
let shift = if shape.nowrap {
let end = line_end(&line.items);
match align {
Align::Center => (inner - end) / 2.0,
Align::Right => inner - end,
_ => 0.0,
}
} else {
0.0
};
translate(&mut line.items, shift, line_top);
items.append(&mut line.items);
line_top += line.height;
}
let top = match shape.anchor.as_deref() {
Some("ctr") => (height - line_top) / 2.0,
Some("b") => height - bottom_inset - line_top,
_ => top_inset,
};
translate(&mut items, x + left, y + top);
let [clip_top, clip_bottom] = if shape.clip {
items.retain(|item| !matches!(item, Item::Text(run) if run.y > y + height));
[region[1].max(y), region[3].min(y + height)]
} else {
[region[1], region[3]]
};
clip_items(items, [region[0], clip_top, region[2], clip_bottom], false)
}
fn rgb(&self, color: Color) -> Option<Rgb> {
color.rgb(self.theme, self.styles.palette.as_deref())
}
fn cell_rgb(&self, color: Color) -> Option<Rgb> {
if self.grid.print.black_and_white {
None
} else {
self.rgb(color)
}
}
}
const NOWRAP_WIDTH_PT: f32 = 100_000.0;
fn line_end(items: &[Item]) -> f32 {
items
.iter()
.filter_map(|item| match item {
Item::Text(run) => run.glyphs.last().map(|glyph| run.x + glyph.x + glyph.advance),
_ => None,
})
.fold(0.0, f32::max)
}
fn shape_inlines(shape: &TextShape, fmt: &RunFmt, scale: f32) -> Vec<Inline> {
let plain;
let runs = if shape.runs.is_empty() {
plain = [ShapeRun {
text: shape.text.clone(),
..ShapeRun::default()
}];
&plain[..]
} else {
&shape.runs[..]
};
let mut inlines = Vec::new();
for run in runs {
let mut own = fmt.clone();
own.bold = run.bold.unwrap_or(own.bold);
own.italic = run.italic.unwrap_or(own.italic);
if run.underline == Some(true) {
own.underline = Some(Underline::Single);
}
if let Some(size) = run.size_pt {
own.size = size * scale;
own.size_cs = own.size;
}
if let Some(color) = run.color {
own.color = Some(color).filter(|rgb| *rgb != [0; 3]);
}
for (index, line) in run.text.split('\n').enumerate() {
if index > 0 {
inlines.push(Inline::Break {
kind: BreakKind::Line,
fmt: own.clone(),
});
}
if !line.is_empty() {
inlines.push(Inline::Text {
text: line.to_string(),
fmt: own.clone(),
});
}
}
}
inlines
}
const DEFAULT_ROW_PT: f32 = 15.0;
const MAX_PRINT_ROWS: u32 = 1_048_575;
const MAX_PRINT_COLS: u32 = 16_383;
fn band(area: &Area, rows: &[u32], page_top: f32) -> Band {
let first = rows.first().copied().unwrap_or(area.range.top);
let last = rows.last().copied().unwrap_or(first);
Band {
rows: rows.to_vec(),
sheet_top: area.y(first),
sheet_bottom: area.y(last + 1),
page_top,
}
}
fn groups(items: &[u32], size: impl Fn(u32) -> f32, room: f32, breaks: &HashSet<u32>, repeated: f32) -> Vec<Vec<u32>> {
let mut groups: Vec<Vec<u32>> = Vec::new();
let mut current: Vec<u32> = Vec::new();
let mut used = 0.0;
for &item in items {
let item_size = size(item);
let room_here = if groups.is_empty() { room } else { room - repeated };
let slack = (room_here * ROOM_SLACK_RATIO).max(MIN_ROOM_SLACK_PT);
let full = !current.is_empty() && (used + item_size > room_here + slack || breaks.contains(&item));
if full {
groups.push(std::mem::take(&mut current));
used = 0.0;
}
current.push(item);
used += item_size;
}
if !current.is_empty() {
groups.push(current);
}
groups
}
const ROOM_SLACK_RATIO: f32 = 0.005;
const MIN_ROOM_SLACK_PT: f32 = 0.5;
fn stacked_columns(text: &str, per_column: usize) -> Vec<Vec<char>> {
let mut columns = Vec::new();
let mut column: Vec<char> = Vec::new();
for ch in text.chars() {
if ch == '\n' {
columns.push(std::mem::take(&mut column));
} else if ch.is_whitespace() {
if column.len() < per_column {
column.push(ch);
}
} else {
if column.len() >= per_column {
columns.push(std::mem::take(&mut column));
}
column.push(ch);
}
}
columns.push(column);
for column in &mut columns {
while column.last().is_some_and(|ch| ch.is_whitespace()) {
column.pop();
}
}
columns
}
fn page_region(columns: &Columns, band: &Band, scale: f32) -> [f32; 4] {
[
columns.page_left,
band.page_top,
columns.page_left + (columns.sheet_right - columns.sheet_left) * scale,
band.page_top + (band.sheet_bottom - band.sheet_top) * scale,
]
}
fn cut(item: Item, [left, top, right, bottom]: [f32; 4]) -> Option<Item> {
let outside = |low: f32, high: f32, from: f32, to: f32| high < from - CUT_SLACK_PT || low > to + CUT_SLACK_PT;
match item {
Item::Line {
x1,
y1,
x2,
y2,
width,
color,
dash,
} => {
if outside(x1.min(x2), x1.max(x2), left, right) || outside(y1.min(y2), y1.max(y2), top, bottom) {
return None;
}
Some(Item::Line {
x1: x1.clamp(left, right),
y1: y1.clamp(top, bottom),
x2: x2.clamp(left, right),
y2: y2.clamp(top, bottom),
width,
color,
dash,
})
}
Item::Rect {
x,
y,
width,
height,
color,
} => {
let (x0, y0) = (x.max(left), y.max(top));
let (x1, y1) = ((x + width).min(right), (y + height).min(bottom));
(x1 > x0 && y1 > y0).then_some(Item::Rect {
x: x0,
y: y0,
width: x1 - x0,
height: y1 - y0,
color,
})
}
other => Some(other),
}
}
const DATA_BAR_INSET_PT: f32 = 1.5;
const CUT_SLACK_PT: f32 = 0.01;
fn distributed_units(text: &str) -> Vec<String> {
let mut units: Vec<String> = Vec::new();
let mut latin = String::new();
for ch in text.trim().chars() {
if ch.is_whitespace() || u32::from(ch) >= WIDE_LETTERS_FROM {
units.extend((!latin.is_empty()).then(|| std::mem::take(&mut latin)));
units.push(ch.to_string());
} else {
latin.push(ch);
}
}
units.extend((!latin.is_empty()).then_some(latin));
units
}
fn fill_lines<T>(runs: Vec<(T, f32, bool)>, room: f32) -> Vec<Vec<(T, f32, bool)>> {
let mut lines: Vec<Vec<(T, f32, bool)>> = vec![Vec::new()];
let mut used = 0.0;
for run in runs {
let line = lines.last_mut().expect("there is always a line");
if run.2 && line.is_empty() {
continue;
}
if !line.is_empty() && used + run.1 > room && !run.2 {
while line.last().is_some_and(|last| last.2) {
line.pop();
}
lines.push(Vec::new());
used = 0.0;
}
used += run.1;
lines.last_mut().expect("there is always a line").push(run);
}
if let Some(line) = lines.last_mut() {
while line.last().is_some_and(|last| last.2) {
line.pop();
}
}
lines.retain(|line| !line.is_empty());
lines
}
fn outline(lines: &mut Vec<Item>, [x, y, width, height]: [f32; 4], scale: f32) {
grid_lines(lines, [x, y, width, height], scale);
grid_lines(lines, [x - width, y - height, width, height], scale);
}
fn grid_lines(lines: &mut Vec<Item>, [x, y, width, height]: [f32; 4], scale: f32) {
let line_w = (0.5 * scale).max(0.25);
for (x1, y1, x2, y2) in [
(x + width, y, x + width, y + height),
(x, y + height, x + width, y + height),
] {
lines.push(Item::Line {
x1,
y1,
x2,
y2,
width: line_w,
color: GRID_LINE_COLOR,
dash: None,
});
}
}
fn image(index: usize, placed: [f32; 4], region: [f32; 4]) -> Option<Item> {
let [x, y, width, height] = placed;
if width <= 0.0 || height <= 0.0 {
return None;
}
let left = x.max(region[0]);
let top = y.max(region[1]);
let right = (x + width).min(region[2]);
let bottom = (y + height).min(region[3]);
if right <= left || bottom <= top {
return None;
}
Some(Item::Image {
index,
x: left,
y: top,
width: right - left,
height: bottom - top,
crop: [
(left - x) / width,
(top - y) / height,
(x + width - right) / width,
(y + height - bottom) / height,
],
})
}
fn translate(items: &mut [Item], dx: f32, dy: f32) {
for item in items {
match item {
Item::Text(run) => {
run.x += dx;
run.y += dy;
}
Item::Rect { x, y, .. } | Item::Image { x, y, .. } | Item::Link { x, y, .. } | Item::Turn { x, y, .. } => {
*x += dx;
*y += dy;
}
Item::Line { x1, y1, x2, y2, .. } => {
*x1 += dx;
*x2 += dx;
*y1 += dy;
*y2 += dy;
}
Item::EndTurn => {}
}
}
}
fn clip_items(items: Vec<Item>, [left, top, right, bottom]: [f32; 4], open_right: bool) -> Vec<Item> {
let mut kept = Vec::with_capacity(items.len());
for mut item in items {
match &mut item {
Item::Text(run) => {
let middle = run.y - run.size * 0.35;
if middle < top || middle > bottom {
continue;
}
let origin = run.x;
run.glyphs.retain(|glyph| {
let start = origin + glyph.x;
let end = if open_right { start } else { start + glyph.advance };
start >= left - 0.5 && end <= right + 0.5
});
if run.glyphs.is_empty() {
continue;
}
keep_shown_text(run);
}
Item::Line { x1, y1, x2, y2, .. } => {
*x1 = x1.clamp(left, right);
*x2 = x2.clamp(left, right);
if *y1 < top || *y1 > bottom || *x1 == *x2 && *y1 == *y2 {
continue;
}
*y2 = y2.clamp(top, bottom);
}
Item::Rect {
x, y, width, height, ..
}
| Item::Link {
x, y, width, height, ..
} => {
let x2 = (*x + *width).min(right);
let y2 = (*y + *height).min(bottom);
*x = x.max(left);
*y = y.max(top);
if x2 <= *x || y2 <= *y {
continue;
}
*width = x2 - *x;
*height = y2 - *y;
}
Item::Image { .. } | Item::Turn { .. } | Item::EndTurn => {}
}
kept.push(item);
}
kept
}
fn keep_shown_text(run: &mut crate::layout::display::TextRun) {
run.glyphs.shrink_to_fit();
let start = run.glyphs.iter().map(|glyph| glyph.text.0).min().unwrap_or(0);
let end = run.glyphs.iter().map(|glyph| glyph.text.1).max().unwrap_or(0);
let (Ok(from), Ok(to)) = (usize::try_from(start), usize::try_from(end)) else {
return;
};
if from == 0 && to == run.text.len() {
return;
}
let Some(shown) = run.text.get(from..to) else {
return;
};
run.text = shown.to_string();
for glyph in &mut run.glyphs {
glyph.text = (glyph.text.0 - start, glyph.text.1 - start);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_merge_cut_by_a_page_break_is_drawn_only_on_the_page() {
let region = [10.0, 0.0, 100.0, 50.0];
let line = |x1, x2| Item::Line {
x1,
y1: 5.0,
x2,
y2: 5.0,
width: 1.0,
color: [0; 3],
dash: None,
};
let Some(Item::Line { x1, x2, .. }) = cut(line(0.0, 200.0), region) else {
panic!()
};
assert_eq!((x1, x2), (10.0, 100.0));
let side = Item::Line {
x1: 200.0,
y1: 0.0,
x2: 200.0,
y2: 50.0,
width: 1.0,
color: [0; 3],
dash: None,
};
assert!(cut(side, region).is_none());
let fill = Item::Rect {
x: 50.0,
y: 0.0,
width: 100.0,
height: 20.0,
color: [0; 3],
};
let Some(Item::Rect { x, width, .. }) = cut(fill, region) else {
panic!()
};
assert_eq!((x, width), (50.0, 50.0));
}
#[test]
fn rows_are_cut_into_pages_by_room_and_breaks() {
let rows: Vec<u32> = (0..10).collect();
let size = |_| 10.0;
let none = HashSet::new();
assert_eq!(
groups(&rows, size, 35.0, &none, 0.0),
[vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8], vec![9]]
);
let breaks: HashSet<u32> = [0, 5].into();
assert_eq!(
groups(&rows, size, 100.0, &breaks, 0.0),
[vec![0, 1, 2, 3, 4], vec![5, 6, 7, 8, 9]]
);
assert_eq!(
groups(&rows, size, 40.0, &none, 20.0),
[vec![0, 1, 2, 3], vec![4, 5], vec![6, 7], vec![8, 9]]
);
assert_eq!(groups(&[0, 1], |_| 50.0, 30.0, &none, 0.0), [vec![0], vec![1]]);
}
#[test]
fn a_picture_across_the_edge_of_a_page_is_cropped() {
let Some(Item::Image {
x,
y,
width,
height,
crop,
..
}) = image(3, [80.0, 10.0, 40.0, 20.0], [0.0, 0.0, 100.0, 100.0])
else {
panic!("a picture")
};
assert_eq!((x, y, width, height), (80.0, 10.0, 20.0, 20.0));
assert_eq!(crop, [0.0, 0.0, 0.5, 0.0]);
assert!(image(3, [120.0, 10.0, 40.0, 20.0], [0.0, 0.0, 100.0, 100.0]).is_none());
}
#[test]
fn text_is_cut_at_its_clip() {
let glyph = |x: f32| crate::layout::display::Glyph {
id: 1,
x,
advance: 5.0,
dx: 0.0,
dy: 0.0,
text: (0, 1),
};
let run = crate::layout::display::TextRun {
face: crate::layout::fonts::FaceId::default(),
size: 10.0,
x: 0.0,
y: 20.0,
color: [0; 3],
glyphs: vec![glyph(0.0), glyph(5.0), glyph(10.0)],
text: "abc".into(),
fake_bold: false,
fake_italic: false,
scale: 1.0,
};
let kept = clip_items(vec![Item::Text(run.clone())], [0.0, 0.0, 10.0, 30.0], false);
let [Item::Text(cut)] = kept.as_slice() else {
panic!("one run")
};
assert_eq!(cut.glyphs.len(), 2);
let kept = clip_items(vec![Item::Text(run.clone())], [0.0, 0.0, 7.0, 30.0], true);
let [Item::Text(cut)] = kept.as_slice() else {
panic!("one run")
};
assert_eq!(cut.glyphs.len(), 2);
assert!(clip_items(vec![Item::Text(run)], [0.0, 30.0, 10.0, 60.0], false).is_empty());
}
}