Qt 4.8
Public Types | Public Functions | Properties | Static Private Attributes | List of all members
QSvgText Class Reference

#include <qsvggraphics_p.h>

Inheritance diagram for QSvgText:
QSvgNode

Public Types

enum  WhitespaceMode { Default, Preserve }
 
- Public Types inherited from QSvgNode
enum  DisplayMode {
  InlineMode, BlockMode, ListItemMode, RunInMode,
  CompactMode, MarkerMode, TableMode, InlineTableMode,
  TableRowGroupMode, TableHeaderGroupMode, TableFooterGroupMode, TableRowMode,
  TableColumnGroupMode, TableColumnMode, TableCellMode, TableCaptionMode,
  NoneMode, InheritMode
}
 
enum  Type {
  DOC, G, DEFS, SWITCH,
  ANIMATION, ARC, CIRCLE, ELLIPSE,
  IMAGE, LINE, PATH, POLYGON,
  POLYLINE, RECT, TEXT, TEXTAREA,
  TSPAN, USE, VIDEO
}
 

Public Functions

void addLineBreak ()
 
void addText (const QString &text)
 
void addTspan (QSvgTspan *tspan)
 
virtual void draw (QPainter *p, QSvgExtraStates &states)
 
 QSvgText (QSvgNode *parent, const QPointF &coord)
 
void setTextArea (const QSizeF &size)
 
void setWhitespaceMode (WhitespaceMode mode)
 
virtual Type type () const
 
 ~QSvgText ()
 
- Public Functions inherited from QSvgNode
void appendStyleProperty (QSvgStyleProperty *prop, const QString &id)
 
void applyStyle (QPainter *p, QSvgExtraStates &states) const
 
virtual QRectF bounds (QPainter *p, QSvgExtraStates &states) const
 
DisplayMode displayMode () const
 
QSvgTinyDocumentdocument () const
 
bool isVisible () const
 
QString nodeId () const
 
QSvgNodeparent () const
 
 QSvgNode (QSvgNode *parent=0)
 
const QStringListrequiredExtensions () const
 
const QStringListrequiredFeatures () const
 
const QStringListrequiredFonts () const
 
const QStringListrequiredFormats () const
 
const QStringListrequiredLanguages () const
 
void revertStyle (QPainter *p, QSvgExtraStates &states) const
 
void setDisplayMode (DisplayMode display)
 
void setNodeId (const QString &i)
 
void setRequiredExtensions (const QStringList &lst)
 
void setRequiredFeatures (const QStringList &lst)
 
void setRequiredFonts (const QStringList &lst)
 
void setRequiredFormats (const QStringList &lst)
 
void setRequiredLanguages (const QStringList &lst)
 
void setVisible (bool visible)
 
void setXmlClass (const QString &str)
 
QSvgStylePropertystyleProperty (QSvgStyleProperty::Type type) const
 
QSvgFillStylePropertystyleProperty (const QString &id) const
 
virtual QRectF transformedBounds (QPainter *p, QSvgExtraStates &states) const
 
QRectF transformedBounds () const
 
QString xmlClass () const
 
virtual ~QSvgNode ()
 

Properties

QPointF m_coord
 
WhitespaceMode m_mode
 
QSizeF m_size
 
QVector< QSvgTspan * > m_tspans
 
Type m_type
 

Static Private Attributes

static QSvgTspan *const LINEBREAK = 0
 

Additional Inherited Members

- Static Protected Functions inherited from QSvgNode
static qreal strokeWidth (QPainter *p)
 
- Protected Variables inherited from QSvgNode
QSvgStyle m_style
 

Detailed Description

Definition at line 181 of file qsvggraphics_p.h.

Enumerations

◆ WhitespaceMode

Enumerator
Default 
Preserve 

Definition at line 184 of file qsvggraphics_p.h.

Constructors and Destructors

◆ QSvgText()

QSvgText::QSvgText ( QSvgNode parent,
const QPointF coord 
)

Definition at line 265 of file qsvggraphics.cpp.

266  : QSvgNode(parent)
267  , m_coord(coord)
268  , m_type(TEXT)
269  , m_size(0, 0)
270  , m_mode(Default)
271 {
272 }
QSvgNode(QSvgNode *parent=0)
Definition: qsvgnode.cpp:52
QSizeF m_size
WhitespaceMode m_mode
QPointF m_coord

◆ ~QSvgText()

QSvgText::~QSvgText ( )

Definition at line 274 of file qsvggraphics.cpp.

275 {
276  for (int i = 0; i < m_tspans.size(); ++i) {
277  if (m_tspans[i] != LINEBREAK)
278  delete m_tspans[i];
279  }
280 }
QVector< QSvgTspan * > m_tspans
static QSvgTspan *const LINEBREAK
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

Functions

◆ addLineBreak()

void QSvgText::addLineBreak ( )
inline

Definition at line 199 of file qsvggraphics_p.h.

QVector< QSvgTspan * > m_tspans
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
static QSvgTspan *const LINEBREAK

◆ addText()

void QSvgText::addText ( const QString text)

Definition at line 459 of file qsvggraphics.cpp.

460 {
461  m_tspans.append(new QSvgTspan(this, false));
463  m_tspans.back()->addText(text);
464 }
void addText(const QString &text)
QVector< QSvgTspan * > m_tspans
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
WhitespaceMode m_mode
reference back()
This function is provided for STL compatibility.
Definition: qvector.h:289
void setWhitespaceMode(QSvgText::WhitespaceMode mode)

◆ addTspan()

void QSvgText::addTspan ( QSvgTspan tspan)
inline

Definition at line 197 of file qsvggraphics_p.h.

197 {m_tspans.append(tspan);}
QVector< QSvgTspan * > m_tspans
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573

◆ draw()

void QSvgText::draw ( QPainter p,
QSvgExtraStates states 
)
virtual

Implements QSvgNode.

Definition at line 290 of file qsvggraphics.cpp.

291 {
292  applyStyle(p, states);
293  qreal oldOpacity = p->opacity();
294  p->setOpacity(oldOpacity * states.fillOpacity);
295 
296  // Force the font to have a size of 100 pixels to avoid truncation problems
297  // when the font is very small.
298  qreal scale = 100.0 / p->font().pointSizeF();
299  Qt::Alignment alignment = states.textAnchor;
300 
301  QTransform oldTransform = p->worldTransform();
302  p->scale(1 / scale, 1 / scale);
303 
304  qreal y = 0;
305  bool initial = true;
306  qreal px = m_coord.x() * scale;
307  qreal py = m_coord.y() * scale;
308  QSizeF scaledSize = m_size * scale;
309 
310  if (m_type == TEXTAREA) {
311  if (alignment == Qt::AlignHCenter)
312  px += scaledSize.width() / 2;
313  else if (alignment == Qt::AlignRight)
314  px += scaledSize.width();
315  }
316 
317  QRectF bounds;
318  if (m_size.height() != 0)
319  bounds = QRectF(0, py, 1, scaledSize.height()); // x and width are not used.
320 
321  bool appendSpace = false;
322  QVector<QString> paragraphs;
323  QStack<QTextCharFormat> formats;
325  paragraphs.push_back(QString());
327 
328  for (int i = 0; i < m_tspans.size(); ++i) {
329  if (m_tspans[i] == LINEBREAK) {
330  if (m_type == TEXTAREA) {
331  if (paragraphs.back().isEmpty()) {
332  QFont font = p->font();
333  font.setPixelSize(font.pointSizeF() * scale);
334 
336  range.start = 0;
337  range.length = 1;
338  range.format.setFont(font);
339  formatRanges.back().append(range);
340 
341  paragraphs.back().append(QLatin1Char(' '));;
342  }
343  appendSpace = false;
344  paragraphs.push_back(QString());
346  }
347  } else {
348  WhitespaceMode mode = m_tspans[i]->whitespaceMode();
349  m_tspans[i]->applyStyle(p, states);
350 
351  QFont font = p->font();
352  font.setPixelSize(font.pointSizeF() * scale);
353 
354  QString newText(m_tspans[i]->text());
355  newText.replace(QLatin1Char('\t'), QLatin1Char(' '));
356  newText.replace(QLatin1Char('\n'), QLatin1Char(' '));
357 
358  bool prependSpace = !appendSpace && !m_tspans[i]->isTspan() && (mode == Default) && !paragraphs.back().isEmpty() && newText.startsWith(QLatin1Char(' '));
359  if (appendSpace || prependSpace)
360  paragraphs.back().append(QLatin1Char(' '));
361 
362  bool appendSpaceNext = (!m_tspans[i]->isTspan() && (mode == Default) && newText.endsWith(QLatin1Char(' ')));
363 
364  if (mode == Default) {
365  newText = newText.simplified();
366  if (newText.isEmpty())
367  appendSpaceNext = false;
368  }
369 
371  range.start = paragraphs.back().length();
372  range.length = newText.length();
373  range.format.setFont(font);
374  range.format.setTextOutline(p->pen());
375  range.format.setForeground(p->brush());
376 
377  if (appendSpace) {
378  Q_ASSERT(!formatRanges.back().isEmpty());
379  ++formatRanges.back().back().length;
380  } else if (prependSpace) {
381  --range.start;
382  ++range.length;
383  }
384  formatRanges.back().append(range);
385 
386  appendSpace = appendSpaceNext;
387  paragraphs.back() += newText;
388 
389  m_tspans[i]->revertStyle(p, states);
390  }
391  }
392 
393  if (states.svgFont) {
394  // SVG fonts not fully supported...
395  QString text = paragraphs.front();
396  for (int i = 1; i < paragraphs.size(); ++i) {
397  text.append(QLatin1Char('\n'));
398  text.append(paragraphs[i]);
399  }
400  states.svgFont->draw(p, m_coord * scale, text, p->font().pointSizeF() * scale, states.textAnchor);
401  } else {
402  for (int i = 0; i < paragraphs.size(); ++i) {
403  QTextLayout tl(paragraphs[i]);
404  QTextOption op = tl.textOption();
406  tl.setTextOption(op);
407  tl.setAdditionalFormats(formatRanges[i]);
408  tl.beginLayout();
409 
410  forever {
411  QTextLine line = tl.createLine();
412  if (!line.isValid())
413  break;
414  if (m_size.width() != 0)
415  line.setLineWidth(scaledSize.width());
416  }
417  tl.endLayout();
418 
419  bool endOfBoundsReached = false;
420  for (int i = 0; i < tl.lineCount(); ++i) {
421  QTextLine line = tl.lineAt(i);
422 
423  qreal x = 0;
424  if (alignment == Qt::AlignHCenter)
425  x -= 0.5 * line.naturalTextWidth();
426  else if (alignment == Qt::AlignRight)
427  x -= line.naturalTextWidth();
428 
429  if (initial && m_type == TEXT)
430  y -= line.ascent();
431  initial = false;
432 
433  line.setPosition(QPointF(x, y));
434 
435  // Check if the current line fits into the bounding rectangle.
436  if ((m_size.width() != 0 && line.naturalTextWidth() > scaledSize.width())
437  || (m_size.height() != 0 && y + line.height() > scaledSize.height())) {
438  // I need to set the bounds height to 'y-epsilon' to avoid drawing the current
439  // line. Since the font is scaled to 100 units, 1 should be a safe epsilon.
440  bounds.setHeight(y - 1);
441  endOfBoundsReached = true;
442  break;
443  }
444 
445  y += 1.1 * line.height();
446  }
447  tl.draw(p, QPointF(px, py), QVector<QTextLayout::FormatRange>(), bounds);
448 
449  if (endOfBoundsReached)
450  break;
451  }
452  }
453 
454  p->setWorldTransform(oldTransform, false);
455  p->setOpacity(oldOpacity);
456  revertStyle(p, states);
457 }
The QTextLayout::FormatRange structure is used to apply extra formatting information for a specified ...
Definition: qtextlayout.h:128
void setTextOutline(const QPen &pen)
Sets the pen used to draw the outlines of characters to the given pen.
Definition: qtextformat.h:489
double qreal
Definition: qglobal.h:1193
void setHeight(qreal h)
Sets the height of the rectangle to the given height.
Definition: qrect.h:787
qreal opacity() const
Returns the opacity of the painter.
Definition: qpainter.cpp:2115
void setPosition(const QPointF &pos)
Moves the line to position pos.
qreal width() const
Returns the width.
Definition: qsize.h:284
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal height() const
Returns the height.
Definition: qsize.h:287
The QTextLine class represents a line of text inside a QTextLayout.
Definition: qtextlayout.h:197
void setFont(const QFont &font)
Sets the text format&#39;s font.
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
void setLineWidth(qreal width)
Lays out the line with the given width.
int start
Specifies the beginning of the format range within the text layout&#39;s text.
Definition: qtextlayout.h:129
void applyStyle(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:118
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isValid() const
Returns true if this text line is valid; otherwise returns false.
Definition: qtextlayout.h:201
qreal ascent() const
Returns the line&#39;s ascent.
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
virtual QRectF bounds(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:199
QVector< QSvgTspan * > m_tspans
const QPen & pen() const
Returns the painter&#39;s current pen.
Definition: qpainter.cpp:4152
qreal pointSizeF() const
Returns the point size of the font.
Definition: qfont.cpp:1142
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const QFont & font() const
Returns the currently set font used for drawing text.
Definition: qpainter.cpp:4312
QSizeF m_size
QSvgFont * svgFont
Definition: qsvgstyle_p.h:147
bool endsWith(const T &t) const
Returns true if this vector is not empty and its last item is equal to value; otherwise returns false...
Definition: qvector.h:265
T & front()
This function is provided for STL compatibility.
Definition: qvector.h:287
const QBrush & brush() const
Returns the painter&#39;s current brush.
Definition: qpainter.cpp:4232
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
QString & append(QChar c)
Definition: qstring.cpp:1777
const QTransform & worldTransform() const
Returns the world transformation matrix.
Definition: qpainter.cpp:9652
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
QPointF m_coord
qreal naturalTextWidth() const
Returns the width of the line that is occupied by text.
void draw(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, Qt::Alignment alignment) const
Definition: qsvgfont.cpp:79
reference back()
This function is provided for STL compatibility.
Definition: qvector.h:289
static QSvgTspan *const LINEBREAK
void push_back(const T &t)
This function is provided for STL compatibility.
Definition: qvector.h:281
The QTextOption class provides a description of general rich text properties.
Definition: qtextoption.h:59
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630
Qt::Alignment textAnchor
Definition: qsvgstyle_p.h:148
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
void scale(qreal sx, qreal sy)
Scales the coordinate system by ({sx}, {sy}).
Definition: qpainter.cpp:3234
int length
Specifies the numer of characters the format range spans.
Definition: qtextlayout.h:130
QTextCharFormat format
Specifies the format to apply.
Definition: qtextlayout.h:131
void setWrapMode(WrapMode wrap)
Sets the option&#39;s text wrap mode to the given mode.
Definition: qtextoption.h:109
void setForeground(const QBrush &brush)
Sets the foreground brush to the specified brush.
Definition: qtextformat.h:350
void setOpacity(qreal opacity)
Sets the opacity of the painter to opacity.
Definition: qpainter.cpp:2139
void setPixelSize(int)
Sets the font size to pixelSize pixels.
Definition: qfont.cpp:1156
void revertStyle(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:123
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
qreal height() const
Returns the line&#39;s height.
#define text
Definition: qobjectdefs.h:80
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

◆ setTextArea()

void QSvgText::setTextArea ( const QSizeF size)

Definition at line 282 of file qsvggraphics.cpp.

Referenced by createTextAreaNode().

283 {
284  m_size = size;
285  m_type = TEXTAREA;
286 }
QSizeF m_size

◆ setWhitespaceMode()

void QSvgText::setWhitespaceMode ( WhitespaceMode  mode)
inline

Definition at line 200 of file qsvggraphics_p.h.

200 {m_mode = mode;}
WhitespaceMode m_mode

◆ type()

QSvgNode::Type QSvgText::type ( ) const
virtual

Implements QSvgNode.

Definition at line 544 of file qsvggraphics.cpp.

545 {
546  return m_type;
547 }

Properties

◆ LINEBREAK

QSvgTspan *const QSvgText::LINEBREAK = 0
staticprivate

Definition at line 204 of file qsvggraphics_p.h.

Referenced by QSvgRect::draw(), draw(), and ~QSvgText().

◆ m_coord

QPointF QSvgText::m_coord
private

Definition at line 206 of file qsvggraphics_p.h.

Referenced by draw().

◆ m_mode

WhitespaceMode QSvgText::m_mode
private

Definition at line 214 of file qsvggraphics_p.h.

Referenced by addText().

◆ m_size

QSizeF QSvgText::m_size
private

Definition at line 213 of file qsvggraphics_p.h.

Referenced by draw(), and setTextArea().

◆ m_tspans

QVector<QSvgTspan *> QSvgText::m_tspans
private

Definition at line 210 of file qsvggraphics_p.h.

Referenced by addText(), draw(), and ~QSvgText().

◆ m_type

Type QSvgText::m_type
private

Definition at line 212 of file qsvggraphics_p.h.

Referenced by draw(), and setTextArea().


The documentation for this class was generated from the following files: