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

refactoring: ZLTextStyleEntry has been moved to a separate header

This commit is contained in:
Nikolay Pultsin 2012-05-07 12:30:25 +01:00
parent 7a3c54c00a
commit 2422f080a8
12 changed files with 188 additions and 161 deletions

View file

@ -24,6 +24,7 @@
#include <ZLFileImage.h>
#include <ZLLogger.h>
#include <ZLCachedMemoryAllocator.h>
#include <ZLTextStyleEntry.h>
#include "BookReader.h"
#include "BookModel.h"

View file

@ -33,6 +33,7 @@ class BookModel;
class ZLTextModel;
class ZLInputStream;
class ZLCachedMemoryAllocator;
class ZLTextStyleEntry;
class BookReader {

View file

@ -160,28 +160,26 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::createControl(const AttributeMap &
num = 700;
} else if (bold[0] == "normal") {
num = 400;
} else if ((bold[0].length() == 3) &&
(bold[0][1] == '0') &&
(bold[0][2] == '0') &&
(bold[0][0] >= '1') &&
(bold[0][0] <= '9')) {
num = 100 * (bold[0][0] - '0');
} else if (bold[0] == "bolder") {
// TODO: implement
} else if (bold[0] == "lighter") {
// TODO: implement
} else {
num = ZLStringUtil::stringToInteger(bold[0], -1);
}
if (num != -1) {
entry->setFontModifier(FONT_MODIFIER_BOLD, num >= 600);
entry->setFontModifier(ZLTextStyleEntry::FONT_MODIFIER_BOLD, num >= 600);
}
}
const std::vector<std::string> &italic = values(styles, "font-style");
if (!italic.empty()) {
entry->setFontModifier(FONT_MODIFIER_ITALIC, italic[0] == "italic");
entry->setFontModifier(ZLTextStyleEntry::FONT_MODIFIER_ITALIC, italic[0] == "italic");
}
const std::vector<std::string> &variant = values(styles, "font-variant");
if (!variant.empty()) {
entry->setFontModifier(FONT_MODIFIER_SMALLCAPS, variant[0] == "small-caps");
entry->setFontModifier(ZLTextStyleEntry::FONT_MODIFIER_SMALLCAPS, variant[0] == "small-caps");
}
const std::vector<std::string> &fontFamily = values(styles, "font-family");

View file

@ -27,6 +27,7 @@
#include <shared_ptr.h>
#include <ZLTextParagraph.h>
#include <ZLTextStyleEntry.h>
class StyleSheetTable {

View file

@ -21,6 +21,7 @@
#include <ZLStringUtil.h>
#include <ZLFileImage.h>
#include <ZLTextStyleEntry.h>
#include "RtfBookReader.h"
#include "../../bookmodel/BookModel.h"

View file

@ -105,11 +105,28 @@ std::string ZLStringUtil::doubleToString(double value) {
return buf;
}
double ZLStringUtil::stringToDouble(const std::string &value, double defaultValue) {
if (!value.empty()) {
double ZLStringUtil::stringToDouble(const std::string &str, double defaultValue) {
if (!str.empty()) {
setlocale(LC_NUMERIC, "C");
return atof(value.c_str());
return atof(str.c_str());
} else {
return defaultValue;
}
}
int ZLStringUtil::stringToInteger(const std::string &str, int defaultValue) {
if (str.empty()) {
return defaultValue;
}
if (!isdigit(str[0]) && (str.length() == 1 || str[0] != '-' || !isdigit(str[1]))) {
return defaultValue;
}
for (size_t i = 1; i < str.length(); ++i) {
if (!isdigit(str[i])) {
return defaultValue;
}
}
return atoi(str.c_str());
}

View file

@ -31,6 +31,7 @@ private:
public:
static bool stringStartsWith(const std::string &str, const std::string &start);
static bool stringEndsWith(const std::string &str, const std::string &end);
static void appendNumber(std::string &str, unsigned int n);
static void append(std::string &str, const std::vector<std::string> &buffer);
static void stripWhiteSpaces(std::string &str);
@ -39,6 +40,7 @@ public:
static std::string doubleToString(double value);
static double stringToDouble(const std::string &value, double defaultValue);
static int stringToInteger(const std::string &str, int defaultValue);
};
#endif /* __ZLSTRINGUTIL_H__ */

View file

@ -1,30 +0,0 @@
/*
* Copyright (C) 2004-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 __ZLTEXTFONTMODIFIER_H__
#define __ZLTEXTFONTMODIFIER_H__
enum ZLTextFontModifier {
FONT_MODIFIER_DEFAULT = 0,
FONT_MODIFIER_BOLD = 1 << 0,
FONT_MODIFIER_ITALIC = 1 << 1,
FONT_MODIFIER_SMALLCAPS = 1 << 2,
};
#endif /* __ZLTEXTFONTMODIFIER_H__ */

View file

@ -32,8 +32,7 @@
//#include <ZLTextMark.h>
#include <ZLCachedMemoryAllocator.h>
class ZLTextParagraph;
class ZLTextTreeParagraph;
class ZLTextStyleEntry;
class ZLTextModel {

View file

@ -26,6 +26,7 @@
#include "ZLCachedMemoryAllocator.h"
#include "ZLTextParagraph.h"
#include "ZLTextStyleEntry.h"
const shared_ptr<ZLTextParagraphEntry> ResetBidiEntry::Instance = new ResetBidiEntry();

View file

@ -29,7 +29,6 @@
#include <ZLHyperlinkType.h>
#include <ZLTextKind.h>
#include <ZLTextAlignmentType.h>
#include <ZLTextFontModifier.h>
class ZLImage;
typedef std::map<std::string,shared_ptr<const ZLImage> > ZLImageMap;
@ -58,85 +57,6 @@ private: // disable copying
const ZLTextParagraphEntry &operator = (const ZLTextParagraphEntry &entry);
};
class ZLTextStyleEntry : public ZLTextParagraphEntry {
public:
enum SizeUnit {
SIZE_UNIT_PIXEL,
SIZE_UNIT_EM_100,
SIZE_UNIT_EX_100,
SIZE_UNIT_PERCENT
};
struct Metrics {
Metrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight);
int FontSize;
int FontXHeight;
int FullWidth;
int FullHeight;
};
enum Length {
LENGTH_LEFT_INDENT = 0,
LENGTH_RIGHT_INDENT = 1,
LENGTH_FIRST_LINE_INDENT_DELTA = 2,
LENGTH_SPACE_BEFORE = 3,
LENGTH_SPACE_AFTER = 4,
NUMBER_OF_LENGTHS = 5,
};
private:
struct LengthType {
SizeUnit Unit;
short Size;
};
public:
ZLTextStyleEntry();
ZLTextStyleEntry(char *address);
~ZLTextStyleEntry();
bool isEmpty() const;
bool isLengthSupported(Length name) const;
short length(Length name, const Metrics &metrics) const;
void setLength(Length name, short length, SizeUnit unit);
bool isAlignmentTypeSupported() const;
ZLTextAlignmentType alignmentType() const;
void setAlignmentType(ZLTextAlignmentType alignmentType);
unsigned char supportedFontModifier() const;
unsigned char fontModifier() const;
void setFontModifier(ZLTextFontModifier style, bool set);
bool isFontSizeMagSupported() const;
signed char fontSizeMag() const;
void setFontSizeMag(signed char fontSizeMag);
bool isFontFamilySupported() const;
const std::string &fontFamily() const;
void setFontFamily(const std::string &fontFamily);
static const unsigned int SUPPORTS_ALIGNMENT_TYPE = 1U << NUMBER_OF_LENGTHS;
static const unsigned int SUPPORTS_FONT_SIZE_MAG = 1U << (NUMBER_OF_LENGTHS + 1);
static const unsigned int SUPPORTS_FONT_FAMILY = 1U << (NUMBER_OF_LENGTHS + 2);
private:
unsigned int myMask;
LengthType myLengths[NUMBER_OF_LENGTHS];
ZLTextAlignmentType myAlignmentType;
unsigned char mySupportedFontModifier;
unsigned char myFontModifier;
signed char myFontSizeMag;
std::string myFontFamily;
friend class ZLTextModel;
};
class ZLTextControlEntry : public ZLTextParagraphEntry {
protected:
@ -330,43 +250,6 @@ private:
inline ZLTextParagraphEntry::ZLTextParagraphEntry() {}
inline ZLTextParagraphEntry::~ZLTextParagraphEntry() {}
inline ZLTextStyleEntry::ZLTextStyleEntry() : myMask(0), myAlignmentType(ALIGN_UNDEFINED), mySupportedFontModifier(0), myFontModifier(0), myFontSizeMag(0) {}
inline ZLTextStyleEntry::~ZLTextStyleEntry() {}
inline ZLTextStyleEntry::Metrics::Metrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight) : FontSize(fontSize), FontXHeight(fontXHeight), FullWidth(fullWidth), FullHeight(fullHeight) {}
inline bool ZLTextStyleEntry::isEmpty() const { return myMask == 0; }
inline bool ZLTextStyleEntry::isLengthSupported(Length name) const { return (myMask & (1U << name)) != 0; }
inline void ZLTextStyleEntry::setLength(Length name, short length, SizeUnit unit) {
myLengths[name].Size = length;
myLengths[name].Unit = unit;
myMask |= 1U << name;
}
inline bool ZLTextStyleEntry::isAlignmentTypeSupported() const { return (myMask & SUPPORTS_ALIGNMENT_TYPE) == SUPPORTS_ALIGNMENT_TYPE; }
inline ZLTextAlignmentType ZLTextStyleEntry::alignmentType() const { return myAlignmentType; }
inline void ZLTextStyleEntry::setAlignmentType(ZLTextAlignmentType alignmentType) { myAlignmentType = alignmentType; myMask |= SUPPORTS_ALIGNMENT_TYPE; }
inline unsigned char ZLTextStyleEntry::supportedFontModifier() const { return mySupportedFontModifier; }
inline unsigned char ZLTextStyleEntry::fontModifier() const { return myFontModifier; }
inline void ZLTextStyleEntry::setFontModifier(ZLTextFontModifier style, bool set) {
if (set) {
myFontModifier |= style;
} else {
myFontModifier &= ~style;
}
mySupportedFontModifier |= style;
}
inline bool ZLTextStyleEntry::isFontSizeMagSupported() const { return (myMask & SUPPORTS_FONT_SIZE_MAG) == SUPPORTS_FONT_SIZE_MAG; }
inline signed char ZLTextStyleEntry::fontSizeMag() const { return myFontSizeMag; }
inline void ZLTextStyleEntry::setFontSizeMag(signed char fontSizeMag) { myFontSizeMag = fontSizeMag; myMask |= SUPPORTS_FONT_SIZE_MAG; }
inline bool ZLTextStyleEntry::isFontFamilySupported() const { return (myMask & SUPPORTS_FONT_FAMILY) == SUPPORTS_FONT_FAMILY; }
inline const std::string &ZLTextStyleEntry::fontFamily() const { return myFontFamily; }
inline void ZLTextStyleEntry::setFontFamily(const std::string &fontFamily) { myFontFamily = fontFamily; myMask |= SUPPORTS_FONT_FAMILY; }
inline ZLTextControlEntry::ZLTextControlEntry(ZLTextKind kind, bool isStart) : myKind(kind), myStart(isStart) {}
inline ZLTextControlEntry::~ZLTextControlEntry() {}
inline ZLTextKind ZLTextControlEntry::kind() const { return myKind; }

View file

@ -0,0 +1,153 @@
/*
* Copyright (C) 2004-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 __ZLTEXTSTYLEENTRY_H__
#define __ZLTEXTSTYLEENTRY_H__
#include <string>
#include <ZLTextParagraph.h>
#include <ZLTextAlignmentType.h>
class ZLTextStyleEntry : public ZLTextParagraphEntry {
public:
enum SizeUnit {
SIZE_UNIT_PIXEL,
SIZE_UNIT_EM_100,
SIZE_UNIT_EX_100,
SIZE_UNIT_PERCENT
};
struct Metrics {
Metrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight);
int FontSize;
int FontXHeight;
int FullWidth;
int FullHeight;
};
enum FontModifier {
FONT_MODIFIER_DEFAULT = 0,
FONT_MODIFIER_BOLD = 1 << 0,
FONT_MODIFIER_ITALIC = 1 << 1,
FONT_MODIFIER_UNDERLINED = 1 << 2,
FONT_MODIFIER_STRIKEDTHROUGH = 1 << 3,
FONT_MODIFIER_SMALLCAPS = 1 << 4,
};
enum Length {
LENGTH_LEFT_INDENT = 0,
LENGTH_RIGHT_INDENT = 1,
LENGTH_FIRST_LINE_INDENT_DELTA = 2,
LENGTH_SPACE_BEFORE = 3,
LENGTH_SPACE_AFTER = 4,
NUMBER_OF_LENGTHS = 5,
};
static const unsigned int SUPPORTS_ALIGNMENT_TYPE = 1U << NUMBER_OF_LENGTHS;
static const unsigned int SUPPORTS_FONT_SIZE_MAG = 1U << (NUMBER_OF_LENGTHS + 1);
static const unsigned int SUPPORTS_FONT_FAMILY = 1U << (NUMBER_OF_LENGTHS + 2);
private:
struct LengthType {
SizeUnit Unit;
short Size;
};
public:
ZLTextStyleEntry();
ZLTextStyleEntry(char *address);
~ZLTextStyleEntry();
bool isEmpty() const;
bool isLengthSupported(Length name) const;
short length(Length name, const Metrics &metrics) const;
void setLength(Length name, short length, SizeUnit unit);
bool isAlignmentTypeSupported() const;
ZLTextAlignmentType alignmentType() const;
void setAlignmentType(ZLTextAlignmentType alignmentType);
unsigned char supportedFontModifier() const;
unsigned char fontModifier() const;
void setFontModifier(FontModifier style, bool set);
bool isFontSizeMagSupported() const;
signed char fontSizeMag() const;
void setFontSizeMag(signed char fontSizeMag);
bool isFontFamilySupported() const;
const std::string &fontFamily() const;
void setFontFamily(const std::string &fontFamily);
private:
unsigned int myMask;
LengthType myLengths[NUMBER_OF_LENGTHS];
ZLTextAlignmentType myAlignmentType;
unsigned char mySupportedFontModifier;
unsigned char myFontModifier;
signed char myFontSizeMag;
std::string myFontFamily;
friend class ZLTextModel;
};
inline ZLTextStyleEntry::ZLTextStyleEntry() : myMask(0), myAlignmentType(ALIGN_UNDEFINED), mySupportedFontModifier(0), myFontModifier(0), myFontSizeMag(0) {}
inline ZLTextStyleEntry::~ZLTextStyleEntry() {}
inline ZLTextStyleEntry::Metrics::Metrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight) : FontSize(fontSize), FontXHeight(fontXHeight), FullWidth(fullWidth), FullHeight(fullHeight) {}
inline bool ZLTextStyleEntry::isEmpty() const { return myMask == 0; }
inline bool ZLTextStyleEntry::isLengthSupported(Length name) const { return (myMask & (1U << name)) != 0; }
inline void ZLTextStyleEntry::setLength(Length name, short length, SizeUnit unit) {
myLengths[name].Size = length;
myLengths[name].Unit = unit;
myMask |= 1U << name;
}
inline bool ZLTextStyleEntry::isAlignmentTypeSupported() const { return (myMask & SUPPORTS_ALIGNMENT_TYPE) == SUPPORTS_ALIGNMENT_TYPE; }
inline ZLTextAlignmentType ZLTextStyleEntry::alignmentType() const { return myAlignmentType; }
inline void ZLTextStyleEntry::setAlignmentType(ZLTextAlignmentType alignmentType) { myAlignmentType = alignmentType; myMask |= SUPPORTS_ALIGNMENT_TYPE; }
inline unsigned char ZLTextStyleEntry::supportedFontModifier() const { return mySupportedFontModifier; }
inline unsigned char ZLTextStyleEntry::fontModifier() const { return myFontModifier; }
inline void ZLTextStyleEntry::setFontModifier(FontModifier style, bool set) {
if (set) {
myFontModifier |= style;
} else {
myFontModifier &= ~style;
}
mySupportedFontModifier |= style;
}
inline bool ZLTextStyleEntry::isFontSizeMagSupported() const { return (myMask & SUPPORTS_FONT_SIZE_MAG) == SUPPORTS_FONT_SIZE_MAG; }
inline signed char ZLTextStyleEntry::fontSizeMag() const { return myFontSizeMag; }
inline void ZLTextStyleEntry::setFontSizeMag(signed char fontSizeMag) { myFontSizeMag = fontSizeMag; myMask |= SUPPORTS_FONT_SIZE_MAG; }
inline bool ZLTextStyleEntry::isFontFamilySupported() const { return (myMask & SUPPORTS_FONT_FAMILY) == SUPPORTS_FONT_FAMILY; }
inline const std::string &ZLTextStyleEntry::fontFamily() const { return myFontFamily; }
inline void ZLTextStyleEntry::setFontFamily(const std::string &fontFamily) { myFontFamily = fontFamily; myMask |= SUPPORTS_FONT_FAMILY; }
#endif /* __ZLTEXTSTYLEENTRY_H__ */