1
1
// SPDX-License-Identifier: MIT OR Apache-2.0
2
2
3
3
use cosmic_text:: {
4
- Action , Attrs , AttrsList , Buffer , BufferLine , Color , Edit , Editor , Family , FontSystem , Metrics ,
5
- Shaping , Style , SwashCache , Weight ,
4
+ Action , Attrs , Buffer , Color , Edit , Editor , Family , FontSystem , Metrics , Shaping , Style ,
5
+ SwashCache , Weight ,
6
6
} ;
7
7
use orbclient:: { EventOption , Renderer , Window , WindowFlag } ;
8
8
use std:: {
@@ -36,8 +36,7 @@ fn main() {
36
36
)
37
37
. unwrap ( ) ;
38
38
39
- let mut editor = Editor :: new ( Buffer :: new (
40
- & mut font_system,
39
+ let mut editor = Editor :: new ( Buffer :: new_empty (
41
40
Metrics :: new ( 32.0 , 44.0 ) . scale ( display_scale) ,
42
41
) ) ;
43
42
@@ -52,99 +51,75 @@ fn main() {
52
51
let mono_attrs = attrs. family ( Family :: Monospace ) ;
53
52
let comic_attrs = attrs. family ( Family :: Name ( "Comic Neue" ) ) ;
54
53
55
- editor. buffer_mut ( ) . lines . clear ( ) ;
56
-
57
- let lines: & [ & [ ( & str , Attrs ) ] ] = & [
58
- & [
59
- ( "B" , attrs. weight ( Weight :: BOLD ) ) ,
60
- ( "old " , attrs) ,
61
- ( "I" , attrs. style ( Style :: Italic ) ) ,
62
- ( "talic " , attrs) ,
63
- ( "f" , attrs) ,
64
- ( "i " , attrs) ,
65
- ( "f" , attrs. weight ( Weight :: BOLD ) ) ,
66
- ( "i " , attrs) ,
67
- ( "f" , attrs. style ( Style :: Italic ) ) ,
68
- ( "i " , attrs) ,
69
- ] ,
70
- & [
71
- ( "Sans-Serif Normal " , attrs) ,
72
- ( "Sans-Serif Bold " , attrs. weight ( Weight :: BOLD ) ) ,
73
- ( "Sans-Serif Italic " , attrs. style ( Style :: Italic ) ) ,
74
- (
75
- "Sans-Serif Bold Italic" ,
76
- attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
77
- ) ,
78
- ] ,
79
- & [
80
- ( "Serif Normal " , serif_attrs) ,
81
- ( "Serif Bold " , serif_attrs. weight ( Weight :: BOLD ) ) ,
82
- ( "Serif Italic " , serif_attrs. style ( Style :: Italic ) ) ,
83
- (
84
- "Serif Bold Italic" ,
85
- serif_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
86
- ) ,
87
- ] ,
88
- & [
89
- ( "Mono Normal " , mono_attrs) ,
90
- ( "Mono Bold " , mono_attrs. weight ( Weight :: BOLD ) ) ,
91
- ( "Mono Italic " , mono_attrs. style ( Style :: Italic ) ) ,
92
- (
93
- "Mono Bold Italic" ,
94
- mono_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
95
- ) ,
96
- ] ,
97
- & [
98
- ( "Comic Normal " , comic_attrs) ,
99
- ( "Comic Bold " , comic_attrs. weight ( Weight :: BOLD ) ) ,
100
- ( "Comic Italic " , comic_attrs. style ( Style :: Italic ) ) ,
101
- (
102
- "Comic Bold Italic" ,
103
- comic_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
104
- ) ,
105
- ] ,
106
- & [
107
- ( "R" , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
108
- ( "A" , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
109
- ( "I" , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
110
- ( "N" , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
111
- ( "B" , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
112
- ( "O" , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
113
- ( "W " , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
114
- ( "Red " , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
115
- ( "Orange " , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
116
- ( "Yellow " , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
117
- ( "Green " , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
118
- ( "Blue " , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
119
- ( "Indigo " , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
120
- ( "Violet " , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
121
- ( "U" , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
122
- ( "N" , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
123
- ( "I" , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
124
- ( "C" , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
125
- ( "O" , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
126
- ( "R" , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
127
- ( "N" , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
128
- ] ,
129
- & [ (
130
- "生活,삶,जिंदगी 😀 FPS" ,
54
+ let spans: & [ ( & str , Attrs ) ] = & [
55
+ ( "B" , attrs. weight ( Weight :: BOLD ) ) ,
56
+ ( "old " , attrs) ,
57
+ ( "I" , attrs. style ( Style :: Italic ) ) ,
58
+ ( "talic " , attrs) ,
59
+ ( "f" , attrs) ,
60
+ ( "i " , attrs) ,
61
+ ( "f" , attrs. weight ( Weight :: BOLD ) ) ,
62
+ ( "i " , attrs) ,
63
+ ( "f" , attrs. style ( Style :: Italic ) ) ,
64
+ ( "i \n " , attrs) ,
65
+ ( "Sans-Serif Normal " , attrs) ,
66
+ ( "Sans-Serif Bold " , attrs. weight ( Weight :: BOLD ) ) ,
67
+ ( "Sans-Serif Italic " , attrs. style ( Style :: Italic ) ) ,
68
+ (
69
+ "Sans-Serif Bold Italic\n " ,
70
+ attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
71
+ ) ,
72
+ ( "Serif Normal " , serif_attrs) ,
73
+ ( "Serif Bold " , serif_attrs. weight ( Weight :: BOLD ) ) ,
74
+ ( "Serif Italic " , serif_attrs. style ( Style :: Italic ) ) ,
75
+ (
76
+ "Serif Bold Italic\n " ,
77
+ serif_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
78
+ ) ,
79
+ ( "Mono Normal " , mono_attrs) ,
80
+ ( "Mono Bold " , mono_attrs. weight ( Weight :: BOLD ) ) ,
81
+ ( "Mono Italic " , mono_attrs. style ( Style :: Italic ) ) ,
82
+ (
83
+ "Mono Bold Italic\n " ,
84
+ mono_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
85
+ ) ,
86
+ ( "Comic Normal " , comic_attrs) ,
87
+ ( "Comic Bold " , comic_attrs. weight ( Weight :: BOLD ) ) ,
88
+ ( "Comic Italic " , comic_attrs. style ( Style :: Italic ) ) ,
89
+ (
90
+ "Comic Bold Italic\n " ,
91
+ comic_attrs. weight ( Weight :: BOLD ) . style ( Style :: Italic ) ,
92
+ ) ,
93
+ ( "R" , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
94
+ ( "A" , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
95
+ ( "I" , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
96
+ ( "N" , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
97
+ ( "B" , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
98
+ ( "O" , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
99
+ ( "W " , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
100
+ ( "Red " , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
101
+ ( "Orange " , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
102
+ ( "Yellow " , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
103
+ ( "Green " , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
104
+ ( "Blue " , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
105
+ ( "Indigo " , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
106
+ ( "Violet " , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
107
+ ( "U" , attrs. color ( Color :: rgb ( 0x94 , 0x00 , 0xD3 ) ) ) ,
108
+ ( "N" , attrs. color ( Color :: rgb ( 0x4B , 0x00 , 0x82 ) ) ) ,
109
+ ( "I" , attrs. color ( Color :: rgb ( 0x00 , 0x00 , 0xFF ) ) ) ,
110
+ ( "C" , attrs. color ( Color :: rgb ( 0x00 , 0xFF , 0x00 ) ) ) ,
111
+ ( "O" , attrs. color ( Color :: rgb ( 0xFF , 0xFF , 0x00 ) ) ) ,
112
+ ( "R" , attrs. color ( Color :: rgb ( 0xFF , 0x7F , 0x00 ) ) ) ,
113
+ ( "N\n " , attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ) ,
114
+ (
115
+ "生活,삶,जिंदगी 😀 FPS\n " ,
131
116
attrs. color ( Color :: rgb ( 0xFF , 0x00 , 0x00 ) ) ,
132
- ) ] ,
117
+ ) ,
133
118
] ;
134
- for & line in lines {
135
- let mut line_text = String :: new ( ) ;
136
- let mut attrs_list = AttrsList :: new ( attrs) ;
137
- for & ( text, attrs) in line {
138
- let start = line_text. len ( ) ;
139
- line_text. push_str ( text) ;
140
- let end = line_text. len ( ) ;
141
- attrs_list. add_span ( start..end, attrs) ;
142
- }
143
- editor
144
- . buffer_mut ( )
145
- . lines
146
- . push ( BufferLine :: new ( line_text, attrs_list, Shaping :: Advanced ) ) ;
147
- }
119
+
120
+ editor
121
+ . buffer_mut ( )
122
+ . set_rich_text ( spans. iter ( ) . copied ( ) , Shaping :: Advanced ) ;
148
123
149
124
let mut swash_cache = SwashCache :: new ( ) ;
150
125
0 commit comments