-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.scenegraph.utils;\r
-\r
-import java.awt.FontMetrics;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-\r
-public final class TextUtil {\r
-\r
- /**\r
- * @param text\r
- * @param g\r
- * @param bounds\r
- * @return\r
- */\r
- public static String[] wordWrap(String text, FontMetrics metrics, int maxLineWidth) {\r
- Collection<String> result = new ArrayList<String>();\r
- int[] whitespacePos = new int[8];\r
- int whitespaceTop = 0;\r
-\r
- int pos = 0;\r
- int length = text.length();\r
-\r
- int cumulatedLineWidth = 0;\r
-\r
- int dotWidth = metrics.charWidth('.');\r
- int dot3Width = 3*dotWidth;\r
- boolean nonWhitespaceFound = false;\r
-\r
- out:\r
- while (pos < length) {\r
- char c = text.charAt(pos);\r
- int cWidth = metrics.charWidth(c);\r
- if (Character.isWhitespace(c)) {\r
- if (nonWhitespaceFound) {\r
- if (whitespacePos.length <= whitespaceTop)\r
- whitespacePos = Arrays.copyOf(whitespacePos, whitespacePos.length*2);\r
- whitespacePos[whitespaceTop++] = pos;\r
- }\r
- } else {\r
- nonWhitespaceFound = true;\r
- }\r
-\r
-// System.out.println("cumulated: " + cumulatedLineWidth);\r
-// System.out.println(" width: " + cWidth);\r
-// System.out.println(" max: " + maxLineWidth);\r
- if (cumulatedLineWidth + cWidth > maxLineWidth) {\r
-// System.out.println("overran line: " + (result.size() + 1) + ": '" + text.substring(0, pos) + "'");\r
-\r
- // Look for last whitespace position to cut at\r
- int nextLineStartPos = -1;\r
- if (whitespaceTop > 0) {\r
- int cutPos = whitespacePos[--whitespaceTop];\r
- result.add(text.substring(0, cutPos));\r
- nextLineStartPos = cutPos + 1;\r
- } else {\r
- // No whitespace available, just cut the string and insert "..." at the end.\r
- int rollbackWidth = cumulatedLineWidth + dot3Width;\r
- int rollbackPos = pos;\r
- while (rollbackPos > 0 && rollbackWidth > maxLineWidth) {\r
- rollbackWidth -= metrics.charWidth(text.charAt(--rollbackPos));\r
- }\r
- // Cannot word wrap a single character anything into the specified max width.\r
- if (rollbackPos == 0) {\r
- result.add(text);\r
- break out;\r
- }\r
-\r
- result.add(text.substring(0, rollbackPos) + "...");\r
-\r
- // Search for next whitespace in the text after pos and start the next line from there.\r
- int nextWhitespacePos = pos;\r
- for (;nextWhitespacePos < length && !Character.isWhitespace(text.charAt(nextWhitespacePos)); ++nextWhitespacePos);\r
- nextLineStartPos = nextWhitespacePos < length ? nextWhitespacePos + 1 : nextWhitespacePos;\r
- }\r
- text = text.substring(nextLineStartPos);\r
- length = text.length();\r
- pos = 0;\r
- cumulatedLineWidth = 0;\r
- nonWhitespaceFound = false;\r
- whitespaceTop = 0;\r
- continue;\r
- }\r
-\r
- cumulatedLineWidth += cWidth;\r
- ++pos;\r
- }\r
-\r
- if (text.length() > 0)\r
- result.add(text);\r
-\r
- return result.toArray(new String[result.size()]);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.scenegraph.utils;
+
+import java.awt.FontMetrics;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+
+public final class TextUtil {
+
+ /**
+ * @param text
+ * @param g
+ * @param bounds
+ * @return
+ */
+ public static String[] wordWrap(String text, FontMetrics metrics, int maxLineWidth) {
+ Collection<String> result = new ArrayList<String>();
+ int[] whitespacePos = new int[8];
+ int whitespaceTop = 0;
+
+ int pos = 0;
+ int length = text.length();
+
+ int cumulatedLineWidth = 0;
+
+ int dotWidth = metrics.charWidth('.');
+ int dot3Width = 3*dotWidth;
+ boolean nonWhitespaceFound = false;
+
+ out:
+ while (pos < length) {
+ char c = text.charAt(pos);
+ int cWidth = metrics.charWidth(c);
+ if (Character.isWhitespace(c)) {
+ if (nonWhitespaceFound) {
+ if (whitespacePos.length <= whitespaceTop)
+ whitespacePos = Arrays.copyOf(whitespacePos, whitespacePos.length*2);
+ whitespacePos[whitespaceTop++] = pos;
+ }
+ } else {
+ nonWhitespaceFound = true;
+ }
+
+// System.out.println("cumulated: " + cumulatedLineWidth);
+// System.out.println(" width: " + cWidth);
+// System.out.println(" max: " + maxLineWidth);
+ if (cumulatedLineWidth + cWidth > maxLineWidth) {
+// System.out.println("overran line: " + (result.size() + 1) + ": '" + text.substring(0, pos) + "'");
+
+ // Look for last whitespace position to cut at
+ int nextLineStartPos = -1;
+ if (whitespaceTop > 0) {
+ int cutPos = whitespacePos[--whitespaceTop];
+ result.add(text.substring(0, cutPos));
+ nextLineStartPos = cutPos + 1;
+ } else {
+ // No whitespace available, just cut the string and insert "..." at the end.
+ int rollbackWidth = cumulatedLineWidth + dot3Width;
+ int rollbackPos = pos;
+ while (rollbackPos > 0 && rollbackWidth > maxLineWidth) {
+ rollbackWidth -= metrics.charWidth(text.charAt(--rollbackPos));
+ }
+ // Cannot word wrap a single character anything into the specified max width.
+ if (rollbackPos == 0) {
+ result.add(text);
+ break out;
+ }
+
+ result.add(text.substring(0, rollbackPos) + "...");
+
+ // Search for next whitespace in the text after pos and start the next line from there.
+ int nextWhitespacePos = pos;
+ for (;nextWhitespacePos < length && !Character.isWhitespace(text.charAt(nextWhitespacePos)); ++nextWhitespacePos);
+ nextLineStartPos = nextWhitespacePos < length ? nextWhitespacePos + 1 : nextWhitespacePos;
+ }
+ text = text.substring(nextLineStartPos);
+ length = text.length();
+ pos = 0;
+ cumulatedLineWidth = 0;
+ nonWhitespaceFound = false;
+ whitespaceTop = 0;
+ continue;
+ }
+
+ cumulatedLineWidth += cWidth;
+ ++pos;
+ }
+
+ if (text.length() > 0)
+ result.add(text);
+
+ return result.toArray(new String[result.size()]);
+ }
+
+}