use std::ops::Range;
use crate::markup::{Align, TextEvent};
use crate::text::metrics::LineKind;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct TcyPlace {
pub offset: f32,
pub scale: f32,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Glyph {
pub ch: char,
pub tail: String,
pub x: f32,
pub width: f32,
pub shift: f32,
pub line: usize,
pub style: usize,
pub tcy: Option<TcyPlace>,
pub source: Range<usize>,
pub var: Option<usize>,
pub icon: Option<String>,
}
pub const ICON_CHAR: char = '\u{FFFC}';
impl Glyph {
pub fn text(&self) -> String {
let mut s = String::with_capacity(self.ch.len_utf8() + self.tail.len());
s.push(self.ch);
s.push_str(&self.tail);
s
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RubyRun {
pub text: String,
pub x: f32,
pub width: f32,
pub line: usize,
pub first_glyph: usize,
pub glyph_count: usize,
pub emphasis: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Line {
pub first_glyph: usize,
pub glyph_count: usize,
pub width: f32,
pub indent: f32,
pub align: Align,
pub size: f32,
pub paragraph_start: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Decoration {
pub kind: LineKind,
pub line: usize,
pub x0: f32,
pub x1: f32,
pub y: f32,
pub thickness: f32,
pub first_glyph: usize,
pub glyph_count: usize,
pub style: usize,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Cue {
Wait(f32),
Click,
Event(TextEvent),
}
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct Page {
pub glyphs: Vec<Glyph>,
pub lines: Vec<Line>,
pub rubies: Vec<RubyRun>,
pub decorations: Vec<Decoration>,
pub cues: Vec<(usize, Cue)>,
}
fn short_ruby_fits(base: &str, reading: &str) -> bool {
let n = base.chars().count();
(1..=32).contains(&n)
&& !base
.chars()
.any(|c| c.is_whitespace() || "||《》[]".contains(c))
&& !reading.trim().is_empty()
&& reading.trim() == reading
&& !reading
.chars()
.any(|c| "《》[]".contains(c) || (c.is_whitespace() && c != ' ' && c != '\u{3000}'))
}
impl Page {
pub fn text(&self) -> String {
let mut out = String::new();
for (ln, line) in self.lines.iter().enumerate() {
if ln > 0 {
out.push('\n');
}
let end = line.first_glyph + line.glyph_count;
let mut i = line.first_glyph;
while i < end {
if let Some(r) = self
.rubies
.iter()
.find(|r| r.first_glyph == i && !r.emphasis)
{
let stop = (i + r.glyph_count).min(end);
let base: String = self.glyphs[i..stop]
.iter()
.flat_map(|g| std::iter::once(g.ch).chain(g.tail.chars()))
.collect();
if short_ruby_fits(&base, &r.text) {
out.push('|');
out.push_str(&base);
out.push('《');
out.push_str(&r.text);
out.push('》');
} else {
out.push_str("[ruby ");
out.push_str(&r.text);
out.push(']');
for c in base.chars() {
out.push(c);
if c == '[' || c == ']' {
out.push(c);
}
}
out.push_str("[/ruby]");
}
i = stop.max(i + 1);
} else {
out.push(self.glyphs[i].ch);
out.push_str(&self.glyphs[i].tail);
i += 1;
}
}
}
out
}
pub fn speech_text(&self) -> String {
let mut out = String::new();
for (ln, line) in self.lines.iter().enumerate() {
if ln > 0 && line.paragraph_start {
out.push('\n');
}
let end = line.first_glyph + line.glyph_count;
let mut i = line.first_glyph;
while i < end {
if let Some(r) = self
.rubies
.iter()
.find(|r| r.first_glyph == i && !r.emphasis)
{
out.push_str(&r.text);
i = (i + r.glyph_count).min(end).max(i + 1);
} else {
let g = &self.glyphs[i];
if g.icon.is_none() {
out.push(g.ch);
out.push_str(&g.tail);
}
i += 1;
}
}
}
out
}
}