10#include "qwt_text_engine.h"
11#include "qwt_painter.h"
18#include <qtextobject.h>
19#include <qtextdocument.h>
20#include <qabstracttextdocumentlayout.h>
22static QString taggedRichText(
const QString& text,
int flags )
24 QString richText = text;
27 if ( flags & Qt::AlignJustify )
29 richText.prepend( QLatin1String(
"<div align=\"justify\">" ) );
30 richText.append( QLatin1String (
"</div>" ) );
32 else if ( flags & Qt::AlignRight )
34 richText.prepend( QLatin1String (
"<div align=\"right\">" ) );
35 richText.append( QLatin1String (
"</div>" ) );
37 else if ( flags & Qt::AlignHCenter )
39 richText.prepend( QLatin1String (
"<div align=\"center\">" ) );
40 richText.append( QLatin1String (
"</div>" ) );
48 class QwtRichTextDocument :
public QTextDocument
51 QwtRichTextDocument(
const QString& text,
int flags,
const QFont& font )
53 setUndoRedoEnabled(
false );
54 setDefaultFont( font );
58 ( void )documentLayout();
60 QTextOption option = defaultTextOption();
61 if ( flags & Qt::TextWordWrap )
62 option.setWrapMode( QTextOption::WordWrap );
64 option.setWrapMode( QTextOption::NoWrap );
66 option.setAlignment(
static_cast< Qt::Alignment
>( flags ) );
67 setDefaultTextOption( option );
69 QTextFrame* root = rootFrame();
70 QTextFrameFormat fm = root->frameFormat();
74 fm.setBottomMargin( 0 );
75 fm.setLeftMargin( 0 );
76 root->setFrameFormat( fm );
83class QwtPlainTextEngine::PrivateData
86 int effectiveAscent(
const QFont& font )
const
88 const QString fontKey = font.key();
91 m_ascentCache.constFind( fontKey );
93 if ( it != m_ascentCache.constEnd() )
96 const int ascent = findAscent( font );
97 m_ascentCache.insert( fontKey, ascent );
103 static int findAscent(
const QFont& font )
105 static const QString dummy(
"E" );
106 static const QColor white( Qt::white );
108 const QFontMetrics fm( font );
115 p.drawText( 0, 0, pm.width(), pm.height(), 0, dummy );
118 const QImage img = pm.toImage();
121 for ( row = 0; row < img.height(); row++ )
123 const QRgb* line =
reinterpret_cast< const QRgb*
>(
124 img.scanLine( row ) );
126 const int w = pm.width();
127 for (
int col = 0; col < w; col++ )
129 if ( line[col] != white.rgb() )
130 return fm.ascent() - row + 1;
153 m_data =
new PrivateData;
173 const QString& text,
double width )
const
175 const QFontMetricsF fm( font );
176 const QRectF rect = fm.boundingRect(
177 QRectF( 0, 0, width, QWIDGETSIZE_MAX ), flags, text );
179 return rect.height();
192 int flags,
const QString& text )
const
194 const QFontMetricsF fm( font );
195 const QRectF rect = fm.boundingRect(
196 QRectF( 0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ), flags, text );
211 double& left,
double& right,
double& top,
double& bottom )
const
213 left = right = top = 0;
215 const QFontMetricsF fm( font );
216 top = fm.ascent() - m_data->effectiveAscent( font );
217 bottom = fm.descent();
231 int flags,
const QString& text )
const
245#ifndef QT_NO_RICHTEXT
263 const QString& text,
double width )
const
265 QwtRichTextDocument doc( text, flags, font );
267 doc.setPageSize( QSizeF( width, QWIDGETSIZE_MAX ) );
268 return doc.documentLayout()->documentSize().height();
282 int flags,
const QString& text )
const
284 QwtRichTextDocument doc( text, flags, font );
286 QTextOption option = doc.defaultTextOption();
287 if ( option.wrapMode() != QTextOption::NoWrap )
289 option.setWrapMode( QTextOption::NoWrap );
290 doc.setDefaultTextOption( option );
306 int flags,
const QString& text )
const
308 QwtRichTextDocument doc( text, flags, painter->font() );
320QString QwtRichTextEngine::taggedText(
const QString& text,
int flags )
const
322 return taggedRichText( text, flags );
333 return Qt::mightBeRichText( text );
345 double& left,
double& right,
double& top,
double& bottom )
const
347 left = right = top = bottom = 0;
static void drawSimpleRichText(QPainter *, const QRectF &, int flags, const QTextDocument &)
static void drawText(QPainter *, qreal x, qreal y, const QString &)
Wrapper for QPainter::drawText()
static int horizontalAdvance(const QFontMetrics &, const QString &)
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const override
QwtPlainTextEngine()
Constructor.
virtual ~QwtPlainTextEngine()
Destructor.
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const override
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const override
virtual void draw(QPainter *, const QRectF &rect, int flags, const QString &text) const override
Draw the text in a clipping rectangle.
virtual bool mightRender(const QString &) const override
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const override
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const override
virtual bool mightRender(const QString &) const override
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const override
QwtRichTextEngine()
Constructor.
virtual void draw(QPainter *, const QRectF &rect, int flags, const QString &text) const override
QwtTextEngine()
Constructor.
virtual ~QwtTextEngine()
Destructor.