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 ***** */
42 #include "nsIDeviceContext.h"
43 #include "nsIRenderingContext.h"
50 #include <pango/pango.h>
51 #include <pango/pangox.h>
52 #include <pango/pango-fontmap.h>
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 #ifndef THEBES_USE_PANGO_CAIRO
61 (* PTR_pango_font_description_get_size_is_absolute
)(PangoFontDescription
*)
64 static void InitPangoLib()
66 static PRBool initialized
= PR_FALSE
;
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",
77 PR_UnloadLibrary(pangoLib
);
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.
96 static inline void InitPangoLib()
100 static inline void ShutdownPangoLib()
104 static inline gboolean
105 MOZ_pango_font_description_get_size_is_absolute(PangoFontDescription
*desc
)
107 pango_font_description_get_size_is_absolute(desc
);
111 nsSystemFontsGTK2::nsSystemFontsGTK2()
112 : mDefaultFontName(NS_LITERAL_STRING("sans-serif"))
113 , mButtonFontName(NS_LITERAL_STRING("sans-serif"))
114 , mFieldFontName(NS_LITERAL_STRING("sans-serif"))
115 , mMenuFontName(NS_LITERAL_STRING("sans-serif"))
120 * Much of the widget creation code here is similar to the code in
121 * nsLookAndFeel::InitColors().
125 GtkWidget
*label
= gtk_label_new("M");
126 GtkWidget
*parent
= gtk_fixed_new();
127 GtkWidget
*window
= gtk_window_new(GTK_WINDOW_POPUP
);
129 gtk_container_add(GTK_CONTAINER(parent
), label
);
130 gtk_container_add(GTK_CONTAINER(window
), parent
);
132 gtk_widget_ensure_style(label
);
134 GetSystemFontInfo(label
, &mDefaultFontName
, &mDefaultFontStyle
);
136 gtk_widget_destroy(window
); // no unref, windows are different
139 GtkWidget
*entry
= gtk_entry_new();
140 parent
= gtk_fixed_new();
141 window
= gtk_window_new(GTK_WINDOW_POPUP
);
143 gtk_container_add(GTK_CONTAINER(parent
), entry
);
144 gtk_container_add(GTK_CONTAINER(window
), parent
);
145 gtk_widget_ensure_style(entry
);
147 GetSystemFontInfo(entry
, &mFieldFontName
, &mFieldFontStyle
);
149 gtk_widget_destroy(window
); // no unref, windows are different
152 GtkWidget
*accel_label
= gtk_accel_label_new("M");
153 GtkWidget
*menuitem
= gtk_menu_item_new();
154 GtkWidget
*menu
= gtk_menu_new();
155 gtk_object_ref(GTK_OBJECT(menu
));
156 gtk_object_sink(GTK_OBJECT(menu
));
158 gtk_container_add(GTK_CONTAINER(menuitem
), accel_label
);
159 gtk_menu_append(GTK_MENU(menu
), menuitem
);
161 gtk_widget_ensure_style(accel_label
);
163 GetSystemFontInfo(accel_label
, &mMenuFontName
, &mMenuFontStyle
);
165 gtk_widget_unref(menu
);
168 parent
= gtk_fixed_new();
169 GtkWidget
*button
= gtk_button_new();
170 label
= gtk_label_new("M");
171 window
= gtk_window_new(GTK_WINDOW_POPUP
);
173 gtk_container_add(GTK_CONTAINER(button
), label
);
174 gtk_container_add(GTK_CONTAINER(parent
), button
);
175 gtk_container_add(GTK_CONTAINER(window
), parent
);
177 gtk_widget_ensure_style(label
);
179 GetSystemFontInfo(label
, &mButtonFontName
, &mButtonFontStyle
);
181 gtk_widget_destroy(window
); // no unref, windows are different
184 nsSystemFontsGTK2::~nsSystemFontsGTK2()
190 nsSystemFontsGTK2::GetSystemFontInfo(GtkWidget
*aWidget
, nsString
*aFontName
,
191 gfxFontStyle
*aFontStyle
) const
193 GtkSettings
*settings
= gtk_widget_get_settings(aWidget
);
195 aFontStyle
->style
= FONT_STYLE_NORMAL
;
198 g_object_get(settings
, "gtk-font-name", &fontname
, NULL
);
200 PangoFontDescription
*desc
;
201 desc
= pango_font_description_from_string(fontname
);
203 aFontStyle
->systemFont
= PR_TRUE
;
207 NS_NAMED_LITERAL_STRING(quote
, "\"");
208 NS_ConvertUTF8toUTF16
family(pango_font_description_get_family(desc
));
209 *aFontName
= quote
+ family
+ quote
;
211 aFontStyle
->weight
= pango_font_description_get_weight(desc
);
213 float size
= float(pango_font_description_get_size(desc
) / PANGO_SCALE
);
215 // |size| is now either pixels or pango-points (not Mozilla-points!)
217 if (!MOZ_pango_font_description_get_size_is_absolute(desc
)) {
218 // |size| is in pango-points, so convert to pixels.
219 size
*= float(gfxPlatformGtk::DPI()) / POINTS_PER_INCH_FLOAT
;
222 // |size| is now pixels
224 aFontStyle
->size
= size
;
226 pango_font_description_free(desc
);
232 nsSystemFontsGTK2::GetSystemFont(nsSystemFontID anID
, nsString
*aFontName
,
233 gfxFontStyle
*aFontStyle
) const
236 case eSystemFont_Menu
: // css2
237 case eSystemFont_PullDownMenu
: // css3
238 *aFontName
= mMenuFontName
;
239 *aFontStyle
= mMenuFontStyle
;
242 case eSystemFont_Field
: // css3
243 case eSystemFont_List
: // css3
244 *aFontName
= mFieldFontName
;
245 *aFontStyle
= mFieldFontStyle
;
248 case eSystemFont_Button
: // css3
249 *aFontName
= mButtonFontName
;
250 *aFontStyle
= mButtonFontStyle
;
253 case eSystemFont_Caption
: // css2
254 case eSystemFont_Icon
: // css2
255 case eSystemFont_MessageBox
: // css2
256 case eSystemFont_SmallCaption
: // css2
257 case eSystemFont_StatusBar
: // css2
258 case eSystemFont_Window
: // css3
259 case eSystemFont_Document
: // css3
260 case eSystemFont_Workspace
: // css3
261 case eSystemFont_Desktop
: // css3
262 case eSystemFont_Info
: // css3
263 case eSystemFont_Dialog
: // css3
264 case eSystemFont_Tooltips
: // moz
265 case eSystemFont_Widget
: // moz
266 *aFontName
= mDefaultFontName
;
267 *aFontStyle
= mDefaultFontStyle
;