Skip to content

Commit

Permalink
add debugging scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Sep 7, 2024
1 parent ab4fd1c commit 64ee053
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 12 deletions.
4 changes: 3 additions & 1 deletion crates/gosub_render_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ pub trait RenderBackend: Sized + Debug {
pub trait Scene<B: RenderBackend> {
fn draw_rect(&mut self, rect: &RenderRect<B>);
fn draw_text(&mut self, text: &RenderText<B>);

fn debug_draw_simple_text(&mut self, text: &str, pos: Point, size: FP);
fn apply_scene(&mut self, scene: &B::Scene, transform: Option<B::Transform>);
fn reset(&mut self);

fn new(data: &mut B::WindowData<'_>) -> Self;
fn new() -> Self;
}

pub struct RenderRect<B: RenderBackend> {
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_render_backend/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{ImageBuffer, RenderBackend};
pub trait SvgRenderer<B: RenderBackend> {
type SvgDocument;

fn new(wd: &mut B::WindowData<'_>) -> Self;
fn new() -> Self;

fn parse_external(data: String) -> Result<Self::SvgDocument>;
fn parse_internal(tree: DocumentHandle, id: NodeId) -> Result<Self::SvgDocument>;
Expand Down
1 change: 1 addition & 0 deletions crates/gosub_renderer/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod scale;
54 changes: 54 additions & 0 deletions crates/gosub_renderer/src/debug/scale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use gosub_render_backend::{
Brush, Color, Point, Rect, RenderBackend, RenderRect, Scene, SizeU32, Transform,
};
use std::cmp::max;
use std::f32::consts::PI;

pub fn px_scale<B: RenderBackend>(size: SizeU32, offset: Point, width: f32) -> B::Scene {
let mut scene = B::Scene::new();

let len = max(
size.width as i32 - offset.x as i32,
size.height as i32 - offset.y as i32,
) as u32;

let scale = draw_scale::<B>(len, 50);

let transform = B::Transform::translate(offset.x, 0.0);

scene.apply_scene(&scale, Some(transform));

let transform = B::Transform::translate(width, offset.y).pre_rotate(PI / 2.0);

scene.apply_scene(&scale, Some(transform));

scene
}

pub fn draw_scale<B: RenderBackend>(len: u32, interval: u32) -> B::Scene {
let mut scene = B::Scene::new();

let mut x = 0;

while x < len {
let mut height = 50.0;

if x % 100 == 0 {
height = 60.0;
}

scene.draw_rect(&RenderRect {
rect: B::Rect::new(x as f32, 0.0, 2.0, height),
transform: None,
radius: None,
brush: B::Brush::color(Color::BLACK),
brush_transform: None,
border: None,
});

scene.debug_draw_simple_text(&format!("{}", x), Point::new(x as f32, height + 10.0), 12.0);
x += interval;
}

scene
}
29 changes: 23 additions & 6 deletions crates/gosub_renderer/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::mpsc::Sender;
use anyhow::anyhow;
use url::Url;

use crate::debug::scale::px_scale;
use crate::draw::img::request_img;
use crate::render_tree::{load_html_rendertree, TreeDrawer};
use gosub_css3::colors::RgbColor;
Expand Down Expand Up @@ -61,7 +62,7 @@ where
if self.tree_scene.is_none() || self.size != Some(size) {
self.size = Some(size);

let mut scene = B::Scene::new(data);
let mut scene = B::Scene::new();

// Apply new maximums to the scene transform
if let Some(scene_transform) = self.scene_transform.as_mut() {
Expand All @@ -78,7 +79,7 @@ where
let mut drawer = Drawer {
scene: &mut scene,
drawer: self,
svg: B::SVGRenderer::new(data),
svg: B::SVGRenderer::new(),
};

drawer.render(size);
Expand Down Expand Up @@ -109,14 +110,30 @@ where

if self.dirty {
if let Some(id) = self.selected_element {
self.debug_annotate(id, data);
self.debug_annotate(id);
}
}

if let Some(scene) = &self.debugger_scene {
self.dirty = false;
backend.apply_scene(data, scene, self.scene_transform.clone());
}

if self.debug {
let pos = self
.scene_transform
.as_ref()
.map(|x| Point::new(x.tx(), x.ty()))
.unwrap_or(Point::ZERO);

let scale = px_scale::<B>(
size,
pos,
self.size.as_ref().map(|x| x.width as f32).unwrap_or(0.0),
);

backend.apply_scene(data, &scale, None);
}
}

fn mouse_move(&mut self, _backend: &mut B, data: &mut B::WindowData<'_>, x: FP, y: FP) -> bool {
Expand All @@ -135,7 +152,7 @@ where
if self.last_hover != Some(e) {
self.last_hover = Some(e);
if self.debug {
return self.debug_annotate(e, data);
return self.debug_annotate(e);
}
}
return false;
Expand Down Expand Up @@ -740,12 +757,12 @@ fn get_border_side<B: RenderBackend, L: Layouter>(
}

impl<B: RenderBackend, L: Layouter> TreeDrawer<B, L> {
fn debug_annotate(&mut self, e: NodeId, data: &mut B::WindowData<'_>) -> bool {
fn debug_annotate(&mut self, e: NodeId) -> bool {
let Some(node) = self.tree.get_node(e) else {
return false;
};

let mut scene = B::Scene::new(data);
let mut scene = B::Scene::new();

let Some(layout) = self.tree.get_layout(e) else {
return false;
Expand Down
1 change: 1 addition & 0 deletions crates/gosub_renderer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod debug;
pub mod draw;
pub mod render_tree;
2 changes: 1 addition & 1 deletion crates/gosub_svg/src/resvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Resvg;
impl<B: RenderBackend> SvgRenderer<B> for Resvg {
type SvgDocument = SVGDocument;

fn new(_: &mut B::WindowData<'_>) -> Self {
fn new() -> Self {
Self
}

Expand Down
2 changes: 2 additions & 0 deletions crates/gosub_typeface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,5 @@
// }

pub mod font;

pub const ROBOTO_FONT: &[u8] = include_bytes!("../../../resources/fonts/Roboto-Regular.ttf");
1 change: 1 addition & 0 deletions crates/gosub_vello/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod text;
113 changes: 113 additions & 0 deletions crates/gosub_vello/src/debug/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::{Brush, Color, Scene, Transform};
use gosub_render_backend::{Brush as _, Color as _, Transform as _};
use gosub_shared::types::Point;
use gosub_typeface::ROBOTO_FONT;
use std::sync::{Arc, LazyLock};
use vello::glyph::Glyph;
use vello::peniko::{Blob, BrushRef, Fill, Font, Style, StyleRef};
use vello::skrifa;
use vello::skrifa::{FontRef, MetadataProvider};

static FONT: LazyLock<Font> = LazyLock::new(|| Font::new(Blob::new(Arc::new(ROBOTO_FONT)), 0));

pub fn render_text_simple(scene: &mut Scene, text: &str, point: Point<f32>, font_size: f32) {
render_text(
scene,
text,
point,
font_size,
&*FONT,
&Brush::color(Color::BLACK),
&Style::Fill(Fill::NonZero),
);
}

pub fn render_text<'a>(
scene: &mut Scene,
text: &str,
point: Point<f32>,
font_size: f32,
font: &Font,
brush: &Brush,
style: impl Into<StyleRef<'a>>,
) {
let transform = Transform::translate(point.x, point.y);

render_text_var(
scene,
text,
font_size,
font,
brush,
transform,
Transform::IDENTITY,
style,
&[],
)
}

pub fn render_text_var<'a>(
scene: &mut Scene,
text: &str,
font_size: f32,
font: &Font,
brush: &Brush,
transform: Transform,
glyph_transform: Transform,
style: impl Into<StyleRef<'a>>,
vars: &[(&str, f32)],
) {
let Some(font_ref) = to_font_ref(font) else {
return;
};
let brush: BrushRef = (&brush.0).into();
let style = style.into();
let axes = font_ref.axes();
let var_loc = axes.location(vars.iter().copied());
let charmap = font_ref.charmap();

let fs = skrifa::instance::Size::new(font_size);

let metrics = font_ref.metrics(fs, &var_loc);
let line_height = metrics.ascent - metrics.descent + metrics.leading;
let glyph_metrics = font_ref.glyph_metrics(fs, &var_loc);
let mut pen_x = 0f32;
let mut pen_y = 0f32;
scene
.0
.draw_glyphs(font)
.font_size(font_size)
.transform(transform.0)
.glyph_transform(Some(glyph_transform.0))
.normalized_coords(var_loc.coords())
.brush(brush)
.hint(false)
.draw(
style,
text.chars().filter_map(|ch| {
if ch == '\n' {
pen_y += line_height;
pen_x = 0.0;
return None;
}
let gid = charmap.map(ch).unwrap_or_default();
let advance = glyph_metrics.advance_width(gid).unwrap_or_default();
let x = pen_x;
pen_x += advance;
Some(Glyph {
id: gid.to_u16() as u32,
x,
y: pen_y,
})
}),
);
}

fn to_font_ref(font: &Font) -> Option<FontRef<'_>> {
use vello::skrifa::raw::FileRef;
let file_ref = FileRef::new(font.data.as_ref()).ok()?;
match file_ref {
FileRef::Font(font) => Some(font),
FileRef::Collection(collection) => collection.get(font.index).ok(),
}
}
2 changes: 2 additions & 0 deletions crates/gosub_vello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ mod scene;
mod text;
mod transform;

mod debug;
#[cfg(feature = "vello_svg")]
mod vello_svg;

pub struct VelloBackend;

impl Debug for VelloBackend {
Expand Down
13 changes: 11 additions & 2 deletions crates/gosub_vello/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use vello::kurbo::RoundedRect;
use vello::peniko::Fill;
use vello::Scene as VelloScene;

use gosub_render_backend::{RenderBackend, RenderRect, RenderText, Scene as TScene};
use gosub_render_backend::{Point, RenderBackend, RenderRect, RenderText, Scene as TScene, FP};

use crate::debug::text::render_text_simple;
use crate::{Border, BorderRenderOptions, Text, Transform, VelloBackend};

pub struct Scene(pub(crate) VelloScene);
Expand All @@ -12,6 +13,10 @@ impl Scene {
pub fn inner(&mut self) -> &mut VelloScene {
&mut self.0
}

pub fn create() -> Self {
Self(VelloScene::new())
}
}

impl TScene<VelloBackend> for Scene {
Expand Down Expand Up @@ -46,6 +51,10 @@ impl TScene<VelloBackend> for Scene {
Text::show(&mut self.0, text)
}

fn debug_draw_simple_text(&mut self, text: &str, pos: Point, size: FP) {
render_text_simple(self, text, pos, size)
}

fn apply_scene(
&mut self,
scene: &<VelloBackend as RenderBackend>::Scene,
Expand All @@ -62,7 +71,7 @@ impl TScene<VelloBackend> for Scene {
self.0.reset()
}

fn new(_data: &mut <VelloBackend as RenderBackend>::WindowData<'_>) -> Self {
fn new() -> Self {
VelloScene::new().into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_vello/src/vello_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct VelloSVG;
impl SvgRenderer<VelloBackend> for VelloSVG {
type SvgDocument = SVGDocument;

fn new(_: &mut WindowData) -> Self {
fn new() -> Self {
Self
}

Expand Down

0 comments on commit 64ee053

Please sign in to comment.