1 /*
2  * Copyright (c) 2000, 2018, 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 
26 package sun.java2d.pipe;
27 
28 import java.awt.Rectangle;
29 import java.awt.Shape;
30 import sun.java2d.SunGraphics2D;
31 import 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  */
37 public class TextRenderer extends GlyphListPipe {
38 
39     CompositePipe outpipe;
40 
TextRenderer(CompositePipe pipe)41     public TextRenderer(CompositePipe pipe) {
42         outpipe = pipe;
43     }
44 
drawGlyphList(SunGraphics2D sg2d, GlyphList gl)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             gl.startGlyphIteration();
55             int[] bounds = gl.getBounds(num);
56             Rectangle r = new Rectangle(bounds[0], bounds[1],
57                                         bounds[2] - bounds[0],
58                                         bounds[3] - bounds[1]);
59             Shape s = sg2d.untransformShape(r);
60             ctx = outpipe.startSequence(sg2d, s, r, bounds);
61             for (int i = 0; i < num; i++) {
62                 gl.setGlyphIndex(i);
63                 int[] metrics = gl.getMetrics();
64                 int gx1 = metrics[0];
65                 int gy1 = metrics[1];
66                 int w = metrics[2];
67                 int gx2 = gx1 + w;
68                 int gy2 = gy1 + metrics[3];
69                 int off = 0;
70                 if (gx1 < cx1) {
71                     off = cx1 - gx1;
72                     gx1 = cx1;
73                 }
74                 if (gy1 < cy1) {
75                     off += (cy1 - gy1) * w;
76                     gy1 = cy1;
77                 }
78                 if (gx2 > cx2) gx2 = cx2;
79                 if (gy2 > cy2) gy2 = cy2;
80                 if (gx2 > gx1 && gy2 > gy1 && !gl.isColorGlyph(i) &&
81                     outpipe.needTile(ctx, gx1, gy1, gx2 - gx1, gy2 - gy1))
82                 {
83                     byte[] alpha = gl.getGrayBits();
84                     outpipe.renderPathTile(ctx, alpha, off, w,
85                                            gx1, gy1, gx2 - gx1, gy2 - gy1);
86                 } else {
87                     outpipe.skipTile(ctx, gx1, gy1);
88                 }
89             }
90         } finally {
91             if (ctx != null) {
92                 outpipe.endSequence(ctx);
93             }
94         }
95     }
96 }
97