1 /* 2 * OpenSCAD (www.openscad.org) 3 * Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and 4 * Marius Kintel <marius@kintel.net> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * As a special exception, you have permission to link this program 12 * with the CGAL library and distribute executables, as long as you 13 * follow the requirements of the GNU GPL in regard to all of the 14 * software in the executable aside from CGAL. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 * 25 */ 26 #pragma once 27 28 #include <map> 29 #include <string> 30 #include <iostream> 31 32 #include <ctime> 33 34 #include <ft2build.h> 35 #include FT_FREETYPE_H 36 #include FT_TRUETYPE_IDS_H 37 38 #include <vector> 39 #include <string> 40 #include <fontconfig/fontconfig.h> 41 42 #include <hb.h> 43 #include <hb-ft.h> 44 45 #include "version_helper.h" 46 47 class FontInfo { 48 public: 49 FontInfo(const std::string &family, const std::string &style, const std::string &file); 50 virtual ~FontInfo(); 51 52 const std::string &get_family() const; 53 const std::string &get_style() const; 54 const std::string &get_file() const; 55 bool operator<(const FontInfo &rhs) const; 56 private: 57 std::string family; 58 std::string style; 59 std::string file; 60 }; 61 62 typedef std::vector<FontInfo> FontInfoList; 63 64 /** 65 * Slow call of the font cache initialization. This is separated here so it 66 * can be passed to the GUI to run in a separate thread while showing a 67 * progress dialog. 68 */ 69 class FontCacheInitializer { 70 public: FontCacheInitializer(FcConfig * config)71 FontCacheInitializer(FcConfig *config) : config(config) { } run()72 void run() { FcConfigBuildFonts(config); } 73 private: 74 FcConfig *config; 75 }; 76 77 class FontCache { 78 public: 79 const static std::string DEFAULT_FONT; 80 const static unsigned int MAX_NR_OF_CACHE_ENTRIES = 3; 81 82 FontCache(); 83 virtual ~FontCache(); 84 85 bool is_init_ok() const; 86 FT_Face get_font(const std::string &font); 87 bool is_windows_symbol_font(const FT_Face &face) const; 88 void register_font_file(const std::string &path); 89 void clear(); 90 FontInfoList *list_fonts() const; 91 const std::string get_freetype_version() const; 92 93 static FontCache *instance(); 94 95 typedef void (InitHandlerFunc)(FontCacheInitializer *initializer, void *userdata); 96 static void registerProgressHandler(InitHandlerFunc *handler, void *userdata = nullptr); 97 98 private: 99 typedef std::pair<FT_Face, std::time_t> cache_entry_t; 100 typedef std::map<std::string, cache_entry_t> cache_t; 101 102 static FontCache *self; 103 static InitHandlerFunc *cb_handler; 104 static void *cb_userdata; 105 106 static void defaultInitHandler(FontCacheInitializer *delegate, void *userdata); 107 108 bool init_ok; 109 cache_t cache; 110 FcConfig *config; 111 FT_Library library; 112 113 void check_cleanup(); 114 void dump_cache(const std::string &info); 115 116 void add_font_dir(const std::string &path); 117 void init_pattern(FcPattern *pattern) const; 118 119 FT_Face find_face(const std::string &font) const; 120 FT_Face find_face_fontconfig(const std::string &font) const; 121 bool try_charmap(FT_Face face, int platform_id, int encoding_id) const; 122 }; 123 124