CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUFontLoader.h
1 //
2 // CUFontLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // fonts. A font asset is identified by both its source file and its size.
7 // The size is required as load time. Hence you may wish to load the same
8 // font file several times.
9 //
10 // As with all of our loaders, this loader is designed to be attached to an
11 // asset manager. In addition, this class uses our standard shared-pointer
12 // architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 //
24 // CUGL zlib License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // Author: Walker White
44 // Version: 1/7/16
45 //
46 #ifndef __CU_FONT_LOADER_H__
47 #define __CU_FONT_LOADER_H__
48 #include <cugl/assets/CULoader.h>
49 #include <cugl/2d/CUFont.h>
50 
51 namespace cugl {
52 
73 class FontLoader : public Loader<Font> {
74 private:
76  CU_DISALLOW_COPY_AND_ASSIGN(FontLoader);
77 
78 protected:
80  int _fontsize;
82  std::string _charset;
83 
84 #pragma mark Asset Loading
85 
99  std::shared_ptr<Font> preload(const std::string& source, const std::string& charset, int size);
100 
118  void materialize(const std::string& key, const std::shared_ptr<Font>& font, LoaderCallback callback);
119 
138  bool read(const std::string& key, const std::string& source,
139  LoaderCallback callback, bool async) override {
140  return read(key,source,_fontsize,callback,async);
141  }
142 
162  bool read(const std::string& key, const std::string& source, int size,
163  LoaderCallback callback, bool async);
164 
189  bool read(const std::shared_ptr<JsonValue>& json, LoaderCallback callback, bool async) override;
190 
191 
192 public:
193 #pragma mark -
194 #pragma mark Constructors
195 
201  FontLoader();
202 
214  void dispose() override {
215  _assets.clear();
216  _loader = nullptr;
217  }
218 
231  static std::shared_ptr<FontLoader> alloc() {
232  std::shared_ptr<FontLoader> result = std::make_shared<FontLoader>();
233  return (result->init() ? result : nullptr);
234  }
235 
247  static std::shared_ptr<FontLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
248  std::shared_ptr<FontLoader> result = std::make_shared<FontLoader>();
249  return (result->init(threads) ? result : nullptr);
250  }
251 
252 #pragma mark -
253 #pragma mark Loading Interface
254 
271  bool load(const std::string& key, const std::string& source, int size) {
272  return read(key, source, size, nullptr, false);
273  }
274 
292  bool load(const char* key, const std::string& source, int size) {
293  return read(std::string(key), source, size, nullptr, false);
294  }
295 
313  bool load(const std::string& key, const char* source, int size) {
314  return read(key, std::string(source), size, nullptr, false);
315  }
316 
334  bool load(const char* key, const char* source, int size) {
335  return read(std::string(key), std::string(source), size, nullptr, false);
336  }
337 
358  void loadAsync(const std::string& key, const std::string& source, int size,
359  LoaderCallback callback) {
360  read(key, source, size, callback, true);
361  }
362 
383  void loadAsync(const char* key, const std::string& source, int size,
384  LoaderCallback callback) {
385  read(std::string(key), source, size, callback, true);
386  }
387 
408  void loadAsync(const std::string& key, const char* source, int size,
409  LoaderCallback callback) {
410  read(key, std::string(source), size, callback, true);
411  }
412 
433  void loadAsync(const char* key, const char* source, int size,
434  LoaderCallback callback) {
435  read(std::string(key), std::string(source), size, callback, true);
436  }
437 
438 #pragma mark -
439 #pragma mark Properties
440 
449  float getFontSize() const { return _fontsize; }
450 
459  void setDefaultSize(float size) { _fontsize = size; }
460 
474  const std::string& getCharacterSet() const { return _charset; }
475 
489  void setCharacterSet(const std::string& charset) { _charset = charset; }
490 };
491 
492 }
493 
494 #endif /* __CU_FONT_LOADER_H__ */
int _fontsize
Definition: CUFontLoader.h:80
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
static std::shared_ptr< FontLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUFontLoader.h:247
float getFontSize() const
Definition: CUFontLoader.h:449
Definition: CUFontLoader.h:73
bool load(const std::string &key, const char *source, int size)
Definition: CUFontLoader.h:313
const std::string & getCharacterSet() const
Definition: CUFontLoader.h:474
void loadAsync(const std::string &key, const char *source, int size, LoaderCallback callback)
Definition: CUFontLoader.h:408
std::shared_ptr< Font > preload(const std::string &source, const std::string &charset, int size)
void setCharacterSet(const std::string &charset)
Definition: CUFontLoader.h:489
static std::shared_ptr< FontLoader > alloc()
Definition: CUFontLoader.h:231
std::unordered_map< std::string, std::shared_ptr< Font > > _assets
Definition: CULoader.h:678
std::function< void(const std::string &key, bool success)> LoaderCallback
Definition: CULoader.h:81
void materialize(const std::string &key, const std::shared_ptr< Font > &font, LoaderCallback callback)
void loadAsync(const char *key, const char *source, int size, LoaderCallback callback)
Definition: CUFontLoader.h:433
std::string _charset
Definition: CUFontLoader.h:82
bool load(const char *key, const char *source, int size)
Definition: CUFontLoader.h:334
void setDefaultSize(float size)
Definition: CUFontLoader.h:459
void loadAsync(const std::string &key, const std::string &source, int size, LoaderCallback callback)
Definition: CUFontLoader.h:358
Definition: CULoader.h:675
void loadAsync(const char *key, const std::string &source, int size, LoaderCallback callback)
Definition: CUFontLoader.h:383
bool load(const std::string &key, const std::string &source, int size)
Definition: CUFontLoader.h:271
void dispose() override
Definition: CUFontLoader.h:214
bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
Definition: CUFontLoader.h:138
Definition: CUAnimationNode.h:52
bool load(const char *key, const std::string &source, int size)
Definition: CUFontLoader.h:292