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

disk-based image map has gone

This commit is contained in:
Nikolay Pultsin 2012-03-27 15:53:11 +01:00
parent e65cb4a501
commit 5518e9ef78
19 changed files with 79 additions and 378 deletions

View file

@ -79,7 +79,6 @@ LOCAL_SRC_FILES := \
NativeFormats/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp \ NativeFormats/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp \
NativeFormats/zlibrary/core/src/unix/library/ZLUnixLibrary.cpp \ NativeFormats/zlibrary/core/src/unix/library/ZLUnixLibrary.cpp \
NativeFormats/zlibrary/text/src/model/ZLCachedMemoryAllocator.cpp \ NativeFormats/zlibrary/text/src/model/ZLCachedMemoryAllocator.cpp \
NativeFormats/zlibrary/text/src/model/ZLImageMapWriter.cpp \
NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp \ NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp \
NativeFormats/zlibrary/text/src/model/ZLTextParagraph.cpp \ NativeFormats/zlibrary/text/src/model/ZLTextParagraph.cpp \
NativeFormats/zlibrary/ui/src/android/filesystem/JavaFSDir.cpp \ NativeFormats/zlibrary/ui/src/android/filesystem/JavaFSDir.cpp \

View file

@ -131,23 +131,6 @@ JNIEXPORT void JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin
fillLanguageAndEncoding(env, javaBook, *book); fillLanguageAndEncoding(env, javaBook, *book);
} }
static bool initBookModel(JNIEnv *env, jobject javaModel, BookModel &model) {
shared_ptr<ZLImageMapWriter> imageMapWriter = model.imageMapWriter();
env->PushLocalFrame(16);
jobjectArray ids = AndroidUtil::createStringArray(env, imageMapWriter->identifiers());
jintArray indices = AndroidUtil::createIntArray(env, imageMapWriter->indices());
jintArray offsets = AndroidUtil::createIntArray(env, imageMapWriter->offsets());
jstring imageDirectoryName = env->NewStringUTF(imageMapWriter->allocator().directoryName().c_str());
jstring imageFileExtension = env->NewStringUTF(imageMapWriter->allocator().fileExtension().c_str());
jint imageBlocksNumber = imageMapWriter->allocator().blocksNumber();
env->CallVoidMethod(javaModel, AndroidUtil::MID_NativeBookModel_initImageMap,
ids, indices, offsets, imageDirectoryName, imageFileExtension, imageBlocksNumber);
env->PopLocalFrame(0);
return !env->ExceptionCheck();
}
static bool initInternalHyperlinks(JNIEnv *env, jobject javaModel, BookModel &model) { static bool initInternalHyperlinks(JNIEnv *env, jobject javaModel, BookModel &model) {
ZLCachedMemoryAllocator allocator(131072, Library::Instance().cacheDirectory(), "nlinks"); ZLCachedMemoryAllocator allocator(131072, Library::Instance().cacheDirectory(), "nlinks");
@ -265,14 +248,13 @@ JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPl
jobject javaBook = env->GetObjectField(javaModel, AndroidUtil::FID_NativeBookModel_Book); jobject javaBook = env->GetObjectField(javaModel, AndroidUtil::FID_NativeBookModel_Book);
shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook); shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook);
shared_ptr<BookModel> model = new BookModel(book); shared_ptr<BookModel> model = new BookModel(book, javaModel);
if (!plugin->readModel(*model)) { if (!plugin->readModel(*model)) {
return JNI_FALSE; return JNI_FALSE;
} }
model->flush(); model->flush();
if (!initBookModel(env, javaModel, *model) || if (!initInternalHyperlinks(env, javaModel, *model) ||
!initInternalHyperlinks(env, javaModel, *model) ||
!initTOC(env, javaModel, *model)) { !initTOC(env, javaModel, *model)) {
return JNI_FALSE; return JNI_FALSE;
} }

View file

@ -30,16 +30,17 @@ JNIEXPORT jobjectArray JNICALL Java_org_geometerplus_fbreader_formats_PluginColl
const std::vector<shared_ptr<FormatPlugin> > plugins = PluginCollection::Instance().plugins(); const std::vector<shared_ptr<FormatPlugin> > plugins = PluginCollection::Instance().plugins();
const size_t size = plugins.size(); const size_t size = plugins.size();
jclass cls = env->FindClass(AndroidUtil::Class_NativeFormatPlugin); jclass cls = env->FindClass(AndroidUtil::Class_NativeFormatPlugin);
// TODO: memory leak?
jobjectArray javaPlugins = env->NewObjectArray(size, cls, 0); jobjectArray javaPlugins = env->NewObjectArray(size, cls, 0);
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
jstring fileType = AndroidUtil::createJavaString(env, plugins[i]->supportedFileType()); jstring fileType = AndroidUtil::createJavaString(env, plugins[i]->supportedFileType());
env->SetObjectArrayElement( jobject p = env->NewObject(cls, AndroidUtil::MID_NativeFormatPlugin_init, fileType);
javaPlugins, i, env->SetObjectArrayElement(javaPlugins, i, p);
env->NewObject(cls, AndroidUtil::MID_NativeFormatPlugin_init, fileType) env->DeleteLocalRef(p);
);
env->DeleteLocalRef(fileType); env->DeleteLocalRef(fileType);
} }
env->DeleteLocalRef(cls);
return javaPlugins; return javaPlugins;
} }

View file

@ -27,9 +27,8 @@
#include "../library/Book.h" #include "../library/Book.h"
#include "../library/Library.h" #include "../library/Library.h"
BookModel::BookModel(const shared_ptr<Book> book) : myBook(book) { BookModel::BookModel(const shared_ptr<Book> book, jobject javaModel) : myBook(book), myJavaModel(javaModel) {
const std::string cacheDirectory = Library::Instance().cacheDirectory(); const std::string cacheDirectory = Library::Instance().cacheDirectory();
myImagesWriter = new ZLImageMapWriter(131072, cacheDirectory, "nimages");
myBookTextModel = new ZLTextPlainModel(std::string(), book->language(), 131072, cacheDirectory, "ncache"); myBookTextModel = new ZLTextPlainModel(std::string(), book->language(), 131072, cacheDirectory, "ncache");
myContentsModel = new ContentsModel(book->language(), cacheDirectory, "ncontents"); myContentsModel = new ContentsModel(book->language(), cacheDirectory, "ncontents");
/*shared_ptr<FormatPlugin> plugin = PluginCollection::Instance().plugin(book->file(), false); /*shared_ptr<FormatPlugin> plugin = PluginCollection::Instance().plugin(book->file(), false);
@ -75,7 +74,6 @@ const shared_ptr<Book> BookModel::book() const {
void BookModel::flush() { void BookModel::flush() {
myBookTextModel->flush(); myBookTextModel->flush();
myContentsModel->flush(); myContentsModel->flush();
myImagesWriter->flush();
std::map<std::string,shared_ptr<ZLTextModel> >::const_iterator it = myFootnotes.begin(); std::map<std::string,shared_ptr<ZLTextModel> >::const_iterator it = myFootnotes.begin();
for (; it != myFootnotes.end(); ++it) { for (; it != myFootnotes.end(); ++it) {

View file

@ -20,13 +20,14 @@
#ifndef __BOOKMODEL_H__ #ifndef __BOOKMODEL_H__
#define __BOOKMODEL_H__ #define __BOOKMODEL_H__
#include <jni.h>
#include <map> #include <map>
#include <string> #include <string>
#include <ZLTextModel.h> #include <ZLTextModel.h>
#include <ZLTextParagraph.h> #include <ZLTextParagraph.h>
#include <ZLUserData.h> #include <ZLUserData.h>
#include <ZLImageMapWriter.h>
class ZLImage; class ZLImage;
class Book; class Book;
@ -60,7 +61,7 @@ public:
}; };
public: public:
BookModel(const shared_ptr<Book> book); BookModel(const shared_ptr<Book> book, jobject javaModel);
~BookModel(); ~BookModel();
void setHyperlinkMatcher(shared_ptr<HyperlinkMatcher> matcher); void setHyperlinkMatcher(shared_ptr<HyperlinkMatcher> matcher);
@ -69,8 +70,6 @@ public:
shared_ptr<ZLTextModel> contentsModel() const; shared_ptr<ZLTextModel> contentsModel() const;
const std::map<std::string,shared_ptr<ZLTextModel> > &footnotes() const; const std::map<std::string,shared_ptr<ZLTextModel> > &footnotes() const;
shared_ptr<ZLImageMapWriter> imageMapWriter() const;
Label label(const std::string &id) const; Label label(const std::string &id) const;
const std::map<std::string,Label> &internalHyperlinks() const; const std::map<std::string,Label> &internalHyperlinks() const;
@ -80,9 +79,9 @@ public:
private: private:
const shared_ptr<Book> myBook; const shared_ptr<Book> myBook;
jobject myJavaModel;
shared_ptr<ZLTextModel> myBookTextModel; shared_ptr<ZLTextModel> myBookTextModel;
shared_ptr<ZLTextModel> myContentsModel; shared_ptr<ZLTextModel> myContentsModel;
shared_ptr<ZLImageMapWriter> myImagesWriter;
std::map<std::string,shared_ptr<ZLTextModel> > myFootnotes; std::map<std::string,shared_ptr<ZLTextModel> > myFootnotes;
std::map<std::string,Label> myInternalHyperlinks; std::map<std::string,Label> myInternalHyperlinks;
shared_ptr<HyperlinkMatcher> myHyperlinkMatcher; shared_ptr<HyperlinkMatcher> myHyperlinkMatcher;
@ -93,7 +92,6 @@ friend class BookReader;
inline shared_ptr<ZLTextModel> BookModel::bookTextModel() const { return myBookTextModel; } inline shared_ptr<ZLTextModel> BookModel::bookTextModel() const { return myBookTextModel; }
inline shared_ptr<ZLTextModel> BookModel::contentsModel() const { return myContentsModel; } inline shared_ptr<ZLTextModel> BookModel::contentsModel() const { return myContentsModel; }
inline const std::map<std::string,shared_ptr<ZLTextModel> > &BookModel::footnotes() const { return myFootnotes; } inline const std::map<std::string,shared_ptr<ZLTextModel> > &BookModel::footnotes() const { return myFootnotes; }
inline shared_ptr<ZLImageMapWriter> BookModel::imageMapWriter() const { return myImagesWriter; }
inline const std::map<std::string,BookModel::Label> &BookModel::internalHyperlinks() const { return myInternalHyperlinks; } inline const std::map<std::string,BookModel::Label> &BookModel::internalHyperlinks() const { return myInternalHyperlinks; }
#endif /* __BOOKMODEL_H__ */ #endif /* __BOOKMODEL_H__ */

View file

@ -17,7 +17,10 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
#include <AndroidUtil.h>
#include <ZLImage.h> #include <ZLImage.h>
#include <ZLFileImage.h>
#include <ZLLogger.h> #include <ZLLogger.h>
#include "BookReader.h" #include "BookReader.h"
@ -191,9 +194,33 @@ void BookReader::flushTextBufferToParagraph() {
} }
void BookReader::addImage(const std::string &id, shared_ptr<const ZLImage> image) { void BookReader::addImage(const std::string &id, shared_ptr<const ZLImage> image) {
if (!image.isNull()) { if (image.isNull()) {
myModel.myImagesWriter->addImage(id, *image); return;
} }
const ZLFileImage &fileImage = (const ZLFileImage&)*image;
JNIEnv *env = AndroidUtil::getEnv();
jstring javaMimeType = AndroidUtil::createJavaString(env, fileImage.mimeType());
jobject javaFile = AndroidUtil::createZLFile(env, fileImage.file().path());
jstring javaEncoding = AndroidUtil::createJavaString(env, fileImage.encoding());
jclass cls = env->FindClass(AndroidUtil::Class_ZLFileImage);
jobject javaImage = env->NewObject(
cls, AndroidUtil::MID_ZLFileImage_init,
javaMimeType, javaFile, javaEncoding,
fileImage.offset(), fileImage.size()
);
jstring javaId = AndroidUtil::createJavaString(env, id);
env->CallObjectMethod(myModel.myJavaModel, AndroidUtil::MID_NativeBookModel_addImage, javaId, javaImage);
env->DeleteLocalRef(javaId);
env->DeleteLocalRef(cls);
env->DeleteLocalRef(javaImage);
env->DeleteLocalRef(javaEncoding);
env->DeleteLocalRef(javaFile);
env->DeleteLocalRef(javaMimeType);
} }
void BookReader::insertEndParagraph(ZLTextParagraph::Kind kind) { void BookReader::insertEndParagraph(ZLTextParagraph::Kind kind) {

View file

@ -36,6 +36,7 @@ const char * const AndroidUtil::Class_EncodingConverter = "org/geometerplus/zlib
const char * const AndroidUtil::Class_JavaEncodingCollection = "org/geometerplus/zlibrary/core/encodings/JavaEncodingCollection"; const char * const AndroidUtil::Class_JavaEncodingCollection = "org/geometerplus/zlibrary/core/encodings/JavaEncodingCollection";
const char * const AndroidUtil::Class_Paths = "org/geometerplus/fbreader/Paths"; const char * const AndroidUtil::Class_Paths = "org/geometerplus/fbreader/Paths";
const char * const AndroidUtil::Class_ZLFile = "org/geometerplus/zlibrary/core/filesystem/ZLFile"; const char * const AndroidUtil::Class_ZLFile = "org/geometerplus/zlibrary/core/filesystem/ZLFile";
const char * const AndroidUtil::Class_ZLFileImage = "org/geometerplus/zlibrary/core/image/ZLFileImage";
const char * const AndroidUtil::Class_Book = "org/geometerplus/fbreader/library/Book"; const char * const AndroidUtil::Class_Book = "org/geometerplus/fbreader/library/Book";
const char * const AndroidUtil::Class_Tag = "org/geometerplus/fbreader/library/Tag"; const char * const AndroidUtil::Class_Tag = "org/geometerplus/fbreader/library/Tag";
const char * const AndroidUtil::Class_NativeBookModel = "org/geometerplus/fbreader/bookmodel/NativeBookModel"; const char * const AndroidUtil::Class_NativeBookModel = "org/geometerplus/fbreader/bookmodel/NativeBookModel";
@ -82,6 +83,8 @@ jmethodID AndroidUtil::MID_ZLFile_getPath;
jmethodID AndroidUtil::MID_ZLFile_isDirectory; jmethodID AndroidUtil::MID_ZLFile_isDirectory;
jmethodID AndroidUtil::MID_ZLFile_size; jmethodID AndroidUtil::MID_ZLFile_size;
jmethodID AndroidUtil::MID_ZLFileImage_init;
jmethodID AndroidUtil::SMID_Paths_cacheDirectory; jmethodID AndroidUtil::SMID_Paths_cacheDirectory;
jfieldID AndroidUtil::FID_Book_File; jfieldID AndroidUtil::FID_Book_File;
@ -99,12 +102,12 @@ jmethodID AndroidUtil::MID_Book_save;
jmethodID AndroidUtil::SMID_Tag_getTag; jmethodID AndroidUtil::SMID_Tag_getTag;
jfieldID AndroidUtil::FID_NativeBookModel_Book; jfieldID AndroidUtil::FID_NativeBookModel_Book;
jmethodID AndroidUtil::MID_NativeBookModel_initImageMap;
jmethodID AndroidUtil::MID_NativeBookModel_initInternalHyperlinks; jmethodID AndroidUtil::MID_NativeBookModel_initInternalHyperlinks;
jmethodID AndroidUtil::MID_NativeBookModel_initTOC; jmethodID AndroidUtil::MID_NativeBookModel_initTOC;
jmethodID AndroidUtil::MID_NativeBookModel_createTextModel; jmethodID AndroidUtil::MID_NativeBookModel_createTextModel;
jmethodID AndroidUtil::MID_NativeBookModel_setBookTextModel; jmethodID AndroidUtil::MID_NativeBookModel_setBookTextModel;
jmethodID AndroidUtil::MID_NativeBookModel_setFootnoteModel; jmethodID AndroidUtil::MID_NativeBookModel_setFootnoteModel;
jmethodID AndroidUtil::MID_NativeBookModel_addImage;
jmethodID AndroidUtil::SMID_BookReadingException_throwForFile; jmethodID AndroidUtil::SMID_BookReadingException_throwForFile;
@ -189,6 +192,10 @@ bool AndroidUtil::init(JavaVM* jvm) {
CHECK_NULL( MID_ZLFile_size = env->GetMethodID(cls, "size", "()J") ); CHECK_NULL( MID_ZLFile_size = env->GetMethodID(cls, "size", "()J") );
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_ZLFileImage) );
CHECK_NULL( MID_ZLFileImage_init = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;Ljava/lang/String;II)V") );
env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_Paths) ); CHECK_NULL( cls = env->FindClass(Class_Paths) );
CHECK_NULL( SMID_Paths_cacheDirectory = env->GetStaticMethodID(cls, "cacheDirectory", "()Ljava/lang/String;") ); CHECK_NULL( SMID_Paths_cacheDirectory = env->GetStaticMethodID(cls, "cacheDirectory", "()Ljava/lang/String;") );
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
@ -213,12 +220,12 @@ bool AndroidUtil::init(JavaVM* jvm) {
CHECK_NULL( cls = env->FindClass(Class_NativeBookModel) ); CHECK_NULL( cls = env->FindClass(Class_NativeBookModel) );
CHECK_NULL( FID_NativeBookModel_Book = env->GetFieldID(cls, "Book", "Lorg/geometerplus/fbreader/library/Book;") ); CHECK_NULL( FID_NativeBookModel_Book = env->GetFieldID(cls, "Book", "Lorg/geometerplus/fbreader/library/Book;") );
CHECK_NULL( MID_NativeBookModel_initImageMap = env->GetMethodID(cls, "initImageMap", "([Ljava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;I)V") );
CHECK_NULL( MID_NativeBookModel_initInternalHyperlinks = env->GetMethodID(cls, "initInternalHyperlinks", "(Ljava/lang/String;Ljava/lang/String;I)V") ); CHECK_NULL( MID_NativeBookModel_initInternalHyperlinks = env->GetMethodID(cls, "initInternalHyperlinks", "(Ljava/lang/String;Ljava/lang/String;I)V") );
CHECK_NULL( MID_NativeBookModel_initTOC = env->GetMethodID(cls, "initTOC", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;[I[I)V") ); CHECK_NULL( MID_NativeBookModel_initTOC = env->GetMethodID(cls, "initTOC", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;[I[I)V") );
CHECK_NULL( MID_NativeBookModel_createTextModel = env->GetMethodID(cls, "createTextModel", "(Ljava/lang/String;Ljava/lang/String;I[I[I[I[I[BLjava/lang/String;Ljava/lang/String;I)Lorg/geometerplus/zlibrary/text/model/ZLTextModel;") ); CHECK_NULL( MID_NativeBookModel_createTextModel = env->GetMethodID(cls, "createTextModel", "(Ljava/lang/String;Ljava/lang/String;I[I[I[I[I[BLjava/lang/String;Ljava/lang/String;I)Lorg/geometerplus/zlibrary/text/model/ZLTextModel;") );
CHECK_NULL( MID_NativeBookModel_setBookTextModel = env->GetMethodID(cls, "setBookTextModel", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;)V") ); CHECK_NULL( MID_NativeBookModel_setBookTextModel = env->GetMethodID(cls, "setBookTextModel", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;)V") );
CHECK_NULL( MID_NativeBookModel_setFootnoteModel = env->GetMethodID(cls, "setFootnoteModel", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;)V") ); CHECK_NULL( MID_NativeBookModel_setFootnoteModel = env->GetMethodID(cls, "setFootnoteModel", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;)V") );
CHECK_NULL( MID_NativeBookModel_addImage = env->GetMethodID(cls, "addImage", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/image/ZLImage;)V") );
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
CHECK_NULL( cls = env->FindClass(Class_BookReadingException) ); CHECK_NULL( cls = env->FindClass(Class_BookReadingException) );
@ -291,6 +298,7 @@ jbyteArray AndroidUtil::createByteArray(JNIEnv *env, const std::vector<jbyte> &d
jobjectArray AndroidUtil::createStringArray(JNIEnv *env, const std::vector<std::string> &data) { jobjectArray AndroidUtil::createStringArray(JNIEnv *env, const std::vector<std::string> &data) {
size_t size = data.size(); size_t size = data.size();
jclass cls = env->FindClass("java/lang/String"); jclass cls = env->FindClass("java/lang/String");
// TODO: memory leak?
jobjectArray array = env->NewObjectArray(size, cls, 0); jobjectArray array = env->NewObjectArray(size, cls, 0);
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
const std::string &str = data[i]; const std::string &str = data[i];

View file

@ -40,6 +40,7 @@ public:
static const char * const Class_java_io_PrintStream; static const char * const Class_java_io_PrintStream;
static const char * const Class_ZLibrary; static const char * const Class_ZLibrary;
static const char * const Class_ZLFile; static const char * const Class_ZLFile;
static const char * const Class_ZLFileImage;
static const char * const Class_NativeFormatPlugin; static const char * const Class_NativeFormatPlugin;
static const char * const Class_PluginCollection; static const char * const Class_PluginCollection;
static const char * const Class_Encoding; static const char * const Class_Encoding;
@ -76,6 +77,8 @@ public:
static jmethodID MID_ZLFile_isDirectory; static jmethodID MID_ZLFile_isDirectory;
static jmethodID MID_ZLFile_size; static jmethodID MID_ZLFile_size;
static jmethodID MID_ZLFileImage_init;
static jmethodID MID_NativeFormatPlugin_init; static jmethodID MID_NativeFormatPlugin_init;
static jmethodID MID_NativeFormatPlugin_supportedFileType; static jmethodID MID_NativeFormatPlugin_supportedFileType;
@ -109,12 +112,12 @@ public:
static jmethodID SMID_Tag_getTag; static jmethodID SMID_Tag_getTag;
static jfieldID FID_NativeBookModel_Book; static jfieldID FID_NativeBookModel_Book;
static jmethodID MID_NativeBookModel_initImageMap;
static jmethodID MID_NativeBookModel_initInternalHyperlinks; static jmethodID MID_NativeBookModel_initInternalHyperlinks;
static jmethodID MID_NativeBookModel_initTOC; static jmethodID MID_NativeBookModel_initTOC;
static jmethodID MID_NativeBookModel_createTextModel; static jmethodID MID_NativeBookModel_createTextModel;
static jmethodID MID_NativeBookModel_setBookTextModel; static jmethodID MID_NativeBookModel_setBookTextModel;
static jmethodID MID_NativeBookModel_setFootnoteModel; static jmethodID MID_NativeBookModel_setFootnoteModel;
static jmethodID MID_NativeBookModel_addImage;
static jmethodID SMID_BookReadingException_throwForFile; static jmethodID SMID_BookReadingException_throwForFile;

View file

@ -1,82 +0,0 @@
/*
* Copyright (C) 2011-2012 Geometer Plus <contact@geometerplus.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <ZLFileImage.h>
#include <ZLUnicodeUtil.h>
#include "ZLImageMapWriter.h"
ZLImageMapWriter::ZLImageMapWriter(const size_t rowSize,
const std::string &directoryName, const std::string &fileExtension) :
myAllocator(rowSize, directoryName, fileExtension) {
}
void ZLImageMapWriter::addImage(const std::string &id, const ZLImage &image) {
const size_t dataSize = myAllocator.blocksNumber();
const size_t bytesOffset = myAllocator.currentBytesOffset();
myIds.push_back(id);
myIndices.push_back((dataSize == 0) ? 0 : (dataSize - 1));
myOffsets.push_back(bytesOffset / 2); // offset in words for future use in Java
if (image.isSingle()) {
addSingleImageEntry((const ZLFileImage&)image);
} else {
addMultiImageEntry((const ZLMultiImage&)image);
}
}
void ZLImageMapWriter::addSingleImageEntry(const ZLFileImage &image) {
ZLUnicodeUtil::Ucs2String mime;
ZLUnicodeUtil::utf8ToUcs2(mime, image.mimeType());
ZLUnicodeUtil::Ucs2String path;
ZLUnicodeUtil::utf8ToUcs2(path, image.file().path());
ZLUnicodeUtil::Ucs2String encoding;
ZLUnicodeUtil::utf8ToUcs2(encoding, image.encoding());
const size_t len = 16 + mime.size() * 2 + path.size() * 2 + encoding.size() * 2;
char *ptr = myAllocator.allocate(len);
*ptr++ = 1;//image.kind();
*ptr++ = 0; // multi ? 1 : 0
ptr = ZLCachedMemoryAllocator::writeString(ptr, mime);
ptr = ZLCachedMemoryAllocator::writeString(ptr, path);
ptr = ZLCachedMemoryAllocator::writeString(ptr, encoding);
ptr = ZLCachedMemoryAllocator::writeUInt32(ptr, image.offset());
ptr = ZLCachedMemoryAllocator::writeUInt32(ptr, image.size());
}
void ZLImageMapWriter::addMultiImageEntry(const ZLMultiImage &image) {
const size_t len = 2;
char *address = myAllocator.allocate(len);
char *ptr = address;
*ptr++ = 0; // kind -- N/A for multi images
*ptr++ = 1; // multi ? 1 : 0
// TODO: implement
}
void ZLImageMapWriter::flush() {
myAllocator.flush();
}

View file

@ -1,70 +0,0 @@
/*
* Copyright (C) 2011-2012 Geometer Plus <contact@geometerplus.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifndef __ZLIMAGEMAPWRITER_H__
#define __ZLIMAGEMAPWRITER_H__
#include <jni.h>
#include <ZLUnicodeUtil.h>
#include <ZLTextParagraph.h>
#include <ZLCachedMemoryAllocator.h>
class ZLImage;
class ZLFileImage;
class ZLMultiImage;
class ZLImageMapWriter {
public:
ZLImageMapWriter(const size_t rowSize, const std::string &directoryName, const std::string &fileExtension);
void addImage(const std::string &id, const ZLImage &image);
void flush();
const std::vector<std::string> identifiers() const;
const std::vector<jint> indices() const;
const std::vector<jint> offsets() const;
const ZLCachedMemoryAllocator &allocator() const;
private:
void addSingleImageEntry(const ZLFileImage &image);
void addMultiImageEntry(const ZLMultiImage &image);
private:
ZLCachedMemoryAllocator myAllocator;
std::vector<std::string> myIds;
std::vector<jint> myIndices;
std::vector<jint> myOffsets;
private:
ZLImageMapWriter(const ZLImageMapWriter &);
const ZLImageMapWriter &operator = (const ZLImageMapWriter &);
};
inline const std::vector<std::string> ZLImageMapWriter::identifiers() const { return myIds; }
inline const std::vector<jint> ZLImageMapWriter::indices() const { return myIndices; }
inline const std::vector<jint> ZLImageMapWriter::offsets() const { return myOffsets; }
inline const ZLCachedMemoryAllocator &ZLImageMapWriter::allocator() const { return myAllocator; }
#endif /* __ZLIMAGEMAPWRITER_H__ */

View file

@ -27,6 +27,11 @@
public ** getPath(); public ** getPath();
public long size(); public long size();
} }
-keep class org.geometerplus.zlibrary.core.image.ZLImage
-keep class org.geometerplus.zlibrary.core.image.ZLFileImage
-keepclassmembers class org.geometerplus.zlibrary.core.image.ZLFileImage {
public <init>(...);
}
-keep class org.geometerplus.zlibrary.text.model.ZLTextModel -keep class org.geometerplus.zlibrary.text.model.ZLTextModel
-keep class org.geometerplus.fbreader.formats.PluginCollection -keep class org.geometerplus.fbreader.formats.PluginCollection
-keepclassmembers class org.geometerplus.fbreader.formats.PluginCollection { -keepclassmembers class org.geometerplus.fbreader.formats.PluginCollection {
@ -74,10 +79,12 @@
-keepclassmembers class org.geometerplus.fbreader.library.Tag { -keepclassmembers class org.geometerplus.fbreader.library.Tag {
public static ** getTag(**,**); public static ** getTag(**,**);
} }
-keepclassmembers class org.geometerplus.fbreader.bookmodel.BookModelImpl {
public void addImage(**,**);
}
-keep class org.geometerplus.fbreader.bookmodel.NativeBookModel -keep class org.geometerplus.fbreader.bookmodel.NativeBookModel
-keepclassmembers class org.geometerplus.fbreader.bookmodel.NativeBookModel { -keepclassmembers class org.geometerplus.fbreader.bookmodel.NativeBookModel {
public ** Book; public ** Book;
public void initImageMap(**[],int[],int[],**,**,int);
public void initInternalHyperlinks(**,**,int); public void initInternalHyperlinks(**,**,int);
public void initTOC(**,int[],int[]); public void initTOC(**,int[],int[]);
public ** createTextModel(**,**,int,int[],int[],int[],int[],byte[],**,**,int); public ** createTextModel(**,**,int,int[],int[],int[],int[],byte[],**,**,int);

View file

@ -29,7 +29,7 @@ import org.geometerplus.fbreader.library.Book;
abstract class BookModelImpl extends BookModel { abstract class BookModelImpl extends BookModel {
protected CharStorage myInternalHyperlinks; protected CharStorage myInternalHyperlinks;
protected ZLImageMap myImageMap; protected final ZLImageMap myImageMap = new ZLImageMap();
protected final HashMap<String,ZLTextModel> myFootnotes = new HashMap<String,ZLTextModel>(); protected final HashMap<String,ZLTextModel> myFootnotes = new HashMap<String,ZLTextModel>();
BookModelImpl(Book book) { BookModelImpl(Book book) {
@ -62,4 +62,9 @@ abstract class BookModelImpl extends BookModel {
} }
return null; return null;
} }
public void addImage(String id, ZLImage image) {
System.err.println(id + ":" + image);
myImageMap.put(id, image);
}
} }

View file

@ -31,7 +31,6 @@ public class JavaBookModel extends BookModelImpl {
JavaBookModel(Book book) { JavaBookModel(Book book) {
super(book); super(book);
myImageMap = new ZLPlainImageMap();
myInternalHyperlinks = new CachedCharStorage(32768, Paths.cacheDirectory(), "links"); myInternalHyperlinks = new CachedCharStorage(32768, Paths.cacheDirectory(), "links");
BookTextModel = new ZLTextWritablePlainModel(null, book.getLanguage(), 1024, 65536, Paths.cacheDirectory(), "cache", myImageMap); BookTextModel = new ZLTextWritablePlainModel(null, book.getLanguage(), 1024, 65536, Paths.cacheDirectory(), "cache", myImageMap);
} }
@ -82,8 +81,4 @@ public class JavaBookModel extends BookModelImpl {
block[offset++] = (char)(paragraphNumber >> 16); block[offset++] = (char)(paragraphNumber >> 16);
myCurrentLinkBlockOffset = offset; myCurrentLinkBlockOffset = offset;
} }
void addImage(String id, ZLImage image) {
((ZLPlainImageMap)myImageMap).put(id, image);
}
} }

View file

@ -32,15 +32,6 @@ public class NativeBookModel extends BookModelImpl {
super(book); super(book);
} }
public void initImageMap(
String[] ids, int[] indices, int[] offsets,
String directoryName, String fileExtension, int blocksNumber
) {
myImageMap = new ZLCachedImageMap(
ids, indices, offsets, directoryName, fileExtension, blocksNumber
);
}
public void initInternalHyperlinks(String directoryName, String fileExtension, int blocksNumber) { public void initInternalHyperlinks(String directoryName, String fileExtension, int blocksNumber) {
myInternalHyperlinks = new CachedCharStorageRO(directoryName, fileExtension, blocksNumber); myInternalHyperlinks = new CachedCharStorageRO(directoryName, fileExtension, blocksNumber);
} }
@ -93,9 +84,6 @@ public class NativeBookModel extends BookModelImpl {
int[] paragraphLenghts, int[] textSizes, byte[] paragraphKinds, int[] paragraphLenghts, int[] textSizes, byte[] paragraphKinds,
String directoryName, String fileExtension, int blocksNumber String directoryName, String fileExtension, int blocksNumber
) { ) {
if (myImageMap == null) {
throw new RuntimeException("NativeBookModel should be initialized with initImageMap method");
}
return new ZLTextNativeModel( return new ZLTextNativeModel(
id, language, paragraphsNumber, id, language, paragraphsNumber,
entryIndices, entryOffsets, entryIndices, entryOffsets,

View file

@ -52,6 +52,10 @@ public class ZLFileImage extends ZLSingleImage {
private final int myOffset; private final int myOffset;
private final int myLength; private final int myLength;
public ZLFileImage(String mimeType, ZLFile file, String encoding, int offset, int length) {
this(MimeType.get(mimeType), file, encoding, offset, length);
}
public ZLFileImage(MimeType mimeType, ZLFile file, String encoding, int offset, int length) { public ZLFileImage(MimeType mimeType, ZLFile file, String encoding, int offset, int length) {
super(mimeType); super(mimeType);
myFile = file; myFile = file;

View file

@ -19,6 +19,10 @@
package org.geometerplus.zlibrary.core.image; package org.geometerplus.zlibrary.core.image;
public interface ZLImageMap { import java.util.HashMap;
ZLImage getImage(String id);
public final class ZLImageMap extends HashMap<String,ZLImage> {
public ZLImage getImage(String id) {
return (ZLImage)super.get(id);
}
} }

View file

@ -1,30 +0,0 @@
/*
* Copyright (C) 2007-2012 Geometer Plus <contact@geometerplus.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.zlibrary.core.image;
import java.util.HashMap;
public final class ZLPlainImageMap extends HashMap<String,ZLImage> implements ZLImageMap {
private static final long serialVersionUID = -4488377408233803199L;
public ZLImage getImage(String id) {
return (ZLImage)super.get(id);
}
}

View file

@ -1,61 +0,0 @@
/*
* Copyright (C) 2011-2012 Geometer Plus <contact@geometerplus.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.zlibrary.text.model;
import java.util.HashMap;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLImageMap;
public class ZLCachedImageMap implements ZLImageMap {
private final HashMap<String, ZLImage> myImagesMap = new HashMap<String, ZLImage>();
private final HashMap<String, Integer> myIdsMap = new HashMap<String, Integer>();
private final int[] myIndices;
private final int[] myOffsets;
private final ZLImageMapReader myReader;
public ZLCachedImageMap(
String[] ids, int[] indices, int[] offsets,
String directoryName, String fileExtension, int blocksNumber
) {
myIndices = indices;
myOffsets = offsets;
for (int i = 0; i < ids.length; ++i) {
final Integer before = myIdsMap.put(ids[i], i);
if (before != null) {
System.err.println("FBREADER: more than one image with id=\"" + ids[i] + "\" -- number " + before + " and number " + i);
}
}
myReader = new ZLImageMapReader(directoryName, fileExtension, blocksNumber);
}
public ZLImage getImage(String id) {
ZLImage image = myImagesMap.get(id);
if (image == null) {
final Integer pos = myIdsMap.get(id);
if (pos != null) {
image = myReader.readImage(myIndices[pos], myOffsets[pos]);
myImagesMap.put(id, image);
}
}
return image;
}
}

View file

@ -1,75 +0,0 @@
/*
* Copyright (C) 2011-2012 Geometer Plus <contact@geometerplus.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.zlibrary.text.model;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLFileImage;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.util.MimeType;
class ZLImageMapReader {
private final CachedCharStorageRO myStorage;
public ZLImageMapReader(String directoryName, String fileExtension, int blocksNumber) {
myStorage = new CachedCharStorageRO(directoryName, fileExtension, blocksNumber);
}
public ZLImage readImage(int index, int offset) {
char[] data = myStorage.block(index);
while (offset == data.length || data[offset] == '\000') {
data = myStorage.block(++index);
offset = 0;
}
final boolean multi = ((byte)(data[offset] >> 8)) != 0;
if (multi) {
return readMultiImage(index, offset + 1, data);
} else {
return readSingleImage(index, offset + 1, data);
}
}
private ZLImage readMultiImage(int index, int offset, char[] data) {
// TODO: implement
return null;
}
private ZLImage readSingleImage(int index, int offset, char[] data) {
short len = (short)data[offset++];
final String mime = new String(data, offset, len);
offset += len;
len = (short)data[offset++];
final String path = new String(data, offset, len);
offset += len;
len = (short)data[offset++];
final String encoding = new String(data, offset, len);
offset += len;
final int fileOffset = (int)data[offset] + (((int)data[offset + 1]) << 16);
offset += 2;
final int fileSize = (int)data[offset] + (((int)data[offset + 1]) << 16);
offset += 2;
return new ZLFileImage(
MimeType.get(mime), ZLFile.createFileByPath(path), encoding, fileOffset, fileSize
);
}
}