Skip to content

Commit 2eb4c71

Browse files
authored
[vector_graphics_codec] Add support for encoding and decoding paint blur commands (flutter#13148)
Adds a new `_paintBlurTag` command (`writePaintBlur` / `onPaintBlur`) to `vector_graphics_codec` (`1.2.0`) to support encoding and decoding Gaussian blur image filters (`sigmaX`, `sigmaY`) alongside paint records. This is split out from flutter/packages#11909 so the codec changes can land and be published first. Part of flutter#158592 ## Pre-Review Checklist
1 parent 18a9c45 commit 2eb4c71

4 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎packages/vector_graphics_codec/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 1.2.0
22

3+
* Adds support for encoding and decoding paint blur commands (`writePaintBlur` and `onPaintBlur`) for SVG filters.
34
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
45

56
## 1.1.13

‎packages/vector_graphics_codec/lib/vector_graphics_codec.dart‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class VectorGraphicsCodec {
126126
static const int _textPositionTag = 50;
127127
static const int _updateTextPositionTag = 51;
128128
static const int _pathTagHalfPrecision = 52;
129+
static const int _paintBlurTag = 53;
129130

130131
static const int _version = 1;
131132
static const int _magicNumber = 0x00882d62;
@@ -230,6 +231,9 @@ class VectorGraphicsCodec {
230231
case _updateTextPositionTag:
231232
_readUpdateTextPosition(buffer, listener);
232233
continue;
234+
case _paintBlurTag:
235+
_readPaintBlur(buffer, listener);
236+
continue;
233237
default:
234238
throw StateError('Unknown type tag $type');
235239
}
@@ -959,6 +963,34 @@ class VectorGraphicsCodec {
959963
final Float64List? transform = buffer.getTransform();
960964
listener?.onPatternStart(patternId, x, y, width, height, transform!);
961965
}
966+
967+
/// Encode a Gaussian blur filter command for a paint object into [buffer].
968+
///
969+
/// The [paintId] must refer to a previously encoded paint object (from
970+
/// [writeFill] or [writeStroke]).
971+
///
972+
/// [sigmaX] and [sigmaY] specify the non-negative standard deviations of the
973+
/// Gaussian blur kernel along the horizontal and vertical axes, respectively.
974+
void writePaintBlur(VectorGraphicsBuffer buffer, int paintId, double sigmaX, double sigmaY) {
975+
assert(
976+
paintId >= 0 && paintId < buffer._nextPaintId,
977+
'paintId ($paintId) must refer to a previously encoded paint object.',
978+
);
979+
assert(sigmaX >= 0.0);
980+
assert(sigmaY >= 0.0);
981+
buffer._checkPhase(_CurrentSection.paints);
982+
buffer._putUint8(_paintBlurTag);
983+
buffer._putUint16(paintId);
984+
buffer._putFloat32(sigmaX);
985+
buffer._putFloat32(sigmaY);
986+
}
987+
988+
void _readPaintBlur(_ReadBuffer buffer, VectorGraphicsCodecListener? listener) {
989+
final int paintId = buffer.getUint16();
990+
final double sigmaX = buffer.getFloat32();
991+
final double sigmaY = buffer.getFloat32();
992+
listener?.onPaintBlur(paintId, sigmaX, sigmaY);
993+
}
962994
}
963995

964996
/// Implement this listener class to support decoding of vector_graphics binary
@@ -1028,6 +1060,11 @@ abstract class VectorGraphicsCodecListener {
10281060
/// Prepare to draw a new mask, until the next [onRestoreLayer] command.
10291061
void onMask();
10301062

1063+
/// A Gaussian blur filter for the paint object identified by [paintId] has
1064+
/// been decoded, with horizontal and vertical standard deviations [sigmaX]
1065+
/// and [sigmaY].
1066+
void onPaintBlur(int paintId, double sigmaX, double sigmaY) {}
1067+
10311068
/// A radial gradient shader has been parsed.
10321069
///
10331070
/// [focalX] and [focalY] are either both `null` or `non-null`.

‎packages/vector_graphics_codec/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics_codec
22
description: An encoding library for the binary format used in `package:vector_graphics`
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_codec
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.1.13
5+
version: 1.2.0
66

77
environment:
88
sdk: ^3.11.0

‎packages/vector_graphics_codec/test/vector_graphics_codec_test.dart‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,54 @@ void main() {
125125
]);
126126
});
127127

128+
test('Can encode and decode paint blur', () {
129+
final buffer = VectorGraphicsBuffer();
130+
final listener = TestListener();
131+
final int paintId = codec.writeFill(buffer, 23, 0);
132+
codec.writePaintBlur(buffer, paintId, 2.5, 3.5);
133+
final int pathId = codec.writePath(
134+
buffer,
135+
Uint8List.fromList(<int>[ControlPointTypes.moveTo, ControlPointTypes.close]),
136+
Float32List.fromList(<double>[1, 2]),
137+
0,
138+
);
139+
codec.writeDrawPath(buffer, pathId, paintId, null);
140+
141+
codec.decode(buffer.done(), listener);
142+
143+
expect(listener.commands, <Object>[
144+
OnPaintObject(
145+
color: 23,
146+
strokeCap: null,
147+
strokeJoin: null,
148+
blendMode: 0,
149+
strokeMiterLimit: null,
150+
strokeWidth: null,
151+
paintStyle: 0,
152+
id: paintId,
153+
shaderId: null,
154+
),
155+
OnPaintBlur(paintId, 2.5, 3.5),
156+
OnPathStart(pathId, 0),
157+
const OnPathMoveTo(1, 2),
158+
const OnPathClose(),
159+
const OnPathFinished(),
160+
OnDrawPath(pathId, paintId, null),
161+
]);
162+
});
163+
164+
test('writePaintBlur asserts that paintId was previously encoded', () {
165+
final buffer = VectorGraphicsBuffer();
166+
expect(() => codec.writePaintBlur(buffer, 0, 2.5, 3.5), throwsA(isA<AssertionError>()));
167+
168+
final int paintId = codec.writeFill(buffer, 23, 0);
169+
expect(
170+
() => codec.writePaintBlur(buffer, paintId + 1, 2.5, 3.5),
171+
throwsA(isA<AssertionError>()),
172+
);
173+
expect(() => codec.writePaintBlur(buffer, -1, 2.5, 3.5), throwsA(isA<AssertionError>()));
174+
});
175+
128176
test('Basic message encode and decode with shaded path', () {
129177
final buffer = VectorGraphicsBuffer();
130178
final listener = TestListener();
@@ -875,6 +923,12 @@ class TestListener extends VectorGraphicsCodecListener {
875923
);
876924
}
877925

926+
@override
927+
void onPaintBlur(int paintId, double sigmaX, double sigmaY) {
928+
super.onPaintBlur(paintId, sigmaX, sigmaY);
929+
commands.add(OnPaintBlur(paintId, sigmaX, sigmaY));
930+
}
931+
878932
@override
879933
void onPathClose() {
880934
commands.add(const OnPathClose());
@@ -1339,6 +1393,28 @@ class OnPaintObject {
13391393
'paintStyle: $paintStyle, id: $id, shaderId: $shaderId)';
13401394
}
13411395

1396+
@immutable
1397+
class OnPaintBlur {
1398+
const OnPaintBlur(this.paintId, this.sigmaX, this.sigmaY);
1399+
1400+
final int paintId;
1401+
final double sigmaX;
1402+
final double sigmaY;
1403+
1404+
@override
1405+
int get hashCode => Object.hash(paintId, sigmaX, sigmaY);
1406+
1407+
@override
1408+
bool operator ==(Object other) =>
1409+
other is OnPaintBlur &&
1410+
other.paintId == paintId &&
1411+
other.sigmaX == sigmaX &&
1412+
other.sigmaY == sigmaY;
1413+
1414+
@override
1415+
String toString() => 'OnPaintBlur(paintId: $paintId, sigmaX: $sigmaX, sigmaY: $sigmaY)';
1416+
}
1417+
13421418
@immutable
13431419
class OnPathClose {
13441420
const OnPathClose();

0 commit comments

Comments
 (0)