1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Ilya S. Okomin 19 * @version $Revision$ 20 */ 21 22 package java.awt.font; 23 24 import java.awt.Font; 25 import java.awt.font.FontRenderContext; 26 import java.awt.font.GlyphJustificationInfo; 27 import java.awt.font.GlyphMetrics; 28 29 import java.awt.Rectangle; 30 import java.awt.Shape; 31 import java.awt.geom.AffineTransform; 32 import java.awt.geom.Point2D; 33 import java.awt.geom.Rectangle2D; 34 35 /** 36 * The GlyphVector class contains a collection of glyphs with geometric 37 * information and each glyph's location. Each GlyphVector can be associated 38 * with only one Font. GlyphVector contains the following properties for each 39 * glyph: 40 * <ul> 41 * <li>the glyph position;</li> 42 * <li>the transform of the glyph;</li> 43 * <li>the metrics of the glyph in the context of the GlyphVector.</li> 44 * </ul> 45 * 46 * @since Android 1.0 47 */ 48 public abstract class GlyphVector implements Cloneable { 49 50 /** 51 * The Constant FLAG_HAS_TRANSFORMS indicates that this GlyphVector has 52 * per-glyph transforms. 53 */ 54 public static final int FLAG_HAS_TRANSFORMS = 1; 55 56 /** 57 * The Constant FLAG_HAS_POSITION_ADJUSTMENTS indicates that the GlyphVector 58 * has per-glyph position adjustments. 59 */ 60 public static final int FLAG_HAS_POSITION_ADJUSTMENTS = 2; 61 62 /** 63 * The Constant FLAG_RUN_RTL indicates that this GlyphVector has a right to 64 * left run direction. 65 */ 66 public static final int FLAG_RUN_RTL = 4; 67 68 /** 69 * The Constant FLAG_COMPLEX_GLYPHS indicates that this GlyphVector has a 70 * complex glyph to char mapping. 71 */ 72 public static final int FLAG_COMPLEX_GLYPHS = 8; 73 74 /** 75 * The Constant FLAG_MASK indicates a mask for supported flags from 76 * getLayoutFlags. 77 */ 78 public static final int FLAG_MASK = 15; // (|) mask of other flags 79 80 /** 81 * Instantiates a new GlyphVector. 82 */ GlyphVector()83 public GlyphVector() { 84 } 85 86 /** 87 * Gets the pixel bounds of the GlyphVector when rendered at the specified 88 * location with the specified FontRenderContext. 89 * 90 * @param frc 91 * the FontRenderContext. 92 * @param x 93 * the X coordinate of the GlyphVector's location. 94 * @param y 95 * the Y coordinate of the GlyphVector's location. 96 * @return the pixel bounds 97 */ getPixelBounds(FontRenderContext frc, float x, float y)98 public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) { 99 // default implementation - integer Rectangle, that encloses visual 100 // bounds rectangle 101 Rectangle2D visualRect = getVisualBounds(); 102 103 int minX = (int)Math.floor(visualRect.getMinX() + x); 104 int minY = (int)Math.floor(visualRect.getMinY() + y); 105 int width = (int)Math.ceil(visualRect.getMaxX() + x) - minX; 106 int height = (int)Math.ceil(visualRect.getMaxY() + y) - minY; 107 108 return new Rectangle(minX, minY, width, height); 109 } 110 111 /** 112 * Gets the pixel bounds of the glyph with the specified index in this 113 * GlyphVector which is rendered with the specified FontRenderContext at the 114 * specified location. 115 * 116 * @param index 117 * the glyph index in this GlyphVector. 118 * @param frc 119 * the FontRenderContext. 120 * @param x 121 * the X coordinate of the GlyphVector's location. 122 * @param y 123 * the Y coordinate of the GlyphVector's location. 124 * @return a Rectangle bounds. 125 */ getGlyphPixelBounds(int index, FontRenderContext frc, float x, float y)126 public Rectangle getGlyphPixelBounds(int index, FontRenderContext frc, float x, float y) { 127 Rectangle2D visualRect = getGlyphVisualBounds(index).getBounds2D(); 128 129 int minX = (int)Math.floor(visualRect.getMinX() + x); 130 int minY = (int)Math.floor(visualRect.getMinY() + y); 131 int width = (int)Math.ceil(visualRect.getMaxX() + x) - minX; 132 int height = (int)Math.ceil(visualRect.getMaxY() + y) - minY; 133 134 return new Rectangle(minX, minY, width, height); 135 } 136 137 /** 138 * Gets the visual bounds of the GlyphVector. 139 * 140 * @return the visual bounds of the GlyphVector. 141 */ getVisualBounds()142 public abstract Rectangle2D getVisualBounds(); 143 144 /** 145 * Gets the logical bounds of the GlyphVector. 146 * 147 * @return the logical bounds of the GlyphVector. 148 */ getLogicalBounds()149 public abstract Rectangle2D getLogicalBounds(); 150 151 /** 152 * Sets the position of the specified glyph in this GlyphVector. 153 * 154 * @param glyphIndex 155 * the glyph index in this GlyphVector. 156 * @param newPos 157 * the new position of the glyph at the specified glyphIndex. 158 */ setGlyphPosition(int glyphIndex, Point2D newPos)159 public abstract void setGlyphPosition(int glyphIndex, Point2D newPos); 160 161 /** 162 * Gets the position of the specified glyph in this GlyphVector. 163 * 164 * @param glyphIndex 165 * the glyph index in this GlyphVector. 166 * @return the position of the specified glyph in this GlyphVector. 167 */ getGlyphPosition(int glyphIndex)168 public abstract Point2D getGlyphPosition(int glyphIndex); 169 170 /** 171 * Sets the affine transform to a glyph with the specified index in this 172 * GlyphVector. 173 * 174 * @param glyphIndex 175 * the glyth index in this GlyphVector. 176 * @param trans 177 * the AffineTransform to be assigned to the specified glyph. 178 */ setGlyphTransform(int glyphIndex, AffineTransform trans)179 public abstract void setGlyphTransform(int glyphIndex, AffineTransform trans); 180 181 /** 182 * Gets the transform of the specified glyph in this GlyphVector. 183 * 184 * @param glyphIndex 185 * the glyph index in this GlyphVector. 186 * @return the new transform of the glyph. 187 */ getGlyphTransform(int glyphIndex)188 public abstract AffineTransform getGlyphTransform(int glyphIndex); 189 190 /** 191 * Compares this GlyphVector with the specified GlyphVector objects. 192 * 193 * @param glyphVector 194 * the GlyphVector object to be compared. 195 * @return true, if this GlyphVector is equal to the specified GlyphVector 196 * object, false otherwise. 197 */ equals(GlyphVector glyphVector)198 public abstract boolean equals(GlyphVector glyphVector); 199 200 /** 201 * Gets the metrics of the glyph with the specified index in this 202 * GlyphVector. 203 * 204 * @param glyphIndex 205 * index in this GlyphVector. 206 * @return the metrics of the glyph with the specified index in this 207 * GlyphVector. 208 */ getGlyphMetrics(int glyphIndex)209 public abstract GlyphMetrics getGlyphMetrics(int glyphIndex); 210 211 /** 212 * Gets the justification information of the glyph whose index is specified. 213 * 214 * @param glyphIndex 215 * the glyph index. 216 * @return the GlyphJustificationInfo for the specified glyph. 217 */ getGlyphJustificationInfo(int glyphIndex)218 public abstract GlyphJustificationInfo getGlyphJustificationInfo(int glyphIndex); 219 220 /** 221 * Gets the FontRenderContext of this GlyphVector. 222 * 223 * @return the FontRenderContext of this GlyphVector. 224 */ getFontRenderContext()225 public abstract FontRenderContext getFontRenderContext(); 226 227 /** 228 * Gets a Shape object which defines the visual representation of the 229 * specified glyph in this GlyphVector, translated a distance of x in the X 230 * direction and y in the Y direction. 231 * 232 * @param glyphIndex 233 * the glyth index in this GlyphVector. 234 * @param x 235 * the distance in the X direction to translate the shape object 236 * before returning it. 237 * @param y 238 * the distance in the Y direction to translate the shape object 239 * before returning it. 240 * @return a Shape object which represents the visual representation of the 241 * specified glyph in this GlyphVector - glyph outline. 242 */ getGlyphOutline(int glyphIndex, float x, float y)243 public Shape getGlyphOutline(int glyphIndex, float x, float y) { 244 Shape initialShape = getGlyphOutline(glyphIndex); 245 AffineTransform trans = AffineTransform.getTranslateInstance(x, y); 246 return trans.createTransformedShape(initialShape); 247 } 248 249 /** 250 * Gets the visual bounds of the specified glyph in the GlyphVector. 251 * 252 * @param glyphIndex 253 * the glyph index in this GlyphVector. 254 * @return the glyph visual bounds of the glyph with the specified index in 255 * the GlyphVector. 256 */ getGlyphVisualBounds(int glyphIndex)257 public abstract Shape getGlyphVisualBounds(int glyphIndex); 258 259 /** 260 * Gets a Shape object which defines the visual representation of the 261 * specified glyph in this GlyphVector. 262 * 263 * @param glyphIndex 264 * the glyth index in this GlyphVector. 265 * @return a Shape object which represents the visual representation of the 266 * specified glyph in this GlyphVector - glyph outline. 267 */ getGlyphOutline(int glyphIndex)268 public abstract Shape getGlyphOutline(int glyphIndex); 269 270 /** 271 * Gets the logical bounds of the specified glyph in the GlyphVector. 272 * 273 * @param glyphIndex 274 * the index in this GlyphVector of the glyph from which to 275 * retrieve its logical bounds 276 * @return the logical bounds of the specified glyph in the GlyphVector. 277 */ getGlyphLogicalBounds(int glyphIndex)278 public abstract Shape getGlyphLogicalBounds(int glyphIndex); 279 280 /** 281 * Gets the visual representation of this GlyphVector rendered in x, y 282 * location as a Shape object. 283 * 284 * @param x 285 * the x coordinate of the GlyphVector. 286 * @param y 287 * the y coordinate of the GlyphVector. 288 * @return the visual representation of this GlyphVector as a Shape object. 289 */ getOutline(float x, float y)290 public abstract Shape getOutline(float x, float y); 291 292 /** 293 * Gets the visual representation of this GlyphVector as a Shape object. 294 * 295 * @return the visual representation of this GlyphVector as a Shape object. 296 */ getOutline()297 public abstract Shape getOutline(); 298 299 /** 300 * Gets the font of this GlyphVector. 301 * 302 * @return the font of this GlyphVector. 303 */ getFont()304 public abstract Font getFont(); 305 306 /** 307 * Gets an array of the glyph codes of the specified glyphs. 308 * 309 * @param beginGlyphIndex 310 * the index into this GlyphVector at which to start retrieving 311 * glyph codes. 312 * @param numEntries 313 * the number of glyph codes. 314 * @param codeReturn 315 * the array into which the resulting glyphcodes will be written. 316 * @return the array of the glyph codes. 317 */ getGlyphCodes(int beginGlyphIndex, int numEntries, int[] codeReturn)318 public abstract int[] getGlyphCodes(int beginGlyphIndex, int numEntries, int[] codeReturn); 319 320 /** 321 * Gets an array of the character indices of the specified glyphs. 322 * 323 * @param beginGlyphIndex 324 * the index of the first glyph to return information for. 325 * @param numEntries 326 * the number of glyph indices to return. 327 * @param codeReturn 328 * the array into which the resulting character indices will be 329 * written. 330 * @return an array of character indices for the specifies glyphs. 331 */ getGlyphCharIndices(int beginGlyphIndex, int numEntries, int[] codeReturn)332 public int[] getGlyphCharIndices(int beginGlyphIndex, int numEntries, int[] codeReturn) { 333 if (codeReturn == null) { 334 codeReturn = new int[numEntries]; 335 } 336 337 for (int i = 0; i < numEntries; i++) { 338 codeReturn[i] = getGlyphCharIndex(i + beginGlyphIndex); 339 } 340 return codeReturn; 341 } 342 343 /** 344 * Gets an array of the positions of the specified glyphs in this 345 * GlyphVector. 346 * 347 * @param beginGlyphIndex 348 * the index of the first glyph to return information for. 349 * @param numEntries 350 * the number of glyphs to return information for. 351 * @param positionReturn 352 * the array where the result will be stored. 353 * @return an array of glyph positions. 354 */ getGlyphPositions(int beginGlyphIndex, int numEntries, float[] positionReturn)355 public abstract float[] getGlyphPositions(int beginGlyphIndex, int numEntries, 356 float[] positionReturn); 357 358 /** 359 * Gets the glyph code of the specified glyph. 360 * 361 * @param glyphIndex 362 * the index in this GlyphVector which corresponds to the glyph 363 * from which to retrieve the glyphcode. 364 * @return the glyphcode of the specified glyph. 365 */ getGlyphCode(int glyphIndex)366 public abstract int getGlyphCode(int glyphIndex); 367 368 /** 369 * Gets the first logical character's index of the specified glyph. 370 * 371 * @param glyphIndex 372 * the glyph index. 373 * @return the the first logical character's index. 374 */ getGlyphCharIndex(int glyphIndex)375 public int getGlyphCharIndex(int glyphIndex) { 376 // default implemetation one-to-one 377 return glyphIndex; 378 } 379 380 /** 381 * Sets default layout to this GlyphVector. 382 */ performDefaultLayout()383 public abstract void performDefaultLayout(); 384 385 /** 386 * Gets the number of glyphs in the GlyphVector. 387 * 388 * @return the number of glyphs in the GlyphVector. 389 */ getNumGlyphs()390 public abstract int getNumGlyphs(); 391 392 /** 393 * Gets flags which describe the global state of the GlyphVector. The 394 * default implementation returns 0. 395 * 396 * @return the layout flags 397 */ getLayoutFlags()398 public int getLayoutFlags() { 399 // default implementation - returned value is 0 400 return 0; 401 } 402 403 } 404