Paint tab characters as a single space (flutter/engine#34389)

This change makes Windows render tabs as a single space. Previously, tabs rendered differently depending on your platform:

* Windows, Web - a tofu is rendered
* iOS, Android, macOS, Linux - a single space is rendered

Part of flutter/flutter#79153
This commit is contained in:
Loïc Sharma
2022-07-01 12:51:11 -07:00
committed by GitHub
parent 04d46cad00
commit 4128781401
2 changed files with 40 additions and 0 deletions

View File

@@ -543,6 +543,45 @@ void main() {
}
});
Future<Image> drawText(String text) {
return toImage((Canvas canvas) {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontFamily: 'RobotoSerif',
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
fontSize: 15.0,
));
builder.pushStyle(TextStyle(color: const Color(0xFF0000FF)));
builder.addText(text);
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: 20 * 5.0));
canvas.drawParagraph(paragraph, Offset.zero);
}, 100, 100);
}
test('Canvas.drawParagraph renders tab as space instead of tofu', () async {
// Skia renders a tofu if the font does not have a glyph for a character.
// However, Flutter opts-in to a Skia feature to render tabs as a single space.
// See: https://github.com/flutter/flutter/issues/79153
final File file = File(path.join('flutter', 'testing', 'resources', 'RobotoSlab-VariableFont_wght.ttf'));
final Uint8List fontData = await file.readAsBytes();
await loadFontFromList(fontData, fontFamily: 'RobotoSerif');
// The backspace character, \b, does not have a corresponding glyph and is rendered as a tofu.
final Image tabImage = await drawText('>\t<');
final Image spaceImage = await drawText('> <');
final Image tofuImage = await drawText('>\b<');
// The tab's image should be identical to the space's image but not the tofu's image.
final bool tabToSpaceComparison = await fuzzyCompareImages(tabImage, spaceImage);
final bool tabToTofuComparison = await fuzzyCompareImages(tabImage, tofuImage);
expect(tabToSpaceComparison, isTrue);
expect(tabToTofuComparison, isFalse);
});
Matcher closeToTransform(Float64List expected) => (dynamic v) {
Expect.type<Float64List>(v);
final Float64List value = v;

View File

@@ -79,6 +79,7 @@ skt::ParagraphStyle TxtToSkia(const ParagraphStyle& txt) {
static_cast<skt::TextHeightBehavior>(txt.text_height_behavior));
skia.turnHintingOff();
skia.setReplaceTabCharacters(true);
return skia;
}