-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.cpp
More file actions
111 lines (84 loc) · 2.82 KB
/
Font.cpp
File metadata and controls
111 lines (84 loc) · 2.82 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
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
#include "common.h"
#include "Font.h"
Font::Font()
{
hFont = 0;
}
Font::~Font()
{
if(hFont != 0)
DeleteObject(hFont);
}
void Font::configure(const string &prefix, Font *baseFont)
{
shadow = GetRCBoolean(prefix, "FontShadow", baseFont ? baseFont->shadow : false);
shadowColor = GetRCColor(prefix, "FontShadowColor", baseFont ? baseFont->shadowColor : RGB(0, 0, 0));
shadowX = GetRCInt(prefix, "FontShadowX", baseFont ? baseFont->shadowX : 1);
shadowY = GetRCInt(prefix, "FontShadowY", baseFont ? baseFont->shadowY : 1);
name = GetRCString(prefix, "Font", baseFont ? baseFont->name : "Arial");
height = GetRCInt(prefix, "FontHeight", baseFont ? baseFont->height : 15, 0);
color = GetRCColor(prefix, "FontColor", baseFont ? baseFont->color : (shadow ? RGB(255, 255, 255) : RGB(0, 0, 0)));
bold = GetRCBoolean(prefix, "FontBold", baseFont ? baseFont->bold : false);
italic = GetRCBoolean(prefix, "FontItalic", baseFont ? baseFont->italic : false);
}
void Font::apply(HDC hDC, int x, int y, int width, int height, const string &text, unsigned int flags)
{
if(hFont == 0)
createHandle();
RECT r;
r.left = x;
r.top = y;
r.right = x + width;
r.bottom = y + height;
hFont = (HFONT) SelectObject(hDC, hFont);
int bkMode = SetBkMode(hDC, TRANSPARENT);
int textColor = SetTextColor(hDC, color);
DrawTextEx(hDC, (char *) text.c_str(), text.length(), &r, flags | DT_CALCRECT, 0);
int neededHeight = r.bottom - r.top;
int vPad = (height - neededHeight) / 2;
r.left = x;
r.top = y + vPad;
r.right = x + width;
r.bottom = y + neededHeight + vPad;
if(shadow)
{
OffsetRect(&r, (shadowX + 1) / 2, (shadowY + 1) / 2);
SetTextColor(hDC, shadowColor);
DrawTextEx(hDC, (char *) text.c_str(), text.length(), &r, flags, 0);
OffsetRect(&r, -shadowX, -shadowY);
}
SetTextColor(hDC, color);
DrawTextEx(hDC, (char *) text.c_str(), text.length(), &r, flags, 0);
hFont = (HFONT) SelectObject(hDC, hFont);
SetBkMode(hDC, bkMode);
SetTextColor(hDC, textColor);
}
void Font::measure(HDC hDC, const string &text, unsigned int flags, long *width, long *height)
{
hFont = (HFONT) SelectObject(hDC, hFont);
LPSIZE lpsize=new SIZE;
lpsize->cx=300;
lpsize->cy=100;
GetTextExtentPoint32(hDC, text.c_str(), strlen(text.c_str()), lpsize);
hFont = (HFONT) SelectObject(hDC, hFont);
*width=lpsize->cx;
*height=lpsize->cy;
delete lpsize;
}
void Font::setColor(int aColor)
{
color = aColor;
}
void Font::createHandle()
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
StringCchCopy(lf.lfFaceName, LF_FACESIZE, name.c_str());
lf.lfHeight = height;
lf.lfItalic = italic ? TRUE : FALSE;
lf.lfWeight = bold ? FW_BOLD : FW_NORMAL;
lf.lfCharSet = DEFAULT_CHARSET;
if(hFont != 0)
DeleteObject(hFont);
hFont = CreateFontIndirect(&lf);
}