summaryrefslogtreecommitdiff
path: root/utils/pango-list.c
blob: c0f0d4ae646418e1e1cec299f43cff5b3f80814e (plain)
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
/* pango-list.c: List all fonts
 *
 * Copyright (C) 2018 Google
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Google Author(s): Behdad Esfahbod
 */

#include "config.h"
#include <pango/pangocairo.h>
#include <glib/gstdio.h>
#include <stdlib.h>

int
main (int    argc,
      char **argv)
{
  gboolean opt_verbose = FALSE;
  gboolean opt_metrics = FALSE;
  GOptionEntry entries[] = {
    {"verbose", 0, 0, G_OPTION_ARG_NONE,    &opt_verbose, "Print verbose information", NULL },
    {"metrics", 0, 0, G_OPTION_ARG_NONE,    &opt_metrics, "Print font metrics", NULL },
    { NULL, }
  };
  GOptionContext *context;
  PangoContext *ctx;
  PangoFontMap *fontmap;
  PangoFontFamily **families;
  int n_families;
  int i, j;
  int width;
  GError *error = NULL;

  g_set_prgname ("pango-list");

  context = g_option_context_new ("");
  g_option_context_add_main_entries (context, entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      if (error != NULL)
        g_printerr ("%s\n", error->message);
      else
        g_printerr ("Option parse error\n");
      exit (1);
    }

  g_option_context_free (context);

  /* Use PangoCairo to get default fontmap so it works on every platform. */
  fontmap = pango_cairo_font_map_get_default ();
  ctx = pango_font_map_create_context (fontmap);

  if (opt_verbose)
    g_print ("Using %s\n\n", G_OBJECT_TYPE_NAME (fontmap));

  pango_font_map_list_families (fontmap, &families, &n_families);
  for (i = 0; i < n_families; i++)
    {
      PangoFontFace **faces;
      int n_faces;
      const char *kind;

      const char *family_name = pango_font_family_get_name (families[i]);
      if (pango_font_family_is_monospace (families[i]))
        {
          if (pango_font_family_is_variable (families[i]))
            kind = "(monospace, variable)";
          else
            kind = "(monospace)";
        }
      else
        {
          if (pango_font_family_is_variable (families[i]))
            kind = "(variable)";
          else
            kind = "";
        }

      g_print ("%s %s\n", family_name, kind);

      pango_font_family_list_faces (families[i], &faces, &n_faces);

      width = 0;
      for (j = 0; j < n_faces; j++)
	{
	  const char *face_name = pango_font_face_get_face_name (faces[j]);
	  gboolean is_synth = pango_font_face_is_synthesized (faces[j]);
	  const char *synth_str = is_synth ? "*" : "";
          width = MAX (width, strlen (synth_str) + strlen (face_name));
        }

      for (j = 0; j < n_faces; j++)
	{
	  const char *face_name = pango_font_face_get_face_name (faces[j]);
	  gboolean is_synth = pango_font_face_is_synthesized (faces[j]);
	  const char *synth_str = is_synth ? "*" : "";
	  PangoFontDescription *desc = pango_font_face_describe (faces[j]);
	  char *desc_str = pango_font_description_to_string (desc);

	  g_print ("  %s%s: %*s%s\n", synth_str, face_name,
                   width - (int)strlen (face_name) - (int)strlen (synth_str), "", desc_str);

          if (opt_metrics)
            {
              PangoFontMetrics *metrics;

              pango_font_description_set_absolute_size (desc, 10 * PANGO_SCALE);
              metrics = pango_context_get_metrics (ctx, desc, pango_language_from_string ("en-us"));
              g_print ("    (a %d d %d h %d cw %d dw %d u %d %d s %d %d)\n",
                       pango_font_metrics_get_ascent (metrics),
                       pango_font_metrics_get_descent (metrics),
                       pango_font_metrics_get_height (metrics),
                       pango_font_metrics_get_approximate_char_width (metrics),
                       pango_font_metrics_get_approximate_digit_width (metrics),
                       pango_font_metrics_get_underline_position (metrics),
                       pango_font_metrics_get_underline_thickness (metrics),
                       pango_font_metrics_get_strikethrough_position (metrics),
                       pango_font_metrics_get_strikethrough_thickness (metrics));
              pango_font_metrics_unref (metrics);
            }

	  g_free (desc_str);
	  pango_font_description_free (desc);
	}

      g_free (faces);
    }
  g_free (families);
  g_object_unref (fontmap);

  return 0;
}