package com.horcrux.svg;

import android.graphics.Paint;
import android.graphics.Path;

import java.util.ArrayList;

class GlyphPathBag {
    private final ArrayList<Path> paths = new ArrayList<>();
    private final int[][] data = new int[255][];
    private final Paint paint;

    GlyphPathBag(Paint paint) {
        this.paint = paint;
        // Make indexed-by-one, to allow zero to represent non-cached
        paths.add(new Path());
    }

    Path getOrCreateAndCache(char ch, String current) {
        int index = getIndex(ch);
        Path cached;

        if (index != 0) {
            cached = paths.get(index);
        } else {
            cached = new Path();
            paint.getTextPath(current, 0, 1, 0, 0, cached);

            int[] bin = data[ch >> 8];
            if (bin == null) {
                bin = data[ch >> 8] = new int[255];
            }
            bin[ch & 0xFF] = paths.size();

            paths.add(cached);
        }

        Path glyph = new Path();
        glyph.addPath(cached);
        return glyph;
    }

    private int getIndex(char ch) {
        int[] bin = data[ch >> 8];
        if (bin == null) return 0;
        return bin[ch & 0xFF];
    }
}

@@ -235,17 +235,17 @@
 w int%5B25
-5
+6
 %5D%5B%5D;%0A   
@@ -847,17 +847,17 @@
 w int%5B25
-5
+6
 %5D;%0A     

fix an "out of range" error