From f93da3b0d87d3ca8599378d0d8d6db32c152fdfa Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Fri, 19 Aug 2016 10:28:43 +0200 Subject: [PATCH] [Kandinsky] Create a KDText class for text measurement Change-Id: Ie3ecd402b5476f41a2f8694a276f62495fbcdbd1 --- escher/src/text_view.cpp | 2 +- kandinsky/Makefile | 1 + kandinsky/include/kandinsky.h | 1 + kandinsky/include/kandinsky/context.h | 1 - kandinsky/include/kandinsky/text.h | 11 +++++++++++ kandinsky/src/context_text.cpp | 5 ----- kandinsky/src/text.cpp | 7 +++++++ quiz/src/runner.cpp | 2 +- 8 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 kandinsky/include/kandinsky/text.h create mode 100644 kandinsky/src/text.cpp diff --git a/escher/src/text_view.cpp b/escher/src/text_view.cpp index 209825e83..bdd24fc56 100644 --- a/escher/src/text_view.cpp +++ b/escher/src/text_view.cpp @@ -17,7 +17,7 @@ void TextView::setText(const char * text) { } void TextView::drawRect(KDContext * ctx, KDRect rect) const { - KDSize textSize = ctx->stringSize(m_text); + KDSize textSize = KDText::stringSize(m_text); KDPoint origin = { (KDCoordinate)(m_horizontalAlignment*(m_frame.width() - textSize.width())), (KDCoordinate)(m_verticalAlignment*(m_frame.height() - textSize.height())) diff --git a/kandinsky/Makefile b/kandinsky/Makefile index 97dfd0d34..8f5c8db29 100644 --- a/kandinsky/Makefile +++ b/kandinsky/Makefile @@ -13,6 +13,7 @@ objs += $(addprefix kandinsky/src/,\ point.o\ rect.o\ size.o\ + text.o\ ) tests += $(addprefix kandinsky/test/,\ color.cpp\ diff --git a/kandinsky/include/kandinsky.h b/kandinsky/include/kandinsky.h index 11386d20f..cd6b21b5f 100644 --- a/kandinsky/include/kandinsky.h +++ b/kandinsky/include/kandinsky.h @@ -10,5 +10,6 @@ #include #include #include +#include #endif diff --git a/kandinsky/include/kandinsky/context.h b/kandinsky/include/kandinsky/context.h index d9f7a2dd1..297c2fb04 100644 --- a/kandinsky/include/kandinsky/context.h +++ b/kandinsky/include/kandinsky/context.h @@ -16,7 +16,6 @@ public: // Text void drawChar(char character, KDPoint p, uint8_t inverse); void drawString(const char * text, KDPoint p, uint8_t inverse); - KDSize stringSize(const char * text); // Line. Not anti-aliased. void drawLine(KDPoint p1, KDPoint p2, KDColor c); diff --git a/kandinsky/include/kandinsky/text.h b/kandinsky/include/kandinsky/text.h new file mode 100644 index 000000000..69cd93b91 --- /dev/null +++ b/kandinsky/include/kandinsky/text.h @@ -0,0 +1,11 @@ +#ifndef KANDINSKY_TEXT_H +#define KANDINSKY_TEXT_H + +#include + +class KDText { +public: + static KDSize stringSize(const char * text); +}; + +#endif diff --git a/kandinsky/src/context_text.cpp b/kandinsky/src/context_text.cpp index eca8da8f8..3d8e62c76 100644 --- a/kandinsky/src/context_text.cpp +++ b/kandinsky/src/context_text.cpp @@ -1,5 +1,4 @@ #include -#include #include "font.h" void KDContext::drawChar(char character, KDPoint p, uint8_t inverse) { @@ -24,7 +23,3 @@ void KDContext::drawString(const char * text, KDPoint p, uint8_t inverse) { position = position.translatedBy(characterSize); } } - -KDSize KDContext::stringSize(const char * text) { - return KDSize(BITMAP_FONT_CHARACTER_WIDTH*strlen(text), BITMAP_FONT_CHARACTER_HEIGHT); -} diff --git a/kandinsky/src/text.cpp b/kandinsky/src/text.cpp new file mode 100644 index 000000000..75e03d07e --- /dev/null +++ b/kandinsky/src/text.cpp @@ -0,0 +1,7 @@ +#include +#include +#include "font.h" + +KDSize KDText::stringSize(const char * text) { + return KDSize(BITMAP_FONT_CHARACTER_WIDTH*strlen(text), BITMAP_FONT_CHARACTER_HEIGHT); +} diff --git a/quiz/src/runner.cpp b/quiz/src/runner.cpp index df6b6e9e0..45050a511 100644 --- a/quiz/src/runner.cpp +++ b/quiz/src/runner.cpp @@ -8,7 +8,7 @@ extern "C" { void print(const char * message) { static int line_y = 0; KDContext * ctx = KDIonContext::sharedContext(); - int line_height = ctx->stringSize("M").height(); + int line_height = KDText::stringSize("M").height(); ctx->drawString(message, KDPoint(0, line_y), 0); line_y += line_height; if (line_y > ION_SCREEN_HEIGHT) {