-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFont.h
149 lines (125 loc) · 2.52 KB
/
Font.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef __FONT_H
#define __FONT_H
#include "common.h"
#include "Canvas.h"
#include "Led.h"
#include "EmBox.h"
#include "FontFace.h"
class EmBoxHolder: public EmBox
{
public:
EmBoxHolder(points lineHeight, points dimensions, Canvas* canvas):
EmBox(dimensions, canvas),
mLineHeight(lineHeight)
{
}
virtual ~EmBoxHolder(void)
{
}
inline void progressX(em delta)
{
mX += delta * mMult;
if (mX + mDimensions >= mCanvas->width())
{
nextLine();
}
}
inline void progressY(em delta)
{
mY += mLineHeight * delta;
mCY = mY + mDimensions;
}
inline void nextLine(void)
{
mX = 0;
progressY(mDimensions);
}
void setX(size_t value)
{
mX = value;
}
void setY(size_t value)
{
mY = value;
mCY = mY + mDimensions;
}
size_t getX(void) const
{
return mX;
}
size_t getY(void) const
{
return mY;
}
void setCanvas(Canvas* canvas)
{
mCanvas = canvas;
}
void setFontSize(points dimensions)
{
mDimensions = dimensions;
mMult = dimensions / MAX_EM;
}
inline bool offCanvas(void) const
{
return mY + (mDimensions * 1.25) >= mCanvas->height();
}
private:
points mLineHeight;
};
class Font
{
public:
Font(FontFace* face, points fontSize):
mFontFace(face),
mBounds(1.15, fontSize, NULL)
{
}
~Font(void)
{
// We don't own the canvas! Don't delete it!!!
}
inline void setCanvas(Canvas* canvas)
{
mBounds.setCanvas(canvas);
reset();
}
inline void reset(void)
{
mBounds.setX(0);
mBounds.setY(0);
}
inline void setFontSize(points size)
{
mBounds.setFontSize(size);
}
size_t write(const char* str)
{
size_t i;
glyph g;
for (i = 0; str[i]; ++i)
{
if (mBounds.offCanvas())
{
break;
}
if (str[i] == '\n')
{
mBounds.nextLine();
continue;
}
// Don't draw a space as the first character of a line.
if (str[i] == ' ' && mBounds.getX() == 0)
{
continue;
}
g = (*mFontFace)[str[i]];
mBounds.progressX((*g)(&mBounds));
}
return i;
}
private:
FontFace* mFontFace;
EmBoxHolder mBounds;
};
#endif