/binding/win32/gdiplusfont.d

http://github.com/wilkie/djehuty · D · 251 lines · 176 code · 49 blank · 26 comment · 18 complexity · 685b3d2ef4dc00e7e654fd6801c3d865 MD5 · raw file

  1. /*
  2. * gdiplusfont.d
  3. *
  4. * This module implements GdiPlusFont.h for D. The original
  5. * copyright info is given below.
  6. *
  7. * Author: Dave Wilkinson
  8. * Originated: November 25th, 2009
  9. *
  10. */
  11. module binding.win32.gdiplusfont;
  12. import binding.win32.windef;
  13. import binding.win32.winbase;
  14. import binding.win32.winnt;
  15. import binding.win32.wingdi;
  16. import binding.win32.guiddef;
  17. import binding.win32.gdiplusbase;
  18. import binding.win32.gdiplustypes;
  19. import binding.win32.gdiplusenums;
  20. import binding.win32.gdipluspixelformats;
  21. import binding.win32.gdiplusgpstubs;
  22. import binding.win32.gdiplusmetaheader;
  23. import binding.win32.gdipluspixelformats;
  24. import binding.win32.gdipluscolor;
  25. import binding.win32.gdipluscolormatrix;
  26. import binding.win32.gdiplusflat;
  27. import binding.win32.gdiplusimaging;
  28. import binding.win32.gdiplusbitmap;
  29. import binding.win32.gdiplusimageattributes;
  30. import binding.win32.gdiplusmatrix;
  31. import binding.win32.gdiplusfontfamily;
  32. import binding.win32.gdiplusfontcollection;
  33. import binding.win32.gdiplusgraphics;
  34. /**************************************************************************\
  35. *
  36. * Copyright (c) 1998-2001, Microsoft Corp. All Rights Reserved.
  37. *
  38. * Module Name:
  39. *
  40. * GdiplusFont.h
  41. *
  42. * Abstract:
  43. *
  44. * GDI+ Font class
  45. *
  46. \**************************************************************************/
  47. //--------------------------------------------------------------------------
  48. // Font
  49. //--------------------------------------------------------------------------
  50. class Font : GdiplusBase {
  51. this(in HDC hdc) {
  52. GpFont *font = null;
  53. lastResult = GdipCreateFontFromDC(hdc, &font);
  54. SetNativeFont(font);
  55. }
  56. this(in HDC hdc, in LOGFONTA* logfont) {
  57. GpFont *font = null;
  58. if (logfont) {
  59. lastResult = GdipCreateFontFromLogfontA(hdc, logfont, &font);
  60. }
  61. else {
  62. lastResult = GdipCreateFontFromDC(hdc, &font);
  63. }
  64. SetNativeFont(font);
  65. }
  66. this(in HDC hdc, in LOGFONTW* logfont) {
  67. GpFont *font = null;
  68. if (logfont) {
  69. lastResult = GdipCreateFontFromLogfontW(hdc, logfont, &font);
  70. }
  71. else {
  72. lastResult = GdipCreateFontFromDC(hdc, &font);
  73. }
  74. SetNativeFont(font);
  75. }
  76. this(in HDC hdc, in HFONT hfont) {
  77. GpFont *font = null;
  78. if (hfont) {
  79. LOGFONTA lf;
  80. if(GetObjectA(hfont, LOGFONTA.sizeof, &lf))
  81. lastResult = GdipCreateFontFromLogfontA(hdc, &lf, &font);
  82. else
  83. lastResult = GdipCreateFontFromDC(hdc, &font);
  84. }
  85. else {
  86. lastResult = GdipCreateFontFromDC(hdc, &font);
  87. }
  88. SetNativeFont(font);
  89. }
  90. this(in FontFamily family, in REAL emSize, in INT style = FontStyle.FontStyleRegular, in Unit unit = Unit.UnitPoint) {
  91. GpFont *font = null;
  92. lastResult = GdipCreateFont(family ? family.nativeFamily : null,
  93. emSize,
  94. style,
  95. unit,
  96. &font);
  97. SetNativeFont(font);
  98. }
  99. this(in WCHAR* familyName, in REAL emSize, in INT style = FontStyle.FontStyleRegular, in Unit unit = Unit.UnitPoint, in FontCollection fontCollection = null) {
  100. nativeFont = null;
  101. FontFamily family = new FontFamily(familyName, fontCollection);
  102. GpFontFamily *nativeFamily = family.nativeFamily;
  103. lastResult = family.GetLastStatus();
  104. if (lastResult != Status.Ok) {
  105. nativeFamily = FontFamily.GenericSansSerif().nativeFamily;
  106. lastResult = FontFamily.GenericSansSerif().lastResult;
  107. if (lastResult != Status.Ok)
  108. return;
  109. }
  110. lastResult = GdipCreateFont(nativeFamily,
  111. emSize,
  112. style,
  113. unit,
  114. &nativeFont);
  115. if (lastResult != Status.Ok) {
  116. nativeFamily = FontFamily.GenericSansSerif().nativeFamily;
  117. lastResult = FontFamily.GenericSansSerif().lastResult;
  118. if (lastResult != Status.Ok)
  119. return;
  120. lastResult = GdipCreateFont(
  121. nativeFamily,
  122. emSize,
  123. style,
  124. unit,
  125. &nativeFont);
  126. }
  127. }
  128. Status GetLogFontA(in Graphics g, LOGFONTA * logfontA) {
  129. return SetStatus(GdipGetLogFontA(nativeFont, g ? g.nativeGraphics : null, logfontA));
  130. }
  131. Status GetLogFontW(in Graphics g, LOGFONTW * logfontW) {
  132. return SetStatus(GdipGetLogFontW(nativeFont, g ? g.nativeGraphics : null, logfontW));
  133. }
  134. Font Clone() {
  135. GpFont *cloneFont = null;
  136. SetStatus(GdipCloneFont(nativeFont, &cloneFont));
  137. return new Font(cloneFont, lastResult);
  138. }
  139. alias Clone dup;
  140. ~this() {
  141. GdipDeleteFont(nativeFont);
  142. }
  143. BOOL IsAvailable() {
  144. return (nativeFont ? TRUE : FALSE);
  145. }
  146. INT GetStyle() {
  147. INT style;
  148. SetStatus(GdipGetFontStyle(nativeFont, &style));
  149. return style;
  150. }
  151. REAL GetSize() {
  152. REAL size;
  153. SetStatus(GdipGetFontSize(nativeFont, &size));
  154. return size;
  155. }
  156. Unit GetUnit() {
  157. Unit unit;
  158. SetStatus(GdipGetFontUnit(nativeFont, &unit));
  159. return unit;
  160. }
  161. Status GetLastStatus() {
  162. return lastResult;
  163. }
  164. REAL GetHeight(in Graphics graphics) {
  165. REAL height;
  166. SetStatus(GdipGetFontHeight(
  167. nativeFont,
  168. graphics ? graphics.nativeGraphics : null,
  169. &height
  170. ));
  171. return height;
  172. }
  173. REAL GetHeight(in REAL dpi) {
  174. REAL height;
  175. SetStatus(GdipGetFontHeightGivenDPI(nativeFont, dpi, &height));
  176. return height;
  177. }
  178. Status GetFamily(FontFamily family) {
  179. if (family is null) {
  180. return SetStatus(Status.InvalidParameter);
  181. }
  182. Status status = GdipGetFamily(nativeFont, &(family.nativeFamily));
  183. family.SetStatus(status);
  184. return SetStatus(status);
  185. }
  186. protected:
  187. this(GpFont* font, Status status) {
  188. lastResult = status;
  189. SetNativeFont(font);
  190. }
  191. VOID SetNativeFont(GpFont *Font) {
  192. nativeFont = Font;
  193. }
  194. Status SetStatus(Status status) {
  195. if (status != Status.Ok)
  196. return (lastResult = status);
  197. else
  198. return status;
  199. }
  200. package GpFont* nativeFont;
  201. package Status lastResult;
  202. }