Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[web] Calculate align offset for each paragraph line (LineMetrics.left)" #14569

Merged
merged 1 commit into from
Dec 19, 2019
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
4 changes: 2 additions & 2 deletions lib/web_ui/lib/src/engine/bitmap_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking {
double x,
double y,
) {
x += line.left;
final double letterSpacing = style.letterSpacing;
if (letterSpacing == null || letterSpacing == 0.0) {
ctx.fillText(line.text, x, y);
Expand Down Expand Up @@ -713,10 +712,11 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking {
}
_applyPaint(paragraph._paint.paintData);

final double x = offset.dx + paragraph._alignOffset;
double y = offset.dy + paragraph.alphabeticBaseline;
final int len = lines.length;
for (int i = 0; i < len; i++) {
_drawTextLine(style, lines[i], offset.dx, y);
_drawTextLine(style, lines[i], x, y);
y += paragraph._lineHeight;
}
_resetPaint();
Expand Down
72 changes: 8 additions & 64 deletions lib/web_ui/lib/src/engine/text/measurement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,13 @@ class DomTextMeasurementService extends TextMeasurementService {
List<EngineLineMetrics> lines;
if (text != null) {
final double lineWidth = maxIntrinsicWidth;
final double alignOffset = _calculateAlignOffsetForLine(
paragraph: paragraph,
lineWidth: lineWidth,
maxWidth: width,
);
lines = <EngineLineMetrics>[
EngineLineMetrics.withText(
text,
startIndex: 0,
endIndex: text.length,
hardBreak: true,
width: lineWidth,
left: alignOffset,
lineNumber: 0,
),
];
Expand All @@ -446,8 +440,6 @@ class DomTextMeasurementService extends TextMeasurementService {
alphabeticBaseline: alphabeticBaseline,
ideographicBaseline: ideographicBaseline,
lines: lines,
textAlign: paragraph._textAlign,
textDirection: paragraph._textDirection,
);
}

Expand Down Expand Up @@ -496,8 +488,6 @@ class DomTextMeasurementService extends TextMeasurementService {
alphabeticBaseline: alphabeticBaseline,
ideographicBaseline: ideographicBaseline,
lines: null,
textAlign: paragraph._textAlign,
textDirection: paragraph._textDirection,
);
}

Expand Down Expand Up @@ -556,7 +546,7 @@ class CanvasTextMeasurementService extends TextMeasurementService {
// TODO(mdebbar): Check if the whole text can fit in a single-line. Then avoid all this ceremony.
_canvasContext.font = style.cssFontString;
final LinesCalculator linesCalculator =
LinesCalculator(_canvasContext, paragraph, constraints.width);
LinesCalculator(_canvasContext, text, style, constraints.width);
final MinIntrinsicCalculator minIntrinsicCalculator =
MinIntrinsicCalculator(_canvasContext, text, style);
final MaxIntrinsicCalculator maxIntrinsicCalculator =
Expand Down Expand Up @@ -607,8 +597,6 @@ class CanvasTextMeasurementService extends TextMeasurementService {
maxIntrinsicWidth: maxIntrinsicCalculator.value,
width: constraints.width,
lines: linesCalculator.lines,
textAlign: paragraph._textAlign,
textDirection: paragraph._textDirection,
);
return result;
}
Expand Down Expand Up @@ -714,18 +702,16 @@ int _excludeTrailing(String text, int start, int end, CharPredicate predicate) {
/// During the text layout phase, this class splits the lines of text so that it
/// ends up fitting into the given width constraint.
///
/// It implements the Flutter engine's behavior when it comes to handling
/// ellipsis and max lines.
/// It mimicks the Flutter engine's behavior when it comes to handling ellipsis
/// and max lines.
class LinesCalculator {
LinesCalculator(this._canvasContext, this._paragraph, this._maxWidth);
LinesCalculator(this._canvasContext, this._text, this._style, this._maxWidth);

final html.CanvasRenderingContext2D _canvasContext;
final EngineParagraph _paragraph;
final String _text;
final ParagraphGeometricStyle _style;
final double _maxWidth;

String get _text => _paragraph._plainText;
ParagraphGeometricStyle get _style => _paragraph._geometricStyle;

/// The lines that have been consumed so far.
List<EngineLineMetrics> lines = <EngineLineMetrics>[];

Expand Down Expand Up @@ -782,20 +768,12 @@ class LinesCalculator {
start: _lineStart,
end: chunkEndWithoutSpace,
);
final double widthOfResultingLine =
measureSubstring(_lineStart, breakingPoint) + _ellipsisWidth;
final double alignOffset = _calculateAlignOffsetForLine(
paragraph: _paragraph,
lineWidth: widthOfResultingLine,
maxWidth: _maxWidth,
);
lines.add(EngineLineMetrics.withText(
_text.substring(_lineStart, breakingPoint) + _style.ellipsis,
startIndex: _lineStart,
endIndex: chunkEnd,
hardBreak: false,
width: widthOfResultingLine,
left: alignOffset,
width: measureSubstring(_lineStart, breakingPoint) + _ellipsisWidth,
lineNumber: lines.length,
));
} else if (isChunkTooLong) {
Expand Down Expand Up @@ -848,19 +826,12 @@ class LinesCalculator {
_whitespacePredicate,
);
final int lineNumber = lines.length;
final double lineWidth = measureSubstring(_lineStart, endWithoutSpace);
final double alignOffset = _calculateAlignOffsetForLine(
paragraph: _paragraph,
lineWidth: lineWidth,
maxWidth: _maxWidth,
);
final EngineLineMetrics metrics = EngineLineMetrics.withText(
_text.substring(_lineStart, endWithoutNewlines),
startIndex: _lineStart,
endIndex: lineEnd,
hardBreak: isHardBreak,
width: lineWidth,
left: alignOffset,
width: measureSubstring(_lineStart, endWithoutSpace),
lineNumber: lineNumber,
);
lines.add(metrics);
Expand Down Expand Up @@ -987,30 +958,3 @@ class MaxIntrinsicCalculator {
_lastHardLineEnd = hardLineEnd;
}
}

/// Calculates the offset necessary for the given line to be correctly aligned.
double _calculateAlignOffsetForLine({
@required EngineParagraph paragraph,
@required double lineWidth,
@required double maxWidth,
}) {
final double emptySpace = maxWidth - lineWidth;
// WARNING: the [paragraph] may not be laid out yet at this point. This
// function must not use layout metrics, such as [paragraph.height].
switch (paragraph._textAlign) {
case ui.TextAlign.center:
return emptySpace / 2.0;
case ui.TextAlign.right:
return emptySpace;
case ui.TextAlign.start:
return paragraph._textDirection == ui.TextDirection.rtl
? emptySpace
: 0.0;
case ui.TextAlign.end:
return paragraph._textDirection == ui.TextDirection.rtl
? 0.0
: emptySpace;
default:
return 0.0;
}
}
3 changes: 1 addition & 2 deletions lib/web_ui/lib/src/engine/text/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ class EngineLineMetrics implements ui.LineMetrics {
this.unscaledAscent,
this.height,
@required this.width,
@required this.left,
this.left,
this.baseline,
@required this.lineNumber,
}) : assert(text != null),
assert(hardBreak != null),
assert(width != null),
assert(left != null),
assert(lineNumber != null && lineNumber >= 0);

/// The textual content representing this line.
Expand Down
18 changes: 3 additions & 15 deletions lib/web_ui/lib/src/engine/text/ruler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,7 @@ class ParagraphRuler {
final int len = constraintCache.length;
for (int i = 0; i < len; i++) {
final MeasurementResult item = constraintCache[i];
if (item.constraintWidth == constraints.width &&
item.textAlign == paragraph._textAlign &&
item.textDirection == paragraph._textDirection) {
if (item.constraintWidth == constraints.width) {
return item;
}
}
Expand Down Expand Up @@ -854,13 +852,7 @@ class MeasurementResult {
/// of each laid out line.
final List<EngineLineMetrics> lines;

/// The text align value of the paragraph.
final ui.TextAlign textAlign;

/// The text direction of the paragraph.
final ui.TextDirection textDirection;

MeasurementResult(
const MeasurementResult(
this.constraintWidth, {
@required this.isSingleLine,
@required this.width,
Expand All @@ -872,8 +864,6 @@ class MeasurementResult {
@required this.alphabeticBaseline,
@required this.ideographicBaseline,
@required this.lines,
@required this.textAlign,
@required this.textDirection,
}) : assert(constraintWidth != null),
assert(isSingleLine != null),
assert(width != null),
Expand All @@ -882,7 +872,5 @@ class MeasurementResult {
assert(minIntrinsicWidth != null),
assert(maxIntrinsicWidth != null),
assert(alphabeticBaseline != null),
assert(ideographicBaseline != null),
assert(textAlign != null),
assert(textDirection != null);
assert(ideographicBaseline != null);
}
Loading