1/*
2 * Copyright (c) 2000, 2016, 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.pipe;
27
28import java.awt.Rectangle;
29import java.awt.Shape;
30import sun.java2d.SunGraphics2D;
31import sun.font.GlyphList;
32
33/*
34 * This class uses the alpha graybits arrays from a GlyphList object to
35 * drive a CompositePipe in much the same way as the antialiasing renderer.
36 */
37public class TextRenderer extends GlyphListPipe {
38
39    CompositePipe outpipe;
40
41    public TextRenderer(CompositePipe pipe) {
42        outpipe = pipe;
43    }
44
45    protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) {
46        int num = gl.getNumGlyphs();
47        Region clipRegion = sg2d.getCompClip();
48        int cx1 = clipRegion.getLoX();
49        int cy1 = clipRegion.getLoY();
50        int cx2 = clipRegion.getHiX();
51        int cy2 = clipRegion.getHiY();
52        Object ctx = null;
53        try {
54            int[] bounds = gl.getBounds();
55            Rectangle r = new Rectangle(bounds[0], bounds[1],
56                                        bounds[2] - bounds[0],
57                                        bounds[3] - bounds[1]);
58            Shape s = sg2d.untransformShape(r);
59            ctx = outpipe.startSequence(sg2d, s, r, bounds);
60            for (int i = 0; i < num; i++) {
61                gl.setGlyphIndex(i);
62                int metrics[] = gl.getMetrics();
63                int gx1 = metrics[0];
64                int gy1 = metrics[1];
65                int w = metrics[2];
66                int gx2 = gx1 + w;
67                int gy2 = gy1 + metrics[3];
68                int off = 0;
69                if (gx1 < cx1) {
70                    off = cx1 - gx1;
71                    gx1 = cx1;
72                }
73                if (gy1 < cy1) {
74                    off += (cy1 - gy1) * w;
75                    gy1 = cy1;
76                }
77                if (gx2 > cx2) gx2 = cx2;
78                if (gy2 > cy2) gy2 = cy2;
79                if (gx2 > gx1 && gy2 > gy1 &&
80                    outpipe.needTile(ctx, gx1, gy1, gx2 - gx1, gy2 - gy1))
81                {
82                    byte alpha[] = gl.getGrayBits();
83                    outpipe.renderPathTile(ctx, alpha, off, w,
84                                           gx1, gy1, gx2 - gx1, gy2 - gy1);
85                } else {
86                    outpipe.skipTile(ctx, gx1, gy1);
87                }
88            }
89        } finally {
90            if (ctx != null) {
91                outpipe.endSequence(ctx);
92            }
93        }
94    }
95}
96