< prev index next >

src/java.desktop/share/classes/javax/swing/text/GlyphPainter1.java

Print this page




  42  * Since the JDK is backward compatible with
  43  * JDK1.1 API, this class will also function on
  44  * Java 2.  The JDK introduces improved
  45  * API for rendering text however, so the GlyphPainter2
  46  * is recommended for the DK.
  47  *
  48  * @author  Timothy Prinzing
  49  * @see GlyphView
  50  */
  51 class GlyphPainter1 extends GlyphView.GlyphPainter {
  52 
  53     /**
  54      * Determine the span the glyphs given a start location
  55      * (for tab expansion).
  56      */
  57     public float getSpan(GlyphView v, int p0, int p1,
  58                          TabExpander e, float x) {
  59         sync(v);
  60         Segment text = v.getText(p0, p1);
  61         int[] justificationData = getJustificationData(v);
  62         int width = Utilities.getTabbedTextWidth(v, text, metrics, (int) x, e, p0,

  63                                                  justificationData);
  64         SegmentCache.releaseSharedSegment(text);
  65         return width;
  66     }
  67 
  68     public float getHeight(GlyphView v) {
  69         sync(v);
  70         return metrics.getHeight();
  71     }
  72 
  73     /**
  74      * Fetches the ascent above the baseline for the glyphs
  75      * corresponding to the given range in the model.
  76      */
  77     public float getAscent(GlyphView v) {
  78         sync(v);
  79         return metrics.getAscent();
  80     }
  81 
  82     /**


 205      *  where a potential break is desired >= 0
 206      * @return the model location desired for a break
 207      * @see View#breakView
 208      */
 209     public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
 210         sync(v);
 211         TabExpander expander = v.getTabExpander();
 212         Segment s = v.getText(p0, v.getEndOffset());
 213         int[] justificationData = getJustificationData(v);
 214         int index = Utilities.getTabbedTextOffset(v, s, metrics, x, (x+len),
 215                                                   expander, p0, false,
 216                                                   justificationData, true);
 217         SegmentCache.releaseSharedSegment(s);
 218         int p1 = p0 + index;
 219         return p1;
 220     }
 221 
 222     @SuppressWarnings("deprecation")
 223     void sync(GlyphView v) {
 224         Font f = v.getFont();
 225         if ((metrics == null) || (! f.equals(metrics.getFont()))) {
 226             // fetch a new FontMetrics
 227             Container c = v.getContainer();
 228             metrics = (c != null) ? c.getFontMetrics(f) :






 229                 Toolkit.getDefaultToolkit().getFontMetrics(f);
 230         }
 231     }
 232 
 233 
 234 
 235     /**
 236      * @return justificationData from the ParagraphRow this GlyphView
 237      * is in or {@code null} if no justification is needed
 238      */
 239     private int[] getJustificationData(GlyphView v) {
 240         View parent = v.getParent();
 241         int [] ret = null;
 242         if (parent instanceof ParagraphView.Row) {
 243             ParagraphView.Row row = ((ParagraphView.Row) parent);
 244             ret = row.justificationData;
 245         }
 246         return ret;
 247     }
 248 


  42  * Since the JDK is backward compatible with
  43  * JDK1.1 API, this class will also function on
  44  * Java 2.  The JDK introduces improved
  45  * API for rendering text however, so the GlyphPainter2
  46  * is recommended for the DK.
  47  *
  48  * @author  Timothy Prinzing
  49  * @see GlyphView
  50  */
  51 class GlyphPainter1 extends GlyphView.GlyphPainter {
  52 
  53     /**
  54      * Determine the span the glyphs given a start location
  55      * (for tab expansion).
  56      */
  57     public float getSpan(GlyphView v, int p0, int p1,
  58                          TabExpander e, float x) {
  59         sync(v);
  60         Segment text = v.getText(p0, p1);
  61         int[] justificationData = getJustificationData(v);       
  62                              
  63         float width = Utilities.getTabbedTextWidth(v, text, metrics, x, e, p0,
  64                                                  justificationData);
  65         SegmentCache.releaseSharedSegment(text);
  66         return width;
  67     }
  68 
  69     public float getHeight(GlyphView v) {
  70         sync(v);
  71         return metrics.getHeight();
  72     }
  73 
  74     /**
  75      * Fetches the ascent above the baseline for the glyphs
  76      * corresponding to the given range in the model.
  77      */
  78     public float getAscent(GlyphView v) {
  79         sync(v);
  80         return metrics.getAscent();
  81     }
  82 
  83     /**


 206      *  where a potential break is desired >= 0
 207      * @return the model location desired for a break
 208      * @see View#breakView
 209      */
 210     public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
 211         sync(v);
 212         TabExpander expander = v.getTabExpander();
 213         Segment s = v.getText(p0, v.getEndOffset());
 214         int[] justificationData = getJustificationData(v);
 215         int index = Utilities.getTabbedTextOffset(v, s, metrics, x, (x+len),
 216                                                   expander, p0, false,
 217                                                   justificationData, true);
 218         SegmentCache.releaseSharedSegment(s);
 219         int p1 = p0 + index;
 220         return p1;
 221     }
 222 
 223     @SuppressWarnings("deprecation")
 224     void sync(GlyphView v) {
 225         Font f = v.getFont();
 226         FontMetrics fm = null;

 227         Container c = v.getContainer();
 228         if (c != null) {
 229             fm = c.getFontMetrics(f);
 230         }
 231         if ((metrics == null) || (! f.equals(metrics.getFont()))
 232                 || (! metrics.equals(fm))) {
 233             // fetch a new FontMetrics
 234             metrics = (c != null) ? fm :
 235                 Toolkit.getDefaultToolkit().getFontMetrics(f);
 236         }
 237     }
 238 
 239 
 240 
 241     /**
 242      * @return justificationData from the ParagraphRow this GlyphView
 243      * is in or {@code null} if no justification is needed
 244      */
 245     private int[] getJustificationData(GlyphView v) {
 246         View parent = v.getParent();
 247         int [] ret = null;
 248         if (parent instanceof ParagraphView.Row) {
 249             ParagraphView.Row row = ((ParagraphView.Row) parent);
 250             ret = row.justificationData;
 251         }
 252         return ret;
 253     }
 254 
< prev index next >