1+ use std:: borrow:: Cow ;
12use std:: collections:: VecDeque ;
23use std:: convert:: TryFrom ;
34
45use skia_safe:: {
56 font_style:: { Slant , Weight , Width } ,
6- FontMetrics ,
7- FontMgr , FontStyle , typeface :: Typeface ,
7+ typeface :: Typeface ,
8+ FontMetrics , FontMgr , FontStyle ,
89} ;
910
11+ use crate :: common:: context:: drawing_text:: typography:: ParsedFontStyle :: { Italic , Normal , Oblique } ;
1012use crate :: {
11- common:: context:: Device ,
1213 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 ,
1416 common:: utils:: dimensions:: parse_size,
1517} ;
16- use crate :: common:: context:: drawing_text:: typography:: ParsedFontStyle :: { Italic , Normal , Oblique } ;
1718
1819const XX_SMALL : & str = "9px" ;
1920const X_SMALL : & str = "10px" ;
@@ -68,19 +69,19 @@ pub(crate) fn to_real_text_align(align: TextAlign, direction: TextDirection) ->
6869 }
6970}
7071
71- #[ derive( Clone , Debug ) ]
72+ #[ derive( Debug , Clone ) ]
7273pub struct Font {
7374 pub ( crate ) font_details : String ,
7475 pub ( crate ) font : ParsedFont ,
75- skia_value : skia_safe :: Font ,
76+ pub ( crate ) device : Device ,
7677}
7778
7879impl Font {
79- pub fn new ( font_details : & str ) -> Self {
80+ pub fn new ( font_details : & str , device : Device ) -> Self {
8081 Self {
81- font_details : font_details. into ( ) ,
82+ font_details : font_details. to_string ( ) ,
8283 font : parse_font ( font_details) ,
83- skia_value : skia_safe :: Font
84+ device ,
8485 }
8586 }
8687
@@ -99,11 +100,11 @@ impl Font {
99100
100101 self . font_details = font_details. to_string ( ) ;
101102 self . font = parse_font ( font_details) ;
103+ }
102104
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 ( ) ) ;
107108 let mut default_typeface =
108109 Typeface :: from_name ( "sans-serif" , style) . unwrap_or ( Typeface :: default ( ) ) ;
109110 let mgr = FontMgr :: default ( ) ;
@@ -118,14 +119,14 @@ impl Font {
118119 }
119120 }
120121
121- self . skia_value = skia_safe:: Font :: from_typeface (
122+ skia_safe:: Font :: from_typeface (
122123 default_typeface,
123- Some ( parse_size ( font_size . as_ref ( ) , device) ) ,
124- ) ;
124+ Some ( parse_size ( self . font . font_size ( ) , self . device ) ) ,
125+ )
125126 }
126127
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 ( )
129130 }
130131}
131132
@@ -139,6 +140,27 @@ pub struct ParsedFont {
139140 font_family : Option < String > ,
140141}
141142
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+
142164impl Default for ParsedFont {
143165 fn default ( ) -> Self {
144166 Self {
@@ -225,7 +247,13 @@ impl ParsedFontWeight {
225247 }
226248
227249 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 )
229257 }
230258}
231259
@@ -241,7 +269,7 @@ pub fn parse_font(font: &str) -> ParsedFont {
241269 if let Some ( part) = res. pop_front ( ) {
242270 if part. eq ( "normal" ) {
243271 } else if part. eq ( "small-caps" ) {
244- parsed_font. font_variant = part. to_string ( ) ;
272+ parsed_font. font_variant = part. into ( ) ;
245273 } else if ParsedFontStyle :: is_supported ( part) {
246274 match part {
247275 NORMAL => {
@@ -262,15 +290,15 @@ pub fn parse_font(font: &str) -> ParsedFont {
262290 let sizes = part. split ( '/' ) ;
263291 for ( j, size) in sizes. enumerate ( ) {
264292 if j == 0 {
265- parsed_font. font_size = Some ( size. to_string ( ) ) ;
293+ parsed_font. font_size = Some ( size. into ( ) ) ;
266294 }
267295
268296 if j == 1 {
269- parsed_font. line_height = Some ( size. to_string ( ) ) ;
297+ parsed_font. line_height = Some ( size. into ( ) ) ;
270298 }
271299 }
272300 } else {
273- parsed_font. font_family = Some ( part. to_string ( ) ) ;
301+ parsed_font. font_family = Some ( part. into ( ) ) ;
274302 if !res. is_empty ( ) {
275303 let mut current = parsed_font. font_family . unwrap_or_default ( ) ;
276304 for item in res. iter ( ) {
@@ -286,7 +314,7 @@ pub fn parse_font(font: &str) -> ParsedFont {
286314 parsed_font
287315}
288316
289- fn parse_font_family ( value : String ) -> Vec < String > {
317+ fn parse_font_family ( value : & str ) -> Vec < String > {
290318 let mut result = Vec :: new ( ) ;
291319 if value. is_empty ( ) {
292320 return result;
@@ -319,4 +347,3 @@ fn is_size_length(value: &str) -> bool {
319347fn to_font_style ( weight : ParsedFontWeight , style : ParsedFontStyle ) -> FontStyle {
320348 FontStyle :: new ( weight. into_skia ( ) , Width :: NORMAL , style. into_skia ( ) )
321349}
322-
0 commit comments