Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Multiple font size / line height spans (updated) #203

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
68b51cd
move `font_size` into `Attrs`, adjust layout
tigregalis Jun 28, 2023
a3a91ea
temporarily update rich-text example
tigregalis Jun 30, 2023
6af188e
update layout runs with line height
tigregalis Jun 30, 2023
fb7c907
WIP implement hit detection
tigregalis Jul 4, 2023
9a8c533
WIP update editor
tigregalis Jul 4, 2023
346564e
fix scrolling partially
tigregalis Jul 23, 2023
7179d3e
fix scrolling
tigregalis Jul 24, 2023
d5940e3
fix max lines and scroll into view
tigregalis Jul 27, 2023
bd16490
cache line heights
tigregalis Jul 28, 2023
44b6043
remove debugging, dead code
tigregalis Sep 8, 2023
3f774c4
temp fix Editor Action::Delete not redrawing
tigregalis Sep 8, 2023
b1b054e
fix rare case
tigregalis Sep 8, 2023
205313d
add `enum LineHeight`
tigregalis Sep 8, 2023
d30f947
add `Attrs::scale()`
tigregalis Sep 8, 2023
de1284f
add guards for font size and line height
tigregalis Sep 8, 2023
19d3c6d
remove `Metrics`
tigregalis Sep 8, 2023
d8a2892
fix compile errors
tigregalis Sep 8, 2023
6c275ad
remove unused imports in example
tigregalis Oct 2, 2023
64c2b76
add `LineHeight` to `Hash` impl for `Attrs`
tigregalis Oct 2, 2023
0ae11e0
update BufferLine layout_in_buffer to match layout
tigregalis Oct 3, 2023
65334fa
impl BufferLine layout using layout_in_buffer
tigregalis Oct 3, 2023
d8def16
combine BufferLine layout_opt and line_heights
tigregalis Oct 4, 2023
a120183
add missing line_height hash
tigregalis Oct 4, 2023
ef09ab3
add more docs to Buffer
tigregalis Oct 6, 2023
2e82f62
add more docs to buffer
tigregalis Oct 6, 2023
506c384
clamp Buffer::set_scroll
tigregalis Oct 6, 2023
fb81f10
Fix terminal example
nicoburns Nov 15, 2023
ca44fda
Fix editor-libcosmic example
nicoburns Nov 15, 2023
d5d8bd3
Use line_heights method instead of manual computation
nicoburns Nov 15, 2023
c48ccbf
Fix test compilation
nicoburns Nov 15, 2023
a2f6518
Merge branch 'main' into multi-size-fix-examples
nicoburns Nov 17, 2023
4a7dddf
Empty lines get a span attached, and those spans are used to determin…
TotalKrill Dec 9, 2023
1a92c3b
always add spans, even if they are the same as default
TotalKrill Dec 10, 2023
6d3e449
nit: font_size instead of size
TotalKrill Dec 10, 2023
bd31f68
Revert "always add spans, even if they are the same as default"
TotalKrill Dec 10, 2023
d1f8a41
Merge pull request #1 from TotalKrill/multi-size-fix-examples
TotalKrill Dec 11, 2023
53ceba8
Merge remote-tracking branch 'origin/main' into nicoburns-multi-size-…
jackpot51 Dec 11, 2023
25380f8
Fix no_std build
jackpot51 Dec 11, 2023
61c76cf
Merge pull request #2 from TotalKrill/multi-size-fix-examples
TotalKrill Dec 11, 2023
16ff8d3
only add newline spans, if they dont match the defaults
TotalKrill Dec 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions examples/editor-libcosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cosmic::{
Element,
};
use cosmic_text::{
Align, Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
Align, Attrs, AttrsList, Buffer, Edit, FontSystem, LineHeight, SyntaxEditor, SyntaxSystem, Wrap,
};
use std::{env, fmt, fs, path::PathBuf, sync::Mutex};

Expand Down Expand Up @@ -46,16 +46,33 @@ impl FontSize {
]
}

pub fn to_metrics(self) -> Metrics {
pub fn to_font_size(self) -> f32 {
match self {
Self::Caption => Metrics::new(10.0, 14.0), // Caption
Self::Body => Metrics::new(14.0, 20.0), // Body
Self::Title4 => Metrics::new(20.0, 28.0), // Title 4
Self::Title3 => Metrics::new(24.0, 32.0), // Title 3
Self::Title2 => Metrics::new(28.0, 36.0), // Title 2
Self::Title1 => Metrics::new(32.0, 44.0), // Title 1
Self::Caption => 10.0, // Caption
Self::Body => 14.0, // Body
Self::Title4 => 20.0, // Title 4
Self::Title3 => 24.0, // Title 3
Self::Title2 => 28.0, // Title 2
Self::Title1 => 32.0, // Title 1
}
}

pub fn to_line_height(self) -> LineHeight {
match self {
Self::Caption => LineHeight::Absolute(14.0), // Caption
Self::Body => LineHeight::Absolute(20.0), // Body
Self::Title4 => LineHeight::Absolute(28.0), // Title 4
Self::Title3 => LineHeight::Absolute(32.0), // Title 3
Self::Title2 => LineHeight::Absolute(36.0), // Title 2
Self::Title1 => LineHeight::Absolute(44.0), // Title 1
}
}

pub fn to_attrs(self) -> Attrs<'static> {
Attrs::new()
.size(self.to_font_size())
.line_height(self.to_line_height())
}
}

impl fmt::Display for FontSize {
Expand Down Expand Up @@ -131,13 +148,12 @@ impl Application for Window {
type Theme = Theme;

fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
let attrs = FontSize::Body
.to_attrs()
.family(cosmic_text::Family::Monospace);

let mut editor = SyntaxEditor::new(
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
FontSize::Body.to_metrics(),
),
Buffer::new(&mut FONT_SYSTEM.lock().unwrap()),
&SYNTAX_SYSTEM,
"base16-eighties.dark",
)
Expand Down Expand Up @@ -234,11 +250,13 @@ impl Application for Window {
}
Message::FontSizeChanged(font_size) => {
self.font_size = font_size;
self.attrs = self
.attrs
.size(font_size.to_font_size())
.line_height(font_size.to_line_height());

let mut editor = self.editor.lock().unwrap();
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut()
.set_metrics(font_size.to_metrics());
update_attrs(&mut *editor, self.attrs);
}
Message::WrapChanged(wrap) => {
let mut editor = self.editor.lock().unwrap();
Expand Down
20 changes: 6 additions & 14 deletions examples/editor-libcosmic/src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,7 @@ where
.buffer_mut()
.shape_until(i32::max_value());

let mut layout_lines = 0;
for line in editor.buffer().lines.iter() {
match line.layout_opt() {
Some(layout) => layout_lines += layout.len(),
None => (),
}
}

let height = layout_lines as f32 * editor.buffer().metrics().line_height;
let height = editor.buffer().line_heights().iter().sum();
let size = Size::new(limits.max().width, height);
log::info!("size {:?}", size);

Expand Down Expand Up @@ -219,10 +211,10 @@ where
let mut editor = editor.borrow_with(&mut font_system);

// Scale metrics
let metrics = editor.buffer().metrics();
editor
.buffer_mut()
.set_metrics(metrics.scale(SCALE_FACTOR as f32));
// let metrics = editor.buffer().metrics();
// editor
// .buffer_mut()
// .set_metrics(metrics.scale(SCALE_FACTOR as f32));

// Set size
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
Expand All @@ -246,7 +238,7 @@ where
);

// Restore original metrics
editor.buffer_mut().set_metrics(metrics);
// editor.buffer_mut().set_metrics(metrics);

let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
image::Renderer::draw(
Expand Down
67 changes: 55 additions & 12 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Shaping, Style,
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, LineHeight, Shaping, Style,
SwashCache, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
Expand Down Expand Up @@ -36,17 +36,18 @@ fn main() {
)
.unwrap();

let mut editor = Editor::new(Buffer::new_empty(
Metrics::new(32.0, 44.0).scale(display_scale),
));
let mut editor = Editor::new(Buffer::new_empty());

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

editor
.buffer_mut()
.set_size(window.width() as f32, window.height() as f32);

let attrs = Attrs::new();
let attrs = Attrs::new()
.size(32.0)
.line_height(LineHeight::Absolute(44.0))
.scale(display_scale);
let serif_attrs = attrs.family(Family::Serif);
let mono_attrs = attrs.family(Family::Monospace);
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
Expand Down Expand Up @@ -97,13 +98,55 @@ fn main() {
("B", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
("O", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("W ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
("Red ", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
("Orange ", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
("Yellow ", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
("Green ", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
("Blue ", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
("Indigo ", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("Violet ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
(
"Red ",
attrs
.color(Color::rgb(0xFF, 0x00, 0x00))
.size(attrs.font_size * 1.9)
.line_height(LineHeight::Proportional(0.9)),
),
(
"Orange ",
attrs
.color(Color::rgb(0xFF, 0x7F, 0x00))
.size(attrs.font_size * 1.6)
.line_height(LineHeight::Proportional(1.0)),
),
(
"Yellow ",
attrs
.color(Color::rgb(0xFF, 0xFF, 0x00))
.size(attrs.font_size * 1.3)
.line_height(LineHeight::Proportional(1.1)),
),
(
"Green ",
attrs
.color(Color::rgb(0x00, 0xFF, 0x00))
.size(attrs.font_size * 1.0)
.line_height(LineHeight::Proportional(1.2)),
),
(
"Blue ",
attrs
.color(Color::rgb(0x00, 0x00, 0xFF))
.size(attrs.font_size * 0.8)
.line_height(LineHeight::Proportional(1.3)),
),
(
"Indigo ",
attrs
.color(Color::rgb(0x4B, 0x00, 0x82))
.size(attrs.font_size * 0.6)
.line_height(LineHeight::Proportional(1.4)),
),
(
"Violet ",
attrs
.color(Color::rgb(0x94, 0x00, 0xD3))
.size(attrs.font_size * 0.4)
.line_height(LineHeight::Proportional(1.5)),
),
("U", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
("N", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("I", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
Expand Down
10 changes: 6 additions & 4 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, Shaping, SwashCache};
use cosmic_text::{Attrs, Buffer, Color, FontSystem, LineHeight, Shaping, SwashCache};
use std::cmp::{self, Ordering};
use termion::{color, cursor};

Expand All @@ -12,10 +12,10 @@ fn main() {
let mut swash_cache = SwashCache::new();

// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(14.0, 20.0);
// let metrics = Metrics::new(14.0, 20.0);

// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&mut font_system, metrics);
let mut buffer = Buffer::new(&mut font_system);

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

Expand All @@ -25,7 +25,9 @@ fn main() {
buffer.set_size(width as f32, height as f32);

// Attributes indicate what font to choose
let attrs = Attrs::new();
let attrs = Attrs::new()
.size(14.0)
.line_height(LineHeight::Absolute(20.0));

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