finish method

void finish({
  1. List<double>? color,
  2. List<double>? fill,
  3. double width = 1,
  4. List<double>? dashes,
  5. int lineCap = 0,
  6. int lineJoin = 0,
  7. bool closePath = false,
  8. bool even_odd = false,
  9. double opacity = 1,
  10. double fillOpacity = 1,
  11. String? morph,
})

Finish the current path with stroke/fill properties.

Equivalent to PyMuPDF's shape.finish().

Implementation

void finish({
  List<double>? color,
  List<double>? fill,
  double width = 1,
  List<double>? dashes,
  int lineCap = 0,
  int lineJoin = 0,
  bool closePath = false,
  bool even_odd = false,
  double opacity = 1,
  double fillOpacity = 1,
  String? morph,
}) {
  if (_pathOps == 0) return;

  final cmd = StringBuffer();
  cmd.writeln('q');

  // Line cap
  if (lineCap != 0) cmd.writeln('$lineCap J');

  // Line join
  if (lineJoin != 0) cmd.writeln('$lineJoin j');

  // Dash pattern
  if (dashes != null && dashes.isNotEmpty) {
    cmd.write('[');
    for (int i = 0; i < dashes.length; i++) {
      if (i > 0) cmd.write(' ');
      cmd.write(_f(dashes[i]));
    }
    cmd.writeln('] 0 d');
  }

  // Line width
  cmd.writeln('${_f(width)} w');

  // Stroke color
  if (color != null) {
    if (color.length == 1) {
      cmd.writeln('${_f(color[0])} G');
    } else if (color.length == 3) {
      cmd.writeln('${_f(color[0])} ${_f(color[1])} ${_f(color[2])} RG');
    } else if (color.length == 4) {
      cmd.writeln(
        '${_f(color[0])} ${_f(color[1])} ${_f(color[2])} ${_f(color[3])} K',
      );
    }
  }

  // Fill color
  if (fill != null) {
    if (fill.length == 1) {
      cmd.writeln('${_f(fill[0])} g');
    } else if (fill.length == 3) {
      cmd.writeln('${_f(fill[0])} ${_f(fill[1])} ${_f(fill[2])} rg');
    } else if (fill.length == 4) {
      cmd.writeln(
        '${_f(fill[0])} ${_f(fill[1])} ${_f(fill[2])} ${_f(fill[3])} k',
      );
    }
  }

  // Path
  cmd.write(_path.toString());
  if (closePath) cmd.write('h ');

  // Painting operator
  if (fill != null && color != null) {
    cmd.writeln(even_odd ? 'B*' : 'B'); // fill and stroke
  } else if (fill != null) {
    cmd.writeln(even_odd ? 'f*' : 'f'); // fill only
  } else if (color != null) {
    cmd.writeln('S'); // stroke only
  } else {
    cmd.writeln('n'); // no-op (path only)
  }

  cmd.writeln('Q');

  _content.write(cmd.toString());
  _path.clear();
  _pathOps = 0;
  totalContents++;
}