001: /*
002: * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.pipe;
027:
028: import java.awt.font.FontRenderContext;
029: import java.awt.font.GlyphVector;
030: import java.awt.font.TextLayout;
031: import sun.java2d.SunGraphics2D;
032: import sun.font.GlyphList;
033: import sun.awt.SunHints;
034:
035: import java.awt.Shape;
036: import java.awt.geom.AffineTransform;
037: import java.awt.font.TextLayout;
038:
039: /**
040: * A delegate pipe of SG2D for drawing "large" text with
041: * a solid source colour to an opaque destination.
042: * The text is drawn as a filled outline.
043: * Since the developer is not explicitly requesting this way of
044: * rendering, this should not be used if the current paint is not
045: * a solid colour.
046: *
047: * If text anti-aliasing is requested by the application, and
048: * filling path, an anti-aliasing fill pipe needs to
049: * be invoked.
050: * This involves making some of the same decisions as in the
051: * validatePipe call, which may be in a SurfaceData subclass, so
052: * its awkward to always ensure that the correct pipe is used.
053: * The easiest thing, rather than reproducing much of that logic
054: * is to call validatePipe() which works but is expensive, although
055: * probably not compared to the cost of filling the path.
056: * Note if AA hint is ON but text-AA hint is OFF this logic will
057: * produce AA text which perhaps isn't what the user expected.
058: * Note that the glyphvector obeys its FRC, not the G2D.
059: */
060:
061: public class OutlineTextRenderer implements TextPipe {
062:
063: // Text with a height greater than the threshhold will be
064: // drawn via this pipe.
065: public static final int THRESHHOLD = 100;
066:
067: public void drawChars(SunGraphics2D g2d, char data[], int offset,
068: int length, int x, int y) {
069:
070: String s = new String(data, offset, length);
071: drawString(g2d, s, x, y);
072: }
073:
074: public void drawString(SunGraphics2D g2d, String str, double x,
075: double y) {
076:
077: if ("".equals(str)) {
078: return; // TextLayout constructor throws IAE on "".
079: }
080: TextLayout tl = new TextLayout(str, g2d.getFont(), g2d
081: .getFontRenderContext());
082: Shape s = tl.getOutline(AffineTransform.getTranslateInstance(x,
083: y));
084:
085: int textAAHint = g2d.getFontInfo().aaHint;
086:
087: int prevaaHint = -1;
088: if (textAAHint != SunHints.INTVAL_TEXT_ANTIALIAS_OFF
089: && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
090: prevaaHint = g2d.antialiasHint;
091: g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;
092: g2d.validatePipe();
093: } else if (textAAHint == SunHints.INTVAL_TEXT_ANTIALIAS_OFF
094: && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {
095: prevaaHint = g2d.antialiasHint;
096: g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
097: g2d.validatePipe();
098: }
099:
100: g2d.fill(s);
101:
102: if (prevaaHint != -1) {
103: g2d.antialiasHint = prevaaHint;
104: g2d.validatePipe();
105: }
106: }
107:
108: public void drawGlyphVector(SunGraphics2D g2d, GlyphVector gv,
109: float x, float y) {
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 == 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
132: && 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: }
|