svnno****@sourc*****
svnno****@sourc*****
Fri Sep 11 12:37:16 JST 2009
Revision: 3838 http://sourceforge.jp/projects/kazehakase/svn/view?view=rev&revision=3838 Author: ikezoe Date: 2009-09-11 12:37:16 +0900 (Fri, 11 Sep 2009) Log Message: ----------- 2009-09-10 Hiroyuki Ikezoe <poinc****@ikezo*****> * src/utils/kz-fonts-utils.[ch], src/prefs/prefs_font.c: Now font list are obtained with pango functions. Modified Paths: -------------- kazehakase/trunk/ChangeLog kazehakase/trunk/src/prefs_ui/prefs_font.c kazehakase/trunk/src/utils/Makefile.am Added Paths: ----------- kazehakase/trunk/src/utils/kz-font-utils.c kazehakase/trunk/src/utils/kz-font-utils.h Modified: kazehakase/trunk/ChangeLog =================================================================== --- kazehakase/trunk/ChangeLog 2009-09-10 03:49:19 UTC (rev 3837) +++ kazehakase/trunk/ChangeLog 2009-09-11 03:37:16 UTC (rev 3838) @@ -1,5 +1,11 @@ 2009-09-10 Hiroyuki Ikezoe <poinc****@ikezo*****> + * src/utils/kz-fonts-utils.[ch], + src/prefs/prefs_font.c: Now font list are obtained with + pango functions. + +2009-09-10 Hiroyuki Ikezoe <poinc****@ikezo*****> + * src/widget/kz-entry.[ch]: adapt to new GtkEntry over all. * src/kz-status-bar.c, src/actions/kz-entry-action.c, src/actions/kz-smart-bookmark-action.c: Use new GtkEntry functions. Modified: kazehakase/trunk/src/prefs_ui/prefs_font.c =================================================================== --- kazehakase/trunk/src/prefs_ui/prefs_font.c 2009-09-10 03:49:19 UTC (rev 3837) +++ kazehakase/trunk/src/prefs_ui/prefs_font.c 2009-09-11 03:37:16 UTC (rev 3838) @@ -22,12 +22,10 @@ #include <string.h> #include <glib/gi18n.h> -#include "kz-embed-prefs.h" -#include "kz-embed.h" #include "kazehakase.h" #include "gtk-utils.h" +#include "kz-font-utils.h" - #define DATA_KEY "KzPrefsFont::info" @@ -113,7 +111,7 @@ typedef struct { gchar *title; - gchar* code; + gchar *code; } FontsLanguageInfo; static const FontsLanguageInfo fonts_language[] = @@ -135,22 +133,47 @@ }; static const guint n_fonts_languages = G_N_ELEMENTS (fonts_language); +static gchar * +get_font_name_from_profile (const gchar *font_type, const gchar *language) +{ + gchar key[256]; + gint keysize = G_N_ELEMENTS(key); + g_snprintf(key, keysize, + "name_%s_%s", + font_type, + language); + return KZ_CONF_GET_STR("Font", key); +} + +static gint +get_font_size_from_profile (const gchar *font_type, const gchar *language) +{ + gchar key[256]; + gint keysize = G_N_ELEMENTS(key); + gint font_size = 0; + + g_snprintf(key, keysize, + "name_%s_%s", + font_type, + language); + KZ_CONF_GET("Font", key, font_size, INT); + return font_size; +} + static FontPref * font_pref_new (const gchar *lang) { FontPref *pref = g_new0(FontPref, 1); - if (lang) - pref->lang = g_strdup(lang); - else - pref->lang = NULL; - pref->serif = NULL; - pref->sans_serif = NULL; - pref->monospace = NULL; - pref->variable_size = 0; - pref->fixed_size = 0; - pref->min_size = 0; + pref->lang = g_strdup(lang); + pref->serif = get_font_name_from_profile("serif", lang); + pref->sans_serif = get_font_name_from_profile("sans-serif", lang); + pref->monospace = get_font_name_from_profile("monospace", lang); + pref->variable_size = get_font_size_from_profile("variable", lang); + pref->fixed_size = get_font_size_from_profile("fixed", lang); + pref->min_size = get_font_size_from_profile("min_size", lang); + return pref; } @@ -174,19 +197,7 @@ const FontPref *pref = a; const gchar *lang = b; - if (!pref || !pref->lang) - { - if (!lang) - return 0; - else - return -1; - } - else if (!lang) - { - return 1; - } - - return strcmp(pref->lang, lang); + return g_strcmp0(pref->lang, lang); } @@ -208,37 +219,24 @@ } static void -font_combo_set (GtkComboBox *combo, - const gchar *lang, const gchar *type, - const gchar *font) +set_font_name (GtkComboBox *combo, + const gchar *lang, const gchar *type, + const gchar *current_font) { - GList *fonts, *all_fonts = NULL, *node; - const gchar *use_font = NULL; - gchar *default_font = NULL; + GList *fonts = NULL, *all_fonts = NULL, *node; GtkListStore *store; gint select = 0, i = 0; - KzEmbedPrefs *embed_prefs = KZ_EMBED_PREFS(kz_embed_new("gecko")); - if (!embed_prefs) - return; - /* remove old list */ store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); gtk_list_store_clear(store); - - kz_embed_prefs_get_font_list(embed_prefs, lang, type, - &fonts, &all_fonts, - &default_font); - gtk_widget_destroy(GTK_WIDGET(embed_prefs)); + kz_font_utils_get_font_list(lang, &fonts); + kz_font_utils_get_all_font_list(&all_fonts); - if (!fonts) return; - - use_font = font && *font ? font : default_font; - for (node = g_list_first(fonts); node; node = g_list_next(node)) { - if (use_font && node->data && !strcmp(use_font, node->data)) + if (current_font && node->data && !strcmp(current_font, node->data)) select = i; gtk_combo_box_append_text(combo, node->data); i++; @@ -250,29 +248,21 @@ for (node = g_list_first(all_fonts); node; node = g_list_next(node)) { - if (use_font && node->data && !strcmp(use_font, node->data)) + if (current_font && node->data && !strcmp(current_font, node->data)) select = i; gtk_combo_box_append_text(combo, node->data); i++; } gtk_combo_box_set_active(combo, select); - - g_free (default_font); - g_list_foreach (fonts, (GFunc)g_free, NULL); - g_list_free (fonts); - - if (all_fonts) - { - g_list_foreach (all_fonts, (GFunc)g_free, NULL); - g_list_free (all_fonts); - } + kz_font_utils_free_font_list(fonts); + kz_font_utils_free_font_list(all_fonts); } static void -font_spin_set (GtkSpinButton *spin, +set_font_size (GtkSpinButton *spin, const gchar *type, const gchar *lang, gint size) { @@ -297,7 +287,7 @@ static const gchar * -get_current_lang(KzPrefsFont *prefsui) +get_current_lang (KzPrefsFont *prefsui) { guint i; @@ -310,7 +300,23 @@ return NULL; } +static FontPref * +get_font_pref_for_language (KzPrefsFont *prefsui, const gchar *language) +{ + GList *found; + FontPref *pref; + found = g_list_find_custom(prefsui->prefs_list, language, + compare_font_pref); + if (found) + return (FontPref*)found->data; + + pref = font_pref_new(language); + prefsui->prefs_list + = g_list_append(prefsui->prefs_list, pref); + return pref; +} + static void font_prefs_reset (KzPrefsFont *prefsui) { @@ -324,33 +330,21 @@ if (lang && *lang) { - FontPref *pref = NULL; - GList *node; + FontPref *pref; + pref = get_font_pref_for_language(prefsui, lang); - node = g_list_find_custom(prefsui->prefs_list, lang, - compare_font_pref); - if (node) - pref = node->data; + set_font_name(prefsui->serif_combo, + lang, "serif", pref->serif); + set_font_name(prefsui->sans_serif_combo, + lang, "sans-serif", pref->sans_serif); + set_font_name(prefsui->monospace_combo, + lang, "monospace", pref->monospace); - if (!pref) - { - pref = font_pref_new(lang); - prefsui->prefs_list - = g_list_append(prefsui->prefs_list, pref); - } - - font_combo_set(prefsui->serif_combo, - lang, "serif", pref->serif); - font_combo_set(prefsui->sans_serif_combo, - lang, "sans-serif", pref->sans_serif); - font_combo_set(prefsui->monospace_combo, - lang, "monospace", pref->monospace); - - font_spin_set(prefsui->variable_size_spin, + set_font_size(prefsui->variable_size_spin, "size_variable", lang, pref->variable_size); - font_spin_set(prefsui->fixed_size_spin, + set_font_size(prefsui->fixed_size_spin, "size_fixed", lang, pref->fixed_size); - font_spin_set(prefsui->min_size_spin, + set_font_size(prefsui->min_size_spin, "minimum-size", lang, pref->min_size); } else @@ -386,8 +380,7 @@ { const gchar *lang, *new_font; gchar **font = NULL; - GList *node; - FontPref *pref = NULL; + FontPref *pref; g_return_if_fail(GTK_IS_COMBO_BOX(combo)); @@ -396,13 +389,8 @@ lang = get_current_lang(prefsui); if (!lang || !*lang) return; - node = g_list_find_custom(prefsui->prefs_list, lang, - compare_font_pref); - if (!node) return; + pref = get_font_pref_for_language(prefsui, lang); - pref = node->data; - if (!pref) return; - new_font = gtk_entry_get_text(GTK_ENTRY(GTK_BIN(combo)->child)); switch (type) { @@ -420,7 +408,8 @@ break; } - if (!font) return; + if (!font) + return; if (new_font && *new_font) { @@ -435,8 +424,7 @@ FontSizeType type) { const gchar *lang; - GList *node; - FontPref *pref = NULL; + FontPref *pref; gint size; g_return_if_fail(GTK_IS_SPIN_BUTTON(spin)); @@ -446,13 +434,8 @@ lang = get_current_lang(prefsui); if (!lang || !*lang) return; - node = g_list_find_custom(prefsui->prefs_list, lang, - compare_font_pref); - if (!node) return; + pref = get_font_pref_for_language(prefsui, lang); - pref = node->data; - if (!pref) return; - size = (gint) gtk_spin_button_get_value(spin); switch (type) { @@ -475,14 +458,14 @@ static void cb_serif_entry_changed (GtkComboBox *combo, KzPrefsFont *prefsui) { - store_new_font (combo, prefsui, FONT_SERIF); + store_new_font(combo, prefsui, FONT_SERIF); } static void cb_sans_serif_entry_changed (GtkComboBox *combo, KzPrefsFont *prefsui) { - store_new_font (combo, prefsui, FONT_SANS_SERIF); + store_new_font(combo, prefsui, FONT_SANS_SERIF); } Modified: kazehakase/trunk/src/utils/Makefile.am =================================================================== --- kazehakase/trunk/src/utils/Makefile.am 2009-09-10 03:49:19 UTC (rev 3837) +++ kazehakase/trunk/src/utils/Makefile.am 2009-09-11 03:37:16 UTC (rev 3838) @@ -23,6 +23,7 @@ gobject-utils.h \ kz-history-utils.h kz-history-utils.c \ utils.c \ + kz-font-utils.h kz-font-utils.c \ $(libkzutils_public_h_sources) libkzutils_la_LIBADD = \ Added: kazehakase/trunk/src/utils/kz-font-utils.c =================================================================== --- kazehakase/trunk/src/utils/kz-font-utils.c (rev 0) +++ kazehakase/trunk/src/utils/kz-font-utils.c 2009-09-11 03:37:16 UTC (rev 3838) @@ -0,0 +1,126 @@ +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2009 Hiroyuki Ikezoe + * + * 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "kz-font-utils.h" + +#include <gtk/gtk.h> + +typedef struct _kz_font_list_set { + GList **fonts; + PangoLanguage *pango_language; +} kz_font_list_set; + +static gboolean +each_font (PangoFontset *fontset, PangoFont *font, kz_font_list_set *list_set) +{ + PangoCoverage *coverage; + PangoFontDescription *font_description; + const gchar *sample_string, *p; + glong i, len; + + coverage = pango_font_get_coverage(font, list_set->pango_language); + sample_string = pango_language_get_sample_string(list_set->pango_language); + len = g_utf8_strlen(sample_string, -1); + p = sample_string; + for (i = 0; i < len; i++) { + gunichar index = g_utf8_get_char(p); + if (pango_coverage_get(coverage, index) < PANGO_COVERAGE_EXACT) { + pango_coverage_unref(coverage); + return FALSE; + } + p = g_utf8_find_next_char(p, NULL); + } + + font_description = pango_font_describe(font); + *(list_set->fonts) = g_list_prepend(*(list_set->fonts), + g_strdup(pango_font_description_get_family(font_description))); + pango_font_description_free(font_description); + pango_coverage_unref(coverage); + + return FALSE; +} + +void +kz_font_utils_get_font_list (const gchar *language, GList **fonts) +{ + PangoContext *pango_context; + PangoLanguage *pango_language; + PangoFontMap *font_map; + PangoFontset *fontset; + PangoFontDescription *font_description; + kz_font_list_set list_set; + + *fonts = NULL; + + pango_context = gdk_pango_context_get(); + pango_language = pango_language_from_string(language); + font_map = pango_cairo_font_map_get_default(); + font_description = pango_font_description_new(); + fontset = pango_font_map_load_fontset(font_map, + pango_context, + font_description, + pango_language); + list_set.fonts = fonts; + list_set.pango_language = pango_language; + pango_fontset_foreach(fontset, (PangoFontsetForeachFunc)each_font, &list_set); + + *fonts = g_list_sort(*fonts, (GCompareFunc)g_strcmp0); + + g_object_unref(fontset); + g_object_unref(pango_context); + pango_font_description_free(font_description); +} + +void +kz_font_utils_get_all_font_list (GList **fonts) +{ + PangoContext *pango_context; + PangoFontFamily **families = NULL; + gint n_families, i; + + *fonts = NULL; + + pango_context = gdk_pango_context_get(); + pango_context_list_families(pango_context, &families, &n_families); + + for (i = 0; i < n_families; i++) { + gchar *font_name; + font_name = g_strdup(pango_font_family_get_name(families[i])); + *fonts = g_list_prepend(*fonts, font_name); + } + + *fonts = g_list_sort(*fonts, (GCompareFunc)g_strcmp0); + + g_object_unref(pango_context); + g_free(families); +} + +void +kz_font_utils_free_font_list (GList *fonts) +{ + if (!fonts) + return; + + g_list_foreach(fonts, (GFunc)g_free, NULL); + g_list_free(fonts); +} + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/ Added: kazehakase/trunk/src/utils/kz-font-utils.h =================================================================== --- kazehakase/trunk/src/utils/kz-font-utils.h (rev 0) +++ kazehakase/trunk/src/utils/kz-font-utils.h 2009-09-11 03:37:16 UTC (rev 3838) @@ -0,0 +1,37 @@ +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2009 Hiroyuki Ikezoe + * + * 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __KZ_FONT_UTILS_H__ +#define __KZ_FONT_UTILS_H__ + +#include <glib.h> + +G_BEGIN_DECLS + +void kz_font_utils_get_font_list (const gchar *language, + GList **fonts); +void kz_font_utils_get_all_font_list (GList **fonts); +void kz_font_utils_free_font_list (GList *fonts); + +G_END_DECLS + +#endif /* __KZ_FONT_UTILS_H__ */ +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/