This one implements the last missing piece in TextLayout, the visual highlighting of ranges. I found the Harmony tests here a little difficult as it depends on other stuff beeing working correctly (most notable getBlackBoxBounds()) which obviously isn't good with us. So to show correctness I wrote a small testprogram with bidi and let it highlight (logical) characters 3-7:
http://kennke.org/~roman/textlayout.png The green thing is what is returned by getVisualHighlightShape() and the red things are the carets returned by getCaretShape() from my last patch. So, telling by that, both of these seem to work correctly. 2006-11-27 Roman Kennke <[EMAIL PROTECTED]> * java/awt/font/TextLayout.java (TextLayout(TextLayout,int,int)): Also layout the new runs. (getVisualHighlightShape): Implemented. (layoutRuns): Fixed boundary so that the last run is also laid out. (left): New helper method. (right): New helper method. /Roman
Index: java/awt/font/TextLayout.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/awt/font/TextLayout.java,v retrieving revision 1.21 diff -u -1 -5 -r1.21 TextLayout.java --- java/awt/font/TextLayout.java 27 Nov 2006 13:11:16 -0000 1.21 +++ java/awt/font/TextLayout.java 27 Nov 2006 14:41:21 -0000 @@ -26,36 +26,35 @@ As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.awt.font; -import gnu.classpath.NotImplementedException; - import java.awt.Font; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.AffineTransform; +import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.awt.geom.GeneralPath; import java.awt.geom.Point2D; import java.text.CharacterIterator; import java.text.AttributedCharacterIterator; import java.text.Bidi; import java.util.ArrayList; import java.util.Map; /** * @author Sven de Marothy */ public final class TextLayout implements Cloneable { /** @@ -286,30 +285,31 @@ // Copy only the relevant parts of the first and last runs. int beginGlyphIndex = (i > 0) ? 0 : t.charIndices[startIndex][1]; int numEntries = ( i < nRuns - 1) ? gv.getNumGlyphs() : 1 + t.charIndices[endIndex - 1][1] - beginGlyphIndex; int[] codes = gv.getGlyphCodes(beginGlyphIndex, numEntries, null); gv = font.createGlyphVector(frc, codes); runs[i] = new Run(gv, font, run.runStart - startIndex, run.runEnd - startIndex); } runs[nRuns - 1].runEnd = endIndex - 1; setCharIndices(); setupMappings(); determineWhiteSpace(); + layoutRuns(); } private void setCharIndices() { charIndices = new int[ getCharacterCount() ][2]; int i = 0; int currentChar = 0; for(int run = 0; run < runs.length; run++) { currentChar = -1; Run current = runs[run]; GlyphVector gv = current.glyphVector; for( int gi = 0; gi < gv.getNumGlyphs(); gi++) { if( gv.getGlyphCharIndex( gi ) != currentChar ) @@ -940,33 +940,105 @@ } return totalAdvance; } public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, TextHitInfo secondEndpoint) { return getVisualHighlightShape( firstEndpoint, secondEndpoint, getBounds() ); } public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, TextHitInfo secondEndpoint, Rectangle2D bounds) - throws NotImplementedException { - throw new Error ("not implemented"); + GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD); + Shape caret1 = getCaretShape(firstEndpoint, bounds); + path.append(caret1, false); + Shape caret2 = getCaretShape(secondEndpoint, bounds); + path.append(caret2, false); + // Append left (top) bounds to selection if necessary. + int c1 = hitToCaret(firstEndpoint); + int c2 = hitToCaret(secondEndpoint); + if (c1 == 0 || c2 == 0) + { + path.append(left(bounds), false); + } + // Append right (bottom) bounds if necessary. + if (c1 == length || c2 == length) + { + path.append(right(bounds), false); + } + System.err.println("visual hl bounds: " + path.getBounds2D()); + System.err.println("bb bounds:" + getBlackBoxBounds(3, 7).getBounds2D()); + return path.getBounds2D(); + } + + /** + * Returns the shape that makes up the left (top) edge of this text layout. + * + * @param b the bounds + * + * @return the shape that makes up the left (top) edge of this text layout + */ + private Shape left(Rectangle2D b) + { + GeneralPath left = new GeneralPath(GeneralPath.WIND_EVEN_ODD); + left.append(getCaretShape(TextHitInfo.beforeOffset(0)), false); + if (isVertical()) + { + float y = (float) b.getMinY(); + left.append(new Line2D.Float((float) b.getMinX(), y, + (float) b.getMaxX(), y), false); + } + else + { + float x = (float) b.getMinX(); + left.append(new Line2D.Float(x, (float) b.getMinY(), + x, (float) b.getMaxY()), false); + } + return left.getBounds2D(); + } + + /** + * Returns the shape that makes up the right (bottom) edge of this text + * layout. + * + * @param b the bounds + * + * @return the shape that makes up the right (bottom) edge of this text + * layout + */ + private Shape right(Rectangle2D b) + { + GeneralPath right = new GeneralPath(GeneralPath.WIND_EVEN_ODD); + right.append(getCaretShape(TextHitInfo.afterOffset(length)), false); + if (isVertical()) + { + float y = (float) b.getMaxY(); + right.append(new Line2D.Float((float) b.getMinX(), y, + (float) b.getMaxX(), y), false); + } + else + { + float x = (float) b.getMaxX(); + right.append(new Line2D.Float(x, (float) b.getMinY(), + x, (float) b.getMaxY()), false); + } + return right.getBounds2D(); } public TextHitInfo getVisualOtherHit (TextHitInfo hit) { checkHitInfo(hit); int hitIndex = hit.getCharIndex(); int index; boolean leading; if (hitIndex == -1 || hitIndex == length) { // Boundary case. int visual; if (isLeftToRight() == (hitIndex == -1)) visual = 0; @@ -1294,31 +1366,31 @@ { Run run = runs[i]; if (run.runStart <= index && run.runEnd > index) found = run; } return found; } /** * Computes the layout locations for each run. */ private void layoutRuns() { float loc = 0.0F; float lastWidth = 0.0F; - for (int i = 0; i < runs.length - 1; i++) + for (int i = 0; i < runs.length; i++) { runs[i].location = loc; Rectangle2D bounds = runs[i].glyphVector.getLogicalBounds(); loc += isVertical() ? bounds.getHeight() : bounds.getWidth(); } } /** * Inner class describing a caret policy */ public static class CaretPolicy { public CaretPolicy() { }