Skip to content

Commit 0f9ba11

Browse files
sysint64FMorschel
authored andcommitted
[vector_graphics_compiler] fix: Stroke opacity not applied (flutter#8986)
Fixes [#158735](flutter/flutter#158735) ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent cc9e136 commit 0f9ba11

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ Alexander Rabin <alex.rabin@sentracam.com>
7979
LinXunFeng <linxunfeng@yeah.net>
8080
Hashir Shoaib <hashirshoaeb@gmail.com>
8181
Ricardo Dalarme <ricardodalarme@outlook.com>
82+
Andrei Kabylin <sys.int64@gmail.com>

packages/vector_graphics_compiler/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.17
2+
3+
* Fixes a bug where stroke opacity not applied by color mapper.
4+
15
## 1.1.16
26

37
* Sets stroke-width to 1 by default when an invalid value is parsed instead of throwing an exception.

packages/vector_graphics_compiler/lib/src/svg/parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,7 @@ class SvgStrokeAttributes {
23062306
}
23072307

23082308
return Stroke(
2309-
color: color.color!.withOpacity(opacity ?? 1.0),
2309+
color: opacity == null ? color.color : color.color!.withOpacity(opacity!),
23102310
shader: shader,
23112311
join: join,
23122312
cap: cap,

packages/vector_graphics_compiler/lib/vector_graphics_compiler.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ VectorInstructions parseWithoutOptimizers(
4040
String key = '',
4141
bool warningsAsErrors = false,
4242
SvgTheme theme = const SvgTheme(),
43+
ColorMapper? colorMapper,
4344
}) {
4445
return parse(
4546
xml,
@@ -49,6 +50,7 @@ VectorInstructions parseWithoutOptimizers(
4950
enableClippingOptimizer: false,
5051
enableMaskingOptimizer: false,
5152
enableOverdrawOptimizer: false,
53+
colorMapper: colorMapper,
5254
);
5355
}
5456

packages/vector_graphics_compiler/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics_compiler
22
description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler
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.16
5+
version: 1.1.17
66

77
executables:
88
vector_graphics_compiler:

packages/vector_graphics_compiler/test/parser_test.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
1010

1111
import 'test_svg_strings.dart';
1212

13+
class _TestOpacityColorMapper implements ColorMapper {
14+
const _TestOpacityColorMapper();
15+
16+
@override
17+
Color substitute(
18+
String? id,
19+
String elementName,
20+
String attributeName,
21+
Color color,
22+
) {
23+
if (color.value == 0xff000000) {
24+
return const Color(0x7fff0000);
25+
} else {
26+
return color;
27+
}
28+
}
29+
}
30+
1331
void main() {
1432
test('Reuse ID self-referentially', () {
1533
final VectorInstructions instructions = parseWithoutOptimizers('''
@@ -266,6 +284,24 @@ void main() {
266284
);
267285
});
268286

287+
test('preserve opacity from color mapper for strokes', () {
288+
const String strokeOpacitySvg = '''
289+
<svg viewBox="0 0 10 10" fill="none">
290+
<rect x="0" y="0" width="5" height="5" stroke="#000000" />
291+
</svg>
292+
''';
293+
294+
final VectorInstructions instructions = parseWithoutOptimizers(
295+
strokeOpacitySvg,
296+
colorMapper: const _TestOpacityColorMapper(),
297+
);
298+
299+
expect(
300+
instructions.paints.single,
301+
const Paint(stroke: Stroke(color: Color(0x7fff0000))),
302+
);
303+
});
304+
269305
test('text attributes are preserved', () {
270306
final VectorInstructions instructions = parseWithoutOptimizers(textTspan);
271307
expect(
@@ -352,6 +388,39 @@ void main() {
352388
);
353389
});
354390

391+
test('currentColor stoke opacity', () {
392+
const String currentColorSvg = '''
393+
<svg viewBox="0 0 10 10">
394+
<rect x="0" y="0" width="5" height="5" fill="currentColor" stroke="currentColor" />
395+
</svg>
396+
''';
397+
398+
final VectorInstructions blueInstructions = parseWithoutOptimizers(
399+
currentColorSvg,
400+
theme: const SvgTheme(currentColor: Color(0x7F0000FF)),
401+
);
402+
final VectorInstructions redInstructions = parseWithoutOptimizers(
403+
currentColorSvg,
404+
theme: const SvgTheme(currentColor: Color(0x7FFF0000)),
405+
);
406+
407+
expect(
408+
blueInstructions.paints.single,
409+
const Paint(
410+
fill: Fill(color: Color(0x7F0000FF)),
411+
stroke: Stroke(color: Color(0x7F0000FF)),
412+
),
413+
);
414+
415+
expect(
416+
redInstructions.paints.single,
417+
const Paint(
418+
fill: Fill(color: Color(0x7FFF0000)),
419+
stroke: Stroke(color: Color(0x7FFF0000)),
420+
),
421+
);
422+
});
423+
355424
test('Opacity with a save layer does not continue to inherit', () {
356425
final VectorInstructions instructions = parseWithoutOptimizers('''
357426
<svg width="283" height="180" viewBox="0 0 283 180" fill="none" xmlns="http://www.w3.org/2000/svg">

0 commit comments

Comments
 (0)