6#include "QskRichTextRenderer.h"
7#include "QskTextColors.h"
8#include "QskTextOptions.h"
10#include <qglobalstatic.h>
17#include <private/qquicktext_p.h>
18#include <private/qquicktext_p_p.h>
25 class TextItem final :
public QQuickText
35 setBottomPadding( 1 );
36 setBottomPadding( 0 );
41 setFontSizeMode( QQuickText::FixedSize );
44 setAntialiasing(
true );
45 setRenderType( QQuickText::QtRendering );
48 setMinimumPixelSize();
49 setMinimumPointSize();
52 setLineHeightMode( ... );
57 inline void setGeometry(
const QRectF& rect )
59 auto d = QQuickTextPrivate::get(
this );
61#if QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 )
62 d->heightValidFlag =
true;
63 d->widthValidFlag =
true;
65 d->heightValid =
true;
69 if ( ( d->x != rect.x() ) || ( d->y != rect.y() ) )
73 d->dirty( QQuickItemPrivate::Position );
76 if ( ( d->width != rect.width() ) || ( d->height != rect.height() ) )
78 d->height = rect.height();
79 d->width = rect.width();
80 d->dirty( QQuickItemPrivate::Size );
84 inline void setAlignment( Qt::Alignment alignment )
86 setHAlign(
static_cast< QQuickText::HAlignment
>(
int( alignment ) & 0x0f ) );
87 setVAlign(
static_cast< QQuickText::VAlignment
>(
int( alignment ) & 0xf0 ) );
93 setTextFormat(
static_cast< QQuickText::TextFormat
>( options.format() ) );
94 setElideMode(
static_cast< QQuickText::TextElideMode
>( options.elideMode() ) );
95 setMaximumLineCount( options.maximumLineCount() );
96 setWrapMode(
static_cast< QQuickText::WrapMode
>( options.wrapMode() ) );
102 QQuickTextPrivate::get(
this )->updateOnComponentComplete =
true;
112 setText( QString() );
115 inline QRectF layedOutTextRect()
const
117 auto that =
const_cast< TextItem*
>( this );
118 return QQuickTextPrivate::get( that )->layedOutTextRect;
121 void updateTextNode( QQuickWindow* window, QSGNode* parentNode )
123 QQuickItemPrivate::get(
this )->refWindow( window );
125 while ( parentNode->firstChild() )
126 delete parentNode->firstChild();
128 auto node = QQuickText::updatePaintNode(
nullptr,
nullptr );
129 node->reparentChildNodesTo( parentNode );
132 QQuickItemPrivate::get(
this )->derefWindow();
136 QSGNode* updatePaintNode( QSGNode*, UpdatePaintNodeData* )
override
148 qDeleteAll( m_hash );
151 inline TextItem* item()
153 const auto thread = QThread::currentThread();
155 QMutexLocker locker( &m_mutex );
157 auto it = m_hash.constFind( thread );
158 if ( it == m_hash.constEnd() )
160 auto textItem =
new TextItem();
161 QObject::connect( thread, &QThread::finished,
162 textItem, [
this, thread ] { removeItem( thread ); } );
164 m_hash.insert( thread, textItem );
172 void removeItem(
const QThread* thread )
174 auto textItem = m_hash.take( thread );
176 textItem->deleteLater();
180 QHash< const QThread*, TextItem* > m_hash;
189Q_GLOBAL_STATIC( TextItemMap, qskTextItemMap )
191QSizeF QskRichTextRenderer::textSize(
192 const QString& text,
const QFont& font,
const QskTextOptions& options )
194 auto& textItem = *qskTextItemMap->item();
198 textItem.setFont( font );
199 textItem.setOptions( options );
201 textItem.setWidth( -1 );
202 textItem.setText( text );
206 const QSizeF sz( textItem.implicitWidth(), textItem.implicitHeight() );
213QRectF QskRichTextRenderer::textRect(
214 const QString& text,
const QFont& font,
217 auto& textItem = *qskTextItemMap->item();
221 textItem.setFont( font );
222 textItem.setOptions( options );
223 textItem.setAlignment( Qt::Alignment() );
225 textItem.setWidth( size.width() );
226 textItem.setHeight( size.height() );
228 textItem.setText( text );
232 const auto rect = textItem.layedOutTextRect();
239void QskRichTextRenderer::updateNode(
240 const QString& text,
const QFont& font,
243 const QRectF& rect,
const QQuickItem* item, QSGTransformNode* node )
248 auto& textItem = *qskTextItemMap->item();
252 textItem.setGeometry( rect );
254 textItem.setBottomPadding( 0 );
255 textItem.setTopPadding( 0 );
256 textItem.setFont( font );
257 textItem.setOptions( options );
258 textItem.setAlignment( alignment );
260 textItem.setColor( colors.textColor );
261 textItem.setStyle(
static_cast< QQuickText::TextStyle
>( style ) );
262 textItem.setStyleColor( colors.styleColor );
263 textItem.setLinkColor( colors.linkColor );
265 textItem.setText( text );
269 if ( alignment & Qt::AlignVCenter )
278 auto d = QQuickTextPrivate::get( &textItem );
280 const qreal h = d->layedOutTextRect.height() + d->lineHeightOffset();
282 if (
static_cast< int >( rect.height() - h ) % 2 )
284 if (
static_cast< int >( h ) % 2 )
285 d->extra->bottomPadding = 1;
287 d->extra->topPadding = 1;
291 textItem.updateTextNode( item->window(), node );