Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/font/XRTextRenderer.java
32287 views
/*1* Copyright (c) 2010, 2013, 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.font;2627import sun.awt.*;28import sun.java2d.SunGraphics2D;29import sun.java2d.pipe.GlyphListPipe;30import sun.java2d.xr.*;3132/**33* A delegate pipe of SG2D for drawing any text to a XRender surface34*35* @author Clemens Eisserer36*/37public class XRTextRenderer extends GlyphListPipe {38// Workarround for a bug in libXrender.39// In case the number of glyphs of an ELT is a multiple of 254,40// a few garbage bytes are sent to the XServer causing hangs.41static final int MAX_ELT_GLYPH_COUNT = 253;4243XRGlyphCache glyphCache;44XRCompositeManager maskBuffer;45XRBackend backend;4647GrowableEltArray eltList;4849public XRTextRenderer(XRCompositeManager buffer) {50glyphCache = new XRGlyphCache(buffer);51maskBuffer = buffer;52backend = buffer.getBackend();53eltList = new GrowableEltArray(64);54}5556protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) {57if (gl.getNumGlyphs() == 0) {58return;59}6061try {62SunToolkit.awtLock();6364XRSurfaceData x11sd = (XRSurfaceData) sg2d.surfaceData;65x11sd.validateAsDestination(null, sg2d.getCompClip());66x11sd.maskBuffer.validateCompositeState(sg2d.composite, sg2d.transform, sg2d.paint, sg2d);6768float advX = gl.getX();69float advY = gl.getY();70int oldPosX = 0, oldPosY = 0;7172if (gl.isSubPixPos()) {73advX += 0.1666667f;74advY += 0.1666667f;75} else {76advX += 0.5f;77advY += 0.5f;78}7980XRGlyphCacheEntry[] cachedGlyphs = glyphCache.cacheGlyphs(gl);81boolean containsLCDGlyphs = false;82int activeGlyphSet = cachedGlyphs[0].getGlyphSet();8384int eltIndex = -1;85gl.getBounds();86float[] positions = gl.getPositions();87for (int i = 0; i < gl.getNumGlyphs(); i++) {88gl.setGlyphIndex(i);89XRGlyphCacheEntry cacheEntry = cachedGlyphs[i];90if (cacheEntry == null) {91continue;92}9394eltList.getGlyphs().addInt(cacheEntry.getGlyphID());95int glyphSet = cacheEntry.getGlyphSet();9697containsLCDGlyphs |= (glyphSet == glyphCache.lcdGlyphSet);9899int posX = 0, posY = 0;100if (gl.usePositions()101|| cacheEntry.getXAdvance() != ((float) cacheEntry.getXOff())102|| cacheEntry.getYAdvance() != ((float) cacheEntry.getYOff())103|| glyphSet != activeGlyphSet104|| eltIndex < 0105|| eltList.getCharCnt(eltIndex) == MAX_ELT_GLYPH_COUNT) {106107eltIndex = eltList.getNextIndex();108eltList.setCharCnt(eltIndex, 1);109activeGlyphSet = glyphSet;110eltList.setGlyphSet(eltIndex, glyphSet);111112if (gl.usePositions()) {113// In this case advX only stores rounding errors114float x = positions[i * 2] + advX;115float y = positions[i * 2 + 1] + advY;116posX = (int) Math.floor(x);117posY = (int) Math.floor(y);118advX -= cacheEntry.getXOff();119advY -= cacheEntry.getYOff();120} else {121/*122* Calculate next glyph's position in the case of123* relative positioning. In XRender we can only position124* glyphs using integer coordinates, therefor we sum all125* the advances up as float, and convert them to integer126* later. This way rounding-error can be corrected, and127* is required to be consistent with the software loops.128*/129posX = (int) Math.floor(advX);130posY = (int) Math.floor(advY);131132// Advance of ELT = difference between stored relative133// positioning information and required float.134advX += (cacheEntry.getXAdvance() - cacheEntry.getXOff());135advY += (cacheEntry.getYAdvance() - cacheEntry.getYOff());136}137138// Offset of the current glyph is the difference139// to the last glyph and this one140eltList.setXOff(eltIndex, (posX - oldPosX));141eltList.setYOff(eltIndex, (posY - oldPosY));142143oldPosX = posX;144oldPosY = posY;145146} else {147eltList.setCharCnt(eltIndex, eltList.getCharCnt(eltIndex) + 1);148}149}150151int maskFormat = containsLCDGlyphs ? XRUtils.PictStandardARGB32 : XRUtils.PictStandardA8;152maskBuffer.compositeText(x11sd, (int) gl.getX(), (int) gl.getY(), 0, maskFormat, eltList);153154eltList.clear();155} finally {156SunToolkit.awtUnlock();157}158}159}160161162