Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +666,7 @@ 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 @@ -712,11 +713,10 @@ 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], x, y);
_drawTextLine(style, lines[i], offset.dx, y);
y += paragraph._lineHeight;
}
_resetPaint();
Expand Down
69 changes: 60 additions & 9 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,26 @@ 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,
),
];
}

return MeasurementResult(
return MeasurementResult.forParagraph(
paragraph,
constraints.width,
isSingleLine: true,
width: width,
Expand Down Expand Up @@ -476,7 +483,8 @@ class DomTextMeasurementService extends TextMeasurementService {
_applySubPixelRoundingHack(minIntrinsicWidth, maxIntrinsicWidth);
assert(minIntrinsicWidth <= maxIntrinsicWidth);
final double ideographicBaseline = alphabeticBaseline * _baselineRatioHack;
return MeasurementResult(
return MeasurementResult.forParagraph(
paragraph,
constraints.width,
isSingleLine: false,
width: width,
Expand Down Expand Up @@ -546,7 +554,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, text, style, constraints.width);
LinesCalculator(_canvasContext, paragraph, constraints.width);
final MinIntrinsicCalculator minIntrinsicCalculator =
MinIntrinsicCalculator(_canvasContext, text, style);
final MaxIntrinsicCalculator maxIntrinsicCalculator =
Expand Down Expand Up @@ -581,7 +589,8 @@ class CanvasTextMeasurementService extends TextMeasurementService {
? naturalHeight
: math.min(lineCount, style.maxLines) * lineHeight;

final MeasurementResult result = MeasurementResult(
final MeasurementResult result = MeasurementResult.forParagraph(
paragraph,
constraints.width,
isSingleLine: lineCount == 1,
alphabeticBaseline: ruler.alphabeticBaseline,
Expand Down Expand Up @@ -705,13 +714,15 @@ int _excludeTrailing(String text, int start, int end, CharPredicate predicate) {
/// It mimicks the Flutter engine's behavior when it comes to handling ellipsis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: mimicks or implements? :)

/// and max lines.
class LinesCalculator {
LinesCalculator(this._canvasContext, this._text, this._style, this._maxWidth);
LinesCalculator(this._canvasContext, this._paragraph, this._maxWidth);

final html.CanvasRenderingContext2D _canvasContext;
final String _text;
final ParagraphGeometricStyle _style;
final EngineParagraph _paragraph;
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 @@ -768,12 +779,20 @@ 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: measureSubstring(_lineStart, breakingPoint) + _ellipsisWidth,
width: widthOfResultingLine,
left: alignOffset,
lineNumber: lines.length,
));
} else if (isChunkTooLong) {
Expand Down Expand Up @@ -826,12 +845,19 @@ 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: measureSubstring(_lineStart, endWithoutSpace),
width: lineWidth,
left: alignOffset,
lineNumber: lineNumber,
);
lines.add(metrics);
Expand Down Expand Up @@ -958,3 +984,28 @@ class MaxIntrinsicCalculator {
_lastHardLineEnd = hardLineEnd;
}
}

double calculateAlignOffsetForLine({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs?

@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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be useful to know why it matters for this function? E.g. you can add "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: 2 additions & 1 deletion lib/web_ui/lib/src/engine/text/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ class EngineLineMetrics implements ui.LineMetrics {
this.unscaledAscent,
this.height,
@required this.width,
this.left,
@required 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
17 changes: 14 additions & 3 deletions lib/web_ui/lib/src/engine/text/ruler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,9 @@ class ParagraphRuler {
final int len = constraintCache.length;
for (int i = 0; i < len; i++) {
final MeasurementResult item = constraintCache[i];
if (item.constraintWidth == constraints.width) {
if (item.constraintWidth == constraints.width &&
item.textAlign == paragraph._textAlign &&
item.textDirection == paragraph._textDirection) {
return item;
}
}
Expand Down Expand Up @@ -852,7 +854,14 @@ class MeasurementResult {
/// of each laid out line.
final List<EngineLineMetrics> lines;

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

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

MeasurementResult.forParagraph(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted this constructor.

EngineParagraph paragraph,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we deconstruct the paragraph prior to calling the constructor? Otherwise, this API creates a temptation to store the paragraph in the MeasurementResult object, which could be very unsafe. At any moment the framework can draw the paragraph, lay it out again with different constraints, then draw it again, resulting in previous MeasurementResult objects holding onto a stale paragraph object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other APIs use the from nomenclature to indicate that the object is constructed based on another object but does not hold onto it (e.g. List.from), but to fully satisfy my paranoia I think I wouldn't use the paragraph at all :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll pass the textDirection and textAlign values individually.

this.constraintWidth, {
@required this.isSingleLine,
@required this.width,
Expand All @@ -872,5 +881,7 @@ class MeasurementResult {
assert(minIntrinsicWidth != null),
assert(maxIntrinsicWidth != null),
assert(alphabeticBaseline != null),
assert(ideographicBaseline != null);
assert(ideographicBaseline != null),
textAlign = paragraph._textAlign,
textDirection = paragraph._textDirection;
}
Loading