2370N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2370N/A */
2370N/A
2370N/Apackage sun.font;
2370N/A
2370N/Aimport sun.awt.*;
2370N/Aimport sun.java2d.SunGraphics2D;
2370N/Aimport sun.java2d.pipe.GlyphListPipe;
2370N/Aimport sun.java2d.xr.*;
2370N/A
2370N/A/**
2370N/A * A delegate pipe of SG2D for drawing any text to a XRender surface
2370N/A *
2370N/A * @author Clemens Eisserer
2370N/A */
2370N/Apublic class XRTextRenderer extends GlyphListPipe {
2370N/A
2370N/A XRGlyphCache glyphCache;
2370N/A XRCompositeManager maskBuffer;
2370N/A XRBackend backend;
2370N/A
2370N/A GrowableEltArray eltList;
2370N/A
2370N/A public XRTextRenderer(XRCompositeManager buffer) {
2370N/A glyphCache = new XRGlyphCache(buffer);
2370N/A maskBuffer = buffer;
2370N/A backend = buffer.getBackend();
2370N/A eltList = new GrowableEltArray(64);
2370N/A }
2370N/A
2370N/A protected void drawGlyphList(SunGraphics2D sg2d, GlyphList gl) {
2370N/A if (gl.getNumGlyphs() == 0) {
2370N/A return;
2370N/A }
2370N/A
2370N/A try {
2370N/A SunToolkit.awtLock();
2370N/A
2370N/A XRSurfaceData x11sd = (XRSurfaceData) sg2d.surfaceData;
2370N/A x11sd.validateAsDestination(null, sg2d.getCompClip());
2370N/A x11sd.maskBuffer.validateCompositeState(sg2d.composite, sg2d.transform, sg2d.paint, sg2d);
2370N/A
2370N/A float advX = gl.getX();
2370N/A float advY = gl.getY();
2370N/A int oldPosX = 0, oldPosY = 0;
2370N/A
2370N/A if (gl.isSubPixPos()) {
2370N/A advX += 0.1666667f;
2370N/A advY += 0.1666667f;
2370N/A } else {
2370N/A advX += 0.5f;
2370N/A advY += 0.5f;
2370N/A }
2370N/A
2370N/A XRGlyphCacheEntry[] cachedGlyphs = glyphCache.cacheGlyphs(gl);
2370N/A boolean containsLCDGlyphs = false;
2370N/A int activeGlyphSet = cachedGlyphs[0].getGlyphSet();
2370N/A
2370N/A int eltIndex = -1;
2370N/A gl.getBounds();
2370N/A float[] positions = gl.getPositions();
2370N/A for (int i = 0; i < gl.getNumGlyphs(); i++) {
2370N/A gl.setGlyphIndex(i);
2370N/A XRGlyphCacheEntry cacheEntry = cachedGlyphs[i];
2370N/A
2370N/A eltList.getGlyphs().addInt(cacheEntry.getGlyphID());
2370N/A int glyphSet = cacheEntry.getGlyphSet();
2370N/A
2370N/A containsLCDGlyphs |= (glyphSet == glyphCache.lcdGlyphSet);
2370N/A
2370N/A int posX = 0, posY = 0;
2370N/A if (gl.usePositions()
2370N/A || (cacheEntry.getXAdvance() != ((float) cacheEntry.getXOff()) || cacheEntry.getYAdvance() != ((float) cacheEntry.getYOff()))
2370N/A || eltIndex < 0 || glyphSet != activeGlyphSet) {
2370N/A
2370N/A eltIndex = eltList.getNextIndex();
2370N/A eltList.setCharCnt(eltIndex, 1);
2370N/A activeGlyphSet = glyphSet;
2370N/A eltList.setGlyphSet(eltIndex, glyphSet);
2370N/A
2370N/A if (gl.usePositions()) {
2370N/A // /*In this case advX only stores rounding errors*/
2370N/A float x = positions[i * 2] + advX;
2370N/A float y = positions[i * 2 + 1] + advY;
2370N/A posX = (int) Math.floor(x);
2370N/A posY = (int) Math.floor(y);
2370N/A advX -= cacheEntry.getXOff();
2370N/A advY -= cacheEntry.getYOff();
2370N/A } else {
2370N/A /*
2370N/A * Calculate next glyph's position in the case of
2370N/A * relative positioning. In XRender we can only position
2370N/A * glyphs using integer coordinates, therefor we sum all
2370N/A * the advances up as float, and convert them to integer
2370N/A * later. This way rounding-error can be corrected, and
2370N/A * is required to be consistent with the software loops.
2370N/A */
2370N/A posX = (int) Math.floor(advX);
2370N/A posY = (int) Math.floor(advY);
2370N/A
2370N/A // Advance of ELT = difference between stored
2370N/A // relative
2370N/A // positioning information and required float.
2370N/A advX += (cacheEntry.getXAdvance() - cacheEntry.getXOff());
2370N/A advY += (cacheEntry.getYAdvance() - cacheEntry.getYOff());
2370N/A }
2370N/A /*
2370N/A * Offset of the current glyph is the difference to the last
2370N/A * glyph and this one
2370N/A */
2370N/A eltList.setXOff(eltIndex, (posX - oldPosX));
2370N/A eltList.setYOff(eltIndex, (posY - oldPosY));
2370N/A
2370N/A oldPosX = posX;
2370N/A oldPosY = posY;
2370N/A
2370N/A } else {
2370N/A eltList.setCharCnt(eltIndex, eltList.getCharCnt(eltIndex) + 1);
2370N/A }
2370N/A }
2370N/A
2370N/A int maskFormat = containsLCDGlyphs ? XRUtils.PictStandardARGB32 : XRUtils.PictStandardA8;
2370N/A maskBuffer.compositeText(x11sd.picture, 0, maskFormat, eltList);
2370N/A
2370N/A eltList.clear();
2370N/A } finally {
2370N/A SunToolkit.awtUnlock();
2370N/A }
2370N/A }
2370N/A}