[web] Add new line break type (prohibited) (flutter/engine#22771)

This commit is contained in:
Mouad Debbar
2020-11-30 13:05:10 -08:00
committed by GitHub
parent a2f762549a
commit 482bad112c
3 changed files with 34 additions and 8 deletions

View File

@@ -10,6 +10,9 @@ enum LineBreakType {
/// Indicates that a line break is possible but not mandatory. /// Indicates that a line break is possible but not mandatory.
opportunity, opportunity,
/// Indicates that a line break isn't possible.
prohibited,
/// Indicates that this is a hard line break that can't be skipped. /// Indicates that this is a hard line break that can't be skipped.
mandatory, mandatory,
@@ -74,6 +77,9 @@ class LineBreakResult {
/// to decide whether to take the line break or not. /// to decide whether to take the line break or not.
final LineBreakType type; final LineBreakType type;
bool get isHard =>
type == LineBreakType.mandatory || type == LineBreakType.endOfText;
@override @override
int get hashCode => ui.hashValues( int get hashCode => ui.hashValues(
index, index,
@@ -160,7 +166,7 @@ bool _hasEastAsianWidthFWH(int charCode) {
/// ///
/// * https://www.unicode.org/reports/tr14/tr14-45.html#Algorithm /// * https://www.unicode.org/reports/tr14/tr14-45.html#Algorithm
/// * https://www.unicode.org/Public/11.0.0/ucd/LineBreak.txt /// * https://www.unicode.org/Public/11.0.0/ucd/LineBreak.txt
LineBreakResult nextLineBreak(String text, int index) { LineBreakResult nextLineBreak(String text, int index, {int? maxEnd}) {
int? codePoint = getCodePoint(text, index); int? codePoint = getCodePoint(text, index);
LineCharProperty curr = lineLookup.findForChar(codePoint); LineCharProperty curr = lineLookup.findForChar(codePoint);
@@ -199,6 +205,15 @@ LineBreakResult nextLineBreak(String text, int index) {
// Always break at the end of text. // Always break at the end of text.
// LB3: ! eot // LB3: ! eot
while (index < text.length) { while (index < text.length) {
if (index == maxEnd) {
return LineBreakResult(
index,
lastNonNewlineIndex,
lastNonSpaceIndex,
LineBreakType.prohibited,
);
}
// Keep count of the RI (regional indicator) sequence. // Keep count of the RI (regional indicator) sequence.
if (curr == LineCharProperty.RI) { if (curr == LineCharProperty.RI) {
regionalIndicatorCount++; regionalIndicatorCount++;

View File

@@ -771,8 +771,6 @@ class LinesCalculator {
/// This method should be called for every line break. As soon as it reaches /// This method should be called for every line break. As soon as it reaches
/// the maximum number of lines required /// the maximum number of lines required
void update(LineBreakResult brk) { void update(LineBreakResult brk) {
final bool isHardBreak = brk.type == LineBreakType.mandatory ||
brk.type == LineBreakType.endOfText;
final int chunkEnd = brk.index; final int chunkEnd = brk.index;
final int chunkEndWithoutNewlines = brk.indexWithoutTrailingNewlines; final int chunkEndWithoutNewlines = brk.indexWithoutTrailingNewlines;
final int chunkEndWithoutSpace = brk.indexWithoutTrailingSpaces; final int chunkEndWithoutSpace = brk.indexWithoutTrailingSpaces;
@@ -855,7 +853,7 @@ class LinesCalculator {
return; return;
} }
if (isHardBreak) { if (brk.isHard) {
_addLineBreak(brk); _addLineBreak(brk);
} }
_lastBreak = brk; _lastBreak = brk;
@@ -872,15 +870,13 @@ class LinesCalculator {
lineWidth: lineWidth, lineWidth: lineWidth,
maxWidth: _maxWidth, maxWidth: _maxWidth,
); );
final bool isHardBreak = brk.type == LineBreakType.mandatory ||
brk.type == LineBreakType.endOfText;
final EngineLineMetrics metrics = EngineLineMetrics.withText( final EngineLineMetrics metrics = EngineLineMetrics.withText(
_text!.substring(_lineStart, brk.indexWithoutTrailingNewlines), _text!.substring(_lineStart, brk.indexWithoutTrailingNewlines),
startIndex: _lineStart, startIndex: _lineStart,
endIndex: brk.index, endIndex: brk.index,
endIndexWithoutNewlines: brk.indexWithoutTrailingNewlines, endIndexWithoutNewlines: brk.indexWithoutTrailingNewlines,
hardBreak: isHardBreak, hardBreak: brk.isHard,
width: lineWidth, width: lineWidth,
widthWithTrailingSpaces: lineWidthWithTrailingSpaces, widthWithTrailingSpaces: lineWidthWithTrailingSpaces,
left: alignOffset, left: alignOffset,
@@ -995,7 +991,7 @@ class MaxIntrinsicCalculator {
/// intrinsic width calculated so far. When the whole text is consumed, /// intrinsic width calculated so far. When the whole text is consumed,
/// [value] will contain the final maximum intrinsic width. /// [value] will contain the final maximum intrinsic width.
void update(LineBreakResult brk) { void update(LineBreakResult brk) {
if (brk.type == LineBreakType.opportunity) { if (!brk.isHard) {
return; return;
} }

View File

@@ -252,6 +252,14 @@ void testMain() {
'"$text"\n' '"$text"\n'
'\nExpected line break at {$lastLineBreak - $i} but found line break at {$lastLineBreak - ${result.index}}.', '\nExpected line break at {$lastLineBreak - $i} but found line break at {$lastLineBreak - ${result.index}}.',
); );
// Since this is a line break, passing a `maxEnd` that's greater
// should return the same line break.
final LineBreakResult maxEndResult =
nextLineBreak(text, lastLineBreak, maxEnd: i + 1);
expect(maxEndResult.index, i);
expect(maxEndResult.type, isNot(LineBreakType.prohibited));
lastLineBreak = i; lastLineBreak = i;
} else { } else {
// This isn't a line break opportunity so the line break should be // This isn't a line break opportunity so the line break should be
@@ -264,6 +272,13 @@ void testMain() {
'"$text"\n' '"$text"\n'
'\nUnexpected line break found at {$lastLineBreak - $i}.', '\nUnexpected line break found at {$lastLineBreak - $i}.',
); );
// Since this isn't a line break, passing it as a `maxEnd` should
// return `maxEnd` as a prohibited line break type.
final LineBreakResult maxEndResult =
nextLineBreak(text, lastLineBreak, maxEnd: i);
expect(maxEndResult.index, i);
expect(maxEndResult.type, LineBreakType.prohibited);
} }
} }
} }