Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DxTextureFont.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/dx/DxTexture.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 dx {
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 ) const;
132  std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacements( const std::string &str, const Rectf &fitRect, const DrawOptions &options ) const;
133 
135  const Font& getFont() const { return mFont; }
137  std::string getName() const { return mFont.getName(); }
139  float getAscent() const { return mFont.getAscent(); }
141  float getDescent() const { return mFont.getDescent(); }
143  bool isPremultiplied() const { return mFormat.getPremultiply(); }
144 
147  static std::string defaultChars() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890().?!,:;'\"&*=+-/\\@#_[]<>%^llflfiphrids\303\251\303\241\303\250\303\240"; }
148 
149  protected:
150  TextureFont( const Font &font, const std::string &supportedChars, const Format &format );
151 
152  struct GlyphInfo {
153  uint8_t mTextureIndex;
156  };
157 
158 #if defined( _MSC_VER ) && ( _MSC_VER >= 1600 ) || defined( _LIBCPP_VERSION )
159  std::unordered_map<Font::Glyph, GlyphInfo> mGlyphMap;
160 #else
161  boost::unordered_map<Font::Glyph, GlyphInfo> mGlyphMap;
162 #endif
163  std::vector<dx::TextureRef> mTextures;
166 };
167 
168 } } // namespace cinder::dx
Format & textureWidth(int32_t textureWidth)
Sets the width of the textures created internally for glyphs. Default 1024.
Definition: DxTextureFont.h:49
static std::string defaultChars()
Definition: DxTextureFont.h:147
bool mClipVertical
Definition: DxTextureFont.h:103
std::shared_ptr< class TextureFont > TextureFontRef
Definition: DxTextureFont.h:39
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: dx.h:198
DrawOptions()
Definition: DxTextureFont.h:74
std::vector< dx::TextureRef > mTextures
Definition: DxTextureFont.h:163
Definition: Area.h:37
TextureFont(const Font &font, const std::string &supportedChars, const Format &format)
GLsizei const GLchar ** string
Definition: GLee.h:2427
DrawOptions & clipHorizontal(bool clipH=true)
Sets whether the output clips horizontally.
Definition: DxTextureFont.h:79
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: DxTextureFont.cpp:509
int32_t getTextureHeight() const
Sets the height of the textures created internally for glyphs. Default 1024.
Definition: DxTextureFont.h:55
Definition: DxTextureFont.h:152
DrawOptions & pixelSnap(bool pixelSnap=true)
Sets whether the output glyphs are snapped to pixel boundaries. This sharpens static text but prevent...
Definition: DxTextureFont.h:89
Definition: DxTextureFont.h:41
std::string getName() const
Returns the name of the font.
Definition: DxTextureFont.h:137
static Vec2< float > zero()
Definition: Vector.h:295
float getAscent() const
Returns the ascent of the font.
Definition: DxTextureFont.h:139
Definition: DxTextureFont.h:73
bool getClipVertical() const
Returns whether the output clips vertically.
Definition: DxTextureFont.h:82
Definition: DxTextureFont.h:43
Area mTexCoords
Definition: DxTextureFont.h:154
std::vector< std::pair< uint16_t, Vec2f > > getGlyphPlacements(const std::string &str, const DrawOptions &options) const
Returns a vector of glyph/placement pairs representing str, suitable for use with drawGlyphs...
Definition: DxTextureFont.cpp:536
boost::unordered_map< Font::Glyph, GlyphInfo > mGlyphMap
Definition: DxTextureFont.h:161
bool getPremultiply() const
Returns whether the TextureFont renders premultiplied output. Default false.
Definition: DxTextureFont.h:60
float getScale() const
Returns the scale at which the type is rendered. 2 is double size. Default 1.
Definition: DxTextureFont.h:97
void drawString(const std::string &str, const Vec2f &baseline, const DrawOptions &options=DrawOptions())
Draws string str at baseline baseline with DrawOptions options.
Definition: DxTextureFont.cpp:495
int32_t mTextureWidth
Definition: DxTextureFont.h:68
float getAscent() const
DrawOptions & scale(float sc)
Sets the scale at which the type is rendered. 2 is double size. Default 1.
Definition: DxTextureFont.h:99
Represents an instance of a font at a point size. Implicitly shared object.
Definition: Font.h:63
bool getPixelSnap() const
Returns whether the output glyphs are snapped to pixel boundaries. This sharpens static text but prev...
Definition: DxTextureFont.h:87
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...
float getDescent() const
Returns the descent of the font.
Definition: DxTextureFont.h:141
GLintptr offset
Definition: GLee.h:2095
bool mPixelSnap
Definition: DxTextureFont.h:103
Format mFormat
Definition: DxTextureFont.h:165
DrawOptions & clipVertical(bool clipV=true)
Sets whether the output clips vertically.
Definition: DxTextureFont.h:84
int32_t getTextureWidth() const
Returns the width of the textures created internally for glyphs. Default 1024.
Definition: DxTextureFont.h:51
bool hasMipmapping() const
Returns whether the TextureFont texture has mipmapping enabled.
Definition: DxTextureFont.h:65
bool getClipHorizontal() const
Returns whether the output clips horizontally.
Definition: DxTextureFont.h:77
bool isPremultiplied() const
Returns whether the TextureFont output premultipled output. Default is false.
Definition: DxTextureFont.h:143
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: DxTextureFont.h:108
DrawOptions & ligate(bool useLigatures=true)
Sets whether advanced ligatures are used, which must have been instantiated by the supportedChars par...
Definition: DxTextureFont.h:94
bool mClipHorizontal
Definition: DxTextureFont.h:103
bool mPremultiply
Definition: DxTextureFont.h:69
int int int * dx
Definition: GLee.h:17162
float mScale
Definition: DxTextureFont.h:104
GLenum GLsizei GLenum format
Definition: GLee.h:969
const Font & getFont() const
Returns the font the TextureFont represents.
Definition: DxTextureFont.h:135
Format & textureHeight(int32_t textureHeight)
Sets the height of the textures created internally for glyphs. Default 1024.
Definition: DxTextureFont.h:53
int32_t mTextureHeight
Definition: DxTextureFont.h:68
bool mMipmapping
Definition: DxTextureFont.h:70
Format & enableMipmapping(bool enable=true)
Enables or disables mipmapping. Default is disabled.
Definition: DxTextureFont.h:63
bool getLigate() const
Returns whether advanced ligatures are used, which must have been instantiated by the supportedChars ...
Definition: DxTextureFont.h:92
float getDescent() const
const std::string & getName() const
Definition: Font.cpp:220
uint8_t mTextureIndex
Definition: DxTextureFont.h:153
bool mLigate
Definition: DxTextureFont.h:103
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: DxTextureFont.cpp:516
Vec2f mOriginOffset
Definition: DxTextureFont.h:155
Format()
Definition: DxTextureFont.h:45
Font mFont
Definition: DxTextureFont.h:164
Format & premultiply(bool premult=true)
Sets whether the TextureFont render premultiplied output. Default false.
Definition: DxTextureFont.h:58