Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inkml parser and writer #1320

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
808 changes: 531 additions & 277 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ usvg = "0.44.0"
winresource = "0.1.17"
xmlwriter = "0.1.0"
# Enabling feature > v20_9 causes linker errors on mingw
poppler-rs = { version = "0.24.1", features = ["v20_9"] }
poppler-rs = { git = "https://github.com/Doublonmousse/poppler-patch", features = ["v20_9"] }
# inkml parser/writer
writer_inkml = {git = "https://github.com/Doublonmousse/writer_reader_inkml"}


[patch.crates-io]

Expand Down
1 change: 1 addition & 0 deletions crates/rnote-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ usvg = { workspace = true }
xmlwriter = { workspace = true }
# the long-term plan is to remove the gtk4 dependency entirely after switching to another renderer.
gtk4 = { workspace = true, optional = true }
writer_inkml = {workspace = true}

[dev-dependencies]
approx = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/rnote-engine/src/engine/strokecontent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ impl StrokeContent {

Ok(())
}

pub fn to_inkml(&self, current_dpi: f64) -> Result<Vec<u8>, std::io::Error> {
writer_inkml::writer(
self.strokes
.iter()
.map(|stroke| stroke.into_inkml(current_dpi))
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect(),
)
}
}
18 changes: 18 additions & 0 deletions crates/rnote-engine/src/pens/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::snap::SnapCorner;
use crate::store::StrokeKey;
use crate::strokes::Content;
use crate::{Camera, DrawableOnDoc, Engine, WidgetFlags};
use core::str;
use futures::channel::oneshot;
use kurbo::Shape;
use p2d::bounding_volume::{Aabb, BoundingSphere, BoundingVolume};
Expand All @@ -24,6 +25,7 @@ use rnote_compose::EventResult;
use rnote_compose::{color, Color};
use std::collections::HashSet;
use std::time::Instant;
use tracing::debug;
use tracing::error;

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -171,6 +173,8 @@ impl PenBehaviour for Selector {
None
};

let dpi = engine_view.document.format.dpi();

rayon::spawn(move || {
let result = move || {
if let Some(stroke_content) = stroke_content {
Expand All @@ -186,6 +190,20 @@ impl PenBehaviour for Selector {
serde_json::to_string(&stroke_content)?.into_bytes(),
StrokeContent::MIME_TYPE.to_string(),
));

// add inkml content
let inkml_contents = stroke_content.to_inkml(dpi);
match inkml_contents {
Ok(inkml_bytes) => {
println!("generated inkml : {:?}", str::from_utf8(&inkml_bytes));
clipboard_content.push((
inkml_bytes,
"application/x.windows.InkML Format".to_string(),
));
}
_ => (),
}

if let Some(stroke_content_svg) = stroke_content_svg {
// Add generated Svg
clipboard_content.push((
Expand Down
60 changes: 60 additions & 0 deletions crates/rnote-engine/src/strokes/stroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rnote_compose::ext::AabbExt;
use rnote_compose::penpath::Element;
use rnote_compose::shapes::{Rectangle, Shapeable};
use rnote_compose::style::smooth::SmoothOptions;
use rnote_compose::style::PressureCurve;
use rnote_compose::transform::Transform;
use rnote_compose::transform::Transformable;
use rnote_compose::{Color, PenPath, Style};
Expand Down Expand Up @@ -672,4 +673,63 @@ impl Stroke {
}
}
}

/// converts a stroke into the input format used by the inkml writer
pub fn into_inkml(
&self,
current_dpi: f64,
) -> Option<(writer_inkml::FormattedStroke, writer_inkml::Brush)> {
let pixel_to_cm_factor = 2.54 / current_dpi;
match self {
Stroke::BrushStroke(brushstroke) => {
// remark : style is not preserved here, we will always get a smooth
// version
let fill_color = brushstroke.style.stroke_color().unwrap_or_default();
let elements = brushstroke.path.clone().into_elements();
let ignore_pressure = match &brushstroke.style {
Style::Smooth(smooth_options) => match smooth_options.pressure_curve {
PressureCurve::Const => false,
_ => true,
},
Style::Rough(_) => false,
Style::Textured(_) => false,
};
tracing::debug!("formatting strokes");
Some((
writer_inkml::FormattedStroke {
x: elements
.iter()
.map(|element| pixel_to_cm_factor * element.pos.x)
.collect(), // need the scale !
y: elements
.iter()
.map(|element| pixel_to_cm_factor * element.pos.y)
.collect(),
f: elements
.iter()
.map(|element| pixel_to_cm_factor * element.pressure)
.collect(),
},
writer_inkml::Brush::init(
String::from(""),
(
(fill_color.r * 255.0) as u8,
(fill_color.g * 255.0) as u8,
(fill_color.b * 255.0) as u8,
),
ignore_pressure,
((1.0 - fill_color.a) * 255.0) as u8,
brushstroke.style.stroke_width() * pixel_to_cm_factor,
),
))
}
Stroke::ShapeStroke(_) => {
// could be done : relatively easy to do for lines, the rest would have to be discretized
None
}
Stroke::TextStroke(_) => None,
Stroke::VectorImage(_) => None,
Stroke::BitmapImage(_) => None,
}
}
}
1 change: 1 addition & 0 deletions crates/rnote-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unicode-segmentation = { workspace = true }
url = { workspace = true }
writer_inkml = { workspace = true}

[build-dependencies]
anyhow = { workspace = true }
Expand Down
116 changes: 115 additions & 1 deletion crates/rnote-ui/src/appwindow/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ use gtk4::{
};
use p2d::bounding_volume::BoundingVolume;
use rnote_compose::penevent::ShortcutKey;
use rnote_compose::penpath::Element;
use rnote_compose::style::smooth::SmoothOptions;
use rnote_compose::style::PressureCurve;
use rnote_compose::Color;
use rnote_compose::PenPath;
use rnote_compose::SplitOrder;
use rnote_engine::engine::StrokeContent;
use rnote_engine::pens::PenStyle;
use rnote_engine::strokes::resize::{ImageSizeOption, Resize};
use rnote_engine::strokes::BrushStroke;
use rnote_engine::strokes::Stroke;
use rnote_engine::{Camera, Engine};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, error};

Expand Down Expand Up @@ -1246,7 +1254,7 @@ impl RnAppWindow {
restrain_to_viewport: false,
respect_borders: appwindow.respect_borders(),
});
if let Err(e) = canvas.insert_stroke_content(json_string.to_string(), resize_argument, target_pos).await {
if let Err(e) = canvas.deserialize_and_insert_stroke_content(json_string.to_string(), resize_argument, target_pos).await {
error!("Failed to insert stroke content while pasting as `{}`, Err: {e:?}", StrokeContent::MIME_TYPE);
}
}
Expand All @@ -1263,6 +1271,112 @@ impl RnAppWindow {
};
}
));
}
// test if we see inkml
// mimetype expected : application/inkml+xml
else if content_formats.contain_mime_type("application/inkml+xml")
|| content_formats.contain_mime_type("application/x.windows.InkML Format")
{
glib::spawn_future_local(clone!(
#[weak]
canvas,
#[weak(rename_to=appwindow)]
self,
async move {
debug!("Recognized clipboard content: inkml");

match appwindow
.clipboard()
.read_future(
&[
"application/inkml+xml",
"application/x.windows.InkML Format",
],
glib::source::Priority::DEFAULT,
)
.await
{
Ok((input_stream, _)) => {
let acc = collect_clipboard_data(input_stream).await;

if !acc.is_empty() {
match crate::utils::str_from_u8_nul_utf8(&acc) {
Ok(text) => {
let stroke_result = writer_inkml::parse_formatted(text.as_bytes());
debug!("stroke result {:?}", stroke_result);

if let Ok(strokes) = stroke_result {
let mut generated_strokes: Vec<Arc<Stroke>> = vec![];
for (formatted_stroke,brush) in strokes {
let mut smooth_options = SmoothOptions::default();
smooth_options.stroke_color = Some(Color::new(
brush.color.0 as f64 / 255.0,
brush.color.1 as f64 / 255.0,
brush.color.2 as f64 / 255.0,
1.0 - brush.transparency as f64 / 255.0
));
let dpi = canvas.engine_ref().document.format.dpi();

// converting from mm to px
smooth_options.stroke_width = dpi * brush.stroke_width/(10.0*2.54);

// pressure curve
if brush.ignorepressure {
smooth_options.pressure_curve = PressureCurve::Const;
} else {
smooth_options.pressure_curve = PressureCurve::Linear;
}


let penpath = PenPath::try_from_elements(
formatted_stroke.x.into_iter().zip(formatted_stroke.y).zip(formatted_stroke.f).map(|((x,y),f)| {
Element::new(
dpi*na::vector![x,y]/2.54,f
)
})
);
if penpath.is_some() {
let new_stroke = BrushStroke::from_penpath(penpath.unwrap(), rnote_compose::Style::Smooth(smooth_options));
generated_strokes.push(
Arc::new(Stroke::BrushStroke(new_stroke))
);
}
}
// then push
let mut stroke_content = StrokeContent {
strokes: generated_strokes,
bounds:None,
background:None
};
stroke_content.bounds = stroke_content.bounds();

let resize_argument = ImageSizeOption::ResizeImage(Resize {
width: canvas.engine_ref().document.format.width(),
height: canvas.engine_ref().document.format.height(),
layout_fixed_width: canvas.engine_ref().document.layout.is_fixed_width(),
max_viewpoint: None,
restrain_to_viewport: false,
respect_borders: appwindow.respect_borders(),
});
if let Err(e) = canvas.insert_stroke_content(stroke_content, resize_argument, target_pos).await {
error!("Failed to insert stroke content while pasting as `inkml`, Err: {e:?}");
}
} else {
error!("could not parse the inkml file");
}
}
Err(e) => error!("Failed to get string from clipboard data while pasting as inkml, Err: {e:?}"),
}
}
}
Err(e) => {
error!(
"Failed to read clipboard data while pasting as inkml, Err: {e:?}"
);
}
};
}
));
} else if content_formats.contain_mime_type("image/svg+xml") {
glib::spawn_future_local(clone!(
#[weak(rename_to=appwindow)]
Expand Down
21 changes: 20 additions & 1 deletion crates/rnote-ui/src/canvas/imexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl RnCanvas {
/// Deserializes the stroke content and inserts it into the engine.
///
/// The data is usually coming from the clipboard, drop source, etc.
pub(crate) async fn insert_stroke_content(
pub(crate) async fn deserialize_and_insert_stroke_content(
&self,
json_string: String,
resize_option: ImageSizeOption,
Expand Down Expand Up @@ -198,6 +198,25 @@ impl RnCanvas {
Ok(())
}

/// Take stroke content and inserts it into the engine.
///
/// The data is usually coming from the clipboard, drop source, etc.
pub(crate) async fn insert_stroke_content(
&self,
stroke_content: StrokeContent,
resize_option: ImageSizeOption,
target_pos: Option<na::Vector2<f64>>,
) -> anyhow::Result<()> {
let pos = self.determine_stroke_import_pos(target_pos);

let widget_flags =
self.engine_mut()
.insert_stroke_content(stroke_content, pos, resize_option);

self.emit_handle_widget_flags(widget_flags);
Ok(())
}

/// Saves the document to the given file.
///
/// Returns Ok(true) if saved successfully, Ok(false) when a save is already in progress and no file operatiosn were
Expand Down