Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Font.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Shape2d.h"
27 #include "cinder/Exception.h"
28 #include "cinder/DataSource.h"
29 #if defined( CINDER_WINRT )
30 #include <ft2build.h>
31 
32 // Note: generic is a reserved word in winrt c++/cx
33 // need to redefine it for freetype.h
34 #define generic GenericFromFreeTypeLibrary
35 #include FT_FREETYPE_H
36 #include FT_OUTLINE_H
37 #undef generic
38 
39 #include FT_GLYPH_H
40 #endif
41 
42 #include <string>
43 #include <vector>
44 
45 #if defined( CINDER_COCOA )
46  typedef struct CGFont *CGFontRef;
47  #if defined( CINDER_COCOA )
48  typedef const struct __CTFont * CTFontRef;
49  #endif
50 #elif defined( CINDER_MSW )
51  #include <windows.h>
52  #undef min
53  #undef max
54 
55  namespace Gdiplus {
56  class Font;
57  }
58 #endif
59 
60 namespace cinder {
61 
63 class Font {
64  public:
65  typedef uint16_t Glyph;
66 
68  Font() {}
71  Font( const std::string &aName, float size );
74  Font( DataSourceRef dataSource, float size );
75 
76  const std::string& getName() const;
77  std::string getFullName() const;
78  float getSize() const;
79 
80  float getLeading() const;
81  float getAscent() const;
82  float getDescent() const;
83  size_t getNumGlyphs() const;
84 
85  Glyph getGlyphIndex( size_t idx ) const;
86  Glyph getGlyphChar( char utf8Char ) const;
87  std::vector<Glyph> getGlyphs( const std::string &utf8String ) const;
89  Shape2d getGlyphShape( Glyph glyphIndex ) const;
91  Rectf getGlyphBoundingBox( Glyph glyph ) const;
92 
93 #if defined( CINDER_WINRT )
94  FT_Face getFace() const { return mObj->mFace; }
95 #endif
96 
97  static const std::vector<std::string>& getNames( bool forceRefresh = false );
98  static Font getDefault();
99 
100 #if defined( CINDER_COCOA )
101  CGFontRef getCgFontRef() const;
102  CTFontRef getCtFontRef() const;
103 #elif defined( CINDER_MSW )
104  ::LOGFONT getLogfont() const { return mObj->mLogFont; }
105  ::HFONT getHfont() const { return mObj->mHfont; }
106  const Gdiplus::Font* getGdiplusFont() const { return mObj->mGdiplusFont.get(); }
107  static HDC getGlobalDc();
108 #endif
109 
110  private:
111  class Obj {
112  public:
113  Obj( const std::string &aName, float aSize );
114  Obj( DataSourceRef dataSource, float size );
115  ~Obj();
116 
117  void finishSetup();
118 
119 
120  std::string mName;
121  float mSize;
122 #if defined( CINDER_COCOA )
123  CGFontRef mCGFont;
124  const struct __CTFont* mCTFont;
125 #elif defined( CINDER_MSW )
126  ::TEXTMETRIC mTextMetric;
127  ::LOGFONTW mLogFont;
128  ::HFONT mHfont;
129  std::shared_ptr<Gdiplus::Font> mGdiplusFont;
130  std::vector<std::pair<uint16_t,uint16_t> > mUnicodeRanges;
131  void *mFileData;
132 #elif defined( CINDER_WINRT )
133  std::vector<std::pair<uint16_t,uint16_t> > mUnicodeRanges;
134  void *mFileData;
135  FT_Face mFace;
136 #endif
137  size_t mNumGlyphs;
138  };
139 
140  std::shared_ptr<Obj> mObj;
141 
142  public:
144  typedef std::shared_ptr<Obj> Font::*unspecified_bool_type;
146  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Font::mObj; }
147  void reset() { mObj.reset(); }
149 };
150 
152  public:
153  FontInvalidNameExc() throw() {}
154  FontInvalidNameExc( const std::string &fontName ) throw();
155  virtual const char* what() const throw() { return mMessage; }
156  private:
157  char mMessage[2048];
158 };
159 
161 };
162 
163 } // namespace cinder
Shape2d getGlyphShape(Glyph glyphIndex) const
Returns a cinder::Shape2d representing the shape of the glyph at glyphIndex.
std::string getFullName() const
Glyph getGlyphIndex(size_t idx) const
GLsizei const GLchar ** string
Definition: GLee.h:2427
float getSize() const
Definition: Font.cpp:225
std::vector< Glyph > getGlyphs(const std::string &utf8String) const
std::shared_ptr< Obj > Font::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: Font.h:145
virtual const char * what() const
Definition: Font.h:155
static const std::vector< std::string > & getNames(bool forceRefresh=false)
Definition: Font.cpp:210
float getAscent() const
Definition: Font.h:160
Represents an instance of a font at a point size. Implicitly shared object.
Definition: Font.h:63
Glyph getGlyphChar(char utf8Char) const
uint16_t Glyph
Definition: Font.h:65
static Font getDefault()
Definition: Font.cpp:215
FontInvalidNameExc()
Definition: Font.h:153
Rectf getGlyphBoundingBox(Glyph glyph) const
Returns the bounding box of a Glyph, relative to the baseline as the origin.
Definition: Exception.h:32
void reset()
Emulates shared_ptr-like behavior.
Definition: Font.h:147
Definition: Font.h:151
float getDescent() const
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
const std::string & getName() const
Definition: Font.cpp:220
Definition: Shape2d.h:34
GLsizeiptr size
Definition: GLee.h:2089
size_t getNumGlyphs() const
float getLeading() const
Font()
constructs a null Font
Definition: Font.h:68