2023-02-25 16:05:42 +01:00
|
|
|
use crate::{Color, Font, Settings, Size, Viewport};
|
2023-02-25 15:38:25 +01:00
|
|
|
|
2023-02-26 23:59:00 +01:00
|
|
|
use iced_graphics::alignment;
|
2023-02-25 15:38:25 +01:00
|
|
|
use iced_graphics::backend;
|
|
|
|
|
use iced_graphics::text;
|
2023-02-26 00:38:46 +01:00
|
|
|
use iced_graphics::{Background, Primitive, Rectangle, Vector};
|
2023-02-25 15:38:25 +01:00
|
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
pub struct Backend {
|
|
|
|
|
default_font: Font,
|
|
|
|
|
default_text_size: f32,
|
2023-02-26 23:40:17 +01:00
|
|
|
text_pipeline: crate::text::Pipeline,
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Backend {
|
|
|
|
|
pub fn new(settings: Settings) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
default_font: settings.default_font,
|
|
|
|
|
default_text_size: settings.default_text_size,
|
2023-02-26 23:40:17 +01:00
|
|
|
text_pipeline: crate::text::Pipeline::new(),
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-25 16:05:42 +01:00
|
|
|
|
|
|
|
|
pub fn draw<T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
2023-02-26 00:49:27 +01:00
|
|
|
pixels: &mut tiny_skia::PixmapMut<'_>,
|
2023-02-26 00:38:46 +01:00
|
|
|
primitives: &[Primitive],
|
|
|
|
|
viewport: &Viewport,
|
2023-02-25 16:05:42 +01:00
|
|
|
background_color: Color,
|
2023-02-26 23:59:00 +01:00
|
|
|
overlay: &[T],
|
2023-02-25 16:05:42 +01:00
|
|
|
) {
|
|
|
|
|
pixels.fill(into_color(background_color));
|
2023-02-26 00:38:46 +01:00
|
|
|
|
|
|
|
|
let scale_factor = viewport.scale_factor() as f32;
|
|
|
|
|
|
|
|
|
|
for primitive in primitives {
|
2023-02-26 23:40:17 +01:00
|
|
|
self.draw_primitive(
|
|
|
|
|
primitive,
|
|
|
|
|
pixels,
|
|
|
|
|
None,
|
|
|
|
|
scale_factor,
|
|
|
|
|
Vector::ZERO,
|
|
|
|
|
);
|
2023-02-26 00:38:46 +01:00
|
|
|
}
|
2023-02-26 23:59:00 +01:00
|
|
|
|
|
|
|
|
for (i, text) in overlay.iter().enumerate() {
|
|
|
|
|
const OVERLAY_TEXT_SIZE: f32 = 20.0;
|
|
|
|
|
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
&Primitive::Text {
|
|
|
|
|
content: text.as_ref().to_owned(),
|
|
|
|
|
size: OVERLAY_TEXT_SIZE,
|
|
|
|
|
bounds: Rectangle {
|
|
|
|
|
x: 10.0,
|
|
|
|
|
y: 10.0 + i as f32 * OVERLAY_TEXT_SIZE * 1.2,
|
|
|
|
|
width: f32::INFINITY,
|
|
|
|
|
height: f32::INFINITY,
|
|
|
|
|
},
|
|
|
|
|
color: Color::BLACK,
|
|
|
|
|
font: Font::Monospace,
|
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
|
|
|
|
},
|
|
|
|
|
pixels,
|
|
|
|
|
None,
|
|
|
|
|
scale_factor,
|
|
|
|
|
Vector::ZERO,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-26 23:40:17 +01:00
|
|
|
|
|
|
|
|
self.text_pipeline.end_frame();
|
2023-02-26 00:38:46 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-26 23:40:17 +01:00
|
|
|
fn draw_primitive(
|
|
|
|
|
&mut self,
|
|
|
|
|
primitive: &Primitive,
|
|
|
|
|
pixels: &mut tiny_skia::PixmapMut<'_>,
|
|
|
|
|
clip_mask: Option<&tiny_skia::ClipMask>,
|
|
|
|
|
scale_factor: f32,
|
|
|
|
|
translation: Vector,
|
|
|
|
|
) {
|
|
|
|
|
match primitive {
|
|
|
|
|
Primitive::None => {}
|
|
|
|
|
Primitive::Quad {
|
|
|
|
|
bounds,
|
|
|
|
|
background,
|
2023-02-27 00:47:53 +01:00
|
|
|
border_radius,
|
2023-02-26 23:40:17 +01:00
|
|
|
border_width,
|
|
|
|
|
border_color,
|
|
|
|
|
} => {
|
|
|
|
|
let transform = tiny_skia::Transform::from_translate(
|
|
|
|
|
translation.x,
|
|
|
|
|
translation.y,
|
2023-02-26 00:38:46 +01:00
|
|
|
)
|
2023-02-26 23:40:17 +01:00
|
|
|
.post_scale(scale_factor, scale_factor);
|
2023-02-26 00:38:46 +01:00
|
|
|
|
2023-02-27 00:47:53 +01:00
|
|
|
let path = rounded_rectangle(*bounds, *border_radius);
|
2023-02-26 00:38:46 +01:00
|
|
|
|
2023-02-26 23:40:17 +01:00
|
|
|
pixels.fill_path(
|
2023-02-26 00:38:46 +01:00
|
|
|
&path,
|
|
|
|
|
&tiny_skia::Paint {
|
2023-02-26 23:40:17 +01:00
|
|
|
shader: match background {
|
|
|
|
|
Background::Color(color) => {
|
|
|
|
|
tiny_skia::Shader::SolidColor(into_color(
|
|
|
|
|
*color,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-02-26 00:38:46 +01:00
|
|
|
anti_alias: true,
|
|
|
|
|
..tiny_skia::Paint::default()
|
|
|
|
|
},
|
2023-02-26 23:40:17 +01:00
|
|
|
tiny_skia::FillRule::EvenOdd,
|
2023-02-26 00:38:46 +01:00
|
|
|
transform,
|
|
|
|
|
clip_mask,
|
|
|
|
|
);
|
2023-02-26 23:40:17 +01:00
|
|
|
|
|
|
|
|
if *border_width > 0.0 {
|
|
|
|
|
pixels.stroke_path(
|
|
|
|
|
&path,
|
|
|
|
|
&tiny_skia::Paint {
|
|
|
|
|
shader: tiny_skia::Shader::SolidColor(into_color(
|
|
|
|
|
*border_color,
|
|
|
|
|
)),
|
|
|
|
|
anti_alias: true,
|
|
|
|
|
..tiny_skia::Paint::default()
|
|
|
|
|
},
|
|
|
|
|
&tiny_skia::Stroke {
|
|
|
|
|
width: *border_width,
|
|
|
|
|
..tiny_skia::Stroke::default()
|
|
|
|
|
},
|
|
|
|
|
transform,
|
|
|
|
|
clip_mask,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-26 00:38:46 +01:00
|
|
|
}
|
2023-02-26 23:40:17 +01:00
|
|
|
Primitive::Text {
|
|
|
|
|
content,
|
|
|
|
|
bounds,
|
|
|
|
|
color,
|
|
|
|
|
size,
|
|
|
|
|
font,
|
|
|
|
|
horizontal_alignment,
|
|
|
|
|
vertical_alignment,
|
|
|
|
|
} => {
|
|
|
|
|
self.text_pipeline.draw(
|
|
|
|
|
content,
|
|
|
|
|
(*bounds + translation) * scale_factor,
|
|
|
|
|
*color,
|
|
|
|
|
*size * scale_factor,
|
|
|
|
|
*font,
|
|
|
|
|
*horizontal_alignment,
|
|
|
|
|
*vertical_alignment,
|
|
|
|
|
pixels,
|
|
|
|
|
clip_mask,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Primitive::Image { .. } => {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
Primitive::Svg { .. } => {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
Primitive::Group { primitives } => {
|
|
|
|
|
for primitive in primitives {
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
primitive,
|
|
|
|
|
pixels,
|
|
|
|
|
clip_mask,
|
|
|
|
|
scale_factor,
|
|
|
|
|
translation,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Primitive::Translate {
|
|
|
|
|
translation: offset,
|
|
|
|
|
content,
|
|
|
|
|
} => {
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
content,
|
2023-02-26 00:38:46 +01:00
|
|
|
pixels,
|
|
|
|
|
clip_mask,
|
|
|
|
|
scale_factor,
|
2023-02-26 23:40:17 +01:00
|
|
|
translation + *offset,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Primitive::Clip { bounds, content } => {
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
content,
|
|
|
|
|
pixels,
|
|
|
|
|
Some(&rectangular_clip_mask(
|
|
|
|
|
pixels,
|
|
|
|
|
*bounds * scale_factor,
|
|
|
|
|
)),
|
|
|
|
|
scale_factor,
|
2023-02-26 00:38:46 +01:00
|
|
|
translation,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-26 23:40:17 +01:00
|
|
|
Primitive::Cached { cache } => {
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
cache,
|
|
|
|
|
pixels,
|
|
|
|
|
clip_mask,
|
|
|
|
|
scale_factor,
|
|
|
|
|
translation,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Primitive::SolidMesh { .. } | Primitive::GradientMesh { .. } => {}
|
2023-02-26 00:38:46 +01:00
|
|
|
}
|
2023-02-25 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn into_color(color: Color) -> tiny_skia::Color {
|
2023-02-26 00:49:27 +01:00
|
|
|
tiny_skia::Color::from_rgba(color.b, color.g, color.r, color.a)
|
2023-02-25 16:05:42 +01:00
|
|
|
.expect("Convert color from iced to tiny_skia")
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-27 00:47:53 +01:00
|
|
|
fn rounded_rectangle(
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
border_radius: [f32; 4],
|
|
|
|
|
) -> tiny_skia::Path {
|
|
|
|
|
let [top_left, top_right, bottom_right, bottom_left] = border_radius;
|
|
|
|
|
|
|
|
|
|
if top_left == top_right
|
|
|
|
|
&& top_left == bottom_right
|
|
|
|
|
&& top_left == bottom_left
|
|
|
|
|
&& top_left == bounds.width / 2.0
|
|
|
|
|
&& top_left == bounds.height / 2.0
|
|
|
|
|
{
|
|
|
|
|
return tiny_skia::PathBuilder::from_circle(
|
|
|
|
|
bounds.x + bounds.width / 2.0,
|
|
|
|
|
bounds.y + bounds.height / 2.0,
|
|
|
|
|
top_left,
|
|
|
|
|
)
|
|
|
|
|
.expect("Build circle path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut builder = tiny_skia::PathBuilder::new();
|
|
|
|
|
|
|
|
|
|
builder.move_to(bounds.x + top_left, bounds.y);
|
|
|
|
|
builder.line_to(bounds.x + bounds.width - top_right, bounds.y);
|
|
|
|
|
|
|
|
|
|
if top_right > 0.0 {
|
|
|
|
|
builder.quad_to(
|
|
|
|
|
bounds.x + bounds.width,
|
|
|
|
|
bounds.y,
|
|
|
|
|
bounds.x + bounds.width,
|
|
|
|
|
bounds.y + top_right,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.line_to(
|
|
|
|
|
bounds.x + bounds.width,
|
|
|
|
|
bounds.y + bounds.height - bottom_right,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if bottom_right > 0.0 {
|
|
|
|
|
builder.quad_to(
|
|
|
|
|
bounds.x + bounds.width,
|
|
|
|
|
bounds.y + bounds.height,
|
|
|
|
|
bounds.x + bounds.width - bottom_right,
|
|
|
|
|
bounds.y + bounds.height,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.line_to(bounds.x + bottom_left, bounds.y + bounds.height);
|
|
|
|
|
|
|
|
|
|
if bottom_right > 0.0 {
|
|
|
|
|
builder.quad_to(
|
|
|
|
|
bounds.x,
|
|
|
|
|
bounds.y + bounds.height,
|
|
|
|
|
bounds.x,
|
|
|
|
|
bounds.y + bounds.height - bottom_left,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.line_to(bounds.x, bounds.y + top_left);
|
|
|
|
|
|
|
|
|
|
if top_left > 0.0 {
|
|
|
|
|
builder.quad_to(bounds.x, bounds.y, bounds.x + top_left, bounds.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.finish().expect("Build rounded rectangle path")
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 00:38:46 +01:00
|
|
|
fn rectangular_clip_mask(
|
2023-02-26 00:49:27 +01:00
|
|
|
pixels: &tiny_skia::PixmapMut<'_>,
|
2023-02-26 00:38:46 +01:00
|
|
|
bounds: Rectangle,
|
|
|
|
|
) -> tiny_skia::ClipMask {
|
|
|
|
|
let mut clip_mask = tiny_skia::ClipMask::new();
|
|
|
|
|
|
|
|
|
|
let path = {
|
|
|
|
|
let mut builder = tiny_skia::PathBuilder::new();
|
|
|
|
|
builder.push_rect(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
|
|
|
|
|
|
|
|
builder.finish().unwrap()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clip_mask
|
|
|
|
|
.set_path(
|
|
|
|
|
pixels.width(),
|
|
|
|
|
pixels.height(),
|
|
|
|
|
&path,
|
|
|
|
|
tiny_skia::FillRule::EvenOdd,
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
.expect("Set path of clipping area");
|
|
|
|
|
|
|
|
|
|
clip_mask
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 15:38:25 +01:00
|
|
|
impl iced_graphics::Backend for Backend {
|
|
|
|
|
fn trim_measurements(&mut self) {
|
2023-02-26 23:40:17 +01:00
|
|
|
self.text_pipeline.trim_measurement_cache();
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl backend::Text for Backend {
|
|
|
|
|
const ICON_FONT: Font = Font::Name("Iced-Icons");
|
|
|
|
|
const CHECKMARK_ICON: char = '\u{f00c}';
|
|
|
|
|
const ARROW_DOWN_ICON: char = '\u{e800}';
|
|
|
|
|
|
|
|
|
|
fn default_font(&self) -> Font {
|
|
|
|
|
self.default_font
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_size(&self) -> f32 {
|
|
|
|
|
self.default_text_size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn measure(
|
|
|
|
|
&self,
|
2023-02-26 23:40:17 +01:00
|
|
|
contents: &str,
|
|
|
|
|
size: f32,
|
|
|
|
|
font: Font,
|
|
|
|
|
bounds: Size,
|
2023-02-25 15:38:25 +01:00
|
|
|
) -> (f32, f32) {
|
2023-02-26 23:40:17 +01:00
|
|
|
self.text_pipeline.measure(contents, size, font, bounds)
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hit_test(
|
|
|
|
|
&self,
|
2023-02-26 23:40:17 +01:00
|
|
|
contents: &str,
|
|
|
|
|
size: f32,
|
|
|
|
|
font: Font,
|
|
|
|
|
bounds: Size,
|
|
|
|
|
point: iced_native::Point,
|
|
|
|
|
nearest_only: bool,
|
2023-02-25 15:38:25 +01:00
|
|
|
) -> Option<text::Hit> {
|
2023-02-26 23:40:17 +01:00
|
|
|
self.text_pipeline.hit_test(
|
|
|
|
|
contents,
|
|
|
|
|
size,
|
|
|
|
|
font,
|
|
|
|
|
bounds,
|
|
|
|
|
point,
|
|
|
|
|
nearest_only,
|
|
|
|
|
)
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-26 23:40:17 +01:00
|
|
|
fn load_font(&mut self, font: Cow<'static, [u8]>) {
|
|
|
|
|
self.text_pipeline.load_font(font);
|
2023-02-25 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "image")]
|
|
|
|
|
impl backend::Image for Backend {
|
|
|
|
|
fn dimensions(&self, _handle: &iced_native::image::Handle) -> Size<u32> {
|
|
|
|
|
// TODO
|
|
|
|
|
Size::new(0, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "svg")]
|
|
|
|
|
impl backend::Svg for Backend {
|
|
|
|
|
fn viewport_dimensions(
|
|
|
|
|
&self,
|
|
|
|
|
_handle: &iced_native::svg::Handle,
|
|
|
|
|
) -> Size<u32> {
|
|
|
|
|
// TODO
|
|
|
|
|
Size::new(0, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|