Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 , freeBuffer(true)
177{
178 if (sv.size() > 0) {
179 attributes = (QCharAttributes *) malloc((sv.size() + 1) * sizeof(QCharAttributes));
180 Q_CHECK_PTR(attributes);
181 init(t, sv, attributes);
182 }
183}
184
207 : t(type)
208 , sv(string)
209 , freeBuffer(true)
210{
211 if (!sv.isEmpty()) {
212 if (buffer && bufferSize / int(sizeof(QCharAttributes)) >= sv.size() + 1) {
213 attributes = reinterpret_cast<QCharAttributes *>(buffer);
214 freeBuffer = false;
215 } else {
216 attributes = (QCharAttributes *) malloc((sv.size() + 1) * sizeof(QCharAttributes));
217 Q_CHECK_PTR(attributes);
218 }
219 init(t, sv, attributes);
220 }
221}
222
229{
230 pos = 0;
231}
232
239{
240 pos = sv.size();
241}
242
252{
253 return pos;
254}
255
269
285{
286 if (sv.data() == s.unicode() && sv.size() == s.size())
287 return s;
288 return sv.toString();
289}
290
291
298{
299 if (!attributes || pos < 0 || pos >= sv.size()) {
300 pos = -1;
301 return pos;
302 }
303
304 ++pos;
305 switch(t) {
306 case Grapheme:
307 while (pos < sv.size() && !attributes[pos].graphemeBoundary)
308 ++pos;
309 break;
310 case Word:
311 while (pos < sv.size() && !attributes[pos].wordBreak)
312 ++pos;
313 break;
314 case Sentence:
315 while (pos < sv.size() && !attributes[pos].sentenceBoundary)
316 ++pos;
317 break;
318 case Line:
319 while (pos < sv.size() && !attributes[pos].lineBreak)
320 ++pos;
321 break;
322 }
323
324 return pos;
325}
326
333{
334 if (!attributes || pos <= 0 || pos > sv.size()) {
335 pos = -1;
336 return pos;
337 }
338
339 --pos;
340 switch(t) {
341 case Grapheme:
342 while (pos > 0 && !attributes[pos].graphemeBoundary)
343 --pos;
344 break;
345 case Word:
346 while (pos > 0 && !attributes[pos].wordBreak)
347 --pos;
348 break;
349 case Sentence:
350 while (pos > 0 && !attributes[pos].sentenceBoundary)
351 --pos;
352 break;
353 case Line:
354 while (pos > 0 && !attributes[pos].lineBreak)
355 --pos;
356 break;
357 }
358
359 return pos;
360}
361
366{
367 if (!attributes || pos < 0 || pos > sv.size())
368 return false;
369
370 switch(t) {
371 case Grapheme:
372 return attributes[pos].graphemeBoundary;
373 case Word:
374 return attributes[pos].wordBreak;
375 case Sentence:
376 return attributes[pos].sentenceBoundary;
377 case Line:
378 // ### TR#14 LB2 prohibits break at sot
379 return attributes[pos].lineBreak || pos == 0;
380 }
381 return false;
382}
383
387QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() const
388{
389 BoundaryReasons reasons = NotAtBoundary;
390 if (!attributes || pos < 0 || pos > sv.size())
391 return reasons;
392
393 const QCharAttributes attr = attributes[pos];
394 switch (t) {
395 case Grapheme:
396 if (attr.graphemeBoundary) {
398 if (pos == 0)
399 reasons &= (~EndOfItem);
400 else if (pos == sv.size())
401 reasons &= (~StartOfItem);
402 }
403 break;
404 case Word:
405 if (attr.wordBreak) {
406 reasons |= BreakOpportunity;
407 if (attr.wordStart)
408 reasons |= StartOfItem;
409 if (attr.wordEnd)
410 reasons |= EndOfItem;
411 }
412 break;
413 case Sentence:
414 if (attr.sentenceBoundary) {
416 if (pos == 0)
417 reasons &= (~EndOfItem);
418 else if (pos == sv.size())
419 reasons &= (~StartOfItem);
420 }
421 break;
422 case Line:
423 // ### TR#14 LB2 prohibits break at sot
424 if (attr.lineBreak || pos == 0) {
425 reasons |= BreakOpportunity;
426 if (attr.mandatoryBreak || pos == 0) {
427 reasons |= MandatoryBreak | StartOfItem | EndOfItem;
428 if (pos == 0)
429 reasons &= (~EndOfItem);
430 else if (pos == sv.size())
431 reasons &= (~StartOfItem);
432 } else if (pos > 0 && sv[pos - 1].unicode() == QChar::SoftHyphen) {
433 reasons |= SoftHyphen;
434 }
435 }
436 break;
437 default:
438 break;
439 }
440
441 return reasons;
442}
443
\inmodule QtCore
Definition qstringview.h:78
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:1121
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1240
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.
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 s
[6]
Definition qopenglext.h:235
GLdouble GLdouble t
Definition qopenglext.h:243
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:752
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define Q_UNUSED(x)
ptrdiff_t qsizetype
Definition qtypes.h:165
Q_CHECK_PTR(a=new int[80])
QSharedPointer< T > other(t)
[5]