1 7 package javax.swing.text; 8 9 import java.awt.*; 10 11 34 class GlyphPainter1 extends GlyphView.GlyphPainter { 35 36 40 public float getSpan(GlyphView v, int p0, int p1, 41 TabExpander e, float x) { 42 sync(v); 43 Segment text = v.getText(p0, p1); 44 int[] justificationData = getJustificationData(v); 45 int width = Utilities.getTabbedTextWidth(v, text, metrics, (int) x, e, p0, 46 justificationData); 47 SegmentCache.releaseSharedSegment(text); 48 return width; 49 } 50 51 public float getHeight(GlyphView v) { 52 sync(v); 53 return metrics.getHeight(); 54 } 55 56 60 public float getAscent(GlyphView v) { 61 sync(v); 62 return metrics.getAscent(); 63 } 64 65 69 public float getDescent(GlyphView v) { 70 sync(v); 71 return metrics.getDescent(); 72 } 73 74 77 public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) { 78 sync(v); 79 Segment text; 80 TabExpander expander = v.getTabExpander(); 81 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 82 83 int x = alloc.x; 85 int p = v.getStartOffset(); 86 int[] justificationData = getJustificationData(v); 87 if (p != p0) { 88 text = v.getText(p, p0); 89 int width = Utilities.getTabbedTextWidth(v, text, metrics, x, expander, p, 90 justificationData); 91 x += width; 92 SegmentCache.releaseSharedSegment(text); 93 } 94 95 int y = alloc.y + metrics.getHeight() - metrics.getDescent(); 97 98 text = v.getText(p0, p1); 100 g.setFont(metrics.getFont()); 101 102 Utilities.drawTabbedText(v, text, x, y, g, expander,p0, 103 justificationData); 104 SegmentCache.releaseSharedSegment(text); 105 } 106 107 public Shape modelToView(GlyphView v, int pos, Position.Bias bias, 108 Shape a) throws BadLocationException { 109 110 sync(v); 111 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 112 int p0 = v.getStartOffset(); 113 int p1 = v.getEndOffset(); 114 TabExpander expander = v.getTabExpander(); 115 Segment text; 116 117 if(pos == p1) { 118 return new Rectangle(alloc.x + alloc.width, alloc.y, 0, 121 metrics.getHeight()); 122 } 123 if ((pos >= p0) && (pos <= p1)) { 124 text = v.getText(p0, pos); 126 int[] justificationData = getJustificationData(v); 127 int width = Utilities.getTabbedTextWidth(v, text, metrics, alloc.x, expander, p0, 128 justificationData); 129 SegmentCache.releaseSharedSegment(text); 130 return new Rectangle(alloc.x + width, alloc.y, 0, metrics.getHeight()); 131 } 132 throw new BadLocationException ("modelToView - can't convert", p1); 133 } 134 135 149 public int viewToModel(GlyphView v, float x, float y, Shape a, 150 Position.Bias [] biasReturn) { 151 152 sync(v); 153 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 154 int p0 = v.getStartOffset(); 155 int p1 = v.getEndOffset(); 156 TabExpander expander = v.getTabExpander(); 157 Segment text = v.getText(p0, p1); 158 int[] justificationData = getJustificationData(v); 159 int offs = Utilities.getTabbedTextOffset(v, text, metrics, 160 alloc.x, (int) x, expander, p0, 161 justificationData); 162 SegmentCache.releaseSharedSegment(text); 163 int retValue = p0 + offs; 164 if(retValue == p1) { 165 retValue--; 168 } 169 biasReturn[0] = Position.Bias.Forward; 170 return retValue; 171 } 172 173 191 public int getBoundedPosition(GlyphView v, int p0, float x, float len) { 192 sync(v); 193 TabExpander expander = v.getTabExpander(); 194 Segment s = v.getText(p0, v.getEndOffset()); 195 int[] justificationData = getJustificationData(v); 196 int index = Utilities.getTabbedTextOffset(v, s, metrics, (int)x, (int)(x+len), 197 expander, p0, false, 198 justificationData); 199 SegmentCache.releaseSharedSegment(s); 200 int p1 = p0 + index; 201 return p1; 202 } 203 204 void sync(GlyphView v) { 205 Font f = v.getFont(); 206 if ((metrics == null) || (! f.equals(metrics.getFont()))) { 207 Container c = v.getContainer(); 209 metrics = (c != null) ? c.getFontMetrics(f) : 210 Toolkit.getDefaultToolkit().getFontMetrics(f); 211 } 212 } 213 214 215 216 220 private int[] getJustificationData(GlyphView v) { 221 View parent = v.getParent(); 222 int [] ret = null; 223 if (parent instanceof ParagraphView.Row ) { 224 ParagraphView.Row row = ((ParagraphView.Row ) parent); 225 ret = row.justificationData; 226 } 227 return ret; 228 } 229 230 232 FontMetrics metrics; 233 } 234 | Popular Tags |