pub type ArcStr = std::sync::Arc<str>;
#[derive(Clone, PartialEq, Default, Debug)]
pub struct BrushIndex(pub usize);
pub type StyleProperty = parley::StyleProperty<'static, BrushIndex>;
pub type StyleSet = parley::StyleSet<BrushIndex>;
use accesskit::{TextDecoration, TextDecorationStyle};
use kurbo::{Affine, Line, Stroke};
use parley::{Layout, PositionedLayoutItem, Style};
use peniko::{Brush, Fill};
use smallvec::SmallVec;
use crate::imaging::{PaintSink, Painter, record::Glyph};
pub fn render_text(
painter: &mut Painter<'_, impl PaintSink + ?Sized>,
transform: Affine,
layout: &Layout<BrushIndex>,
brushes: &[Brush],
hint: bool,
) {
for line in layout.lines() {
for item in line.items() {
let PositionedLayoutItem::GlyphRun(glyph_run) = item else {
continue;
};
let style = glyph_run.style();
if let Some(underline) = &style.underline {
let underline_brush = &brushes[underline.brush.0];
let run_metrics = glyph_run.run().font_metrics();
let offset = match underline.offset {
Some(offset) => offset,
None => run_metrics.underline_offset,
};
let width = match underline.size {
Some(size) => size,
None => run_metrics.underline_size,
};
let y = glyph_run.baseline() - offset + width / 2.;
let line = Line::new(
(glyph_run.offset() as f64, y as f64),
((glyph_run.offset() + glyph_run.advance()) as f64, y as f64),
);
painter
.stroke(line, &Stroke::new(width.into()), underline_brush)
.transform(transform)
.draw();
}
let mut x = glyph_run.offset();
let y = glyph_run.baseline();
let run = glyph_run.run();
let font = run.font();
let font_size = run.font_size();
let synthesis = run.synthesis();
let glyph_xform = synthesis
.skew()
.map(|angle| Affine::skew(angle.to_radians().tan() as f64, 0.0));
let coords: SmallVec<[i16; 8]> = run
.normalized_coords()
.iter()
.map(|coord| coord.to_bits())
.collect();
let brush = &brushes[style.brush.0];
let glyphs: SmallVec<[Glyph; 16]> = glyph_run
.glyphs()
.map(|glyph| {
let gx = x + glyph.x;
let gy = y + glyph.y;
x += glyph.advance;
Glyph {
id: glyph.id,
x: gx,
y: gy,
}
})
.collect();
painter
.glyphs(font, brush)
.hint(hint)
.transform(transform)
.glyph_transform(glyph_xform)
.font_size(font_size)
.normalized_coords(&coords)
.draw(&peniko::Style::Fill(Fill::NonZero), &glyphs);
if let Some(strikethrough) = &style.strikethrough {
let strikethrough_brush = &brushes[strikethrough.brush.0];
let run_metrics = glyph_run.run().font_metrics();
let offset = match strikethrough.offset {
Some(offset) => offset,
None => run_metrics.strikethrough_offset,
};
let width = match strikethrough.size {
Some(size) => size,
None => run_metrics.strikethrough_size,
};
let y = glyph_run.baseline() - offset + run_metrics.strikethrough_size / 2.;
let line = Line::new(
(glyph_run.offset() as f64, y as f64),
((glyph_run.offset() + glyph_run.advance()) as f64, y as f64),
);
painter
.stroke(line, &Stroke::new(width.into()), strikethrough_brush)
.transform(transform)
.draw();
}
}
}
}
fn to_accesskit_color(brush: &Brush) -> Option<accesskit::Color> {
if let Brush::Solid(color) = brush {
let rgba = color.to_rgba8();
Some(accesskit::Color {
red: rgba.r,
green: rgba.g,
blue: rgba.b,
alpha: rgba.a,
})
} else {
None
}
}
pub fn set_accesskit_brush_properties(
node: &mut accesskit::Node,
style: &Style<BrushIndex>,
brushes: &[Brush],
) {
if let Some(color) = to_accesskit_color(&brushes[style.brush.0]) {
node.set_foreground_color(color);
}
if let Some(deco) = &style.underline
&& let Some(color) = to_accesskit_color(&brushes[deco.brush.0])
{
node.set_underline(TextDecoration {
style: TextDecorationStyle::Solid,
color,
});
}
if let Some(deco) = &style.strikethrough
&& let Some(color) = to_accesskit_color(&brushes[deco.brush.0])
{
node.set_strikethrough(TextDecoration {
style: TextDecorationStyle::Solid,
color,
});
}
}