2 **********************************************************************
3 * Copyright (C) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 #ifndef __PARAGRAPHLAYOUT_H
10 #define __PARAGRAPHLAYOUT_H
13 * ParagraphLayout doesn't make much sense without
16 #include "unicode/uscript.h"
17 #if ! UCONFIG_NO_BREAK_ITERATION
19 #include "layout/LETypes.h"
20 #include "layout/LEFontInstance.h"
21 #include "layout/LayoutEngine.h"
22 #include "unicode/ubidi.h"
23 #include "unicode/brkiter.h"
25 #include "layout/RunArrays.h"
32 * The <code>ParagraphLayout</code> object will analyze the text into runs of text in the
33 * same font, script and direction, and will create a <code>LayoutEngine</code> object for each run.
34 * The <code>LayoutEngine</code> will transform the characters into glyph codes in visual order.
36 * Clients can use this to break a paragraph into lines, and to display the glyphs in each line.
39 class U_LAYOUTEX_API ParagraphLayout
: public UObject
45 * This class represents a single line of text in a <code>ParagraphLayout</code>. They
46 * can only be created by calling <code>ParagraphLayout::nextLine()</code>. Each line
47 * consists of multiple visual runs, represented by <code>ParagraphLayout::VisualRun</code>
50 * @see ParagraphLayout
51 * @see ParagraphLayout::VisualRun
55 class U_LAYOUTEX_API Line
: public UObject
59 * The constructor is private since these objects can only be
60 * created by <code>ParagraphLayout</code>. However, it is the
61 * clients responsibility to destroy the objects, so the destructor
69 * Count the number of visual runs in the line.
71 * @return the number of visual runs.
75 le_int32
countRuns() const;
78 * Get the ascent of the line. This is the maximum ascent
79 * of all the fonts on the line.
81 * @return the ascent of the line.
85 le_int32
getAscent() const;
88 * Get the descent of the line. This is the maximum descent
89 * of all the fonts on the line.
91 * @return the descent of the line.
95 le_int32
getDescent() const;
98 * Get the leading of the line. This is the maximum leading
99 * of all the fonts on the line.
101 * @return the leading of the line.
105 le_int32
getLeading() const;
108 * Get the width of the line. This is a convenience method
109 * which returns the last X position of the last visual run
112 * @return the width of the line.
116 le_int32
getWidth() const;
119 * Get a <code>ParagraphLayout::VisualRun</code> object for a given
120 * visual run in the line.
122 * @param runIndex is the index of the run, in visual order.
124 * @return the <code>ParagraphLayout::VisualRun</code> object representing the
125 * visual run. This object is owned by the <code>Line</code> object which
126 * created it, and will remain valid for as long as the <code>Line</code>
129 * @see ParagraphLayout::VisualRun
133 const VisualRun
*getVisualRun(le_int32 runIndex
) const;
136 * ICU "poor man's RTTI", returns a UClassID for the actual class.
140 virtual inline UClassID
getDynamicClassID() const { return getStaticClassID(); }
143 * ICU "poor man's RTTI", returns a UClassID for this class.
147 static inline UClassID
getStaticClassID() { return (UClassID
)&fgClassID
; }
152 * The address of this static class variable serves as this class's ID
153 * for ICU "poor man's RTTI".
155 static const char fgClassID
;
157 friend class ParagraphLayout
;
164 le_int32 fRunCapacity
;
169 Line(const Line
&other
);
170 Line
&operator=(const Line
& /*other*/) { return *this; };
172 void computeMetrics();
174 void append(const LEFontInstance
*font
, UBiDiDirection direction
, le_int32 glyphCount
,
175 const LEGlyphID glyphs
[], const float positions
[], const le_int32 glyphToCharMap
[]);
179 * This object represents a single visual run in a line of text in
180 * a paragraph. A visual run is text which is in the same font,
181 * script, and direction. The text is represented by an array of
182 * <code>LEGlyphIDs</code>, an array of (x, y) glyph positions and
183 * a table which maps indices into the glyph array to indices into
184 * the original character array which was used to create the paragraph.
186 * These objects are only created by <code>ParagraphLayout::Line</code> objects,
187 * so their constructors and destructors are private.
189 * @see ParagraphLayout::Line
193 class U_LAYOUTEX_API VisualRun
: public UObject
197 * Get the <code>LEFontInstance</code> object which
198 * represents the font of the visual run. This will always
199 * be a non-composite font.
201 * @return the <code>LEFontInstance</code> object which represents the
202 * font of the visual run.
204 * @see LEFontInstance
208 const LEFontInstance
*getFont() const;
211 * Get the direction of the visual run.
213 * @return the direction of the run. This will be UBIDI_LTR if the
214 * run is left-to-right and UBIDI_RTL if the line is right-to-left.
218 UBiDiDirection
getDirection() const;
221 * Get the number of glyphs in the visual run.
223 * @return the number of glyphs.
227 le_int32
getGlyphCount() const;
230 * Get the glyphs in the visual run. Glyphs with the values <code>0xFFFE</code> and
231 * <code>0xFFFF</code> should be ignored.
233 * @return the address of the array of glyphs for this visual run. The storage
234 * is owned by the <code>VisualRun</code> object and must not be deleted.
235 * It will remain valid as long as the <code>VisualRun</code> object is valid.
239 const LEGlyphID
*getGlyphs() const;
242 * Get the (x, y) positions of the glyphs in the visual run. To simplify storage
243 * management, the x and y positions are stored in a single array with the x positions
244 * at even offsets in the array and the corresponding y position in the following odd offset.
245 * There is an extra (x, y) pair at the end of the array which represents the advance of
246 * the final glyph in the run.
248 * @return the address of the array of glyph positions for this visual run. The storage
249 * is owned by the <code>VisualRun</code> object and must not be deleted.
250 * It will remain valid as long as the <code>VisualRun</code> object is valid.
254 const float *getPositions() const;
257 * Get the glyph-to-character map for this visual run. This maps the indices into
258 * the glyph array to indices into the character array used to create the paragraph.
260 * @return the address of the character-to-glyph map for this visual run. The storage
261 * is owned by the <code>VisualRun</code> object and must not be deleted.
262 * It will remain valid as long as the <code>VisualRun</code> object is valid.
266 const le_int32
*getGlyphToCharMap() const;
269 * A convenience method which returns the ascent value for the font
270 * associated with this run.
272 * @return the ascent value of this run's font.
276 le_int32
getAscent() const;
279 * A convenience method which returns the descent value for the font
280 * associated with this run.
282 * @return the descent value of this run's font.
286 le_int32
getDescent() const;
289 * A convenience method which returns the leading value for the font
290 * associated with this run.
292 * @return the leading value of this run's font.
296 le_int32
getLeading() const;
299 * ICU "poor man's RTTI", returns a UClassID for the actual class.
303 virtual inline UClassID
getDynamicClassID() const { return getStaticClassID(); }
306 * ICU "poor man's RTTI", returns a UClassID for this class.
310 static inline UClassID
getStaticClassID() { return (UClassID
)&fgClassID
; }
315 * The address of this static class variable serves as this class's ID
316 * for ICU "poor man's RTTI".
318 static const char fgClassID
;
320 const LEFontInstance
*fFont
;
321 const UBiDiDirection fDirection
;
323 const le_int32 fGlyphCount
;
325 const LEGlyphID
*fGlyphs
;
326 const float *fPositions
;
327 const le_int32
*fGlyphToCharMap
;
332 VisualRun(const VisualRun
&other
);
333 VisualRun
&operator=(const VisualRun
&other
) { return *this; };
335 VisualRun(const LEFontInstance
*font
, UBiDiDirection direction
, le_int32 glyphCount
,
336 const LEGlyphID glyphs
[], const float positions
[], const le_int32 glyphToCharMap
[]);
342 * Construct a <code>ParagraphLayout</code> object for a styled paragraph. The paragraph is specified
343 * as runs of text all in the same font. An <code>LEFontInstance</code> object and a limit offset
344 * are specified for each font run. The limit offset is the offset of the character immediately
345 * after the font run.
347 * Clients can optionally specify directional runs and / or script runs. If these aren't specified
348 * they will be computed.
350 * If any errors are encountered during construction, <code>status</code> will be set, and the object
351 * will be set to be empty.
353 * @param chars is an array of the characters in the paragraph
355 * @param count is the number of characters in the paragraph.
357 * @param fontRuns a pointer to a <code>FontRuns</code> object representing the font runs.
359 * @param levelRuns is a pointer to a <code>ValueRuns</code> object representing the directional levels.
360 * If this pointer in <code>NULL</code> the levels will be determined by running the Unicde
363 * @param scriptRuns is a pointer to a <code>ValueRuns</code> object representing script runs.
364 * If this pointer in <code>NULL</code> the script runs will be determined using the
365 * Unicode code points.
367 * @param localeRuns is a pointer to a <code>LocaleRuns</code> object representing locale runs.
368 * The <code>Locale</code> objects are used to determind the language of the text. If this
369 * pointer is <code>NULL</code> the default locale will be used for all of the text.
371 * @param paragraphLevel is the directionality of the paragraph, as in the UBiDi object.
373 * @param vertical is <code>TRUE</code> if the paragraph should be set vertically.
375 * @param status will be set to any error code encountered during construction.
378 * @see LEFontInstance.h
379 * @see LayoutEngine.h
384 ParagraphLayout(const LEUnicode chars
[], le_int32 count
,
385 const FontRuns
*fontRuns
,
386 const ValueRuns
*levelRuns
,
387 const ValueRuns
*scriptRuns
,
388 const LocaleRuns
*localeRuns
,
389 UBiDiLevel paragraphLevel
, le_bool vertical
,
390 LEErrorCode
&status
);
393 * The destructor. Virtual so that it works correctly with
400 // Note: the following is #if 0'd out because there's no good
401 // way to implement it without either calling layoutEngineFactory()
402 // or duplicating the logic there...
405 * Examine the given styled paragraph and determine if it contains any text which
406 * requires complex processing. (i.e. that cannot be correctly rendered by
407 * just mapping the characters to glyphs and rendering them in order)
409 * @param chars is an array of the characters in the paragraph
411 * @param count is the number of characters in the paragraph.
413 * @param fontRuns is a pointer to a <code>FontRuns</code> object representing the font runs.
415 * @return <code>TRUE</code> if the paragraph contains complex text.
419 static le_bool
isComplex(const LEUnicode chars
[], le_int32 count
, const FontRuns
*fontRuns
);
422 * Examine the given text and determine if it contains characters in any
423 * script which requires complex processing to be rendered correctly.
425 * @param chars is an array of the characters in the paragraph
427 * @param count is the number of characters in the paragraph.
429 * @return <code>TRUE</code> if any of the text requires complex processing.
433 static le_bool
isComplex(const LEUnicode chars
[], le_int32 count
);
438 * Return the resolved paragraph level. This is useful for those cases
439 * where the bidi analysis has determined the level based on the first
440 * strong character in the paragraph.
442 * @return the resolved paragraph level.
446 UBiDiLevel
getParagraphLevel();
449 * Return the directionality of the text in the paragraph.
451 * @return <code>UBIDI_LTR</code> if the text is all left to right,
452 * <code>UBIDI_RTL</code> if the text is all right to left,
453 * or <code>UBIDI_MIXED</code> if the text has mixed direction.
457 UBiDiDirection
getTextDirection();
460 * Return the max ascent value for all the fonts
463 * @return the ascent value.
467 virtual le_int32
getAscent() const;
470 * Return the max descent value for all the fonts
473 * @return the decent value.
477 virtual le_int32
getDescent() const;
480 * Return the max leading value for all the fonts
483 * @return the leading value.
487 virtual le_int32
getLeading() const;
490 * Reset line breaking to start from the beginning of the paragraph.
498 * Return a <code>ParagraphLayout::Line</code> object which represents next line
499 * in the paragraph. The width of the line is specified each time so that it can
500 * be varied to support arbitrary paragraph shapes.
502 * @param width is the width of the line. If <code>width</code> is less than or equal
503 * to zero, a <code>ParagraphLayout::Line</code> object representing the
504 * rest of the paragraph will be returned.
506 * @return a <code>ParagraphLayout::Line</code> object which represents the line. The caller
507 * is responsible for deleting the object. Returns <code>NULL</code> if there are no
508 * more lines in the paragraph.
510 * @see ParagraphLayout::Line
514 Line
*nextLine(float width
);
517 * ICU "poor man's RTTI", returns a UClassID for the actual class.
521 virtual inline UClassID
getDynamicClassID() const { return getStaticClassID(); }
524 * ICU "poor man's RTTI", returns a UClassID for this class.
528 static inline UClassID
getStaticClassID() { return (UClassID
)&fgClassID
; }
534 * The address of this static class variable serves as this class's ID
535 * for ICU "poor man's RTTI".
537 static const char fgClassID
;
541 LayoutEngine
*engine
;
542 const LEFontInstance
*font
;
543 const Locale
*locale
;
554 ParagraphLayout() {};
555 ParagraphLayout(const ParagraphLayout
& /*other*/) : UObject( ){};
556 ParagraphLayout
&operator=(const ParagraphLayout
& /*other*/) { return *this; };
558 void computeLevels(UBiDiLevel paragraphLevel
);
560 Line
*computeVisualRuns();
561 void appendRun(Line
*line
, le_int32 run
, le_int32 firstChar
, le_int32 lastChar
);
563 void computeScripts();
565 void computeLocales();
567 void computeSubFonts(const FontRuns
*fontRuns
, LEErrorCode
&status
);
569 void computeMetrics();
571 le_int32
getLanguageCode(const Locale
*locale
);
573 le_int32
getCharRun(le_int32 charIndex
);
575 static le_bool
isComplex(UScriptCode script
);
577 le_int32
previousBreak(le_int32 charIndex
);
580 const LEUnicode
*fChars
;
583 const FontRuns
*fFontRuns
;
584 const ValueRuns
*fLevelRuns
;
585 const ValueRuns
*fScriptRuns
;
586 const LocaleRuns
*fLocaleRuns
;
589 le_bool fClientLevels
;
590 le_bool fClientScripts
;
591 le_bool fClientLocales
;
593 UBiDiLevel
*fEmbeddingLevels
;
599 le_int32
*fGlyphToCharMap
;
600 le_int32
*fCharToMinGlyphMap
;
601 le_int32
*fCharToMaxGlyphMap
;
603 le_int32 fGlyphCount
;
608 le_int32
*fStyleRunLimits
;
609 le_int32
*fStyleIndices
;
610 StyleRunInfo
*fStyleRunInfo
;
611 le_int32 fStyleRunCount
;
613 BreakIterator
*fBreakIterator
;
617 le_int32 fFirstVisualRun
;
618 le_int32 fLastVisualRun
;
619 float fVisualRunLastX
;
620 float fVisualRunLastY
;
623 inline UBiDiLevel
ParagraphLayout::getParagraphLevel()
625 return ubidi_getParaLevel(fParaBidi
);
628 inline UBiDiDirection
ParagraphLayout::getTextDirection()
630 return ubidi_getDirection(fParaBidi
);
633 inline void ParagraphLayout::reflow()
638 inline ParagraphLayout::Line::Line()
639 : UObject(), fAscent(0), fDescent(0), fLeading(0), fRunCount(0), fRunCapacity(0), fRuns(NULL
)
641 // nothing else to do
644 inline ParagraphLayout::Line::Line(const Line
& /*other*/)
645 : UObject(), fAscent(0), fDescent(0), fLeading(0), fRunCount(0), fRunCapacity(0), fRuns(NULL
)
647 // nothing else to do
650 inline le_int32
ParagraphLayout::Line::countRuns() const
655 inline const LEFontInstance
*ParagraphLayout::VisualRun::getFont() const
660 inline UBiDiDirection
ParagraphLayout::VisualRun::getDirection() const
665 inline le_int32
ParagraphLayout::VisualRun::getGlyphCount() const
670 inline const LEGlyphID
*ParagraphLayout::VisualRun::getGlyphs() const
675 inline const float *ParagraphLayout::VisualRun::getPositions() const
680 inline const le_int32
*ParagraphLayout::VisualRun::getGlyphToCharMap() const
682 return fGlyphToCharMap
;
685 inline le_int32
ParagraphLayout::VisualRun::getAscent() const
687 return fFont
->getAscent();
690 inline le_int32
ParagraphLayout::VisualRun::getDescent() const
692 return fFont
->getDescent();
695 inline le_int32
ParagraphLayout::VisualRun::getLeading() const
697 return fFont
->getLeading();
700 inline ParagraphLayout::VisualRun::VisualRun()
701 : UObject(), fFont(NULL
), fDirection(UBIDI_LTR
), fGlyphCount(0), fGlyphs(NULL
), fPositions(NULL
), fGlyphToCharMap(NULL
)
706 inline ParagraphLayout::VisualRun::VisualRun(const VisualRun
&other
)
707 : UObject(), fFont(NULL
), fDirection(UBIDI_LTR
), fGlyphCount(0), fGlyphs(NULL
), fPositions(NULL
), fGlyphToCharMap(NULL
)
712 inline ParagraphLayout::VisualRun::VisualRun(const LEFontInstance
*font
, UBiDiDirection direction
, le_int32 glyphCount
,
713 const LEGlyphID glyphs
[], const float positions
[], const le_int32 glyphToCharMap
[])
714 : fFont(font
), fDirection(direction
), fGlyphCount(glyphCount
),
715 fGlyphs(glyphs
), fPositions(positions
), fGlyphToCharMap(glyphToCharMap
)
717 // nothing else needs to be done!