From ea26ee7898b093fc971a22105e2ea3076f7e151f Mon Sep 17 00:00:00 2001 From: Bill Foote Date: Sat, 14 Sep 2024 12:09:26 -0700 Subject: [PATCH] Clean up gradient stop style API: #117 --- lib/dom.dart | 1 + lib/src/svg_graph.dart | 45 +++++++++++++++++++++++++++++++++++++---- lib/src/svg_parser.dart | 4 +++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/dom.dart b/lib/dom.dart index f8362ed..0cb3255 100644 --- a/lib/dom.dart +++ b/lib/dom.dart @@ -92,6 +92,7 @@ export 'src/svg_graph.dart' SvgValueColor, SvgColorReference, SvgGradientStop, + SvgGradientStopStyle, SvgGradientColor, SvgCoordinate, SvgLinearGradientColor, diff --git a/lib/src/svg_graph.dart b/lib/src/svg_graph.dart index 5fc44e1..0d565bb 100644 --- a/lib/src/svg_graph.dart +++ b/lib/src/svg_graph.dart @@ -373,7 +373,7 @@ class Style extends SvgInheritableAttributes { /// /// The gradient stop attributes for this style. /// - SvgGradientStop? gradientStop; + SvgGradientStopStyle? gradientStop; // We inherit an applyStyle implementation that's unreachable, so we need to // stub this out. @@ -2700,6 +2700,20 @@ class SvgColorReference extends SvgColor { throw StateError('Internal error: color inheritance'); } +/// +/// The style attributes for an SVG gradient stop. See +/// https://www.w3.org/TR/2008/REC-SVGTiny12-20081222/painting.html#Gradients . +/// +/// {@category SVG DOM} +/// +class SvgGradientStopStyle { + double? offset; + SvgColor? color; + int? alpha; + + SvgGradientStopStyle(this.offset, this.color, this.alpha); +} + /// /// An SVG gradient stop. See /// https://www.w3.org/TR/2008/REC-SVGTiny12-20081222/painting.html#Gradients . @@ -2710,10 +2724,22 @@ class SvgGradientStop extends _HasStylesheet { double? _offset; SvgColor? _color; // But not a gradient! int? _alpha; + + /// The offset, or 0 if it is not set. See [offsetIsSet]. offset cannot + /// return null due to backwards compatibility constraints. double get offset => _offset ?? 0.0; + bool get offsetIsSet => _offset != null; + + /// The offset, or black if it is not set. See [colorIsSet]. color + /// cannot return null due to backwards compatibility constraints. SvgColor get color => _color ?? SvgColor.black; // But not a gradient! + bool get colorIsSet => _offset != null; //default stop-color is 'black'. Ref https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stop-color + + /// The alpha, or 0xff if it is not set. See [alphaIsSet]. alpha + /// cannot return null due to backwards compatibility constraints. int get alpha => _alpha ?? 0xff; + bool get alphaIsSet => _offset != null; /// /// The class of this stop for applying stylesheets @@ -2739,6 +2765,17 @@ class SvgGradientStop extends _HasStylesheet { } } + /// + /// Get the style attributes for this stop, or null if none are set + /// + SvgGradientStopStyle? getStopStyle() { + if (alphaIsSet || colorIsSet || offsetIsSet) { + return SvgGradientStopStyle(_offset, _color, _alpha); + } else { + return null; + } + } + /// /// Apply the stylesheet. If it results in a change, create a new stop /// with the changes; otherwise, return null. @@ -2762,9 +2799,9 @@ class SvgGradientStop extends _HasStylesheet { @override void _takeFrom(Style s, void Function(String) warn) { - _alpha ??= s.gradientStop?._alpha; - _color ??= s.gradientStop?._color; - _offset ??= s.gradientStop?._offset; + _alpha ??= s.gradientStop?.alpha; + _color ??= s.gradientStop?.color; + _offset ??= s.gradientStop?.offset; } } diff --git a/lib/src/svg_parser.dart b/lib/src/svg_parser.dart index 3f4f9dd..93ac21f 100644 --- a/lib/src/svg_parser.dart +++ b/lib/src/svg_parser.dart @@ -1056,7 +1056,9 @@ abstract class SvgParser extends GenericParser { // Unlike a node, our styleClass doesn't come from the // parser. A badly formed CSS entry could try to set an attribute // called 'class,' so we set styleClass last. - s.gradientStop = _processStop(attrs); + + final stop = _processStop(attrs); + s.gradientStop = stop?.getStopStyle(); } } assert(lastPos != pos);