Compare commits
1 Commits
4e9eacc7b0
...
markdown-p
| Author | SHA1 | Date | |
|---|---|---|---|
|
7f93084e64
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1344,7 +1344,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "inkr"
|
||||
version = "1.0.0"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"eframe",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "inkr"
|
||||
version = "1.0.0"
|
||||
version = "0.1.0"
|
||||
authors = []
|
||||
edition = "2024"
|
||||
|
||||
|
||||
27
PKGBUILD
27
PKGBUILD
@ -1,27 +0,0 @@
|
||||
pkgname=inkr
|
||||
pkgver=1.0.0
|
||||
pkgrel=1
|
||||
pkgdesc="A note-taking and handwriting tool"
|
||||
arch=('x86_64' 'aarch64')
|
||||
url="https://git.nubo.sh/hulthe/inkr"
|
||||
#license=('GPL')
|
||||
groups=('base-devel')
|
||||
depends=('glibc')
|
||||
makedepends=('cargo')
|
||||
#optdepends=('ed: for "patch -e" functionality')
|
||||
#source=(" ftp://ftp.gnu.org/gnu/$pkgname/$pkgname-$pkgver.tar.xz"{,.sig})
|
||||
#sha256sums=('SKIP')
|
||||
prepare() {
|
||||
export RUSTUP_TOOLCHAIN=stable
|
||||
cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
|
||||
}
|
||||
build() {
|
||||
export RUSTUP_TOOLCHAIN=stable
|
||||
cargo build --frozen --release
|
||||
}
|
||||
package() {
|
||||
cd ..
|
||||
install -Dm0755 -t "$pkgdir/usr/bin/" "${CARGO_TARGET_DIR:-target}/release/$pkgname"
|
||||
install -Dm0755 -t "$pkgdir/usr/share/applications/" "assets/$pkgname.desktop"
|
||||
install -Dm0755 "assets/icon.svg" "$pkgdir/usr/share/pixmaps/$pkgname.svg"
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
[Desktop Entry]
|
||||
Name=inkr
|
||||
Exec=inkr
|
||||
Terminal=false
|
||||
Type=Application
|
||||
Icon=inkr
|
||||
StartupWMClass=inkr
|
||||
MimeType=x-scheme-handler/inkr;
|
||||
Categories=Office;
|
||||
164
src/app.rs
164
src/app.rs
@ -2,14 +2,11 @@ use std::{
|
||||
fs,
|
||||
path::PathBuf,
|
||||
sync::{Arc, mpsc},
|
||||
thread::JoinHandle,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use crate::{file_editor::FileEditor, preferences::Preferences, util::GuiSender};
|
||||
use egui::{
|
||||
Align, Button, Color32, Context, FontData, FontDefinitions, Key, Modifiers, PointerButton,
|
||||
RichText, ScrollArea, Stroke,
|
||||
Align, Button, Color32, FontData, FontDefinitions, PointerButton, RichText, ScrollArea, Stroke,
|
||||
};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
@ -21,8 +18,6 @@ pub struct App {
|
||||
actions_tx: mpsc::Sender<Action>,
|
||||
#[serde(skip)]
|
||||
actions_rx: mpsc::Receiver<Action>,
|
||||
#[serde(skip)]
|
||||
jobs: Jobs,
|
||||
|
||||
tabs: Vec<(TabId, Tab)>,
|
||||
open_tab_index: Option<usize>,
|
||||
@ -30,33 +25,6 @@ pub struct App {
|
||||
next_tab_id: TabId,
|
||||
}
|
||||
|
||||
pub struct Jobs {
|
||||
handles: Vec<JoinHandle<()>>,
|
||||
actions_tx: mpsc::Sender<Action>,
|
||||
}
|
||||
|
||||
impl Jobs {
|
||||
fn start(&mut self, ctx: &Context, job: impl FnOnce() -> Option<Action> + Send + 'static) {
|
||||
let ctx = ctx.clone();
|
||||
let actions_tx = self.actions_tx.clone();
|
||||
self.handles.push(std::thread::spawn(move || {
|
||||
// start rendering the spinner thingy
|
||||
ctx.request_repaint();
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
if let Some(action) = job() {
|
||||
let _ = actions_tx.send(action);
|
||||
ctx.request_repaint();
|
||||
};
|
||||
|
||||
// Make sure that task takes at least 250ms to run, so that the spinner won't blink
|
||||
let sleep_for = Duration::from_millis(250).saturating_sub(start.elapsed());
|
||||
std::thread::sleep(sleep_for);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
enum Tab {
|
||||
File(FileEditor),
|
||||
@ -68,12 +36,6 @@ impl Tab {
|
||||
Tab::File(file_editor) => file_editor.title(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_dirty(&self) -> bool {
|
||||
match self {
|
||||
Tab::File(file_editor) => file_editor.is_dirty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type TabId = usize;
|
||||
@ -93,12 +55,8 @@ impl Default for App {
|
||||
let (actions_tx, actions_rx) = mpsc::channel();
|
||||
Self {
|
||||
preferences: Preferences::default(),
|
||||
actions_tx: actions_tx.clone(/* this is silly, i know */),
|
||||
actions_rx,
|
||||
jobs: Jobs {
|
||||
handles: Default::default(),
|
||||
actions_tx,
|
||||
},
|
||||
actions_rx,
|
||||
tabs: vec![(1, Tab::File(FileEditor::new("note.md")))],
|
||||
open_tab_index: None,
|
||||
next_tab_id: 2,
|
||||
@ -188,7 +146,7 @@ impl App {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn actions_tx(&self, ctx: &Context) -> GuiSender<Action> {
|
||||
fn actions_tx(&self, ctx: &egui::Context) -> GuiSender<Action> {
|
||||
GuiSender::new(self.actions_tx.clone(), ctx)
|
||||
}
|
||||
|
||||
@ -217,11 +175,9 @@ impl eframe::App for App {
|
||||
eframe::set_value(storage, eframe::APP_KEY, self);
|
||||
}
|
||||
|
||||
fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
self.preferences.apply(ctx);
|
||||
|
||||
self.jobs.handles.retain(|job| !job.is_finished());
|
||||
|
||||
while let Ok(action) = self.actions_rx.try_recv() {
|
||||
self.handle_action(action);
|
||||
}
|
||||
@ -230,11 +186,11 @@ impl eframe::App for App {
|
||||
self.open_tab_index = Some(self.tabs.len().saturating_sub(1));
|
||||
}
|
||||
|
||||
ctx.input_mut(|input| {
|
||||
if input.consume_key(Modifiers::CTRL, Key::S) {
|
||||
self.save_active_tab(ctx);
|
||||
}
|
||||
});
|
||||
//ctx.input_mut(|input| {
|
||||
// if input.consume_key(Modifiers::CTRL, Key::H) {
|
||||
// self.buffer.push(BufferItem::Painting(Default::default()));
|
||||
// }
|
||||
//});
|
||||
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
egui::containers::menu::Bar::new().ui(ui, |ui| {
|
||||
@ -249,15 +205,22 @@ impl eframe::App for App {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if ui.button("Open File").clicked() {
|
||||
self.jobs.start(ui.ctx(), move || {
|
||||
let file_path = rfd::FileDialog::new().pick_file()?;
|
||||
let actions_tx = self.actions_tx(ui.ctx());
|
||||
std::thread::spawn(move || {
|
||||
let file = rfd::FileDialog::new().pick_file();
|
||||
|
||||
let text = fs::read_to_string(&file_path)
|
||||
.inspect_err(|e| log::error!("Failed to read {file_path:?}: {e}"))
|
||||
.ok()?;
|
||||
let Some(file_path) = file else { return };
|
||||
|
||||
let text = match fs::read_to_string(&file_path) {
|
||||
Ok(text) => text,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read {file_path:?}: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let editor = FileEditor::from_file(file_path, &text);
|
||||
Some(Action::OpenFile(editor))
|
||||
let _ = actions_tx.send(Action::OpenFile(editor));
|
||||
});
|
||||
}
|
||||
|
||||
@ -274,19 +237,6 @@ impl eframe::App for App {
|
||||
}
|
||||
}
|
||||
|
||||
let can_save_file = self
|
||||
.open_tab_index
|
||||
.and_then(|i| self.tabs.get(i))
|
||||
.and_then(|(id, tab)| match tab {
|
||||
Tab::File(file_editor) => Some((*id, file_editor)),
|
||||
})
|
||||
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)))
|
||||
.is_some();
|
||||
|
||||
if ui.add_enabled(can_save_file, Button::new("Save")).clicked() {
|
||||
self.save_active_tab(ui.ctx());
|
||||
}
|
||||
|
||||
let open_file =
|
||||
self.open_tab_index
|
||||
.and_then(|i| self.tabs.get(i))
|
||||
@ -294,22 +244,45 @@ impl eframe::App for App {
|
||||
Tab::File(file_editor) => Some((*id, file_editor)),
|
||||
});
|
||||
|
||||
let open_file_with_path = open_file
|
||||
.clone()
|
||||
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)));
|
||||
|
||||
if ui
|
||||
.add_enabled(open_file_with_path.is_some(), Button::new("Save"))
|
||||
.clicked()
|
||||
{
|
||||
if let Some((file_path, file_editor)) = open_file_with_path {
|
||||
let text = file_editor.to_string();
|
||||
let file_path = file_path.to_owned();
|
||||
std::thread::spawn(move || {
|
||||
if let Err(e) = fs::write(file_path, text.as_bytes()) {
|
||||
log::error!("{e}");
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if ui
|
||||
.add_enabled(open_file.is_some(), Button::new("Save As"))
|
||||
.clicked()
|
||||
{
|
||||
let actions_tx = self.actions_tx(ui.ctx());
|
||||
let (tab_id, editor) =
|
||||
open_file.expect("We checked that open_file is_some");
|
||||
let text = editor.to_string();
|
||||
self.jobs.start(ui.ctx(), move || {
|
||||
let file_path = rfd::FileDialog::new().save_file()?;
|
||||
std::thread::spawn(move || {
|
||||
let Some(file_path) = rfd::FileDialog::new().save_file() else {
|
||||
return;
|
||||
};
|
||||
|
||||
fs::write(&file_path, text.as_bytes())
|
||||
.inspect_err(|e| log::error!("{e}"))
|
||||
.ok()?;
|
||||
if let Err(e) = fs::write(&file_path, text.as_bytes()) {
|
||||
log::error!("{e}");
|
||||
return;
|
||||
};
|
||||
|
||||
Some(Action::MoveFile(tab_id, file_path))
|
||||
let _ = actions_tx.send(Action::MoveFile(tab_id, file_path));
|
||||
});
|
||||
}
|
||||
|
||||
@ -324,9 +297,7 @@ impl eframe::App for App {
|
||||
}
|
||||
});
|
||||
|
||||
if !self.jobs.handles.is_empty() {
|
||||
ui.spinner();
|
||||
}
|
||||
ui.add_space(16.0);
|
||||
|
||||
ui.add_space(16.0);
|
||||
|
||||
@ -335,7 +306,8 @@ impl eframe::App for App {
|
||||
let selected = self.open_tab_index == Some(i);
|
||||
let mut button = Button::new(tab.title()).selected(selected);
|
||||
|
||||
if tab.is_dirty() {
|
||||
let dirty = i == 0; // TODO: mark as dirty when contents hasn't been saved
|
||||
if dirty {
|
||||
button = button.right_text(RichText::new("*").strong())
|
||||
}
|
||||
|
||||
@ -382,33 +354,5 @@ impl App {
|
||||
let id = self.next_tab_id;
|
||||
self.next_tab_id += 1;
|
||||
self.tabs.insert(i, (id, tab));
|
||||
self.open_tab_index = Some(i);
|
||||
}
|
||||
|
||||
fn save_active_tab(&mut self, ctx: &Context) {
|
||||
let open_file = self
|
||||
.open_tab_index
|
||||
.and_then(|i| self.tabs.get_mut(i))
|
||||
.and_then(|(id, tab)| match tab {
|
||||
Tab::File(file_editor) => Some((*id, file_editor)),
|
||||
})
|
||||
.and_then(|(_, file_editor)| {
|
||||
file_editor
|
||||
.path()
|
||||
.map(ToOwned::to_owned)
|
||||
.zip(Some(file_editor))
|
||||
});
|
||||
|
||||
if let Some((file_path, file_editor)) = open_file {
|
||||
file_editor.is_dirty = false;
|
||||
let text = file_editor.to_string();
|
||||
let file_path = file_path.to_owned();
|
||||
self.jobs.start(ctx, move || {
|
||||
if let Err(e) = fs::write(file_path, text.as_bytes()) {
|
||||
log::error!("{e}");
|
||||
};
|
||||
None
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,243 +0,0 @@
|
||||
use egui::text::{CCursorRange, LayoutJob};
|
||||
|
||||
use crate::easy_mark::easy_mark_parser;
|
||||
|
||||
/// Highlight easymark, memoizing previous output to save CPU.
|
||||
///
|
||||
/// In practice, the highlighter is fast enough not to need any caching.
|
||||
#[derive(Default)]
|
||||
pub struct MemoizedHighlighter {
|
||||
style: egui::Style,
|
||||
code: String,
|
||||
output: LayoutJob,
|
||||
}
|
||||
|
||||
impl MemoizedHighlighter {
|
||||
pub fn highlight(
|
||||
&mut self,
|
||||
egui_style: &egui::Style,
|
||||
code: &str,
|
||||
cursor: Option<CCursorRange>,
|
||||
) -> LayoutJob {
|
||||
if (&self.style, self.code.as_str()) != (egui_style, code) {
|
||||
self.style = egui_style.clone();
|
||||
code.clone_into(&mut self.code);
|
||||
self.output = highlight_easymark(egui_style, code, cursor);
|
||||
}
|
||||
self.output.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highlight_easymark(
|
||||
egui_style: &egui::Style,
|
||||
mut text: &str,
|
||||
|
||||
// TODO: hide special characters where cursor isn't
|
||||
_cursor: Option<CCursorRange>,
|
||||
) -> LayoutJob {
|
||||
let mut job = LayoutJob::default();
|
||||
let mut style = easy_mark_parser::Style::default();
|
||||
let mut start_of_line = true;
|
||||
|
||||
const CODE_INDENT: f32 = 10.0;
|
||||
|
||||
while !text.is_empty() {
|
||||
if start_of_line && text.starts_with("```") {
|
||||
let astyle = format_from_style(
|
||||
egui_style,
|
||||
&easy_mark_parser::Style {
|
||||
code: true,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
// Render the initial backticks as spaces
|
||||
text = &text[3..];
|
||||
job.append(" ", CODE_INDENT, astyle.clone());
|
||||
|
||||
match text.find("\n```") {
|
||||
Some(n) => {
|
||||
for line in text[..n + 1].lines() {
|
||||
job.append(line, CODE_INDENT, astyle.clone());
|
||||
job.append("\n", 0.0, astyle.clone());
|
||||
}
|
||||
// Render the final backticks as spaces
|
||||
job.append(" ", CODE_INDENT, astyle);
|
||||
text = &text[n + 4..];
|
||||
}
|
||||
None => {
|
||||
job.append(text, 0.0, astyle.clone());
|
||||
text = "";
|
||||
}
|
||||
};
|
||||
style = Default::default();
|
||||
continue;
|
||||
}
|
||||
|
||||
if text.starts_with('`') {
|
||||
style.code = true;
|
||||
let end = text[1..]
|
||||
.find(&['`', '\n'][..])
|
||||
.map_or_else(|| text.len(), |i| i + 2);
|
||||
job.append(&text[..end], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[end..];
|
||||
style.code = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
let skip;
|
||||
|
||||
// zero-width space
|
||||
let _zws = "\u{200b}";
|
||||
|
||||
let mut apply_basic_style =
|
||||
|text: &mut &str,
|
||||
style: &mut easy_mark_parser::Style,
|
||||
access: fn(&mut easy_mark_parser::Style) -> &mut bool| {
|
||||
let skip = if *access(style) {
|
||||
// Include the character that is ending this style:
|
||||
job.append(&text[..1], 0.0, format_from_style(egui_style, style));
|
||||
*text = &text[1..];
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
*access(style) ^= true;
|
||||
skip
|
||||
};
|
||||
|
||||
if text.starts_with('*') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.strong);
|
||||
} else if text.starts_with('/') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.italics);
|
||||
} else if text.starts_with('_') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.underline);
|
||||
} else if text.starts_with('$') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.small);
|
||||
} else if text.starts_with('~') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.strikethrough);
|
||||
} else if text.starts_with('^') {
|
||||
skip = apply_basic_style(&mut text, &mut style, |style| &mut style.raised);
|
||||
} else if text.starts_with('\\') && text.len() >= 2 {
|
||||
skip = 2;
|
||||
} else if start_of_line && text.starts_with(' ') {
|
||||
// we don't preview indentation, because it is confusing
|
||||
skip = 1;
|
||||
} else if start_of_line && text.starts_with("###### ") {
|
||||
style.heading = true;
|
||||
skip = 7;
|
||||
} else if start_of_line && text.starts_with("##### ") {
|
||||
style.heading = true;
|
||||
skip = 6;
|
||||
} else if start_of_line && text.starts_with("#### ") {
|
||||
style.heading = true;
|
||||
skip = 5;
|
||||
} else if start_of_line && text.starts_with("### ") {
|
||||
style.heading = true;
|
||||
skip = 4;
|
||||
} else if start_of_line && text.starts_with("## ") {
|
||||
style.heading = true;
|
||||
skip = 3;
|
||||
} else if start_of_line && text.starts_with("# ") {
|
||||
style.heading = true;
|
||||
skip = 2;
|
||||
} else if start_of_line && text.starts_with("> ") {
|
||||
style.quoted = true;
|
||||
skip = 2;
|
||||
// we don't preview indentation, because it is confusing
|
||||
} else if start_of_line && text.trim_start().starts_with("- ") {
|
||||
job.append("• ", 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[2..];
|
||||
skip = 0;
|
||||
// we don't preview indentation, because it is confusing
|
||||
} else {
|
||||
skip = 0;
|
||||
}
|
||||
// Note: we don't preview underline, strikethrough and italics because it confuses things.
|
||||
|
||||
// Swallow everything up to the next special character:
|
||||
let line_end = text[skip..]
|
||||
.find('\n')
|
||||
.map_or_else(|| text.len(), |i| (skip + i + 1));
|
||||
let end = text[skip..]
|
||||
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '['][..])
|
||||
.map_or_else(|| text.len(), |i| (skip + i).max(1));
|
||||
|
||||
if line_end <= end {
|
||||
job.append(
|
||||
&text[..line_end],
|
||||
0.0,
|
||||
format_from_style(egui_style, &style),
|
||||
);
|
||||
text = &text[line_end..];
|
||||
start_of_line = true;
|
||||
style = Default::default();
|
||||
} else {
|
||||
job.append(&text[..end], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[end..];
|
||||
start_of_line = false;
|
||||
}
|
||||
}
|
||||
|
||||
job
|
||||
}
|
||||
|
||||
fn format_from_style(
|
||||
egui_style: &egui::Style,
|
||||
emark_style: &easy_mark_parser::Style,
|
||||
) -> egui::text::TextFormat {
|
||||
use egui::{Align, Color32, Stroke, TextStyle};
|
||||
|
||||
let color = if emark_style.strong || emark_style.heading {
|
||||
egui_style.visuals.strong_text_color()
|
||||
} else if emark_style.quoted {
|
||||
egui_style.visuals.weak_text_color()
|
||||
} else {
|
||||
egui_style.visuals.text_color()
|
||||
};
|
||||
|
||||
let text_style = if emark_style.heading {
|
||||
TextStyle::Heading
|
||||
} else if emark_style.code {
|
||||
TextStyle::Monospace
|
||||
} else if emark_style.small | emark_style.raised {
|
||||
TextStyle::Small
|
||||
} else {
|
||||
TextStyle::Body
|
||||
};
|
||||
|
||||
let background = if emark_style.code {
|
||||
egui_style.visuals.code_bg_color
|
||||
} else {
|
||||
Color32::TRANSPARENT
|
||||
};
|
||||
|
||||
let underline = if emark_style.underline {
|
||||
Stroke::new(1.0, color)
|
||||
} else {
|
||||
Stroke::NONE
|
||||
};
|
||||
|
||||
let strikethrough = if emark_style.strikethrough {
|
||||
Stroke::new(1.0, color)
|
||||
} else {
|
||||
Stroke::NONE
|
||||
};
|
||||
|
||||
let valign = if emark_style.raised {
|
||||
Align::TOP
|
||||
} else {
|
||||
Align::BOTTOM
|
||||
};
|
||||
|
||||
egui::text::TextFormat {
|
||||
font_id: text_style.resolve(egui_style),
|
||||
color,
|
||||
background,
|
||||
italics: emark_style.italics,
|
||||
underline,
|
||||
strikethrough,
|
||||
valign,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@ -1,346 +0,0 @@
|
||||
//! A parser for `EasyMark`: a very simple markup language.
|
||||
//!
|
||||
//! WARNING: `EasyMark` is subject to change.
|
||||
//
|
||||
//! # `EasyMark` design goals:
|
||||
//! 1. easy to parse
|
||||
//! 2. easy to learn
|
||||
//! 3. similar to markdown
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Item<'a> {
|
||||
/// `\n`
|
||||
// TODO(emilk): add Style here so empty heading still uses up the right amount of space.
|
||||
Newline,
|
||||
|
||||
/// Text
|
||||
Text(Style, &'a str),
|
||||
|
||||
/// title, url
|
||||
Hyperlink(Style, &'a str, &'a str),
|
||||
|
||||
/// leading space before e.g. a [`Self::BulletPoint`].
|
||||
Indentation(usize),
|
||||
|
||||
/// >
|
||||
QuoteIndent,
|
||||
|
||||
/// - a point well made.
|
||||
BulletPoint,
|
||||
|
||||
/// 1. numbered list. The string is the number(s).
|
||||
NumberedPoint(&'a str),
|
||||
|
||||
/// ---
|
||||
Separator,
|
||||
|
||||
/// language, code
|
||||
CodeBlock(&'a str, &'a str),
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct Style {
|
||||
/// # heading (large text)
|
||||
pub heading: bool,
|
||||
|
||||
/// > quoted (slightly dimmer color or other font style)
|
||||
pub quoted: bool,
|
||||
|
||||
/// `code` (monospace, some other color)
|
||||
pub code: bool,
|
||||
|
||||
/// self.strong* (emphasized, e.g. bold)
|
||||
pub strong: bool,
|
||||
|
||||
/// _underline_
|
||||
pub underline: bool,
|
||||
|
||||
/// ~strikethrough~
|
||||
pub strikethrough: bool,
|
||||
|
||||
/// /italics/
|
||||
pub italics: bool,
|
||||
|
||||
/// $small$
|
||||
pub small: bool,
|
||||
|
||||
/// ^raised^
|
||||
pub raised: bool,
|
||||
}
|
||||
|
||||
/// Parser for the `EasyMark` markup language.
|
||||
pub struct Parser<'a> {
|
||||
/// The remainder of the input text
|
||||
s: &'a str,
|
||||
|
||||
/// Are we at the start of a line?
|
||||
start_of_line: bool,
|
||||
|
||||
/// Current self.style. Reset after a newline.
|
||||
style: Style,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
pub fn new(s: &'a str) -> Self {
|
||||
Self {
|
||||
s,
|
||||
start_of_line: true,
|
||||
style: Style::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// `1. `, `42. ` etc.
|
||||
fn numbered_list(&mut self) -> Option<Item<'a>> {
|
||||
let n_digits = self.s.chars().take_while(|c| c.is_ascii_digit()).count();
|
||||
if n_digits > 0 && self.s.chars().skip(n_digits).take(2).eq(". ".chars()) {
|
||||
let number = &self.s[..n_digits];
|
||||
self.s = &self.s[(n_digits + 2)..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::NumberedPoint(number));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// ```{language}\n{code}```
|
||||
fn code_block(&mut self) -> Option<Item<'a>> {
|
||||
if let Some(language_start) = self.s.strip_prefix("```") {
|
||||
if let Some(newline) = language_start.find('\n') {
|
||||
let language = &language_start[..newline];
|
||||
let code_start = &language_start[newline + 1..];
|
||||
if let Some(end) = code_start.find("\n```") {
|
||||
let code = &code_start[..end].trim();
|
||||
self.s = &code_start[end + 4..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::CodeBlock(language, code));
|
||||
} else {
|
||||
self.s = "";
|
||||
return Some(Item::CodeBlock(language, code_start));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// `code`
|
||||
fn inline_code(&mut self) -> Option<Item<'a>> {
|
||||
if let Some(rest) = self.s.strip_prefix('`') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.code = true;
|
||||
let rest_of_line = &self.s[..self.s.find('\n').unwrap_or(self.s.len())];
|
||||
if let Some(end) = rest_of_line.find('`') {
|
||||
let item = Item::Text(self.style, &self.s[..end]);
|
||||
self.s = &self.s[end + 1..];
|
||||
self.style.code = false;
|
||||
return Some(item);
|
||||
} else {
|
||||
let end = rest_of_line.len();
|
||||
let item = Item::Text(self.style, rest_of_line);
|
||||
self.s = &self.s[end..];
|
||||
self.style.code = false;
|
||||
return Some(item);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// `<url>` or `[link](url)`
|
||||
fn url(&mut self) -> Option<Item<'a>> {
|
||||
if self.s.starts_with('<') {
|
||||
let this_line = &self.s[..self.s.find('\n').unwrap_or(self.s.len())];
|
||||
if let Some(url_end) = this_line.find('>') {
|
||||
let url = &self.s[1..url_end];
|
||||
self.s = &self.s[url_end + 1..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::Hyperlink(self.style, url, url));
|
||||
}
|
||||
}
|
||||
|
||||
// [text](url)
|
||||
if self.s.starts_with('[') {
|
||||
let this_line = &self.s[..self.s.find('\n').unwrap_or(self.s.len())];
|
||||
if let Some(bracket_end) = this_line.find(']') {
|
||||
let text = &this_line[1..bracket_end];
|
||||
if this_line[bracket_end + 1..].starts_with('(') {
|
||||
if let Some(parens_end) = this_line[bracket_end + 2..].find(')') {
|
||||
let parens_end = bracket_end + 2 + parens_end;
|
||||
let url = &self.s[bracket_end + 2..parens_end];
|
||||
self.s = &self.s[parens_end + 1..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::Hyperlink(self.style, text, url));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Parser<'a> {
|
||||
type Item = Item<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
if self.s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// \n
|
||||
if self.s.starts_with('\n') {
|
||||
self.s = &self.s[1..];
|
||||
self.start_of_line = true;
|
||||
self.style = Style::default();
|
||||
return Some(Item::Newline);
|
||||
}
|
||||
|
||||
// Ignore line break (continue on the same line)
|
||||
if self.s.starts_with("\\\n") && self.s.len() >= 2 {
|
||||
self.s = &self.s[2..];
|
||||
self.start_of_line = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// \ escape (to show e.g. a backtick)
|
||||
if self.s.starts_with('\\') && self.s.len() >= 2 {
|
||||
let text = &self.s[1..2];
|
||||
self.s = &self.s[2..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::Text(self.style, text));
|
||||
}
|
||||
|
||||
if self.start_of_line {
|
||||
// leading space (indentation)
|
||||
if self.s.starts_with(' ') {
|
||||
let length = self.s.find(|c| c != ' ').unwrap_or(self.s.len());
|
||||
self.s = &self.s[length..];
|
||||
self.start_of_line = true; // indentation doesn't count
|
||||
return Some(Item::Indentation(length));
|
||||
}
|
||||
|
||||
// # Heading
|
||||
if let Some(after) = self.s.strip_prefix("# ") {
|
||||
self.s = after;
|
||||
self.start_of_line = false;
|
||||
self.style.heading = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// > quote
|
||||
if let Some(after) = self.s.strip_prefix("> ") {
|
||||
self.s = after;
|
||||
self.start_of_line = true; // quote indentation doesn't count
|
||||
self.style.quoted = true;
|
||||
return Some(Item::QuoteIndent);
|
||||
}
|
||||
|
||||
// - bullet point
|
||||
if self.s.starts_with("- ") {
|
||||
self.s = &self.s[2..];
|
||||
self.start_of_line = false;
|
||||
return Some(Item::BulletPoint);
|
||||
}
|
||||
|
||||
// `1. `, `42. ` etc.
|
||||
if let Some(item) = self.numbered_list() {
|
||||
return Some(item);
|
||||
}
|
||||
|
||||
// --- separator
|
||||
if let Some(after) = self.s.strip_prefix("---") {
|
||||
self.s = after.trim_start_matches('-'); // remove extra dashes
|
||||
self.s = self.s.strip_prefix('\n').unwrap_or(self.s); // remove trailing newline
|
||||
self.start_of_line = false;
|
||||
return Some(Item::Separator);
|
||||
}
|
||||
|
||||
// ```{language}\n{code}```
|
||||
if let Some(item) = self.code_block() {
|
||||
return Some(item);
|
||||
}
|
||||
}
|
||||
|
||||
// `code`
|
||||
if let Some(item) = self.inline_code() {
|
||||
return Some(item);
|
||||
}
|
||||
|
||||
if let Some(rest) = self.s.strip_prefix('*') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.strong = !self.style.strong;
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = self.s.strip_prefix('_') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.underline = !self.style.underline;
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = self.s.strip_prefix('~') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.strikethrough = !self.style.strikethrough;
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = self.s.strip_prefix('/') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.italics = !self.style.italics;
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = self.s.strip_prefix('$') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.small = !self.style.small;
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = self.s.strip_prefix('^') {
|
||||
self.s = rest;
|
||||
self.start_of_line = false;
|
||||
self.style.raised = !self.style.raised;
|
||||
continue;
|
||||
}
|
||||
|
||||
// `<url>` or `[link](url)`
|
||||
if let Some(item) = self.url() {
|
||||
return Some(item);
|
||||
}
|
||||
|
||||
// Swallow everything up to the next special character:
|
||||
let end = self
|
||||
.s
|
||||
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '[', '\n'][..])
|
||||
.map_or_else(|| self.s.len(), |special| special.max(1));
|
||||
|
||||
let item = Item::Text(self.style, &self.s[..end]);
|
||||
self.s = &self.s[end..];
|
||||
self.start_of_line = false;
|
||||
return Some(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_easy_mark_parser() {
|
||||
let items: Vec<_> = Parser::new("~strikethrough `code`~").collect();
|
||||
assert_eq!(
|
||||
items,
|
||||
vec![
|
||||
Item::Text(
|
||||
Style {
|
||||
strikethrough: true,
|
||||
..Default::default()
|
||||
},
|
||||
"strikethrough "
|
||||
),
|
||||
Item::Text(
|
||||
Style {
|
||||
code: true,
|
||||
strikethrough: true,
|
||||
..Default::default()
|
||||
},
|
||||
"code"
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
//! Experimental markup language
|
||||
|
||||
mod easy_mark_highlighter;
|
||||
pub mod easy_mark_parser;
|
||||
|
||||
pub use easy_mark_highlighter::{MemoizedHighlighter, highlight_easymark};
|
||||
pub use easy_mark_parser as parser;
|
||||
@ -12,7 +12,7 @@ use egui::{
|
||||
|
||||
use crate::{
|
||||
custom_code_block::{MdItem, iter_lines_and_code_blocks},
|
||||
handwriting::{self, Handwriting, HandwritingStyle},
|
||||
painting::{self, Handwriting, HandwritingStyle},
|
||||
preferences::Preferences,
|
||||
text_editor::MdTextEdit,
|
||||
};
|
||||
@ -22,9 +22,6 @@ pub struct FileEditor {
|
||||
title: String,
|
||||
pub path: Option<PathBuf>,
|
||||
pub buffer: Vec<BufferItem>,
|
||||
|
||||
/// Whether the file has been edited since it was laste saved to disk.
|
||||
pub is_dirty: bool,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
@ -40,7 +37,6 @@ impl FileEditor {
|
||||
title: title.into(),
|
||||
path: None,
|
||||
buffer,
|
||||
is_dirty: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,11 +69,9 @@ impl FileEditor {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("new");
|
||||
if ui.button("text").clicked() {
|
||||
self.is_dirty = true;
|
||||
self.buffer.push(BufferItem::Text(Default::default()));
|
||||
}
|
||||
if ui.button("writing").clicked() {
|
||||
self.is_dirty = true;
|
||||
self.buffer
|
||||
.push(BufferItem::Handwriting(Default::default()));
|
||||
}
|
||||
@ -144,18 +138,14 @@ impl FileEditor {
|
||||
|
||||
let item_response = ui.allocate_ui(item_size, |ui| match item {
|
||||
BufferItem::Text(text_edit) => {
|
||||
if text_edit.ui(ui).changed {
|
||||
self.is_dirty = true;
|
||||
text_edit.ui(ui);
|
||||
}
|
||||
}
|
||||
BufferItem::Handwriting(handwriting) => {
|
||||
BufferItem::Handwriting(painting) => {
|
||||
let style = HandwritingStyle {
|
||||
animate: preferences.animations,
|
||||
..HandwritingStyle::from_theme(ui.ctx().theme())
|
||||
};
|
||||
if handwriting.ui(&style, ui).changed {
|
||||
self.is_dirty = true;
|
||||
}
|
||||
painting.ui(&style, ui);
|
||||
}
|
||||
});
|
||||
|
||||
@ -221,12 +211,10 @@ impl FileEditor {
|
||||
Ordering::Greater => {
|
||||
let item = self.buffer.remove(from);
|
||||
self.buffer.insert(to, item);
|
||||
self.is_dirty = true;
|
||||
}
|
||||
Ordering::Less => {
|
||||
let item = self.buffer.remove(from);
|
||||
self.buffer.insert(to - 1, item);
|
||||
self.is_dirty = true;
|
||||
}
|
||||
Ordering::Equal => {}
|
||||
}
|
||||
@ -287,7 +275,7 @@ impl From<&str> for FileEditor {
|
||||
match item {
|
||||
MdItem::Line(line) => push_text(buffer, line),
|
||||
MdItem::CodeBlock { key, content, span } => match key {
|
||||
handwriting::CODE_BLOCK_KEY => match Handwriting::from_str(span) {
|
||||
painting::CODE_BLOCK_KEY => match Handwriting::from_str(span) {
|
||||
Ok(handwriting) => {
|
||||
if let Some(BufferItem::Text(text_edit)) = buffer.last_mut() {
|
||||
if text_edit.text.ends_with('\n') {
|
||||
|
||||
3
src/handwriting/advanced-example.md
Normal file
3
src/handwriting/advanced-example.md
Normal file
File diff suppressed because one or more lines are too long
@ -1,97 +0,0 @@
|
||||
//! see [Packet]
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use half::f16;
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
/// A `u16` encoded in little-endian.
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone, Copy, FromBytes, IntoBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
#[repr(C, packed)]
|
||||
pub struct u16_le([u8; 2]);
|
||||
|
||||
/// An `f16` encoded in little-endian.
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone, Copy, FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
pub struct f16_le(u16_le);
|
||||
|
||||
/// Top-level type describing the handwriting disk-format.
|
||||
#[derive(FromBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
pub struct DiskFormat {
|
||||
pub header: Header,
|
||||
|
||||
/// A packed array of [Stroke]s.
|
||||
pub strokes: [u8],
|
||||
}
|
||||
|
||||
pub const V1: u16_le = u16_le::new(1);
|
||||
|
||||
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
pub struct Header {
|
||||
/// Version of the disk format
|
||||
pub version: u16_le,
|
||||
}
|
||||
|
||||
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
pub struct RawStrokeHeader {
|
||||
/// Number of points in the stroke.
|
||||
pub len: u16_le,
|
||||
}
|
||||
|
||||
#[derive(FromBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
pub struct RawStroke {
|
||||
pub header: RawStrokeHeader,
|
||||
pub positions: [f16_le],
|
||||
}
|
||||
|
||||
impl RawStroke {
|
||||
pub const MIN_LEN: usize = size_of::<RawStrokeHeader>();
|
||||
}
|
||||
|
||||
impl u16_le {
|
||||
pub const fn new(init: u16) -> Self {
|
||||
u16_le(init.to_le_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl f16_le {
|
||||
pub const fn new(init: f16) -> Self {
|
||||
f16_le(u16_le::new(init.to_bits()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for u16_le {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
u16::from(*self).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16_le> for u16 {
|
||||
fn from(value: u16_le) -> Self {
|
||||
u16::from_le_bytes(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f16_le> for f16 {
|
||||
fn from(value: f16_le) -> Self {
|
||||
f16::from_bits(u16::from(value.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for u16_le {
|
||||
fn from(value: u16) -> Self {
|
||||
u16_le::new(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f16> for f16_le {
|
||||
fn from(value: f16) -> Self {
|
||||
f16_le::new(value)
|
||||
}
|
||||
}
|
||||
@ -3,9 +3,9 @@
|
||||
pub mod app;
|
||||
pub mod constants;
|
||||
pub mod custom_code_block;
|
||||
pub mod easy_mark;
|
||||
pub mod file_editor;
|
||||
pub mod handwriting;
|
||||
pub mod markdown;
|
||||
pub mod painting;
|
||||
pub mod preferences;
|
||||
pub mod rasterizer;
|
||||
pub mod text_editor;
|
||||
|
||||
54
src/markdown/ast.rs
Normal file
54
src/markdown/ast.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use super::span::Span;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Heading {
|
||||
H1,
|
||||
H2,
|
||||
H3,
|
||||
H4,
|
||||
H5,
|
||||
H6,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct Style {
|
||||
/// # heading (large text)
|
||||
pub heading: Option<Heading>,
|
||||
|
||||
/// > quoted (slightly dimmer color or other font style)
|
||||
pub quoted: bool,
|
||||
|
||||
/// `code` (monospace, some other color)
|
||||
pub code: bool,
|
||||
|
||||
/// self.strong* (emphasized, e.g. bold)
|
||||
pub strong: bool,
|
||||
|
||||
/// _underline_
|
||||
pub underline: bool,
|
||||
|
||||
/// ~strikethrough~
|
||||
pub strikethrough: bool,
|
||||
|
||||
/// /italics/
|
||||
pub italics: bool,
|
||||
|
||||
/// $small$
|
||||
pub small: bool,
|
||||
|
||||
/// ^raised^
|
||||
pub raised: bool,
|
||||
}
|
||||
|
||||
pub enum MarkdownItem<'a> {
|
||||
Text {
|
||||
span: Span<'a>,
|
||||
style: Style,
|
||||
},
|
||||
|
||||
CodeBlock {
|
||||
all: Span<'a>,
|
||||
language: Span<'a>,
|
||||
code: Span<'a>,
|
||||
},
|
||||
}
|
||||
10
src/markdown/grammar.lalrpop
Normal file
10
src/markdown/grammar.lalrpop
Normal file
@ -0,0 +1,10 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
grammar;
|
||||
|
||||
pub Term: i32 = {
|
||||
<n:Num> => n,
|
||||
"(" <t:Term> ")" => t,
|
||||
};
|
||||
|
||||
Num: i32 = <s:r"[0-9]+"> => i32::from_str(s).unwrap();
|
||||
268
src/markdown/highlighter.rs
Normal file
268
src/markdown/highlighter.rs
Normal file
@ -0,0 +1,268 @@
|
||||
use egui::text::{CCursorRange, LayoutJob};
|
||||
|
||||
use crate::markdown::{
|
||||
span::Span,
|
||||
tokenizer::{Heading, Token, TokenKind, tokenize},
|
||||
};
|
||||
|
||||
/// Highlight markdown, caching previous output to save CPU.
|
||||
#[derive(Default)]
|
||||
pub struct MemoizedHighlighter {
|
||||
style: egui::Style,
|
||||
code: String,
|
||||
output: LayoutJob,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct Style {
|
||||
/// # heading (large text)
|
||||
pub heading: Option<Heading>,
|
||||
|
||||
/// > quoted (slightly dimmer color or other font style)
|
||||
pub quoted: bool,
|
||||
|
||||
/// `code` (monospace, some other color)
|
||||
pub code: bool,
|
||||
|
||||
/// self.strong* (emphasized, e.g. bold)
|
||||
pub strong: bool,
|
||||
|
||||
/// _underline_
|
||||
pub underline: bool,
|
||||
|
||||
/// ~strikethrough~
|
||||
pub strikethrough: bool,
|
||||
|
||||
/// /italics/
|
||||
pub italics: bool,
|
||||
|
||||
/// $small$
|
||||
pub small: bool,
|
||||
|
||||
/// ^raised^
|
||||
pub raised: bool,
|
||||
}
|
||||
|
||||
impl MemoizedHighlighter {
|
||||
pub fn highlight(
|
||||
&mut self,
|
||||
egui_style: &egui::Style,
|
||||
code: &str,
|
||||
cursor: Option<CCursorRange>,
|
||||
) -> LayoutJob {
|
||||
if (&self.style, self.code.as_str()) != (egui_style, code) {
|
||||
self.style = egui_style.clone();
|
||||
code.clone_into(&mut self.code);
|
||||
self.output = highlight_markdown(egui_style, code, cursor);
|
||||
}
|
||||
self.output.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highlight_markdown(
|
||||
egui_style: &egui::Style,
|
||||
text: &str,
|
||||
|
||||
// TODO: hide special characters where cursor isn't
|
||||
_cursor: Option<CCursorRange>,
|
||||
) -> LayoutJob {
|
||||
let mut job = LayoutJob::default();
|
||||
let mut style = Style::default();
|
||||
|
||||
let mut prev = TokenKind::Newline;
|
||||
|
||||
let tokens: Vec<_> = tokenize(text).collect();
|
||||
let mut tokens = &tokens[..];
|
||||
|
||||
const CODE_INDENT: f32 = 10.0;
|
||||
|
||||
while !tokens.is_empty() {
|
||||
let token = tokens.first().unwrap();
|
||||
tokens = &tokens[1..];
|
||||
|
||||
let start_of_line = prev == TokenKind::Newline;
|
||||
prev = token.kind;
|
||||
|
||||
let mut basic_style: Option<fn(&mut Style) -> &mut bool> = None;
|
||||
|
||||
match token.kind {
|
||||
TokenKind::CodeBlock if start_of_line => {
|
||||
let span = collect_until(
|
||||
token,
|
||||
&mut tokens,
|
||||
series([TokenKind::Newline, TokenKind::CodeBlock]),
|
||||
);
|
||||
|
||||
let code_style = format_from_style(
|
||||
egui_style,
|
||||
&Style {
|
||||
code: true,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
job.append(&*span, CODE_INDENT, code_style.clone());
|
||||
style = Default::default();
|
||||
continue;
|
||||
}
|
||||
|
||||
TokenKind::Newline => style = Style::default(),
|
||||
|
||||
TokenKind::Strong => basic_style = Some(|s| &mut s.strong),
|
||||
TokenKind::Italic => basic_style = Some(|s| &mut s.italics),
|
||||
TokenKind::Strikethrough => basic_style = Some(|s| &mut s.strikethrough),
|
||||
|
||||
TokenKind::CodeBlock | TokenKind::Mono => {
|
||||
style.code = true;
|
||||
let span = collect_until(
|
||||
token,
|
||||
&mut tokens,
|
||||
any_of([TokenKind::Mono, TokenKind::CodeBlock, TokenKind::Newline]),
|
||||
);
|
||||
job.append(&*span, 0.0, format_from_style(egui_style, &style));
|
||||
style.code = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: different heading strengths
|
||||
TokenKind::Heading(h) if start_of_line => style.heading = Some(h),
|
||||
TokenKind::Quote if start_of_line => style.quoted = true,
|
||||
|
||||
// TODO: indented list entries
|
||||
TokenKind::ListEntry if start_of_line => {
|
||||
job.append("• ", 0.0, format_from_style(egui_style, &style));
|
||||
continue;
|
||||
}
|
||||
|
||||
TokenKind::Text
|
||||
// the following tokens are only richly rendered if encountered e.g. at start_of_line.
|
||||
| TokenKind::Indentation
|
||||
| TokenKind::ListEntry
|
||||
| TokenKind::Heading(..)
|
||||
| TokenKind::Quote => {}
|
||||
}
|
||||
|
||||
// if we encountered a marker for Bold, Italic, or Strikethrough, toggle that style and
|
||||
// render the token with the style enabled.
|
||||
if let Some(basic_style) = basic_style {
|
||||
let mut tmp_style = style;
|
||||
*basic_style(&mut tmp_style) = true;
|
||||
*basic_style(&mut style) ^= true; // toggle
|
||||
job.append(&token.span, 0.0, format_from_style(egui_style, &tmp_style));
|
||||
continue;
|
||||
}
|
||||
|
||||
job.append(&token.span, 0.0, format_from_style(egui_style, &style));
|
||||
}
|
||||
|
||||
job
|
||||
}
|
||||
|
||||
fn series<'a, const N: usize>(of: [TokenKind; N]) -> impl FnMut(&[Token<'a>; N]) -> bool {
|
||||
move |token| {
|
||||
of.iter()
|
||||
.zip(token)
|
||||
.all(|(kind, token)| kind == &token.kind)
|
||||
}
|
||||
}
|
||||
|
||||
fn any_of<'a, const N: usize>(these: [TokenKind; N]) -> impl FnMut(&[Token<'a>; 1]) -> bool {
|
||||
move |[token]| these.contains(&token.kind)
|
||||
}
|
||||
|
||||
/// Collect all tokens up to and including `pattern`, and merge them into a signle span.
|
||||
///
|
||||
/// `N` determines how many specific and consecutive tokens we are looking for.
|
||||
/// i.e. if we were looking for a [TokenKind::Newline] followed by a [TokenKind::Quote], `N`
|
||||
/// would equal `2`.
|
||||
///
|
||||
/// `pattern` is a function that accepts an array of `N` tokens and returns `true` if they match,
|
||||
/// i.e. if we should stop collecting. [any_of] and [series] can help to construct this function.
|
||||
///
|
||||
/// The collected tokens will be split off the head of the slice referred to by `tokens`.
|
||||
///
|
||||
/// # Panic
|
||||
/// Panics if `tokens` does not contain only consecutive adjacent spans.
|
||||
fn collect_until<'a, const N: usize>(
|
||||
token: &Token<'a>,
|
||||
tokens: &mut &[Token<'a>],
|
||||
pattern: impl FnMut(&[Token<'a>; N]) -> bool,
|
||||
) -> Span<'a>
|
||||
where
|
||||
for<'b> &'b [Token<'a>; N]: TryFrom<&'b [Token<'a>]>,
|
||||
{
|
||||
let mut windows = tokens
|
||||
.windows(N)
|
||||
.map(|slice| <&[Token<'a>; N]>::try_from(slice).ok().unwrap());
|
||||
|
||||
let split_at = match windows.position(pattern) {
|
||||
Some(i) => i + N,
|
||||
None => tokens.len(), // consume everything
|
||||
};
|
||||
|
||||
let (consume, keep) = tokens.split_at(split_at);
|
||||
*tokens = keep;
|
||||
|
||||
consume
|
||||
.iter()
|
||||
.fold(token.span.clone(), |span: Span<'_>, token| {
|
||||
span.try_merge(&token.span).unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
fn format_from_style(egui_style: &egui::Style, emark_style: &Style) -> egui::text::TextFormat {
|
||||
use egui::{Align, Color32, Stroke, TextStyle};
|
||||
|
||||
let color = if emark_style.strong || emark_style.heading.is_some() {
|
||||
egui_style.visuals.strong_text_color()
|
||||
} else if emark_style.quoted {
|
||||
egui_style.visuals.weak_text_color()
|
||||
} else {
|
||||
egui_style.visuals.text_color()
|
||||
};
|
||||
|
||||
let text_style = if emark_style.heading.is_some() {
|
||||
TextStyle::Heading
|
||||
} else if emark_style.code {
|
||||
TextStyle::Monospace
|
||||
} else if emark_style.small | emark_style.raised {
|
||||
TextStyle::Small
|
||||
} else {
|
||||
TextStyle::Body
|
||||
};
|
||||
|
||||
let background = if emark_style.code {
|
||||
egui_style.visuals.code_bg_color
|
||||
} else {
|
||||
Color32::TRANSPARENT
|
||||
};
|
||||
|
||||
let underline = if emark_style.underline {
|
||||
Stroke::new(1.0, color)
|
||||
} else {
|
||||
Stroke::NONE
|
||||
};
|
||||
|
||||
let strikethrough = if emark_style.strikethrough {
|
||||
Stroke::new(1.0, color)
|
||||
} else {
|
||||
Stroke::NONE
|
||||
};
|
||||
|
||||
let valign = if emark_style.raised {
|
||||
Align::TOP
|
||||
} else {
|
||||
Align::BOTTOM
|
||||
};
|
||||
|
||||
egui::text::TextFormat {
|
||||
font_id: text_style.resolve(egui_style),
|
||||
color,
|
||||
background,
|
||||
italics: emark_style.italics,
|
||||
underline,
|
||||
strikethrough,
|
||||
valign,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
7
src/markdown/mod.rs
Normal file
7
src/markdown/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
mod highlighter;
|
||||
mod span;
|
||||
mod tokenizer;
|
||||
|
||||
pub use highlighter::*;
|
||||
pub use span::*;
|
||||
pub use tokenizer::*;
|
||||
@ -0,0 +1,46 @@
|
||||
---
|
||||
source: src/markdown/tokenizer.rs
|
||||
expression: examples
|
||||
---
|
||||
- string: "just some normal text :D"
|
||||
tokens:
|
||||
- "Token { span: Span(0..24, \"just some normal text :D\"), kind: Text }"
|
||||
- string: normal *bold* normal
|
||||
tokens:
|
||||
- "Token { span: Span(0..7, \"normal \"), kind: Text }"
|
||||
- "Token { span: Span(7..8, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(8..12, \"bold\"), kind: Text }"
|
||||
- "Token { span: Span(12..13, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(13..20, \" normal\"), kind: Text }"
|
||||
- string: normal * maybe bold? * normal
|
||||
tokens:
|
||||
- "Token { span: Span(0..7, \"normal \"), kind: Text }"
|
||||
- "Token { span: Span(7..8, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(8..21, \" maybe bold? \"), kind: Text }"
|
||||
- "Token { span: Span(21..22, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(22..29, \" normal\"), kind: Text }"
|
||||
- string: "```lang\ncode code code\n```"
|
||||
tokens:
|
||||
- "Token { span: Span(0..3, \"```\"), kind: CodeBlock }"
|
||||
- "Token { span: Span(3..7, \"lang\"), kind: Text }"
|
||||
- "Token { span: Span(7..8, \"\\n\"), kind: Newline }"
|
||||
- "Token { span: Span(8..22, \"code code code\"), kind: Text }"
|
||||
- "Token { span: Span(22..23, \"\\n\"), kind: Newline }"
|
||||
- "Token { span: Span(23..26, \"```\"), kind: CodeBlock }"
|
||||
- string: "*/``/*"
|
||||
tokens:
|
||||
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(1..2, \"/\"), kind: Italic }"
|
||||
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
||||
- "Token { span: Span(3..4, \"`\"), kind: Mono }"
|
||||
- "Token { span: Span(4..5, \"/\"), kind: Italic }"
|
||||
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
||||
- string: "*/`*/*/"
|
||||
tokens:
|
||||
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(1..2, \"/\"), kind: Italic }"
|
||||
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
||||
- "Token { span: Span(3..4, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(4..5, \"/\"), kind: Italic }"
|
||||
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
||||
- "Token { span: Span(6..7, \"/\"), kind: Italic }"
|
||||
83
src/markdown/span.rs
Normal file
83
src/markdown/span.rs
Normal file
@ -0,0 +1,83 @@
|
||||
use std::{
|
||||
fmt,
|
||||
ops::{Deref, Range},
|
||||
};
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Span<'a> {
|
||||
complete_str: &'a str,
|
||||
range: Range<usize>,
|
||||
}
|
||||
|
||||
impl<'a> Span<'a> {
|
||||
pub fn new(complete_str: &'a str) -> Self {
|
||||
Self {
|
||||
complete_str,
|
||||
range: 0..complete_str.len(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, slice: Range<usize>) -> Option<Self> {
|
||||
let start = self.range.start.checked_add(slice.start)?;
|
||||
let end = self.range.start.checked_add(slice.end)?;
|
||||
|
||||
if end > self.range.end || end < start {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self {
|
||||
complete_str: self.complete_str,
|
||||
range: Range { start, end },
|
||||
})
|
||||
}
|
||||
|
||||
pub fn complete_str(&self) -> Self {
|
||||
Self::new(self.complete_str)
|
||||
}
|
||||
|
||||
pub fn split_at(&self, i: usize) -> Option<(Self, Self)> {
|
||||
let head = self.get(0..i)?;
|
||||
let tail = self.get(i..self.range.len())?;
|
||||
Some((head, tail))
|
||||
}
|
||||
|
||||
/// Try to merge the spans.
|
||||
///
|
||||
/// This only works if spans are pointing into the same backing buffer, and are adjacent.
|
||||
pub fn try_merge(&self, other: &Self) -> Option<Self> {
|
||||
if self.complete_str.as_ptr() != other.complete_str.as_ptr() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if self.range.end == other.range.start {
|
||||
Some(Self {
|
||||
range: self.range.start..other.range.end,
|
||||
..*self
|
||||
})
|
||||
} else if self.range.start == other.range.end {
|
||||
Some(Self {
|
||||
range: other.range.start..self.range.end,
|
||||
..*self
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Span<'_> {
|
||||
type Target = str;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.complete_str[self.range.clone()]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Debug for Span<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_tuple("Span")
|
||||
.field(&self.range)
|
||||
.field(&self.deref())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
156
src/markdown/tokenizer.rs
Normal file
156
src/markdown/tokenizer.rs
Normal file
@ -0,0 +1,156 @@
|
||||
use std::iter;
|
||||
|
||||
use super::span::Span;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Heading {
|
||||
H6,
|
||||
H5,
|
||||
H4,
|
||||
H3,
|
||||
H2,
|
||||
H1,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TokenKind {
|
||||
/// A newline that isn't a codeblock
|
||||
Newline,
|
||||
|
||||
/// "#" to "######"
|
||||
Heading(Heading),
|
||||
|
||||
/// A newline followed by three `
|
||||
CodeBlock,
|
||||
|
||||
Mono,
|
||||
Strong,
|
||||
Italic,
|
||||
Strikethrough,
|
||||
|
||||
/// ">"
|
||||
Quote,
|
||||
|
||||
/// Two spaces
|
||||
Indentation,
|
||||
|
||||
/// "- "
|
||||
ListEntry,
|
||||
|
||||
/// Normal text
|
||||
Text,
|
||||
}
|
||||
|
||||
const TOKENS: &[(&'static str, TokenKind)] = &[
|
||||
("\n", TokenKind::Newline),
|
||||
("######", TokenKind::Heading(Heading::H6)),
|
||||
("#####", TokenKind::Heading(Heading::H5)),
|
||||
("####", TokenKind::Heading(Heading::H4)),
|
||||
("###", TokenKind::Heading(Heading::H3)),
|
||||
("##", TokenKind::Heading(Heading::H2)),
|
||||
("#", TokenKind::Heading(Heading::H1)),
|
||||
("```", TokenKind::CodeBlock),
|
||||
("`", TokenKind::Mono),
|
||||
("*", TokenKind::Strong),
|
||||
("_", TokenKind::Italic),
|
||||
("~", TokenKind::Strikethrough),
|
||||
(">", TokenKind::Quote),
|
||||
(" ", TokenKind::Indentation),
|
||||
("- ", TokenKind::ListEntry),
|
||||
];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Token<'a> {
|
||||
pub span: Span<'a>,
|
||||
pub kind: TokenKind,
|
||||
}
|
||||
|
||||
pub fn tokenize<'a>(s: &'a str) -> impl Iterator<Item = Token<'a>> {
|
||||
let mut s = Span::new(s);
|
||||
let mut yield_n: usize = 0;
|
||||
|
||||
iter::from_fn(move || {
|
||||
loop {
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if yield_n == s.len() {
|
||||
let (token, rest) = s.split_at(s.len()).unwrap();
|
||||
let token = Token {
|
||||
span: token,
|
||||
kind: TokenKind::Text,
|
||||
};
|
||||
s = rest;
|
||||
return Some(token);
|
||||
}
|
||||
|
||||
let token = TOKENS.iter().find_map(|(token_str, token_kind)| {
|
||||
s[yield_n..]
|
||||
.starts_with(token_str)
|
||||
.then(|| (*token_kind, token_str.len()))
|
||||
});
|
||||
|
||||
let Some((kind, len)) = token else {
|
||||
yield_n += s[yield_n..].chars().next().unwrap_or('\0').len_utf8();
|
||||
continue;
|
||||
};
|
||||
|
||||
if yield_n > 0 {
|
||||
let (token, rest) = s.split_at(yield_n).unwrap();
|
||||
let token = Token {
|
||||
span: token,
|
||||
kind: TokenKind::Text,
|
||||
};
|
||||
s = rest;
|
||||
yield_n = 0;
|
||||
return Some(token);
|
||||
}
|
||||
|
||||
let (token, rest) = s.split_at(len).unwrap();
|
||||
let token = Token { span: token, kind };
|
||||
s = rest;
|
||||
return Some(token);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde::Serialize;
|
||||
|
||||
use super::tokenize;
|
||||
|
||||
#[test]
|
||||
fn test_tokenize() {
|
||||
let examples = [
|
||||
"just some normal text :D",
|
||||
"normal *bold* normal",
|
||||
"normal * maybe bold? * normal",
|
||||
"```lang\ncode code code\n```",
|
||||
"*/``/*",
|
||||
"*/`*/*/",
|
||||
];
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Result {
|
||||
pub string: &'static str,
|
||||
|
||||
/// Debug-printed tokens
|
||||
pub tokens: Vec<String>,
|
||||
}
|
||||
|
||||
let examples = examples
|
||||
.into_iter()
|
||||
.map(|string| {
|
||||
let tokens = tokenize(string)
|
||||
.map(|tokens| format!("{tokens:?}"))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Result { string, tokens }
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
insta::assert_yaml_snapshot!(examples);
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,6 @@ use std::{
|
||||
};
|
||||
|
||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||
use disk_format::{DiskFormat, RawStroke, RawStrokeHeader, f16_le};
|
||||
use egui::{
|
||||
Color32, ColorImage, CornerRadius, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense,
|
||||
Shape, Stroke, TextureHandle, Theme, Ui, Vec2,
|
||||
@ -17,7 +16,7 @@ use egui::{
|
||||
use eyre::{Context, bail};
|
||||
use eyre::{OptionExt, eyre};
|
||||
use half::f16;
|
||||
use zerocopy::{FromBytes, IntoBytes};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::{
|
||||
custom_code_block::try_from_custom_code_block,
|
||||
@ -25,8 +24,6 @@ use crate::{
|
||||
};
|
||||
use crate::{custom_code_block::write_custom_code_block, util::random_id};
|
||||
|
||||
mod disk_format;
|
||||
|
||||
const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
|
||||
const HANDWRITING_BOTTOM_PADDING: f32 = 80.0;
|
||||
const HANDWRITING_MARGIN: f32 = 0.05;
|
||||
@ -97,10 +94,6 @@ pub struct Handwriting {
|
||||
last_mesh_ctx: Option<MeshContext>,
|
||||
}
|
||||
|
||||
pub struct HandwritingResponse {
|
||||
pub changed: bool,
|
||||
}
|
||||
|
||||
/// Context of a mesh render.
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
struct MeshContext {
|
||||
@ -171,7 +164,6 @@ impl Handwriting {
|
||||
&mut self,
|
||||
style: Option<&mut HandwritingStyle>,
|
||||
ui: &mut egui::Ui,
|
||||
response: &mut HandwritingResponse,
|
||||
) -> egui::Response {
|
||||
ui.horizontal(|ui| {
|
||||
if let Some(style) = style {
|
||||
@ -183,14 +175,12 @@ impl Handwriting {
|
||||
if ui.button("Clear Painting").clicked() {
|
||||
self.strokes.clear();
|
||||
self.refresh_texture = true;
|
||||
response.changed = true;
|
||||
}
|
||||
|
||||
ui.add_enabled_ui(!self.strokes.is_empty(), |ui| {
|
||||
if ui.button("Undo").clicked() {
|
||||
self.strokes.pop();
|
||||
self.refresh_texture = true;
|
||||
response.changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
@ -200,18 +190,12 @@ impl Handwriting {
|
||||
.response
|
||||
}
|
||||
|
||||
fn commit_current_line(&mut self, response: &mut HandwritingResponse) {
|
||||
fn commit_current_line(&mut self) {
|
||||
debug_assert!(!self.current_stroke.is_empty());
|
||||
self.strokes.push(mem::take(&mut self.current_stroke));
|
||||
response.changed = true;
|
||||
}
|
||||
|
||||
pub fn ui_content(
|
||||
&mut self,
|
||||
style: &HandwritingStyle,
|
||||
ui: &mut Ui,
|
||||
hw_response: &mut HandwritingResponse,
|
||||
) -> egui::Response {
|
||||
pub fn ui_content(&mut self, style: &HandwritingStyle, ui: &mut Ui) -> egui::Response {
|
||||
if style.animate {
|
||||
self.height = ui.ctx().animate_value_with_time(
|
||||
self.id.with("height animation"),
|
||||
@ -222,8 +206,8 @@ impl Handwriting {
|
||||
self.height = self.desired_height;
|
||||
}
|
||||
|
||||
let desired_size = Vec2::new(ui.available_width(), self.height);
|
||||
let (response, painter) = ui.allocate_painter(desired_size, Sense::drag());
|
||||
let size = Vec2::new(ui.available_width(), self.height);
|
||||
let (response, painter) = ui.allocate_painter(size, Sense::drag());
|
||||
|
||||
let mut response = response
|
||||
//.on_hover_cursor(CursorIcon::Crosshair)
|
||||
@ -232,24 +216,20 @@ impl Handwriting {
|
||||
|
||||
let size = response.rect.size();
|
||||
|
||||
// Calculate matrices that convert between screen-space and image-space.
|
||||
// - image-space: 0,0 is the top-left of the texture.
|
||||
// - screen-space: 0,0 is the top-left of the window.
|
||||
// Both spaces use the same logical points, not pixels.
|
||||
let to_screen =
|
||||
emath::RectTransform::from_to(Rect::from_min_size(Pos2::ZERO, size), response.rect);
|
||||
let to_screen = emath::RectTransform::from_to(
|
||||
//Rect::from_min_size(Pos2::ZERO, response.rect.square_proportions()),
|
||||
Rect::from_min_size(Pos2::ZERO, size),
|
||||
response.rect,
|
||||
);
|
||||
let from_screen = to_screen.inverse();
|
||||
|
||||
// Was the user in the process of drawing a stroke last frame?
|
||||
let is_drawing = response.interact_pointer_pos().is_some();
|
||||
let was_drawing = !self.current_stroke.is_empty();
|
||||
|
||||
// Is the user in the process of drawing a stroke now?
|
||||
let is_drawing = response.interact_pointer_pos().is_some();
|
||||
|
||||
if !is_drawing {
|
||||
if was_drawing {
|
||||
// commit current line
|
||||
self.commit_current_line(hw_response);
|
||||
if was_drawing {
|
||||
self.commit_current_line();
|
||||
response.mark_changed();
|
||||
}
|
||||
|
||||
@ -261,8 +241,6 @@ impl Handwriting {
|
||||
.map(|p| p.y + HANDWRITING_BOTTOM_PADDING)
|
||||
.fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y));
|
||||
|
||||
// Change the height of the handwriting item.
|
||||
// We don't do this mid-stroke, only when the user e.g. lifts the pen.
|
||||
if self.desired_height != lines_max_y {
|
||||
self.desired_height = lines_max_y;
|
||||
response.mark_changed();
|
||||
@ -297,7 +275,6 @@ impl Handwriting {
|
||||
.collect::<Vec<_>>()
|
||||
});
|
||||
|
||||
// Process input events and turn them into strokes
|
||||
for event in events {
|
||||
let last_canvas_pos = self.current_stroke.last();
|
||||
|
||||
@ -347,7 +324,7 @@ impl Handwriting {
|
||||
(PointerButton::Primary, false) => {
|
||||
if last_canvas_pos.is_some() {
|
||||
self.push_to_stroke(from_screen * pos);
|
||||
self.commit_current_line(hw_response);
|
||||
self.commit_current_line();
|
||||
response.mark_changed();
|
||||
}
|
||||
|
||||
@ -364,7 +341,7 @@ impl Handwriting {
|
||||
// in the same frame. Should handle this.
|
||||
Event::PointerGone | Event::WindowFocused(false) => {
|
||||
if !self.current_stroke.is_empty() {
|
||||
self.commit_current_line(hw_response);
|
||||
self.commit_current_line();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -384,7 +361,6 @@ impl Handwriting {
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the horizontal ruled lines
|
||||
(1..)
|
||||
.map(|n| n as f32 * HANDWRITING_LINE_SPACING)
|
||||
.take_while(|&y| y < size.y)
|
||||
@ -397,12 +373,9 @@ impl Handwriting {
|
||||
painter.add(shape);
|
||||
});
|
||||
|
||||
// Get the dimensions of the image
|
||||
let mesh_rect = response
|
||||
.rect
|
||||
.with_max_y(response.rect.min.y + self.desired_height);
|
||||
|
||||
// These are the values that, if changed, would require the mesh to be re-rendered.
|
||||
let new_context = MeshContext {
|
||||
ui_theme: ui.ctx().theme(),
|
||||
pixels_per_point: ui.pixels_per_point(),
|
||||
@ -410,25 +383,24 @@ impl Handwriting {
|
||||
stroke: style.stroke,
|
||||
};
|
||||
|
||||
// Figure out if we need to re-rasterize the mesh.
|
||||
if Some(&new_context) != self.last_mesh_ctx.as_ref() {
|
||||
self.refresh_texture = true;
|
||||
}
|
||||
|
||||
if self.refresh_texture {
|
||||
// ...if we do, rasterize the entire texture from scratch
|
||||
// rasterize the entire texture from scratch
|
||||
self.refresh_texture(style, new_context, ui);
|
||||
self.unblitted_lines.clear();
|
||||
} else if !self.unblitted_lines.is_empty() {
|
||||
// ...if we don't, we can get away with only rasterizing the *new* lines onto the
|
||||
// existing texture.
|
||||
// only rasterize the new lines onto the existing texture
|
||||
for [from, to] in std::mem::take(&mut self.unblitted_lines) {
|
||||
self.draw_line_to_texture(from, to, &new_context, ui);
|
||||
}
|
||||
self.unblitted_lines.clear();
|
||||
}
|
||||
|
||||
// Draw the texture
|
||||
//painter.add(self.mesh.clone());
|
||||
|
||||
if let Some(texture) = &self.texture {
|
||||
let texture = SizedTexture::new(texture.id(), texture.size_vec2());
|
||||
let shape = RectShape {
|
||||
@ -488,13 +460,6 @@ impl Handwriting {
|
||||
tesselator.tessellate_shape(shape, mesh);
|
||||
});
|
||||
|
||||
// sanity-check that tesselation did not produce any NaNs.
|
||||
// this can happen if the line contains duplicated consecutive positions
|
||||
//for vertex in &mesh.vertices {
|
||||
// debug_assert!(vertex.pos.x.is_finite(), "{} must be finite", vertex.pos.x);
|
||||
// debug_assert!(vertex.pos.y.is_finite(), "{} must be finite", vertex.pos.y);
|
||||
//}
|
||||
|
||||
let texture = texture!(self, ui, &mesh_context);
|
||||
let triangles = mesh_triangles(&self.mesh);
|
||||
|
||||
@ -510,11 +475,9 @@ impl Handwriting {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, style: &HandwritingStyle, ui: &mut Ui) -> HandwritingResponse {
|
||||
let mut response = HandwritingResponse { changed: false };
|
||||
|
||||
pub fn ui(&mut self, style: &HandwritingStyle, ui: &mut Ui) {
|
||||
ui.vertical_centered_justified(|ui| {
|
||||
self.ui_control(None, ui, &mut response);
|
||||
self.ui_control(None, ui);
|
||||
|
||||
//ui.label("Paint with your mouse/touch!");
|
||||
Frame::canvas(ui.style())
|
||||
@ -522,11 +485,9 @@ impl Handwriting {
|
||||
.stroke(Stroke::new(5.0, Color32::from_black_alpha(40)))
|
||||
.fill(style.bg_color)
|
||||
.show(ui, |ui| {
|
||||
self.ui_content(style, ui, &mut response);
|
||||
self.ui_content(style, ui);
|
||||
});
|
||||
});
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
fn push_to_stroke(&mut self, new_canvas_pos: Pos2) {
|
||||
@ -596,40 +557,24 @@ impl Handwriting {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_as_disk_format(&self) -> Box<[u8]> {
|
||||
let mut bytes = vec![];
|
||||
let header = disk_format::Header {
|
||||
version: disk_format::V1,
|
||||
};
|
||||
|
||||
bytes.extend_from_slice(header.as_bytes());
|
||||
|
||||
for stroke in &self.strokes {
|
||||
let Ok(len) = u16::try_from(stroke.len()) else {
|
||||
log::error!("More than u16::MAX points in a stroke!");
|
||||
continue;
|
||||
};
|
||||
|
||||
let header = RawStrokeHeader { len: len.into() };
|
||||
bytes.extend_from_slice(header.as_bytes());
|
||||
|
||||
for position in stroke {
|
||||
for v in [position.x, position.y] {
|
||||
let v = f16::from_f32(v);
|
||||
let v = f16_le::from(v);
|
||||
bytes.extend_from_slice(v.as_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bytes.into_boxed_slice()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Handwriting {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let raw = self.encode_as_disk_format();
|
||||
let mut raw = vec![];
|
||||
|
||||
for stroke in &self.strokes {
|
||||
raw.push((stroke.len() as u16).to_le_bytes());
|
||||
for position in stroke {
|
||||
let x = half::f16::from_f32(position.x);
|
||||
let y = half::f16::from_f32(position.y);
|
||||
raw.push(x.to_bits().to_le_bytes());
|
||||
raw.push(y.to_bits().to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
let raw = raw.as_slice().as_bytes();
|
||||
|
||||
write_custom_code_block(f, CODE_BLOCK_KEY, BASE64_STANDARD.encode(raw))
|
||||
}
|
||||
}
|
||||
@ -645,71 +590,53 @@ impl FromStr for Handwriting {
|
||||
.decode(s)
|
||||
.wrap_err("Failed to decode painting data from base64")?;
|
||||
|
||||
// HACK: first iteration of disk format did not have version header
|
||||
//bytes.insert(0, 0);
|
||||
//bytes.insert(0, 1);
|
||||
#[allow(non_camel_case_types)]
|
||||
type u16_le = [u8; 2];
|
||||
|
||||
let disk_format = DiskFormat::ref_from_bytes(&bytes[..]).map_err(|_| eyre!("Too short"))?;
|
||||
#[allow(non_camel_case_types)]
|
||||
type f16_le = [u8; 2];
|
||||
|
||||
if disk_format.header.version != disk_format::V1 {
|
||||
bail!(
|
||||
"Unknown disk_format version: {}",
|
||||
disk_format.header.version
|
||||
);
|
||||
#[derive(FromBytes, KnownLayout, Immutable)]
|
||||
#[repr(C, packed)]
|
||||
struct Stroke {
|
||||
pub len: u16_le,
|
||||
pub positions: [f16_le],
|
||||
}
|
||||
|
||||
let mut raw_strokes = &disk_format.strokes[..];
|
||||
let mut bytes = &bytes[..];
|
||||
let mut strokes = vec![];
|
||||
|
||||
while !raw_strokes.is_empty() {
|
||||
if raw_strokes.len() < RawStroke::MIN_LEN {
|
||||
bail!("Invalid remaining length: {}", raw_strokes.len());
|
||||
while !bytes.is_empty() {
|
||||
let header_len = size_of::<u16_le>();
|
||||
if bytes.len() < header_len {
|
||||
bail!("Invalid remaining length: {}", bytes.len());
|
||||
}
|
||||
|
||||
let stroke = RawStroke::ref_from_bytes(&raw_strokes[..RawStroke::MIN_LEN])
|
||||
.expect("length is correct");
|
||||
let stroke = Stroke::ref_from_bytes(&bytes[..header_len]).expect("length is correct");
|
||||
let len = usize::from(u16::from_le_bytes(stroke.len));
|
||||
let len = len * size_of::<f16_le>() * 2;
|
||||
|
||||
// get length as number of points
|
||||
let len = usize::from(u16::from(stroke.header.len));
|
||||
|
||||
// convert to length in bytes
|
||||
let byte_len = 2 * size_of::<f16_le>() * len;
|
||||
|
||||
if raw_strokes.len() < byte_len {
|
||||
bail!("Invalid remaining length: {}", raw_strokes.len());
|
||||
if bytes.len() < len {
|
||||
bail!("Invalid remaining length: {}", bytes.len());
|
||||
}
|
||||
|
||||
let (stroke, rest) = raw_strokes.split_at(RawStroke::MIN_LEN + byte_len);
|
||||
raw_strokes = rest;
|
||||
let (stroke, rest) = bytes.split_at(header_len + len);
|
||||
bytes = rest;
|
||||
let stroke = Stroke::ref_from_bytes(stroke)
|
||||
.map_err(|e| eyre!("Failed to decode stroke bytes: {e}"))?;
|
||||
|
||||
let stroke = RawStroke::ref_from_bytes(stroke).expect("length is correct");
|
||||
|
||||
debug_assert_eq!(
|
||||
stroke.positions.len().rem_euclid(2),
|
||||
0,
|
||||
"{} must be divisible by 2",
|
||||
stroke.positions.len()
|
||||
);
|
||||
debug_assert_eq!(stroke.positions.len(), len * 2);
|
||||
|
||||
let mut last_pos = Pos2::new(f32::NEG_INFINITY, f32::INFINITY);
|
||||
|
||||
// positions are encoded as an array of f16s [x, y, x, y, x, y, ..]
|
||||
let stroke: Vec<Pos2> = stroke
|
||||
let mut positions = stroke
|
||||
.positions
|
||||
.chunks_exact(2)
|
||||
.map(|chunk| [chunk[0], chunk[1]])
|
||||
.map(|pos| pos.map(f16::from)) // interpret bytes as f16
|
||||
.map(|pos| pos.map(f32::from)) // widen to f32
|
||||
.filter(|pos| pos.iter().all(|f| f.is_finite())) // filter out NaNs and Infs
|
||||
.map(|[x, y]| Pos2::new(x, y))
|
||||
.filter(|pos| {
|
||||
let is_duplicate = pos == &last_pos;
|
||||
last_pos = *pos;
|
||||
!is_duplicate // skip duplicates
|
||||
})
|
||||
.collect();
|
||||
.iter()
|
||||
.map(|&position| f16::from_bits(u16::from_le_bytes(position)));
|
||||
|
||||
let mut stroke = vec![];
|
||||
while let Some(x) = positions.next() {
|
||||
let Some(y) = positions.next() else {
|
||||
unreachable!("len is a multiple of two");
|
||||
};
|
||||
stroke.push(Pos2::new(x.into(), y.into()));
|
||||
}
|
||||
strokes.push(stroke);
|
||||
}
|
||||
|
||||
@ -72,11 +72,15 @@ pub fn rasterize_onto<'a, Blend: BlendFn>(
|
||||
|
||||
// If the pixel is within the triangle, fill it in.
|
||||
if point_in_triangle.inside {
|
||||
let [c0, c1, c2] = [0, 1, 2].map(|i| {
|
||||
triangle[i]
|
||||
let c0 = triangle[0]
|
||||
.color
|
||||
.linear_multiply(point_in_triangle.weights[i])
|
||||
});
|
||||
.linear_multiply(point_in_triangle.weights[0]);
|
||||
let c1 = triangle[1]
|
||||
.color
|
||||
.linear_multiply(point_in_triangle.weights[1]);
|
||||
let c2 = triangle[2]
|
||||
.color
|
||||
.linear_multiply(point_in_triangle.weights[2]);
|
||||
|
||||
let color = c0 + c1 + c2;
|
||||
|
||||
@ -165,12 +169,6 @@ fn point_in_triangle(point: Pos2, triangle: [&Vertex; 3]) -> PointInTriangle {
|
||||
// Normalize the weights.
|
||||
let weights = areas.map(|area| area / triangle_area);
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
if weights.into_iter().any(f32::is_nan) {
|
||||
panic!("weights must not be NaN! {weights:?} {triangle_area:?} {areas:?} {sides:?}");
|
||||
}
|
||||
}
|
||||
|
||||
PointInTriangle { inside, weights }
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,44 @@
|
||||
---
|
||||
source: src/custom_code_block.rs
|
||||
assertion_line: 133
|
||||
expression: list
|
||||
---
|
||||
[
|
||||
Line(
|
||||
"\n",
|
||||
),
|
||||
Line(
|
||||
"# Hello world\n",
|
||||
),
|
||||
Line(
|
||||
"## Subheader\n",
|
||||
),
|
||||
Line(
|
||||
"- 1\n",
|
||||
),
|
||||
CodeBlock {
|
||||
key: "foo",
|
||||
content: " whatever\n some code\n Hi mom!",
|
||||
span: "```foo\n whatever\n some code\n Hi mom!\n```",
|
||||
},
|
||||
Line(
|
||||
" \n",
|
||||
),
|
||||
Line(
|
||||
"\n",
|
||||
),
|
||||
CodeBlock {
|
||||
key: "` # wrong number of ticks, but that's ok",
|
||||
content: " ``` # indented ticks",
|
||||
span: "```` # wrong number of ticks, but that's ok\n ``` # indented ticks\n```\n",
|
||||
},
|
||||
Line(
|
||||
"\n",
|
||||
),
|
||||
Line(
|
||||
"``` # no closing ticks\n",
|
||||
),
|
||||
Line(
|
||||
" ",
|
||||
),
|
||||
]
|
||||
@ -0,0 +1,8 @@
|
||||
---
|
||||
source: src/painting.rs
|
||||
assertion_line: 695
|
||||
expression: serialized
|
||||
---
|
||||
```handwriting
|
||||
BQAAvAA8AEIAPABCAEIAPgBAAAAAAAQAAEIAQgC8ADwAAAAAAEIAPA==
|
||||
```
|
||||
@ -8,7 +8,7 @@ use egui::{
|
||||
Color32, InputState, Key, Modifiers, TextBuffer, TextEdit, Ui, Vec2, text::CCursorRange,
|
||||
};
|
||||
|
||||
use crate::easy_mark::MemoizedHighlighter;
|
||||
use crate::markdown::MemoizedHighlighter;
|
||||
|
||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
||||
pub struct MdTextEdit {
|
||||
@ -24,10 +24,6 @@ pub struct MdTextEdit {
|
||||
cursor: Option<CCursorRange>,
|
||||
}
|
||||
|
||||
pub struct MdTextEditOutput {
|
||||
pub changed: bool,
|
||||
}
|
||||
|
||||
impl MdTextEdit {
|
||||
pub fn new() -> Self {
|
||||
MdTextEdit::default()
|
||||
@ -40,7 +36,7 @@ impl MdTextEdit {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut Ui) -> MdTextEditOutput {
|
||||
pub fn ui(&mut self, ui: &mut Ui) {
|
||||
let Self {
|
||||
text,
|
||||
highlighter,
|
||||
@ -50,8 +46,8 @@ impl MdTextEdit {
|
||||
|
||||
let w = ui.available_width();
|
||||
|
||||
let mut layouter = |ui: &egui::Ui, easymark: &dyn TextBuffer, _wrap_width: f32| {
|
||||
let mut layout_job = highlighter.highlight(ui.style(), easymark.as_str(), *cursor);
|
||||
let mut layouter = |ui: &egui::Ui, markdown: &dyn TextBuffer, _wrap_width: f32| {
|
||||
let mut layout_job = highlighter.highlight(ui.style(), markdown.as_str(), *cursor);
|
||||
layout_job.wrap.max_width = w - 10.0;
|
||||
ui.fonts(|f| f.layout_job(layout_job))
|
||||
};
|
||||
@ -76,10 +72,6 @@ impl MdTextEdit {
|
||||
*cursor = text_edit.cursor_range;
|
||||
//ui.ctx().request_repaint();
|
||||
}
|
||||
|
||||
MdTextEditOutput {
|
||||
changed: text_edit.response.changed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user