use pltrs_core::{Affine2D, Path, Point, Rgba};
use pltrs_text::TEXT_PATH_FONT_SCALE;
use pltrs_text::tex::{TexError, TexText};
use pltrs_text::{TextEngine, TextRun};
use crate::collection::PathCollection;
use crate::filter::{FilterFn, SharedFilter};
use crate::gc::GraphicsContext;
use crate::image::RgbaImage;
use crate::quad_mesh::QuadMesh;
#[derive(Clone, Copy, Debug)]
pub struct TextLine<'a> {
pub run: &'a TextRun,
pub origin: Point,
}
#[derive(Clone, Copy)]
pub struct TextLines<'a> {
pub lines: &'a [TextLine<'a>],
pub angle: f64,
pub source: &'a dyn std::any::Any,
}
impl std::fmt::Debug for TextLines<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TextLines")
.field("lines", &self.lines)
.field("angle", &self.angle)
.finish_non_exhaustive()
}
}
pub trait Renderer {
fn canvas_size(&self) -> (f64, f64);
fn dpi(&self) -> f64;
fn points_to_pixels(&self, points: f64) -> f64 {
points * self.dpi() / 72.0
}
fn draw_path(
&mut self,
gc: &GraphicsContext,
path: &Path,
transform: &Affine2D,
fill: Option<Rgba>,
);
fn draw_markers(
&mut self,
gc: &GraphicsContext,
marker: &Path,
marker_transform: &Affine2D,
path: &Path,
transform: &Affine2D,
fill: Option<Rgba>,
) {
for &vertex in path.vertices() {
let at = transform.transform_point(vertex);
if at.is_finite() {
let placed = marker_transform.translate(at.x, at.y);
self.draw_path(gc, marker, &placed, fill);
}
}
}
fn draw_path_collection(&mut self, gc: &GraphicsContext, collection: &PathCollection<'_>) {
for item in collection.items(gc) {
self.draw_path(&item.gc, item.path, &item.transform, item.fill);
}
}
fn draw_quad_mesh(&mut self, gc: &GraphicsContext, mesh: &QuadMesh<'_>) {
let paths = mesh.cell_paths();
let widths = [gc.linewidth];
let flags = [mesh.antialiased];
self.draw_path_collection(gc, &mesh.collection(&paths, &widths, &flags));
}
fn draw_gouraud_triangles(
&mut self,
gc: &GraphicsContext,
triangles: &[[Point; 3]],
colors: &[[Rgba; 3]],
transform: &Affine2D,
);
fn draw_image(
&mut self,
gc: &GraphicsContext,
origin: Point,
image: &RgbaImage,
transform: Option<&Affine2D>,
);
fn draw_text(&mut self, gc: &GraphicsContext, run: &TextRun, origin: Point, angle: f64) {
fill_text(self, gc, run, origin, angle);
}
fn draw_text_lines(&mut self, _gc: &GraphicsContext, _text: &TextLines<'_>) -> bool {
false
}
fn takes_texts_whole(&self) -> bool {
false
}
fn draw_tex(
&mut self,
gc: &GraphicsContext,
_run: &TextRun,
tex: &TexText,
origin: Point,
angle: f64,
) -> Result<(), TexError> {
fill_tex(self, gc, tex, origin, angle)
}
fn rounds_glyph_positions(&self) -> bool {
false
}
fn open_group(&mut self, _name: &str, _gid: Option<&str>) {}
fn close_group(&mut self, _name: &str) {}
fn open_part_group(&mut self, name: &str, gid: Option<&str>) {
self.open_group(name, gid);
}
fn close_part_group(&mut self, name: &str) {
self.close_group(name);
}
fn start_rasterizing(&mut self) {}
fn stop_rasterizing(&mut self) {}
fn raster_dpi(&self) -> Option<f64> {
None
}
fn start_filter(&mut self) {}
fn stop_filter(&mut self, _filter: &FilterFn<'_>) {}
fn stop_shared_filter(&mut self, filter: &SharedFilter) {
self.stop_filter(filter.as_ref());
}
fn option_image_nocomposite(&self) -> bool {
false
}
fn option_scale_image(&self) -> bool {
false
}
fn image_magnification(&self) -> f64 {
1.0
}
}
pub(crate) fn fill_tex<R: Renderer + ?Sized>(
renderer: &mut R,
gc: &GraphicsContext,
tex: &TexText,
origin: Point,
angle: f64,
) -> Result<(), TexError> {
let path = tex.text_path()?;
let scale = renderer.points_to_pixels(tex.size()) / TEXT_PATH_FONT_SCALE;
let transform = Affine2D::scaling(scale, scale)
.rotate_deg(angle)
.translate(origin.x, origin.y);
let mut fill_gc = gc.clone();
fill_gc.linewidth = 0.0;
renderer.draw_path(&fill_gc, &path, &transform, Some(gc.stroke_color()));
Ok(())
}
pub fn draw_text_as_path<R: Renderer + ?Sized>(
renderer: &mut R,
gc: &GraphicsContext,
run: &TextRun,
origin: Point,
angle: f64,
) {
let engine = TextEngine::global();
let scaled = run.source.as_ref().and_then(|source| {
let laid_out = source
.layout(engine, TEXT_PATH_FONT_SCALE, pltrs_text::TEXT_PATH_DPI)
.ok()?;
let scale = renderer.points_to_pixels(source.size()) / TEXT_PATH_FONT_SCALE;
Some((laid_out.outline(engine), scale))
});
let (outline, scale) = scaled.unwrap_or_else(|| (run.outline(engine), 1.0));
let placement = Affine2D::scaling(scale, scale)
.rotate_deg(angle)
.translate(origin.x, origin.y);
let gc = GraphicsContext {
linewidth: 0.0,
..gc.clone()
};
renderer.draw_path(&gc, &outline, &placement, Some(gc.foreground));
}
pub(crate) fn text_placement(origin: Point, angle: f64) -> Affine2D {
Affine2D::identity()
.rotate_deg(angle)
.translate(origin.x, origin.y)
}
pub(crate) fn fill_text<R: Renderer + ?Sized>(
renderer: &mut R,
gc: &GraphicsContext,
run: &TextRun,
origin: Point,
angle: f64,
) {
let engine = TextEngine::global();
let placement = text_placement(origin, angle);
let mut fill_gc = gc.clone();
fill_gc.linewidth = 0.0;
let color = gc.stroke_color();
for glyph in &run.glyphs {
let Ok(outline) = engine.glyph_outline(glyph.font, glyph.glyph, glyph.size, run.dpi) else {
continue;
};
let at = Affine2D::translation(glyph.x, glyph.y).then(placement);
renderer.draw_path(&fill_gc, &outline, &at, Some(color));
}
for rect in &run.rects {
let at = crate::unit_box(rect).then(placement);
renderer.draw_path(&fill_gc, &Path::unit_rectangle(), &at, Some(color));
}
}