Qt 4.8
Public Types | Public Functions | Private Types | Private Functions | Properties | List of all members
QTextHtmlExporter Class Reference

#include <qtextdocument_p.h>

Public Types

enum  ExportMode { ExportEntireDocument, ExportFragment }
 

Public Functions

 QTextHtmlExporter (const QTextDocument *_doc)
 
QString toHtml (const QByteArray &encoding, ExportMode mode=ExportEntireDocument)
 Returns the document in HTML format. More...
 

Private Types

enum  FrameType { TextFrame, TableFrame, RootFrame }
 
enum  StyleMode { EmitStyleTag, OmitStyleTag }
 

Private Functions

void emitAlignment (Qt::Alignment alignment)
 
void emitAttribute (const char *attribute, const QString &value)
 
void emitBackgroundAttribute (const QTextFormat &format)
 
void emitBlock (const QTextBlock &block)
 
void emitBlockAttributes (const QTextBlock &block)
 
void emitBorderStyle (QTextFrameFormat::BorderStyle style)
 
bool emitCharFormatStyle (const QTextCharFormat &format)
 
void emitFloatStyle (QTextFrameFormat::Position pos, StyleMode mode=EmitStyleTag)
 
void emitFontFamily (const QString &family)
 
void emitFragment (const QTextFragment &fragment)
 
void emitFrame (QTextFrame::Iterator frameIt)
 
void emitFrameStyle (const QTextFrameFormat &format, FrameType frameType)
 
void emitMargins (const QString &top, const QString &bottom, const QString &left, const QString &right)
 
void emitPageBreakPolicy (QTextFormat::PageBreakFlags policy)
 
void emitTable (const QTextTable *table)
 
void emitTextFrame (const QTextFrame *frame)
 
void emitTextLength (const char *attribute, const QTextLength &length)
 
QString findUrlForImage (const QTextDocument *doc, qint64 cacheKey, bool isPixmap)
 

Properties

QTextCharFormat defaultCharFormat
 
const QTextDocumentdoc
 
bool fragmentMarkers
 
QString html
 

Detailed Description

Definition at line 366 of file qtextdocument_p.h.

Enumerations

◆ ExportMode

Enumerator
ExportEntireDocument 
ExportFragment 

Definition at line 371 of file qtextdocument_p.h.

◆ FrameType

Enumerator
TextFrame 
TableFrame 
RootFrame 

Definition at line 380 of file qtextdocument_p.h.

◆ StyleMode

Enumerator
EmitStyleTag 
OmitStyleTag 

Definition at line 379 of file qtextdocument_p.h.

Constructors and Destructors

◆ QTextHtmlExporter()

QTextHtmlExporter::QTextHtmlExporter ( const QTextDocument _doc)

Definition at line 2222 of file qtextdocument.cpp.

Referenced by QTextDocument::toHtml().

2223  : doc(_doc), fragmentMarkers(false)
2224 {
2225  const QFont defaultFont = doc->defaultFont();
2226  defaultCharFormat.setFont(defaultFont);
2227  // don't export those for the default font since we cannot turn them off with CSS
2232 }
QTextCharFormat defaultCharFormat
void setFont(const QFont &font)
Sets the text format&#39;s font.
const QTextDocument * doc
QFont defaultFont
the default font used to display the document&#39;s text
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
void clearProperty(int propertyId)
Clears the value of the property given by propertyId.

Functions

◆ emitAlignment()

void QTextHtmlExporter::emitAlignment ( Qt::Alignment  alignment)
private

Definition at line 2490 of file qtextdocument.cpp.

Referenced by emitBlockAttributes(), and emitTable().

2491 {
2492  if (align & Qt::AlignLeft)
2493  return;
2494  else if (align & Qt::AlignRight)
2495  html += QLatin1String(" align=\"right\"");
2496  else if (align & Qt::AlignHCenter)
2497  html += QLatin1String(" align=\"center\"");
2498  else if (align & Qt::AlignJustify)
2499  html += QLatin1String(" align=\"justify\"");
2500 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ emitAttribute()

void QTextHtmlExporter::emitAttribute ( const char *  attribute,
const QString value 
)
private

Definition at line 2310 of file qtextdocument.cpp.

Referenced by emitBackgroundAttribute(), emitFragment(), emitTable(), and emitTextFrame().

2311 {
2312  html += QLatin1Char(' ');
2313  html += QLatin1String(attribute);
2314  html += QLatin1String("=\"");
2315  html += Qt::escape(value);
2316  html += QLatin1Char('"');
2317 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_GUI_EXPORT QString escape(const QString &plain)
Converts the plain text string plain to a HTML string with HTML metacharacters <, >...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ emitBackgroundAttribute()

void QTextHtmlExporter::emitBackgroundAttribute ( const QTextFormat format)
private

Definition at line 2954 of file qtextdocument.cpp.

Referenced by emitTable(), emitTextFrame(), and toHtml().

2955 {
2958  emitAttribute("background", url);
2959  } else {
2960  const QBrush &brush = format.background();
2961  if (brush.style() == Qt::SolidPattern) {
2962  emitAttribute("bgcolor", brush.color().name());
2963  } else if (brush.style() == Qt::TexturePattern) {
2964  const bool isPixmap = qHasPixmapTexture(brush);
2965  const qint64 cacheKey = isPixmap ? brush.texture().cacheKey() : brush.textureImage().cacheKey();
2966 
2967  const QString url = findUrlForImage(doc, cacheKey, isPixmap);
2968 
2969  if (!url.isEmpty())
2970  emitAttribute("background", url);
2971  }
2972  }
2973 }
QString findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap)
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
const QTextDocument * doc
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool qHasPixmapTexture(const QBrush &brush)
Definition: qbrush.cpp:223
__int64 qint64
Definition: qglobal.h:942
QBrush background() const
Returns the brush used to paint the document&#39;s background.
Definition: qtextformat.h:345
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182
QPixmap texture() const
Returns the custom brush pattern, or a null pixmap if no custom brush pattern has been set...
Definition: qbrush.cpp:785
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
QVariant property(int propertyId) const
Returns the property specified by the given propertyId.
qint64 cacheKey() const
Returns a number that identifies this QPixmap.
Definition: qpixmap.cpp:1136
void emitAttribute(const char *attribute, const QString &value)
QImage textureImage() const
Returns the custom brush pattern, or a null image if no custom brush pattern has been set...
Definition: qbrush.cpp:829
QString name() const
Returns the name of the color in the format "#RRGGBB"; i.e.
Definition: qcolor.cpp:529
qint64 cacheKey() const
Returns a number that identifies the contents of this QImage object.
Definition: qimage.cpp:6282

◆ emitBlock()

void QTextHtmlExporter::emitBlock ( const QTextBlock block)
private

Definition at line 2767 of file qtextdocument.cpp.

Referenced by emitFrame().

2768 {
2769  if (block.begin().atEnd()) {
2770  // ### HACK, remove once QTextFrame::Iterator is fixed
2771  int p = block.position();
2772  if (p > 0)
2773  --p;
2775  QChar ch = doc->docHandle()->buffer().at(frag->stringPosition);
2776  if (ch == QTextBeginningOfFrame
2777  || ch == QTextEndOfFrame)
2778  return;
2779  }
2780 
2781  html += QLatin1Char('\n');
2782 
2783  // save and later restore, in case we 'change' the default format by
2784  // emitting block char format information
2785  QTextCharFormat oldDefaultCharFormat = defaultCharFormat;
2786 
2787  QTextList *list = block.textList();
2788  if (list) {
2789  if (list->itemNumber(block) == 0) { // first item? emit <ul> or appropriate
2790  const QTextListFormat format = list->format();
2791  const int style = format.style();
2792  switch (style) {
2793  case QTextListFormat::ListDecimal: html += QLatin1String("<ol"); break;
2794  case QTextListFormat::ListDisc: html += QLatin1String("<ul"); break;
2795  case QTextListFormat::ListCircle: html += QLatin1String("<ul type=\"circle\""); break;
2796  case QTextListFormat::ListSquare: html += QLatin1String("<ul type=\"square\""); break;
2797  case QTextListFormat::ListLowerAlpha: html += QLatin1String("<ol type=\"a\""); break;
2798  case QTextListFormat::ListUpperAlpha: html += QLatin1String("<ol type=\"A\""); break;
2799  case QTextListFormat::ListLowerRoman: html += QLatin1String("<ol type=\"i\""); break;
2800  case QTextListFormat::ListUpperRoman: html += QLatin1String("<ol type=\"I\""); break;
2801  default: html += QLatin1String("<ul"); // ### should not happen
2802  }
2803 
2804  QString styleString = QString::fromLatin1("margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px;");
2805 
2806  if (format.hasProperty(QTextFormat::ListIndent)) {
2807  styleString += QLatin1String(" -qt-list-indent: ");
2808  styleString += QString::number(format.indent());
2809  styleString += QLatin1Char(';');
2810  }
2811 
2813  QString numberPrefix = format.numberPrefix();
2814  numberPrefix.replace(QLatin1Char('"'), QLatin1String("\\22"));
2815  numberPrefix.replace(QLatin1Char('\''), QLatin1String("\\27")); // FIXME: There's a problem in the CSS parser the prevents this from being correctly restored
2816  styleString += QLatin1String(" -qt-list-number-prefix: ");
2817  styleString += QLatin1Char('\'');
2818  styleString += numberPrefix;
2819  styleString += QLatin1Char('\'');
2820  styleString += QLatin1Char(';');
2821  }
2822 
2824  if (format.numberSuffix() != QLatin1String(".")) { // this is our default
2825  QString numberSuffix = format.numberSuffix();
2826  numberSuffix.replace(QLatin1Char('"'), QLatin1String("\\22"));
2827  numberSuffix.replace(QLatin1Char('\''), QLatin1String("\\27")); // see above
2828  styleString += QLatin1String(" -qt-list-number-suffix: ");
2829  styleString += QLatin1Char('\'');
2830  styleString += numberSuffix;
2831  styleString += QLatin1Char('\'');
2832  styleString += QLatin1Char(';');
2833  }
2834  }
2835 
2836  html += QLatin1String(" style=\"");
2837  html += styleString;
2838  html += QLatin1String("\">");
2839  }
2840 
2841  html += QLatin1String("<li");
2842 
2843  const QTextCharFormat blockFmt = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat();
2844  if (!blockFmt.properties().isEmpty()) {
2845  html += QLatin1String(" style=\"");
2846  emitCharFormatStyle(blockFmt);
2847  html += QLatin1Char('\"');
2848 
2850  }
2851  }
2852 
2853  const QTextBlockFormat blockFormat = block.blockFormat();
2855  html += QLatin1String("<hr");
2856 
2858  if (width.type() != QTextLength::VariableLength)
2859  emitTextLength("width", width);
2860  else
2861  html += QLatin1Char(' ');
2862 
2863  html += QLatin1String("/>");
2864  return;
2865  }
2866 
2867  const bool pre = blockFormat.nonBreakableLines();
2868  if (pre) {
2869  if (list)
2870  html += QLatin1Char('>');
2871  html += QLatin1String("<pre");
2872  } else if (!list) {
2873  html += QLatin1String("<p");
2874  }
2875 
2876  emitBlockAttributes(block);
2877 
2878  html += QLatin1Char('>');
2879  if (block.begin().atEnd())
2880  html += QLatin1String("<br />");
2881 
2882  QTextBlock::Iterator it = block.begin();
2883  if (fragmentMarkers && !it.atEnd() && block == doc->begin())
2884  html += QLatin1String("<!--StartFragment-->");
2885 
2886  for (; !it.atEnd(); ++it)
2887  emitFragment(it.fragment());
2888 
2889  if (fragmentMarkers && block.position() + block.length() == doc->docHandle()->length())
2890  html += QLatin1String("<!--EndFragment-->");
2891 
2892  if (pre)
2893  html += QLatin1String("</pre>");
2894  else if (list)
2895  html += QLatin1String("</li>");
2896  else
2897  html += QLatin1String("</p>");
2898 
2899  if (list) {
2900  if (list->itemNumber(block) == list->count() - 1) { // last item? close list
2901  if (isOrderedList(list->format().style()))
2902  html += QLatin1String("</ol>");
2903  else
2904  html += QLatin1String("</ul>");
2905  }
2906  }
2907 
2908  defaultCharFormat = oldDefaultCharFormat;
2909 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QTextCharFormat defaultCharFormat
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
The QTextListFormat class provides formatting information for lists in a QTextDocument.
Definition: qtextformat.h:642
int count() const
Returns the number of items in the list.
Definition: qtextlist.cpp:139
QTextList * textList() const
If the block represents a list item, returns the list that the item belongs to; otherwise returns 0...
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QTextBlock begin() const
Returns the document&#39;s first text block.
#define it(className, varName)
Type type() const
Returns the type of this length object.
Definition: qtextformat.h:93
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
bool atEnd() const
Returns true if the current item is the last item in the text block.
Definition: qtextobject.h:264
const QTextDocument * doc
iterator begin() const
Returns a text block iterator pointing to the beginning of the text block.
QMap< int, QVariant > properties() const
Returns a map with all properties of this text format.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define QTextBeginningOfFrame
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
FragmentMap::ConstIterator FragmentIterator
int position() const
Returns the index of the block&#39;s first character within the document.
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
FragmentIterator find(int pos) const
Style style() const
Returns the list format&#39;s style.
Definition: qtextformat.h:662
QString numberSuffix() const
Returns the list format&#39;s number suffix.
Definition: qtextformat.h:674
QTextDocumentPrivate * docHandle() const
So that not all classes have to be friends of each other.
static bool isOrderedList(int style)
int length() const
Returns the length of the block in characters.
int itemNumber(const QTextBlock &) const
Returns the index of the list item that corresponds to the given block.
Definition: qtextlist.cpp:185
The QTextBlock::iterator class provides an iterator for reading the contents of a QTextBlock...
Definition: qtextobject.h:251
QTextCharFormat charFormat() const
Returns the QTextCharFormat that describes the block&#39;s character format.
bool nonBreakableLines() const
Returns true if the lines in the paragraph are non-breakable; otherwise returns false.
Definition: qtextformat.h:603
QString numberPrefix() const
Returns the list format&#39;s number prefix.
Definition: qtextformat.h:670
void merge(const QTextFormat &other)
Merges the other format with this format; where there are conflicts the other format takes precedence...
QString buffer() const
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
The QTextLength class encapsulates the different types of length used in a QTextDocument.
Definition: qtextformat.h:84
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
void emitTextLength(const char *attribute, const QTextLength &length)
The QTextList class provides a decorated list of items in a QTextDocument.
Definition: qtextlist.h:57
void emitBlockAttributes(const QTextBlock &block)
QTextFragment fragment() const
Returns the text fragment the iterator currently points to.
static QTextFormat formatDifference(const QTextFormat &from, const QTextFormat &to)
int indent() const
Returns the list format&#39;s indentation.
Definition: qtextformat.h:666
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool emitCharFormatStyle(const QTextCharFormat &format)
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
#define QTextEndOfFrame
void emitFragment(const QTextFragment &fragment)
QTextListFormat format() const
Returns the list&#39;s format.
Definition: qtextlist.h:80
QTextLength lengthProperty(int propertyId) const
Returns the value of the property given by propertyId.

◆ emitBlockAttributes()

void QTextHtmlExporter::emitBlockAttributes ( const QTextBlock block)
private

Definition at line 2708 of file qtextdocument.cpp.

Referenced by emitBlock().

2709 {
2711  emitAlignment(format.alignment());
2712 
2713  // assume default to not bloat the html too much
2714  // html += QLatin1String(" dir='ltr'");
2715  if (block.textDirection() == Qt::RightToLeft)
2716  html += QLatin1String(" dir='rtl'");
2717 
2718  QLatin1String style(" style=\"");
2719  html += style;
2720 
2721  const bool emptyBlock = block.begin().atEnd();
2722  if (emptyBlock) {
2723  html += QLatin1String("-qt-paragraph-type:empty;");
2724  }
2725 
2727  QString::number(format.bottomMargin()),
2728  QString::number(format.leftMargin()),
2729  QString::number(format.rightMargin()));
2730 
2731  html += QLatin1String(" -qt-block-indent:");
2732  html += QString::number(format.indent());
2733  html += QLatin1Char(';');
2734 
2735  html += QLatin1String(" text-indent:");
2736  html += QString::number(format.textIndent());
2737  html += QLatin1String("px;");
2738 
2739  if (block.userState() != -1) {
2740  html += QLatin1String(" -qt-user-state:");
2741  html += QString::number(block.userState());
2742  html += QLatin1Char(';');
2743  }
2744 
2746 
2748  if (emptyBlock) { // only print character properties when we don't expect them to be repeated by actual text in the parag
2749  const QTextCharFormat blockCharFmt = block.charFormat();
2750  diff = formatDifference(defaultCharFormat, blockCharFmt).toCharFormat();
2751  }
2752 
2755  QBrush bg = format.background();
2756  if (bg.style() != Qt::NoBrush)
2758  }
2759 
2760  if (!diff.properties().isEmpty())
2761  emitCharFormatStyle(diff);
2762 
2763  html += QLatin1Char('"');
2764 
2765 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QTextCharFormat defaultCharFormat
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
bool atEnd() const
Returns true if the current item is the last item in the text block.
Definition: qtextobject.h:264
PageBreakFlags pageBreakPolicy() const
Returns the currently set page break policy for the paragraph.
Definition: qtextformat.h:608
void emitAlignment(Qt::Alignment alignment)
iterator begin() const
Returns a text block iterator pointing to the beginning of the text block.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
qreal topMargin() const
Returns the paragraph&#39;s top margin.
Definition: qtextformat.h:566
int indent() const
Returns the paragraph&#39;s indent.
Definition: qtextformat.h:590
qreal rightMargin() const
Returns the paragraph&#39;s right margin.
Definition: qtextformat.h:581
Qt::Alignment alignment() const
Returns the paragraph&#39;s alignment.
Definition: qtextformat.h:561
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
static Bigint * diff(Bigint *a, Bigint *b)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QBrush background() const
Returns the brush used to paint the document&#39;s background.
Definition: qtextformat.h:345
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182
void emitPageBreakPolicy(QTextFormat::PageBreakFlags policy)
qreal textIndent() const
Returns the paragraph&#39;s text indent.
Definition: qtextformat.h:586
QTextCharFormat charFormat() const
Returns the QTextCharFormat that describes the block&#39;s character format.
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
int userState() const
Returns the integer value previously set with setUserState() or -1.
qreal bottomMargin() const
Returns the paragraph&#39;s bottom margin.
Definition: qtextformat.h:571
QVariant property(int propertyId) const
Returns the property specified by the given propertyId.
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
void emitMargins(const QString &top, const QString &bottom, const QString &left, const QString &right)
Qt::LayoutDirection textDirection() const
Returns the resolved text direction.
QTextCharFormat toCharFormat() const
Returns this format as a character format.
void clearProperty(int propertyId)
Clears the value of the property given by propertyId.
static QTextFormat formatDifference(const QTextFormat &from, const QTextFormat &to)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool emitCharFormatStyle(const QTextCharFormat &format)
qreal leftMargin() const
Returns the paragraph&#39;s left margin.
Definition: qtextformat.h:576
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.

◆ emitBorderStyle()

void QTextHtmlExporter::emitBorderStyle ( QTextFrameFormat::BorderStyle  style)
private

Definition at line 2523 of file qtextdocument.cpp.

Referenced by emitFrameStyle().

2524 {
2526 
2527  html += QLatin1String(" border-style:");
2528 
2529  switch (style) {
2531  html += QLatin1String("none");
2532  break;
2534  html += QLatin1String("dotted");
2535  break;
2537  html += QLatin1String("dashed");
2538  break;
2540  html += QLatin1String("solid");
2541  break;
2543  html += QLatin1String("double");
2544  break;
2546  html += QLatin1String("dot-dash");
2547  break;
2549  html += QLatin1String("dot-dot-dash");
2550  break;
2552  html += QLatin1String("groove");
2553  break;
2555  html += QLatin1String("ridge");
2556  break;
2558  html += QLatin1String("inset");
2559  break;
2561  html += QLatin1String("outset");
2562  break;
2563  default:
2564  Q_ASSERT(false);
2565  break;
2566  };
2567 
2568  html += QLatin1Char(';');
2569 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ emitCharFormatStyle()

bool QTextHtmlExporter::emitCharFormatStyle ( const QTextCharFormat format)
private

Definition at line 2319 of file qtextdocument.cpp.

Referenced by emitBlock(), emitBlockAttributes(), and emitFragment().

2320 {
2321  bool attributesEmitted = false;
2322 
2323  {
2324  const QString family = format.fontFamily();
2325  if (!family.isEmpty() && family != defaultCharFormat.fontFamily()) {
2326  emitFontFamily(family);
2327  attributesEmitted = true;
2328  }
2329  }
2330 
2332  && format.fontPointSize() != defaultCharFormat.fontPointSize()) {
2333  html += QLatin1String(" font-size:");
2334  html += QString::number(format.fontPointSize());
2335  html += QLatin1String("pt;");
2336  attributesEmitted = true;
2337  } else if (format.hasProperty(QTextFormat::FontSizeAdjustment)) {
2338  static const char * const sizeNames[] = {
2339  "small", "medium", "large", "x-large", "xx-large"
2340  };
2341  const char *name = 0;
2342  const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1;
2343  if (idx >= 0 && idx <= 4) {
2344  name = sizeNames[idx];
2345  }
2346  if (name) {
2347  html += QLatin1String(" font-size:");
2348  html += QLatin1String(name);
2349  html += QLatin1Char(';');
2350  attributesEmitted = true;
2351  }
2352  } else if (format.hasProperty(QTextFormat::FontPixelSize)) {
2353  html += QLatin1String(" font-size:");
2355  html += QLatin1String("px;");
2356  attributesEmitted = true;
2357  }
2358 
2360  && format.fontWeight() != defaultCharFormat.fontWeight()) {
2361  html += QLatin1String(" font-weight:");
2362  html += QString::number(format.fontWeight() * 8);
2363  html += QLatin1Char(';');
2364  attributesEmitted = true;
2365  }
2366 
2368  && format.fontItalic() != defaultCharFormat.fontItalic()) {
2369  html += QLatin1String(" font-style:");
2370  html += (format.fontItalic() ? QLatin1String("italic") : QLatin1String("normal"));
2371  html += QLatin1Char(';');
2372  attributesEmitted = true;
2373  }
2374 
2375  QLatin1String decorationTag(" text-decoration:");
2376  html += decorationTag;
2377  bool hasDecoration = false;
2378  bool atLeastOneDecorationSet = false;
2379 
2381  && format.fontUnderline() != defaultCharFormat.fontUnderline()) {
2382  hasDecoration = true;
2383  if (format.fontUnderline()) {
2384  html += QLatin1String(" underline");
2385  atLeastOneDecorationSet = true;
2386  }
2387  }
2388 
2390  && format.fontOverline() != defaultCharFormat.fontOverline()) {
2391  hasDecoration = true;
2392  if (format.fontOverline()) {
2393  html += QLatin1String(" overline");
2394  atLeastOneDecorationSet = true;
2395  }
2396  }
2397 
2399  && format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) {
2400  hasDecoration = true;
2401  if (format.fontStrikeOut()) {
2402  html += QLatin1String(" line-through");
2403  atLeastOneDecorationSet = true;
2404  }
2405  }
2406 
2407  if (hasDecoration) {
2408  if (!atLeastOneDecorationSet)
2409  html += QLatin1String("none");
2410  html += QLatin1Char(';');
2411  attributesEmitted = true;
2412  } else {
2413  html.chop(qstrlen(decorationTag.latin1()));
2414  }
2415 
2416  if (format.foreground() != defaultCharFormat.foreground()
2417  && format.foreground().style() != Qt::NoBrush) {
2418  html += QLatin1String(" color:");
2419  html += format.foreground().color().name();
2420  html += QLatin1Char(';');
2421  attributesEmitted = true;
2422  }
2423 
2424  if (format.background() != defaultCharFormat.background()
2425  && format.background().style() == Qt::SolidPattern) {
2426  html += QLatin1String(" background-color:");
2427  html += format.background().color().name();
2428  html += QLatin1Char(';');
2429  attributesEmitted = true;
2430  }
2431 
2434  {
2435  html += QLatin1String(" vertical-align:");
2436 
2438  if (valign == QTextCharFormat::AlignSubScript)
2439  html += QLatin1String("sub");
2440  else if (valign == QTextCharFormat::AlignSuperScript)
2441  html += QLatin1String("super");
2442  else if (valign == QTextCharFormat::AlignMiddle)
2443  html += QLatin1String("middle");
2444  else if (valign == QTextCharFormat::AlignTop)
2445  html += QLatin1String("top");
2446  else if (valign == QTextCharFormat::AlignBottom)
2447  html += QLatin1String("bottom");
2448 
2449  html += QLatin1Char(';');
2450  attributesEmitted = true;
2451  }
2452 
2453  if (format.fontCapitalization() != QFont::MixedCase) {
2454  const QFont::Capitalization caps = format.fontCapitalization();
2455  if (caps == QFont::AllUppercase)
2456  html += QLatin1String(" text-transform:uppercase;");
2457  else if (caps == QFont::AllLowercase)
2458  html += QLatin1String(" text-transform:lowercase;");
2459  else if (caps == QFont::SmallCaps)
2460  html += QLatin1String(" font-variant:small-caps;");
2461  attributesEmitted = true;
2462  }
2463 
2464  if (format.fontWordSpacing() != 0.0) {
2465  html += QLatin1String(" word-spacing:");
2466  html += QString::number(format.fontWordSpacing());
2467  html += QLatin1String("px;");
2468  attributesEmitted = true;
2469  }
2470 
2471  return attributesEmitted;
2472 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QTextCharFormat defaultCharFormat
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
bool fontStrikeOut() const
Returns true if the text format&#39;s font is struck out (has a horizontal line drawn through it); otherw...
Definition: qtextformat.h:443
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Capitalization
Rendering option for text this font applies to.
Definition: qfont.h:129
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
VerticalAlignment
This enum describes the ways that adjacent characters can be vertically aligned.
Definition: qtextformat.h:375
bool fontOverline() const
Returns true if the text format&#39;s font is overlined; otherwise returns false.
Definition: qtextformat.h:438
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
int fontWeight() const
Returns the text format&#39;s font weight.
Definition: qtextformat.h:413
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QBrush background() const
Returns the brush used to paint the document&#39;s background.
Definition: qtextformat.h:345
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182
void emitFontFamily(const QString &family)
VerticalAlignment verticalAlignment() const
Returns the vertical alignment used for characters with this format.
Definition: qtextformat.h:486
uint qstrlen(const char *str)
Definition: qbytearray.h:79
qreal fontWordSpacing() const
Returns the current word spacing value.
Definition: qtextformat.h:429
QBrush foreground() const
Returns the brush used to render foreground details, such as text, frame outlines, and table borders.
Definition: qtextformat.h:352
qreal fontPointSize() const
Returns the font size used to display text in this format.
Definition: qtextformat.h:408
int intProperty(int propertyId) const
Returns the value of the property specified by propertyId.
bool fontItalic() const
Returns true if the text format&#39;s font is italic; otherwise returns false.
Definition: qtextformat.h:417
QString fontFamily() const
Returns the text format&#39;s font family.
Definition: qtextformat.h:403
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString name() const
Returns the name of the color in the format "#RRGGBB"; i.e.
Definition: qcolor.cpp:529
QFont::Capitalization fontCapitalization() const
Returns the current capitalization type of the font.
Definition: qtextformat.h:421
bool fontUnderline() const
Returns true if the text format&#39;s font is underlined; otherwise returns false.

◆ emitFloatStyle()

void QTextHtmlExporter::emitFloatStyle ( QTextFrameFormat::Position  pos,
StyleMode  mode = EmitStyleTag 
)
private

Definition at line 2502 of file qtextdocument.cpp.

Referenced by emitFragment(), and emitFrameStyle().

2503 {
2504  if (pos == QTextFrameFormat::InFlow)
2505  return;
2506 
2507  if (mode == EmitStyleTag)
2508  html += QLatin1String(" style=\"float:");
2509  else
2510  html += QLatin1String(" float:");
2511 
2512  if (pos == QTextFrameFormat::FloatLeft)
2513  html += QLatin1String(" left;");
2514  else if (pos == QTextFrameFormat::FloatRight)
2515  html += QLatin1String(" right;");
2516  else
2517  Q_ASSERT_X(0, "QTextHtmlExporter::emitFloatStyle()", "pos should be a valid enum type");
2518 
2519  if (mode == EmitStyleTag)
2520  html += QLatin1Char('\"');
2521 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ emitFontFamily()

void QTextHtmlExporter::emitFontFamily ( const QString family)
private

Definition at line 2580 of file qtextdocument.cpp.

Referenced by emitCharFormatStyle(), and toHtml().

2581 {
2582  html += QLatin1String(" font-family:");
2583 
2584  QLatin1String quote("\'");
2585  if (family.contains(QLatin1Char('\'')))
2586  quote = QLatin1String("&quot;");
2587 
2588  html += quote;
2589  html += Qt::escape(family);
2590  html += quote;
2591  html += QLatin1Char(';');
2592 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
Q_GUI_EXPORT QString escape(const QString &plain)
Converts the plain text string plain to a HTML string with HTML metacharacters <, >...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ emitFragment()

void QTextHtmlExporter::emitFragment ( const QTextFragment fragment)
private

Definition at line 2613 of file qtextdocument.cpp.

Referenced by emitBlock().

2614 {
2615  const QTextCharFormat format = fragment.charFormat();
2616 
2617  bool closeAnchor = false;
2618 
2619  if (format.isAnchor()) {
2620  const QString name = format.anchorName();
2621  if (!name.isEmpty()) {
2622  html += QLatin1String("<a name=\"");
2623  html += Qt::escape(name);
2624  html += QLatin1String("\"></a>");
2625  }
2626  const QString href = format.anchorHref();
2627  if (!href.isEmpty()) {
2628  html += QLatin1String("<a href=\"");
2629  html += Qt::escape(href);
2630  html += QLatin1String("\">");
2631  closeAnchor = true;
2632  }
2633  }
2634 
2635  QString txt = fragment.text();
2636  const bool isObject = txt.contains(QChar::ObjectReplacementCharacter);
2637  const bool isImage = isObject && format.isImageFormat();
2638 
2639  QLatin1String styleTag("<span style=\"");
2640  html += styleTag;
2641 
2642  bool attributesEmitted = false;
2643  if (!isImage)
2644  attributesEmitted = emitCharFormatStyle(format);
2645  if (attributesEmitted)
2646  html += QLatin1String("\">");
2647  else
2648  html.chop(qstrlen(styleTag.latin1()));
2649 
2650  if (isObject) {
2651  for (int i = 0; isImage && i < txt.length(); ++i) {
2652  QTextImageFormat imgFmt = format.toImageFormat();
2653 
2654  html += QLatin1String("<img");
2655 
2656  if (imgFmt.hasProperty(QTextFormat::ImageName))
2657  emitAttribute("src", imgFmt.name());
2658 
2660  emitAttribute("width", QString::number(imgFmt.width()));
2661 
2663  emitAttribute("height", QString::number(imgFmt.height()));
2664 
2666  html += QLatin1String(" style=\"vertical-align: middle;\"");
2667  else if (imgFmt.verticalAlignment() == QTextCharFormat::AlignTop)
2668  html += QLatin1String(" style=\"vertical-align: top;\"");
2669 
2670  if (QTextFrame *imageFrame = qobject_cast<QTextFrame *>(doc->objectForFormat(imgFmt)))
2671  emitFloatStyle(imageFrame->frameFormat().position());
2672 
2673  html += QLatin1String(" />");
2674  }
2675  } else {
2677 
2678  txt = Qt::escape(txt);
2679 
2680  // split for [\n{LineSeparator}]
2681  QString forcedLineBreakRegExp = QString::fromLatin1("[\\na]");
2682  forcedLineBreakRegExp[3] = QChar::LineSeparator;
2683 
2684  const QStringList lines = txt.split(QRegExp(forcedLineBreakRegExp));
2685  for (int i = 0; i < lines.count(); ++i) {
2686  if (i > 0)
2687  html += QLatin1String("<br />"); // space on purpose for compatibility with Netscape, Lynx & Co.
2688  html += lines.at(i);
2689  }
2690  }
2691 
2692  if (attributesEmitted)
2693  html += QLatin1String("</span>");
2694 
2695  if (closeAnchor)
2696  html += QLatin1String("</a>");
2697 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QTextFrame class represents a frame in a QTextDocument.
Definition: qtextobject.h:122
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
const QTextDocument * doc
QString text() const
Returns the text fragment&#39;s as plain text.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString anchorName() const
This function is deprecated.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QString anchorHref() const
Returns the text format&#39;s hypertext link, or an empty string if none has been set.
Definition: qtextformat.h:506
The QString class provides a Unicode character string.
Definition: qstring.h:83
qreal height() const
Returns the height of the rectangle occupied by the image.
Definition: qtextformat.h:710
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QTextObject * objectForFormat(const QTextFormat &) const
Returns the text object associated with the format f.
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
QTextCharFormat charFormat() const
Returns the text fragment&#39;s character format.
The QTextImageFormat class provides formatting information for images in a QTextDocument.
Definition: qtextformat.h:694
bool isAnchor() const
Returns true if the text is formatted as an anchor; otherwise returns false.
Definition: qtextformat.h:501
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
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 QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void emitFloatStyle(QTextFrameFormat::Position pos, StyleMode mode=EmitStyleTag)
Q_GUI_EXPORT QString escape(const QString &plain)
Converts the plain text string plain to a HTML string with HTML metacharacters <, >...
VerticalAlignment verticalAlignment() const
Returns the vertical alignment used for characters with this format.
Definition: qtextformat.h:486
uint qstrlen(const char *str)
Definition: qbytearray.h:79
QTextImageFormat toImageFormat() const
Returns this format as an image format.
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
void emitAttribute(const char *attribute, const QString &value)
bool isImageFormat() const
Returns true if this text format is an image format; otherwise returns false.
Definition: qtextformat.h:322
bool emitCharFormatStyle(const QTextCharFormat &format)
QString name() const
Returns the name of the image.
Definition: qtextformat.h:702
qreal width() const
Returns the width of the rectangle occupied by the image.
Definition: qtextformat.h:706

◆ emitFrame()

void QTextHtmlExporter::emitFrame ( QTextFrame::Iterator  frameIt)
private

Definition at line 3101 of file qtextdocument.cpp.

Referenced by emitTable(), emitTextFrame(), and toHtml().

3102 {
3103  if (!frameIt.atEnd()) {
3104  QTextFrame::Iterator next = frameIt;
3105  ++next;
3106  if (next.atEnd()
3107  && frameIt.currentFrame() == 0
3108  && frameIt.parentFrame() != doc->rootFrame()
3109  && frameIt.currentBlock().begin().atEnd())
3110  return;
3111  }
3112 
3113  for (QTextFrame::Iterator it = frameIt;
3114  !it.atEnd(); ++it) {
3115  if (QTextFrame *f = it.currentFrame()) {
3116  if (QTextTable *table = qobject_cast<QTextTable *>(f)) {
3117  emitTable(table);
3118  } else {
3119  emitTextFrame(f);
3120  }
3121  } else if (it.currentBlock().isValid()) {
3122  emitBlock(it.currentBlock());
3123  }
3124  }
3125 }
#define it(className, varName)
The QTextFrame class represents a frame in a QTextDocument.
Definition: qtextobject.h:122
bool atEnd() const
Returns true if the current item is the last item in the text block.
Definition: qtextobject.h:264
QTextFrame * currentFrame() const
Returns the current frame pointed to by the iterator, or 0 if the iterator currently points to a bloc...
const QTextDocument * doc
iterator begin() const
Returns a text block iterator pointing to the beginning of the text block.
void emitBlock(const QTextBlock &block)
QTextFrame * rootFrame() const
Returns the document&#39;s root frame.
void emitTextFrame(const QTextFrame *frame)
void emitTable(const QTextTable *table)
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
The iterator class provides an iterator for reading the contents of a QTextFrame. ...
Definition: qtextobject.h:144
QTextFrame * parentFrame() const
Returns the parent frame of the current frame.
Definition: qtextobject.h:160
bool atEnd() const
Returns true if the current item is the last item in the text frame.
Definition: qtextobject.h:165
QTextBlock currentBlock() const
Returns the current block the iterator points to.

◆ emitFrameStyle()

void QTextHtmlExporter::emitFrameStyle ( const QTextFrameFormat format,
FrameType  frameType 
)
private

Definition at line 3152 of file qtextdocument.cpp.

Referenced by emitTable(), and emitTextFrame().

3153 {
3154  QLatin1String styleAttribute(" style=\"");
3155  html += styleAttribute;
3156  const int originalHtmlLength = html.length();
3157 
3158  if (frameType == TextFrame)
3159  html += QLatin1String("-qt-table-type: frame;");
3160  else if (frameType == RootFrame)
3161  html += QLatin1String("-qt-table-type: root;");
3162 
3163  const QTextFrameFormat defaultFormat;
3164 
3167 
3168  if (format.borderBrush() != defaultFormat.borderBrush()) {
3169  html += QLatin1String(" border-color:");
3170  html += format.borderBrush().color().name();
3171  html += QLatin1Char(';');
3172  }
3173 
3174  if (format.borderStyle() != defaultFormat.borderStyle())
3175  emitBorderStyle(format.borderStyle());
3176 
3183  QString::number(format.bottomMargin()),
3184  QString::number(format.leftMargin()),
3185  QString::number(format.rightMargin()));
3186 
3187  if (html.length() == originalHtmlLength) // nothing emitted?
3188  html.chop(qstrlen(styleAttribute.latin1()));
3189  else
3190  html += QLatin1Char('\"');
3191 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
qreal leftMargin() const
Returns the width of the frame&#39;s left margin in pixels.
Position position() const
Returns the positioning policy for frames with this frame format.
Definition: qtextformat.h:758
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
qreal topMargin() const
Returns the width of the frame&#39;s top margin in pixels.
qreal bottomMargin() const
Returns the width of the frame&#39;s bottom margin in pixels.
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void emitFloatStyle(QTextFrameFormat::Position pos, StyleMode mode=EmitStyleTag)
void emitPageBreakPolicy(QTextFormat::PageBreakFlags policy)
uint qstrlen(const char *str)
Definition: qbytearray.h:79
void emitBorderStyle(QTextFrameFormat::BorderStyle style)
qreal rightMargin() const
Returns the width of the frame&#39;s right margin in pixels.
The QTextFrameFormat class provides formatting information for frames in a QTextDocument.
Definition: qtextformat.h:727
QBrush borderBrush() const
Returns the brush used for the frame&#39;s border.
Definition: qtextformat.h:767
void emitMargins(const QString &top, const QString &bottom, const QString &left, const QString &right)
PageBreakFlags pageBreakPolicy() const
Returns the currently set page break policy for the frame/table.
Definition: qtextformat.h:808
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString name() const
Returns the name of the color in the format "#RRGGBB"; i.e.
Definition: qcolor.cpp:529
BorderStyle borderStyle() const
Returns the style of the frame&#39;s border.
Definition: qtextformat.h:772

◆ emitMargins()

void QTextHtmlExporter::emitMargins ( const QString top,
const QString bottom,
const QString left,
const QString right 
)
private

Definition at line 2594 of file qtextdocument.cpp.

Referenced by emitBlockAttributes(), and emitFrameStyle().

2595 {
2596  html += QLatin1String(" margin-top:");
2597  html += top;
2598  html += QLatin1String("px;");
2599 
2600  html += QLatin1String(" margin-bottom:");
2601  html += bottom;
2602  html += QLatin1String("px;");
2603 
2604  html += QLatin1String(" margin-left:");
2605  html += left;
2606  html += QLatin1String("px;");
2607 
2608  html += QLatin1String(" margin-right:");
2609  html += right;
2610  html += QLatin1String("px;");
2611 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_CORE_EXPORT QTextStream & left(QTextStream &s)

◆ emitPageBreakPolicy()

void QTextHtmlExporter::emitPageBreakPolicy ( QTextFormat::PageBreakFlags  policy)
private

Definition at line 2571 of file qtextdocument.cpp.

Referenced by emitBlockAttributes(), and emitFrameStyle().

2572 {
2574  html += QLatin1String(" page-break-before:always;");
2575 
2577  html += QLatin1String(" page-break-after:always;");
2578 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ emitTable()

void QTextHtmlExporter::emitTable ( const QTextTable table)
private

Definition at line 2975 of file qtextdocument.cpp.

Referenced by emitFrame().

2976 {
2977  QTextTableFormat format = table->format();
2978 
2979  html += QLatin1String("\n<table");
2980 
2982  emitAttribute("border", QString::number(format.border()));
2983 
2984  emitFrameStyle(format, TableFrame);
2985 
2986  emitAlignment(format.alignment());
2987  emitTextLength("width", format.width());
2988 
2990  emitAttribute("cellspacing", QString::number(format.cellSpacing()));
2992  emitAttribute("cellpadding", QString::number(format.cellPadding()));
2993 
2994  emitBackgroundAttribute(format);
2995 
2996  html += QLatin1Char('>');
2997 
2998  const int rows = table->rows();
2999  const int columns = table->columns();
3000 
3001  QVector<QTextLength> columnWidths = format.columnWidthConstraints();
3002  if (columnWidths.isEmpty()) {
3003  columnWidths.resize(columns);
3004  columnWidths.fill(QTextLength());
3005  }
3006  Q_ASSERT(columnWidths.count() == columns);
3007 
3008  QVarLengthArray<bool> widthEmittedForColumn(columns);
3009  for (int i = 0; i < columns; ++i)
3010  widthEmittedForColumn[i] = false;
3011 
3012  const int headerRowCount = qMin(format.headerRowCount(), rows);
3013  if (headerRowCount > 0)
3014  html += QLatin1String("<thead>");
3015 
3016  for (int row = 0; row < rows; ++row) {
3017  html += QLatin1String("\n<tr>");
3018 
3019  for (int col = 0; col < columns; ++col) {
3020  const QTextTableCell cell = table->cellAt(row, col);
3021 
3022  // for col/rowspans
3023  if (cell.row() != row)
3024  continue;
3025 
3026  if (cell.column() != col)
3027  continue;
3028 
3029  html += QLatin1String("\n<td");
3030 
3031  if (!widthEmittedForColumn[col] && cell.columnSpan() == 1) {
3032  emitTextLength("width", columnWidths.at(col));
3033  widthEmittedForColumn[col] = true;
3034  }
3035 
3036  if (cell.columnSpan() > 1)
3037  emitAttribute("colspan", QString::number(cell.columnSpan()));
3038 
3039  if (cell.rowSpan() > 1)
3040  emitAttribute("rowspan", QString::number(cell.rowSpan()));
3041 
3042  const QTextTableCellFormat cellFormat = cell.format().toTableCellFormat();
3043  emitBackgroundAttribute(cellFormat);
3044 
3045  QTextCharFormat oldDefaultCharFormat = defaultCharFormat;
3046 
3048 
3049  QString styleString;
3050  if (valign >= QTextCharFormat::AlignMiddle && valign <= QTextCharFormat::AlignBottom) {
3051  styleString += QLatin1String(" vertical-align:");
3052  switch (valign) {
3054  styleString += QLatin1String("middle");
3055  break;
3057  styleString += QLatin1String("top");
3058  break;
3060  styleString += QLatin1String("bottom");
3061  break;
3062  default:
3063  break;
3064  }
3065  styleString += QLatin1Char(';');
3066 
3067  QTextCharFormat temp;
3068  temp.setVerticalAlignment(valign);
3069  defaultCharFormat.merge(temp);
3070  }
3071 
3073  styleString += QLatin1String(" padding-left:") + QString::number(cellFormat.leftPadding()) + QLatin1Char(';');
3075  styleString += QLatin1String(" padding-right:") + QString::number(cellFormat.rightPadding()) + QLatin1Char(';');
3077  styleString += QLatin1String(" padding-top:") + QString::number(cellFormat.topPadding()) + QLatin1Char(';');
3079  styleString += QLatin1String(" padding-bottom:") + QString::number(cellFormat.bottomPadding()) + QLatin1Char(';');
3080 
3081  if (!styleString.isEmpty())
3082  html += QLatin1String(" style=\"") + styleString + QLatin1Char('\"');
3083 
3084  html += QLatin1Char('>');
3085 
3086  emitFrame(cell.begin());
3087 
3088  html += QLatin1String("</td>");
3089 
3090  defaultCharFormat = oldDefaultCharFormat;
3091  }
3092 
3093  html += QLatin1String("</tr>");
3094  if (headerRowCount > 0 && row == headerRowCount - 1)
3095  html += QLatin1String("</thead>");
3096  }
3097 
3098  html += QLatin1String("</table>");
3099 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
int columns() const
Returns the number of columns in the table.
qreal cellSpacing() const
Returns the table&#39;s cell spacing.
Definition: qtextformat.h:862
qreal rightPadding() const
Gets the right padding of the table cell.
Definition: qtextformat.h:959
QTextCharFormat defaultCharFormat
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
int headerRowCount() const
Returns the number of rows in the table that define the header.
Definition: qtextformat.h:877
QVector< T > & fill(const T &t, int size=-1)
Assigns value to all items in the vector.
Definition: qvector.h:665
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QTextFrame::iterator begin() const
Returns a frame iterator pointing to the beginning of the table&#39;s cell.
Definition: qtexttable.cpp:306
QTextTableCellFormat toTableCellFormat() const
Returns this format as a table cell format.
void emitAlignment(Qt::Alignment alignment)
void setVerticalAlignment(VerticalAlignment alignment)
Sets the vertical alignment used for the characters with this format to the alignment specified...
Definition: qtextformat.h:484
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
int rowSpan() const
Returns the number of rows this cell spans.
Definition: qtexttable.cpp:218
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
VerticalAlignment
This enum describes the ways that adjacent characters can be vertically aligned.
Definition: qtextformat.h:375
Qt::Alignment alignment() const
Returns the table&#39;s alignment.
Definition: qtextformat.h:872
QTextCharFormat format() const
Returns the cell&#39;s character format.
Definition: qtexttable.cpp:153
QTextTableFormat format() const
Returns the table&#39;s format.
Definition: qtexttable.h:133
int column() const
Returns the number of the column in the table that contains this cell.
Definition: qtexttable.cpp:201
void emitBackgroundAttribute(const QTextFormat &format)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
VerticalAlignment verticalAlignment() const
Returns the vertical alignment used for characters with this format.
Definition: qtextformat.h:486
void emitFrame(QTextFrame::Iterator frameIt)
QTextLength width() const
Returns the width of the frame&#39;s border rectangle.
Definition: qtextformat.h:798
void merge(const QTextFormat &other)
Merges the other format with this format; where there are conflicts the other format takes precedence...
int rows() const
Returns the number of rows in the table.
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
The QTextLength class encapsulates the different types of length used in a QTextDocument.
Definition: qtextformat.h:84
void emitTextLength(const char *attribute, const QTextLength &length)
The QTextTableCellFormat class provides formatting information for table cells in a QTextDocument...
Definition: qtextformat.h:898
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
Definition: qtextformat.h:842
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
void emitAttribute(const char *attribute, const QString &value)
qreal topPadding() const
Gets the top padding of the table cell.
Definition: qtextformat.h:929
int columnSpan() const
Returns the number of columns this cell spans.
Definition: qtexttable.cpp:228
void emitFrameStyle(const QTextFrameFormat &format, FrameType frameType)
qreal border() const
Returns the width of the border in pixels.
Definition: qtextformat.h:762
qreal cellPadding() const
Returns the table&#39;s cell padding.
Definition: qtextformat.h:867
qreal leftPadding() const
Gets the left padding of the table cell.
Definition: qtextformat.h:949
qreal bottomPadding() const
Gets the bottom padding of the table cell.
Definition: qtextformat.h:939
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QVector< QTextLength > columnWidthConstraints() const
Returns a list of constraints used by this table format to control the appearance of columns in a tab...
Definition: qtextformat.h:856

◆ emitTextFrame()

void QTextHtmlExporter::emitTextFrame ( const QTextFrame frame)
private

Definition at line 3127 of file qtextdocument.cpp.

Referenced by emitFrame(), and toHtml().

3128 {
3129  FrameType frameType = f->parentFrame() ? TextFrame : RootFrame;
3130 
3131  html += QLatin1String("\n<table");
3132  QTextFrameFormat format = f->frameFormat();
3133 
3135  emitAttribute("border", QString::number(format.border()));
3136 
3137  emitFrameStyle(format, frameType);
3138 
3139  emitTextLength("width", format.width());
3140  emitTextLength("height", format.height());
3141 
3142  // root frame's bcolor goes in the <body> tag
3143  if (frameType != RootFrame)
3144  emitBackgroundAttribute(format);
3145 
3146  html += QLatin1Char('>');
3147  html += QLatin1String("\n<tr>\n<td style=\"border: none;\">");
3148  emitFrame(f->begin());
3149  html += QLatin1String("</td></tr></table>");
3150 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
void emitBackgroundAttribute(const QTextFormat &format)
QTextLength height() const
Returns the height of the frame&#39;s border rectangle.
Definition: qtextformat.h:803
void emitFrame(QTextFrame::Iterator frameIt)
QTextLength width() const
Returns the width of the frame&#39;s border rectangle.
Definition: qtextformat.h:798
The QTextFrameFormat class provides formatting information for frames in a QTextDocument.
Definition: qtextformat.h:727
void emitTextLength(const char *attribute, const QTextLength &length)
void emitAttribute(const char *attribute, const QString &value)
void emitFrameStyle(const QTextFrameFormat &format, FrameType frameType)
qreal border() const
Returns the width of the border in pixels.
Definition: qtextformat.h:762
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ emitTextLength()

void QTextHtmlExporter::emitTextLength ( const char *  attribute,
const QTextLength length 
)
private

Definition at line 2474 of file qtextdocument.cpp.

Referenced by emitBlock(), emitTable(), and emitTextFrame().

2475 {
2476  if (length.type() == QTextLength::VariableLength) // default
2477  return;
2478 
2479  html += QLatin1Char(' ');
2480  html += QLatin1String(attribute);
2481  html += QLatin1String("=\"");
2482  html += QString::number(length.rawValue());
2483 
2484  if (length.type() == QTextLength::PercentageLength)
2485  html += QLatin1String("%\"");
2486  else
2487  html += QLatin1Char('\"');
2488 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
Type type() const
Returns the type of this length object.
Definition: qtextformat.h:93
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
qreal rawValue() const
Returns the constraint value that is specific for the type of the length.
Definition: qtextformat.h:104
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ findUrlForImage()

QString QTextHtmlExporter::findUrlForImage ( const QTextDocument doc,
qint64  cacheKey,
bool  isPixmap 
)
private

Definition at line 2913 of file qtextdocument.cpp.

Referenced by emitBackgroundAttribute().

2914 {
2915  QString url;
2916  if (!doc)
2917  return url;
2918 
2919  if (QTextDocument *parent = qobject_cast<QTextDocument *>(doc->parent()))
2920  return findUrlForImage(parent, cacheKey, isPixmap);
2921 
2922  if (doc && doc->docHandle()) {
2925  for (; it != priv->cachedResources.constEnd(); ++it) {
2926 
2927  const QVariant &v = it.value();
2928  if (v.type() == QVariant::Image && !isPixmap) {
2929  if (qvariant_cast<QImage>(v).cacheKey() == cacheKey)
2930  break;
2931  }
2932 
2933  if (v.type() == QVariant::Pixmap && isPixmap) {
2934  if (qvariant_cast<QPixmap>(v).cacheKey() == cacheKey)
2935  break;
2936  }
2937  }
2938 
2939  if (it != priv->cachedResources.constEnd())
2940  url = it.key().toString();
2941  }
2942 
2943  return url;
2944 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QString findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap)
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
#define it(className, varName)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTextDocumentPrivate * docHandle() const
So that not all classes have to be friends of each other.
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
QMap< QUrl, QVariant > cachedResources
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
static const QMetaObjectPrivate * priv(const uint *data)
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ toHtml()

QString QTextHtmlExporter::toHtml ( const QByteArray encoding,
ExportMode  mode = ExportEntireDocument 
)

Returns the document in HTML format.

The conversion may not be perfect, especially for complex documents, due to the limitations of HTML.

Definition at line 2239 of file qtextdocument.cpp.

Referenced by QTextDocumentFragment::toHtml().

2240 {
2241  html = QLatin1String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" "
2242  "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
2243  "<html><head><meta name=\"qrichtext\" content=\"1\" />");
2244  html.reserve(doc->docHandle()->length());
2245 
2246  fragmentMarkers = (mode == ExportFragment);
2247 
2248  if (!encoding.isEmpty())
2249  html += QString::fromLatin1("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\" />").arg(QString::fromAscii(encoding));
2250 
2252  if (!title.isEmpty())
2253  html += QString::fromLatin1("<title>") + title + QString::fromLatin1("</title>");
2254  html += QLatin1String("<style type=\"text/css\">\n");
2255  html += QLatin1String("p, li { white-space: pre-wrap; }\n");
2256  html += QLatin1String("</style>");
2257  html += QLatin1String("</head><body");
2258 
2259  if (mode == ExportEntireDocument) {
2260  html += QLatin1String(" style=\"");
2261 
2263 
2265  html += QLatin1String(" font-size:");
2267  html += QLatin1String("pt;");
2269  html += QLatin1String(" font-size:");
2271  html += QLatin1String("px;");
2272  }
2273 
2274  html += QLatin1String(" font-weight:");
2276  html += QLatin1Char(';');
2277 
2278  html += QLatin1String(" font-style:");
2279  html += (defaultCharFormat.fontItalic() ? QLatin1String("italic") : QLatin1String("normal"));
2280  html += QLatin1Char(';');
2281 
2282  // do not set text-decoration on the default font since those values are /always/ propagated
2283  // and cannot be turned off with CSS
2284 
2285  html += QLatin1Char('\"');
2286 
2287  const QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
2289 
2290  } else {
2292  }
2293  html += QLatin1Char('>');
2294 
2295  QTextFrameFormat rootFmt = doc->rootFrame()->frameFormat();
2297 
2298  QTextFrameFormat defaultFmt;
2299  defaultFmt.setMargin(doc->documentMargin());
2300 
2301  if (rootFmt == defaultFmt)
2302  emitFrame(doc->rootFrame()->begin());
2303  else
2305 
2306  html += QLatin1String("</body></html>");
2307  return html;
2308 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QTextCharFormat defaultCharFormat
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
const QTextDocument * doc
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTextFrame * rootFrame() const
Returns the document&#39;s root frame.
void emitTextFrame(const QTextFrame *frame)
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false...
void emitBackgroundAttribute(const QTextFormat &format)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QTextDocumentPrivate * docHandle() const
So that not all classes have to be friends of each other.
int fontWeight() const
Returns the text format&#39;s font weight.
Definition: qtextformat.h:413
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void emitFontFamily(const QString &family)
void emitFrame(QTextFrame::Iterator frameIt)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
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
QString metaInformation(MetaInformation info) const
Returns meta information about the document of the type specified by info.
The QTextFrameFormat class provides formatting information for frames in a QTextDocument.
Definition: qtextformat.h:727
qreal documentMargin
The margin around the document.
void setMargin(qreal margin)
Sets the frame&#39;s margin in pixels.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
void clearProperty(int propertyId)
Clears the value of the property given by propertyId.
qreal fontPointSize() const
Returns the font size used to display text in this format.
Definition: qtextformat.h:408
int intProperty(int propertyId) const
Returns the value of the property specified by propertyId.
bool fontItalic() const
Returns true if the text format&#39;s font is italic; otherwise returns false.
Definition: qtextformat.h:417
QString fontFamily() const
Returns the text format&#39;s font family.
Definition: qtextformat.h:403
QTextFrameFormat frameFormat() const
Returns the frame&#39;s format.
Definition: qtextobject.h:131
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
iterator begin() const
Returns an iterator pointing to the first document element inside the frame.

Properties

◆ defaultCharFormat

QTextCharFormat QTextHtmlExporter::defaultCharFormat
private

◆ doc

const QTextDocument* QTextHtmlExporter::doc
private

◆ fragmentMarkers

bool QTextHtmlExporter::fragmentMarkers
private

Definition at line 407 of file qtextdocument_p.h.

Referenced by emitBlock(), and toHtml().

◆ html

QString QTextHtmlExporter::html
private

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