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

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),