-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.cpp
More file actions
45 lines (36 loc) · 1.09 KB
/
text.cpp
File metadata and controls
45 lines (36 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "text.h"
#include "nanovg.h"
#include "screen.h"
extern Screen *gScreen;
Text::Text(Item *parent, float x, float y, float w, float h): Item (parent, x, y, w, h) {
}
void Text::performLayout() {
if(!needsUpdate && width != 0)
return;
needsUpdate = false;
auto ctx = gScreen->vg();
nvgFontFace(ctx, m_fontName.c_str());
nvgFontSize(ctx, m_fontSize);
nvgTextAlign(ctx, m_alignment);
nvgFillColor(ctx, m_color.vgColor());
float bounds[4];
nvgTextBounds(ctx, x(), y(), m_text.c_str(), nullptr, bounds);
if (width == 0)
width = bounds[2] - bounds[0] + 4;
if (hieght == 0)
hieght = bounds[3] - bounds[1];
}
void Text::draw(NVGcontext *ctx) {
Item::draw(ctx);
nvgFontFace(ctx, m_fontName.c_str());
nvgFontSize(ctx, m_fontSize);
nvgTextAlign(ctx, m_alignment);
nvgFillColor(ctx, m_color.vgColor());
auto _x = x();
auto _y = y();
if(m_alignment & NVG_ALIGN_CENTER)
_x += width()/2;
if(m_alignment & NVG_ALIGN_MIDDLE)
_y += hieght()/2;
nvgText(ctx, _x, _y, m_text.c_str(), nullptr);
}