Skip to content

Commit

Permalink
Update READMEs
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLemoine committed Feb 26, 2025
1 parent 695783b commit 058af28
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 187 deletions.
68 changes: 2 additions & 66 deletions dessin-dioxus/README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,3 @@
# [dessin](https://docs.rs/dessin/)
# dessin-dioxus

**dessin is library aimed at building complex drawings, combine them, move them and export them as PDF or SVG.**

## Getting started

Add `dessin` and `dessin-svg` to your project dependencies

```
cargo add dessin dessin-svg
```

Documentation on [docs.rs](https://docs.rs/dessin/0.8.2-pre/)

### Overview

```rust
use dessin::prelude::*;
use dessin_svg::ToSVG;

#[derive(Default)]
struct MyShape {
text: String
}
impl MyShape {
fn say_this(&mut self, what: &str) {
self.text = format!("{} And check this out: `{what}`", self.text);
}
}
impl From<MyShape> for Shape {
fn from(value: MyShape) -> Self {
dessin!(Text: #(
fill={Color::RED}
text={value.text}
)).into()
}
}

let dessin = dessin!(for x in {0..10}: {
let radius = x as f32 * 10.;

dessin!(group: [
{ Circle: #(
fill={Color::RED}
radius={radius}
translate={[x as f32 * 5., 10.]}
) }
{ Text: #(
fill={Color::BLACK}
font_size={10.}
text={"Hi !"}
) }
])
});

let dessin = dessin!(group: [
{ var { dessin }: (
scale={[2., 2.]}
) }
{ MyShape: (
say_this={"Hello world"}
) }
]);

let svg = dessin.to_svg().unwrap();

```
License: MIT
2 changes: 2 additions & 0 deletions dessin-dioxus/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

use ::image::ImageFormat;
use dessin::prelude::*;
use dioxus::prelude::*;
Expand Down
3 changes: 3 additions & 0 deletions dessin-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# dessin-image

License: MIT
2 changes: 2 additions & 0 deletions dessin-image/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

use ::image::{DynamicImage, RgbaImage};
use dessin::{
export::{Export, Exporter},
Expand Down
Empty file added dessin-macros/README.md
Empty file.
4 changes: 3 additions & 1 deletion dessin-pdf/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Dessin PDF
# dessin-pdf

License: MIT
68 changes: 2 additions & 66 deletions dessin-svg/README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,3 @@
# [dessin](https://docs.rs/dessin/)
# dessin-svg

**dessin is library aimed at building complex drawings, combine them, move them and export them as PDF or SVG.**

## Getting started

Add `dessin` and `dessin-svg` to your project dependencies

```
cargo add dessin dessin-svg
```

Documentation on [docs.rs](https://docs.rs/dessin/0.8.2-pre/)

### Overview

```rust
use dessin::prelude::*;
use dessin_svg::ToSVG;

#[derive(Default)]
struct MyShape {
text: String
}
impl MyShape {
fn say_this(&mut self, what: &str) {
self.text = format!("{} And check this out: `{what}`", self.text);
}
}
impl From<MyShape> for Shape {
fn from(value: MyShape) -> Self {
dessin!(Text: #(
fill={Color::RED}
text={value.text}
)).into()
}
}

let dessin = dessin!(for x in {0..10}: {
let radius = x as f32 * 10.;

dessin!(group: [
{ Circle: #(
fill={Color::RED}
radius={radius}
translate={[x as f32 * 5., 10.]}
) }
{ Text: #(
fill={Color::BLACK}
font_size={10.}
text={"Hi !"}
) }
])
});

let dessin = dessin!(group: [
{ var { dessin }: (
scale={[2., 2.]}
) }
{ MyShape: (
say_this={"Hello world"}
) }
]);

let svg = dessin.to_svg().unwrap();

```
License: MIT
2 changes: 1 addition & 1 deletion dessin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = [
"Olivier Lemoine <[email protected]>",
"Francois Morillon <[email protected]>",
]
description = "Build complex drawing for PDF, SVG, ..."
description = "Build complex drawing for PDF, SVG, Images or Dioxus"
categories = ["graphics", "gui", "rendering", "template-engine"]
keywords = ["graphics", "draw", "layout"]
edition = "2021"
Expand Down
175 changes: 122 additions & 53 deletions dessin/README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,142 @@
# [dessin](https://docs.rs/dessin/)
# dessin

**dessin is library aimed at building complex drawings, combine them, move them and export them as PDF or SVG.**
**dessin** is library aimed at building complex drawings, combine them, move them and export them as PDF or SVG.

## Getting started
Details about the [macro][`crate::macros`].

Add `dessin` and `dessin-svg` to your project dependencies

```
cargo add dessin dessin-svg
```

or if you need PDF:

```
cargo add dessin dessin-pdf
```

Documentation on [docs.rs](https://docs.rs/dessin/0.8.2-pre/)

### Overview
### Example

```rust
use dessin::prelude::*;
use dessin_svg::ToSVG;
use palette::{named, Srgb};

#[derive(Default)]
#[derive(Default, Shape)]
struct MyShape {
text: String
text: String,
}
impl MyShape {
fn say_this(&mut self, what: &str) {
self.text = format!("{} And check this out: `{what}`", self.text);
}
}
impl From<MyShape> for Shape {
fn from(value: MyShape) -> Self {
dessin!(Text: #(
fill={Color::RED}
text={value.text}
)).into()
fn from(MyShape { text }: MyShape) -> Self {
dessin2!(*Text(fill = Srgb::<f32>::from_format(named::RED).into_linear(), { text })).into()
}
}

let dessin = dessin!(for x in {0..10}: {
let radius = x as f32 * 10.;

dessin!(group: [
{ Circle: #(
fill={Color::RED}
radius={radius}
translate={[x as f32 * 5., 10.]}
) }
{ Text: #(
fill={Color::BLACK}
font_size={10.}
text={"Hi !"}
) }
])
});

let dessin = dessin!(group: [
{ var { dessin }: (
scale={[2., 2.]}
) }
{ MyShape: (
say_this={"Hello world"}
) }
]);

let svg = dessin.to_svg().unwrap();
fn main() {
let dessin = dessin2!(for x in 0..10 {
let radius = x as f32 * 10.;

dessin2!([
*Circle(
fill = Srgb::<f32>::from_format(named::RED).into_linear(),
{ radius },
translate = [x as f32 * 5., 10.],
),
*Text(fill = Srgb::<f32>::from_format(named::BLACK).into_linear(), font_size = 10., text = "Hi !",),
])
});

let dessin = dessin2!([
{ dessin }(scale = [2., 2.]),
MyShape(say_this = "Hello world"),
]);
}
```

### Components

[Base components][`crate::shapes`] are defined in the `shapes` module.

[Components using base ones][`crate::contrib`] are defined in the `contrib` module.

In `dessin`, a component is just a struct/enum that can be converted to a [Shape][crate::shapes::Shape],
and implements the [Default][`std::default::Default`] trait.

This means that a component can be as simple as:
```rust
use dessin::prelude::*;

#[derive(Default)]
struct MyComponent {}

impl From<MyComponent> for Shape {
fn from(my_component: MyComponent) -> Shape {
dessin2!(
// Implementation...
)
}
}
```

Since the [dessin2!][`dessin_macros::dessin2`] macro is only syntactic sugar for creating a [Shape][crate::shapes::Shape],
all parameters are simply rust function with the following signature: `fn (&mut self, argument_value: ArgumentType) {...}`.

It can be tedious to create these function for all parameters, so the derive macro [Shape][`dessin_macro::shape`]
is here to do exactly that.

So
```rust
#[derive(Default, Shape)]
struct MyComponent {
// This auto implements ShapeOp for MyComponent using `my_local_transform` as the storage.
#[local_transform]
my_local_transform: Transform2<f32>,

// Generate a function for u32
value: u32,

// Does not generate a function for this field
#[shape(skip)]
skip_value: u32,

// Generates a function for Into<u32>
#[shape(into)]
into_value: u32,

// Generates a function that does not take any arguments, but set `my_bool` to true
#[shape(bool)]
my_bool: bool,
}

```

becomes
```rust

#[derive(Default)]
struct MyComponent {
my_local_transform: Transform2<f32>,
value: u32,
skip_value: u32,
into_value: u32,
my_bool: bool,
}

impl ShapeOp for MyComponent { /* skip impl */ }

impl MyComponent {
pub fn value(&mut self, value: u32) -> &mut Self {
/* skip impl */
}
pub fn into_value<T: Into<u32>>(&mut self, value: T) -> &mut Self {
/* skip impl */
}
pub fn my_bool(&mut self) -> &mut Self {
/* skip impl */
}
}

```
To be precise, all functions generated by this derive macro, return `&mut Self` to chain function together.
Generated functions have the same name as their corresponding field.
This derive macro also generate corresponding `with_xxxx`, taking `self` instead of `&mut self` and returning `Self`.

One still does need to implement `From<MyComponent> for Shape { ... }` manually.

### Implement own export format.
Documentation can be found in the [`export`] module.

License: MIT
1 change: 1 addition & 0 deletions dessin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
#![warn(missing_docs)]
#![allow(clippy::tabs_in_doc_comments)]
#![doc = include_str!("../../README.md")]

pub mod macros;

Expand Down

0 comments on commit 058af28

Please sign in to comment.