-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab4fd1c
commit 64ee053
Showing
13 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod scale; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod debug; | ||
pub mod draw; | ||
pub mod render_tree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod text; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters