[flutter_tools] never tree shake 0x20 (space) font codepoints on web (#128302)

Always pass the space code point (`0x20`) to the font subsetter when targetting web because the web engine relies on that character to calculate a font's height.

Fixes #127270
This commit is contained in:
Christopher Fujino
2023-06-06 14:06:15 -07:00
committed by GitHub
parent 73e1f23426
commit 44e7206aa1
3 changed files with 116 additions and 2 deletions

View File

@@ -21,6 +21,8 @@ const String inputPath = '/input/fonts/MaterialIcons-Regular.otf';
const String outputPath = '/output/fonts/MaterialIcons-Regular.otf';
const String relativePath = 'fonts/MaterialIcons-Regular.otf';
final RegExp whitespace = RegExp(r'\s+');
void main() {
late BufferLogger logger;
late MemoryFileSystem fileSystem;
@@ -122,6 +124,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
expect(
@@ -153,6 +156,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
expect(
@@ -176,6 +180,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
expect(
@@ -199,6 +204,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
expect(
@@ -227,6 +233,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
final CompleterIOSink stdinSink = CompleterIOSink();
addConstFinderInvocation(appDill.path, stdout: validConstFinderResult);
@@ -276,6 +283,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
final CompleterIOSink stdinSink = CompleterIOSink();
@@ -308,6 +316,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
final CompleterIOSink stdinSink = CompleterIOSink();
@@ -341,6 +350,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: platform,
);
addConstFinderInvocation(appDill.path, stdout: constFinderResultWithInvalid);
@@ -360,6 +370,97 @@ void main() {
expect(processManager, hasNoRemainingExpectations);
});
}
testWithoutContext('Does not add 0x32 for non-web builds', () async {
final Environment environment = createEnvironment(<String, String>{
kIconTreeShakerFlag: 'true',
kBuildMode: 'release',
});
final File appDill = environment.buildDir.childFile('app.dill')
..createSync(recursive: true);
final IconTreeShaker iconTreeShaker = IconTreeShaker(
environment,
fontManifestContent,
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android_arm64,
);
addConstFinderInvocation(
appDill.path,
// Does not contain space char
stdout: validConstFinderResult,
);
final CompleterIOSink stdinSink = CompleterIOSink();
resetFontSubsetInvocation(stdinSink: stdinSink);
expect(processManager.hasRemainingExpectations, isTrue);
final File inputFont = fileSystem.file(inputPath)
..writeAsBytesSync(List<int>.filled(2500, 0));
fileSystem.file(outputPath)
..createSync(recursive: true)
..writeAsBytesSync(List<int>.filled(1200, 0));
final bool result = await iconTreeShaker.subsetFont(
input: inputFont,
outputPath: outputPath,
relativePath: relativePath,
);
expect(result, isTrue);
final List<String> codePoints = stdinSink.getAndClear().trim().split(whitespace);
expect(codePoints, isNot(contains('32')));
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('Ensures 0x32 is included for web builds', () async {
final Environment environment = createEnvironment(<String, String>{
kIconTreeShakerFlag: 'true',
kBuildMode: 'release',
});
final File appDill = environment.buildDir.childFile('app.dill')
..createSync(recursive: true);
final IconTreeShaker iconTreeShaker = IconTreeShaker(
environment,
fontManifestContent,
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.web_javascript,
);
addConstFinderInvocation(
appDill.path,
// Does not contain space char
stdout: validConstFinderResult,
);
final CompleterIOSink stdinSink = CompleterIOSink();
resetFontSubsetInvocation(stdinSink: stdinSink);
expect(processManager.hasRemainingExpectations, isTrue);
final File inputFont = fileSystem.file(inputPath)
..writeAsBytesSync(List<int>.filled(2500, 0));
fileSystem.file(outputPath)
..createSync(recursive: true)
..writeAsBytesSync(List<int>.filled(1200, 0));
final bool result = await iconTreeShaker.subsetFont(
input: inputFont,
outputPath: outputPath,
relativePath: relativePath,
);
expect(result, isTrue);
final List<String> codePoints = stdinSink.getAndClear().trim().split(whitespace);
expect(codePoints, containsAllInOrder(const <String>['59470', '32']));
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('Non-zero font-subset exit code', () async {
final Environment environment = createEnvironment(<String, String>{
kIconTreeShakerFlag: 'true',
@@ -376,6 +477,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
final CompleterIOSink stdinSink = CompleterIOSink();
@@ -408,6 +510,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
final CompleterIOSink stdinSink = CompleterIOSink(throwOnAdd: true);
@@ -442,6 +545,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
addConstFinderInvocation(appDill.path, stdout: validConstFinderResult);
@@ -474,6 +578,7 @@ void main() {
processManager: processManager,
fileSystem: fileSystem,
artifacts: artifacts,
targetPlatform: TargetPlatform.android,
);
addConstFinderInvocation(appDill.path, exitCode: -1);