xoreos  0.0.5
fontman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/scopedptr.h"
26 #include "src/common/error.h"
27 #include "src/common/systemfonts.h"
28 
34 
36 
37 namespace Graphics {
38 
39 namespace Aurora {
40 
41 const char *kSystemFontMono = "_xoreosSystemFontMono";
42 
44 }
45 
47  clear();
48 }
49 
52 
53  _fonts.clear();
54 
56  _aliases.clear();
57 }
58 
61 
62  _format = format;
63 }
64 
65 void FontManager::addAlias(const Common::UString &alias, const Common::UString &realName) {
67 
68  _aliases[alias] = realName;
69 }
70 
71 bool FontManager::hasFont(const Common::UString &name, int height) {
73 
74  FontMap::const_iterator font = _fonts.find(getIndexName(name, height));
75 
76  return font != _fonts.end();
77 }
78 
81 
82  Common::ScopedPtr<ManagedFont> managedFont(new ManagedFont(font));
83 
84  std::pair<FontMap::iterator, bool> result = _fonts.insert(std::make_pair(name, managedFont.get()));
85  if (!result.second)
86  throw Common::Exception("Font \"%s\" already exists", name.c_str());
87 
88  managedFont.release();
89  FontMap::iterator fontIterator = result.first;
90 
91  return FontHandle(fontIterator);
92 }
93 
94 FontHandle FontManager::get(const Common::UString &name, int height) {
95  return get(_format, name, height);
96 }
97 
98 FontHandle FontManager::get(FontFormat format, const Common::UString &name, int height) {
100 
101  Common::UString aliasName = getAliasName(name);
102  Common::UString indexName = getIndexName(name, height);
103 
104  FontMap::iterator font = _fonts.find(indexName);
105  if (font == _fonts.end()) {
106  std::pair<FontMap::iterator, bool> result;
107 
108  ManagedFont *f = createFont(format, aliasName, height);
109 
110  result = _fonts.insert(std::make_pair(indexName, f));
111 
112  font = result.first;
113  }
114 
115  return FontHandle(font);
116 }
117 
120 
121  FontMap::iterator font = _fonts.find(getIndexName(name, height));
122  if (font != _fonts.end())
123  return FontHandle(font);
124 
125  return FontHandle();
126 }
127 
129  std::map<Common::UString, Common::UString>::iterator realName = _aliases.find(name);
130  if (realName != _aliases.end())
131  return realName->second;
132 
133  return name;
134 }
135 
137  // Lock up the name in our alias map first
138  name = getAliasName(name);
139 
140  if (height <= 0)
141  return name;
142 
143  // If we have been given a height, the font is indexed with the height
144  return Common::UString::format("%s-%d", name.c_str(), height);
145 }
146 
147 void FontManager::assign(FontHandle &font, const FontHandle &from) {
149 
150  font._empty = from._empty;
151  font._it = from._it;
152 
153  if (!font._empty)
154  font._it->second->referenceCount++;
155 }
156 
159 
160  if (!font._empty && (font._it != _fonts.end()))
161  if (--font._it->second->referenceCount == 0)
162  _fonts.erase(font._it);
163 
164  font._empty = true;
165  font._it = _fonts.end();
166 }
167 
169  const Common::UString &name, int height) {
170 
171  try {
172  if (name == kSystemFontMono)
173  return new ManagedFont(new TTFFont(Common::getSystemFontMono(), height));
174 
175  if (format == kFontFormatUnknown)
176  throw Common::Exception("Font format unknown");
177 
178  if (format == kFontFormatTexture)
179  return new ManagedFont(new TextureFont(name));
180  if (format == kFontFormatABC)
181  return new ManagedFont(new ABCFont(name));
182  if (format == kFontFormatTTF)
183  return new ManagedFont(new TTFFont(name, height));
184  if (format == kFontFormatNFTR)
185  return new ManagedFont(new NFTRFont(name));
186 
187  throw Common::Exception("Invalid font format %d", format);
188 
189  } catch (Common::Exception &e) {
190  e.add("Failed to create font \"%s\" (%d)", name.c_str(), format);
191  throw;
192  }
193 
194  return 0;
195 }
196 
197 } // End of namespace Aurora
198 
199 } // End of namespace Graphics
static ManagedFont * createFont(FontFormat format, const Common::UString &name, int height)
Definition: fontman.cpp:168
SeekableReadStream * getSystemFontMono()
Return a stream of the monospaced system font TTF.
ABC/SBM font, used by Jade Empire.
Definition: fontman.h:48
A managed font, storing how often it&#39;s referenced.
Definition: fonthandle.h:42
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
void erase(typename std::map< Key, T *, Compare >::iterator position)
Definition: ptrmap.h:56
NFTR font, used by Sonic.
Definition: fontman.h:50
A class holding an UTF-8 string.
Definition: ustring.h:48
A TrueType font.
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
TTF font, used by NWN2.
Definition: fontman.h:49
FontHandle getIfExist(const Common::UString &name, int height=0)
Retrieve this named font, returning an empty handle if it&#39;s not managed.
Definition: fontman.cpp:118
A handle to a font.
Definition: fonthandle.h:52
std::map< Common::UString, Common::UString > _aliases
Definition: fontman.h:83
A texture font, as used by NWN and KotOR/KotOR2.
Definition: texturefont.h:51
The Aurora font manager.
Exception that provides a stack of explanations.
Definition: error.h:36
bool hasFont(const Common::UString &name, int height=0)
Does this named managed font exist?
Definition: fontman.cpp:71
A simple scoped smart pointer template.
Built-in system fonts.
An ABC/SBM font, as used by Jade Empire.
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
FontFormat
The format of a font.
Definition: fontman.h:45
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
void addAlias(const Common::UString &alias, const Common::UString &realName)
Add an alias for a specific font name.
Definition: fontman.cpp:65
void clear()
Definition: ptrmap.h:48
const char * kSystemFontMono
Identifier used for the monospaced system font.
Definition: fontman.cpp:41
void release(FontHandle &handle)
Definition: fontman.cpp:157
Nintendo&#39;s NFTR font, found in Sonic.
Common::UString getIndexName(Common::UString name, int height)
Definition: fontman.cpp:136
An abstract font.
Definition: font.h:39
StackException Exception
Definition: error.h:59
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void clear()
Remove and delete all managed fonts.
Definition: fontman.cpp:50
void assign(FontHandle &font, const FontHandle &from)
Definition: fontman.cpp:147
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
FontHandle get(FontFormat format, const Common::UString &name, int height=0)
Retrieve this named font in this format, loading it if it&#39;s not yet managed.
Definition: fontman.cpp:98
Textured font, used by NWN and KotOR/KotOR2.
Definition: fontman.h:47
The global Aurora font manager.
Definition: fontman.h:54
Common::UString getAliasName(const Common::UString &name)
Definition: fontman.cpp:128
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
void setFormat(FontFormat format)
Load fonts in this format by default.
Definition: fontman.cpp:59
Unknown font format.
Definition: fontman.h:46
FontHandle add(Font *font, const Common::UString &name)
Add this font to the FontManager.
Definition: fontman.cpp:79
A texture font, as used by NWN and KotOR/KotOR2.
FontMap::iterator _it
Definition: fonthandle.h:69
An ABC/SBM font, as used by Jade Empire.
Definition: abcfont.h:50