Switch to mainline glyphon 0.5.0

This commit is contained in:
TÖRÖK Attila 2024-03-07 01:34:52 +01:00
parent 469adc63a4
commit 0cd1588a0e
4 changed files with 78 additions and 96 deletions

61
Cargo.lock generated
View file

@ -513,9 +513,6 @@ name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
dependencies = [
"serde",
]
[[package]]
name = "block"
@ -841,9 +838,9 @@ dependencies = [
[[package]]
name = "cosmic-text"
version = "0.10.0"
source = "git+https://github.com/StratusFearMe21/cosmic-text?branch=serde#5e40dd454c243a282b401291066a3e9ba807fcce"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75acbfb314aeb4f5210d379af45ed1ec2c98c7f1790bf57b8a4c562ac0c51b71"
dependencies = [
"bitflags 2.4.2",
"fontdb",
"libm",
"log",
@ -851,10 +848,8 @@ dependencies = [
"rustc-hash",
"rustybuzz",
"self_cell",
"serde",
"swash",
"sys-locale",
"ttf-parser 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi",
"unicode-linebreak",
"unicode-script",
@ -1303,16 +1298,16 @@ dependencies = [
[[package]]
name = "fontdb"
version = "0.16.0"
source = "git+https://github.com/StratusFearMe21/fontdb?branch=serde#145231493e0315a1c6a5ca41cbed8db99fd4a22d"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020e203f177c0fb250fb19455a252e838d2bbbce1f80f25ecc42402aafa8cd38"
dependencies = [
"fontconfig-parser",
"log",
"memmap2",
"serde",
"memmap2 0.8.0",
"slotmap",
"tinyvec",
"ttf-parser 0.20.0 (git+https://github.com/StratusFearMe21/ttf-parser?branch=serde)",
"ttf-parser 0.19.2",
]
[[package]]
@ -1541,7 +1536,8 @@ dependencies = [
[[package]]
name = "glyphon"
version = "0.5.0"
source = "git+https://github.com/StratusFearMe21/glyphon#e6851e7c2880119ff5f927ef5415f0ef4a970cc7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a62d0338e4056db6a73221c2fb2e30619452f6ea9651bac4110f51b0f7a7581"
dependencies = [
"cosmic-text",
"etagere",
@ -1881,6 +1877,15 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "memmap2"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a5a03cefb0d953ec0be133036f14e109412fa594edc2f77227249db66cc3ed"
dependencies = [
"libc",
]
[[package]]
name = "memmap2"
version = "0.9.4"
@ -2150,7 +2155,7 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7"
dependencies = [
"ttf-parser 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ttf-parser 0.20.0",
]
[[package]]
@ -2377,9 +2382,6 @@ name = "rangemap"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60fcc7d6849342eff22c4350c8b9a989ee8ceabc4b481253e8946b9fe83d684"
dependencies = [
"serde",
]
[[package]]
name = "raw-window-handle"
@ -2496,15 +2498,15 @@ dependencies = [
[[package]]
name = "rustybuzz"
version = "0.12.1"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0ae5692c5beaad6a9e22830deeed7874eae8a4e3ba4076fb48e12c56856222c"
checksum = "2ee8fe2a8461a0854a37101fe7a1b13998d0cfa987e43248e81d2a5f4570f6fa"
dependencies = [
"bitflags 2.4.2",
"bitflags 1.3.2",
"bytemuck",
"libm",
"smallvec",
"ttf-parser 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ttf-parser 0.20.0",
"unicode-bidi-mirroring",
"unicode-ccc",
"unicode-properties",
@ -2540,7 +2542,7 @@ checksum = "82b2eaf3a5b264a521b988b2e73042e742df700c4f962cde845d1541adb46550"
dependencies = [
"ab_glyph",
"log",
"memmap2",
"memmap2 0.9.4",
"smithay-client-toolkit",
"tiny-skia",
]
@ -2623,7 +2625,6 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a"
dependencies = [
"serde",
"version_check",
]
@ -2645,7 +2646,7 @@ dependencies = [
"cursor-icon",
"libc",
"log",
"memmap2",
"memmap2 0.9.4",
"rustix 0.38.31",
"thiserror",
"wayland-backend",
@ -2899,17 +2900,15 @@ dependencies = [
[[package]]
name = "ttf-parser"
version = "0.20.0"
version = "0.19.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4"
checksum = "49d64318d8311fc2668e48b63969f4343e0a85c4a109aa8460d6672e364b8bd1"
[[package]]
name = "ttf-parser"
version = "0.20.0"
source = "git+https://github.com/StratusFearMe21/ttf-parser?branch=serde#0599c4c650883cf6add9d7b78329a17b736bf060"
dependencies = [
"serde",
]
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4"
[[package]]
name = "type-map"
@ -3677,7 +3676,7 @@ dependencies = [
"js-sys",
"libc",
"log",
"memmap2",
"memmap2 0.9.4",
"ndk",
"ndk-sys",
"objc2 0.4.1",

View file

@ -8,8 +8,8 @@ edition = "2021"
[dependencies]
egui = { version = "0.26.2", default-features = false }
egui-wgpu = "0.26.2"
glyphon = { git = "https://github.com/StratusFearMe21/glyphon", version = "0.5.0" }
glyphon = "0.5.0"
[dev-dependencies]
eframe = { version = "0.26.2", features = ["wgpu"] }
env_logger = "0.11.1"
env_logger = "0.11.3"

View file

@ -1,9 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::{
ops::{Deref, DerefMut},
sync::Arc,
};
use std::sync::Arc;
use eframe::{
egui::{self, Slider},
@ -18,6 +15,7 @@ use egui_glyphon::{
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
};
use glyphon::Buffer;
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@ -32,27 +30,6 @@ fn main() -> Result<(), eframe::Error> {
)
}
struct Buffer(egui_glyphon::glyphon::Buffer);
impl AsRef<egui_glyphon::glyphon::Buffer> for Buffer {
fn as_ref(&self) -> &egui_glyphon::glyphon::Buffer {
&self.0
}
}
impl Deref for Buffer {
type Target = egui_glyphon::glyphon::Buffer;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
struct MyApp {
font_system: Arc<Mutex<FontSystem>>,
size: f32,
@ -62,14 +39,17 @@ struct MyApp {
impl Default for MyApp {
fn default() -> Self {
let mut font_system = FontSystem::new();
let mut buffer = Buffer(egui_glyphon::glyphon::Buffer::new(
&mut font_system,
Metrics::new(30.0, 42.0),
));
let mut buffer =
egui_glyphon::glyphon::Buffer::new(&mut font_system, Metrics::new(30.0, 42.0));
buffer.set_size(&mut font_system, 16.0, 9.0);
buffer.set_text(&mut font_system, "<== Hello world! ==> 👋\nThis is rendered with 🦅 glyphon 🦁\nThe text below should be partially clipped.\na b c d e f g h i j k l m n o p q r s t u v w x y z", Attrs::new().family(Family::SansSerif), Shaping::Advanced);
buffer.shape_until_scroll(&mut font_system, true);
buffer.set_text(&mut font_system, "<== Hello world! ==> 👋\nThis is rendered with 🦅 glyphon 🦁\nThe text below should be partially clipped.\na b c d e f g h i j k l m n o p q r s t u v w x y z fi ffi 🐕‍🦺 fi ffi
fi تما 🐕🦺 ffi تما
ffi fi 🐕🦺 ffi fi
تما تما 🐕🦺 تما
تما ffi 🐕🦺 تما fi تما
تما تما 🐕🦺 تما", Attrs::new().family(Family::SansSerif), Shaping::Advanced);
buffer.shape_until_scroll(&mut font_system);
Self {
font_system: Arc::new(Mutex::new(font_system)),
buffer: Arc::new(RwLock::new(buffer)),
@ -99,14 +79,14 @@ impl eframe::App for MyApp {
let mut buffer = self.buffer.write();
buffer.set_metrics(&mut font_system, Metrics::new(self.size, self.size));
buffer.set_size(&mut font_system, size.x, size.y);
buffer.shape_until_scroll(&mut font_system, true);
buffer.shape_until_scroll(&mut font_system);
}
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(Slider::new(&mut self.size, 0.1..=67.5));
let rect = Rect::from_min_size(ui.cursor().min, size);
let buffers: Vec<BufferWithTextArea<Buffer>> = vec![BufferWithTextArea::new(
Arc::clone(&self.buffer),
let buffers: Vec<BufferWithTextArea> = vec![BufferWithTextArea::new(
self.buffer.clone(),
rect,
1.0,
egui_glyphon::glyphon::Color::rgb(255, 255, 255),

View file

@ -1,6 +1,6 @@
//! This crate is for using [`glyphon`] to render advanced shaped text to the screen in an [`egui`] application
//! Please see the example for a primer on how to use this crate
use std::ops::{Deref, DerefMut};
use std::ops::DerefMut;
use std::sync::Arc;
use egui::mutex::{Mutex, RwLock};
@ -15,8 +15,8 @@ use glyphon::{
pub use glyphon;
/// A text buffer with some accosiated data used to construect a [`glyphon::TextArea`]
pub struct BufferWithTextArea<T: AsRef<Buffer> + Send + Sync> {
pub buffer: Arc<RwLock<T>>,
pub struct BufferWithTextArea {
pub buffer: Arc<RwLock<Buffer>>,
pub rect: Rect,
pub scale: f32,
pub opacity: f32,
@ -47,9 +47,9 @@ pub fn measure_buffer(buffer: &Buffer) -> Rect {
)
}
impl<T: AsRef<Buffer> + Send + Sync + 'static> BufferWithTextArea<T> {
impl BufferWithTextArea {
pub fn new(
buffer: Arc<RwLock<T>>,
buffer: Arc<RwLock<Buffer>>,
rect: Rect,
opacity: f32,
default_color: Color,
@ -77,10 +77,7 @@ pub struct GlyphonRenderer {
impl GlyphonRenderer {
/// Insert an instance of itself into the [`egui_wgpu::RenderState`]
pub fn insert<'a>(
wgpu_render_state: &'a egui_wgpu::RenderState,
font_system: Arc<Mutex<FontSystem>>,
) {
pub fn insert(wgpu_render_state: &egui_wgpu::RenderState, font_system: Arc<Mutex<FontSystem>>) {
let device = &wgpu_render_state.device;
let queue = &wgpu_render_state.queue;
@ -89,7 +86,7 @@ impl GlyphonRenderer {
device,
queue,
wgpu_render_state.target_format,
ColorMode::Egui,
ColorMode::Web,
);
let text_renderer =
TextRenderer::new(&mut atlas, device, wgpu::MultisampleState::default(), None);
@ -106,12 +103,12 @@ impl GlyphonRenderer {
});
}
fn prepare<A: AsRef<Buffer>, T: Deref<Target = A>>(
fn prepare<'a>(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
screen_resolution: Resolution,
text_areas: impl IntoIterator<Item = TextArea<A, T>>,
text_areas: impl IntoIterator<Item = TextArea<'a>>,
) -> Result<(), PrepareError> {
self.text_renderer.prepare(
device,
@ -132,12 +129,12 @@ impl GlyphonRenderer {
/// A callback which can be put into an [`egui_wgpu::renderer::Callback`].
// And wrapped with an [`egui::PaintCallback`]. Only add one callback per individual
// deffered viewport.
pub struct GlyphonRendererCallback<T: AsRef<Buffer> + Send + Sync> {
pub struct GlyphonRendererCallback {
/// These buffers will be rendered to the screen all at the same time on the same layer.
pub buffers: Vec<BufferWithTextArea<T>>,
pub buffers: Vec<BufferWithTextArea>,
}
impl<T: AsRef<Buffer> + Send + Sync> egui_wgpu::CallbackTrait for GlyphonRendererCallback<T> {
impl egui_wgpu::CallbackTrait for GlyphonRendererCallback {
fn prepare(
&self,
device: &wgpu::Device,
@ -148,6 +145,25 @@ impl<T: AsRef<Buffer> + Send + Sync> egui_wgpu::CallbackTrait for GlyphonRendere
) -> Vec<wgpu::CommandBuffer> {
let glyphon_renderer: &mut GlyphonRenderer = resources.get_mut().unwrap();
glyphon_renderer.atlas.trim();
let bufrefs: Vec<_> = self.buffers.iter().map(|b| b.buffer.read()).collect();
let text_areas: Vec<_> = self
.buffers
.iter()
.enumerate()
.map(|(i, b)| TextArea {
buffer: bufrefs.get(i).unwrap(),
left: b.rect.left(),
top: b.rect.top(),
scale: b.scale,
bounds: TextBounds {
left: b.rect.left() as i32,
top: b.rect.top() as i32,
right: b.rect.right() as i32,
bottom: b.rect.bottom() as i32,
},
default_color: b.default_color,
})
.collect();
glyphon_renderer
.prepare(
device,
@ -156,20 +172,7 @@ impl<T: AsRef<Buffer> + Send + Sync> egui_wgpu::CallbackTrait for GlyphonRendere
width: screen_descriptor.size_in_pixels[0],
height: screen_descriptor.size_in_pixels[1],
},
self.buffers.iter().map(|b| TextArea {
buffer: b.buffer.read(),
left: b.rect.left(),
top: b.rect.top(),
scale: b.scale,
opacity: b.opacity,
bounds: TextBounds {
left: b.rect.left() as i32,
top: b.rect.top() as i32,
right: b.rect.right() as i32,
bottom: b.rect.bottom() as i32,
},
default_color: b.default_color,
}),
text_areas,
)
.unwrap();
Vec::new()