@@ -2,7 +2,7 @@ use crate::Result;
22use cosmic_text:: fontdb;
33use std:: path:: PathBuf ;
44
5- pub fn get_fonts_info ( ) -> Result < Vec < ( String , PathBuf ) > > {
5+ pub fn get_fonts_info ( ) -> Result < Vec < ( String , PathBuf , String ) > > {
66 let mut db = fontdb:: Database :: new ( ) ;
77 db. load_system_fonts ( ) ;
88
@@ -13,7 +13,8 @@ pub fn get_fonts_info() -> Result<Vec<(String, PathBuf)>> {
1313 && let fontdb:: Source :: File ( path) = & source
1414 && let Some ( family) = face. families . first ( )
1515 {
16- fonts. push ( ( family. 0 . clone ( ) , path. clone ( ) ) ) ;
16+ let style = format_font_style ( face. weight , face. style ) ;
17+ fonts. push ( ( family. 0 . clone ( ) , path. clone ( ) , style) ) ;
1718 }
1819 }
1920
@@ -23,19 +24,53 @@ pub fn get_fonts_info() -> Result<Vec<(String, PathBuf)>> {
2324 Ok ( fonts)
2425}
2526
26- pub fn get_font_family_from_file ( path : & PathBuf ) -> Result < String > {
27+ fn format_font_style ( weight : fontdb:: Weight , style : fontdb:: Style ) -> String {
28+ let weight_str = match weight {
29+ fontdb:: Weight :: THIN => "Thin" ,
30+ fontdb:: Weight :: EXTRA_LIGHT => "ExtraLight" ,
31+ fontdb:: Weight :: LIGHT => "Light" ,
32+ fontdb:: Weight :: NORMAL => "" ,
33+ fontdb:: Weight :: MEDIUM => "Medium" ,
34+ fontdb:: Weight :: SEMIBOLD => "SemiBold" ,
35+ fontdb:: Weight :: BOLD => "Bold" ,
36+ fontdb:: Weight :: EXTRA_BOLD => "ExtraBold" ,
37+ fontdb:: Weight :: BLACK => "Black" ,
38+ _ => "" ,
39+ } ;
40+
41+ let style_str = match style {
42+ fontdb:: Style :: Normal => "" ,
43+ fontdb:: Style :: Italic => "Italic" ,
44+ fontdb:: Style :: Oblique => "Oblique" ,
45+ } ;
46+
47+ if weight_str. is_empty ( ) && style_str. is_empty ( ) {
48+ "Normal" . to_string ( )
49+ } else if weight_str. is_empty ( ) {
50+ style_str. to_string ( )
51+ } else if style_str. is_empty ( ) {
52+ weight_str. to_string ( )
53+ } else {
54+ format ! ( "{} {}" , weight_str, style_str)
55+ }
56+ }
57+
58+ pub fn get_font_family_from_file ( path : & PathBuf ) -> Result < ( String , String ) > {
2759 let mut db = fontdb:: Database :: new ( ) ;
2860 db. load_font_file ( path. clone ( ) ) ?;
2961
3062 for face in db. faces ( ) {
3163 if let Some ( family) = face. families . first ( ) {
32- return Ok ( family. 0 . clone ( ) ) ;
64+ let style = format_font_style ( face. weight , face. style ) ;
65+ return Ok ( ( family. 0 . clone ( ) , style) ) ;
3366 }
3467 }
3568
36- Ok ( path
37- . file_stem ( )
38- . and_then ( |s| s. to_str ( ) )
39- . unwrap_or ( "Unknown" )
40- . to_string ( ) )
69+ Ok ( (
70+ path. file_stem ( )
71+ . and_then ( |s| s. to_str ( ) )
72+ . unwrap_or ( "Unknown" )
73+ . to_string ( ) ,
74+ "Normal" . to_string ( ) ,
75+ ) )
4176}
0 commit comments