1 |
/* TextLayout.java |
/* TextLayout.java -- |
2 |
Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
38 |
|
|
39 |
package java.awt.font; |
package java.awt.font; |
40 |
|
|
41 |
|
import gnu.java.awt.ClasspathToolkit; |
42 |
|
import gnu.java.awt.peer.ClasspathTextLayoutPeer; |
43 |
|
|
44 |
import java.awt.Font; |
import java.awt.Font; |
45 |
import java.awt.Graphics2D; |
import java.awt.Graphics2D; |
46 |
import java.awt.Shape; |
import java.awt.Shape; |
47 |
|
import java.awt.Toolkit; |
48 |
import java.awt.geom.AffineTransform; |
import java.awt.geom.AffineTransform; |
49 |
import java.awt.geom.Rectangle2D; |
import java.awt.geom.Rectangle2D; |
50 |
import java.text.AttributedCharacterIterator; |
import java.text.AttributedCharacterIterator; |
51 |
import java.text.AttributedString; |
import java.text.AttributedString; |
|
import java.text.CharacterIterator; |
|
52 |
import java.util.Map; |
import java.util.Map; |
53 |
|
|
54 |
/** |
/** |
57 |
public final class TextLayout implements Cloneable |
public final class TextLayout implements Cloneable |
58 |
{ |
{ |
59 |
public static final CaretPolicy DEFAULT_CARET_POLICY = new CaretPolicy (); |
public static final CaretPolicy DEFAULT_CARET_POLICY = new CaretPolicy (); |
60 |
|
ClasspathTextLayoutPeer peer; |
61 |
|
|
62 |
public static class CaretPolicy |
public static class CaretPolicy |
63 |
{ |
{ |
69 |
public TextHitInfo getStrongCaret (TextHitInfo hit1, TextHitInfo hit2, |
public TextHitInfo getStrongCaret (TextHitInfo hit1, TextHitInfo hit2, |
70 |
TextLayout layout) |
TextLayout layout) |
71 |
{ |
{ |
72 |
throw new Error ("not implemented"); |
return layout.peer.getStrongCaret(hit1, hit2); |
73 |
} |
} |
74 |
} |
} |
75 |
|
|
|
private AttributedString attributedString; |
|
|
private FontRenderContext fontRenderContext; |
|
|
|
|
76 |
public TextLayout (AttributedCharacterIterator text, FontRenderContext frc) |
public TextLayout (AttributedCharacterIterator text, FontRenderContext frc) |
77 |
{ |
{ |
78 |
attributedString = new AttributedString (text); |
AttributedString as = new AttributedString (text); |
79 |
fontRenderContext = frc; |
ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ()); |
80 |
|
peer = tk.getClasspathTextLayoutPeer(as, frc); |
81 |
} |
} |
82 |
|
|
83 |
public TextLayout (String string, Font font, FontRenderContext frc) |
public TextLayout (String string, Font font, FontRenderContext frc) |
84 |
{ |
{ |
85 |
attributedString = new AttributedString (string); |
AttributedString as = new AttributedString (string); |
86 |
attributedString.addAttribute (TextAttribute.FONT, font); |
as.addAttribute (TextAttribute.FONT, font); |
87 |
fontRenderContext = frc; |
ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ()); |
88 |
|
peer = tk.getClasspathTextLayoutPeer(as, frc); |
89 |
} |
} |
90 |
|
|
91 |
public TextLayout (String string, Map attributes, FontRenderContext frc) |
public TextLayout (String string, Map attributes, FontRenderContext frc) |
92 |
{ |
{ |
93 |
attributedString = new AttributedString (string, attributes); |
AttributedString as = new AttributedString (string, attributes); |
94 |
fontRenderContext = frc; |
ClasspathToolkit tk = (ClasspathToolkit)(Toolkit.getDefaultToolkit ()); |
95 |
|
peer = tk.getClasspathTextLayoutPeer(as, frc); |
96 |
} |
} |
97 |
|
|
98 |
protected Object clone () |
protected Object clone () |
99 |
{ |
{ |
100 |
try |
try |
101 |
{ |
{ |
102 |
return super.clone (); |
TextLayout tl = (TextLayout) super.clone (); |
103 |
|
tl.peer = (ClasspathTextLayoutPeer) this.peer.clone(); |
104 |
|
return tl; |
105 |
} |
} |
106 |
catch (CloneNotSupportedException e) |
catch (CloneNotSupportedException e) |
107 |
{ |
{ |
111 |
} |
} |
112 |
|
|
113 |
|
|
|
protected class CharacterIteratorProxy |
|
|
implements CharacterIterator |
|
|
{ |
|
|
public CharacterIterator target; |
|
|
public int begin; |
|
|
public int limit; |
|
|
public int index; |
|
|
|
|
|
public CharacterIteratorProxy (CharacterIterator ci) |
|
|
{ |
|
|
target = ci; |
|
|
} |
|
|
|
|
|
public int getBeginIndex () |
|
|
{ |
|
|
return begin; |
|
|
} |
|
|
|
|
|
public int getEndIndex () |
|
|
{ |
|
|
return limit; |
|
|
} |
|
|
|
|
|
public int getIndex () |
|
|
{ |
|
|
return index; |
|
|
} |
|
|
|
|
|
public char setIndex (int idx) |
|
|
throws IllegalArgumentException |
|
|
{ |
|
|
if (idx < begin || idx >= limit) |
|
|
throw new IllegalArgumentException (); |
|
|
char ch = target.setIndex (idx); |
|
|
index = idx; |
|
|
return ch; |
|
|
} |
|
|
|
|
|
public char first () |
|
|
{ |
|
|
int save = target.getIndex (); |
|
|
char ch = target.setIndex (begin); |
|
|
target.setIndex (save); |
|
|
return ch; |
|
|
} |
|
|
|
|
|
public char last () |
|
|
{ |
|
|
if (begin == limit) |
|
|
return this.first (); |
|
|
|
|
|
int save = target.getIndex (); |
|
|
char ch = target.setIndex (limit - 1); |
|
|
target.setIndex (save); |
|
|
return ch; |
|
|
} |
|
|
|
|
|
public char current () |
|
|
{ |
|
|
return target.current(); |
|
|
} |
|
|
|
|
|
public char next () |
|
|
{ |
|
|
if (index >= limit - 1) |
|
|
return CharacterIterator.DONE; |
|
|
else |
|
|
{ |
|
|
index++; |
|
|
return target.next(); |
|
|
} |
|
|
} |
|
|
|
|
|
public char previous () |
|
|
{ |
|
|
if (index <= begin) |
|
|
return CharacterIterator.DONE; |
|
|
else |
|
|
{ |
|
|
index--; |
|
|
return target.previous (); |
|
|
} |
|
|
} |
|
|
|
|
|
public Object clone () |
|
|
{ |
|
|
CharacterIteratorProxy cip = new CharacterIteratorProxy (this.target); |
|
|
cip.begin = this.begin; |
|
|
cip.limit = this.limit; |
|
|
cip.index = this.index; |
|
|
return cip; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
114 |
public void draw (Graphics2D g2, float x, float y) |
public void draw (Graphics2D g2, float x, float y) |
115 |
{ |
{ |
116 |
AttributedCharacterIterator ci = attributedString.getIterator (); |
peer.draw(g2, x, y); |
|
CharacterIteratorProxy proxy = new CharacterIteratorProxy (ci); |
|
|
Font defFont = g2.getFont (); |
|
|
|
|
|
/* Note: this implementation currently only interprets FONT text |
|
|
* attributes. There is a reasonable argument to be made for some |
|
|
* attributes being interpreted out here, where we have control of the |
|
|
* Graphics2D and can construct or derive new fonts, and some |
|
|
* attributes being interpreted by the GlyphVector itself. So far, for |
|
|
* all attributes except FONT we do neither. |
|
|
*/ |
|
|
|
|
|
for (char c = ci.first (); |
|
|
c != CharacterIterator.DONE; |
|
|
c = ci.next ()) |
|
|
{ |
|
|
proxy.begin = ci.getIndex (); |
|
|
proxy.limit = ci.getRunLimit(TextAttribute.FONT); |
|
|
if (proxy.limit <= proxy.begin) |
|
|
continue; |
|
|
|
|
|
proxy.index = proxy.begin; |
|
|
|
|
|
Object fnt = ci.getAttribute(TextAttribute.FONT); |
|
|
GlyphVector gv; |
|
|
if (fnt instanceof Font) |
|
|
gv = ((Font)fnt).createGlyphVector (fontRenderContext, proxy); |
|
|
else |
|
|
gv = defFont.createGlyphVector (fontRenderContext, proxy); |
|
|
|
|
|
g2.drawGlyphVector (gv, x, y); |
|
|
|
|
|
int n = gv.getNumGlyphs (); |
|
|
for (int i = 0; i < n; ++i) |
|
|
{ |
|
|
GlyphMetrics gm = gv.getGlyphMetrics (i); |
|
|
if (gm.getAdvanceX() == gm.getAdvance ()) |
|
|
x += gm.getAdvanceX (); |
|
|
else |
|
|
y += gm.getAdvanceY (); |
|
|
} |
|
|
} |
|
117 |
} |
} |
118 |
|
|
119 |
public boolean equals (Object obj) |
public boolean equals (Object obj) |
126 |
|
|
127 |
public boolean equals (TextLayout tl) |
public boolean equals (TextLayout tl) |
128 |
{ |
{ |
129 |
throw new Error ("not implemented"); |
return this.peer.equals(tl.peer); |
130 |
} |
} |
131 |
|
|
132 |
public float getAdvance () |
public float getAdvance () |
133 |
{ |
{ |
134 |
throw new Error ("not implemented"); |
return peer.getAdvance(); |
135 |
} |
} |
136 |
|
|
137 |
public float getAscent () |
public float getAscent () |
138 |
{ |
{ |
139 |
throw new Error ("not implemented"); |
return peer.getAscent(); |
140 |
} |
} |
141 |
|
|
142 |
public byte getBaseline () |
public byte getBaseline () |
143 |
{ |
{ |
144 |
throw new Error ("not implemented"); |
return peer.getBaseline(); |
145 |
} |
} |
146 |
|
|
147 |
public float[] getBaselineOffsets () |
public float[] getBaselineOffsets () |
148 |
{ |
{ |
149 |
throw new Error ("not implemented"); |
return peer.getBaselineOffsets(); |
150 |
} |
} |
151 |
|
|
152 |
public Shape getBlackBoxBounds (int firstEndpoint, int secondEndpoint) |
public Shape getBlackBoxBounds (int firstEndpoint, int secondEndpoint) |
153 |
{ |
{ |
154 |
throw new Error ("not implemented"); |
return peer.getBlackBoxBounds(firstEndpoint, secondEndpoint); |
155 |
} |
} |
156 |
|
|
157 |
public Rectangle2D getBounds() |
public Rectangle2D getBounds() |
158 |
{ |
{ |
159 |
throw new Error ("not implemented"); |
return peer.getBounds(); |
160 |
} |
} |
161 |
|
|
162 |
public float[] getCaretInfo (TextHitInfo hit) |
public float[] getCaretInfo (TextHitInfo hit) |
163 |
{ |
{ |
164 |
throw new Error ("not implemented"); |
return getCaretInfo(hit, getBounds()); |
165 |
} |
} |
166 |
|
|
167 |
public float[] getCaretInfo (TextHitInfo hit, Rectangle2D bounds) |
public float[] getCaretInfo (TextHitInfo hit, Rectangle2D bounds) |
168 |
{ |
{ |
169 |
throw new Error ("not implemented"); |
return peer.getCaretInfo(hit, bounds); |
170 |
} |
} |
171 |
|
|
172 |
public Shape getCaretShape (TextHitInfo hit) |
public Shape getCaretShape (TextHitInfo hit) |
173 |
{ |
{ |
174 |
throw new Error ("not implemented"); |
return getCaretShape(hit, getBounds()); |
175 |
} |
} |
176 |
|
|
177 |
public Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds) |
public Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds) |
178 |
{ |
{ |
179 |
throw new Error ("not implemented"); |
return peer.getCaretShape(hit, bounds); |
180 |
} |
} |
181 |
|
|
182 |
public Shape[] getCaretShapes (int offset) |
public Shape[] getCaretShapes (int offset) |
183 |
{ |
{ |
184 |
throw new Error ("not implemented"); |
return getCaretShapes(offset, getBounds()); |
185 |
} |
} |
186 |
|
|
187 |
public Shape[] getCaretShapes (int offset, Rectangle2D bounds) |
public Shape[] getCaretShapes (int offset, Rectangle2D bounds) |
188 |
{ |
{ |
189 |
throw new Error ("not implemented"); |
return getCaretShapes(offset, getBounds(), DEFAULT_CARET_POLICY); |
190 |
} |
} |
191 |
|
|
192 |
public Shape[] getCaretShapes (int offset, Rectangle2D bounds, |
public Shape[] getCaretShapes (int offset, Rectangle2D bounds, |
193 |
TextLayout.CaretPolicy policy) |
TextLayout.CaretPolicy policy) |
194 |
{ |
{ |
195 |
throw new Error ("not implemented"); |
return peer.getCaretShapes(offset, bounds, policy); |
196 |
} |
} |
197 |
|
|
198 |
public int getCharacterCount () |
public int getCharacterCount () |
199 |
{ |
{ |
200 |
throw new Error ("not implemented"); |
return peer.getCharacterCount(); |
201 |
} |
} |
202 |
|
|
203 |
public byte getCharacterLevel (int index) |
public byte getCharacterLevel (int index) |
204 |
{ |
{ |
205 |
throw new Error ("not implemented"); |
return peer.getCharacterLevel(index); |
206 |
} |
} |
207 |
|
|
208 |
public float getDescent () |
public float getDescent () |
209 |
{ |
{ |
210 |
throw new Error ("not implemented"); |
return peer.getDescent(); |
211 |
} |
} |
212 |
|
|
213 |
public TextLayout getJustifiedLayout (float justificationWidth) |
public TextLayout getJustifiedLayout (float justificationWidth) |
214 |
{ |
{ |
215 |
throw new Error ("not implemented"); |
return peer.getJustifiedLayout(justificationWidth); |
216 |
} |
} |
217 |
|
|
218 |
public float getLeading () |
public float getLeading () |
219 |
{ |
{ |
220 |
throw new Error ("not implemented"); |
return peer.getLeading(); |
221 |
} |
} |
222 |
|
|
223 |
public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint) |
public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint) |
224 |
{ |
{ |
225 |
throw new Error ("not implemented"); |
return getLogicalHighlightShape (firstEndpoint, secondEndpoint, getBounds()); |
226 |
} |
} |
227 |
|
|
228 |
public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint, |
public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint, |
229 |
Rectangle2D bounds) |
Rectangle2D bounds) |
230 |
{ |
{ |
231 |
throw new Error ("not implemented"); |
return peer.getLogicalHighlightShape(firstEndpoint, secondEndpoint, bounds); |
232 |
} |
} |
233 |
|
|
234 |
public int[] getLogicalRangesForVisualSelection (TextHitInfo firstEndpoint, |
public int[] getLogicalRangesForVisualSelection (TextHitInfo firstEndpoint, |
235 |
TextHitInfo secondEndpoint) |
TextHitInfo secondEndpoint) |
236 |
{ |
{ |
237 |
throw new Error ("not implemented"); |
return peer.getLogicalRangesForVisualSelection(firstEndpoint, secondEndpoint); |
238 |
} |
} |
239 |
|
|
240 |
public TextHitInfo getNextLeftHit (int offset) |
public TextHitInfo getNextLeftHit (int offset) |
241 |
{ |
{ |
242 |
throw new Error ("not implemented"); |
return getNextLeftHit(offset, DEFAULT_CARET_POLICY); |
243 |
} |
} |
244 |
|
|
245 |
public TextHitInfo getNextLeftHit (int offset, TextLayout.CaretPolicy policy) |
public TextHitInfo getNextLeftHit (int offset, TextLayout.CaretPolicy policy) |
246 |
{ |
{ |
247 |
throw new Error ("not implemented"); |
return peer.getNextLeftHit(offset, policy); |
248 |
} |
} |
249 |
|
|
250 |
public TextHitInfo getNextLeftHit (TextHitInfo hit) |
public TextHitInfo getNextLeftHit (TextHitInfo hit) |
251 |
{ |
{ |
252 |
throw new Error ("not implemented"); |
return getNextLeftHit(hit.getCharIndex()); |
253 |
} |
} |
254 |
|
|
255 |
public TextHitInfo getNextRightHit (int offset) |
public TextHitInfo getNextRightHit (int offset) |
256 |
{ |
{ |
257 |
throw new Error ("not implemented"); |
return getNextRightHit(offset, DEFAULT_CARET_POLICY); |
258 |
} |
} |
259 |
|
|
260 |
public TextHitInfo getNextRightHit (int offset, TextLayout.CaretPolicy policy) |
public TextHitInfo getNextRightHit (int offset, TextLayout.CaretPolicy policy) |
261 |
{ |
{ |
262 |
throw new Error ("not implemented"); |
return peer.getNextRightHit(offset, policy); |
263 |
} |
} |
264 |
|
|
265 |
public TextHitInfo getNextRightHit (TextHitInfo hit) |
public TextHitInfo getNextRightHit (TextHitInfo hit) |
266 |
{ |
{ |
267 |
throw new Error ("not implemented"); |
return getNextRightHit(hit.getCharIndex()); |
268 |
} |
} |
269 |
|
|
270 |
public Shape getOutline (AffineTransform tx) |
public Shape getOutline (AffineTransform tx) |
271 |
{ |
{ |
272 |
throw new Error ("not implemented"); |
return peer.getOutline(tx); |
273 |
} |
} |
274 |
|
|
275 |
public float getVisibleAdvance () |
public float getVisibleAdvance () |
276 |
{ |
{ |
277 |
throw new Error ("not implemented"); |
return peer.getVisibleAdvance(); |
278 |
} |
} |
279 |
|
|
280 |
public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, |
public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, |
281 |
TextHitInfo secondEndpoint) |
TextHitInfo secondEndpoint) |
282 |
{ |
{ |
283 |
throw new Error ("not implemented"); |
return getVisualHighlightShape(firstEndpoint, secondEndpoint, getBounds()); |
284 |
} |
} |
285 |
|
|
286 |
public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, |
public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, |
287 |
TextHitInfo secondEndpoint, |
TextHitInfo secondEndpoint, |
288 |
Rectangle2D bounds) |
Rectangle2D bounds) |
289 |
{ |
{ |
290 |
throw new Error ("not implemented"); |
return peer.getVisualHighlightShape(firstEndpoint, secondEndpoint, bounds); |
291 |
} |
} |
292 |
|
|
293 |
public TextHitInfo getVisualOtherHit (TextHitInfo hit) |
public TextHitInfo getVisualOtherHit (TextHitInfo hit) |
294 |
{ |
{ |
295 |
throw new Error ("not implemented"); |
return peer.getVisualOtherHit(hit); |
296 |
} |
} |
297 |
|
|
298 |
protected void handleJustify (float justificationWidth) |
protected void handleJustify (float justificationWidth) |
299 |
{ |
{ |
300 |
throw new Error ("not implemented"); |
peer.handleJustify(justificationWidth); |
301 |
} |
} |
302 |
|
|
303 |
public int hashCode () |
public int hashCode () |
304 |
{ |
{ |
305 |
throw new Error ("not implemented"); |
return peer.hashCode(); |
306 |
} |
} |
307 |
|
|
308 |
public TextHitInfo hitTestChar (float x, float y) |
public TextHitInfo hitTestChar (float x, float y) |
309 |
{ |
{ |
310 |
throw new Error ("not implemented"); |
return hitTestChar(x, y, getBounds()); |
311 |
} |
} |
312 |
|
|
313 |
public TextHitInfo hitTestChar (float x, float y, Rectangle2D bounds) |
public TextHitInfo hitTestChar (float x, float y, Rectangle2D bounds) |
314 |
{ |
{ |
315 |
throw new Error ("not implemented"); |
return peer.hitTestChar(x, y, bounds); |
316 |
} |
} |
317 |
|
|
318 |
public boolean isLeftToRight () |
public boolean isLeftToRight () |
319 |
{ |
{ |
320 |
throw new Error ("not implemented"); |
return peer.isLeftToRight(); |
321 |
} |
} |
322 |
|
|
323 |
public boolean isVertical () |
public boolean isVertical () |
324 |
{ |
{ |
325 |
throw new Error ("not implemented"); |
return peer.isVertical(); |
326 |
} |
} |
327 |
|
|
328 |
public String toString () |
public String toString () |
329 |
{ |
{ |
330 |
throw new Error ("not implemented"); |
return peer.toString(); |
331 |
} |
} |
332 |
} |
} |