1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

do not skip text from ePub cover files

This commit is contained in:
Nikolay Pultsin 2013-08-10 03:06:10 +02:00
parent c9505c21f9
commit 1feb61a896
4 changed files with 61 additions and 40 deletions

View file

@ -109,38 +109,14 @@ void OEBBookReader::startElementHandler(const char *tag, const char **xmlattribu
if (title != 0) { if (title != 0) {
myGuideTOC.push_back(std::make_pair(std::string(title), reference)); myGuideTOC.push_back(std::make_pair(std::string(title), reference));
} }
if (type != 0) { if ((type != 0) && (COVER == type || COVER_IMAGE == type)) {
if (COVER == type) {
ZLFile imageFile(myFilePrefix + reference); ZLFile imageFile(myFilePrefix + reference);
myCoverFileName = imageFile.path(); myCoverFileName = imageFile.path();
myCoverFileType = type;
const std::map<std::string,std::string>::const_iterator it = const std::map<std::string,std::string>::const_iterator it =
myHrefToMediatype.find(reference); myHrefToMediatype.find(reference);
const std::string mimeType = myCoverMimeType =
it != myHrefToMediatype.end() ? it->second : std::string(); it != myHrefToMediatype.end() ? it->second : std::string();
shared_ptr<const ZLImage> 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();
}
} }
} }
} }
@ -157,6 +133,34 @@ void OEBBookReader::startElementHandler(const char *tag, const char **xmlattribu
} }
} }
void OEBBookReader::addCoverImage() {
if (COVER == myCoverFileType) {
ZLFile imageFile(myCoverFileName);
shared_ptr<const ZLImage> 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) { void OEBBookReader::endElementHandler(const char *tag) {
std::string tagString = ZLUnicodeUtil::toLower(tag); std::string tagString = ZLUnicodeUtil::toLower(tag);
@ -193,6 +197,8 @@ bool OEBBookReader::readBook(const ZLFile &file) {
myHtmlFileNames.clear(); myHtmlFileNames.clear();
myNCXTOCFileName.erase(); myNCXTOCFileName.erase();
myCoverFileName.erase(); myCoverFileName.erase();
myCoverFileType.erase();
myCoverMimeType.erase();
myTourTOC.clear(); myTourTOC.clear();
myGuideTOC.clear(); myGuideTOC.clear();
myState = READ_NONE; myState = READ_NONE;
@ -208,10 +214,13 @@ bool OEBBookReader::readBook(const ZLFile &file) {
bool firstFile = true; bool firstFile = true;
for (std::vector<std::string>::const_iterator it = myHtmlFileNames.begin(); it != myHtmlFileNames.end(); ++it) { for (std::vector<std::string>::const_iterator it = myHtmlFileNames.begin(); it != myHtmlFileNames.end(); ++it) {
const ZLFile xhtmlFile(myFilePrefix + *it); const ZLFile xhtmlFile(myFilePrefix + *it);
if (firstFile && myCoverFileName == xhtmlFile.path()) { if (firstFile) {
continue; if (myCoverFileName == xhtmlFile.path()) {
xhtmlReader.setMarkFirstImageAsCover();
} else {
addCoverImage();
} }
if (!firstFile) { } else {
myModelReader.insertEndOfSectionParagraph(); myModelReader.insertEndOfSectionParagraph();
} }
xhtmlReader.readFile(xhtmlFile, *it); xhtmlReader.readFile(xhtmlFile, *it);

View file

@ -44,6 +44,7 @@ private:
const std::vector<std::string> &externalDTDs() const; const std::vector<std::string> &externalDTDs() const;
void generateTOC(const XHTMLReader &xhtmlReader); void generateTOC(const XHTMLReader &xhtmlReader);
void addCoverImage();
private: private:
enum ReaderState { enum ReaderState {
@ -63,6 +64,8 @@ private:
std::vector<std::string> myHtmlFileNames; std::vector<std::string> myHtmlFileNames;
std::string myNCXTOCFileName; std::string myNCXTOCFileName;
std::string myCoverFileName; std::string myCoverFileName;
std::string myCoverFileType;
std::string myCoverMimeType;
std::vector<std::pair<std::string,std::string> > myTourTOC; std::vector<std::pair<std::string,std::string> > myTourTOC;
std::vector<std::pair<std::string,std::string> > myGuideTOC; std::vector<std::pair<std::string,std::string> > myGuideTOC;
}; };

View file

@ -218,8 +218,9 @@ void XHTMLTagLinkAction::doAtStart(XHTMLReader &reader, const char **xmlattribut
return; return;
} }
ZLLogger::Instance().println("CSS", "style file: " + reader.myPathPrefix + MiscUtil::decodeHtmlURL(href)); const std::string cssFilePath = reader.myPathPrefix + MiscUtil::decodeHtmlURL(href);
shared_ptr<ZLInputStream> cssStream = ZLFile(reader.myPathPrefix + MiscUtil::decodeHtmlURL(href)).inputStream(); ZLLogger::Instance().println("CSS", "style file: " + cssFilePath);
shared_ptr<ZLInputStream> cssStream = ZLFile(cssFilePath).inputStream();
if (cssStream.isNull()) { if (cssStream.isNull()) {
return; return;
} }
@ -307,8 +308,9 @@ void XHTMLTagImageAction::doAtStart(XHTMLReader &reader, const char **xmlattribu
endParagraph(reader); endParagraph(reader);
} }
const std::string imageName = imageFile.name(false); 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)); bookReader(reader).addImage(imageName, new ZLFileImage(imageFile, "", 0));
reader.myMarkNextImageAsCover = false;
if (flag) { if (flag) {
beginParagraph(reader); beginParagraph(reader);
} }
@ -516,6 +518,11 @@ void XHTMLReader::fillTagTable() {
} }
XHTMLReader::XHTMLReader(BookReader &modelReader) : myModelReader(modelReader) { XHTMLReader::XHTMLReader(BookReader &modelReader) : myModelReader(modelReader) {
myMarkNextImageAsCover = false;
}
void XHTMLReader::setMarkFirstImageAsCover() {
myMarkNextImageAsCover = true;
} }
bool XHTMLReader::readFile(const ZLFile &file, const std::string &referenceName) { 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<ZLTextStyleEntry> entry = myStyleParser->parseString(style); shared_ptr<ZLTextStyleEntry> entry = myStyleParser->parseString(style);
myModelReader.addStyleEntry(*entry); myModelReader.addStyleEntry(*entry);
myStyleEntryStack.push_back(entry); myStyleEntryStack.push_back(entry);
} else {
} }
myCSSStack.push_back(myStyleEntryStack.size() - sizeBefore); myCSSStack.push_back(myStyleEntryStack.size() - sizeBefore);
} }

View file

@ -65,6 +65,7 @@ public:
bool readFile(const ZLFile &file, const std::string &referenceName); bool readFile(const ZLFile &file, const std::string &referenceName);
const std::string &fileAlias(const std::string &fileName) const; const std::string &fileAlias(const std::string &fileName) const;
const std::string normalizedReference(const std::string &reference) const; const std::string normalizedReference(const std::string &reference) const;
void setMarkFirstImageAsCover();
private: private:
XHTMLTagAction *getAction(const std::string &tag); XHTMLTagAction *getAction(const std::string &tag);
@ -104,6 +105,7 @@ private:
READ_BODY READ_BODY
} myReadState; } myReadState;
int myBodyCounter; int myBodyCounter;
bool myMarkNextImageAsCover;
friend class XHTMLTagAction; friend class XHTMLTagAction;
friend class XHTMLTagStyleAction; friend class XHTMLTagStyleAction;
@ -113,6 +115,7 @@ private:
friend class XHTMLTagParagraphAction; friend class XHTMLTagParagraphAction;
friend class XHTMLTagBodyAction; friend class XHTMLTagBodyAction;
friend class XHTMLTagRestartParagraphAction; friend class XHTMLTagRestartParagraphAction;
friend class XHTMLTagImageAction;
}; };
#endif /* __XHTMLREADER_H__ */ #endif /* __XHTMLREADER_H__ */