1 /* 2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text; 26 27 import java.awt.*; 28 29 /** 30 * A class to perform rendering of the glyphs. 31 * This can be implemented to be stateless, or 32 * to hold some information as a cache to 33 * facilitate faster rendering and model/view 34 * translation. At a minimum, the GlyphPainter 35 * allows a View implementation to perform its 36 * duties independent of a particular version 37 * of JVM and selection of capabilities (i.e. 38 * shaping for i18n, etc). 39 * <p> 40 * This implementation is intended for operation 41 * under the JDK1.1 API of the Java Platform. 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 Component comp = v.getContainer(); 64 FontMetrics metrics1 = comp.getFontMetrics(comp.getFont()); 65 66 float width = Utilities.getTabbedTextWidth(v, text, metrics1, x, e, p0, 67 justificationData); 68 SegmentCache.releaseSharedSegment(text); 69 return width; 70 } 71 72 public float getHeight(GlyphView v) { 73 sync(v); 74 return metrics.getHeight(); 75 } 76 77 /** 78 * Fetches the ascent above the baseline for the glyphs 79 * corresponding to the given range in the model. 80 */ 81 public float getAscent(GlyphView v) { 82 sync(v); 83 return metrics.getAscent(); 84 } 85 86 /** 87 * Fetches the descent below the baseline for the glyphs 88 * corresponding to the given range in the model. 89 */ 90 public float getDescent(GlyphView v) { 91 sync(v); 92 return metrics.getDescent(); 93 } 94 95 /** 96 * Paints the glyphs representing the given range. 97 */ 98 public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) { 99 sync(v); 100 Segment text; 101 TabExpander expander = v.getTabExpander(); 102 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 103 104 // determine the x coordinate to render the glyphs 105 float x = alloc.x; 106 int p = v.getStartOffset(); 107 int[] justificationData = getJustificationData(v); 108 if (p != p0) { 109 text = v.getText(p, p0); 110 float width = Utilities.getTabbedTextWidth(v, text, metrics, x, 111 expander, p, 112 justificationData); 113 x += width; 114 SegmentCache.releaseSharedSegment(text); 115 } 116 117 // determine the y coordinate to render the glyphs 118 float y = alloc.y + metrics.getHeight() - metrics.getDescent(); 119 120 // render the glyphs 121 text = v.getText(p0, p1); 122 g.setFont(metrics.getFont()); 123 124 Utilities.drawTabbedText(v, text, x, y, g, expander,p0, 125 justificationData, true); 126 SegmentCache.releaseSharedSegment(text); 127 } 128 129 public Shape modelToView(GlyphView v, int pos, Position.Bias bias, 130 Shape a) throws BadLocationException { 131 132 sync(v); 133 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 134 int p0 = v.getStartOffset(); 135 int p1 = v.getEndOffset(); 136 TabExpander expander = v.getTabExpander(); 137 Segment text; 138 139 if(pos == p1) { 140 // The caller of this is left to right and borders a right to 141 // left view, return our end location. 142 return new Rectangle(alloc.x + alloc.width, alloc.y, 0, 143 metrics.getHeight()); 144 } 145 if ((pos >= p0) && (pos <= p1)) { 146 // determine range to the left of the position 147 text = v.getText(p0, pos); 148 int[] justificationData = getJustificationData(v); 149 int width = Utilities.getTabbedTextWidth(v, text, metrics, alloc.x, expander, p0, 150 justificationData); 151 SegmentCache.releaseSharedSegment(text); 152 return new Rectangle(alloc.x + width, alloc.y, 0, metrics.getHeight()); 153 } 154 throw new BadLocationException("modelToView - can't convert", p1); 155 } 156 157 /** 158 * Provides a mapping from the view coordinate space to the logical 159 * coordinate space of the model. 160 * 161 * @param v the view containing the view coordinates 162 * @param x the X coordinate 163 * @param y the Y coordinate 164 * @param a the allocated region to render into 165 * @param biasReturn always returns <code>Position.Bias.Forward</code> 166 * as the zero-th element of this array 167 * @return the location within the model that best represents the 168 * given point in the view 169 * @see View#viewToModel 170 */ 171 public int viewToModel(GlyphView v, float x, float y, Shape a, 172 Position.Bias[] biasReturn) { 173 174 sync(v); 175 Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); 176 int p0 = v.getStartOffset(); 177 int p1 = v.getEndOffset(); 178 TabExpander expander = v.getTabExpander(); 179 Segment text = v.getText(p0, p1); 180 int[] justificationData = getJustificationData(v); 181 int offs = Utilities.getTabbedTextOffset(v, text, metrics, 182 alloc.x, (int) x, expander, p0, 183 justificationData); 184 SegmentCache.releaseSharedSegment(text); 185 int retValue = p0 + offs; 186 if(retValue == p1) { 187 // No need to return backward bias as GlyphPainter1 is used for 188 // ltr text only. 189 retValue--; 190 } 191 biasReturn[0] = Position.Bias.Forward; 192 return retValue; 193 } 194 195 /** 196 * Determines the best location (in the model) to break 197 * the given view. 198 * This method attempts to break on a whitespace 199 * location. If a whitespace location can't be found, the 200 * nearest character location is returned. 201 * 202 * @param v the view 203 * @param p0 the location in the model where the 204 * fragment should start its representation >= 0 205 * @param x the graphic location along the axis that the 206 * broken view would occupy >= 0; this may be useful for 207 * things like tab calculations 208 * @param len specifies the distance into the view 209 * where a potential break is desired >= 0 210 * @return the model location desired for a break 211 * @see View#breakView 212 */ 213 public int getBoundedPosition(GlyphView v, int p0, float x, float len) { 214 sync(v); 215 TabExpander expander = v.getTabExpander(); 216 Segment s = v.getText(p0, v.getEndOffset()); 217 int[] justificationData = getJustificationData(v); 218 int index = Utilities.getTabbedTextOffset(v, s, metrics, x, (x+len), 219 expander, p0, false, 220 justificationData, true); 221 SegmentCache.releaseSharedSegment(s); 222 int p1 = p0 + index; 223 return p1; 224 } 225 226 @SuppressWarnings("deprecation") 227 void sync(GlyphView v) { 228 Font f = v.getFont(); 229 if ((metrics == null) || (! f.equals(metrics.getFont()))) { 230 // fetch a new FontMetrics 231 Container c = v.getContainer(); 232 metrics = (c != null) ? c.getFontMetrics(f) : 233 Toolkit.getDefaultToolkit().getFontMetrics(f); 234 } 235 } 236 237 238 239 /** 240 * @return justificationData from the ParagraphRow this GlyphView 241 * is in or {@code null} if no justification is needed 242 */ 243 private int[] getJustificationData(GlyphView v) { 244 View parent = v.getParent(); 245 int [] ret = null; 246 if (parent instanceof ParagraphView.Row) { 247 ParagraphView.Row row = ((ParagraphView.Row) parent); 248 ret = row.justificationData; 249 } 250 return ret; 251 } 252 253 // --- variables --------------------------------------------- 254 255 FontMetrics metrics; 256 }