Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

emftgl.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: David Woolford, 7/16/2008 (woolford@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  * 
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  * 
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  * 
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  * 
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  * 
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 //static init
00054 // string  EMFTGL::font_file_name = "/usr/share/fonts/liberation/LiberationSans-Regular.ttf";
00055 // unsigned int EMFTGL::face_size = 32;
00056 // unsigned int EMFTGL::depth = 32;
00057 // EMFTGL::FontMode EMFTGL::mode = EMFTGL::TEXTURE;
00058 // EMFTGL::EMFTGLManager EMFTGL::fm;
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 // EMFTGL::EMFTGLManager& EMFTGL::EMFTGLManager::instance() {
00086 //      static EMFTGLManager fm;
00087 //      return fm;
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         // If we make it here there was no match
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

Generated on Tue Jun 11 13:46:14 2013 for EMAN2 by  doxygen 1.3.9.1