📄 qtextlayout.cpp
字号:
}/*! Returns the layout's text. \sa setText()*/QString QTextLayout::text() const{ return d->text;}/*! Sets the text option structure that controls the layout process to the given \a option. \sa textOption() QTextOption*/void QTextLayout::setTextOption(const QTextOption &option){ d->option = option;}/*! Returns the current text option used to control the layout process. \sa setTextOption() QTextOption*/QTextOption QTextLayout::textOption() const{ return d->option;}/*! Sets the \a position and \a text of the area in the layout that is processed before editing occurs.*/void QTextLayout::setPreeditArea(int position, const QString &text){ if (text.isEmpty()) { if (!d->specialData) return; if (d->specialData->addFormats.isEmpty()) { delete d->specialData; d->specialData = 0; } else { d->specialData->preeditText = QString(); d->specialData->preeditPosition = -1; } } else { if (!d->specialData) d->specialData = new QTextEngine::SpecialData; d->specialData->preeditPosition = position; d->specialData->preeditText = text; } d->invalidate(); if (d->block.docHandle()) d->block.docHandle()->documentChange(d->block.position(), d->block.length());}/*! Returns the position of the area in the text layout that will be processed before editing occurs.*/int QTextLayout::preeditAreaPosition() const{ return d->specialData ? d->specialData->preeditPosition : -1;}/*! Returns the text that is inserted in the layout before editing occurs.*/QString QTextLayout::preeditAreaText() const{ return d->specialData ? d->specialData->preeditText : QString();}/*! Sets the additional formats supported by the text layout to \a formatList. \sa additionalFormats(), clearAdditionalFormats()*/void QTextLayout::setAdditionalFormats(const QList<FormatRange> &formatList){ if (formatList.isEmpty()) { if (!d->specialData) return; if (d->specialData->preeditText.isEmpty()) { delete d->specialData; d->specialData = 0; } else { d->specialData->addFormats = formatList; d->specialData->addFormatIndices.clear(); } } else { if (!d->specialData) { d->specialData = new QTextEngine::SpecialData; d->specialData->preeditPosition = -1; } d->specialData->addFormats = formatList; d->indexAdditionalFormats(); } if (d->block.docHandle()) d->block.docHandle()->documentChange(d->block.position(), d->block.length());}/*! Returns the list of additional formats supported by the text layout. \sa setAdditionalFormats(), clearAdditionalFormats()*/QList<QTextLayout::FormatRange> QTextLayout::additionalFormats() const{ QList<FormatRange> formats; if (!d->specialData) return formats; formats = d->specialData->addFormats; if (d->specialData->addFormatIndices.isEmpty()) return formats; const QTextFormatCollection *collection = d->formats(); for (int i = 0; i < d->specialData->addFormatIndices.count(); ++i) formats[i].format = collection->charFormat(d->specialData->addFormatIndices.at(i)); return formats;}/*! Clears the list of additional formats supported by the text layout. \sa additionalFormats(), setAdditionalFormats()*/void QTextLayout::clearAdditionalFormats(){ setAdditionalFormats(QList<FormatRange>());}/*! Enables caching of the complete layout information if \a enable is true; otherwise disables layout caching. Usually QTextLayout throws most of the layouting information away after a call to endLayout() to reduce memory consumption. If you however want to draw the laid out text directly afterwards enabling caching might speed up drawing significantly. \sa cacheEnabled()*/void QTextLayout::setCacheEnabled(bool enable){ d->cacheGlyphs = enable;}/*! Returns true if the complete layout information is cached; otherwise returns false. \sa setCacheEnabled()*/bool QTextLayout::cacheEnabled() const{ return d->cacheGlyphs;}/*! Begins the layout process.*/void QTextLayout::beginLayout(){#ifndef QT_NO_DEBUG if (d->layoutData && d->layoutData->inLayout) { qWarning("QTextLayout::beginLayout: Called while already doing layout"); return; }#endif d->invalidate(); d->itemize(); d->layoutData->inLayout = true;}/*! Ends the layout process.*/void QTextLayout::endLayout(){#ifndef QT_NO_DEBUG if (!d->layoutData || !d->layoutData->inLayout) { qWarning("QTextLayout::endLayout: Called without beginLayout()"); return; }#endif int l = d->lines.size(); if (l && d->lines.at(l-1).length < 0) { QTextLine(l-1, d).setNumColumns(INT_MAX); } d->layoutData->inLayout = false; if (!d->cacheGlyphs) d->freeMemory();}/*! Returns the next valid cursor position after \a oldPos that respects the given cursor \a mode. \sa isValidCursorPosition() previousCursorPosition()*/int QTextLayout::nextCursorPosition(int oldPos, CursorMode mode) const{// qDebug("looking for next cursor pos for %d", oldPos); const QCharAttributes *attributes = d->attributes(); if (!attributes) return 0; int len = d->layoutData->string.length(); if (oldPos >= len) return oldPos; oldPos++; if (mode == SkipCharacters) { while (oldPos < len && !attributes[oldPos].charStop) oldPos++; } else { while (oldPos < len && attributes[oldPos].whiteSpace) oldPos++; while (oldPos < len && !attributes[oldPos-1].whiteSpace && !d->atWordSeparator(oldPos)) oldPos++; }// qDebug(" -> %d", oldPos); return oldPos;}/*! Returns the first valid cursor position before \a oldPos that respects the given cursor \a mode. \sa isValidCursorPosition() nextCursorPosition()*/int QTextLayout::previousCursorPosition(int oldPos, CursorMode mode) const{// qDebug("looking for previous cursor pos for %d", oldPos); const QCharAttributes *attributes = d->attributes(); if (!attributes || oldPos <= 0) return 0; oldPos--; if (mode == SkipCharacters) { while (oldPos && !attributes[oldPos].charStop) oldPos--; } else { while (oldPos && attributes[oldPos].whiteSpace) oldPos--; while (oldPos && !attributes[oldPos-1].whiteSpace && !d->atWordSeparator(oldPos - 1)) oldPos--; }// qDebug(" -> %d", oldPos); return oldPos;}/*! Returns true if position \a pos is a valid cursor position. In a Unicode context some positions in the text are not valid cursor positions, because the position is inside a Unicode surrogate or a grapheme cluster. A grapheme cluster is a sequence of two or more Unicode characters that form one indivisible entity on the screen. For example the latin character `\Auml' can be represented in Unicode by two characters, `A' (0x41), and the combining diaresis (0x308). A text cursor can only validly be positioned before or after these two characters, never between them since that wouldn't make sense. In indic languages every syllable forms a grapheme cluster.*/bool QTextLayout::isValidCursorPosition(int pos) const{ const QCharAttributes *attributes = d->attributes(); if (!attributes || pos < 0 || pos > (int)d->layoutData->string.length()) return false; return attributes[pos].charStop;}/*! Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise returns an invalid text line. The text layout creates a new line object that starts after the last line in the layout, or at the beginning if the layout is empty. The layout maintains an internal cursor, and each line is filled with text from the cursor position onwards when the QTextLine::setLineWidth() function is called. Once QTextLine::setLineWidth() is called, a new line can be created and filled with text. Repeating this process will lay out the whole block of text contained in the QTextLayout. If there is no text left to be inserted into the layout, the QTextLine returned will not be valid (isValid() will return false).*/QTextLine QTextLayout::createLine(){#ifndef QT_NO_DEBUG if (!d->layoutData || !d->layoutData->inLayout) { qWarning("QTextLayout::createLine: Called without layouting"); return QTextLine(); }#endif int l = d->lines.size(); if (l && d->lines.at(l-1).length < 0) { QTextLine(l-1, d).setNumColumns(INT_MAX); } int from = l > 0 ? d->lines.at(l-1).from + d->lines.at(l-1).length : 0; int strlen = d->layoutData->string.length(); if (l && from >= strlen) { if (!d->lines.at(l-1).length || d->layoutData->string.at(strlen - 1) != QChar::LineSeparator) return QTextLine(); } QScriptLine line; line.from = from; line.length = -1; line.justified = false; line.gridfitted = false; d->lines.append(line); return QTextLine(l, d);}/*! Returns the number of lines in this text layout. \sa lineAt()*/int QTextLayout::lineCount() const{ return d->lines.size();}/*! Returns the \a{i}-th line of text in this text layout. \sa lineCount() lineForTextPosition()*/QTextLine QTextLayout::lineAt(int i) const{ return QTextLine(i, d);}/*! Returns the line that contains the cursor position specified by \a pos. \sa isValidCursorPosition() lineAt()*/QTextLine QTextLayout::lineForTextPosition(int pos) const{ for (int i = 0; i < d->lines.size(); ++i) { const QScriptLine& line = d->lines[i]; if (line.from + (int)line.length > pos) return QTextLine(i, d); } if (!d->layoutData) d->itemize(); if (pos == d->layoutData->string.length() && d->lines.size()) return QTextLine(d->lines.size()-1, d); return QTextLine();}/*! \since 4.2 The global position of the layout. This is independent of the bounding rectangle and of the layout process. \sa setPosition()*/QPointF QTextLayout::position() const{ return d->position;}/*! Moves the text layout to point \a p. \sa position()*/void QTextLayout::setPosition(const QPointF &p){ d->position = p;}/*! The smallest rectangle that contains all the lines in the layout.*/QRectF QTextLayout::boundingRect() const{ if (d->lines.isEmpty()) return QRectF(); QFixed xmax, ymax; QFixed xmin = d->lines.at(0).x; QFixed ymin = d->lines.at(0).y; for (int i = 0; i < d->lines.size(); ++i) { const QScriptLine &si = d->lines[i]; xmin = qMin(xmin, si.x); ymin = qMin(ymin, si.y); xmax = qMax(xmax, si.x+si.width); // ### shouldn't the ascent be used in ymin??? ymax = qMax(ymax, si.y+si.ascent+si.descent+1); } return QRectF(xmin.toReal(), ymin.toReal(), (xmax-xmin).toReal(), (ymax-ymin).toReal());}/*! The minimum width the layout needs. This is the width of the layout's smallest non-breakable substring. \warning This function only returns a valid value after the layout has been done.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -