Skip to main content

truce_cpu/
lib.rs

1// `draw_text_fontdue` and the tiny-skia path take several
2// independent geometry / color arguments; bundling them into a
3// struct would obscure the call sites without simplifying any.
4#![allow(clippy::too_many_arguments)]
5
6//! CPU rendering backend for truce plugins (tiny-skia + fontdue).
7//!
8//! Parallels [`truce-gpu`](../truce_gpu/) at the same level of the
9//! crate graph: both implement [`truce_gui_types::RenderBackend`] on
10//! their respective primitives ([`CpuBackend`] uses tiny-skia
11//! software rasterization; `truce_gpu::WgpuBackend` uses wgpu). The
12//! built-in editor (`truce_gui::BuiltinEditor`) holds an internal
13//! `CpuBackend` for its iOS / pre-blit rendering path; `GpuEditor`
14//! routes through `WgpuBackend` for non-iOS.
15//!
16//! Plugin authors don't usually depend on this crate directly -
17//! `truce-gui::default_editor` pulls it in. Custom-editor plugins
18//! (egui / iced / slint) that want a software `RenderBackend` for
19//! testing without a GPU can opt in here.
20
21mod backend;
22pub mod font;
23
24pub use backend::CpuBackend;
25
26/// Extension trait giving [`truce_gui_types::theme::Color`] the
27/// `to_skia` / `to_premultiplied` methods. Lives here (next to the
28/// tiny-skia rasterizer that consumes them) so `truce-gui-types`
29/// stays rasterizer-free.
30pub trait ColorExt {
31    fn to_skia(&self) -> tiny_skia::Color;
32    fn to_premultiplied(&self) -> tiny_skia::PremultipliedColorU8;
33}
34
35impl ColorExt for truce_gui_types::theme::Color {
36    fn to_skia(&self) -> tiny_skia::Color {
37        tiny_skia::Color::from_rgba(self.r, self.g, self.b, self.a)
38            .unwrap_or(tiny_skia::Color::BLACK)
39    }
40
41    fn to_premultiplied(&self) -> tiny_skia::PremultipliedColorU8 {
42        self.to_skia().premultiply().to_color_u8()
43    }
44}