diff options
Diffstat (limited to 'packages/svgbob')
60 files changed, 14926 insertions, 0 deletions
diff --git a/packages/svgbob/Behavior.md b/packages/svgbob/Behavior.md new file mode 100644 index 0000000..4b001eb --- /dev/null +++ b/packages/svgbob/Behavior.md @@ -0,0 +1,39 @@ + +`.` will make a rounded corner for top-left and top-right +`,` will make a rounded corner, only on the top-left +`\`` will make a rounded corner for bottom-left +`'` will make a rounded corner for bottom-left and bottom-right + +`+` will make a sharp corner when connected from 2 perpendicular lines. +will become a cross-section when connected from 4 directions (top, right, bottom, left) + +`\*` is a small solid circle when connected to a line + +`o` is a small clear circle when connected to a line +`O` is a bigger clear circle when connected to a line + +`-` is a solid line + when 3 `- - -` is separated by space will make a broken line + +`|` is a vertical line + +`~` is a broken line + +`\_` is a lowered solid line + +`:` is a vertical broken line + +`!` is a vertical broken line + +`<` will make an arrow to the left if a line connects from right + +`>` will make an arrow to the right if a line connects from left + +`V` will make an arrow pointing bottom if a line connects from top + will make an arrow pointing bottom-left if a line connects from top-right + will make an arrow pointing bottom-right if a line connects from top-left + +`^` will make an upward arrow if a line connects from bottom + will make an arrow pointing top-left if a line connects from bottom-right + will make an arrow pointing top-right if a line connects from bottom-left + diff --git a/packages/svgbob/Cargo.toml b/packages/svgbob/Cargo.toml new file mode 100644 index 0000000..a81790c --- /dev/null +++ b/packages/svgbob/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "svgbob" +version = "0.6.2" +authors = ["Jovansonlee Cesar <ivanceras@gmail.com>"] +edition = "2018" +description = "Transform your ascii diagrams into happy little SVG" +repository = "https://github.com/ivanceras/svgbob" +documentation = "https://docs.rs/svgbob" +keywords = ["ascii","plain","text", "svg", "bob"] +license = "Apache-2.0" + +[dependencies] +nalgebra = "0.29" +parry2d = "0.7" +lazy_static = "1.3.0" +sauron = { version = "0.43.4", default-features = false} +#sauron = { version = "0.43", default-features = false, path = "../../../sauron"} +unicode-width = "0.1.5" +itertools = "0.8.0" +pom = { version = "3.1.0" } +log = "0.4" + + + +[features] +# This affects the rendering of text nodes +# if the svg is rendered statically, characters such as '<' needed to be escaped +# while if it is rendered in the dom, no escaping is needed. +with-dom = ["sauron/with-dom"] + diff --git a/packages/svgbob/concepts.md b/packages/svgbob/concepts.md new file mode 100644 index 0000000..f550968 --- /dev/null +++ b/packages/svgbob/concepts.md @@ -0,0 +1,11 @@ + +Turns svg into text respresentation by approximating the lines, +circles, arcs into character blocks + +A bounding box of a line is computed and the rectangles (AKA the cells) in the grid +are then tested which points for each rectangle intersects the line. +This points are then approximated into the closes SubCell location. + +Each subcell will now contain drawing fragments such as line, arc, circles +and then tried to be match to which characters/letter/ascii would best +represent the drawing fragment. diff --git a/packages/svgbob/examples/circles.rs b/packages/svgbob/examples/circles.rs new file mode 100644 index 0000000..95f5ce8 --- /dev/null +++ b/packages/svgbob/examples/circles.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/circles.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/circles.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/circuits.rs b/packages/svgbob/examples/circuits.rs new file mode 100644 index 0000000..c94fe60 --- /dev/null +++ b/packages/svgbob/examples/circuits.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/circuits.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/circuits.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/demo.rs b/packages/svgbob/examples/demo.rs new file mode 100644 index 0000000..5c8a8b8 --- /dev/null +++ b/packages/svgbob/examples/demo.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/demo.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/demo.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/example.rs b/packages/svgbob/examples/example.rs new file mode 100644 index 0000000..ad4a3c4 --- /dev/null +++ b/packages/svgbob/examples/example.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/example.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/example.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/long.rs b/packages/svgbob/examples/long.rs new file mode 100644 index 0000000..a2901ac --- /dev/null +++ b/packages/svgbob/examples/long.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/long.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/long.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/merge.rs b/packages/svgbob/examples/merge.rs new file mode 100644 index 0000000..766b262 --- /dev/null +++ b/packages/svgbob/examples/merge.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/merge.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/merge.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/examples/simple.rs b/packages/svgbob/examples/simple.rs new file mode 100644 index 0000000..4418e38 --- /dev/null +++ b/packages/svgbob/examples/simple.rs @@ -0,0 +1,12 @@ +use std::{fs, io, time::Instant}; + +extern crate svgbob; + +fn main() -> io::Result<()> { + let art = include_str!("../test_data/simple.bob"); + let t1 = Instant::now(); + fs::create_dir_all("out")?; + fs::write("out/simple.svg", svgbob::to_svg(art))?; + println!("took {}ms", t1.elapsed().as_millis()); + Ok(()) +} diff --git a/packages/svgbob/run_circles.sh b/packages/svgbob/run_circles.sh new file mode 100755 index 0000000..d09eb23 --- /dev/null +++ b/packages/svgbob/run_circles.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example circles diff --git a/packages/svgbob/run_demo.sh b/packages/svgbob/run_demo.sh new file mode 100755 index 0000000..aa6dbd1 --- /dev/null +++ b/packages/svgbob/run_demo.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example demo diff --git a/packages/svgbob/run_example.sh b/packages/svgbob/run_example.sh new file mode 100755 index 0000000..8556916 --- /dev/null +++ b/packages/svgbob/run_example.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example example diff --git a/packages/svgbob/run_long.sh b/packages/svgbob/run_long.sh new file mode 100755 index 0000000..b6d8c7f --- /dev/null +++ b/packages/svgbob/run_long.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example long diff --git a/packages/svgbob/run_merge.sh b/packages/svgbob/run_merge.sh new file mode 100755 index 0000000..c837cbc --- /dev/null +++ b/packages/svgbob/run_merge.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example merge diff --git a/packages/svgbob/run_simple.sh b/packages/svgbob/run_simple.sh new file mode 100755 index 0000000..9a77a9a --- /dev/null +++ b/packages/svgbob/run_simple.sh @@ -0,0 +1 @@ +reset && RUST_BACKTRACE=1 cargo run --release --example simple diff --git a/packages/svgbob/rustfmt.toml b/packages/svgbob/rustfmt.toml new file mode 100644 index 0000000..503ede8 --- /dev/null +++ b/packages/svgbob/rustfmt.toml @@ -0,0 +1,19 @@ +# Use unstable features +unstable_features = true + +max_width = 80 + +## Visually align, useful in writing the view +indent_style = "Block" +imports_indent = "Block" +reorder_imports = true +reorder_impl_items = true +merge_imports = true +## I want to be able to delete unused imports easily +imports_layout = "Vertical" +## Default value is false, yet clipy keeps nagging on this +use_field_init_shorthand = true + +## also format macro +format_macro_matchers = true +force_multiline_blocks = true diff --git a/packages/svgbob/spec.md b/packages/svgbob/spec.md new file mode 100644 index 0000000..b1f5c5c --- /dev/null +++ b/packages/svgbob/spec.md @@ -0,0 +1,184 @@ +## Svgbob specification + +[Rendered](https://ivanceras.github.io/spongedown-editor/?file=https://raw.githubusercontent.com/ivanceras/svgbobrus/master/spec.md) + +Svgbob is a diagramming model which uses common typing characters to approximate the shape + +|characters| names | description +|----------|----------------------|------------ +| `-` | dash, hypen, minus | for horizontal lines +| `_` | underscore | for horizontal lines +| `\|` | pipe, or | for vertical lines +| `/` | forward slash | for lines slanted to the right +| `\\` | backslash | for lines slanted to the left +| `+` | plus, add, cross | for sharp intersection +| `.` | dot, period | for rounded corner intersection +| `,` | comma | for rounded corner intersection +| `'` | single quote | for rounded corner intersection +| `` ` `` | backtick | for rounded corner intersection +| `(` | open parenthesis | rounded side of an arc +| `)` | close parenthesis | rounded side of an arc +| `[` | open braces | rectangular corner +| `]` | close braces | rectangular corner +| `*` | asterisk | for emphasized intersection +| `o` | lowercase letter `o` | for intersection +| `O` | uppercase letter `O` | for intersection +| `^` | caret | for arrow up +| `v` | lowercase letter `v` | for arrow down +| `V` | uppercase letter `V` | for arrow down +| `<` | lesser than | for arrow left +| `>` | greater than | for arrow right +| `=` | equal sign | for double horizontal line +| `x` | lowercase letter `x` | for intersection +| `X` | uppercase letter `X` | for intersection + +## Principle +The rendered shape should closely resembles to that of the ascii +drawing / formation. + +## Non-goals +The goal is NOT to be able to make graphs and diagrams with less effort. + +## Default sizes +Text height is 2 times the text width, both should be a multiple of 4. + +Default values: +- text width = 8px; +- text height = 16px; + +------------------- +## Horizontal line `-` +- if next to an alphanumeric character it will be rendered as text. +- if next to another drawing character, it will be rendered as a line. +- if alone it will be rendered as a line. + + **Example 1:** + +|text | svg |rendered +|------|---------------------------------------|-------------- +|`-` |`<line x1="0" y1="8" x2="8" y2="8"/>` |<svg width="16" height="16"><line x1="0" y1="8" x2="8" y2="8"/></svg> +|`--` |`<line x1="0" y1="8" x2="16" y2="8"/>` |<svg width="16" height="16"><line x1="0" y1="8" x2="16" y2="8"/></svg> +|`----`|`<line x1="0" y1="8" x2="32" y2="8"/>` |<svg width="32" height="16"><line x1="0" y1="8" x2="32" y2="8"/></svg> + +```bob ++---------------+-------- +|"-" | - | ++-------+-------+-------- +|"--" | -- | ++-------+-------+-------- +|"----" | ---- | ++-------+-------+--------- +``` + +**Example 3:** Next to an alphanumeric character + +|text |svg |rendered +|------|------------------------------|------- +|`1-` |`<text x="2" y="12">1-</text>`|<svg width="32" height="16"><text x="2" y="12">1-</text></svg> +|`-a` |`<text x="2" y="12">-a</text>`|<svg width="32" height="16"><text x="2" y="12">-a</text></svg> + +**Example 4:** Used together with text + +|text |svg |rendered +|-----------|-------------------------------------|---------- +|`opt-in` |`<text x="2" y="12">opt-in</text>` |<svg width="48" height="16"><text x="2" y="12">opt-in</text></svg> +|`chat-room`|`<text x="2" y="12">chat-room</text>`|<svg width="144" height="16"><text x="2" y="12">chat-room</text></svg> + +--------------------- +## Vertical line `|` +- if next to an alphanumeric character it will be rendered as text. +- if next to another drawing character it will be rendered as vertical line. +- if alone it will be rendered as vertical line + +**Example 1:** + +``` +| +| +``` +|text | svg | rendered +|------------------|-----------------------------|---------------- +|`\|`<br>`\|` |`<line x1="4" y1="0" x2="4" y2="32"></line>` |<svg width="8" height="32"><line x1="4" y1="0" x2="4" y2="32"></line></svg> | + + use as or expression + Example 2: + a||b <text>a||b</text> + +------------------ +## Forward slash `/` +- if next to an alphanumeric character it will be rendered as text. +- if at least one if its 8 neighbors: (top,bottom,left,right, topleft, topright, bottomleft, bottomright) + is a drawing character then it will be rendered as a slanted line to the right +- if used as text but next to a drawing element at the same time, rendering to drawing + element takes precedence. + +**Example 1:** +``` + / +/ +``` + +|text | svg | rendered +|--------|--------|--------------- +| `/`<br>`/`|`<line x1="0" y1="16" x2="16" y2="32"></line>`|<svg width="16" height="32"><line x1="0" y1="32" x2="16" y2="0"></line></svg> + + +**Example 2:** + +|text | svg | rendered +|-----|-----|----------- +|`folder/` |`<text x="2" y="12">folder/</text>`|<svg width="56" height="16"><text x="2" y="12">folder/</text></svg> +|`/usr/bin` |`<text x="2" y="12">/usr/bin</text>`|<svg width="72" height="16"><text x="2" y="12">/usr/bin</text></svg> + +**Example 3:** Aligned next to a drawing element + +|text | svg | rendered +|-----|-----|----------- +|`folder/`<br> `/usr/bin`|`<line x1="40" y1="32" x2="56" y2="0"/>`<br>`<text x="2" y="12">folder</text>`<br>`<text x="10" y="28">/usr</text>`<br>`<text x="58" y="28">bin</text>`|<svg width="100" height="32"><line x1="40" y1="32" x2="56" y2="0"/><text x="2" y="12">folder</text><text x="10" y="28">/usr</text><text x="58" y="28">bin</text></svg> + +-------------------- +## Backward slash `\` +- if next to an alphanumeric character, then it will be rendered as text +- if connects to a drawing element, then it will be rendered as a slanted line to the left +- if alone then it will be rendered as slanted line to the left + +**Example 1:** +``` + \ + \ +``` + +|text | svg | rendered +|--------|---------|--------------- +|`\\`<br> `\\`|`<line x1="0" y1="0" x2="16" y2="32"/>`|<svg width="16" height="32"><line x1="0" y1="0" x2="16" y2="32"/></svg> + +**Example 2:** + +|text | svg | rendered +|---------|-----|----------- +|`C:\\users`|`<text x="2" y="12">C:\\users</text>`|<svg width="72" height="16"><text x="2" y="12">C:\\users</text></svg> + +## Cross `+` +- If the left side is horizontal, then this will become a horizontal line connecting midway to the left +- If the right side is horizontal, then this will come a horizontal line connecting midway to the right +- If top of this character is a vertical line, then this will become a vertical line connecting midway to the the top +- If bottom of this is a vertical line, then this will become a vertical line connecting miday to bottom + +**Example 1:** + +|text | svg | rendered +|-----|-----|---------- +|`-+` |`<line x1="0" y1="4" x2="12" y2="4"/>`|<svg width="32" height="8"><line x1="0" y1="4" x2="12" y2="4"/></svg> +|`+-` |`<line x1="4" y1="4" x2="16" y2="4"/>`|<svg width="32" height="8"><line x1="4" y1="4" x2="16" y2="4"/></svg> +|`\|`<br>`+` |`<line x1="4" y1="0" x2="4" y2="24"/>`|<svg width="8" height="32"><line x1="4" y1="0" x2="4" y2="24"/></svg> +|`+`<br>`\|` |`<line x1="4" y1="8" x2="4" y2="32"/>`|<svg width="8" height="32"><line x1="4" y1="8" x2="4" y2="32"/></svg> + +## Dot and Comma (.,) +- Primary purpose is to make rounded corners, top_left rounded corner and top_right rounded corner + +** Example 1:** + +|text | svg | rendered +|-----|-----|--------- +|`.-`<br>`\|``| + diff --git a/packages/svgbob/src/buffer.rs b/packages/svgbob/src/buffer.rs new file mode 100644 index 0000000..867bd3c --- /dev/null +++ b/packages/svgbob/src/buffer.rs @@ -0,0 +1,10 @@ +pub use cell_buffer::{Cell, CellBuffer, CellGrid, Contacts, Settings, Span}; +pub use fragment_buffer::Direction; +pub use fragment_buffer::{fragment, fragment::Fragment, FragmentBuffer}; +pub use property_buffer::{Property, PropertyBuffer, Signal}; +pub use string_buffer::StringBuffer; + +mod cell_buffer; +mod fragment_buffer; +mod property_buffer; +mod string_buffer; diff --git a/packages/svgbob/src/buffer/cell_buffer.rs b/packages/svgbob/src/buffer/cell_buffer.rs new file mode 100644 index 0000000..c1f4421 --- /dev/null +++ b/packages/svgbob/src/buffer/cell_buffer.rs @@ -0,0 +1,803 @@ +use crate::fragment::CellText; +use crate::{ + buffer::{fragment_buffer::FragmentTree, Fragment, StringBuffer}, + util::parser, +}; +pub use cell::{Cell, CellGrid}; +pub use contacts::Contacts; +use itertools::Itertools; +use sauron::{ + html, + html::{attributes::*, *}, + svg::{attributes::*, *}, + Node, +}; +pub use settings::Settings; +pub use span::Span; +use std::{ + collections::BTreeMap, + fmt, + ops::{Deref, DerefMut}, +}; +use unicode_width::UnicodeWidthStr; + +mod cell; +mod contacts; +mod endorse; +mod settings; +mod span; + +/// The simplest buffer. +/// This is maps which char belong to which cell skipping the whitespaces +#[derive(Debug)] +pub struct CellBuffer { + map: BTreeMap<Cell, char>, + /// class, <style> + /// assemble into + /// |