Move text layout positioning into DrawCommandRecorder
This commit is contained in:
1 parent
3e065a8838
commit
01697e32bd
2 files changed
+32
-34
No files matched your search
+27
-14
@@ -4,7 +4,8 @@ use ahash::AHasher;
|
||||
|
||||
use crate::{
|
||||
color::Srgba,
|
||||
dpi::{LogicalQuad, LogicalRect, LogicalSize},
|
||||
dpi::{LogicalPoint, LogicalQuad, LogicalRect, LogicalSize, ScaleFactor},
|
||||
fonts::{Layout, LayoutToLogical},
|
||||
math::Size,
|
||||
renderer::textures::TextureId,
|
||||
uv::{UvConstructors, UvQuad, UvRect},
|
||||
@@ -57,10 +58,6 @@ impl DrawCommandRecorder {
|
||||
self.current_viewport = viewport;
|
||||
}
|
||||
|
||||
pub fn get_viewport(&self) -> LogicalRect<f32> {
|
||||
self.current_viewport
|
||||
}
|
||||
|
||||
pub fn set_viewport_from_size(&mut self, size: LogicalSize<f32>) {
|
||||
self.set_viewport(LogicalRect::from_size(size));
|
||||
}
|
||||
@@ -147,20 +144,36 @@ impl DrawCommandRecorder {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn draw_glyph(
|
||||
/// Records a laid-out string whose top-left corner is at `layout_origin`.
|
||||
pub fn draw_glyphs(
|
||||
&mut self,
|
||||
rect: LogicalRect<f32>,
|
||||
layout: &Layout,
|
||||
layout_origin: LogicalPoint<f32>,
|
||||
is_absolute_position: bool,
|
||||
scale_factor: ScaleFactor<f32>,
|
||||
) {
|
||||
// resolve the layout origin to screen space
|
||||
let origin = if is_absolute_position {
|
||||
layout_origin
|
||||
} else {
|
||||
layout_origin + self.current_viewport.min.to_vector()
|
||||
};
|
||||
// align the layout origin with the physical pixel grid to reduce blurriness
|
||||
let origin = (origin * scale_factor).round() / scale_factor;
|
||||
|
||||
for glyph in &layout.glyphs {
|
||||
let rect = LayoutToLogical::new(origin.x, origin.y).transform_box(&glyph.rect);
|
||||
self.draw_glyph(rect, glyph.uv, glyph.color, glyph.layer_idx);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_glyph(
|
||||
&mut self,
|
||||
positions: LogicalRect<f32>,
|
||||
uv: UvRect,
|
||||
color: Srgba,
|
||||
layer_idx: u32,
|
||||
is_absolute_position: bool,
|
||||
) {
|
||||
let positions = if is_absolute_position {
|
||||
rect
|
||||
} else {
|
||||
rect.translate(self.current_viewport.min.to_vector())
|
||||
};
|
||||
|
||||
// cull if outside of viewport
|
||||
if !positions.intersects(&self.current_viewport) {
|
||||
return;
|
||||
|
||||
+5
-20
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
color::Srgba,
|
||||
dpi::{LogicalPoint, LogicalVector},
|
||||
fonts::{Alignment, FontStyle, LayoutJob, LayoutToLogical},
|
||||
dpi::LogicalPoint,
|
||||
fonts::{Alignment, FontStyle, LayoutJob},
|
||||
math::Point,
|
||||
pob::Context,
|
||||
};
|
||||
@@ -101,25 +101,10 @@ pub unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int
|
||||
let layout = ctx.fonts.borrow_mut().layout(job, scale_factor);
|
||||
|
||||
// apply offset to account for anchor position
|
||||
let position = position + layout.anchor_offset(halign);
|
||||
// align layout anchor with the physical pixel grid to reduce blurriness
|
||||
let viewport_offset = if is_absolute_position {
|
||||
LogicalVector::zero()
|
||||
} else {
|
||||
ctx.recorder.get_viewport().min.to_vector()
|
||||
};
|
||||
let position =
|
||||
((position + viewport_offset) * scale_factor).round() / scale_factor - viewport_offset;
|
||||
let layout_origin = position + layout.anchor_offset(halign);
|
||||
|
||||
for glyph in &layout.glyphs {
|
||||
ctx.recorder.draw_glyph(
|
||||
LayoutToLogical::new(position.x, position.y).transform_box(&glyph.rect),
|
||||
glyph.uv,
|
||||
glyph.color,
|
||||
glyph.layer_idx,
|
||||
is_absolute_position,
|
||||
);
|
||||
}
|
||||
ctx.recorder
|
||||
.draw_glyphs(&layout, layout_origin, is_absolute_position, scale_factor);
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user