📄 qtextdocument.cpp
字号:
bool QTextHtmlExporter::emitCharFormatStyle(const QTextCharFormat &format){ bool attributesEmitted = false; { const QString family = format.fontFamily(); if (!family.isEmpty() && family != defaultCharFormat.fontFamily()) { emitFontFamily(family); attributesEmitted = true; } } if (format.hasProperty(QTextFormat::FontPointSize) && format.fontPointSize() != defaultCharFormat.fontPointSize()) { html += QLatin1String(" font-size:"); html += QString::number(format.fontPointSize()); html += QLatin1String("pt;"); attributesEmitted = true; } else if (format.hasProperty(QTextFormat::FontSizeAdjustment)) { static const char * const sizeNames[] = { "small", "medium", "large", "x-large", "xx-large" }; const char *name = 0; const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1; if (idx >= 0 && idx <= 4) { name = sizeNames[idx]; } if (name) { html += QLatin1String(" font-size:"); html += QLatin1String(name); html += QLatin1Char(';'); attributesEmitted = true; } } if (format.hasProperty(QTextFormat::FontWeight) && format.fontWeight() != defaultCharFormat.fontWeight()) { html += QLatin1String(" font-weight:"); html += QString::number(format.fontWeight() * 8); html += QLatin1Char(';'); attributesEmitted = true; } if (format.hasProperty(QTextFormat::FontItalic) && format.fontItalic() != defaultCharFormat.fontItalic()) { html += QLatin1String(" font-style:"); html += (format.fontItalic() ? QLatin1String("italic") : QLatin1String("normal")); html += QLatin1Char(';'); attributesEmitted = true; } QLatin1String decorationTag(" text-decoration:"); html += decorationTag; bool hasDecoration = false; bool atLeastOneDecorationSet = false; if ((format.hasProperty(QTextFormat::FontUnderline) || format.hasProperty(QTextFormat::TextUnderlineStyle)) && format.fontUnderline() != defaultCharFormat.fontUnderline()) { hasDecoration = true; if (format.fontUnderline()) { html += QLatin1String(" underline"); atLeastOneDecorationSet = true; } } if (format.hasProperty(QTextFormat::FontOverline) && format.fontOverline() != defaultCharFormat.fontOverline()) { hasDecoration = true; if (format.fontOverline()) { html += QLatin1String(" overline"); atLeastOneDecorationSet = true; } } if (format.hasProperty(QTextFormat::FontStrikeOut) && format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) { hasDecoration = true; if (format.fontStrikeOut()) { html += QLatin1String(" line-through"); atLeastOneDecorationSet = true; } } if (hasDecoration) { if (!atLeastOneDecorationSet) html += QLatin1String("none"); html += QLatin1Char(';'); attributesEmitted = true; } else { html.chop(qstrlen(decorationTag.latin1())); } if (format.foreground() != defaultCharFormat.foreground() && format.foreground().style() != Qt::NoBrush) { html += QLatin1String(" color:"); html += format.foreground().color().name(); html += QLatin1Char(';'); attributesEmitted = true; } if (format.background() != defaultCharFormat.background() && format.background().style() != Qt::NoBrush) { html += QLatin1String(" background-color:"); html += format.background().color().name(); html += QLatin1Char(';'); attributesEmitted = true; } if (format.verticalAlignment() != defaultCharFormat.verticalAlignment() && format.verticalAlignment() != QTextCharFormat::AlignNormal) { html += QLatin1String(" vertical-align:"); QTextCharFormat::VerticalAlignment valign = format.verticalAlignment(); if (valign == QTextCharFormat::AlignSubScript) html += QLatin1String("sub"); else if (valign == QTextCharFormat::AlignSuperScript) html += QLatin1String("super"); else if (valign == QTextCharFormat::AlignMiddle) html += QLatin1String("middle"); else if (valign == QTextCharFormat::AlignTop) html += QLatin1String("top"); else if (valign == QTextCharFormat::AlignBottom) html += QLatin1String("bottom"); html += QLatin1Char(';'); attributesEmitted = true; } return attributesEmitted;}void QTextHtmlExporter::emitTextLength(const char *attribute, const QTextLength &length){ if (length.type() == QTextLength::VariableLength) // default return; html += QLatin1Char(' '); html += QLatin1String(attribute); html += QLatin1String("=\""); html += QString::number(length.rawValue()); if (length.type() == QTextLength::PercentageLength) html += QLatin1String("%\""); else html += QLatin1String("\"");}void QTextHtmlExporter::emitAlignment(Qt::Alignment align){ if (align & Qt::AlignLeft) return; else if (align & Qt::AlignRight) html += QLatin1String(" align=\"right\""); else if (align & Qt::AlignHCenter) html += QLatin1String(" align=\"center\""); else if (align & Qt::AlignJustify) html += QLatin1String(" align=\"justify\"");}void QTextHtmlExporter::emitFloatStyle(QTextFrameFormat::Position pos, StyleMode mode){ if (pos == QTextFrameFormat::InFlow) return; if (mode == EmitStyleTag) html += QLatin1String(" style=\"float:"); else html += QLatin1String(" float:"); if (pos == QTextFrameFormat::FloatLeft) html += QLatin1String(" left;"); else if (pos == QTextFrameFormat::FloatRight) html += QLatin1String(" right;"); else Q_ASSERT_X(0, "QTextHtmlExporter::emitFloatStyle()", "pos should be a valid enum type"); if (mode == EmitStyleTag) html += QLatin1Char('\"');}void QTextHtmlExporter::emitBorderStyle(QTextFrameFormat::BorderStyle style){ Q_ASSERT(style <= QTextFrameFormat::BorderStyle_Outset); html += QLatin1String(" border-style:"); switch (style) { case QTextFrameFormat::BorderStyle_None: html += QLatin1String("none"); break; case QTextFrameFormat::BorderStyle_Dotted: html += QLatin1String("dotted"); break; case QTextFrameFormat::BorderStyle_Dashed: html += QLatin1String("dashed"); break; case QTextFrameFormat::BorderStyle_Solid: html += QLatin1String("solid"); break; case QTextFrameFormat::BorderStyle_Double: html += QLatin1String("double"); break; case QTextFrameFormat::BorderStyle_DotDash: html += QLatin1String("dot-dash"); break; case QTextFrameFormat::BorderStyle_DotDotDash: html += QLatin1String("dot-dot-dash"); break; case QTextFrameFormat::BorderStyle_Groove: html += QLatin1String("groove"); break; case QTextFrameFormat::BorderStyle_Ridge: html += QLatin1String("ridge"); break; case QTextFrameFormat::BorderStyle_Inset: html += QLatin1String("inset"); break; case QTextFrameFormat::BorderStyle_Outset: html += QLatin1String("outset"); break; default: Q_ASSERT(false); break; }; html += QLatin1Char(';');}void QTextHtmlExporter::emitPageBreakPolicy(QTextFormat::PageBreakFlags policy){ if (policy & QTextFormat::PageBreak_AlwaysBefore) html += QLatin1String(" page-break-before:always;"); if (policy & QTextFormat::PageBreak_AlwaysAfter) html += QLatin1String(" page-break-after:always;");}void QTextHtmlExporter::emitFontFamily(const QString &family){ html += QLatin1String(" font-family:"); QLatin1Char quote('\''); if (family.contains(quote)) quote = QLatin1Char('\"'); html += quote; html += family; html += quote; html += QLatin1Char(';');}void QTextHtmlExporter::emitMargins(const QString &top, const QString &bottom, const QString &left, const QString &right){ html += QLatin1String(" margin-top:"); html += top; html += QLatin1String("px;"); html += QLatin1String(" margin-bottom:"); html += bottom; html += QLatin1String("px;"); html += QLatin1String(" margin-left:"); html += left; html += QLatin1String("px;"); html += QLatin1String(" margin-right:"); html += right; html += QLatin1String("px;");}void QTextHtmlExporter::emitFragment(const QTextFragment &fragment){ const QTextCharFormat format = fragment.charFormat(); bool closeAnchor = false; if (format.isAnchor()) { const QString name = format.anchorName(); if (!name.isEmpty()) { html += QLatin1String("<a name=\""); html += name; html += QLatin1String("\"></a>"); } const QString href = format.anchorHref(); if (!href.isEmpty()) { html += QLatin1String("<a href=\""); html += href; html += QLatin1String("\">"); closeAnchor = true; } } QString txt = fragment.text(); const bool isObject = txt.contains(QChar::ObjectReplacementCharacter); const bool isImage = isObject && format.isImageFormat(); QLatin1String styleTag("<span style=\""); html += styleTag; bool attributesEmitted = false; if (!isImage) attributesEmitted = emitCharFormatStyle(format); if (attributesEmitted) html += QLatin1String("\">"); else html.chop(qstrlen(styleTag.latin1())); if (isObject) { for (int i = 0; isImage && i < txt.length(); ++i) { QTextImageFormat imgFmt = format.toImageFormat(); html += QLatin1String("<img"); if (imgFmt.hasProperty(QTextFormat::ImageName)) emitAttribute("src", imgFmt.name()); if (imgFmt.hasProperty(QTextFormat::ImageWidth)) emitAttribute("width", QString::number(imgFmt.width())); if (imgFmt.hasProperty(QTextFormat::ImageHeight)) emitAttribute("height", QString::number(imgFmt.height())); if (imgFmt.verticalAlignment() == QTextCharFormat::AlignMiddle) html += QLatin1String(" style=\"vertical-align: middle;\""); else if (imgFmt.verticalAlignment() == QTextCharFormat::AlignTop) html += QLatin1String(" style=\"vertical-align: top;\""); if (QTextFrame *imageFrame = qobject_cast<QTextFrame *>(doc->objectForFormat(imgFmt))) emitFloatStyle(imageFrame->frameFormat().position()); html += QLatin1String(" />"); } } else { Q_ASSERT(!txt.contains(QChar::ObjectReplacementCharacter)); txt = Qt::escape(txt); // split for [\n{LineSeparator}] QString forcedLineBreakRegExp = QString::fromLatin1("[\\na]"); forcedLineBreakRegExp[3] = QChar::LineSeparator; const QStringList lines = txt.split(QRegExp(forcedLineBreakRegExp)); for (int i = 0; i < lines.count(); ++i) { if (i > 0) html += QLatin1String("<br />"); // space on purpose for compatibility with Netscape, Lynx & Co. html += lines.at(i); } } if (attributesEmitted) html += QLatin1String("</span>"); if (closeAnchor) html += QLatin1String("</a>");}static bool isOrderedList(int style){ return style == QTextListFormat::ListDecimal || style == QTextListFormat::ListLowerAlpha || style == QTextListFormat::ListUpperAlpha;}void QTextHtmlExporter::emitBlockAttributes(const QTextBlock &block){ QTextBlockFormat format = block.blockFormat(); emitAlignment(format.alignment()); Qt::LayoutDirection dir = format.layoutDirection(); if (dir == Qt::LeftToRight) { // assume default to not bloat the html too much // html += QLatin1String(" dir='ltr'"); } else { html += QLatin1String(" dir='rtl'"); } QLatin1String style(" style=\""); html += style; if (block.begin().atEnd()) { html += QLatin1String("-qt-paragraph-type:empty;"); } emitMargins(QString::number(format.topMargin()), QString::number(format.bottomMargin()), QString::number(format.leftMargin()), QString::number(format.rightMargin())); html += QLatin1String(" -qt-block-indent:"); html += QString::number(format.indent()); html += QLatin1Char(';'); html += QLatin1String(" text-indent:"); html += QString::number(format.indent()); html += QLatin1String("px;"); if (block.userState() != -1) { html += QLatin1String(" -qt-user-state:"); html += QString::number(block.userState()); html += QLatin1Char(';'); } emitPageBreakPolicy(format.pageBreakPolicy()); const QTextCharFormat blockCharFmt = block.charFormat(); QTextCharFormat diff = formatDifference(defaultCharFormat, blockCharFmt).toCharFormat(); diff.clearProperty(QTextFormat::BackgroundBrush); if (format.hasProperty(QTextFormat::BackgroundBrush)) { QBrush bg = format.background(); if (bg.style() != Qt::NoBrush) diff.setProperty(QTextFormat::BackgroundBrush, format.property(QTextFormat::BackgroundBrush)); } if (!diff.properties().isEmpty()) emitCharFormatStyle(diff);#if 0 QBrush blockCharFmtBg = blockCharFmt.b
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -