Skip to content

Commit

Permalink
Merge pull request #122 from hecrj/feature/shaping-switch
Browse files Browse the repository at this point in the history
`Shaping` strategy selection
  • Loading branch information
jackpot51 authored Jun 8, 2023
2 parents bb176df + b85d6a4 commit 9062cce
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use cosmic_text::{
Action, Attrs, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family, FontSystem, Metrics,
Style, SwashCache, Weight,
Shaping, Style, SwashCache, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
Expand Down Expand Up @@ -143,7 +143,7 @@ fn main() {
editor
.buffer_mut()
.lines
.push(BufferLine::new(line_text, attrs_list));
.push(BufferLine::new(line_text, attrs_list, Shaping::Advanced));
}

let mut swash_cache = SwashCache::new();
Expand Down
4 changes: 2 additions & 2 deletions examples/terminal/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

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

Expand Down Expand Up @@ -28,7 +28,7 @@ fn main() {
let attrs = Attrs::new();

// Add some text!
buffer.set_text(" Hi, Rust! 🦀", attrs);
buffer.set_text(" Hi, Rust! 🦀", attrs, Shaping::Advanced);

// Perform shaping as desired
buffer.shape_until_scroll();
Expand Down
30 changes: 21 additions & 9 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::Color;
use crate::{
Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, FontSystem, LayoutGlyph, LayoutLine,
ShapeLine, Wrap,
ShapeLine, Shaping, Wrap,
};

/// Current cursor location
Expand Down Expand Up @@ -331,7 +331,7 @@ impl Buffer {
redraw: false,
wrap: Wrap::Word,
};
buffer.set_text(font_system, "", Attrs::new());
buffer.set_text(font_system, "", Attrs::new(), Shaping::Advanced);
buffer
}

Expand Down Expand Up @@ -562,16 +562,28 @@ impl Buffer {
}

/// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, font_system: &mut FontSystem, text: &str, attrs: Attrs) {
pub fn set_text(
&mut self,
font_system: &mut FontSystem,
text: &str,
attrs: Attrs,
shaping: Shaping,
) {
self.lines.clear();
for line in text.lines() {
self.lines
.push(BufferLine::new(line.to_string(), AttrsList::new(attrs)));
self.lines.push(BufferLine::new(
line.to_string(),
AttrsList::new(attrs),
shaping,
));
}
// Make sure there is always one line
if self.lines.is_empty() {
self.lines
.push(BufferLine::new(String::new(), AttrsList::new(attrs)));
self.lines.push(BufferLine::new(
String::new(),
AttrsList::new(attrs),
shaping,
));
}

self.scroll = 0;
Expand Down Expand Up @@ -769,8 +781,8 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> {
}

/// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, text: &str, attrs: Attrs) {
self.inner.set_text(self.font_system, text, attrs);
pub fn set_text(&mut self, text: &str, attrs: Attrs, shaping: Shaping) {
self.inner.set_text(self.font_system, text, attrs, shaping);
}

/// Draw the buffer
Expand Down
15 changes: 11 additions & 4 deletions src/buffer_line.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

use crate::{Align, AttrsList, FontSystem, LayoutLine, ShapeLine, Wrap};
use crate::{Align, AttrsList, FontSystem, LayoutLine, ShapeLine, Shaping, Wrap};

/// A line (or paragraph) of text that is shaped and laid out
pub struct BufferLine {
Expand All @@ -12,20 +12,22 @@ pub struct BufferLine {
align: Option<Align>,
shape_opt: Option<ShapeLine>,
layout_opt: Option<Vec<LayoutLine>>,
shaping: Shaping,
}

impl BufferLine {
/// Create a new line with the given text and attributes list
/// Cached shaping and layout can be done using the [`Self::shape`] and
/// [`Self::layout`] functions
pub fn new<T: Into<String>>(text: T, attrs_list: AttrsList) -> Self {
pub fn new<T: Into<String>>(text: T, attrs_list: AttrsList, shaping: Shaping) -> Self {
Self {
text: text.into(),
attrs_list,
wrap: Wrap::Word,
align: None,
shape_opt: None,
layout_opt: None,
shaping,
}
}

Expand Down Expand Up @@ -142,7 +144,7 @@ impl BufferLine {
let attrs_list = self.attrs_list.split_off(index);
self.reset();

let mut new = Self::new(text, attrs_list);
let mut new = Self::new(text, attrs_list, self.shaping);
new.wrap = self.wrap;
new
}
Expand All @@ -167,7 +169,12 @@ impl BufferLine {
/// Shape line, will cache results
pub fn shape(&mut self, font_system: &mut FontSystem) -> &ShapeLine {
if self.shape_opt.is_none() {
self.shape_opt = Some(ShapeLine::new(font_system, &self.text, &self.attrs_list));
self.shape_opt = Some(ShapeLine::new(
font_system,
&self.text,
&self.attrs_list,
self.shaping,
));
self.layout_opt = None;
}
self.shape_opt.as_ref().expect("shape not found")
Expand Down
4 changes: 4 additions & 0 deletions src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::Color;
use crate::{
Action, Affinity, AttrsList, Buffer, BufferLine, Cursor, Edit, FontSystem, LayoutCursor,
Shaping,
};

/// A wrapper of [`Buffer`] for easy editing
Expand Down Expand Up @@ -245,6 +246,7 @@ impl Edit for Editor {
.strip_suffix(char::is_control)
.unwrap_or(data_line),
these_attrs,
Shaping::Advanced,
));
} else {
panic!("str::lines() did not yield any elements");
Expand All @@ -256,6 +258,7 @@ impl Edit for Editor {
.strip_suffix(char::is_control)
.unwrap_or(data_line),
final_attrs.split_off(remaining_split_len),
Shaping::Advanced,
);
tmp.append(after);
self.buffer.lines.insert(insert_line, tmp);
Expand All @@ -270,6 +273,7 @@ impl Edit for Editor {
.strip_suffix(char::is_control)
.unwrap_or(data_line),
final_attrs.split_off(remaining_split_len),
Shaping::Advanced,
);
self.buffer.lines.insert(insert_line, tmp);
self.cursor.line += 1;
Expand Down
6 changes: 4 additions & 2 deletions src/edit/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};

use crate::{
Action, AttrsList, BorrowedWithFontSystem, Buffer, Color, Cursor, Edit, Editor, FontSystem,
Style, Weight, Wrap,
Shaping, Style, Weight, Wrap,
};

pub struct SyntaxSystem {
Expand Down Expand Up @@ -75,7 +75,9 @@ impl<'a> SyntaxEditor<'a> {
let path = path.as_ref();

let text = fs::read_to_string(path)?;
self.editor.buffer_mut().set_text(font_system, &text, attrs);
self.editor
.buffer_mut()
.set_text(font_system, &text, attrs, Shaping::Advanced);

//TODO: re-use text
self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! point, you can use the `SwashCache` to rasterize glyphs into either images or pixels.
//!
//! ```
//! use cosmic_text::{Attrs, Color, FontSystem, SwashCache, Buffer, Metrics};
//! use cosmic_text::{Attrs, Color, FontSystem, SwashCache, Buffer, Metrics, Shaping};
//!
//! // A FontSystem provides access to detected system fonts, create one per application
//! let mut font_system = FontSystem::new();
Expand All @@ -36,7 +36,7 @@
//! let attrs = Attrs::new();
//!
//! // Add some text!
//! buffer.set_text("Hello, Rust! 🦀\n", attrs);
//! buffer.set_text("Hello, Rust! 🦀\n", attrs, Shaping::Advanced);
//!
//! // Perform shaping as desired
//! buffer.shape_until_scroll();
Expand Down
Loading

0 comments on commit 9062cce

Please sign in to comment.