GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1#pragma once
2#include "ga/defines.h"
3#include "ga/math.h"
4#include "ga/resource.h"
5#include <sstream>
6#include <string>
7
8#ifdef GA_OPENFRAMEWORKS
9#include "ofTrueTypeFont.h"
10#endif
11
12namespace ga {
13
14#ifdef GA_OPENFRAMEWORKS
15class Font : public ofTrueTypeFont
16{
17public:
18 const ofTrueTypeFontSettings& getSettings()
19 {
20 return ofTrueTypeFont::settings;
21 }
22 float getWidth( const std::string& str ) const
23 {
24 return getStringBoundingBox( str, 0, 0 ).width;
25 }
26 float getHeight( const std::string& str ) const
27 {
28 return getStringBoundingBox( str, 0, 0 ).width;
29 }
30 Rect getBounds( const std::string& str ) const
31 {
32 return getStringBoundingBox( str, 0, 0 );
33 }
34};
35
36inline bool load( Font& font, const ofTrueTypeFontSettings& settings )
37{
38 return font.load( settings );
39}
40
41#endif
42// todo: Font for Cinder
43
44// ---------------------------
45// Global Texture Cache
46// ---------------------------
48
50{
51 static FontCache fontCache;
52 return fontCache;
53}
54
55// ---------------------------
56
58{
60 FontStyle( const std::string& file_, int size_, const std::string& name = "" )
61 : file( file_ )
62 , size( size_ )
63 {
64#ifdef GA_OPENFRAMEWORKS
65 ofTrueTypeFontSettings settings( file, size );
66 settings.antialiased = true;
67 settings.contours = false;
68 settings.simplifyAmt = 0.3f;
69 settings.dpi = 72;
70 settings.ranges = {
71 ofUnicode::Latin1Supplement,
72 ofUnicode::LatinExtendedAdditional,
73 ofUnicode::Latin,
74 ofUnicode::LatinA,
75 ofUnicode::GeneralPunctuation,
76 };
77
78 m_name = name;
79 if ( m_name.empty() ) {
80 std::stringstream ss;
81 ss << file.substr( file.find_last_of( "/\\" ) + 1 ) << "@" << size;
82 m_name = ss.str();
83 }
84
85 if ( !fontCache().load( m_name, settings ) ) {
86 m_name = "";
87 }
88#endif
89 }
90
91 bool loaded() { return !m_name.empty(); }
92 const std::string& name() { return m_name; }
93 const std::shared_ptr<Font> font() { return fontCache().get( m_name ); }
94
95 std::string file;
96 int size;
97
98protected:
99 std::string m_name;
100};
101
102} // namespace ga
resource cache class, represents a container for a resource type organized by name (string)
Definition: resource.h:30
std::shared_ptr< ResourceT > get(const std::string &name)
Definition: resource.h:33
Definition: color.h:9
FontCache & fontCache()
Definition: font.h:49
Definition: font.h:58
std::string m_name
Definition: font.h:99
int size
Definition: font.h:96
std::string file
Definition: font.h:95
const std::string & name()
Definition: font.h:92
FontStyle()
Definition: font.h:59
const std::shared_ptr< Font > font()
Definition: font.h:93
FontStyle(const std::string &file_, int size_, const std::string &name="")
Definition: font.h:60
bool loaded()
Definition: font.h:91