1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 02:09:35 +02:00

introduced FontEntry & FontMap classes

This commit is contained in:
Nikolay Pultsin 2014-03-28 13:16:00 +03:00
parent 6868ca2f97
commit 7ff13974d3
9 changed files with 153 additions and 15 deletions

View file

@ -100,6 +100,7 @@ LOCAL_SRC_FILES := \
NativeFormats/fbreader/src/formats/fb2/FB2Reader.cpp \
NativeFormats/fbreader/src/formats/fb2/FB2TagManager.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/StyleSheetTable.cpp \
NativeFormats/fbreader/src/formats/html/HtmlBookReader.cpp \

View 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());
}

View 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__ */

View file

@ -237,7 +237,7 @@ shared_ptr<ZLTextStyleEntry> StyleSheetSingleStyleParser::parseString(const char
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) {
@ -299,10 +299,12 @@ void StyleSheetMultiStyleParser::processAtRule(const std::string &name, const St
ZLLogger::Instance().println("FONT", "Source not specified for " + family);
return;
}
const ZLFile fontFile(path);
const bool bold = firstValue(attributes, "font-weight") == "bold";
const bool italic = firstValue(attributes, "font-style") == "italic";
ZLLogger::Instance().println("FONT", family + " => " + fontFile.path());
myFontMap.appendFontFace(
family,
firstValue(attributes, "font-weight"),
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) {
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());
}
@ -347,7 +349,7 @@ void StyleSheetParserWithCache::importCSS(const std::string &path) {
ZLLogger::Instance().println("CSS-IMPORT", "Go to process imported file " + fileToImport.path());
shared_ptr<ZLInputStream> stream = fileToImport.inputStream(myEncryptionMap);
if (!stream.isNull()) {
StyleSheetParserWithCache importParser(fileToImport, myPathPrefix, myEncryptionMap);
StyleSheetParserWithCache importParser(fileToImport, myPathPrefix, myFontMap, myEncryptionMap);
importParser.myProcessedFiles.insert(myProcessedFiles.begin(), myProcessedFiles.end());
importParser.parseStream(*stream);
myEntries.insert(myEntries.end(), importParser.myEntries.begin(), importParser.myEntries.end());

View file

@ -27,6 +27,7 @@
#include <FileEncryptionInfo.h>
#include "StyleSheetTable.h"
#include "FontMap.h"
class ZLFile;
class ZLInputStream;
@ -85,7 +86,7 @@ public:
class StyleSheetMultiStyleParser : public StyleSheetParser {
protected:
StyleSheetMultiStyleParser(const std::string &pathPrefix);
StyleSheetMultiStyleParser(const std::string &pathPrefix, FontMap &map);
public:
void parseStream(ZLInputStream &stream);
@ -96,18 +97,21 @@ protected:
private:
void storeData(const std::string &selector, const StyleSheetTable::AttributeMap &map);
void processAtRule(const std::string &name, const StyleSheetTable::AttributeMap &map);
protected:
FontMap &myFontMap;
};
class StyleSheetTableParser : public StyleSheetMultiStyleParser {
public:
StyleSheetTableParser(const std::string &pathPrexix, StyleSheetTable &table);
StyleSheetTableParser(const std::string &pathPrexix, StyleSheetTable &styleTable, FontMap &fontMap);
private:
void store(const std::string &tag, const std::string &aClass, const StyleSheetTable::AttributeMap &map);
private:
StyleSheetTable &myTable;
StyleSheetTable &myStyleTable;
};
class StyleSheetParserWithCache : public StyleSheetMultiStyleParser {
@ -122,7 +126,7 @@ private:
};
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;
private:

View file

@ -274,7 +274,7 @@ HtmlStyleTagAction::HtmlStyleTagAction(HtmlBookReader &reader) : HtmlTagAction(r
}
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) {
myReader.myStyleSheetTable.dump();

View file

@ -27,6 +27,7 @@
#include "HtmlReader.h"
#include "../../bookmodel/BookReader.h"
#include "../css/StyleSheetTable.h"
#include "../css/FontMap.h"
class BookModel;
class PlainTextFormat;
@ -74,6 +75,7 @@ private:
StyleSheetTable myStyleSheetTable;
shared_ptr<StyleSheetParser> myStyleSheetParser;
FontMap myFontMap;
int mySpaceCounter;
int myBreakCounter;

View file

@ -229,7 +229,7 @@ void XHTMLTagStyleAction::doAtStart(XHTMLReader &reader, const char **xmlattribu
if (reader.myReadState == XHTML_READ_NOTHING) {
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");
}
}
@ -269,6 +269,7 @@ void XHTMLTagLinkAction::doAtStart(XHTMLReader &reader, const char **xmlattribut
parser = new StyleSheetParserWithCache(
cssFile,
MiscUtil::htmlDirectoryPrefix(cssFilePath),
*reader.myFontMap,
reader.myEncryptionMap
);
reader.myFileParsers[cssFilePath] = parser;
@ -639,6 +640,7 @@ bool XHTMLReader::readFile(const ZLFile &file, const std::string &referenceName)
myCurrentParagraphIsEmpty = true;
myStyleSheetTable.clear();
myFontMap = new FontMap();
myCSSStack.clear();
myStyleEntryStack.clear();
myStylesToRemove = 0;

View file

@ -28,6 +28,7 @@
#include <ZLVideoEntry.h>
#include "../css/StyleSheetTable.h"
#include "../css/FontMap.h"
#include "../css/StyleSheetParser.h"
class ZLFile;
@ -105,6 +106,7 @@ private:
bool myPreformatted;
bool myNewParagraphInProgress;
StyleSheetTable myStyleSheetTable;
shared_ptr<FontMap> myFontMap;
std::vector<int> myCSSStack;
std::vector<shared_ptr<ZLTextStyleEntry> > myStyleEntryStack;
int myStylesToRemove;