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