From 43bfe572f79eacb80a492f0f60a72c00dd2d364c Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sun, 30 Mar 2014 20:39:23 +0300 Subject: [PATCH] pass to java font families list index instead of list --- .../fbreader/src/bookmodel/BookModel.cpp | 2 +- .../fbreader/src/bookmodel/BookReader.cpp | 2 +- .../zlibrary/text/src/fonts/FontManager.cpp | 11 +++++++ .../zlibrary/text/src/fonts/FontManager.h | 3 ++ .../zlibrary/text/src/model/ZLTextModel.cpp | 31 +++++++------------ .../zlibrary/text/src/model/ZLTextModel.h | 11 ++++--- .../android/filesystem/JavaInputStream.cpp | 4 +-- .../zlibrary/text/model/ZLTextPlainModel.java | 9 +----- .../zlibrary/text/model/ZLTextStyleEntry.java | 10 +++--- .../style/ZLTextExplicitlyDecoratedStyle.java | 2 +- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/jni/NativeFormats/fbreader/src/bookmodel/BookModel.cpp b/jni/NativeFormats/fbreader/src/bookmodel/BookModel.cpp index 35e9a9b5e..f82d883b3 100644 --- a/jni/NativeFormats/fbreader/src/bookmodel/BookModel.cpp +++ b/jni/NativeFormats/fbreader/src/bookmodel/BookModel.cpp @@ -33,7 +33,7 @@ BookModel::BookModel(const shared_ptr book, jobject javaModel) : myBook(bo myJavaModel = AndroidUtil::getEnv()->NewGlobalRef(javaModel); const std::string cacheDirectory = Library::Instance().cacheDirectory(); - myBookTextModel = new ZLTextPlainModel(std::string(), book->language(), 131072, cacheDirectory, "ncache"); + myBookTextModel = new ZLTextPlainModel(std::string(), book->language(), 131072, cacheDirectory, "ncache", myFontManager); myContentsTree = new ContentsTree(); /*shared_ptr plugin = PluginCollection::Instance().plugin(book->file(), false); if (!plugin.isNull()) { diff --git a/jni/NativeFormats/fbreader/src/bookmodel/BookReader.cpp b/jni/NativeFormats/fbreader/src/bookmodel/BookReader.cpp index a8e448f8f..d009d18db 100644 --- a/jni/NativeFormats/fbreader/src/bookmodel/BookReader.cpp +++ b/jni/NativeFormats/fbreader/src/bookmodel/BookReader.cpp @@ -55,7 +55,7 @@ void BookReader::setFootnoteTextModel(const std::string &id) { if (myFootnotesAllocator.isNull()) { myFootnotesAllocator = new ZLCachedMemoryAllocator(8192, Library::Instance().cacheDirectory(), "footnotes"); } - myCurrentTextModel = new ZLTextPlainModel(id, myModel.myBookTextModel->language(), myFootnotesAllocator); + myCurrentTextModel = new ZLTextPlainModel(id, myModel.myBookTextModel->language(), myFootnotesAllocator, myModel.myFontManager); myModel.myFootnotes.insert(std::make_pair(id, myCurrentTextModel)); } } diff --git a/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.cpp b/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.cpp index b68767c11..0988c235f 100644 --- a/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.cpp +++ b/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.cpp @@ -45,3 +45,14 @@ std::string FontManager::put(const std::string &family, shared_ptr en return std::string(); } + +int FontManager::familyListIndex(const std::vector &familyList) { + std::vector >::const_iterator it = + std::find(myFamilyLists.begin(), myFamilyLists.end(), familyList); + if (it == myFamilyLists.end()) { + myFamilyLists.push_back(familyList); + return myFamilyLists.size() - 1; + } else { + return it - myFamilyLists.begin(); + } +} diff --git a/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.h b/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.h index 9ef32c8e2..41a5b60f7 100644 --- a/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.h +++ b/jni/NativeFormats/zlibrary/text/src/fonts/FontManager.h @@ -22,6 +22,7 @@ #include #include +#include #include @@ -31,9 +32,11 @@ class FontManager { public: std::string put(const std::string &family, shared_ptr entry); + int familyListIndex(const std::vector &familyList); private: std::map > myMap; + std::vector > myFamilyLists; }; #endif /* __FONTMANAGER_H__ */ diff --git a/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp b/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp index b5e58d78f..41c8cd7c2 100644 --- a/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp +++ b/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.cpp @@ -27,6 +27,7 @@ #include //#include #include +#include #include "ZLTextModel.h" #include "ZLTextParagraph.h" @@ -34,18 +35,20 @@ #include "ZLVideoEntry.h" ZLTextModel::ZLTextModel(const std::string &id, const std::string &language, const std::size_t rowSize, - const std::string &directoryName, const std::string &fileExtension) : + const std::string &directoryName, const std::string &fileExtension, FontManager &fManager) : myId(id), myLanguage(language.empty() ? ZLibrary::Language() : language), myAllocator(new ZLCachedMemoryAllocator(rowSize, directoryName, fileExtension)), - myLastEntryStart(0) { + myLastEntryStart(0), + myFontManager(fManager) { } -ZLTextModel::ZLTextModel(const std::string &id, const std::string &language, shared_ptr allocator) : +ZLTextModel::ZLTextModel(const std::string &id, const std::string &language, shared_ptr allocator, FontManager &fManager) : myId(id), myLanguage(language.empty() ? ZLibrary::Language() : language), myAllocator(allocator), - myLastEntryStart(0) { + myLastEntryStart(0), + myFontManager(fManager) { } ZLTextModel::~ZLTextModel() { @@ -135,12 +138,12 @@ void ZLTextModel::addParagraphInternal(ZLTextParagraph *paragraph) { } ZLTextPlainModel::ZLTextPlainModel(const std::string &id, const std::string &language, const std::size_t rowSize, - const std::string &directoryName, const std::string &fileExtension) : - ZLTextModel(id, language, rowSize, directoryName, fileExtension) { + const std::string &directoryName, const std::string &fileExtension, FontManager &fManager) : + ZLTextModel(id, language, rowSize, directoryName, fileExtension, fManager) { } -ZLTextPlainModel::ZLTextPlainModel(const std::string &id, const std::string &language, shared_ptr allocator) : - ZLTextModel(id, language, allocator) { +ZLTextPlainModel::ZLTextPlainModel(const std::string &id, const std::string &language, shared_ptr allocator, FontManager &fManager) : + ZLTextModel(id, language, allocator, fManager) { } void ZLTextPlainModel::createParagraph(ZLTextParagraph::Kind kind) { @@ -251,15 +254,8 @@ void ZLTextModel::addStyleEntry(const ZLTextStyleEntry &entry, const std::vector if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE)) { len += 2; } - std::vector fontFamiliesUcs2; if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) { len += 2; - for (std::vector::const_iterator it = fontFamilies.begin(); it != fontFamilies.end(); ++it) { - ZLUnicodeUtil::Ucs2String ucs2; - ZLUnicodeUtil::utf8ToUcs2(ucs2, *it); - len += 2 + ucs2.size() * 2; - fontFamiliesUcs2.push_back(ucs2); - } } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_STYLE_MODIFIER)) { len += 2; @@ -297,10 +293,7 @@ void ZLTextModel::addStyleEntry(const ZLTextStyleEntry &entry, const std::vector *address++ = 0; } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) { - address = ZLCachedMemoryAllocator::writeUInt16(address, fontFamiliesUcs2.size()); - for (std::vector::const_iterator it = fontFamiliesUcs2.begin(); it != fontFamiliesUcs2.end(); ++it) { - address = ZLCachedMemoryAllocator::writeString(address, *it); - } + address = ZLCachedMemoryAllocator::writeUInt16(address, myFontManager.familyListIndex(fontFamilies)); } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_STYLE_MODIFIER)) { *address++ = entry.mySupportedFontModifier; diff --git a/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.h b/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.h index f3eb05283..467cb54e5 100644 --- a/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.h +++ b/jni/NativeFormats/zlibrary/text/src/model/ZLTextModel.h @@ -34,14 +34,15 @@ class ZLTextStyleEntry; class ZLVideoEntry; +class FontManager; class ZLTextModel { protected: ZLTextModel(const std::string &id, const std::string &language, const std::size_t rowSize, - const std::string &directoryName, const std::string &fileExtension); + const std::string &directoryName, const std::string &fileExtension, FontManager &fManager); ZLTextModel(const std::string &id, const std::string &language, - shared_ptr allocator); + shared_ptr allocator, FontManager &fManager); public: virtual ~ZLTextModel(); @@ -105,6 +106,8 @@ private: std::vector myTextSizes; std::vector myParagraphKinds; + FontManager &myFontManager; + private: ZLTextModel(const ZLTextModel&); const ZLTextModel &operator = (const ZLTextModel&); @@ -114,9 +117,9 @@ class ZLTextPlainModel : public ZLTextModel { public: ZLTextPlainModel(const std::string &id, const std::string &language, const std::size_t rowSize, - const std::string &directoryName, const std::string &fileExtension); + const std::string &directoryName, const std::string &fileExtension, FontManager &fManager); ZLTextPlainModel(const std::string &id, const std::string &language, - shared_ptr allocator); + shared_ptr allocator, FontManager &fManager); void createParagraph(ZLTextParagraph::Kind kind); }; diff --git a/jni/NativeFormats/zlibrary/ui/src/android/filesystem/JavaInputStream.cpp b/jni/NativeFormats/zlibrary/ui/src/android/filesystem/JavaInputStream.cpp index 1db299ab7..e299d4278 100644 --- a/jni/NativeFormats/zlibrary/ui/src/android/filesystem/JavaInputStream.cpp +++ b/jni/NativeFormats/zlibrary/ui/src/android/filesystem/JavaInputStream.cpp @@ -183,11 +183,11 @@ void JavaInputStream::seek(int offset, bool absoluteOffset) { return; } JNIEnv *env = AndroidUtil::getEnv(); - if (myNeedRepositionToStart || offset < myOffset) { + if (myNeedRepositionToStart || offset < (int)myOffset) { rewind(env); myNeedRepositionToStart = false; } - if (offset > myOffset) { + if (offset > (int)myOffset) { skip(env, offset - myOffset); } } diff --git a/src/org/geometerplus/zlibrary/text/model/ZLTextPlainModel.java b/src/org/geometerplus/zlibrary/text/model/ZLTextPlainModel.java index c1d391a1b..3d404d5fb 100644 --- a/src/org/geometerplus/zlibrary/text/model/ZLTextPlainModel.java +++ b/src/org/geometerplus/zlibrary/text/model/ZLTextPlainModel.java @@ -213,14 +213,7 @@ public class ZLTextPlainModel implements ZLTextModel, ZLTextStyleEntry.Feature { entry.setAlignmentType((byte)(value & 0xFF)); } if (ZLTextStyleEntry.isFeatureSupported(mask, FONT_FAMILY)) { - final short listLength = (short)data[dataOffset++]; - final ArrayList families = new ArrayList(listLength); - for (short i = 0; i < listLength; ++i) { - final short familyLength = (short)data[dataOffset++]; - families.add(new String(data, dataOffset, familyLength)); - dataOffset += familyLength; - } - entry.setFontFamilies(families); + entry.setFontFamiliesIndex((short)data[dataOffset++]); } if (ZLTextStyleEntry.isFeatureSupported(mask, FONT_STYLE_MODIFIER)) { final short value = (short)data[dataOffset++]; diff --git a/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java b/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java index ccba3f816..c85e7163f 100644 --- a/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java +++ b/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java @@ -71,7 +71,7 @@ public abstract class ZLTextStyleEntry { private Length[] myLengths = new Length[Feature.NUMBER_OF_LENGTHS]; private byte myAlignmentType; - private List myFontFamilies; + private int myFontFamiliesIndex; private byte mySupportedFontModifiers; private byte myFontModifiers; @@ -134,13 +134,13 @@ public abstract class ZLTextStyleEntry { return myAlignmentType; } - final void setFontFamilies(List fontFamilies) { + final void setFontFamiliesIndex(int fontFamiliesIndex) { myFeatureMask |= 1 << Feature.FONT_FAMILY; - myFontFamilies = new ArrayList(fontFamilies); + myFontFamiliesIndex = fontFamiliesIndex; } - public final List getFontFamilies() { - return myFontFamilies; + public final int getFontFamiliesIndex() { + return myFontFamiliesIndex; } final void setFontModifiers(byte supported, byte values) { diff --git a/src/org/geometerplus/zlibrary/text/view/style/ZLTextExplicitlyDecoratedStyle.java b/src/org/geometerplus/zlibrary/text/view/style/ZLTextExplicitlyDecoratedStyle.java index 455e39fb8..713c2972b 100644 --- a/src/org/geometerplus/zlibrary/text/view/style/ZLTextExplicitlyDecoratedStyle.java +++ b/src/org/geometerplus/zlibrary/text/view/style/ZLTextExplicitlyDecoratedStyle.java @@ -35,7 +35,7 @@ public class ZLTextExplicitlyDecoratedStyle extends ZLTextDecoratedStyle impleme @Override protected String getFontFamilyInternal() { if (myEntry.isFeatureSupported(FONT_FAMILY)) { - System.err.println(myEntry.getFontFamilies()); + System.err.println("FONT FAMILIES INDEX = " + myEntry.getFontFamiliesIndex()); // TODO: implement } return Parent.getFontFamily();