00001 #include "fontserver.h"
00002 #include "font.h"
00003 #include <boost/scoped_array.hpp>
00004 #include <salt/fileclasses.h>
00005 #include <zeitgeist/fileserver/fileserver.h>
00006 #include <zeitgeist/logserver/logserver.h>
00007 #include <zeitgeist/scriptserver/scriptserver.h>
00008 #include <kerosin/openglserver/openglserver.h>
00009
00010 using namespace kerosin;
00011 using namespace std;
00012 using namespace boost;
00013
00014 FontServer::FontServer()
00015 {
00016 FT_Init_FreeType(&mFreeTypeLib);
00017 }
00018
00019 FontServer::~FontServer()
00020 {
00021 mFonts.clear();
00022 }
00023
00024 shared_ptr<kerosin::Font> FontServer::GetFont(const string &name, unsigned int size)
00025 {
00026 shared_ptr<kerosin::Font> theFont = FindFont(name, size);
00027
00028 if(theFont.get() != 0)
00029 {
00030 return theFont;
00031 }
00032
00033
00034
00035 theFont.reset(new Font(*this));
00036
00037 if(! LoadFont(name, size, theFont))
00038 {
00039 return shared_ptr<kerosin::Font>();
00040 }
00041
00042
00043 mFonts.push_back(theFont);
00044 return theFont;
00045 }
00046
00047 shared_ptr<kerosin::Font> FontServer::FindFont(const string &name,
00048 unsigned int size) const
00049 {
00050 for (TFontList::const_iterator i = mFonts.begin(); i != mFonts.end(); ++i)
00051 {
00052 if (
00053 ((*i)->GetName().compare(name) == 0) &&
00054 ((*i)->GetSize() == size)
00055 )
00056 {
00057 return (*i);
00058 }
00059 }
00060
00061 return shared_ptr<kerosin::Font>();
00062 }
00063
00064 bool FontServer::LoadFont(const string &name, unsigned int size,
00065 shared_ptr<kerosin::Font> &font)
00066 {
00067 shared_ptr<salt::RFile> file = GetFile()->Open(name.c_str());
00068
00069 if (file.get() == 0)
00070 {
00071
00072 string fontPath;
00073 if(GetScript()->GetVariable("System.FontPath", fontPath))
00074 {
00075 file = GetFile()->Open((fontPath+name).c_str());
00076 }
00077 }
00078
00079 if (file.get() == 0)
00080 {
00081 GetLog()->Error() << "(FontServer) ERROR: font file '"
00082 << name << "' not found\n";
00083 return false;
00084 }
00085
00086 unsigned int fileSize = file->Size();
00087 scoped_array<unsigned char> buffer(new unsigned char[fileSize]);
00088 file->Read(buffer.get(), fileSize);
00089
00090 FT_Face face;
00091 int error = FT_New_Memory_Face(mFreeTypeLib, buffer.get(), fileSize,
00092 0, &face);
00093
00094 if (error == FT_Err_Unknown_File_Format)
00095 {
00096 GetLog()->Error() << "(FontServer) ERROR: Unknown file format\n";
00097 }
00098 else if (error)
00099 {
00100 GetLog()->Error() << "(FontServer) ERROR: Could not create face\n";
00101 }
00102
00103 if (error)
00104 {
00105 FT_Done_Face(face);
00106 return false;
00107 }
00108
00109 bool ok = font->Init(name, size, face);
00110 FT_Done_Face(face);
00111
00112 return ok;
00113 }
00114
00115 void FontServer::Begin()
00116 {
00117 glEnable(GL_BLEND);
00118 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00119 glDisable(GL_DEPTH_TEST);
00120 glDisable(GL_CULL_FACE);
00121
00122 glMatrixMode(GL_MODELVIEW);
00123 glPushMatrix();
00124 glLoadIdentity();
00125 glTranslatef(0,0,-1);
00126 glColor4f(1,1,1,1);
00127 }
00128
00129 void FontServer::End()
00130 {
00131 glDisable(GL_BLEND);
00132 glMatrixMode(GL_MODELVIEW);
00133 glPopMatrix();
00134 glMatrixMode(GL_PROJECTION);
00135 glPopMatrix();
00136 }