[web] Improve perf and code size of line breaker (flutter/engine#19787)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -27,39 +27,8 @@ class LineBreakResult {
|
||||
final LineBreakType type;
|
||||
}
|
||||
|
||||
/// Normalizes properties that behave the same way into one common property.
|
||||
LineCharProperty _normalizeLineProperty(int? codePoint) {
|
||||
if (codePoint == null) {
|
||||
return LineCharProperty.AL;
|
||||
}
|
||||
|
||||
final LineCharProperty? prop = lineLookup.findForChar(codePoint);
|
||||
// NL behaves exactly the same as BK.
|
||||
// See: https://www.unicode.org/reports/tr14/tr14-45.html#NL
|
||||
if (prop == LineCharProperty.NL) {
|
||||
return LineCharProperty.BK;
|
||||
}
|
||||
// In the absence of extra data (ICU data and language dictionaries), the
|
||||
// following properties will be treated as AL (alphabetic): AI, SA, SG and XX.
|
||||
// See LB1: https://www.unicode.org/reports/tr14/tr14-45.html#LB1
|
||||
if (prop == null ||
|
||||
prop == LineCharProperty.AI ||
|
||||
prop == LineCharProperty.SA ||
|
||||
prop == LineCharProperty.SG ||
|
||||
prop == LineCharProperty.XX) {
|
||||
return LineCharProperty.AL;
|
||||
}
|
||||
|
||||
if (prop == LineCharProperty.CJ) {
|
||||
return LineCharProperty.NS;
|
||||
}
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
bool _isHardBreak(LineCharProperty? prop) {
|
||||
// No need to check for NL because it should've been normalized to BK already
|
||||
// by [_normalizeLineProperty].
|
||||
// No need to check for NL because it's already normalized to BK.
|
||||
return prop == LineCharProperty.LF || prop == LineCharProperty.BK;
|
||||
}
|
||||
|
||||
@@ -98,7 +67,7 @@ bool _hasEastAsianWidthFWH(int charCode) {
|
||||
/// * https://www.unicode.org/Public/11.0.0/ucd/LineBreak.txt
|
||||
LineBreakResult nextLineBreak(String text, int index) {
|
||||
int? codePoint = getCodePoint(text, index);
|
||||
LineCharProperty curr = _normalizeLineProperty(codePoint);
|
||||
LineCharProperty curr = lineLookup.findForChar(codePoint);
|
||||
|
||||
LineCharProperty? prev1;
|
||||
|
||||
@@ -153,7 +122,7 @@ LineBreakResult nextLineBreak(String text, int index) {
|
||||
}
|
||||
|
||||
codePoint = getCodePoint(text, index);
|
||||
curr = _normalizeLineProperty(codePoint);
|
||||
curr = lineLookup.findForChar(codePoint);
|
||||
|
||||
isCurrZWJ = curr == LineCharProperty.ZWJ;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ const double _baselineRatioHack = 1.1662499904632568;
|
||||
typedef CharPredicate = bool Function(int char);
|
||||
|
||||
bool _whitespacePredicate(int char) {
|
||||
final LineCharProperty? prop = _normalizeLineProperty(char);
|
||||
final LineCharProperty prop = lineLookup.findForChar(char);
|
||||
return prop == LineCharProperty.SP ||
|
||||
prop == LineCharProperty.BK ||
|
||||
prop == LineCharProperty.LF ||
|
||||
@@ -24,7 +24,7 @@ bool _whitespacePredicate(int char) {
|
||||
}
|
||||
|
||||
bool _newlinePredicate(int char) {
|
||||
final LineCharProperty? prop = _normalizeLineProperty(char);
|
||||
final LineCharProperty prop = lineLookup.findForChar(char);
|
||||
return prop == LineCharProperty.BK || prop == LineCharProperty.LF || prop == LineCharProperty.CR;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,37 +107,48 @@ int? getCodePoint(String text, int index) {
|
||||
/// has. The properties are then used to decide word boundaries, line break
|
||||
/// opportunities, etc.
|
||||
class UnicodePropertyLookup<P> {
|
||||
const UnicodePropertyLookup(this.ranges);
|
||||
const UnicodePropertyLookup(this.ranges, this.defaultProperty);
|
||||
|
||||
/// Creates a [UnicodePropertyLookup] from packed line break data.
|
||||
factory UnicodePropertyLookup.fromPackedData(
|
||||
String packedData,
|
||||
int singleRangesCount,
|
||||
List<P> propertyEnumValues,
|
||||
P defaultProperty,
|
||||
) {
|
||||
return UnicodePropertyLookup<P>(
|
||||
_unpackProperties<P>(packedData, singleRangesCount, propertyEnumValues),
|
||||
defaultProperty,
|
||||
);
|
||||
}
|
||||
|
||||
/// The list of unicode ranges and their associated properties.
|
||||
final List<UnicodeRange<P>> ranges;
|
||||
|
||||
/// The default property to use when a character doesn't belong in any
|
||||
/// known range.
|
||||
final P defaultProperty;
|
||||
|
||||
/// Take a [text] and an [index], and returns the property of the character
|
||||
/// located at that [index].
|
||||
///
|
||||
/// If the [index] is out of range, null will be returned.
|
||||
P? find(String text, int index) {
|
||||
P find(String text, int index) {
|
||||
final int? codePoint = getCodePoint(text, index);
|
||||
return codePoint == null ? null : findForChar(codePoint);
|
||||
return codePoint == null ? defaultProperty : findForChar(codePoint);
|
||||
}
|
||||
|
||||
/// Takes one character as an integer code unit and returns its property.
|
||||
///
|
||||
/// If a property can't be found for the given character, null will be
|
||||
/// returned.
|
||||
P? findForChar(int char) {
|
||||
/// If a property can't be found for the given character, then the default
|
||||
/// property will be returned.
|
||||
P findForChar(int? char) {
|
||||
if (char == null) {
|
||||
return defaultProperty;
|
||||
}
|
||||
|
||||
final int rangeIndex = _binarySearch(char);
|
||||
return rangeIndex == -1 ? null : ranges[rangeIndex].property;
|
||||
return rangeIndex == -1 ? defaultProperty : ranges[rangeIndex].property;
|
||||
}
|
||||
|
||||
int _binarySearch(int value) {
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
// # Unicode and the Unicode Logo are registered trademarks of Unicode, Inc. in the U.S. and other countries.
|
||||
// # For terms of use, see http://www.unicode.org/terms_of_use.html
|
||||
|
||||
|
||||
part of engine;
|
||||
|
||||
/// For an explanation of these enum values, see:
|
||||
@@ -37,6 +36,7 @@ enum WordCharProperty {
|
||||
ExtendNumLet, // serialized as "P"
|
||||
ZWJ, // serialized as "Q"
|
||||
WSegSpace, // serialized as "R"
|
||||
Unknown, // serialized as "S"
|
||||
}
|
||||
|
||||
const String _packedWordBreakProperties =
|
||||
@@ -48,4 +48,5 @@ UnicodePropertyLookup<WordCharProperty> wordLookup =
|
||||
_packedWordBreakProperties,
|
||||
231,
|
||||
WordCharProperty.values,
|
||||
WordCharProperty.Unknown,
|
||||
);
|
||||
|
||||
@@ -29,6 +29,25 @@ final ArgParser argParser = ArgParser()
|
||||
'The output is printed to the console.',
|
||||
);
|
||||
|
||||
/// A map of properties that could safely be normalized into other properties.
|
||||
///
|
||||
/// For example, a NL behaves exactly the same as BK so it gets normalized to BK
|
||||
/// in the generated code.
|
||||
const Map<String, String> normalizationTable = <String, String>{
|
||||
// NL behaves exactly the same as BK.
|
||||
// See: https://www.unicode.org/reports/tr14/tr14-45.html#NL
|
||||
'NL': 'BK',
|
||||
// In the absence of extra data (ICU data and language dictionaries), the
|
||||
// following properties will be treated as AL (alphabetic): AI, SA, SG and XX.
|
||||
// See LB1: https://www.unicode.org/reports/tr14/tr14-45.html#LB1
|
||||
'AI': 'AL',
|
||||
'SA': 'AL',
|
||||
'SG': 'AL',
|
||||
'XX': 'AL',
|
||||
// https://unicode.org/reports/tr14/tr14-45.html#CJ
|
||||
'CJ': 'NS',
|
||||
};
|
||||
|
||||
/// A tuple that holds a [start] and [end] of a unicode range and a [property].
|
||||
class UnicodeRange {
|
||||
const UnicodeRange(this.start, this.end, this.property);
|
||||
@@ -155,10 +174,15 @@ abstract class PropertiesSyncer {
|
||||
String get prefix;
|
||||
String get enumDocLink;
|
||||
|
||||
/// The default property to be used when a certain code point doesn't belong
|
||||
/// to any known range.
|
||||
String get defaultProperty;
|
||||
|
||||
void perform() async {
|
||||
final List<String> lines = await File(_src).readAsLines();
|
||||
final List<String> header = extractHeader(lines);
|
||||
final PropertyCollection data = PropertyCollection.fromLines(lines);
|
||||
final PropertyCollection data =
|
||||
PropertyCollection.fromLines(lines, defaultProperty);
|
||||
|
||||
final String output = template(header, data);
|
||||
|
||||
@@ -182,7 +206,6 @@ abstract class PropertiesSyncer {
|
||||
// Source:
|
||||
// ${header.join('\n// ')}
|
||||
|
||||
// @dart = 2.6
|
||||
part of engine;
|
||||
|
||||
/// For an explanation of these enum values, see:
|
||||
@@ -201,14 +224,18 @@ UnicodePropertyLookup<${prefix}CharProperty> ${prefix.toLowerCase()}Lookup =
|
||||
_packed${prefix}BreakProperties,
|
||||
${_getSingleRangesCount(data)},
|
||||
${prefix}CharProperty.values,
|
||||
${prefix}CharProperty.${defaultProperty},
|
||||
);
|
||||
''';
|
||||
}
|
||||
|
||||
Iterable<String> _getEnumValues(EnumCollection enumCollection) {
|
||||
return enumCollection.values.map(
|
||||
(EnumValue value) =>
|
||||
'${value.enumName}, // serialized as "${value.serialized}"',
|
||||
return enumCollection.values.expand(
|
||||
(EnumValue value) => <String>[
|
||||
if (value.normalizedFrom.isNotEmpty)
|
||||
'// Normalized from: ${value.normalizedFrom.join(', ')}',
|
||||
'${value.enumName}, // serialized as "${value.serialized}"',
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -242,9 +269,15 @@ class WordBreakPropertiesSyncer extends PropertiesSyncer {
|
||||
WordBreakPropertiesSyncer(String src, String dest) : super(src, dest);
|
||||
WordBreakPropertiesSyncer.dry(String src) : super.dry(src);
|
||||
|
||||
@override
|
||||
final String prefix = 'Word';
|
||||
|
||||
@override
|
||||
final String enumDocLink =
|
||||
'http://unicode.org/reports/tr29/#Table_Word_Break_Property_Values';
|
||||
|
||||
@override
|
||||
final String defaultProperty = 'Unknown';
|
||||
}
|
||||
|
||||
/// Syncs Unicode's line break properties.
|
||||
@@ -252,20 +285,34 @@ class LineBreakPropertiesSyncer extends PropertiesSyncer {
|
||||
LineBreakPropertiesSyncer(String src, String dest) : super(src, dest);
|
||||
LineBreakPropertiesSyncer.dry(String src) : super.dry(src);
|
||||
|
||||
@override
|
||||
final String prefix = 'Line';
|
||||
|
||||
@override
|
||||
final String enumDocLink =
|
||||
'https://www.unicode.org/reports/tr14/tr14-45.html#DescriptionOfProperties';
|
||||
|
||||
@override
|
||||
final String defaultProperty = 'AL';
|
||||
}
|
||||
|
||||
/// Holds the collection of properties parsed from the unicode spec file.
|
||||
class PropertyCollection {
|
||||
PropertyCollection.fromLines(List<String> lines) {
|
||||
PropertyCollection.fromLines(List<String> lines, String defaultProperty) {
|
||||
final List<UnicodeRange> unprocessedRanges = lines
|
||||
.map(removeCommentFromLine)
|
||||
.where((String line) => line.isNotEmpty)
|
||||
.map(parseLineIntoUnicodeRange)
|
||||
.toList();
|
||||
ranges = processRanges(unprocessedRanges);
|
||||
// Insert the default property if it doesn't exist.
|
||||
final EnumValue found = enumCollection.values.firstWhere(
|
||||
(property) => property.name == defaultProperty,
|
||||
orElse: () => null,
|
||||
);
|
||||
if (found == null) {
|
||||
enumCollection.add(defaultProperty);
|
||||
}
|
||||
ranges = processRanges(unprocessedRanges, defaultProperty);
|
||||
}
|
||||
|
||||
List<UnicodeRange> ranges;
|
||||
@@ -288,10 +335,14 @@ class PropertyCollection {
|
||||
final String rangeStr = split[0].trim();
|
||||
final String propertyStr = split[1].trim();
|
||||
|
||||
final EnumValue property = normalizationTable.containsKey(propertyStr)
|
||||
? enumCollection.add(normalizationTable[propertyStr], propertyStr)
|
||||
: enumCollection.add(propertyStr);
|
||||
|
||||
return UnicodeRange(
|
||||
getRangeStart(rangeStr),
|
||||
getRangeEnd(rangeStr),
|
||||
enumCollection.add(propertyStr),
|
||||
property,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -300,7 +351,7 @@ class PropertyCollection {
|
||||
class EnumCollection {
|
||||
final List<EnumValue> values = <EnumValue>[];
|
||||
|
||||
EnumValue add(String name) {
|
||||
EnumValue add(String name, [String normalizedFrom]) {
|
||||
final int index =
|
||||
values.indexWhere((EnumValue value) => value.name == name);
|
||||
EnumValue value;
|
||||
@@ -310,6 +361,10 @@ class EnumCollection {
|
||||
} else {
|
||||
value = values[index];
|
||||
}
|
||||
|
||||
if (normalizedFrom != null) {
|
||||
value.normalizedFrom.add(normalizedFrom);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -321,6 +376,9 @@ class EnumValue {
|
||||
final int index;
|
||||
final String name;
|
||||
|
||||
/// The properties that were normalized to this value.
|
||||
final Set<String> normalizedFrom = <String>{};
|
||||
|
||||
/// Returns a serialized, compact format of the enum value.
|
||||
///
|
||||
/// Enum values are serialized based on their index. We start serializing them
|
||||
@@ -351,7 +409,10 @@ class EnumValue {
|
||||
|
||||
/// Sorts ranges and combines adjacent ranges that have the same property and
|
||||
/// can be merged.
|
||||
Iterable<UnicodeRange> processRanges(List<UnicodeRange> data) {
|
||||
Iterable<UnicodeRange> processRanges(
|
||||
List<UnicodeRange> data,
|
||||
String defaultProperty,
|
||||
) {
|
||||
data.sort(
|
||||
// Ranges don't overlap so it's safe to sort based on the start of each
|
||||
// range.
|
||||
@@ -359,7 +420,7 @@ Iterable<UnicodeRange> processRanges(List<UnicodeRange> data) {
|
||||
range1.start.compareTo(range2.start),
|
||||
);
|
||||
verifyNoOverlappingRanges(data);
|
||||
return combineAdjacentRanges(data);
|
||||
return combineAdjacentRanges(data, defaultProperty);
|
||||
}
|
||||
|
||||
/// Example:
|
||||
@@ -375,13 +436,24 @@ Iterable<UnicodeRange> processRanges(List<UnicodeRange> data) {
|
||||
/// ```
|
||||
/// 0x01C4..0x02AF; ALetter
|
||||
/// ```
|
||||
List<UnicodeRange> combineAdjacentRanges(List<UnicodeRange> data) {
|
||||
List<UnicodeRange> combineAdjacentRanges(
|
||||
List<UnicodeRange> data,
|
||||
String defaultProperty,
|
||||
) {
|
||||
final List<UnicodeRange> result = <UnicodeRange>[data.first];
|
||||
for (int i = 1; i < data.length; i++) {
|
||||
if (result.last.isAdjacent(data[i])) {
|
||||
result.last = result.last.extendRange(data[i]);
|
||||
final UnicodeRange prev = result.last;
|
||||
final UnicodeRange next = data[i];
|
||||
if (prev.isAdjacent(next)) {
|
||||
result.last = prev.extendRange(next);
|
||||
} else if (prev.property == next.property &&
|
||||
prev.property.name == defaultProperty) {
|
||||
// When there's a gap between two ranges, but they both have the default
|
||||
// property, it's safe to combine them.
|
||||
result.last = prev.extendRange(next);
|
||||
} else {
|
||||
result.add(data[i]);
|
||||
// Check if there's a gap between the previous range and this range.
|
||||
result.add(next);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user