001: /*
002: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.pipe;
027:
028: import java.awt.BasicStroke;
029: import java.awt.Rectangle;
030: import java.awt.Shape;
031: import java.awt.geom.PathIterator;
032: import sun.awt.SunHints;
033: import sun.java2d.SunGraphics2D;
034:
035: /**
036: * This class is used to convert raw geometry into 8-bit alpha tiles
037: * using an AATileGenerator for application by the next stage of
038: * the pipeline.
039: * This class sets up the Generator and computes the alpha tiles
040: * and then passes them on to a CompositePipe object for painting.
041: */
042: public class AAShapePipe implements ShapeDrawPipe {
043: static RenderingEngine renderengine = RenderingEngine.getInstance();
044:
045: CompositePipe outpipe;
046:
047: public AAShapePipe(CompositePipe pipe) {
048: outpipe = pipe;
049: }
050:
051: public void draw(SunGraphics2D sg, Shape s) {
052: BasicStroke bs;
053:
054: if (sg.stroke instanceof BasicStroke) {
055: bs = (BasicStroke) sg.stroke;
056: } else {
057: s = sg.stroke.createStrokedShape(s);
058: bs = null;
059: }
060:
061: renderPath(sg, s, bs);
062: }
063:
064: public void fill(SunGraphics2D sg, Shape s) {
065: renderPath(sg, s, null);
066: }
067:
068: private static byte[] theTile;
069:
070: public synchronized static byte[] getAlphaTile(int len) {
071: byte[] t = theTile;
072: if (t == null || t.length < len) {
073: t = new byte[len];
074: } else {
075: theTile = null;
076: }
077: return t;
078: }
079:
080: public synchronized static void dropAlphaTile(byte[] t) {
081: theTile = t;
082: }
083:
084: public void renderPath(SunGraphics2D sg, Shape s, BasicStroke bs) {
085: boolean adjust = (bs != null && sg.strokeHint != SunHints.INTVAL_STROKE_PURE);
086: boolean thin = (sg.strokeState <= sg.STROKE_THINDASHED);
087: Object context = null;
088: byte alpha[] = null;
089:
090: Region clip = sg.getCompClip();
091: int abox[] = new int[4];
092: AATileGenerator aatg = renderengine.getAATileGenerator(s,
093: sg.transform, clip, bs, thin, adjust, abox);
094: if (aatg == null) {
095: // Nothing to render
096: return;
097: }
098:
099: try {
100: context = outpipe.startSequence(sg, s, new Rectangle(
101: abox[0], abox[1], abox[2] - abox[0], abox[3]
102: - abox[1]), abox);
103:
104: int tw = aatg.getTileWidth();
105: int th = aatg.getTileHeight();
106: alpha = getAlphaTile(tw * th);
107:
108: byte[] atile;
109:
110: for (int y = abox[1]; y < abox[3]; y += th) {
111: for (int x = abox[0]; x < abox[2]; x += tw) {
112: int w = Math.min(tw, abox[2] - x);
113: int h = Math.min(th, abox[3] - y);
114:
115: int a = aatg.getTypicalAlpha();
116: if (a == 0x00
117: || outpipe.needTile(context, x, y, w, h) == false) {
118: aatg.nextTile();
119: outpipe.skipTile(context, x, y);
120: continue;
121: }
122: if (a == 0xff) {
123: atile = null;
124: aatg.nextTile();
125: } else {
126: atile = alpha;
127: aatg.getAlpha(alpha, 0, tw);
128: }
129:
130: outpipe.renderPathTile(context, atile, 0, tw, x, y,
131: w, h);
132: }
133: }
134: } finally {
135: aatg.dispose();
136: if (context != null) {
137: outpipe.endSequence(context);
138: }
139: if (alpha != null) {
140: dropAlphaTile(alpha);
141: }
142: }
143: }
144: }
|