mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
introduced FontEntry & FontMap classes
This commit is contained in:
parent
6868ca2f97
commit
7ff13974d3
9 changed files with 153 additions and 15 deletions
|
@ -100,6 +100,7 @@ LOCAL_SRC_FILES := \
|
||||||
NativeFormats/fbreader/src/formats/fb2/FB2Reader.cpp \
|
NativeFormats/fbreader/src/formats/fb2/FB2Reader.cpp \
|
||||||
NativeFormats/fbreader/src/formats/fb2/FB2TagManager.cpp \
|
NativeFormats/fbreader/src/formats/fb2/FB2TagManager.cpp \
|
||||||
NativeFormats/fbreader/src/formats/fb2/FB2UidReader.cpp \
|
NativeFormats/fbreader/src/formats/fb2/FB2UidReader.cpp \
|
||||||
|
NativeFormats/fbreader/src/formats/css/FontMap.cpp \
|
||||||
NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp \
|
NativeFormats/fbreader/src/formats/css/StyleSheetParser.cpp \
|
||||||
NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp \
|
NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp \
|
||||||
NativeFormats/fbreader/src/formats/html/HtmlBookReader.cpp \
|
NativeFormats/fbreader/src/formats/html/HtmlBookReader.cpp \
|
||||||
|
|
70
jni/NativeFormats/fbreader/src/formats/css/FontMap.cpp
Normal file
70
jni/NativeFormats/fbreader/src/formats/css/FontMap.cpp
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2004-2014 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 <ZLLogger.h>
|
||||||
|
#include <ZLStringUtil.h>
|
||||||
|
#include <ZLFile.h>
|
||||||
|
|
||||||
|
#include "FontMap.h"
|
||||||
|
|
||||||
|
void FontEntry::addFile(const std::string &weight, const std::string &style, const std::string &filePath) {
|
||||||
|
if (weight == "bold") {
|
||||||
|
if (style == "italic") {
|
||||||
|
BoldItalic = new std::string(filePath);
|
||||||
|
} else {
|
||||||
|
Bold = new std::string(filePath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (style == "italic") {
|
||||||
|
Italic = new std::string(filePath);
|
||||||
|
} else {
|
||||||
|
Normal = new std::string(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool compareStringPtrs(shared_ptr<std::string> str0, shared_ptr<std::string> str1) {
|
||||||
|
return str0.isNull() ? str1.isNull() : (!str1.isNull() && *str0 == *str1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontEntry::operator == (const FontEntry &other) const {
|
||||||
|
return
|
||||||
|
compareStringPtrs(Normal, other.Normal) &&
|
||||||
|
compareStringPtrs(Bold, other.Bold) &&
|
||||||
|
compareStringPtrs(Italic, other.Italic) &&
|
||||||
|
compareStringPtrs(BoldItalic, other.BoldItalic);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontEntry::operator != (const FontEntry &other) const {
|
||||||
|
return !operator ==(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontMap::operator == (const FontMap &other) const {
|
||||||
|
return myMap == other.myMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontMap::operator != (const FontMap &other) const {
|
||||||
|
return !operator ==(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontMap::appendFontFace(const std::string &family, const std::string &weight, const std::string &style, const std::string &path) {
|
||||||
|
const ZLFile fontFile(path);
|
||||||
|
myMap[family].addFile(weight, style, fontFile.path());
|
||||||
|
ZLLogger::Instance().println("FONT", family + " => " + fontFile.path());
|
||||||
|
}
|
55
jni/NativeFormats/fbreader/src/formats/css/FontMap.h
Normal file
55
jni/NativeFormats/fbreader/src/formats/css/FontMap.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2004-2014 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 __FONTMAP_H__
|
||||||
|
#define __FONTMAP_H__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include <shared_ptr.h>
|
||||||
|
|
||||||
|
class FontEntry {
|
||||||
|
|
||||||
|
public:
|
||||||
|
void addFile(const std::string &weight, const std::string &style, const std::string &filePath);
|
||||||
|
|
||||||
|
bool operator == (const FontEntry &other) const;
|
||||||
|
bool operator != (const FontEntry &other) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
shared_ptr<std::string> Normal;
|
||||||
|
shared_ptr<std::string> Bold;
|
||||||
|
shared_ptr<std::string> Italic;
|
||||||
|
shared_ptr<std::string> BoldItalic;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FontMap {
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool operator == (const FontMap &other) const;
|
||||||
|
bool operator != (const FontMap &other) const;
|
||||||
|
|
||||||
|
void appendFontFace(const std::string &family, const std::string &weight, const std::string &style, const std::string &path);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<std::string,FontEntry> myMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* __FONTMAP_H__ */
|
|
@ -237,7 +237,7 @@ shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char
|
||||||
return control;
|
return control;
|
||||||
}
|
}
|
||||||
|
|
||||||
StyleSheetMultiStyleParser::StyleSheetMultiStyleParser(const std::string &pathPrefix) : StyleSheetParser(pathPrefix) {
|
StyleSheetMultiStyleParser::StyleSheetMultiStyleParser(const std::string &pathPrefix, FontMap &fontMap) : StyleSheetParser(pathPrefix), myFontMap(fontMap) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyleSheetMultiStyleParser::storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map) {
|
void StyleSheetMultiStyleParser::storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map) {
|
||||||
|
@ -299,10 +299,12 @@ void StyleSheetMultiStyleParser::processAtRule(const std::string &name, const St
|
||||||
ZLLogger::Instance().println("FONT", "Source not specified for " + family);
|
ZLLogger::Instance().println("FONT", "Source not specified for " + family);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const ZLFile fontFile(path);
|
myFontMap.appendFontFace(
|
||||||
const bool bold = firstValue(attributes, "font-weight") == "bold";
|
family,
|
||||||
const bool italic = firstValue(attributes, "font-style") == "italic";
|
firstValue(attributes, "font-weight"),
|
||||||
ZLLogger::Instance().println("FONT", family + " => " + fontFile.path());
|
firstValue(attributes, "font-style"),
|
||||||
|
path
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,14 +323,14 @@ void StyleSheetMultiStyleParser::parseStream(ZLInputStream &stream) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StyleSheetTableParser::StyleSheetTableParser(const std::string &pathPrefix, StyleSheetTable &table) : StyleSheetMultiStyleParser(pathPrefix), myTable(table) {
|
StyleSheetTableParser::StyleSheetTableParser(const std::string &pathPrefix, StyleSheetTable &styleTable, FontMap &fontMap) : StyleSheetMultiStyleParser(pathPrefix, fontMap), myStyleTable(styleTable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyleSheetTableParser::store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) {
|
void StyleSheetTableParser::store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map) {
|
||||||
myTable.addMap(tag, aClass, map);
|
myStyleTable.addMap(tag, aClass, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
StyleSheetParserWithCache::StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, shared_ptr<EncryptionMap> encryptionMap) : StyleSheetMultiStyleParser(pathPrefix), myEncryptionMap(encryptionMap) {
|
StyleSheetParserWithCache::StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, FontMap &fontMap, shared_ptr<EncryptionMap> encryptionMap) : StyleSheetMultiStyleParser(pathPrefix, fontMap), myEncryptionMap(encryptionMap) {
|
||||||
myProcessedFiles.insert(file.path());
|
myProcessedFiles.insert(file.path());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,7 +349,7 @@ void StyleSheetParserWithCache::importCSS(const std::string &path) {
|
||||||
ZLLogger::Instance().println("CSS-IMPORT", "Go to process imported file " + fileToImport.path());
|
ZLLogger::Instance().println("CSS-IMPORT", "Go to process imported file " + fileToImport.path());
|
||||||
shared_ptr<ZLInputStream> stream = fileToImport.inputStream(myEncryptionMap);
|
shared_ptr<ZLInputStream> stream = fileToImport.inputStream(myEncryptionMap);
|
||||||
if (!stream.isNull()) {
|
if (!stream.isNull()) {
|
||||||
StyleSheetParserWithCache importParser(fileToImport, myPathPrefix, myEncryptionMap);
|
StyleSheetParserWithCache importParser(fileToImport, myPathPrefix, myFontMap, myEncryptionMap);
|
||||||
importParser.myProcessedFiles.insert(myProcessedFiles.begin(), myProcessedFiles.end());
|
importParser.myProcessedFiles.insert(myProcessedFiles.begin(), myProcessedFiles.end());
|
||||||
importParser.parseStream(*stream);
|
importParser.parseStream(*stream);
|
||||||
myEntries.insert(myEntries.end(), importParser.myEntries.begin(), importParser.myEntries.end());
|
myEntries.insert(myEntries.end(), importParser.myEntries.begin(), importParser.myEntries.end());
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <FileEncryptionInfo.h>
|
#include <FileEncryptionInfo.h>
|
||||||
|
|
||||||
#include "StyleSheetTable.h"
|
#include "StyleSheetTable.h"
|
||||||
|
#include "FontMap.h"
|
||||||
|
|
||||||
class ZLFile;
|
class ZLFile;
|
||||||
class ZLInputStream;
|
class ZLInputStream;
|
||||||
|
@ -85,7 +86,7 @@ public:
|
||||||
class StyleSheetMultiStyleParser : public StyleSheetParser {
|
class StyleSheetMultiStyleParser : public StyleSheetParser {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
StyleSheetMultiStyleParser(const std::string &pathPrefix);
|
StyleSheetMultiStyleParser(const std::string &pathPrefix, FontMap &map);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void parseStream(ZLInputStream &stream);
|
void parseStream(ZLInputStream &stream);
|
||||||
|
@ -96,18 +97,21 @@ protected:
|
||||||
private:
|
private:
|
||||||
void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map);
|
void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map);
|
||||||
void processAtRule(const std::string &name, const StyleSheetTable::AttributeMap &map);
|
void processAtRule(const std::string &name, const StyleSheetTable::AttributeMap &map);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
FontMap &myFontMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StyleSheetTableParser : public StyleSheetMultiStyleParser {
|
class StyleSheetTableParser : public StyleSheetMultiStyleParser {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StyleSheetTableParser(const std::string &pathPrexix, StyleSheetTable &table);
|
StyleSheetTableParser(const std::string &pathPrexix, StyleSheetTable &styleTable, FontMap &fontMap);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map);
|
void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StyleSheetTable &myTable;
|
StyleSheetTable &myStyleTable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StyleSheetParserWithCache : public StyleSheetMultiStyleParser {
|
class StyleSheetParserWithCache : public StyleSheetMultiStyleParser {
|
||||||
|
@ -122,7 +126,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, shared_ptr<EncryptionMap> encryptionMap);
|
StyleSheetParserWithCache(const ZLFile &file, const std::string &pathPrefix, FontMap &fontMap, shared_ptr<EncryptionMap> encryptionMap);
|
||||||
void applyToTable(StyleSheetTable &table) const;
|
void applyToTable(StyleSheetTable &table) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -274,7 +274,7 @@ HtmlStyleTagAction::HtmlStyleTagAction(HtmlBookReader &reader) : HtmlTagAction(r
|
||||||
}
|
}
|
||||||
|
|
||||||
void HtmlStyleTagAction::run(const HtmlReader::HtmlTag &tag) {
|
void HtmlStyleTagAction::run(const HtmlReader::HtmlTag &tag) {
|
||||||
myReader.myStyleSheetParser = tag.Start ? new StyleSheetTableParser(myReader.myBaseDirPath, myReader.myStyleSheetTable) : 0;
|
myReader.myStyleSheetParser = tag.Start ? new StyleSheetTableParser(myReader.myBaseDirPath, myReader.myStyleSheetTable, myReader.myFontMap) : 0;
|
||||||
/*
|
/*
|
||||||
if (!tag.Start) {
|
if (!tag.Start) {
|
||||||
myReader.myStyleSheetTable.dump();
|
myReader.myStyleSheetTable.dump();
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "HtmlReader.h"
|
#include "HtmlReader.h"
|
||||||
#include "../../bookmodel/BookReader.h"
|
#include "../../bookmodel/BookReader.h"
|
||||||
#include "../css/StyleSheetTable.h"
|
#include "../css/StyleSheetTable.h"
|
||||||
|
#include "../css/FontMap.h"
|
||||||
|
|
||||||
class BookModel;
|
class BookModel;
|
||||||
class PlainTextFormat;
|
class PlainTextFormat;
|
||||||
|
@ -74,6 +75,7 @@ private:
|
||||||
|
|
||||||
StyleSheetTable myStyleSheetTable;
|
StyleSheetTable myStyleSheetTable;
|
||||||
shared_ptr<StyleSheetParser> myStyleSheetParser;
|
shared_ptr<StyleSheetParser> myStyleSheetParser;
|
||||||
|
FontMap myFontMap;
|
||||||
|
|
||||||
int mySpaceCounter;
|
int mySpaceCounter;
|
||||||
int myBreakCounter;
|
int myBreakCounter;
|
||||||
|
|
|
@ -229,7 +229,7 @@ void XHTMLTagStyleAction::doAtStart(XHTMLReader &reader, const char **xmlattribu
|
||||||
|
|
||||||
if (reader.myReadState == XHTML_READ_NOTHING) {
|
if (reader.myReadState == XHTML_READ_NOTHING) {
|
||||||
reader.myReadState = XHTML_READ_STYLE;
|
reader.myReadState = XHTML_READ_STYLE;
|
||||||
reader.myTableParser = new StyleSheetTableParser(reader.myPathPrefix, reader.myStyleSheetTable);
|
reader.myTableParser = new StyleSheetTableParser(reader.myPathPrefix, reader.myStyleSheetTable, *reader.myFontMap);
|
||||||
ZLLogger::Instance().println("CSS", "parsing style tag content");
|
ZLLogger::Instance().println("CSS", "parsing style tag content");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,6 +269,7 @@ void XHTMLTagLinkAction::doAtStart(XHTMLReader &reader, const char **xmlattribut
|
||||||
parser = new StyleSheetParserWithCache(
|
parser = new StyleSheetParserWithCache(
|
||||||
cssFile,
|
cssFile,
|
||||||
MiscUtil::htmlDirectoryPrefix(cssFilePath),
|
MiscUtil::htmlDirectoryPrefix(cssFilePath),
|
||||||
|
*reader.myFontMap,
|
||||||
reader.myEncryptionMap
|
reader.myEncryptionMap
|
||||||
);
|
);
|
||||||
reader.myFileParsers[cssFilePath] = parser;
|
reader.myFileParsers[cssFilePath] = parser;
|
||||||
|
@ -639,6 +640,7 @@ bool XHTMLReader::readFile(const ZLFile &file, const std::string &referenceName)
|
||||||
myCurrentParagraphIsEmpty = true;
|
myCurrentParagraphIsEmpty = true;
|
||||||
|
|
||||||
myStyleSheetTable.clear();
|
myStyleSheetTable.clear();
|
||||||
|
myFontMap = new FontMap();
|
||||||
myCSSStack.clear();
|
myCSSStack.clear();
|
||||||
myStyleEntryStack.clear();
|
myStyleEntryStack.clear();
|
||||||
myStylesToRemove = 0;
|
myStylesToRemove = 0;
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include <ZLVideoEntry.h>
|
#include <ZLVideoEntry.h>
|
||||||
|
|
||||||
#include "../css/StyleSheetTable.h"
|
#include "../css/StyleSheetTable.h"
|
||||||
|
#include "../css/FontMap.h"
|
||||||
#include "../css/StyleSheetParser.h"
|
#include "../css/StyleSheetParser.h"
|
||||||
|
|
||||||
class ZLFile;
|
class ZLFile;
|
||||||
|
@ -105,6 +106,7 @@ private:
|
||||||
bool myPreformatted;
|
bool myPreformatted;
|
||||||
bool myNewParagraphInProgress;
|
bool myNewParagraphInProgress;
|
||||||
StyleSheetTable myStyleSheetTable;
|
StyleSheetTable myStyleSheetTable;
|
||||||
|
shared_ptr<FontMap> myFontMap;
|
||||||
std::vector<int> myCSSStack;
|
std::vector<int> myCSSStack;
|
||||||
std::vector<shared_ptr<ZLTextStyleEntry> > myStyleEntryStack;
|
std::vector<shared_ptr<ZLTextStyleEntry> > myStyleEntryStack;
|
||||||
int myStylesToRemove;
|
int myStylesToRemove;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue