1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/*
* Copyright (C) 2006 Lars Knoll <lars@trolltech.com>
* Copyright (C) 2007 Apple Inc. All rights reserved.
* Copyright (C) 2008 Jürg Billeter <j@bitron.ch>
* Copyright (C) 2008 Dominik Röttsches <dominik.roettsches@access-company.com>
* Copyright (C) 2010 Igalia S.L.
*
* 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
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "TextBreakIterator.h"
#include <wtf/Atomics.h>
#include <wtf/gobject/GOwnPtr.h>
#include <pango/pango.h>
using namespace WTF;
using namespace std;
#define UTF8_IS_SURROGATE(character) (character >= 0x10000 && character <= 0x10FFFF)
namespace WebCore {
class CharacterIterator {
public:
bool setText(const UChar* string, int length);
const gchar* getText() { return m_utf8.get(); }
int getLength() { return m_length; }
glong getSize() { return m_size; }
void setIndex(int index);
int getIndex() { return m_index; }
void setUTF16Index(int index);
int getUTF16Index() { return m_utf16Index; }
int getUTF16Length() { return m_utf16Length; }
int first();
int last();
int next();
int previous();
private:
int characterSize(int index);
GOwnPtr<char> m_utf8;
int m_length;
long m_size;
int m_index;
int m_utf16Index;
int m_utf16Length;
};
int CharacterIterator::characterSize(int index)
{
if (index == m_length || index < 0)
return 0;
if (m_length == m_utf16Length)
return 1;
gchar* indexPtr = g_utf8_offset_to_pointer(m_utf8.get(), index);
gunichar character = g_utf8_get_char(indexPtr);
return UTF8_IS_SURROGATE(character) ? 2 : 1;
}
bool CharacterIterator::setText(const UChar* string, int length)
{
long utf8Size = 0;
m_utf8.set(g_utf16_to_utf8(string, length, 0, &utf8Size, 0));
if (!utf8Size)
return false;
m_utf16Length = length;
m_length = g_utf8_strlen(m_utf8.get(), utf8Size);
m_size = utf8Size;
m_index = 0;
m_utf16Index = 0;
return true;
}
void CharacterIterator::setIndex(int index)
{
if (index == m_index)
return;
if (index <= 0)
m_index = m_utf16Index = 0;
else if (index >= m_length) {
m_index = m_length;
m_utf16Index = m_utf16Length;
} else if (m_length == m_utf16Length)
m_index = m_utf16Index = index;
else {
m_index = index;
int utf16Index = 0;
int utf8Index = 0;
while (utf8Index < index) {
utf16Index += characterSize(utf8Index);
utf8Index++;
}
m_utf16Index = utf16Index;
}
}
void CharacterIterator::setUTF16Index(int index)
{
if (index == m_utf16Index)
return;
if (index <= 0)
m_utf16Index = m_index = 0;
else if (index >= m_utf16Length) {
m_utf16Index = m_utf16Length;
m_index = m_length;
} else if (m_length == m_utf16Length)
m_utf16Index = m_index = index;
else {
m_utf16Index = index;
int utf16Index = 0;
int utf8Index = 0;
while (utf16Index < index) {
utf16Index += characterSize(utf8Index);
utf8Index++;
}
m_index = utf8Index;
}
}
int CharacterIterator::first()
{
m_index = m_utf16Index = 0;
return m_index;
}
int CharacterIterator::last()
{
m_index = m_length;
m_utf16Index = m_utf16Length;
return m_index;
}
int CharacterIterator::next()
{
int next = m_index + 1;
if (next <= m_length) {
m_utf16Index = min(m_utf16Index + characterSize(m_index), m_utf16Length);
m_index = next;
} else {
m_index = TextBreakDone;
m_utf16Index = TextBreakDone;
}
return m_index;
}
int CharacterIterator::previous()
{
int previous = m_index - 1;
if (previous >= 0) {
m_utf16Index = max(m_utf16Index - characterSize(previous), 0);
m_index = previous;
} else {
m_index = TextBreakDone;
m_utf16Index = TextBreakDone;
}
return m_index;
}
enum UBreakIteratorType {
UBRK_CHARACTER,
UBRK_WORD,
UBRK_LINE,
UBRK_SENTENCE
};
class TextBreakIterator {
public:
UBreakIteratorType m_type;
PangoLogAttr* m_logAttrs;
CharacterIterator m_charIterator;
};
static TextBreakIterator* setUpIterator(bool& createdIterator, TextBreakIterator*& iterator,
UBreakIteratorType type, const UChar* string, int length)
{
if (!string)
return 0;
if (!createdIterator) {
iterator = new TextBreakIterator();
createdIterator = true;
}
if (!iterator)
return 0;
if (!iterator->m_charIterator.setText(string, length))
return 0;
int charLength = iterator->m_charIterator.getLength();
iterator->m_type = type;
if (createdIterator)
g_free(iterator->m_logAttrs);
iterator->m_logAttrs = g_new0(PangoLogAttr, charLength + 1);
pango_get_log_attrs(iterator->m_charIterator.getText(), iterator->m_charIterator.getSize(),
-1, 0, iterator->m_logAttrs, charLength + 1);
return iterator;
}
static TextBreakIterator* nonSharedCharacterBreakIterator;
NonSharedCharacterBreakIterator::NonSharedCharacterBreakIterator(const UChar* buffer, int length)
{
m_iterator = nonSharedCharacterBreakIterator;
bool createdIterator = m_iterator && weakCompareAndSwap(reinterpret_cast<void**>(&nonSharedCharacterBreakIterator), m_iterator, 0);
m_iterator = setUpIterator(createdIterator, m_iterator, UBRK_CHARACTER, buffer, length);
}
NonSharedCharacterBreakIterator::~NonSharedCharacterBreakIterator()
{
if (!weakCompareAndSwap(reinterpret_cast<void**>(&nonSharedCharacterBreakIterator), 0, m_iterator))
delete m_iterator;
}
TextBreakIterator* cursorMovementIterator(const UChar* string, int length)
{
// FIXME: This needs closer inspection to achieve behaviour identical to the ICU version.
static bool createdCursorMovementIterator = false;
static TextBreakIterator* staticCursorMovementIterator;
return setUpIterator(createdCursorMovementIterator, staticCursorMovementIterator, UBRK_CHARACTER, string, length);
}
TextBreakIterator* wordBreakIterator(const UChar* string, int length)
{
static bool createdWordBreakIterator = false;
static TextBreakIterator* staticWordBreakIterator;
return setUpIterator(createdWordBreakIterator, staticWordBreakIterator, UBRK_WORD, string, length);
}
static bool createdLineBreakIterator = false;
static TextBreakIterator* staticLineBreakIterator;
TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length, const AtomicString&)
{
TextBreakIterator* lineBreakIterator = 0;
if (!createdLineBreakIterator || staticLineBreakIterator) {
setUpIterator(createdLineBreakIterator, staticLineBreakIterator, UBRK_LINE, string, length);
swap(staticLineBreakIterator, lineBreakIterator);
}
if (!lineBreakIterator) {
bool createdNewLineBreakIterator = false;
setUpIterator(createdNewLineBreakIterator, lineBreakIterator, UBRK_LINE, string, length);
}
return lineBreakIterator;
}
void releaseLineBreakIterator(TextBreakIterator* iterator)
{
ASSERT(createdLineBreakIterator);
ASSERT(iterator);
if (!staticLineBreakIterator)
staticLineBreakIterator = iterator;
else
delete iterator;
}
TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
{
static bool createdSentenceBreakIterator = false;
static TextBreakIterator* staticSentenceBreakIterator;
return setUpIterator(createdSentenceBreakIterator, staticSentenceBreakIterator, UBRK_SENTENCE, string, length);
}
int textBreakFirst(TextBreakIterator* iterator)
{
iterator->m_charIterator.first();
return iterator->m_charIterator.getUTF16Index();
}
int textBreakLast(TextBreakIterator* iterator)
{
// TextBreakLast is not meant to find just any break according to bi->m_type
// but really the one near the last character.
// (cmp ICU documentation for ubrk_first and ubrk_last)
// From ICU docs for ubrk_last:
// "Determine the index immediately beyond the last character in the text being scanned."
// So we should advance or traverse back based on bi->m_logAttrs cursor positions.
// If last character position in the original string is a whitespace,
// traverse to the left until the first non-white character position is found
// and return the position of the first white-space char after this one.
// Otherwise return m_length, as "the first character beyond the last" is outside our string.
bool whiteSpaceAtTheEnd = true;
int nextWhiteSpacePos = iterator->m_charIterator.getLength();
int pos = iterator->m_charIterator.last();
while (pos >= 0 && whiteSpaceAtTheEnd) {
if (iterator->m_logAttrs[pos].is_cursor_position) {
if (whiteSpaceAtTheEnd = iterator->m_logAttrs[pos].is_white)
nextWhiteSpacePos = pos;
}
pos = iterator->m_charIterator.previous();
}
iterator->m_charIterator.setIndex(nextWhiteSpacePos);
return iterator->m_charIterator.getUTF16Index();
}
int textBreakNext(TextBreakIterator* iterator)
{
while (iterator->m_charIterator.next() != TextBreakDone) {
int index = iterator->m_charIterator.getIndex();
// FIXME: UBRK_WORD case: Single multibyte characters (i.e. white space around them), such as the euro symbol €,
// are not marked as word_start & word_end as opposed to the way ICU does it.
// This leads to - for example - different word selection behaviour when right clicking.
if ((iterator->m_type == UBRK_LINE && iterator->m_logAttrs[index].is_line_break)
|| (iterator->m_type == UBRK_WORD && (iterator->m_logAttrs[index].is_word_start || iterator->m_logAttrs[index].is_word_end))
|| (iterator->m_type == UBRK_CHARACTER && iterator->m_logAttrs[index].is_cursor_position)
|| (iterator->m_type == UBRK_SENTENCE && iterator->m_logAttrs[index].is_sentence_boundary)) {
break;
}
}
return iterator->m_charIterator.getUTF16Index();
}
int textBreakPrevious(TextBreakIterator* iterator)
{
while (iterator->m_charIterator.previous() != TextBreakDone) {
int index = iterator->m_charIterator.getIndex();
if ((iterator->m_type == UBRK_LINE && iterator->m_logAttrs[index].is_line_break)
|| (iterator->m_type == UBRK_WORD && (iterator->m_logAttrs[index].is_word_start || iterator->m_logAttrs[index].is_word_end))
|| (iterator->m_type == UBRK_CHARACTER && iterator->m_logAttrs[index].is_cursor_position)
|| (iterator->m_type == UBRK_SENTENCE && iterator->m_logAttrs[index].is_sentence_boundary)) {
break;
}
}
return iterator->m_charIterator.getUTF16Index();
}
int textBreakPreceding(TextBreakIterator* iterator, int offset)
{
if (offset > iterator->m_charIterator.getUTF16Length())
return TextBreakDone;
if (offset < 0)
return 0;
iterator->m_charIterator.setUTF16Index(offset);
return textBreakPrevious(iterator);
}
int textBreakFollowing(TextBreakIterator* iterator, int offset)
{
if (offset > iterator->m_charIterator.getUTF16Length())
return TextBreakDone;
if (offset < 0)
return 0;
iterator->m_charIterator.setUTF16Index(offset);
return textBreakNext(iterator);
}
int textBreakCurrent(TextBreakIterator* iterator)
{
return iterator->m_charIterator.getUTF16Index();
}
bool isTextBreak(TextBreakIterator* iterator, int offset)
{
if (!offset)
return true;
if (offset > iterator->m_charIterator.getUTF16Length())
return false;
iterator->m_charIterator.setUTF16Index(offset);
int index = iterator->m_charIterator.getIndex();
iterator->m_charIterator.previous();
textBreakNext(iterator);
return iterator->m_charIterator.getIndex() == index;
}
bool isWordTextBreak(TextBreakIterator*)
{
return true;
}
}
|