qwt_text_engine.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_text_engine.h"
11 #include "qwt_math.h"
12 #include "qwt_painter.h"
13 #include <qpainter.h>
14 #include <qpixmap.h>
15 #include <qimage.h>
16 #include <qmap.h>
17 #include <qwidget.h>
18 #include <qtextobject.h>
19 #include <qtextdocument.h>
20 #include <qabstracttextdocumentlayout.h>
21 
22 static QString taggedRichText( const QString &text, int flags )
23 {
24  QString richText = text;
25 
26  // By default QSimpleRichText is Qt::AlignLeft
27  if ( flags & Qt::AlignJustify )
28  {
29  richText.prepend( QLatin1String( "<div align=\"justify\">" ) );
30  richText.append( QLatin1String ( "</div>" ) );
31  }
32  else if ( flags & Qt::AlignRight )
33  {
34  richText.prepend( QLatin1String ( "<div align=\"right\">" ) );
35  richText.append( QLatin1String ( "</div>" ) );
36  }
37  else if ( flags & Qt::AlignHCenter )
38  {
39  richText.prepend( QLatin1String ( "<div align=\"center\">" ) );
40  richText.append( QLatin1String ( "</div>" ) );
41  }
42 
43  return richText;
44 }
45 
46 class QwtRichTextDocument: public QTextDocument
47 {
48 public:
49  QwtRichTextDocument( const QString &text, int flags, const QFont &font )
50  {
51  setUndoRedoEnabled( false );
52  setDefaultFont( font );
53  setHtml( text );
54 
55  // make sure we have a document layout
56  ( void )documentLayout();
57 
58  QTextOption option = defaultTextOption();
59  if ( flags & Qt::TextWordWrap )
60  option.setWrapMode( QTextOption::WordWrap );
61  else
62  option.setWrapMode( QTextOption::NoWrap );
63 
64  option.setAlignment( static_cast<Qt::Alignment>( flags ) );
65  setDefaultTextOption( option );
66 
67  QTextFrame *root = rootFrame();
68  QTextFrameFormat fm = root->frameFormat();
69  fm.setBorder( 0 );
70  fm.setMargin( 0 );
71  fm.setPadding( 0 );
72  fm.setBottomMargin( 0 );
73  fm.setLeftMargin( 0 );
74  root->setFrameFormat( fm );
75 
76  adjustSize();
77  }
78 };
79 
81 {
82 public:
83  int effectiveAscent( const QFont &font ) const
84  {
85  const QString fontKey = font.key();
86 
87  QMap<QString, int>::const_iterator it =
88  d_ascentCache.constFind( fontKey );
89  if ( it == d_ascentCache.constEnd() )
90  {
91  int ascent = findAscent( font );
92  it = d_ascentCache.insert( fontKey, ascent );
93  }
94 
95  return ( *it );
96  }
97 
98 private:
99  static int findAscent( const QFont &font )
100  {
101  static const QString dummy( "E" );
102  static const QColor white( Qt::white );
103 
104  const QFontMetrics fm( font );
105  QPixmap pm( fm.width( dummy ), fm.height() );
106  pm.fill( white );
107 
108  QPainter p( &pm );
109  p.setFont( font );
110  p.drawText( 0, 0, pm.width(), pm.height(), 0, dummy );
111  p.end();
112 
113  const QImage img = pm.toImage();
114 
115  int row = 0;
116  for ( row = 0; row < img.height(); row++ )
117  {
118  const QRgb *line = reinterpret_cast<const QRgb *>(
119  img.scanLine( row ) );
120 
121  const int w = pm.width();
122  for ( int col = 0; col < w; col++ )
123  {
124  if ( line[col] != white.rgb() )
125  return fm.ascent() - row + 1;
126  }
127  }
128 
129  return fm.ascent();
130  }
131 
132  mutable QMap<QString, int> d_ascentCache;
133 };
134 
137 {
138 }
139 
142 {
143 }
144 
147 {
148  d_data = new PrivateData;
149 }
150 
153 {
154  delete d_data;
155 }
156 
167 double QwtPlainTextEngine::heightForWidth( const QFont& font, int flags,
168  const QString& text, double width ) const
169 {
170  const QFontMetricsF fm( font );
171  const QRectF rect = fm.boundingRect(
172  QRectF( 0, 0, width, QWIDGETSIZE_MAX ), flags, text );
173 
174  return rect.height();
175 }
176 
186 QSizeF QwtPlainTextEngine::textSize( const QFont &font,
187  int flags, const QString& text ) const
188 {
189  const QFontMetricsF fm( font );
190  const QRectF rect = fm.boundingRect(
191  QRectF( 0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ), flags, text );
192 
193  return rect.size();
194 }
195 
205 void QwtPlainTextEngine::textMargins( const QFont &font, const QString &,
206  double &left, double &right, double &top, double &bottom ) const
207 {
208  left = right = top = 0;
209 
210  const QFontMetricsF fm( font );
211  top = fm.ascent() - d_data->effectiveAscent( font );
212  bottom = fm.descent();
213 }
214 
225 void QwtPlainTextEngine::draw( QPainter *painter, const QRectF &rect,
226  int flags, const QString& text ) const
227 {
228  QwtPainter::drawText( painter, rect, flags, text );
229 }
230 
235 bool QwtPlainTextEngine::mightRender( const QString & ) const
236 {
237  return true;
238 }
239 
240 #ifndef QT_NO_RICHTEXT
241 
244 {
245 }
246 
257 double QwtRichTextEngine::heightForWidth( const QFont& font, int flags,
258  const QString& text, double width ) const
259 {
260  QwtRichTextDocument doc( text, flags, font );
261 
262  doc.setPageSize( QSizeF( width, QWIDGETSIZE_MAX ) );
263  return doc.documentLayout()->documentSize().height();
264 }
265 
276 QSizeF QwtRichTextEngine::textSize( const QFont &font,
277  int flags, const QString& text ) const
278 {
279  QwtRichTextDocument doc( text, flags, font );
280 
281  QTextOption option = doc.defaultTextOption();
282  if ( option.wrapMode() != QTextOption::NoWrap )
283  {
284  option.setWrapMode( QTextOption::NoWrap );
285  doc.setDefaultTextOption( option );
286  doc.adjustSize();
287  }
288 
289  return doc.size();
290 }
291 
300 void QwtRichTextEngine::draw( QPainter *painter, const QRectF &rect,
301  int flags, const QString& text ) const
302 {
303  QwtRichTextDocument doc( text, flags, painter->font() );
304  QwtPainter::drawSimpleRichText( painter, rect, flags, doc );
305 }
306 
315 QString QwtRichTextEngine::taggedText( const QString &text, int flags ) const
316 {
317  return taggedRichText( text, flags );
318 }
319 
326 bool QwtRichTextEngine::mightRender( const QString &text ) const
327 {
328  return Qt::mightBeRichText( text );
329 }
330 
339 void QwtRichTextEngine::textMargins( const QFont &, const QString &,
340  double &left, double &right, double &top, double &bottom ) const
341 {
342  left = right = top = bottom = 0;
343 }
344 
345 #endif // !QT_NO_RICHTEXT
QwtRichTextEngine()
Constructor.
virtual bool mightRender(const QString &) const
virtual void draw(QPainter *painter, const QRectF &rect, int flags, const QString &text) const
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const
static void drawText(QPainter *, double x, double y, const QString &)
Wrapper for QPainter::drawText()
virtual ~QwtPlainTextEngine()
Destructor.
static QString taggedRichText(const QString &text, int flags)
static void drawSimpleRichText(QPainter *, const QRectF &, int flags, const QTextDocument &)
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const
virtual void draw(QPainter *painter, const QRectF &rect, int flags, const QString &text) const
Draw the text in a clipping rectangle.
QwtPlainTextEngine()
Constructor.
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const
QwtRichTextDocument(const QString &text, int flags, const QFont &font)
TFSIMD_FORCE_INLINE const tfScalar & w() const
int effectiveAscent(const QFont &font) const
virtual bool mightRender(const QString &) const
static int findAscent(const QFont &font)
QString taggedText(const QString &, int flags) const
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const
QwtTextEngine()
Constructor.
QMap< QString, int > d_ascentCache
virtual ~QwtTextEngine()
Destructor.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18