Unverified Commit 45e02daf authored by Cyp's avatar Cyp Committed by GitHub

Correctly handle centerSlice with resoultion-aware assets. (#68325)

parent 1edec6fc
......@@ -448,12 +448,9 @@ void paintImage({
Size inputSize = Size(image.width.toDouble(), image.height.toDouble());
Offset? sliceBorder;
if (centerSlice != null) {
sliceBorder = Offset(
centerSlice.left + inputSize.width - centerSlice.right,
centerSlice.top + inputSize.height - centerSlice.bottom,
);
sliceBorder = inputSize / scale - centerSlice.size as Offset;
outputSize = outputSize - sliceBorder as Size;
inputSize = inputSize - sliceBorder as Size;
inputSize = inputSize - sliceBorder * scale as Size;
}
fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill;
assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover));
......@@ -559,7 +556,7 @@ void paintImage({
}
}
final bool needSave = repeat != ImageRepeat.noRepeat || flipHorizontally;
final bool needSave = centerSlice != null || repeat != ImageRepeat.noRepeat || flipHorizontally;
if (needSave)
canvas.save();
if (repeat != ImageRepeat.noRepeat)
......@@ -581,11 +578,12 @@ void paintImage({
canvas.drawImageRect(image, sourceRect, tileRect, paint);
}
} else {
canvas.scale(1 / scale);
if (repeat == ImageRepeat.noRepeat) {
canvas.drawImageNine(image, centerSlice, destinationRect, paint);
canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(destinationRect, scale), paint);
} else {
for (final Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
canvas.drawImageNine(image, centerSlice, tileRect, paint);
canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(tileRect, scale), paint);
}
}
if (needSave)
......@@ -619,3 +617,5 @@ Iterable<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect, Im
yield fundamentalRect.shift(Offset(i * strideX, j * strideY));
}
}
Rect _scaleRect(Rect rect, double scale) => Rect.fromLTRB(rect.left * scale, rect.top * scale, rect.right * scale, rect.bottom * scale);
......@@ -108,6 +108,26 @@ void main() {
FlutterError.onError = oldFlutterError;
});
test('centerSlice with scale ≠ 1', () async {
final TestCanvas canvas = TestCanvas();
paintImage(
canvas: canvas,
rect: const Rect.fromLTRB(10, 20, 430, 420),
image: image300x300,
scale: 2.0,
centerSlice: const Rect.fromLTRB(50, 40, 250, 260),
);
final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
return invocation.memberName == #drawImageNine;
});
expect(command, isNotNull);
expect(command.positionalArguments[0], equals(image300x300));
expect(command.positionalArguments[1], equals(const Rect.fromLTRB(100.0, 80.0, 500.0, 520.0)));
expect(command.positionalArguments[2], equals(const Rect.fromLTRB(20.0, 40.0, 860.0, 840.0)));
});
testWidgets('Reports Image painting', (WidgetTester tester) async {
late ImageSizeInfo imageSizeInfo;
int count = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment