diff org.eclipse.draw2d/src/org/eclipse/draw2d/TextUtilities.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/TextUtilities.d	Sat Mar 14 18:23:29 2009 +0100
@@ -0,0 +1,140 @@
+/*******************************************************************************
+ * Copyright (c) 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+
+module org.eclipse.draw2d.TextUtilities;
+
+import java.lang.all;
+
+
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.FontMetrics;
+import org.eclipse.draw2d.geometry.Dimension;
+import org.eclipse.draw2d.FigureUtilities;
+
+/**
+ * Provides miscellaneous text operations. Clients may subclass this class if
+ * necessary.
+ *
+ * @author crevells
+ * @since 3.4
+ */
+public class TextUtilities {
+
+    /**
+     * a singleton default instance
+     */
+    private static TextUtilities INSTANCE_;
+    public static TextUtilities INSTANCE(){
+        if( INSTANCE_ is null ){
+            synchronized( TextUtilities.classinfo ){
+                if( INSTANCE_ is null ){
+                    INSTANCE_ = new TextUtilities();
+                }
+            }
+        }
+        return INSTANCE_;
+    }
+    /**
+     * Returns the Dimensions of <i>s</i> in Font <i>f</i>.
+     *
+     * @param s
+     *            the string
+     * @param f
+     *            the font
+     * @return the dimensions of the given string
+     */
+    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);
+    }
+
+    /**
+     * 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();
+    }
+
+    /**
+     * Returns the largest substring of <i>s</i> in Font <i>f</i> that can be
+     * confined to the number of pixels in <i>availableWidth<i>.
+     *
+     * @param s
+     *            the original string
+     * @param f
+     *            the font
+     * @param availableWidth
+     *            the available width
+     * @return the largest substring that fits in the given width
+     */
+    public int getLargestSubstringConfinedTo(String s, Font f,
+            int availableWidth) {
+        FontMetrics metrics = FigureUtilities.getFontMetrics(f);
+        int min, max;
+        float avg = metrics.getAverageCharWidth();
+        min = 0;
+        max = s.length + 1;
+
+        // The size of the current guess
+        int guess = 0, guessSize = 0;
+        while ((max - min) > 1) {
+            // Pick a new guess size
+            // New guess is the last guess plus the missing width in pixels
+            // divided by the average character size in pixels
+            guess = guess + cast(int) ((availableWidth - guessSize) / avg);
+
+            if (guess >= max)
+                guess = max - 1;
+            if (guess <= min)
+                guess = min + 1;
+
+            // Measure the current guess
+            guessSize = getTextExtents(s.substring(0, guess), f).width;
+
+            if (guessSize < availableWidth)
+                // We did not use the available width
+                min = guess;
+            else
+                // We exceeded the available width
+                max = guess;
+        }
+        return min;
+    }
+}