$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <GL/gl.h> 00037 #include <vlr/vlrException.h> 00038 00039 #include "vlr/arial.h" 00040 #include "vlr/fontRenderer.h" 00041 00042 namespace vlr { 00043 00044 FontRenderer::FontRenderer() : defaultFont(0), size_(10) { 00045 if (!addFont("arial", arial_resource_data, arial_resource_size)) { 00046 throw VLRException("Cannot add default font resource."); 00047 } 00048 defaultFont = (*fontMap.begin()).second; 00049 // defaultFont->UseDisplayList(false); 00050 } 00051 00052 FontRenderer::~FontRenderer() { 00053 std::map<std::string, FTFont*>::const_iterator fit, fit_end; 00054 00055 for (fit = fontMap.begin(), fit_end = fontMap.end(); fit != fit_end; ++fit) { 00056 delete (*fit).second; 00057 } 00058 } 00059 00060 bool FontRenderer::addFont(const std::string& fileName) { 00061 FTFont* font = NULL; 00062 00063 try { 00064 // font = new FTGLPixmapFont(fileName.c_str()); 00065 font = new FTGLPolygonFont(fileName.c_str()); 00066 } 00067 catch (...) { 00068 return false; 00069 } 00070 00071 font->FaceSize(size_); 00072 00073 //TODO: Extract fontName from file / fileName 00074 std::pair<std::map<std::string, FTFont*>::iterator, bool> pair = fontMap.insert( 00075 std::make_pair(fileName.c_str(), font)); 00076 00077 if (pair.second) { 00078 return true; 00079 } 00080 00081 delete font; 00082 00083 return false; 00084 } 00085 00086 bool FontRenderer::addFont(const std::string& fontName, const unsigned char* mem, size_t memSize) { 00087 FTFont* font = NULL; 00088 00089 if (!mem) { 00090 return false; 00091 } 00092 00093 try { 00094 // font = new FTGLPixmapFont(mem, memSize); 00095 font = new FTGLPolygonFont(mem, memSize); 00096 } 00097 catch (...) { 00098 return false; 00099 } 00100 00101 font->FaceSize(size_); 00102 00103 std::pair<std::map<std::string, FTFont*>::iterator, bool> pair = fontMap.insert(std::make_pair(fontName, font)); 00104 00105 if (pair.second) { 00106 return true; 00107 } 00108 00109 delete font; 00110 00111 return false; 00112 } 00113 00114 void FontRenderer::removeFont(const std::string& fontName) { 00115 std::map<std::string, FTFont*>::iterator fit = fontMap.find(fontName); 00116 00117 if (fit == fontMap.end()) { 00118 return; 00119 } 00120 00121 FTFont* font = (*fit).second; 00122 00123 fontMap.erase(fit); 00124 00125 delete font; 00126 } 00127 00128 void FontRenderer::drawString2D(const std::string& text, float x, float y, const std::string& fontName) { 00129 std::map<std::string, FTFont*>::iterator fit = fontMap.find(fontName); 00130 00131 if (fit == fontMap.end()) { 00132 return; 00133 } 00134 00135 FTFont* font = (*fit).second; 00136 00137 glMatrixMode(GL_MODELVIEW); 00138 glPushMatrix(); 00139 glTranslatef(x, y, 0.); 00140 00141 glScalef(0.1, 0.1, 1); // compatibility scale... 00142 00143 font->Render(text.c_str()); 00144 glPopMatrix(); 00145 } 00146 00147 void FontRenderer::drawString2D(const std::string& text, float x, float y) { 00148 return; // TODO: Fix performance issue 00149 glPushMatrix(); 00150 glTranslatef(x, y, 0.); 00151 00152 glScalef(0.1, 0.1, 1); // compatibility scale... 00153 00154 defaultFont->Render(text.c_str()); 00155 glPopMatrix(); 00156 } 00157 00158 void FontRenderer::drawString2D(const std::string& text, float x, float y, float font_size) { 00159 // return; // TODO: Fix performance issue 00160 glPushMatrix(); 00161 glTranslatef(x, y, 0.); 00162 00163 glScalef(0.1*font_size, 0.1*font_size, 1); // compatibility scale... 00164 00165 defaultFont->Render(text.c_str()); 00166 glPopMatrix(); 00167 } 00168 00169 void FontRenderer::drawString3D(const std::string& text, float x, float y, float z, const std::string& fontName) { 00170 std::map<std::string, FTFont*>::iterator fit = fontMap.find(fontName); 00171 00172 if (fit == fontMap.end()) { 00173 return; 00174 } 00175 00176 FTFont* font = (*fit).second; 00177 00178 glPushMatrix(); 00179 glTranslatef(x, y, z); 00180 00181 glScalef(0.1, 0.1, 1); // compatibility scale... 00182 00183 font->Render(text.c_str()); 00184 glPopMatrix(); 00185 } 00186 00187 void FontRenderer::drawString3D(const std::string& text, float x, float y, float z) { 00188 glPushMatrix(); 00189 glTranslatef(x, y, z); 00190 00191 glScalef(0.1, 0.1, 1); // compatibility scale... 00192 00193 defaultFont->Render(text.c_str()); 00194 glPopMatrix(); 00195 } 00196 00197 void FontRenderer::setFontSize(unsigned int size) { 00198 defaultFont->FaceSize(size_); 00199 size_=size; 00200 } 00201 00202 } // namespace vlr