示例#1
0
std::shared_ptr<FontList> make_font_list(std::string resource_dir, const FontConfig& cfg)
{
    std::shared_ptr<FontLoader> loader = new_font_loader(resource_dir);
    if (cfg.list) {
        std::shared_ptr<FontList> list = (cfg.file.empty() ? new_font_list(loader, cfg.path) :
                                          new_font_list(loader, cfg.file, 0, 12, 12));
        print_font_list(list, std::cout);
        return nullptr;
    }

    double w = cfg.size[0], h = cfg.size[1];
    if (w <= 0 || h <= 0)
        w = h = 12;

    if (!cfg.file.empty())
        return new_font_list(loader, cfg.file, 0, w, h);

    std::shared_ptr<FontList> list = new_font_list(loader, cfg.path, FontList::find_BestSize,
                                                   cfg.family, cfg.bold, cfg.italic, w, h);
    if (list)
        return list;

    std::string name =
        (cfg.family.empty() ? "default family name" : "family name '"+cfg.family+"'");
    std::string style = (cfg.bold ? (cfg.italic ? "bold italic" : "bold") :
                         (cfg.italic ? "italic" : "regular"));
    std::cerr << "No font face with "<<name<<" and style '"<<style<<"'\n";
    return nullptr;
}
示例#2
0
int main(int argc, char **argv)
{
    struct GModule *module;
    struct Option *opt1, *opt2, *opt3;
    struct Flag *flag1, *flag2;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("display"));
    G_add_keyword(_("settings"));
    module->description =
	_("Selects the font in which text will be displayed "
	  "on the user's graphics monitor.");

    opt1 = G_define_option();
    opt1->key = "font";
    opt1->type = TYPE_STRING;
    opt1->required = NO;
    opt1->answer = "romans";
    opt1->description = _("Choose new current font");

    opt2 = G_define_standard_option(G_OPT_F_INPUT);
    opt2->key = "path";
    opt2->required = NO;
    opt2->description =
	_("Path to Freetype-compatible font including file name");
    opt2->gisprompt = "old,font,file";

    opt3 = G_define_option();
    opt3->key = "charset";
    opt3->type = TYPE_STRING;
    opt3->required = NO;
    opt3->answer = "UTF-8";
    opt3->description = _("Character encoding");

    flag1 = G_define_flag();
    flag1->key = 'l';
    flag1->description = _("List fonts");

    flag2 = G_define_flag();
    flag2->key = 'v';
    flag2->description = _("List fonts verbosely");

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    /* load the font */
    D_open_driver();

    if (flag1->answer) {	/* List font names */
	print_font_list(stdout, 0);
	D_close_driver();
	exit(EXIT_SUCCESS);
    }

    if (flag2->answer) {	/* List fonts verbosely */
	print_font_list(stdout, 1);
	D_close_driver();
	exit(EXIT_SUCCESS);
    }

    if (opt2->answer) {		/* Full path to freetype font */
	struct stat info;

	/* Check a valid filename has been supplied */
	if (stat(opt2->answer, &info) != 0)
	    G_fatal_error(_("Unable to access font path %s: %s"),
			  opt2->answer, strerror(errno));

	if (!S_ISREG(info.st_mode))
	    G_fatal_error(_("Font path %s is not a file"), opt2->answer);
	else
	    D_font(opt2->answer);
    }
    else if (opt1->answer) {	/* Font name from fontcap */
	int i = 0;

	/* Check the fontname given is valid */
	read_freetype_fonts(0);
	while (i < num_fonts) {
	    if (strcmp(opt1->answer, fonts[i]) == 0) {
		D_font(opt1->answer);
		break;
	    }
	    i++;
	}
	if (i >= num_fonts)
	    G_fatal_error(_("Font name <%s> is invalid. Check font name or consider running 'g.mkfontcap'"),
			  opt1->answer);
    }

    if (opt3->answer)		/* Set character encoding */
	D_encoding(opt3->answer);

    /* add this command to the list */
    D_close_driver();

    exit(EXIT_SUCCESS);
}