1/*
2 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.java2d.loops;
27
28import sun.java2d.loops.GraphicsPrimitive;
29import sun.java2d.pipe.Region;
30import sun.java2d.SunGraphics2D;
31import sun.java2d.SurfaceData;
32import sun.font.GlyphList;
33
34/**
35 *   DrawGlyphListAA - loops for AATextRenderer pipe
36 *   1) draw anti-aliased text onto destination surface
37 *   2) must accept output area [x, y, dx, dy]
38 *      from within the surface description data for clip rect
39 */
40public class DrawGlyphListAA extends GraphicsPrimitive {
41
42    public static final String methodSignature = "DrawGlyphListAA(...)".toString();
43
44    public static final int primTypeID = makePrimTypeID();
45
46    public static DrawGlyphListAA locate(SurfaceType srctype,
47                                   CompositeType comptype,
48                                   SurfaceType dsttype)
49    {
50        return (DrawGlyphListAA)
51            GraphicsPrimitiveMgr.locate(primTypeID,
52                                        srctype, comptype, dsttype);
53    }
54
55    protected DrawGlyphListAA(SurfaceType srctype,
56                         CompositeType comptype,
57                         SurfaceType dsttype)
58    {
59        super(methodSignature, primTypeID, srctype, comptype, dsttype);
60    }
61
62    public DrawGlyphListAA(long pNativePrim,
63                           SurfaceType srctype,
64                           CompositeType comptype,
65                           SurfaceType dsttype)
66    {
67        super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);
68    }
69
70    public native void DrawGlyphListAA(SunGraphics2D sg2d, SurfaceData dest,
71                                       GlyphList srcData);
72
73    static {
74        GraphicsPrimitiveMgr.registerGeneral(
75                                   new DrawGlyphListAA(null, null, null));
76    }
77
78    public GraphicsPrimitive makePrimitive(SurfaceType srctype,
79                                           CompositeType comptype,
80                                           SurfaceType dsttype) {
81        return new General(srctype, comptype, dsttype);
82    }
83
84    public static class General extends DrawGlyphListAA {
85        MaskFill maskop;
86
87        public General(SurfaceType srctype,
88                       CompositeType comptype,
89                       SurfaceType dsttype)
90        {
91            super(srctype, comptype, dsttype);
92            maskop = MaskFill.locate(srctype, comptype, dsttype);
93        }
94
95        public void DrawGlyphListAA(SunGraphics2D sg2d, SurfaceData dest,
96                                    GlyphList gl)
97        {
98            gl.getBounds(); // Don't delete, bug 4895493
99            int num = gl.getNumGlyphs();
100            Region clip = sg2d.getCompClip();
101            int cx1 = clip.getLoX();
102            int cy1 = clip.getLoY();
103            int cx2 = clip.getHiX();
104            int cy2 = clip.getHiY();
105            for (int i = 0; i < num; i++) {
106                gl.setGlyphIndex(i);
107                int metrics[] = gl.getMetrics();
108                int gx1 = metrics[0];
109                int gy1 = metrics[1];
110                int w = metrics[2];
111                int gx2 = gx1 + w;
112                int gy2 = gy1 + metrics[3];
113                int off = 0;
114                if (gx1 < cx1) {
115                    off = cx1 - gx1;
116                    gx1 = cx1;
117                }
118                if (gy1 < cy1) {
119                    off += (cy1 - gy1) * w;
120                    gy1 = cy1;
121                }
122                if (gx2 > cx2) gx2 = cx2;
123                if (gy2 > cy2) gy2 = cy2;
124                if (gx2 > gx1 && gy2 > gy1) {
125                    byte alpha[] = gl.getGrayBits();
126                    maskop.MaskFill(sg2d, dest, sg2d.composite,
127                                    gx1, gy1, gx2 - gx1, gy2 - gy1,
128                                    alpha, off, w);
129                }
130            }
131        }
132    }
133
134    public GraphicsPrimitive traceWrap() {
135        return new TraceDrawGlyphListAA(this);
136    }
137
138    private static class TraceDrawGlyphListAA extends DrawGlyphListAA {
139        DrawGlyphListAA target;
140
141        public TraceDrawGlyphListAA(DrawGlyphListAA target) {
142            super(target.getSourceType(),
143                  target.getCompositeType(),
144                  target.getDestType());
145            this.target = target;
146        }
147
148        public GraphicsPrimitive traceWrap() {
149            return this;
150        }
151
152        public void DrawGlyphListAA(SunGraphics2D sg2d, SurfaceData dest,
153                                    GlyphList glyphs)
154        {
155            tracePrimitive(target);
156            target.DrawGlyphListAA(sg2d, dest, glyphs);
157        }
158    }
159}
160