1. /*
  2. * @(#)GlyphPainter2.java 1.12 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text;
  11. import java.text.*;
  12. import java.awt.*;
  13. import java.awt.font.*;
  14. import java.awt.geom.Rectangle2D;
  15. /**
  16. * A class to perform rendering of the glyphs.
  17. * This can be implemented to be stateless, or
  18. * to hold some information as a cache to
  19. * facilitate faster rendering and model/view
  20. * translation. At a minimum, the GlyphPainter
  21. * allows a View implementation to perform its
  22. * duties independant of a particular version
  23. * of JVM and selection of capabilities (i.e.
  24. * shaping for i18n, etc).
  25. * <p>
  26. * This implementation is intended for operation
  27. * under the Java 2 SDK. It uses the
  28. * java.awt.font.TextLayout class to do i18n capable
  29. * rendering.
  30. *
  31. * @author Timothy Prinzing
  32. * @version 1.12 02/02/00
  33. * @see GlyphView
  34. */
  35. class GlyphPainter2 extends GlyphView.GlyphPainter {
  36. public GlyphPainter2(TextLayout layout) {
  37. this.layout = layout;
  38. }
  39. /**
  40. * Create a painter to use for the given GlyphView.
  41. */
  42. public GlyphView.GlyphPainter getPainter(GlyphView v, int p0, int p1) {
  43. return null;
  44. }
  45. /**
  46. * Determine the span the glyphs given a start location
  47. * (for tab expansion). This implementation assumes it
  48. * has no tabs (i.e. TextLayout doesn't deal with tab
  49. * expansion).
  50. */
  51. public float getSpan(GlyphView v, int p0, int p1,
  52. TabExpander e, float x) {
  53. if ((p0 == v.getStartOffset()) && (p1 == v.getEndOffset())) {
  54. return layout.getAdvance();
  55. }
  56. int p = v.getStartOffset();
  57. int index0 = p0 - p;
  58. int index1 = p1 - p;
  59. TextHitInfo hit0 = TextHitInfo.afterOffset(index0);
  60. TextHitInfo hit1 = TextHitInfo.beforeOffset(index1);
  61. float[] locs = layout.getCaretInfo(hit0);
  62. float x0 = locs[0];
  63. locs = layout.getCaretInfo(hit1);
  64. float x1 = locs[0];
  65. return (x1 > x0) ? x1 - x0 : x0 - x1;
  66. }
  67. public float getHeight(GlyphView v) {
  68. return layout.getAscent() + layout.getDescent() + layout.getLeading();
  69. }
  70. /**
  71. * Fetch the ascent above the baseline for the glyphs
  72. * corresponding to the given range in the model.
  73. */
  74. public float getAscent(GlyphView v) {
  75. return layout.getAscent();
  76. }
  77. /**
  78. * Fetch the descent below the baseline for the glyphs
  79. * corresponding to the given range in the model.
  80. */
  81. public float getDescent(GlyphView v) {
  82. return layout.getDescent();
  83. }
  84. /**
  85. * Paint the glyphs for the given view. This is implemented
  86. * to only render if the Graphics is of type Graphics2D which
  87. * is required by TextLayout (and this should be the case if
  88. * running on the Java2 SDK).
  89. */
  90. public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) {
  91. if (g instanceof Graphics2D) {
  92. Rectangle2D alloc = a.getBounds2D();
  93. Graphics2D g2d = (Graphics2D)g;
  94. float y = (float) alloc.getY() + layout.getAscent() + layout.getLeading();
  95. float x = (float) alloc.getX();
  96. if( p0 > v.getStartOffset() || p1 < v.getEndOffset() ) {
  97. try {
  98. //TextLayout can't render only part of it's range, so if a
  99. //partial range is required, add a clip region.
  100. Shape s = v.modelToView(p0, Position.Bias.Forward,
  101. p1, Position.Bias.Backward, a);
  102. Shape savedClip = g.getClip();
  103. g.setClip(s);
  104. layout.draw(g2d, x, y);
  105. g.setClip(savedClip);
  106. } catch (BadLocationException e) {}
  107. } else {
  108. layout.draw(g2d, x, y);
  109. }
  110. }
  111. }
  112. public Shape modelToView(GlyphView v, int pos, Position.Bias bias,
  113. Shape a) throws BadLocationException {
  114. int offs = pos - v.getStartOffset();
  115. Rectangle2D alloc = a.getBounds2D();
  116. TextHitInfo hit = (bias == Position.Bias.Forward) ?
  117. TextHitInfo.afterOffset(offs) : TextHitInfo.beforeOffset(offs);
  118. float[] locs = layout.getCaretInfo(hit);
  119. // vertical at the baseline, should use slope and check if glyphs
  120. // are being rendered vertically.
  121. alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight());
  122. return alloc;
  123. }
  124. /**
  125. * Provides a mapping from the view coordinate space to the logical
  126. * coordinate space of the model.
  127. *
  128. * @param x the X coordinate
  129. * @param y the Y coordinate
  130. * @param a the allocated region to render into
  131. * @param rightToLeft true if the text is rendered right to left
  132. * @return the location within the model that best represents the
  133. * given point of view
  134. * @see View#viewToModel
  135. */
  136. public int viewToModel(GlyphView v, float x, float y, Shape a,
  137. Position.Bias[] biasReturn) {
  138. Rectangle2D alloc = (a instanceof Rectangle2D) ? (Rectangle2D)a : a.getBounds2D();
  139. //Move the y co-ord of the hit onto the baseline. This is because TextLayout supports
  140. //italic carets and we do not.
  141. TextHitInfo hit = layout.hitTestChar(x - (float)alloc.getX(), 0);
  142. int pos = hit.getInsertionIndex();
  143. biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
  144. return pos + v.getStartOffset();
  145. }
  146. /**
  147. * Determines the model location that represents the
  148. * maximum advance that fits within the given span.
  149. * This could be used to break the given view. The result
  150. * should be a location just shy of the given advance. This
  151. * differs from viewToModel which returns the closest
  152. * position which might be proud of the maximum advance.
  153. *
  154. * @param v the view to find the model location to break at.
  155. * @param p0 the location in the model where the
  156. * fragment should start it's representation >= 0.
  157. * @param pos the graphic location along the axis that the
  158. * broken view would occupy >= 0. This may be useful for
  159. * things like tab calculations.
  160. * @param len specifies the distance into the view
  161. * where a potential break is desired >= 0.
  162. * @return the maximum model location possible for a break.
  163. * @see View#breakView
  164. */
  165. public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
  166. if( len < 0 )
  167. throw new IllegalArgumentException("Length must be >= 0.");
  168. TextHitInfo hit = layout.hitTestChar(len, 0);
  169. if( (hit.getCharIndex() == -1) && !layout.isLeftToRight() ) {
  170. return v.getEndOffset();
  171. }
  172. int pos = (hit.isLeadingEdge()) ? hit.getInsertionIndex()
  173. : hit.getInsertionIndex() - 1;
  174. return pos + v.getStartOffset();
  175. }
  176. /**
  177. * Provides a way to determine the next visually represented model
  178. * location that one might place a caret. Some views may not be
  179. * visible, they might not be in the same order found in the model, or
  180. * they just might not allow access to some of the locations in the
  181. * model.
  182. *
  183. * @param v the view to use
  184. * @param pos the position to convert >= 0
  185. * @param a the allocated region to render into
  186. * @param direction the direction from the current position that can
  187. * be thought of as the arrow keys typically found on a keyboard.
  188. * This may be SwingConstants.WEST, SwingConstants.EAST,
  189. * SwingConstants.NORTH, or SwingConstants.SOUTH.
  190. * @return the location within the model that best represents the next
  191. * location visual position.
  192. * @exception BadLocationException
  193. * @exception IllegalArgumentException for an invalid direction
  194. */
  195. public int getNextVisualPositionFrom(GlyphView v, int pos,
  196. Position.Bias b, Shape a,
  197. int direction,
  198. Position.Bias[] biasRet)
  199. throws BadLocationException {
  200. int startOffset = v.getStartOffset();
  201. int endOffset = v.getEndOffset();
  202. Segment text;
  203. AbstractDocument doc;
  204. boolean viewIsLeftToRight;
  205. TextHitInfo currentHit, nextHit;
  206. switch (direction) {
  207. case View.NORTH:
  208. break;
  209. case View.SOUTH:
  210. break;
  211. case View.EAST:
  212. doc = (AbstractDocument)v.getDocument();
  213. viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
  214. if(startOffset == doc.getLength()) {
  215. if(pos == -1) {
  216. biasRet[0] = Position.Bias.Forward;
  217. return startOffset;
  218. }
  219. // End case for bidi text where newline is at beginning
  220. // of line.
  221. return -1;
  222. }
  223. if(pos == -1) {
  224. // Entering view from the left.
  225. if( viewIsLeftToRight ) {
  226. biasRet[0] = Position.Bias.Forward;
  227. return startOffset;
  228. } else {
  229. text = v.getText(endOffset - 1, endOffset);
  230. if(text.array[text.offset] == '\n') {
  231. biasRet[0] = Position.Bias.Forward;
  232. return endOffset-1;
  233. }
  234. biasRet[0] = Position.Bias.Backward;
  235. return endOffset;
  236. }
  237. }
  238. if( b==Position.Bias.Forward )
  239. currentHit = TextHitInfo.afterOffset(pos-startOffset);
  240. else
  241. currentHit = TextHitInfo.beforeOffset(pos-startOffset);
  242. nextHit = layout.getNextRightHit(currentHit);
  243. if( nextHit == null ) {
  244. return -1;
  245. }
  246. if( viewIsLeftToRight != layout.isLeftToRight() ) {
  247. // If the layout's base direction is different from
  248. // this view's run direction, we need to use the weak
  249. // carrat.
  250. nextHit = layout.getVisualOtherHit(nextHit);
  251. }
  252. pos = nextHit.getInsertionIndex() + startOffset;
  253. if(pos == endOffset) {
  254. // A move to the right from an internal position will
  255. // only take us to the endOffset in a left to right run.
  256. text = v.getText(endOffset - 1, endOffset);
  257. if(text.array[text.offset] == '\n') {
  258. return -1;
  259. }
  260. biasRet[0] = Position.Bias.Backward;
  261. }
  262. else {
  263. biasRet[0] = Position.Bias.Forward;
  264. }
  265. return pos;
  266. case View.WEST:
  267. doc = (AbstractDocument)v.getDocument();
  268. viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
  269. if(startOffset == doc.getLength()) {
  270. if(pos == -1) {
  271. biasRet[0] = Position.Bias.Forward;
  272. return startOffset;
  273. }
  274. // End case for bidi text where newline is at beginning
  275. // of line.
  276. return -1;
  277. }
  278. if(pos == -1) {
  279. // Entering view from the right
  280. if( viewIsLeftToRight ) {
  281. text = v.getText(endOffset - 1, endOffset);
  282. if(text.array[text.offset] == '\n') {
  283. biasRet[0] = Position.Bias.Forward;
  284. return endOffset - 1;
  285. }
  286. biasRet[0] = Position.Bias.Backward;
  287. return endOffset;
  288. } else {
  289. biasRet[0] = Position.Bias.Forward;
  290. return startOffset;
  291. }
  292. }
  293. if( b==Position.Bias.Forward )
  294. currentHit = TextHitInfo.afterOffset(pos-startOffset);
  295. else
  296. currentHit = TextHitInfo.beforeOffset(pos-startOffset);
  297. nextHit = layout.getNextLeftHit(currentHit);
  298. if( nextHit == null ) {
  299. return -1;
  300. }
  301. if( viewIsLeftToRight != layout.isLeftToRight() ) {
  302. // If the layout's base direction is different from
  303. // this view's run direction, we need to use the weak
  304. // carrat.
  305. nextHit = layout.getVisualOtherHit(nextHit);
  306. }
  307. pos = nextHit.getInsertionIndex() + startOffset;
  308. if(pos == endOffset) {
  309. // A move to the left from an internal position will
  310. // only take us to the endOffset in a right to left run.
  311. text = v.getText(endOffset - 1, endOffset);
  312. if(text.array[text.offset] == '\n') {
  313. return -1;
  314. }
  315. biasRet[0] = Position.Bias.Backward;
  316. }
  317. else {
  318. biasRet[0] = Position.Bias.Forward;
  319. }
  320. return pos;
  321. default:
  322. throw new IllegalArgumentException("Bad direction: " + direction);
  323. }
  324. return pos;
  325. }
  326. // --- variables ---------------------------------------------
  327. TextLayout layout;
  328. }