Skip to content

Commit

Permalink
Added ability to draw pre-measured text
Browse files Browse the repository at this point in the history
This also fixes the TextOrigin handling for both prepared and renderer
flows.
  • Loading branch information
ecton committed Jul 11, 2023
1 parent 17de895 commit 936a65d
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 176 deletions.
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
"Hello, World!",
TextOrigin::Center,
Point::<Lp>::new(RED_SQUARE_SIZE / 2, RED_SQUARE_SIZE / 2),
None,
Some(angle),
None,
);
true
Expand Down
12 changes: 6 additions & 6 deletions examples/text-metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn main() {

let inset = Point::new(Lp::cm(1), Lp::cm(1));

let metrics = renderer.measure_text::<Lp>("Kludgine");
let measured = renderer.measure_text::<Lp>("Kludgine", Color::WHITE);
renderer.draw_shape(
&Shape::filled_rect(
Rect::new(
Point::new(metrics.left, line_height - metrics.ascent),
Size::new(metrics.width - metrics.left, metrics.ascent),
Point::new(measured.left, line_height - measured.ascent),
Size::new(measured.size.width - measured.left, measured.ascent),
),
Color::new(0, 40, 0, 255),
),
Expand All @@ -29,8 +29,8 @@ fn main() {
renderer.draw_shape(
&Shape::filled_rect(
Rect::new(
Point::new(metrics.left, line_height),
Size::new(metrics.width - metrics.left, -metrics.descent),
Point::new(measured.left, line_height),
Size::new(measured.size.width - measured.left, -measured.descent),
),
Color::new(0, 0, 40, 255),
),
Expand All @@ -39,7 +39,7 @@ fn main() {
None,
);

renderer.draw_text("Kludgine", TextOrigin::TopLeft, inset, None, None);
renderer.draw_measured_text(&measured, TextOrigin::TopLeft, inset, None, None);

true
})
Expand Down
2 changes: 1 addition & 1 deletion examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl WindowBehavior for Test {
);
self.text.buffer_mut().set_metrics(
graphics.font_system(),
Metrics::new(24.0, 32.0).scale(scale.into_f32()),
Metrics::new(24.0, 24.0).scale(scale.into_f32()),
);
self.text.shape_as_needed(graphics.font_system());
self.prepared = graphics.prepare_text(self.text.buffer(), Color::WHITE, TextOrigin::Center);
Expand Down
6 changes: 6 additions & 0 deletions src/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ impl TextureCollection {
let data = self.data.read().map_or_else(PoisonError::into_inner, |g| g);
data.texture.prepare(dest, graphics)
}

/// Returns the format of the texture backing this collection.
#[must_use]
pub const fn format(&self) -> wgpu::TextureFormat {
self.format
}
}

impl TextureSource for TextureCollection {}
Expand Down
68 changes: 15 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::borrow::Cow;
use std::fmt::{self, Debug, Formatter};
use std::hash::{self, Hash};
use std::ops::{Add, Deref, DerefMut, Div, Neg};
use std::ops::{Add, AddAssign, Deref, DerefMut, Div, Neg};
use std::sync::Arc;

use ahash::AHashMap;
Expand All @@ -24,7 +24,6 @@ use wgpu::util::DeviceExt;

use crate::buffer::Buffer;
use crate::pipeline::{Uniforms, Vertex};
use crate::text::TextSystem;

/// Application and Windowing Support.
#[cfg(feature = "app")]
Expand Down Expand Up @@ -73,12 +72,13 @@ pub struct Kludgine {
size: Size<UPx>,
scale: Fraction,
#[cfg(feature = "cosmic-text")]
text: TextSystem,
text: text::TextSystem,
}

impl Kludgine {
/// Returns a new instance of Kludgine with the provided parameters.
#[must_use]
#[cfg_attr(not(feature = "cosmic-text"), allow(unused_variables))]
pub fn new(
device: &wgpu::Device,
queue: &wgpu::Queue,
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Kludgine {

Self {
#[cfg(feature = "cosmic-text")]
text: TextSystem::new(&ProtoGraphics {
text: text::TextSystem::new(&ProtoGraphics {
device,
queue,
binding_layout: &binding_layout,
Expand Down Expand Up @@ -178,13 +178,6 @@ impl Kludgine {
}
}

/// Returns a mutable reference to the [`cosmic_text::FontSystem`] used when
/// rendering text.
#[cfg(feature = "cosmic-text")]
pub fn font_system(&mut self) -> &mut cosmic_text::FontSystem {
&mut self.text.fonts
}

/// Returns the currently configured size to render.
pub const fn size(&self) -> Size<UPx> {
self.size
Expand All @@ -195,47 +188,6 @@ impl Kludgine {
pub const fn scale(&self) -> Fraction {
self.scale
}

#[cfg(feature = "cosmic-text")]
pub(crate) fn update_scratch_buffer(&mut self, text: &str) {
self.text.update_scratch_buffer(text, self.scale);
}

/// Sets the font size.
#[cfg(feature = "cosmic-text")]
pub fn set_font_size(
&mut self,
size: impl figures::traits::ScreenScale<Lp = figures::units::Lp>,
) {
self.text.set_font_size(
figures::traits::ScreenScale::into_lp(size, self.scale),
self.scale,
);
}

/// Returns the current font size.
#[cfg(feature = "cosmic-text")]
pub fn font_size(&self) -> figures::units::Lp {
self.text.font_size
}

/// Sets the line height for multi-line layout.
#[cfg(feature = "cosmic-text")]
pub fn set_line_height(
&mut self,
size: impl figures::traits::ScreenScale<Lp = figures::units::Lp>,
) {
self.text.set_line_height(
figures::traits::ScreenScale::into_lp(size, self.scale),
self.scale,
);
}

/// Returns the current line height.
#[cfg(feature = "cosmic-text")]
pub fn line_height(&self) -> figures::units::Lp {
self.text.line_height
}
}

/// A frame that can be rendered.
Expand Down Expand Up @@ -1342,11 +1294,12 @@ where
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
struct TextureBlit<Unit> {
verticies: [Vertex<Unit>; 4],
}

#[cfg_attr(not(feature = "cosmic-text"), allow(dead_code))]
impl<Unit> TextureBlit<Unit> {
pub fn new(source: Rect<UPx>, dest: Rect<Unit>, color: Color) -> Self
where
Expand Down Expand Up @@ -1395,4 +1348,13 @@ impl<Unit> TextureBlit<Unit> {
pub const fn bottom_right(&self) -> &Vertex<Unit> {
&self.verticies[3]
}

pub fn translate_by(&mut self, offset: Point<Unit>)
where
Unit: AddAssign + Copy,
{
for vertex in &mut self.verticies {
vertex.location += offset;
}
}
}
Loading

0 comments on commit 936a65d

Please sign in to comment.