@@ -9,70 +9,68 @@ pub fn output_css(ctx: &mut Context, css: &CssProperties) -> String {
9
9
let name_table = & ctx. name_table ;
10
10
11
11
// fontData.preferredFamily 不使用这个,因为这个容易引起歧义
12
- let font_family: String = css. font_family . clone ( ) . unwrap_or (
12
+ let font_family = css. font_family . clone ( ) . unwrap_or_else ( || {
13
13
name_table
14
14
. get_name_first ( "FontFamilyName" )
15
- . unwrap_or ( "default_font_family" . to_string ( ) ) ,
16
- ) ;
15
+ . unwrap_or ( "default_font_family" . to_string ( ) )
16
+ } ) ;
17
17
18
18
// 优先使用preferredSubFamily,如果没有,则使用fontSubFamily或fontSubfamily。
19
+ // 提取字体样式
19
20
let preferred_sub_family =
20
- name_table. get_name_first ( "FontSubfamilyName" ) . unwrap_or (
21
- name_table. get_name_first ( "FullFontName" ) . unwrap_or ( "" . to_string ( ) ) ,
22
- ) ;
23
- let font_style =
24
- css . font_style . clone ( ) . unwrap_or ( if is_italic ( & preferred_sub_family) {
21
+ name_table. get_name_first ( "FontSubfamilyName" ) . unwrap_or_else ( || {
22
+ name_table. get_name_first ( "FullFontName" ) . unwrap_or ( "" . to_string ( ) )
23
+ } ) ;
24
+ let font_style = css . font_style . clone ( ) . unwrap_or_else ( || {
25
+ if is_italic ( & preferred_sub_family) {
25
26
"italic" . to_string ( )
26
27
} else {
27
28
"normal" . to_string ( )
28
- } ) ;
29
+ }
30
+ } ) ;
29
31
30
- let font_weight = css. font_weight . clone ( ) . unwrap_or (
32
+ // 提取字体权重
33
+ let font_weight = css. font_weight . clone ( ) . unwrap_or_else ( || {
31
34
ctx. fvar_table
32
35
. clone ( )
33
36
. map ( |x| x. vf_weight )
34
- . unwrap_or ( get_weight ( & preferred_sub_family) . to_string ( ) ) ,
35
- ) ;
37
+ . unwrap_or ( get_weight ( & preferred_sub_family) . to_string ( ) )
38
+ } ) ;
36
39
37
- // 创建本地字体声明字符串。
40
+ // 生成本地字体声明
38
41
let locals = if css. local_family . len ( ) == 0 {
39
42
vec ! [ font_family. clone( ) ]
40
43
} else {
41
44
css. local_family . clone ( )
42
45
} ;
43
46
let locals = locals
44
47
. iter ( )
45
- . map ( |x| format ! ( "local(\" {x}\" )" ) . clone ( ) )
48
+ . map ( |x| format ! ( "local(\" {x}\" )" ) )
46
49
. collect :: < Vec < String > > ( ) ;
47
50
51
+ // 生成 polyfill 字符串
48
52
let polyfill_str = css
49
53
. polyfill
50
54
. iter ( )
51
- . map ( |p| {
52
- format ! (
53
- "url(\" {}\" ) {}" ,
54
- p. name,
55
- format!( "format(\" {}\" )" , p. format)
56
- )
57
- } )
55
+ . map ( |p| format ! ( r#"url("{}") format("{}")"# , p. name, p. format) )
58
56
. collect :: < Vec < String > > ( )
59
57
. join ( "," ) ;
60
58
61
59
let display = css. font_display . clone ( ) . unwrap_or ( "swap" . to_string ( ) ) ;
62
- let codes: Vec < String > = ctx
60
+ // 生成 @font-face 规则
61
+ let codes = ctx
63
62
. run_subset_result
64
63
. iter ( )
65
64
. rev ( )
66
65
. map ( |res| {
67
- let src_str: String = [
66
+ let src_str = [
68
67
locals. join ( "," ) ,
69
- format ! ( r#"url("./{}")format("woff2")"# , res. file_name. clone ( ) ) ,
68
+ format ! ( r#"url("./{}")format("woff2")"# , res. file_name) ,
70
69
]
71
70
. join ( "," )
72
- + polyfill_str. as_str ( ) ;
73
- let unicode_range = & UnicodeRange :: stringify ( & res. unicodes ) ;
74
- let space =
75
- if css. compress . unwrap_or ( true ) == true { "" } else { " " } ;
71
+ + & polyfill_str;
72
+ let unicode_range = UnicodeRange :: stringify ( & res. unicodes ) ;
73
+ let space = if css. compress . unwrap_or ( true ) { "" } else { " " } ;
76
74
let face_code = format ! (
77
75
r#"@font-face{{
78
76
{space}font-family:"{font_family}";
@@ -83,24 +81,20 @@ pub fn output_css(ctx: &mut Context, css: &CssProperties) -> String {
83
81
{space}unicode-range:{unicode_range};
84
82
}}"#
85
83
) ;
86
- // css 这个句尾不需要分号😭
87
- // 根据注释设置生成Unicode范围的注释。
88
84
let comment = if css. comment_unicodes . unwrap_or ( false ) {
89
- let code_string = vec_u32_to_string ( & res. unicodes ) ;
90
- format ! ( "/* {} */\n " , code_string)
85
+ format ! ( "/* {} */\n " , vec_u32_to_string( & res. unicodes) )
91
86
} else {
92
87
"" . to_string ( )
93
88
} ;
94
- // 根据压缩选项返回压缩或未压缩的样式字符串。
95
-
96
89
let compressed = if css. compress . unwrap_or ( true ) {
97
90
face_code. replace ( "\n " , "" )
98
91
} else {
99
92
face_code
100
93
} ;
101
94
comment + & compressed
102
95
} )
103
- . collect ( ) ;
96
+ . collect :: < Vec < String > > ( ) ;
97
+
104
98
ctx. reporter . css = Some ( output_report:: Css {
105
99
family : font_family. clone ( ) ,
106
100
style : font_style. clone ( ) ,
@@ -110,26 +104,24 @@ pub fn output_css(ctx: &mut Context, css: &CssProperties) -> String {
110
104
111
105
let header_comment = create_header_comment ( ctx, css) ;
112
106
113
- header_comment + & codes. join ( "\n " )
107
+ header_comment + & codes. join ( "" )
114
108
}
115
109
116
110
fn create_header_comment (
117
111
ctx : & mut Context < ' _ , ' _ , ' _ > ,
118
112
css : & CssProperties ,
119
113
) -> String {
120
- // ctx.input.css.and_then(|x|x.comment_base)
121
- let mut comment = String :: from ( "" ) ;
114
+ let mut comment = String :: new ( ) ;
122
115
123
116
if css. comment_base . unwrap_or ( true ) {
124
- let utc: DateTime < Utc > = Utc :: now ( ) ;
125
- let base_comment = format ! ( "Generated By cn-font-split@{} https://www.npmjs.com/package/cn-font-split\n CreateTime: {};" ,
117
+ let utc = Utc :: now ( ) ;
118
+ comment. push_str ( & format ! (
119
+ "Generated By cn-font-split@{} https://www.npmjs.com/package/cn-font-split\n CreateTime: {};\n " ,
126
120
ctx. reporter. version,
127
- utc. to_string( )
128
- ) ;
129
- comment. push_str ( & base_comment) ;
121
+ utc. to_rfc3339( )
122
+ ) ) ;
130
123
}
131
124
132
- // name table 的转换
133
125
if css. comment_name_table . unwrap_or ( true ) {
134
126
let name_table_comment = ctx
135
127
. name_table
@@ -140,15 +132,20 @@ fn create_header_comment(
140
132
} )
141
133
. collect :: < Vec < String > > ( )
142
134
. join ( "\n " ) ;
143
-
144
- comment. push_str ( "\n " ) ;
145
135
comment. push_str ( & name_table_comment) ;
146
136
}
147
137
148
- if comment. len ( ) == 0 {
149
- return String :: from ( "" ) ;
138
+ if comment. is_empty ( ) {
139
+ return String :: new ( ) ;
150
140
}
151
- "/* " . to_owned ( ) + & comment + "\n */\n \n "
141
+ format ! ( "/* {}\n */\n \n " , comment. trim( ) )
142
+ }
143
+
144
+ pub fn get_weight ( sub_family : & str ) -> u32 {
145
+ let sub_family = sub_family. to_ascii_lowercase ( ) ;
146
+ FONT_WEIGHT_NAME
147
+ . binary_search_by_key ( & sub_family. as_str ( ) , |& ( name, _) | name)
148
+ . map_or ( 600 , |idx| FONT_WEIGHT_NAME [ idx] . 1 )
152
149
}
153
150
154
151
/** 判断是否为斜体 */
@@ -173,13 +170,3 @@ const FONT_WEIGHT_NAME: [(&str, u32); 15] = [
173
170
( "heavy" , 900 ) ,
174
171
( "black" , 900 ) ,
175
172
] ;
176
-
177
- pub fn get_weight ( sub_family : & str ) -> u32 {
178
- let sub_family = sub_family. to_ascii_lowercase ( ) ;
179
- for ( name, weight) in FONT_WEIGHT_NAME {
180
- if sub_family. contains ( name) {
181
- return weight;
182
- }
183
- }
184
- 600
185
- }
0 commit comments