Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / gfx / src / thebes / nsSystemFontsMac.cpp
blobacb0d21d23c4050d16400cb86d96f403bd2a0a4e
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
13 * License.
15 * The Original Code is Mozilla Corporation code.
17 * The Initial Developer of the Original Code is Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include <Carbon/Carbon.h>
40 #include "nsSystemFontsMac.h"
42 nsSystemFontsMac::nsSystemFontsMac()
46 // helper function to get the system font for a specific script
47 #define FONTNAME_MAX_UNICHRS sizeof(fontName255) * 2
48 nsresult
49 GetSystemFontForScript(ThemeFontID aFontID, ScriptCode aScriptCode,
50 nsAFlatString& aFontName, SInt16& aFontSize,
51 Style& aFontStyle)
53 Str255 fontName255;
54 ::GetThemeFont(aFontID, aScriptCode, fontName255, &aFontSize, &aFontStyle);
55 if (fontName255[0] == 255) {
56 NS_WARNING("Too long fong name (> 254 chrs)");
57 return NS_ERROR_FAILURE;
59 fontName255[fontName255[0]+1] = 0;
61 OSStatus err;
63 // the theme font could contains font name in different encoding.
64 // we need to covert them to unicode according to the font's text encoding.
65 TECObjectRef converter = 0;
66 TextEncoding unicodeEncoding =
67 ::CreateTextEncoding(kTextEncodingUnicodeDefault,
68 kTextEncodingDefaultVariant,
69 kTextEncodingDefaultFormat);
71 FMFontFamily fontFamily;
72 TextEncoding fontEncoding = 0;
73 fontFamily = ::FMGetFontFamilyFromName(fontName255);
75 err = ::FMGetFontFamilyTextEncoding(fontFamily, &fontEncoding);
76 if (err != noErr) {
77 NS_WARNING("Could not get the encoding for the font.");
78 return NS_ERROR_FAILURE;
81 err = ::TECCreateConverter(&converter, fontEncoding, unicodeEncoding);
82 if (err != noErr) {
83 NS_WARNING("Could not create the converter.");
84 return NS_ERROR_FAILURE;
87 PRUnichar unicodeFontName[FONTNAME_MAX_UNICHRS + 1];
88 ByteCount actualInputLength, actualOutputLength;
89 err = ::TECConvertText(converter, &fontName255[1], fontName255[0],
90 &actualInputLength,
91 (TextPtr)unicodeFontName,
92 FONTNAME_MAX_UNICHRS * sizeof(PRUnichar),
93 &actualOutputLength);
95 if (err != noErr) {
96 NS_WARNING("Could not convert the font name.");
97 return NS_ERROR_FAILURE;
100 ::TECDisposeConverter(converter);
102 unicodeFontName[actualOutputLength / sizeof(PRUnichar)] = PRUnichar('\0');
103 aFontName = nsDependentString(unicodeFontName);
104 return NS_OK;
107 nsresult
108 nsSystemFontsMac::GetSystemFont(nsSystemFontID aID, nsString *aFontName,
109 gfxFontStyle *aFontStyle) const
111 nsresult rv;
113 // hack for now
114 if (aID == eSystemFont_Window ||
115 aID == eSystemFont_Document)
117 aFontStyle->style = FONT_STYLE_NORMAL;
118 aFontStyle->weight = FONT_WEIGHT_NORMAL;
120 aFontName->AssignLiteral("sans-serif");
121 aFontStyle->size = 14;
122 aFontStyle->systemFont = PR_TRUE;
124 return NS_OK;
127 ThemeFontID fontID = kThemeViewsFont;
128 switch (aID) {
129 // css2
130 case eSystemFont_Caption: fontID = kThemeSystemFont; break;
131 case eSystemFont_Icon: fontID = kThemeViewsFont; break;
132 case eSystemFont_Menu: fontID = kThemeSystemFont; break;
133 case eSystemFont_MessageBox: fontID = kThemeSmallSystemFont; break;
134 case eSystemFont_SmallCaption: fontID = kThemeSmallEmphasizedSystemFont; break;
135 case eSystemFont_StatusBar: fontID = kThemeSmallSystemFont; break;
136 // css3
137 //case eSystemFont_Window: = 'sans-serif'
138 //case eSystemFont_Document: = 'sans-serif'
139 case eSystemFont_Workspace: fontID = kThemeViewsFont; break;
140 case eSystemFont_Desktop: fontID = kThemeViewsFont; break;
141 case eSystemFont_Info: fontID = kThemeViewsFont; break;
142 case eSystemFont_Dialog: fontID = kThemeSystemFont; break;
143 case eSystemFont_Button: fontID = kThemeSmallSystemFont; break;
144 case eSystemFont_PullDownMenu: fontID = kThemeMenuItemFont; break;
145 case eSystemFont_List: fontID = kThemeSmallSystemFont; break;
146 case eSystemFont_Field: fontID = kThemeSmallSystemFont; break;
147 // moz
148 case eSystemFont_Tooltips: fontID = kThemeSmallSystemFont; break;
149 case eSystemFont_Widget: fontID = kThemeSmallSystemFont; break;
150 default:
151 // should never hit this
152 return NS_ERROR_FAILURE;
155 nsAutoString fontName;
156 SInt16 fontSize;
157 Style fontStyle;
159 ScriptCode sysScript = GetScriptManagerVariable(smSysScript);
160 rv = GetSystemFontForScript(fontID, smRoman, fontName, fontSize, fontStyle);
161 if (NS_FAILED(rv))
162 fontName = NS_LITERAL_STRING("Lucida Grande");
164 if (sysScript != smRoman) {
165 SInt16 localFontSize;
166 Style localFontStyle;
167 nsAutoString localSysFontName;
168 rv = GetSystemFontForScript(fontID, sysScript,
169 localSysFontName,
170 localFontSize, localFontStyle);
171 if (NS_SUCCEEDED(rv) && !fontName.Equals(localSysFontName)) {
172 fontName += NS_LITERAL_STRING(",") + localSysFontName;
173 fontSize = localFontSize;
174 fontStyle = localFontStyle;
178 *aFontName = fontName;
179 aFontStyle->size = gfxFloat(fontSize);
181 if (fontStyle & bold)
182 aFontStyle->weight = FONT_WEIGHT_BOLD;
183 if (fontStyle & italic)
184 aFontStyle->style = FONT_STYLE_ITALIC;
186 aFontStyle->systemFont = PR_TRUE;
188 return NS_OK;