EMAN2
emftgl.cpp
Go to the documentation of this file.
1/*
2 * Author: David Woolford, 7/16/2008 (woolford@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31#ifdef USE_FTGL
32
33#include "emftgl.h"
34using namespace EMAN;
35
36#include "exception.h"
37#include <iostream>
38using std::cerr;
39using std::cout;
40using std::endl;
41
42#include <algorithm>
43#include <FTGL/FTGLExtrdFont.h>
44#include <FTGL/FTGLPixmapFont.h>
45#include <FTGL/FTGLTextureFont.h>
46#include <FTGL/FTGLBitmapFont.h>
47#include <FTGL/FTGLOutlineFont.h>
48#include <FTGL/FTGLPolygonFont.h>
49//static init
50// string EMFTGL::font_file_name = "/usr/share/fonts/liberation/LiberationSans-Regular.ttf";
51// unsigned int EMFTGL::face_size = 32;
52// unsigned int EMFTGL::depth = 32;
53// EMFTGL::FontMode EMFTGL::mode = EMFTGL::TEXTURE;
54// EMFTGL::EMFTGLManager EMFTGL::fm;
55
56void EMFTGL::render_string(const string& message) {
57
58 FTFont* font = fm.get_font(mode,font_file_name,face_size,depth,true);
59
60 if (font == 0) {
61 cerr << "Couldn't open font, no action taken. Current font is " << font_file_name << endl;
62 return;
63 }
64
65 font->Render(message.c_str());
66}
67
68vector<float> EMFTGL::bounding_box(const string& message)
69{
70 FTFont* font = fm.get_font(mode,font_file_name,face_size,depth,true);
71 if (font == 0) {
72 cerr << "Couldn't open font, no action taken. Current font is " << font_file_name << endl;
73 return vector<float>();
74 }
75 vector<float> bounds(6);
76 font->BBox(message.c_str(),bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]);
77 return bounds;
78}
79
80
81// EMFTGL::EMFTGLManager& EMFTGL::EMFTGLManager::instance() {
82// static EMFTGLManager fm;
83// return fm;
84// }
85
86EMFTGL::EMFTGLManager::EMFTGLManager() : font_instances() { }
87
88
89EMFTGL::EMFTGLManager::~EMFTGLManager() {
90
91 for (vector<EMFTGLFontInstance*>::iterator it = font_instances.begin(); it != font_instances.end(); ++it ) {
92 delete (*it);
93 *it = 0;
94 }
95 font_instances.clear();
96}
97
98FTFont* EMFTGL::EMFTGLManager::get_font(EMFTGL::FontMode mode, const string& file_name, const unsigned int face_size, const unsigned int depth, const bool use_dl)
99{
100 for (vector<EMFTGLFontInstance*>::const_iterator it = font_instances.begin(); it != font_instances.end(); ++it ) {
101 if ((*it)->params_match(mode,file_name,face_size,depth,use_dl)) {
102 return (*it)->get_font();
103 }
104 }
105 // If we make it here there was no match
106 EMFTGLFontInstance* fi = new EMFTGLFontInstance(mode,file_name,face_size,depth,use_dl);
107 font_instances.push_back(fi);
108 return fi->get_font();
109}
110
112 if (font != 0) {
113 delete font;
114 font = 0;
115 }
116}
117
118EMFTGL::EMFTGLFontInstance::EMFTGLFontInstance(EMFTGL::FontMode mode, const string& file_name, const unsigned int _face_size, const unsigned int _depth, const bool use_dl) :
119 font_mode(mode), font_file_name(file_name), face_size(_face_size), depth(_depth),use_display_lists(use_dl), font(0)
120{
121 if (mode == EMFTGL::PIXMAP) {
122 font = new FTGLPixmapFont(font_file_name.c_str());
123 }
124 else if (mode == EMFTGL::TEXTURE) {
125 font = new FTGLTextureFont(font_file_name.c_str());
126 }
127 else if ( mode == EMFTGL::EXTRUDE ) {
128 font = new FTGLExtrdFont(font_file_name.c_str());
129 font->Depth(depth);
130 }
131 else if ( mode == EMFTGL::BITMAP ) {
132 font = new FTGLBitmapFont(font_file_name.c_str());
133 font->Depth(depth);
134 }
135 else if ( mode == EMFTGL::POLYGON ) {
136 font = new FTGLPolygonFont(font_file_name.c_str());
137 font->Depth(depth);
138 }
139 else if ( mode == EMFTGL::OUTLINE ) {
140 font = new FTGLOutlineFont(font_file_name.c_str());
141 font->Depth(depth);
142 }
143 else {
144 LOGERR("Error, unsupported mode ");
145 return;
146 }
147
148 if (font->Error()) {
149 delete font;
150 LOGERR( string("Could not open font file " + font_file_name).c_str());
151 font = 0;
152 }
153 else {
154 font->UseDisplayList(use_display_lists);
155 font->FaceSize(face_size);
156 }
157}
158
159bool EMFTGL::EMFTGLFontInstance::params_match(EMFTGL::FontMode mode, const string& file_name, const unsigned int face_size, const unsigned int depth, const bool use_dl) {
160 if ( this->font_mode == mode && this->font_file_name == file_name &&
161 this->face_size == face_size && this->depth == depth && this->use_display_lists == use_dl ) return true;
162
163 return false;
164}
165
166
167
168#endif //USE_FTGL
EMFTGLFontInstance(EMFTGL::FontMode mode, const string &file_name, const unsigned int face_size, const unsigned int d, const bool use_dl)
Constructor - must supply the 5 important parameters.
EMFTGL::FontMode font_mode
Disallow copy construction.
Definition: emftgl.h:200
vector< EMFTGLFontInstance * > font_instances
Disallow copy construction.
Definition: emftgl.h:231
FTFont * get_font(EMFTGL::FontMode mode, const string &file_name, const unsigned int face_size, const unsigned int d, const bool use_dl)
Get a font with the associated parameters.
unsigned int depth
Definition: emftgl.h:167
FontMode mode
Definition: emftgl.h:170
string font_file_name
Disallow copy construction.
Definition: emftgl.h:165
vector< float > bounding_box(const string &message)
Obtains the bounding box of the given message, as would be rendered using the current state variables...
FontMode
FontModes correspond to the different FTFont types - this correspondence is EXTRUDE - FTGLExtrdFont,...
Definition: emftgl.h:98
bool use_display_lists
Definition: emftgl.h:168
unsigned int face_size
Definition: emftgl.h:166
EMFTGLManager fm
Definition: emftgl.h:234
#define LOGERR
Definition: log.h:51
E2Exception class.
Definition: aligner.h:40