All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.batik.gvt.font.MultiGlyphVector Maven / Gradle / Ivy

There is a newer version: 1.18
Show newest version
/*

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package org.apache.batik.gvt.font;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphJustificationInfo;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.AttributedCharacterIterator;
import java.util.Iterator;
import java.util.List;

import org.apache.batik.gvt.text.AttributedCharacterSpanIterator;

/**
 *
 * @version $Id: MultiGlyphVector.java 1733416 2016-03-03 07:07:13Z gadams $
 */
public class MultiGlyphVector implements GVTGlyphVector {

    GVTGlyphVector [] gvs;
    int [] nGlyphs;
    int [] off;

    int nGlyph;

    public MultiGlyphVector(List gvs) {
        int nSlots = gvs.size();
        this.gvs     = new GVTGlyphVector[ nSlots ];
        this.nGlyphs = new int[ nSlots ];
        this.off     = new int[ nSlots ];

        Iterator iter = gvs.iterator();
        int i=0;
        while (iter.hasNext()) {
            off[i]      = nGlyph;

            GVTGlyphVector gv = (GVTGlyphVector)iter.next();
            this.gvs[i] = gv;
            nGlyphs[i]  = gv.getNumGlyphs();
            nGlyph     += nGlyphs[i];
            i++;
        }
        nGlyphs[i-1]++;
    }

    /**
     * Returns the number of glyphs in this GlyphVector.
     */
    public int getNumGlyphs() {
        return nGlyph;
    }


    int getGVIdx(int glyphIdx) {
        if (glyphIdx > nGlyph) return -1;
        if (glyphIdx == nGlyph) return gvs.length-1;
        for (int i=0; i " + newPos);
        gvs[idx].setGlyphPosition(glyphIndex-off[idx], newPos);
    }

    /**
     * Sets the transform of the specified glyph within this GlyphVector.
     */
    public void setGlyphTransform(int glyphIndex, AffineTransform newTX) {
        int idx = getGVIdx(glyphIndex);
        gvs[idx].setGlyphTransform(glyphIndex-off[idx], newTX);
    }

    /**
     * Tells the glyph vector whether or not to draw the specified glyph.
     */
    public void setGlyphVisible(int glyphIndex, boolean visible) {
        int idx = getGVIdx(glyphIndex);
        gvs[idx].setGlyphVisible(glyphIndex-off[idx], visible);
    }

    /**
     * Returns true if specified glyph will be drawn.
     */
    public boolean isGlyphVisible(int glyphIndex) {
        int idx = getGVIdx(glyphIndex);
        return gvs[idx].isGlyphVisible(glyphIndex-off[idx]);
    }

    /**
     * Returns an array of glyphcodes for the specified glyphs.
     */
    public int[] getGlyphCodes(int beginGlyphIndex, int numEntries,
                        int[] codeReturn) {
        int [] ret = codeReturn;
        if (ret == null)
            ret = new int[numEntries];
        int [] tmp = null;

        int gvIdx = getGVIdx(beginGlyphIndex);
        int gi    = beginGlyphIndex-off[gvIdx];
        int i=0;
        GVTGlyphVector gv;
        while (numEntries != 0) {
            int len = numEntries;
            if (gi+len > nGlyphs[gvIdx])
                len = nGlyphs[gvIdx]-gi;
            gv = gvs[gvIdx];
            if (i == 0) {
                gv.getGlyphCodes(gi, len, ret);
            } else {
                if ((tmp == null) || (tmp.length < len))
                    tmp = new int[len];

                gv.getGlyphCodes(gi, len, tmp);
                System.arraycopy( tmp, 0, ret, i, len );
            }
            gi=0;
            gvIdx++;
            numEntries -= len;
            i+=len;
        }
        return ret;
    }


    /**
     * Returns an array of glyph positions for the specified glyphs
     */
    public float[] getGlyphPositions(int beginGlyphIndex,
                              int numEntries,
                              float[] positionReturn) {
        float [] ret = positionReturn;
        if (ret == null)
            ret = new float[numEntries*2];
        float [] tmp = null;

        int gvIdx = getGVIdx(beginGlyphIndex);
        int gi    = beginGlyphIndex-off[gvIdx];
        int i=0;
        GVTGlyphVector gv;
        while (numEntries != 0) {
            int len = numEntries;
            if (gi+len > nGlyphs[gvIdx])
                len = nGlyphs[gvIdx]-gi;

            gv = gvs[gvIdx];
            if (i == 0) {
                gv.getGlyphPositions(gi, len, ret);
            } else {
                if ((tmp == null) || (tmp.length < len*2))
                    tmp = new float[len*2];

                gv.getGlyphPositions(gi, len, tmp);
                System.arraycopy( tmp, 0, ret, i, len * 2 );
            }
            gi=0;
            gvIdx++;
            numEntries -= len;
            i+=len*2;
        }
        return ret;
    }


    /**
     *  Returns the logical bounds of this GlyphVector.
     */
    public Rectangle2D getLogicalBounds() {
        Rectangle2D ret = null;
        for (int idx=0; idx= nGlyphs[idx]) {
                gi2 = nGlyphs[idx]-1;
            }
            ret += gvs[idx].getCharacterCount(gi1, gi2);
            startGlyphIndex += (gi2-gi1+1);
        }
        return ret;
    }

    @Override
    public boolean isReversed() {
        return false;
    }

    @Override
    public void maybeReverse(boolean mirror) {
    }

    /**
     * Draws the glyph vector.
     */
    public void draw(Graphics2D g2d,
              AttributedCharacterIterator aci) {
        int begin = aci.getBeginIndex();
        for (int idx=0; idx




© 2015 - 2024 Weber Informatics LLC | Privacy Policy