1 /*
2  * Copyright (c) 2000, 2005, 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.font.FontRenderContext;
29 import java.awt.font.GlyphVector;
30 import java.awt.font.TextLayout;
31 import sun.java2d.SunGraphics2D;
32 import sun.font.GlyphList;
33 import sun.awt.SunHints;
34 
35 import java.awt.Shape;
36 import java.awt.geom.AffineTransform;
37 import java.awt.font.TextLayout;
38 
39 /**
40  * A delegate pipe of SG2D for drawing "large" text with
41  * a solid source colour to an opaque destination.
42  * The text is drawn as a filled outline.
43  * Since the developer is not explicitly requesting this way of
44  * rendering, this should not be used if the current paint is not
45  * a solid colour.
46  *
47  * If text anti-aliasing is requested by the application, and
48  * filling path, an anti-aliasing fill pipe needs to
49  * be invoked.
50  * This involves making some of the same decisions as in the
51  * validatePipe call, which may be in a SurfaceData subclass, so
52  * its awkward to always ensure that the correct pipe is used.
53  * The easiest thing, rather than reproducing much of that logic
54  * is to call validatePipe() which works but is expensive, although
55  * probably not compared to the cost of filling the path.
56  * Note if AA hint is ON but text-AA hint is OFF this logic will
57  * produce AA text which perhaps isn't what the user expected.
58  * Note that the glyphvector obeys its FRC, not the G2D.
59  */
60 
61 public class OutlineTextRenderer implements TextPipe {
62 
63     // Text with a height greater than the threshhold will be
64     // drawn via this pipe.
65     public static final int THRESHHOLD = 100;
66 
drawChars(SunGraphics2D g2d, char data[], int offset, int length, int x, int y)67     public void drawChars(SunGraphics2D g2d,
68                           char data[], int offset, int length,
69                           int x, int y) {
70 
71         String s = new String(data, offset, length);
72         drawString(g2d, s, x, y);
73     }
74 
drawString(SunGraphics2D g2d, String str, double x, double y)75     public void drawString(SunGraphics2D g2d, String str, double x, double y) {
76 
77         if ("".equals(str)) {
78             return; // TextLayout constructor throws IAE on "".
79         }
80         TextLayout tl = new TextLayout(str, g2d.getFont(),
81                                        g2d.getFontRenderContext());
82         Shape s = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
83 
84         int textAAHint = g2d.getFontInfo().aaHint;
85 
86         int prevaaHint = - 1;
87         if (textAAHint != SunHints.INTVAL_TEXT_ANTIALIAS_OFF &&
88             g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
89             prevaaHint = g2d.antialiasHint;
90             g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_ON;
91             g2d.validatePipe();
92         } else if (textAAHint == SunHints.INTVAL_TEXT_ANTIALIAS_OFF
93             && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {
94             prevaaHint = g2d.antialiasHint;
95             g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_OFF;
96             g2d.validatePipe();
97         }
98 
99         g2d.fill(s);
100 
101         if (prevaaHint != -1) {
102              g2d.antialiasHint = prevaaHint;
103              g2d.validatePipe();
104         }
105     }
106 
drawGlyphVector(SunGraphics2D g2d, GlyphVector gv, float x, float y)107     public void drawGlyphVector(SunGraphics2D g2d, GlyphVector gv,
108                                 float x, float y) {
109 
110 
111         Shape s = gv.getOutline(x, y);
112         int prevaaHint = - 1;
113         FontRenderContext frc = gv.getFontRenderContext();
114         boolean aa = frc.isAntiAliased();
115 
116         /* aa will be true if any AA mode has been specified.
117          * ie for LCD and 'gasp' modes too.
118          * We will check if 'gasp' has resolved AA to be "OFF", and
119          * in all other cases (ie AA ON and all LCD modes) use AA outlines.
120          */
121         if (aa) {
122             if (g2d.getGVFontInfo(gv.getFont(), frc).aaHint ==
123                 SunHints.INTVAL_TEXT_ANTIALIAS_OFF) {
124                 aa = false;
125             }
126         }
127 
128         if (aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
129             prevaaHint = g2d.antialiasHint;
130             g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_ON;
131             g2d.validatePipe();
132         } else if (!aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {
133             prevaaHint = g2d.antialiasHint;
134             g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_OFF;
135             g2d.validatePipe();
136         }
137 
138         g2d.fill(s);
139 
140         if (prevaaHint != -1) {
141              g2d.antialiasHint = prevaaHint;
142              g2d.validatePipe();
143         }
144     }
145 }
146