forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawHTML.C
429 lines (393 loc) · 10.7 KB
/
DrawHTML.C
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* DrawHTML()
* Drop-in replacement for DrawText() supporting a tiny subset of HTML.
*/
#include <windows.h>
#include <tchar.h>
#include <assert.h>
//use unprintable characters so it doesn't find copied html to convert
#define ENDFLAG 0x100
enum { tNONE, tB, tBR, tFONT, tI, tP, tSUB, tSUP, tU, tNUMTAGS };
struct
{
char *mnemonic;
short token, param, block;
} Tags[] = {
{ NULL, tNONE, 0, 0},
{ _T("\x04"), tFONT, 1, 0 },
{ _T("\x05"), tBR, 0, 1 },
/*{ _T("b"), tB, 0, 0},
{ _T("br"), tBR, 0, 1},
{ _T("em"), tI, 0, 0},
{ _T("font"), tFONT, 1, 0},
{ _T("i"), tI, 0, 0},
{ _T("p"), tP, 0, 1},
{ _T("strong"), tB, 0, 0},
{ _T("sub"), tSUB, 0, 0},
{ _T("sup"), tSUP, 0, 0},
{ _T("u"), tU, 0, 0},*/
};
static int GetToken(LPCTSTR *String, int *Size, int *TokenLength, BOOL *WhiteSpace)
{
LPCTSTR Start, EndToken;
int Length, EntryWhiteSpace, Index, IsEndTag;
assert(String != NULL && *String != NULL);
assert(Size != NULL);
Start = *String;
/* check for leading white space, then skip it */
if (WhiteSpace != NULL)
{
EntryWhiteSpace = *WhiteSpace;
*WhiteSpace = EntryWhiteSpace || _istspace(*Start);
}
else
{
EntryWhiteSpace = FALSE;
} /* if */
while (*Size > 0 && _istspace(*Start))
{
Start++;
*Size -= 1;
} /* if */
if (*Size <= 0)
return -1; /* no printable text left */
EndToken = Start;
Length = 0;
IsEndTag = 0;
if (*EndToken == _T('\x01'))
{
/* might be a HTML tag, check */
EndToken++;
Length++;
if (Length < *Size && *EndToken == _T('\x03'))
{
IsEndTag = ENDFLAG;
EndToken++;
Length++;
} /* if */
while (Length < *Size && !_istspace(*EndToken)
&& *EndToken != _T('\x01') && *EndToken != _T('\x02'))
{
EndToken++;
Length++;
} /* while */
for (Index = sizeof Tags / sizeof Tags[0] - 1; Index > 0; Index--)
if (!_tcsnicmp(Start + (IsEndTag ? 2 : 1), Tags[Index].mnemonic,
_tcslen(Tags[Index].mnemonic)))
break;
if (Index > 0)
{
/* so it is a tag, see whether to accept parameters */
if (Tags[Index].param && !IsEndTag)
{
while (Length < *Size
&& *EndToken != _T('\x01') && *EndToken != _T('\x02'))
{
EndToken++;
Length++;
} /* while */
}
else if (*EndToken != _T('\x02'))
{
/* no parameters, then '>' must follow the tag */
Index = 0;
} /* if */
if (WhiteSpace != NULL && Tags[Index].block)
*WhiteSpace = FALSE;
} /* if */
if (*EndToken == _T('\x02'))
{
EndToken++;
Length++;
} /* if */
/* skip trailing white space in some circumstances */
if (Index > 0 && (Tags[Index].block || EntryWhiteSpace))
{
while (Length < *Size && _istspace(*EndToken))
{
EndToken++;
Length++;
} /* while */
} /* if */
}
else
{
/* normal word (no tag) */
Index = 0;
while (Length < *Size && !_istspace(*EndToken) && *EndToken != _T('\x01'))
{
EndToken++;
Length++;
} /* while */
} /* if */
if (TokenLength != NULL)
*TokenLength = Length;
*Size -= Length;
*String = Start;
return Tags[Index].token | IsEndTag;
}
static int HexDigit(TCHAR ch)
{
if (ch >= _T('0') && ch <= _T('9'))
return ch - _T('0');
if (ch >= _T('A') && ch <= _T('F'))
return ch - _T('A') + 10;
if (ch >= _T('a') && ch <= _T('f'))
return ch - _T('a') + 10;
return 0;
}
static COLORREF ParseColor(LPCTSTR String)
{
int Red, Green, Blue;
if (*String == _T('\'') || *String == _T('"'))
String++;
if (*String == _T('#'))
String++;
Red = (HexDigit(String[0]) << 4) | HexDigit(String[1]);
Green = (HexDigit(String[2]) << 4) | HexDigit(String[3]);
Blue = (HexDigit(String[4]) << 4) | HexDigit(String[5]);
return RGB(Red, Green, Blue);
}
#define STACKSIZE 8
static COLORREF stack[STACKSIZE];
static int stacktop;
static BOOL PushColor(HDC hdc, COLORREF clr)
{
if (stacktop < STACKSIZE)
stack[stacktop++] = GetTextColor(hdc);
SetTextColor(hdc, clr);
return TRUE;
}
static BOOL PopColor(HDC hdc)
{
COLORREF clr;
BOOL okay = (stacktop > 0);
if (okay)
clr = stack[--stacktop];
else
clr = stack[0];
SetTextColor(hdc, clr);
return okay;
}
#define FV_BOLD 0x01
#define FV_ITALIC (FV_BOLD << 1)
#define FV_UNDERLINE (FV_ITALIC << 1)
#define FV_SUPERSCRIPT (FV_UNDERLINE << 1)
#define FV_SUBSCRIPT (FV_SUPERSCRIPT << 1)
#define FV_NUMBER (FV_SUBSCRIPT << 1)
static HFONT GetFontVariant(HDC hdc, HFONT hfontSource, int Styles)
{
LOGFONT logFont = { 0 };
SelectObject(hdc, (HFONT)GetStockObject(SYSTEM_FONT));
if (!GetObject(hfontSource, sizeof logFont, &logFont))
return NULL;
/* set parameters, create new font */
logFont.lfWeight = (Styles & FV_BOLD) ? FW_BOLD : FW_NORMAL;
logFont.lfItalic = (BYTE)(Styles & FV_ITALIC) != 0;
logFont.lfUnderline = (BYTE)(Styles & FV_UNDERLINE) != 0;
if (Styles & (FV_SUPERSCRIPT | FV_SUBSCRIPT))
logFont.lfHeight = logFont.lfHeight * 7 / 10;
return CreateFontIndirect(&logFont);
}
#if defined __cplusplus
extern "C"
#endif
int __stdcall DrawHTML(
HDC hdc, // handle of device context
LPCTSTR lpString, // address of string to draw
int nCount, // string length, in characters
LPRECT lpRect, // address of structure with formatting dimensions
UINT uFormat // text-drawing flags
)
{
LPCTSTR Start;
int Left, Top, MaxWidth, MinWidth, Height, MaxHeight;
int SavedDC;
int Tag, TokenLength;
HFONT hfontBase, hfontSpecial[FV_NUMBER];
int Styles, CurStyles;
SIZE size;
int Index, LineHeight;
POINT CurPos;
int WidthOfSPace, XPos;
BOOL WhiteSpace;
RECT rc;
if (hdc == NULL || lpString == NULL)
return 0;
if (nCount < 0)
nCount = _tcslen(lpString);
MaxHeight = INT_MAX;
if (lpRect != NULL)
{
Left = lpRect->left;
Top = lpRect->top;
MaxWidth = lpRect->right - lpRect->left;
MaxHeight = lpRect->bottom - lpRect->top;
}
else
{
GetCurrentPositionEx(hdc, &CurPos);
Left = CurPos.x;
Top = CurPos.y;
MaxWidth = GetDeviceCaps(hdc, HORZRES) - Left;
} /* if */
if (MaxWidth < 0)
MaxWidth = 0;
/* toggle flags we do not support */
uFormat &= ~(DT_CENTER | DT_RIGHT | DT_TABSTOP);
uFormat |= (DT_LEFT | DT_NOPREFIX);
/* get the "default" font from the DC */
SavedDC = SaveDC(hdc);
hfontBase = SelectObject(hdc, (HFONT)GetStockObject(SYSTEM_FONT));
SelectObject(hdc, hfontBase);
/* clear the other fonts, they are created "on demand" */
for (Index = 0; Index < FV_NUMBER; Index++)
hfontSpecial[Index] = NULL;
hfontSpecial[0] = hfontBase;
Styles = 0; /* assume the active font is normal weight, roman, non-underlined */
/* get font height (use characters with ascender and descender);
* we make the assumption here that changing the font style will
* not change the font height
*/
GetTextExtentPoint32(hdc, _T("Åy"), 2, &size);
LineHeight = size.cy;
/* run through the string, word for word */
XPos = 0;
MinWidth = 0;
stacktop = 0;
CurStyles = -1; /* force a select of the proper style */
Height = 0;
WhiteSpace = FALSE;
Start = lpString;
for (;; )
{
Tag = GetToken(&Start, &nCount, &TokenLength, &WhiteSpace);
if (Tag < 0)
break;
switch (Tag & ~ENDFLAG)
{
case tP:
if ((Tag & ENDFLAG) == 0 && (uFormat & DT_SINGLELINE) == 0)
{
if (Start != lpString)
Height += 3 * LineHeight / 2;
XPos = 0;
} /* if */
break;
case tBR:
if ((Tag & ENDFLAG) == 0 && (uFormat & DT_SINGLELINE) == 0)
{
Height += LineHeight;
XPos = 0;
} /* if */
break;
case tB:
Styles = (Tag & ENDFLAG) ? Styles & ~FV_BOLD : Styles | FV_BOLD;
break;
case tI:
Styles = (Tag & ENDFLAG) ? Styles & ~FV_ITALIC : Styles | FV_ITALIC;
break;
case tU:
Styles = (Tag & ENDFLAG) ? Styles & ~FV_UNDERLINE : Styles | FV_UNDERLINE;
break;
case tSUB:
Styles = (Tag & ENDFLAG) ? Styles & ~FV_SUBSCRIPT : Styles | FV_SUBSCRIPT;
break;
case tSUP:
Styles = (Tag & ENDFLAG) ? Styles & ~FV_SUPERSCRIPT : Styles | FV_SUPERSCRIPT;
break;
case tFONT:
if ((Tag & ENDFLAG) == 0)
{
if (_tcsnicmp(Start + 3, _T("color="), 6) == 0)
PushColor(hdc, ParseColor(Start + 9));
}
else
{
PopColor(hdc);
} /* if */
break;
default:
if (Tag == (tNONE | ENDFLAG))
break;
if (CurStyles != Styles)
{
if (hfontSpecial[Styles] == NULL)
hfontSpecial[Styles] = GetFontVariant(hdc, hfontBase, Styles);
CurStyles = Styles;
SelectObject(hdc, hfontSpecial[Styles]);
/* get the width of a space character (for word spacing) */
GetTextExtentPoint32(hdc, _T(" "), 1, &size);
WidthOfSPace = size.cx;
} /* if */
/* check word length, check whether to wrap around */
GetTextExtentPoint32(hdc, Start, TokenLength, &size);
if (size.cx > MaxWidth)
MaxWidth = size.cx; /* must increase width: long non-breakable word */
if (WhiteSpace)
XPos += WidthOfSPace;
if (XPos + size.cx > MaxWidth && WhiteSpace)
{
if ((uFormat & DT_WORDBREAK) != 0)
{
/* word wrap */
Height += LineHeight;
XPos = 0;
}
else
{
/* no word wrap, must increase the width */
MaxWidth = XPos + size.cx;
} /* if */
} /* if */
/* output text (unless DT_CALCRECT is set) */
if ((uFormat & DT_CALCRECT) == 0)
{
/* handle negative heights, too (suggestion of "Sims") */
if (Top < 0)
SetRect(&rc, Left + XPos, Top - Height,
Left + MaxWidth, Top - (Height + LineHeight));
else
SetRect(&rc, Left + XPos, Top + Height,
Left + MaxWidth, Top + Height + LineHeight);
/* reposition subscript text to align below the baseline */
DrawText(hdc, Start, TokenLength, &rc,
uFormat | ((Styles & FV_SUBSCRIPT) ? DT_BOTTOM | DT_SINGLELINE : 0));
/* for the underline style, the spaces between words should be
* underlined as well
*/
if (WhiteSpace && (Styles & FV_UNDERLINE) && XPos >= WidthOfSPace)
{
if (Top < 0)
SetRect(&rc, Left + XPos - WidthOfSPace, Top - Height,
Left + XPos, Top - (Height + LineHeight));
else
SetRect(&rc, Left + XPos - WidthOfSPace, Top + Height,
Left + XPos, Top + Height + LineHeight);
DrawText(hdc, " ", 1, &rc, uFormat);
} /* if */
} /* if */
/* update current position */
XPos += size.cx;
if (XPos > MinWidth)
MinWidth = XPos;
WhiteSpace = FALSE;
} /* if */
if ((Height + LineHeight) >= MaxHeight)
break;
Start += TokenLength;
} /* for */
RestoreDC(hdc, SavedDC);
for (Index = 1; Index < FV_NUMBER; Index++) /* do not erase hfontSpecial[0] */
if (hfontSpecial[Index] != NULL)
DeleteObject(hfontSpecial[Index]);
/* store width and height back into the lpRect structure */
if ((uFormat & DT_CALCRECT) != 0 && lpRect != NULL)
{
lpRect->right = lpRect->left + MinWidth;
if (lpRect->top < 0)
lpRect->bottom = lpRect->top - (Height + LineHeight);
else
lpRect->bottom = lpRect->top + Height + LineHeight;
} /* if */
return Height;
}