Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / font / TextLayout.java
blob7d6c4d8c63598f91bae9f8bf49bcce34a9e877eb
1 /* TextLayout.java --
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.font;
41 import gnu.java.awt.ClasspathToolkit;
42 import gnu.java.awt.peer.ClasspathTextLayoutPeer;
44 import java.awt.Font;
45 import java.awt.Graphics2D;
46 import java.awt.Shape;
47 import java.awt.Toolkit;
48 import java.awt.geom.AffineTransform;
49 import java.awt.geom.Rectangle2D;
50 import java.text.AttributedCharacterIterator;
51 import java.text.AttributedString;
52 import java.util.Map;
54 /**
55 * @author Michael Koch
57 public final class TextLayout implements Cloneable
59 public static final CaretPolicy DEFAULT_CARET_POLICY = new CaretPolicy ();
60 ClasspathTextLayoutPeer peer;
62 public static class CaretPolicy
64 public CaretPolicy ()
66 // Do nothing here.
69 public TextHitInfo getStrongCaret (TextHitInfo hit1, TextHitInfo hit2,
70 TextLayout layout)
72 return layout.peer.getStrongCaret(hit1, hit2);
76 public TextLayout (AttributedCharacterIterator text, FontRenderContext frc)
78 AttributedString as = new AttributedString (text);
79 ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
80 peer = tk.getClasspathTextLayoutPeer(as, frc);
83 public TextLayout (String string, Font font, FontRenderContext frc)
85 AttributedString as = new AttributedString (string);
86 as.addAttribute (TextAttribute.FONT, font);
87 ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
88 peer = tk.getClasspathTextLayoutPeer(as, frc);
91 public TextLayout (String string, Map attributes, FontRenderContext frc)
93 AttributedString as = new AttributedString (string, attributes);
94 ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
95 peer = tk.getClasspathTextLayoutPeer(as, frc);
98 protected Object clone ()
102 TextLayout tl = (TextLayout) super.clone ();
103 tl.peer = (ClasspathTextLayoutPeer) this.peer.clone();
104 return tl;
106 catch (CloneNotSupportedException e)
108 // This should never occur
109 throw new InternalError ();
114 public void draw (Graphics2D g2, float x, float y)
116 peer.draw(g2, x, y);
119 public boolean equals (Object obj)
121 if (! (obj instanceof TextLayout))
122 return false;
124 return equals ((TextLayout) obj);
127 public boolean equals (TextLayout tl)
129 return this.peer.equals(tl.peer);
132 public float getAdvance ()
134 return peer.getAdvance();
137 public float getAscent ()
139 return peer.getAscent();
142 public byte getBaseline ()
144 return peer.getBaseline();
147 public float[] getBaselineOffsets ()
149 return peer.getBaselineOffsets();
152 public Shape getBlackBoxBounds (int firstEndpoint, int secondEndpoint)
154 return peer.getBlackBoxBounds(firstEndpoint, secondEndpoint);
157 public Rectangle2D getBounds()
159 return peer.getBounds();
162 public float[] getCaretInfo (TextHitInfo hit)
164 return getCaretInfo(hit, getBounds());
167 public float[] getCaretInfo (TextHitInfo hit, Rectangle2D bounds)
169 return peer.getCaretInfo(hit, bounds);
172 public Shape getCaretShape (TextHitInfo hit)
174 return getCaretShape(hit, getBounds());
177 public Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds)
179 return peer.getCaretShape(hit, bounds);
182 public Shape[] getCaretShapes (int offset)
184 return getCaretShapes(offset, getBounds());
187 public Shape[] getCaretShapes (int offset, Rectangle2D bounds)
189 return getCaretShapes(offset, getBounds(), DEFAULT_CARET_POLICY);
192 public Shape[] getCaretShapes (int offset, Rectangle2D bounds,
193 TextLayout.CaretPolicy policy)
195 return peer.getCaretShapes(offset, bounds, policy);
198 public int getCharacterCount ()
200 return peer.getCharacterCount();
203 public byte getCharacterLevel (int index)
205 return peer.getCharacterLevel(index);
208 public float getDescent ()
210 return peer.getDescent();
213 public TextLayout getJustifiedLayout (float justificationWidth)
215 return peer.getJustifiedLayout(justificationWidth);
218 public float getLeading ()
220 return peer.getLeading();
223 public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint)
225 return getLogicalHighlightShape (firstEndpoint, secondEndpoint, getBounds());
228 public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint,
229 Rectangle2D bounds)
231 return peer.getLogicalHighlightShape(firstEndpoint, secondEndpoint, bounds);
234 public int[] getLogicalRangesForVisualSelection (TextHitInfo firstEndpoint,
235 TextHitInfo secondEndpoint)
237 return peer.getLogicalRangesForVisualSelection(firstEndpoint, secondEndpoint);
240 public TextHitInfo getNextLeftHit (int offset)
242 return getNextLeftHit(offset, DEFAULT_CARET_POLICY);
245 public TextHitInfo getNextLeftHit (int offset, TextLayout.CaretPolicy policy)
247 return peer.getNextLeftHit(offset, policy);
250 public TextHitInfo getNextLeftHit (TextHitInfo hit)
252 return getNextLeftHit(hit.getCharIndex());
255 public TextHitInfo getNextRightHit (int offset)
257 return getNextRightHit(offset, DEFAULT_CARET_POLICY);
260 public TextHitInfo getNextRightHit (int offset, TextLayout.CaretPolicy policy)
262 return peer.getNextRightHit(offset, policy);
265 public TextHitInfo getNextRightHit (TextHitInfo hit)
267 return getNextRightHit(hit.getCharIndex());
270 public Shape getOutline (AffineTransform tx)
272 return peer.getOutline(tx);
275 public float getVisibleAdvance ()
277 return peer.getVisibleAdvance();
280 public Shape getVisualHighlightShape (TextHitInfo firstEndpoint,
281 TextHitInfo secondEndpoint)
283 return getVisualHighlightShape(firstEndpoint, secondEndpoint, getBounds());
286 public Shape getVisualHighlightShape (TextHitInfo firstEndpoint,
287 TextHitInfo secondEndpoint,
288 Rectangle2D bounds)
290 return peer.getVisualHighlightShape(firstEndpoint, secondEndpoint, bounds);
293 public TextHitInfo getVisualOtherHit (TextHitInfo hit)
295 return peer.getVisualOtherHit(hit);
298 protected void handleJustify (float justificationWidth)
300 peer.handleJustify(justificationWidth);
303 public int hashCode ()
305 return peer.hashCode();
308 public TextHitInfo hitTestChar (float x, float y)
310 return hitTestChar(x, y, getBounds());
313 public TextHitInfo hitTestChar (float x, float y, Rectangle2D bounds)
315 return peer.hitTestChar(x, y, bounds);
318 public boolean isLeftToRight ()
320 return peer.isLeftToRight();
323 public boolean isVertical ()
325 return peer.isVertical();
328 public String toString ()
330 return peer.toString();