### Eclipse Workspace Patch 1.0 #P org.eclipse.draw2d Index: src/org/eclipse/draw2d/text/FlowUtilities.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/text/FlowUtilities.java,v retrieving revision 1.56 diff -u -r1.56 FlowUtilities.java --- src/org/eclipse/draw2d/text/FlowUtilities.java 4 Apr 2006 12:43:12 -0000 1.56 +++ src/org/eclipse/draw2d/text/FlowUtilities.java 5 Sep 2007 15:16:56 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -15,18 +15,20 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontMetrics; +import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.TextLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.draw2d.FigureUtilities; +import org.eclipse.draw2d.geometry.Dimension; /** * Utility class for FlowFigures. * @author hudsonr * @since 2.1 */ -class FlowUtilities - extends FigureUtilities +public class FlowUtilities { interface LookAhead { @@ -61,10 +63,21 @@ return Math.min(macNL, unixNL); } -private static float getAverageCharWidth(TextFragmentBox fragment, Font font) { +/** + * Gets the average character width. + * + * @param fragment the supplied TextFragmentBox to use for calculation. + * if the length is 0 or if the width is or below 0, + * the average character width is taken from standard + * font metrics. + * @param font the font to use in case the TextFragmentBox conditions + * above are true. + * @return the average character width + */ +protected float getAverageCharWidth(TextFragmentBox fragment, Font font) { if (fragment.getWidth() > 0 && fragment.length != 0) return fragment.getWidth() / (float)fragment.length; - return getFontMetrics(font).getAverageCharWidth(); + return FigureUtilities.getFontMetrics(font).getAverageCharWidth(); } static int getBorderAscent(InlineFlow owner) { @@ -129,30 +142,24 @@ } } -private static int measureString(TextFragmentBox frag, String string, int guess, Font font) { +private int measureString(TextFragmentBox frag, String string, int guess, Font font) { if (frag.requiresBidi()) { // The text and/or could have changed if the lookAhead was invoked. This will // happen at most once. - TextLayout layout = getTextLayout(); - layout.setText(string); - layout.setFont(font); - return layout.getBounds(0, guess - 1).width; + return getTextLayoutBounds(string, font, 0, guess - 1).width; } else - return getStringDimension(string.substring(0, guess), font).x; + return getStringExtents(string.substring(0, guess), font).width; } -static void setupFragment(TextFragmentBox frag, Font f, String s) { +void setupFragment(TextFragmentBox frag, Font f, String s) { if (frag.getWidth() == -1 || frag.isTruncated()) { int width; if (s.length() == 0 || frag.length == 0) width = 0; else if (frag.requiresBidi()) { - TextLayout textLayout = getTextLayout(); - textLayout.setFont(f); - textLayout.setText(s); - width = textLayout.getBounds(0, frag.length - 1).width; + width = getTextLayoutBounds(s, f, 0, frag.length - 1).width; } else - width = getStringDimension(s.substring(0, frag.length), f).x; + width = getStringExtents(s.substring(0, frag.length), f).width; if (frag.isTruncated()) width += ELLIPSIS_SIZE; frag.setWidth(width); @@ -172,7 +179,7 @@ * @return the number of characters that will fit in the given space; can be 0 (eg., when * the first character of the given string is a newline) */ -public static int wrapFragmentInContext(TextFragmentBox frag, String string, +public int wrapFragmentInContext(TextFragmentBox frag, String string, FlowContext context, LookAhead lookahead, Font font, int wrapping) { frag.setTruncated(false); int strLen = string.length(); @@ -332,4 +339,56 @@ return result; } +/** + * Returns the Dimensions of s in Font f. + * + * @param s the string + * @param f the font + * @return the dimensions of the given string + * @since 2.0 + */ +public Dimension getStringExtents(String s, Font f) { + return FigureUtilities.getStringExtents(s, f); +} + +/** + * Returns the Dimensions of the given text, converting newlines and tabs appropriately. + * + * @param s the text + * @param f the font + * @return the dimensions of the given text + */ +public Dimension getTextExtents(String s, Font f) { + return FigureUtilities.getTextExtents(s, f); +} + +/** + * @see TextLayout#getBounds() + */ +protected Rectangle getTextLayoutBounds(String s, Font f, int start, int end) { + TextLayout textLayout = getTextLayout(); + textLayout.setFont(f); + textLayout.setText(s); + return textLayout.getBounds(start, end); +} + +/** + * Gets the font's ascent. + * @param font + * @return the font's ascent + */ +public int getAscent(Font font) { + FontMetrics fm = FigureUtilities.getFontMetrics(font); + return fm.getHeight() - fm.getDescent(); +} + +/** + * Gets the font's descent. + * @param font + * @return the font's descent + */ +public int getDescent(Font font) { + return FigureUtilities.getFontMetrics(font).getDescent(); +} + } Index: src/org/eclipse/draw2d/text/TextFlow.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java,v retrieving revision 1.55 diff -u -r1.55 TextFlow.java --- src/org/eclipse/draw2d/text/TextFlow.java 14 Feb 2006 20:13:32 -0000 1.55 +++ src/org/eclipse/draw2d/text/TextFlow.java 5 Sep 2007 15:16:56 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -12,11 +12,9 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.TextLayout; import org.eclipse.draw2d.ColorConstants; -import org.eclipse.draw2d.FigureUtilities; import org.eclipse.draw2d.Graphics; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.draw2d.geometry.Point; @@ -36,10 +34,11 @@ extends InlineFlow { -static final String ELLIPSIS = "..."; //$NON-NLS-1$ +public static final String ELLIPSIS = "..."; //$NON-NLS-1$ private BidiInfo bidiInfo; private int selectionEnd = -1; private String text; +private FlowUtilities flowUtilities = new FlowUtilities(); /** * Constructs a new TextFlow with the empty String. @@ -95,7 +94,7 @@ text = text.substring(1, index + 1); if (bidiInfo == null) - width[0] += FlowUtilities.getStringExtents(text, getFont()).width; + width[0] += flowUtilities.getStringExtents(text, getFont()).width; else { TextLayout textLayout = FlowUtilities.getTextLayout(); textLayout.setFont(getFont()); @@ -174,9 +173,8 @@ return findOffset(p, trailing, closestBox, index); } -int getAscent() { - FontMetrics fm = FigureUtilities.getFontMetrics(getFont()); - return fm.getHeight() - fm.getDescent(); +protected int getAscent() { + return getFlowUtilities().getAscent(getFont()); } /** @@ -267,7 +265,7 @@ if (trailing && offset < box.length) offset++; String substring = getText().substring(box.offset, box.offset + offset); - result.x = FigureUtilities.getStringExtents(substring, getFont()).width; + result.x = getFlowUtilities().getStringExtents(substring, getFont()).width; } else { TextLayout layout = FlowUtilities.getTextLayout(); layout.setFont(getFont()); @@ -282,8 +280,8 @@ return result; } -int getDescent() { - return FigureUtilities.getFontMetrics(getFont()).getDescent(); +protected int getDescent() { + return getFlowUtilities().getDescent(getFont()); } /** @@ -629,4 +627,12 @@ return Math.max(0, y - (box.getBaseline() + box.getLineRoot().getDescent())); } +public FlowUtilities getFlowUtilities() { + return flowUtilities; +} + +public void setFlowUtilities(FlowUtilities flowUtilities) { + this.flowUtilities = flowUtilities; +} + } \ No newline at end of file Index: src/org/eclipse/draw2d/text/SimpleTextLayout.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/text/SimpleTextLayout.java,v retrieving revision 1.10 diff -u -r1.10 SimpleTextLayout.java --- src/org/eclipse/draw2d/text/SimpleTextLayout.java 7 Sep 2005 21:34:54 -0000 1.10 +++ src/org/eclipse/draw2d/text/SimpleTextLayout.java 5 Sep 2007 15:16:56 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -48,13 +48,14 @@ int i = 0; int offset = 0; + FlowUtilities flowUtilities = textFlow.getFlowUtilities(); do { nextLineBreak(text, offset); fragment = getFragment(i++, fragments); fragment.length = result - offset; fragment.offset = offset; fragment.setWidth(-1); - FlowUtilities.setupFragment(fragment, font, text); + flowUtilities.setupFragment(fragment, font, text); getContext().addToCurrentLine(fragment); getContext().endLine(); offset = result + delimeterLength; Index: src/org/eclipse/draw2d/text/ParagraphTextLayout.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/text/ParagraphTextLayout.java,v retrieving revision 1.31 diff -u -r1.31 ParagraphTextLayout.java --- src/org/eclipse/draw2d/text/ParagraphTextLayout.java 1 Jun 2005 19:56:30 -0000 1.31 +++ src/org/eclipse/draw2d/text/ParagraphTextLayout.java 5 Sep 2007 15:16:56 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -168,6 +168,7 @@ context.addToCurrentLine(fragment); } + FlowUtilities flowUtilities = textFlow.getFlowUtilities(); for (seg = 0; seg < segments.length; seg++) { segment = segments[seg]; lookahead.setIndex(seg); @@ -178,7 +179,7 @@ fragment.offset = offset; fragment.setBidiLevel(levelInfo[seg * 2]); - advance = FlowUtilities.wrapFragmentInContext(fragment, segment, + advance = flowUtilities.wrapFragmentInContext(fragment, segment, context, lookahead, font, wrappingStyle); segment = segment.substring(advance); offset += advance;