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

Commit bbdbc3a

Browse files
Reverts "Remove migration flag and unused header files" (#50229)
Reverts #50216 Initiated by: zanderso This change reverts the following previous change: Original Description: The `applyRoundingHack` flag is no longer used by the framework. This also removes the [lib/ui/text/line_metrics.h](https://github.com/flutter/engine/pull/50216/files#diff-9175619f2b114dffef67eba38511b34afe6abefd4f697f4758647133895b34f5) file which doesn't seem to be referenced anywhere. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 89077a0 commit bbdbc3a

File tree

17 files changed

+211
-12
lines changed

17 files changed

+211
-12
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5859,6 +5859,7 @@ ORIGIN: ../../../flutter/lib/ui/text/asset_manager_font_provider.cc + ../../../f
58595859
ORIGIN: ../../../flutter/lib/ui/text/asset_manager_font_provider.h + ../../../flutter/LICENSE
58605860
ORIGIN: ../../../flutter/lib/ui/text/font_collection.cc + ../../../flutter/LICENSE
58615861
ORIGIN: ../../../flutter/lib/ui/text/font_collection.h + ../../../flutter/LICENSE
5862+
ORIGIN: ../../../flutter/lib/ui/text/line_metrics.h + ../../../flutter/LICENSE
58625863
ORIGIN: ../../../flutter/lib/ui/text/paragraph.cc + ../../../flutter/LICENSE
58635864
ORIGIN: ../../../flutter/lib/ui/text/paragraph.h + ../../../flutter/LICENSE
58645865
ORIGIN: ../../../flutter/lib/ui/text/paragraph_builder.cc + ../../../flutter/LICENSE
@@ -8721,6 +8722,7 @@ FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.cc
87218722
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.h
87228723
FILE: ../../../flutter/lib/ui/text/font_collection.cc
87238724
FILE: ../../../flutter/lib/ui/text/font_collection.h
8725+
FILE: ../../../flutter/lib/ui/text/line_metrics.h
87248726
FILE: ../../../flutter/lib/ui/text/paragraph.cc
87258727
FILE: ../../../flutter/lib/ui/text/paragraph.h
87268728
FILE: ../../../flutter/lib/ui/text/paragraph_builder.cc

lib/ui/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ source_set("ui") {
123123
"text/asset_manager_font_provider.h",
124124
"text/font_collection.cc",
125125
"text/font_collection.h",
126+
"text/line_metrics.h",
126127
"text/paragraph.cc",
127128
"text/paragraph.h",
128129
"text/paragraph_builder.cc",

lib/ui/text.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,23 @@ abstract class ParagraphBuilder {
33553355
/// [Paragraph].
33563356
factory ParagraphBuilder(ParagraphStyle style) = _NativeParagraphBuilder;
33573357

3358+
/// Whether the rounding hack enabled by default in SkParagraph and TextPainter
3359+
/// is disabled.
3360+
///
3361+
/// Do not rely on this getter as it exists for migration purposes only and
3362+
/// will soon be removed.
3363+
@Deprecated('''
3364+
The shouldDisableRoundingHack flag is for internal migration purposes only and should not be used.
3365+
''')
3366+
static bool get shouldDisableRoundingHack => _shouldDisableRoundingHack;
3367+
static bool _shouldDisableRoundingHack = true;
3368+
/// Do not call this method as it is for migration purposes only and will soon
3369+
/// be removed.
3370+
// ignore: use_setters_to_change_properties
3371+
static void setDisableRoundingHack(bool disableRoundingHack) {
3372+
_shouldDisableRoundingHack = disableRoundingHack;
3373+
}
3374+
33583375
/// The number of placeholders currently in the paragraph.
33593376
int get placeholderCount;
33603377

@@ -3472,10 +3489,11 @@ base class _NativeParagraphBuilder extends NativeFieldWrapperClass1 implements P
34723489
style._height ?? 0,
34733490
style._ellipsis ?? '',
34743491
_encodeLocale(style._locale),
3492+
!ParagraphBuilder.shouldDisableRoundingHack,
34753493
);
34763494
}
34773495

3478-
@Native<Void Function(Handle, Handle, Handle, Handle, Handle, Double, Double, Handle, Handle)>(symbol: 'ParagraphBuilder::Create')
3496+
@Native<Void Function(Handle, Handle, Handle, Handle, Handle, Double, Double, Handle, Handle, Bool)>(symbol: 'ParagraphBuilder::Create')
34793497
external void _constructor(
34803498
Int32List encoded,
34813499
ByteData? strutData,
@@ -3485,7 +3503,7 @@ base class _NativeParagraphBuilder extends NativeFieldWrapperClass1 implements P
34853503
double height,
34863504
String ellipsis,
34873505
String locale,
3488-
);
3506+
bool applyRoundingHack);
34893507

34903508
@override
34913509
int get placeholderCount => _placeholderCount;

lib/ui/text/line_metrics.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_LIB_UI_TEXT_LINE_METRICS_H_
6+
#define FLUTTER_LIB_UI_TEXT_LINE_METRICS_H_
7+
8+
#include "third_party/dart/runtime/include/dart_api.h"
9+
#include "third_party/tonic/converter/dart_converter.h"
10+
11+
namespace flutter {
12+
13+
struct LineMetrics {
14+
const bool* hard_break;
15+
16+
// The final computed ascent and descent for the line. This can be impacted by
17+
// the strut, height, scaling, as well as outlying runs that are very tall.
18+
//
19+
// The top edge is `baseline - ascent` and the bottom edge is `baseline +
20+
// descent`. Ascent and descent are provided as positive numbers. Raw numbers
21+
// for specific runs of text can be obtained in run_metrics_map. These values
22+
// are the cumulative metrics for the entire line.
23+
const double* ascent;
24+
const double* descent;
25+
const double* unscaled_ascent;
26+
// Height of the line.
27+
const double* height;
28+
// Width of the line.
29+
const double* width;
30+
// The left edge of the line. The right edge can be obtained with `left +
31+
// width`
32+
const double* left;
33+
// The y position of the baseline for this line from the top of the paragraph.
34+
const double* baseline;
35+
// Zero indexed line number.
36+
const size_t* line_number;
37+
38+
LineMetrics();
39+
40+
LineMetrics(const bool* hard_break,
41+
const double* ascent,
42+
const double* descent,
43+
const double* unscaled_ascent,
44+
const double* height,
45+
const double* width,
46+
const double* left,
47+
const double* baseline,
48+
const size_t* line_number)
49+
: hard_break(hard_break),
50+
ascent(ascent),
51+
descent(descent),
52+
unscaled_ascent(unscaled_ascent),
53+
height(height),
54+
width(width),
55+
left(left),
56+
baseline(baseline),
57+
line_number(line_number) {}
58+
};
59+
60+
} // namespace flutter
61+
62+
#endif // FLUTTER_LIB_UI_TEXT_LINE_METRICS_H_

lib/ui/text/paragraph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "flutter/fml/message_loop.h"
99
#include "flutter/lib/ui/dart_wrapper.h"
1010
#include "flutter/lib/ui/painting/canvas.h"
11+
#include "flutter/lib/ui/text/line_metrics.h"
1112
#include "flutter/third_party/txt/src/txt/paragraph.h"
1213

1314
namespace flutter {

lib/ui/text/paragraph_builder.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ void ParagraphBuilder::Create(Dart_Handle wrapper,
151151
double fontSize,
152152
double height,
153153
const std::u16string& ellipsis,
154-
const std::string& locale) {
154+
const std::string& locale,
155+
bool applyRoundingHack) {
155156
UIDartState::ThrowIfUIOperationsProhibited();
156157
auto res = fml::MakeRefCounted<ParagraphBuilder>(
157158
encoded_handle, strutData, fontFamily, strutFontFamilies, fontSize,
158-
height, ellipsis, locale);
159+
height, ellipsis, locale, applyRoundingHack);
159160
res->AssociateWithDartWrapper(wrapper);
160161
}
161162

@@ -230,7 +231,8 @@ ParagraphBuilder::ParagraphBuilder(
230231
double fontSize,
231232
double height,
232233
const std::u16string& ellipsis,
233-
const std::string& locale) {
234+
const std::string& locale,
235+
bool applyRoundingHack) {
234236
int32_t mask = 0;
235237
txt::ParagraphStyle style;
236238
{
@@ -291,6 +293,7 @@ ParagraphBuilder::ParagraphBuilder(
291293
if (mask & kPSLocaleMask) {
292294
style.locale = locale;
293295
}
296+
style.apply_rounding_hack = applyRoundingHack;
294297

295298
FontCollection& font_collection = UIDartState::Current()
296299
->platform_configuration()

lib/ui/text/paragraph_builder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class ParagraphBuilder : public RefCountedDartWrappable<ParagraphBuilder> {
3030
double fontSize,
3131
double height,
3232
const std::u16string& ellipsis,
33-
const std::string& locale);
33+
const std::string& locale,
34+
bool applyRoundingHack);
3435

3536
~ParagraphBuilder() override;
3637

@@ -76,7 +77,8 @@ class ParagraphBuilder : public RefCountedDartWrappable<ParagraphBuilder> {
7677
double fontSize,
7778
double height,
7879
const std::u16string& ellipsis,
79-
const std::string& locale);
80+
const std::string& locale,
81+
bool applyRoundingHack);
8082

8183
std::unique_ptr<txt::ParagraphBuilder> m_paragraph_builder_;
8284
};

lib/web_ui/lib/src/engine/canvaskit/renderer.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ class CanvasKitRenderer implements Renderer {
379379
strutStyle: strutStyle,
380380
ellipsis: ellipsis,
381381
locale: locale,
382+
applyRoundingHack: !ui.ParagraphBuilder.shouldDisableRoundingHack,
382383
);
383384

384385
@override

lib/web_ui/lib/src/engine/canvaskit/text.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
3333
ui.StrutStyle? strutStyle,
3434
String? ellipsis,
3535
ui.Locale? locale,
36+
bool applyRoundingHack = true,
3637
}) : skParagraphStyle = toSkParagraphStyle(
3738
textAlign,
3839
textDirection,
@@ -46,6 +47,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
4647
strutStyle,
4748
ellipsis,
4849
locale,
50+
applyRoundingHack,
4951
),
5052
_textAlign = textAlign,
5153
_textDirection = textDirection,
@@ -161,6 +163,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
161163
ui.StrutStyle? strutStyle,
162164
String? ellipsis,
163165
ui.Locale? locale,
166+
bool applyRoundingHack,
164167
) {
165168
final SkParagraphStyleProperties properties = SkParagraphStyleProperties();
166169

@@ -197,7 +200,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
197200
properties.replaceTabCharacters = true;
198201
properties.textStyle = toSkTextStyleProperties(
199202
fontFamily, fontSize, height, fontWeight, fontStyle);
200-
properties.applyRoundingHack = false;
203+
properties.applyRoundingHack = applyRoundingHack;
201204

202205
return canvasKit.ParagraphStyle(properties);
203206
}

lib/web_ui/lib/src/engine/text/canvas_paragraph.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ class CanvasParagraph implements ui.Paragraph {
8787

8888
@override
8989
void layout(ui.ParagraphConstraints constraints) {
90+
// When constraint width has a decimal place, we floor it to avoid getting
91+
// a layout width that's higher than the constraint width.
92+
//
93+
// For example, if constraint width is `30.8` and the text has a width of
94+
// `30.5` then the TextPainter in the framework will ceil the `30.5` width
95+
// which will result in a width of `40.0` that's higher than the constraint
96+
// width.
97+
if (!ui.ParagraphBuilder.shouldDisableRoundingHack) {
98+
constraints = ui.ParagraphConstraints(width: constraints.width.floorToDouble());
99+
}
100+
90101
if (constraints == _lastUsedConstraints) {
91102
return;
92103
}

lib/web_ui/lib/text.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,13 @@ abstract class ParagraphBuilder {
733733
factory ParagraphBuilder(ParagraphStyle style) =>
734734
engine.renderer.createParagraphBuilder(style);
735735

736+
static bool get shouldDisableRoundingHack => _shouldDisableRoundingHack;
737+
static bool _shouldDisableRoundingHack = true;
738+
// ignore: use_setters_to_change_properties
739+
static void setDisableRoundingHack(bool disableRoundingHack) {
740+
_shouldDisableRoundingHack = disableRoundingHack;
741+
}
742+
736743
void pushStyle(TextStyle style);
737744
void pop();
738745
void addText(String text);

lib/web_ui/test/canvaskit/text_test.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ void testMain() {
204204
expect(bottomRight?.writingDirection, ui.TextDirection.ltr);
205205
});
206206

207-
test('rounding hack disabled', () {
207+
test('rounding hack disabled by default', () {
208+
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isTrue);
209+
208210
const double fontSize = 1.25;
209211
const String text = '12345';
210212
assert((fontSize * text.length).truncate() != fontSize * text.length);
@@ -224,6 +226,32 @@ void testMain() {
224226
}
225227
});
226228

229+
test('setDisableRoundinghHack to false works in tests', () {
230+
bool assertsEnabled = false;
231+
assert(() {
232+
assertsEnabled = true;
233+
return true;
234+
}());
235+
if (!assertsEnabled){
236+
return;
237+
}
238+
239+
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
240+
ui.ParagraphBuilder.setDisableRoundingHack(false);
241+
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true));
242+
}
243+
244+
assert(!ui.ParagraphBuilder.shouldDisableRoundingHack);
245+
const double fontSize = 1.25;
246+
const String text = '12345';
247+
assert((fontSize * text.length).truncate() != fontSize * text.length);
248+
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'));
249+
builder.addText(text);
250+
final ui.Paragraph paragraph = builder.build()
251+
..layout(const ui.ParagraphConstraints(width: text.length * fontSize));
252+
expect(paragraph.computeLineMetrics().length, greaterThan(1));
253+
});
254+
227255
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/71520
228256
}, skip: isSafari || isFirefox);
229257
}

lib/web_ui/test/html/text/canvas_paragraph_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,21 @@ Future<void> testMain() async {
777777
expect(paragraph.longestLine, 50.0);
778778
});
779779

780+
test('$CanvasParagraph.width should be a whole integer when shouldDisableRoundingHack is false', () {
781+
if (ui.ParagraphBuilder.shouldDisableRoundingHack) {
782+
ui.ParagraphBuilder.setDisableRoundingHack(false);
783+
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true));
784+
}
785+
// The paragraph width is only rounded to a whole integer if
786+
// shouldDisableRoundingHack is false.
787+
assert(!ui.ParagraphBuilder.shouldDisableRoundingHack);
788+
final ui.Paragraph paragraph = plain(ahemStyle, 'abc');
789+
paragraph.layout(const ui.ParagraphConstraints(width: 30.8));
790+
791+
expect(paragraph.width, 30);
792+
expect(paragraph.height, 10);
793+
});
794+
780795
test('Render after dispose', () async {
781796
final ui.Paragraph paragraph = plain(ahemStyle, 'abc');
782797
paragraph.layout(const ui.ParagraphConstraints(width: 30.8));

lib/web_ui/test/html/text_test.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ Future<void> testMain() async {
200200
expect(bottomRight?.graphemeClusterLayoutBounds, const Rect.fromLTWH(0.0, 0.0, 10.0, 10.0));
201201
}, skip: domIntl.v8BreakIterator == null); // Intended: Intl.v8breakiterator is needed for correctly breaking grapheme clusters.
202202

203-
test('disable rounding hack', () {
203+
test('Can disable rounding hack', () {
204+
if (!ParagraphBuilder.shouldDisableRoundingHack) {
205+
ParagraphBuilder.setDisableRoundingHack(true);
206+
addTearDown(() => ParagraphBuilder.setDisableRoundingHack(false));
207+
}
208+
assert(ParagraphBuilder.shouldDisableRoundingHack);
204209
const double fontSize = 1;
205210
const String text = '12345';
206211
const double letterSpacing = 0.25;

testing/dart/paragraph_test.dart

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,42 @@ void main() {
333333
}
334334
});
335335

336-
test('rounding hack disabled', () {
336+
test('can set disableRoundingHack to false in tests', () {
337+
bool assertsEnabled = false;
338+
assert(() {
339+
assertsEnabled = true;
340+
return true;
341+
}());
342+
if (!assertsEnabled){
343+
return;
344+
}
345+
const double fontSize = 1.25;
346+
const String text = '12345';
347+
assert((fontSize * text.length).truncate() != fontSize * text.length);
348+
// ignore: deprecated_member_use
349+
final bool roundingHackWasDisabled = ParagraphBuilder.shouldDisableRoundingHack;
350+
if (roundingHackWasDisabled) {
351+
ParagraphBuilder.setDisableRoundingHack(false);
352+
}
353+
// ignore: deprecated_member_use
354+
assert(!ParagraphBuilder.shouldDisableRoundingHack);
355+
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize));
356+
builder.addText(text);
357+
final Paragraph paragraph = builder.build()
358+
..layout(const ParagraphConstraints(width: text.length * fontSize));
359+
expect(paragraph.computeLineMetrics().length, greaterThan(1));
360+
361+
if (roundingHackWasDisabled) {
362+
ParagraphBuilder.setDisableRoundingHack(true);
363+
}
364+
});
365+
366+
test('rounding hack disabled by default', () {
337367
const double fontSize = 1.25;
338368
const String text = '12345';
339369
assert((fontSize * text.length).truncate() != fontSize * text.length);
370+
// ignore: deprecated_member_use
371+
expect(ParagraphBuilder.shouldDisableRoundingHack, isTrue);
340372
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(fontSize: fontSize));
341373
builder.addText(text);
342374
final Paragraph paragraph = builder.build()

third_party/txt/src/skia/paragraph_builder_skia.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ skt::ParagraphStyle ParagraphBuilderSkia::TxtToSkia(const ParagraphStyle& txt) {
139139

140140
skia.turnHintingOff();
141141
skia.setReplaceTabCharacters(true);
142-
skia.setApplyRoundingHack(false);
142+
skia.setApplyRoundingHack(txt.apply_rounding_hack);
143143

144144
return skia;
145145
}

0 commit comments

Comments
 (0)