Skip to content

Commit

Permalink
sugarloaf text background stabilization for monospaced fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed May 13, 2023
1 parent 89bafc4 commit 68377b9
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 95 deletions.
2 changes: 1 addition & 1 deletion rio/src/crosswords/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Default for Square {
fn default() -> Square {
Square {
c: ' ',
bg: AnsiColor::Named(NamedColor::Black),
bg: AnsiColor::Named(NamedColor::Background),
fg: AnsiColor::Named(NamedColor::Foreground),
extra: None,
flags: Flags::empty(),
Expand Down
6 changes: 3 additions & 3 deletions rio/src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl Screen {

#[inline]
pub fn skeleton(&mut self, color: colors::ColorWGPU) {
self.sugarloaf.skeleton(color);
self.sugarloaf.init(color, self.layout.styles.term);
}

#[inline]
pub fn render(&mut self, color: colors::ColorWGPU) {
pub fn render(&mut self) {
let mut terminal = self.terminal.lock();
let visible_rows = terminal.visible_rows();
let cursor_position = terminal.cursor();
Expand All @@ -118,7 +118,7 @@ impl Screen {
self.layout.styles.term,
);

self.sugarloaf.render(color);
self.sugarloaf.render();

// self.sugarloaf.set_cursor(cursor_position, false);

Expand Down
10 changes: 4 additions & 6 deletions rio/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Sequencer {
{
return;
}
screen.render(self.config.colors.background.1);
screen.render();
}
RioEvent::Title(_title) => {
// if !self.ctx.preserve_title && self.ctx.config.window.dynamic_title {
Expand All @@ -60,7 +60,7 @@ impl Sequencer {
}
RioEvent::MouseCursorDirty => {
screen.layout().reset_mouse();
screen.render(self.config.colors.background.1);
screen.render();
}
RioEvent::ClipboardLoad(clipboard_type, format) => {
if is_focused {
Expand Down Expand Up @@ -181,9 +181,7 @@ impl Sequencer {
return;
}

screen
.resize(new_size)
.render(self.config.colors.background.1);
screen.resize(new_size).render();
}

Event::WindowEvent {
Expand All @@ -196,7 +194,7 @@ impl Sequencer {
} => {
screen
.set_scale(scale_factor as f32, *new_inner_size)
.render(self.config.colors.background.1);
.render();
}

Event::MainEventsCleared { .. } => {}
Expand Down
4 changes: 2 additions & 2 deletions sugarloaf/examples/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn main() {
sugarloaf
.rescale(scale_factor as f32)
.resize(new_inner_size.width, new_inner_size.height)
.render(wgpu::Color::BLUE);
.render();
}
_ => (),
},
Expand Down Expand Up @@ -92,7 +92,7 @@ async fn main() {
size: [200.0, 200.0],
},
])
.render(wgpu::Color::BLUE);
.render();
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
9 changes: 3 additions & 6 deletions sugarloaf/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ async fn main() {
let font_size = 180.;

let style = SugarloafStyle {
screen_position: (
(20. + 10.) * scale_factor,
(20. + font_size) * scale_factor,
),
screen_position: ((20. + 10.) * scale_factor, (20. + font_size) * scale_factor),
text_scale: font_size * scale_factor,
bounds: (width * scale_factor, height * scale_factor),
};
Expand Down Expand Up @@ -74,7 +71,7 @@ async fn main() {
sugarloaf
.rescale(scale_factor as f32)
.resize(new_inner_size.width, new_inner_size.height)
.render(wgpu::Color::BLACK);
.render();
}
_ => (),
},
Expand Down Expand Up @@ -181,7 +178,7 @@ async fn main() {
sugarloaf.stack(sugar, style);
sugarloaf.stack(loaf, style);
sugarloaf.stack(rio, style);
sugarloaf.render(wgpu::Color::RED);
sugarloaf.render();
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
191 changes: 114 additions & 77 deletions sugarloaf/src/sugarloaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ use crate::components::text;
use crate::context::Context;
use crate::core::{SugarStack, SugarloafStyle};
use crate::font::Font;
use glyph_brush::ab_glyph::Font as GFont;
use glyph_brush::ab_glyph::{self, FontArc};
use glyph_brush::FontId;
use glyph_brush::GlyphCruncher;
use glyph_brush::{OwnedSection, OwnedText};
use glyph_brush::ab_glyph::{self, Font as GFont, FontArc};
use glyph_brush::{FontId, GlyphCruncher, OwnedSection, OwnedText};

// TODO: Use macro instead
pub enum RendererTarget {
Expand Down Expand Up @@ -77,6 +74,7 @@ pub struct Sugarloaf {
acc_line: f32,
acc_line_y: f32,
sugar_dimension: (f32, f32),
background_color: wgpu::Color,
}

// TODO: Sugarloaf integrate CustomRenderer (or Renderer) layer usage
Expand Down Expand Up @@ -111,7 +109,8 @@ impl Sugarloaf {
text_brush,
acc_line: 0.0,
acc_line_y: 0.0,
sugar_dimension: (0., 0.)
sugar_dimension: (0., 0.),
background_color: wgpu::Color::BLACK,
})
}
Err(err_message) => Err(format!(
Expand All @@ -120,7 +119,44 @@ impl Sugarloaf {
}
}

pub fn clear(&mut self) {
match self.ctx.surface.get_current_texture() {
Ok(frame) => {
let mut encoder = self.ctx.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
);

let view = &frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());

encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sugarloaf::init -> Clear frame"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(self.background_color),
store: true,
},
})],
depth_stencil_attachment: None,
});
self.ctx.staging_belt.finish();
self.ctx.queue.submit(Some(encoder.finish()));
frame.present();
self.ctx.staging_belt.recall();
}
Err(error) => {
if error == wgpu::SurfaceError::OutOfMemory {
panic!("Swapchain error: {error}. Rendering cannot continue.")
}
}
}
}

pub fn resize(&mut self, width: u32, height: u32) -> &mut Self {
self.clear();
self.ctx.size.width = width;
self.ctx.size.height = height;
self.ctx.surface.configure(
Expand Down Expand Up @@ -149,9 +185,8 @@ impl Sugarloaf {
let mut x = 0.;

if self.acc_line_y == 0.0 {
self.acc_line_y = 20.;
println!("{:?}", self.acc_line_y);
// self.acc_line_y = self.acc_line_y;
self.acc_line_y =
(style.screen_position.1 - style.text_scale) / self.ctx.scale;
}

let fonts = self.text_brush.fonts();
Expand Down Expand Up @@ -181,16 +216,16 @@ impl Sugarloaf {
.with_scale(style.text_scale),
);

// Sugar dimension will return 150px
// (style.screen_position.1 + self.acc_line) / 2.

self.rects.push(Rect {
position: [style.screen_position.0 - 30. + x, self.acc_line_y],
position: [
(style.screen_position.0 / self.ctx.scale) + x,
self.acc_line_y,
],
color: sugar.background_color,
size: [self.sugar_dimension.0, self.sugar_dimension.0],
});

x += (self.sugar_dimension.0)/ 2.;
x += (self.sugar_dimension.0) / self.ctx.scale;
}

let section = &OwnedSection {
Expand All @@ -206,8 +241,6 @@ impl Sugarloaf {

self.text_brush.queue(section);

println!("{:?}", self.acc_line_y);

self.acc_line_y = (style.screen_position.1 + self.acc_line) / 2.;
self.acc_line += style.text_scale;
}
Expand All @@ -218,73 +251,77 @@ impl Sugarloaf {

#[inline]
pub fn init(&mut self, color: wgpu::Color, style: SugarloafStyle) {
let mut encoder =
self.ctx
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sugarloaf::init -> Create command encoder"),
});
let frame = self
.ctx
.surface
.get_current_texture()
.expect("Get next frame");
let view = &frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sugarloaf::init -> Clear frame"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(color),
store: true,
},
})],
depth_stencil_attachment: None,
});

if self.sugar_dimension == (0., 0.) {
let text = vec![OwnedText::new(' ')
.with_font_id(FontId(0))
.with_color([0.,0.,0.,0.])
.with_scale(style.text_scale)];

let section = &OwnedSection {
screen_position: (
style.screen_position.0,
style.screen_position.1 + self.acc_line,
),
bounds: style.bounds,
text,
layout: glyph_brush::Layout::default_single_line()
.v_align(glyph_brush::VerticalAlign::Bottom),
};
self.reset_state();
self.rects = vec![];
self.background_color = color;

self.text_brush.queue(section);
match self.ctx.surface.get_current_texture() {
Ok(frame) => {
let mut encoder = self.ctx.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
);

let view = &frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());

// TODO: Now Sugarloaf depends of run an init operation
// and also font has same size for get dimensions.
if let Some(rect) = self.text_brush.glyph_bounds(section) {
let width = rect.max.x - rect.min.x;
let height = rect.max.y - rect.min.y;
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sugarloaf::init -> Clear frame"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(color),
store: true,
},
})],
depth_stencil_attachment: None,
});

println!("{:?} {:?}", width, height);
self.sugar_dimension = (width, height);
if self.sugar_dimension == (0., 0.) {
let text = vec![OwnedText::new(' ')
.with_font_id(FontId(0))
.with_color([0., 0., 0., 0.])
.with_scale(style.text_scale)];

let section = &OwnedSection {
screen_position: (
style.screen_position.0,
style.screen_position.1 + self.acc_line,
),
bounds: style.bounds,
text,
layout: glyph_brush::Layout::default_single_line()
.v_align(glyph_brush::VerticalAlign::Bottom),
};

self.text_brush.queue(section);

// TODO: Now Sugarloaf depends of run an init operation
// and also font has same size for get dimensions.
if let Some(rect) = self.text_brush.glyph_bounds(section) {
let width = rect.max.x - rect.min.x;
let height = rect.max.y - rect.min.y;
self.sugar_dimension = (width, height);
};
}

};
self.ctx.staging_belt.finish();
self.ctx.queue.submit(Some(encoder.finish()));
frame.present();
self.ctx.staging_belt.recall();
}
Err(error) => {
if error == wgpu::SurfaceError::OutOfMemory {
panic!("Swapchain error: {error}. Rendering cannot continue.")
}
}
}

self.ctx.staging_belt.finish();
self.ctx.queue.submit(Some(encoder.finish()));
frame.present();
self.ctx.staging_belt.recall();
}

fn reset_state(&mut self) {
self.acc_line = 0.0;
self.acc_line_y = -0.175;
self.acc_line_y = 0.0;
}

pub fn pile_rect(&mut self, instances: Vec<Rect>) -> &mut Self {
Expand All @@ -293,7 +330,7 @@ impl Sugarloaf {
}

#[inline]
pub fn render(&mut self, color: colors::ColorWGPU) {
pub fn render(&mut self) {
self.reset_state();

match self.ctx.surface.get_current_texture() {
Expand All @@ -307,12 +344,12 @@ impl Sugarloaf {
.create_view(&wgpu::TextureViewDescriptor::default());

encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Clear background frame"),
label: Some("sugarloaf::render -> Clear frame"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(color),
load: wgpu::LoadOp::Clear(self.background_color),
store: true,
},
})],
Expand Down

0 comments on commit 68377b9

Please sign in to comment.