diff --git a/examples/editor-libcosmic/Cargo.toml b/examples/editor-libcosmic/Cargo.toml index 9412290..94a4846 100644 --- a/examples/editor-libcosmic/Cargo.toml +++ b/examples/editor-libcosmic/Cargo.toml @@ -3,7 +3,7 @@ name = "editor-libcosmic" version = "0.1.0" authors = ["Jeremy Soller "] edition = "2021" -license = "MPL2" +license = "MIT OR Apache-2.0" publish = false [dependencies] diff --git a/examples/editor-orbclient/Cargo.toml b/examples/editor-orbclient/Cargo.toml index 718a6cb..1d1b573 100644 --- a/examples/editor-orbclient/Cargo.toml +++ b/examples/editor-orbclient/Cargo.toml @@ -3,7 +3,7 @@ name = "editor-orbclient" version = "0.1.0" authors = ["Jeremy Soller "] edition = "2021" -license = "MPL2" +license = "MIT OR Apache-2.0" publish = false [dependencies] diff --git a/examples/editor-test/Cargo.toml b/examples/editor-test/Cargo.toml index 3342022..7564acb 100644 --- a/examples/editor-test/Cargo.toml +++ b/examples/editor-test/Cargo.toml @@ -3,7 +3,7 @@ name = "editor-test" version = "0.1.0" authors = ["Jeremy Soller "] edition = "2021" -license = "MPL2" +license = "MIT OR Apache-2.0" publish = false [dependencies] diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 24af48c..ff1e84a 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -3,7 +3,6 @@ use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics}; use orbclient::{Color, EventOption, Renderer, Window, WindowFlag}; use std::{env, fs, process, thread, time::{Duration, Instant}}; -use unicode_segmentation::UnicodeSegmentation; fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) { let bg_color = Color::rgb(0x34, 0x34, 0x34); diff --git a/examples/markdown/Cargo.toml b/examples/markdown/Cargo.toml new file mode 100644 index 0000000..d84a471 --- /dev/null +++ b/examples/markdown/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "markdown" +version = "0.1.0" +authors = ["Jeremy Soller "] +edition = "2021" +license = "MIT OR Apache-2.0" +publish = false + +[dependencies] +cosmic-text = { path = "../../" } +env_logger = "0.9" +fontdb = "0.9" +log = "0.4" +minimad = "0.9" +orbclient = "0.3.35" diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs new file mode 100644 index 0000000..d914a35 --- /dev/null +++ b/examples/markdown/src/main.rs @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cosmic_text::{Attrs, Color, Family, FontSystem, Style, SwashCache, + TextAction, TextBuffer, TextBufferLine, TextMetrics, Weight}; +use orbclient::{EventOption, Renderer, Window, WindowFlag}; +use std::{env, fs, process, thread, time::{Duration, Instant}}; + +fn main() { + env_logger::init(); + + let font_system = FontSystem::new(); + + let md = if let Some(arg) = env::args().nth(1) { + fs::read_to_string(&arg).expect("failed to open file") + } else { + "Markdown can have **bold**, *italic*, and `code` styles".to_string() + }; + let text = minimad::parse_text(&md); + + let mut window = Window::new_flags( + -1, + -1, + 1024, + 768, + &format!("COSMIC TEXT - {}", font_system.locale), + &[WindowFlag::Resizable], + ) + .unwrap(); + + let attrs = cosmic_text::Attrs::new(); + let mut buffer = TextBuffer::new( + &font_system, + attrs, + TextMetrics::new(32, 44) + ); + + buffer.set_size( + window.width() as i32, + window.height() as i32 + ); + + for (line_i, md_line) in text.lines.iter().enumerate() { + while line_i >= buffer.lines.len() { + buffer.lines.push(TextBufferLine::new(String::new(), attrs)); + } + + let composite = match md_line { + minimad::Line::Normal(composite) => composite, + _ => { + //TODO: other md_line types + continue + } + }; + + let line = &mut buffer.lines[line_i]; + //TODO: use composite style + for compound in composite.compounds.iter() { + let start = line.text.len(); + line.text.push_str(compound.src); + let end = line.text.len(); + line.attrs_list.add_span( + start, + end, + attrs + .monospaced(compound.code) + .family(if compound.code { Family::Monospace } else { Family::SansSerif }) + .style(if compound.italic { Style::Italic } else { Style::Normal }) + .weight(if compound.bold { Weight::BOLD } else { Weight::NORMAL }) + ); + line.reset(); + } + } + + + let mut swash_cache = SwashCache::new(&font_system); + + //TODO: make window not async? + let mut mouse_x = -1; + let mut mouse_y = -1; + let mut mouse_left = false; + loop { + let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34); + let font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF); + + if buffer.cursor_moved { + buffer.shape_until_cursor(); + buffer.cursor_moved = false; + } else { + buffer.shape_until_scroll(); + } + + if buffer.redraw { + let instant = Instant::now(); + + window.set(bg_color); + + buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| { + window.rect(x, y, w, h, orbclient::Color { data: color }); + }); + + window.sync(); + + buffer.redraw = false; + + let duration = instant.elapsed(); + log::debug!("redraw: {:?}", duration); + } + + for event in window.events() { + match event.to_option() { + EventOption::Mouse(mouse) => { + mouse_x = mouse.x; + mouse_y = mouse.y; + if mouse_left { + buffer.action(TextAction::Drag { x: mouse_x, y: mouse_y }); + } + }, + EventOption::Button(button) => { + mouse_left = button.left; + if mouse_left { + buffer.action(TextAction::Click { x: mouse_x, y: mouse_y }); + } + }, + EventOption::Resize(resize) => { + buffer.set_size(resize.width as i32, resize.height as i32); + }, + EventOption::Quit(_) => process::exit(0), + _ => (), + } + } + + thread::sleep(Duration::from_millis(1)); + } +} diff --git a/examples/rich-text/Cargo.toml b/examples/rich-text/Cargo.toml index b139f78..6b4a4e2 100644 --- a/examples/rich-text/Cargo.toml +++ b/examples/rich-text/Cargo.toml @@ -3,7 +3,7 @@ name = "rich-text" version = "0.1.0" authors = ["Jeremy Soller "] edition = "2021" -license = "MPL2" +license = "MIT OR Apache-2.0" publish = false [dependencies] @@ -12,7 +12,3 @@ env_logger = "0.9" fontdb = "0.9" log = "0.4" orbclient = "0.3.35" -unicode-segmentation = "1.7" - -[features] -mono = [] diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index abb72e5..b8bb5af 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -4,7 +4,6 @@ use cosmic_text::{Attrs, Color, Family, FontSystem, Style, SwashCache, TextAction, TextBuffer, TextBufferLine, TextMetrics, Weight}; use orbclient::{EventOption, Renderer, Window, WindowFlag}; use std::{env, fs, process, thread, time::{Duration, Instant}}; -use unicode_segmentation::UnicodeSegmentation; fn main() { env_logger::init(); diff --git a/markdown.sh b/markdown.sh new file mode 100755 index 0000000..a464ed8 --- /dev/null +++ b/markdown.sh @@ -0,0 +1 @@ +RUST_LOG=cosmic_text=debug cargo run --release --package markdown -- "$@" diff --git a/rich-text.sh b/rich-text.sh index 5ba3ada..0048a4b 100755 --- a/rich-text.sh +++ b/rich-text.sh @@ -1 +1 @@ -RUST_LOG=cosmic_text=debug cargo run --release --package rich-text +RUST_LOG=cosmic_text=debug cargo run --release --package rich-text -- "$@"