1
+ use std:: borrow:: Cow ;
1
2
use std:: collections:: VecDeque ;
2
3
use std:: convert:: TryFrom ;
3
4
4
5
use skia_safe:: {
5
6
font_style:: { Slant , Weight , Width } ,
6
- FontMetrics ,
7
- FontMgr , FontStyle , typeface :: Typeface ,
7
+ typeface :: Typeface ,
8
+ FontMetrics , FontMgr , FontStyle ,
8
9
} ;
9
10
11
+ use crate :: common:: context:: drawing_text:: typography:: ParsedFontStyle :: { Italic , Normal , Oblique } ;
10
12
use crate :: {
11
- common:: context:: Device ,
12
13
common:: context:: text_styles:: text_align:: TextAlign ,
13
- common:: context:: text_styles:: text_baseline:: TextBaseLine , common:: context:: text_styles:: text_direction:: TextDirection ,
14
+ common:: context:: text_styles:: text_baseline:: TextBaseLine ,
15
+ common:: context:: text_styles:: text_direction:: TextDirection , common:: context:: Device ,
14
16
common:: utils:: dimensions:: parse_size,
15
17
} ;
16
- use crate :: common:: context:: drawing_text:: typography:: ParsedFontStyle :: { Italic , Normal , Oblique } ;
17
18
18
19
const XX_SMALL : & str = "9px" ;
19
20
const X_SMALL : & str = "10px" ;
@@ -68,19 +69,19 @@ pub(crate) fn to_real_text_align(align: TextAlign, direction: TextDirection) ->
68
69
}
69
70
}
70
71
71
- #[ derive( Clone , Debug ) ]
72
+ #[ derive( Debug , Clone ) ]
72
73
pub struct Font {
73
74
pub ( crate ) font_details : String ,
74
75
pub ( crate ) font : ParsedFont ,
75
- skia_value : skia_safe :: Font ,
76
+ pub ( crate ) device : Device ,
76
77
}
77
78
78
79
impl Font {
79
- pub fn new ( font_details : & str ) -> Self {
80
+ pub fn new ( font_details : & str , device : Device ) -> Self {
80
81
Self {
81
- font_details : font_details. into ( ) ,
82
+ font_details : font_details. to_string ( ) ,
82
83
font : parse_font ( font_details) ,
83
- skia_value : skia_safe :: Font
84
+ device ,
84
85
}
85
86
}
86
87
@@ -99,11 +100,11 @@ impl Font {
99
100
100
101
self . font_details = font_details. to_string ( ) ;
101
102
self . font = parse_font ( font_details) ;
103
+ }
102
104
103
- let style = to_font_style ( weight. as_ref ( ) , style. as_ref ( ) ) ;
104
-
105
- let mut families: Vec < String > =
106
- parse_font_family ( parsed_font. font_family . unwrap_or ( "sans-serif" . to_string ( ) ) ) ;
105
+ fn to_font ( & self ) -> skia_safe:: Font {
106
+ let style = to_font_style ( self . font . font_weight ( ) , self . font . font_style ( ) ) ;
107
+ let mut families: Vec < String > = parse_font_family ( self . font . font_family ( ) ) ;
107
108
let mut default_typeface =
108
109
Typeface :: from_name ( "sans-serif" , style) . unwrap_or ( Typeface :: default ( ) ) ;
109
110
let mgr = FontMgr :: default ( ) ;
@@ -118,14 +119,14 @@ impl Font {
118
119
}
119
120
}
120
121
121
- self . skia_value = skia_safe:: Font :: from_typeface (
122
+ skia_safe:: Font :: from_typeface (
122
123
default_typeface,
123
- Some ( parse_size ( font_size . as_ref ( ) , device) ) ,
124
- ) ;
124
+ Some ( parse_size ( self . font . font_size ( ) , self . device ) ) ,
125
+ )
125
126
}
126
127
127
- pub fn to_skia ( & self ) -> & skia_safe:: Font {
128
- & self . skia_value
128
+ pub fn to_skia ( & self ) -> skia_safe:: Font {
129
+ self . to_font ( )
129
130
}
130
131
}
131
132
@@ -139,6 +140,27 @@ pub struct ParsedFont {
139
140
font_family : Option < String > ,
140
141
}
141
142
143
+ impl ParsedFont {
144
+ pub fn font_style ( & self ) -> ParsedFontStyle {
145
+ self . font_style
146
+ }
147
+ pub fn font_variant ( & self ) -> & str {
148
+ & self . font_variant
149
+ }
150
+ pub fn font_weight ( & self ) -> ParsedFontWeight {
151
+ self . font_weight
152
+ }
153
+ pub fn line_height ( & self ) -> & str {
154
+ self . line_height . as_ref ( ) . map_or ( "1.4px" , |v| v)
155
+ }
156
+ pub fn font_size ( & self ) -> & str {
157
+ self . font_size . as_ref ( ) . map_or ( "10px" , |v| v)
158
+ }
159
+ pub fn font_family ( & self ) -> & str {
160
+ self . font_family . as_ref ( ) . map_or ( "sans-serif" , |v| v)
161
+ }
162
+ }
163
+
142
164
impl Default for ParsedFont {
143
165
fn default ( ) -> Self {
144
166
Self {
@@ -225,7 +247,13 @@ impl ParsedFontWeight {
225
247
}
226
248
227
249
pub fn into_skia ( & self ) -> Weight {
228
- Weight :: from ( self as i32 )
250
+ ( * self ) . into ( )
251
+ }
252
+ }
253
+
254
+ impl Into < Weight > for ParsedFontWeight {
255
+ fn into ( self ) -> Weight {
256
+ Weight :: from ( * & self as i32 )
229
257
}
230
258
}
231
259
@@ -241,7 +269,7 @@ pub fn parse_font(font: &str) -> ParsedFont {
241
269
if let Some ( part) = res. pop_front ( ) {
242
270
if part. eq ( "normal" ) {
243
271
} else if part. eq ( "small-caps" ) {
244
- parsed_font. font_variant = part. to_string ( ) ;
272
+ parsed_font. font_variant = part. into ( ) ;
245
273
} else if ParsedFontStyle :: is_supported ( part) {
246
274
match part {
247
275
NORMAL => {
@@ -262,15 +290,15 @@ pub fn parse_font(font: &str) -> ParsedFont {
262
290
let sizes = part. split ( '/' ) ;
263
291
for ( j, size) in sizes. enumerate ( ) {
264
292
if j == 0 {
265
- parsed_font. font_size = Some ( size. to_string ( ) ) ;
293
+ parsed_font. font_size = Some ( size. into ( ) ) ;
266
294
}
267
295
268
296
if j == 1 {
269
- parsed_font. line_height = Some ( size. to_string ( ) ) ;
297
+ parsed_font. line_height = Some ( size. into ( ) ) ;
270
298
}
271
299
}
272
300
} else {
273
- parsed_font. font_family = Some ( part. to_string ( ) ) ;
301
+ parsed_font. font_family = Some ( part. into ( ) ) ;
274
302
if !res. is_empty ( ) {
275
303
let mut current = parsed_font. font_family . unwrap_or_default ( ) ;
276
304
for item in res. iter ( ) {
@@ -286,7 +314,7 @@ pub fn parse_font(font: &str) -> ParsedFont {
286
314
parsed_font
287
315
}
288
316
289
- fn parse_font_family ( value : String ) -> Vec < String > {
317
+ fn parse_font_family ( value : & str ) -> Vec < String > {
290
318
let mut result = Vec :: new ( ) ;
291
319
if value. is_empty ( ) {
292
320
return result;
@@ -319,4 +347,3 @@ fn is_size_length(value: &str) -> bool {
319
347
fn to_font_style ( weight : ParsedFontWeight , style : ParsedFontStyle ) -> FontStyle {
320
348
FontStyle :: new ( weight. into_skia ( ) , Width :: NORMAL , style. into_skia ( ) )
321
349
}
322
-
0 commit comments