static QImage getImage(QTextDocument *doc, const QTextImageFormat &format) { QImage image; QString name = format.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources name.prepend(QLatin1String("qrc")); QUrl url = QUrl::fromEncoded(name.toUtf8()); const QVariant data = doc->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; #ifndef QT_NO_TEXTBROWSER QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); if (browser) context = browser->source().toString(); #endif if (QTextImageHandler::externalLoader) image = QTextImageHandler::externalLoader(name, context); if (image.isNull()) { // try direct loading name = format.name(); // remove qrc:/ prefix again if (name.isEmpty() || !image.load(name)) return QImage(QLatin1String(":/trolltech/styles/commonstyle/images/file-16.png")); } doc->addResource(QTextDocument::ImageResource, url, image); } return image; }
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 KTextCursor::insertFragment( QList<QPair<QString,QTextCharFormat>>& frags ) { for(QList<QPair<QString,QTextCharFormat>>::iterator iter = frags.begin(); iter != frags.end(); iter++) { QPair<QString,QTextCharFormat>& item = *iter; QString text = item.first; QTextCharFormat fmt = item.second; if(text.at(0) == QChar::ObjectReplacementCharacter) { //图片 QTextImageFormat imgFmt = fmt.toImageFormat(); QString file = imgFmt.name(); if(!file.isEmpty()) { if(moviePool()->insertMovie(file, file)) { fmt.setProperty(KAnimationImage, true); document()->addResource(QTextDocument::ImageResource, QUrl(file), moviePool()->currentImage(file)); } else { QImage image(file); if(!image.isNull()) { document()->addResource(QTextDocument::ImageResource, QUrl(file), image); } } } } insertText(text, fmt); } }
void TextDocument::replaceImageUrl(const QUrl &oldName, const QString &newName) { QList <QPair<int, int> > fragments; QTextBlock block = begin(); while(block.isValid()) { QTextBlock::iterator iterator; for(iterator = block.begin(); !(iterator.atEnd()); ++iterator) { QTextFragment fragment = iterator.fragment(); if(fragment.isValid() && fragment.charFormat().isImageFormat()) { QTextImageFormat format = fragment.charFormat().toImageFormat(); if (QUrl::fromEncoded(format.name().toUtf8()) != oldName) {continue;} fragments.append(QPair<int, int>(fragment.position(), fragment.length())); } } block = block.next(); } QTextCursor cursor(this); cursor.beginEditBlock(); QPair<int, int> pair; foreach (pair, fragments) { cursor.setPosition(pair.first); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, pair.second); QTextImageFormat format = cursor.charFormat().toImageFormat(); format.setName(newName); cursor.mergeCharFormat(format); }
void KTextCursor::insertHtml( const QString &text, const QMap<QString, QString>& imgs ) { if(!imgs.isEmpty()) { for(QMap<QString,QString>::const_iterator iter = imgs.begin(); iter != imgs.end(); iter++) { QString key = iter.key(); QString file = iter.value(); if(moviePool()->insertMovie(file, file)) { //修改属性为动画。 document()->addResource(QTextDocument::ImageResource, QUrl(file), moviePool()->currentImage(file)); } else { QImage image(file); if(!image.isNull()) { document()->addResource(QTextDocument::ImageResource, QUrl(file), image); } } } } int istart = position(); insertHtml(text); int inow = position(); if(!imgs.isEmpty() && inow > istart) { setPosition(istart); movePosition(NextCharacter, KeepAnchor, inow-istart); QString txt = selectedText(); int index = txt.indexOf(QChar::ObjectReplacementCharacter, 0); while(index >= 0) { /*修改字体类型。*/ setPosition(istart+index); movePosition(NextCharacter, KeepAnchor, 1); QTextCharFormat fmt = charFormat(); QTextImageFormat imgFmt = fmt.toImageFormat(); QString key = imgFmt.name(); if(imgs.contains(key)) { imgFmt.setProperty(KImageKey, key); imgFmt.setName(imgs.value(key)); imgFmt.setProperty(KAnimationImage, true); setCharFormat(imgFmt); } int idx = index+1; index = txt.indexOf(QChar::ObjectReplacementCharacter, idx); } } setPosition(inow); }
QTextImageFormat EpsRenderer::render(QTextDocument *document, const Cantor::LatexRenderer* latex) { QTextImageFormat format = render(document, QUrl::fromLocalFile(latex->imagePath())); if (!format.name().isEmpty()) { format.setProperty(CantorFormula, latex->method()); format.setProperty(ImagePath, latex->imagePath()); format.setProperty(Code, latex->latexCode()); } return format; }
QStandardItem *TextDocumentModel::formatItem(const QTextFormat &format) { QStandardItem *item = new QStandardItem; if (!format.isValid()) { item->setText(tr("no format")); } else if (format.isImageFormat()) { const QTextImageFormat imgformat = format.toImageFormat(); item->setText(tr("Image: %1").arg(imgformat.name())); } else { item->setText(tr("Format type: %1").arg(format.type())); } return item; }
QStandardItem *TextDocumentModel::formatItem(const QTextFormat &format) { auto *item = new QStandardItem; if (!format.isValid()) { item->setText(tr("no format")); } else if (format.isImageFormat()) { const QTextImageFormat imgformat = format.toImageFormat(); item->setText(tr("Image: %1").arg(imgformat.name())); } else { item->setText(formatTypeToString(format.type())); } item->setEditable(false); return item; }
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; }
QString Text::extractSanitizedText(int from, int to) const { if (!doc) return ""; QString txt; QTextBlock begin = doc->findBlock(from); QTextBlock end = doc->findBlock(to); for (QTextBlock block = begin; block != end.next() && block.isValid(); block = block.next()) { for (QTextBlock::Iterator itr = block.begin(); itr != block.end(); ++itr) { int pos = itr.fragment() .position(); // fragment position -> position of the first character in the fragment if (itr.fragment().charFormat().isImageFormat()) { QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat(); QString key = imgFmt.name(); // img key (eg. key::D for :D) QString rune = key.mid(4); if (pos >= from && pos < to) { txt += rune; ++pos; } } else { for (QChar c : itr.fragment().text()) { if (pos >= from && pos < to) txt += c; ++pos; } } } txt += '\n'; } txt.chop(1); return txt; }
static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0) { QPixmap pm; QString name = format.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources and convert them to url name.prepend(QLatin1String("qrc")); QUrl url = QUrl(name); name = resolveFileName(name, &url, devicePixelRatio); const QVariant data = doc->resource(QTextDocument::ImageResource, url); if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) { pm = qvariant_cast<QPixmap>(data); } else if (data.type() == QVariant::ByteArray) { pm.loadFromData(data.toByteArray()); } if (pm.isNull()) { #if 0 QString context; // ### Qt5 QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); if (browser) context = browser->source().toString(); #endif // try direct loading QImage img; if (name.isEmpty() || !img.load(name)) return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); pm = QPixmap::fromImage(img); doc->addResource(QTextDocument::ImageResource, url, pm); } if (name.contains(QStringLiteral("@2x"))) pm.setDevicePixelRatio(2.0); return pm; }
QString TextEditEx::getPlainText(int _from, int _to) const { if (_from == _to) return ""; if (_to != -1 && _to < _from) { assert(!"invalid data"); return ""; } QString out_string; QTextStream result(&out_string); int pos_start = 0; int length = 0; bool first = true; for (QTextBlock it_block = document()->begin(); it_block != document()->end(); it_block = it_block.next()) { if (!first) result << '\n'; pos_start = it_block.position(); if (_to != -1 && pos_start >= _to) break; for (QTextBlock::iterator it_fragment = it_block.begin(); it_fragment != it_block.end(); ++it_fragment) { QTextFragment currentFragment = it_fragment.fragment(); if (currentFragment.isValid()) { pos_start = currentFragment.position(); length = currentFragment.length(); if (pos_start + length <= _from) continue; if (_to != -1 && pos_start >= _to) break; first = false; if (currentFragment.charFormat().isImageFormat()) { if (pos_start < _from) continue; QTextImageFormat imgFmt = currentFragment.charFormat().toImageFormat(); auto iter = resource_index_.find(imgFmt.name()); if (iter != resource_index_.end()) result << iter->second; } else { QString fragment_text = currentFragment.text(); int c_start = std::max((_from - pos_start), 0); int count = -1; if (_to != -1 && _to <= pos_start + length) count = _to - pos_start - c_start; QString txt = fragment_text.mid(c_start, count); txt.remove(QChar::SoftHyphen); QChar *uc = txt.data(); QChar *e = uc + txt.size(); for (; uc != e; ++uc) { switch (uc->unicode()) { case 0xfdd0: // QTextBeginningOfFrame case 0xfdd1: // QTextEndOfFrame case QChar::ParagraphSeparator: case QChar::LineSeparator: *uc = QLatin1Char('\n'); break; case QChar::Nbsp: *uc = QLatin1Char(' '); break; default: ; } } result << txt; } } } } return out_string; }
void Interface::SetFormat( QTextImageFormat e ) { nowimage = e; SetPic(e.name()); }
QString TextDocumentSerializer::relativeImagePath(QTextImageFormat format) { QString text = format.name(); text.replace("file://", QString("./%1/").arg(resources->imageFolderName())); return text; }
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; }
QString validHtml(const QString &html, bool allowReplacement, QTextCursor *tc) { QDesktopWidget dw; ValidDocument oValidDocument(allowReplacement); QRectF qr = dw.availableGeometry(); oValidDocument.setTextWidth(qr.width() / 2); oValidDocument.setDefaultStyleSheet(qApp->styleSheet()); oValidDocument.setHtml(html); bool fIsValid = oValidDocument.isValid(); QStringList qslAllowed = allowedSchemes(); for (QTextBlock qtb = oValidDocument.begin(); qtb != oValidDocument.end(); qtb = qtb.next()) { for (QTextBlock::iterator qtbi = qtb.begin(); qtbi != qtb.end(); ++qtbi) { const QTextFragment &qtf = qtbi.fragment(); QTextCharFormat qcf = qtf.charFormat(); if (! qcf.anchorHref().isEmpty()) { QUrl url(qcf.anchorHref()); if (! url.isValid() || ! qslAllowed.contains(url.scheme())) { QTextCharFormat qcfn = QTextCharFormat(); QTextCursor qtc(&oValidDocument); qtc.setPosition(qtf.position(), QTextCursor::MoveAnchor); qtc.setPosition(qtf.position()+qtf.length(), QTextCursor::KeepAnchor); qtc.setCharFormat(qcfn); qtbi = qtb.begin(); } } if (qcf.isImageFormat()) { QTextImageFormat qtif = qcf.toImageFormat(); QUrl url(qtif.name()); if (! qtif.name().isEmpty() && ! url.isValid()) fIsValid = false; } } } oValidDocument.adjustSize(); QSizeF s = oValidDocument.size(); if (!fIsValid || (s.width() > qr.width()) || (s.height() > qr.height())) { oValidDocument.setPlainText(html); oValidDocument.adjustSize(); s = oValidDocument.size(); if ((s.width() > qr.width()) || (s.height() > qr.height())) { QString errorMessage = "[[ Text object too large to display ]]"; if (tc) { tc->insertText(errorMessage); return QString(); } else { return errorMessage; } } } if (tc) { QTextCursor tcNew(&oValidDocument); tcNew.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); tc->insertFragment(tcNew.selection()); return QString(); } else { return oValidDocument.toHtml(); } }
QString Format::frameToString( QTextFrame *frame ) { QString out; QTextFrame::iterator it; for( it = frame->begin(); it != frame->end(); ++it ) { QTextBlock block = it.currentBlock(); if ( block.isValid() ) { out += "<block"; QTextCursor c( block ); QDateTime dt = TextFormats::lastModified( c ); if ( dt.isValid() ) { out += " lastmodified=\"" + dt.toString( Qt::ISODate ) + "\""; } if ( TextFormats::isTitle( c ) ) { out += " titlestyle=\"title\""; } else if ( TextFormats::isSubTitle( c ) ) { out += " titlestyle=\"subtitle\""; } QTextBlockFormat blockFormat = block.blockFormat(); if ( blockFormat.isValid() ) { QTextList *list = block.textList(); if ( list ) { QTextListFormat f = list->format(); out += " liststyle=\""; switch( f.style() ) { default: case QTextListFormat::ListDisc: out += "disc"; break; case QTextListFormat::ListDecimal: out += "decimal"; break; } out += "\""; out += " listindent=\"" + QString::number( f.indent() ) + "\""; } else { if ( blockFormat.indent() != 0 ) { out += " blockindent=\"" + QString::number( blockFormat.indent() ) + "\""; } } } out += ">\n"; QTextBlock::iterator it2; for( it2 = block.begin(); it2 != block.end(); ++it2 ) { QTextFragment fragment = it2.fragment(); if ( !fragment.isValid() ) continue; QString text = fragment.text(); QString outText; for( int i = 0; i < text.size(); ++i ) { if ( text.at( i ) == 0xfffc ) { outText += "<todo status=\""; QTextImageFormat imageFormat = fragment.charFormat().toImageFormat(); if ( imageFormat.isValid() ) { if ( imageFormat.name().contains( "done" ) ) outText += "done"; else outText += "todo"; } else { dbg() << "NO IMAGE FORMAT" << endl; } outText += "\"/>"; } else { outText += escape( QString( text.at( i ) ) ); } } out += " <fragment"; QTextCharFormat format = fragment.charFormat(); if ( !format.anchorHref().isEmpty() ) { out += " link=\"" + escape( format.anchorHref() ) + "\""; } if ( format.fontWeight() == QFont::Bold ) { out += " bold=\"true\""; } if ( format.fontItalic() ) { out += " italic=\"true\""; } if ( format.hasProperty( QTextFormat::FontPointSize ) && format.fontPointSize() != 10 ) { out += " fontsize=\"" + QString::number( format.fontPointSize() ) + "\""; } if ( outText.trimmed().isEmpty() ) outText.replace( " ", "[FIXME:space]" ); out += ">" + outText + "</fragment>\n"; } out += "</block>"; out += "\n"; } QTextFrame *f = it.currentFrame(); if ( f ) { QTextFrameFormat format = f->frameFormat(); out += "<frame"; if ( format.hasProperty( TextFormats::FrameType ) ) { out += " type="; if ( format.property( TextFormats::FrameType ) == TextFormats::CodeFrame ) { out += "\"code\""; } else { out += "\"undefined\""; } } out += ">\n"; out += frameToString( f ); out += "</frame>\n"; } } return out; }