diff --git a/generators/mobipocket/mobidocument.cpp b/generators/mobipocket/mobidocument.cpp index 5244c80f8..14e857ffa 100644 --- a/generators/mobipocket/mobidocument.cpp +++ b/generators/mobipocket/mobidocument.cpp @@ -1,105 +1,107 @@ /*************************************************************************** * Copyright (C) 2008 by Jakub Stachowski * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "mobidocument.h" #include "mobipocket.h" #include "qfilestream.h" #include #include #include #include using namespace Mobi; MobiDocument::MobiDocument(const QString &fileName) : QTextDocument() { file = new Mobipocket::QFileStream(fileName); doc = new Mobipocket::Document(file); if (doc->isValid()) { QString text=doc->text(); QString header=text.left(1024); if (header.contains("") || header.contains("")) setHtml(fixMobiMarkup(text)); else setPlainText(text); } } MobiDocument::~MobiDocument() { delete doc; delete file; } QVariant MobiDocument::loadResource(int type, const QUrl &name) { if (type!=QTextDocument::ImageResource || name.scheme()!=QString("pdbrec")) return QVariant(); bool ok; quint16 recnum=name.path().mid(1).toUShort(&ok); if (!ok || recnum>=doc->imageCount()) return QVariant(); QVariant resource; resource.setValue(doc->getImage(recnum-1)); addResource(type, name, resource); return resource; } // starting from 'pos', find position in the string that is not inside a tag int outsideTag(const QString& data, int pos) { for (int i=pos-1;i>=0;i--) { if (data[i]=='>') return pos; if (data[i]=='<') return i; } return pos; } QString MobiDocument::fixMobiMarkup(const QString& data) { QString ret=data; QMap anchorPositions; static QRegExp anchors(" it(anchorPositions); while (it.hasNext()) { it.next(); // link pointing outside the document, ignore if ( (it.key()+offset) >= ret.size()) continue; int fixedpos=outsideTag(ret, it.key()+offset); ret.insert(fixedpos,QString(" ")); // inserting anchor shifts all offsets after the anchor offset+=21+it.value().size(); } // replace links referencing filepos with normal internal links ret.replace(anchors," where recindex is number of // record containing image static QRegExp imgs("", Qt::CaseInsensitive); imgs.setMinimal(true); ret.replace(imgs,""); ret.replace("","

"); static QRegExp rgb(" color=\"rgb\\((\\d+),(\\d+),(\\d+)\\)\"", Qt::CaseSensitive); pos = 0; while ((pos = rgb.indexIn(ret, pos)) != -1) { const QString qtColor = QColor(rgb.cap(1).toInt(), rgb.cap(2).toInt(), rgb.cap(3).toInt()).name(); - ret.replace(pos, rgb.matchedLength(), QString(" color=\"%1\"").arg(qtColor)); + const QString newColorString = QString(" color=\"%1\"").arg(qtColor); + ret.replace(pos, rgb.matchedLength(), newColorString); + pos += newColorString.length() - rgb.matchedLength(); } return ret; }