Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qtextboundaryfinder.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include <QtCore/qtextboundaryfinder.h>
4#include <QtCore/qvarlengtharray.h>
5
6#include <private/qunicodetools_p.h>
7
9
11{
13 QUnicodeTools::initScripts(str, &scriptItems);
14
15 QUnicodeTools::CharAttributeOptions options;
16 switch (type) {
21 default: break;
22 }
23 QUnicodeTools::initCharAttributes(str, scriptItems.data(), scriptItems.size(), attributes, options);
24}
25
104 : freeBuffer(true)
105{
106}
107
112 : t(other.t)
113 , s(other.s)
114 , sv(other.sv)
115 , pos(other.pos)
116 , freeBuffer(true)
117{
118 if (other.attributes) {
119 Q_ASSERT(sv.size() > 0);
120 attributes = (QCharAttributes *) malloc((sv.size() + 1) * sizeof(QCharAttributes));
121 Q_CHECK_PTR(attributes);
122 memcpy(attributes, other.attributes, (sv.size() + 1) * sizeof(QCharAttributes));
123 }
124}
125
130{
131 if (&other == this)
132 return *this;
133
134 if (other.attributes) {
135 Q_ASSERT(other.sv.size() > 0);
136 size_t newCapacity = (size_t(other.sv.size()) + 1) * sizeof(QCharAttributes);
137 QCharAttributes *newD = (QCharAttributes *) realloc(freeBuffer ? attributes : nullptr, newCapacity);
138 Q_CHECK_PTR(newD);
139 freeBuffer = true;
140 attributes = newD;
141 }
142
143 t = other.t;
144 s = other.s;
145 sv = other.sv;
146 pos = other.pos;
147
148 if (other.attributes) {
149 memcpy(attributes, other.attributes, (sv.size() + 1) * sizeof(QCharAttributes));
150 } else {
151 if (freeBuffer)
152 free(attributes);
153 attributes = nullptr;
154 }
155
156 return *this;
157}
158
163{
164 Q_UNUSED(unused);
165 if (freeBuffer)
166 free(attributes);
167}
168
173 : t(type)
174 , s(string)
175 , sv(s)
176 , pos(0)
177 , freeBuffer(true)
178 , attributes(nullptr)
179{
180 if (sv.size() > 0) {
181 attributes = (QCharAttributes *) malloc((sv.size() + 1) * sizeof(QCharAttributes));
182 Q_CHECK_PTR(attributes);
183 init(t, sv, attributes);
184 }
185}
186
209 : t(type)
210 , sv(string)
211 , pos(0)
212 , freeBuffer(true)
213 , attributes(nullptr)
214{
215 if (!sv.isEmpty()) {
216 if (buffer && bufferSize / int(sizeof(QCharAttributes)) >= sv.size() + 1) {
217 attributes = reinterpret_cast<QCharAttributes *>(buffer);
218 freeBuffer = false;
219 } else {
220 attributes = (QCharAttributes *) malloc((sv.size() + 1) * sizeof(QCharAttributes));
221 Q_CHECK_PTR(attributes);
222 }
223 init(t, sv, attributes);
224 }
225}
226
233{
234 pos = 0;
235}
236
243{
244 pos = sv.size();
245}
246
256{
257 return pos;
258}
259
270{
271 pos = qBound(0, position, sv.size());
272}
273
289{
290 if (sv.data() == s.unicode() && sv.size() == s.size())
291 return s;
292 return sv.toString();
293}
294
295
302{
303 if (!attributes || pos < 0 || pos >= sv.size()) {
304 pos = -1;
305 return pos;
306 }
307
308 ++pos;
309 switch(t) {
310 case Grapheme:
311 while (pos < sv.size() && !attributes[pos].graphemeBoundary)
312 ++pos;
313 break;
314 case Word:
315 while (pos < sv.size() && !attributes[pos].wordBreak)
316 ++pos;
317 break;
318 case Sentence:
319 while (pos < sv.size() && !attributes[pos].sentenceBoundary)
320 ++pos;
321 break;
322 case Line:
323 while (pos < sv.size() && !attributes[pos].lineBreak)
324 ++pos;
325 break;
326 }
327
328 return pos;
329}
330
337{
338 if (!attributes || pos <= 0 || pos > sv.size()) {
339 pos = -1;
340 return pos;
341 }
342
343 --pos;
344 switch(t) {
345 case Grapheme:
346 while (pos > 0 && !attributes[pos].graphemeBoundary)
347 --pos;
348 break;
349 case Word:
350 while (pos > 0 && !attributes[pos].wordBreak)
351 --pos;
352 break;
353 case Sentence:
354 while (pos > 0 && !attributes[pos].sentenceBoundary)
355 --pos;
356 break;
357 case Line:
358 while (pos > 0 && !attributes[pos].lineBreak)
359 --pos;
360 break;
361 }
362
363 return pos;
364}
365
370{
371 if (!attributes || pos < 0 || pos > sv.size())
372 return false;
373
374 switch(t) {
375 case Grapheme:
376 return attributes[pos].graphemeBoundary;
377 case Word:
378 return attributes[pos].wordBreak;
379 case Sentence:
380 return attributes[pos].sentenceBoundary;
381 case Line:
382 // ### TR#14 LB2 prohibits break at sot
383 return attributes[pos].lineBreak || pos == 0;
384 }
385 return false;
386}
387
391QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() const
392{
393 BoundaryReasons reasons = NotAtBoundary;
394 if (!attributes || pos < 0 || pos > sv.size())
395 return reasons;
396
397 const QCharAttributes attr = attributes[pos];
398 switch (t) {
399 case Grapheme:
400 if (attr.graphemeBoundary) {
402 if (pos == 0)
403 reasons &= (~EndOfItem);
404 else if (pos == sv.size())
405 reasons &= (~StartOfItem);
406 }
407 break;
408 case Word:
409 if (attr.wordBreak) {
410 reasons |= BreakOpportunity;
411 if (attr.wordStart)
412 reasons |= StartOfItem;
413 if (attr.wordEnd)
414 reasons |= EndOfItem;
415 }
416 break;
417 case Sentence:
418 if (attr.sentenceBoundary) {
420 if (pos == 0)
421 reasons &= (~EndOfItem);
422 else if (pos == sv.size())
423 reasons &= (~StartOfItem);
424 }
425 break;
426 case Line:
427 // ### TR#14 LB2 prohibits break at sot
428 if (attr.lineBreak || pos == 0) {
429 reasons |= BreakOpportunity;
430 if (attr.mandatoryBreak || pos == 0) {
431 reasons |= MandatoryBreak | StartOfItem | EndOfItem;
432 if (pos == 0)
433 reasons &= (~EndOfItem);
434 else if (pos == sv.size())
435 reasons &= (~StartOfItem);
436 } else if (pos > 0 && sv[pos - 1].unicode() == QChar::SoftHyphen) {
437 reasons |= SoftHyphen;
438 }
439 }
440 break;
441 default:
442 break;
443 }
444
445 return reasons;
446}
447
@ SoftHyphen
Definition qchar.h:58
constexpr char16_t unicode() const noexcept
Returns the numeric Unicode value of the QChar.
Definition qchar.h:458
\inmodule QtCore
Definition qstringview.h:76
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
constexpr qsizetype size() const noexcept
Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for...
const_pointer data() const noexcept
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1014
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
BoundaryReasons boundaryReasons() const
Returns the reasons for the boundary finder to have chosen the current position as a boundary.
QString string() const
Returns the string the QTextBoundaryFinder object operates on.
void setPosition(qsizetype position)
Sets the current position of the QTextBoundaryFinder to position.
~QTextBoundaryFinder()
Destructs the QTextBoundaryFinder object.
QTextBoundaryFinder & operator=(const QTextBoundaryFinder &other)
Assigns the object, other, to another QTextBoundaryFinder object.
qsizetype toNextBoundary()
Moves the QTextBoundaryFinder to the next boundary position and returns that position.
QTextBoundaryFinder()
Constructs an invalid QTextBoundaryFinder object.
qsizetype toPreviousBoundary()
Moves the QTextBoundaryFinder to the previous boundary position and returns that position.
void toStart()
Moves the finder to the start of the string.
void toEnd()
Moves the finder to the end of the string.
bool isAtBoundary() const
Returns true if the object's position() is currently at a valid text boundary.
qsizetype position() const
Returns the current position of the QTextBoundaryFinder.
BoundaryType
\value Grapheme Finds a grapheme which is the smallest boundary.
constexpr size_type size() const noexcept
T * data() noexcept
QString str
[2]
Combined button and popup list for selecting options.
Q_CORE_EXPORT void initCharAttributes(QStringView string, const ScriptItem *items, qsizetype numItems, QCharAttributes *attributes, CharAttributeOptions options)
Q_CORE_EXPORT void initScripts(QStringView string, ScriptItemArray *scripts)
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
GLenum GLuint buffer
GLenum type
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble s
[6]
Definition qopenglext.h:235
GLsizei const GLchar *const * string
[0]
Definition qopenglext.h:694
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QString lineBreak(QString s)
Definition main.cpp:652
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define Q_UNUSED(x)
ptrdiff_t qsizetype
Definition qtypes.h:70
Q_CHECK_PTR(a=new int[80])
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]