Skip to content

Commit

Permalink
Add ab_glyph feature to examples and use the new draw_with method
Browse files Browse the repository at this point in the history
  • Loading branch information
EHfive committed Oct 1, 2023
1 parent e422660 commit b3a9861
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/editor-libcosmic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ version = "0.11"
[features]
default = []
vi = ["cosmic-text/vi"]
ab_glyph = ["cosmic-text/ab_glyph_image"]
16 changes: 13 additions & 3 deletions examples/editor-libcosmic/src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use cosmic::{
iced_runtime::keyboard::KeyCode,
theme::{Theme, ThemeType},
};
use cosmic_text::{Action, Edit, SwashCache};
#[cfg(feature = "ab_glyph")]
use cosmic_text::AbGlyphDraw;
#[cfg(not(feature = "ab_glyph"))]
use cosmic_text::SwashCache;
use cosmic_text::{Action, Edit};
use std::{cmp, sync::Mutex, time::Instant};

use crate::FONT_SYSTEM;
Expand Down Expand Up @@ -232,8 +236,8 @@ where

// Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
editor.draw(
&mut state.cache.lock().unwrap(),
editor.draw_with(
&mut (*state.cache.lock().unwrap()),
text_color,
|x, y, w, h, color| {
//TODO: improve performance
Expand Down Expand Up @@ -387,6 +391,9 @@ where

pub struct State {
is_dragging: bool,
#[cfg(feature = "ab_glyph")]
cache: Mutex<AbGlyphDraw>,
#[cfg(not(feature = "ab_glyph"))]
cache: Mutex<SwashCache>,
}

Expand All @@ -395,6 +402,9 @@ impl State {
pub fn new() -> State {
State {
is_dragging: false,
#[cfg(feature = "ab_glyph")]
cache: Mutex::new(AbGlyphDraw::new()),
#[cfg(not(feature = "ab_glyph"))]
cache: Mutex::new(SwashCache::new()),
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/editor-orbclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ unicode-segmentation = "1.7"
[features]
default = []
vi = ["cosmic-text/vi"]
ab_glyph = ["cosmic-text/ab_glyph_image"]
10 changes: 6 additions & 4 deletions examples/editor-orbclient/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, SwashCache, SyntaxEditor,
SyntaxSystem,
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, SyntaxEditor, SyntaxSystem,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
Expand Down Expand Up @@ -81,7 +80,10 @@ fn main() {
}
}

let mut swash_cache = SwashCache::new();
#[cfg(feature = "ab_glyph")]
let mut renderer = cosmic_text::AbGlyphDraw::new();
#[cfg(not(feature = "ab_glyph"))]
let mut renderer = cosmic_text::SwashCache::new();

let mut ctrl_pressed = false;
let mut mouse_x = -1;
Expand All @@ -96,7 +98,7 @@ fn main() {
window.set(orbclient::Color::rgb(bg.r(), bg.g(), bg.b()));

let fg = editor.foreground_color();
editor.draw(&mut swash_cache, fg, |x, y, w, h, color| {
editor.draw_with(&mut renderer, fg, |x, y, w, h, color| {
window.rect(
line_x as i32 + x,
y,
Expand Down
3 changes: 3 additions & 0 deletions examples/editor-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ fontdb = "0.13"
log = "0.4"
orbclient = "0.3.35"
unicode-segmentation = "1.7"

[features]
ab_glyph = ["cosmic-text/ab_glyph_image"]
17 changes: 10 additions & 7 deletions examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, BidiParagraphs, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem,
Metrics, SwashCache,
Action, BidiParagraphs, BorrowedWithFontSystem, Buffer, Color, Draw, Edit, Editor, FontSystem,
Metrics,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, time::Instant};
use unicode_segmentation::UnicodeSegmentation;

fn redraw(
fn redraw<D: Draw>(
window: &mut Window,
editor: &mut BorrowedWithFontSystem<Editor>,
swash_cache: &mut SwashCache,
renderer: &mut D,
) {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
Expand All @@ -22,7 +22,7 @@ fn redraw(

window.set(bg_color);

editor.draw(swash_cache, font_color, |x, y, w, h, color| {
editor.draw_with(renderer, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
});

Expand Down Expand Up @@ -70,7 +70,10 @@ fn main() {

let mut editor = editor.borrow_with(&mut font_system);

let mut swash_cache = SwashCache::new();
#[cfg(feature = "ab_glyph")]
let mut renderer = cosmic_text::AbGlyphDraw::new();
#[cfg(not(feature = "ab_glyph"))]
let mut renderer = cosmic_text::SwashCache::new();

let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")
Expand Down Expand Up @@ -138,7 +141,7 @@ fn main() {
// Finally, normal enter
editor.action(Action::Enter);

redraw(&mut window, &mut editor, &mut swash_cache);
redraw(&mut window, &mut editor, &mut renderer);

for event in window.events() {
if let EventOption::Quit(_) = event.to_option() {
Expand Down
3 changes: 3 additions & 0 deletions examples/rich-text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ env_logger = "0.10"
fontdb = "0.13"
log = "0.4"
orbclient = "0.3.35"

[features]
ab_glyph = ["cosmic-text/ab_glyph_image"]
11 changes: 7 additions & 4 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Shaping, Style,
SwashCache, Weight,
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Shaping, Style, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
Expand Down Expand Up @@ -121,7 +120,11 @@ fn main() {
.buffer_mut()
.set_rich_text(spans.iter().copied(), Shaping::Advanced);

let mut swash_cache = SwashCache::new();
#[cfg(feature = "ab_glyph")]
let mut renderer = cosmic_text::AbGlyphDraw::new();
#[cfg(not(feature = "ab_glyph"))]
// A SwashCache stores rasterized glyphs, create one per application
let mut renderer = cosmic_text::SwashCache::new();

//TODO: make window not async?
let mut mouse_x = -1;
Expand All @@ -137,7 +140,7 @@ fn main() {

window.set(bg_color);

editor.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
editor.draw_with(&mut renderer, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
});

Expand Down
3 changes: 3 additions & 0 deletions examples/terminal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ env_logger = "0.10"
fontdb = "0.13"
log = "0.4"
termion = "2.0"

[features]
ab_glyph = ["cosmic-text/ab_glyph_image"]
9 changes: 6 additions & 3 deletions examples/terminal/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, Shaping, SwashCache};
use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, Shaping};
use std::cmp::{self, Ordering};
use termion::{color, cursor};

fn main() {
// A FontSystem provides access to detected system fonts, create one per application
let mut font_system = FontSystem::new();

#[cfg(feature = "ab_glyph")]
let mut renderer = cosmic_text::AbGlyphDraw::new();
#[cfg(not(feature = "ab_glyph"))]
// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new();
let mut renderer = cosmic_text::SwashCache::new();

// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(14.0, 20.0);
Expand Down Expand Up @@ -57,7 +60,7 @@ fn main() {
// Print the buffer
let mut last_x = 0;
let mut last_y = 0;
buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
buffer.draw_with(&mut renderer, text_color, |x, y, w, h, color| {
let a = color.a();
if a == 0 || x < 0 || y < 0 || w != 1 || h != 1 {
// Ignore alphas of 0, or invalid x, y coordinates, or unimplemented sizes
Expand Down

0 comments on commit b3a9861

Please sign in to comment.