Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/OutlineTextRenderer.java
41159 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pipe;2627import java.awt.font.FontRenderContext;28import java.awt.font.GlyphVector;29import java.awt.font.TextLayout;30import sun.java2d.SunGraphics2D;31import sun.awt.SunHints;3233import java.awt.Shape;34import java.awt.geom.AffineTransform;35import java.awt.font.TextLayout;3637/**38* A delegate pipe of SG2D for drawing "large" text with39* a solid source colour to an opaque destination.40* The text is drawn as a filled outline.41* Since the developer is not explicitly requesting this way of42* rendering, this should not be used if the current paint is not43* a solid colour.44*45* If text anti-aliasing is requested by the application, and46* filling path, an anti-aliasing fill pipe needs to47* be invoked.48* This involves making some of the same decisions as in the49* validatePipe call, which may be in a SurfaceData subclass, so50* its awkward to always ensure that the correct pipe is used.51* The easiest thing, rather than reproducing much of that logic52* is to call validatePipe() which works but is expensive, although53* probably not compared to the cost of filling the path.54* Note if AA hint is ON but text-AA hint is OFF this logic will55* produce AA text which perhaps isn't what the user expected.56* Note that the glyphvector obeys its FRC, not the G2D.57*/5859public class OutlineTextRenderer implements TextPipe {6061// Text with a height greater than the threshhold will be62// drawn via this pipe.63public static final int THRESHHOLD = 100;6465public void drawChars(SunGraphics2D g2d,66char[] data, int offset, int length,67int x, int y) {6869String s = new String(data, offset, length);70drawString(g2d, s, x, y);71}7273public void drawString(SunGraphics2D g2d, String str, double x, double y) {7475if ("".equals(str)) {76return; // TextLayout constructor throws IAE on "".77}78TextLayout tl = new TextLayout(str, g2d.getFont(),79g2d.getFontRenderContext());80Shape s = tl.getOutline(AffineTransform.getTranslateInstance(x, y));8182int textAAHint = g2d.getFontInfo().aaHint;8384int prevaaHint = - 1;85if (textAAHint != SunHints.INTVAL_TEXT_ANTIALIAS_OFF &&86g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {87prevaaHint = g2d.antialiasHint;88g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;89g2d.validatePipe();90} else if (textAAHint == SunHints.INTVAL_TEXT_ANTIALIAS_OFF91&& g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {92prevaaHint = g2d.antialiasHint;93g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;94g2d.validatePipe();95}9697g2d.fill(s);9899if (prevaaHint != -1) {100g2d.antialiasHint = prevaaHint;101g2d.validatePipe();102}103}104105public void drawGlyphVector(SunGraphics2D g2d, GlyphVector gv,106float x, float y) {107108109Shape s = gv.getOutline(x, y);110int prevaaHint = - 1;111FontRenderContext frc = gv.getFontRenderContext();112boolean aa = frc.isAntiAliased();113114/* aa will be true if any AA mode has been specified.115* ie for LCD and 'gasp' modes too.116* We will check if 'gasp' has resolved AA to be "OFF", and117* in all other cases (ie AA ON and all LCD modes) use AA outlines.118*/119if (aa) {120if (g2d.getGVFontInfo(gv.getFont(), frc).aaHint ==121SunHints.INTVAL_TEXT_ANTIALIAS_OFF) {122aa = false;123}124}125126if (aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {127prevaaHint = g2d.antialiasHint;128g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;129g2d.validatePipe();130} else if (!aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {131prevaaHint = g2d.antialiasHint;132g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;133g2d.validatePipe();134}135136g2d.fill(s);137138if (prevaaHint != -1) {139g2d.antialiasHint = prevaaHint;140g2d.validatePipe();141}142}143}144145146