001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.pisces;
027:
028: /**
029: * An object used to cache pre-rendered complex paths.
030: *
031: * @see PiscesRenderer#render
032: */
033: public final class PiscesCache {
034:
035: boolean isValid = false;
036:
037: int bboxX0, bboxY0, bboxX1, bboxY1;
038:
039: byte[] rowAARLE = null;
040: int alphaRLELength = 0;
041: int[] rowOffsetsRLE = null;
042:
043: int alphaWidth = 0;
044: int alphaHeight = 0;
045:
046: int[] minTouched = null;
047:
048: private PiscesCache() {
049: }
050:
051: public static PiscesCache createInstance() {
052: return new PiscesCache();
053: }
054:
055: private static final float ROWAA_RLE_FACTOR = 1.2f;
056: private static final float TOUCHED_FACTOR = 1.2f;
057: private static final int MIN_TOUCHED_LEN = 32;
058:
059: private void reallocRowAARLE(int newLength) {
060: if (rowAARLE == null) {
061: rowAARLE = new byte[newLength];
062: } else if (rowAARLE.length < newLength) {
063: int len = Math.max(newLength,
064: (int) (rowAARLE.length * ROWAA_RLE_FACTOR));
065: byte[] newRowAARLE = new byte[len];
066: System.arraycopy(rowAARLE, 0, newRowAARLE, 0,
067: rowAARLE.length);
068: rowAARLE = newRowAARLE;
069: }
070: }
071:
072: private void reallocRowInfo(int newHeight) {
073: if (minTouched == null) {
074: int len = Math.max(newHeight, MIN_TOUCHED_LEN);
075: minTouched = new int[len];
076: rowOffsetsRLE = new int[len];
077: } else if (minTouched.length < newHeight) {
078: int len = Math.max(newHeight,
079: (int) (minTouched.length * TOUCHED_FACTOR));
080: int[] newMinTouched = new int[len];
081: int[] newRowOffsetsRLE = new int[len];
082: System.arraycopy(minTouched, 0, newMinTouched, 0,
083: minTouched.length);
084: System.arraycopy(rowOffsetsRLE, 0, newRowOffsetsRLE, 0,
085: rowOffsetsRLE.length);
086: minTouched = newMinTouched;
087: rowOffsetsRLE = newRowOffsetsRLE;
088: }
089: }
090:
091: void addRLERun(byte val, int runLen) {
092: reallocRowAARLE(alphaRLELength + 2);
093: rowAARLE[alphaRLELength++] = val;
094: rowAARLE[alphaRLELength++] = (byte) runLen;
095: }
096:
097: void addRow(int minX, int offset) {
098: reallocRowInfo(alphaHeight + 1);
099: minTouched[alphaHeight] = minX;
100: rowOffsetsRLE[alphaHeight] = offset;
101: ++alphaHeight;
102: }
103:
104: public synchronized boolean isValid() {
105: return isValid;
106: }
107:
108: public synchronized void dispose() {
109: alphaWidth = alphaHeight = 0;
110:
111: rowAARLE = null;
112: alphaRLELength = 0;
113:
114: minTouched = null;
115: rowOffsetsRLE = null;
116:
117: isValid = false;
118: }
119: }
|