From aba28f101e7f0b8dcb8ce175a1bdd340e2d1ba3a Mon Sep 17 00:00:00 2001 From: rachytski Date: Sat, 29 Jan 2011 13:31:11 +0200 Subject: [PATCH] refactored textRect to work with fixed font. crash at startup fixed. --- map/information_display.cpp | 2 +- yg/geometry_batcher.cpp | 21 ++++++++++++++++----- yg/geometry_batcher.hpp | 3 ++- yg/text_renderer.cpp | 2 +- yg/yg_tests/screengl_test.cpp | 30 ++++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/map/information_display.cpp b/map/information_display.cpp index e3c954d219..3e8c63d465 100644 --- a/map/information_display.cpp +++ b/map/information_display.cpp @@ -245,7 +245,7 @@ void InformationDisplay::drawRuler(DrawerYG * pDrawer) pDrawer->screen()->skin()->mapPenInfo(yg::PenInfo(yg::Color(0, 0, 0, 255), 2, 0, 0, 0)), yg::maxDepth); - m2::RectD textRect = pDrawer->screen()->textRect(scalerText.c_str(), 10, false); + m2::RectD textRect = pDrawer->screen()->textRect(scalerText.c_str(), 10, true, false); pDrawer->screen()->drawText(scalerPts[1] + m2::PointD(7, -7), 0, 10, diff --git a/yg/geometry_batcher.cpp b/yg/geometry_batcher.cpp index 311188262e..17a4914920 100644 --- a/yg/geometry_batcher.cpp +++ b/yg/geometry_batcher.cpp @@ -628,7 +628,7 @@ namespace yg ForEachGlyph(fontSize, text, false, isFixedFont, bind(&GeometryBatcher::drawGlyph, this, cref(pt), _1, angle, 0, _2, depth)); } - m2::RectD const GeometryBatcher::textRect(string const & utf8Text, uint8_t fontSize, bool log2vis) + m2::RectD const GeometryBatcher::textRect(string const & utf8Text, uint8_t fontSize, bool fixedFont, bool log2vis) { m2::RectD rect; m2::PointD pt(0, 0); @@ -639,11 +639,22 @@ namespace yg for (size_t i = 0; i < text.size(); ++i) { - GlyphMetrics const m = resourceManager()->getGlyphMetrics(GlyphKey(text[i], fontSize, false)); + if (fixedFont) + { + uint32_t glyphID = m_skin->mapGlyph(GlyphKey(text[i], fontSize, false), fixedFont); + CharStyle const * p = static_cast(m_skin->fromID(glyphID)); + rect.Add(pt); + rect.Add(pt + m2::PointD(p->m_xOffset + p->m_texRect.SizeX() - 4, -p->m_yOffset - (int)p->m_texRect.SizeY() + 4)); + pt += m2::PointD(p->m_xAdvance, 0); + } + else + { + GlyphMetrics const m = resourceManager()->getGlyphMetrics(GlyphKey(text[i], fontSize, false)); - rect.Add(pt); - rect.Add(pt + m2::PointD(m.m_xOffset + m.m_width, - m.m_yOffset - m.m_height)); - pt += m2::PointD(m.m_xAdvance, 0); + rect.Add(pt); + rect.Add(pt + m2::PointD(m.m_xOffset + m.m_width, - m.m_yOffset - m.m_height)); + pt += m2::PointD(m.m_xAdvance, 0); + } } rect.Inflate(2, 2); diff --git a/yg/geometry_batcher.hpp b/yg/geometry_batcher.hpp index 83cab85861..7fee9e6580 100644 --- a/yg/geometry_batcher.hpp +++ b/yg/geometry_batcher.hpp @@ -154,7 +154,8 @@ namespace yg m2::RectD const textRect(string const & utf8Text, uint8_t fontSize, - bool log2vis); + bool fixedFont = false, + bool log2vis = true); /// Drawing text in the middle of the path. bool drawPathText(m2::PointD const * path, diff --git a/yg/text_renderer.cpp b/yg/text_renderer.cpp index 023a5ee86b..8f9bde194e 100644 --- a/yg/text_renderer.cpp +++ b/yg/text_renderer.cpp @@ -33,7 +33,7 @@ namespace yg m2::RectD const TextRenderer::TextObj::GetLimitRect(GeometryBatcher * pBatcher) const { - return m2::Offset(pBatcher->textRect(m_utf8Text, m_size, m_log2vis), m_pt); + return m2::Offset(pBatcher->textRect(m_utf8Text, m_size, false, m_log2vis), m_pt); } void TextRenderer::drawText(m2::PointD const & pt, diff --git a/yg/yg_tests/screengl_test.cpp b/yg/yg_tests/screengl_test.cpp index 1a86c3b819..774ae9c101 100644 --- a/yg/yg_tests/screengl_test.cpp +++ b/yg/yg_tests/screengl_test.cpp @@ -140,7 +140,7 @@ namespace } }; - struct TestDrawPathWithOffset : TestDrawPathBase + struct PathWithOffset : TestDrawPathBase { typedef TestDrawPathBase base_t; @@ -592,6 +592,31 @@ namespace } }; + struct TestDrawTextRectWithFixedFont : TestDrawStringWithFixedFont + { + typedef TestDrawStringWithFixedFont base_t; + void DoDraw(shared_ptr p) + { + m2::RectD r = p->textRect("Simplicity is the ultimate sophistication", 12, true, false); + + m2::PointD startPt(40, 50); + + m2::PointD pts[6] = { + startPt + m2::PointD(r.minX(), r.minY()), + startPt + m2::PointD(r.maxX(), r.minY()), + startPt + m2::PointD(r.maxX(), r.maxY()), + startPt + m2::PointD(r.minX(), r.minY()), + startPt + m2::PointD(r.maxX(), r.maxY()), + startPt + m2::PointD(r.minX(), r.maxY()) + }; + + p->drawTrianglesList(pts, 6, p->skin()->mapColor(yg::Color(0, 0, 255, 255)), 0); + + base_t::DoDraw(p); + } + }; + + double calc_length(vector const & v) { double ret = 0.0; @@ -876,6 +901,7 @@ namespace // UNIT_TEST_GL(TestDrawSingleSymbolAndSolidPath); // UNIT_TEST_GL(TestDrawString); // UNIT_TEST_GL(TestDrawStringWithFixedFont); + UNIT_TEST_GL(TestDrawTextRectWithFixedFont); // UNIT_TEST_GL(TestDrawStringOnString); // UNIT_TEST_GL(TestDrawTextOnPath); // UNIT_TEST_GL(TestDrawTextOnPathWithOffset); @@ -891,7 +917,7 @@ namespace // UNIT_TEST_GL(TestDrawPathSolid1PX); // UNIT_TEST_GL(TestDrawPathSolid2PX); // UNIT_TEST_GL(TestDrawPathSolid); - UNIT_TEST_GL(TestDrawSector); +// UNIT_TEST_GL(TestDrawSector); // UNIT_TEST_GL(TestDrawPathSolidDiffWidth); // UNIT_TEST_GL(TestDrawPathSolidWithZ); // UNIT_TEST_GL(TestDrawPathSolidWithClipRect);