Skip to content

Commit

Permalink
[canvaskit] default to Ahem in flutter_tester emulation mode (flutter…
Browse files Browse the repository at this point in the history
…#31024)

* [canvaskit] default to Ahem in flutter_tester emulation mode
  • Loading branch information
yjbanov authored Jan 24, 2022
1 parent 02806cc commit bc97236
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/web_ui/lib/src/engine/canvaskit/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
textAlign,
textDirection,
maxLines,
fontFamily,
ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
fontSize,
height,
textHeightBehavior,
Expand All @@ -43,7 +43,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
locale,
),
_textDirection = textDirection ?? ui.TextDirection.ltr,
_fontFamily = fontFamily,
_fontFamily = ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_fontSize = fontSize,
_height = height,
_leadingDistribution = textHeightBehavior?.leadingDistribution,
Expand Down Expand Up @@ -228,8 +228,8 @@ class CkTextStyle implements ui.TextStyle {
fontWeight,
fontStyle,
textBaseline,
fontFamily,
fontFamilyFallback,
ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
ui.debugEmulateFlutterTesterEnvironment ? null : fontFamilyFallback,
fontSize,
letterSpacing,
wordSpacing,
Expand Down Expand Up @@ -464,8 +464,8 @@ class CkStrutStyle implements ui.StrutStyle {
ui.FontWeight? fontWeight,
ui.FontStyle? fontStyle,
bool? forceStrutHeight,
}) : _fontFamily = fontFamily,
_fontFamilyFallback = fontFamilyFallback,
}) : _fontFamily = ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_fontFamilyFallback = ui.debugEmulateFlutterTesterEnvironment ? null : fontFamilyFallback,
_fontSize = fontSize,
_height = height,
_leadingDistribution = leadingDistribution,
Expand Down
118 changes: 118 additions & 0 deletions lib/web_ui/test/canvaskit/flutter_tester_emulation_golden_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;

import 'common.dart';

void main() {
internalBootstrapBrowserTest(() => testMain);
}

const ui.Rect kDefaultRegion = ui.Rect.fromLTRB(0, 0, 500, 250);

void testMain() {
ui.debugEmulateFlutterTesterEnvironment = true;

group('flutter_tester emulation', () {
setUpCanvasKitTest();

test('defaults to Ahem font family',
() async {
final CkPictureRecorder recorder = CkPictureRecorder();
final CkCanvas canvas = recorder.beginRecording(kDefaultRegion);
canvas.translate(10, 10);

void _drawTextWithOutline(String text, {
String? paragraphFontFamily,
String? textFontFamily,
List<String>? textFontFallbacks,
String? strutStyleFontFamily,
List<String>? strutStyleFontFallbacks,
double? strutStyleFontSize,
}) {
final CkStrutStyle? strutStyle;

if (strutStyleFontFamily != null || strutStyleFontFallbacks != null || strutStyleFontSize != null) {
strutStyle = CkStrutStyle(
fontFamily: strutStyleFontFamily,
fontFamilyFallback: strutStyleFontFallbacks,
fontSize: strutStyleFontSize,
);
} else {
strutStyle = null;
}

final CkParagraphBuilder builder = CkParagraphBuilder(CkParagraphStyle(
fontFamily: paragraphFontFamily,
strutStyle: strutStyle,
));

final bool needsTextStyle = textFontFamily != null || textFontFallbacks != null;

if (needsTextStyle) {
builder.pushStyle(CkTextStyle(
fontFamily: textFontFamily,
fontFamilyFallback: textFontFallbacks,
));
}

builder.addText(text);

if (needsTextStyle) {
builder.pop();
}

final CkParagraph paragraph = builder.build();
paragraph.layout(const ui.ParagraphConstraints(width: 10000));
canvas.drawParagraph(paragraph, ui.Offset.zero);
canvas.drawRect(
ui.Rect.fromLTWH(-4, -4, paragraph.maxIntrinsicWidth + 8, paragraph.height + 8),
CkPaint()
..style = ui.PaintingStyle.stroke
..strokeWidth = 1,
);
canvas.translate(
0,
paragraph.height + 16,
);
}

_drawTextWithOutline('default');
_drawTextWithOutline(
'roboto paragraph',
paragraphFontFamily: 'Roboto',
);
_drawTextWithOutline(
'roboto text',
textFontFamily: 'Roboto',
);
_drawTextWithOutline(
'roboto text fallback',
textFontFallbacks: <String>['Roboto'],
);
_drawTextWithOutline(
'roboto strut style',
strutStyleFontFamily: 'Roboto',
strutStyleFontSize: 40,
);
_drawTextWithOutline(
'roboto strut style fallback',
strutStyleFontFallbacks: <String>['Roboto'],
strutStyleFontSize: 40,
);

await matchPictureGolden(
'canvaskit_defaults_to_ahem.png',
recorder.endRecording(),
region: kDefaultRegion,
);
});
// TODO(yjbanov): https://github.com/flutter/flutter/issues/60040
// TODO(yjbanov): https://github.com/flutter/flutter/issues/71520
}, skip: isIosSafari || isFirefox);
}

0 comments on commit bc97236

Please sign in to comment.