public CachedPathIteratorFactory()

in shadows/framework/src/main/java/org/robolectric/shadows/CachedPathIteratorFactory.java [33:119]


  public CachedPathIteratorFactory(PathIterator iterator) {
    mWindingRule = iterator.getWindingRule();

    ArrayList<Integer> typesArray = new ArrayList<>();
    ArrayList<float[]> pointsArray = new ArrayList<>();
    float[] points = new float[6];
    while (!iterator.isDone()) {
      int type = iterator.currentSegment(points);
      int nPoints = getNumberOfPoints(type) * 2; // 2 coordinates per point

      typesArray.add(type);
      float[] itemPoints = new float[nPoints];
      System.arraycopy(points, 0, itemPoints, 0, nPoints);
      pointsArray.add(itemPoints);
      iterator.next();
    }

    mTypes = new int[typesArray.size()];
    mCoordinates = new float[mTypes.length][];
    for (int i = 0; i < typesArray.size(); i++) {
      mTypes[i] = typesArray.get(i);
      mCoordinates[i] = pointsArray.get(i);
    }

    // Do measurement
    mSegmentsLength = new float[mTypes.length];

    // Curves that we can reuse to estimate segments length
    CubicCurve2D.Float cubicCurve = new CubicCurve2D.Float();
    QuadCurve2D.Float quadCurve = new QuadCurve2D.Float();
    float lastX = 0;
    float lastY = 0;
    float totalLength = 0;
    for (int i = 0; i < mTypes.length; i++) {
      switch (mTypes[i]) {
        case PathIterator.SEG_CUBICTO:
          cubicCurve.setCurve(
              lastX,
              lastY,
              mCoordinates[i][0],
              mCoordinates[i][1],
              mCoordinates[i][2],
              mCoordinates[i][3],
              lastX = mCoordinates[i][4],
              lastY = mCoordinates[i][5]);
          mSegmentsLength[i] = getFlatPathLength(cubicCurve.getPathIterator(null, PRECISION));
          break;
        case PathIterator.SEG_QUADTO:
          quadCurve.setCurve(
              lastX,
              lastY,
              mCoordinates[i][0],
              mCoordinates[i][1],
              lastX = mCoordinates[i][2],
              lastY = mCoordinates[i][3]);
          mSegmentsLength[i] = getFlatPathLength(quadCurve.getPathIterator(null, PRECISION));
          break;
        case PathIterator.SEG_CLOSE:
          mSegmentsLength[i] =
              (float)
                  Point2D.distance(
                      lastX, lastY, lastX = mCoordinates[0][0], lastY = mCoordinates[0][1]);
          mCoordinates[i] = new float[2];
          // We convert a SEG_CLOSE segment to a SEG_LINETO so we do not have to worry
          // about this special case in the rest of the code.
          mTypes[i] = PathIterator.SEG_LINETO;
          mCoordinates[i][0] = mCoordinates[0][0];
          mCoordinates[i][1] = mCoordinates[0][1];
          break;
        case PathIterator.SEG_MOVETO:
          mSegmentsLength[i] = 0;
          lastX = mCoordinates[i][0];
          lastY = mCoordinates[i][1];
          break;
        case PathIterator.SEG_LINETO:
          mSegmentsLength[i] =
              (float) Point2D.distance(lastX, lastY, mCoordinates[i][0], mCoordinates[i][1]);
          lastX = mCoordinates[i][0];
          lastY = mCoordinates[i][1];
          break;
        default:
      }
      totalLength += mSegmentsLength[i];
    }

    mTotalLength = totalLength;
  }