Home | History | Annotate | Download | only in qt
      1 /*
      2  * Copyright (C) 2006 Lars Knoll <lars (at) trolltech.com>
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #include "config.h"
     22 #include "TextBreakIterator.h"
     23 
     24 #include <QtCore/qtextboundaryfinder.h>
     25 #include <qdebug.h>
     26 
     27 // #define DEBUG_TEXT_ITERATORS
     28 #ifdef DEBUG_TEXT_ITERATORS
     29 #define DEBUG qDebug
     30 #else
     31 #define DEBUG if (1) {} else qDebug
     32 #endif
     33 
     34 namespace WebCore {
     35 
     36     class TextBreakIterator : public QTextBoundaryFinder {
     37     };
     38     static QTextBoundaryFinder* iterator = 0;
     39     static unsigned char buffer[1024];
     40 
     41     TextBreakIterator* wordBreakIterator(const UChar* string, int length)
     42     {
     43         if (!string)
     44             return 0;
     45         if (!iterator)
     46             iterator = new QTextBoundaryFinder;
     47 
     48         *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Word, (const QChar *)string, length, buffer, sizeof(buffer));
     49         return static_cast<TextBreakIterator*>(iterator);
     50     }
     51 
     52     TextBreakIterator* characterBreakIterator(const UChar* string, int length)
     53     {
     54         if (!string)
     55             return 0;
     56         if (!iterator)
     57             iterator = new QTextBoundaryFinder;
     58 
     59         *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Grapheme, (const QChar *)string, length, buffer, sizeof(buffer));
     60         return static_cast<TextBreakIterator*>(iterator);
     61     }
     62 
     63     TextBreakIterator* cursorMovementIterator(const UChar* string, int length)
     64     {
     65         return characterBreakIterator(string, length);
     66     }
     67 
     68     TextBreakIterator* lineBreakIterator(const UChar* string, int length)
     69     {
     70         static QTextBoundaryFinder *iterator = 0;
     71         if (!string)
     72             return 0;
     73         if (!iterator)
     74             iterator = new QTextBoundaryFinder;
     75 
     76         *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Line, (const QChar *)string, length, buffer, sizeof(buffer));
     77         return static_cast<TextBreakIterator*>(iterator);
     78     }
     79 
     80     TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
     81     {
     82         if (!string)
     83             return 0;
     84         if (!iterator)
     85             iterator = new QTextBoundaryFinder;
     86 
     87         *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Sentence, (const QChar *)string, length, buffer, sizeof(buffer));
     88         return static_cast<TextBreakIterator*>(iterator);
     89     }
     90 
     91     int textBreakFirst(TextBreakIterator* bi)
     92     {
     93         bi->toStart();
     94         DEBUG() << "textBreakFirst" << bi->position();
     95         return bi->position();
     96     }
     97 
     98     int textBreakNext(TextBreakIterator* bi)
     99     {
    100         int pos = bi->toNextBoundary();
    101         DEBUG() << "textBreakNext" << pos;
    102         return pos;
    103     }
    104 
    105     int textBreakPreceding(TextBreakIterator* bi, int pos)
    106     {
    107         bi->setPosition(pos);
    108         int newpos = bi->toPreviousBoundary();
    109         DEBUG() << "textBreakPreceding" << pos << newpos;
    110         return newpos;
    111     }
    112 
    113     int textBreakFollowing(TextBreakIterator* bi, int pos)
    114     {
    115         bi->setPosition(pos);
    116         int newpos = bi->toNextBoundary();
    117         DEBUG() << "textBreakFollowing" << pos << newpos;
    118         return newpos;
    119     }
    120 
    121     int textBreakCurrent(TextBreakIterator* bi)
    122     {
    123         return bi->position();
    124     }
    125 
    126     bool isTextBreak(TextBreakIterator*, int)
    127     {
    128         return true;
    129     }
    130 
    131 }
    132