getOrBuild method

PathSelectionContoursV2 getOrBuild({
  1. required PathNodeSnapshot node,
  2. required Path localPath,
})

Implementation

PathSelectionContoursV2 getOrBuild({
  required PathNodeSnapshot node,
  required Path localPath,
}) {
  final cached = _entries.remove(node.id);
  if (cached != null &&
      cached.svgPathData == node.svgPathData &&
      cached.fillRule == node.fillRule) {
    _entries[node.id] = cached;
    _debugHitCount += 1;
    return cached.contours;
  }

  final fillType = _fillTypeFromSnapshot(node.fillRule);
  Path? closedContours;
  final openContours = <Path>[];
  for (final metric in localPath.computeMetrics()) {
    final contour = metric.extractPath(
      0,
      metric.length,
      startWithMoveTo: true,
    );
    contour.fillType = fillType;
    if (metric.isClosed) {
      contour.close();
      closedContours ??= Path()..fillType = fillType;
      closedContours.addPath(contour, Offset.zero);
    } else {
      openContours.add(contour);
    }
  }

  final contours = PathSelectionContoursV2(
    closedContours: closedContours,
    openContours: openContours,
  );
  _entries[node.id] = _PathMetricsEntryV2(
    svgPathData: node.svgPathData,
    fillRule: node.fillRule,
    contours: contours,
  );
  _debugBuildCount += 1;
  _evictIfNeeded();
  return contours;
}