Link to forgejo dep
This commit is contained in:
parent
206aa971e2
commit
3626c7e55d
4 changed files with 133 additions and 69 deletions
|
@ -1,6 +1,6 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
use eframe::{
|
||||
egui::{self, Slider},
|
||||
|
@ -11,11 +11,12 @@ use eframe::{
|
|||
},
|
||||
CreationContext,
|
||||
};
|
||||
use egui::{Align2, Pos2};
|
||||
use egui_glyphon::{
|
||||
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
|
||||
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
|
||||
};
|
||||
use glyphon::Buffer;
|
||||
use glyphon::{cosmic_text::Align, Buffer};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
|
@ -32,28 +33,26 @@ fn main() -> Result<(), eframe::Error> {
|
|||
|
||||
struct MyApp {
|
||||
font_system: Arc<Mutex<FontSystem>>,
|
||||
size: f32,
|
||||
buffer: Arc<RwLock<Buffer>>,
|
||||
buffers: Vec<Arc<RwLock<Buffer>>>,
|
||||
}
|
||||
|
||||
impl Default for MyApp {
|
||||
fn default() -> Self {
|
||||
let mut font_system = FontSystem::new();
|
||||
let mut buffer =
|
||||
egui_glyphon::glyphon::Buffer::new(&mut font_system, Metrics::new(30.0, 42.0));
|
||||
let buffers = cosmic_jotdown::jotdown_into_buffers(
|
||||
r#"# Header
|
||||
|
||||
Test _test_ *test* 'test' "`test`""#,
|
||||
&mut font_system,
|
||||
Metrics::new(30.0, 42.0),
|
||||
f32::MAX,
|
||||
)
|
||||
.map(|buffer| Arc::new(RwLock::new(buffer)))
|
||||
.collect();
|
||||
|
||||
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 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)),
|
||||
size: 35.0,
|
||||
buffers,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,28 +69,56 @@ impl MyApp {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn measure_buffer(buffer: &Buffer, vb: Vec2) -> Rect {
|
||||
let mut rtl = false;
|
||||
let (width, total_lines) =
|
||||
buffer
|
||||
.layout_runs()
|
||||
.fold((0.0, 0usize), |(width, total_lines), run| {
|
||||
if run.rtl {
|
||||
rtl = true;
|
||||
}
|
||||
(run.line_w.max(width), total_lines + 1)
|
||||
});
|
||||
|
||||
let (max_width, max_height) = buffer.size();
|
||||
|
||||
let size = Vec2::new(
|
||||
if rtl { vb.x } else { width.min(max_width) },
|
||||
(total_lines as f32 * buffer.metrics().line_height).min(max_height),
|
||||
);
|
||||
match buffer.lines[0].align() {
|
||||
Some(Align::Right) | Some(Align::End) => {
|
||||
Align2::RIGHT_TOP.align_size_within_rect(size, Rect::from_min_size(Pos2::ZERO, vb))
|
||||
}
|
||||
Some(Align::Center) | Some(Align::Justified) => {
|
||||
Align2::CENTER_TOP.align_size_within_rect(size, Rect::from_min_size(Pos2::ZERO, vb))
|
||||
}
|
||||
Some(Align::Left) | None => Rect::from_min_size(Pos2::ZERO, size),
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let size = Vec2::new(16.0 * self.size, 9.0 * self.size);
|
||||
|
||||
{
|
||||
let mut font_system = self.font_system.lock();
|
||||
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);
|
||||
}
|
||||
|
||||
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> = vec![BufferWithTextArea::new(
|
||||
self.buffer.clone(),
|
||||
rect,
|
||||
1.0,
|
||||
egui_glyphon::glyphon::Color::rgb(255, 255, 255),
|
||||
ui.ctx(),
|
||||
)];
|
||||
let mut rect = Rect::from_min_size(ui.cursor().min, Vec2::INFINITY);
|
||||
let buffers: Vec<BufferWithTextArea> = self
|
||||
.buffers
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|buffer| {
|
||||
let buffer_size = measure_buffer(buffer.read().deref(), ui.max_rect().size());
|
||||
let this_rect = rect;
|
||||
rect.min.y += buffer_size.height() + 48.0;
|
||||
BufferWithTextArea::new(
|
||||
buffer,
|
||||
this_rect,
|
||||
1.0,
|
||||
egui_glyphon::glyphon::Color::rgb(255, 255, 255),
|
||||
ui.ctx(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
|
||||
ui.max_rect(),
|
||||
GlyphonRendererCallback { buffers },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue