LCOV - code coverage report
Current view: directory - objdir/dist/include - nsLineBreaker.h (source / functions) Found Hit Coverage
Test: app.info Lines: 11 0 0.0 %
Date: 2012-06-02 Functions: 6 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Novell code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Novell.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2006
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *         Robert O'Callahan <robert@ocallahan.org> (Original Author)
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #ifndef NSLINEBREAKER_H_
      39                 : #define NSLINEBREAKER_H_
      40                 : 
      41                 : #include "nsString.h"
      42                 : #include "nsTArray.h"
      43                 : #include "nsILineBreaker.h"
      44                 : 
      45                 : class nsIAtom;
      46                 : class nsHyphenator;
      47                 : 
      48                 : /**
      49                 :  * A receiver of line break data.
      50                 :  */
      51               0 : class nsILineBreakSink {
      52                 : public:
      53                 :   /**
      54                 :    * Sets the break data for a substring of the associated text chunk.
      55                 :    * One or more of these calls will be performed; the union of all substrings
      56                 :    * will cover the entire text chunk. Substrings may overlap (i.e., we may
      57                 :    * set the break-before state of a character more than once).
      58                 :    * @param aBreakBefore the break-before states for the characters in the substring.
      59                 :    * These are enum values from gfxTextRun::CompressedGlyph:
      60                 :    *    FLAG_BREAK_TYPE_NONE     - no linebreak is allowed here
      61                 :    *    FLAG_BREAK_TYPE_NORMAL   - a normal (whitespace) linebreak
      62                 :    *    FLAG_BREAK_TYPE_HYPHEN   - a hyphenation point
      63                 :    */
      64                 :   virtual void SetBreaks(PRUint32 aStart, PRUint32 aLength, PRUint8* aBreakBefore) = 0;
      65                 :   
      66                 :   /**
      67                 :    * Indicates which characters should be capitalized. Only called if
      68                 :    * BREAK_NEED_CAPITALIZATION was requested.
      69                 :    */
      70                 :   virtual void SetCapitalization(PRUint32 aStart, PRUint32 aLength, bool* aCapitalize) = 0;
      71                 : };
      72                 : 
      73                 : /**
      74                 :  * A line-breaking state machine. You feed text into it via AppendText calls
      75                 :  * and it computes the possible line breaks. Because break decisions can
      76                 :  * require a lot of context, the breaks for a piece of text are sometimes not
      77                 :  * known until later text has been seen (or all text ends). So breaks are
      78                 :  * returned via a call to SetBreaks on the nsILineBreakSink object passed
      79                 :  * with each text chunk, which might happen during the corresponding AppendText
      80                 :  * call, or might happen during a later AppendText call or even a Reset()
      81                 :  * call.
      82                 :  * 
      83                 :  * The linebreak results MUST NOT depend on how the text is broken up
      84                 :  * into AppendText calls.
      85                 :  * 
      86                 :  * The current strategy is that we break the overall text into
      87                 :  * whitespace-delimited "words". Then those words are passed to the nsILineBreaker
      88                 :  * service for deeper analysis if they contain a "complex" character as described
      89                 :  * below.
      90                 :  * 
      91                 :  * This class also handles detection of which characters should be capitalized
      92                 :  * for text-transform:capitalize. This is a good place to handle that because
      93                 :  * we have all the context we need.
      94                 :  */
      95                 : class nsLineBreaker {
      96                 : public:
      97                 :   nsLineBreaker();
      98                 :   ~nsLineBreaker();
      99                 :   
     100               0 :   static inline bool IsSpace(PRUnichar u) { return NS_IsSpace(u); }
     101                 : 
     102               0 :   static inline bool IsComplexASCIIChar(PRUnichar u)
     103                 :   {
     104                 :     return !((0x0030 <= u && u <= 0x0039) ||
     105                 :              (0x0041 <= u && u <= 0x005A) ||
     106                 :              (0x0061 <= u && u <= 0x007A) ||
     107               0 :              (0x000a == u));
     108                 :   }
     109                 : 
     110               0 :   static inline bool IsComplexChar(PRUnichar u)
     111                 :   {
     112               0 :     return IsComplexASCIIChar(u) ||
     113               0 :            NS_NeedsPlatformNativeHandling(u) ||
     114                 :            (0x1100 <= u && u <= 0x11ff) || // Hangul Jamo
     115                 :            (0x2000 <= u && u <= 0x21ff) || // Punctuations and Symbols
     116                 :            (0x2e80 <= u && u <= 0xd7ff) || // several CJK blocks
     117                 :            (0xf900 <= u && u <= 0xfaff) || // CJK Compatibility Idographs
     118               0 :            (0xff00 <= u && u <= 0xffef);   // Halfwidth and Fullwidth Forms
     119                 :   }
     120                 : 
     121                 :   // Break opportunities exist at the end of each run of breakable whitespace
     122                 :   // (see IsSpace above). Break opportunities can also exist between pairs of
     123                 :   // non-whitespace characters, as determined by nsILineBreaker. We pass a whitespace-
     124                 :   // delimited word to nsILineBreaker if it contains at least one character
     125                 :   // matching IsComplexChar.
     126                 :   // We provide flags to control on a per-chunk basis where breaks are allowed.
     127                 :   // At any character boundary, exactly one text chunk governs whether a
     128                 :   // break is allowed at that boundary.
     129                 :   //
     130                 :   // We operate on text after whitespace processing has been applied, so
     131                 :   // other characters (e.g. tabs and newlines) may have been converted to
     132                 :   // spaces.
     133                 : 
     134                 :   /**
     135                 :    * Flags passed with each chunk of text.
     136                 :    */
     137                 :   enum {
     138                 :     /*
     139                 :      * Do not introduce a break opportunity at the start of this chunk of text.
     140                 :      */
     141                 :     BREAK_SUPPRESS_INITIAL = 0x01,
     142                 :     /**
     143                 :      * Do not introduce a break opportunity in the interior of this chunk of text.
     144                 :      * Also, whitespace in this chunk is treated as non-breakable.
     145                 :      */
     146                 :     BREAK_SUPPRESS_INSIDE = 0x02,
     147                 :     /**
     148                 :      * The sink currently is already set up to have no breaks in it;
     149                 :      * if no breaks are possible, nsLineBreaker does not need to call
     150                 :      * SetBreaks on it. This is useful when handling large quantities of
     151                 :      * preformatted text; the textruns will never have any breaks set on them,
     152                 :      * and there is no need to ever actually scan the text for breaks, except
     153                 :      * at the end of textruns in case context is needed for following breakable
     154                 :      * text.
     155                 :      */
     156                 :     BREAK_SKIP_SETTING_NO_BREAKS = 0x04,
     157                 :     /**
     158                 :      * We need to be notified of characters that should be capitalized
     159                 :      * (as in text-transform:capitalize) in this chunk of text.
     160                 :      */
     161                 :     BREAK_NEED_CAPITALIZATION = 0x08,
     162                 :     /**
     163                 :      * Auto-hyphenation is enabled, so we need to get a hyphenator
     164                 :      * (if available) and use it to find breakpoints.
     165                 :      */
     166                 :     BREAK_USE_AUTO_HYPHENATION = 0x10
     167                 :   };
     168                 : 
     169                 :   /**
     170                 :    * Append "invisible whitespace". This acts like whitespace, but there is
     171                 :    * no actual text associated with it. Only the BREAK_SUPPRESS_INSIDE flag
     172                 :    * is relevant here.
     173                 :    */
     174                 :   nsresult AppendInvisibleWhitespace(PRUint32 aFlags);
     175                 : 
     176                 :   /**
     177                 :    * Feed Unicode text into the linebreaker for analysis. aLength must be
     178                 :    * nonzero.
     179                 :    * @param aSink can be null if the breaks are not actually needed (we may
     180                 :    * still be setting up state for later breaks)
     181                 :    */
     182                 :   nsresult AppendText(nsIAtom* aLangGroup, const PRUnichar* aText, PRUint32 aLength,
     183                 :                       PRUint32 aFlags, nsILineBreakSink* aSink);
     184                 :   /**
     185                 :    * Feed 8-bit text into the linebreaker for analysis. aLength must be nonzero.
     186                 :    * @param aSink can be null if the breaks are not actually needed (we may
     187                 :    * still be setting up state for later breaks)
     188                 :    */
     189                 :   nsresult AppendText(nsIAtom* aLangGroup, const PRUint8* aText, PRUint32 aLength,
     190                 :                       PRUint32 aFlags, nsILineBreakSink* aSink);
     191                 :   /**
     192                 :    * Reset all state. This means the current run has ended; any outstanding
     193                 :    * calls through nsILineBreakSink are made, and all outstanding references to
     194                 :    * nsILineBreakSink objects are dropped.
     195                 :    * After this call, this linebreaker can be reused.
     196                 :    * This must be called at least once between any call to AppendText() and
     197                 :    * destroying the object.
     198                 :    * @param aTrailingBreak this is set to true when there is a break opportunity
     199                 :    * at the end of the text. This will normally only be declared true when there
     200                 :    * is breakable whitespace at the end.
     201                 :    */
     202                 :   nsresult Reset(bool* aTrailingBreak);
     203                 : 
     204                 : private:
     205                 :   // This is a list of text sources that make up the "current word" (i.e.,
     206                 :   // run of text which does not contain any whitespace). All the mLengths
     207                 :   // are are nonzero, these cannot overlap.
     208               0 :   struct TextItem {
     209               0 :     TextItem(nsILineBreakSink* aSink, PRUint32 aSinkOffset, PRUint32 aLength,
     210                 :              PRUint32 aFlags)
     211               0 :       : mSink(aSink), mSinkOffset(aSinkOffset), mLength(aLength), mFlags(aFlags) {}
     212                 : 
     213                 :     nsILineBreakSink* mSink;
     214                 :     PRUint32          mSinkOffset;
     215                 :     PRUint32          mLength;
     216                 :     PRUint32          mFlags;
     217                 :   };
     218                 : 
     219                 :   // State for the nonwhitespace "word" that started in previous text and hasn't
     220                 :   // finished yet.
     221                 : 
     222                 :   // When the current word ends, this computes the linebreak opportunities
     223                 :   // *inside* the word (excluding either end) and sets them through the
     224                 :   // appropriate sink(s). Then we clear the current word state.
     225                 :   nsresult FlushCurrentWord();
     226                 : 
     227                 :   void UpdateCurrentWordLangGroup(nsIAtom *aLangGroup);
     228                 : 
     229                 :   void FindHyphenationPoints(nsHyphenator *aHyphenator,
     230                 :                              const PRUnichar *aTextStart,
     231                 :                              const PRUnichar *aTextLimit,
     232                 :                              PRUint8 *aBreakState);
     233                 : 
     234                 :   nsAutoTArray<PRUnichar,100> mCurrentWord;
     235                 :   // All the items that contribute to mCurrentWord
     236                 :   nsAutoTArray<TextItem,2>    mTextItems;
     237                 :   nsIAtom*                    mCurrentWordLangGroup;
     238                 :   bool                        mCurrentWordContainsMixedLang;
     239                 :   bool                        mCurrentWordContainsComplexChar;
     240                 : 
     241                 :   // True if the previous character was breakable whitespace
     242                 :   bool                        mAfterBreakableSpace;
     243                 :   // True if a break must be allowed at the current position because
     244                 :   // a run of breakable whitespace ends here
     245                 :   bool                        mBreakHere;
     246                 : };
     247                 : 
     248                 : #endif /*NSLINEBREAKER_H_*/

Generated by: LCOV version 1.7