feat: bound scene visibility rendering and continuous native input

This commit is contained in:
2026-11-11 00:53:21 +08:00
parent 15fc01542c
commit 32d5da5528
15 changed files with 900 additions and 134 deletions
+108 -49
View File
@@ -198,71 +198,104 @@ pub fn measure_wrapped(value: &str, style: TextStyle, width: f32) -> TextSize {
engine.buffers[&key].size
}
/// Draw at the actual device font size; no scaling of an already rasterized bitmap.
pub fn draw(value: &str, style: TextStyle, color: Color, pixel: impl FnMut(i32, i32, Color)) {
draw_width(value, style, color, None, pixel)
}
pub fn draw_wrapped(
value: &str,
style: TextStyle,
color: Color,
width: f32,
pixel: impl FnMut(i32, i32, Color),
) {
if width > 0.0 {
draw_width(value, style, color, Some(width), pixel)
}
}
fn draw_width(
/// Rasterize only visible glyph pixels. Large camera zooms use a bounded glyph
/// bitmap and sample it at the requested size instead of allocating an enormous
/// font image. Layout and wrapping still use proportionally identical metrics.
pub fn draw_clipped(
value: &str,
style: TextStyle,
color: Color,
width: Option<f32>,
clip: crate::geometry::Rect,
mut pixel: impl FnMut(i32, i32, Color),
) {
if clip.width <= 0.
|| clip.height <= 0.
|| !style.font_size.is_finite()
|| style.font_size <= 0.
{
return;
}
let lod = (512. / style.font_size).min(1.);
let mut engine = engine().lock().expect("text engine lock");
let key = engine.prepare_width(value, style, width);
let key = engine.prepare_width(value, style.scaled(lod), width.map(|width| width * lod));
let TextEngine {
fonts,
swash,
buffers,
..
} = &mut *engine;
let opacity = u16::from(color.alpha);
buffers.get_mut(&key).expect("prepared text").buffer.draw(
fonts,
swash,
cosmic_text::Color::rgb(color.red, color.green, color.blue),
|x, y, width, height, color| {
for dy in 0..height as i32 {
for dx in 0..width as i32 {
pixel(
x + dx,
y + dy,
Color::rgba(
color.r(),
color.g(),
color.b(),
(u16::from(color.a()) * opacity / 255) as u8,
let buffer = &buffers[&key].buffer;
let mut cache_bytes: usize = swash
.image_cache
.values()
.flatten()
.map(|image| image.data.len())
.sum();
for run in buffer.layout_runs() {
for glyph in run.glyphs {
let physical = glyph.physical((0., run.line_y), 1.);
// Conservative font-sized guard includes bearings/overhangs. The
// exact bitmap bounds below determine the actual visible pixels.
let guard = glyph.font_size * 4. / lod;
let gx = physical.x as f32 / lod;
let gy = physical.y as f32 / lod;
if gx + guard < clip.x
|| gx - guard > clip.x + clip.width
|| gy + guard < clip.y
|| gy - guard > clip.y + clip.height
{
continue;
}
if swash.image_cache.len() >= 4096 || cache_bytes >= 33_554_432 {
*swash = SwashCache::new();
cache_bytes = 0;
}
let cached = swash.image_cache.contains_key(&physical.cache_key);
let Some(image) = swash.get_image(fonts, physical.cache_key) else {
continue;
};
if !cached {
cache_bytes += image.data.len();
}
let left = (physical.x + image.placement.left) as f32 / lod;
let top = (physical.y - image.placement.top) as f32 / lod;
let right = left + image.placement.width as f32 / lod;
let bottom = top + image.placement.height as f32 / lod;
let base = glyph.color_opt.map_or(color, |value| {
Color::rgba(value.r(), value.g(), value.b(), color.alpha)
});
for y in top.max(clip.y).ceil() as i32..bottom.min(clip.y + clip.height).ceil() as i32 {
for x in
left.max(clip.x).ceil() as i32..right.min(clip.x + clip.width).ceil() as i32
{
let u = (((x as f32 + 0.5 - left) * lod) as u32)
.min(image.placement.width.saturating_sub(1));
let v = (((y as f32 + 0.5 - top) * lod) as u32)
.min(image.placement.height.saturating_sub(1));
let index = (v * image.placement.width + u) as usize;
let sample = match image.content {
cosmic_text::SwashContent::Mask => Color::rgba(
base.red,
base.green,
base.blue,
(u16::from(image.data[index]) * u16::from(color.alpha) / 255) as u8,
),
);
cosmic_text::SwashContent::Color => {
let i = index * 4;
Color::rgba(
image.data[i],
image.data[i + 1],
image.data[i + 2],
(u16::from(image.data[i + 3]) * u16::from(color.alpha) / 255) as u8,
)
}
cosmic_text::SwashContent::SubpixelMask => continue,
};
pixel(x, y, sample);
}
}
},
);
// The cache is an optimization, never an archive of all submitted glyphs.
if swash.image_cache.len() > 4096
|| swash
.image_cache
.values()
.flatten()
.map(|image| image.data.len())
.sum::<usize>()
> 33_554_432
{
*swash = SwashCache::new();
}
}
}
@@ -316,6 +349,32 @@ mod tests {
use super::*;
use unicode_segmentation::UnicodeSegmentation;
#[test]
fn extreme_zoom_rasterizes_only_the_clip_with_bounded_glyph_sizes() {
let mut count = 0;
draw_clipped(
"M",
TextStyle::ui(1).scaled(10_000.),
Color::rgba(1, 2, 3, 255),
None,
crate::geometry::Rect::new(50_000., 60_000., 32., 32.),
|x, y, _| {
assert!((50_000..50_032).contains(&x));
assert!((60_000..60_032).contains(&y));
count += 1;
},
);
assert_eq!(count, 1024);
let engine = engine().lock().unwrap();
assert!(
engine
.swash
.image_cache
.keys()
.all(|key| f32::from_bits(key.font_size_bits) <= 512.)
);
}
#[test]
fn width_sensitive_shaping_wraps_words_and_clusters_without_changing_intrinsic_metrics() {
let style = TextStyle::ui(1);