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