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,25 +96,22 @@ 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 { if already_on_gpu {
continue; continue;
} }
let image = layout_line.rasterize_glyph(glyph).unwrap(); let image = self
let bitmap = image.data.clone(); .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 width = image.placement.width as usize;
let height = image.placement.height as usize; let height = image.placement.height as usize;
@ -162,21 +163,22 @@ impl TextRenderer {
(GpuCache::SkipRasterization, None) (GpuCache::SkipRasterization, None)
}; };
if !atlas.glyph_cache.contains_key(&key) { if !atlas.glyph_cache.contains_key(&glyph.cache_key) {
atlas.glyph_cache.insert( atlas.glyph_cache.insert(
key, glyph.cache_key,
GlyphDetails { GlyphDetails {
width: width as u16, width: width as u16,
height: height as u16, height: height as u16,
gpu_cache, gpu_cache,
atlas_id, atlas_id,
top: image.placement.top as i16,
left: image.placement.left as i16,
}, },
); );
} }
} }
} }
} }
}
if let Some(ub) = upload_bounds { if let Some(ub) = upload_bounds {
queue.write_texture( queue.write_texture(
@ -216,22 +218,14 @@ 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),
@ -310,7 +304,6 @@ impl TextRenderer {
} }
} }
} }
}
const VERTICES_PER_GLYPH: u32 = 6; const VERTICES_PER_GLYPH: u32 = 6;
self.vertices_to_render = glyphs_added as u32 * VERTICES_PER_GLYPH; self.vertices_to_render = glyphs_added as u32 * VERTICES_PER_GLYPH;