use std::ops::Range;
use anyhow::Result;
use swash::shape::ShapeContext;
use swash::shape::cluster::{Glyph, GlyphCluster};
use swash::shape::partition::{SelectedFont, Selector, ShapeOptions, shape};
use swash::text::cluster::{Boundary, CharCluster, CharInfo, Status, Token};
use swash::text::{Script, analyze};
use swash::{FontRef, Setting};
use crate::font::Font;
use crate::types::Point;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Orientation {
Horizontal,
Vertical,
}
impl Orientation {
fn is_vertical(self) -> bool {
matches!(self, Orientation::Vertical)
}
fn baseline_for_line(self, line_index: usize, line_height: f32) -> Point {
let secondary_offset = line_index as f32 * line_height;
match self {
Orientation::Horizontal => (0.0, secondary_offset),
Orientation::Vertical => (-secondary_offset, 0.0),
}
}
fn position_glyph(self, glyph: &Glyph, baseline: Point, primary_offset: f32) -> Point {
match self {
Orientation::Horizontal => {
(baseline.0 + primary_offset + glyph.x, baseline.1 + glyph.y)
}
Orientation::Vertical => {
(baseline.0 + glyph.x, baseline.1 + primary_offset + glyph.y)
}
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct LayoutRequest<'a> {
pub text: &'a str,
pub fonts: &'a [Font],
pub font_size: f32,
pub line_height: f32,
pub script: Script,
pub max_primary_axis: f32,
pub direction: Orientation,
}
pub type LayoutResult = Vec<LayoutLine>;
#[derive(Debug, Clone, Default)]
pub struct LayoutLine {
pub font: Font,
pub glyphs: Vec<Glyph>,
pub range: Range<usize>,
pub advance: f32,
pub baseline: Point,
}
struct FontSelector<'a> {
fonts: &'a [Font],
}
impl<'a> Selector for FontSelector<'a> {
type SelectedFont = Font;
fn select_font(&mut self, cluster: &mut CharCluster) -> Option<Self::SelectedFont> {
for font in self.fonts {
let charmap = font.font_ref().ok()?.charmap();
match cluster.map(|ch| charmap.map(ch)) {
Status::Complete => return Some(font.clone()),
_ => continue,
}
}
None
}
}
impl SelectedFont for Font {
fn font(&self) -> FontRef<'_> {
self.font_ref().unwrap()
}
}
impl ShapeOptions for LayoutRequest<'_> {
type Features = std::vec::IntoIter<Setting<u16>>;
type Variations = std::iter::Empty<Setting<f32>>;
fn script(&self) -> Script {
self.script
}
fn size(&self) -> f32 {
self.font_size
}
fn features(&self) -> Self::Features {
let mut features = vec![];
if self.direction.is_vertical() {
features.push(("vert", 1).into());
features.push(("vrt2", 1).into());
}
features.into_iter()
}
fn variations(&self) -> Self::Variations {
std::iter::empty()
}
}
pub struct Layouter {
shape_context: ShapeContext,
}
impl Layouter {
pub fn new() -> Self {
Self {
shape_context: ShapeContext::new(),
}
}
pub fn layout(&mut self, request: &LayoutRequest<'_>) -> Result<LayoutResult> {
let mut lines = Vec::new();
let mut current_line = LayoutLine::default();
let mut line_index = 0;
let mut primary_offset = 0.0;
let mut selector = FontSelector {
fonts: request.fonts,
};
let tokens = request
.text
.char_indices()
.zip(analyze(request.text.chars()))
.map(|((i, ch), (props, boundary))| Token {
ch,
offset: i as u32,
len: ch.len_utf8() as u8,
info: CharInfo::new(props, boundary),
data: 0,
});
shape(
&mut self.shape_context,
&mut selector,
request,
tokens,
|font, shaper| {
shaper.shape_with(|cluster| {
if should_break_line(cluster, primary_offset, request.max_primary_axis) {
if !current_line.glyphs.is_empty() {
current_line.baseline = request
.direction
.baseline_for_line(line_index, request.line_height);
lines.push(std::mem::take(&mut current_line));
line_index += 1;
}
current_line = LayoutLine::default();
current_line.font = font.clone();
primary_offset = 0.0;
}
if current_line.glyphs.is_empty() {
current_line.font = font.clone();
}
let cluster_advance = add_cluster_to_line(
cluster,
&mut current_line,
primary_offset,
request.direction,
);
primary_offset += cluster_advance;
current_line.advance = primary_offset;
let source_range = cluster.source;
if current_line.range.is_empty() {
current_line.range = source_range.start as usize..source_range.end as usize;
} else {
current_line.range.end = source_range.end as usize;
}
});
},
);
if !current_line.glyphs.is_empty() {
current_line.baseline = request
.direction
.baseline_for_line(line_index, request.line_height);
lines.push(current_line);
}
Ok(lines)
}
}
pub fn calculate_bounds(layout: &LayoutResult) -> (f32, f32, f32, f32) {
let mut min_x = f32::MAX;
let mut min_y = f32::MAX;
let mut max_x = f32::MIN;
let mut max_y = f32::MIN;
for line in layout {
let baseline_x = line.baseline.0;
let baseline_y = line.baseline.1;
for glyph in &line.glyphs {
let glyph_x = baseline_x + glyph.x;
let glyph_y = baseline_y + glyph.y;
min_x = min_x.min(glyph_x);
min_y = min_y.min(glyph_y);
max_x = max_x.max(glyph_x + glyph.advance);
max_y = max_y.max(glyph_y);
}
}
(min_x, min_y, max_x, max_y)
}
fn should_break_line(cluster: &GlyphCluster, current_offset: f32, max_primary_axis: f32) -> bool {
if cluster.info.boundary() == Boundary::Mandatory {
return true;
}
let cluster_advance = cluster.advance();
let would_exceed = current_offset + cluster_advance > max_primary_axis;
would_exceed
&& (matches!(
cluster.info.boundary(),
Boundary::Line | Boundary::Mandatory
))
}
fn add_cluster_to_line(
cluster: &swash::shape::cluster::GlyphCluster,
line: &mut LayoutLine,
primary_offset: f32,
direction: Orientation,
) -> f32 {
let baseline = line.baseline;
let mut cluster_advance = 0.0;
for glyph in cluster.glyphs {
let mut positioned_glyph = *glyph;
let pos = direction.position_glyph(glyph, baseline, primary_offset + cluster_advance);
positioned_glyph.x = pos.0;
positioned_glyph.y = pos.1;
line.glyphs.push(positioned_glyph);
cluster_advance += glyph.advance;
}
cluster_advance
}