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
  
     | 
    
      /* 
 *  gretl -- Gnu Regression, Econometrics and Time-series Library
 *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
 * 
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 */
#include "gretl.h"
#include "fontfilter.h"
#define FDEBUG 0
static GtkWidget *font_test_widget;
static PangoContext *font_test_context;
static PangoLanguage *font_test_lang;
int gretl_font_filter_init (void)
{
    font_test_widget = gtk_label_new(NULL);  
    font_test_context = gtk_widget_get_pango_context(font_test_widget); 
    if (font_test_context == NULL) {
	gtk_widget_destroy(font_test_widget);
	font_test_widget = NULL;
	return 1;
    }    
    font_test_lang = pango_language_from_string("eng");
    if (font_test_lang == NULL) {
	gretl_font_filter_cleanup();
	return 1;
    }
    return 0;
}
void gretl_font_filter_cleanup (void)
{
    if (font_test_context != NULL) {
	g_object_unref(G_OBJECT(font_test_context));
	font_test_context = NULL;
    }
    if (font_test_widget != NULL) {
	gtk_widget_destroy(font_test_widget);
	font_test_widget = NULL;
    }
}
enum {
    HACK_LATIN_FONT = 1 << 0,
    HACK_MONO_FONT  = 1 << 1,
    HACK_WEIRD_FONT = 1 << 2
};
/* We test a font for "latin text" compatibility, via the heuristic
   of seeing if it contains the letters 'i' and 'W' in English.  Given the
   latin text characteristic, we then see if the font is monospaced
   using pango_font_family_is_monospace().
*/
static int get_font_characteristics (PangoFontFamily *family,
				     PangoFontDescription *desc)
{
    PangoFont *font;
    int ret = 0;
    font = pango_context_load_font(font_test_context, desc);
    if (font != NULL) {
	PangoCoverage *coverage;
	coverage = pango_font_get_coverage(font, font_test_lang);
	if (coverage != NULL) {
	    if (pango_coverage_get(coverage, 'i') == PANGO_COVERAGE_EXACT &&
		pango_coverage_get(coverage, 'W') == PANGO_COVERAGE_EXACT) {
		ret = HACK_LATIN_FONT;
		if (pango_font_family_is_monospace(family)) {
		    ret |= HACK_MONO_FONT;
		}
	    }
	    pango_coverage_unref(coverage);
	}
	g_object_unref(font);
    }
#if FDEBUG
    fprintf(stderr, " latin %s, monospaced %s\n",
	    (ret & HACK_LATIN_FONT)? "yes" : "no",
	    (ret & HACK_MONO_FONT)? "yes" : "no");
#endif
    return ret;
}
static void font_progress_bar (int i, int nf)
{
    static int (*show_progress) (double, double, int) = NULL;
    static int show = 1;
    if (show && show_progress == NULL) {
	show_progress = gui_get_plugin_function("show_progress");
	if (show_progress == NULL) {
	    show = 0;
	} else {
	    (*show_progress)(0, nf, SP_FONT_INIT);
	}
    }
    if (show) {
	if (i == nf - 1) {
	    (*show_progress)(0, nf, SP_FINISH);
	    show = 0;
	} else if (i > 0) {
	    (*show_progress)(1, nf, SP_NONE);
	}
    }
}
#define weird_font(s) (strstr(s, "arioso") || \
                       strstr(s, "dings") || \
                       strstr(s, "ancery"))
/* If we're on the first run, check the font's characteristics and
   cache the info.  Otherwise just read off the information we cached
   previously.  In either case, return non-zero iff the font validates
   in respect of the criterion in @filter.
*/
int validate_font_family (PangoFontFamily *family,
			  const gchar *famname, 
			  gint i, gint nf,
			  gint filter, gint *err)
{
    static char *fcache;
    static int build_cache;
    int ret;
    if (i >= nf) {
	fprintf(stderr, "validate_font_family: got excess font!\n");
	return 0;
    }
    if (fcache == NULL) {
	fcache = calloc(nf, 1);
	if (fcache == NULL) {
	    *err = 1;
	    return 0;
	} 
	build_cache = 1;
	gretl_font_filter_init();
    }
    if (build_cache) {
#if FDEBUG
	fprintf(stderr, "Checking font family %d, '%s'\n", i, famname);
#endif
	font_progress_bar(i, nf);
	if (weird_font(famname)) {
	    fcache[i] = HACK_WEIRD_FONT;
	} else {
	    gchar *fontname = g_strdup_printf("%s 10", famname);
	    PangoFontDescription *desc = 
		pango_font_description_from_string(fontname);
	    if (desc != NULL) {
		fcache[i] = get_font_characteristics(family, desc);
		pango_font_description_free(desc);
	    }
	    g_free(fontname);
	}
    } 
    if (build_cache && i == nf - 1) {
	gretl_font_filter_cleanup();
	build_cache = 0;
    }
    if (filter == FONT_FILTER_LATIN) {
	ret = fcache[i] & HACK_LATIN_FONT;
    } else if (filter == FONT_FILTER_LATIN_MONO) {
	ret = fcache[i] & HACK_MONO_FONT;
    } else {
	ret = !(fcache[i] & HACK_WEIRD_FONT);
    }
    return ret;
}
int validate_single_font (const PangoFontFamily *family,
			  gint filter)
{
    const gchar *famname = 
	pango_font_family_get_name((PangoFontFamily *) family);
    int ret, fc = 0;
#if FDEBUG
    fprintf(stderr, "Checking font '%s'\n", famname);
#endif
    if (weird_font(famname)) {
	fc = HACK_WEIRD_FONT;
    } else {
	gchar *font = g_strdup_printf("%s 10", famname);
	PangoFontDescription *desc = pango_font_description_from_string(font);
	if (desc != NULL) {
	    fc = get_font_characteristics((PangoFontFamily *) family, desc);
	    pango_font_description_free(desc);
	}
	g_free(font);
    }
    if (filter == FONT_FILTER_LATIN) {
	ret = fc & HACK_LATIN_FONT;
    } else if (filter == FONT_FILTER_LATIN_MONO) {
	ret = fc & HACK_MONO_FONT;
    } else {
	ret = !(fc & HACK_WEIRD_FONT);
    }
    return ret;
}
 
     |