00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "fontPool.h"
00020 #include "config_util.h"
00021 #include "config_express.h"
00022 #include "virtualFileSystem.h"
00023 #include "nodePath.h"
00024 #include "loader.h"
00025
00026 FontPool *FontPool::_global_ptr = (FontPool *)NULL;
00027
00028 static Loader model_loader;
00029
00030
00031
00032
00033
00034
00035 bool FontPool::
00036 ns_has_font(const string &str) {
00037 Filename filename;
00038 int face_index;
00039 lookup_filename(str, filename, face_index);
00040
00041 Fonts::const_iterator ti;
00042 ti = _fonts.find(filename);
00043 if (ti != _fonts.end()) {
00044
00045 return true;
00046 }
00047
00048 return false;
00049 }
00050
00051
00052
00053
00054
00055
00056 TextFont *FontPool::
00057 ns_load_font(const string &str) {
00058 Filename filename;
00059 int face_index;
00060 lookup_filename(str, filename, face_index);
00061
00062 Fonts::const_iterator ti;
00063 ti = _fonts.find(filename);
00064 if (ti != _fonts.end()) {
00065
00066 return (*ti).second;
00067 }
00068
00069 text_cat.info()
00070 << "Loading font " << filename << "\n";
00071
00072
00073
00074
00075 PT(TextFont) font;
00076
00077 string extension = filename.get_extension();
00078 if (extension.empty() || extension == "egg" || extension == "bam") {
00079 PT(PandaNode) node = model_loader.load_sync(filename);
00080 if (node != (PandaNode *)NULL) {
00081
00082
00083 NodePath np(node);
00084 np.adjust_all_priorities(1);
00085 font = new StaticTextFont(node);
00086 }
00087 }
00088
00089 #ifdef HAVE_FREETYPE
00090 if (font == (TextFont *)NULL || !font->is_valid()) {
00091
00092
00093 font = new DynamicTextFont(filename, face_index);
00094 }
00095 #endif
00096
00097 if (font == (TextFont *)NULL || !font->is_valid()) {
00098
00099 return NULL;
00100 }
00101
00102 _fonts[filename] = font;
00103 return font;
00104 }
00105
00106
00107
00108
00109
00110
00111 void FontPool::
00112 ns_add_font(const string &filename, TextFont *font) {
00113
00114 _fonts[filename] = font;
00115 }
00116
00117
00118
00119
00120
00121
00122 void FontPool::
00123 ns_release_font(const string &filename) {
00124 Fonts::iterator ti;
00125 ti = _fonts.find(filename);
00126 if (ti != _fonts.end()) {
00127 _fonts.erase(ti);
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136 void FontPool::
00137 ns_release_all_fonts() {
00138 _fonts.clear();
00139 }
00140
00141
00142
00143
00144
00145
00146 int FontPool::
00147 ns_garbage_collect() {
00148 int num_released = 0;
00149 Fonts new_set;
00150
00151 Fonts::iterator ti;
00152 for (ti = _fonts.begin(); ti != _fonts.end(); ++ti) {
00153 TextFont *font = (*ti).second;
00154 if (font->get_ref_count() == 1) {
00155 if (text_cat.is_debug()) {
00156 text_cat.debug()
00157 << "Releasing " << (*ti).first << "\n";
00158 }
00159 num_released++;
00160 } else {
00161 new_set.insert(new_set.end(), *ti);
00162 }
00163 }
00164
00165 _fonts.swap(new_set);
00166 return num_released;
00167 }
00168
00169
00170
00171
00172
00173
00174 void FontPool::
00175 ns_list_contents(ostream &out) {
00176 out << _fonts.size() << " fonts:\n";
00177 Fonts::iterator ti;
00178 for (ti = _fonts.begin(); ti != _fonts.end(); ++ti) {
00179 TextFont *font = (*ti).second;
00180 out << " " << (*ti).first
00181 << " (count = " << font->get_ref_count() << ")\n";
00182 }
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 void FontPool::
00195 lookup_filename(const string &str,
00196 Filename &filename, int &face_index) {
00197 int colon = (int)str.length() - 1;
00198
00199 while (colon >= 0 && isdigit(str[colon])) {
00200 colon--;
00201 }
00202 if (colon >= 0 && str[colon] == ':') {
00203 string digits = str.substr(colon + 1);
00204 filename = str.substr(0, colon);
00205 face_index = atoi(digits.c_str());
00206
00207 } else {
00208 filename = str;
00209 face_index = 0;
00210 }
00211
00212
00213 if (use_vfs) {
00214 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00215 vfs->resolve_filename(filename, get_model_path());
00216
00217 } else {
00218 filename.resolve_filename(get_model_path());
00219 }
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 FontPool *FontPool::
00229 get_ptr() {
00230 if (_global_ptr == (FontPool *)NULL) {
00231 _global_ptr = new FontPool;
00232 }
00233 return _global_ptr;
00234 }