Skip to content

Commit

Permalink
Clean up gradient stop style API: #117
Browse files Browse the repository at this point in the history
  • Loading branch information
zathras committed Sep 14, 2024
1 parent b62c062 commit ea26ee7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export 'src/svg_graph.dart'
SvgValueColor,
SvgColorReference,
SvgGradientStop,
SvgGradientStopStyle,
SvgGradientColor,
SvgCoordinate,
SvgLinearGradientColor,
Expand Down
45 changes: 41 additions & 4 deletions lib/src/svg_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 .
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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;
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/svg_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ea26ee7

Please sign in to comment.