parseStyle method

dynamic parseStyle(
  1. dynamic node,
  2. dynamic style
)

Implementation

parseStyle(node, style) {
  // style = Object.assign( {}, style );
  // clone style
  Map<String, dynamic> style2 = <String, dynamic>{};
  style2.addAll(style);

  var stylesheetStyles = {};

  if (node.hasAttribute('class')) {
    var reg = RegExp(r"\s");
    var classSelectors = node
        .getAttribute('class')
        .split(reg)
        // .filter( Boolean )
        .map((i) => i.trim())
        .toList();

    for (var i = 0; i < classSelectors.length; i++) {
      // stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '.' + classSelectors[ i ] ] );
      stylesheetStyles.addAll(stylesheets['.${classSelectors[i]}'] ?? {});
    }
  }

  if (node.hasAttribute('id')) {
    // stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '#' + node.getAttribute( 'id' ) ] );

    stylesheetStyles.addAll(stylesheets['#${node.getAttribute('id')}'] ?? {});
  }

  void addStyle(svgName, jsName, [adjustFunction]) {
    adjustFunction ??= (v) {
      if (v.startsWith('url')) {
        print('SVGLoader: url access in attributes is not implemented.');
      }

      return v;
    };

    if (node.hasAttribute(svgName)) {
      style2[jsName] = adjustFunction(node.getAttribute(svgName));
    }
    if (stylesheetStyles[svgName] != null) {
      style2[jsName] = adjustFunction(stylesheetStyles[svgName]);
    }

    if (node.style != null) {
      var st = node.style;
      var val = st.getPropertyValue(svgName);
      if (val != "") {
        // print("svgName: ${svgName} value: ${_value} ");
        style2[jsName] = adjustFunction(val);
      }
    }
  }

  num clamp(v) {
    return Math.max<num>(0, Math.min(1, parseFloatWithUnits(v)));
  }

  num positive(v) {
    return Math.max<num>(0, parseFloatWithUnits(v));
  }

  addStyle('fill', 'fill');
  addStyle('fill-opacity', 'fillOpacity', clamp);
  addStyle('fill-rule', 'fillRule');
  addStyle('opacity', 'opacity', clamp);
  addStyle('stroke', 'stroke');
  addStyle('stroke-opacity', 'strokeOpacity', clamp);
  addStyle('stroke-width', 'strokeWidth', positive);
  addStyle('stroke-linejoin', 'strokeLineJoin');
  addStyle('stroke-linecap', 'strokeLineCap');
  addStyle('stroke-miterlimit', 'strokeMiterLimit', positive);
  addStyle('visibility', 'visibility');

  return style2;
}