Compare commits
No commits in common. "3626c7e55d45734eff9b1b7df04349d97de77667" and "b67c861a8d21a23d1311a09c465c8d3450cdff7e" have entirely different histories.
3626c7e55d
...
b67c861a8d
5 changed files with 165 additions and 2788 deletions
2729
Cargo.lock
generated
2729
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
11
Cargo.toml
11
Cargo.toml
|
@ -6,11 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
egui = { version = "0.26.2", default-features = false }
|
||||
egui-wgpu = "0.26.2"
|
||||
glyphon = { git = "https://git.nations.lol/fnmain/glyphon.git" }
|
||||
|
||||
[dev-dependencies]
|
||||
cosmic-jotdown = { path = "../cosmic-jotdown" }
|
||||
eframe = { version = "0.26.2", features = ["wgpu"] }
|
||||
env_logger = "0.11.3"
|
||||
egui = { git = "https://github.com/emilk/egui", version = "0.25.0" }
|
||||
egui-wgpu = { git = "https://github.com/emilk/egui", version = "0.25.0" }
|
||||
glyphon = { git = "https://github.com/StratusFearMe21/glyphon" }
|
||||
|
|
21
LICENSE
21
LICENSE
|
@ -1,21 +0,0 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2024 Isaac Mills
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -1,128 +0,0 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
use eframe::{
|
||||
egui::{self, Slider},
|
||||
egui_wgpu,
|
||||
epaint::{
|
||||
mutex::{Mutex, RwLock},
|
||||
Rect, Vec2,
|
||||
},
|
||||
CreationContext,
|
||||
};
|
||||
use egui::{Align2, Pos2};
|
||||
use egui_glyphon::{
|
||||
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
|
||||
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
|
||||
};
|
||||
use glyphon::{cosmic_text::Align, Buffer};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"My egui App",
|
||||
options,
|
||||
Box::new(|cc| Box::new(MyApp::new(cc))),
|
||||
)
|
||||
}
|
||||
|
||||
struct MyApp {
|
||||
font_system: Arc<Mutex<FontSystem>>,
|
||||
buffers: Vec<Arc<RwLock<Buffer>>>,
|
||||
}
|
||||
|
||||
impl Default for MyApp {
|
||||
fn default() -> Self {
|
||||
let mut font_system = FontSystem::new();
|
||||
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();
|
||||
|
||||
Self {
|
||||
font_system: Arc::new(Mutex::new(font_system)),
|
||||
buffers,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
fn new(cc: &CreationContext<'_>) -> Self {
|
||||
let app = Self::default();
|
||||
|
||||
if let Some(ref wgpu) = cc.wgpu_render_state {
|
||||
GlyphonRenderer::insert(wgpu, Arc::clone(&app.font_system));
|
||||
}
|
||||
|
||||
app
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
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 },
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
64
src/lib.rs
64
src/lib.rs
|
@ -1,12 +1,12 @@
|
|||
//! 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::DerefMut;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::Arc;
|
||||
|
||||
use egui::mutex::{Mutex, RwLock};
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use egui_wgpu::renderer::ScreenDescriptor;
|
||||
use egui_wgpu::wgpu;
|
||||
use egui_wgpu::ScreenDescriptor;
|
||||
use glyphon::{
|
||||
Buffer, Color, ColorMode, FontSystem, PrepareError, RenderError, Resolution, SwashCache,
|
||||
TextArea, TextAtlas, TextBounds, TextRenderer,
|
||||
|
@ -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 {
|
||||
pub buffer: Arc<RwLock<Buffer>>,
|
||||
pub struct BufferWithTextArea<T: AsRef<Buffer> + Send + Sync> {
|
||||
pub buffer: Arc<RwLock<T>>,
|
||||
pub rect: Rect,
|
||||
pub scale: f32,
|
||||
pub opacity: f32,
|
||||
|
@ -47,9 +47,9 @@ pub fn measure_buffer(buffer: &Buffer) -> Rect {
|
|||
)
|
||||
}
|
||||
|
||||
impl BufferWithTextArea {
|
||||
impl<T: AsRef<Buffer> + Send + Sync + 'static> BufferWithTextArea<T> {
|
||||
pub fn new(
|
||||
buffer: Arc<RwLock<Buffer>>,
|
||||
buffer: Arc<RwLock<T>>,
|
||||
rect: Rect,
|
||||
opacity: f32,
|
||||
default_color: Color,
|
||||
|
@ -77,7 +77,10 @@ pub struct GlyphonRenderer {
|
|||
|
||||
impl GlyphonRenderer {
|
||||
/// Insert an instance of itself into the [`egui_wgpu::RenderState`]
|
||||
pub fn insert(wgpu_render_state: &egui_wgpu::RenderState, font_system: Arc<Mutex<FontSystem>>) {
|
||||
pub fn insert<'a>(
|
||||
wgpu_render_state: &'a egui_wgpu::RenderState,
|
||||
font_system: Arc<Mutex<FontSystem>>,
|
||||
) {
|
||||
let device = &wgpu_render_state.device;
|
||||
let queue = &wgpu_render_state.queue;
|
||||
|
||||
|
@ -86,7 +89,7 @@ impl GlyphonRenderer {
|
|||
device,
|
||||
queue,
|
||||
wgpu_render_state.target_format,
|
||||
ColorMode::Web,
|
||||
ColorMode::Egui,
|
||||
);
|
||||
let text_renderer =
|
||||
TextRenderer::new(&mut atlas, device, wgpu::MultisampleState::default(), None);
|
||||
|
@ -103,12 +106,12 @@ impl GlyphonRenderer {
|
|||
});
|
||||
}
|
||||
|
||||
fn prepare<'a>(
|
||||
fn prepare<A: AsRef<Buffer>, T: Deref<Target = A>>(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
screen_resolution: Resolution,
|
||||
text_areas: impl IntoIterator<Item = TextArea<'a>>,
|
||||
text_areas: impl IntoIterator<Item = TextArea<A, T>>,
|
||||
) -> Result<(), PrepareError> {
|
||||
self.text_renderer.prepare(
|
||||
device,
|
||||
|
@ -129,12 +132,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 {
|
||||
pub struct GlyphonRendererCallback<T: AsRef<Buffer> + Send + Sync> {
|
||||
/// These buffers will be rendered to the screen all at the same time on the same layer.
|
||||
pub buffers: Vec<BufferWithTextArea>,
|
||||
pub buffers: Vec<BufferWithTextArea<T>>,
|
||||
}
|
||||
|
||||
impl egui_wgpu::CallbackTrait for GlyphonRendererCallback {
|
||||
impl<T: AsRef<Buffer> + Send + Sync> egui_wgpu::CallbackTrait for GlyphonRendererCallback<T> {
|
||||
fn prepare(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
|
@ -145,26 +148,6 @@ impl egui_wgpu::CallbackTrait for GlyphonRendererCallback {
|
|||
) -> 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,
|
||||
opacity: b.opacity,
|
||||
})
|
||||
.collect();
|
||||
glyphon_renderer
|
||||
.prepare(
|
||||
device,
|
||||
|
@ -173,7 +156,20 @@ impl egui_wgpu::CallbackTrait for GlyphonRendererCallback {
|
|||
width: screen_descriptor.size_in_pixels[0],
|
||||
height: screen_descriptor.size_in_pixels[1],
|
||||
},
|
||||
text_areas,
|
||||
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,
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
Vec::new()
|
||||
|
|
Loading…
Reference in a new issue