Skip to content

Commit

Permalink
Add new test font (flutter#39809)
Browse files Browse the repository at this point in the history
Add new test font
  • Loading branch information
LongCatIsLooong authored Mar 3, 2023
1 parent 1f806b5 commit 170cbea
Show file tree
Hide file tree
Showing 23 changed files with 1,053 additions and 69 deletions.
1 change: 1 addition & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
../../../flutter/third_party/web_locale_keymap/README.md
../../../flutter/third_party/web_locale_keymap/pubspec.yaml
../../../flutter/third_party/web_locale_keymap/test
../../../flutter/third_party/web_test_fonts/pubspec.yaml
../../../flutter/third_party/web_unicode/README.md
../../../flutter/third_party/web_unicode/pubspec.yaml
../../../flutter/tools
Expand Down
5 changes: 5 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ LIBRARY: engine
LIBRARY: spring_animation
LIBRARY: tonic
LIBRARY: txt
LIBRARY: web_test_fonts
LIBRARY: web_unicode
ORIGIN: ../../../flutter/LICENSE
ORIGIN: ../../../flutter/assets/asset_manager.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -3166,6 +3167,8 @@ ORIGIN: ../../../flutter/third_party/txt/src/txt/platform_fuchsia.cc + ../../../
ORIGIN: ../../../flutter/third_party/txt/src/txt/platform_linux.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/txt/src/txt/platform_mac.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/txt/src/txt/platform_windows.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/web_test_fonts/lib/web_test_fonts.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/web_test_fonts/lib/web_test_fonts/web_test_fonts.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/web_unicode/lib/web_unicode.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/third_party/web_unicode/tool/unicode_sync_script.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/vulkan/procs/vulkan_handle.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -5712,6 +5715,8 @@ FILE: ../../../flutter/third_party/txt/src/txt/platform_fuchsia.cc
FILE: ../../../flutter/third_party/txt/src/txt/platform_linux.cc
FILE: ../../../flutter/third_party/txt/src/txt/platform_mac.mm
FILE: ../../../flutter/third_party/txt/src/txt/platform_windows.cc
FILE: ../../../flutter/third_party/web_test_fonts/lib/web_test_fonts.dart
FILE: ../../../flutter/third_party/web_test_fonts/lib/web_test_fonts/web_test_fonts.dart
FILE: ../../../flutter/third_party/web_unicode/lib/web_unicode.dart
FILE: ../../../flutter/third_party/web_unicode/tool/unicode_sync_script.dart
FILE: ../../../flutter/vulkan/procs/vulkan_handle.cc
Expand Down
11 changes: 4 additions & 7 deletions lib/ui/text/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,16 @@ void FontCollection::RegisterFonts(
}

void FontCollection::RegisterTestFonts() {
std::vector<sk_sp<SkTypeface>> test_typefaces;
std::vector<std::unique_ptr<SkStreamAsset>> font_data = GetTestFontData();
for (auto& font : font_data) {
test_typefaces.push_back(SkTypeface::MakeFromStream(std::move(font)));
}

std::vector<sk_sp<SkTypeface>> test_typefaces = GetTestFontData();
std::unique_ptr<txt::TypefaceFontAssetProvider> font_provider =
std::make_unique<txt::TypefaceFontAssetProvider>();

size_t index = 0;
std::vector<std::string> names = GetTestFontFamilyNames();
for (sk_sp<SkTypeface> typeface : test_typefaces) {
font_provider->RegisterTypeface(std::move(typeface), names[index]);
if (typeface) {
font_provider->RegisterTypeface(std::move(typeface), names[index]);
}
index++;
}

Expand Down
11 changes: 9 additions & 2 deletions lib/web_ui/lib/src/engine/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ const String ahemFontFamily = 'Ahem';
const String ahemFontUrl = '/assets/fonts/ahem.ttf';
const String robotoFontFamily = 'Roboto';
const String robotoTestFontUrl = '/assets/fonts/Roboto-Regular.ttf';
const String robotoVariableFontFamily = 'RobotoVariable';
const String robotoVariableTestFontUrl = '/assets/fonts/RobotoSlab-VariableFont_wght.ttf';

/// The list of test fonts, in the form of font family name - font file url pairs.
/// This list does not include embedded test fonts, which need to be loaded and
/// registered separately in [FontCollection.debugDownloadTestFonts].
const Map<String, String> testFontUrls = <String, String>{
ahemFontFamily: ahemFontUrl,
robotoFontFamily: robotoTestFontUrl,
'RobotoVariable': '/assets/fonts/RobotoSlab-VariableFont_wght.ttf',
};

/// This class downloads assets over the network.
///
Expand Down
20 changes: 11 additions & 9 deletions lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:web_test_fonts/web_test_fonts.dart';

import '../assets.dart';
import '../dom.dart';
import '../fonts.dart';
Expand Down Expand Up @@ -176,17 +178,17 @@ class SkiaFontCollection implements FontCollection {
@override
Future<void> debugDownloadTestFonts() async {
final List<Future<UnregisteredFont?>> pendingFonts = <Future<UnregisteredFont?>>[];
if (!_isFontFamilyDownloaded(ahemFontFamily)) {
_downloadFont(pendingFonts, ahemFontUrl, ahemFontFamily);
}
if (!_isFontFamilyDownloaded(robotoFontFamily)) {
_downloadFont(pendingFonts, robotoTestFontUrl, robotoFontFamily);
}
if (!_isFontFamilyDownloaded(robotoVariableFontFamily)) {
_downloadFont(pendingFonts, robotoVariableTestFontUrl, robotoVariableFontFamily);
for (final MapEntry<String, String> fontEntry in testFontUrls.entries) {
if (!_isFontFamilyDownloaded(fontEntry.key)) {
_downloadFont(pendingFonts, fontEntry.value, fontEntry.key);
}
}

final List<UnregisteredFont?> completedPendingFonts = await Future.wait(pendingFonts);
completedPendingFonts.add(UnregisteredFont(
EmbeddedTestFont.flutterTest.data.buffer,
'<embedded>',
EmbeddedTestFont.flutterTest.fontFamily,
));
_unregisteredFonts.addAll(completedPendingFonts.whereType<UnregisteredFont>());

// Ahem must be added to font fallbacks list regardless of where it was
Expand Down
15 changes: 11 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import 'skia_object_cache.dart';
import 'text_fragmenter.dart';
import 'util.dart';

final List<String> _testFonts = <String>['Ahem', 'FlutterTest'];
String? _effectiveFontFamily(String? fontFamily) {
return ui.debugEmulateFlutterTesterEnvironment && !_testFonts.contains(fontFamily)
? _testFonts.first
: fontFamily;
}

@immutable
class CkParagraphStyle implements ui.ParagraphStyle {
CkParagraphStyle({
Expand All @@ -35,7 +42,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
textAlign,
textDirection,
maxLines,
ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_effectiveFontFamily(fontFamily),
fontSize,
height,
textHeightBehavior,
Expand All @@ -45,7 +52,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
ellipsis,
locale,
),
_fontFamily = ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_fontFamily = _effectiveFontFamily(fontFamily),
_fontSize = fontSize,
_height = height,
_leadingDistribution = textHeightBehavior?.leadingDistribution,
Expand Down Expand Up @@ -231,7 +238,7 @@ class CkTextStyle implements ui.TextStyle {
fontWeight,
fontStyle,
textBaseline,
ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_effectiveFontFamily(fontFamily),
ui.debugEmulateFlutterTesterEnvironment ? null : fontFamilyFallback,
fontSize,
letterSpacing,
Expand Down Expand Up @@ -483,7 +490,7 @@ class CkStrutStyle implements ui.StrutStyle {
ui.FontWeight? fontWeight,
ui.FontStyle? fontStyle,
bool? forceStrutHeight,
}) : _fontFamily = ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
}) : _fontFamily = _effectiveFontFamily(fontFamily),
_fontFamilyFallback = ui.debugEmulateFlutterTesterEnvironment ? null : fontFamilyFallback,
_fontSize = fontSize,
_height = height,
Expand Down
18 changes: 10 additions & 8 deletions lib/web_ui/lib/src/engine/text/font_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:ui/src/engine/fonts.dart';
import 'package:web_test_fonts/web_test_fonts.dart';

import '../assets.dart';
import '../dom.dart';
Expand Down Expand Up @@ -74,14 +75,15 @@ class HtmlFontCollection implements FontCollection {
/// Downloads fonts that are used by tests.
@override
Future<void> debugDownloadTestFonts() async {
_testFontManager = FontManager();
_testFontManager!.downloadAsset(
ahemFontFamily, 'url($ahemFontUrl)', const <String, String>{});
_testFontManager!.downloadAsset(robotoFontFamily,
'url($robotoTestFontUrl)', const <String, String>{});
_testFontManager!.downloadAsset(robotoVariableFontFamily,
'url($robotoVariableTestFontUrl)', const <String, String>{});
await _testFontManager!.downloadAllFonts();
final FontManager fontManager = _testFontManager = FontManager();
for (final MapEntry<String, String> fontEntry in testFontUrls.entries) {
fontManager.downloadAsset(fontEntry.key, 'url(${fontEntry.value})', const <String, String>{});
}
fontManager._downloadedFonts.add(createDomFontFace(
EmbeddedTestFont.flutterTest.fontFamily,
EmbeddedTestFont.flutterTest.data,
));
await fontManager.downloadAllFonts();
}

@override
Expand Down
20 changes: 8 additions & 12 deletions lib/web_ui/lib/src/engine/text/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -464,18 +464,14 @@ class EngineTextStyle implements ui.TextStyle {
final ui.Paint? foreground;
final List<ui.Shadow>? shadows;

static final List<String> _testFonts = <String>['Ahem', 'FlutterTest'];
String get effectiveFontFamily {
if (assertionsEnabled) {
// In the flutter tester environment, we use a predictable-size font
// "Ahem". This makes widget tests predictable and less flaky.
if (ui.debugEmulateFlutterTesterEnvironment) {
return 'Ahem';
}
}
if (fontFamily.isEmpty) {
return FlutterViewEmbedder.defaultFontFamily;
}
return fontFamily;
final String fontFamily = this.fontFamily.isEmpty ? FlutterViewEmbedder.defaultFontFamily : this.fontFamily;
// In the flutter tester environment, we use predictable-size test fonts.
// This makes widget tests predictable and less flaky.
return assertionsEnabled && ui.debugEmulateFlutterTesterEnvironment && !_testFonts.contains(fontFamily)
? _testFonts.first
: fontFamily;
}

String? _cssFontString;
Expand Down Expand Up @@ -815,7 +811,7 @@ void applyTextStyleToElement({
style.fontStyle == ui.FontStyle.normal ? 'normal' : 'italic';
}
// For test environment use effectiveFontFamily since we need to
// consistently use Ahem font.
// consistently use the correct test font.
if (ui.debugEmulateFlutterTesterEnvironment) {
cssStyle.fontFamily = canonicalizeFontFamily(style.effectiveFontFamily)!;
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/web_ui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies:
web_unicode:
path: ../../third_party/web_unicode

web_test_fonts:
path: ../../third_party/web_test_fonts

dev_dependencies:
analyzer: 5.2.0
archive: 3.1.2
Expand Down
31 changes: 31 additions & 0 deletions lib/web_ui/test/canvaskit/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ void testMain() {
expect(await matchImage(tabImage, spaceImage), isTrue);
expect(await matchImage(tabImage, tofuImage), isFalse);
});

group('test fonts in flutterTester environment', () {
final bool resetValue = ui.debugEmulateFlutterTesterEnvironment;
ui.debugEmulateFlutterTesterEnvironment = true;
tearDownAll(() => ui.debugEmulateFlutterTesterEnvironment = resetValue);
const List<String> testFonts = <String>['Ahem', 'FlutterTest'];

test('The default test font is used when a non-test fontFamily is specified', () {
final String defaultTestFontFamily = testFonts.first;

expect(CkTextStyle(fontFamily: 'BogusFontFamily').fontFamily, defaultTestFontFamily);
expect(CkParagraphStyle(fontFamily: 'BogusFontFamily').getTextStyle().fontFamily, defaultTestFontFamily);
expect(CkStrutStyle(fontFamily: 'BogusFontFamily'), CkStrutStyle(fontFamily: defaultTestFontFamily));
});

test('The default test font is used when fontFamily is unspecified', () {
final String defaultTestFontFamily = testFonts.first;

expect(CkTextStyle().fontFamily, defaultTestFontFamily);
expect(CkParagraphStyle().getTextStyle().fontFamily, defaultTestFontFamily);
expect(CkStrutStyle(), CkStrutStyle(fontFamily: defaultTestFontFamily));
});

test('Can specify test fontFamily to use', () {
for (final String testFont in testFonts) {
expect(CkTextStyle(fontFamily: testFont).fontFamily, testFont);
expect(CkParagraphStyle(fontFamily: testFont).getTextStyle().fontFamily, testFont);
}
});
});
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/71520
}, skip: isSafari || isFirefox);

}
20 changes: 20 additions & 0 deletions lib/web_ui/test/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,24 @@ Future<void> testMain() async {
expect(FontWeight.w800.value, 800);
expect(FontWeight.w900.value, 900);
});

group('test fonts in flutterTester environment', () {
final bool resetValue = debugEmulateFlutterTesterEnvironment;
debugEmulateFlutterTesterEnvironment = true;
tearDownAll(() => debugEmulateFlutterTesterEnvironment = resetValue);
const List<String> testFonts = <String>['Ahem', 'FlutterTest'];

test('The default test font is used when a non-test fontFamily is specified, or fontFamily is not specified', () {
final String defaultTestFontFamily = testFonts.first;

expect(EngineTextStyle.only().effectiveFontFamily, defaultTestFontFamily);
expect(EngineTextStyle.only(fontFamily: 'BogusFontFamily').effectiveFontFamily, defaultTestFontFamily);
});

test('Can specify test fontFamily to use', () {
for (final String testFont in testFonts) {
expect(EngineTextStyle.only(fontFamily: testFont).effectiveFontFamily, testFont);
}
});
});
}
Loading

0 comments on commit 170cbea

Please sign in to comment.