Make line breaking obey the -webkit-locale property
[WebKit-https.git] / Source / WebCore / platform / text / qt / TextBreakIteratorQt.cpp
1 /*
2  * Copyright (C) 2006 Lars Knoll <lars@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 <algorithm>
26 #include <qdebug.h>
27
28 // #define DEBUG_TEXT_ITERATORS
29 #ifdef DEBUG_TEXT_ITERATORS
30 #define DEBUG qDebug
31 #else
32 #define DEBUG if (1) {} else qDebug
33 #endif
34
35 using namespace std;
36
37 namespace WebCore {
38
39     class TextBreakIterator : public QTextBoundaryFinder {
40     public:
41         TextBreakIterator(QTextBoundaryFinder::BoundaryType type, const QString& string)
42             : QTextBoundaryFinder(type, string)
43         { }
44         TextBreakIterator()
45             : QTextBoundaryFinder()
46         { }
47     };
48
49     TextBreakIterator* setUpIterator(TextBreakIterator& iterator, QTextBoundaryFinder::BoundaryType type, const UChar* characters, int length)
50     {
51         if (!characters || !length)
52             return 0;
53
54         if (iterator.isValid() && type == iterator.type() && iterator.string() == QString::fromRawData(reinterpret_cast<const QChar*>(characters), length)) {
55             iterator.toStart();
56             return &iterator;
57         }
58
59         iterator = TextBreakIterator(type, QString(reinterpret_cast<const QChar*>(characters), length));
60         return &iterator;
61     }
62
63     TextBreakIterator* wordBreakIterator(const UChar* string, int length)
64     {
65         static TextBreakIterator staticWordBreakIterator;
66         return setUpIterator(staticWordBreakIterator, QTextBoundaryFinder::Word, string, length);
67     }
68
69     TextBreakIterator* characterBreakIterator(const UChar* string, int length)
70     {
71         static TextBreakIterator staticCharacterBreakIterator;
72         return setUpIterator(staticCharacterBreakIterator, QTextBoundaryFinder::Grapheme, string, length);
73     }
74
75     TextBreakIterator* cursorMovementIterator(const UChar* string, int length)
76     {
77         return characterBreakIterator(string, length);
78     }
79
80     static TextBreakIterator* staticLineBreakIterator;
81
82     TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length, const AtomicString&)
83     {
84         TextBreakIterator* lineBreakIterator = 0;
85         if (staticLineBreakIterator) {
86             setUpIterator(*staticLineBreakIterator, QTextBoundaryFinder::Line, string, length);
87             std::swap(staticLineBreakIterator, lineBreakIterator);
88         }
89
90         if (!lineBreakIterator && string && length)
91             lineBreakIterator = new TextBreakIterator(QTextBoundaryFinder::Line, QString(reinterpret_cast<const QChar*>(string), length));
92
93         return lineBreakIterator;
94     }
95
96     void releaseLineBreakIterator(TextBreakIterator* iterator)
97     {
98         ASSERT(iterator);
99
100         if (!staticLineBreakIterator)
101             staticLineBreakIterator = iterator;
102         else
103             delete iterator;
104     }
105
106     TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
107     {
108         static TextBreakIterator staticSentenceBreakIterator;
109         return setUpIterator(staticSentenceBreakIterator, QTextBoundaryFinder::Sentence, string, length);
110
111     }
112
113     int textBreakFirst(TextBreakIterator* bi)
114     {
115         bi->toStart();
116         DEBUG() << "textBreakFirst" << bi->position();
117         return bi->position();
118     }
119
120     int textBreakNext(TextBreakIterator* bi)
121     {
122         int pos = bi->toNextBoundary();
123         DEBUG() << "textBreakNext" << pos;
124         return pos;
125     }
126
127     int textBreakPreceding(TextBreakIterator* bi, int pos)
128     {
129         bi->setPosition(pos);
130         int newpos = bi->toPreviousBoundary();
131         DEBUG() << "textBreakPreceding" << pos << newpos;
132         return newpos;
133     }
134
135     int textBreakFollowing(TextBreakIterator* bi, int pos)
136     {
137         bi->setPosition(pos);
138         int newpos = bi->toNextBoundary();
139         DEBUG() << "textBreakFollowing" << pos << newpos;
140         return newpos;
141     }
142
143     int textBreakCurrent(TextBreakIterator* bi)
144     {
145         return bi->position();
146     }
147
148     bool isTextBreak(TextBreakIterator*, int)
149     {
150         return true;
151     }
152
153 }