Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pipe/OutlineTextRenderer.java
38918 views
/*1* Copyright (c) 2000, 2005, 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.font.GlyphList;32import sun.awt.SunHints;3334import java.awt.Shape;35import java.awt.geom.AffineTransform;36import java.awt.font.TextLayout;3738/**39* A delegate pipe of SG2D for drawing "large" text with40* 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 of43* rendering, this should not be used if the current paint is not44* a solid colour.45*46* If text anti-aliasing is requested by the application, and47* filling path, an anti-aliasing fill pipe needs to48* be invoked.49* This involves making some of the same decisions as in the50* validatePipe call, which may be in a SurfaceData subclass, so51* its awkward to always ensure that the correct pipe is used.52* The easiest thing, rather than reproducing much of that logic53* is to call validatePipe() which works but is expensive, although54* 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 will56* produce AA text which perhaps isn't what the user expected.57* Note that the glyphvector obeys its FRC, not the G2D.58*/5960public class OutlineTextRenderer implements TextPipe {6162// Text with a height greater than the threshhold will be63// drawn via this pipe.64public static final int THRESHHOLD = 100;6566public void drawChars(SunGraphics2D g2d,67char data[], int offset, int length,68int x, int y) {6970String s = new String(data, offset, length);71drawString(g2d, s, x, y);72}7374public void drawString(SunGraphics2D g2d, String str, double x, double y) {7576if ("".equals(str)) {77return; // TextLayout constructor throws IAE on "".78}79TextLayout tl = new TextLayout(str, g2d.getFont(),80g2d.getFontRenderContext());81Shape s = tl.getOutline(AffineTransform.getTranslateInstance(x, y));8283int textAAHint = g2d.getFontInfo().aaHint;8485int prevaaHint = - 1;86if (textAAHint != SunHints.INTVAL_TEXT_ANTIALIAS_OFF &&87g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {88prevaaHint = g2d.antialiasHint;89g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;90g2d.validatePipe();91} else if (textAAHint == SunHints.INTVAL_TEXT_ANTIALIAS_OFF92&& g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {93prevaaHint = g2d.antialiasHint;94g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;95g2d.validatePipe();96}9798g2d.fill(s);99100if (prevaaHint != -1) {101g2d.antialiasHint = prevaaHint;102g2d.validatePipe();103}104}105106public void drawGlyphVector(SunGraphics2D g2d, GlyphVector gv,107float x, float y) {108109110Shape s = gv.getOutline(x, y);111int prevaaHint = - 1;112FontRenderContext frc = gv.getFontRenderContext();113boolean aa = frc.isAntiAliased();114115/* 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", and118* in all other cases (ie AA ON and all LCD modes) use AA outlines.119*/120if (aa) {121if (g2d.getGVFontInfo(gv.getFont(), frc).aaHint ==122SunHints.INTVAL_TEXT_ANTIALIAS_OFF) {123aa = false;124}125}126127if (aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {128prevaaHint = g2d.antialiasHint;129g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;130g2d.validatePipe();131} else if (!aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {132prevaaHint = g2d.antialiasHint;133g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;134g2d.validatePipe();135}136137g2d.fill(s);138139if (prevaaHint != -1) {140g2d.antialiasHint = prevaaHint;141g2d.validatePipe();142}143}144}145146147