update rich_text example with set_rich_text
This commit is contained in:
parent
4a05c9c1cf
commit
0d9173c05b
1 changed files with 70 additions and 95 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
use cosmic_text::{
|
use cosmic_text::{
|
||||||
Action, Attrs, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family, FontSystem, Metrics,
|
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Shaping, Style,
|
||||||
Shaping, Style, SwashCache, Weight,
|
SwashCache, Weight,
|
||||||
};
|
};
|
||||||
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
||||||
use std::{
|
use std::{
|
||||||
|
|
@ -36,8 +36,7 @@ fn main() {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut editor = Editor::new(Buffer::new(
|
let mut editor = Editor::new(Buffer::new_empty(
|
||||||
&mut font_system,
|
|
||||||
Metrics::new(32.0, 44.0).scale(display_scale),
|
Metrics::new(32.0, 44.0).scale(display_scale),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
@ -52,99 +51,75 @@ fn main() {
|
||||||
let mono_attrs = attrs.family(Family::Monospace);
|
let mono_attrs = attrs.family(Family::Monospace);
|
||||||
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
|
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
|
||||||
|
|
||||||
editor.buffer_mut().lines.clear();
|
let spans: &[(&str, Attrs)] = &[
|
||||||
|
("B", attrs.weight(Weight::BOLD)),
|
||||||
let lines: &[&[(&str, Attrs)]] = &[
|
("old ", attrs),
|
||||||
&[
|
("I", attrs.style(Style::Italic)),
|
||||||
("B", attrs.weight(Weight::BOLD)),
|
("talic ", attrs),
|
||||||
("old ", attrs),
|
("f", attrs),
|
||||||
("I", attrs.style(Style::Italic)),
|
("i ", attrs),
|
||||||
("talic ", attrs),
|
("f", attrs.weight(Weight::BOLD)),
|
||||||
("f", attrs),
|
("i ", attrs),
|
||||||
("i ", attrs),
|
("f", attrs.style(Style::Italic)),
|
||||||
("f", attrs.weight(Weight::BOLD)),
|
("i \n", attrs),
|
||||||
("i ", attrs),
|
("Sans-Serif Normal ", attrs),
|
||||||
("f", attrs.style(Style::Italic)),
|
("Sans-Serif Bold ", attrs.weight(Weight::BOLD)),
|
||||||
("i ", attrs),
|
("Sans-Serif Italic ", attrs.style(Style::Italic)),
|
||||||
],
|
(
|
||||||
&[
|
"Sans-Serif Bold Italic\n",
|
||||||
("Sans-Serif Normal ", attrs),
|
attrs.weight(Weight::BOLD).style(Style::Italic),
|
||||||
("Sans-Serif Bold ", attrs.weight(Weight::BOLD)),
|
),
|
||||||
("Sans-Serif Italic ", attrs.style(Style::Italic)),
|
("Serif Normal ", serif_attrs),
|
||||||
(
|
("Serif Bold ", serif_attrs.weight(Weight::BOLD)),
|
||||||
"Sans-Serif Bold Italic",
|
("Serif Italic ", serif_attrs.style(Style::Italic)),
|
||||||
attrs.weight(Weight::BOLD).style(Style::Italic),
|
(
|
||||||
),
|
"Serif Bold Italic\n",
|
||||||
],
|
serif_attrs.weight(Weight::BOLD).style(Style::Italic),
|
||||||
&[
|
),
|
||||||
("Serif Normal ", serif_attrs),
|
("Mono Normal ", mono_attrs),
|
||||||
("Serif Bold ", serif_attrs.weight(Weight::BOLD)),
|
("Mono Bold ", mono_attrs.weight(Weight::BOLD)),
|
||||||
("Serif Italic ", serif_attrs.style(Style::Italic)),
|
("Mono Italic ", mono_attrs.style(Style::Italic)),
|
||||||
(
|
(
|
||||||
"Serif Bold Italic",
|
"Mono Bold Italic\n",
|
||||||
serif_attrs.weight(Weight::BOLD).style(Style::Italic),
|
mono_attrs.weight(Weight::BOLD).style(Style::Italic),
|
||||||
),
|
),
|
||||||
],
|
("Comic Normal ", comic_attrs),
|
||||||
&[
|
("Comic Bold ", comic_attrs.weight(Weight::BOLD)),
|
||||||
("Mono Normal ", mono_attrs),
|
("Comic Italic ", comic_attrs.style(Style::Italic)),
|
||||||
("Mono Bold ", mono_attrs.weight(Weight::BOLD)),
|
(
|
||||||
("Mono Italic ", mono_attrs.style(Style::Italic)),
|
"Comic Bold Italic\n",
|
||||||
(
|
comic_attrs.weight(Weight::BOLD).style(Style::Italic),
|
||||||
"Mono Bold Italic",
|
),
|
||||||
mono_attrs.weight(Weight::BOLD).style(Style::Italic),
|
("R", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
||||||
),
|
("A", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
||||||
],
|
("I", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
|
||||||
&[
|
("N", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
|
||||||
("Comic Normal ", comic_attrs),
|
("B", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
|
||||||
("Comic Bold ", comic_attrs.weight(Weight::BOLD)),
|
("O", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
|
||||||
("Comic Italic ", comic_attrs.style(Style::Italic)),
|
("W ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
|
||||||
(
|
("Red ", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
||||||
"Comic Bold Italic",
|
("Orange ", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
||||||
comic_attrs.weight(Weight::BOLD).style(Style::Italic),
|
("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))),
|
||||||
("R", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
("Violet ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
|
||||||
("A", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
("U", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
|
||||||
("I", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
|
("N", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
|
||||||
("N", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
|
("I", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
|
||||||
("B", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
|
("C", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
|
||||||
("O", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
|
("O", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
|
||||||
("W ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
|
("R", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
||||||
("Red ", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
("N\n", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
||||||
("Orange ", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
(
|
||||||
("Yellow ", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
|
"生活,삶,जिंदगी 😀 FPS\n",
|
||||||
("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))),
|
|
||||||
("U", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
|
|
||||||
("N", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
|
|
||||||
("I", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
|
|
||||||
("C", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
|
|
||||||
("O", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
|
|
||||||
("R", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
|
|
||||||
("N", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
|
|
||||||
],
|
|
||||||
&[(
|
|
||||||
"生活,삶,जिंदगी 😀 FPS",
|
|
||||||
attrs.color(Color::rgb(0xFF, 0x00, 0x00)),
|
attrs.color(Color::rgb(0xFF, 0x00, 0x00)),
|
||||||
)],
|
),
|
||||||
];
|
];
|
||||||
for &line in lines {
|
|
||||||
let mut line_text = String::new();
|
editor
|
||||||
let mut attrs_list = AttrsList::new(attrs);
|
.buffer_mut()
|
||||||
for &(text, attrs) in line {
|
.set_rich_text(spans.iter().copied(), Shaping::Advanced);
|
||||||
let start = line_text.len();
|
|
||||||
line_text.push_str(text);
|
|
||||||
let end = line_text.len();
|
|
||||||
attrs_list.add_span(start..end, attrs);
|
|
||||||
}
|
|
||||||
editor
|
|
||||||
.buffer_mut()
|
|
||||||
.lines
|
|
||||||
.push(BufferLine::new(line_text, attrs_list, Shaping::Advanced));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut swash_cache = SwashCache::new();
|
let mut swash_cache = SwashCache::new();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue