Skip to content
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

add debugging scale #586

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
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
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
}
33 changes: 25 additions & 8 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 All @@ -25,7 +26,7 @@ mod img;

pub trait SceneDrawer<B: RenderBackend, L: Layouter, LT: LayoutTree<L>> {
fn draw(&mut self, backend: &mut B, data: &mut B::WindowData<'_>, size: SizeU32);
fn mouse_move(&mut self, backend: &mut B, data: &mut B::WindowData<'_>, x: FP, y: FP) -> bool;
fn mouse_move(&mut self, backend: &mut B, x: FP, y: FP) -> bool;

fn scroll(&mut self, point: Point);
fn from_url(url: Url, layouter: L, debug: bool) -> Result<Self>
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,17 +110,33 @@ 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 {
fn mouse_move(&mut self, _backend: &mut B, x: FP, y: FP) -> bool {
let x = x - self
.scene_transform
.clone()
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");
10 changes: 4 additions & 6 deletions crates/gosub_useragent/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ impl<'a, D: SceneDrawer<B, L, LT>, B: RenderBackend, L: Layouter, LT: LayoutTree
return Ok(());
};

if tab.data.mouse_move(
backend,
&mut self.renderer_data,
position.x as FP,
position.y as FP,
) {
if tab
.data
.mouse_move(backend, position.x as FP, position.y as FP)
{
self.window.request_redraw();
}
}
Expand Down
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;
114 changes: 114 additions & 0 deletions crates/gosub_vello/src/debug/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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,
&[],
)
}

#[allow(clippy::too_many_arguments)]
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