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