Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / gfx / src / thebes / nsSystemFontsGTK2.cpp
blob624f9ca8d8fa8e6a464d6b4c057c502bfd12553f
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 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.
21 * Contributor(s):
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 // for strtod()
40 #include <stdlib.h>
42 #include "nsIDeviceContext.h"
43 #include "nsIRenderingContext.h"
44 #include "prlink.h"
46 #include <gtk/gtk.h>
47 #include <gdk/gdk.h>
49 #ifdef MOZ_PANGO
50 #include <pango/pango.h>
51 #include <pango/pango-fontmap.h>
52 #endif
54 #include <fontconfig/fontconfig.h>
55 #include "nsSystemFontsGTK2.h"
56 #include "gfxPlatformGtk.h"
58 // Glue to avoid build/runtime dependencies on Pango > 1.6
59 #if defined(MOZ_PANGO) && !defined(THEBES_USE_PANGO_CAIRO)
60 static gboolean
61 (* PTR_pango_font_description_get_size_is_absolute)(PangoFontDescription*)
62 = nsnull;
64 static void InitPangoLib()
66 static PRBool initialized = PR_FALSE;
67 if (initialized)
68 return;
69 initialized = PR_TRUE;
71 PRLibrary *pangoLib = nsnull;
72 PTR_pango_font_description_get_size_is_absolute =
73 (gboolean (*)(PangoFontDescription*))
74 PR_FindFunctionSymbolAndLibrary("pango_font_description_get_size_is_absolute",
75 &pangoLib);
76 if (pangoLib)
77 PR_UnloadLibrary(pangoLib);
80 static void
81 ShutdownPangoLib()
85 static gboolean
86 MOZ_pango_font_description_get_size_is_absolute(PangoFontDescription *desc)
88 if (PTR_pango_font_description_get_size_is_absolute) {
89 return PTR_pango_font_description_get_size_is_absolute(desc);
92 // In old versions of pango, this was always false.
93 return PR_FALSE;
95 #else
96 static inline void InitPangoLib()
100 static inline void ShutdownPangoLib()
104 #ifdef MOZ_PANGO
105 static inline gboolean
106 MOZ_pango_font_description_get_size_is_absolute(PangoFontDescription *desc)
108 pango_font_description_get_size_is_absolute(desc);
110 #endif
111 #endif
113 nsSystemFontsGTK2::nsSystemFontsGTK2()
114 : mDefaultFontName(NS_LITERAL_STRING("sans-serif"))
115 , mButtonFontName(NS_LITERAL_STRING("sans-serif"))
116 , mFieldFontName(NS_LITERAL_STRING("sans-serif"))
117 , mMenuFontName(NS_LITERAL_STRING("sans-serif"))
119 InitPangoLib();
122 * Much of the widget creation code here is similar to the code in
123 * nsLookAndFeel::InitColors().
126 // mDefaultFont
127 GtkWidget *label = gtk_label_new("M");
128 GtkWidget *parent = gtk_fixed_new();
129 GtkWidget *window = gtk_window_new(GTK_WINDOW_POPUP);
131 gtk_container_add(GTK_CONTAINER(parent), label);
132 gtk_container_add(GTK_CONTAINER(window), parent);
134 gtk_widget_ensure_style(label);
136 GetSystemFontInfo(label, &mDefaultFontName, &mDefaultFontStyle);
138 gtk_widget_destroy(window); // no unref, windows are different
140 // mFieldFont
141 GtkWidget *entry = gtk_entry_new();
142 parent = gtk_fixed_new();
143 window = gtk_window_new(GTK_WINDOW_POPUP);
145 gtk_container_add(GTK_CONTAINER(parent), entry);
146 gtk_container_add(GTK_CONTAINER(window), parent);
147 gtk_widget_ensure_style(entry);
149 GetSystemFontInfo(entry, &mFieldFontName, &mFieldFontStyle);
151 gtk_widget_destroy(window); // no unref, windows are different
153 // mMenuFont
154 GtkWidget *accel_label = gtk_accel_label_new("M");
155 GtkWidget *menuitem = gtk_menu_item_new();
156 GtkWidget *menu = gtk_menu_new();
157 gtk_object_ref(GTK_OBJECT(menu));
158 gtk_object_sink(GTK_OBJECT(menu));
160 gtk_container_add(GTK_CONTAINER(menuitem), accel_label);
161 gtk_menu_append(GTK_MENU(menu), menuitem);
163 gtk_widget_ensure_style(accel_label);
165 GetSystemFontInfo(accel_label, &mMenuFontName, &mMenuFontStyle);
167 gtk_widget_unref(menu);
169 // mButtonFont
170 parent = gtk_fixed_new();
171 GtkWidget *button = gtk_button_new();
172 label = gtk_label_new("M");
173 window = gtk_window_new(GTK_WINDOW_POPUP);
175 gtk_container_add(GTK_CONTAINER(button), label);
176 gtk_container_add(GTK_CONTAINER(parent), button);
177 gtk_container_add(GTK_CONTAINER(window), parent);
179 gtk_widget_ensure_style(label);
181 GetSystemFontInfo(label, &mButtonFontName, &mButtonFontStyle);
183 gtk_widget_destroy(window); // no unref, windows are different
186 nsSystemFontsGTK2::~nsSystemFontsGTK2()
188 ShutdownPangoLib();
191 nsresult
192 nsSystemFontsGTK2::GetSystemFontInfo(GtkWidget *aWidget, nsString *aFontName,
193 gfxFontStyle *aFontStyle) const
195 #ifdef MOZ_PANGO
196 GtkSettings *settings = gtk_widget_get_settings(aWidget);
198 aFontStyle->style = FONT_STYLE_NORMAL;
200 gchar *fontname;
201 g_object_get(settings, "gtk-font-name", &fontname, NULL);
203 PangoFontDescription *desc;
204 desc = pango_font_description_from_string(fontname);
206 aFontStyle->systemFont = PR_TRUE;
208 g_free(fontname);
210 NS_NAMED_LITERAL_STRING(quote, "\"");
211 NS_ConvertUTF8toUTF16 family(pango_font_description_get_family(desc));
212 *aFontName = quote + family + quote;
214 aFontStyle->weight = pango_font_description_get_weight(desc);
216 float size = float(pango_font_description_get_size(desc) / PANGO_SCALE);
218 // |size| is now either pixels or pango-points (not Mozilla-points!)
220 if (!MOZ_pango_font_description_get_size_is_absolute(desc)) {
221 // |size| is in pango-points, so convert to pixels.
222 size *= gfxPlatformGtk::DPI() / POINTS_PER_INCH_FLOAT;
225 // |size| is now pixels
227 aFontStyle->size = size;
229 pango_font_description_free(desc);
231 #else
232 /* FIXME: DFB FT2 Hardcoding the system font info for now.. */
233 aFontStyle->style = FONT_STYLE_NORMAL;
234 aFontStyle->systemFont = PR_TRUE;
236 NS_NAMED_LITERAL_STRING(fontname, "\"Sans\"");
237 *aFontName = fontname;
238 aFontStyle->weight = 400;
239 aFontStyle->size = 40/3;
241 #endif
243 return NS_OK;
246 nsresult
247 nsSystemFontsGTK2::GetSystemFont(nsSystemFontID anID, nsString *aFontName,
248 gfxFontStyle *aFontStyle) const
250 switch (anID) {
251 case eSystemFont_Menu: // css2
252 case eSystemFont_PullDownMenu: // css3
253 *aFontName = mMenuFontName;
254 *aFontStyle = mMenuFontStyle;
255 break;
257 case eSystemFont_Field: // css3
258 case eSystemFont_List: // css3
259 *aFontName = mFieldFontName;
260 *aFontStyle = mFieldFontStyle;
261 break;
263 case eSystemFont_Button: // css3
264 *aFontName = mButtonFontName;
265 *aFontStyle = mButtonFontStyle;
266 break;
268 case eSystemFont_Caption: // css2
269 case eSystemFont_Icon: // css2
270 case eSystemFont_MessageBox: // css2
271 case eSystemFont_SmallCaption: // css2
272 case eSystemFont_StatusBar: // css2
273 case eSystemFont_Window: // css3
274 case eSystemFont_Document: // css3
275 case eSystemFont_Workspace: // css3
276 case eSystemFont_Desktop: // css3
277 case eSystemFont_Info: // css3
278 case eSystemFont_Dialog: // css3
279 case eSystemFont_Tooltips: // moz
280 case eSystemFont_Widget: // moz
281 *aFontName = mDefaultFontName;
282 *aFontStyle = mDefaultFontStyle;
283 break;
286 return NS_OK;