webengine/osswebengine/WebCore/platform/symbian/TextBreakIteratorSymbian.cpp
changeset 0 dd21522fd290
child 5 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * This file is part of the DOM implementation for KDE.
       
     3  *
       
     4  * Copyright (C) 2006 Lars Knoll <lars@trolltech.com>
       
     5  * Copyright (C) 2007 Nokia Inc.
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public License
       
    18  * along with this library; see the file COPYING.LIB.  If not, write to
       
    19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  *
       
    22  */
       
    23 
       
    24 #include "config.h"
       
    25 #include "TextBreakIterator.h"
       
    26 #include <e32cmn.h>
       
    27 #include <KJS/ustring.h>
       
    28 
       
    29 namespace WebCore {
       
    30 
       
    31     class TextBreakIterator
       
    32     {
       
    33     public:
       
    34         virtual int first() = 0;
       
    35         virtual int next() = 0;
       
    36         virtual int previous() = 0;
       
    37         inline int following(int pos) {
       
    38             currentPos = pos;
       
    39             return next();
       
    40         }
       
    41         inline int preceding(int pos) {
       
    42             currentPos = pos;
       
    43             return previous();
       
    44         }
       
    45         int currentPos;
       
    46         const UChar *string;
       
    47         int length;
       
    48     };
       
    49 
       
    50     class WordBreakIteratorSymbian : public TextBreakIterator
       
    51     {
       
    52     public:
       
    53         virtual int first();
       
    54         virtual int next();
       
    55         virtual int previous();
       
    56     };
       
    57 
       
    58     class CharBreakIteratorSymbian : public TextBreakIterator
       
    59     {
       
    60     public:
       
    61         virtual int first();
       
    62         virtual int next();
       
    63         virtual int previous();
       
    64     };
       
    65     
       
    66     class LineBreakIteratorSymbian : public TextBreakIterator
       
    67     {
       
    68     public:
       
    69         virtual int first();
       
    70         virtual int next();
       
    71         virtual int previous();
       
    72     };
       
    73 
       
    74     int WordBreakIteratorSymbian::first() {
       
    75         currentPos = 0;
       
    76         return currentPos;
       
    77     }
       
    78 
       
    79     int WordBreakIteratorSymbian::next() {
       
    80         if (currentPos == length) {
       
    81             currentPos = -1;
       
    82             return currentPos;
       
    83         }
       
    84         bool haveSpace = false;
       
    85         while (currentPos < length) {
       
    86             if (haveSpace && !TChar(string[currentPos]).IsSpace())
       
    87                 break;
       
    88             if (TChar(string[currentPos]).IsSpace())
       
    89                 haveSpace = true;
       
    90             ++currentPos;
       
    91         }
       
    92         return currentPos;
       
    93     }
       
    94 
       
    95     int WordBreakIteratorSymbian::previous() {
       
    96         if (currentPos == 0) {
       
    97             currentPos = -1;
       
    98             return currentPos;
       
    99         }
       
   100         bool haveSpace = false;
       
   101         while (currentPos > 0) {
       
   102             if (haveSpace && !TChar(string[currentPos]).IsSpace())
       
   103                 break;
       
   104             if (TChar(string[currentPos]).IsSpace())
       
   105                 haveSpace = true;
       
   106             --currentPos;
       
   107         }
       
   108         return currentPos;
       
   109     }
       
   110 
       
   111     int CharBreakIteratorSymbian::first() {
       
   112         currentPos = 0;
       
   113         return currentPos;
       
   114     }
       
   115 
       
   116     int CharBreakIteratorSymbian::next() {
       
   117         if (currentPos == length)
       
   118             return -1;
       
   119         
       
   120         // FIXME: symbian doesn't have Grapheme Cluster support
       
   121         // currentPos = layout.nextCursorPosition(currentPos);
       
   122 
       
   123         currentPos++;
       
   124         return currentPos;
       
   125     }
       
   126     int CharBreakIteratorSymbian::previous() {
       
   127         if (currentPos == 0)
       
   128             return -1;
       
   129 
       
   130         // FIXME: symbian doesn't have Grapheme Cluster support
       
   131         // currentPos = layout.nextCursorPosition(currentPos);
       
   132 
       
   133         currentPos--;
       
   134 
       
   135         //currentPos = layout.previousCursorPosition(currentPos);
       
   136         return currentPos;
       
   137     }
       
   138 
       
   139     int LineBreakIteratorSymbian::first() {
       
   140         currentPos = 0;
       
   141         return currentPos;
       
   142     }
       
   143 
       
   144    int LineBreakIteratorSymbian::next() 
       
   145    {
       
   146         if (currentPos == length)
       
   147             return -1;
       
   148         
       
   149       // Asian line breaking. Everywhere allowed except directly in front of a punctuation character.
       
   150         int iRet = -1;
       
   151         currentPos++;   
       
   152         unsigned char row = (KJS::UChar(string[currentPos])).high();
       
   153         
       
   154         if ( row > 0x2d && row < 0xfb || row == 0x11 )
       
   155             iRet = currentPos;
       
   156         else
       
   157            iRet = -1;
       
   158         
       
   159        return iRet;
       
   160  }
       
   161     
       
   162     int LineBreakIteratorSymbian::previous() 
       
   163     {
       
   164        //not implemented
       
   165        return -1;
       
   166     }
       
   167     
       
   168 
       
   169 TextBreakIterator* wordBreakIterator(const UChar* string, int length)
       
   170 {
       
   171     static WordBreakIteratorSymbian *iterator = 0;
       
   172     if (!iterator)
       
   173         iterator = new WordBreakIteratorSymbian;
       
   174 
       
   175     iterator->string = string;
       
   176     iterator->length = length;
       
   177     iterator->currentPos = 0;
       
   178 
       
   179     return iterator;
       
   180 }
       
   181 
       
   182 TextBreakIterator* characterBreakIterator(const UChar* string, int length)
       
   183 {
       
   184     static CharBreakIteratorSymbian *iterator = 0;
       
   185     if (!iterator)
       
   186         iterator = new CharBreakIteratorSymbian;
       
   187 
       
   188     iterator->string = string;
       
   189     iterator->length = length;
       
   190     iterator->currentPos = 0;
       
   191 
       
   192     return iterator;
       
   193 }
       
   194 
       
   195 TextBreakIterator* lineBreakIterator(const UChar* string, int length)
       
   196 {
       
   197    static LineBreakIteratorSymbian *iterator = 0;
       
   198     if (!iterator)
       
   199         iterator = new LineBreakIteratorSymbian;
       
   200 
       
   201     iterator->string = string;
       
   202     iterator->length = length;
       
   203     iterator->currentPos = 0;
       
   204 
       
   205     return iterator;
       
   206 }
       
   207 
       
   208 TextBreakIterator* sentenceBreakIterator(const UChar*, int)
       
   209 {
       
   210     // not yet implemented
       
   211     return 0;
       
   212 }
       
   213 
       
   214 
       
   215 int textBreakFirst(TextBreakIterator* bi)
       
   216 {
       
   217     return bi->first();
       
   218 }
       
   219 
       
   220 int textBreakNext(TextBreakIterator* bi)
       
   221 {
       
   222     return bi->next();
       
   223 }
       
   224 
       
   225 int textBreakPreceding(TextBreakIterator* bi, int pos)
       
   226 {
       
   227     return bi->preceding(pos);
       
   228 }
       
   229 
       
   230 int textBreakFollowing(TextBreakIterator* bi, int pos)
       
   231 {
       
   232     return bi->following(pos);
       
   233 }
       
   234 
       
   235 int textBreakCurrent(TextBreakIterator* bi)
       
   236 {
       
   237     return bi->currentPos;
       
   238 }
       
   239 
       
   240 bool isTextBreak(TextBreakIterator*, int)
       
   241 {
       
   242     return true;
       
   243 }
       
   244 
       
   245 }
       
   246