2 * (C) Copyright IBM Corp. 1998-2011 - All Rights Reserved
5 #ifndef __LAYOUTENGINE_H
6 #define __LAYOUTENGINE_H
12 * \brief C++ API: Virtual base class for complex text layout.
22 * This is a virtual base class used to do complex text layout. The text must all
23 * be in a single font, script, and language. An instance of a LayoutEngine can be
24 * created by calling the layoutEngineFactory method. Fonts are identified by
25 * instances of the LEFontInstance class. Script and language codes are identified
26 * by integer codes, which are defined in ScriptAndLanuageTags.h.
28 * Note that this class is not public API. It is declared public so that it can be
29 * exported from the library that it is a part of.
31 * The input to the layout process is an array of characters in logical order,
32 * and a starting X, Y position for the text. The output is an array of glyph indices,
33 * an array of character indices for the glyphs, and an array of glyph positions.
34 * These arrays are protected members of LayoutEngine which can be retreived by a
35 * public method. The reset method can be called to free these arrays so that the
36 * LayoutEngine can be reused.
38 * The layout process is done in three steps. There is a protected virtual method
39 * for each step. These methods have a default implementation which only does
40 * character to glyph mapping and default positioning using the glyph's advance
41 * widths. Subclasses can override these methods for more advanced layout.
42 * There is a public method which invokes the steps in the correct order.
46 * 1) Glyph processing - character to glyph mapping and any other glyph processing
47 * such as ligature substitution and contextual forms.
49 * 2) Glyph positioning - position the glyphs based on their advance widths.
51 * 3) Glyph position adjustments - adjustment of glyph positions for kerning,
52 * accent placement, etc.
54 * NOTE: in all methods below, output parameters are references to pointers so
55 * the method can allocate and free the storage as needed. All storage allocated
56 * in this way is owned by the object which created it, and will be freed when it
57 * is no longer needed, or when the object's destructor is invoked.
60 * @see ScriptAndLanguageTags.h
64 class U_LAYOUT_API LayoutEngine
: public UObject
{
66 #ifndef U_HIDE_INTERNAL_API
67 /** @internal Flag to request kerning. */
68 static const le_int32 kTypoFlagKern
;
69 /** @internal Flag to request ligatures. */
70 static const le_int32 kTypoFlagLiga
;
71 #endif /* U_HIDE_INTERNAL_API */
75 * The object which holds the glyph storage
79 LEGlyphStorage
*fGlyphStorage
;
82 * The font instance for the text font.
88 const LEFontInstance
*fFontInstance
;
91 * The script code for the text
93 * @see ScriptAndLanguageTags.h for script codes.
100 * The langauge code for the text
102 * @see ScriptAndLanguageTags.h for language codes.
106 le_int32 fLanguageCode
;
109 * The typographic control flags
116 * <code>TRUE</code> if <code>mapCharsToGlyphs</code> should replace ZWJ / ZWNJ with a glyph
121 le_bool fFilterZeroWidth
;
123 #ifndef U_HIDE_INTERNAL_API
125 * This constructs an instance for a given font, script and language. Subclass constructors
126 * must call this constructor.
128 * @param fontInstance - the font for the text
129 * @param scriptCode - the script for the text
130 * @param languageCode - the language for the text
131 * @param typoFlags - the typographic control flags for the text (a bitfield). Use kTypoFlagKern
132 * if kerning is desired, kTypoFlagLiga if ligature formation is desired. Others are reserved.
133 * @param success - set to an error code if the operation fails
135 * @see LEFontInstance
136 * @see ScriptAndLanguageTags.h
140 LayoutEngine(const LEFontInstance
*fontInstance
,
142 le_int32 languageCode
,
144 LEErrorCode
&success
);
145 #endif /* U_HIDE_INTERNAL_API */
147 // Do not enclose the protected default constructor with #ifndef U_HIDE_INTERNAL_API
148 // or else the compiler will create a public default constructor.
150 * This overrides the default no argument constructor to make it
151 * difficult for clients to call it. Clients are expected to call
152 * layoutEngineFactory.
159 * This method does any required pre-processing to the input characters. It
160 * may generate output characters that differ from the input charcters due to
161 * insertions, deletions, or reorderings. In such cases, it will also generate an
162 * output character index array reflecting these changes.
164 * Subclasses must override this method.
167 * @param chars - the input character context
168 * @param offset - the index of the first character to process
169 * @param count - the number of characters to process
170 * @param max - the number of characters in the input context
171 * @param rightToLeft - TRUE if the characters are in a right to left directional run
172 * @param outChars - the output character array, if different from the input
173 * @param glyphStorage - the object that holds the per-glyph storage. The character index array may be set.
174 * @param success - set to an error code if the operation fails
176 * @return the output character count (input character count if no change)
180 virtual le_int32
characterProcessing(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_int32 max
, le_bool rightToLeft
,
181 LEUnicode
*&outChars
, LEGlyphStorage
&glyphStorage
, LEErrorCode
&success
);
184 * This method does the glyph processing. It converts an array of characters
185 * into an array of glyph indices and character indices. The characters to be
186 * processed are passed in a surrounding context. The context is specified as
187 * a starting address and a maximum character count. An offset and a count are
188 * used to specify the characters to be processed.
190 * The default implementation of this method only does character to glyph mapping.
191 * Subclasses needing more elaborate glyph processing must override this method.
194 * @param chars - the character context
195 * @param offset - the offset of the first character to process
196 * @param count - the number of characters to process
197 * @param max - the number of characters in the context.
198 * @param rightToLeft - TRUE if the text is in a right to left directional run
199 * @param glyphStorage - the object which holds the per-glyph storage. The glyph and char indices arrays
203 * @param success - set to an error code if the operation fails
205 * @return the number of glyphs in the glyph index array
209 virtual le_int32
computeGlyphs(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_int32 max
, le_bool rightToLeft
, LEGlyphStorage
&glyphStorage
, LEErrorCode
&success
);
212 * This method does basic glyph positioning. The default implementation positions
213 * the glyphs based on their advance widths. This is sufficient for most uses. It
214 * is not expected that many subclasses will override this method.
217 * @param glyphStorage - the object which holds the per-glyph storage. The glyph position array will be set.
218 * @param x - the starting X position
219 * @param y - the starting Y position
220 * @param success - set to an error code if the operation fails
224 virtual void positionGlyphs(LEGlyphStorage
&glyphStorage
, float x
, float y
, LEErrorCode
&success
);
227 * This method does positioning adjustments like accent positioning and
228 * kerning. The default implementation does nothing. Subclasses needing
229 * position adjustments must override this method.
231 * Note that this method has both characters and glyphs as input so that
232 * it can use the character codes to determine glyph types if that information
233 * isn't directly available. (e.g. Some Arabic OpenType fonts don't have a GDEF
236 * @param chars - the input character context
237 * @param offset - the offset of the first character to process
238 * @param count - the number of characters to process
239 * @param reverse - <code>TRUE</code> if the glyphs in the glyph array have been reordered
240 * @param glyphStorage - the object which holds the per-glyph storage. The glyph positions will be
241 * adjusted as needed.
242 * @param success - output parameter set to an error code if the operation fails
246 virtual void adjustGlyphPositions(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_bool reverse
, LEGlyphStorage
&glyphStorage
, LEErrorCode
&success
);
249 * This method gets a table from the font associated with
250 * the text. The default implementation gets the table from
251 * the font instance. Subclasses which need to get the tables
252 * some other way must override this method.
254 * @param tableTag - the four byte table tag.
256 * @return the address of the table.
260 virtual const void *getFontTable(LETag tableTag
) const;
263 * This method does character to glyph mapping. The default implementation
264 * uses the font instance to do the mapping. It will allocate the glyph and
265 * character index arrays if they're not already allocated. If it allocates the
266 * character index array, it will fill it it.
268 * This method supports right to left
269 * text with the ability to store the glyphs in reverse order, and by supporting
270 * character mirroring, which will replace a character which has a left and right
271 * form, such as parens, with the opposite form before mapping it to a glyph index.
274 * @param chars - the input character context
275 * @param offset - the offset of the first character to be mapped
276 * @param count - the number of characters to be mapped
277 * @param reverse - if <code>TRUE</code>, the output will be in reverse order
278 * @param mirror - if <code>TRUE</code>, do character mirroring
279 * @param glyphStorage - the object which holds the per-glyph storage. The glyph and char
280 * indices arrays will be filled in.
281 * @param success - set to an error code if the operation fails
283 * @see LEFontInstance
287 virtual void mapCharsToGlyphs(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_bool reverse
, le_bool mirror
, LEGlyphStorage
&glyphStorage
, LEErrorCode
&success
);
289 #ifndef U_HIDE_INTERNAL_API
291 * This is a convenience method that forces the advance width of mark
292 * glyphs to be zero, which is required for proper selection and highlighting.
294 * @param glyphStorage - the object containing the per-glyph storage. The positions array will be modified.
295 * @param markFilter - used to identify mark glyphs
296 * @param success - output parameter set to an error code if the operation fails
302 static void adjustMarkGlyphs(LEGlyphStorage
&glyphStorage
, LEGlyphFilter
*markFilter
, LEErrorCode
&success
);
306 * This is a convenience method that forces the advance width of mark
307 * glyphs to be zero, which is required for proper selection and highlighting.
308 * This method uses the input characters to identify marks. This is required in
309 * cases where the font does not contain enough information to identify them based
312 * @param chars - the array of input characters
313 * @param charCount - the number of input characers
314 * @param glyphStorage - the object containing the per-glyph storage. The positions array will be modified.
315 * @param reverse - <code>TRUE</code> if the glyph array has been reordered
316 * @param markFilter - used to identify mark glyphs
317 * @param success - output parameter set to an error code if the operation fails
323 static void adjustMarkGlyphs(const LEUnicode chars
[], le_int32 charCount
, le_bool reverse
, LEGlyphStorage
&glyphStorage
, LEGlyphFilter
*markFilter
, LEErrorCode
&success
);
324 #endif /* U_HIDE_INTERNAL_API */
328 * The destructor. It will free any storage allocated for the
329 * glyph, character index and position arrays by calling the reset
330 * method. It is declared virtual so that it will be invoked by the
331 * subclass destructors.
335 virtual ~LayoutEngine();
338 * This method will invoke the layout steps in their correct order by calling
339 * the computeGlyphs, positionGlyphs and adjustGlyphPosition methods. It will
340 * compute the glyph, character index and position arrays.
342 * @param chars - the input character context
343 * @param offset - the offset of the first character to process
344 * @param count - the number of characters to process
345 * @param max - the number of characters in the input context
346 * @param rightToLeft - TRUE if the characers are in a right to left directional run
347 * @param x - the initial X position
348 * @param y - the initial Y position
349 * @param success - output parameter set to an error code if the operation fails
351 * @return the number of glyphs in the glyph array
353 * Note: The glyph, character index and position array can be accessed
354 * using the getter methods below.
356 * Note: If you call this method more than once, you must call the reset()
357 * method first to free the glyph, character index and position arrays
358 * allocated by the previous call.
362 virtual le_int32
layoutChars(const LEUnicode chars
[], le_int32 offset
, le_int32 count
, le_int32 max
, le_bool rightToLeft
, float x
, float y
, LEErrorCode
&success
);
365 * This method returns the number of glyphs in the glyph array. Note
366 * that the number of glyphs will be greater than or equal to the number
367 * of characters used to create the LayoutEngine.
369 * @return the number of glyphs in the glyph array
373 le_int32
getGlyphCount() const;
376 * This method copies the glyph array into a caller supplied array.
377 * The caller must ensure that the array is large enough to hold all
380 * @param glyphs - the destiniation glyph array
381 * @param success - set to an error code if the operation fails
385 void getGlyphs(LEGlyphID glyphs
[], LEErrorCode
&success
) const;
388 * This method copies the glyph array into a caller supplied array,
389 * ORing in extra bits. (This functionality is needed by the JDK,
390 * which uses 32 bits pre glyph idex, with the high 16 bits encoding
391 * the composite font slot number)
393 * @param glyphs - the destination (32 bit) glyph array
394 * @param extraBits - this value will be ORed with each glyph index
395 * @param success - set to an error code if the operation fails
399 virtual void getGlyphs(le_uint32 glyphs
[], le_uint32 extraBits
, LEErrorCode
&success
) const;
402 * This method copies the character index array into a caller supplied array.
403 * The caller must ensure that the array is large enough to hold a
404 * character index for each glyph.
406 * @param charIndices - the destiniation character index array
407 * @param success - set to an error code if the operation fails
411 void getCharIndices(le_int32 charIndices
[], LEErrorCode
&success
) const;
414 * This method copies the character index array into a caller supplied array.
415 * The caller must ensure that the array is large enough to hold a
416 * character index for each glyph.
418 * @param charIndices - the destiniation character index array
419 * @param indexBase - an offset which will be added to each index
420 * @param success - set to an error code if the operation fails
424 void getCharIndices(le_int32 charIndices
[], le_int32 indexBase
, LEErrorCode
&success
) const;
427 * This method copies the position array into a caller supplied array.
428 * The caller must ensure that the array is large enough to hold an
429 * X and Y position for each glyph, plus an extra X and Y for the
430 * advance of the last glyph.
432 * @param positions - the destiniation position array
433 * @param success - set to an error code if the operation fails
437 void getGlyphPositions(float positions
[], LEErrorCode
&success
) const;
440 * This method returns the X and Y position of the glyph at
444 * @param glyphIndex - the index of the glyph
447 * @param x - the glyph's X position
448 * @param y - the glyph's Y position
449 * @param success - set to an error code if the operation fails
453 void getGlyphPosition(le_int32 glyphIndex
, float &x
, float &y
, LEErrorCode
&success
) const;
456 * This method frees the glyph, character index and position arrays
457 * so that the LayoutEngine can be reused to layout a different
458 * characer array. (This method is also called by the destructor)
462 virtual void reset();
465 * This method returns a LayoutEngine capable of laying out text
466 * in the given font, script and langauge. Note that the LayoutEngine
467 * returned may be a subclass of LayoutEngine.
469 * @param fontInstance - the font of the text
470 * @param scriptCode - the script of the text
471 * @param languageCode - the language of the text
472 * @param success - output parameter set to an error code if the operation fails
474 * @return a LayoutEngine which can layout text in the given font.
476 * @see LEFontInstance
480 static LayoutEngine
*layoutEngineFactory(const LEFontInstance
*fontInstance
, le_int32 scriptCode
, le_int32 languageCode
, LEErrorCode
&success
);
483 * Override of existing call that provides flags to control typography.
486 static LayoutEngine
*layoutEngineFactory(const LEFontInstance
*fontInstance
, le_int32 scriptCode
, le_int32 languageCode
, le_int32 typo_flags
, LEErrorCode
&success
);
489 * ICU "poor man's RTTI", returns a UClassID for the actual class.
493 virtual UClassID
getDynamicClassID() const;
496 * ICU "poor man's RTTI", returns a UClassID for this class.
500 static UClassID
getStaticClassID();