DrawHTML.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* DrawHTML()
  2. * Drop-in replacement for DrawText() supporting a tiny subset of HTML.
  3. */
  4. #include <windows.h>
  5. #include <tchar.h>
  6. #include <assert.h>
  7. //use unprintable characters so it doesn't find copied html to convert
  8. #define ENDFLAG 0x100
  9. enum { tNONE, tB, tBR, tFONT, tI, tP, tSUB, tSUP, tU, tNUMTAGS };
  10. struct
  11. {
  12. char *mnemonic;
  13. short token, param, block;
  14. } Tags[] = {
  15. { NULL, tNONE, 0, 0},
  16. { _T("\x04"), tFONT, 1, 0 },
  17. { _T("\x05"), tBR, 0, 1 },
  18. /*{ _T("b"), tB, 0, 0},
  19. { _T("br"), tBR, 0, 1},
  20. { _T("em"), tI, 0, 0},
  21. { _T("font"), tFONT, 1, 0},
  22. { _T("i"), tI, 0, 0},
  23. { _T("p"), tP, 0, 1},
  24. { _T("strong"), tB, 0, 0},
  25. { _T("sub"), tSUB, 0, 0},
  26. { _T("sup"), tSUP, 0, 0},
  27. { _T("u"), tU, 0, 0},*/
  28. };
  29. static int GetToken(LPCTSTR *String, int *Size, int *TokenLength, BOOL *WhiteSpace)
  30. {
  31. LPCTSTR Start, EndToken;
  32. int Length, EntryWhiteSpace, Index, IsEndTag;
  33. assert(String != NULL && *String != NULL);
  34. assert(Size != NULL);
  35. Start = *String;
  36. /* check for leading white space, then skip it */
  37. if (WhiteSpace != NULL)
  38. {
  39. EntryWhiteSpace = *WhiteSpace;
  40. *WhiteSpace = EntryWhiteSpace || _istspace(*Start);
  41. }
  42. else
  43. {
  44. EntryWhiteSpace = FALSE;
  45. } /* if */
  46. while (*Size > 0 && _istspace(*Start))
  47. {
  48. Start++;
  49. *Size -= 1;
  50. } /* if */
  51. if (*Size <= 0)
  52. return -1; /* no printable text left */
  53. EndToken = Start;
  54. Length = 0;
  55. IsEndTag = 0;
  56. if (*EndToken == _T('\x01'))
  57. {
  58. /* might be a HTML tag, check */
  59. EndToken++;
  60. Length++;
  61. if (Length < *Size && *EndToken == _T('\x03'))
  62. {
  63. IsEndTag = ENDFLAG;
  64. EndToken++;
  65. Length++;
  66. } /* if */
  67. while (Length < *Size && !_istspace(*EndToken)
  68. && *EndToken != _T('\x01') && *EndToken != _T('\x02'))
  69. {
  70. EndToken++;
  71. Length++;
  72. } /* while */
  73. for (Index = sizeof Tags / sizeof Tags[0] - 1; Index > 0; Index--)
  74. if (!_tcsnicmp(Start + (IsEndTag ? 2 : 1), Tags[Index].mnemonic,
  75. _tcslen(Tags[Index].mnemonic)))
  76. break;
  77. if (Index > 0)
  78. {
  79. /* so it is a tag, see whether to accept parameters */
  80. if (Tags[Index].param && !IsEndTag)
  81. {
  82. while (Length < *Size
  83. && *EndToken != _T('\x01') && *EndToken != _T('\x02'))
  84. {
  85. EndToken++;
  86. Length++;
  87. } /* while */
  88. }
  89. else if (*EndToken != _T('\x02'))
  90. {
  91. /* no parameters, then '>' must follow the tag */
  92. Index = 0;
  93. } /* if */
  94. if (WhiteSpace != NULL && Tags[Index].block)
  95. *WhiteSpace = FALSE;
  96. } /* if */
  97. if (*EndToken == _T('\x02'))
  98. {
  99. EndToken++;
  100. Length++;
  101. } /* if */
  102. /* skip trailing white space in some circumstances */
  103. if (Index > 0 && (Tags[Index].block || EntryWhiteSpace))
  104. {
  105. while (Length < *Size && _istspace(*EndToken))
  106. {
  107. EndToken++;
  108. Length++;
  109. } /* while */
  110. } /* if */
  111. }
  112. else
  113. {
  114. /* normal word (no tag) */
  115. Index = 0;
  116. while (Length < *Size && !_istspace(*EndToken) && *EndToken != _T('\x01'))
  117. {
  118. EndToken++;
  119. Length++;
  120. } /* while */
  121. } /* if */
  122. if (TokenLength != NULL)
  123. *TokenLength = Length;
  124. *Size -= Length;
  125. *String = Start;
  126. return Tags[Index].token | IsEndTag;
  127. }
  128. static int HexDigit(TCHAR ch)
  129. {
  130. if (ch >= _T('0') && ch <= _T('9'))
  131. return ch - _T('0');
  132. if (ch >= _T('A') && ch <= _T('F'))
  133. return ch - _T('A') + 10;
  134. if (ch >= _T('a') && ch <= _T('f'))
  135. return ch - _T('a') + 10;
  136. return 0;
  137. }
  138. static COLORREF ParseColor(LPCTSTR String)
  139. {
  140. int Red, Green, Blue;
  141. if (*String == _T('\'') || *String == _T('"'))
  142. String++;
  143. if (*String == _T('#'))
  144. String++;
  145. Red = (HexDigit(String[0]) << 4) | HexDigit(String[1]);
  146. Green = (HexDigit(String[2]) << 4) | HexDigit(String[3]);
  147. Blue = (HexDigit(String[4]) << 4) | HexDigit(String[5]);
  148. return RGB(Red, Green, Blue);
  149. }
  150. #define STACKSIZE 8
  151. static COLORREF stack[STACKSIZE];
  152. static int stacktop;
  153. static BOOL PushColor(HDC hdc, COLORREF clr)
  154. {
  155. if (stacktop < STACKSIZE)
  156. stack[stacktop++] = GetTextColor(hdc);
  157. SetTextColor(hdc, clr);
  158. return TRUE;
  159. }
  160. static BOOL PopColor(HDC hdc)
  161. {
  162. COLORREF clr;
  163. BOOL okay = (stacktop > 0);
  164. if (okay)
  165. clr = stack[--stacktop];
  166. else
  167. clr = stack[0];
  168. SetTextColor(hdc, clr);
  169. return okay;
  170. }
  171. #define FV_BOLD 0x01
  172. #define FV_ITALIC (FV_BOLD << 1)
  173. #define FV_UNDERLINE (FV_ITALIC << 1)
  174. #define FV_SUPERSCRIPT (FV_UNDERLINE << 1)
  175. #define FV_SUBSCRIPT (FV_SUPERSCRIPT << 1)
  176. #define FV_NUMBER (FV_SUBSCRIPT << 1)
  177. static HFONT GetFontVariant(HDC hdc, HFONT hfontSource, int Styles)
  178. {
  179. LOGFONT logFont = { 0 };
  180. SelectObject(hdc, (HFONT)GetStockObject(SYSTEM_FONT));
  181. if (!GetObject(hfontSource, sizeof logFont, &logFont))
  182. return NULL;
  183. /* set parameters, create new font */
  184. logFont.lfWeight = (Styles & FV_BOLD) ? FW_BOLD : FW_NORMAL;
  185. logFont.lfItalic = (BYTE)(Styles & FV_ITALIC) != 0;
  186. logFont.lfUnderline = (BYTE)(Styles & FV_UNDERLINE) != 0;
  187. if (Styles & (FV_SUPERSCRIPT | FV_SUBSCRIPT))
  188. logFont.lfHeight = logFont.lfHeight * 7 / 10;
  189. return CreateFontIndirect(&logFont);
  190. }
  191. #if defined __cplusplus
  192. extern "C"
  193. #endif
  194. int __stdcall DrawHTML(
  195. HDC hdc, // handle of device context
  196. LPCTSTR lpString, // address of string to draw
  197. int nCount, // string length, in characters
  198. LPRECT lpRect, // address of structure with formatting dimensions
  199. UINT uFormat // text-drawing flags
  200. )
  201. {
  202. LPCTSTR Start;
  203. int Left, Top, MaxWidth, MinWidth, Height, MaxHeight;
  204. int SavedDC;
  205. int Tag, TokenLength;
  206. HFONT hfontBase, hfontSpecial[FV_NUMBER];
  207. int Styles, CurStyles;
  208. SIZE size;
  209. int Index, LineHeight;
  210. POINT CurPos;
  211. int WidthOfSPace, XPos;
  212. BOOL WhiteSpace;
  213. RECT rc;
  214. if (hdc == NULL || lpString == NULL)
  215. return 0;
  216. if (nCount < 0)
  217. nCount = _tcslen(lpString);
  218. MaxHeight = INT_MAX;
  219. if (lpRect != NULL)
  220. {
  221. Left = lpRect->left;
  222. Top = lpRect->top;
  223. MaxWidth = lpRect->right - lpRect->left;
  224. MaxHeight = lpRect->bottom - lpRect->top;
  225. }
  226. else
  227. {
  228. GetCurrentPositionEx(hdc, &CurPos);
  229. Left = CurPos.x;
  230. Top = CurPos.y;
  231. MaxWidth = GetDeviceCaps(hdc, HORZRES) - Left;
  232. } /* if */
  233. if (MaxWidth < 0)
  234. MaxWidth = 0;
  235. /* toggle flags we do not support */
  236. uFormat &= ~(DT_CENTER | DT_RIGHT | DT_TABSTOP);
  237. uFormat |= (DT_LEFT | DT_NOPREFIX);
  238. /* get the "default" font from the DC */
  239. SavedDC = SaveDC(hdc);
  240. hfontBase = SelectObject(hdc, (HFONT)GetStockObject(SYSTEM_FONT));
  241. SelectObject(hdc, hfontBase);
  242. /* clear the other fonts, they are created "on demand" */
  243. for (Index = 0; Index < FV_NUMBER; Index++)
  244. hfontSpecial[Index] = NULL;
  245. hfontSpecial[0] = hfontBase;
  246. Styles = 0; /* assume the active font is normal weight, roman, non-underlined */
  247. /* get font height (use characters with ascender and descender);
  248. * we make the assumption here that changing the font style will
  249. * not change the font height
  250. */
  251. GetTextExtentPoint32(hdc, _T("Åy"), 2, &size);
  252. LineHeight = size.cy;
  253. /* run through the string, word for word */
  254. XPos = 0;
  255. MinWidth = 0;
  256. stacktop = 0;
  257. CurStyles = -1; /* force a select of the proper style */
  258. Height = 0;
  259. WhiteSpace = FALSE;
  260. Start = lpString;
  261. for (;; )
  262. {
  263. Tag = GetToken(&Start, &nCount, &TokenLength, &WhiteSpace);
  264. if (Tag < 0)
  265. break;
  266. switch (Tag & ~ENDFLAG)
  267. {
  268. case tP:
  269. if ((Tag & ENDFLAG) == 0 && (uFormat & DT_SINGLELINE) == 0)
  270. {
  271. if (Start != lpString)
  272. Height += 3 * LineHeight / 2;
  273. XPos = 0;
  274. } /* if */
  275. break;
  276. case tBR:
  277. if ((Tag & ENDFLAG) == 0 && (uFormat & DT_SINGLELINE) == 0)
  278. {
  279. Height += LineHeight;
  280. XPos = 0;
  281. } /* if */
  282. break;
  283. case tB:
  284. Styles = (Tag & ENDFLAG) ? Styles & ~FV_BOLD : Styles | FV_BOLD;
  285. break;
  286. case tI:
  287. Styles = (Tag & ENDFLAG) ? Styles & ~FV_ITALIC : Styles | FV_ITALIC;
  288. break;
  289. case tU:
  290. Styles = (Tag & ENDFLAG) ? Styles & ~FV_UNDERLINE : Styles | FV_UNDERLINE;
  291. break;
  292. case tSUB:
  293. Styles = (Tag & ENDFLAG) ? Styles & ~FV_SUBSCRIPT : Styles | FV_SUBSCRIPT;
  294. break;
  295. case tSUP:
  296. Styles = (Tag & ENDFLAG) ? Styles & ~FV_SUPERSCRIPT : Styles | FV_SUPERSCRIPT;
  297. break;
  298. case tFONT:
  299. if ((Tag & ENDFLAG) == 0)
  300. {
  301. if (_tcsnicmp(Start + 3, _T("color="), 6) == 0)
  302. PushColor(hdc, ParseColor(Start + 9));
  303. }
  304. else
  305. {
  306. PopColor(hdc);
  307. } /* if */
  308. break;
  309. default:
  310. if (Tag == (tNONE | ENDFLAG))
  311. break;
  312. if (CurStyles != Styles)
  313. {
  314. if (hfontSpecial[Styles] == NULL)
  315. hfontSpecial[Styles] = GetFontVariant(hdc, hfontBase, Styles);
  316. CurStyles = Styles;
  317. SelectObject(hdc, hfontSpecial[Styles]);
  318. /* get the width of a space character (for word spacing) */
  319. GetTextExtentPoint32(hdc, _T(" "), 1, &size);
  320. WidthOfSPace = size.cx;
  321. } /* if */
  322. /* check word length, check whether to wrap around */
  323. GetTextExtentPoint32(hdc, Start, TokenLength, &size);
  324. if (size.cx > MaxWidth)
  325. MaxWidth = size.cx; /* must increase width: long non-breakable word */
  326. if (WhiteSpace)
  327. XPos += WidthOfSPace;
  328. if (XPos + size.cx > MaxWidth && WhiteSpace)
  329. {
  330. if ((uFormat & DT_WORDBREAK) != 0)
  331. {
  332. /* word wrap */
  333. Height += LineHeight;
  334. XPos = 0;
  335. }
  336. else
  337. {
  338. /* no word wrap, must increase the width */
  339. MaxWidth = XPos + size.cx;
  340. } /* if */
  341. } /* if */
  342. /* output text (unless DT_CALCRECT is set) */
  343. if ((uFormat & DT_CALCRECT) == 0)
  344. {
  345. /* handle negative heights, too (suggestion of "Sims") */
  346. if (Top < 0)
  347. SetRect(&rc, Left + XPos, Top - Height,
  348. Left + MaxWidth, Top - (Height + LineHeight));
  349. else
  350. SetRect(&rc, Left + XPos, Top + Height,
  351. Left + MaxWidth, Top + Height + LineHeight);
  352. /* reposition subscript text to align below the baseline */
  353. DrawText(hdc, Start, TokenLength, &rc,
  354. uFormat | ((Styles & FV_SUBSCRIPT) ? DT_BOTTOM | DT_SINGLELINE : 0));
  355. /* for the underline style, the spaces between words should be
  356. * underlined as well
  357. */
  358. if (WhiteSpace && (Styles & FV_UNDERLINE) && XPos >= WidthOfSPace)
  359. {
  360. if (Top < 0)
  361. SetRect(&rc, Left + XPos - WidthOfSPace, Top - Height,
  362. Left + XPos, Top - (Height + LineHeight));
  363. else
  364. SetRect(&rc, Left + XPos - WidthOfSPace, Top + Height,
  365. Left + XPos, Top + Height + LineHeight);
  366. DrawText(hdc, " ", 1, &rc, uFormat);
  367. } /* if */
  368. } /* if */
  369. /* update current position */
  370. XPos += size.cx;
  371. if (XPos > MinWidth)
  372. MinWidth = XPos;
  373. WhiteSpace = FALSE;
  374. } /* if */
  375. if ((Height + LineHeight) >= MaxHeight)
  376. break;
  377. Start += TokenLength;
  378. } /* for */
  379. RestoreDC(hdc, SavedDC);
  380. for (Index = 1; Index < FV_NUMBER; Index++) /* do not erase hfontSpecial[0] */
  381. if (hfontSpecial[Index] != NULL)
  382. DeleteObject(hfontSpecial[Index]);
  383. /* store width and height back into the lpRect structure */
  384. if ((uFormat & DT_CALCRECT) != 0 && lpRect != NULL)
  385. {
  386. lpRect->right = lpRect->left + MinWidth;
  387. if (lpRect->top < 0)
  388. lpRect->bottom = lpRect->top - (Height + LineHeight);
  389. else
  390. lpRect->bottom = lpRect->top + Height + LineHeight;
  391. } /* if */
  392. return Height;
  393. }