summaryrefslogtreecommitdiffstats
path: root/missing-glyph-checker/src/main.c
blob: c79b0b4590daf0c98c7483ece3b4895380683eb9 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * main.c
 * Copyright (C) 2010 Red Hat, Inc.
 * 
 * Authors:
 *   Akira TAGOH  <tagoh@redhat.com>
 * 
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <fontconfig/fontconfig.h>
#include <glib.h>
#include <ft2build.h>
#include FT_FREETYPE_H

/** from fcint.h **/
#define FcCharSetLeaves(c)	FcOffsetMember(c,leaves_offset,intptr_t)
#define FcCharSetNumbers(c)	FcOffsetMember(c,numbers_offset,FcChar16)
#define FcOffsetMember(s,m,t)	FcOffsetToPtr(s,(s)->m,t)
#define FcOffsetToPtr(b,o,t)	((t *) ((intptr_t) (b) + (o)))
struct _FcCharSet {
	int      ref;        /* reference count */
	int      num;        /* size of leaves and numbers arrays */
	intptr_t leaves_offset;
	intptr_t numbers_offset;
};
typedef struct _FcCharLeaf {
	FcChar32 map[256/32];
} FcCharLeaf;

int
main(int    argc,
     char **argv)
{
	gchar *arg_lang = NULL;
	gboolean arg_verbose = FALSE;
	GOptionContext *ctxt = g_option_context_new(NULL);
	GOptionEntry entries[] = {
		{"lang", 'l', 0, G_OPTION_ARG_STRING, &arg_lang, "Specify the language to check glyphs from fontconfig orth file", "LANG"},
		{"verbose", 'v', 0, G_OPTION_ARG_NONE, &arg_verbose, "more output for debugging", NULL},
		{NULL},
	};
	GError *err = NULL;
	gint i, j, k;
	GPtrArray *lang_array;
	const FcCharSet *charset;
	FT_Library ft;
	FT_Error fterr;
	FT_Face face;

	setlocale(LC_ALL, "");
	g_option_context_add_main_entries(ctxt, entries, NULL);
	if (!g_option_context_parse(ctxt, &argc, &argv, &err)) {
		if (err) {
			g_print("%s (code: %d)\n",
				err->message, err->code);
		} else {
			g_print("Unknown error on parsing options\n");
		}
		exit(1);
	}
	g_option_context_free(ctxt);

	if (argc < 2) {
		g_print("No font specified.\n");
		exit(1);
	}
	lang_array = g_ptr_array_new();
	if (arg_lang == NULL) {
		gchar *lang, *s, *p;

		lang = setlocale(LC_CTYPE, NULL);
		if (lang == NULL || lang[0] == 'C') {
			g_print("E: No lang specified nor unable to guess.\n");
			exit(1);
		}
		s = g_strdup(lang);
		p = strchr(s, '.');
		if (p)
			*p = 0;
		p = strchr(s, '@');
		if (p)
			*p = 0;
		p = strchr(s, '_');
		if (p)
			*p = '-';
		for (p = s; *p != 0; p++)
			*p = g_ascii_tolower(*p);
		g_ptr_array_add(lang_array, g_strdup(s));
		p = strchr(s, '-');
		if (p) {
			*p = 0;
			g_ptr_array_add(lang_array, g_strdup(s));
		}
		g_free(s);
	} else {
		g_ptr_array_add(lang_array, g_strdup(arg_lang));
	}
	if (arg_verbose) {
		g_print("D: entering in the verbose mode\n");
		g_print("D: target font filename: %s\n", argv[1]);
		g_print("D: target lang: ");
		for (i = 0; i < lang_array->len; i++) {
			g_print("%s ", (gchar *)g_ptr_array_index(lang_array, i));
		}
		g_print("\n");
	}
	FcInit();
	fterr = FT_Init_FreeType(&ft);
	if (fterr) {
		g_print("E: Unable to initialize the FreeType library.\n");
		exit(1);
	}
	fterr = FT_New_Face(ft, argv[1], 0, &face);
	if (fterr) {
		g_print("E: Unable to obtain the face information for %s\n", argv[1]);
		exit(1);
	}

	for (i = 0; i < lang_array->len; i++) {
		FcChar8 *slang = g_ptr_array_index(lang_array, i);
		intptr_t *leaves;
		FcChar16 *numbers;

		g_print("I: checking lang `%s'\n", slang);
		charset = FcLangGetCharSet(slang);
		if (charset == NULL) {
			g_print("W: Unable to obtain charset for lang `%s'\n", slang);
			continue;
		}
		leaves = FcCharSetLeaves (charset);
		numbers = FcCharSetNumbers (charset);
		for (i = 0; i < charset->num; i++) {
			intptr_t leaf_offset = leaves[i];
			FcCharLeaf *leaf = FcOffsetToPtr (leaves, leaf_offset, FcCharLeaf);

			for (j = 0; j < 256/32; j++) {
				FcChar32 x = leaf->map[j];

				for (k = 0; k < 32; k++) {
					if ((x & 1) != 0) {
						gunichar c = (numbers[i] << 8) | (j << 5) | k;

						if (FT_Get_Char_Index(face, c) != 0) {
							if (arg_verbose)
								g_print("I: %x\n", c);
						} else {
							g_print("W: U+%X [not supported]\n", c);
						}
					}
					x >>= 1;
				}
			}
		}
	}
	FT_Done_Face(face);

	FcFini();

	return 0;
}