Skip to content

Commit 20175b0

Browse files
committed
update parser, small reorganisation
Moving some of the methods closer to the engine for pasting.
1 parent e3cdf52 commit 20175b0

File tree

7 files changed

+73
-52
lines changed

7 files changed

+73
-52
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ poppler-rs = { git = "https://github.com/Doublonmousse/poppler-patch", features
9191
"v20_9",
9292
], rev = "3acaa4e28d45a05939a71116b0d5ebd4b1c99b98" }
9393
# inkml parser/writer
94-
writer_inkml = {git = "https://github.com/Doublonmousse/writer_reader_inkml"}
94+
writer_inkml = {git = "https://github.com/Doublonmousse/writer_reader_inkml", rev = "9498471ca978be417fb104e015151c1f8ade2f6a"}
9595

9696
[patch.crates-io]
9797

crates/rnote-engine/src/engine/strokecontent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl StrokeContent {
175175
Ok(())
176176
}
177177

178-
pub fn to_inkml(&self, current_dpi: f64) -> Result<Vec<u8>, std::io::Error> {
178+
pub fn to_inkml(&self, current_dpi: f64) -> anyhow::Result<Vec<u8>> {
179179
writer_inkml::writer(
180180
self.strokes
181181
.iter()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::strokes::BrushStroke;
2+
use crate::strokes::Stroke;
3+
use rnote_compose::penpath::Element;
4+
use rnote_compose::style::smooth::SmoothOptions;
5+
use rnote_compose::style::PressureCurve;
6+
use rnote_compose::Color;
7+
use rnote_compose::PenPath;
8+
use std::sync::Arc;
9+
use writer_inkml::{Brush, FormattedStroke};
10+
11+
pub fn inkml_to_stroke(
12+
formatted_stroke: FormattedStroke,
13+
brush: Brush,
14+
dpi: &f64,
15+
) -> Option<Arc<Stroke>> {
16+
let mut smooth_options = SmoothOptions::default();
17+
smooth_options.stroke_color = Some(Color::new(
18+
brush.color.0 as f64 / 255.0,
19+
brush.color.1 as f64 / 255.0,
20+
brush.color.2 as f64 / 255.0,
21+
1.0 - brush.transparency as f64 / 255.0,
22+
));
23+
24+
// converting from mm to px
25+
smooth_options.stroke_width = dpi * brush.stroke_width / (10.0 * 2.54);
26+
27+
// pressure curve
28+
if brush.ignorepressure {
29+
smooth_options.pressure_curve = PressureCurve::Const;
30+
} else {
31+
smooth_options.pressure_curve = PressureCurve::Linear;
32+
}
33+
34+
let penpath = PenPath::try_from_elements(
35+
formatted_stroke
36+
.x
37+
.into_iter()
38+
.zip(formatted_stroke.y)
39+
.zip(formatted_stroke.f)
40+
.map(|((x, y), f)| Element::new(*dpi * na::vector![x, y] / 2.54, f)),
41+
);
42+
if penpath.is_some() {
43+
let new_stroke = BrushStroke::from_penpath(
44+
penpath.unwrap(),
45+
rnote_compose::Style::Smooth(smooth_options),
46+
);
47+
Some(Arc::new(Stroke::BrushStroke(new_stroke)))
48+
} else {
49+
None
50+
}
51+
}

crates/rnote-engine/src/fileformats/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Modules
2+
pub mod inkmlformat;
23
pub mod rnoteformat;
34
pub mod xoppformat;
45

crates/rnote-engine/src/pens/selector/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rnote_compose::penpath::Element;
2424
use rnote_compose::style::indicators;
2525
use rnote_compose::{Color, color};
2626
use std::time::Instant;
27-
use tracing::debug;
2827
use tracing::error;
2928

3029
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -195,13 +194,18 @@ impl PenBehaviour for Selector {
195194
let inkml_contents = stroke_content.to_inkml(dpi);
196195
match inkml_contents {
197196
Ok(inkml_bytes) => {
198-
println!("generated inkml : {:?}", str::from_utf8(&inkml_bytes));
197+
tracing::debug!(
198+
"generated inkml : {:?}",
199+
str::from_utf8(&inkml_bytes)
200+
);
199201
clipboard_content.push((
200202
inkml_bytes,
201203
"application/x.windows.InkML Format".to_string(),
202204
));
203205
}
204-
_ => (),
206+
Err(e) => error!(
207+
"Could not convert strokes to inkml to add to the clipboard, {e}"
208+
),
205209
}
206210

207211
if let Some(stroke_content_svg) = stroke_content_svg {

crates/rnote-ui/src/appwindow/actions.rs

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ use gtk4::{
88
prelude::*,
99
};
1010
use p2d::bounding_volume::BoundingVolume;
11-
use rnote_compose::penpath::Element;
12-
use rnote_compose::style::smooth::SmoothOptions;
13-
use rnote_compose::style::PressureCurve;
14-
use rnote_compose::Color;
15-
use rnote_compose::PenPath;
1611
use rnote_compose::SplitOrder;
1712
use rnote_compose::penevent::ShortcutKey;
1813
use rnote_engine::engine::StrokeContent;
14+
use rnote_engine::fileformats::inkmlformat;
1915
use rnote_engine::ext::GraphenePointExt;
20-
use rnote_engine::pens::PenStyle;
2116
use rnote_engine::strokes::resize::{ImageSizeOption, Resize};
22-
use rnote_engine::strokes::BrushStroke;
23-
use rnote_engine::strokes::Stroke;
2417
use rnote_engine::{Camera, Engine};
2518
use std::path::PathBuf;
26-
use std::sync::Arc;
2719
use std::time::Instant;
2820
use tracing::{debug, error};
2921

@@ -1219,46 +1211,19 @@ impl RnAppWindow {
12191211
match crate::utils::str_from_u8_nul_utf8(&acc) {
12201212
Ok(text) => {
12211213
let stroke_result = writer_inkml::parse_formatted(text.as_bytes());
1214+
let dpi = canvas.engine_ref().document.config.format.dpi();
1215+
12221216
debug!("stroke result {:?}", stroke_result);
12231217

12241218
if let Ok(strokes) = stroke_result {
1225-
let mut generated_strokes: Vec<Arc<Stroke>> = vec![];
1226-
for (formatted_stroke,brush) in strokes {
1227-
let mut smooth_options = SmoothOptions::default();
1228-
smooth_options.stroke_color = Some(Color::new(
1229-
brush.color.0 as f64 / 255.0,
1230-
brush.color.1 as f64 / 255.0,
1231-
brush.color.2 as f64 / 255.0,
1232-
1.0 - brush.transparency as f64 / 255.0
1233-
));
1234-
let dpi = canvas.engine_ref().document.config.format.dpi();
1235-
1236-
// converting from mm to px
1237-
smooth_options.stroke_width = dpi * brush.stroke_width/(10.0*2.54);
1238-
1239-
// pressure curve
1240-
if brush.ignorepressure {
1241-
smooth_options.pressure_curve = PressureCurve::Const;
1242-
} else {
1243-
smooth_options.pressure_curve = PressureCurve::Linear;
1244-
}
1245-
1246-
1247-
let penpath = PenPath::try_from_elements(
1248-
formatted_stroke.x.into_iter().zip(formatted_stroke.y).zip(formatted_stroke.f).map(|((x,y),f)| {
1249-
Element::new(
1250-
dpi*na::vector![x,y]/2.54,f
1251-
)
1252-
})
1253-
);
1254-
if penpath.is_some() {
1255-
let new_stroke = BrushStroke::from_penpath(penpath.unwrap(), rnote_compose::Style::Smooth(smooth_options));
1256-
generated_strokes.push(
1257-
Arc::new(Stroke::BrushStroke(new_stroke))
1258-
);
1259-
}
1260-
}
1261-
// then push
1219+
let generated_strokes = strokes.into_iter().map(|(formatted_stroke,brush)| {
1220+
inkmlformat::inkml_to_stroke(
1221+
formatted_stroke,brush, &dpi
1222+
)})
1223+
.filter(|x| x.is_some())
1224+
.map(|x| x.unwrap())
1225+
.collect();
1226+
12621227
let mut stroke_content = StrokeContent {
12631228
strokes: generated_strokes,
12641229
bounds:None,

0 commit comments

Comments
 (0)