epoc32/include/mw/aknbiditextutils.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    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.
    19 *
    20 *
    21 */
    22 
    23 
    24 
    25 #ifndef AKN_BIDI_TEXT_UTILS_H
    26 #define AKN_BIDI_TEXT_UTILS_H
    27 
    28 //  INCLUDES
    29 #include <e32base.h>
    30 #include <uikon.hrh> // KEllipsis
    31 #include <gdi.h>     // For CCFont::TMeasureTextInput::TFlags
    32 
    33 // CONSTANTS
    34 
    35 const TInt KAknBidiExtraSpacePerLine = 4;
    36 
    37 // FORWARD DECLARATIONS
    38 
    39 
    40 // CLASS DECLARATION
    41 
    42 /**
    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.
    47 *
    48 * Input text buffers given as parameters must contain extra space for some additional
    49 * characters as stated in method descriptions.
    50 *
    51 * @lib avkon.dll
    52 * @since 2.0
    53 */
    54 class AknBidiTextUtils
    55     {
    56     public:
    57 
    58     /**
    59     * Overall paragraph directionality
    60     */
    61     enum TParagraphDirectionality
    62         {
    63         EImplicit = 0, // implicit directionality of input text
    64         ELeftToRight = 1,
    65         ERightToLeft = 2
    66         };
    67 
    68     public: // New functions
    69 
    70     /** 
    71     * Generic text clipping. This method allocates memory for visual buffer,
    72     * and can leave in OOM situations.
    73     *
    74     * @since 2.0
    75     *   
    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.
    80     *
    81     * @param aFont          Font used when drawing the text.
    82     *
    83     * @param aMaxWidthInPixels Maximum width of text that is not be clipped.
    84     *
    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.
    88     *
    89     * @param aDirectionality Paragraph directionality.
    90     *
    91     * @param aClipChar      The truncation character.
    92     *                       0xFFFF if no truncation character is to be used.
    93     * 
    94     * @return ETrue if the text was clipped, EFalse otherwise.
    95     */
    96     IMPORT_C static TBool ConvertToVisualAndClipL(
    97         TDes& aLogicalText,
    98         const CFont& aFont,
    99         TInt aMaxWidthInPixels,
   100         TInt aMaxClippedWidthInPixels,
   101         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit,
   102         TChar aClipChar = KEllipsis );
   103 
   104     /** 
   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.
   108     *
   109     * @since 2.0
   110     *   
   111     * @param aLogicalText   String to be clipped in logical order.
   112     *
   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.
   119     *
   120     * @param aFont          Font used when drawing the text.
   121     *
   122     * @param aMaxWidthInPixels Maximum width of text that is not clipped.
   123     *
   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.
   127     *
   128     * @param aDirectionality Paragraph directionality.
   129     *
   130     * @param aClipChar      The truncation character.
   131     *                       0xFFFF if no truncation character is to be used.
   132     * 
   133     * @return ETrue if the text was clipped, EFalse otherwise.
   134     */
   135     IMPORT_C static TBool ConvertToVisualAndClip(
   136         const TDesC& aLogicalText,
   137         TDes& aVisualText,
   138         const CFont& aFont,
   139         TInt aMaxWidthInPixels,
   140         TInt aMaxClippedWidthInPixels,
   141         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit,
   142         TChar aClipChar = KEllipsis );
   143 
   144     /**
   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).
   147     *
   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().
   153     *
   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.
   156     *
   157     * @since 2.0
   158     *   
   159     * @param aLogicalText Text in logical order.
   160     *
   161     * @return KErrNone or KErrNoMemory.
   162     */
   163     IMPORT_C static TInt PrepareRunInfoArray( const TDesC& aLogicalText );
   164 
   165     /**
   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.
   170     *
   171     * @since 2.0
   172     *   
   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.
   181     *
   182     * @param aLineWidthArray    Line widths in pixels. Also determines maximum
   183     *                           number of lines.
   184     *
   185     * @param aFont              Used font.
   186     *
   187     * @param aWrappedArray      Pointers to wrapped lines.
   188     *
   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.
   192     *
   193     * @param aDirectionality    Paragraph directionality.
   194     */
   195     IMPORT_C static void ConvertToVisualAndWrapToArrayL(
   196         TDes& aLogicalText,
   197         const CArrayFix<TInt>& aLineWidthArray,
   198         const CFont& aFont,
   199         CArrayFix<TPtrC>& aWrappedArray,
   200         TBool aInsertTruncationChar,
   201         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
   202 
   203     /**
   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.
   207     *
   208     * @since 2.0
   209     *   
   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
   214     *                           order.)
   215     *                           These lines can then be renderered using e.g.
   216     *                           CGraphicsContext::DrawText().
   217     *
   218     * @param aLineWidth         Constant line width in pixels.
   219     *
   220     * @param aFont              Used font.
   221     *
   222     * @param aWrappedArray      Pointers to wrapped lines.
   223     *
   224     * @param aDirectionality    Paragraph directionality.
   225     *
   226     * @return Heap descriptor containing the wrapped string in visual form.
   227     *         Ownership is transferred to the caller.
   228     */
   229     IMPORT_C static HBufC* ConvertToVisualAndWrapToArrayL( 
   230         const TDesC& aLogicalText,
   231         TInt aLineWidth,
   232         const CFont& aFont,
   233         CArrayFix<TPtrC>& aWrappedArray,
   234         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
   235     
   236     /**
   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.
   243     *
   244     * @since 2.0
   245     *   
   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.
   255     *
   256     * @param aLineWidthArray    Line widths in pixels. Also determines maximum
   257     *                           number of lines.
   258     *
   259     * @param aFont              Used font.
   260     *
   261     * @param aChoppedArray      Pointers to chopped lines.
   262     *
   263     * @param aDirectionality    Paragraph directionality.
   264     */
   265     IMPORT_C static void ConvertToVisualAndChopToArrayL(
   266         TDes& aLogicalText,
   267         const CArrayFix<TInt>& aLineWidthArray, 
   268         const CFont& aFont,                    
   269         CArrayFix<TPtrC>& aChoppedArray,
   270         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
   271 
   272     /**
   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.
   279     *
   280     * @since 2.0
   281     *   
   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
   287     *                           order.)
   288     *                           These lines can then be renderered using e.g.
   289     *                           CGraphicsContext::DrawText().
   290     *
   291     * @param aLineWidth         Constant line width in pixels
   292     *
   293     * @param aFont              Used font.
   294     *
   295     * @param aChoppedArray      Pointers to chopped lines.
   296     *
   297     * @param aDirectionality Paragraph directionality.
   298     *
   299     * @return Heap descriptor containing the chopped string in visual form.
   300     *         Ownership is transferred to the caller.
   301     */
   302     IMPORT_C static HBufC* ConvertToVisualAndChopToArrayL(
   303         const TDesC& aLogicalText,
   304         TInt aLineWidth,
   305         const CFont& aFont,
   306         CArrayFix<TPtrC>& aChoppedArray,
   307         TParagraphDirectionality aDirectionality = EImplicit );
   308 
   309     /**
   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.
   313     *
   314     * @since 2.0
   315     *   
   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().
   322     *                           
   323     * @param aLineWidthArray    Line widths in pixels. Also determines maximum
   324     *                           number of lines.
   325     *
   326     * @param aFont              Used font.
   327     *
   328     * @param aWrappedString     Wrapped string. The maximum length of the descriptor
   329     *                           must be at least
   330     *                   ( aLogicalText.Length() +
   331     *                     aLineWidthArray.Count() * (KAknBidiExtraSpacePerLine+1) ).
   332     *                           Otherwise, a panic is raised.
   333     *
   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.
   337     *
   338     * @param aDirectionality    Paragraph directionality.
   339     */
   340     IMPORT_C static void ConvertToVisualAndWrapToStringL( 
   341         const TDesC& aLogicalText,
   342         const CArrayFix<TInt>& aLineWidthArray,
   343         const CFont& aFont,
   344         TDes& aWrappedString,
   345         TBool aInsertTruncationChar,
   346         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
   347 
   348     // -------------------------------------------------------------------------
   349     // The following methods are introduced in release 2.1
   350     // -------------------------------------------------------------------------
   351 
   352     /**
   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.
   358     *
   359     * @since 2.1
   360     *
   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
   365     *                           order.)
   366     *                           These lines can then be renderered using e.g.
   367     *                           CGraphicsContext::DrawText().
   368     *
   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
   372     *                           lines.
   373     *
   374     * @param aFont              Used font.
   375     *
   376     * @param aWrappedArray      Pointers to wrapped lines.
   377     *
   378     * @param aDirectionality    Paragraph directionality.
   379     *
   380     * @return Heap descriptor containing the wrapped string in visual form.
   381     *         Ownership is transferred to the caller.
   382     */
   383     IMPORT_C static HBufC* ConvertToVisualAndWrapToArrayWholeTextL( 
   384         const TDesC& aLogicalText,
   385         const CArrayFix<TInt>& aLineWidthArray,
   386         const CFont& aFont,
   387         CArrayFix<TPtrC>& aWrappedArray,
   388         AknBidiTextUtils::TParagraphDirectionality aDirectionality = EImplicit );
   389 
   390     /**
   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. 
   394     * 
   395     * It cannot be used for vertical text measurement.
   396     * 
   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. 
   401     *
   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.
   404     * 
   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
   407     * exact use case.
   408     * 
   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>)
   413     *   
   414     * This method should be used when the proposed text is going to be drawn using any horizontal
   415     * CGraphicsContext::DrawText drawing API. 
   416     *
   417     * The text can be in visual or logical order.
   418     *
   419     * @since 3.1
   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.
   424     */
   425     IMPORT_C static TInt AknBidiTextUtils::MeasureTextBoundsWidth(
   426         const CFont& aFont,
   427         const TDesC& aText,
   428         CFont::TMeasureTextInput::TFlags aOrder);
   429 
   430     private:
   431 
   432         static TBool DoConvertToVisualAndClip(
   433             const TDesC& aLogicalText,
   434             TDes& aVisualText,
   435             const CFont& aFont,
   436             TInt aMaxWidthInPixels,
   437             TInt aMaxClippedWidthInPixels,
   438             AknBidiTextUtils::TParagraphDirectionality aDirectionality,
   439             TChar aClipChar );
   440 
   441         /**
   442         * C++ default constructor.
   443         */
   444         AknBidiTextUtils();
   445     };
   446 
   447 #endif      // AKN_BIDI_TEXT_UTILS_H
   448             
   449 // End of File