PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/textarea/TextRenderer.java

#
Java | 264 lines | 181 code | 47 blank | 36 comment | 23 complexity | eec30bee2a46b00ec859fd7b5dcd1a16 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * TextRenderer.java - Draws text
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.textarea;
  23. //{{{ Imports
  24. import javax.swing.text.TabExpander;
  25. import java.awt.font.*;
  26. import java.awt.geom.*;
  27. import java.awt.*;
  28. import java.util.Hashtable;
  29. import org.gjt.sp.util.Log;
  30. //}}}
  31. public class TextRenderer
  32. {
  33. //{{{ setupGraphics() method
  34. public void setupGraphics(Graphics g)
  35. {
  36. ((Graphics2D)g).setRenderingHints(renderingHints);
  37. } //}}}
  38. //{{{ configure() method
  39. public void configure(boolean antiAlias, boolean fracFontMetrics)
  40. {
  41. Hashtable hints = new Hashtable();
  42. if(antiAlias)
  43. {
  44. hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  45. hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  46. hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  47. hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
  48. fracFontMetrics ?
  49. RenderingHints.VALUE_FRACTIONALMETRICS_ON
  50. : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
  51. }
  52. else
  53. hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  54. renderingHints = new RenderingHints(hints);
  55. fontRenderContext = new FontRenderContext(null,antiAlias,
  56. fracFontMetrics);
  57. } //}}}
  58. //{{{ drawChars() method
  59. public float drawChars(char[] text, int off, int len, Graphics _g,
  60. float x, float y, TabExpander e, Color foreground,
  61. Color tokenBackground, Color componentBackground)
  62. {
  63. Graphics2D g = (Graphics2D)_g;
  64. //{{{ this probably should be moved elsewhere
  65. if(tokenBackground != null)
  66. {
  67. float width = charsWidth(text,off,len,g.getFont(),x,e);
  68. FontMetrics fm = g.getFontMetrics();
  69. float height = fm.getHeight();
  70. float descent = fm.getDescent();
  71. float leading = fm.getLeading();
  72. g.setXORMode(componentBackground);
  73. g.setColor(tokenBackground);
  74. g.fillRect((int)x,(int)(y - height + descent + leading),
  75. (int)width,(int)height);
  76. g.setPaintMode();
  77. } //}}}
  78. g.setColor(foreground);
  79. int flushLen = 0;
  80. int flushIndex = off;
  81. int end = off + len;
  82. for(int i = off; i < end; i++)
  83. {
  84. if(text[i] == '\t')
  85. {
  86. if(flushLen > 0)
  87. {
  88. x += _drawChars(text,flushIndex,
  89. flushLen,g,x,y);
  90. flushLen = 0;
  91. }
  92. flushIndex = i + 1;
  93. x = e.nextTabStop(x,i - off);
  94. }
  95. else
  96. flushLen++;
  97. }
  98. if(flushLen > 0)
  99. x += _drawChars(text,flushIndex,flushLen,g,x,y);
  100. return x;
  101. } //}}}
  102. //{{{ charsWidth() method
  103. public float charsWidth(char[] text, int off, int len, Font font, float x,
  104. TabExpander e)
  105. {
  106. float newX = x;
  107. int flushLen = 0;
  108. int flushIndex = off;
  109. int end = off + len;
  110. for(int i = off; i < end; i++)
  111. {
  112. if(text[i] == '\t')
  113. {
  114. if(flushLen > 0)
  115. {
  116. newX += _getWidth(text,flushIndex,flushLen,font);
  117. flushLen = 0;
  118. }
  119. flushIndex = i + 1;
  120. newX = e.nextTabStop(newX,i - off);
  121. }
  122. else
  123. flushLen++;
  124. }
  125. if(flushLen > 0)
  126. newX += _getWidth(text,flushIndex,flushLen,font);
  127. return newX - x;
  128. } //}}}
  129. //{{{ xToOffset() method
  130. public int xToOffset(char[] text, int off, int len, Font font, float x,
  131. TabExpander e, boolean round, float[] widthArray)
  132. {
  133. int flushLen = 0;
  134. int flushIndex = off;
  135. int end = off + len;
  136. float width = widthArray[0];
  137. for(int i = off; i < end; i++)
  138. {
  139. if(text[i] == '\t')
  140. {
  141. if(flushLen > 0)
  142. {
  143. float newWidth = _getWidth(text,flushIndex,
  144. flushLen,font);
  145. if(x <= width + newWidth)
  146. {
  147. return _xToOffset(text,flushIndex,
  148. flushLen,font,x - width,
  149. round) + flushIndex;
  150. }
  151. else
  152. width += newWidth;
  153. flushLen = 0;
  154. }
  155. flushIndex = i + 1;
  156. float newWidth = e.nextTabStop(width,i - off) - width;
  157. if(x <= width + newWidth)
  158. {
  159. if(round && (x - width) < (width + newWidth - x))
  160. return i;
  161. else
  162. return i + 1;
  163. }
  164. else
  165. width += newWidth;
  166. }
  167. else
  168. flushLen++;
  169. }
  170. if(flushLen > 0)
  171. {
  172. float newWidth = _getWidth(text,flushIndex,flushLen,font);
  173. if(x <= width + newWidth)
  174. {
  175. return _xToOffset(text,flushIndex,flushLen,font,
  176. x - width,round) + flushIndex;
  177. }
  178. else
  179. width += newWidth;
  180. }
  181. widthArray[0] = width;
  182. return -1;
  183. } //}}}
  184. //{{{ Private members
  185. private RenderingHints renderingHints;
  186. private FontRenderContext fontRenderContext;
  187. //{{{ _drawChars() method
  188. private float _drawChars(char[] text, int start, int len, Graphics2D g,
  189. float x, float y)
  190. {
  191. Font font = g.getFont();
  192. // update it just in case
  193. fontRenderContext = g.getFontRenderContext();
  194. GlyphVector glyphs = font.createGlyphVector(fontRenderContext,
  195. new String(text,start,len));
  196. g.drawGlyphVector(glyphs,x,y);
  197. return (float)glyphs.getLogicalBounds().getWidth();
  198. } //}}}
  199. //{{{ _getWidth() method
  200. private float _getWidth(char[] text, int start, int len, Font font)
  201. {
  202. GlyphVector glyphs = font.createGlyphVector(fontRenderContext,
  203. new String(text,start,len));
  204. return (float)glyphs.getLogicalBounds().getWidth();
  205. } //}}}
  206. //{{{ _xToOffset() method
  207. private int _xToOffset(char[] text, int start, int len, Font font,
  208. float x, boolean round)
  209. {
  210. // this is slow!
  211. TextLayout layout = new TextLayout(new String(text,start,len),font,
  212. fontRenderContext);
  213. TextHitInfo info = layout.hitTestChar(x,0);
  214. return (round ? info.getInsertionIndex() : info.getCharIndex());
  215. } //}}}
  216. //}}}
  217. }