4
4
5
5
import 'package:meta/meta.dart' ;
6
6
import 'package:process/process.dart' ;
7
+ import 'package:mime/mime.dart' as mime;
7
8
8
9
import '../../artifacts.dart' ;
9
10
import '../../base/common.dart' ;
@@ -20,7 +21,7 @@ import 'dart.dart';
20
21
const String kIconTreeShakerFlag = 'TreeShakeIcons' ;
21
22
22
23
/// Whether icon font subsetting is enabled by default.
23
- const bool kIconTreeShakerEnabledDefault = false ;
24
+ const bool kIconTreeShakerEnabledDefault = true ;
24
25
25
26
List <Map <String , dynamic >> _getList (dynamic object, String errorMessage) {
26
27
try {
@@ -66,6 +67,12 @@ class IconTreeShaker {
66
67
}
67
68
}
68
69
70
+ /// The MIME type for ttf fonts.
71
+ static const Set <String > kTtfMimeTypes = < String > {
72
+ 'font/ttf' , // based on internet search
73
+ 'application/x-font-ttf' , // based on running locally.
74
+ };
75
+
69
76
/// The [Source] inputs that targets using this should depend on.
70
77
///
71
78
/// See [Target.inputs] .
@@ -77,6 +84,7 @@ class IconTreeShaker {
77
84
78
85
final Environment _environment;
79
86
final String _fontManifest;
87
+ Future <void > _iconDataProcessing;
80
88
Map <String , _IconTreeShakerData > _iconData;
81
89
82
90
final ProcessManager _processManager;
@@ -89,10 +97,10 @@ class IconTreeShaker {
89
97
&& _environment.defines[kIconTreeShakerFlag] == 'true'
90
98
&& _environment.defines[kBuildMode] != 'debug' ;
91
99
92
- /// Fills the [_iconData] map.
93
- Future <Map < String , _IconTreeShakerData > > _getIconData (Environment environment) async {
100
+ // Fills the [_iconData] map.
101
+ Future <void > _getIconData (Environment environment) async {
94
102
if (! enabled) {
95
- return null ;
103
+ return ;
96
104
}
97
105
98
106
final File appDill = environment.buildDir.childFile ('app.dill' );
@@ -135,13 +143,11 @@ class IconTreeShaker {
135
143
codePoints: iconData[entry.key],
136
144
);
137
145
}
138
- return result;
146
+ _iconData = result;
139
147
}
140
148
141
- /// Calls font-subset, which transforms the `inputPath` font file to a
142
- /// subsetted version at `outputPath` .
143
- ///
144
- /// The `relativePath` parameter
149
+ /// Calls font-subset, which transforms the [input] font file to a
150
+ /// subsetted version at [outputPath] .
145
151
///
146
152
/// All parameters are required.
147
153
///
@@ -150,15 +156,24 @@ class IconTreeShaker {
150
156
/// If the font-subset subprocess fails, it will [throwToolExit] .
151
157
/// Otherwise, it will return true.
152
158
Future <bool > subsetFont ({
153
- @required String inputPath ,
159
+ @required File input ,
154
160
@required String outputPath,
155
161
@required String relativePath,
156
162
}) async {
157
163
if (! enabled) {
158
164
return false ;
159
165
}
160
-
161
- _iconData ?? = await _getIconData (_environment);
166
+ if (input.lengthSync () < 12 ) {
167
+ return false ;
168
+ }
169
+ final String mimeType = mime.lookupMimeType (
170
+ input.path,
171
+ headerBytes: await input.openRead (0 , 12 ).first,
172
+ );
173
+ if (! kTtfMimeTypes.contains (mimeType)) {
174
+ return false ;
175
+ }
176
+ await (_iconDataProcessing ?? = _getIconData (_environment));
162
177
assert (_iconData != null );
163
178
164
179
final _IconTreeShakerData iconTreeShakerData = _iconData[relativePath];
@@ -176,7 +191,7 @@ class IconTreeShaker {
176
191
final List <String > cmd = < String > [
177
192
fontSubset.path,
178
193
outputPath,
179
- inputPath ,
194
+ input.path ,
180
195
];
181
196
final String codePoints = iconTreeShakerData.codePoints.join (' ' );
182
197
_logger.printTrace ('Running font-subset: ${cmd .join (' ' )}, '
@@ -186,9 +201,7 @@ class IconTreeShaker {
186
201
fontSubsetProcess.stdin.writeln (codePoints);
187
202
await fontSubsetProcess.stdin.flush ();
188
203
await fontSubsetProcess.stdin.close ();
189
- } on Exception catch (_) {
190
- // handled by checking the exit code.
191
- } on OSError catch (_) { // ignore: dead_code_on_catch_subtype
204
+ } on Exception {
192
205
// handled by checking the exit code.
193
206
}
194
207
0 commit comments