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" }
etagere = "0.2.6"
#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]
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 wgpu::{
Backends, CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance,
@ -64,16 +64,20 @@ async fn run() {
};
surface.configure(&device, &config);
unsafe {
FONT_SYSTEM = Some(FontSystem::new());
}
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
let mut text_renderer = TextRenderer::new(&device, &queue);
unsafe { FONT_SYSTEM = Some(FontSystem::new()) };
let font_matches = unsafe {
FONT_SYSTEM.as_ref().unwrap().matches(|info| {
info.style == fontdb::Style::Normal
&& info.weight == fontdb::Weight::NORMAL
&& info.stretch == fontdb::Stretch::Normal
})
};
let mut buffer = TextBuffer::new(
unsafe { FONT_SYSTEM.as_ref().unwrap() },
Attrs::new(),
TextMetrics::new(32, 44),
);
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| {
let _ = (&instance, &adapter);
@ -90,12 +94,6 @@ async fn run() {
window.request_redraw();
}
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
.prepare(
&device,

View file

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

View file

@ -1,4 +1,4 @@
use cosmic_text::{CacheKey, TextBuffer};
use cosmic_text::{CacheKey, SwashCache, TextBuffer};
use etagere::{size2, Allocation};
use std::{collections::HashSet, iter, mem::size_of, num::NonZeroU32, slice};
@ -21,6 +21,7 @@ pub struct TextRenderer {
vertices_to_render: u32,
glyphs_in_use: HashSet<CacheKey>,
screen_resolution: Resolution,
swash_cache: SwashCache,
}
impl TextRenderer {
@ -42,6 +43,8 @@ impl TextRenderer {
mapped_at_creation: false,
});
let swash_cache = SwashCache::new();
Self {
vertex_buffer,
vertex_buffer_size,
@ -53,6 +56,7 @@ impl TextRenderer {
width: 0,
height: 0,
},
swash_cache,
}
}
@ -92,25 +96,22 @@ impl TextRenderer {
let mut buffers = [(buffer, TextOverflow::Hide)];
for (buffer, _) in buffers.iter_mut() {
for line in buffer.lines.iter() {
let layout = match line.layout_opt.as_ref() {
Some(l) => l,
None => continue,
};
for layout_line in layout {
for glyph in layout_line.glyphs.iter() {
let key = glyph.inner.0;
for run in buffer.layout_runs() {
for glyph in run.glyphs.iter() {
self.glyphs_in_use.insert(glyph.cache_key);
self.glyphs_in_use.insert(key);
let already_on_gpu = atlas.glyph_cache.contains_key(&key);
let already_on_gpu = atlas.glyph_cache.contains_key(&glyph.cache_key);
if already_on_gpu {
continue;
}
let image = layout_line.rasterize_glyph(glyph).unwrap();
let bitmap = image.data.clone();
let image = self
.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;
@ -162,21 +163,22 @@ impl TextRenderer {
(GpuCache::SkipRasterization, None)
};
if !atlas.glyph_cache.contains_key(&key) {
if !atlas.glyph_cache.contains_key(&glyph.cache_key) {
atlas.glyph_cache.insert(
key,
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,
},
);
}
}
}
}
}
if let Some(ub) = upload_bounds {
queue.write_texture(
@ -216,22 +218,14 @@ impl TextRenderer {
let bounds_min_y = 0u32;
let bounds_max_y = u32::MAX;
for line in buffer.lines.iter() {
let layout = match line.layout_opt.as_ref() {
Some(l) => l,
None => continue,
};
for layout_line in layout {
for glyph in layout_line.glyphs.iter() {
let key = glyph.inner.0;
for run in buffer.layout_runs() {
let line_y = run.line_y;
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 y: u32 = (buffer.metrics.line_height as i32
- details.height as i32)
.try_into()
.unwrap();
let mut x = (glyph.x_int + details.left as i32) as u32;
let mut y = (line_y + glyph.y_int - details.top as i32) as u32;
let (mut atlas_x, mut atlas_y) = match details.gpu_cache {
GpuCache::InAtlas { x, y } => (x, y),
@ -310,7 +304,6 @@ impl TextRenderer {
}
}
}
}
const VERTICES_PER_GLYPH: u32 = 6;
self.vertices_to_render = glyphs_added as u32 * VERTICES_PER_GLYPH;