TextBreakIteratorQt.cpp 7.44 KB
Newer Older
hyatt's avatar
hyatt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 2006 Lars Knoll <lars@trolltech.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
18 19
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
hyatt's avatar
hyatt committed
20 21 22 23
 *
 */

#include "TextBreakIterator.h"
24 25 26 27 28

#if QT_VERSION >= 0x040400
#include <QtCore/qtextboundaryfinder.h>
#include <qdebug.h>

29
// #define DEBUG_TEXT_ITERATORS
30 31 32 33 34 35 36 37 38 39 40
#ifdef DEBUG_TEXT_ITERATORS
#define DEBUG qDebug
#else
#define DEBUG if (1) {} else qDebug
#endif

namespace WebCore {

    class TextBreakIterator : public QTextBoundaryFinder
    {
    };
41 42
    static QTextBoundaryFinder* iterator = 0;
    static unsigned char buffer[1024];
43 44 45

    TextBreakIterator* wordBreakIterator(const UChar* string, int length)
    {
46 47
        if (!string)
            return 0;
48 49 50 51
        if (!iterator)
            iterator = new QTextBoundaryFinder;

        *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Word, (const QChar *)string, length, buffer, sizeof(buffer));
52 53 54 55 56
        return static_cast<TextBreakIterator*>(iterator);
    }

    TextBreakIterator* characterBreakIterator(const UChar* string, int length)
    {
57 58
        if (!string)
            return 0;
59 60 61 62
        if (!iterator)
            iterator = new QTextBoundaryFinder;

        *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Grapheme, (const QChar *)string, length, buffer, sizeof(buffer));
63 64 65 66 67 68
        return static_cast<TextBreakIterator*>(iterator);
    }

    TextBreakIterator* lineBreakIterator(const UChar* string, int length)
    {
        static QTextBoundaryFinder *iterator = 0;
69 70
        if (!string)
            return 0;
71 72 73 74
        if (!iterator)
            iterator = new QTextBoundaryFinder;

        *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Line, (const QChar *)string, length, buffer, sizeof(buffer));
75 76 77 78 79
        return static_cast<TextBreakIterator*>(iterator);
    }

    TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
    {
80 81
        if (!string)
            return 0;
82 83 84 85
        if (!iterator)
            iterator = new QTextBoundaryFinder;

        *iterator = QTextBoundaryFinder(QTextBoundaryFinder::Sentence, (const QChar *)string, length, buffer, sizeof(buffer));
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        return static_cast<TextBreakIterator*>(iterator);
    }

    int textBreakFirst(TextBreakIterator* bi)
    {
        bi->toStart();
        DEBUG() << "textBreakFirst" << bi->position();
        return bi->position();
    }

    int textBreakNext(TextBreakIterator* bi)
    {
        int pos = bi->toNextBoundary();
        DEBUG() << "textBreakNext" << pos;
        return pos;
    }

    int textBreakPreceding(TextBreakIterator* bi, int pos)
    {
        bi->setPosition(pos);
        int newpos = bi->toPreviousBoundary();
        DEBUG() << "textBreakPreceding" << pos << newpos;
        return newpos;
    }

    int textBreakFollowing(TextBreakIterator* bi, int pos)
    {
        bi->setPosition(pos);
        int newpos = bi->toNextBoundary();
        DEBUG() << "textBreakFollowing" << pos << newpos;
        return newpos;
    }

    int textBreakCurrent(TextBreakIterator* bi)
    {
        return bi->position();
    }

    bool isTextBreak(TextBreakIterator*, int)
    {
        return true;
    }

}
#else
hyatt's avatar
hyatt committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#include <qtextlayout.h>

namespace WebCore {

    class TextBreakIterator
    {
    public:
        virtual int first() = 0;
        virtual int next() = 0;
        virtual int previous() = 0;
        inline int following(int pos) {
            currentPos = pos;
            return next();
        }
        inline int preceding(int pos) {
            currentPos = pos;
            return previous();
        }
        int currentPos;
        const UChar *string;
        int length;
    };

    class WordBreakIteratorQt : public TextBreakIterator
    {
    public:
        virtual int first();
        virtual int next();
        virtual int previous();
    };

    class CharBreakIteratorQt : public TextBreakIterator
    {
    public:
        virtual int first();
        virtual int next();
        virtual int previous();
        QTextLayout layout;
    };

    int WordBreakIteratorQt::first() {
        currentPos = 0;
        return currentPos;
    }

    int WordBreakIteratorQt::next() {
177
        if (currentPos >= length) {
hyatt's avatar
hyatt committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
            currentPos = -1;
            return currentPos;
        }
        bool haveSpace = false;
        while (currentPos < length) {
            if (haveSpace && !QChar(string[currentPos]).isSpace())
                break;
            if (QChar(string[currentPos]).isSpace())
                haveSpace = true;
            ++currentPos;
        }
        return currentPos;
    }
    int WordBreakIteratorQt::previous() {
192
        if (currentPos <= 0) {
hyatt's avatar
hyatt committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            currentPos = -1;
            return currentPos;
        }
        bool haveSpace = false;
        while (currentPos > 0) {
            if (haveSpace && !QChar(string[currentPos]).isSpace())
                break;
            if (QChar(string[currentPos]).isSpace())
                haveSpace = true;
            --currentPos;
        }
        return currentPos;
    }

    int CharBreakIteratorQt::first() {
        currentPos = 0;
        return currentPos;
    }

    int CharBreakIteratorQt::next() {
213
        if (currentPos >= length)
hyatt's avatar
hyatt committed
214 215 216 217 218
            return -1;
        currentPos = layout.nextCursorPosition(currentPos);
        return currentPos;
    }
    int CharBreakIteratorQt::previous() {
219
        if (currentPos <= 0)
hyatt's avatar
hyatt committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            return -1;
        currentPos = layout.previousCursorPosition(currentPos);
        return currentPos;
    }


TextBreakIterator* wordBreakIterator(const UChar* string, int length)
{
    static WordBreakIteratorQt *iterator = 0;
    if (!iterator)
        iterator = new WordBreakIteratorQt;

    iterator->string = string;
    iterator->length = length;
    iterator->currentPos = 0;

    return iterator;
}

TextBreakIterator* characterBreakIterator(const UChar* string, int length)
{
    static CharBreakIteratorQt *iterator = 0;
    if (!iterator)
        iterator = new CharBreakIteratorQt;

    iterator->string = string;
    iterator->length = length;
    iterator->currentPos = 0;
    iterator->layout.setText(QString(reinterpret_cast<const QChar*>(string), length));
249
    
hyatt's avatar
hyatt committed
250 251 252
    return iterator;
}

darin's avatar
darin committed
253 254 255 256 257 258
TextBreakIterator* lineBreakIterator(const UChar*, int)
{
    // not yet implemented
    return 0;
}

darin's avatar
darin committed
259 260 261 262 263 264
TextBreakIterator* sentenceBreakIterator(const UChar*, int)
{
    // not yet implemented
    return 0;
}

hyatt's avatar
hyatt committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
int textBreakFirst(TextBreakIterator* bi)
{
    return bi->first();
}

int textBreakNext(TextBreakIterator* bi)
{
    return bi->next();
}

int textBreakPreceding(TextBreakIterator* bi, int pos)
{
    return bi->preceding(pos);
}

int textBreakFollowing(TextBreakIterator* bi, int pos)
{
    return bi->following(pos);
}

int textBreakCurrent(TextBreakIterator* bi)
{
    return bi->currentPos;
}

aroben's avatar
aroben committed
290 291 292 293 294
bool isTextBreak(TextBreakIterator*, int)
{
    return true;
}

hyatt's avatar
hyatt committed
295 296
}

297
#endif