All files / packages/tools/src/drawingSvg drawPath.ts

87.09% Statements 27/31
54.54% Branches 12/22
100% Functions 1/1
85.71% Lines 24/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                            1x   1x                 1x     1x   1x 1x 1x 1x   1x 1x 1x   1x       1x 640x 640x   640x         1x 1x       1x       1x                 1x           1x   1x 1x      
import type { Types } from '@cornerstonejs/core';
import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import { SVGDrawingHelper } from '../types';
 
/**
 * Draws an SVG path with the given points.
 *
 * The `closePath` option, if true, draws a closed path (last point
 * connected to the first).
 */
export default function drawPath(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  pathUID: string,
  points: Types.Point2[] | Types.Point2[][],
  options: {
    color?: string;
    fillColor?: string;
    fillOpacity?: number;
    width?: number;
    lineWidth?: number;
    lineDash?: string;
    closePath?: boolean;
  }
): void {
  // It may be a polyline with holes that will be an array with multiple
  // 'points' arrays
  const hasSubArrays =
    points.length && points[0].length && Array.isArray(points[0][0]);
 
  const pointsArrays = hasSubArrays ? points : [points];
  const {
    color = 'rgb(0, 255, 0)',
    width = 10,
    fillColor = 'none',
    fillOpacity = 0,
    lineWidth,
    lineDash,
    closePath = false,
  } = options;
 
  // for supporting both lineWidth and width options
  const strokeWidth = lineWidth || width;
 
  const svgns = 'http://www.w3.org/2000/svg';
  const svgNodeHash = _getHash(annotationUID, 'path', pathUID);
  const existingNode = svgDrawingHelper.getSvgNode(svgNodeHash);
  let pointsAttribute = '';
 
  for (let i = 0, numArrays = pointsArrays.length; i < numArrays; i++) {
    const points = pointsArrays[i];
    const numPoints = points.length;
 
    Iif (numPoints < 2) {
      continue;
    }
 
    for (let j = 0; j < numPoints; j++) {
      const point = points[j];
      const cmd = j ? 'L' : 'M';
 
      pointsAttribute += `${cmd} ${point[0].toFixed(1)}, ${point[1].toFixed(
        1
      )} `;
    }
 
    Eif (closePath) {
      pointsAttribute += 'Z ';
    }
  }
 
  Iif (!pointsAttribute) {
    return;
  }
 
  const attributes = {
    d: pointsAttribute,
    stroke: color,
    fill: fillColor,
    'fill-opacity': fillOpacity,
    'stroke-width': strokeWidth,
    'stroke-dasharray': lineDash,
  };
 
  Iif (existingNode) {
    // This is run to avoid re-rendering annotations that actually haven't changed
    setAttributesIfNecessary(attributes, existingNode);
 
    svgDrawingHelper.setNodeTouched(svgNodeHash);
  } else {
    const newNode = document.createElementNS(svgns, 'path');
 
    setNewAttributesIfValid(attributes, newNode);
    svgDrawingHelper.appendNode(newNode, svgNodeHash);
  }
}