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

fixed style _attribute_ support

vertical-align CSS property support (only lengths, "sub" and "super" values)
This commit is contained in:
Nikolay Pultsin 2014-10-13 20:04:18 +02:00
parent 15bf3fbd21
commit 7a57572056
9 changed files with 126 additions and 53 deletions

View file

@ -7,6 +7,10 @@
* (planned) Editable notes (by Tamotsu Takahashi)
* (planned) Fixed authors list/tags list editing
===== 2.2 beta 1 (Oct ??, 2014) =====
* Fixed "style" attribute support in ePubs
* Added "vertical-align" CSS property support (lengths, "sub" and "super" values)
===== 2.2 beta 0 (Oct 12, 2014) =====
* Added combined CSS selectors support (http://www.w3.org/TR/css3-selectors/#combinators)

View file

@ -20,7 +20,6 @@
#include <cstdlib>
#include <ZLStringUtil.h>
#include <ZLLogger.h>
#include "StyleSheetTable.h"
#include "StyleSheetUtil.h"
@ -84,15 +83,21 @@ static bool parseLength(const std::string &toParse, short &size, ZLTextStyleEntr
return false;
}
void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature featureId, const AttributeMap &map, const std::string &attributeName) {
StyleSheetTable::AttributeMap::const_iterator it = map.find(attributeName);
if (it == map.end()) {
return;
}
static bool trySetLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature featureId, const std::string &value) {
short size;
ZLTextStyleEntry::SizeUnit unit;
if (parseLength(it->second, size, unit)) {
if (::parseLength(value, size, unit)) {
entry.setLength(featureId, size, unit);
return true;
}
return false;
}
void StyleSheetTable::setLength(ZLTextStyleEntry &entry, ZLTextStyleEntry::Feature featureId, const AttributeMap &map, const std::string &attributeName) {
StyleSheetTable::AttributeMap::const_iterator it = map.find(attributeName);
if (it != map.end()) {
::trySetLength(entry, featureId, it->second);
return;
}
}
@ -140,17 +145,6 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::control(const std::string &tag, co
return it != myControlMap.end() ? it->second : 0;
}
static std::string STR0(const CSSSelector& selector) {
return selector.Tag + "." + selector.Class;
}
static std::string STR1(const CSSSelector& selector) {
if (selector.Next.isNull()) {
return STR0(selector);
}
return STR0(selector) + " " + ZLStringUtil::numberToString(selector.Next->Delimiter) + " " + STR1(*(selector.Next->Selector));
}
std::vector<std::pair<CSSSelector,shared_ptr<ZLTextStyleEntry> > > StyleSheetTable::allControls(const std::string &tag, const std::string &aClass) const {
const CSSSelector key(tag, aClass);
std::vector<std::pair<CSSSelector,shared_ptr<ZLTextStyleEntry> > > pairs;
@ -158,7 +152,6 @@ std::vector<std::pair<CSSSelector,shared_ptr<ZLTextStyleEntry> > > StyleSheetTab
std::map<CSSSelector,shared_ptr<ZLTextStyleEntry> >::const_iterator it =
myControlMap.lower_bound(key);
for (std::map<CSSSelector,shared_ptr<ZLTextStyleEntry> >::const_iterator jt = it; jt != myControlMap.end() && key.weakEquals(jt->first); ++jt) {
ZLLogger::Instance().print("CSS-SELECTOR", STR1(key) + " => " + STR1(jt->first));
pairs.push_back(*jt);
}
return pairs;
@ -261,7 +254,7 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::createOrUpdateControl(const Attrib
} else if (fontSize == "larger") {
entry->setFontModifier(ZLTextStyleEntry::FONT_MODIFIER_LARGER, true);
doSetFontSize = false;
} else if (!parseLength(fontSize, size, unit)) {
} else if (!::parseLength(fontSize, size, unit)) {
doSetFontSize = false;
}
if (doSetFontSize) {
@ -269,9 +262,9 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::createOrUpdateControl(const Attrib
}
}
StyleSheetTable::AttributeMap::const_iterator it = styles.find("margin");
if (it != styles.end()) {
std::vector<std::string> split = ZLStringUtil::split(it->second, " ", true);
const std::string margin = value(styles, "margin");
if (!margin.empty()) {
std::vector<std::string> split = ZLStringUtil::split(margin, " ", true);
if (split.size() > 0) {
switch (split.size()) {
case 1:
@ -285,20 +278,10 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::createOrUpdateControl(const Attrib
break;
}
}
short size;
ZLTextStyleEntry::SizeUnit unit;
if (parseLength(split[0], size, unit)) {
entry->setLength(ZLTextStyleEntry::LENGTH_SPACE_BEFORE, size, unit);
}
if (parseLength(split[1], size, unit)) {
entry->setLength(ZLTextStyleEntry::LENGTH_RIGHT_INDENT, size, unit);
}
if (parseLength(split[2], size, unit)) {
entry->setLength(ZLTextStyleEntry::LENGTH_SPACE_AFTER, size, unit);
}
if (parseLength(split[3], size, unit)) {
entry->setLength(ZLTextStyleEntry::LENGTH_LEFT_INDENT, size, unit);
}
::trySetLength(*entry, ZLTextStyleEntry::LENGTH_SPACE_BEFORE, split[0]);
::trySetLength(*entry, ZLTextStyleEntry::LENGTH_RIGHT_INDENT, split[1]);
::trySetLength(*entry, ZLTextStyleEntry::LENGTH_SPACE_AFTER, split[2]);
::trySetLength(*entry, ZLTextStyleEntry::LENGTH_LEFT_INDENT, split[3]);
}
setLength(*entry, ZLTextStyleEntry::LENGTH_LEFT_INDENT, styles, "margin-left");
setLength(*entry, ZLTextStyleEntry::LENGTH_RIGHT_INDENT, styles, "margin-right");
@ -308,6 +291,22 @@ shared_ptr<ZLTextStyleEntry> StyleSheetTable::createOrUpdateControl(const Attrib
setLength(*entry, ZLTextStyleEntry::LENGTH_SPACE_AFTER, styles, "margin-bottom");
setLength(*entry, ZLTextStyleEntry::LENGTH_SPACE_AFTER, styles, "padding-bottom");
const std::string verticalAlign = value(styles, "vertical-align");
if (!verticalAlign.empty()) {
static const char* values[] = { "sub", "super", "top", "text-top", "middle", "bottom", "text-bottom", "initial", "inherit" };
int index = sizeof(values) / sizeof(const char*) - 1;
for (; index >= 0; --index) {
if (verticalAlign == values[index]) {
break;
}
}
if (index >= 0) {
entry->setVerticalAlignCode((unsigned char)index);
} else {
::trySetLength(*entry, ZLTextStyleEntry::LENGTH_VERTICAL_ALIGN, verticalAlign);
}
}
return entry;
}

View file

@ -817,13 +817,13 @@ void XHTMLReader::startElementHandler(const char *tag, const char **attributes)
for (std::vector<std::string>::const_iterator it = classesList.begin(); it != classesList.end(); ++it) {
addTextStyleEntry("", *it);
addTextStyleEntry(sTag, *it);
const char *style = attributeValue(attributes, "style");
if (style != 0) {
//ZLLogger::Instance().println("CSS", std::string("parsing style attribute: ") + style);
shared_ptr<ZLTextStyleEntry> entry = myStyleParser->parseSingleEntry(style);
addTextStyleEntry(*entry);
myTagDataStack.back()->StyleEntries.push_back(entry);
}
}
const char *style = attributeValue(attributes, "style");
if (style != 0) {
//ZLLogger::Instance().println("CSS", std::string("parsing style attribute: ") + style);
shared_ptr<ZLTextStyleEntry> entry = myStyleParser->parseSingleEntry(style);
addTextStyleEntry(*entry);
myTagDataStack.back()->StyleEntries.push_back(entry);
}
}

View file

@ -26,7 +26,7 @@
//#include <ZLLanguageUtil.h>
#include <ZLUnicodeUtil.h>
//#include <ZLStringUtil.h>
#include <ZLLogger.h>
//#include <ZLLogger.h>
#include <FontManager.h>
#include "ZLTextModel.h"
@ -251,7 +251,8 @@ void ZLTextModel::addStyleEntry(const ZLTextStyleEntry &entry, const std::vector
len += 4; // each supported length
}
}
if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE)) {
if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE) ||
entry.isFeatureSupported(ZLTextStyleEntry::NON_LENGTH_VERTICAL_ALIGN)) {
len += 2;
}
if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) {
@ -288,9 +289,10 @@ void ZLTextModel::addStyleEntry(const ZLTextStyleEntry &entry, const std::vector
*address++ = 0;
}
}
if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE)) {
if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE) ||
entry.isFeatureSupported(ZLTextStyleEntry::NON_LENGTH_VERTICAL_ALIGN)) {
*address++ = entry.myAlignmentType;
*address++ = 0;
*address++ = entry.myVerticalAlignCode;
}
if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) {
address = ZLCachedMemoryAllocator::writeUInt16(address, myFontManager.familyListIndex(fontFamilies));

View file

@ -29,6 +29,7 @@ shared_ptr<ZLTextStyleEntry> ZLTextStyleEntry::start() const {
clone->mySupportedFontModifier = mySupportedFontModifier;
clone->myFontModifier = myFontModifier;
clone->myFontFamilies = myFontFamilies;
clone->myVerticalAlignCode = myVerticalAlignCode;
return clone;
}
@ -52,5 +53,6 @@ shared_ptr<ZLTextStyleEntry> ZLTextStyleEntry::inherited() const {
clone->mySupportedFontModifier = mySupportedFontModifier;
clone->myFontModifier = myFontModifier;
clone->myFontFamilies = myFontFamilies;
clone->myVerticalAlignCode = myVerticalAlignCode;
return clone;
}

View file

@ -68,10 +68,12 @@ public:
LENGTH_SPACE_BEFORE = 3,
LENGTH_SPACE_AFTER = 4,
LENGTH_FONT_SIZE = 5,
NUMBER_OF_LENGTHS = 6,
LENGTH_VERTICAL_ALIGN = 6,
NUMBER_OF_LENGTHS = 7,
ALIGNMENT_TYPE = NUMBER_OF_LENGTHS,
FONT_FAMILY = NUMBER_OF_LENGTHS + 1,
FONT_STYLE_MODIFIER = NUMBER_OF_LENGTHS + 2,
NON_LENGTH_VERTICAL_ALIGN = NUMBER_OF_LENGTHS + 3,
};
private:
@ -102,6 +104,9 @@ public:
const std::vector<std::string> &fontFamilies() const;
void setFontFamilies(const std::vector<std::string> &fontFamilies);
unsigned char verticalAlignCode() const;
void setVerticalAlignCode(unsigned char code);
shared_ptr<ZLTextStyleEntry> start() const;
shared_ptr<ZLTextStyleEntry> end() const;
shared_ptr<ZLTextStyleEntry> inherited() const;
@ -115,6 +120,7 @@ private:
unsigned char mySupportedFontModifier;
unsigned char myFontModifier;
std::vector<std::string> myFontFamilies;
unsigned char myVerticalAlignCode;
friend class ZLTextModel;
};
@ -167,4 +173,10 @@ inline void ZLTextStyleEntry::setFontFamilies(const std::vector<std::string> &fo
}
}
inline unsigned char ZLTextStyleEntry::verticalAlignCode() const { return myVerticalAlignCode; }
inline void ZLTextStyleEntry::setVerticalAlignCode(unsigned char code) {
myFeatureMask |= 1 << NON_LENGTH_VERTICAL_ALIGN;
myVerticalAlignCode = code;
}
#endif /* __ZLTEXTSTYLEENTRY_H__ */

View file

@ -218,9 +218,15 @@ public class ZLTextPlainModel implements ZLTextModel, ZLTextStyleEntry.Feature {
entry.setLength(i, size, unit);
}
}
if (ZLTextStyleEntry.isFeatureSupported(mask, ALIGNMENT_TYPE)) {
if (ZLTextStyleEntry.isFeatureSupported(mask, ALIGNMENT_TYPE) ||
ZLTextStyleEntry.isFeatureSupported(mask, NON_LENGTH_VERTICAL_ALIGN)) {
final short value = (short)data[dataOffset++];
entry.setAlignmentType((byte)(value & 0xFF));
if (ZLTextStyleEntry.isFeatureSupported(mask, ALIGNMENT_TYPE)) {
entry.setAlignmentType((byte)(value & 0xFF));
}
if (ZLTextStyleEntry.isFeatureSupported(mask, NON_LENGTH_VERTICAL_ALIGN)) {
entry.setVerticalAlignCode((byte)((value >> 8) & 0xFF));
}
}
if (ZLTextStyleEntry.isFeatureSupported(mask, FONT_FAMILY)) {
entry.setFontFamilies(myFontManager, (short)data[dataOffset++]);

View file

@ -34,10 +34,12 @@ public abstract class ZLTextStyleEntry {
int LENGTH_SPACE_BEFORE = 3;
int LENGTH_SPACE_AFTER = 4;
int LENGTH_FONT_SIZE = 5;
int NUMBER_OF_LENGTHS = 6;
int LENGTH_VERTICAL_ALIGN = 6;
int NUMBER_OF_LENGTHS = 7;
int ALIGNMENT_TYPE = NUMBER_OF_LENGTHS;
int FONT_FAMILY = NUMBER_OF_LENGTHS + 1;
int FONT_STYLE_MODIFIER = NUMBER_OF_LENGTHS + 2;
int NON_LENGTH_VERTICAL_ALIGN = NUMBER_OF_LENGTHS + 3;
}
public interface FontModifier {
@ -83,6 +85,7 @@ public abstract class ZLTextStyleEntry {
private List<FontEntry> myFontEntries;
private byte mySupportedFontModifiers;
private byte myFontModifiers;
private byte myVerticalAlignCode;
static boolean isFeatureSupported(short mask, int featureId) {
return (mask & (1 << featureId)) != 0;
@ -110,6 +113,7 @@ public abstract class ZLTextStyleEntry {
case Feature.LENGTH_SPACE_BEFORE:
case Feature.LENGTH_SPACE_AFTER:
return metrics.FullHeight;
case Feature.LENGTH_VERTICAL_ALIGN:
case Feature.LENGTH_FONT_SIZE:
return fontSize;
}
@ -179,6 +183,15 @@ public abstract class ZLTextStyleEntry {
return (myFontModifiers & modifier) == 0 ? ZLBoolean3.B3_FALSE : ZLBoolean3.B3_TRUE;
}
public final void setVerticalAlignCode(byte code) {
myFeatureMask |= 1 << Feature.NON_LENGTH_VERTICAL_ALIGN;
myVerticalAlignCode = code;
}
public final byte getVerticalAlignCode() {
return myVerticalAlignCode;
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder("StyleEntry[");

View file

@ -176,7 +176,42 @@ public class ZLTextExplicitlyDecoratedStyle extends ZLTextDecoratedStyle impleme
@Override
protected int getVerticalAlignInternal(ZLTextMetrics metrics, int fontSize) {
// TODO: implement
return Parent.getVerticalAlign(metrics);
if (myEntry.isFeatureSupported(LENGTH_VERTICAL_ALIGN)) {
return myEntry.getLength(LENGTH_VERTICAL_ALIGN, metrics, fontSize);
} else if (myEntry.isFeatureSupported(NON_LENGTH_VERTICAL_ALIGN)) {
switch (myEntry.getVerticalAlignCode()) {
default:
return Parent.getVerticalAlign(metrics);
case 0: // sub
return ZLTextStyleEntry.compute(
new ZLTextStyleEntry.Length((short)-50, ZLTextStyleEntry.SizeUnit.EM_100),
metrics, fontSize, LENGTH_VERTICAL_ALIGN
);
case 1: // super
return ZLTextStyleEntry.compute(
new ZLTextStyleEntry.Length((short)50, ZLTextStyleEntry.SizeUnit.EM_100),
metrics, fontSize, LENGTH_VERTICAL_ALIGN
);
/*
case 2: // top
return 0;
case 3: // text-top
return 0;
case 4: // middle
return 0;
case 5: // bottom
return 0;
case 6: // text-bottom
return 0;
case 7: // initial
return 0;
case 8: // inherit
return 0;
*/
}
} else {
return Parent.getVerticalAlign(metrics);
}
}
@Override
protected int getSpaceBeforeInternal(ZLTextMetrics metrics, int fontSize) {