From 762f4e6f9ed737876d181ed4f1830c1b8246888b Mon Sep 17 00:00:00 2001 From: rachytski Date: Mon, 23 May 2011 22:30:10 +0300 Subject: [PATCH] refactored LayoutElement's hierarchy. implemented TextLayoutElement class. --- yg/layout_element.hpp | 22 ------- yg/symbol_layout_element.hpp | 16 +++++ yg/text_layout_element.cpp | 93 ++++++++++++++++++++++++++++++ yg/text_layout_element.hpp | 42 ++++++++++++++ yg/text_on_path_layout_element.hpp | 23 ++++++++ yg/text_renderer.hpp | 15 +++-- yg/yg.pro | 8 ++- 7 files changed, 187 insertions(+), 32 deletions(-) create mode 100644 yg/symbol_layout_element.hpp create mode 100644 yg/text_layout_element.cpp create mode 100644 yg/text_layout_element.hpp create mode 100644 yg/text_on_path_layout_element.hpp diff --git a/yg/layout_element.hpp b/yg/layout_element.hpp index 6259e84ce2..c52520a376 100644 --- a/yg/layout_element.hpp +++ b/yg/layout_element.hpp @@ -44,26 +44,4 @@ namespace yg /// draw layout element virtual void draw(Screen * screen) = 0; }; - - class TextLayoutElement : public LayoutElement - { - public: - - TextLayoutElement(char const * text, double depth, FontDesc const & fontDesc); - - m2::RectD const boundRect() const; - - void draw(Screen * screen); - }; - - class SymbolLayoutElement : public LayoutElement - { - public: - - SymbolLayoutElement(); - - m2::RectD const boundRect() const; - - void draw(Screen * screen); - }; } diff --git a/yg/symbol_layout_element.hpp b/yg/symbol_layout_element.hpp new file mode 100644 index 0000000000..6dea0d41e2 --- /dev/null +++ b/yg/symbol_layout_element.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "layout_element.hpp" + +namespace yg +{ + class SymbolLayoutElement : public LayoutElement + { + public: + SymbolLayoutElement(); + + m2::RectD const boundRect() const; + + void draw(Screen * screen); + }; +} diff --git a/yg/text_layout_element.cpp b/yg/text_layout_element.cpp new file mode 100644 index 0000000000..7fa355c12f --- /dev/null +++ b/yg/text_layout_element.cpp @@ -0,0 +1,93 @@ +#include "../base/SRC_FIRST.hpp" +#include "../coding/strutil.hpp" + +#include "text_layout_element.hpp" +#include "glyph_cache.hpp" +#include "screen.hpp" +#include "resource_manager.hpp" +#include "skin.hpp" + +namespace yg +{ + TextLayoutElement::TextLayoutElement( + char const * text, + double depth, + FontDesc const & fontDesc, + bool log2vis, + shared_ptr const & skin, + shared_ptr const & rm, + m2::PointD const & pivot, + yg::EPosition pos) + : m_text(FromUtf8(text)), + m_depth(depth), + m_fontDesc(fontDesc), + m_skin(skin), + m_rm(rm), + m_log2vis(log2vis), + LayoutElement(0, pivot, pos) + { + for (size_t i = 0; i < m_text.size(); ++i) + { + GlyphKey glyphKey(m_text[i], m_fontDesc.m_size, m_fontDesc.m_isMasked, m_fontDesc.m_color); + + if (m_fontDesc.m_isStatic) + { + uint32_t glyphID = m_skin->mapGlyph(glyphKey, m_fontDesc.m_isStatic); + CharStyle const * p = static_cast(m_skin->fromID(glyphID)); + if (p != 0) + { + if (i == 0) + m_limitRect = m2::RectD(p->m_xOffset, p->m_yOffset, p->m_xOffset, p->m_yOffset); + else + m_limitRect.Add(m2::PointD(p->m_xOffset, p->m_yOffset)); + + m_limitRect.Add(m2::PointD(p->m_xOffset + p->m_texRect.SizeX() - 4, + p->m_yOffset + p->m_texRect.SizeY() - 4)); + + } + } + else + { + GlyphMetrics const m = m_rm->getGlyphMetrics(glyphKey); + if (i == 0) + m_limitRect = m2::RectD(m.m_xOffset, m.m_yOffset, m.m_xOffset, m.m_yOffset); + else + m_limitRect.Add(m2::PointD(m.m_xOffset, m.m_yOffset)); + + m_limitRect.Add(m2::PointD(m.m_xOffset + m.m_xAdvance, + m.m_yOffset + m.m_yAdvance)); + } + } + + /// centered by default + m_limitRect.Offset(-m_limitRect.SizeX() / 2, + -m_limitRect.SizeY() / 2); + + /// adjusting according to position + if (position() & EPosLeft) + m_limitRect.Offset(-m_limitRect.SizeX() / 2, 0); + if (position() & EPosRight) + m_limitRect.Offset(m_limitRect.SizeX() / 2, 0); + + if (position() & EPosAbove) + m_limitRect.Offset(0, m_limitRect.SizeY() / 2); + + if (position() & EPosUnder) + m_limitRect.Offset(0, -m_limitRect.SizeY() / 2); + } + + m2::RectD const TextLayoutElement::boundRect() const + { + return m_limitRect; + } + + void TextLayoutElement::draw(Screen *screen) + { +/* yg::FontDesc desc = m_fontDesc; + if (desc.m_isMasked) + { + desc.m_isMasked = false; + } + */ + } +} diff --git a/yg/text_layout_element.hpp b/yg/text_layout_element.hpp new file mode 100644 index 0000000000..285c603514 --- /dev/null +++ b/yg/text_layout_element.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "layout_element.hpp" +#include "skin.hpp" +#include "font_desc.hpp" + +#include "../std/shared_ptr.hpp" + +namespace yg +{ + class Skin; + class ResourceManager; + + class TextLayoutElement : public LayoutElement + { + private: + + wstring m_text; + double m_depth; + FontDesc m_fontDesc; + shared_ptr m_skin; + shared_ptr m_rm; + bool m_log2vis; + m2::RectD m_limitRect; + + public: + + TextLayoutElement( + char const * text, + double depth, + FontDesc const & fontDesc, + bool log2vis, + shared_ptr const & skin, + shared_ptr const & rm, + m2::PointD const & pivot, + yg::EPosition pos); + + m2::RectD const boundRect() const; + + void draw(Screen * screen); + }; +} diff --git a/yg/text_on_path_layout_element.hpp b/yg/text_on_path_layout_element.hpp new file mode 100644 index 0000000000..237d7aeaa4 --- /dev/null +++ b/yg/text_on_path_layout_element.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "text_layout_element.hpp" +#include "../std/shared_ptr.hpp" + +namespace yg +{ + struct FontDesc; + class TextPath; + + class TextOnPathLayoutElement : public TextLayoutElement + { + public: + TextOnPathLayoutElement(shared_ptr const & path, + char const * text, + double depth, + FontDesc const & fontDesc); + + m2::RectD const boundRect() const; + + void draw(Screen * screen); + }; +} diff --git a/yg/text_renderer.hpp b/yg/text_renderer.hpp index 777ec980a8..65b35df63b 100644 --- a/yg/text_renderer.hpp +++ b/yg/text_renderer.hpp @@ -62,14 +62,6 @@ namespace yg template void ForEachGlyph(FontDesc const & fontDesc, wstring const & text, ToDo toDo); - void drawGlyph(m2::PointD const & ptOrg, - m2::PointD const & ptGlyph, - float angle, - float blOffset, - CharStyle const * p, - double depth); - - bool drawPathTextImpl(FontDesc const & fontDesc, m2::PointD const * path, size_t s, @@ -108,6 +100,13 @@ namespace yg TextRenderer(Params const & params); + void drawGlyph(m2::PointD const & ptOrg, + m2::PointD const & ptGlyph, + float angle, + float blOffset, + CharStyle const * p, + double depth); + /// Drawing text from point rotated by the angle. void drawText(FontDesc const & fontDesc, m2::PointD const & pt, diff --git a/yg/yg.pro b/yg/yg.pro index 54f1a5b821..386f00a95b 100644 --- a/yg/yg.pro +++ b/yg/yg.pro @@ -57,7 +57,8 @@ SOURCES += \ area_renderer.cpp \ layout_element.cpp \ font_desc.cpp \ - glyph_layout.cpp + glyph_layout.cpp \ + text_layout_element.cpp HEADERS += \ internal/opengl.hpp \ @@ -107,7 +108,10 @@ HEADERS += \ area_renderer.hpp \ layout_element.hpp \ font_desc.hpp \ - glyph_layout.hpp + glyph_layout.hpp \ + text_on_path_layout_element.hpp \ + text_layout_element.hpp \ + symbol_layout_element.hpp win32 { HEADERS += internal/opengl_win32.hpp