diff --git a/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.cpp b/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.cpp index dede78350..b17273757 100644 --- a/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.cpp +++ b/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.cpp @@ -109,38 +109,14 @@ void OEBBookReader::startElementHandler(const char *tag, const char **xmlattribu if (title != 0) { myGuideTOC.push_back(std::make_pair(std::string(title), reference)); } - if (type != 0) { - if (COVER == type) { - ZLFile imageFile(myFilePrefix + reference); - myCoverFileName = imageFile.path(); - const std::map::const_iterator it = - myHrefToMediatype.find(reference); - const std::string mimeType = - it != myHrefToMediatype.end() ? it->second : std::string(); - shared_ptr image; - if (ZLStringUtil::stringStartsWith(mimeType, "image/")) { - image = new ZLFileImage(imageFile, "", 0); - } else { - image = XHTMLImageFinder().readImage(imageFile); - } - if (!image.isNull()) { - const std::string imageName = imageFile.name(false); - myModelReader.setMainTextModel(); - myModelReader.addImageReference(imageName, (short)0, true); - myModelReader.addImage(imageName, image); - myModelReader.insertEndOfSectionParagraph(); - } else { - myCoverFileName.erase(); - } - } else if (COVER_IMAGE == type) { - ZLFile imageFile(myFilePrefix + reference); - myCoverFileName = imageFile.path(); - const std::string imageName = imageFile.name(false); - myModelReader.setMainTextModel(); - myModelReader.addImageReference(imageName, 0, true); - myModelReader.addImage(imageName, new ZLFileImage(imageFile, "", 0)); - myModelReader.insertEndOfSectionParagraph(); - } + if ((type != 0) && (COVER == type || COVER_IMAGE == type)) { + ZLFile imageFile(myFilePrefix + reference); + myCoverFileName = imageFile.path(); + myCoverFileType = type; + const std::map::const_iterator it = + myHrefToMediatype.find(reference); + myCoverMimeType = + it != myHrefToMediatype.end() ? it->second : std::string(); } } } @@ -157,6 +133,34 @@ void OEBBookReader::startElementHandler(const char *tag, const char **xmlattribu } } +void OEBBookReader::addCoverImage() { + if (COVER == myCoverFileType) { + ZLFile imageFile(myCoverFileName); + shared_ptr image; + if (ZLStringUtil::stringStartsWith(myCoverMimeType, "image/")) { + image = new ZLFileImage(imageFile, "", 0); + } else { + image = XHTMLImageFinder().readImage(imageFile); + } + if (!image.isNull()) { + const std::string imageName = imageFile.name(false); + myModelReader.setMainTextModel(); + myModelReader.addImageReference(imageName, (short)0, true); + myModelReader.addImage(imageName, image); + myModelReader.insertEndOfSectionParagraph(); + } else { + myCoverFileName.erase(); + } + } else if (COVER_IMAGE == myCoverFileType) { + ZLFile imageFile(myCoverFileName); + const std::string imageName = imageFile.name(false); + myModelReader.setMainTextModel(); + myModelReader.addImageReference(imageName, 0, true); + myModelReader.addImage(imageName, new ZLFileImage(imageFile, "", 0)); + myModelReader.insertEndOfSectionParagraph(); + } +} + void OEBBookReader::endElementHandler(const char *tag) { std::string tagString = ZLUnicodeUtil::toLower(tag); @@ -193,6 +197,8 @@ bool OEBBookReader::readBook(const ZLFile &file) { myHtmlFileNames.clear(); myNCXTOCFileName.erase(); myCoverFileName.erase(); + myCoverFileType.erase(); + myCoverMimeType.erase(); myTourTOC.clear(); myGuideTOC.clear(); myState = READ_NONE; @@ -208,10 +214,13 @@ bool OEBBookReader::readBook(const ZLFile &file) { bool firstFile = true; for (std::vector::const_iterator it = myHtmlFileNames.begin(); it != myHtmlFileNames.end(); ++it) { const ZLFile xhtmlFile(myFilePrefix + *it); - if (firstFile && myCoverFileName == xhtmlFile.path()) { - continue; - } - if (!firstFile) { + if (firstFile) { + if (myCoverFileName == xhtmlFile.path()) { + xhtmlReader.setMarkFirstImageAsCover(); + } else { + addCoverImage(); + } + } else { myModelReader.insertEndOfSectionParagraph(); } xhtmlReader.readFile(xhtmlFile, *it); diff --git a/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.h b/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.h index dafc1fdcf..9314e870d 100644 --- a/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.h +++ b/jni/NativeFormats/fbreader/src/formats/oeb/OEBBookReader.h @@ -44,6 +44,7 @@ private: const std::vector &externalDTDs() const; void generateTOC(const XHTMLReader &xhtmlReader); + void addCoverImage(); private: enum ReaderState { @@ -63,6 +64,8 @@ private: std::vector myHtmlFileNames; std::string myNCXTOCFileName; std::string myCoverFileName; + std::string myCoverFileType; + std::string myCoverMimeType; std::vector > myTourTOC; std::vector > myGuideTOC; }; diff --git a/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.cpp b/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.cpp index c5652b375..aa66607af 100644 --- a/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.cpp +++ b/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.cpp @@ -218,8 +218,9 @@ void XHTMLTagLinkAction::doAtStart(XHTMLReader &reader, const char **xmlattribut return; } - ZLLogger::Instance().println("CSS", "style file: " + reader.myPathPrefix + MiscUtil::decodeHtmlURL(href)); - shared_ptr cssStream = ZLFile(reader.myPathPrefix + MiscUtil::decodeHtmlURL(href)).inputStream(); + const std::string cssFilePath = reader.myPathPrefix + MiscUtil::decodeHtmlURL(href); + ZLLogger::Instance().println("CSS", "style file: " + cssFilePath); + shared_ptr cssStream = ZLFile(cssFilePath).inputStream(); if (cssStream.isNull()) { return; } @@ -307,8 +308,9 @@ void XHTMLTagImageAction::doAtStart(XHTMLReader &reader, const char **xmlattribu endParagraph(reader); } const std::string imageName = imageFile.name(false); - bookReader(reader).addImageReference(imageName, 0, false); + bookReader(reader).addImageReference(imageName, 0, reader.myMarkNextImageAsCover); bookReader(reader).addImage(imageName, new ZLFileImage(imageFile, "", 0)); + reader.myMarkNextImageAsCover = false; if (flag) { beginParagraph(reader); } @@ -516,6 +518,11 @@ void XHTMLReader::fillTagTable() { } XHTMLReader::XHTMLReader(BookReader &modelReader) : myModelReader(modelReader) { + myMarkNextImageAsCover = false; +} + +void XHTMLReader::setMarkFirstImageAsCover() { + myMarkNextImageAsCover = true; } bool XHTMLReader::readFile(const ZLFile &file, const std::string &referenceName) { @@ -588,7 +595,6 @@ void XHTMLReader::startElementHandler(const char *tag, const char **attributes) shared_ptr entry = myStyleParser->parseString(style); myModelReader.addStyleEntry(*entry); myStyleEntryStack.push_back(entry); - } else { } myCSSStack.push_back(myStyleEntryStack.size() - sizeBefore); } diff --git a/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.h b/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.h index fc1d52d17..bb497abf4 100644 --- a/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.h +++ b/jni/NativeFormats/fbreader/src/formats/xhtml/XHTMLReader.h @@ -65,6 +65,7 @@ public: bool readFile(const ZLFile &file, const std::string &referenceName); const std::string &fileAlias(const std::string &fileName) const; const std::string normalizedReference(const std::string &reference) const; + void setMarkFirstImageAsCover(); private: XHTMLTagAction *getAction(const std::string &tag); @@ -104,6 +105,7 @@ private: READ_BODY } myReadState; int myBodyCounter; + bool myMarkNextImageAsCover; friend class XHTMLTagAction; friend class XHTMLTagStyleAction; @@ -113,6 +115,7 @@ private: friend class XHTMLTagParagraphAction; friend class XHTMLTagBodyAction; friend class XHTMLTagRestartParagraphAction; + friend class XHTMLTagImageAction; }; #endif /* __XHTMLREADER_H__ */