J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This code is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 only, as |
| 6 | * published by the Free Software Foundation. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun in the LICENSE file that accompanied this code. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | * accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License version |
| 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any questions. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * |
| 29 | * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #ifndef __LEFONTINSTANCE_H |
| 34 | #define __LEFONTINSTANCE_H |
| 35 | |
| 36 | #include "LETypes.h" |
| 37 | |
| 38 | /** |
| 39 | * Instances of this class are used by <code>LEFontInstance::mapCharsToGlyphs</code> and |
| 40 | * <code>LEFontInstance::mapCharToGlyph</code> to adjust character codes before the character |
| 41 | * to glyph mapping process. Examples of this are filtering out control characters |
| 42 | * and character mirroring - replacing a character which has both a left and a right |
| 43 | * hand form with the opposite form. |
| 44 | * |
| 45 | * @stable ICU 3.2 |
| 46 | */ |
| 47 | class LECharMapper |
| 48 | { |
| 49 | public: |
| 50 | /** |
| 51 | * Destructor. |
| 52 | * @stable ICU 3.2 |
| 53 | */ |
| 54 | virtual inline ~LECharMapper() {}; |
| 55 | |
| 56 | /** |
| 57 | * This method does the adjustments. |
| 58 | * |
| 59 | * @param ch - the input character |
| 60 | * |
| 61 | * @return the adjusted character |
| 62 | * |
| 63 | * @stable ICU 2.8 |
| 64 | */ |
| 65 | virtual LEUnicode32 mapChar(LEUnicode32 ch) const = 0; |
| 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * This is a forward reference to the class which holds the per-glyph |
| 70 | * storage. |
| 71 | * |
| 72 | * @draft ICU 3.0 |
| 73 | */ |
| 74 | class LEGlyphStorage; |
| 75 | |
| 76 | /** |
| 77 | * This is a virtual base class that serves as the interface between a LayoutEngine |
| 78 | * and the platform font environment. It allows a LayoutEngine to access font tables, do |
| 79 | * character to glyph mapping, and obtain metrics information without knowing any platform |
| 80 | * specific details. There are also a few utility methods for converting between points, |
| 81 | * pixels and funits. (font design units) |
| 82 | * |
| 83 | * An instance of an <code>LEFontInstance</code> represents a font at a particular point |
| 84 | * size. Each instance can represent either a single physical font, or a composite font. |
| 85 | * A composite font is a collection of physical fonts, each of which contains a subset of |
| 86 | * the characters contained in the composite font. |
| 87 | * |
| 88 | * Note: with the exception of <code>getSubFont</code>, the methods in this class only |
| 89 | * make sense for a physical font. If you have an <code>LEFontInstance</code> which |
| 90 | * represents a composite font you should only call the methods below which have |
| 91 | * an <code>LEGlyphID</code>, an <code>LEUnicode</code> or an <code>LEUnicode32</code> |
| 92 | * as one of the arguments because these can be used to select a particular subfont. |
| 93 | * |
| 94 | * Subclasses which implement composite fonts should supply an implementation of these |
| 95 | * methods with some default behavior such as returning constant values, or using the |
| 96 | * values from the first subfont. |
| 97 | * |
| 98 | * @draft ICU 3.0 |
| 99 | */ |
| 100 | class LEFontInstance |
| 101 | { |
| 102 | public: |
| 103 | |
| 104 | /** |
| 105 | * This virtual destructor is here so that the subclass |
| 106 | * destructors can be invoked through the base class. |
| 107 | * |
| 108 | * @stable ICU 2.8 |
| 109 | */ |
| 110 | virtual inline ~LEFontInstance() {}; |
| 111 | |
| 112 | /** |
| 113 | * Get a physical font which can render the given text. For composite fonts, |
| 114 | * if there is no single physical font which can render all of the text, |
| 115 | * return a physical font which can render an initial substring of the text, |
| 116 | * and set the <code>offset</code> parameter to the end of that substring. |
| 117 | * |
| 118 | * Internally, the LayoutEngine works with runs of text all in the same |
| 119 | * font and script, so it is best to call this method with text which is |
| 120 | * in a single script, passing the script code in as a hint. If you don't |
| 121 | * know the script of the text, you can use zero, which is the script code |
| 122 | * for characters used in more than one script. |
| 123 | * |
| 124 | * The default implementation of this method is intended for instances of |
| 125 | * <code>LEFontInstance</code> which represent a physical font. It returns |
| 126 | * <code>this</code> and indicates that the entire string can be rendered. |
| 127 | * |
| 128 | * This method will return a valid <code>LEFontInstance</code> unless you |
| 129 | * have passed illegal parameters, or an internal error has been encountered. |
| 130 | * For composite fonts, it may return the warning <code>LE_NO_SUBFONT_WARNING</code> |
| 131 | * to indicate that the returned font may not be able to render all of |
| 132 | * the text. Whenever a valid font is returned, the <code>offset</code> parameter |
| 133 | * will be advanced by at least one. |
| 134 | * |
| 135 | * Subclasses which implement composite fonts must override this method. |
| 136 | * Where it makes sense, they should use the script code as a hint to render |
| 137 | * characters from the COMMON script in the font which is used for the given |
| 138 | * script. For example, if the input text is a series of Arabic words separated |
| 139 | * by spaces, and the script code passed in is <code>arabScriptCode</code> you |
| 140 | * should return the font used for Arabic characters for all of the input text, |
| 141 | * including the spaces. If, on the other hand, the input text contains characters |
| 142 | * which cannot be rendered by the font used for Arabic characters, but which can |
| 143 | * be rendered by another font, you should return that font for those characters. |
| 144 | * |
| 145 | * @param chars - the array of Unicode characters. |
| 146 | * @param offset - a pointer to the starting offset in the text. On exit this |
| 147 | * will be set the the limit offset of the text which can be |
| 148 | * rendered using the returned font. |
| 149 | * @param limit - the limit offset for the input text. |
| 150 | * @param script - the script hint. |
| 151 | * @param success - set to an error code if the arguments are illegal, or no font |
| 152 | * can be returned for some reason. May also be set to |
| 153 | * <code>LE_NO_SUBFONT_WARNING</code> if the subfont which |
| 154 | * was returned cannot render all of the text. |
| 155 | * |
| 156 | * @return an <code>LEFontInstance</code> for the sub font which can render the characters, or |
| 157 | * <code>NULL</code> if there is an error. |
| 158 | * |
| 159 | * @see LEScripts.h |
| 160 | * |
| 161 | * @stable ICU 3.2 |
| 162 | */ |
| 163 | virtual const LEFontInstance *getSubFont(const LEUnicode chars[], le_int32 *offset, |
| 164 | le_int32 limit, le_int32 script, LEErrorCode &success) const; |
| 165 | |
| 166 | // |
| 167 | // Font file access |
| 168 | // |
| 169 | |
| 170 | /** |
| 171 | * This method reads a table from the font. Note that in general, |
| 172 | * it only makes sense to call this method on an <code>LEFontInstance</code> |
| 173 | * which represents a physical font - i.e. one which has been returned by |
| 174 | * <code>getSubFont()</code>. This is because each subfont in a composite font |
| 175 | * will have different tables, and there's no way to know which subfont to access. |
| 176 | * |
| 177 | * Subclasses which represent composite fonts should always return <code>NULL</code>. |
| 178 | * |
| 179 | * @param tableTag - the four byte table tag. (e.g. 'cmap') |
| 180 | * |
| 181 | * @return the address of the table in memory, or <code>NULL</code> |
| 182 | * if the table doesn't exist. |
| 183 | * |
| 184 | * @stable ICU 2.8 |
| 185 | */ |
| 186 | virtual const void *getFontTable(LETag tableTag) const = 0; |
| 187 | |
| 188 | virtual void *getKernPairs() const = 0; |
| 189 | virtual void setKernPairs(void *pairs) const = 0; |
| 190 | |
| 191 | /** |
| 192 | * This method is used to determine if the font can |
| 193 | * render the given character. This can usually be done |
| 194 | * by looking the character up in the font's character |
| 195 | * to glyph mapping. |
| 196 | * |
| 197 | * The default implementation of this method will return |
| 198 | * <code>TRUE</code> if <code>mapCharToGlyph(ch)</code> |
| 199 | * returns a non-zero value. |
| 200 | * |
| 201 | * @param ch - the character to be tested |
| 202 | * |
| 203 | * @return <code>TRUE</code> if the font can render ch. |
| 204 | * |
| 205 | * @stable ICU 3.2 |
| 206 | */ |
| 207 | virtual inline le_bool canDisplay(LEUnicode32 ch) const; |
| 208 | |
| 209 | /** |
| 210 | * This method returns the number of design units in |
| 211 | * the font's EM square. |
| 212 | * |
| 213 | * @return the number of design units pre EM. |
| 214 | * |
| 215 | * @stable ICU 2.8 |
| 216 | */ |
| 217 | virtual le_int32 getUnitsPerEM() const = 0; |
| 218 | |
| 219 | /** |
| 220 | * This method maps an array of character codes to an array of glyph |
| 221 | * indices, using the font's character to glyph map. |
| 222 | * |
| 223 | * The default implementation iterates over all of the characters and calls |
| 224 | * <code>mapCharToGlyph(ch, mapper)</code> on each one. It also handles surrogate |
| 225 | * characters, storing the glyph ID for the high surrogate, and a deleted glyph (0xFFFF) |
| 226 | * for the low surrogate. |
| 227 | * |
| 228 | * Most sublcasses will not need to implement this method. |
| 229 | * |
| 230 | * @param chars - the character array |
| 231 | * @param offset - the index of the first character |
| 232 | * @param count - the number of characters |
| 233 | * @param reverse - if <code>TRUE</code>, store the glyph indices in reverse order. |
| 234 | * @param mapper - the character mapper. |
| 235 | * @param glyphStorage - the object which contains the output glyph array |
| 236 | * |
| 237 | * @see LECharMapper |
| 238 | * |
| 239 | * @draft ICU 3.0 |
| 240 | */ |
| 241 | virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, |
| 242 | le_bool reverse, const LECharMapper *mapper, LEGlyphStorage &glyphStorage) const; |
| 243 | |
| 244 | /** |
| 245 | * This method maps a single character to a glyph index, using the |
| 246 | * font's character to glyph map. The default implementation of this |
| 247 | * method calls the mapper, and then calls <code>mapCharToGlyph(mappedCh)</code>. |
| 248 | * |
| 249 | * @param ch - the character |
| 250 | * @param mapper - the character mapper |
| 251 | * |
| 252 | * @return the glyph index |
| 253 | * |
| 254 | * @see LECharMapper |
| 255 | * |
| 256 | * @stable ICU 3.2 |
| 257 | */ |
| 258 | virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const; |
| 259 | |
| 260 | /** |
| 261 | * This method maps a single character to a glyph index, using the |
| 262 | * font's character to glyph map. There is no default implementation |
| 263 | * of this method because it requires information about the platform |
| 264 | * font implementation. |
| 265 | * |
| 266 | * @param ch - the character |
| 267 | * |
| 268 | * @return the glyph index |
| 269 | * |
| 270 | * @stable ICU 3.2 |
| 271 | */ |
| 272 | virtual LEGlyphID mapCharToGlyph(LEUnicode32 ch) const = 0; |
| 273 | |
| 274 | // |
| 275 | // Metrics |
| 276 | // |
| 277 | |
| 278 | /** |
| 279 | * This method gets the X and Y advance of a particular glyph, in pixels. |
| 280 | * |
| 281 | * @param glyph - the glyph index |
| 282 | * @param advance - the X and Y pixel values will be stored here |
| 283 | * |
| 284 | * @stable ICU 3.2 |
| 285 | */ |
| 286 | virtual void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const = 0; |
| 287 | |
| 288 | virtual void getKerningAdjustment(LEPoint &adjustment) const = 0; |
| 289 | |
| 290 | /** |
| 291 | * This method gets the hinted X and Y pixel coordinates of a particular |
| 292 | * point in the outline of the given glyph. |
| 293 | * |
| 294 | * @param glyph - the glyph index |
| 295 | * @param pointNumber - the number of the point |
| 296 | * @param point - the point's X and Y pixel values will be stored here |
| 297 | * |
| 298 | * @return <code>TRUE</code> if the point coordinates could be stored. |
| 299 | * |
| 300 | * @stable ICU 2.8 |
| 301 | */ |
| 302 | virtual le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const = 0; |
| 303 | |
| 304 | /** |
| 305 | * This method returns the width of the font's EM square |
| 306 | * in pixels. |
| 307 | * |
| 308 | * @return the pixel width of the EM square |
| 309 | * |
| 310 | * @stable ICU 2.8 |
| 311 | */ |
| 312 | virtual float getXPixelsPerEm() const = 0; |
| 313 | |
| 314 | /** |
| 315 | * This method returns the height of the font's EM square |
| 316 | * in pixels. |
| 317 | * |
| 318 | * @return the pixel height of the EM square |
| 319 | * |
| 320 | * @stable ICU 2.8 |
| 321 | */ |
| 322 | virtual float getYPixelsPerEm() const = 0; |
| 323 | |
| 324 | /** |
| 325 | * This method converts font design units in the |
| 326 | * X direction to points. |
| 327 | * |
| 328 | * @param xUnits - design units in the X direction |
| 329 | * |
| 330 | * @return points in the X direction |
| 331 | * |
| 332 | * @stable ICU 3.2 |
| 333 | */ |
| 334 | virtual inline float xUnitsToPoints(float xUnits) const; |
| 335 | |
| 336 | /** |
| 337 | * This method converts font design units in the |
| 338 | * Y direction to points. |
| 339 | * |
| 340 | * @param yUnits - design units in the Y direction |
| 341 | * |
| 342 | * @return points in the Y direction |
| 343 | * |
| 344 | * @stable ICU 3.2 |
| 345 | */ |
| 346 | virtual inline float yUnitsToPoints(float yUnits) const; |
| 347 | |
| 348 | /** |
| 349 | * This method converts font design units to points. |
| 350 | * |
| 351 | * @param units - X and Y design units |
| 352 | * @param points - set to X and Y points |
| 353 | * |
| 354 | * @stable ICU 3.2 |
| 355 | */ |
| 356 | virtual inline void unitsToPoints(LEPoint &units, LEPoint &points) const; |
| 357 | |
| 358 | /** |
| 359 | * This method converts pixels in the |
| 360 | * X direction to font design units. |
| 361 | * |
| 362 | * @param xPixels - pixels in the X direction |
| 363 | * |
| 364 | * @return font design units in the X direction |
| 365 | * |
| 366 | * @stable ICU 3.2 |
| 367 | */ |
| 368 | virtual inline float xPixelsToUnits(float xPixels) const; |
| 369 | |
| 370 | /** |
| 371 | * This method converts pixels in the |
| 372 | * Y direction to font design units. |
| 373 | * |
| 374 | * @param yPixels - pixels in the Y direction |
| 375 | * |
| 376 | * @return font design units in the Y direction |
| 377 | * |
| 378 | * @stable ICU 3.2 |
| 379 | */ |
| 380 | virtual inline float yPixelsToUnits(float yPixels) const; |
| 381 | |
| 382 | /** |
| 383 | * This method converts pixels to font design units. |
| 384 | * |
| 385 | * @param pixels - X and Y pixel |
| 386 | * @param units - set to X and Y font design units |
| 387 | * |
| 388 | * @stable ICU 3.2 |
| 389 | */ |
| 390 | virtual inline void pixelsToUnits(LEPoint &pixels, LEPoint &units) const; |
| 391 | |
| 392 | /** |
| 393 | * Get the X scale factor from the font's transform. The default |
| 394 | * implementation of <code>transformFunits()</code> will call this method. |
| 395 | * |
| 396 | * @return the X scale factor. |
| 397 | * |
| 398 | * |
| 399 | * @see transformFunits |
| 400 | * |
| 401 | * @stable ICU 3.2 |
| 402 | */ |
| 403 | virtual float getScaleFactorX() const = 0; |
| 404 | |
| 405 | /** |
| 406 | * Get the Y scale factor from the font's transform. The default |
| 407 | * implementation of <code>transformFunits()</code> will call this method. |
| 408 | * |
| 409 | * @return the Yscale factor. |
| 410 | * |
| 411 | * @see transformFunits |
| 412 | * |
| 413 | * @stable ICU 3.2 |
| 414 | */ |
| 415 | virtual float getScaleFactorY() const = 0; |
| 416 | |
| 417 | /** |
| 418 | * This method transforms an X, Y point in font design units to a |
| 419 | * pixel coordinate, applying the font's transform. The default |
| 420 | * implementation of this method calls <code>getScaleFactorX()</code> |
| 421 | * and <code>getScaleFactorY()</code>. |
| 422 | * |
| 423 | * @param xFunits - the X coordinate in font design units |
| 424 | * @param yFunits - the Y coordinate in font design units |
| 425 | * @param pixels - the tranformed co-ordinate in pixels |
| 426 | * |
| 427 | * @see getScaleFactorX |
| 428 | * @see getScaleFactorY |
| 429 | * |
| 430 | * @stable ICU 3.2 |
| 431 | */ |
| 432 | virtual inline void transformFunits(float xFunits, float yFunits, LEPoint &pixels) const; |
| 433 | |
| 434 | /** |
| 435 | * This is a convenience method used to convert |
| 436 | * values in a 16.16 fixed point format to floating point. |
| 437 | * |
| 438 | * @param fixed - the fixed point value |
| 439 | * |
| 440 | * @return the floating point value |
| 441 | * |
| 442 | * @stable ICU 2.8 |
| 443 | */ |
| 444 | static inline float fixedToFloat(le_int32 fixed); |
| 445 | |
| 446 | /** |
| 447 | * This is a convenience method used to convert |
| 448 | * floating point values to 16.16 fixed point format. |
| 449 | * |
| 450 | * @param theFloat - the floating point value |
| 451 | * |
| 452 | * @return the fixed point value |
| 453 | * |
| 454 | * @stable ICU 2.8 |
| 455 | */ |
| 456 | static inline le_int32 floatToFixed(float theFloat); |
| 457 | |
| 458 | // |
| 459 | // These methods won't ever be called by the LayoutEngine, |
| 460 | // but are useful for clients of <code>LEFontInstance</code> who |
| 461 | // need to render text. |
| 462 | // |
| 463 | |
| 464 | /** |
| 465 | * Get the font's ascent. |
| 466 | * |
| 467 | * @return the font's ascent, in points. This value |
| 468 | * will always be positive. |
| 469 | * |
| 470 | * @stable ICU 3.2 |
| 471 | */ |
| 472 | virtual le_int32 getAscent() const = 0; |
| 473 | |
| 474 | /** |
| 475 | * Get the font's descent. |
| 476 | * |
| 477 | * @return the font's descent, in points. This value |
| 478 | * will always be positive. |
| 479 | * |
| 480 | * @stable ICU 3.2 |
| 481 | */ |
| 482 | virtual le_int32 getDescent() const = 0; |
| 483 | |
| 484 | /** |
| 485 | * Get the font's leading. |
| 486 | * |
| 487 | * @return the font's leading, in points. This value |
| 488 | * will always be positive. |
| 489 | * |
| 490 | * @stable ICU 3.2 |
| 491 | */ |
| 492 | virtual le_int32 getLeading() const = 0; |
| 493 | |
| 494 | /** |
| 495 | * Get the line height required to display text in |
| 496 | * this font. The default implementation of this method |
| 497 | * returns the sum of the ascent, descent, and leading. |
| 498 | * |
| 499 | * @return the line height, in points. This vaule will |
| 500 | * always be positive. |
| 501 | * |
| 502 | * @stable ICU 3.2 |
| 503 | */ |
| 504 | virtual le_int32 getLineHeight() const; |
| 505 | }; |
| 506 | |
| 507 | inline le_bool LEFontInstance::canDisplay(LEUnicode32 ch) const |
| 508 | { |
| 509 | return LE_GET_GLYPH(mapCharToGlyph(ch)) != 0; |
| 510 | } |
| 511 | |
| 512 | inline float LEFontInstance::xUnitsToPoints(float xUnits) const |
| 513 | { |
| 514 | return (xUnits * getXPixelsPerEm()) / (float) getUnitsPerEM(); |
| 515 | } |
| 516 | |
| 517 | inline float LEFontInstance::yUnitsToPoints(float yUnits) const |
| 518 | { |
| 519 | return (yUnits * getYPixelsPerEm()) / (float) getUnitsPerEM(); |
| 520 | } |
| 521 | |
| 522 | inline void LEFontInstance::unitsToPoints(LEPoint &units, LEPoint &points) const |
| 523 | { |
| 524 | points.fX = xUnitsToPoints(units.fX); |
| 525 | points.fY = yUnitsToPoints(units.fY); |
| 526 | } |
| 527 | |
| 528 | inline float LEFontInstance::xPixelsToUnits(float xPixels) const |
| 529 | { |
| 530 | return (xPixels * getUnitsPerEM()) / (float) getXPixelsPerEm(); |
| 531 | } |
| 532 | |
| 533 | inline float LEFontInstance::yPixelsToUnits(float yPixels) const |
| 534 | { |
| 535 | return (yPixels * getUnitsPerEM()) / (float) getYPixelsPerEm(); |
| 536 | } |
| 537 | |
| 538 | inline void LEFontInstance::pixelsToUnits(LEPoint &pixels, LEPoint &units) const |
| 539 | { |
| 540 | units.fX = xPixelsToUnits(pixels.fX); |
| 541 | units.fY = yPixelsToUnits(pixels.fY); |
| 542 | } |
| 543 | |
| 544 | inline void LEFontInstance::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const |
| 545 | { |
| 546 | pixels.fX = xUnitsToPoints(xFunits) * getScaleFactorX(); |
| 547 | pixels.fY = yUnitsToPoints(yFunits) * getScaleFactorY(); |
| 548 | } |
| 549 | |
| 550 | inline float LEFontInstance::fixedToFloat(le_int32 fixed) |
| 551 | { |
| 552 | return (float) (fixed / 65536.0); |
| 553 | } |
| 554 | |
| 555 | inline le_int32 LEFontInstance::floatToFixed(float theFloat) |
| 556 | { |
| 557 | return (le_int32) (theFloat * 65536.0); |
| 558 | } |
| 559 | |
| 560 | inline le_int32 LEFontInstance::getLineHeight() const |
| 561 | { |
| 562 | return getAscent() + getDescent() + getLeading(); |
| 563 | } |
| 564 | |
| 565 | #endif |