示例#1
0
// Richtext simplification filter: Remove hard-coded font settings,
// <style> elements, <p> attributes other than 'align' and
// and unnecessary meta-information.
QString simplifyRichTextFilter(const QString &in, bool *isPlainTextPtr = 0)
{
	unsigned elementCount = 0;
	bool paragraphAlignmentFound = false;
	QString out;
	QXmlStreamReader reader(in);
	QXmlStreamWriter writer(&out);
	writer.setAutoFormatting(false);
	writer.setAutoFormattingIndent(0);

	while (!reader.atEnd()) {
		switch (reader.readNext()) {
		case QXmlStreamReader::StartElement:
			elementCount++;
			if (filterElement(reader.name())) {
				const QStringRef name = reader.name();
				QXmlStreamAttributes attributes = reader.attributes();
				filterAttributes(name, &attributes, &paragraphAlignmentFound);
				writer.writeStartElement(name.toString());
				if (!attributes.isEmpty())
					writer.writeAttributes(attributes);
			} else
				reader.readElementText(); // Skip away all nested elements and characters.
			break;
		case QXmlStreamReader::Characters:
			if (!isWhiteSpace(reader.text()))
				writer.writeCharacters(reader.text().toString());
			break;
		case QXmlStreamReader::EndElement:
			writer.writeEndElement();
			break;
		default:
			break;
		}
	}
	// Check for plain text (no spans, just <html><head><body><p>)
	if (isPlainTextPtr)
		*isPlainTextPtr = !paragraphAlignmentFound && elementCount == 4u; //
	return out;
}
示例#2
0
bool OdtWriter::writeParagraphStyle(const QTextBlockFormat& format, const QString& name)
{
	QXmlStreamAttributes attributes;
	bool rtl = format.layoutDirection() == Qt::RightToLeft;
	if (rtl) {
		attributes.append(QString::fromLatin1("style:writing-mode"), QString::fromLatin1("rl"));
	}

	Qt::Alignment align = format.alignment();
	if (rtl && (align & Qt::AlignLeft)) {
		attributes.append(QString::fromLatin1("fo:text-align"), QString::fromLatin1("left"));
	} else if (align & Qt::AlignRight) {
		attributes.append(QString::fromLatin1("fo:text-align"), QString::fromLatin1("right"));
	} else if (align & Qt::AlignCenter) {
		attributes.append(QString::fromLatin1("fo:text-align"), QString::fromLatin1("center"));
	} else if (align & Qt::AlignJustify) {
		attributes.append(QString::fromLatin1("fo:text-align"), QString::fromLatin1("justify"));
	}

	if (format.indent() > 0) {
		attributes.append(QString::fromLatin1("fo:margin-left"), QString::number(format.indent() * 0.5) + QString::fromLatin1("in"));
	}

	if (attributes.isEmpty()) {
		return false;
	}

	m_xml.writeStartElement(QString::fromLatin1("style:style"));
	m_xml.writeAttribute(QString::fromLatin1("style:name"), name);
	m_xml.writeAttribute(QString::fromLatin1("style:family"), QString::fromLatin1("paragraph"));
	m_xml.writeAttribute(QString::fromLatin1("style:parent-style-name"), QString::fromLatin1("Normal"));

	m_xml.writeEmptyElement(QString::fromLatin1("style:paragraph-properties"));
	m_xml.writeAttributes(attributes);
	m_xml.writeEndElement();

	return true;
}
示例#3
0
bool OdtWriter::writeTextStyle(const QTextCharFormat& format, const QString& name)
{
	QXmlStreamAttributes attributes;
	if (format.fontWeight() == QFont::Bold) {
		attributes.append(QString::fromLatin1("fo:font-weight"), QString::fromLatin1("bold"));
	}
	if (format.fontItalic()) {
		attributes.append(QString::fromLatin1("fo:font-style"), QString::fromLatin1("italic"));
	}
	if (format.fontUnderline()) {
		attributes.append(QString::fromLatin1("style:text-underline-type"), QString::fromLatin1("single"));
		attributes.append(QString::fromLatin1("style:text-underline-style"), QString::fromLatin1("solid"));
	}
	if (format.fontStrikeOut()) {
		attributes.append(QString::fromLatin1("style:text-line-through-type"), QString::fromLatin1("single"));
	}
	if (format.verticalAlignment() == QTextCharFormat::AlignSuperScript) {
		attributes.append(QString::fromLatin1("style:text-position"), QString::fromLatin1("super"));
	} else if (format.verticalAlignment() == QTextCharFormat::AlignSubScript) {
		attributes.append(QString::fromLatin1("style:text-position"), QString::fromLatin1("sub"));
	}

	if (attributes.isEmpty()) {
		return false;
	}

	m_xml.writeStartElement(QString::fromLatin1("style:style"));
	m_xml.writeAttribute(QString::fromLatin1("style:name"), name);
	m_xml.writeAttribute(QString::fromLatin1("style:family"), QString::fromLatin1("text"));

	m_xml.writeEmptyElement(QString::fromLatin1("style:text-properties"));
	m_xml.writeAttributes(attributes);
	m_xml.writeEndElement();

	return true;
}