| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Behdad Esfahbod <[email protected]> |
| 4 |
* Copyright (C) 2008 Richard Hughes <[email protected]> |
| 5 |
* |
| 6 |
* Licensed under the GNU General Public License Version 2 |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#define G_LOG_DOMAIN "PkGtkModule" |
| 24 |
|
| 25 |
#define PANGO_ENABLE_BACKEND |
| 26 |
#include <fontconfig/fontconfig.h> |
| 27 |
#include <pango/pango.h> |
| 28 |
#include <pango/pangofc-fontmap.h> |
| 29 |
#include <pango/pangocairo.h> |
| 30 |
#include <gtk/gtk.h> |
| 31 |
#include <gio/gio.h> |
| 32 |
|
| 33 |
static gchar * |
| 34 |
pk_guess_application_id (void) |
| 35 |
{ |
| 36 |
GApplication *app; |
| 37 |
const gchar *app_id; |
| 38 |
|
| 39 |
app = g_application_get_default (); |
| 40 |
if (app == NULL) |
| 41 |
return NULL; |
| 42 |
|
| 43 |
app_id = g_application_get_application_id (app); |
| 44 |
if (app_id == NULL) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
return g_strconcat (app_id, ".desktop", NULL); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Invoke the PackageKit InstallFonts method over D-BUS |
| 52 |
**/ |
| 53 |
|
| 54 |
static void |
| 55 |
pk_install_fonts_method_finished_cb (GObject *source_object, |
| 56 |
GAsyncResult *res, |
| 57 |
gpointer user_data) |
| 58 |
{ |
| 59 |
GDBusProxy *proxy = G_DBUS_PROXY (source_object); |
| 60 |
g_autoptr(GError) error = NULL; |
| 61 |
g_autoptr(GVariant) value = NULL; |
| 62 |
|
| 63 |
value = g_dbus_proxy_call_finish (proxy, res, &error); |
| 64 |
if (value == NULL) { |
| 65 |
g_warning ("Error occurred during install: %s", error->message); |
| 66 |
return; |
| 67 |
} |
| 68 |
/* XXX Make gtk/pango reload fonts? */ |
| 69 |
} |
| 70 |
|
| 71 |
static GVariant * |
| 72 |
pk_make_platform_data (void) |
| 73 |
{ |
| 74 |
GVariantBuilder builder; |
| 75 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| 76 |
return g_variant_builder_end (&builder); |
| 77 |
} |
| 78 |
|
| 79 |
static GPtrArray *tags; |
| 80 |
|
| 81 |
static gboolean |
| 82 |
pk_install_fonts_idle_cb (gpointer data G_GNUC_UNUSED) |
| 83 |
{ |
| 84 |
g_autofree gchar *application_id = NULL; |
| 85 |
g_autoptr(GError) error = NULL; |
| 86 |
g_autoptr(GDBusProxy) proxy = NULL; |
| 87 |
g_auto(GStrv) font_tags = NULL; |
| 88 |
|
| 89 |
g_return_val_if_fail (tags->len > 0, FALSE); |
| 90 |
|
| 91 |
/* get a strv out of the array that we will then own */ |
| 92 |
g_ptr_array_add (tags, NULL); |
| 93 |
font_tags = (gchar **) g_ptr_array_free (tags, FALSE); |
| 94 |
tags = NULL; |
| 95 |
|
| 96 |
application_id = pk_guess_application_id (); |
| 97 |
|
| 98 |
/* get proxy */ |
| 99 |
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
| 100 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 101 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 102 |
NULL, |
| 103 |
"org.freedesktop.PackageKit", |
| 104 |
"/org/freedesktop/PackageKit", |
| 105 |
"org.freedesktop.PackageKit.Modify2", |
| 106 |
NULL, |
| 107 |
&error); |
| 108 |
if (proxy == NULL) { |
| 109 |
g_warning ("Error connecting to PK session instance: %s", error->message); |
| 110 |
return FALSE; |
| 111 |
} |
| 112 |
|
| 113 |
/* invoke the method */ |
| 114 |
g_dbus_proxy_call (proxy, |
| 115 |
"InstallFontconfigResources", |
| 116 |
g_variant_new ("(^a&sss@a{sv})", |
| 117 |
font_tags, |
| 118 |
"hide-finished", |
| 119 |
application_id ? application_id : "", |
| 120 |
pk_make_platform_data ()), |
| 121 |
G_DBUS_CALL_FLAGS_NONE, |
| 122 |
60 * 60 * 1000, /* 1 hour */ |
| 123 |
NULL, |
| 124 |
pk_install_fonts_method_finished_cb, |
| 125 |
NULL); |
| 126 |
|
| 127 |
g_debug ("InstallFontconfigResources method invoked"); |
| 128 |
return FALSE; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
queue_install_fonts_tag (const char *tag) |
| 133 |
{ |
| 134 |
guint idle_id; |
| 135 |
if (tags == NULL) { |
| 136 |
tags = g_ptr_array_new (); |
| 137 |
idle_id = g_idle_add (pk_install_fonts_idle_cb, NULL); |
| 138 |
g_source_set_name_by_id (idle_id, "[PkGtkModule] install fonts"); |
| 139 |
} |
| 140 |
|
| 141 |
g_debug ("Queue install of: %s", tag); |
| 142 |
g_ptr_array_add (tags, (gpointer) g_strdup (tag)); |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
pk_font_not_found (PangoLanguage *language) |
| 147 |
{ |
| 148 |
FcPattern *pat = NULL; |
| 149 |
gchar *tag = NULL; |
| 150 |
const gchar *lang; |
| 151 |
|
| 152 |
g_return_if_fail (language != NULL); |
| 153 |
|
| 154 |
/* convert to language */ |
| 155 |
lang = pango_language_to_string (language); |
| 156 |
if (lang == NULL || lang[0] == '\0') { |
| 157 |
g_warning ("failed to convert language to string"); |
| 158 |
goto out; |
| 159 |
} |
| 160 |
|
| 161 |
/* create the font tag used in as a package provides */ |
| 162 |
pat = FcPatternCreate (); |
| 163 |
FcPatternAddString (pat, FC_LANG, (FcChar8 *) lang); |
| 164 |
tag = (gchar *) FcNameUnparse (pat); |
| 165 |
if (tag == NULL || tag[0] == '\0') { |
| 166 |
g_warning ("failed to create font tag: %s", lang); |
| 167 |
goto out; |
| 168 |
} |
| 169 |
|
| 170 |
/* add to array for processing in idle callback */ |
| 171 |
queue_install_fonts_tag (tag); |
| 172 |
|
| 173 |
out: |
| 174 |
if (pat != NULL) |
| 175 |
FcPatternDestroy (pat); |
| 176 |
if (tag != NULL) |
| 177 |
free (tag); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* A PangoFcFontMap implementation that detects font-not-found events |
| 183 |
**/ |
| 184 |
|
| 185 |
|
| 186 |
typedef struct { |
| 187 |
PangoLanguage *language; |
| 188 |
gboolean found; |
| 189 |
} FontsetForeachClosure; |
| 190 |
|
| 191 |
static gboolean |
| 192 |
fontset_foreach_cb (PangoFontset *fontset G_GNUC_UNUSED, |
| 193 |
PangoFont *font, |
| 194 |
gpointer data) |
| 195 |
{ |
| 196 |
FontsetForeachClosure *closure = data; |
| 197 |
PangoFcFont *fcfont = PANGO_FC_FONT (font); |
| 198 |
const FcPattern *pattern = NULL; |
| 199 |
FcLangSet *langset = NULL; |
| 200 |
|
| 201 |
g_object_get (fcfont, "pattern", &pattern, NULL); |
| 202 |
|
| 203 |
/* old Pango version with non-readable pattern */ |
| 204 |
if (pattern == NULL) { |
| 205 |
g_warning ("Old Pango version with non-readable pattern. " |
| 206 |
"Skipping automatic missing-font installation."); |
| 207 |
return closure->found = TRUE; |
| 208 |
} |
| 209 |
|
| 210 |
if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) == FcResultMatch && |
| 211 |
FcLangGetCharSet ((FcChar8 *) closure->language) != NULL && |
| 212 |
FcLangSetHasLang (langset, (FcChar8 *) closure->language) != FcLangDifferentLang) |
| 213 |
closure->found = TRUE; |
| 214 |
|
| 215 |
return closure->found; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
static PangoFontset *(*pk_pango_fc_font_map_load_fontset_default) (PangoFontMap *font_map, |
| 220 |
PangoContext *context, |
| 221 |
const PangoFontDescription *desc, |
| 222 |
PangoLanguage *language); |
| 223 |
|
| 224 |
static PangoFontset * |
| 225 |
pk_pango_fc_font_map_load_fontset (PangoFontMap *font_map, |
| 226 |
PangoContext *context, |
| 227 |
const PangoFontDescription *desc, |
| 228 |
PangoLanguage *language) |
| 229 |
{ |
| 230 |
static PangoLanguage *last_language = NULL; |
| 231 |
static GHashTable *seen_languages = NULL; |
| 232 |
PangoFontset *fontset; |
| 233 |
|
| 234 |
fontset = pk_pango_fc_font_map_load_fontset_default (font_map, context, desc, language); |
| 235 |
|
| 236 |
/* "xx" is Pango's "unknown language" language code. |
| 237 |
* we can fall back to scripts maybe, but the facilities for that |
| 238 |
* is not in place yet. Maybe Pango can use a four-letter script |
| 239 |
* code instead of "xx"... */ |
| 240 |
if (G_LIKELY (language == last_language) || |
| 241 |
language == NULL || |
| 242 |
pango_language_matches (language, "c;xx")) |
| 243 |
return fontset; |
| 244 |
|
| 245 |
if (G_UNLIKELY (!seen_languages)) |
| 246 |
seen_languages = g_hash_table_new (NULL, NULL); |
| 247 |
|
| 248 |
if (G_UNLIKELY (!g_hash_table_lookup (seen_languages, language))) { |
| 249 |
FontsetForeachClosure closure; |
| 250 |
|
| 251 |
g_hash_table_insert (seen_languages, language, language); |
| 252 |
|
| 253 |
closure.language = language; |
| 254 |
closure.found = FALSE; |
| 255 |
pango_fontset_foreach (fontset, fontset_foreach_cb, &closure); |
| 256 |
if (!closure.found) |
| 257 |
pk_font_not_found (language); |
| 258 |
} |
| 259 |
|
| 260 |
last_language = language; |
| 261 |
return fontset; |
| 262 |
} |
| 263 |
|
| 264 |
static void |
| 265 |
pk_pango_fc_font_map_class_init (PangoFontMapClass *klass, gpointer class_data) |
| 266 |
{ |
| 267 |
g_return_if_fail (pk_pango_fc_font_map_load_fontset_default == NULL); |
| 268 |
|
| 269 |
pk_pango_fc_font_map_load_fontset_default = klass->load_fontset; |
| 270 |
klass->load_fontset = pk_pango_fc_font_map_load_fontset; |
| 271 |
} |
| 272 |
|
| 273 |
static GType |
| 274 |
pk_pango_fc_font_map_overload_type (GType default_pango_fc_font_map_type) |
| 275 |
{ |
| 276 |
GTypeQuery query; |
| 277 |
g_type_query (default_pango_fc_font_map_type, &query); |
| 278 |
|
| 279 |
return g_type_register_static_simple (default_pango_fc_font_map_type, |
| 280 |
g_intern_static_string ("PkPangoFcFontMap"), |
| 281 |
query.class_size, |
| 282 |
(GClassInitFunc) pk_pango_fc_font_map_class_init, |
| 283 |
query.instance_size, |
| 284 |
NULL, 0); |
| 285 |
} |
| 286 |
|
| 287 |
static void |
| 288 |
install_pango_font_map (void) |
| 289 |
{ |
| 290 |
static GType font_map_type = 0; |
| 291 |
|
| 292 |
if (!font_map_type) { |
| 293 |
PangoFontMap *font_map; |
| 294 |
|
| 295 |
font_map = pango_cairo_font_map_get_default (); |
| 296 |
if (!PANGO_IS_FC_FONT_MAP (font_map)) { |
| 297 |
g_warning ("Default pangocairo font map is not a pangofc fontmap. " |
| 298 |
"Skipping automatic missing-font installation."); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
font_map_type = pk_pango_fc_font_map_overload_type (G_TYPE_FROM_INSTANCE (font_map)); |
| 303 |
font_map = g_object_new (font_map_type, NULL); |
| 304 |
pango_cairo_font_map_set_default (PANGO_CAIRO_FONT_MAP (font_map)); |
| 305 |
g_object_unref (font_map); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* GTK module declaraction |
| 312 |
**/ |
| 313 |
|
| 314 |
void gtk_module_init (gint *argc, gchar ***argv); |
| 315 |
|
| 316 |
void |
| 317 |
gtk_module_init (gint *argc G_GNUC_UNUSED, |
| 318 |
gchar ***argv G_GNUC_UNUSED) |
| 319 |
{ |
| 320 |
install_pango_font_map (); |
| 321 |
} |
| 322 |
|
| 323 |
const char *g_module_check_init (GModule *module); |
| 324 |
|
| 325 |
const char * |
| 326 |
g_module_check_init (GModule *module) |
| 327 |
{ |
| 328 |
/* make the GTK+ module resident |
| 329 |
* without doing this, killing gnome-settings-daemon brings down every |
| 330 |
* single application in the session, since the module doesn't clean up |
| 331 |
* when being unloaded */ |
| 332 |
g_module_make_resident (module); |
| 333 |
return NULL; |
| 334 |
} |