🔧 Refactor RenderState scale calculation

This commit is contained in:
Elena Torro 2025-03-20 14:36:45 +01:00
parent 2d61644b05
commit e60e36a0e2
4 changed files with 29 additions and 28 deletions

View file

@ -378,14 +378,10 @@ impl RenderState {
for stroke in shape.strokes().rev() { for stroke in shape.strokes().rev() {
strokes::render(self, &shape, stroke); strokes::render(self, &shape, stroke);
} }
let scale = self.get_scale();
for shadow in shape.inner_shadows().rev().filter(|s| !s.hidden()) { for shadow in shape.inner_shadows().rev().filter(|s| !s.hidden()) {
shadows::render_inner_shadow( shadows::render_inner_shadow(self, shadow, scale, shape.fills().len() > 0);
self,
shadow,
self.viewbox.zoom * self.options.dpr(),
shape.fills().len() > 0,
);
} }
shadows::render_drop_shadows(self, &shape); shadows::render_drop_shadows(self, &shape);
@ -419,14 +415,12 @@ impl RenderState {
self.cancel_animation_frame(frame_id); self.cancel_animation_frame(frame_id);
} }
} }
let scale = self.get_scale();
self.reset_canvas(); self.reset_canvas();
self.surfaces.apply_mut( self.surfaces.apply_mut(
&[SurfaceId::Fills, SurfaceId::Strokes, SurfaceId::DropShadows], &[SurfaceId::Fills, SurfaceId::Strokes, SurfaceId::DropShadows],
|s| { |s| {
s.canvas().scale(( s.canvas().scale((scale, scale));
self.viewbox.zoom * self.options.dpr(),
self.viewbox.zoom * self.options.dpr(),
));
}, },
); );
@ -517,7 +511,7 @@ impl RenderState {
.save_layer(&mask_rec); .save_layer(&mask_rec);
} }
if let Some(image_filter) = element.image_filter(self.viewbox.zoom * self.options.dpr()) { if let Some(image_filter) = element.image_filter(self.get_scale()) {
paint.set_image_filter(image_filter); paint.set_image_filter(image_filter);
} }
@ -546,9 +540,9 @@ impl RenderState {
pub fn get_current_tile_bounds(&mut self) -> Rect { pub fn get_current_tile_bounds(&mut self) -> Rect {
let (tile_x, tile_y) = self.current_tile.unwrap(); let (tile_x, tile_y) = self.current_tile.unwrap();
let zoom = self.viewbox.zoom * self.options.dpr(); let scale = self.get_scale();
let offset_x = self.viewbox.area.left * zoom; let offset_x = self.viewbox.area.left * scale;
let offset_y = self.viewbox.area.top * zoom; let offset_y = self.viewbox.area.top * scale;
Rect::from_xywh( Rect::from_xywh(
(tile_x as f32 * tiles::TILE_SIZE) - offset_x, (tile_x as f32 * tiles::TILE_SIZE) - offset_x,
(tile_y as f32 * tiles::TILE_SIZE) - offset_y, (tile_y as f32 * tiles::TILE_SIZE) - offset_y,
@ -782,4 +776,8 @@ impl RenderState {
} }
} }
} }
pub fn get_scale(&self) -> f32 {
self.viewbox.zoom() * self.options.dpr()
}
} }

View file

@ -9,7 +9,7 @@ pub fn render_drop_shadows(render_state: &mut RenderState, shape: &Shape) {
render_fill_drop_shadow(render_state, &shape, &shadow); render_fill_drop_shadow(render_state, &shape, &shadow);
} }
} else { } else {
let scale = render_state.viewbox.zoom * render_state.options.dpr(); let scale = render_state.get_scale();
for shadow in shape.drop_shadows().rev().filter(|s| !s.hidden()) { for shadow in shape.drop_shadows().rev().filter(|s| !s.hidden()) {
render_stroke_drop_shadow(render_state, &shadow, scale); render_stroke_drop_shadow(render_state, &shadow, scale);
} }

View file

@ -154,7 +154,7 @@ fn handle_stroke_caps(
canvas: &skia::Canvas, canvas: &skia::Canvas,
is_open: bool, is_open: bool,
svg_attrs: &HashMap<String, String>, svg_attrs: &HashMap<String, String>,
dpr_scale: f32, scale: f32,
) { ) {
let points_count = path.count_points(); let points_count = path.count_points();
let mut points = vec![Point::default(); points_count]; let mut points = vec![Point::default(); points_count];
@ -165,7 +165,7 @@ fn handle_stroke_caps(
let first_point = points.first().unwrap(); let first_point = points.first().unwrap();
let last_point = points.last().unwrap(); let last_point = points.last().unwrap();
let mut paint_stroke = stroke.to_stroked_paint(is_open, selrect, svg_attrs, dpr_scale); let mut paint_stroke = stroke.to_stroked_paint(is_open, selrect, svg_attrs, scale);
handle_stroke_cap( handle_stroke_cap(
canvas, canvas,
@ -332,11 +332,11 @@ fn draw_image_stroke_in_container(
} }
let size = image_fill.size(); let size = image_fill.size();
let scale = render_state.get_scale();
let canvas = render_state.surfaces.canvas(SurfaceId::Fills); let canvas = render_state.surfaces.canvas(SurfaceId::Fills);
let container = &shape.selrect; let container = &shape.selrect;
let path_transform = shape.to_path_transform(); let path_transform = shape.to_path_transform();
let svg_attrs = &shape.svg_attrs; let svg_attrs = &shape.svg_attrs;
let dpr_scale = render_state.viewbox.zoom * render_state.options.dpr();
// Save canvas and layer state // Save canvas and layer state
let mut pb = skia::Paint::default(); let mut pb = skia::Paint::default();
@ -358,11 +358,11 @@ fn draw_image_stroke_in_container(
&outer_rect, &outer_rect,
&shape_type.corners(), &shape_type.corners(),
svg_attrs, svg_attrs,
dpr_scale, scale,
); );
} }
Type::Circle => { Type::Circle => {
draw_stroke_on_circle(canvas, stroke, container, &outer_rect, svg_attrs, dpr_scale) draw_stroke_on_circle(canvas, stroke, container, &outer_rect, svg_attrs, scale)
} }
shape_type @ (Type::Path(_) | Type::Bool(_)) => { shape_type @ (Type::Path(_) | Type::Bool(_)) => {
@ -381,12 +381,12 @@ fn draw_image_stroke_in_container(
} }
} }
let is_open = p.is_open(); let is_open = p.is_open();
let mut paint = stroke.to_stroked_paint(is_open, &outer_rect, svg_attrs, dpr_scale); let mut paint = stroke.to_stroked_paint(is_open, &outer_rect, svg_attrs, scale);
canvas.draw_path(&path, &paint); canvas.draw_path(&path, &paint);
if stroke.render_kind(is_open) == StrokeKind::OuterStroke { if stroke.render_kind(is_open) == StrokeKind::OuterStroke {
// Small extra inner stroke to overlap with the fill // Small extra inner stroke to overlap with the fill
// and avoid unnecesary artifacts. // and avoid unnecesary artifacts.
paint.set_stroke_width(1. / dpr_scale); paint.set_stroke_width(1. / scale);
canvas.draw_path(&path, &paint); canvas.draw_path(&path, &paint);
} }
handle_stroke_caps( handle_stroke_caps(
@ -396,7 +396,7 @@ fn draw_image_stroke_in_container(
canvas, canvas,
is_open, is_open,
svg_attrs, svg_attrs,
dpr_scale, scale,
); );
canvas.restore(); canvas.restore();
} }
@ -437,8 +437,8 @@ fn draw_image_stroke_in_container(
* This SHOULD be the only public function in this module. * This SHOULD be the only public function in this module.
*/ */
pub fn render(render_state: &mut RenderState, shape: &Shape, stroke: &Stroke) { pub fn render(render_state: &mut RenderState, shape: &Shape, stroke: &Stroke) {
let scale = render_state.get_scale();
let canvas = render_state.surfaces.canvas(SurfaceId::Strokes); let canvas = render_state.surfaces.canvas(SurfaceId::Strokes);
let dpr_scale = render_state.viewbox.zoom * render_state.options.dpr();
let selrect = shape.selrect; let selrect = shape.selrect;
let path_transform = shape.to_path_transform(); let path_transform = shape.to_path_transform();
let svg_attrs = &shape.svg_attrs; let svg_attrs = &shape.svg_attrs;
@ -455,15 +455,14 @@ pub fn render(render_state: &mut RenderState, shape: &Shape, stroke: &Stroke) {
&selrect, &selrect,
&shape_type.corners(), &shape_type.corners(),
svg_attrs, svg_attrs,
dpr_scale, scale,
); );
} }
Type::Circle => { Type::Circle => {
draw_stroke_on_circle(canvas, stroke, &selrect, &selrect, &svg_attrs, dpr_scale) draw_stroke_on_circle(canvas, stroke, &selrect, &selrect, svg_attrs, scale)
} }
shape_type @ (Type::Path(_) | Type::Bool(_)) => { shape_type @ (Type::Path(_) | Type::Bool(_)) => {
if let Some(path) = shape_type.path() { if let Some(path) = shape_type.path() {
let svg_attrs = &shape.svg_attrs;
draw_stroke_on_path( draw_stroke_on_path(
canvas, canvas,
stroke, stroke,
@ -471,7 +470,7 @@ pub fn render(render_state: &mut RenderState, shape: &Shape, stroke: &Stroke) {
&selrect, &selrect,
path_transform.as_ref(), path_transform.as_ref(),
svg_attrs, svg_attrs,
dpr_scale, scale,
); );
} }
} }

View file

@ -51,4 +51,8 @@ impl Viewbox {
self.area self.area
.set_wh(self.width / self.zoom, self.height / self.zoom); .set_wh(self.width / self.zoom, self.height / self.zoom);
} }
pub fn zoom(&self) -> f32 {
self.zoom
}
} }