Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TextureFont.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011, The Cinder Project: http://libcinder.org All rights reserved.
3  This code is intended for use with the Cinder C++ library: http://libcinder.org
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/Text.h"
27 #include "cinder/Font.h"
28 #include "cinder/gl/Texture.h"
29 
30 #include <map>
31 #if defined( _MSC_VER ) && ( _MSC_VER >= 1600 ) || defined( _LIBCPP_VERSION )
32  #include <unordered_map>
33 #else
34  #include <boost/unordered_map.hpp>
35 #endif
36 
37 namespace cinder { namespace gl {
38 
39 typedef std::shared_ptr<class TextureFont> TextureFontRef;
40 
41 class TextureFont {
42  public:
43  class Format {
44  public:
45  Format() : mTextureWidth( 1024 ), mTextureHeight( 1024 ), mPremultiply( false ), mMipmapping( false )
46  {}
47 
49  Format& textureWidth( int32_t textureWidth ) { mTextureWidth = textureWidth; return *this; }
51  int32_t getTextureWidth() const { return mTextureWidth; }
55  int32_t getTextureHeight() const { return mTextureHeight; }
56 
58  Format& premultiply( bool premult = true ) { mPremultiply = premult; return *this; }
60  bool getPremultiply() const { return mPremultiply; }
61 
63  Format& enableMipmapping( bool enable = true ) { mMipmapping = enable; return *this; }
65  bool hasMipmapping() const { return mMipmapping; }
66 
67  protected:
71  };
72 
73  struct DrawOptions {
74  DrawOptions() : mClipHorizontal( true ), mClipVertical( true ), mPixelSnap( true ), mLigate( false ), mScale( 1 ) {}
75 
77  bool getClipHorizontal() const { return mClipHorizontal; }
79  DrawOptions& clipHorizontal( bool clipH = true ) { mClipHorizontal = clipH; return *this; }
80 
82  bool getClipVertical() const { return mClipVertical; }
84  DrawOptions& clipVertical( bool clipV = true ) { mClipVertical = clipV; return *this; }
85 
87  bool getPixelSnap() const { return mPixelSnap; }
89  DrawOptions& pixelSnap( bool pixelSnap = true ) { mPixelSnap = pixelSnap; return *this; }
90 
92  bool getLigate() const { return mLigate; }
94  DrawOptions& ligate( bool useLigatures = true ) { mLigate = useLigatures; return *this; }
95 
97  float getScale() const { return mScale; }
99  DrawOptions& scale( float sc ) { mScale = sc; return *this; }
100 
101 
102  protected:
104  float mScale;
105  };
106 
108  static TextureFontRef create( const Font &font, const Format &format = Format(), const std::string &supportedChars = TextureFont::defaultChars() )
109  { return TextureFontRef( new TextureFont( font, supportedChars, format ) ); }
110 
112  void drawString( const std::string &str, const Vec2f &baseline, const DrawOptions &options = DrawOptions() );
114  void drawString( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
116  void drawStringWrapped( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
118  void drawGlyphs( const std::vector<std::pair<uint16_t,Vec2f> > &glyphMeasures, const Vec2f &baseline, const DrawOptions &options = DrawOptions(), const std::vector<ColorA8u> &colors = std::vector<ColorA8u>() );
120  void drawGlyphs( const std::vector<std::pair<uint16_t,Vec2f> > &glyphMeasures, const Rectf &clip, Vec2f offset, const DrawOptions &options = DrawOptions(), const std::vector<ColorA8u> &colors = std::vector<ColorA8u>() );
121 
123  Vec2f measureString( const std::string &str, const DrawOptions &options = DrawOptions() ) const;
124 #if defined( CINDER_COCOA )
125  Vec2f measureStringWrapped( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
127 #endif
128 
130  std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacements( const std::string &str, const DrawOptions &options = DrawOptions() ) const;
132  std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacements( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
134  std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacementsWrapped( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
135 
137  const Font& getFont() const { return mFont; }
139  std::string getName() const { return mFont.getName(); }
141  float getAscent() const { return mFont.getAscent(); }
143  float getDescent() const { return mFont.getDescent(); }
145  bool isPremultiplied() const { return mFormat.getPremultiply(); }
146 
149  static std::string defaultChars() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890().?!,:;'\"&*=+-/\\@#_[]<>%^llflfiphrids\303\251\303\241\303\250\303\240"; }
150 
151  protected:
152  TextureFont( const Font &font, const std::string &supportedChars, const Format &format );
153 
154  struct GlyphInfo {
155  uint8_t mTextureIndex;
158  };
159 
160 #if defined( _MSC_VER ) && ( _MSC_VER >= 1600 ) || defined( _LIBCPP_VERSION )
161  std::unordered_map<Font::Glyph, GlyphInfo> mGlyphMap;
162 #else
163  boost::unordered_map<Font::Glyph, GlyphInfo> mGlyphMap;
164 #endif
165  std::vector<gl::Texture> mTextures;
168 };
169 
170 } } // namespace cinder::gl
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: gl.h:187
uint8_t mTextureIndex
Definition: TextureFont.h:155
bool mMipmapping
Definition: TextureFont.h:70
void drawString(const std::string &str, const Vec2f &baseline, const DrawOptions &options=DrawOptions())
Draws string str at baseline baseline with DrawOptions options.
Definition: TextureFont.cpp:489
static TextureFontRef create(const Font &font, const Format &format=Format(), const std::string &supportedChars=TextureFont::defaultChars())
Creates a new TextureFontRef with font font, ensuring that glyphs necessary to render supportedChars ...
Definition: TextureFont.h:108
bool mPixelSnap
Definition: TextureFont.h:103
Definition: Area.h:37
GLsizei const GLchar ** string
Definition: GLee.h:2427
Format & enableMipmapping(bool enable=true)
Enables or disables mipmapping. Default is disabled.
Definition: TextureFont.h:63
Definition: TextureFont.h:154
bool getLigate() const
Returns whether advanced ligatures are used, which must have been instantiated by the supportedChars ...
Definition: TextureFont.h:92
static std::string defaultChars()
Definition: TextureFont.h:149
static Vec2< float > zero()
Definition: Vector.h:295
bool hasMipmapping() const
Returns whether the TextureFont texture has mipmapping enabled.
Definition: TextureFont.h:65
Definition: TextureFont.h:73
int32_t mTextureHeight
Definition: TextureFont.h:68
const Font & getFont() const
Returns the font the TextureFont represents.
Definition: TextureFont.h:137
TextureFont(const Font &font, const std::string &supportedChars, const Format &format)
int32_t getTextureWidth() const
Returns the width of the textures created internally for glyphs. Default 1024.
Definition: TextureFont.h:51
float getDescent() const
Returns the descent of the font.
Definition: TextureFont.h:143
bool getClipVertical() const
Returns whether the output clips vertically.
Definition: TextureFont.h:82
Definition: TextureFont.h:41
float getAscent() const
Returns the ascent of the font.
Definition: TextureFont.h:141
bool mPremultiply
Definition: TextureFont.h:69
int32_t mTextureWidth
Definition: TextureFont.h:68
float getScale() const
Returns the scale at which the type is rendered. 2 is double size. Default 1.
Definition: TextureFont.h:97
std::vector< std::pair< uint16_t, Vec2f > > getGlyphPlacementsWrapped(const std::string &str, const Rectf &fitRect, const DrawOptions &options=DrawOptions()) const
Returns a word-wrapped vector of glyph/placement pairs representing str fit inside fitRect...
Definition: TextureFont.cpp:550
Format & premultiply(bool premult=true)
Sets whether the TextureFont render premultiplied output. Default false.
Definition: TextureFont.h:58
float getAscent() const
Represents an instance of a font at a point size. Implicitly shared object.
Definition: Font.h:63
GLintptr offset
Definition: GLee.h:2095
Font mFont
Definition: TextureFont.h:166
bool getClipHorizontal() const
Returns whether the output clips horizontally.
Definition: TextureFont.h:77
bool getPixelSnap() const
Returns whether the output glyphs are snapped to pixel boundaries. This sharpens static text but prev...
Definition: TextureFont.h:87
bool mClipVertical
Definition: TextureFont.h:103
float mScale
Definition: TextureFont.h:104
std::shared_ptr< class TextureFont > TextureFontRef
Definition: TextureFont.h:39
Definition: TextureFont.h:43
Vec2f mOriginOffset
Definition: TextureFont.h:157
DrawOptions & scale(float sc)
Sets the scale at which the type is rendered. 2 is double size. Default 1.
Definition: TextureFont.h:99
boost::unordered_map< Font::Glyph, GlyphInfo > mGlyphMap
Definition: TextureFont.h:163
DrawOptions & clipVertical(bool clipV=true)
Sets whether the output clips vertically.
Definition: TextureFont.h:84
void drawGlyphs(const std::vector< std::pair< uint16_t, Vec2f > > &glyphMeasures, const Vec2f &baseline, const DrawOptions &options=DrawOptions(), const std::vector< ColorA8u > &colors=std::vector< ColorA8u >())
Draws the glyphs in glyphMeasures at baseline baseline with DrawOptions options. glyphMeasures is a v...
DrawOptions()
Definition: TextureFont.h:74
bool getPremultiply() const
Returns whether the TextureFont renders premultiplied output. Default false.
Definition: TextureFont.h:60
bool isPremultiplied() const
Returns whether the TextureFont output premultipled output. Default is false.
Definition: TextureFont.h:145
void drawStringWrapped(const std::string &str, const Rectf &fitRect, const Vec2f &offset=Vec2f::zero(), const DrawOptions &options=DrawOptions())
Draws word-wrapped string str fit inside fitRect, with internal offset offset and DrawOptions options...
Definition: TextureFont.cpp:503
std::string getName() const
Returns the name of the font.
Definition: TextureFont.h:139
std::vector< std::pair< uint16_t, Vec2f > > getGlyphPlacements(const std::string &str, const DrawOptions &options=DrawOptions()) const
Returns a vector of glyph/placement pairs representing str, suitable for use with drawGlyphs...
Definition: TextureFont.cpp:538
DrawOptions & clipHorizontal(bool clipH=true)
Sets whether the output clips horizontally.
Definition: TextureFont.h:79
GLenum GLsizei GLenum format
Definition: GLee.h:969
Format & textureHeight(int32_t textureHeight)
Sets the height of the textures created internally for glyphs. Default 1024.
Definition: TextureFont.h:53
bool mLigate
Definition: TextureFont.h:103
DrawOptions & pixelSnap(bool pixelSnap=true)
Sets whether the output glyphs are snapped to pixel boundaries. This sharpens static text but prevent...
Definition: TextureFont.h:89
DrawOptions & ligate(bool useLigatures=true)
Sets whether advanced ligatures are used, which must have been instantiated by the supportedChars par...
Definition: TextureFont.h:94
int32_t getTextureHeight() const
Sets the height of the textures created internally for glyphs. Default 1024.
Definition: TextureFont.h:55
float getDescent() const
bool mClipHorizontal
Definition: TextureFont.h:103
std::vector< gl::Texture > mTextures
Definition: TextureFont.h:165
const std::string & getName() const
Definition: Font.cpp:220
Format & textureWidth(int32_t textureWidth)
Sets the width of the textures created internally for glyphs. Default 1024.
Definition: TextureFont.h:49
Vec2f measureString(const std::string &str, const DrawOptions &options=DrawOptions()) const
Returns the size in pixels necessary to render the string str with DrawOptions options.
Definition: TextureFont.cpp:510
Format mFormat
Definition: TextureFont.h:167
Format()
Definition: TextureFont.h:45
Area mTexCoords
Definition: TextureFont.h:156