use crate::geometry::{Color, Rect, Size};
use crate::text::style::*;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq)]
pub struct Glyph {
pub id: u32,
pub x: f32,
pub y: f32,
pub advance: f32,
pub cluster: u32,
}
#[derive(Debug, Clone)]
pub struct TextRun {
pub text: String,
pub font_data: Arc<Vec<u8>>,
pub font_index: u32,
pub font_size: f32,
pub font_weight_bold: bool,
pub font_style_italic: bool,
pub color: Color,
pub advance: f32,
pub glyphs: Vec<Glyph>,
pub is_rtl: bool,
pub baseline_x: f32,
pub baseline_y: f32,
pub url: Option<String>,
pub decoration: TextDecoration,
pub baseline_shift: f32,
pub background_color: Option<Color>,
}
impl TextRun {
#[must_use]
pub fn split_at(&self, offsets: &[usize]) -> Vec<TextRun> {
let len = self.text.len();
let mut cuts: Vec<usize> = offsets.to_vec();
cuts.retain(|&o| {
o > 0
&& o < len
&& self.text.is_char_boundary(o)
&& self.glyphs.iter().any(|g| g.cluster as usize == o)
});
cuts.sort_unstable();
cuts.dedup();
if cuts.is_empty() {
return vec![self.clone()];
}
let mut bounds = cuts;
bounds.insert(0, 0);
bounds.push(len);
let mut out = Vec::with_capacity(bounds.len() - 1);
for w in bounds.windows(2) {
let (start, end) = (w[0], w[1]);
let glyphs: Vec<Glyph> = self
.glyphs
.iter()
.filter(|g| {
let c = g.cluster as usize;
c >= start && c < end
})
.map(|g| Glyph {
cluster: g.cluster - start as u32,
..g.clone()
})
.collect();
if glyphs.is_empty() {
continue;
}
let advance: f32 = glyphs.iter().map(|g| g.advance).sum();
let baseline_x = glyphs[0].x;
out.push(TextRun {
text: self.text[start..end].to_string(),
advance,
glyphs,
baseline_x,
..self.clone()
});
}
out
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LineMetrics {
pub line_height: f32,
pub baseline: f32,
pub block_min: f32,
pub block_max: f32,
pub content_block_min: f32,
pub content_block_max: f32,
pub cap_height: f32,
pub x_height: f32,
}
impl LineMetrics {
pub fn ascent(&self) -> f32 {
self.baseline - self.content_block_min
}
pub fn descent(&self) -> f32 {
self.content_block_max - self.baseline
}
}
#[derive(Debug, Clone)]
pub struct TextLine {
pub runs: Vec<TextRun>,
pub bounds: Rect,
pub metrics: LineMetrics,
}
impl TextLine {
pub fn layout_bounds(&self) -> Rect {
self.bounds
}
pub fn line_box_bounds(&self) -> Rect {
let top = self.bounds.min_y();
Rect::new(
self.bounds.min_x(),
top + self.metrics.block_min as f64,
self.bounds.max_x(),
top + self.metrics.block_max as f64,
)
}
pub fn logical_bounds(&self) -> Rect {
let top = self.bounds.min_y();
Rect::new(
self.bounds.min_x(),
top + self.metrics.content_block_min as f64,
self.bounds.max_x(),
top + self.metrics.content_block_max as f64,
)
}
pub fn cap_bounds(&self) -> Rect {
if self.metrics.cap_height > 0.0 {
let top = self.bounds.min_y();
let baseline_y = top + self.metrics.baseline as f64;
Rect::new(
self.bounds.min_x(),
baseline_y - self.metrics.cap_height as f64,
self.bounds.max_x(),
baseline_y,
)
} else {
self.logical_bounds()
}
}
}
#[derive(Debug, Clone)]
pub struct TextLayout {
pub lines: Vec<TextLine>,
pub width: f64,
pub height: f64,
}
impl TextLayout {
pub fn layout_bounds(&self) -> Rect {
self.lines
.iter()
.map(TextLine::layout_bounds)
.fold(None, union_rect)
.unwrap_or_else(|| Rect::new(0.0, 0.0, self.width, self.height))
}
pub fn line_box_bounds(&self) -> Rect {
self.lines
.iter()
.map(TextLine::line_box_bounds)
.fold(None, union_rect)
.unwrap_or_else(|| Rect::new(0.0, 0.0, self.width, self.height))
}
pub fn logical_bounds(&self) -> Rect {
self.lines
.iter()
.map(TextLine::logical_bounds)
.fold(None, union_rect)
.unwrap_or_else(|| Rect::new(0.0, 0.0, self.width, self.height))
}
pub fn cap_bounds(&self) -> Rect {
self.lines
.iter()
.map(TextLine::cap_bounds)
.fold(None, union_rect)
.unwrap_or_else(|| Rect::new(0.0, 0.0, self.width, self.height))
}
}
fn union_rect(acc: Option<Rect>, r: Rect) -> Option<Rect> {
Some(match acc {
None => r,
Some(a) => a.union(r),
})
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextMetrics {
pub width: f64,
pub height: f64,
pub actual_bounding_box_left: f64,
pub actual_bounding_box_right: f64,
pub font_bounding_box_ascent: f64,
pub font_bounding_box_descent: f64,
pub actual_bounding_box_ascent: f64,
pub actual_bounding_box_descent: f64,
pub em_height_ascent: f64,
pub em_height_descent: f64,
pub hanging_baseline: f64,
pub alphabetic_baseline: f64,
pub ideographic_baseline: f64,
pub line_height: f64,
}
pub struct TextMeasure {
pub size: Size,
pub metrics: TextMetrics,
pub layout: Arc<TextLayout>,
}