PageRenderTime 118ms CodeModel.GetById 85ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

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