00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifdef EMAN2_USING_FTGL
00036
00037 #include "emftgl.h"
00038 using namespace EMAN;
00039
00040 #include "exception.h"
00041 #include <iostream>
00042 using std::cerr;
00043 using std::cout;
00044 using std::endl;
00045
00046 #include <algorithm>
00047 #include <FTGL/FTGLExtrdFont.h>
00048 #include <FTGL/FTGLPixmapFont.h>
00049 #include <FTGL/FTGLTextureFont.h>
00050 #include <FTGL/FTGLBitmapFont.h>
00051 #include <FTGL/FTGLOutlineFont.h>
00052 #include <FTGL/FTGLPolygonFont.h>
00053
00054
00055
00056
00057
00058
00059
00060 void EMFTGL::render_string(const string& message) {
00061
00062 FTFont* font = fm.get_font(mode,font_file_name,face_size,depth,true);
00063
00064 if (font == 0) {
00065 cerr << "Couldn't open font, no action taken. Current font is " << font_file_name << endl;
00066 return;
00067 }
00068
00069 font->Render(message.c_str());
00070 }
00071
00072 vector<float> EMFTGL::bounding_box(const string& message)
00073 {
00074 FTFont* font = fm.get_font(mode,font_file_name,face_size,depth,true);
00075 if (font == 0) {
00076 cerr << "Couldn't open font, no action taken. Current font is " << font_file_name << endl;
00077 return vector<float>();
00078 }
00079 vector<float> bounds(6);
00080 font->BBox(message.c_str(),bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]);
00081 return bounds;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090 EMFTGL::EMFTGLManager::EMFTGLManager() : font_instances() { }
00091
00092
00093 EMFTGL::EMFTGLManager::~EMFTGLManager() {
00094
00095 for (vector<EMFTGLFontInstance*>::iterator it = font_instances.begin(); it != font_instances.end(); ++it ) {
00096 delete (*it);
00097 *it = 0;
00098 }
00099 font_instances.clear();
00100 }
00101
00102 FTFont* EMFTGL::EMFTGLManager::get_font(EMFTGL::FontMode mode, const string& file_name, const unsigned int face_size, const unsigned int depth, const bool use_dl)
00103 {
00104 for (vector<EMFTGLFontInstance*>::const_iterator it = font_instances.begin(); it != font_instances.end(); ++it ) {
00105 if ((*it)->params_match(mode,file_name,face_size,depth,use_dl)) {
00106 return (*it)->get_font();
00107 }
00108 }
00109
00110 EMFTGLFontInstance* fi = new EMFTGLFontInstance(mode,file_name,face_size,depth,use_dl);
00111 font_instances.push_back(fi);
00112 return fi->get_font();
00113 }
00114
00115 EMFTGL::EMFTGLFontInstance::~EMFTGLFontInstance(){
00116 if (font != 0) {
00117 delete font;
00118 font = 0;
00119 }
00120 }
00121
00122 EMFTGL::EMFTGLFontInstance::EMFTGLFontInstance(EMFTGL::FontMode mode, const string& file_name, const unsigned int _face_size, const unsigned int _depth, const bool use_dl) :
00123 font_mode(mode), font_file_name(file_name), face_size(_face_size), depth(_depth),use_display_lists(use_dl), font(0)
00124 {
00125 if (mode == EMFTGL::PIXMAP) {
00126 font = new FTGLPixmapFont(font_file_name.c_str());
00127 }
00128 else if (mode == EMFTGL::TEXTURE) {
00129 font = new FTGLTextureFont(font_file_name.c_str());
00130 }
00131 else if ( mode == EMFTGL::EXTRUDE ) {
00132 font = new FTGLExtrdFont(font_file_name.c_str());
00133 font->Depth(depth);
00134 }
00135 else if ( mode == EMFTGL::BITMAP ) {
00136 font = new FTGLBitmapFont(font_file_name.c_str());
00137 font->Depth(depth);
00138 }
00139 else if ( mode == EMFTGL::POLYGON ) {
00140 font = new FTGLPolygonFont(font_file_name.c_str());
00141 font->Depth(depth);
00142 }
00143 else if ( mode == EMFTGL::OUTLINE ) {
00144 font = new FTGLOutlineFont(font_file_name.c_str());
00145 font->Depth(depth);
00146 }
00147 else {
00148 LOGERR("Error, unsupported mode ");
00149 return;
00150 }
00151
00152 if (font->Error()) {
00153 delete font;
00154 LOGERR( string("Could not open font file " + font_file_name).c_str());
00155 font = 0;
00156 }
00157 else {
00158 font->UseDisplayList(use_display_lists);
00159 font->FaceSize(face_size);
00160 }
00161 }
00162
00163 bool EMFTGL::EMFTGLFontInstance::params_match(EMFTGL::FontMode mode, const string& file_name, const unsigned int face_size, const unsigned int depth, const bool use_dl) {
00164 if ( this->font_mode == mode && this->font_file_name == file_name &&
00165 this->face_size == face_size && this->depth == depth && this->use_display_lists == use_dl ) return true;
00166
00167 return false;
00168 }
00169
00170
00171
00172 #endif //EMAN2_USING_FTGL