parseStyle function

List<Code>? parseStyle(
  1. XmlElement elem
)

Parses paint styles from an SVG element and returns a list of Code statements to apply to a Paint() object.

Supports fill, stroke, stroke-width, stroke-linecap, and stroke-linejoin. Returns null if no valid style attributes are found or if the style is unsupported.

Example output:

..style = PaintingStyle.stroke
..strokeWidth = 2.0
..strokeCap = StrokeCap.round

Implementation

List<Code>? parseStyle(XmlElement elem) {
  final List<Code> stylesCodes = [];
  if (elem.getAttribute("fill") != null &&
      elem.getAttribute("fill") != "none") {
    stylesCodes.add(Code("..style = PaintingStyle.fill"));
  } else if (elem.getAttribute("stroke") != null) {
    stylesCodes.add(Code("..style = PaintingStyle.stroke"));
  } else {
    return null;
  }
  if (elem.getAttribute("stroke-width") != null) {
    stylesCodes.add(
      Code("..strokeWidth = ${elem.getAttribute("stroke-width")}"),
    );
  }
  if (elem.getAttribute("stroke-linecap") != null) {
    switch (elem.getAttribute("stroke-linecap")) {
      case "round":
        stylesCodes.add(Code("..strokeCap = StrokeCap.round"));
      case "square":
        stylesCodes.add(Code("..strokeCap = StrokeCap.square"));
      case "butt":
        stylesCodes.add(Code("..strokeCap = StrokeCap.butt"));
    }
  }
  if (elem.getAttribute("stroke-linejoin") != null) {
    switch (elem.getAttribute("stroke-linejoin")) {
      case "miter":
        stylesCodes.add(Code("..strokeJoin = strokeJoin.miter"));
      case "round":
        stylesCodes.add(Code("..strokeJoin = strokeJoin.round"));
      case "bevel":
        stylesCodes.add(Code("..strokeJoin = strokeJoin.bevel"));
    }
  }
  return stylesCodes;
}