2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Text utilities e.g. truncating and wrapping to be used in code
15 * that needs to support text that requires conversion from logical
16 * to visual order, e.g. Arabic/Hebrew.
17 * Logical text is given as input to all methods.
18 * Output text is in visual order.
25 #ifndef AKN_BIDI_TEXT_UTILS_H
26 #define AKN_BIDI_TEXT_UTILS_H
30 #include <uikon.hrh> // KEllipsis
31 #include <gdi.h> // For CCFont::TMeasureTextInput::TFlags
35 const TInt KAknBidiExtraSpacePerLine = 4;
37 // FORWARD DECLARATIONS
43 * Text utilities e.g. truncating and wrapping to be used in code that needs to
44 * support text that requires conversion from logical to visual order,
45 * e.g. Arabic/Hebrew Logical text is given as input to all
46 * methods. Output text is in visual order.
48 * Input text buffers given as parameters must contain extra space for some additional
49 * characters as stated in method descriptions.
54 class AknBidiTextUtils
59 * Overall paragraph directionality
61 enum TParagraphDirectionality
63 EImplicit = 0, // implicit directionality of input text
68 public: // New functions
71 * Generic text clipping. This method allocates memory for visual buffer,
72 * and can leave in OOM situations.
76 * @param aLogicalText String to be clipped in logical order. This method
77 * converts it in visual order and clips it if necessary.
78 * The descriptor must contain extra space of at least
79 * KAknBidiExtraSpacePerLine. Otherwise, a panic is raised.
81 * @param aFont Font used when drawing the text.
83 * @param aMaxWidthInPixels Maximum width of text that is not be clipped.
85 * @param aMaxClippedWidthInPixels Maximum width of text that is clipped.
86 * Note that this should not be many pixels bigger than aMaxWidthInPixels
87 * because then truncation character could appear after fully fitting text.
89 * @param aDirectionality Paragraph directionality.
91 * @param aClipChar The truncation character.
92 * 0xFFFF if no truncation character is to be used.
94 * @return ETrue if the text was clipped, EFalse otherwise.
96 IMPORT_C static TBool ConvertToVisualAndClipL(
99 TInt aMaxWidthInPixels,
100 TInt aMaxClippedWidthInPixels,
101 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit,
102 TChar aClipChar = KEllipsis );
105 * Generic text clipping. This method does not allocate memory and can
106 * be used in non-leaving functions. You must give a preallocated visual
107 * buffer as a parameter.
111 * @param aLogicalText String to be clipped in logical order.
113 * @param aVisualText The reordered text in visual form is returned here.
114 * This method converts aLogicalText into visual order
115 * and clips it if necessary. Maximum length of this
116 * descriptor must be at least
117 * aLogicalText.Length() + KAknBidiExtraSpacePerLine.
118 * otherwise a panic is raised.
120 * @param aFont Font used when drawing the text.
122 * @param aMaxWidthInPixels Maximum width of text that is not clipped.
124 * @param aMaxClippedWidthInPixels Maximum width of text that is clipped.
125 * Note that this should not be many pixels bigger than aMaxWidthInPixels
126 * because then truncation character could appear after fully fitting text.
128 * @param aDirectionality Paragraph directionality.
130 * @param aClipChar The truncation character.
131 * 0xFFFF if no truncation character is to be used.
133 * @return ETrue if the text was clipped, EFalse otherwise.
135 IMPORT_C static TBool ConvertToVisualAndClip(
136 const TDesC& aLogicalText,
139 TInt aMaxWidthInPixels,
140 TInt aMaxClippedWidthInPixels,
141 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit,
142 TChar aClipChar = KEllipsis );
145 * Tests how big run info array the given text requires and
146 * tries to increase the size of the array if required (stored in CAknEnv).
148 * This method is useful with the non-leaving version of
149 * ConvertToVisualAndClip(). By calling this method successfully
150 * (means KErrNone is returned) at any point during the lifetime of the application
151 * it is guaranteed that the text gets correctly converted in visual form even
152 * in out-of-memory situation when calling ConvertToVisualAndClip().
154 * Note that there is no use calling this method if you do not handle the
155 * return code, because the truncating methods already do that.
159 * @param aLogicalText Text in logical order.
161 * @return KErrNone or KErrNoMemory.
163 IMPORT_C static TInt PrepareRunInfoArray( const TDesC& aLogicalText );
166 * Converts a string in visual order and wraps it to an array of pointers.
167 * The maximum number of lines and line widths are specified in
168 * aLineWidthArray. The pointers in aWrappedArray are set to point to
169 * positions inside descriptor given in aLogicalText.
173 * @param aLogicalText Logical text to be wrapped. This method
174 * converts it in visual form (in strictly left to
175 * right order) and wraps it to lines.
176 * These lines can then be renderered using e.g.
177 * CGraphicsContext::DrawText().
178 * The descriptor must contain extra space of at least
179 * (aLineWidthArray.Count() * KAknBidiExtraSpacePerLine).
180 * Otherwise, a panic is raised.
182 * @param aLineWidthArray Line widths in pixels. Also determines maximum
185 * @param aFont Used font.
187 * @param aWrappedArray Pointers to wrapped lines.
189 * @param aInsertTruncationChar Whether to insert truncation character
190 * (KEllipsis) or not if the text does not fit in
191 * given maximum number of lines.
193 * @param aDirectionality Paragraph directionality.
195 IMPORT_C static void ConvertToVisualAndWrapToArrayL(
197 const CArrayFix<TInt>& aLineWidthArray,
199 CArrayFix<TPtrC>& aWrappedArray,
200 TBool aInsertTruncationChar,
201 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
204 * Converts a string in visual order and wraps it to an array of pointers.
205 * Constant line width is given. The pointers in aWrappedArray are set to
206 * point to positions inside the returned heap descriptor.
210 * @param aLogicalText Logical text to be wrapped. This method
211 * converts it in visual form (in strictly left to
212 * right order) and wraps it to lines. (The returned
213 * heap descriptor contains the string in visual
215 * These lines can then be renderered using e.g.
216 * CGraphicsContext::DrawText().
218 * @param aLineWidth Constant line width in pixels.
220 * @param aFont Used font.
222 * @param aWrappedArray Pointers to wrapped lines.
224 * @param aDirectionality Paragraph directionality.
226 * @return Heap descriptor containing the wrapped string in visual form.
227 * Ownership is transferred to the caller.
229 IMPORT_C static HBufC* ConvertToVisualAndWrapToArrayL(
230 const TDesC& aLogicalText,
233 CArrayFix<TPtrC>& aWrappedArray,
234 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
237 * Converts a string in visual order and chops each line when a line break
238 * character is encountered.
239 * Clips at the end of each line if there isn't enough space
240 * on that line. When clipping, KEllipsis (shown as 3 dots) is inserted at
241 * the end of the line. The pointers in aChoppedArray are set to point to
242 * positions inside aLogicalText.
246 * @param aLogicalText Logical text to be chopped. This method
247 * converts it in visual form (in strictly left to
248 * right order) and chops each line when a line break
249 * character is encountered.
250 * These lines can then be renderered using e.g.
251 * CGraphicsContext::DrawText().
252 * The descriptor must contain extra space of at least
253 * (aLineWidthArray.Count() * KAknBidiExtraSpacePerLine).
254 * Otherwise, a panic is raised.
256 * @param aLineWidthArray Line widths in pixels. Also determines maximum
259 * @param aFont Used font.
261 * @param aChoppedArray Pointers to chopped lines.
263 * @param aDirectionality Paragraph directionality.
265 IMPORT_C static void ConvertToVisualAndChopToArrayL(
267 const CArrayFix<TInt>& aLineWidthArray,
269 CArrayFix<TPtrC>& aChoppedArray,
270 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
273 * Converts a string in visual order and chops each line when a line break
274 * character is encountered.
275 * Clips at the end of each line if there isn't enough space
276 * on that line. When clipping, KEllipsis (shown as 3 dots) is inserted at
277 * the end of the line. The pointers in aChoppedArray are set to point to
278 * positions inside the returned heap descriptor.
282 * @param aLogicalText Logical text to be chopped. This method
283 * converts it in visual form (in strictly left to
284 * right order) and chops each line when a line break
285 * character is encountered. (The returned
286 * heap descriptor contains the string in visual
288 * These lines can then be renderered using e.g.
289 * CGraphicsContext::DrawText().
291 * @param aLineWidth Constant line width in pixels
293 * @param aFont Used font.
295 * @param aChoppedArray Pointers to chopped lines.
297 * @param aDirectionality Paragraph directionality.
299 * @return Heap descriptor containing the chopped string in visual form.
300 * Ownership is transferred to the caller.
302 IMPORT_C static HBufC* ConvertToVisualAndChopToArrayL(
303 const TDesC& aLogicalText,
306 CArrayFix<TPtrC>& aChoppedArray,
307 TParagraphDirectionality aDirectionality = EImplicit );
310 * Converts a string in visual order and wraps it to lines by inserting
311 * '\n' after each line in text.
312 * The result is copied into aWrappedString.
316 * @param aLogicalText Logical text to be wrapped. This method
317 * converts it in visual form (in strictly left to
318 * right order) and wraps it to lines in aWrappedString.
319 * '\n' is inserted after each line.
320 * These lines can then be renderered using e.g.
321 * CGraphicsContext::DrawText().
323 * @param aLineWidthArray Line widths in pixels. Also determines maximum
326 * @param aFont Used font.
328 * @param aWrappedString Wrapped string. The maximum length of the descriptor
330 * ( aLogicalText.Length() +
331 * aLineWidthArray.Count() * (KAknBidiExtraSpacePerLine+1) ).
332 * Otherwise, a panic is raised.
334 * @param aInsertTruncationChar Whether to insert truncation character
335 * (KEllipsis) or not if the text does not fit in
336 * given maximum number of lines.
338 * @param aDirectionality Paragraph directionality.
340 IMPORT_C static void ConvertToVisualAndWrapToStringL(
341 const TDesC& aLogicalText,
342 const CArrayFix<TInt>& aLineWidthArray,
344 TDes& aWrappedString,
345 TBool aInsertTruncationChar,
346 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
348 // -------------------------------------------------------------------------
349 // The following methods are introduced in release 2.1
350 // -------------------------------------------------------------------------
353 * Converts a string in visual order and wraps it to an array of pointers.
354 * Line widths are specified in aLineWidthArray. If all the text does not fit
355 * in the amount of lines in aLineWidthArray, the last line width in the array
356 * is used for all the remaining lines. The pointers in aWrappedArray are set
357 * to point to positions inside the returned heap descriptor.
361 * @param aLogicalText Logical text to be wrapped. This method
362 * converts it in visual form (in strictly left to
363 * right order) and wraps it to lines. (The returned
364 * heap descriptor contains the string in visual
366 * These lines can then be renderered using e.g.
367 * CGraphicsContext::DrawText().
369 * @param aLineWidthArray Line widths in pixels. If the whole text does
370 * not fit in the number of lines specified in the
371 * array, the last width is used for the remaining
374 * @param aFont Used font.
376 * @param aWrappedArray Pointers to wrapped lines.
378 * @param aDirectionality Paragraph directionality.
380 * @return Heap descriptor containing the wrapped string in visual form.
381 * Ownership is transferred to the caller.
383 IMPORT_C static HBufC* ConvertToVisualAndWrapToArrayWholeTextL(
384 const TDesC& aLogicalText,
385 const CArrayFix<TInt>& aLineWidthArray,
387 CArrayFix<TPtrC>& aWrappedArray,
388 AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
391 * Measures the full horizontal width in pixels of the passed-in text using a particular font,
392 * including in the width any side-bearings of the glyphs at the ends of the text, and any
393 * declared "advance" of the run of glyphs.
395 * It cannot be used for vertical text measurement.
397 * Side-bearings are parts of glyphs that extend left or right from the normal width
398 * or "advance" of the glyph. A left side-bearing, for instance, will overlap with a glyph
399 * to its left. Another way of thinking about this is that the origin (0,0) of the glyph is
400 * not at its bottom left.
402 * The advance of a run of glyphs is the sum of the advances - once in visual ordering and
403 * shaping has been performed - of all the glyphs. It is defined relative to a drawing origin.
405 * Within runs of text, side-bearings do not contribute to the width of the text. However,
406 * at the (visual) ends of text, they are likely to need to be counted, depending upon the
409 * This method returns the width of the horizontal envelope of the text by taking the extreme
410 * horizontal extents of the text bounds rectangle (which includes side-bearings on either end)
411 * and the extent of the advance. Thus it returns the width of :
412 * Min(<left_text_bound>, 0), Max( <right_text_bound>, <advance>)
414 * This method should be used when the proposed text is going to be drawn using any horizontal
415 * CGraphicsContext::DrawText drawing API.
417 * The text can be in visual or logical order.
420 * @param aFont Font to use
421 * @param aText Text to be measured
422 * @param aOrder Whether the text provided is in visual or logical order
423 * @return width of the text in pixels.
425 IMPORT_C static TInt AknBidiTextUtils::MeasureTextBoundsWidth(
428 CFont::TMeasureTextInput::TFlags aOrder);
432 static TBool DoConvertToVisualAndClip(
433 const TDesC& aLogicalText,
436 TInt aMaxWidthInPixels,
437 TInt aMaxClippedWidthInPixels,
438 AknBidiTextUtils::TParagraphDirectionality aDirectionality,
442 * C++ default constructor.
447 #endif // AKN_BIDI_TEXT_UTILS_H