This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[gui] Patch: javax.swing.text.Utilities
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 11 Aug 2004 13:14:04 +0200
- Subject: [gui] Patch: javax.swing.text.Utilities
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi list,
I just commited the attached patch to add a new file written by Roman
Kennke.
Michael
2004-08-11 Roman Kennke <roman@ontographics.com>
* javax/swing/text/Utilities.java: New file.
2004-08-11 Michael Koch <konqueror@gmx.de>
* Makefile.am: Added javax/swing/text/Utilities.java.
* Makefile.in: Regenerated.
- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFBGf+AWSOgCCdjSDsRAgVuAJwL990rHJ2wWaBMSCxJTBCK9CJPAQCeI+VP
3LuLP3gwrjK+cIodv4ZBsGg=
=Df0B
-----END PGP SIGNATURE-----
Index: javax/swing/text/Utilities.java
===================================================================
RCS file: javax/swing/text/Utilities.java
diff -N javax/swing/text/Utilities.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ javax/swing/text/Utilities.java 11 Aug 2004 11:08:48 -0000
@@ -0,0 +1,222 @@
+/* Utilities.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+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 javax.swing.text;
+
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+
+/**
+ * A set of utilities to deal with text. This is used by several other classes
+ * inside this package.
+ *
+ * @author Roman Kennke <roman@ontographics.com>
+ */
+public class Utilities
+{
+ /**
+ * The length of the char buffer that holds the characters to be drawn.
+ */
+ private static final int BUF_LENGTH = 64;
+
+ /**
+ * Creates a new Utilities object.
+ */
+ public Utilities()
+ {
+ // Nothing to be done here.
+ }
+
+ /**
+ * Draws the given text segment. Contained tabs and newline characters
+ * are taken into account. Tabs are expanded using the
+ * specified {@link TabExpander}.
+ *
+ * @param s the text fragment to be drawn.
+ * @param x the x position for drawing.
+ * @param y the y position for drawing.
+ * @param g the {@link Graphics} context for drawing.
+ * @param e the {@link TabExpander} which specifies the Tab-expanding
+ * technique.
+ * @param startOffset starting offset in the text.
+ * @return the x coordinate at the end of the drawn text.
+ */
+ public static final int drawTabbedText(Segment s, int x, int y, Graphics g,
+ TabExpander e, int startOffset)
+ {
+ // This buffers the chars to be drawn.
+ char[] buffer = new char[BUF_LENGTH];
+
+ // The current index in the buffer.
+ int bufIndex = 0;
+
+ // Set the character iterator to startOffset.
+ s.setIndex(startOffset);
+
+ // The current character.
+ char currentChar = Segment.DONE;
+
+ // The current x and y pixel coordinates.
+ int pixelX = x;
+ int pixelY = y;
+
+ // The font metrics of the current selected font.
+ FontMetrics metrics = g.getFontMetrics();
+
+ // Loop over every character.
+ do
+ {
+ currentChar = s.next();
+
+ if (currentChar != Segment.DONE)
+ {
+ switch (currentChar)
+ {
+ case '\t':
+ // In case we have a tab, we must draw the
+ // buffer and 'jump' over the tab.
+ g.drawChars(buffer, 0, bufIndex, pixelX, pixelY);
+ pixelX += metrics.charWidth(buffer[bufIndex]);
+ pixelX += (int) e.nextTabStop((float) pixelX, s.getIndex());
+ bufIndex = 0;
+ break;
+ case '\n':
+ // In case we have a newline, we must draw
+ // the buffer and jump on the next line.
+ g.drawChars(buffer, 0, bufIndex, pixelX, pixelY);
+ pixelY -= metrics.getHeight();
+ pixelX = x;
+ bufIndex = 0;
+ break;
+ default:
+ // Here we have to put the char into the buffer
+ // and draw it, if it's full.
+ buffer[bufIndex] = currentChar;
+ bufIndex++;
+ if (bufIndex == BUF_LENGTH)
+ {
+ g.drawChars(buffer, 0, bufIndex, pixelX, pixelY);
+ pixelX += metrics.charWidth(buffer[bufIndex]);
+ bufIndex = 0;
+ }
+ break;
+ }
+ }
+ }
+ while (currentChar != Segment.DONE);
+
+ return pixelX;
+ }
+
+ /**
+ * Determines the width, that the given text <code>s</code> would take
+ * if it was printed with the given {@link java.awt.FontMetrics} on the
+ * specified screen position.
+ * @param s the text fragment
+ * @param metrics the font metrics of the font to be used
+ * @param x the x coordinate of the point at which drawing should be done
+ * @param e the {@link TabExpander} to be used
+ * @param startOffset the index in <code>s</code> where to start
+ * @returns the width of the given text s. This takes tabs and newlines
+ * into account.
+ */
+ public static final int getTabbedTextWidth(Segment s, FontMetrics metrics,
+ int x, TabExpander e,
+ int startOffset)
+ {
+ // This buffers the chars to be drawn.
+ char[] buffer = new char[BUF_LENGTH];
+
+ // The current index in the buffer.
+ int bufIndex = 0;
+
+ // The current x coordinate.
+ int pixelX = x;
+
+ // The current maximum width.
+ int maxWidth = 0;
+
+ // The current character.
+ char currentChar = Segment.DONE;
+
+ // Loop over every char.
+ do
+ {
+ currentChar = s.next();
+
+ if (currentChar != Segment.DONE)
+ {
+ switch (currentChar)
+ {
+ case '\t':
+ // In case we have a tab, we must 'draw' the
+ // buffer and 'jump' over the tab.
+ pixelX += metrics.charWidth(buffer[bufIndex]);
+ pixelX += (int) e.nextTabStop((float) pixelX, s.getIndex());
+ bufIndex = 0;
+ break;
+ case '\n':
+ // In case we have a newline, we must 'draw'
+ // the buffer and jump on the next line.
+ pixelX += metrics.charWidth(buffer[bufIndex]);
+ maxWidth = Math.max(maxWidth, pixelX - x);
+ pixelX = x;
+ bufIndex = 0;
+ break;
+ default:
+ // Here we have to put the char into the buffer
+ // and draw it, if it's full.
+ buffer[bufIndex] = currentChar;
+ bufIndex++;
+ if (bufIndex == BUF_LENGTH)
+ {
+ pixelX += metrics.charWidth(buffer[bufIndex]);
+ bufIndex = 0;
+ }
+ break;
+ }
+ }
+ else
+ // Take the last 'drawn' line into account.
+ maxWidth = Math.max(maxWidth, pixelX - x);
+ }
+ while (currentChar != Segment.DONE);
+
+ return maxWidth;
+ }
+}
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.361.2.35
diff -u -b -B -r1.361.2.35 Makefile.am
--- Makefile.am 10 Aug 2004 23:29:30 -0000 1.361.2.35
+++ Makefile.am 11 Aug 2004 11:08:48 -0000
@@ -1530,6 +1530,7 @@
javax/swing/text/TabExpander.java \
javax/swing/text/TabSet.java \
javax/swing/text/TabStop.java \
+javax/swing/text/Utilities.java \
javax/swing/text/View.java \
javax/swing/text/ViewFactory.java \
javax/swing/text/MutableAttributeSet.java \
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.385.2.35
diff -u -b -B -r1.385.2.35 Makefile.in
--- Makefile.in 10 Aug 2004 23:29:30 -0000 1.385.2.35
+++ Makefile.in 11 Aug 2004 11:08:51 -0000
@@ -1208,6 +1208,7 @@
javax/swing/text/TabExpander.java \
javax/swing/text/TabSet.java \
javax/swing/text/TabStop.java \
+javax/swing/text/Utilities.java \
javax/swing/text/View.java \
javax/swing/text/ViewFactory.java \
javax/swing/text/MutableAttributeSet.java \
@@ -3279,8 +3280,8 @@
javax/swing/text/StyleConstants.lo javax/swing/text/StyleContext.lo \
javax/swing/text/TabableView.lo javax/swing/text/TabExpander.lo \
javax/swing/text/TabSet.lo javax/swing/text/TabStop.lo \
-javax/swing/text/View.lo javax/swing/text/ViewFactory.lo \
-javax/swing/text/MutableAttributeSet.lo \
+javax/swing/text/Utilities.lo javax/swing/text/View.lo \
+javax/swing/text/ViewFactory.lo javax/swing/text/MutableAttributeSet.lo \
javax/swing/text/NavigationFilter.lo javax/swing/text/StyledDocument.lo \
javax/swing/text/StyledEditorKit.lo javax/swing/text/TextAction.lo \
javax/swing/text/html/HTML.lo \
@@ -5375,8 +5376,9 @@
.deps/javax/swing/text/StyledEditorKit.P \
.deps/javax/swing/text/TabExpander.P .deps/javax/swing/text/TabSet.P \
.deps/javax/swing/text/TabStop.P .deps/javax/swing/text/TabableView.P \
-.deps/javax/swing/text/TextAction.P .deps/javax/swing/text/View.P \
-.deps/javax/swing/text/ViewFactory.P .deps/javax/swing/text/html/HTML.P \
+.deps/javax/swing/text/TextAction.P .deps/javax/swing/text/Utilities.P \
+.deps/javax/swing/text/View.P .deps/javax/swing/text/ViewFactory.P \
+.deps/javax/swing/text/html/HTML.P \
.deps/javax/swing/text/html/parser/ParserDelegator.P \
.deps/javax/swing/tree/AbstractLayoutCache.P \
.deps/javax/swing/tree/DefaultMutableTreeNode.P \