Skip to content

fix(skia renderer): fix image rendering and render_custom_element parameter #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions src/renderers/skia.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::math::BoundingBox;
use crate::render_commands::{RenderCommand, RenderCommandConfig};
use crate::Color as ClayColor;
use skia_safe::{Canvas, ClipOp, Color, Font, Image, Paint, Point, RRect, Rect};
use crate::math::{BoundingBox, Dimensions};
use crate::render_commands::{Custom, RenderCommand, RenderCommandConfig};
use crate::text::TextConfig;
use crate::{ClayLayoutScope, Color as ClayColor};
use skia_safe::{
Canvas, ClipOp, Color, Font, Image, Paint, Point, RRect, Rect, SamplingOptions, Typeface,
};

fn clay_to_skia_color(color: ClayColor) -> Color {
pub fn clay_to_skia_color(color: ClayColor) -> Color {
Color::from_argb(
(color.a).round() as u8,
(color.r).round() as u8,
Expand All @@ -19,28 +22,41 @@ fn clay_to_skia_rect(rect: BoundingBox) -> Rect {
pub fn clay_skia_render<'a, CustomElementData: 'a>(
canvas: &Canvas,
render_commands: impl Iterator<Item = RenderCommand<'a, Image, CustomElementData>>,
mut render_custom_element: impl FnMut(&'a CustomElementData, &Canvas),
mut render_custom_element: impl FnMut(
&RenderCommand<'a, Image, CustomElementData>,
&Custom<'a, CustomElementData>,
&Canvas,
),
fonts: &[&Typeface],
) {
for command in render_commands {
match command.config {
RenderCommandConfig::Text(text) => {
let text_data = text.text;
let pos = Point::new(command.bounding_box.x, command.bounding_box.y);
let mut paint = Paint::default();
paint.set_color(clay_to_skia_color(text.color));
let mut font = Font::default();
font.set_size(text.font_size as f32);
let font = Font::new(fonts[text.font_id as usize].clone(), text.font_size as f32);
let pos = Point::new(
command.bounding_box.x,
command.bounding_box.y + text.font_size as f32,
);
canvas.draw_str(&text_data, pos, &font, &paint);
}

RenderCommandConfig::Image(image) => {
let skia_image = image.data;
let mut paint = Paint::default();
paint.set_color(Color::WHITE);
canvas.draw_image_rect(
paint.set_anti_alias(true);

canvas.draw_image_rect_with_sampling_options(
skia_image,
None,
clay_to_skia_rect(command.bounding_box),
SamplingOptions::new(
skia_safe::FilterMode::Linear,
skia_safe::MipmapMode::Linear,
),
&paint,
);
}
Expand Down Expand Up @@ -230,8 +246,31 @@ pub fn clay_skia_render<'a, CustomElementData: 'a>(
);
}
}
RenderCommandConfig::Custom(custom) => render_custom_element(custom.data, canvas),
RenderCommandConfig::Custom(ref custom) => {
render_custom_element(&command, custom, canvas)
}
RenderCommandConfig::None() => {}
}
}
}

pub type SkiaClayScope<'clay, 'render, CustomElements> =
ClayLayoutScope<'clay, 'render, Image, CustomElements>;

pub fn get_source_dimensions_from_skia_image(image: &Image) -> Dimensions {
(image.width() as f32, image.height() as f32).into()
}

pub fn create_measure_text_function(
fonts: &'static [&Typeface],
) -> impl Fn(&str, &TextConfig) -> Dimensions + 'static {
|text, text_config| {
let font = Font::new(
fonts[text_config.font_id as usize],
text_config.font_size as f32,
);
let width = font.measure_str(text, None).0;
dbg!(text, width);
(width, font.metrics().1.bottom - font.metrics().1.top).into()
}
}
Loading