QtBase  v6.3.1
qtextformat.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qtextformat.h"
41 #include "qtextformat_p.h"
42 
43 #include <qvariant.h>
44 #include <qdatastream.h>
45 #include <qdebug.h>
46 #include <qmap.h>
47 #include <qhashfunctions.h>
48 
50 
145 QTextLength::operator QVariant() const
146 {
147  return QVariant::fromValue(*this);
148 }
149 
150 #ifndef QT_NO_DATASTREAM
152 {
153  return stream << qint32(length.lengthType) << double(length.fixedValueOrPercentage);
154 }
155 
157 {
158  qint32 type;
159  double fixedValueOrPercentage;
160  stream >> type >> fixedValueOrPercentage;
161  length.fixedValueOrPercentage = fixedValueOrPercentage;
162  length.lengthType = QTextLength::Type(type);
163  return stream;
164 }
165 #endif // QT_NO_DATASTREAM
166 
167 namespace {
168 struct Property
169 {
170  inline Property(qint32 k, const QVariant &v) : key(k), value(v) {}
171  inline Property() {}
172 
173  qint32 key = -1;
174  QVariant value;
175 
176  inline bool operator==(const Property &other) const
177  { return key == other.key && value == other.value; }
178 };
179 }
181 
183 {
184 public:
185  QTextFormatPrivate() : hashDirty(true), fontDirty(true), hashValue(0) {}
186 
187  inline size_t hash() const
188  {
189  if (!hashDirty)
190  return hashValue;
191  return recalcHash();
192  }
193 
194  inline bool operator==(const QTextFormatPrivate &rhs) const {
195  if (hash() != rhs.hash())
196  return false;
197 
198  return props == rhs.props;
199  }
200 
201  inline void insertProperty(qint32 key, const QVariant &value)
202  {
203  hashDirty = true;
205  fontDirty = true;
206 
207  for (int i = 0; i < props.count(); ++i)
208  if (props.at(i).key == key) {
209  props[i].value = value;
210  return;
211  }
212  props.append(Property(key, value));
213  }
214 
215  inline void clearProperty(qint32 key)
216  {
217  for (int i = 0; i < props.count(); ++i)
218  if (props.at(i).key == key) {
219  hashDirty = true;
221  fontDirty = true;
222  props.remove(i);
223  return;
224  }
225  }
226 
227  inline int propertyIndex(qint32 key) const
228  {
229  for (int i = 0; i < props.count(); ++i)
230  if (props.at(i).key == key)
231  return i;
232  return -1;
233  }
234 
235  inline QVariant property(qint32 key) const
236  {
237  const int idx = propertyIndex(key);
238  if (idx < 0)
239  return QVariant();
240  return props.at(idx).value;
241  }
242 
243  inline bool hasProperty(qint32 key) const
244  { return propertyIndex(key) != -1; }
245 
246  void resolveFont(const QFont &defaultFont);
247 
248  inline const QFont &font() const {
249  if (fontDirty)
250  recalcFont();
251  return fnt;
252  }
253 
255 private:
256 
257  size_t recalcHash() const;
258  void recalcFont() const;
259 
260  mutable bool hashDirty;
261  mutable bool fontDirty;
262  mutable size_t hashValue;
263  mutable QFont fnt;
264 
265  friend QDataStream &operator<<(QDataStream &, const QTextFormat &);
267 };
268 
269 static inline size_t hash(const QColor &color)
270 {
271  return (color.isValid()) ? color.rgba() : 0x234109;
272 }
273 
274 static inline size_t hash(const QPen &pen)
275 {
276  return hash(pen.color()) + qHash(pen.widthF());
277 }
278 
279 static inline size_t hash(const QBrush &brush)
280 {
281  return hash(brush.color()) + (brush.style() << 3);
282 }
283 
284 static inline size_t variantHash(const QVariant &variant)
285 {
286  // simple and fast hash functions to differentiate between type and value
287  switch (variant.userType()) { // sorted by occurrence frequency
288  case QMetaType::QString: return qHash(variant.toString());
289  case QMetaType::Double: return qHash(variant.toDouble());
290  case QMetaType::Int: return 0x811890U + variant.toInt();
291  case QMetaType::QBrush:
292  return 0x01010101 + hash(qvariant_cast<QBrush>(variant));
293  case QMetaType::Bool: return 0x371818 + variant.toBool();
294  case QMetaType::QPen: return 0x02020202 + hash(qvariant_cast<QPen>(variant));
296  return 0x8377U + qvariant_cast<QVariantList>(variant).count();
297  case QMetaType::QColor: return hash(qvariant_cast<QColor>(variant));
298  case QMetaType::QTextLength:
299  return 0x377 + hash(qvariant_cast<QTextLength>(variant).rawValue());
300  case QMetaType::Float: return qHash(variant.toFloat());
301  case QMetaType::UnknownType: return 0;
302  default: break;
303  }
304  return qHash(variant.typeName());
305 }
306 
307 static inline size_t getHash(const QTextFormatPrivate *d, int format)
308 {
309  return (d ? d->hash() : 0) + format;
310 }
311 
312 size_t QTextFormatPrivate::recalcHash() const
313 {
314  hashValue = 0;
315  const auto end = props.constEnd();
316  for (auto it = props.constBegin(); it != end; ++it)
317  hashValue += (static_cast<quint32>(it->key) << 16) + variantHash(it->value);
318 
319  hashDirty = false;
320 
321  return hashValue;
322 }
323 
324 void QTextFormatPrivate::resolveFont(const QFont &defaultFont)
325 {
326  recalcFont();
327  const uint oldMask = fnt.resolveMask();
328  fnt = fnt.resolve(defaultFont);
329 
331  const qreal scaleFactors[7] = {qreal(0.7), qreal(0.8), qreal(1.0), qreal(1.2), qreal(1.5), qreal(2), qreal(2.4)};
332 
333  const int htmlFontSize = qBound(0, property(QTextFormat::FontSizeAdjustment).toInt() + 3 - 1, 6);
334 
335 
336  if (defaultFont.pointSize() <= 0) {
337  qreal pixelSize = scaleFactors[htmlFontSize] * defaultFont.pixelSize();
338  fnt.setPixelSize(qRound(pixelSize));
339  } else {
340  qreal pointSize = scaleFactors[htmlFontSize] * defaultFont.pointSizeF();
341  fnt.setPointSizeF(pointSize);
342  }
343  }
344 
345  fnt.setResolveMask(oldMask);
346 }
347 
348 void QTextFormatPrivate::recalcFont() const
349 {
350  // update cached font as well
351  QFont f;
352 
353  bool hasSpacingInformation = false;
355  qreal letterSpacing = 0.0;
356 
357  for (int i = 0; i < props.count(); ++i) {
358  switch (props.at(i).key) {
360  f.setFamilies(props.at(i).value.toStringList());
361  break;
363  f.setStyleName(props.at(i).value.toString());
364  break;
366  f.setPointSizeF(props.at(i).value.toReal());
367  break;
369  f.setPixelSize(props.at(i).value.toInt());
370  break;
372  const QVariant weightValue = props.at(i).value;
373  int weight = weightValue.toInt();
374  if (weight >= 0 && weightValue.isValid())
375  f.setWeight(QFont::Weight(weight));
376  break; }
378  f.setItalic(props.at(i).value.toBool());
379  break;
381  if (! hasProperty(QTextFormat::TextUnderlineStyle)) // don't use the old one if the new one is there.
382  f.setUnderline(props.at(i).value.toBool());
383  break;
385  f.setUnderline(static_cast<QTextCharFormat::UnderlineStyle>(props.at(i).value.toInt()) == QTextCharFormat::SingleUnderline);
386  break;
388  f.setOverline(props.at(i).value.toBool());
389  break;
391  f.setStrikeOut(props.at(i).value.toBool());
392  break;
394  spacingType = static_cast<QFont::SpacingType>(props.at(i).value.toInt());
395  hasSpacingInformation = true;
396  break;
398  letterSpacing = props.at(i).value.toReal();
399  hasSpacingInformation = true;
400  break;
402  f.setWordSpacing(props.at(i).value.toReal());
403  break;
405  f.setCapitalization(static_cast<QFont::Capitalization> (props.at(i).value.toInt()));
406  break;
408  const bool value = props.at(i).value.toBool();
409  if (f.fixedPitch() != value)
410  f.setFixedPitch(value);
411  break; }
413  f.setStretch(props.at(i).value.toInt());
414  break;
416  f.setStyleHint(static_cast<QFont::StyleHint>(props.at(i).value.toInt()), f.styleStrategy());
417  break;
419  f.setHintingPreference(static_cast<QFont::HintingPreference>(props.at(i).value.toInt()));
420  break;
422  f.setStyleStrategy(static_cast<QFont::StyleStrategy>(props.at(i).value.toInt()));
423  break;
425  f.setKerning(props.at(i).value.toBool());
426  break;
427  default:
428  break;
429  }
430  }
431 
432  if (hasSpacingInformation)
433  f.setLetterSpacing(spacingType, letterSpacing);
434 
435  fnt = f;
436  fontDirty = false;
437 }
438 
439 #ifndef QT_NO_DATASTREAM
441 {
442  QMap<int, QVariant> properties = fmt.properties();
443  if (stream.version() < QDataStream::Qt_6_0) {
444  auto it = properties.find(QTextFormat::FontLetterSpacingType);
445  if (it != properties.end()) {
446  properties[QTextFormat::OldFontLetterSpacingType] = it.value();
447  properties.erase(it);
448  }
449 
450  it = properties.find(QTextFormat::FontStretch);
451  if (it != properties.end()) {
452  properties[QTextFormat::OldFontStretch] = it.value();
453  properties.erase(it);
454  }
455 
456  it = properties.find(QTextFormat::TextUnderlineColor);
457  if (it != properties.end()) {
458  properties[QTextFormat::OldTextUnderlineColor] = it.value();
459  properties.erase(it);
460  }
461 
462  it = properties.find(QTextFormat::FontFamilies);
463  if (it != properties.end()) {
464  properties[QTextFormat::FontFamily] = QVariant(it.value().toStringList().first());
465  properties.erase(it);
466  }
467  }
468 
469  stream << fmt.format_type << properties;
470  return stream;
471 }
472 
474 {
475  QMap<qint32, QVariant> properties;
476  stream >> fmt.format_type >> properties;
477 
478  // QTextFormat's default constructor doesn't allocate the private structure, so
479  // we have to do this, in case fmt is a default constructed value.
480  if (!fmt.d)
481  fmt.d = new QTextFormatPrivate();
482 
483  for (QMap<qint32, QVariant>::ConstIterator it = properties.constBegin();
484  it != properties.constEnd(); ++it) {
485  qint32 key = it.key();
488  else if (key == QTextFormat::OldFontStretch)
492  else if (key == QTextFormat::FontFamily)
494  fmt.d->insertProperty(key, it.value());
495  }
496 
497  return stream;
498 }
499 
501 {
503 }
504 
506 {
507  return stream >> static_cast<QTextFormat &>(fmt);
508 }
509 
511 {
513 }
514 
516 {
517  return stream >> static_cast<QTextFormat &>(fmt);
518 }
519 
521 {
523 }
524 
526 {
527  return stream >> static_cast<QTextFormat &>(fmt);
528 }
529 
531 {
533 }
534 
536 {
537  return stream >> static_cast<QTextFormat &>(fmt);
538 }
539 
541 {
543 }
544 
546 {
547  return stream >> static_cast<QTextFormat &>(fmt);
548 }
549 #endif // QT_NO_DATASTREAM
550 
905  : format_type(InvalidFormat)
906 {
907 }
908 
915  : format_type(type)
916 {
917 }
918 
919 
927  : d(rhs.d), format_type(rhs.format_type)
928 {
929 }
930 
938 {
939  d = rhs.d;
940  format_type = rhs.format_type;
941  return *this;
942 }
943 
956 {
957 }
958 
959 
963 QTextFormat::operator QVariant() const
964 {
965  return QVariant::fromValue(*this);
966 }
967 
973 {
974  if (format_type != other.format_type)
975  return;
976 
977  if (!d) {
978  d = other.d;
979  return;
980  }
981 
982  if (!other.d)
983  return;
984 
985  QTextFormatPrivate *p = d.data();
986 
987  const QList<QT_PREPEND_NAMESPACE(Property)> &otherProps = other.d.constData()->props;
988  p->props.reserve(p->props.size() + otherProps.size());
989  for (int i = 0; i < otherProps.count(); ++i) {
990  const QT_PREPEND_NAMESPACE(Property) &prop = otherProps.at(i);
991  p->insertProperty(prop.key, prop.value);
992  }
993 }
994 
1001 {
1002  return format_type;
1003 }
1004 
1009 {
1010  return QTextBlockFormat(*this);
1011 }
1012 
1017 {
1018  return QTextCharFormat(*this);
1019 }
1020 
1025 {
1026  return QTextListFormat(*this);
1027 }
1028 
1033 {
1034  return QTextTableFormat(*this);
1035 }
1036 
1041 {
1042  return QTextFrameFormat(*this);
1043 }
1044 
1049 {
1050  return QTextImageFormat(*this);
1051 }
1052 
1059 {
1060  return QTextTableCellFormat(*this);
1061 }
1062 
1070 bool QTextFormat::boolProperty(int propertyId) const
1071 {
1072  if (!d)
1073  return false;
1074  const QVariant prop = d->property(propertyId);
1075  if (prop.userType() != QMetaType::Bool)
1076  return false;
1077  return prop.toBool();
1078 }
1079 
1087 int QTextFormat::intProperty(int propertyId) const
1088 {
1089  // required, since the default layout direction has to be LayoutDirectionAuto, which is not integer 0
1090  int def = (propertyId == QTextFormat::LayoutDirection) ? int(Qt::LayoutDirectionAuto) : 0;
1091 
1092  if (!d)
1093  return def;
1094  const QVariant prop = d->property(propertyId);
1095  if (prop.userType() != QMetaType::Int)
1096  return def;
1097  return prop.toInt();
1098 }
1099 
1108 qreal QTextFormat::doubleProperty(int propertyId) const
1109 {
1110  if (!d)
1111  return 0.;
1112  const QVariant prop = d->property(propertyId);
1113  if (prop.userType() != QMetaType::Double && prop.userType() != QMetaType::Float)
1114  return 0.;
1115  return qvariant_cast<qreal>(prop);
1116 }
1117 
1127 {
1128  if (!d)
1129  return QString();
1130  const QVariant prop = d->property(propertyId);
1131  if (prop.userType() != QMetaType::QString)
1132  return QString();
1133  return prop.toString();
1134 }
1135 
1144 QColor QTextFormat::colorProperty(int propertyId) const
1145 {
1146  if (!d)
1147  return QColor();
1148  const QVariant prop = d->property(propertyId);
1149  if (prop.userType() != QMetaType::QColor)
1150  return QColor();
1151  return qvariant_cast<QColor>(prop);
1152 }
1153 
1162 QPen QTextFormat::penProperty(int propertyId) const
1163 {
1164  if (!d)
1165  return QPen(Qt::NoPen);
1166  const QVariant prop = d->property(propertyId);
1167  if (prop.userType() != QMetaType::QPen)
1168  return QPen(Qt::NoPen);
1169  return qvariant_cast<QPen>(prop);
1170 }
1171 
1180 QBrush QTextFormat::brushProperty(int propertyId) const
1181 {
1182  if (!d)
1183  return QBrush(Qt::NoBrush);
1184  const QVariant prop = d->property(propertyId);
1185  if (prop.userType() != QMetaType::QBrush)
1186  return QBrush(Qt::NoBrush);
1187  return qvariant_cast<QBrush>(prop);
1188 }
1189 
1197 {
1198  if (!d)
1199  return QTextLength();
1200  return qvariant_cast<QTextLength>(d->property(propertyId));
1201 }
1202 
1212 {
1214  if (!d)
1215  return list;
1216  const QVariant prop = d->property(propertyId);
1217  if (prop.userType() != QMetaType::QVariantList)
1218  return list;
1219 
1220  const QList<QVariant> propertyList = prop.toList();
1221  for (const auto &var : propertyList) {
1222  if (var.userType() == QMetaType::QTextLength)
1223  list.append(qvariant_cast<QTextLength>(var));
1224  }
1225 
1226  return list;
1227 }
1228 
1234 QVariant QTextFormat::property(int propertyId) const
1235 {
1236  return d ? d->property(propertyId) : QVariant();
1237 }
1238 
1244 void QTextFormat::setProperty(int propertyId, const QVariant &value)
1245 {
1246  if (!d)
1247  d = new QTextFormatPrivate;
1248  if (!value.isValid())
1249  clearProperty(propertyId);
1250  else
1251  d->insertProperty(propertyId, value);
1252 }
1253 
1260 {
1261  if (!d)
1262  d = new QTextFormatPrivate;
1264  const int numValues = value.size();
1265  list.reserve(numValues);
1266  for (int i = 0; i < numValues; ++i)
1267  list << value.at(i);
1268  d->insertProperty(propertyId, list);
1269 }
1270 
1276 void QTextFormat::clearProperty(int propertyId)
1277 {
1278  if (!d)
1279  return;
1280  d->clearProperty(propertyId);
1281 }
1282 
1283 
1308 {
1309  if (!d)
1310  return -1;
1311  const QVariant prop = d->property(ObjectIndex);
1312  if (prop.userType() != QMetaType::Int) // ####
1313  return -1;
1314  return prop.toInt();
1315 }
1316 
1325 {
1326  if (o == -1) {
1327  if (d.constData())
1328  d->clearProperty(ObjectIndex);
1329  } else {
1330  if (!d.constData())
1331  d = new QTextFormatPrivate;
1332  // ### type
1333  d->insertProperty(ObjectIndex, o);
1334  }
1335 }
1336 
1343 bool QTextFormat::hasProperty(int propertyId) const
1344 {
1345  return d ? d->hasProperty(propertyId) : false;
1346 }
1347 
1348 /*
1349  Returns the property type for the given \a propertyId.
1350 
1351  \sa hasProperty(), allPropertyIds(), Property
1352 */
1353 
1358 {
1360  if (d) {
1361  for (int i = 0; i < d->props.count(); ++i)
1362  map.insert(d->props.at(i).key, d->props.at(i).value);
1363  }
1364  return map;
1365 }
1366 
1372 {
1373  return d ? d->props.count() : 0;
1374 }
1375 
1391 {
1392  if (format_type != rhs.format_type)
1393  return false;
1394 
1395  if (d == rhs.d)
1396  return true;
1397 
1398  if (d && d->props.isEmpty() && !rhs.d)
1399  return true;
1400 
1401  if (!d && rhs.d && rhs.d->props.isEmpty())
1402  return true;
1403 
1404  if (!d || !rhs.d)
1405  return false;
1406 
1407  return *d == *rhs.d;
1408 }
1409 
1487 
1496  : QTextFormat(fmt)
1497 {
1498 }
1499 
1536 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
1549 #else
1550 /* // Qt 7 documents this function
1551  \fn QStringList QTextCharFormat::fontFamilies() const
1552  \since 5.13
1553 
1554  Returns the text format's font families.
1555 
1556  \sa font()
1557 */
1558 #endif
1559 
1569 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
1582 #else
1583 /* // Qt 7 documents this function
1584  \fn QStringList QTextCharFormat::fontStyleName() const
1585  \since 5.13
1586 
1587  Returns the text format's font style name.
1588 
1589  \sa font(), QFont::styleName()
1590 */
1591 #endif
1592 
1668 {
1670  return underlineStyle() == SingleUnderline;
1671  return boolProperty(FontUnderline);
1672 }
1673 
1688 {
1690  // for compatibility
1692 }
1693 
2018 {
2019  QVariant prop = property(AnchorName);
2020  if (prop.userType() == QMetaType::QStringList)
2021  return prop.toStringList();
2022  else if (prop.userType() != QMetaType::QString)
2023  return QStringList();
2024  return QStringList(prop.toString());
2025 }
2026 
2027 
2128 {
2130  : font.resolveMask();
2131 
2136 
2137  if (mask & QFont::SizeResolved) {
2138  const qreal pointSize = font.pointSizeF();
2139  if (pointSize > 0) {
2140  setFontPointSize(pointSize);
2141  } else {
2142  const int pixelSize = font.pixelSize();
2143  if (pixelSize > 0)
2145  }
2146  }
2147 
2150  if (mask & QFont::StyleResolved)
2167  }
2178 }
2179 
2184 {
2185  return d ? d->font() : QFont();
2186 }
2187 
2252 
2261  : QTextFormat(fmt)
2262 {
2263 }
2264 
2273 {
2275  list.reserve(tabs.count());
2277  while (iter != tabs.constEnd()) {
2278  QVariant v;
2279  v.setValue(*iter);
2280  list.append(v);
2281  ++iter;
2282  }
2284 }
2285 
2293 {
2295  if (variant.isNull())
2296  return QList<QTextOption::Tab>();
2297  QList<QTextOption::Tab> answer;
2298  QList<QVariant> variantsList = qvariant_cast<QList<QVariant> >(variant);
2299  QList<QVariant>::Iterator iter = variantsList.begin();
2300  answer.reserve(variantsList.count());
2301  while(iter != variantsList.end()) {
2302  answer.append( qvariant_cast<QTextOption::Tab>(*iter));
2303  ++iter;
2304  }
2305  return answer;
2306 }
2307 
2682  : QTextFormat(ListFormat)
2683 {
2684  setIndent(1);
2685 }
2686 
2695  : QTextFormat(fmt)
2696 {
2697 }
2698 
2862 {
2865 }
2866 
2875  : QTextFormat(fmt)
2876 {
2877 }
2878 
2947 {
2948  setProperty(FrameMargin, amargin);
2949  setProperty(FrameTopMargin, amargin);
2950  setProperty(FrameBottomMargin, amargin);
2951  setProperty(FrameLeftMargin, amargin);
2952  setProperty(FrameRightMargin, amargin);
2953 }
2954 
2955 
2976 {
2978  return margin();
2980 }
2981 
2996 {
2998  return margin();
3000 }
3001 
3016 {
3018  return margin();
3020 }
3021 
3036 {
3038  return margin();
3040 }
3041 
3167  : QTextFrameFormat()
3168 {
3170  setCellSpacing(2);
3171  setBorder(1);
3172 }
3173 
3182  : QTextFrameFormat(fmt)
3183 {
3184 }
3185 
3395 
3404  : QTextCharFormat(fmt)
3405 {
3406 }
3407 
3911  : QTextCharFormat()
3912 {
3914 }
3915 
3924  : QTextCharFormat(fmt)
3925 {
3926 }
3927 
3950 // ------------------------------------------------------
3951 
3953 {
3954 }
3955 
3957 {
3958  formats.clear();
3959  objFormats.clear();
3960  hashes.clear();
3961 }
3962 
3964 {
3965  size_t hash = getHash(format.d, format.format_type);
3966  auto i = hashes.constFind(hash);
3967  while (i != hashes.constEnd() && i.key() == hash) {
3968  if (formats.value(i.value()) == format) {
3969  return i.value();
3970  }
3971  ++i;
3972  }
3973 
3974  int idx = formats.size();
3976 
3977  QT_TRY{
3978  QTextFormat &f = formats.last();
3979  if (!f.d)
3980  f.d = new QTextFormatPrivate;
3981  f.d->resolveFont(defaultFnt);
3982 
3983  hashes.insert(hash, idx);
3984 
3985  } QT_CATCH(...) {
3986  formats.pop_back();
3987  QT_RETHROW;
3988  }
3989  return idx;
3990 }
3991 
3993 {
3994  size_t hash = getHash(format.d, format.format_type);
3995  auto i = hashes.constFind(hash);
3996  while (i != hashes.constEnd() && i.key() == hash) {
3997  if (formats.value(i.value()) == format) {
3998  return true;
3999  }
4000  ++i;
4001  }
4002  return false;
4003 }
4004 
4006 {
4007  if (objectIndex == -1)
4008  return -1;
4009  return objFormats.at(objectIndex);
4010 }
4011 
4012 void QTextFormatCollection::setObjectFormatIndex(int objectIndex, int formatIndex)
4013 {
4014  objFormats[objectIndex] = formatIndex;
4015 }
4016 
4018 {
4019  const int objectIndex = objFormats.size();
4021  return objectIndex;
4022 }
4023 
4025 {
4026  if (idx < 0 || idx >= formats.count())
4027  return QTextFormat();
4028 
4029  return formats.at(idx);
4030 }
4031 
4033 {
4034  defaultFnt = f;
4035  for (int i = 0; i < formats.count(); ++i)
4036  if (formats.at(i).d)
4037  formats[i].d->resolveFont(defaultFnt);
4038 }
4039 
4040 #ifndef QT_NO_DEBUG_STREAM
4042 {
4043  QDebugStateSaver saver(dbg);
4044  dbg.nospace() << "QTextLength(QTextLength::Type(" << l.type() << "))";
4045  return dbg;
4046 }
4047 
4049 {
4050  QDebugStateSaver saver(dbg);
4051  dbg.nospace() << "QTextFormat(QTextFormat::FormatType(" << f.type() << "))";
4052  return dbg;
4053 }
4054 
4055 #endif
4056 
4058 
4059 #include "moc_qtextformat.cpp"
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
Arabic default style
Definition: afstyles.h:94
#define value
[5]
@ Float
@ Double
FT_UInt idx
Definition: cffcmap.c:135
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:66
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
QDebug & nospace()
Definition: qdebug.h:113
Convenience class for custom QDebug operators.
Definition: qdebug.h:176
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QFont class specifies a query for a font used for drawing text.
Definition: qfont.h:56
StyleHint
Definition: qfont.h:59
StyleStrategy styleStrategy() const
Definition: qfont.cpp:1414
QString styleName() const
Definition: qfont.cpp:864
int pixelSize() const
Definition: qfont.cpp:1094
StyleHint styleHint() const
Definition: qfont.cpp:1427
bool strikeOut() const
Definition: qfont.cpp:1324
bool underline() const
Definition: qfont.cpp:1271
Capitalization
Definition: qfont.h:130
qreal letterSpacing() const
Definition: qfont.cpp:1633
QFont resolve(const QFont &) const
Definition: qfont.cpp:1874
HintingPreference hintingPreference() const
Definition: qfont.cpp:995
SpacingType
Definition: qfont.h:139
@ PercentageSpacing
Definition: qfont.h:140
Capitalization capitalization() const
Definition: qfont.cpp:1755
QStringList families() const
Definition: qfont.cpp:2284
Weight weight() const
Definition: qfont.cpp:1153
@ StretchResolved
Definition: qfont.h:157
@ LetterSpacingResolved
Definition: qfont.h:160
@ SizeResolved
Definition: qfont.h:148
@ OverlineResolved
Definition: qfont.h:154
@ UnderlineResolved
Definition: qfont.h:153
@ StyleHintResolved
Definition: qfont.h:149
@ StyleNameResolved
Definition: qfont.h:163
@ AllPropertiesResolved
Definition: qfont.h:165
@ StrikeOutResolved
Definition: qfont.h:155
@ KerningResolved
Definition: qfont.h:158
@ WeightResolved
Definition: qfont.h:151
@ CapitalizationResolved
Definition: qfont.h:159
@ HintingPreferenceResolved
Definition: qfont.h:162
@ FixedPitchResolved
Definition: qfont.h:156
@ WordSpacingResolved
Definition: qfont.h:161
@ StyleStrategyResolved
Definition: qfont.h:150
@ StyleResolved
Definition: qfont.h:152
@ FamiliesResolved
Definition: qfont.h:164
qreal wordSpacing() const
Definition: qfont.cpp:1683
int pointSize() const
Definition: qfont.cpp:899
int stretch() const
Definition: qfont.cpp:1576
bool kerning() const
Definition: qfont.cpp:1379
void setPixelSize(int)
Definition: qfont.cpp:1069
bool fixedPitch() const
Definition: qfont.cpp:1351
HintingPreference
Definition: qfont.h:88
void setResolveMask(uint mask)
Definition: qfont.h:275
uint resolveMask() const
Definition: qfont.h:274
qreal pointSizeF() const
Definition: qfont.cpp:1055
SpacingType letterSpacingType() const
Definition: qfont.cpp:1672
Style style() const
Definition: qfont.cpp:1125
StyleStrategy
Definition: qfont.h:72
void setPointSizeF(qreal)
Definition: qfont.cpp:1031
Weight
Definition: qfont.h:96
bool overline() const
Definition: qfont.cpp:1298
@ StyleNormal
Definition: qfont.h:110
void pop_back() noexcept
Definition: qlist.h:691
qsizetype size() const noexcept
Definition: qlist.h:414
iterator end()
Definition: qlist.h:624
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
T value(qsizetype i) const
Definition: qlist.h:676
T & last()
Definition: qlist.h:646
const_iterator constBegin() const noexcept
Definition: qlist.h:630
qsizetype count() const noexcept
Definition: qlist.h:415
iterator begin()
Definition: qlist.h:623
void reserve(qsizetype size)
Definition: qlist.h:757
void append(parameter_type t)
Definition: qlist.h:469
const_iterator constEnd() const noexcept
Definition: qlist.h:631
void clear()
Definition: qlist.h:445
Definition: qmap.h:222
iterator insert(const Key &key, const T &value)
Definition: qmap.h:719
@ UnknownType
Definition: qmetatype.h:346
const_iterator constEnd() const noexcept
Definition: qhash.h:1774
iterator insert(const Key &key, const T &value)
Definition: qhash.h:1861
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Definition: qhash.h:1456
const_iterator constFind(const Key &key) const noexcept
Definition: qhash.h:1852
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
qreal widthF() const
Definition: qpen.cpp:634
QColor color() const
Definition: qpen.cpp:754
The QSharedData class is a base class for shared data objects. \reentrant.
Definition: qshareddata.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument....
Definition: qtextformat.h:640
QList< QTextOption::Tab > tabPositions() const
void setTabPositions(const QList< QTextOption::Tab > &tabs)
The QTextCharFormat class provides formatting information for characters in a QTextDocument....
Definition: qtextformat.h:416
FontPropertiesInheritanceBehavior
Definition: qtextformat.h:442
QStringList anchorNames() const
void setFontFamilies(const QStringList &families)
Definition: qtextformat.h:456
void setUnderlineStyle(UnderlineStyle style)
UnderlineStyle underlineStyle() const
Definition: qtextformat.h:560
void setFontWordSpacing(qreal spacing)
Definition: qtextformat.h:501
void setFontPointSize(qreal size)
Definition: qtextformat.h:476
QFont font() const
void setFontKerning(bool enable)
Definition: qtextformat.h:554
void setFontStyleHint(QFont::StyleHint hint, QFont::StyleStrategy strategy=QFont::PreferDefault)
Definition: qtextformat.h:535
void setFontFixedPitch(bool fixedPitch)
Definition: qtextformat.h:525
void setFontLetterSpacing(qreal spacing)
Definition: qtextformat.h:497
void setFontStyleStrategy(QFont::StyleStrategy strategy)
Definition: qtextformat.h:537
void setFontStrikeOut(bool strikeOut)
Definition: qtextformat.h:515
void setFontHintingPreference(QFont::HintingPreference hintingPreference)
Definition: qtextformat.h:544
void setFontStretch(int factor)
Definition: qtextformat.h:530
bool fontUnderline() const
void setFontOverline(bool overline)
Definition: qtextformat.h:510
void setFontLetterSpacingType(QFont::SpacingType letterSpacingType)
Definition: qtextformat.h:493
void setFontStyleName(const QString &styleName)
Definition: qtextformat.h:466
void setFontItalic(bool italic)
Definition: qtextformat.h:485
void setFontWeight(int weight)
Definition: qtextformat.h:481
void setFontCapitalization(QFont::Capitalization capitalization)
Definition: qtextformat.h:489
void setFont(const QFont &font, FontPropertiesInheritanceBehavior behavior=FontPropertiesAll)
FormatVector formats
Definition: qtextformat_p.h:97
int indexForFormat(const QTextFormat &f)
QMultiHash< size_t, int > hashes
Definition: qtextformat_p.h:99
QList< qint32 > objFormats
Definition: qtextformat_p.h:98
int createObjectIndex(const QTextFormat &f)
int objectFormatIndex(int objectIndex) const
void setObjectFormatIndex(int objectIndex, int formatIndex)
QTextFormat format(int idx) const
bool hasFormatCached(const QTextFormat &format) const
void setDefaultFont(const QFont &f)
The QTextFormat class provides formatting information for a QTextDocument. \inmodule QtGui.
Definition: qtextformat.h:126
QTextCharFormat toCharFormat() const
QString stringProperty(int propertyId) const
QTextBlockFormat toBlockFormat() const
@ FontCapitalization
Definition: qtextformat.h:174
@ OldTextUnderlineColor
Definition: qtextformat.h:217
@ TextUnderlineColor
Definition: qtextformat.h:200
@ FontSizeAdjustment
Definition: qtextformat.h:189
@ FontLetterSpacingType
Definition: qtextformat.h:183
@ OldFontLetterSpacingType
Definition: qtextformat.h:215
@ TextUnderlineStyle
Definition: qtextformat.h:203
@ FontHintingPreference
Definition: qtextformat.h:180
QTextLength lengthProperty(int propertyId) const
int intProperty(int propertyId) const
int objectIndex() const
QColor colorProperty(int propertyId) const
bool boolProperty(int propertyId) const
QTextFormat & operator=(const QTextFormat &rhs)
void setProperty(int propertyId, const QVariant &value)
friend class QTextCharFormat
Definition: qtextformat.h:403
QBrush brushProperty(int propertyId) const
QTextTableFormat toTableFormat() const
QTextImageFormat toImageFormat() const
void setObjectType(int type)
Definition: qtextformat.h:410
QMap< int, QVariant > properties() const
QTextTableCellFormat toTableCellFormat() const
int propertyCount() const
QTextListFormat toListFormat() const
bool hasProperty(int propertyId) const
QPen penProperty(int propertyId) const
void setObjectIndex(int object)
bool operator==(const QTextFormat &rhs) const
QTextFrameFormat toFrameFormat() const
int type() const
void clearProperty(int propertyId)
void merge(const QTextFormat &other)
QList< QTextLength > lengthVectorProperty(int propertyId) const
qreal doubleProperty(int propertyId) const
QVariant property(int propertyId) const
QList< Property > props
friend QDataStream & operator>>(QDataStream &, QTextFormat &)
friend QDataStream & operator<<(QDataStream &, const QTextFormat &)
bool hasProperty(qint32 key) const
bool operator==(const QTextFormatPrivate &rhs) const
size_t hash() const
void resolveFont(const QFont &defaultFont)
void insertProperty(qint32 key, const QVariant &value)
QVariant property(qint32 key) const
int propertyIndex(qint32 key) const
void clearProperty(qint32 key)
const QFont & font() const
The QTextFrameFormat class provides formatting information for frames in a QTextDocument....
Definition: qtextformat.h:859
qreal leftMargin() const
void setBorderBrush(const QBrush &brush)
Definition: qtextformat.h:896
void setBorderStyle(BorderStyle style)
Definition: qtextformat.h:901
void setBorder(qreal border)
Definition: qtextformat.h:951
qreal bottomMargin() const
qreal rightMargin() const
qreal margin() const
Definition: qtextformat.h:907
void setMargin(qreal margin)
qreal topMargin() const
The QTextImageFormat class provides formatting information for images in a QTextDocument....
Definition: qtextformat.h:811
The QTextLength class encapsulates the different types of length used in a QTextDocument....
Definition: qtextformat.h:81
The QTextListFormat class provides formatting information for lists in a QTextDocument....
Definition: qtextformat.h:757
void setIndent(int indent)
Definition: qtextformat.h:801
The QTextTableCellFormat class provides formatting information for table cells in a QTextDocument....
Definition: qtextformat.h:1041
The QTextTableFormat class provides formatting information for tables in a QTextDocument....
Definition: qtextformat.h:978
void setCellSpacing(qreal spacing)
Definition: qtextformat.h:999
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
static auto fromValue(const T &value) -> std::enable_if_t< std::is_copy_constructible_v< T >, QVariant >
Definition: qvariant.h:391
bool isValid() const
Definition: qvariant.h:582
double toDouble(bool *ok=nullptr) const
Definition: qvariant.cpp:1929
QList< QVariant > toList() const
Definition: qvariant.cpp:1979
int toInt(bool *ok=nullptr) const
Definition: qvariant.cpp:1833
int userType() const
Definition: qvariant.h:240
float toFloat(bool *ok=nullptr) const
Definition: qvariant.cpp:1948
QString toString() const
Definition: qvariant.cpp:1416
bool toBool() const
Definition: qvariant.cpp:1906
bool isNull() const
Definition: qvariant.cpp:2483
const char * typeName() const
Definition: qvariant.cpp:1073
T value() const
Definition: qvariant.h:378
QStringList toStringList() const
Definition: qvariant.cpp:1396
QHash< int, QWidget * > hash
[35multi]
QMap< QString, QString > map
[6]
#define true
Definition: ftrandom.c:51
char Bool
Definition: ftraster.c:315
int Int
Definition: ftraster.c:307
@ FontFamily
Definition: qcssparser_p.h:99
@ LayoutDirectionAuto
Definition: qnamespace.h:1466
@ darkGray
Definition: qnamespace.h:63
@ NoPen
Definition: qnamespace.h:1112
@ NoBrush
Definition: qnamespace.h:1140
Definition: brush.cpp:52
#define QString()
Definition: parse-defines.h:51
QList< QString > QStringList
Definition: qcontainerfwd.h:64
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
int qRound(qfloat16 d) noexcept
Definition: qfloat16.h:227
unsigned int quint32
Definition: qglobal.h:288
int qint32
Definition: qglobal.h:287
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
unsigned int uint
Definition: qglobal.h:334
QList< QVariant > QVariantList
Definition: qjsonarray.h:50
bool operator==(const QMakeBaseKey &one, const QMakeBaseKey &two)
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLenum type
Definition: qopengl.h:270
GLsizei const GLfloat * v
[13]
GLuint64 key
GLuint GLuint end
GLfloat GLfloat f
GLuint GLuint GLfloat weight
GLuint color
[2]
GLenum GLuint GLsizei const GLenum * props
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
int QT_PREPEND_NAMESPACE(QSharedMemoryPrivate)
Q_DECLARE_TYPEINFO(Property, Q_RELOCATABLE_TYPE)
@ Q_RELOCATABLE_TYPE
Definition: qtypeinfo.h:156
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
QStringList::Iterator it
QStringList list
[0]
#define rhs