1/*
2 * Copyright (c) 2000, 2003, 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 *   DrawGlyphList - loops for SolidTextRenderer pipe.
36 *   1) draw solid color 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 DrawGlyphList extends GraphicsPrimitive {
41
42    public static final String methodSignature = "DrawGlyphList(...)".toString();
43
44    public static final int primTypeID = makePrimTypeID();
45
46    public static DrawGlyphList locate(SurfaceType srctype,
47                                   CompositeType comptype,
48                                   SurfaceType dsttype)
49    {
50        return (DrawGlyphList)
51            GraphicsPrimitiveMgr.locate(primTypeID,
52                                        srctype, comptype, dsttype);
53    }
54
55    protected DrawGlyphList(SurfaceType srctype,
56                         CompositeType comptype,
57                         SurfaceType dsttype)
58    {
59        super(methodSignature, primTypeID, srctype, comptype, dsttype);
60    }
61
62    public DrawGlyphList(long pNativePrim,
63                         SurfaceType srctype,
64                         CompositeType comptype,
65                         SurfaceType dsttype)
66    {
67        super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);
68    }
69
70
71    public native void DrawGlyphList(SunGraphics2D sg2d, SurfaceData dest,
72                                     GlyphList srcData);
73
74    // This instance is used only for lookup.
75    static {
76        GraphicsPrimitiveMgr.registerGeneral(
77                                new DrawGlyphList(null, null, null));
78    }
79
80    public GraphicsPrimitive makePrimitive(SurfaceType srctype,
81                                           CompositeType comptype,
82                                           SurfaceType dsttype) {
83        return new General(srctype, comptype, dsttype);
84    }
85
86    private static class General extends DrawGlyphList {
87        MaskFill maskop;
88
89        public General(SurfaceType srctype,
90                       CompositeType comptype,
91                       SurfaceType dsttype)
92        {
93            super(srctype, comptype, dsttype);
94            maskop = MaskFill.locate(srctype, comptype, dsttype);
95        }
96
97        public void DrawGlyphList(SunGraphics2D sg2d, SurfaceData dest,
98                                  GlyphList gl) {
99
100            int strbounds[] = gl.getBounds(); // Don't delete, bug 4895493
101            int num = gl.getNumGlyphs();
102            Region clip = sg2d.getCompClip();
103            int cx1 = clip.getLoX();
104            int cy1 = clip.getLoY();
105            int cx2 = clip.getHiX();
106            int cy2 = clip.getHiY();
107            for (int i = 0; i < num; i++) {
108                gl.setGlyphIndex(i);
109                int metrics[] = gl.getMetrics();
110                int gx1 = metrics[0];
111                int gy1 = metrics[1];
112                int w = metrics[2];
113                int gx2 = gx1 + w;
114                int gy2 = gy1 + metrics[3];
115                int off = 0;
116                if (gx1 < cx1) {
117                    off = cx1 - gx1;
118                    gx1 = cx1;
119                }
120                if (gy1 < cy1) {
121                    off += (cy1 - gy1) * w;
122                    gy1 = cy1;
123                }
124                if (gx2 > cx2) gx2 = cx2;
125                if (gy2 > cy2) gy2 = cy2;
126                if (gx2 > gx1 && gy2 > gy1) {
127                    byte alpha[] = gl.getGrayBits();
128                    maskop.MaskFill(sg2d, dest, sg2d.composite,
129                                    gx1, gy1, gx2 - gx1, gy2 - gy1,
130                                    alpha, off, w);
131                }
132            }
133        }
134    }
135
136    public GraphicsPrimitive traceWrap() {
137        return new TraceDrawGlyphList(this);
138    }
139
140    private static class TraceDrawGlyphList extends DrawGlyphList {
141        DrawGlyphList target;
142
143        public TraceDrawGlyphList(DrawGlyphList target) {
144            super(target.getSourceType(),
145                  target.getCompositeType(),
146                  target.getDestType());
147            this.target = target;
148        }
149
150        public GraphicsPrimitive traceWrap() {
151            return this;
152        }
153
154        public void DrawGlyphList(SunGraphics2D sg2d, SurfaceData dest,
155                                  GlyphList glyphs)
156        {
157            tracePrimitive(target);
158            target.DrawGlyphList(sg2d, dest, glyphs);
159        }
160    }
161}
162