0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright IBM Corp. 1998-2003, All Rights Reserved
0N/A *
0N/A */
0N/A
0N/Apackage java.awt.font;
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.GeneralPath;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.im.InputMethodHighlight;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.text.Annotation;
0N/Aimport java.text.AttributedCharacterIterator;
0N/Aimport java.text.Bidi;
0N/Aimport java.text.CharacterIterator;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Map;
0N/Aimport sun.font.AttributeValues;
0N/Aimport sun.font.BidiUtils;
0N/Aimport sun.font.CoreMetrics;
0N/Aimport sun.font.Decoration;
0N/Aimport sun.font.FontLineMetrics;
0N/Aimport sun.font.FontResolver;
0N/Aimport sun.font.GraphicComponent;
0N/Aimport sun.font.LayoutPathImpl;
0N/Aimport sun.font.LayoutPathImpl.EmptyPath;
0N/Aimport sun.font.LayoutPathImpl.SegmentPathBuilder;
0N/Aimport sun.font.TextLabelFactory;
0N/Aimport sun.font.TextLineComponent;
0N/Aimport sun.text.CodePointIterator;
0N/A
0N/Aimport java.awt.geom.Line2D;
0N/A
0N/Afinal class TextLine {
0N/A
0N/A static final class TextLineMetrics {
0N/A public final float ascent;
0N/A public final float descent;
0N/A public final float leading;
0N/A public final float advance;
0N/A
0N/A public TextLineMetrics(float ascent,
0N/A float descent,
0N/A float leading,
0N/A float advance) {
0N/A this.ascent = ascent;
0N/A this.descent = descent;
0N/A this.leading = leading;
0N/A this.advance = advance;
0N/A }
0N/A }
0N/A
0N/A private TextLineComponent[] fComponents;
0N/A private float[] fBaselineOffsets;
0N/A private int[] fComponentVisualOrder; // if null, ltr
0N/A private float[] locs; // x,y pairs for components in visual order
0N/A private char[] fChars;
0N/A private int fCharsStart;
0N/A private int fCharsLimit;
0N/A private int[] fCharVisualOrder; // if null, ltr
0N/A private int[] fCharLogicalOrder; // if null, ltr
0N/A private byte[] fCharLevels; // if null, 0
0N/A private boolean fIsDirectionLTR;
0N/A private LayoutPathImpl lp;
0N/A private boolean isSimple;
0N/A private Rectangle pixelBounds;
0N/A private FontRenderContext frc;
0N/A
0N/A private TextLineMetrics fMetrics = null; // built on demand in getMetrics
0N/A
0N/A public TextLine(FontRenderContext frc,
0N/A TextLineComponent[] components,
0N/A float[] baselineOffsets,
0N/A char[] chars,
0N/A int charsStart,
0N/A int charsLimit,
0N/A int[] charLogicalOrder,
0N/A byte[] charLevels,
0N/A boolean isDirectionLTR) {
0N/A
0N/A int[] componentVisualOrder = computeComponentOrder(components,
0N/A charLogicalOrder);
0N/A
0N/A this.frc = frc;
0N/A fComponents = components;
0N/A fBaselineOffsets = baselineOffsets;
0N/A fComponentVisualOrder = componentVisualOrder;
0N/A fChars = chars;
0N/A fCharsStart = charsStart;
0N/A fCharsLimit = charsLimit;
0N/A fCharLogicalOrder = charLogicalOrder;
0N/A fCharLevels = charLevels;
0N/A fIsDirectionLTR = isDirectionLTR;
0N/A checkCtorArgs();
0N/A
0N/A init();
0N/A }
0N/A
0N/A private void checkCtorArgs() {
0N/A
0N/A int checkCharCount = 0;
0N/A for (int i=0; i < fComponents.length; i++) {
0N/A checkCharCount += fComponents[i].getNumCharacters();
0N/A }
0N/A
0N/A if (checkCharCount != this.characterCount()) {
0N/A throw new IllegalArgumentException("Invalid TextLine! " +
0N/A "char count is different from " +
0N/A "sum of char counts of components.");
0N/A }
0N/A }
0N/A
0N/A private void init() {
0N/A
0N/A // first, we need to check for graphic components on the TOP or BOTTOM baselines. So
0N/A // we perform the work that used to be in getMetrics here.
0N/A
0N/A float ascent = 0;
0N/A float descent = 0;
0N/A float leading = 0;
0N/A float advance = 0;
0N/A
0N/A // ascent + descent must not be less than this value
0N/A float maxGraphicHeight = 0;
0N/A float maxGraphicHeightWithLeading = 0;
0N/A
0N/A // walk through EGA's
0N/A TextLineComponent tlc;
0N/A boolean fitTopAndBottomGraphics = false;
0N/A
0N/A isSimple = true;
0N/A
0N/A for (int i = 0; i < fComponents.length; i++) {
0N/A tlc = fComponents[i];
0N/A
0N/A isSimple &= tlc.isSimple();
0N/A
0N/A CoreMetrics cm = tlc.getCoreMetrics();
0N/A
0N/A byte baseline = (byte)cm.baselineIndex;
0N/A
0N/A if (baseline >= 0) {
0N/A float baselineOffset = fBaselineOffsets[baseline];
0N/A
0N/A ascent = Math.max(ascent, -baselineOffset + cm.ascent);
0N/A
0N/A float gd = baselineOffset + cm.descent;
0N/A descent = Math.max(descent, gd);
0N/A
0N/A leading = Math.max(leading, gd + cm.leading);
0N/A }
0N/A else {
0N/A fitTopAndBottomGraphics = true;
0N/A float graphicHeight = cm.ascent + cm.descent;
0N/A float graphicHeightWithLeading = graphicHeight + cm.leading;
0N/A maxGraphicHeight = Math.max(maxGraphicHeight, graphicHeight);
0N/A maxGraphicHeightWithLeading = Math.max(maxGraphicHeightWithLeading,
0N/A graphicHeightWithLeading);
0N/A }
0N/A }
0N/A
0N/A if (fitTopAndBottomGraphics) {
0N/A if (maxGraphicHeight > ascent + descent) {
0N/A descent = maxGraphicHeight - ascent;
0N/A }
0N/A if (maxGraphicHeightWithLeading > ascent + leading) {
0N/A leading = maxGraphicHeightWithLeading - ascent;
0N/A }
0N/A }
0N/A
0N/A leading -= descent;
0N/A
0N/A // we now know enough to compute the locs, but we need the final loc
0N/A // for the advance before we can create the metrics object
0N/A
0N/A if (fitTopAndBottomGraphics) {
0N/A // we have top or bottom baselines, so expand the baselines array
0N/A // full offsets are needed by CoreMetrics.effectiveBaselineOffset
0N/A fBaselineOffsets = new float[] {
0N/A fBaselineOffsets[0],
0N/A fBaselineOffsets[1],
0N/A fBaselineOffsets[2],
0N/A descent,
0N/A -ascent
0N/A };
0N/A }
0N/A
0N/A float x = 0;
0N/A float y = 0;
0N/A CoreMetrics pcm = null;
0N/A
0N/A boolean needPath = false;
0N/A locs = new float[fComponents.length * 2 + 2];
0N/A
0N/A for (int i = 0, n = 0; i < fComponents.length; ++i, n += 2) {
0N/A tlc = fComponents[getComponentLogicalIndex(i)];
0N/A CoreMetrics cm = tlc.getCoreMetrics();
0N/A
0N/A if ((pcm != null) &&
0N/A (pcm.italicAngle != 0 || cm.italicAngle != 0) && // adjust because of italics
0N/A (pcm.italicAngle != cm.italicAngle ||
0N/A pcm.baselineIndex != cm.baselineIndex ||
0N/A pcm.ssOffset != cm.ssOffset)) {
0N/A
0N/A // 1) compute the area of overlap - min effective ascent and min effective descent
0N/A // 2) compute the x positions along italic angle of ascent and descent for left and right
0N/A // 3) compute maximum left - right, adjust right position by this value
0N/A // this is a crude form of kerning between textcomponents
0N/A
0N/A // note glyphvectors preposition glyphs based on offset,
0N/A // so tl doesn't need to adjust glyphvector position
0N/A // 1)
0N/A float pb = pcm.effectiveBaselineOffset(fBaselineOffsets);
0N/A float pa = pb - pcm.ascent;
0N/A float pd = pb + pcm.descent;
0N/A // pb += pcm.ssOffset;
0N/A
0N/A float cb = cm.effectiveBaselineOffset(fBaselineOffsets);
0N/A float ca = cb - cm.ascent;
0N/A float cd = cb + cm.descent;
0N/A // cb += cm.ssOffset;
0N/A
0N/A float a = Math.max(pa, ca);
0N/A float d = Math.min(pd, cd);
0N/A
0N/A // 2)
0N/A float pax = pcm.italicAngle * (pb - a);
0N/A float pdx = pcm.italicAngle * (pb - d);
0N/A
0N/A float cax = cm.italicAngle * (cb - a);
0N/A float cdx = cm.italicAngle * (cb - d);
0N/A
0N/A // 3)
0N/A float dax = pax - cax;
0N/A float ddx = pdx - cdx;
0N/A float dx = Math.max(dax, ddx);
0N/A
0N/A x += dx;
0N/A y = cb;
0N/A } else {
0N/A // no italic adjustment for x, but still need to compute y
0N/A y = cm.effectiveBaselineOffset(fBaselineOffsets); // + cm.ssOffset;
0N/A }
0N/A
0N/A locs[n] = x;
0N/A locs[n+1] = y;
0N/A
0N/A x += tlc.getAdvance();
0N/A pcm = cm;
0N/A
0N/A needPath |= tlc.getBaselineTransform() != null;
0N/A }
0N/A
0N/A // do we want italic padding at the right of the line?
0N/A if (pcm.italicAngle != 0) {
0N/A float pb = pcm.effectiveBaselineOffset(fBaselineOffsets);
0N/A float pa = pb - pcm.ascent;
0N/A float pd = pb + pcm.descent;
0N/A pb += pcm.ssOffset;
0N/A
0N/A float d;
0N/A if (pcm.italicAngle > 0) {
0N/A d = pb + pcm.ascent;
0N/A } else {
0N/A d = pb - pcm.descent;
0N/A }
0N/A d *= pcm.italicAngle;
0N/A
0N/A x += d;
0N/A }
0N/A locs[locs.length - 2] = x;
0N/A // locs[locs.length - 1] = 0; // final offset is always back on baseline
0N/A
0N/A // ok, build fMetrics since we have the final advance
0N/A advance = x;
0N/A fMetrics = new TextLineMetrics(ascent, descent, leading, advance);
0N/A
0N/A // build path if we need it
0N/A if (needPath) {
0N/A isSimple = false;
0N/A
0N/A Point2D.Double pt = new Point2D.Double();
0N/A double tx = 0, ty = 0;
0N/A SegmentPathBuilder builder = new SegmentPathBuilder();
0N/A builder.moveTo(locs[0], 0);
0N/A for (int i = 0, n = 0; i < fComponents.length; ++i, n += 2) {
0N/A tlc = fComponents[getComponentLogicalIndex(i)];
0N/A AffineTransform at = tlc.getBaselineTransform();
0N/A if (at != null && ((at.getType() & at.TYPE_TRANSLATION) != 0)) {
0N/A double dx = at.getTranslateX();
0N/A double dy = at.getTranslateY();
0N/A builder.moveTo(tx += dx, ty += dy);
0N/A }
0N/A pt.x = locs[n+2] - locs[n];
0N/A pt.y = 0;
0N/A if (at != null) {
0N/A at.deltaTransform(pt, pt);
0N/A }
0N/A builder.lineTo(tx += pt.x, ty += pt.y);
0N/A }
0N/A lp = builder.complete();
0N/A
0N/A if (lp == null) { // empty path
0N/A tlc = fComponents[getComponentLogicalIndex(0)];
0N/A AffineTransform at = tlc.getBaselineTransform();
0N/A if (at != null) {
0N/A lp = new EmptyPath(at);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {
0N/A Rectangle result = null;
0N/A
0N/A // if we have a matching frc, set it to null so we don't have to test it
0N/A // for each component
0N/A if (frc != null && frc.equals(this.frc)) {
0N/A frc = null;
0N/A }
0N/A
0N/A // only cache integral locations with the default frc, this is a bit strict
0N/A int ix = (int)Math.floor(x);
0N/A int iy = (int)Math.floor(y);
0N/A float rx = x - ix;
0N/A float ry = y - iy;
0N/A boolean canCache = frc == null && rx == 0 && ry == 0;
0N/A
0N/A if (canCache && pixelBounds != null) {
0N/A result = new Rectangle(pixelBounds);
0N/A result.x += ix;
0N/A result.y += iy;
0N/A return result;
0N/A }
0N/A
0N/A // couldn't use cache, or didn't have it, so compute
0N/A
0N/A if (isSimple) { // all glyphvectors with no decorations, no layout path
0N/A for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A Rectangle pb = tlc.getPixelBounds(frc, locs[n] + rx, locs[n+1] + ry);
0N/A if (!pb.isEmpty()) {
0N/A if (result == null) {
0N/A result = pb;
0N/A } else {
0N/A result.add(pb);
0N/A }
0N/A }
0N/A }
0N/A if (result == null) {
0N/A result = new Rectangle(0, 0, 0, 0);
0N/A }
0N/A } else { // draw and test
0N/A final int MARGIN = 3;
0N/A Rectangle2D r2d = getVisualBounds();
0N/A if (lp != null) {
0N/A r2d = lp.mapShape(r2d).getBounds();
0N/A }
0N/A Rectangle bounds = r2d.getBounds();
0N/A BufferedImage im = new BufferedImage(bounds.width + MARGIN * 2,
0N/A bounds.height + MARGIN * 2,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A
0N/A Graphics2D g2d = im.createGraphics();
0N/A g2d.setColor(Color.WHITE);
0N/A g2d.fillRect(0, 0, im.getWidth(), im.getHeight());
0N/A
0N/A g2d.setColor(Color.BLACK);
0N/A draw(g2d, rx + MARGIN - bounds.x, ry + MARGIN - bounds.y);
0N/A
0N/A result = computePixelBounds(im);
0N/A result.x -= MARGIN - bounds.x;
0N/A result.y -= MARGIN - bounds.y;
0N/A }
0N/A
0N/A if (canCache) {
0N/A pixelBounds = new Rectangle(result);
0N/A }
0N/A
0N/A result.x += ix;
0N/A result.y += iy;
0N/A return result;
0N/A }
0N/A
0N/A static Rectangle computePixelBounds(BufferedImage im) {
0N/A int w = im.getWidth();
0N/A int h = im.getHeight();
0N/A
0N/A int l = -1, t = -1, r = w, b = h;
0N/A
0N/A {
0N/A // get top
0N/A int[] buf = new int[w];
0N/A loop: while (++t < h) {
0N/A im.getRGB(0, t, buf.length, 1, buf, 0, w); // w ignored
0N/A for (int i = 0; i < buf.length; i++) {
0N/A if (buf[i] != -1) {
0N/A break loop;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // get bottom
0N/A {
0N/A int[] buf = new int[w];
0N/A loop: while (--b > t) {
0N/A im.getRGB(0, b, buf.length, 1, buf, 0, w); // w ignored
0N/A for (int i = 0; i < buf.length; ++i) {
0N/A if (buf[i] != -1) {
0N/A break loop;
0N/A }
0N/A }
0N/A }
0N/A ++b;
0N/A }
0N/A
0N/A // get left
0N/A {
0N/A loop: while (++l < r) {
0N/A for (int i = t; i < b; ++i) {
0N/A int v = im.getRGB(l, i);
0N/A if (v != -1) {
0N/A break loop;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // get right
0N/A {
0N/A loop: while (--r > l) {
0N/A for (int i = t; i < b; ++i) {
0N/A int v = im.getRGB(r, i);
0N/A if (v != -1) {
0N/A break loop;
0N/A }
0N/A }
0N/A }
0N/A ++r;
0N/A }
0N/A
0N/A return new Rectangle(l, t, r-l, b-t);
0N/A }
0N/A
0N/A private abstract static class Function {
0N/A
0N/A abstract float computeFunction(TextLine line,
0N/A int componentIndex,
0N/A int indexInArray);
0N/A }
0N/A
0N/A private static Function fgPosAdvF = new Function() {
0N/A float computeFunction(TextLine line,
0N/A int componentIndex,
0N/A int indexInArray) {
0N/A
0N/A TextLineComponent tlc = line.fComponents[componentIndex];
0N/A int vi = line.getComponentVisualIndex(componentIndex);
0N/A return line.locs[vi * 2] + tlc.getCharX(indexInArray) + tlc.getCharAdvance(indexInArray);
0N/A }
0N/A };
0N/A
0N/A private static Function fgAdvanceF = new Function() {
0N/A
0N/A float computeFunction(TextLine line,
0N/A int componentIndex,
0N/A int indexInArray) {
0N/A
0N/A TextLineComponent tlc = line.fComponents[componentIndex];
0N/A return tlc.getCharAdvance(indexInArray);
0N/A }
0N/A };
0N/A
0N/A private static Function fgXPositionF = new Function() {
0N/A
0N/A float computeFunction(TextLine line,
0N/A int componentIndex,
0N/A int indexInArray) {
0N/A
0N/A int vi = line.getComponentVisualIndex(componentIndex);
0N/A TextLineComponent tlc = line.fComponents[componentIndex];
0N/A return line.locs[vi * 2] + tlc.getCharX(indexInArray);
0N/A }
0N/A };
0N/A
0N/A private static Function fgYPositionF = new Function() {
0N/A
0N/A float computeFunction(TextLine line,
0N/A int componentIndex,
0N/A int indexInArray) {
0N/A
0N/A TextLineComponent tlc = line.fComponents[componentIndex];
0N/A float charPos = tlc.getCharY(indexInArray);
0N/A
0N/A // charPos is relative to the component - adjust for
0N/A // baseline
0N/A
0N/A return charPos + line.getComponentShift(componentIndex);
0N/A }
0N/A };
0N/A
0N/A public int characterCount() {
0N/A
0N/A return fCharsLimit - fCharsStart;
0N/A }
0N/A
0N/A public boolean isDirectionLTR() {
0N/A
0N/A return fIsDirectionLTR;
0N/A }
0N/A
0N/A public TextLineMetrics getMetrics() {
0N/A return fMetrics;
0N/A }
0N/A
0N/A public int visualToLogical(int visualIndex) {
0N/A
0N/A if (fCharLogicalOrder == null) {
0N/A return visualIndex;
0N/A }
0N/A
0N/A if (fCharVisualOrder == null) {
0N/A fCharVisualOrder = BidiUtils.createInverseMap(fCharLogicalOrder);
0N/A }
0N/A
0N/A return fCharVisualOrder[visualIndex];
0N/A }
0N/A
0N/A public int logicalToVisual(int logicalIndex) {
0N/A
0N/A return (fCharLogicalOrder == null)?
0N/A logicalIndex : fCharLogicalOrder[logicalIndex];
0N/A }
0N/A
0N/A public byte getCharLevel(int logicalIndex) {
0N/A
0N/A return fCharLevels==null? 0 : fCharLevels[logicalIndex];
0N/A }
0N/A
0N/A public boolean isCharLTR(int logicalIndex) {
0N/A
0N/A return (getCharLevel(logicalIndex) & 0x1) == 0;
0N/A }
0N/A
0N/A public int getCharType(int logicalIndex) {
0N/A
0N/A return Character.getType(fChars[logicalIndex + fCharsStart]);
0N/A }
0N/A
0N/A public boolean isCharSpace(int logicalIndex) {
0N/A
0N/A return Character.isSpaceChar(fChars[logicalIndex + fCharsStart]);
0N/A }
0N/A
0N/A public boolean isCharWhitespace(int logicalIndex) {
0N/A
0N/A return Character.isWhitespace(fChars[logicalIndex + fCharsStart]);
0N/A }
0N/A
0N/A public float getCharAngle(int logicalIndex) {
0N/A
0N/A return getCoreMetricsAt(logicalIndex).italicAngle;
0N/A }
0N/A
0N/A public CoreMetrics getCoreMetricsAt(int logicalIndex) {
0N/A
0N/A if (logicalIndex < 0) {
0N/A throw new IllegalArgumentException("Negative logicalIndex.");
0N/A }
0N/A
0N/A if (logicalIndex > fCharsLimit - fCharsStart) {
0N/A throw new IllegalArgumentException("logicalIndex too large.");
0N/A }
0N/A
0N/A int currentTlc = 0;
0N/A int tlcStart = 0;
0N/A int tlcLimit = 0;
0N/A
0N/A do {
0N/A tlcLimit += fComponents[currentTlc].getNumCharacters();
0N/A if (tlcLimit > logicalIndex) {
0N/A break;
0N/A }
0N/A ++currentTlc;
0N/A tlcStart = tlcLimit;
0N/A } while(currentTlc < fComponents.length);
0N/A
0N/A return fComponents[currentTlc].getCoreMetrics();
0N/A }
0N/A
0N/A public float getCharAscent(int logicalIndex) {
0N/A
0N/A return getCoreMetricsAt(logicalIndex).ascent;
0N/A }
0N/A
0N/A public float getCharDescent(int logicalIndex) {
0N/A
0N/A return getCoreMetricsAt(logicalIndex).descent;
0N/A }
0N/A
0N/A public float getCharShift(int logicalIndex) {
0N/A
0N/A return getCoreMetricsAt(logicalIndex).ssOffset;
0N/A }
0N/A
0N/A private float applyFunctionAtIndex(int logicalIndex, Function f) {
0N/A
0N/A if (logicalIndex < 0) {
0N/A throw new IllegalArgumentException("Negative logicalIndex.");
0N/A }
0N/A
0N/A int tlcStart = 0;
0N/A
0N/A for(int i=0; i < fComponents.length; i++) {
0N/A
0N/A int tlcLimit = tlcStart + fComponents[i].getNumCharacters();
0N/A if (tlcLimit > logicalIndex) {
0N/A return f.computeFunction(this, i, logicalIndex - tlcStart);
0N/A }
0N/A else {
0N/A tlcStart = tlcLimit;
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException("logicalIndex too large.");
0N/A }
0N/A
0N/A public float getCharAdvance(int logicalIndex) {
0N/A
0N/A return applyFunctionAtIndex(logicalIndex, fgAdvanceF);
0N/A }
0N/A
0N/A public float getCharXPosition(int logicalIndex) {
0N/A
0N/A return applyFunctionAtIndex(logicalIndex, fgXPositionF);
0N/A }
0N/A
0N/A public float getCharYPosition(int logicalIndex) {
0N/A
0N/A return applyFunctionAtIndex(logicalIndex, fgYPositionF);
0N/A }
0N/A
0N/A public float getCharLinePosition(int logicalIndex) {
0N/A
0N/A return getCharXPosition(logicalIndex);
0N/A }
0N/A
0N/A public float getCharLinePosition(int logicalIndex, boolean leading) {
0N/A Function f = isCharLTR(logicalIndex) == leading ? fgXPositionF : fgPosAdvF;
0N/A return applyFunctionAtIndex(logicalIndex, f);
0N/A }
0N/A
0N/A public boolean caretAtOffsetIsValid(int offset) {
0N/A
0N/A if (offset < 0) {
0N/A throw new IllegalArgumentException("Negative offset.");
0N/A }
0N/A
0N/A int tlcStart = 0;
0N/A
0N/A for(int i=0; i < fComponents.length; i++) {
0N/A
0N/A int tlcLimit = tlcStart + fComponents[i].getNumCharacters();
0N/A if (tlcLimit > offset) {
0N/A return fComponents[i].caretAtOffsetIsValid(offset-tlcStart);
0N/A }
0N/A else {
0N/A tlcStart = tlcLimit;
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException("logicalIndex too large.");
0N/A }
0N/A
0N/A /**
0N/A * map a component visual index to the logical index.
0N/A */
0N/A private int getComponentLogicalIndex(int vi) {
0N/A if (fComponentVisualOrder == null) {
0N/A return vi;
0N/A }
0N/A return fComponentVisualOrder[vi];
0N/A }
0N/A
0N/A /**
0N/A * map a component logical index to the visual index.
0N/A */
0N/A private int getComponentVisualIndex(int li) {
0N/A if (fComponentVisualOrder == null) {
0N/A return li;
0N/A }
0N/A for (int i = 0; i < fComponentVisualOrder.length; ++i) {
0N/A if (fComponentVisualOrder[i] == li) {
0N/A return i;
0N/A }
0N/A }
0N/A throw new IndexOutOfBoundsException("bad component index: " + li);
0N/A }
0N/A
0N/A public Rectangle2D getCharBounds(int logicalIndex) {
0N/A
0N/A if (logicalIndex < 0) {
0N/A throw new IllegalArgumentException("Negative logicalIndex.");
0N/A }
0N/A
0N/A int tlcStart = 0;
0N/A
0N/A for (int i=0; i < fComponents.length; i++) {
0N/A
0N/A int tlcLimit = tlcStart + fComponents[i].getNumCharacters();
0N/A if (tlcLimit > logicalIndex) {
0N/A
0N/A TextLineComponent tlc = fComponents[i];
0N/A int indexInTlc = logicalIndex - tlcStart;
0N/A Rectangle2D chBounds = tlc.getCharVisualBounds(indexInTlc);
0N/A
0N/A int vi = getComponentVisualIndex(i);
0N/A chBounds.setRect(chBounds.getX() + locs[vi * 2],
0N/A chBounds.getY() + locs[vi * 2 + 1],
0N/A chBounds.getWidth(),
0N/A chBounds.getHeight());
0N/A return chBounds;
0N/A }
0N/A else {
0N/A tlcStart = tlcLimit;
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException("logicalIndex too large.");
0N/A }
0N/A
0N/A private float getComponentShift(int index) {
0N/A CoreMetrics cm = fComponents[index].getCoreMetrics();
0N/A return cm.effectiveBaselineOffset(fBaselineOffsets);
0N/A }
0N/A
0N/A public void draw(Graphics2D g2, float x, float y) {
0N/A if (lp == null) {
0N/A for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A tlc.draw(g2, locs[n] + x, locs[n+1] + y);
0N/A }
0N/A } else {
0N/A AffineTransform oldTx = g2.getTransform();
0N/A Point2D.Float pt = new Point2D.Float();
0N/A for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A lp.pathToPoint(locs[n], locs[n+1], false, pt);
0N/A pt.x += x;
0N/A pt.y += y;
0N/A AffineTransform at = tlc.getBaselineTransform();
0N/A
0N/A if (at != null) {
0N/A g2.translate(pt.x - at.getTranslateX(), pt.y - at.getTranslateY());
0N/A g2.transform(at);
0N/A tlc.draw(g2, 0, 0);
0N/A g2.setTransform(oldTx);
0N/A } else {
0N/A tlc.draw(g2, pt.x, pt.y);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the union of the visual bounds of all the components.
0N/A * This incorporates the path. It does not include logical
0N/A * bounds (used by carets).
0N/A */
0N/A public Rectangle2D getVisualBounds() {
0N/A Rectangle2D result = null;
0N/A
0N/A for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A Rectangle2D r = tlc.getVisualBounds();
0N/A
0N/A Point2D.Float pt = new Point2D.Float(locs[n], locs[n+1]);
0N/A if (lp == null) {
0N/A r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
0N/A r.getWidth(), r.getHeight());
0N/A } else {
0N/A lp.pathToPoint(pt, false, pt);
0N/A
0N/A AffineTransform at = tlc.getBaselineTransform();
0N/A if (at != null) {
0N/A AffineTransform tx = AffineTransform.getTranslateInstance
0N/A (pt.x - at.getTranslateX(), pt.y - at.getTranslateY());
0N/A tx.concatenate(at);
0N/A r = tx.createTransformedShape(r).getBounds2D();
0N/A } else {
0N/A r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
0N/A r.getWidth(), r.getHeight());
0N/A }
0N/A }
0N/A
0N/A if (result == null) {
0N/A result = r;
0N/A } else {
0N/A result.add(r);
0N/A }
0N/A }
0N/A
0N/A if (result == null) {
0N/A result = new Rectangle2D.Float(Float.MAX_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
0N/A }
0N/A
0N/A return result;
0N/A }
0N/A
0N/A public Rectangle2D getItalicBounds() {
0N/A
0N/A float left = Float.MAX_VALUE, right = -Float.MAX_VALUE;
0N/A float top = Float.MAX_VALUE, bottom = -Float.MAX_VALUE;
0N/A
0N/A for (int i=0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A
0N/A Rectangle2D tlcBounds = tlc.getItalicBounds();
0N/A float x = locs[n];
0N/A float y = locs[n+1];
0N/A
0N/A left = Math.min(left, x + (float)tlcBounds.getX());
0N/A right = Math.max(right, x + (float)tlcBounds.getMaxX());
0N/A
0N/A top = Math.min(top, y + (float)tlcBounds.getY());
0N/A bottom = Math.max(bottom, y + (float)tlcBounds.getMaxY());
0N/A }
0N/A
0N/A return new Rectangle2D.Float(left, top, right-left, bottom-top);
0N/A }
0N/A
0N/A public Shape getOutline(AffineTransform tx) {
0N/A
0N/A GeneralPath dstShape = new GeneralPath(GeneralPath.WIND_NON_ZERO);
0N/A
0N/A for (int i=0, n = 0; i < fComponents.length; i++, n += 2) {
0N/A TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
0N/A
0N/A dstShape.append(tlc.getOutline(locs[n], locs[n+1]), false);
0N/A }
0N/A
0N/A if (tx != null) {
0N/A dstShape.transform(tx);
0N/A }
0N/A return dstShape;
0N/A }
0N/A
0N/A public int hashCode() {
0N/A return (fComponents.length << 16) ^
0N/A (fComponents[0].hashCode() << 3) ^ (fCharsLimit-fCharsStart);
0N/A }
0N/A
0N/A public String toString() {
0N/A StringBuilder buf = new StringBuilder();
0N/A
0N/A for (int i = 0; i < fComponents.length; i++) {
0N/A buf.append(fComponents[i]);
0N/A }
0N/A
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Create a TextLine from the text. The Font must be able to
0N/A * display all of the text.
0N/A * attributes==null is equivalent to using an empty Map for
0N/A * attributes
0N/A */
0N/A public static TextLine fastCreateTextLine(FontRenderContext frc,
0N/A char[] chars,
0N/A Font font,
0N/A CoreMetrics lm,
0N/A Map attributes) {
0N/A
0N/A boolean isDirectionLTR = true;
0N/A byte[] levels = null;
0N/A int[] charsLtoV = null;
0N/A Bidi bidi = null;
0N/A int characterCount = chars.length;
0N/A
0N/A boolean requiresBidi = false;
0N/A byte[] embs = null;
0N/A
0N/A AttributeValues values = null;
0N/A if (attributes != null) {
0N/A values = AttributeValues.fromMap(attributes);
0N/A if (values.getRunDirection() >= 0) {
0N/A isDirectionLTR = values.getRunDirection() == 0;
0N/A requiresBidi = !isDirectionLTR;
0N/A }
0N/A if (values.getBidiEmbedding() != 0) {
0N/A requiresBidi = true;
0N/A byte level = (byte)values.getBidiEmbedding();
0N/A embs = new byte[characterCount];
0N/A for (int i = 0; i < embs.length; ++i) {
0N/A embs[i] = level;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // dlf: get baseRot from font for now???
0N/A
0N/A if (!requiresBidi) {
0N/A requiresBidi = Bidi.requiresBidi(chars, 0, chars.length);
0N/A }
0N/A
0N/A if (requiresBidi) {
0N/A int bidiflags = values == null
0N/A ? Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT
0N/A : values.getRunDirection();
0N/A
0N/A bidi = new Bidi(chars, 0, embs, 0, chars.length, bidiflags);
0N/A if (!bidi.isLeftToRight()) {
0N/A levels = BidiUtils.getLevels(bidi);
0N/A int[] charsVtoL = BidiUtils.createVisualToLogicalMap(levels);
0N/A charsLtoV = BidiUtils.createInverseMap(charsVtoL);
0N/A isDirectionLTR = bidi.baseIsLeftToRight();
0N/A }
0N/A }
0N/A
0N/A Decoration decorator = Decoration.getDecoration(values);
0N/A
0N/A int layoutFlags = 0; // no extra info yet, bidi determines run and line direction
0N/A TextLabelFactory factory = new TextLabelFactory(frc, chars, bidi, layoutFlags);
0N/A
0N/A TextLineComponent[] components = new TextLineComponent[1];
0N/A
0N/A components = createComponentsOnRun(0, chars.length,
0N/A chars,
0N/A charsLtoV, levels,
0N/A factory, font, lm,
0N/A frc,
0N/A decorator,
0N/A components,
0N/A 0);
0N/A
0N/A int numComponents = components.length;
0N/A while (components[numComponents-1] == null) {
0N/A numComponents -= 1;
0N/A }
0N/A
0N/A if (numComponents != components.length) {
0N/A TextLineComponent[] temp = new TextLineComponent[numComponents];
0N/A System.arraycopy(components, 0, temp, 0, numComponents);
0N/A components = temp;
0N/A }
0N/A
0N/A return new TextLine(frc, components, lm.baselineOffsets,
0N/A chars, 0, chars.length, charsLtoV, levels, isDirectionLTR);
0N/A }
0N/A
0N/A private static TextLineComponent[] expandArray(TextLineComponent[] orig) {
0N/A
0N/A TextLineComponent[] newComponents = new TextLineComponent[orig.length + 8];
0N/A System.arraycopy(orig, 0, newComponents, 0, orig.length);
0N/A
0N/A return newComponents;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array in logical order of the TextLineComponents on
0N/A * the text in the given range, with the given attributes.
0N/A */
0N/A public static TextLineComponent[] createComponentsOnRun(int runStart,
0N/A int runLimit,
0N/A char[] chars,
0N/A int[] charsLtoV,
0N/A byte[] levels,
0N/A TextLabelFactory factory,
0N/A Font font,
0N/A CoreMetrics cm,
0N/A FontRenderContext frc,
0N/A Decoration decorator,
0N/A TextLineComponent[] components,
0N/A int numComponents) {
0N/A
0N/A int pos = runStart;
0N/A do {
0N/A int chunkLimit = firstVisualChunk(charsLtoV, levels, pos, runLimit); // <= displayLimit
0N/A
0N/A do {
0N/A int startPos = pos;
0N/A int lmCount;
0N/A
0N/A if (cm == null) {
0N/A LineMetrics lineMetrics = font.getLineMetrics(chars, startPos, chunkLimit, frc);
0N/A cm = CoreMetrics.get(lineMetrics);
0N/A lmCount = lineMetrics.getNumChars();
0N/A }
0N/A else {
0N/A lmCount = (chunkLimit-startPos);
0N/A }
0N/A
0N/A TextLineComponent nextComponent =
0N/A factory.createExtended(font, cm, decorator, startPos, startPos + lmCount);
0N/A
0N/A ++numComponents;
0N/A if (numComponents >= components.length) {
0N/A components = expandArray(components);
0N/A }
0N/A
0N/A components[numComponents-1] = nextComponent;
0N/A
0N/A pos += lmCount;
0N/A } while (pos < chunkLimit);
0N/A
0N/A } while (pos < runLimit);
0N/A
0N/A return components;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array (in logical order) of the TextLineComponents representing
0N/A * the text. The components are both logically and visually contiguous.
0N/A */
0N/A public static TextLineComponent[] getComponents(StyledParagraph styledParagraph,
0N/A char[] chars,
0N/A int textStart,
0N/A int textLimit,
0N/A int[] charsLtoV,
0N/A byte[] levels,
0N/A TextLabelFactory factory) {
0N/A
0N/A FontRenderContext frc = factory.getFontRenderContext();
0N/A
0N/A int numComponents = 0;
0N/A TextLineComponent[] tempComponents = new TextLineComponent[1];
0N/A
0N/A int pos = textStart;
0N/A do {
0N/A int runLimit = Math.min(styledParagraph.getRunLimit(pos), textLimit);
0N/A
0N/A Decoration decorator = styledParagraph.getDecorationAt(pos);
0N/A
0N/A Object graphicOrFont = styledParagraph.getFontOrGraphicAt(pos);
0N/A
0N/A if (graphicOrFont instanceof GraphicAttribute) {
0N/A // AffineTransform baseRot = styledParagraph.getBaselineRotationAt(pos);
0N/A // !!! For now, let's assign runs of text with both fonts and graphic attributes
0N/A // a null rotation (e.g. the baseline rotation goes away when a graphic
0N/A // is applied.
0N/A AffineTransform baseRot = null;
0N/A GraphicAttribute graphicAttribute = (GraphicAttribute) graphicOrFont;
0N/A do {
0N/A int chunkLimit = firstVisualChunk(charsLtoV, levels,
0N/A pos, runLimit);
0N/A
0N/A GraphicComponent nextGraphic =
0N/A new GraphicComponent(graphicAttribute, decorator, charsLtoV, levels, pos, chunkLimit, baseRot);
0N/A pos = chunkLimit;
0N/A
0N/A ++numComponents;
0N/A if (numComponents >= tempComponents.length) {
0N/A tempComponents = expandArray(tempComponents);
0N/A }
0N/A
0N/A tempComponents[numComponents-1] = nextGraphic;
0N/A
0N/A } while(pos < runLimit);
0N/A }
0N/A else {
0N/A Font font = (Font) graphicOrFont;
0N/A
0N/A tempComponents = createComponentsOnRun(pos, runLimit,
0N/A chars,
0N/A charsLtoV, levels,
0N/A factory, font, null,
0N/A frc,
0N/A decorator,
0N/A tempComponents,
0N/A numComponents);
0N/A pos = runLimit;
0N/A numComponents = tempComponents.length;
0N/A while (tempComponents[numComponents-1] == null) {
0N/A numComponents -= 1;
0N/A }
0N/A }
0N/A
0N/A } while (pos < textLimit);
0N/A
0N/A TextLineComponent[] components;
0N/A if (tempComponents.length == numComponents) {
0N/A components = tempComponents;
0N/A }
0N/A else {
0N/A components = new TextLineComponent[numComponents];
0N/A System.arraycopy(tempComponents, 0, components, 0, numComponents);
0N/A }
0N/A
0N/A return components;
0N/A }
0N/A
0N/A /**
0N/A * Create a TextLine from the Font and character data over the
0N/A * range. The range is relative to both the StyledParagraph and the
0N/A * character array.
0N/A */
0N/A public static TextLine createLineFromText(char[] chars,
0N/A StyledParagraph styledParagraph,
0N/A TextLabelFactory factory,
0N/A boolean isDirectionLTR,
0N/A float[] baselineOffsets) {
0N/A
0N/A factory.setLineContext(0, chars.length);
0N/A
0N/A Bidi lineBidi = factory.getLineBidi();
0N/A int[] charsLtoV = null;
0N/A byte[] levels = null;
0N/A
0N/A if (lineBidi != null) {
0N/A levels = BidiUtils.getLevels(lineBidi);
0N/A int[] charsVtoL = BidiUtils.createVisualToLogicalMap(levels);
0N/A charsLtoV = BidiUtils.createInverseMap(charsVtoL);
0N/A }
0N/A
0N/A TextLineComponent[] components =
0N/A getComponents(styledParagraph, chars, 0, chars.length, charsLtoV, levels, factory);
0N/A
0N/A return new TextLine(factory.getFontRenderContext(), components, baselineOffsets,
0N/A chars, 0, chars.length, charsLtoV, levels, isDirectionLTR);
0N/A }
0N/A
0N/A /**
0N/A * Compute the components order from the given components array and
0N/A * logical-to-visual character mapping. May return null if canonical.
0N/A */
0N/A private static int[] computeComponentOrder(TextLineComponent[] components,
0N/A int[] charsLtoV) {
0N/A
0N/A /*
0N/A * Create a visual ordering for the glyph sets. The important thing
0N/A * here is that the values have the proper rank with respect to
0N/A * each other, not the exact values. For example, the first glyph
0N/A * set that appears visually should have the lowest value. The last
0N/A * should have the highest value. The values are then normalized
0N/A * to map 1-1 with positions in glyphs.
0N/A *
0N/A */
0N/A int[] componentOrder = null;
0N/A if (charsLtoV != null && components.length > 1) {
0N/A componentOrder = new int[components.length];
0N/A int gStart = 0;
0N/A for (int i = 0; i < components.length; i++) {
0N/A componentOrder[i] = charsLtoV[gStart];
0N/A gStart += components[i].getNumCharacters();
0N/A }
0N/A
0N/A componentOrder = BidiUtils.createContiguousOrder(componentOrder);
0N/A componentOrder = BidiUtils.createInverseMap(componentOrder);
0N/A }
0N/A return componentOrder;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Create a TextLine from the text. chars is just the text in the iterator.
0N/A */
0N/A public static TextLine standardCreateTextLine(FontRenderContext frc,
0N/A AttributedCharacterIterator text,
0N/A char[] chars,
0N/A float[] baselineOffsets) {
0N/A
0N/A StyledParagraph styledParagraph = new StyledParagraph(text, chars);
0N/A Bidi bidi = new Bidi(text);
0N/A if (bidi.isLeftToRight()) {
0N/A bidi = null;
0N/A }
0N/A int layoutFlags = 0; // no extra info yet, bidi determines run and line direction
0N/A TextLabelFactory factory = new TextLabelFactory(frc, chars, bidi, layoutFlags);
0N/A
0N/A boolean isDirectionLTR = true;
0N/A if (bidi != null) {
0N/A isDirectionLTR = bidi.baseIsLeftToRight();
0N/A }
0N/A return createLineFromText(chars, styledParagraph, factory, isDirectionLTR, baselineOffsets);
0N/A }
0N/A
0N/A
0N/A
0N/A /*
0N/A * A utility to get a range of text that is both logically and visually
0N/A * contiguous.
0N/A * If the entire range is ok, return limit, otherwise return the first
0N/A * directional change after start. We could do better than this, but
0N/A * it doesn't seem worth it at the moment.
0N/A private static int firstVisualChunk(int order[], byte direction[],
0N/A int start, int limit)
0N/A {
0N/A if (order != null) {
0N/A int min = order[start];
0N/A int max = order[start];
0N/A int count = limit - start;
0N/A for (int i = start + 1; i < limit; i++) {
0N/A min = Math.min(min, order[i]);
0N/A max = Math.max(max, order[i]);
0N/A if (max - min >= count) {
0N/A if (direction != null) {
0N/A byte baseLevel = direction[start];
0N/A for (int j = start + 1; j < i; j++) {
0N/A if (direction[j] != baseLevel) {
0N/A return j;
0N/A }
0N/A }
0N/A }
0N/A return i;
0N/A }
0N/A }
0N/A }
0N/A return limit;
0N/A }
0N/A */
0N/A
0N/A /**
0N/A * When this returns, the ACI's current position will be at the start of the
0N/A * first run which does NOT contain a GraphicAttribute. If no such run exists
0N/A * the ACI's position will be at the end, and this method will return false.
0N/A */
0N/A static boolean advanceToFirstFont(AttributedCharacterIterator aci) {
0N/A
0N/A for (char ch = aci.first(); ch != aci.DONE; ch = aci.setIndex(aci.getRunLimit())) {
0N/A
0N/A if (aci.getAttribute(TextAttribute.CHAR_REPLACEMENT) == null) {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A static float[] getNormalizedOffsets(float[] baselineOffsets, byte baseline) {
0N/A
0N/A if (baselineOffsets[baseline] != 0) {
0N/A float base = baselineOffsets[baseline];
0N/A float[] temp = new float[baselineOffsets.length];
0N/A for (int i = 0; i < temp.length; i++)
0N/A temp[i] = baselineOffsets[i] - base;
0N/A baselineOffsets = temp;
0N/A }
0N/A return baselineOffsets;
0N/A }
0N/A
0N/A static Font getFontAtCurrentPos(AttributedCharacterIterator aci) {
0N/A
0N/A Object value = aci.getAttribute(TextAttribute.FONT);
0N/A if (value != null) {
0N/A return (Font) value;
0N/A }
0N/A if (aci.getAttribute(TextAttribute.FAMILY) != null) {
0N/A return Font.getFont(aci.getAttributes());
0N/A }
0N/A
0N/A int ch = CodePointIterator.create(aci).next();
0N/A if (ch != CodePointIterator.DONE) {
0N/A FontResolver resolver = FontResolver.getInstance();
0N/A return resolver.getFont(resolver.getFontIndex(ch), aci.getAttributes());
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * The new version requires that chunks be at the same level.
0N/A */
0N/A private static int firstVisualChunk(int order[], byte direction[],
0N/A int start, int limit)
0N/A {
0N/A if (order != null && direction != null) {
0N/A byte dir = direction[start];
0N/A while (++start < limit && direction[start] == dir) {}
0N/A return start;
0N/A }
0N/A return limit;
0N/A }
0N/A
0N/A /*
0N/A * create a new line with characters between charStart and charLimit
0N/A * justified using the provided width and ratio.
0N/A */
0N/A public TextLine getJustifiedLine(float justificationWidth, float justifyRatio, int justStart, int justLimit) {
0N/A
0N/A TextLineComponent[] newComponents = new TextLineComponent[fComponents.length];
0N/A System.arraycopy(fComponents, 0, newComponents, 0, fComponents.length);
0N/A
0N/A float leftHang = 0;
0N/A float adv = 0;
0N/A float justifyDelta = 0;
0N/A boolean rejustify = false;
0N/A do {
0N/A adv = getAdvanceBetween(newComponents, 0, characterCount());
0N/A
0N/A // all characters outside the justification range must be in the base direction
0N/A // of the layout, otherwise justification makes no sense.
0N/A
0N/A float justifyAdvance = getAdvanceBetween(newComponents, justStart, justLimit);
0N/A
0N/A // get the actual justification delta
0N/A justifyDelta = (justificationWidth - justifyAdvance) * justifyRatio;
0N/A
0N/A // generate an array of GlyphJustificationInfo records to pass to
0N/A // the justifier. Array is visually ordered.
0N/A
0N/A // get positions that each component will be using
0N/A int[] infoPositions = new int[newComponents.length];
0N/A int infoCount = 0;
0N/A for (int visIndex = 0; visIndex < newComponents.length; visIndex++) {
0N/A int logIndex = getComponentLogicalIndex(visIndex);
0N/A infoPositions[logIndex] = infoCount;
0N/A infoCount += newComponents[logIndex].getNumJustificationInfos();
0N/A }
0N/A GlyphJustificationInfo[] infos = new GlyphJustificationInfo[infoCount];
0N/A
0N/A // get justification infos
0N/A int compStart = 0;
0N/A for (int i = 0; i < newComponents.length; i++) {
0N/A TextLineComponent comp = newComponents[i];
0N/A int compLength = comp.getNumCharacters();
0N/A int compLimit = compStart + compLength;
0N/A if (compLimit > justStart) {
0N/A int rangeMin = Math.max(0, justStart - compStart);
0N/A int rangeMax = Math.min(compLength, justLimit - compStart);
0N/A comp.getJustificationInfos(infos, infoPositions[i], rangeMin, rangeMax);
0N/A
0N/A if (compLimit >= justLimit) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // records are visually ordered, and contiguous, so start and end are
0N/A // simply the places where we didn't fetch records
0N/A int infoStart = 0;
0N/A int infoLimit = infoCount;
0N/A while (infoStart < infoLimit && infos[infoStart] == null) {
0N/A ++infoStart;
0N/A }
0N/A
0N/A while (infoLimit > infoStart && infos[infoLimit - 1] == null) {
0N/A --infoLimit;
0N/A }
0N/A
0N/A // invoke justifier on the records
0N/A TextJustifier justifier = new TextJustifier(infos, infoStart, infoLimit);
0N/A
0N/A float[] deltas = justifier.justify(justifyDelta);
0N/A
0N/A boolean canRejustify = rejustify == false;
0N/A boolean wantRejustify = false;
0N/A boolean[] flags = new boolean[1];
0N/A
0N/A // apply justification deltas
0N/A compStart = 0;
0N/A for (int i = 0; i < newComponents.length; i++) {
0N/A TextLineComponent comp = newComponents[i];
0N/A int compLength = comp.getNumCharacters();
0N/A int compLimit = compStart + compLength;
0N/A if (compLimit > justStart) {
0N/A int rangeMin = Math.max(0, justStart - compStart);
0N/A int rangeMax = Math.min(compLength, justLimit - compStart);
0N/A newComponents[i] = comp.applyJustificationDeltas(deltas, infoPositions[i] * 2, flags);
0N/A
0N/A wantRejustify |= flags[0];
0N/A
0N/A if (compLimit >= justLimit) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A rejustify = wantRejustify && !rejustify; // only make two passes
0N/A } while (rejustify);
0N/A
0N/A return new TextLine(frc, newComponents, fBaselineOffsets, fChars, fCharsStart,
0N/A fCharsLimit, fCharLogicalOrder, fCharLevels,
0N/A fIsDirectionLTR);
0N/A }
0N/A
0N/A // return the sum of the advances of text between the logical start and limit
0N/A public static float getAdvanceBetween(TextLineComponent[] components, int start, int limit) {
0N/A float advance = 0;
0N/A
0N/A int tlcStart = 0;
0N/A for(int i = 0; i < components.length; i++) {
0N/A TextLineComponent comp = components[i];
0N/A
0N/A int tlcLength = comp.getNumCharacters();
0N/A int tlcLimit = tlcStart + tlcLength;
0N/A if (tlcLimit > start) {
0N/A int measureStart = Math.max(0, start - tlcStart);
0N/A int measureLimit = Math.min(tlcLength, limit - tlcStart);
0N/A advance += comp.getAdvanceBetween(measureStart, measureLimit);
0N/A if (tlcLimit >= limit) {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A tlcStart = tlcLimit;
0N/A }
0N/A
0N/A return advance;
0N/A }
0N/A
0N/A LayoutPathImpl getLayoutPath() {
0N/A return lp;
0N/A }
0N/A}