2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2003-03-26 06:00:59 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpfontlist.c
|
2004-03-28 22:27:37 +08:00
|
|
|
* Copyright (C) 2003-2004 Michael Natterer <mitch@gimp.org>
|
|
|
|
* Sven Neumann <sven@gimp.org>
|
|
|
|
* Manish Singh <yosh@gimp.org>
|
2003-03-26 06:00:59 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2003-03-26 06:00:59 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-03-26 06:00:59 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-03-26 06:00:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
2008-10-31 15:24:11 +08:00
|
|
|
#include <pango/pangocairo.h>
|
2004-04-16 08:56:25 +08:00
|
|
|
#include <pango/pangofc-fontmap.h>
|
2003-03-26 06:00:59 +08:00
|
|
|
|
|
|
|
#include "text-types.h"
|
|
|
|
|
|
|
|
#include "gimpfont.h"
|
2005-02-10 13:33:01 +08:00
|
|
|
#include "gimpfont-utils.h"
|
2003-03-26 06:00:59 +08:00
|
|
|
#include "gimpfontlist.h"
|
|
|
|
|
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
2003-05-02 13:50:36 +08:00
|
|
|
/* Use fontconfig directly for speed. We can use the pango stuff when/if
|
|
|
|
* fontconfig/pango get more efficient.
|
|
|
|
*/
|
|
|
|
#define USE_FONTCONFIG_DIRECTLY
|
|
|
|
|
|
|
|
#ifdef USE_FONTCONFIG_DIRECTLY
|
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
typedef char * (* GimpFontDescToStringFunc) (const PangoFontDescription *desc);
|
|
|
|
|
|
|
|
|
2005-12-11 03:24:36 +08:00
|
|
|
static void gimp_font_list_add_font (GimpFontList *list,
|
|
|
|
PangoContext *context,
|
|
|
|
PangoFontDescription *desc);
|
2003-03-26 06:00:59 +08:00
|
|
|
|
2005-12-11 03:24:36 +08:00
|
|
|
static void gimp_font_list_load_names (GimpFontList *list,
|
|
|
|
PangoFontMap *fontmap,
|
|
|
|
PangoContext *context);
|
2003-05-02 13:50:36 +08:00
|
|
|
|
2003-03-26 06:00:59 +08:00
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
G_DEFINE_TYPE (GimpFontList, gimp_font_list, GIMP_TYPE_LIST)
|
2003-03-26 06:00:59 +08:00
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
static GimpFontDescToStringFunc font_desc_to_string = NULL;
|
|
|
|
|
2003-03-26 06:00:59 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_font_list_class_init (GimpFontListClass *klass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_font_list_init (GimpFontList *list)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GimpContainer *
|
2003-03-26 06:53:10 +08:00
|
|
|
gimp_font_list_new (gdouble xresolution,
|
|
|
|
gdouble yresolution)
|
2003-03-26 06:00:59 +08:00
|
|
|
{
|
|
|
|
GimpFontList *list;
|
2003-03-26 06:53:10 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (xresolution > 0.0, NULL);
|
|
|
|
g_return_val_if_fail (yresolution > 0.0, NULL);
|
2003-03-26 06:00:59 +08:00
|
|
|
|
|
|
|
list = g_object_new (GIMP_TYPE_FONT_LIST,
|
2006-12-11 03:13:58 +08:00
|
|
|
"children-type", GIMP_TYPE_FONT,
|
2003-03-26 06:00:59 +08:00
|
|
|
"policy", GIMP_CONTAINER_POLICY_STRONG,
|
|
|
|
NULL);
|
|
|
|
|
2003-03-26 06:53:10 +08:00
|
|
|
list->xresolution = xresolution;
|
|
|
|
list->yresolution = yresolution;
|
|
|
|
|
2003-03-26 06:00:59 +08:00
|
|
|
return GIMP_CONTAINER (list);
|
|
|
|
}
|
|
|
|
|
2003-03-26 06:53:10 +08:00
|
|
|
void
|
|
|
|
gimp_font_list_restore (GimpFontList *list)
|
|
|
|
{
|
2008-10-31 15:24:11 +08:00
|
|
|
PangoCairoFontMap *fontmap;
|
|
|
|
PangoContext *context;
|
2003-03-26 06:53:10 +08:00
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_FONT_LIST (list));
|
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
if (font_desc_to_string == NULL)
|
|
|
|
{
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
gchar *name;
|
|
|
|
gchar last_char;
|
|
|
|
|
|
|
|
desc = pango_font_description_new ();
|
|
|
|
pango_font_description_set_family (desc, "Wilber 12");
|
|
|
|
|
|
|
|
name = pango_font_description_to_string (desc);
|
|
|
|
last_char = name[strlen (name) - 1];
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
pango_font_description_free (desc);
|
|
|
|
|
|
|
|
if (last_char != ',')
|
|
|
|
font_desc_to_string = &gimp_font_util_pango_font_description_to_string;
|
|
|
|
else
|
|
|
|
font_desc_to_string = &pango_font_description_to_string;
|
|
|
|
}
|
|
|
|
|
2009-08-27 05:02:23 +08:00
|
|
|
fontmap = PANGO_CAIRO_FONT_MAP (pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT));
|
|
|
|
if (fontmap == NULL)
|
|
|
|
g_error ("You are using a Pango that has been built against a cairo that lacks the Freetype font backend");
|
2008-10-31 15:24:11 +08:00
|
|
|
pango_cairo_font_map_set_resolution (fontmap, list->yresolution);
|
|
|
|
context = pango_cairo_font_map_create_context (fontmap);
|
2003-03-27 23:52:25 +08:00
|
|
|
g_object_unref (fontmap);
|
|
|
|
|
|
|
|
gimp_container_freeze (GIMP_CONTAINER (list));
|
2003-03-26 07:24:30 +08:00
|
|
|
|
2008-10-31 15:24:11 +08:00
|
|
|
gimp_font_list_load_names (list, PANGO_FONT_MAP (fontmap), context);
|
2003-05-02 13:50:36 +08:00
|
|
|
g_object_unref (context);
|
|
|
|
|
2003-10-09 21:35:15 +08:00
|
|
|
gimp_list_sort_by_name (GIMP_LIST (list));
|
2003-05-02 13:50:36 +08:00
|
|
|
|
|
|
|
gimp_container_thaw (GIMP_CONTAINER (list));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_font_list_add_font (GimpFontList *list,
|
|
|
|
PangoContext *context,
|
|
|
|
PangoFontDescription *desc)
|
|
|
|
{
|
2007-11-11 20:48:42 +08:00
|
|
|
gchar *name;
|
2003-05-02 13:50:36 +08:00
|
|
|
|
2004-03-28 22:27:37 +08:00
|
|
|
if (! desc)
|
2003-05-02 13:50:36 +08:00
|
|
|
return;
|
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
name = font_desc_to_string (desc);
|
2003-05-02 13:50:36 +08:00
|
|
|
|
2007-11-11 20:48:42 +08:00
|
|
|
if (g_utf8_validate (name, -1, NULL))
|
2004-03-28 22:27:37 +08:00
|
|
|
{
|
2007-11-11 20:48:42 +08:00
|
|
|
GimpFont *font;
|
2004-03-28 22:27:37 +08:00
|
|
|
|
2007-11-11 20:48:42 +08:00
|
|
|
font = g_object_new (GIMP_TYPE_FONT,
|
|
|
|
"name", name,
|
|
|
|
"pango-context", context,
|
|
|
|
NULL);
|
2007-07-12 17:11:03 +08:00
|
|
|
|
2007-11-11 20:48:42 +08:00
|
|
|
gimp_container_add (GIMP_CONTAINER (list), GIMP_OBJECT (font));
|
|
|
|
g_object_unref (font);
|
|
|
|
}
|
2003-05-02 13:50:36 +08:00
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_FONTCONFIG_DIRECTLY
|
|
|
|
/* We're really chummy here with the implementation. Oh well. */
|
|
|
|
|
|
|
|
/* This is copied straight from make_alias_description in pango, plus
|
2004-03-28 22:27:37 +08:00
|
|
|
* the gimp_font_list_add_font bits.
|
|
|
|
*/
|
2003-05-02 13:50:36 +08:00
|
|
|
static void
|
|
|
|
gimp_font_list_make_alias (GimpFontList *list,
|
|
|
|
PangoContext *context,
|
|
|
|
const gchar *family,
|
|
|
|
gboolean bold,
|
|
|
|
gboolean italic)
|
|
|
|
{
|
|
|
|
PangoFontDescription *desc = pango_font_description_new ();
|
|
|
|
|
|
|
|
pango_font_description_set_family (desc, family);
|
2003-10-09 21:35:15 +08:00
|
|
|
pango_font_description_set_style (desc,
|
|
|
|
italic ?
|
|
|
|
PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
|
2003-05-02 13:50:36 +08:00
|
|
|
pango_font_description_set_variant (desc, PANGO_VARIANT_NORMAL);
|
2003-10-09 21:35:15 +08:00
|
|
|
pango_font_description_set_weight (desc,
|
|
|
|
bold ?
|
|
|
|
PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
|
2003-05-02 13:50:36 +08:00
|
|
|
pango_font_description_set_stretch (desc, PANGO_STRETCH_NORMAL);
|
|
|
|
|
|
|
|
gimp_font_list_add_font (list, context, desc);
|
|
|
|
|
|
|
|
pango_font_description_free (desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_font_list_load_aliases (GimpFontList *list,
|
|
|
|
PangoContext *context)
|
|
|
|
{
|
|
|
|
const gchar *families[] = { "Sans", "Serif", "Monospace" };
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
gimp_font_list_make_alias (list, context, families[i], FALSE, FALSE);
|
|
|
|
gimp_font_list_make_alias (list, context, families[i], TRUE, FALSE);
|
|
|
|
gimp_font_list_make_alias (list, context, families[i], FALSE, TRUE);
|
|
|
|
gimp_font_list_make_alias (list, context, families[i], TRUE, TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_font_list_load_names (GimpFontList *list,
|
|
|
|
PangoFontMap *fontmap,
|
|
|
|
PangoContext *context)
|
|
|
|
{
|
|
|
|
FcObjectSet *os;
|
|
|
|
FcPattern *pat;
|
|
|
|
FcFontSet *fontset;
|
|
|
|
gint i;
|
|
|
|
|
2004-01-16 04:54:32 +08:00
|
|
|
os = FcObjectSetBuild (FC_FAMILY, FC_STYLE,
|
|
|
|
FC_SLANT, FC_WEIGHT, FC_WIDTH,
|
|
|
|
NULL);
|
|
|
|
|
2003-05-02 13:50:36 +08:00
|
|
|
pat = FcPatternCreate ();
|
|
|
|
|
|
|
|
fontset = FcFontList (NULL, pat, os);
|
2003-10-09 21:35:15 +08:00
|
|
|
|
2003-05-02 13:50:36 +08:00
|
|
|
FcPatternDestroy (pat);
|
|
|
|
FcObjectSetDestroy (os);
|
|
|
|
|
|
|
|
for (i = 0; i < fontset->nfont; i++)
|
|
|
|
{
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
|
2004-04-16 08:56:25 +08:00
|
|
|
desc = pango_fc_font_description_from_pattern (fontset->fonts[i], FALSE);
|
2003-05-02 13:50:36 +08:00
|
|
|
gimp_font_list_add_font (list, context, desc);
|
|
|
|
pango_font_description_free (desc);
|
|
|
|
}
|
|
|
|
|
2003-11-05 07:59:58 +08:00
|
|
|
/* only create aliases if there is at least one font available */
|
|
|
|
if (fontset->nfont > 0)
|
|
|
|
gimp_font_list_load_aliases (list, context);
|
|
|
|
|
2003-05-02 13:50:36 +08:00
|
|
|
FcFontSetDestroy (fontset);
|
|
|
|
}
|
2003-11-05 07:59:58 +08:00
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
#else /* ! USE_FONTCONFIG_DIRECTLY */
|
2003-11-05 07:59:58 +08:00
|
|
|
|
2003-05-02 13:50:36 +08:00
|
|
|
static void
|
|
|
|
gimp_font_list_load_names (GimpFontList *list,
|
|
|
|
PangoFontMap *fontmap,
|
|
|
|
PangoContext *context)
|
|
|
|
{
|
|
|
|
PangoFontFamily **families;
|
|
|
|
PangoFontFace **faces;
|
|
|
|
gint n_families;
|
|
|
|
gint n_faces;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
pango_font_map_list_families (fontmap, &families, &n_families);
|
|
|
|
|
2003-03-26 07:24:30 +08:00
|
|
|
for (i = 0; i < n_families; i++)
|
2003-03-26 06:53:10 +08:00
|
|
|
{
|
2003-03-26 07:41:01 +08:00
|
|
|
pango_font_family_list_faces (families[i], &faces, &n_faces);
|
2003-10-09 21:35:15 +08:00
|
|
|
|
2003-03-26 07:41:01 +08:00
|
|
|
for (j = 0; j < n_faces; j++)
|
|
|
|
{
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
|
2004-01-16 04:54:32 +08:00
|
|
|
desc = pango_font_face_describe (faces[j]);
|
2003-05-02 13:50:36 +08:00
|
|
|
gimp_font_list_add_font (list, context, desc);
|
2003-03-26 07:41:01 +08:00
|
|
|
pango_font_description_free (desc);
|
|
|
|
}
|
2003-03-26 06:53:10 +08:00
|
|
|
}
|
2003-03-26 07:24:30 +08:00
|
|
|
|
|
|
|
g_free (families);
|
2003-03-26 06:00:59 +08:00
|
|
|
}
|
2004-03-28 22:27:37 +08:00
|
|
|
|
2005-02-22 09:28:45 +08:00
|
|
|
#endif /* USE_FONTCONFIG_DIRECTLY */
|