-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextObject.cpp
170 lines (134 loc) · 2.9 KB
/
TextObject.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "TextObject.h"
void TextObject::RemoveChars (PTSTR pBegin, PTSTR pEnd)
{
do
{
*pBegin = *pEnd;
pBegin++;
pEnd++;
}
while (*(pEnd - 1) != '\0');
}
int TextObject::ProcessTabsAndLineBreaks (PTSTR pText)
{
bool loop = true;
int cSpaceChars = 0;
while (loop)
{
loop = false;
if (*pText == '\t' || *pText == '\n' || *pText == '\r')
{
if (cSpaceChars == 0)
{
*pText = ' ';
pText++;
cSpaceChars++;
}
else
RemoveChars (pText, pText + 1);
loop = true;
}
}
return cSpaceChars;
}
void TextObject::PreprocessText ()
{
int cSpaceChars;
PTSTR pText, pPrevious;
pText = text;
while (*pText != '\0')
{
pPrevious = pText;
cSpaceChars = 0;
while (*pText == ' ')
{
cSpaceChars++;
pText++;
}
if (cSpaceChars > 1)
RemoveChars (pPrevious + 1, pText);
cSpaceChars += ProcessTabsAndLineBreaks (pText);
pText++;
}
}
void TextObject::DrawFormatted (HDC hdc, RECT *prc, AlignmentTypes align)
{
int xStart, yStart, cSpaceChars;
PTSTR pBegin, pEnd, pText;
SIZE size;
pText = text;
yStart = prc->top;
do
{
cSpaceChars = 0;
while (*pText == ' ')
pText++;
pBegin = pText;
do
{
pEnd = pText;
while (*pText != '\0' && *pText != ' ')
pText++;
if (*pText == ' ')
{
pText++;
cSpaceChars++;
GetTextExtentPoint32A (hdc, pBegin, pText - pBegin, &size);
}
} while (*pText != '\0' && size.cx < (prc->right - prc->left));
if (*pText != '\0')
cSpaceChars--;
while (*(pEnd - 1) == ' ')
{
pEnd--;
cSpaceChars--;
}
if (cSpaceChars < 0 && *pText != '\0' && size.cx >= (prc->right - prc->left))
{
pText = pBegin;
cSpaceChars = 0;
do
{
pEnd = pText;
pText++;
GetTextExtentPoint32A (hdc, pBegin, pText - pBegin, &size);
if (*pText == ' ')
cSpaceChars++;
} while (size.cx < (prc->right - prc->left));
}
if (*pText == '\0')
{
GetTextExtentPoint32 (hdc, pBegin, pText - pBegin - 1, &size);
if (size.cx < (prc->right - prc->left))
pEnd = pText;
}
GetTextExtentPoint32 (hdc, pBegin, pEnd - pBegin, &size);
switch (align)
{
case Align_Left:
xStart = prc->left;
break;
case Align_Right:
xStart = prc->right - size.cx;
break;
case Align_Center:
xStart = (prc->right + prc->left - size.cx) / 2;
break;
case Align_Justify:
if (*pEnd != '\0' && cSpaceChars > 0)
SetTextJustification (hdc, prc->right - prc->left - size.cx, cSpaceChars);
xStart = prc->left;
break;
}
TextOut (hdc, xStart, yStart, pBegin, pEnd - pBegin);
SetTextJustification (hdc, 0, 0);
yStart += size.cy;
pText = pEnd;
}
while (*pText && yStart < prc->bottom - size.cy);
}
void TextObject::SetText (char *src)
{
text = new char [strlen (src) + 1];
strcpy (text, src);
}