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