Update to latest cosmic-text

This commit is contained in:
grovesNL 2022-10-25 22:38:12 -02:30 committed by Josh Groves
parent 75fc8f978f
commit 7fe35bdce3
4 changed files with 160 additions and 167 deletions

View file

@ -12,7 +12,7 @@ wgpu = "0.14.0"
fontdue = { git = "https://github.com/mooman219/fontdue", rev = "828b4f4" } fontdue = { git = "https://github.com/mooman219/fontdue", rev = "828b4f4" }
etagere = "0.2.6" etagere = "0.2.6"
#cosmic-text = { git = "https://github.com/pop-os/cosmic-text", rev = "ef686f8" } #cosmic-text = { git = "https://github.com/pop-os/cosmic-text", rev = "ef686f8" }
cosmic-text = { path = "../cosmic-text" } cosmic-text = { path = "../cosmic-text", features = ["swash"] }
[dev-dependencies] [dev-dependencies]
winit = "0.27.0" winit = "0.27.0"

View file

@ -1,4 +1,4 @@
use cosmic_text::{fontdb, FontSystem, TextBuffer, TextMetrics}; use cosmic_text::{Attrs, FontSystem, TextBuffer, TextMetrics};
use glyphon::{Color, HasColor, Resolution, TextAtlas, TextRenderer}; use glyphon::{Color, HasColor, Resolution, TextAtlas, TextRenderer};
use wgpu::{ use wgpu::{
Backends, CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance, Backends, CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance,
@ -64,16 +64,20 @@ async fn run() {
}; };
surface.configure(&device, &config); surface.configure(&device, &config);
unsafe {
FONT_SYSTEM = Some(FontSystem::new());
}
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format); let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
let mut text_renderer = TextRenderer::new(&device, &queue); let mut text_renderer = TextRenderer::new(&device, &queue);
unsafe { FONT_SYSTEM = Some(FontSystem::new()) };
let font_matches = unsafe { let mut buffer = TextBuffer::new(
FONT_SYSTEM.as_ref().unwrap().matches(|info| { unsafe { FONT_SYSTEM.as_ref().unwrap() },
info.style == fontdb::Style::Normal Attrs::new(),
&& info.weight == fontdb::Weight::NORMAL TextMetrics::new(32, 44),
&& info.stretch == fontdb::Stretch::Normal );
}) buffer.set_size(800, 600);
}; buffer.set_text("Hello from cosmic inside of glyphon/wgpu!");
buffer.shape_until_cursor();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); let _ = (&instance, &adapter);
@ -90,12 +94,6 @@ async fn run() {
window.request_redraw(); window.request_redraw();
} }
Event::RedrawRequested(_) => { Event::RedrawRequested(_) => {
let mut buffer =
TextBuffer::new(font_matches.as_ref().unwrap(), TextMetrics::new(20, 50));
buffer.set_text("HELLO_FROM_COSMIC_INSIDE_OF_GLYPHON_WGPU");
buffer.shape_until_cursor();
text_renderer text_renderer
.prepare( .prepare(
&device, &device,

View file

@ -42,6 +42,8 @@ pub(crate) struct GlyphDetails {
height: u16, height: u16,
gpu_cache: GpuCache, gpu_cache: GpuCache,
atlas_id: Option<AllocId>, atlas_id: Option<AllocId>,
top: i16,
left: i16,
} }
#[repr(C)] #[repr(C)]

View file

@ -1,4 +1,4 @@
use cosmic_text::{CacheKey, TextBuffer}; use cosmic_text::{CacheKey, SwashCache, TextBuffer};
use etagere::{size2, Allocation}; use etagere::{size2, Allocation};
use std::{collections::HashSet, iter, mem::size_of, num::NonZeroU32, slice}; use std::{collections::HashSet, iter, mem::size_of, num::NonZeroU32, slice};
@ -21,6 +21,7 @@ pub struct TextRenderer {
vertices_to_render: u32, vertices_to_render: u32,
glyphs_in_use: HashSet<CacheKey>, glyphs_in_use: HashSet<CacheKey>,
screen_resolution: Resolution, screen_resolution: Resolution,
swash_cache: SwashCache,
} }
impl TextRenderer { impl TextRenderer {
@ -42,6 +43,8 @@ impl TextRenderer {
mapped_at_creation: false, mapped_at_creation: false,
}); });
let swash_cache = SwashCache::new();
Self { Self {
vertex_buffer, vertex_buffer,
vertex_buffer_size, vertex_buffer_size,
@ -53,6 +56,7 @@ impl TextRenderer {
width: 0, width: 0,
height: 0, height: 0,
}, },
swash_cache,
} }
} }
@ -92,87 +96,85 @@ impl TextRenderer {
let mut buffers = [(buffer, TextOverflow::Hide)]; let mut buffers = [(buffer, TextOverflow::Hide)];
for (buffer, _) in buffers.iter_mut() { for (buffer, _) in buffers.iter_mut() {
for line in buffer.lines.iter() { for run in buffer.layout_runs() {
let layout = match line.layout_opt.as_ref() { for glyph in run.glyphs.iter() {
Some(l) => l, self.glyphs_in_use.insert(glyph.cache_key);
None => continue,
};
for layout_line in layout {
for glyph in layout_line.glyphs.iter() {
let key = glyph.inner.0;
self.glyphs_in_use.insert(key); let already_on_gpu = atlas.glyph_cache.contains_key(&glyph.cache_key);
let already_on_gpu = atlas.glyph_cache.contains_key(&key); if already_on_gpu {
continue;
}
if already_on_gpu { let image = self
continue; .swash_cache
} .get_image(&buffer.font_matches, glyph.cache_key)
.as_ref()
.unwrap();
let bitmap = image.data.as_slice();
let width = image.placement.width as usize;
let height = image.placement.height as usize;
let image = layout_line.rasterize_glyph(glyph).unwrap(); let should_rasterize = width > 0 && height > 0;
let bitmap = image.data.clone();
let width = image.placement.width as usize;
let height = image.placement.height as usize;
let should_rasterize = width > 0 && height > 0; let (gpu_cache, atlas_id) = if should_rasterize {
// Find a position in the packer
let (gpu_cache, atlas_id) = if should_rasterize { let allocation = match try_allocate(atlas, width, height) {
// Find a position in the packer Some(a) => a,
let allocation = match try_allocate(atlas, width, height) { None => return Err(PrepareError::AtlasFull),
Some(a) => a,
None => return Err(PrepareError::AtlasFull),
};
let atlas_min = allocation.rectangle.min;
let atlas_max = allocation.rectangle.max;
for row in 0..height {
let y_offset = atlas_min.y as usize;
let x_offset =
(y_offset + row) * atlas.width as usize + atlas_min.x as usize;
let bitmap_row = &bitmap[row * width..(row + 1) * width];
atlas.texture_pending[x_offset..x_offset + width]
.copy_from_slice(bitmap_row);
}
match upload_bounds.as_mut() {
Some(ub) => {
ub.x_min = ub.x_min.min(atlas_min.x as usize);
ub.x_max = ub.x_max.max(atlas_max.x as usize);
ub.y_min = ub.y_min.min(atlas_min.y as usize);
ub.y_max = ub.y_max.max(atlas_max.y as usize);
}
None => {
upload_bounds = Some(UploadBounds {
x_min: atlas_min.x as usize,
x_max: atlas_max.x as usize,
y_min: atlas_min.y as usize,
y_max: atlas_max.y as usize,
});
}
}
(
GpuCache::InAtlas {
x: atlas_min.x as u16,
y: atlas_min.y as u16,
},
Some(allocation.id),
)
} else {
(GpuCache::SkipRasterization, None)
}; };
let atlas_min = allocation.rectangle.min;
let atlas_max = allocation.rectangle.max;
if !atlas.glyph_cache.contains_key(&key) { for row in 0..height {
atlas.glyph_cache.insert( let y_offset = atlas_min.y as usize;
key, let x_offset =
GlyphDetails { (y_offset + row) * atlas.width as usize + atlas_min.x as usize;
width: width as u16, let bitmap_row = &bitmap[row * width..(row + 1) * width];
height: height as u16, atlas.texture_pending[x_offset..x_offset + width]
gpu_cache, .copy_from_slice(bitmap_row);
atlas_id,
},
);
} }
match upload_bounds.as_mut() {
Some(ub) => {
ub.x_min = ub.x_min.min(atlas_min.x as usize);
ub.x_max = ub.x_max.max(atlas_max.x as usize);
ub.y_min = ub.y_min.min(atlas_min.y as usize);
ub.y_max = ub.y_max.max(atlas_max.y as usize);
}
None => {
upload_bounds = Some(UploadBounds {
x_min: atlas_min.x as usize,
x_max: atlas_max.x as usize,
y_min: atlas_min.y as usize,
y_max: atlas_max.y as usize,
});
}
}
(
GpuCache::InAtlas {
x: atlas_min.x as u16,
y: atlas_min.y as u16,
},
Some(allocation.id),
)
} else {
(GpuCache::SkipRasterization, None)
};
if !atlas.glyph_cache.contains_key(&glyph.cache_key) {
atlas.glyph_cache.insert(
glyph.cache_key,
GlyphDetails {
width: width as u16,
height: height as u16,
gpu_cache,
atlas_id,
top: image.placement.top as i16,
left: image.placement.left as i16,
},
);
} }
} }
} }
@ -216,98 +218,89 @@ impl TextRenderer {
let bounds_min_y = 0u32; let bounds_min_y = 0u32;
let bounds_max_y = u32::MAX; let bounds_max_y = u32::MAX;
for line in buffer.lines.iter() { for run in buffer.layout_runs() {
let layout = match line.layout_opt.as_ref() { let line_y = run.line_y;
Some(l) => l,
None => continue,
};
for layout_line in layout {
for glyph in layout_line.glyphs.iter() {
let key = glyph.inner.0;
let details = atlas.glyph_cache.get(&key).unwrap(); for glyph in run.glyphs.iter() {
let details = atlas.glyph_cache.get(&glyph.cache_key).unwrap();
let mut x = glyph.x.trunc() as u32; let mut x = (glyph.x_int + details.left as i32) as u32;
let mut y: u32 = (buffer.metrics.line_height as i32 let mut y = (line_y + glyph.y_int - details.top as i32) as u32;
- details.height as i32)
.try_into()
.unwrap();
let (mut atlas_x, mut atlas_y) = match details.gpu_cache { let (mut atlas_x, mut atlas_y) = match details.gpu_cache {
GpuCache::InAtlas { x, y } => (x, y), GpuCache::InAtlas { x, y } => (x, y),
GpuCache::SkipRasterization => continue, GpuCache::SkipRasterization => continue,
}; };
let mut width = details.width as u32; let mut width = details.width as u32;
let mut height = details.height as u32; let mut height = details.height as u32;
match overflow { match overflow {
TextOverflow::Overflow => {} TextOverflow::Overflow => {}
TextOverflow::Hide => { TextOverflow::Hide => {
// Starts beyond right edge or ends beyond left edge // Starts beyond right edge or ends beyond left edge
let max_x = x + width; let max_x = x + width;
if x > bounds_max_x || max_x < bounds_min_x { if x > bounds_max_x || max_x < bounds_min_x {
continue; continue;
} }
// Starts beyond bottom edge or ends beyond top edge // Starts beyond bottom edge or ends beyond top edge
let max_y = y + height; let max_y = y + height;
if y > bounds_max_y || max_y < bounds_min_y { if y > bounds_max_y || max_y < bounds_min_y {
continue; continue;
} }
// Clip left ege // Clip left ege
if x < bounds_min_x { if x < bounds_min_x {
let right_shift = bounds_min_x - x; let right_shift = bounds_min_x - x;
x = bounds_min_x; x = bounds_min_x;
width = max_x - bounds_min_x; width = max_x - bounds_min_x;
atlas_x += right_shift as u16; atlas_x += right_shift as u16;
} }
// Clip right edge // Clip right edge
if x + width > bounds_max_x { if x + width > bounds_max_x {
width = bounds_max_x - x; width = bounds_max_x - x;
} }
// Clip top edge // Clip top edge
if y < bounds_min_y { if y < bounds_min_y {
let bottom_shift = bounds_min_y - y; let bottom_shift = bounds_min_y - y;
y = bounds_min_y; y = bounds_min_y;
height = max_y - bounds_min_y; height = max_y - bounds_min_y;
atlas_y += bottom_shift as u16; atlas_y += bottom_shift as u16;
} }
// Clip bottom edge // Clip bottom edge
if y + height > bounds_max_y { if y + height > bounds_max_y {
height = bounds_max_y - y; height = bounds_max_y - y;
}
} }
} }
glyph_vertices.extend(
iter::repeat(GlyphToRender {
pos: [x as i32, y as i32],
dim: [width as u16, height as u16],
uv: [atlas_x, atlas_y],
color: [255, 0, 255, 255],
})
.take(4),
);
let start = 4 * glyphs_added as u32;
glyph_indices.extend([
start,
start + 1,
start + 2,
start,
start + 2,
start + 3,
]);
glyphs_added += 1;
} }
glyph_vertices.extend(
iter::repeat(GlyphToRender {
pos: [x as i32, y as i32],
dim: [width as u16, height as u16],
uv: [atlas_x, atlas_y],
color: [255, 0, 255, 255],
})
.take(4),
);
let start = 4 * glyphs_added as u32;
glyph_indices.extend([
start,
start + 1,
start + 2,
start,
start + 2,
start + 3,
]);
glyphs_added += 1;
} }
} }
} }