Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
FontManager.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
33 #include <vlCore/Log.hpp>
34 #include <algorithm>
35 
36 #include "ft2build.h"
37 #include FT_FREETYPE_H
38 
39 using namespace vl;
40 
41 //-----------------------------------------------------------------------------
42 FontManager::FontManager(void* free_type_library)
43 {
44  mFreeTypeLibrary = free_type_library;
45  if (!free_type_library)
46  {
47  FT_Library freetype = NULL;
48  FT_Error error = FT_Init_FreeType( &freetype );
49  mFreeTypeLibrary = freetype;
50  if ( error )
51  {
52  Log::error("FontManager::FontManager(): an error occurred during FreeType library initialization!\n");
53  VL_TRAP()
54  }
55  }
56 }
57 //-----------------------------------------------------------------------------
59 {
61  if (mFreeTypeLibrary)
62  {
63  FT_Done_FreeType( (FT_Library)mFreeTypeLibrary );
64  mFreeTypeLibrary = NULL;
65  }
66 }
67 //-----------------------------------------------------------------------------
68 Font* FontManager::acquireFont(const String& path, int size, bool smooth)
69 {
70  ref<Font> font;
71  for(unsigned i=0; !font && i<mFonts.size(); ++i)
72  if (fonts()[i]->filePath() == path && fonts()[i]->size() == size && fonts()[i]->smooth() == smooth)
73  font = fonts()[i];
74 
75  if (!font)
76  {
77  font = new Font(this);
78  font->loadFont(path);
79  font->setSize(size);
80  font->setSmooth(smooth);
81  mFonts.push_back( font );
82  }
83 
84  return font.get();
85 }
86 //-----------------------------------------------------------------------------
88 {
89  std::vector< ref<Font> >::iterator it = std::find(mFonts.begin(), mFonts.end(), font);
90  if (it != mFonts.end())
91  mFonts.erase(it);
92 }
93 //-----------------------------------------------------------------------------
95 {
96  for(unsigned i=0; i<mFonts.size(); ++i)
97  mFonts[i]->releaseFreeTypeData();
98 }
99 //-----------------------------------------------------------------------------
const T * get() const
Definition: Object.hpp:128
const std::vector< ref< Font > > & fonts() const
Returns the list of Fonts created till now.
Definition: FontManager.hpp:62
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
A font to be used with a Text renderable.
Definition: Font.hpp:127
void * mFreeTypeLibrary
Definition: FontManager.hpp:82
void loadFont(const String &path)
Loads a font using fileSystem()locateFile().
Font * acquireFont(const String &font, int size, bool smooth=false)
Creates or returns an already created Font.
Definition: FontManager.cpp:68
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
void setSize(int size)
The size of the font.
Visualization Library main namespace.
#define VL_TRAP()
Definition: checks.hpp:70
FontManager(void *free_type_library=NULL)
Constructor: uses the given FT_Library handle otherwise will initialize and use its own FT_Library...
Definition: FontManager.cpp:42
void releaseAllFonts()
Releases all Fonts and associated resources and memory.
Definition: FontManager.cpp:94
void releaseFont(Font *font)
Releases a given Font and its associated resources and memory.
Definition: FontManager.cpp:87
void setSmooth(bool smooth)
Whether the font rendering should use linear filtering or not.
#define NULL
Definition: OpenGLDefs.hpp:81
std::vector< ref< Font > > mFonts
Definition: FontManager.hpp:81
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
~FontManager()
Destructor: releases all fonts and disposes the FT_Library if not NULL.
Definition: FontManager.cpp:58