From cb5f661596df2a03a33fe065ac1c9fb5d40fe1ce Mon Sep 17 00:00:00 2001 From: MiroslavR Date: Sat, 27 Sep 2014 13:31:59 +0200 Subject: [PATCH] Book formatter: Support FONT closing tag, minor code cleanup --- apps/openmw/mwgui/formatting.cpp | 33 ++++++++++++++++++++++++++++---- apps/openmw/mwgui/formatting.hpp | 19 +++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/apps/openmw/mwgui/formatting.cpp b/apps/openmw/mwgui/formatting.cpp index d838ae8af..8cca621b8 100644 --- a/apps/openmw/mwgui/formatting.cpp +++ b/apps/openmw/mwgui/formatting.cpp @@ -21,7 +21,7 @@ namespace MWGui { /* BookTextParser */ BookTextParser::BookTextParser(const std::string & text) - : mIndex(0), mText(text), mIgnoreNewlineTags(true), mIgnoreLineEndings(true) + : mIndex(0), mText(text), mIgnoreNewlineTags(true), mIgnoreLineEndings(true), mClosingTag(false) { MWScript::InterpreterContext interpreterContext(NULL, MWWorld::Ptr()); // empty arguments, because there is no locals or actor mText = Interpreter::fixDefinesBook(mText, interpreterContext); @@ -40,7 +40,7 @@ namespace MWGui mTagTypes[tag] = type; } - std::string BookTextParser::getReadyText() + std::string BookTextParser::getReadyText() const { return mReadyText; } @@ -117,12 +117,27 @@ namespace MWGui return mAttributes; } + bool BookTextParser::isClosingTag() const + { + return mClosingTag; + } + void BookTextParser::parseTag(std::string tag) { size_t tagNameEndPos = tag.find(' '); + mAttributes.clear(); mTag = tag.substr(0, tagNameEndPos); Misc::StringUtils::toLower(mTag); - mAttributes.clear(); + if (mTag.empty()) + return; + + mClosingTag = (mTag[0] == '/'); + if (mClosingTag) + { + mTag.erase(mTag.begin()); + return; + } + if (tagNameEndPos == std::string::npos) return; tag.erase(0, tagNameEndPos+1); @@ -232,7 +247,10 @@ namespace MWGui break; } case BookTextParser::Event_FontTag: - handleFont(parser.getAttributes()); + if (parser.isClosingTag()) + resetFontProperties(); + else + handleFont(parser.getAttributes()); break; case BookTextParser::Event_DivTag: handleDiv(parser.getAttributes()); @@ -256,6 +274,13 @@ namespace MWGui return markupToWidget(parent, markup, parent->getWidth(), parent->getHeight()); } + void BookFormatter::resetFontProperties() + { + MyGUI::Align align = mTextStyle.mTextAlign; + mTextStyle = TextStyle(); + mTextStyle.mTextAlign = align; + } + void BookFormatter::handleDiv(const BookTextParser::Attributes & attr) { if (attr.find("align") == attr.end()) diff --git a/apps/openmw/mwgui/formatting.hpp b/apps/openmw/mwgui/formatting.hpp index cf55b36fb..64764a13b 100644 --- a/apps/openmw/mwgui/formatting.hpp +++ b/apps/openmw/mwgui/formatting.hpp @@ -40,15 +40,18 @@ namespace MWGui }; BookTextParser(const std::string & text); - void registerTag(const std::string & tag, Events type); - std::string getReadyText(); Events next(); - void flushBuffer(); + const Attributes & getAttributes() const; - void parseTag(std::string tag); + std::string getReadyText() const; + bool isClosingTag() const; private: + void registerTag(const std::string & tag, Events type); + void flushBuffer(); + void parseTag(std::string tag); + size_t mIndex; std::string mText; std::string mReadyText; @@ -57,6 +60,7 @@ namespace MWGui bool mIgnoreLineEndings; Attributes mAttributes; std::string mTag; + bool mClosingTag; std::map mTagTypes; std::string mBuffer; }; @@ -101,11 +105,12 @@ namespace MWGui Paginator::Pages markupToWidget(MyGUI::Widget * parent, const std::string & markup, const int pageWidth, const int pageHeight); Paginator::Pages markupToWidget(MyGUI::Widget * parent, const std::string & markup); - protected: - void handleImg(const BookTextParser::Attributes & attr); + private: + void resetFontProperties(); + void handleDiv(const BookTextParser::Attributes & attr); void handleFont(const BookTextParser::Attributes & attr); - private: + TextStyle mTextStyle; };