PageRenderTime 76ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/Samples/Chap17/FontClip/EZFont.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 108 lines | 80 code | 23 blank | 5 comment | 3 complexity | 34faad9c71ea069aac671078508d340b MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module EZFont;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. import core.sys.windows.commdlg;
  25. enum EZ_ATTR_BOLD = 1;
  26. enum EZ_ATTR_ITALIC = 2;
  27. enum EZ_ATTR_UNDERLINE = 4;
  28. enum EZ_ATTR_STRIKEOUT = 8;
  29. HFONT EzCreateFont(HDC hdc, string szFaceName, int iDeciPtHeight,
  30. int iDeciPtWidth, int iAttributes, BOOL fLogRes)
  31. {
  32. FLOAT cxDpi, cyDpi;
  33. HFONT hFont;
  34. LOGFONT lf;
  35. POINT pt;
  36. TEXTMETRIC tm;
  37. SaveDC(hdc);
  38. SetGraphicsMode(hdc, GM_ADVANCED);
  39. ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
  40. SetViewportOrgEx(hdc, 0, 0, NULL);
  41. SetWindowOrgEx(hdc, 0, 0, NULL);
  42. if (fLogRes)
  43. {
  44. cxDpi = cast(FLOAT)GetDeviceCaps(hdc, LOGPIXELSX);
  45. cyDpi = cast(FLOAT)GetDeviceCaps(hdc, LOGPIXELSY);
  46. }
  47. else
  48. {
  49. cxDpi = cast(FLOAT)(25.4 * GetDeviceCaps(hdc, HORZRES) /
  50. GetDeviceCaps(hdc, HORZSIZE));
  51. cyDpi = cast(FLOAT)(25.4 * GetDeviceCaps(hdc, VERTRES) /
  52. GetDeviceCaps(hdc, VERTSIZE));
  53. }
  54. pt.x = cast(int)(iDeciPtWidth * cxDpi / 72);
  55. pt.y = cast(int)(iDeciPtHeight * cyDpi / 72);
  56. DPtoLP(hdc, &pt, 1);
  57. lf.lfHeight = -cast(int)(fabs(cast(float)pt.y) / 10.0 + 0.5);
  58. lf.lfWidth = 0;
  59. lf.lfEscapement = 0;
  60. lf.lfOrientation = 0;
  61. lf.lfWeight = iAttributes & EZ_ATTR_BOLD ? 700 : 0;
  62. lf.lfItalic = iAttributes & EZ_ATTR_ITALIC ? 1 : 0;
  63. lf.lfUnderline = iAttributes & EZ_ATTR_UNDERLINE ? 1 : 0;
  64. lf.lfStrikeOut = iAttributes & EZ_ATTR_STRIKEOUT ? 1 : 0;
  65. lf.lfCharSet = DEFAULT_CHARSET;
  66. lf.lfOutPrecision = 0;
  67. lf.lfClipPrecision = 0;
  68. lf.lfQuality = 0;
  69. lf.lfPitchAndFamily = 0;
  70. lf.lfFaceName[] = ' ';
  71. // unsure about this, investigate
  72. lf.lfFaceName[0..szFaceName.length] = szFaceName.toUTF16;
  73. hFont = CreateFontIndirect(&lf);
  74. if (iDeciPtWidth != 0)
  75. {
  76. hFont = cast(HFONT)SelectObject(hdc, hFont);
  77. GetTextMetrics(hdc, &tm);
  78. DeleteObject(SelectObject(hdc, hFont));
  79. lf.lfWidth = cast(int)(tm.tmAveCharWidth *
  80. fabs(cast(float)pt.x) / fabs(cast(float)pt.y) + 0.5);
  81. hFont = CreateFontIndirect(&lf);
  82. }
  83. RestoreDC(hdc, -1);
  84. return hFont;
  85. }