NCBI C++ ToolKit
ftglfontmanager.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ftglfontmanager.cpp 30755 2014-07-21 20:20:08Z falkrb $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Bob Falk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
34 
35 #include <corelib/ncbistr.hpp>
36 #include <corelib/ncbifile.hpp>
37 
38 #ifdef HAVE_LIBFTGL
39 #include <FTGL/ftgl.h>
40 #endif
41 
42 
44 
46 {
47  static CFtglFontManager tm;
48  return tm;
49 }
50 
52 {
53  // Since this object is a singleton, memory (especially OpenGL) may already
54  // be cleaned up by the system by the time this dtor is called. User should call
55  // 'Clear' to delete fonts.
56  /*
57  Clear();
58  */
59 }
60 
62 {
63  FontIter font;
64  for(font = fonts.begin(); font != fonts.end(); ++font)
65  {
66  delete (*font).second;
67  }
68 
69  fonts.clear();
70 }
71 
72 void CFtglFontManager::SetFontPath(const string& p)
73 {
74  m_FontPath = p;
76 
77  // If it is not blank, make sure there is a termination separator, e.g. '/':
78  if (!NStr::IsBlank(m_FontPath)) {
81  }
82  m_FontPath += "fonts";
84  }
85 }
86 
87 FTFont* CFtglFontManager::GetFont(const char *filename,
88  unsigned int size,
89  EFtglFontType ft)
90 {
91 #ifdef HAVE_LIBFTGL
92  char buf[256];
93  sprintf(buf, "%s %i %i", filename, size, ft);
94  string fontKey = string(buf);
95 
96  FontIter result = fonts.find(fontKey);
97  if(result != fonts.end())
98  {
99  //_TRACE("Found font " << fontKey << " in list");
100  return result->second;
101  }
102 
103  string fullname = m_FontPath + string(filename);
104 
105  // Look for file first in default directory. If not found
106  // try to use filename as the full path name. If neither
107  // work, return null
108  {
109  CDirEntry f1(fullname);
110  if (!f1.Exists()) {
111  CDirEntry f2(filename);
112  if (!f2.Exists()) {
113  ERR_POST(Error << "Font file: " << fullname <<
114  " failed to open. To change font directory call \
115 CFtglFontManager::Instance().SetFontPath(\"..font dir..\") before loading fonts.");
116  return NULL;
117  }
118  else {
119  fullname = filename;
120  }
121  }
122  }
123 
124  FTFont* font = NULL;
125  switch (ft) {
126  case eTextureFont:
127  font = new FTTextureFont(fullname.c_str());
128  break;
129  case ePixmapFont:
130  font = new FTPixmapFont(fullname.c_str());
131  break;
132  case eBitmapFont:
133  font = new FTBitmapFont(fullname.c_str());
134  break;
135  case eOutlineFont:
136  font = new FTOutlineFont(fullname.c_str());
137  break;
138  case ePolygonFont:
139  font = new FTPolygonFont(fullname.c_str());
140  break;
141  case eExtrudedFont:
142  font = new FTExtrudeFont(fullname.c_str());
143  break;
144  };
145 
146  //_TRACE("Created font " << fontKey);
147 
148  if(font->Error())
149  {
150  ERR_POST(Error << "Font file: " << fullname <<
151  " failed to open. Default font path: " << m_FontPath );
152  delete font;
153  return NULL;
154  }
155 
156  if(!font->FaceSize(size, m_DeviceResolution))
157  {
158  ERR_POST(Error << "Font " << filename << " failed to set size " << size);
159  delete font;
160  return NULL;
161  }
162  // If we want to actually use device resolution, have to call this twice (or apply fix)
163  // and use a fake device resolution the first time (since it is ignored). But for now we ignore
164  // device resolution because all the non-font rendering ignores it as well and we would want
165  // all our rendering to scale together.
166  // font->FaceSize(size, m_DeviceResolution);
167 
168  font->UseDisplayList(true);
169 
170  fonts[fontKey] = font;
171 
172  return font;
173 #else
174  ERR_POST(Error << "FTGL is unavailable.");
175  return NULL;
176 #endif
177 }
178 
char * buf
unsigned int m_DeviceResolution
target device (screen) resolution in pixels per inch (defaults to 72)
FontList fonts
All the currently loaded fonts.
static bool IsBlank(const CTempString &str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:102
CDirEntry –.
Definition: ncbifile.hpp:241
FTFont * GetFont(const char *filename, unsigned int size, EFtglFontType ft=eTextureFont)
string
Definition: cgiapp.hpp:407
static char GetPathSeparator(void)
Get path separator symbol specific for the current platform.
Definition: ncbifile.cpp:419
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
static bool IsPathSeparator(const char c)
Check whether a character "c" is a path separator symbol specific for the current platform...
Definition: ncbifile.cpp:425
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:185
static CFtglFontManager & Instance()
Get an instance of the manager so you can retrieve fonts.
virtual bool Exists(void) const
Check the entry existence.
Definition: ncbifile.hpp:3724
int size
string m_FontPath
Directory from which to read font files.
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1014
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock, CFileIO, CFileReader, CFileWriter, CFileReaderWriter, CFileException.
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:2899
void SetFontPath(const string &p)
This path will be prepended to filename when calling GetFont It should be set to "ToStdString(CSysPat...
FontList::const_iterator FontIter
void Clear()
Delete all current fonts.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
EFtglFontType
FTGL can create different types of fonts from files read in using freetype, but the texture fonts are...
Modified on Fri Aug 28 13:22:42 2015 by modify_doxy.py rev. 426318