static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format) { QImage image; const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); const int width = qRound(format.width()); const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); const int height = qRound(format.height()); QSize size(width, height); if (!hasWidth || !hasHeight) { image = getImage(doc, format); if (!hasWidth) size.setWidth(image.width()); if (!hasHeight) size.setHeight(image.height()); } qreal scale = 1.0; QPaintDevice *pdev = doc->documentLayout()->paintDevice(); if (pdev) { extern int qt_defaultDpi(); if (image.isNull()) image = getImage(doc, format); if (!image.isNull()) scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); } size *= scale; return size; }
void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const { writer.writeStartElement(drawNS, QString::fromLatin1("frame")); if (m_strategy == 0) { // don't do anything. } else if (fragment.charFormat().isImageFormat()) { QTextImageFormat imageFormat = fragment.charFormat().toImageFormat(); writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name()); // vvv Copy pasted mostly from Qt ================= QImage image; QString name = imageFormat.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources name.prepend(QLatin1String("qrc")); QUrl url = QUrl::fromEncoded(name.toUtf8()); const QVariant data = m_document->resource(QTextDocument::ImageResource, url); if (data.type() == QVariant::Image) { image = qvariant_cast<QImage>(data); } else if (data.type() == QVariant::ByteArray) { image.loadFromData(data.toByteArray()); } if (image.isNull()) { QString context; if (QTextImageHandler::externalLoader) image = QTextImageHandler::externalLoader(name, context); if (image.isNull()) { // try direct loading name = imageFormat.name(); // remove qrc:/ prefix again image.load(name); } } // ^^^ Copy pasted mostly from Qt ================= if (! image.isNull()) { QBuffer imageBytes; QImageWriter imageWriter(&imageBytes, "png"); imageWriter.write(image); QString filename = m_strategy->createUniqueImageName(); m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data()); // get the width/height from the format. qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width(); writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width)); qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height(); writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height)); writer.writeStartElement(drawNS, QString::fromLatin1("image")); writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename); writer.writeEndElement(); // image } } writer.writeEndElement(); // frame }
void KDReports::TextDocumentData::updatePercentSize( QTextImageFormat& imageFormat, const QSizeF& size ) { // "W50" means W=50%. "H60" means H=60%. QString prop = imageFormat.property( ResizableImageProperty ).toString(); const qreal imageRatio = imageFormat.height() / imageFormat.width(); const qreal pageWidth = size.width(); const qreal pageHeight = size.height(); const qreal pageRatio = pageWidth ? pageHeight / pageWidth : 0; if ( prop[0] == QLatin1Char( 'T' ) ) { //qDebug() << "updatePercentSize fitToPage" << imageRatio << pageRatio; if ( imageRatio < pageRatio ) { prop = QString::fromLatin1( "W100" ); } else { prop = QString::fromLatin1( "H100" ); } } const qreal percent = prop.mid( 1 ).toDouble(); switch ( prop[0].toLatin1() ) { case 'W': { const qreal newWidth = pageWidth * percent / 100.0; imageFormat.setWidth( newWidth ); imageFormat.setHeight( newWidth * imageRatio ); // ### I needed to add this -2 here for 100%-width images to fit in if ( percent == 100.0 ) imageFormat.setWidth( imageFormat.width() - 2 ); } break; case 'H': imageFormat.setHeight( pageHeight * percent / 100.0 ); // ### I needed to add -6 here for 100%-height images to fit in (with Qt-4.4) // and it became -9 with Qt-4.5, and even QtSw doesn't know why. // Task number 241890 if ( percent == 100.0 ) imageFormat.setHeight( imageFormat.height() - 10 ); imageFormat.setWidth( imageRatio ? imageFormat.height() / imageRatio : 0 ); //qDebug() << "updatePercentSize" << size << "->" << imageFormat.width() << "x" << imageFormat.height(); break; default: qWarning( "Unhandled image format property type - internal error" ); } }
void TextDocumentRtfOutput::createImage(const QImage &image, const QTextImageFormat &format) { #if 0 QString imageUuid = QString("rtfparser://") + QUuid::createUuid().toString(); m_document->addResource(QTextDocument::ImageResource, QUrl(imageUuid), QVariant(image)); format.setName(imageUuid); m_cursor->insertImage(format); #else m_cursor->insertImage(image.scaled(format.width(), format.height())); #endif }
QString TextDocumentSerializer::getImageTag(QTextImageFormat format) { int width = format.width(); int height = format.height(); QString text; resources->setDocument(document); resources->saveImage(format.name()); if(width == 0 || height == 0) text = QString("<img src=\"%1\">").arg(relativeImagePath(format)); else text = QString("<img src=\"%1\" width=\"%2\" height=\"%3\">") .arg(relativeImagePath(format)) .arg(width) .arg(height); return text; }
static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format) { QPixmap pm; const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); const int width = qRound(format.width()); const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); const int height = qRound(format.height()); QSize size(width, height); if (!hasWidth || !hasHeight) { pm = getPixmap(doc, format); const int pmWidth = pm.width() / pm.devicePixelRatio(); const int pmHeight = pm.height() / pm.devicePixelRatio(); if (!hasWidth) { if (!hasHeight) size.setWidth(pmWidth); else size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight))); } if (!hasHeight) { if (!hasWidth) size.setHeight(pmHeight); else size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth))); } } qreal scale = 1.0; QPaintDevice *pdev = doc->documentLayout()->paintDevice(); if (pdev) { if (pm.isNull()) pm = getPixmap(doc, format); if (!pm.isNull()) scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); } size *= scale; return size; }
void HtmlExporter::emitFragment( const QTextFragment &fragment, const QTextBlockFormat &blockFormat ) { // kDebug() << "html" << html; const QTextCharFormat format = fragment.charFormat(); bool closeAnchor = false; bool anchorIsOpen = false; if ( format.isAnchor() ) { // const QStringList names = format.anchorNames(); // if (!names.isEmpty()) { // html += QLatin1String("<a name=\""); // html += names.at(0); // html += QLatin1String("\" "); // anchorIsOpen = true; // } const QString href = format.anchorHref(); if ( !href.isEmpty() ) { // if (!anchorIsOpen) { // html += QLatin1String("<a "); // anchorIsOpen = true; // } html += QLatin1String( "<a href=\"" ); html += href; html += QLatin1String( "\"" ); anchorIsOpen = true; // closeAnchor = true; // html += QLatin1String("\""); } if ( format.hasProperty( BilboTextFormat::AnchorTitle ) ) { const QString title = format.stringProperty( BilboTextFormat::AnchorTitle ); if ( !title.isEmpty() ) { html += QLatin1String( " title=\"" ); html += title; html += QLatin1String( "\"" ); } } if ( format.hasProperty( BilboTextFormat::AnchorTarget ) ) { const QString target = format.stringProperty( BilboTextFormat::AnchorTarget ); if ( !target.isEmpty() ) { html += QLatin1String( " target=\"" ); html += target; html += QLatin1String( "\"" ); } } if ( anchorIsOpen ) { html += QLatin1String( ">" ); closeAnchor = true; } } QList<tag> tags = emitCharFormatStyle( format, blockFormat ); // if ( !format.anchorHref().isNull() ) { // html += QLatin1String(">"); // closeAnchor = true; // } // kDebug() << "tags count" << tags.count() << endl; for ( int i = 0; i < tags.count(); ++i ) { switch ( tags.at( i ) ) { case span: break; //Jump // case h1: // html += QLatin1String( "<h1>" ); // break; // case h2: // html += QLatin1String( "<h2>" ); // break; // case h3: // html += QLatin1String( "<h3>" ); // break; // case h4: // html += QLatin1String( "<h4>" ); // break; // case h5: // html += QLatin1String( "<h5>" ); // break; case strong: html += QLatin1String( "<strong>" ); break; case em: html += QLatin1String( "<em>" ); break; case s: html += QLatin1String( "<s>" ); break; case u: if ( !closeAnchor ) html += QLatin1String( "<u>" ); break; case code: html += QLatin1String( "<code>" ); break; case sub: html += QLatin1String( "<sub>" ); break; case sup: html += QLatin1String( "<sup>" ); break; } } /* QLatin1String styleTag("<span style=\""); html += styleTag; const bool attributesEmitted = emitCharFormatStyle(format); if (attributesEmitted) html += QLatin1String("\">"); else html.chop(qstrlen(styleTag.latin1())); */ QString txt = fragment.text(); // kDebug() << txt ; if ( txt.count() == 1 && txt.at( 0 ) == QChar::ObjectReplacementCharacter ) { if ( format.isImageFormat() ) { QTextImageFormat imgFmt = format.toImageFormat(); html += QLatin1String( "<img" ); if ( imgFmt.hasProperty( QTextFormat::ImageName ) ) { emitAttribute( "src", imgFmt.name() ); } if ( imgFmt.hasProperty( BilboTextFormat::ImageTitle ) ) { const QString title = imgFmt.stringProperty( BilboTextFormat::ImageTitle ); if ( !title.isEmpty() ) { emitAttribute( "title", imgFmt.stringProperty( BilboTextFormat::ImageTitle ) ); } } if ( imgFmt.hasProperty( BilboTextFormat::ImageAlternateText ) ) { const QString alternate = imgFmt.stringProperty( BilboTextFormat::ImageAlternateText ); if ( !alternate.isEmpty() ) { emitAttribute( "alt", imgFmt.stringProperty( BilboTextFormat::ImageAlternateText ) ); } } if ( imgFmt.hasProperty( QTextFormat::ImageWidth ) ) { emitAttribute( "width", QString::number( imgFmt.width() ) ); } if ( imgFmt.hasProperty( QTextFormat::ImageHeight ) ) { emitAttribute( "height", QString::number( imgFmt.height() ) ); } 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. //and to convert LineSeparators to <br /> tags. html += lines.at( i ); } } // kDebug() << html ; //Close Tags //if (!closeAnchor) for ( int i = tags.count(); i > 0; --i ) { switch ( tags.at( i - 1 ) ) { case span: html += QLatin1String( "</span>" ); break; //Jump // case h1: // html += QLatin1String( "</h1>" ); // break; // case h2: // html += QLatin1String( "</h2>" ); // break; // case h3: // html += QLatin1String( "</h3>" ); // break; // case h4: // html += QLatin1String( "</h4>" ); // break; // case h5: // html += QLatin1String( "</h5>" ); // break; case strong: html += QLatin1String( "</strong>" ); break; case em: html += QLatin1String( "</em>" ); break; case s: html += QLatin1String( "</s>" ); break; case u: if ( !closeAnchor ) html += QLatin1String( "</u>" ); break; case code: html += QLatin1String( "</code>" ); break; case sub: html += QLatin1String( "</sub>" ); break; case sup: html += QLatin1String( "</sup>" ); break; } } /* if (attributesEmitted) html += QLatin1String("</span>"); */ if ( closeAnchor ) { html += QLatin1String( "</a>" ); } // kDebug() << "html=>" << html; }