mod build;
mod decoration;
mod line_break;
mod measure;
mod options;
mod page;
mod paragraph;
mod span;
#[cfg(test)]
mod tests;
pub use options::{Indent, Kinsoku, LayoutOptions, Vars};
pub use page::{Cue, Decoration, Glyph, Line, Page, RubyRun, TcyPlace, ICON_CHAR};
pub use span::{Extent, MarkRun, Span};
use crate::markup::{Script, Style};
use crate::text::metrics::FontMetrics;
use crate::text::vertical::Writing;
use build::build_pages;
use paragraph::split_pages;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Layout {
pub writing: Writing,
pub pages: Vec<Page>,
pub styles: Vec<Style>,
pub missing_vars: Vec<String>,
pub vars: Vec<String>,
}
impl Layout {
pub fn build(
script: &Script,
metrics: &dyn FontMetrics,
opts: &LayoutOptions,
vars: &Vars,
) -> Layout {
let mut missing = Vec::new();
let mut used = Vec::new();
let sources = split_pages(script, vars, &mut missing, &mut used);
let pages = sources
.iter()
.flat_map(|src| build_pages(src, &script.styles, metrics, opts))
.collect();
Layout {
writing: opts.writing,
pages,
styles: script.styles.clone(),
missing_vars: missing,
vars: used,
}
}
pub fn marks_of(&self, glyph: &Glyph) -> &[String] {
self.styles.get(glyph.style).map_or(&[], |s| &s.marks)
}
pub fn marks(&self, page: usize) -> Vec<MarkRun> {
let Some(page) = self.pages.get(page) else {
return Vec::new();
};
let mut out: Vec<MarkRun> = Vec::new();
let mut open: Vec<(String, usize)> = Vec::new();
for (i, g) in page.glyphs.iter().enumerate() {
let ids = self.marks_of(g);
open.retain(|(id, _)| ids.contains(id));
for (n, id) in ids.iter().enumerate() {
if ids[..n].contains(id) {
continue;
}
match open.iter().find(|(o, _)| o == id) {
Some(&(_, k)) => out[k].glyph_count += 1,
None => {
open.push((id.clone(), out.len()));
out.push(MarkRun {
id: id.clone(),
first_glyph: i,
glyph_count: 1,
});
}
}
}
}
out.sort_by_key(|m| m.first_glyph);
out
}
pub fn mark_spans(&self, page: usize, id: &str) -> Vec<Span> {
let Some(p) = self.pages.get(page) else {
return Vec::new();
};
self.marks(page)
.iter()
.filter(|m| m.id == id)
.flat_map(|m| p.spans(m.first_glyph, m.glyph_count))
.collect()
}
pub fn glyph_count(&self) -> usize {
self.pages.iter().map(|p| p.glyphs.len()).sum()
}
}