1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is thebes gfx
17 * The Initial Developer of the Original Code is mozilla.org.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * Vladimir Vukicevic <vladimir@pobox.com>
23 * Stuart Parmenter <pavlov@pavlov.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsIRenderingContext.h"
41 #include "gfxWindowsSurface.h"
43 #include "nsSystemFontsWin.h"
46 nsresult
nsSystemFontsWin::CopyLogFontToNSFont(HDC
* aHDC
, const LOGFONTW
* ptrLogFont
,
48 gfxFontStyle
*aFontStyle
) const
50 PRUnichar name
[LF_FACESIZE
];
52 memcpy(name
, ptrLogFont
->lfFaceName
, LF_FACESIZE
*sizeof(PRUnichar
));
56 aFontStyle
->style
= FONT_STYLE_NORMAL
;
57 if (ptrLogFont
->lfItalic
)
59 aFontStyle
->style
= FONT_STYLE_ITALIC
;
61 // XXX What about oblique?
64 aFontStyle
->weight
= (ptrLogFont
->lfWeight
== FW_BOLD
?
65 FONT_WEIGHT_BOLD
: FONT_WEIGHT_NORMAL
);
67 // XXX mPixelScale is currently hardcoded to 1 in thebes gfx...
68 float mPixelScale
= 1.0f
;
71 // The lfHeight is in pixel and it needs to be adjusted for the
72 // device it will be "displayed" on
73 // Screens and Printers will differe in DPI
75 // So this accounts for the difference in the DeviceContexts
76 // The mPixelScale will be a "1" for the screen and could be
77 // any value when going to a printer, for example mPixleScale is
78 // 6.25 when going to a 600dpi printer.
79 // round, but take into account whether it is negative
80 float pixelHeight
= -ptrLogFont
->lfHeight
;
81 if (pixelHeight
< 0) {
82 HFONT hFont
= ::CreateFontIndirectW(ptrLogFont
);
84 return NS_ERROR_OUT_OF_MEMORY
;
85 HGDIOBJ hObject
= ::SelectObject(*aHDC
, hFont
);
87 ::GetTextMetrics(*aHDC
, &tm
);
88 ::SelectObject(*aHDC
, hObject
);
89 ::DeleteObject(hFont
);
90 pixelHeight
= tm
.tmAscent
;
93 pixelHeight
*= mPixelScale
;
95 // we have problem on Simplified Chinese system because the system
96 // report the default font size is 8 points. but if we use 8, the text
97 // display very ugly. force it to be at 9 points (12 pixels) on that
98 // system (cp936), but leave other sizes alone.
99 if ((pixelHeight
< 12) &&
103 aFontStyle
->size
= pixelHeight
;
107 nsresult
nsSystemFontsWin::GetSysFontInfo(HDC aHDC
, nsSystemFontID anID
,
109 gfxFontStyle
*aFontStyle
) const
114 LOGFONTW
* ptrLogFont
= NULL
;
117 hGDI
= ::GetStockObject(SYSTEM_FONT
);
119 return NS_ERROR_UNEXPECTED
;
121 if (::GetObjectW(hGDI
, sizeof(logFont
), &logFont
) > 0)
122 ptrLogFont
= &logFont
;
125 NONCLIENTMETRICSW ncm
;
128 if (anID
== eSystemFont_Icon
)
130 status
= ::SystemParametersInfoW(SPI_GETICONTITLELOGFONT
,
137 ncm
.cbSize
= sizeof(NONCLIENTMETRICSW
);
138 status
= ::SystemParametersInfoW(SPI_GETNONCLIENTMETRICS
,
146 return NS_ERROR_FAILURE
;
151 // Caption in CSS is NOT the same as Caption on Windows
152 //case eSystemFont_Caption:
153 // ptrLogFont = &ncm.lfCaptionFont;
156 case eSystemFont_Icon
:
157 ptrLogFont
= &logFont
;
160 case eSystemFont_Menu
:
161 ptrLogFont
= &ncm
.lfMenuFont
;
164 case eSystemFont_MessageBox
:
165 ptrLogFont
= &ncm
.lfMessageFont
;
168 case eSystemFont_SmallCaption
:
169 ptrLogFont
= &ncm
.lfSmCaptionFont
;
172 case eSystemFont_StatusBar
:
173 case eSystemFont_Tooltips
:
174 ptrLogFont
= &ncm
.lfStatusFont
;
177 case eSystemFont_Widget
:
179 case eSystemFont_Window
: // css3
180 case eSystemFont_Document
:
181 case eSystemFont_Workspace
:
182 case eSystemFont_Desktop
:
183 case eSystemFont_Info
:
184 case eSystemFont_Dialog
:
185 case eSystemFont_Button
:
186 case eSystemFont_PullDownMenu
:
187 case eSystemFont_List
:
188 case eSystemFont_Field
:
189 case eSystemFont_Caption
:
190 hGDI
= ::GetStockObject(DEFAULT_GUI_FONT
);
193 if (::GetObjectW(hGDI
, sizeof(logFont
), &logFont
) > 0)
195 ptrLogFont
= &logFont
;
203 if (nsnull
== ptrLogFont
)
205 return NS_ERROR_FAILURE
;
208 aFontStyle
->systemFont
= PR_TRUE
;
210 return CopyLogFontToNSFont(&aHDC
, ptrLogFont
, aFontName
, aFontStyle
);
213 nsresult
nsSystemFontsWin::GetSystemFont(nsSystemFontID anID
,
215 gfxFontStyle
*aFontStyle
) const
217 nsresult status
= NS_OK
;
220 case eSystemFont_Caption
:
221 case eSystemFont_Icon
:
222 case eSystemFont_Menu
:
223 case eSystemFont_MessageBox
:
224 case eSystemFont_SmallCaption
:
225 case eSystemFont_StatusBar
:
226 case eSystemFont_Tooltips
:
227 case eSystemFont_Widget
:
229 case eSystemFont_Window
: // css3
230 case eSystemFont_Document
:
231 case eSystemFont_Workspace
:
232 case eSystemFont_Desktop
:
233 case eSystemFont_Info
:
234 case eSystemFont_Dialog
:
235 case eSystemFont_Button
:
236 case eSystemFont_PullDownMenu
:
237 case eSystemFont_List
:
238 case eSystemFont_Field
:
241 HDC tdc
= GetDC(hwnd
);
243 status
= GetSysFontInfo(tdc
, anID
, aFontName
, aFontStyle
);
245 ReleaseDC(hwnd
, tdc
);
254 nsSystemFontsWin::nsSystemFontsWin()