[web] Fix svg stroke when style is not provided (flutter/engine#24153)

This commit is contained in:
Ferhat
2021-02-10 17:35:29 -08:00
committed by GitHub
parent 8ba461dda4
commit 29f55bb772
6 changed files with 78 additions and 21 deletions

View File

@@ -1,2 +1,2 @@
repository: https://github.com/flutter/goldens.git
revision: 92381b8ca48729e9b21c6b19dd39ed605a98d4ee
revision: 578ecb91ea33004cd0ba0af513884cc28ba88cf4

View File

@@ -544,14 +544,35 @@ class BitmapCanvas extends EngineCanvas {
..transformOrigin = '0 0 0';
}
}
_applyFilter(svgElm, paint);
_drawElement(svgElm, ui.Offset(0, 0), paint);
} else {
_setUpPaint(paint, paint.shader != null ? path.getBounds() : null);
_canvasPool.drawPath(path, paint.style);
if (paint.style == null && paint.strokeWidth != null) {
_canvasPool.drawPath(path, ui.PaintingStyle.stroke);
} else {
_canvasPool.drawPath(path, paint.style);
}
_tearDownPaint();
}
}
void _applyFilter(html.Element element, SurfacePaintData paint) {
if (paint.maskFilter != null) {
final bool isStroke = paint.style == ui.PaintingStyle.stroke;
String cssColor =
paint.color == null ? '#000000' : colorToCssString(paint.color)!;// ignore: unnecessary_null_comparison
final double sigma = paint.maskFilter!.webOnlySigma;
if (browserEngine == BrowserEngine.webkit && !isStroke) {
// A bug in webkit leaves artifacts when this element is animated
// with filter: blur, we use boxShadow instead.
element.style.boxShadow = '0px 0px ${sigma * 2.0}px $cssColor';
} else {
element.style.filter = 'blur(${sigma}px)';
}
}
}
@override
void drawShadow(ui.Path path, ui.Color color, double elevation,
bool transparentOccluder) {

View File

@@ -253,9 +253,11 @@ html.Element _pathToSvgElement(SurfacePath path, SurfacePaintData paint,
'<svg viewBox="0 0 $width $height" width="${width}px" height="${height}px">');
sb.write('<path ');
final ui.Color color = paint.color ?? const ui.Color(0xFF000000);
if (paint.style == ui.PaintingStyle.stroke) {
if (paint.style == ui.PaintingStyle.stroke ||
(paint.style != ui.PaintingStyle.fill &&
paint.strokeWidth != 0)) {
sb.write('stroke="${colorToCssString(color)}" ');
sb.write('stroke-width="${paint.strokeWidth}" ');
sb.write('stroke-width="${paint.strokeWidth ?? 1.0}" ');
sb.write('fill="none" ');
} else if (paint.color != null) {
sb.write('fill="${colorToCssString(color)}" ');

View File

@@ -363,7 +363,9 @@ class PersistedPhysicalShape extends PersistedContainerSurface
final ui.Rect pathBounds2 = path.getBounds();
_svgElement = _pathToSvgElement(
path, SurfacePaintData()..color = color, '${pathBounds2.right}', '${pathBounds2.bottom}');
path, SurfacePaintData()
..style = ui.PaintingStyle.fill
..color = color, '${pathBounds2.right}', '${pathBounds2.bottom}');
/// Render element behind the clipped content.
rootElement!.insertBefore(_svgElement!, childContainer);

View File

@@ -417,11 +417,15 @@ class RecordingCanvas {
final double paintSpread = _getPaintSpread(paint);
final PaintDrawDRRect command =
PaintDrawDRRect(outer, inner, paint.paintData);
final double left = math.min(outer.left, outer.right);
final double right = math.max(outer.left, outer.right);
final double top = math.min(outer.top, outer.bottom);
final double bottom = math.max(outer.top, outer.bottom);
_paintBounds.growLTRB(
outer.left - paintSpread,
outer.top - paintSpread,
outer.right + paintSpread,
outer.bottom + paintSpread,
left - paintSpread,
top - paintSpread,
right + paintSpread,
bottom + paintSpread,
command,
);
_commands.add(command);
@@ -1026,6 +1030,9 @@ class PaintDrawDRRect extends DrawCommand {
@override
void apply(EngineCanvas canvas) {
if (paint.style == null) {
paint.style = ui.PaintingStyle.fill;
}
canvas.drawPath(path!, paint);
}

View File

@@ -16,17 +16,26 @@ void main() {
internalBootstrapBrowserTest(() => testMain);
}
enum PaintMode {
kStrokeAndFill,
kStroke,
kFill,
kStrokeWidthOnly,
}
void testMain() async {
final Rect region = Rect.fromLTWH(8, 8, 600, 800); // Compensate for old scuba tester padding
final Rect region = Rect.fromLTWH(8, 8, 600, 400); // Compensate for old scuba tester padding
Future<void> testPath(Path path, String scubaFileName,
{Paint paint, double maxDiffRatePercent = null, bool write = false,
bool strokeEnabled = true, bool enableFill = true}) async {
const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 800);
PaintMode mode = PaintMode.kStrokeAndFill}) async {
const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 400);
final BitmapCanvas bitmapCanvas = BitmapCanvas(canvasBounds,
RenderStrategy());
final RecordingCanvas canvas = RecordingCanvas(canvasBounds);
bool enableFill = mode == PaintMode.kStrokeAndFill ||
mode == PaintMode.kFill;
if (enableFill) {
paint ??= Paint()
..color = const Color(0x807F7F7F)
@@ -34,7 +43,7 @@ void testMain() async {
canvas.drawPath(path, paint);
}
if (strokeEnabled) {
if (mode == PaintMode.kStrokeAndFill || mode == PaintMode.kStroke) {
paint = Paint()
..strokeWidth = 2
..color = enableFill ? const Color(0xFFFF0000) :
@@ -42,6 +51,12 @@ void testMain() async {
..style = PaintingStyle.stroke;
}
if (mode == PaintMode.kStrokeWidthOnly) {
paint = Paint()
..color = const Color(0xFF4060E0)
..strokeWidth = 10;
}
canvas.drawPath(path, paint);
final html.Element svgElement = pathToSvgElement(path, paint,
@@ -95,17 +110,17 @@ void testMain() async {
largeArc: false, clockwise: false, distance: 20),
ArcSample(const Offset(200, 0),
largeArc: true, clockwise: false, distance: 20),
ArcSample(const Offset(0, 150),
ArcSample(const Offset(0, 0),
largeArc: false, clockwise: true, distance: 20),
ArcSample(const Offset(200, 150),
ArcSample(const Offset(200, 0),
largeArc: true, clockwise: true, distance: 20),
ArcSample(const Offset(0, 300),
ArcSample(const Offset(0, 0),
largeArc: false, clockwise: false, distance: -20),
ArcSample(const Offset(200, 300),
ArcSample(const Offset(200, 0),
largeArc: true, clockwise: false, distance: -20),
ArcSample(const Offset(0, 450),
ArcSample(const Offset(0, 0),
largeArc: false, clockwise: true, distance: -20),
ArcSample(const Offset(200, 450),
ArcSample(const Offset(200, 0),
largeArc: true, clockwise: true, distance: -20)
];
int sampleIndex = 0;
@@ -157,7 +172,16 @@ void testMain() async {
path.lineTo(4, 51.5);
path.conicTo(0.5, 51.5, 0.5, 48, w);
path.lineTo(0.5, 14);
await testPath(path, 'svg_editoutline', enableFill: false);
await testPath(path, 'svg_editoutline', mode: PaintMode.kStroke);
});
/// Regression test for https://github.com/flutter/flutter/issues/74416
test('render stroke', () async {
final Path path = Path();
path.moveTo(20, 20);
path.lineTo(200, 200);
await testPath(path, 'svg_stroke_width',
mode: PaintMode.kStrokeWidthOnly);
});
}
@@ -169,7 +193,8 @@ html.Element pathToSvgElement(Path path, Paint paint,
'<svg viewBox="0 0 ${bounds.right} ${bounds.bottom}" '
'width="${bounds.right}" height="${bounds.bottom}">');
sb.write('<path ');
if (paint.style == PaintingStyle.stroke) {
if (paint.style == PaintingStyle.stroke ||
(paint.strokeWidth != null && paint.strokeWidth != 0.0)) {
sb.write('stroke="${colorToCssString(paint.color)}" ');
sb.write('stroke-width="${paint.strokeWidth}" ');
if (!enableFill) {