Qt 4.8
Classes | Typedefs | Functions | Variables
qpicture.cpp File Reference
#include "qpicture.h"
#include <private/qpicture_p.h>
#include <private/qfactoryloader_p.h>
#include <private/qpaintengine_pic_p.h>
#include <private/qfont_p.h>
#include "qdatastream.h"
#include "qfile.h"
#include "qimage.h"
#include "qmutex.h"
#include "qpainter.h"
#include "qpainterpath.h"
#include "qpixmap.h"
#include "qregion.h"
#include "qdebug.h"
#include "qregexp.h"
#include "qapplication.h"
#include "qpictureformatplugin.h"

Go to the source code of this file.

Classes

class  QFakeDevice
 
class  QPictureHandler
 
struct  QPictureIOData
 

Typedefs

typedef QList< QPictureHandler * > QPHList
 

Functions

static void cleanup ()
 
static QPictureHandlerget_picture_handler (const char *format)
 
 Q_GLOBAL_STATIC_WITH_ARGS (QFactoryLoader, factoryLoader,(QPictureFormatInterface_iid, QLatin1String("/pictureformats"))) void qt_init_picture_plugins()
 
void qt_format_text (const QFont &fnt, const QRectF &_r, int tf, const QTextOption *opt, const QString &str, QRectF *brect, int tabstops, int *, int tabarraylen, QPainter *painter)
 
void qt_init_picture_handlers ()
 
static QStringList qToStringList (const QList< QByteArray > arr)
 

Variables

static const quint16 mfhdr_maj = 11
 
static const quint16 mfhdr_min = 0
 
const char * qt_mfhdr_tag = "QPIC"
 

Typedef Documentation

◆ QPHList

Definition at line 1487 of file qpicture.cpp.

Function Documentation

◆ cleanup()

static void cleanup ( )
static

◆ get_picture_handler()

static QPictureHandler* get_picture_handler ( const char *  format)
static

Definition at line 1525 of file qpicture.cpp.

Referenced by QPictureIO::read(), and QPictureIO::write().

1526 { // get pointer to handler
1528  qt_init_picture_plugins();
1529  if (QPHList *list = pictureHandlers()) {
1530  for (int i = 0; i < list->size(); ++i) {
1531  if (list->at(i)->format == format)
1532  return list->at(i);
1533  }
1534  }
1535  return 0; // no such handler
1536 }
void qt_init_picture_handlers()
Definition: qpicture.cpp:1517
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ Q_GLOBAL_STATIC_WITH_ARGS()

Q_GLOBAL_STATIC_WITH_ARGS ( QFactoryLoader  ,
factoryLoader  ,
(QPictureFormatInterface_iid, QLatin1String("/pictureformats"))   
)

Definition at line 1492 of file qpicture.cpp.

1497 {
1498 #ifndef QT_NO_LIBRARY
1499  QMutexLocker locker(mutex());
1500  QFactoryLoader *loader = factoryLoader();
1501  QStringList keys = loader->keys();
1502  for (int i = 0; i < keys.count(); ++i)
1503  if (QPictureFormatInterface *format = qobject_cast<QPictureFormatInterface*>(loader->instance(keys.at(i))))
1504  format->installIOHandler(keys.at(i));
1505 #endif
1506 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QStringList keys
QStringList keys() const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QObject * instance(const QString &key) const

◆ qt_format_text()

void qt_format_text ( const QFont fnt,
const QRectF _r,
int  tf,
const QTextOption opt,
const QString str,
QRectF brect,
int  tabstops,
int *  ta,
int  tabarraylen,
QPainter painter 
)

Definition at line 8458 of file qpainter.cpp.

Referenced by QPicture::exec().

8462 {
8463 
8464  Q_ASSERT( !((tf & ~Qt::TextDontPrint)!=0 && option!=0) ); // we either have an option or flags
8465 
8466  if (option) {
8467  tf |= option->alignment();
8468  if (option->wrapMode() != QTextOption::NoWrap)
8469  tf |= Qt::TextWordWrap;
8470 
8471  if (option->flags() & QTextOption::IncludeTrailingSpaces)
8473 
8474  if (option->tabStop() >= 0 || !option->tabArray().isEmpty())
8475  tf |= Qt::TextExpandTabs;
8476  }
8477 
8478  // we need to copy r here to protect against the case (&r == brect).
8479  QRectF r(_r);
8480 
8481  bool dontclip = (tf & Qt::TextDontClip);
8482  bool wordwrap = (tf & Qt::TextWordWrap) || (tf & Qt::TextWrapAnywhere);
8483  bool singleline = (tf & Qt::TextSingleLine);
8484  bool showmnemonic = (tf & Qt::TextShowMnemonic);
8485  bool hidemnmemonic = (tf & Qt::TextHideMnemonic);
8486 
8488  if (tf & Qt::TextForceLeftToRight)
8489  layout_direction = Qt::LeftToRight;
8490  else if (tf & Qt::TextForceRightToLeft)
8491  layout_direction = Qt::RightToLeft;
8492  else if (option)
8493  layout_direction = option->textDirection();
8494  else if (painter)
8495  layout_direction = painter->layoutDirection();
8496  else
8497  layout_direction = Qt::LeftToRight;
8498 
8499  tf = QStyle::visualAlignment(layout_direction, QFlag(tf));
8500 
8501  bool isRightToLeft = layout_direction == Qt::RightToLeft;
8502  bool expandtabs = ((tf & Qt::TextExpandTabs) &&
8503  (((tf & Qt::AlignLeft) && !isRightToLeft) ||
8504  ((tf & Qt::AlignRight) && isRightToLeft)));
8505 
8506  if (!painter)
8507  tf |= Qt::TextDontPrint;
8508 
8509  uint maxUnderlines = 0;
8510  int numUnderlines = 0;
8511  QVarLengthArray<int, 32> underlinePositions(1);
8512 
8513  QFontMetricsF fm(fnt);
8514  QString text = str;
8515  int offset = 0;
8516 start_lengthVariant:
8517  bool hasMoreLengthVariants = false;
8518  // compatible behaviour to the old implementation. Replace
8519  // tabs by spaces
8520  int old_offset = offset;
8521  for (; offset < text.length(); offset++) {
8522  QChar chr = text.at(offset);
8523  if (chr == QLatin1Char('\r') || (singleline && chr == QLatin1Char('\n'))) {
8524  text[offset] = QLatin1Char(' ');
8525  } else if (chr == QLatin1Char('\n')) {
8526  text[offset] = QChar::LineSeparator;
8527  } else if (chr == QLatin1Char('&')) {
8528  ++maxUnderlines;
8529  } else if (chr == QLatin1Char('\t')) {
8530  if (!expandtabs) {
8531  text[offset] = QLatin1Char(' ');
8532  } else if (!tabarraylen && !tabstops) {
8533  tabstops = qRound(fm.width(QLatin1Char('x'))*8);
8534  }
8535  } else if (chr == QChar(ushort(0x9c))) {
8536  // string with multiple length variants
8537  hasMoreLengthVariants = true;
8538  break;
8539  }
8540  }
8541 
8542  int length = offset - old_offset;
8543  if ((hidemnmemonic || showmnemonic) && maxUnderlines > 0) {
8544  underlinePositions.resize(maxUnderlines + 1);
8545 
8546  QChar *cout = text.data() + old_offset;
8547  QChar *cin = cout;
8548  int l = length;
8549  while (l) {
8550  if (*cin == QLatin1Char('&')) {
8551  ++cin;
8552  --length;
8553  --l;
8554  if (!l)
8555  break;
8556  if (*cin != QLatin1Char('&') && !hidemnmemonic)
8557  underlinePositions[numUnderlines++] = cout - text.data() - old_offset;
8558  }
8559  *cout = *cin;
8560  ++cout;
8561  ++cin;
8562  --l;
8563  }
8564  }
8565 
8566  // no need to do extra work for underlines if we don't paint
8567  if (tf & Qt::TextDontPrint)
8568  numUnderlines = 0;
8569 
8570  underlinePositions[numUnderlines] = -1;
8571  qreal height = 0;
8572  qreal width = 0;
8573 
8574  QString finalText = text.mid(old_offset, length);
8575  QStackTextEngine engine(finalText, fnt);
8576  if (option) {
8577  engine.option = *option;
8578  }
8579 
8580  if (engine.option.tabStop() < 0 && tabstops > 0)
8581  engine.option.setTabStop(tabstops);
8582 
8583  if (engine.option.tabs().isEmpty() && ta) {
8584  QList<qreal> tabs;
8585  for (int i = 0; i < tabarraylen; i++)
8586  tabs.append(qreal(ta[i]));
8587  engine.option.setTabArray(tabs);
8588  }
8589 
8590  engine.option.setTextDirection(layout_direction);
8591  if (tf & Qt::AlignJustify)
8592  engine.option.setAlignment(Qt::AlignJustify);
8593  else
8594  engine.option.setAlignment(Qt::AlignLeft); // do not do alignment twice
8595 
8596  if (!option && (tf & Qt::TextWrapAnywhere))
8597  engine.option.setWrapMode(QTextOption::WrapAnywhere);
8598 
8599  if (tf & Qt::TextJustificationForced)
8600  engine.forceJustification = true;
8601  QTextLayout textLayout(&engine);
8602  textLayout.setCacheEnabled(true);
8603  textLayout.engine()->underlinePositions = underlinePositions.data();
8604 
8605  if (finalText.isEmpty()) {
8606  height = fm.height();
8607  width = 0;
8608  tf |= Qt::TextDontPrint;
8609  } else {
8610  qreal lineWidth = 0x01000000;
8611  if (wordwrap || (tf & Qt::TextJustificationForced))
8612  lineWidth = qMax<qreal>(0, r.width());
8613  if(!wordwrap)
8615  textLayout.engine()->ignoreBidi = bool(tf & Qt::TextDontPrint);
8616  textLayout.beginLayout();
8617 
8618  qreal leading = fm.leading();
8619  height = -leading;
8620 
8621  while (1) {
8622  QTextLine l = textLayout.createLine();
8623  if (!l.isValid())
8624  break;
8625 
8626  l.setLineWidth(lineWidth);
8627  height += leading;
8628  l.setPosition(QPointF(0., height));
8629  height += l.height();
8630  width = qMax(width, l.naturalTextWidth());
8631  if (!dontclip && !brect && height >= r.height())
8632  break;
8633  }
8634  textLayout.endLayout();
8635  }
8636 
8637  qreal yoff = 0;
8638  qreal xoff = 0;
8639  if (tf & Qt::AlignBottom) {
8640  yoff = r.height() - height;
8641  } else if (tf & Qt::AlignVCenter) {
8642  yoff = (r.height() - height)/2;
8643  if (painter) {
8645  if (type <= QTransform::TxScale) {
8646  // do the rounding manually to work around inconsistencies
8647  // in the paint engines when drawing on floating point offsets
8648  const qreal scale = painter->transform().m22();
8649  if (scale != 0)
8650  yoff = -qRound(-yoff * scale) / scale;
8651  }
8652  }
8653  }
8654  if (tf & Qt::AlignRight) {
8655  xoff = r.width() - width;
8656  } else if (tf & Qt::AlignHCenter) {
8657  xoff = (r.width() - width)/2;
8658  if (painter) {
8659  QTransform::TransformationType type = painter->transform().type();
8660  if (type <= QTransform::TxScale) {
8661  // do the rounding manually to work around inconsistencies
8662  // in the paint engines when drawing on floating point offsets
8663  const qreal scale = painter->transform().m11();
8664  if (scale != 0)
8665  xoff = qRound(xoff * scale) / scale;
8666  }
8667  }
8668  }
8669  QRectF bounds = QRectF(r.x() + xoff, r.y() + yoff, width, height);
8670 
8671  if (hasMoreLengthVariants && !(tf & Qt::TextLongestVariant) && !r.contains(bounds)) {
8672  offset++;
8673  goto start_lengthVariant;
8674  }
8675  if (brect)
8676  *brect = bounds;
8677 
8678  if (!(tf & Qt::TextDontPrint)) {
8679  bool restore = false;
8680  if (!dontclip && !r.contains(bounds)) {
8681  restore = true;
8682  painter->save();
8683  painter->setClipRect(r, Qt::IntersectClip);
8684  }
8685 
8686  for (int i = 0; i < textLayout.lineCount(); i++) {
8687  QTextLine line = textLayout.lineAt(i);
8688 
8689  qreal advance = line.horizontalAdvance();
8690  xoff = 0;
8691  if (tf & Qt::AlignRight) {
8692  QTextEngine *eng = textLayout.engine();
8693  xoff = r.width() - advance -
8694  eng->leadingSpaceWidth(eng->lines[line.lineNumber()]).toReal();
8695  }
8696  else if (tf & Qt::AlignHCenter)
8697  xoff = (r.width() - advance) / 2;
8698 
8699  line.draw(painter, QPointF(r.x() + xoff, r.y() + yoff));
8700  }
8701 
8702  if (restore) {
8703  painter->restore();
8704  }
8705  }
8706 }
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
const QTransform & transform() const
Returns the world transformation matrix.
Definition: qpainter.cpp:9558
qreal horizontalAdvance() const
Returns the horizontal advance of the text.
void setPosition(const QPointF &pos)
Moves the line to position pos.
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QFlag class is a helper data type for QFlags.
Definition: qglobal.h:2289
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
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
The QTextLine class represents a line of text inside a QTextLayout.
Definition: qtextlayout.h:197
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
qreal m22() const
Returns the vertical scaling factor.
Definition: qtransform.h:253
void setLineWidth(qreal width)
Lays out the line with the given width.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
TransformationType type() const
Returns the transformation type of this matrix.
bool isValid() const
Returns true if this text line is valid; otherwise returns false.
Definition: qtextlayout.h:201
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
TransformationType
Definition: qtransform.h:68
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
static Qt::LayoutDirection layout_direction
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
int lineNumber() const
Returns the position of the line in the text engine.
Definition: qtextlayout.h:243
LayoutDirection
Definition: qnamespace.h:1580
unsigned int uint
Definition: qglobal.h:996
QScriptLineArray lines
Qt::LayoutDirection layoutDirection() const
Returns the layout direction used by the painter when drawing text.
Definition: qpainter.cpp:8729
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition: qstyle.cpp:2149
unsigned short ushort
Definition: qglobal.h:995
qreal naturalTextWidth() const
Returns the width of the line that is occupied by text.
QFactoryLoader * l
The QFontMetricsF class provides font metrics information.
Definition: qfontmetrics.h:145
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void draw(QPainter *p, const QPointF &point, const QTextLayout::FormatRange *selection=0) const
Draws a line on the given painter at the specified position.
qreal height() const
Returns the line&#39;s height.
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QFixed leadingSpaceWidth(const QScriptLine &line)
qreal m11() const
Returns the horizontal scaling factor.
Definition: qtransform.h:237
#define text
Definition: qobjectdefs.h:80
static qreal toReal(Register *reg, int type, bool *ok=0)
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QFixed width(int charFrom, int numChars) const

◆ qt_init_picture_handlers()

void qt_init_picture_handlers ( )

Definition at line 1517 of file qpicture.cpp.

Referenced by QPictureIO::defineIOHandler(), get_picture_handler(), QPictureIO::inputFormats(), QPictureIO::outputFormats(), and QPictureIO::pictureFormat().

1518 {
1520  if (done.testAndSetRelaxed(0, 1)) {
1522  }
1523 }
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
bool testAndSetRelaxed(int expectedValue, int newValue)
static void cleanup()
Definition: qpicture.cpp:1508
void qAddPostRoutine(QtCleanUpFunction ptr)
Adds a global routine that will be called from the QApplication destructor.

◆ qToStringList()

static QStringList qToStringList ( const QList< QByteArray arr)
static

Definition at line 1291 of file qpicture.cpp.

Referenced by QPicture::inputFormatList(), and QPicture::outputFormatList().

1292 {
1293  QStringList list;
1294  for (int i = 0; i < arr.count(); ++i)
1295  list.append(QString::fromLatin1(arr.at(i)));
1296  return list;
1297 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188

Variable Documentation

◆ mfhdr_maj

const quint16 mfhdr_maj = 11
static

◆ mfhdr_min

const quint16 mfhdr_min = 0
static

Definition at line 114 of file qpicture.cpp.

Referenced by QPicturePrivate::resetFormat().

◆ qt_mfhdr_tag

const char* qt_mfhdr_tag = "QPIC"

Definition at line 112 of file qpicture.cpp.

Referenced by QPicturePaintEngine::begin(), and QPicturePrivate::checkFormat().