[web] Reduce code size impact of fallback font data (flutter/engine#38787)
* [web] Reduce code size impact of fallback font data * fix rangeEnd vs rangeLength mistake
This commit is contained in:
@@ -111,9 +111,10 @@ class GenerateFallbackFontDataCommand extends Command<bool>
|
||||
sb.writeln('// dev/generate_fallback_font_data.dart');
|
||||
sb.writeln("import 'noto_font.dart';");
|
||||
sb.writeln();
|
||||
sb.writeln('const List<NotoFont> fallbackFonts = <NotoFont>[');
|
||||
sb.writeln('final List<NotoFont> fallbackFonts = <NotoFont>[');
|
||||
|
||||
for (final String family in fallbackFonts) {
|
||||
sb.write(" NotoFont('$family', '${urlForFamily[family]!}', ");
|
||||
sb.writeln(" NotoFont('$family', '${urlForFamily[family]!}',");
|
||||
final List<String> starts = <String>[];
|
||||
final List<String> ends = <String>[];
|
||||
for (final String range in charsetForFamily[family]!.split(' ')) {
|
||||
@@ -126,15 +127,22 @@ class GenerateFallbackFontDataCommand extends Command<bool>
|
||||
ends.add(parts[1]);
|
||||
}
|
||||
}
|
||||
sb.write('<int>[');
|
||||
|
||||
// Print the unicode ranges in a readable format for easier review. This
|
||||
// shouldn't affect code size because comments are removed in release mode.
|
||||
sb.write(' // <int>[');
|
||||
for (final String start in starts) {
|
||||
sb.write('0x$start,');
|
||||
}
|
||||
sb.write('], <int>[');
|
||||
sb.writeln('],');
|
||||
sb.write(' // <int>[');
|
||||
for (final String end in ends) {
|
||||
sb.write('0x$end,');
|
||||
}
|
||||
sb.writeln(']),');
|
||||
sb.writeln(']');
|
||||
|
||||
sb.writeln(" '${_packFontRanges(starts, ends)}',");
|
||||
sb.writeln(' ),');
|
||||
}
|
||||
sb.writeln('];');
|
||||
|
||||
@@ -293,3 +301,23 @@ const List<String> fallbackFonts = <String>[
|
||||
'Noto Sans Yi',
|
||||
'Noto Sans Zanabazar Square',
|
||||
];
|
||||
|
||||
String _packFontRanges(List<String> starts, List<String> ends) {
|
||||
assert(starts.length == ends.length);
|
||||
|
||||
final StringBuffer sb = StringBuffer();
|
||||
|
||||
for (int i = 0; i < starts.length; i++) {
|
||||
final int start = int.parse(starts[i], radix: 16);
|
||||
final int end = int.parse(ends[i], radix: 16);
|
||||
|
||||
sb.write(start.toRadixString(36));
|
||||
sb.write('|');
|
||||
if (start != end) {
|
||||
sb.write((end - start).toRadixString(36));
|
||||
}
|
||||
sb.write(';');
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2,48 +2,33 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
class NotoFont {
|
||||
const NotoFont(this.name, this.url, this._rangeStarts, this._rangeEnds);
|
||||
import '../text/unicode_range.dart';
|
||||
|
||||
factory NotoFont.fromFlatRanges(String name, String url, List<int> flatRanges) {
|
||||
final List<int> starts = <int>[];
|
||||
final List<int> ends = <int>[];
|
||||
for (int i = 0; i < flatRanges.length; i += 2) {
|
||||
starts.add(flatRanges[i]);
|
||||
ends.add(flatRanges[i+1]);
|
||||
}
|
||||
return NotoFont(name, url, starts, ends);
|
||||
}
|
||||
class NotoFont {
|
||||
NotoFont(this.name, this.url, this._packedRanges);
|
||||
|
||||
final String name;
|
||||
final String url;
|
||||
// A sorted list of Unicode range start points.
|
||||
final List<int> _rangeStarts;
|
||||
final String _packedRanges;
|
||||
// A sorted list of Unicode ranges.
|
||||
late final List<CodeunitRange> _ranges = _unpackFontRange(_packedRanges);
|
||||
|
||||
// A sorted list of Unicode range end points.
|
||||
final List<int> _rangeEnds;
|
||||
|
||||
List<CodeunitRange> computeUnicodeRanges() {
|
||||
final List<CodeunitRange> result = <CodeunitRange>[];
|
||||
for (int i = 0; i < _rangeStarts.length; i++) {
|
||||
result.add(CodeunitRange(_rangeStarts[i], _rangeEnds[i]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
List<CodeunitRange> computeUnicodeRanges() => _ranges;
|
||||
|
||||
// Returns `true` if this font has a glyph for the given [codeunit].
|
||||
bool contains(int codeUnit) {
|
||||
// Binary search through the starts and ends to see if there
|
||||
// Binary search through the unicode ranges to see if there
|
||||
// is a range that contains the codeunit.
|
||||
int min = 0;
|
||||
int max = _rangeStarts.length - 1;
|
||||
int max = _ranges.length - 1;
|
||||
while (min <= max) {
|
||||
final int mid = (min + max) ~/ 2;
|
||||
if (_rangeStarts[mid] > codeUnit) {
|
||||
final CodeunitRange range = _ranges[mid];
|
||||
if (range.start > codeUnit) {
|
||||
max = mid - 1;
|
||||
} else {
|
||||
// _rangeStarts[mid] <= codeUnit
|
||||
if (_rangeEnds[mid] >= codeUnit) {
|
||||
// range.start <= codeUnit
|
||||
if (range.end >= codeUnit) {
|
||||
return true;
|
||||
}
|
||||
min = mid + 1;
|
||||
@@ -59,7 +44,6 @@ class CodeunitRange {
|
||||
final int start;
|
||||
final int end;
|
||||
|
||||
|
||||
bool contains(int codeUnit) {
|
||||
return start <= codeUnit && codeUnit <= end;
|
||||
}
|
||||
@@ -79,3 +63,46 @@ class CodeunitRange {
|
||||
@override
|
||||
String toString() => '[$start, $end]';
|
||||
}
|
||||
|
||||
final int _kCharPipe = '|'.codeUnitAt(0);
|
||||
final int _kCharSemicolon = ';'.codeUnitAt(0);
|
||||
|
||||
class MutableInt {
|
||||
MutableInt(this.value);
|
||||
|
||||
int value;
|
||||
}
|
||||
|
||||
List<CodeunitRange> _unpackFontRange(String packedRange) {
|
||||
final MutableInt i = MutableInt(0);
|
||||
final List<CodeunitRange> ranges = <CodeunitRange>[];
|
||||
|
||||
while (i.value < packedRange.length) {
|
||||
final int rangeStart = _consumeInt36(packedRange, i, until: _kCharPipe);
|
||||
final int rangeLength = _consumeInt36(packedRange, i, until: _kCharSemicolon);
|
||||
final int rangeEnd = rangeStart + rangeLength;
|
||||
ranges.add(CodeunitRange(rangeStart, rangeEnd));
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
int _consumeInt36(String packedData, MutableInt index, {required int until}) {
|
||||
// The implementation is similar to:
|
||||
//
|
||||
// ```dart
|
||||
// return int.tryParse(packedData.substring(index, indexOfUntil), radix: 36);
|
||||
// ```
|
||||
//
|
||||
// But using substring is slow when called too many times. This custom
|
||||
// implementation parses the integer without extra memory.
|
||||
|
||||
int result = 0;
|
||||
while (true) {
|
||||
final int charCode = packedData.codeUnitAt(index.value);
|
||||
index.value++;
|
||||
if (charCode == until) {
|
||||
return result;
|
||||
}
|
||||
result = result * 36 + getIntFromCharCode(charCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,16 +253,16 @@ int _consumeInt(String packedData, int index) {
|
||||
//
|
||||
// But using substring is slow when called too many times. This custom
|
||||
// implementation makes the unpacking 25%-45% faster than using substring.
|
||||
final int digit0 = _getIntFromCharCode(packedData.codeUnitAt(index + 3));
|
||||
final int digit1 = _getIntFromCharCode(packedData.codeUnitAt(index + 2));
|
||||
final int digit2 = _getIntFromCharCode(packedData.codeUnitAt(index + 1));
|
||||
final int digit3 = _getIntFromCharCode(packedData.codeUnitAt(index));
|
||||
final int digit0 = getIntFromCharCode(packedData.codeUnitAt(index + 3));
|
||||
final int digit1 = getIntFromCharCode(packedData.codeUnitAt(index + 2));
|
||||
final int digit2 = getIntFromCharCode(packedData.codeUnitAt(index + 1));
|
||||
final int digit3 = getIntFromCharCode(packedData.codeUnitAt(index));
|
||||
return digit0 + (digit1 * 36) + (digit2 * 36 * 36) + (digit3 * 36 * 36 * 36);
|
||||
}
|
||||
|
||||
/// Does the same thing as [int.parse(str, 36)] but takes only a single
|
||||
/// character as a [charCode] integer.
|
||||
int _getIntFromCharCode(int charCode) {
|
||||
int getIntFromCharCode(int charCode) {
|
||||
assert((charCode >= kChar_0 && charCode <= kChar_9) ||
|
||||
(charCode >= kChar_a && charCode <= kChar_z));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user