1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
41 #include <QtCore/qtextboundaryfinder.h>
42 #include <QtCore/qvarlengtharray.h>
43 #include <private/qunicodetables_p.h>
45 #include "private/qharfbuzz_p.h"
49 class QTextBoundaryFinderPrivate
52 HB_CharAttributes attributes[1];
55 static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int length, HB_CharAttributes *attributes)
57 QVarLengthArray<HB_ScriptItem> scriptItems;
59 const ushort *string = reinterpret_cast<const ushort *>(chars);
60 const ushort *unicode = string;
61 // correctly assign script, isTab and isObject to the script analysis
62 const ushort *uc = unicode;
63 const ushort *e = uc + length;
64 int script = QUnicodeTables::Common;
65 int lastScript = QUnicodeTables::Common;
66 const ushort *start = uc;
68 int s = QUnicodeTables::script(*uc);
69 if (s != QUnicodeTables::Inherited)
71 if (*uc == QChar::ObjectReplacementCharacter || *uc == QChar::LineSeparator || *uc == 9)
72 script = QUnicodeTables::Common;
73 if (script != lastScript) {
76 item.pos = start - string;
77 item.length = uc - start;
78 item.script = (HB_Script)lastScript;
79 item.bidiLevel = 0; // ### what's the proper value?
80 scriptItems.append(item);
89 item.pos = start - string;
90 item.length = uc - start;
91 item.script = (HB_Script)lastScript;
92 item.bidiLevel = 0; // ### what's the proper value?
93 scriptItems.append(item);
96 qGetCharAttributes(string, length, scriptItems.data(), scriptItems.count(), attributes);
97 if (type == QTextBoundaryFinder::Word)
98 HB_GetWordBoundaries(string, length, scriptItems.data(), scriptItems.count(), attributes);
99 else if (type == QTextBoundaryFinder::Sentence)
100 HB_GetSentenceBoundaries(string, length, scriptItems.data(), scriptItems.count(), attributes);
104 \class QTextBoundaryFinder
106 \brief The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string.
111 \ingroup string-processing
114 QTextBoundaryFinder allows to find Unicode text boundaries in a
115 string, similar to the Unicode text boundary specification (see
116 http://www.unicode.org/reports/tr29/tr29-11.html).
118 QTextBoundaryFinder can operate on a QString in four possible
119 modes depending on the value of \a BoundaryType.
121 Units of Unicode characters that make up what the user thinks of
122 as a character or basic unit of the language are here called
123 Grapheme clusters. The two unicode characters 'A' + diaeresis do
124 for example form one grapheme cluster as the user thinks of them
125 as one character, yet it is in this case represented by two
128 Word boundaries are there to locate the start and end of what a
129 language considers to be a word.
131 Line break boundaries give possible places where a line break
132 might happen and sentence boundaries will show the beginning and
133 end of whole sentences.
135 The first position in a string is always a valid boundary and
136 refers to the position before the first character. The last
137 position at the length of the string is also valid and refers
138 to the position after the last character.
142 \enum QTextBoundaryFinder::BoundaryType
144 \value Grapheme Finds a grapheme which is the smallest boundary. It
145 including letters, punctation marks, numerals and more.
146 \value Word Finds a word.
147 \value Line Finds possible positions for breaking the text into multiple
149 \value Sentence Finds sentence boundaries. These include periods, question
154 \enum QTextBoundaryFinder::BoundaryReason
156 \value NotAtBoundary The boundary finder is not at a boundary position.
157 \value StartWord The boundary finder is at the start of a word.
158 \value EndWord The boundary finder is at the end of a word.
162 Constructs an invalid QTextBoundaryFinder object.
164 QTextBoundaryFinder::QTextBoundaryFinder()
174 Copies the QTextBoundaryFinder object, \a other.
176 QTextBoundaryFinder::QTextBoundaryFinder(const QTextBoundaryFinder &other)
180 , length(other.length)
184 d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
186 memcpy(d, other.d, length*sizeof(HB_CharAttributes));
190 Assigns the object, \a other, to another QTextBoundaryFinder object.
192 QTextBoundaryFinder &QTextBoundaryFinder::operator=(const QTextBoundaryFinder &other)
200 length = other.length;
203 QTextBoundaryFinderPrivate *newD = (QTextBoundaryFinderPrivate *)
204 realloc(freePrivate ? d : 0, length*sizeof(HB_CharAttributes));
208 memcpy(d, other.d, length*sizeof(HB_CharAttributes));
214 Destructs the QTextBoundaryFinder object.
216 QTextBoundaryFinder::~QTextBoundaryFinder()
223 Creates a QTextBoundaryFinder object of \a type operating on \a string.
225 QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QString &string)
228 , chars(string.unicode())
229 , length(string.length())
233 d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
235 init(t, chars, length, d->attributes);
239 Creates a QTextBoundaryFinder object of \a type operating on \a chars
242 \a buffer is an optional working buffer of size \a bufferSize you can pass to
243 the QTextBoundaryFinder. If the buffer is large enough to hold the working
244 data required, it will use this instead of allocating its own buffer.
246 \warning QTextBoundaryFinder does not create a copy of \a chars. It is the
247 application programmer's responsibility to ensure the array is allocated for
248 as long as the QTextBoundaryFinder object stays alive. The same applies to
251 QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QChar *chars, int length, unsigned char *buffer, int bufferSize)
257 if (buffer && (uint)bufferSize >= length*sizeof(HB_CharAttributes)) {
258 d = (QTextBoundaryFinderPrivate *)buffer;
261 d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
265 init(t, chars, length, d->attributes);
269 Moves the finder to the start of the string. This is equivalent to setPosition(0).
271 \sa setPosition(), position()
273 void QTextBoundaryFinder::toStart()
279 Moves the finder to the end of the string. This is equivalent to setPosition(string.length()).
281 \sa setPosition(), position()
283 void QTextBoundaryFinder::toEnd()
289 Returns the current position of the QTextBoundaryFinder.
291 The range is from 0 (the beginning of the string) to the length of
292 the string inclusive.
296 int QTextBoundaryFinder::position() const
302 Sets the current position of the QTextBoundaryFinder to \a position.
304 If \a position is out of bounds, it will be bound to only valid
305 positions. In this case, valid positions are from 0 to the length of
306 the string inclusive.
310 void QTextBoundaryFinder::setPosition(int position)
312 pos = qBound(0, position, length);
315 /*! \fn QTextBoundaryFinder::BoundaryType QTextBoundaryFinder::type() const
317 Returns the type of the QTextBoundaryFinder.
320 /*! \fn bool QTextBoundaryFinder::isValid() const
322 Returns true if the text boundary finder is valid; otherwise returns false.
323 A default QTextBoundaryFinder is invalid.
327 Returns the string the QTextBoundaryFinder object operates on.
329 QString QTextBoundaryFinder::string() const
331 if (chars == s.unicode() && length == s.length())
333 return QString(chars, length);
338 Moves the QTextBoundaryFinder to the next boundary position and returns that position.
340 Returns -1 if there is no next boundary.
342 int QTextBoundaryFinder::toNextBoundary()
349 if (pos < 0 || pos >= length) {
359 while (pos < length && !d->attributes[pos].charStop)
363 while (pos < length && !d->attributes[pos].wordBoundary)
367 while (pos < length && !d->attributes[pos].sentenceBoundary)
372 while (pos < length && d->attributes[pos-1].lineBreakType < HB_Break)
381 Moves the QTextBoundaryFinder to the previous boundary position and returns that position.
383 Returns -1 if there is no previous boundary.
385 int QTextBoundaryFinder::toPreviousBoundary()
392 if (pos <= 0 || pos > length) {
402 while (pos > 0 && !d->attributes[pos].charStop)
406 while (pos > 0 && !d->attributes[pos].wordBoundary)
410 while (pos > 0 && !d->attributes[pos].sentenceBoundary)
414 while (pos > 0 && d->attributes[pos-1].lineBreakType < HB_Break)
423 Returns true if the object's position() is currently at a valid text boundary.
425 bool QTextBoundaryFinder::isAtBoundary() const
435 return d->attributes[pos].charStop;
437 return d->attributes[pos].wordBoundary;
439 return (pos > 0) ? d->attributes[pos-1].lineBreakType >= HB_Break : true;
441 return d->attributes[pos].sentenceBoundary;
447 Returns the reasons for the boundary finder to have chosen the current position as a boundary.
449 QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() const
452 return NotAtBoundary;
453 if (! isAtBoundary())
454 return NotAtBoundary;
456 if (d->attributes[pos].whiteSpace)
457 return NotAtBoundary;
461 if (d->attributes[length-1].whiteSpace)
462 return NotAtBoundary;
466 const bool nextIsSpace = d->attributes[pos].whiteSpace;
467 const bool prevIsSpace = d->attributes[pos - 1].whiteSpace;
469 if (prevIsSpace && !nextIsSpace)
471 else if (!prevIsSpace && nextIsSpace)
473 else if (!prevIsSpace && !nextIsSpace)
474 return BoundaryReasons(StartWord | EndWord);
476 return NotAtBoundary;