static void font_folder_list(std::ofstream& fout, std::string path)
{
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir (path.c_str())) != NULL) {
        bool found = false;
        while (!found && (ent = readdir (dir)) != NULL) {
            if( 0 == strcmp( ent->d_name, "." ) ||
                0 == strcmp( ent->d_name, ".." ) ) {
                continue;
            }
            #if (defined _WIN32 || defined WINDOWS)
                std::string f = path + "\\" + ent->d_name;
            #else
                std::string f = path + "/" + ent->d_name;
            #endif

            struct stat stat_buffer;
            if( stat( f.c_str(), &stat_buffer ) == -1 ) {
                continue;
            }
            if( S_ISDIR(stat_buffer.st_mode) ) {
                font_folder_list( fout, f );
                continue;
            }
            TTF_Font* fnt = TTF_OpenFont(f.c_str(), 12);
            long nfaces = 0;
            if(fnt != NULL) {
                nfaces = TTF_FontFaces(fnt);
                TTF_CloseFont(fnt);
                fnt = NULL;
            }
            for(long i = 0; i < nfaces; i++) {
                fnt = TTF_OpenFontIndex(f.c_str(), 12, i);
                if(fnt != NULL) {
                    char *fami = TTF_FontFaceFamilyName(fnt);
                    char *style = TTF_FontFaceStyleName(fnt);
                    bool isbitmap = (0 == strcasecmp(".fon", f.substr(f.length() - 4).c_str()) );
                    if(fami != NULL && (!isbitmap || i == 0) ) {
                        fout << fami << std::endl;
                        fout << f << std::endl;
                        fout << i << std::endl;
                    }
                    if(fami != NULL && style != NULL && 0 != strcasecmp(style, "Regular")) {
                        if(!isbitmap) {
                            fout << fami << " " << style << std::endl;
                            fout << f << std::endl;
                            fout << i << std::endl;
                        }
                    }
                    TTF_CloseFont(fnt);
                    fnt = NULL;
                }
            }
        }
        closedir (dir);
    }

}
static void save_font_list()
{
	std::ofstream fout("data/fontlist.txt", std::ios_base::trunc);

	font_folder_list(fout, "data");
	font_folder_list(fout, "data/font");

#if (defined _WIN32 || defined WINDOWS)
	char buf[256];
	GetSystemWindowsDirectory(buf, 256);
	strcat(buf, "\\fonts");
	font_folder_list(fout, buf);
#elif (defined _APPLE_ && defined _MACH_)

	/*
	// Well I don't know how osx actually works ....
	font_folder_list(fout, "/System/Library/Fonts");
	font_folder_list(fout, "/Library/Fonts");

	wordexp_t exp;
	wordexp("~/Library/Fonts", &exp, 0);
	font_folder_list(fout, exp.we_wordv[0]);
	wordfree(&exp);*/
#elif (defined linux || defined __linux)
	font_folder_list(fout, "/usr/share/fonts");
	font_folder_list(fout, "/usr/local/share/fonts");
	wordexp_t exp;
	wordexp("~/.fonts", &exp, 0);
	font_folder_list(fout, exp.we_wordv[0]);
	wordfree(&exp);
#endif
	//TODO: other systems

	fout << "end of list" << std::endl;

}