00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <iostream>
00012
00013 #include "glut-demo/glut-demo.h"
00014 #include "glut-font/glut-font.h"
00015 #include "perf/perf.h"
00016 #include "util/file.h"
00017
00018
00019
00020 static const int s_desiredHeight = 36;
00021
00022
00023
00024
00025
00026
00027
00028
00029 static void
00030 writeLine
00031 (
00032 IN glut::Font * font,
00033 IN const char * text
00034 )
00035 {
00036 ASSERT(font, "null");
00037 ASSERT(text, "null");
00038
00039 glut::font_rect_t fr = font->getBoundingRect(text);
00040 if (fr.rise > 0) {
00041 glTranslatef(0, fr.rise, 0);
00042 }
00043
00044 glColor3f(0.3, 0.3, 0.3);
00045 glBegin(GL_LINES);
00046 glVertex3f(-fr.lead, 0, 0);
00047 glVertex3f(fr.trail, 0, 0);
00048 glVertex3f(0, -fr.rise, 0);
00049 glVertex3f(0, fr.drop, 0);
00050 glEnd();
00051 glBegin(GL_LINE_LOOP);
00052 glVertex3f(-fr.lead, -fr.rise, 0);
00053 glVertex3f(fr.trail, -fr.rise, 0);
00054 glVertex3f(fr.trail, fr.drop, 0);
00055 glVertex3f(-fr.lead, fr.drop, 0);
00056 glEnd();
00057
00058 glColor3f(1.0, 0.6, 1.0);
00059 font->display(0, 0, 0, text);
00060
00061 glTranslatef(0, fr.drop + 5, 0);
00062 }
00063
00064
00065
00066 class Drawer : public glut::DemoHost {
00067 public:
00068 Drawer(IN const char * dir) {
00069 ASSERT(dir, "null");
00070 m_fontDir = dir;
00071 m_index = -2;
00072 }
00073 ~Drawer(void) throw() { }
00074
00075
00076 void onInit(void) {
00077
00078 this->addFiles("ttf");
00079
00080
00081 m_fontMgr = glut::FontManager::create();
00082 ASSERT_THROW(m_fontMgr,
00083 "failed to create font manager");
00084
00085 this->updateFont(-1);
00086 }
00087
00088 bool displayAxes(void) { return false; }
00089
00090 void onKey(IN int key, IN int mods) {
00091 int deltaIndex = 0;
00092 switch (key) {
00093 case 'n': deltaIndex = +1; break;
00094 case 'N': deltaIndex = -1; break;
00095 default:
00096
00097 break;
00098 }
00099
00100 int nMax = m_paths.size();
00101 int newIndex = m_index + deltaIndex;
00102 if (newIndex < -1) {
00103 newIndex = nMax - 1;
00104 } else if (newIndex >= nMax) {
00105 newIndex = -1;
00106 }
00107 this->updateFont(newIndex);
00108 }
00109
00110 void display2D(IN int width, IN int height) {
00111 ASSERT(m_currFont, "null");
00112
00113 int nMax = m_paths.size();
00114 const int bufsize = 1024;
00115 char buffer[bufsize];
00116 snprintf(buffer, bufsize, "font: %s",
00117 m_currFont->getName());
00118 glut::Font * defFont = glut::getDefaultFont();
00119 defFont->display(1, 47, 0, buffer);
00120 snprintf(buffer, bufsize, "font %d of %d",
00121 m_index + 1, nMax);
00122 defFont->display(1, 62, 0, buffer);
00123 snprintf(buffer, bufsize, "font size: %4.1f point",
00124 m_currFont->getFaceSize());
00125 defFont->display(1, 77, 0, buffer);
00126 defFont->display(1, 92, 0, "(n)ext font");
00127 int yStart = 92 + 16;
00128
00129 glMatrixMode(GL_MODELVIEW);
00130 glPushMatrix();
00131 glTranslatef(16, yStart, 0);
00132 writeLine(m_currFont, "Hello World!");
00133 writeLine(m_currFont, "This is line 2...");
00134 writeLine(m_currFont, "abcdefghijklmnopqrstuvwxyz");
00135 writeLine(m_currFont, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
00136 writeLine(m_currFont, "0123456789!@#$%^&*()-=_+");
00137 writeLine(m_currFont, "{}[]|\\<>,.?/");
00138 writeLine(m_currFont, "ao");
00139 writeLine(m_currFont, "___");
00140 writeLine(m_currFont, "You have +3 dollars to spend!");
00141 writeLine(m_currFont, "This is not a true story");
00142 writeLine(m_currFont, " ...but it could be.");
00143
00144 glPopMatrix();
00145 }
00146
00147 private:
00148
00149
00150
00151 void addFiles(IN const char * ext) {
00152 ASSERT(ext, "null extension");
00153 VecString files;
00154 walkDirectoryTree(m_fontDir.c_str(), ext, files);
00155 int nFiles = files.size();
00156 for (int i = 0; i < nFiles; ++i) {
00157 const char * path = files[i].c_str();
00158 DPRINTF("Font file: '%s'", path);
00159 m_paths.push_back(path);
00160 }
00161
00162 m_streamMgr =
00163 nstream::getFilesystemManager(m_fontDir.c_str());
00164 ASSERT(m_streamMgr, "null");
00165 }
00166
00167 void updateFont(IN int newIndex) {
00168 if (m_index == newIndex)
00169 return;
00170 m_index = newIndex;
00171
00172 int nMax = m_paths.size();
00173 const char * path = NULL;
00174 if (m_index >= 0 && m_index < nMax) {
00175 path = m_paths[m_index].c_str();
00176 }
00177
00178
00179 smart_ptr<nstream::Stream> stream;
00180 if (path) {
00181 stream =
00182 nstream::openNamedStream(m_streamMgr, path);
00183 ASSERT(stream,
00184 "failed to open font stream: %s", path);
00185 }
00186 m_currFont = m_fontMgr->getFont(stream);
00187 ASSERT(m_currFont, "null font?");
00188
00189
00190 scaleFontToPixelHeight(m_currFont, "M", s_desiredHeight);
00191 }
00192
00193
00194 std::string m_fontDir;
00195 VecString m_paths;
00196 smart_ptr<glut::FontManager> m_fontMgr;
00197 smart_ptr<nstream::Manager> m_streamMgr;
00198 smart_ptr<glut::Font> m_currFont;
00199 int m_index;
00200 };
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 int
00211 main
00212 (
00213 IN int argc,
00214 IN const char * argv[]
00215 )
00216 {
00217 ASSERT(2 == argc, "Usage: glut-font-test <font-dir>");
00218 const char * font_dir = argv[1];
00219 DPRINTF("Using font directory: '%s'", font_dir);
00220
00221 try {
00222
00223 std::string title = "glut-font test: ";
00224 title += font_dir;
00225
00226
00227 smart_ptr<glut::DemoHost> host = new Drawer(font_dir);
00228 ASSERT(host, "out of memory");
00229 glut::startDemo(argc, argv, title.c_str(), host);
00230
00231 } catch (std::exception& e) {
00232 DPRINTF("Exception: %s", e.what());
00233 }
00234
00235
00236 return -1;
00237 }
00238