Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Disable text rounding hack by default #44544

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3026,11 +3026,14 @@ abstract class ParagraphBuilder {
///
/// Do not rely on this getter as it exists for migration purposes only and
/// will soon be removed.
@Deprecated('''
The shouldDisableRoundingHack flag is for internal migration purposes only and should not be used.
''')
static bool get shouldDisableRoundingHack {
return const bool.hasEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK')
return const bool.fromEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK', defaultValue: true)
|| _roundingHackDisabledInDebugMode;
}
static bool _roundingHackDisabledInDebugMode = false;
static bool _roundingHackDisabledInDebugMode = true;

/// Only works in debug mode. Do not call this method as it is for migration
/// purposes only and will soon be removed.
Expand Down
4 changes: 2 additions & 2 deletions lib/web_ui/lib/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ abstract class ParagraphBuilder {
engine.renderer.createParagraphBuilder(style);

static bool get shouldDisableRoundingHack {
return const bool.hasEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK')
return const bool.fromEnvironment('SKPARAGRAPH_REMOVE_ROUNDING_HACK', defaultValue: true)
|| _roundingHackDisabledInDebugMode;
}
static bool _roundingHackDisabledInDebugMode = false;
static bool _roundingHackDisabledInDebugMode = true;
static void setDisableRoundingHack(bool disableRoundingHack) {
assert(() {
_roundingHackDisabledInDebugMode = disableRoundingHack;
Expand Down
6 changes: 3 additions & 3 deletions lib/web_ui/test/canvaskit/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ void testMain() {
ui.ParagraphBuilder.setDisableRoundingHack(roundingHackWasDisabled);
});

test('rounding hack applied by default', () {
test('rounding hack disabled by default', () {
const double fontSize = 1.25;
const String text = '12345';
assert((fontSize * text.length).truncate() != fontSize * text.length);
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isFalse);
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isTrue);
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'));
builder.addText(text);
final ui.Paragraph paragraph = builder.build()
..layout(const ui.ParagraphConstraints(width: text.length * fontSize));
expect(paragraph.computeLineMetrics().length, greaterThan(1));
expect(paragraph.computeLineMetrics().length, 1);
});

// TODO(hterkelsen): https://github.com/flutter/flutter/issues/71520
Expand Down
14 changes: 13 additions & 1 deletion lib/web_ui/test/html/text/canvas_paragraph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,19 @@ Future<void> testMain() async {
expect(paragraph.longestLine, 50.0);
});

test('$CanvasParagraph.width should be a whole integer', () {
test('$CanvasParagraph.width should be a whole integer when shouldDisableRoundingHack is false', () {
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
// Try applying the rounding hack if it's disabled. This may not work if
// the 'SKPARAGRAPH_REMOVE_ROUNDING_HACK' dart environment declaration
// is set to 'false'.
ui.ParagraphBuilder.setDisableRoundingHack(false);
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true));
}
// The paragraph width is only rounded to a whole integer if
// shouldDisableRoundingHack is false.
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
return;
}
final ui.Paragraph paragraph = plain(ahemStyle, 'abc');
paragraph.layout(const ui.ParagraphConstraints(width: 30.8));

Expand Down
8 changes: 5 additions & 3 deletions testing/dart/paragraph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ void main() {
const double fontSize = 1.25;
const String text = '12345';
assert((fontSize * text.length).truncate() != fontSize * text.length);
// ignore: deprecated_member_use
final bool roundingHackWasDisabled = ParagraphBuilder.shouldDisableRoundingHack;
ParagraphBuilder.setDisableRoundingHack(true);
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize));
Expand All @@ -263,15 +264,16 @@ void main() {
ParagraphBuilder.setDisableRoundingHack(roundingHackWasDisabled);
});

test('rounding hack applied by default', () {
test('rounding hack disabled by default', () {
const double fontSize = 1.25;
const String text = '12345';
assert((fontSize * text.length).truncate() != fontSize * text.length);
expect(ParagraphBuilder.shouldDisableRoundingHack, isFalse);
// ignore: deprecated_member_use
expect(ParagraphBuilder.shouldDisableRoundingHack, isTrue);
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize));
builder.addText(text);
final Paragraph paragraph = builder.build()
..layout(const ParagraphConstraints(width: text.length * fontSize));
expect(paragraph.computeLineMetrics().length, greaterThan(1));
expect(paragraph.computeLineMetrics().length, 1);
});
}