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 | * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #ifndef __LAYOUTENGINE_H |
| 33 | #define __LAYOUTENGINE_H |
| 34 | |
| 35 | #include "LETypes.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | class LEFontInstance; |
| 40 | class LEGlyphFilter; |
| 41 | class LEGlyphStorage; |
| 42 | |
| 43 | /** |
| 44 | * This is a virtual base class used to do complex text layout. The |
| 45 | * text must all be in a single font, script, and language. An |
| 46 | * instance of a LayoutEngine can be created by calling the |
| 47 | * layoutEngineFactory method. Fonts are identified by instances of |
| 48 | * the LEFontInstance class. Script and language codes are identified |
| 49 | * by integer codes, which are defined in ScriptAndLanuageTags.h. |
| 50 | * |
| 51 | * Note that this class is not public API. It is declared public so |
| 52 | * that it can be exported from the library that it is a part of. |
| 53 | * |
| 54 | * The input to the layout process is an array of characters in |
| 55 | * logical order, and a starting X, Y position for the text. The |
| 56 | * output is an array of glyph indices, an array of character indices |
| 57 | * for the glyphs, and an array of glyph positions. These arrays are |
| 58 | * protected members of LayoutEngine which can be retreived by a |
| 59 | * public method. The reset method can be called to free these arrays |
| 60 | * so that the LayoutEngine can be reused. |
| 61 | * |
| 62 | * The layout process is done in three steps. There is a protected |
| 63 | * virtual method for each step. These methods have a default |
| 64 | * implementation which only does character to glyph mapping and |
| 65 | * default positioning using the glyph's advance widths. Subclasses |
| 66 | * can override these methods for more advanced layout. There is a |
| 67 | * public method which invokes the steps in the correct order. |
| 68 | * |
| 69 | * The steps are: |
| 70 | * |
| 71 | * 1) Glyph processing - character to glyph mapping and any other |
| 72 | * glyph processing such as ligature substitution and contextual |
| 73 | * forms. |
| 74 | * |
| 75 | * 2) Glyph positioning - position the glyphs based on their advance |
| 76 | * widths. |
| 77 | * |
| 78 | * 3) Glyph position adjustments - adjustment of glyph positions for |
| 79 | * kerning, accent placement, etc. |
| 80 | * |
| 81 | * NOTE: in all methods below, output parameters are references to |
| 82 | * pointers so the method can allocate and free the storage as |
| 83 | * needed. All storage allocated in this way is owned by the object |
| 84 | * which created it, and will be freed when it is no longer needed, or |
| 85 | * when the object's destructor is invoked. |
| 86 | * |
| 87 | * @see LEFontInstance |
| 88 | * @see ScriptAndLanguageTags.h |
| 89 | * |
| 90 | * @stable ICU 2.8 |
| 91 | */ |
| 92 | class U_LAYOUT_API LayoutEngine |
| 93 | { |
| 94 | protected: |
| 95 | /** |
| 96 | * The object which holds the glyph storage |
| 97 | * |
| 98 | * @internal |
| 99 | */ |
| 100 | LEGlyphStorage *fGlyphStorage; |
| 101 | |
| 102 | /** |
| 103 | * The font instance for the text font. |
| 104 | * |
| 105 | * @see LEFontInstance |
| 106 | * |
| 107 | * @internal |
| 108 | */ |
| 109 | const LEFontInstance *fFontInstance; |
| 110 | |
| 111 | /** |
| 112 | * The script code for the text |
| 113 | * |
| 114 | * @see ScriptAndLanguageTags.h for script codes. |
| 115 | * |
| 116 | * @internal |
| 117 | */ |
| 118 | le_int32 fScriptCode; |
| 119 | |
| 120 | /** |
| 121 | * The langauge code for the text |
| 122 | * |
| 123 | * @see ScriptAndLanguageTags.h for language codes. |
| 124 | * |
| 125 | * @internal |
| 126 | */ |
| 127 | le_int32 fLanguageCode; |
| 128 | |
| 129 | /** |
| 130 | * The typographic control flags |
| 131 | * |
| 132 | * @internal |
| 133 | */ |
| 134 | le_int32 fTypoFlags; |
| 135 | |
| 136 | /** |
| 137 | * This constructs an instance for a given font, script and |
| 138 | * language. Subclass constructors |
| 139 | * must call this constructor. |
| 140 | * |
| 141 | * @param fontInstance - the font for the text |
| 142 | * @param scriptCode - the script for the text |
| 143 | * @param languageCode - the language for the text |
| 144 | * |
| 145 | * @see LEFontInstance |
| 146 | * @see ScriptAndLanguageTags.h |
| 147 | * |
| 148 | * @internal |
| 149 | */ |
| 150 | LayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, |
| 151 | le_int32 languageCode, le_int32 typoFlags); |
| 152 | |
| 153 | /** |
| 154 | * This overrides the default no argument constructor to make it |
| 155 | * difficult for clients to call it. Clients are expected to call |
| 156 | * layoutEngineFactory. |
| 157 | * |
| 158 | * @internal |
| 159 | */ |
| 160 | LayoutEngine(); |
| 161 | |
| 162 | /** |
| 163 | * This method does any required pre-processing to the input |
| 164 | * characters. It may generate output characters that differ from |
| 165 | * the input charcters due to insertions, deletions, or |
| 166 | * reorderings. In such cases, it will also generate an output |
| 167 | * character index array reflecting these changes. |
| 168 | * |
| 169 | * Subclasses must override this method. |
| 170 | * |
| 171 | * Input parameters: |
| 172 | * @param chars - the input character context |
| 173 | * @param offset - the index of the first character to process |
| 174 | * @param count - the number of characters to process |
| 175 | * @param max - the number of characters in the input context |
| 176 | * @param rightToLeft - TRUE if the characters are in a right to |
| 177 | * left directional run |
| 178 | * @param outChars - the output character array, if different from |
| 179 | * the input |
| 180 | * @param glyphStorage - the object that holds the per-glyph |
| 181 | * storage. The character index array may be set. |
| 182 | * @param success - set to an error code if the operation fails |
| 183 | * @return the output character count (input character count if no |
| 184 | * change) |
| 185 | * |
| 186 | * @internal |
| 187 | */ |
| 188 | virtual le_int32 characterProcessing(const LEUnicode chars[], le_int32 offset, |
| 189 | le_int32 count, le_int32 max, le_bool rightToLeft, |
| 190 | LEUnicode *&outChars, LEGlyphStorage &glyphStorage, LEErrorCode &success); |
| 191 | |
| 192 | /** |
| 193 | * This method does the glyph processing. It converts an array of |
| 194 | * characters into an array of glyph indices and character |
| 195 | * indices. The characters to be processed are passed in a |
| 196 | * surrounding context. The context is specified as a starting |
| 197 | * address and a maximum character count. An offset and a count |
| 198 | * are used to specify the characters to be processed. |
| 199 | * |
| 200 | * The default implementation of this method only does character |
| 201 | * to glyph mapping. Subclasses needing more elaborate glyph |
| 202 | * processing must override this method. |
| 203 | * |
| 204 | * Input parameters: |
| 205 | * @param chars - the character context |
| 206 | * @param offset - the offset of the first character to process |
| 207 | * @param count - the number of characters to process |
| 208 | * @param max - the number of characters in the context. |
| 209 | * @param rightToLeft - TRUE if the text is in a right to left |
| 210 | * directional run |
| 211 | * @param glyphStorage - the object which holds the per-glyph |
| 212 | * storage. The glyph and char indices arrays will be |
| 213 | * set. |
| 214 | * |
| 215 | * Output parameters: |
| 216 | * @param success - set to an error code if the operation fails |
| 217 | * |
| 218 | * @return the number of glyphs in the glyph index array |
| 219 | * |
| 220 | * @internal |
| 221 | */ |
| 222 | virtual le_int32 computeGlyphs(const LEUnicode chars[], le_int32 offset, |
| 223 | le_int32 count, le_int32 max, le_bool rightToLeft, |
| 224 | LEGlyphStorage &glyphStorage, LEErrorCode &success); |
| 225 | |
| 226 | /** |
| 227 | * This method does basic glyph positioning. The default |
| 228 | * implementation positions the glyphs based on their advance |
| 229 | * widths. This is sufficient for most uses. It is not expected |
| 230 | * that many subclasses will override this method. |
| 231 | * |
| 232 | * Input parameters: |
| 233 | * @param glyphStorage - the object which holds the per-glyph storage. |
| 234 | * The glyph position array will be set. |
| 235 | * @param x - the starting X position |
| 236 | * @param y - the starting Y position |
| 237 | * @param success - set to an error code if the operation fails |
| 238 | * |
| 239 | * @internal |
| 240 | */ |
| 241 | virtual void positionGlyphs(LEGlyphStorage &glyphStorage, |
| 242 | float x, float y, LEErrorCode &success); |
| 243 | |
| 244 | /** |
| 245 | * This method does positioning adjustments like accent |
| 246 | * positioning and kerning. The default implementation does |
| 247 | * nothing. Subclasses needing position adjustments must override |
| 248 | * this method. |
| 249 | * |
| 250 | * Note that this method has both characters and glyphs as input |
| 251 | * so that it can use the character codes to determine glyph types |
| 252 | * if that information isn't directly available. (e.g. Some Arabic |
| 253 | * OpenType fonts don't have a GDEF table) |
| 254 | * |
| 255 | * @param chars - the input character context |
| 256 | * @param offset - the offset of the first character to process |
| 257 | * @param count - the number of characters to process |
| 258 | * @param reverse - <code>TRUE</code> if the glyphs in the glyph |
| 259 | * array have been reordered |
| 260 | * @param glyphStorage - the object which holds the per-glyph |
| 261 | * storage. The glyph positions will be adjusted as needed. |
| 262 | * @param success - output parameter set to an error code if the |
| 263 | * operation fails |
| 264 | * |
| 265 | * @internal |
| 266 | */ |
| 267 | virtual void adjustGlyphPositions(const LEUnicode chars[], |
| 268 | le_int32 offset, le_int32 count, le_bool reverse, |
| 269 | LEGlyphStorage &glyphStorage, LEErrorCode &success); |
| 270 | |
| 271 | /** |
| 272 | * This method gets a table from the font associated with the |
| 273 | * text. The default implementation gets the table from the font |
| 274 | * instance. Subclasses which need to get the tables some other |
| 275 | * way must override this method. |
| 276 | * |
| 277 | * @param tableTag - the four byte table tag. |
| 278 | * |
| 279 | * @return the address of the table. |
| 280 | * |
| 281 | * @internal |
| 282 | */ |
| 283 | virtual const void *getFontTable(LETag tableTag) const; |
| 284 | |
| 285 | /** |
| 286 | * This method does character to glyph mapping. The default |
| 287 | * implementation uses the font instance to do the mapping. It |
| 288 | * will allocate the glyph and character index arrays if they're |
| 289 | * not already allocated. If it allocates the character index |
| 290 | * array, it will fill it it. |
| 291 | * |
| 292 | * This method supports right to left text with the ability to |
| 293 | * store the glyphs in reverse order, and by supporting character |
| 294 | * mirroring, which will replace a character which has a left and |
| 295 | * right form, such as parens, with the opposite form before |
| 296 | * mapping it to a glyph index. |
| 297 | * |
| 298 | * Input parameters: |
| 299 | * @param chars - the input character context |
| 300 | * @param offset - the offset of the first character to be mapped |
| 301 | * @param count - the number of characters to be mapped |
| 302 | * @param reverse - if <code>TRUE</code>, the output will be in |
| 303 | * reverse order |
| 304 | * @param mirror - if <code>TRUE</code>, do character mirroring |
| 305 | * @param glyphStorage - the object which holds the per-glyph |
| 306 | * storage. The glyph and char indices arrays will be |
| 307 | * filled in. |
| 308 | * @param success - set to an error code if the operation fails |
| 309 | * |
| 310 | * @see LEFontInstance |
| 311 | * |
| 312 | * @internal |
| 313 | */ |
| 314 | virtual void mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, |
| 315 | le_int32 count, le_bool reverse, le_bool mirror, |
| 316 | LEGlyphStorage &glyphStorage, LEErrorCode &success); |
| 317 | |
| 318 | /** |
| 319 | * This is a convenience method that forces the advance width of |
| 320 | * mark glyphs to be zero, which is required for proper selection |
| 321 | * and highlighting. |
| 322 | * |
| 323 | * @param glyphStorage - the object containing the per-glyph |
| 324 | * storage. The positions array will be modified. |
| 325 | * @param markFilter - used to identify mark glyphs |
| 326 | * @param success - output parameter set to an error code if the |
| 327 | * operation fails |
| 328 | * |
| 329 | * @see LEGlyphFilter |
| 330 | * |
| 331 | * @internal |
| 332 | */ |
| 333 | static void adjustMarkGlyphs(LEGlyphStorage &glyphStorage, |
| 334 | LEGlyphFilter *markFilter, LEErrorCode &success); |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * This is a convenience method that forces the advance width of |
| 339 | * mark glyphs to be zero, which is required for proper selection |
| 340 | * and highlighting. This method uses the input characters to |
| 341 | * identify marks. This is required in cases where the font does |
| 342 | * not contain enough information to identify them based on the |
| 343 | * glyph IDs. |
| 344 | * |
| 345 | * @param chars - the array of input characters |
| 346 | * @param charCount - the number of input characers |
| 347 | * @param glyphStorage - the object containing the per-glyph |
| 348 | * storage. The positions array will be modified. |
| 349 | * @param reverse - <code>TRUE</code> if the glyph array has been |
| 350 | * reordered |
| 351 | * @param markFilter - used to identify mark glyphs |
| 352 | * @param success - output parameter set to an error code if the |
| 353 | * operation fails |
| 354 | * |
| 355 | * @see LEGlyphFilter |
| 356 | * |
| 357 | * @internal |
| 358 | */ |
| 359 | static void adjustMarkGlyphs(const LEUnicode chars[], |
| 360 | le_int32 charCount, le_bool reverse, |
| 361 | LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, |
| 362 | LEErrorCode &success); |
| 363 | |
| 364 | public: |
| 365 | /** |
| 366 | * The destructor. It will free any storage allocated for the |
| 367 | * glyph, character index and position arrays by calling the reset |
| 368 | * method. It is declared virtual so that it will be invoked by |
| 369 | * the subclass destructors. |
| 370 | * |
| 371 | * @stable ICU 2.8 |
| 372 | */ |
| 373 | virtual ~LayoutEngine(); |
| 374 | |
| 375 | /** |
| 376 | * This method will invoke the layout steps in their correct order |
| 377 | * by calling the 32 bit versions of the computeGlyphs and |
| 378 | * positionGlyphs methods.(It doesn't * call the |
| 379 | * adjustGlyphPositions method because that doesn't apply for |
| 380 | * default * processing.) It will compute the glyph, character |
| 381 | * index and position arrays. |
| 382 | * |
| 383 | * @param chars - the input character context |
| 384 | * @param offset - the offset of the first character to process |
| 385 | * @param count - the number of characters to process |
| 386 | * @param max - the number of characters in the input context |
| 387 | * @param rightToLeft - true if the characers are in a right to |
| 388 | * left directional run |
| 389 | * @param x - the initial X position |
| 390 | * @param y - the initial Y position |
| 391 | * @param success - output parameter set to an error code if the |
| 392 | * operation fails |
| 393 | * @return the number of glyphs in the glyph array |
| 394 | * |
| 395 | * Note: the glyph, character index and position array can be |
| 396 | * accessed using the getter method below. |
| 397 | */ |
| 398 | le_int32 layoutChars(const LEUnicode chars[], le_int32 offset, |
| 399 | le_int32 count, le_int32 max, le_bool rightToLeft, float x, |
| 400 | float y, LEErrorCode &success); |
| 401 | |
| 402 | /** |
| 403 | * This method returns the number of glyphs in the glyph |
| 404 | * array. Note that the number of glyphs will be greater than or |
| 405 | * equal to the number of characters used to create the |
| 406 | * LayoutEngine. |
| 407 | * |
| 408 | * @return the number of glyphs in the glyph array |
| 409 | * |
| 410 | * @stable ICU 2.8 |
| 411 | */ |
| 412 | le_int32 getGlyphCount() const; |
| 413 | |
| 414 | /** |
| 415 | * This method copies the glyph array into a caller supplied |
| 416 | * array. The caller must ensure that the array is large enough |
| 417 | * to hold all the glyphs. |
| 418 | * |
| 419 | * @param glyphs - the destiniation glyph array |
| 420 | * @param success - set to an error code if the operation fails |
| 421 | * |
| 422 | * @stable ICU 2.8 |
| 423 | */ |
| 424 | void getGlyphs(LEGlyphID glyphs[], LEErrorCode &success) const; |
| 425 | |
| 426 | /** |
| 427 | * This method copies the glyph array into a caller supplied |
| 428 | * array, ORing in extra bits. (This functionality is needed by |
| 429 | * the JDK, which uses 32 bits pre glyph idex, with the high 16 |
| 430 | * bits encoding the composite font slot number) |
| 431 | * |
| 432 | * @param glyphs - the destination (32 bit) glyph array |
| 433 | * @param extraBits - this value will be ORed with each glyph index |
| 434 | * @param success - set to an error code if the operation fails |
| 435 | * |
| 436 | * @stable ICU 2.8 |
| 437 | */ |
| 438 | virtual void getGlyphs(le_uint32 glyphs[], le_uint32 extraBits, |
| 439 | LEErrorCode &success) const; |
| 440 | |
| 441 | /** |
| 442 | * This method copies the character index array into a caller |
| 443 | * supplied array. The caller must ensure that the array is large |
| 444 | * enough to hold a character index for each glyph. |
| 445 | * |
| 446 | * @param charIndices - the destiniation character index array |
| 447 | * @param success - set to an error code if the operation fails |
| 448 | * |
| 449 | * @stable ICU 2.8 |
| 450 | */ |
| 451 | void getCharIndices(le_int32 charIndices[], LEErrorCode &success) const; |
| 452 | |
| 453 | /** |
| 454 | * This method copies the character index array into a caller |
| 455 | * supplied array. The caller must ensure that the array is large |
| 456 | * enough to hold a character index for each glyph. |
| 457 | * |
| 458 | * @param charIndices - the destiniation character index array |
| 459 | * @param indexBase - an offset which will be added to each index |
| 460 | * @param success - set to an error code if the operation fails |
| 461 | * |
| 462 | * @stable ICU 2.8 |
| 463 | */ |
| 464 | void getCharIndices(le_int32 charIndices[], le_int32 indexBase, |
| 465 | LEErrorCode &success) const; |
| 466 | |
| 467 | /** |
| 468 | * This method copies the position array into a caller supplied |
| 469 | * array. The caller must ensure that the array is large enough |
| 470 | * to hold an X and Y position for each glyph, plus an extra X and |
| 471 | * Y for the advance of the last glyph. |
| 472 | * |
| 473 | * @param positions - the destiniation position array |
| 474 | * @param success - set to an error code if the operation fails |
| 475 | * |
| 476 | * @stable ICU 2.8 |
| 477 | */ |
| 478 | void getGlyphPositions(float positions[], LEErrorCode &success) const; |
| 479 | |
| 480 | /** |
| 481 | * This method returns the X and Y position of the glyph at |
| 482 | * the given index. |
| 483 | * |
| 484 | * Input parameters: |
| 485 | * @param glyphIndex - the index of the glyph |
| 486 | * |
| 487 | * Output parameters: |
| 488 | * @param x - the glyph's X position |
| 489 | * @param y - the glyph's Y position |
| 490 | * @param success - set to an error code if the operation fails |
| 491 | * |
| 492 | * @stable ICU 2.8 |
| 493 | */ |
| 494 | void getGlyphPosition(le_int32 glyphIndex, float &x, float &y, |
| 495 | LEErrorCode &success) const; |
| 496 | |
| 497 | /** |
| 498 | * This method frees the glyph, character index and position arrays |
| 499 | * so that the LayoutEngine can be reused to layout a different |
| 500 | * characer array. (This method is also called by the destructor) |
| 501 | * |
| 502 | * @stable ICU 2.8 |
| 503 | */ |
| 504 | virtual void reset(); |
| 505 | |
| 506 | /** |
| 507 | * This method returns a LayoutEngine capable of laying out text |
| 508 | * in the given font, script and langauge. Note that the LayoutEngine |
| 509 | * returned may be a subclass of LayoutEngine. |
| 510 | * |
| 511 | * @param fontInstance - the font of the text |
| 512 | * @param scriptCode - the script of the text |
| 513 | * @param languageCode - the language of the text |
| 514 | * @param success - output parameter set to an error code if the |
| 515 | * operation fails |
| 516 | * |
| 517 | * @return a LayoutEngine which can layout text in the given font. |
| 518 | * |
| 519 | * @see LEFontInstance |
| 520 | * |
| 521 | * @stable ICU 2.8 |
| 522 | */ |
| 523 | static LayoutEngine *layoutEngineFactory(const LEFontInstance *fontInstance, |
| 524 | le_int32 scriptCode, le_int32 languageCode, LEErrorCode &success); |
| 525 | |
| 526 | /** |
| 527 | * Override of existing call that provides flags to control typography. |
| 528 | * @draft ICU 3.4 |
| 529 | */ |
| 530 | static LayoutEngine *layoutEngineFactory( |
| 531 | const LEFontInstance *fontInstance, |
| 532 | le_int32 scriptCode, le_int32 languageCode, |
| 533 | le_int32 typo_flags, LEErrorCode &success); |
| 534 | }; |
| 535 | |
| 536 | #endif |