[Kandinsky] Create a KDText class for text measurement

Change-Id: Ie3ecd402b5476f41a2f8694a276f62495fbcdbd1
This commit is contained in:
Romain Goyet
2016-08-19 10:28:43 +02:00
parent d2ff3457b5
commit f93da3b0d8
8 changed files with 22 additions and 8 deletions

View File

@@ -17,7 +17,7 @@ void TextView::setText(const char * text) {
} }
void TextView::drawRect(KDContext * ctx, KDRect rect) const { void TextView::drawRect(KDContext * ctx, KDRect rect) const {
KDSize textSize = ctx->stringSize(m_text); KDSize textSize = KDText::stringSize(m_text);
KDPoint origin = { KDPoint origin = {
(KDCoordinate)(m_horizontalAlignment*(m_frame.width() - textSize.width())), (KDCoordinate)(m_horizontalAlignment*(m_frame.width() - textSize.width())),
(KDCoordinate)(m_verticalAlignment*(m_frame.height() - textSize.height())) (KDCoordinate)(m_verticalAlignment*(m_frame.height() - textSize.height()))

View File

@@ -13,6 +13,7 @@ objs += $(addprefix kandinsky/src/,\
point.o\ point.o\
rect.o\ rect.o\
size.o\ size.o\
text.o\
) )
tests += $(addprefix kandinsky/test/,\ tests += $(addprefix kandinsky/test/,\
color.cpp\ color.cpp\

View File

@@ -10,5 +10,6 @@
#include <kandinsky/point.h> #include <kandinsky/point.h>
#include <kandinsky/rect.h> #include <kandinsky/rect.h>
#include <kandinsky/size.h> #include <kandinsky/size.h>
#include <kandinsky/text.h>
#endif #endif

View File

@@ -16,7 +16,6 @@ public:
// Text // Text
void drawChar(char character, KDPoint p, uint8_t inverse); void drawChar(char character, KDPoint p, uint8_t inverse);
void drawString(const char * text, KDPoint p, uint8_t inverse); void drawString(const char * text, KDPoint p, uint8_t inverse);
KDSize stringSize(const char * text);
// Line. Not anti-aliased. // Line. Not anti-aliased.
void drawLine(KDPoint p1, KDPoint p2, KDColor c); void drawLine(KDPoint p1, KDPoint p2, KDColor c);

View File

@@ -0,0 +1,11 @@
#ifndef KANDINSKY_TEXT_H
#define KANDINSKY_TEXT_H
#include <kandinsky/size.h>
class KDText {
public:
static KDSize stringSize(const char * text);
};
#endif

View File

@@ -1,5 +1,4 @@
#include <kandinsky/context.h> #include <kandinsky/context.h>
#include <string.h>
#include "font.h" #include "font.h"
void KDContext::drawChar(char character, KDPoint p, uint8_t inverse) { 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); position = position.translatedBy(characterSize);
} }
} }
KDSize KDContext::stringSize(const char * text) {
return KDSize(BITMAP_FONT_CHARACTER_WIDTH*strlen(text), BITMAP_FONT_CHARACTER_HEIGHT);
}

7
kandinsky/src/text.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include <kandinsky/text.h>
#include <string.h>
#include "font.h"
KDSize KDText::stringSize(const char * text) {
return KDSize(BITMAP_FONT_CHARACTER_WIDTH*strlen(text), BITMAP_FONT_CHARACTER_HEIGHT);
}

View File

@@ -8,7 +8,7 @@ extern "C" {
void print(const char * message) { void print(const char * message) {
static int line_y = 0; static int line_y = 0;
KDContext * ctx = KDIonContext::sharedContext(); 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); ctx->drawString(message, KDPoint(0, line_y), 0);
line_y += line_height; line_y += line_height;
if (line_y > ION_SCREEN_HEIGHT) { if (line_y > ION_SCREEN_HEIGHT) {