This commit is contained in:
Blaž Hrastnik 2022-04-13 13:05:58 +09:00
parent 3b2b9e102d
commit 9438430e98
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640
4 changed files with 94 additions and 51 deletions

11
Cargo.lock generated
View File

@ -512,8 +512,6 @@ dependencies = [
[[package]] [[package]]
name = "femtovg" name = "femtovg"
version = "0.3.3" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f95b1b0a3a4dc9bb7f866b2d6efc9c5c5f5bccefc21e40be75dfc2c12cb6ef8"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"fnv", "fnv",
@ -920,6 +918,7 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"femtovg", "femtovg",
"glutin", "glutin",
"helix-view",
"image", "image",
"instant", "instant",
"resource", "resource",
@ -1406,9 +1405,9 @@ dependencies = [
[[package]] [[package]]
name = "ouroboros" name = "ouroboros"
version = "0.14.2" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71643f290d126e18ac2598876d01e1d57aed164afc78fdb6e2a0c6589a1f6662" checksum = "9f31a3b678685b150cba82b702dcdc5e155893f63610cf388d30cd988d4ca2bf"
dependencies = [ dependencies = [
"aliasable", "aliasable",
"ouroboros_macro", "ouroboros_macro",
@ -1417,9 +1416,9 @@ dependencies = [
[[package]] [[package]]
name = "ouroboros_macro" name = "ouroboros_macro"
version = "0.14.2" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9a247206016d424fe8497bc611e510887af5c261fbbf977877c4bb55ca4d82" checksum = "084fd65d5dd8b3772edccb5ffd1e4b7eba43897ecd0f9401e330e8c542959408"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"proc-macro-error", "proc-macro-error",

View File

@ -58,7 +58,7 @@
{ name = "HELIX_RUNTIME"; eval = "$PWD/runtime"; } { name = "HELIX_RUNTIME"; eval = "$PWD/runtime"; }
{ name = "RUST_BACKTRACE"; value = "1"; } { name = "RUST_BACKTRACE"; value = "1"; }
# { name = "RUSTFLAGS"; value = "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment"; } # { name = "RUSTFLAGS"; value = "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment"; }
{ name = "LD_LIBRARY_PATH"; value = nixpkgs.lib.makeLibraryPath (with common.pkgs; [ wayland libxkbcommon libGL ]); } { name = "LD_LIBRARY_PATH"; value = nixpkgs.lib.makeLibraryPath (with common.pkgs; [ wayland libxkbcommon libGL xorg.libxcb ]); }
]; ];
}; };
}; };

View File

@ -6,7 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
femtovg = "0.3.3" helix-view = { version = "0.6", path = "../helix-view", features = ["ui"] }
femtovg = { path = "../../femtovg" }
winit = { version = "0.26.1", default-features = false, features = ["wayland"] } # TODO: figure out wayland-dlopen winit = { version = "0.26.1", default-features = false, features = ["wayland"] } # TODO: figure out wayland-dlopen
resource = "0.5.0" resource = "0.5.0"
image = { version = "0.24.0", default-features = false, features = ["jpeg", "png"] } image = { version = "0.24.0", default-features = false, features = ["jpeg", "png"] }

View File

@ -21,9 +21,18 @@
Solidity, Solidity,
}; };
// mezzopiano // new femto-like framework:
// — // wgpu renderer
// 03/13/2022 // kurbo, (alternative is euclid + lyon)
// vector math: glam? and drop euclid (glam is faster https://docs.rs/glam/latest/glam/)
// swash + parley for text
// imgref, bitflags
// fnv, rgb
// resource, image
// usvg for svg
// I'm also assuming that there's some logic bugs in the demo application, which wasn't built with this in mind; I have a much simpler application that I'm happy to show if that would be helpful (would need to extract an example). As a rough solution, I apply the following transformation on Winit's WindowEvent::ScaleFactorChanged: // I'm also assuming that there's some logic bugs in the demo application, which wasn't built with this in mind; I have a much simpler application that I'm happy to show if that would be helpful (would need to extract an example). As a rough solution, I apply the following transformation on Winit's WindowEvent::ScaleFactorChanged:
// /* ... in an application struct/impl ... */ // /* ... in an application struct/impl ... */
@ -48,8 +57,6 @@
// ) // )
// } // }
// With this, the canvas position and scale is preserved while the window is moved across screens, but as I'd like to apply further translations and keeping track of everything is getting very hard 😅 . I'm wondering if I'm making a mistake somewhere, or if there might be some way to do this in femto.
pub fn quantize(a: f32, d: f32) -> f32 { pub fn quantize(a: f32, d: f32) -> f32 {
(a / d + 0.5).trunc() * d (a / d + 0.5).trunc() * d
} }
@ -57,6 +64,7 @@ pub fn quantize(a: f32, d: f32) -> f32 {
struct Fonts { struct Fonts {
regular: FontId, regular: FontId,
bold: FontId, bold: FontId,
code: FontId,
icons: FontId, icons: FontId,
} }
@ -120,11 +128,14 @@ fn main() {
// TODO: better femtovg support for variable fonts // TODO: better femtovg support for variable fonts
let fonts = Fonts { let fonts = Fonts {
regular: canvas regular: canvas
.add_font_mem(&resource!("assets/fonts/Inter\ Variable/Inter.ttf")) .add_font_mem(&resource!("assets/fonts/Inter Variable/Inter.ttf"))
.expect("Cannot add font"), .expect("Cannot add font"),
bold: canvas bold: canvas
.add_font_mem(&resource!("assets/fonts/Inter Variable/Inter.ttf")) .add_font_mem(&resource!("assets/fonts/Inter Variable/Inter.ttf"))
.expect("Cannot add font"), .expect("Cannot add font"),
code: canvas
.add_font_mem(&resource!("assets/fonts/Fira Code/FiraCode-VF.ttf"))
.expect("Cannot add font"),
icons: canvas icons: canvas
.add_font_mem(&resource!("assets/entypo.ttf")) .add_font_mem(&resource!("assets/entypo.ttf"))
.expect("Cannot add font"), .expect("Cannot add font"),
@ -148,6 +159,10 @@ fn main() {
let mut screenshot_image_id = None; let mut screenshot_image_id = None;
//
//
let start = Instant::now(); let start = Instant::now();
let mut prevt = start; let mut prevt = start;
@ -157,6 +172,14 @@ fn main() {
let mut perf = PerfGraph::new(); let mut perf = PerfGraph::new();
{
#[cfg(not(target_arch = "wasm32"))]
let window = windowed_context.window();
let dpi_factor = window.scale_factor();
canvas.set_size(0, 0, dpi_factor as f32);
canvas.reset();
}
el.run(move |event, _, control_flow| { el.run(move |event, _, control_flow| {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let window = windowed_context.window(); let window = windowed_context.window();
@ -168,9 +191,11 @@ fn main() {
Event::WindowEvent { ref event, .. } => match event { Event::WindowEvent { ref event, .. } => match event {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
WindowEvent::Resized(physical_size) => { WindowEvent::Resized(physical_size) => {
println!("resized!");
// TODO: use DPI here?
windowed_context.resize(*physical_size); windowed_context.resize(*physical_size);
let dpi_factor = window.scale_factor();
canvas.set_size(0, 0, dpi_factor as f32);
canvas.reset();
} }
WindowEvent::CursorMoved { WindowEvent::CursorMoved {
device_id: _, device_id: _,
@ -178,48 +203,48 @@ fn main() {
.. ..
} => { } => {
if dragging { if dragging {
let p0 = canvas // let p0 = canvas
.transform() // .transform()
.inversed() // .inversed()
.transform_point(mousex, mousey); // .transform_point(mousex, mousey);
let p1 = canvas // let p1 = canvas
.transform() // .transform()
.inversed() // .inversed()
.transform_point(position.x as f32, position.y as f32); // .transform_point(position.x as f32, position.y as f32);
canvas.translate(p1.0 - p0.0, p1.1 - p0.1); // canvas.translate(p1.0 - p0.0, p1.1 - p0.1);
} }
mousex = position.x as f32; mousex = position.x as f32;
mousey = position.y as f32; mousey = position.y as f32;
} }
WindowEvent::MouseWheel { // WindowEvent::MouseWheel {
device_id: _, // device_id: _,
delta, // delta,
.. // ..
} => match delta { // } => match delta {
winit::event::MouseScrollDelta::LineDelta(_, y) => { // winit::event::MouseScrollDelta::LineDelta(_, y) => {
let pt = canvas // let pt = canvas
.transform() // .transform()
.inversed() // .inversed()
.transform_point(mousex, mousey); // .transform_point(mousex, mousey);
canvas.translate(pt.0, pt.1); // canvas.translate(pt.0, pt.1);
canvas.scale(1.0 + (y / 10.0), 1.0 + (y / 10.0)); // canvas.scale(1.0 + (y / 10.0), 1.0 + (y / 10.0));
canvas.translate(-pt.0, -pt.1); // canvas.translate(-pt.0, -pt.1);
} // }
winit::event::MouseScrollDelta::PixelDelta(pos) => { // winit::event::MouseScrollDelta::PixelDelta(pos) => {
let y = pos.y as f32; // let y = pos.y as f32;
let pt = canvas // let pt = canvas
.transform() // .transform()
.inversed() // .inversed()
.transform_point(mousex, mousey); // .transform_point(mousex, mousey);
let rate = 2000.0; // let rate = 2000.0;
canvas.translate(pt.0, pt.1); // canvas.translate(pt.0, pt.1);
canvas.scale(1.0 + (y / rate), 1.0 + (y / rate)); // canvas.scale(1.0 + (y / rate), 1.0 + (y / rate));
canvas.translate(-pt.0, -pt.1); // canvas.translate(-pt.0, -pt.1);
} // }
}, // },
WindowEvent::MouseInput { WindowEvent::MouseInput {
button: MouseButton::Left, button: MouseButton::Left,
state, state,
@ -291,6 +316,8 @@ fn main() {
let rel_mousex = pt.0; let rel_mousex = pt.0;
let rel_mousey = pt.1; let rel_mousey = pt.1;
draw_code(&mut canvas, fonts.code, 25.0, 500.0);
draw_paragraph( draw_paragraph(
&mut canvas, &mut canvas,
fonts.regular, fonts.regular,
@ -421,6 +448,22 @@ fn main() {
}); });
} }
fn draw_code<T: Renderer>(canvas: &mut Canvas<T>, font: FontId, x: f32, y: f32) {
let text = "canvas.fill_text(x, y, &text[line_range], paint)";
canvas.save();
let mut paint = Paint::color(Color::rgba(255, 255, 255, 255));
paint.set_font_size(14.0);
paint.set_font(&[font]);
paint.set_text_align(Align::Left);
paint.set_text_baseline(Baseline::Top);
canvas.fill_text(x, y, text, paint).unwrap();
canvas.restore();
}
fn draw_paragraph<T: Renderer>( fn draw_paragraph<T: Renderer>(
canvas: &mut Canvas<T>, canvas: &mut Canvas<T>,
font: FontId, font: FontId,