David PHAM-VAN

Add SVG Color filter

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 - Fix documentation 5 - Fix documentation
6 - Add Positioned.fill() 6 - Add Positioned.fill()
7 - Improve GraphicState 7 - Improve GraphicState
  8 +- Add SVG Color filter
8 9
9 ## 3.1.0 10 ## 3.1.0
10 11
@@ -38,6 +38,10 @@ class SvgColor { @@ -38,6 +38,10 @@ class SvgColor {
38 return none; 38 return none;
39 } 39 }
40 40
  41 + if (painter.parser.colorFilter != null) {
  42 + return SvgColor(color: painter.parser.colorFilter);
  43 + }
  44 +
41 if (svgColors.containsKey(color)) { 45 if (svgColors.containsKey(color)) {
42 return SvgColor(color: svgColors[color]); 46 return SvgColor(color: svgColors[color]);
43 } 47 }
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 */ 15 */
16 16
17 import 'package:pdf/pdf.dart'; 17 import 'package:pdf/pdf.dart';
  18 +import 'package:pdf/src/svg/color.dart';
18 import 'package:pdf/widgets.dart'; 19 import 'package:pdf/widgets.dart';
19 20
20 import 'brush.dart'; 21 import 'brush.dart';
@@ -38,11 +39,12 @@ class SvgPainter { @@ -38,11 +39,12 @@ class SvgPainter {
38 final PdfRect boundingBox; 39 final PdfRect boundingBox;
39 40
40 void paint() { 41 void paint() {
41 - SvgGroup.fromXml(  
42 - parser.root,  
43 - this,  
44 - SvgBrush.defaultContext,  
45 - ).paint(_canvas!); 42 + final brush = parser.colorFilter == null
  43 + ? SvgBrush.defaultContext
  44 + : SvgBrush.defaultContext
  45 + .copyWith(fill: SvgColor(color: parser.colorFilter));
  46 +
  47 + SvgGroup.fromXml(parser.root, this, brush).paint(_canvas!);
46 } 48 }
47 49
48 final _fontCache = <String, Font>{}; 50 final _fontCache = <String, Font>{};
@@ -22,7 +22,10 @@ import 'brush.dart'; @@ -22,7 +22,10 @@ import 'brush.dart';
22 class SvgParser { 22 class SvgParser {
23 /// Create an SVG parser 23 /// Create an SVG parser
24 24
25 - factory SvgParser({required XmlDocument xml}) { 25 + factory SvgParser({
  26 + required XmlDocument xml,
  27 + PdfColor? colorFilter,
  28 + }) {
26 final root = xml.rootElement; 29 final root = xml.rootElement;
27 30
28 final vbattr = root.getAttribute('viewBox'); 31 final vbattr = root.getAttribute('viewBox');
@@ -45,10 +48,22 @@ class SvgParser { @@ -45,10 +48,22 @@ class SvgParser {
45 48
46 final viewBox = PdfRect(fvb[0], fvb[1], fvb[2], fvb[3]); 49 final viewBox = PdfRect(fvb[0], fvb[1], fvb[2], fvb[3]);
47 50
48 - return SvgParser._(width, height, viewBox, root); 51 + return SvgParser._(
  52 + width,
  53 + height,
  54 + viewBox,
  55 + root,
  56 + colorFilter,
  57 + );
49 } 58 }
50 59
51 - SvgParser._(this.width, this.height, this.viewBox, this.root); 60 + SvgParser._(
  61 + this.width,
  62 + this.height,
  63 + this.viewBox,
  64 + this.root,
  65 + this.colorFilter,
  66 + );
52 67
53 final PdfRect viewBox; 68 final PdfRect viewBox;
54 69
@@ -58,6 +73,8 @@ class SvgParser { @@ -58,6 +73,8 @@ class SvgParser {
58 73
59 final XmlElement root; 74 final XmlElement root;
60 75
  76 + final PdfColor? colorFilter;
  77 +
61 static final _transformParameterRegExp = 78 static final _transformParameterRegExp =
62 RegExp(r'[\w.-]+(px|pt|em|cm|mm|in|%|)'); 79 RegExp(r'[\w.-]+(px|pt|em|cm|mm|in|%|)');
63 80
@@ -31,9 +31,13 @@ class SvgImage extends Widget { @@ -31,9 +31,13 @@ class SvgImage extends Widget {
31 bool clip = true, 31 bool clip = true,
32 double? width, 32 double? width,
33 double? height, 33 double? height,
  34 + PdfColor? colorFilter,
34 }) { 35 }) {
35 final xml = XmlDocument.parse(svg); 36 final xml = XmlDocument.parse(svg);
36 - final parser = SvgParser(xml: xml); 37 + final parser = SvgParser(
  38 + xml: xml,
  39 + colorFilter: colorFilter,
  40 + );
37 41
38 return SvgImage._fromParser( 42 return SvgImage._fromParser(
39 parser, 43 parser,