Skip to content

Commit 6f227c0

Browse files
Space character should be optional when tree shaking fonts (#132880)
Addresses the other part of flutter/flutter#132711
1 parent 6d75704 commit 6f227c0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

packages/flutter_tools/lib/src/build_system/targets/icon_tree_shaker.dart

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ class IconTreeShaker {
138138
if (codePoints == null) {
139139
throw IconTreeShakerException._('Expected to font code points for ${entry.key}, but none were found.');
140140
}
141-
if (_targetPlatform == TargetPlatform.web_javascript) {
142-
if (!codePoints.contains(kSpacePoint)) {
143-
codePoints.add(kSpacePoint);
144-
}
145-
}
141+
142+
// Add space as an optional code point, as web uses it to measure the font height.
143+
final List<int> optionalCodePoints = _targetPlatform == TargetPlatform.web_javascript
144+
? <int>[kSpacePoint] : <int>[];
146145
result[entry.value] = _IconTreeShakerData(
147146
family: entry.key,
148147
relativePath: entry.value,
149148
codePoints: codePoints,
149+
optionalCodePoints: optionalCodePoints,
150150
);
151151
}
152152
_iconData = result;
@@ -197,12 +197,17 @@ class IconTreeShaker {
197197
outputPath,
198198
input.path,
199199
];
200-
final String codePoints = iconTreeShakerData.codePoints.join(' ');
200+
final Iterable<String> requiredCodePointStrings = iconTreeShakerData.codePoints
201+
.map((int codePoint) => codePoint.toString());
202+
final Iterable<String> optionalCodePointStrings = iconTreeShakerData.optionalCodePoints
203+
.map((int codePoint) => 'optional:$codePoint');
204+
final String codePointsString = requiredCodePointStrings
205+
.followedBy(optionalCodePointStrings).join(' ');
201206
_logger.printTrace('Running font-subset: ${cmd.join(' ')}, '
202-
'using codepoints $codePoints');
207+
'using codepoints $codePointsString');
203208
final Process fontSubsetProcess = await _processManager.start(cmd);
204209
try {
205-
fontSubsetProcess.stdin.writeln(codePoints);
210+
fontSubsetProcess.stdin.writeln(codePointsString);
206211
await fontSubsetProcess.stdin.flush();
207212
await fontSubsetProcess.stdin.close();
208213
} on Exception {
@@ -369,6 +374,7 @@ class _IconTreeShakerData {
369374
required this.family,
370375
required this.relativePath,
371376
required this.codePoints,
377+
required this.optionalCodePoints,
372378
});
373379

374380
/// The font family name, e.g. "MaterialIcons".
@@ -380,6 +386,10 @@ class _IconTreeShakerData {
380386
/// The list of code points for the font.
381387
final List<int> codePoints;
382388

389+
/// The list of code points to be optionally added, if they exist in the
390+
/// input font. Otherwise, the tool will silently omit them.
391+
final List<int> optionalCodePoints;
392+
383393
@override
384394
String toString() => 'FontSubsetData($family, $relativePath, $codePoints)';
385395
}

packages/flutter_tools/test/general.shard/build_system/targets/icon_tree_shaker_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void main() {
411411

412412
expect(result, isTrue);
413413
final List<String> codePoints = stdinSink.getAndClear().trim().split(whitespace);
414-
expect(codePoints, isNot(contains('32')));
414+
expect(codePoints, isNot(contains('optional:32')));
415415

416416
expect(processManager, hasNoRemainingExpectations);
417417
});
@@ -456,7 +456,7 @@ void main() {
456456

457457
expect(result, isTrue);
458458
final List<String> codePoints = stdinSink.getAndClear().trim().split(whitespace);
459-
expect(codePoints, containsAllInOrder(const <String>['59470', '32']));
459+
expect(codePoints, containsAllInOrder(const <String>['59470', 'optional:32']));
460460

461461
expect(processManager, hasNoRemainingExpectations);
462462
});

0 commit comments

Comments
 (0)