Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #pragma once
00024
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Text.h"
00027 #include "cinder/Font.h"
00028 #include "cinder/gl/Texture.h"
00029
00030 #include <map>
00031 #include <boost/unordered_map.hpp>
00032
00033 namespace cinder { namespace gl {
00034
00035 typedef std::shared_ptr<class TextureFont> TextureFontRef;
00036
00037 class TextureFont {
00038 public:
00039 class Format {
00040 public:
00041 Format() : mTextureWidth( 1024 ), mTextureHeight( 1024 ), mPremultiply( false ), mMipmapping( false )
00042 {}
00043
00045 Format& textureWidth( int32_t textureWidth ) { mTextureWidth = textureWidth; return *this; }
00047 int32_t getTextureWidth() const { return mTextureWidth; }
00049 Format& textureHeight( int32_t textureHeight ) { mTextureHeight = textureHeight; return *this; }
00051 int32_t getTextureHeight() const { return mTextureHeight; }
00052
00054 Format& premultiply( bool premult = true ) { mPremultiply = premult; return *this; }
00056 bool getPremultiply() const { return mPremultiply; }
00057
00059 Format& enableMipmapping( bool enable = true ) { mMipmapping = enable; return *this; }
00061 bool hasMipmapping() const { return mMipmapping; }
00062
00063 protected:
00064 int32_t mTextureWidth, mTextureHeight;
00065 bool mPremultiply;
00066 bool mMipmapping;
00067 };
00068
00069 struct DrawOptions {
00070 DrawOptions() : mClipHorizontal( true ), mClipVertical( true ), mPixelSnap( true ), mLigate( false ), mScale( 1 ) {}
00071
00073 bool getClipHorizontal() const { return mClipHorizontal; }
00075 DrawOptions& clipHorizontal( bool clipH = true ) { mClipHorizontal = clipH; return *this; }
00076
00078 bool getClipVertical() const { return mClipVertical; }
00080 DrawOptions& clipVertical( bool clipV = true ) { mClipVertical = clipV; return *this; }
00081
00083 bool getPixelSnap() const { return mPixelSnap; }
00085 DrawOptions& pixelSnap( bool pixelSnap = true ) { mPixelSnap = pixelSnap; return *this; }
00086
00088 bool getLigate() const { return mLigate; }
00090 DrawOptions& ligate( bool useLigatures = true ) { mLigate = useLigatures; return *this; }
00091
00093 float getScale() const { return mScale; }
00095 DrawOptions& scale( float sc ) { mScale = sc; return *this; }
00096
00097
00098 protected:
00099 bool mClipHorizontal, mClipVertical, mPixelSnap, mLigate;
00100 float mScale;
00101 };
00102
00104 static TextureFontRef create( const Font &font, const Format &format = Format(), const std::string &supportedChars = TextureFont::defaultChars() )
00105 { return TextureFontRef( new TextureFont( font, supportedChars, format ) ); }
00106
00108 void drawString( const std::string &str, const Vec2f &baseline, const DrawOptions &options = DrawOptions() );
00110 void drawString( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
00112 void drawStringWrapped( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
00114 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>() );
00116 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>() );
00117
00119 Vec2f measureString( const std::string &str, const DrawOptions &options = DrawOptions() ) const;
00120 #if defined( CINDER_COCOA )
00121
00122 Vec2f measureStringWrapped( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
00123 #endif
00124
00126 std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacements( const std::string &str, const DrawOptions &options ) const;
00128 std::vector<std::pair<uint16_t,Vec2f> > getGlyphPlacements( const std::string &str, const Rectf &fitRect, const DrawOptions &options ) const;
00129
00131 const Font& getFont() const { return mFont; }
00133 std::string getName() const { return mFont.getName(); }
00135 float getAscent() const { return mFont.getAscent(); }
00137 float getDescent() const { return mFont.getDescent(); }
00139 bool isPremultiplied() const { return mFormat.getPremultiply(); }
00140
00143 static std::string defaultChars() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890().?!,:;'\"&*=+-/\\@#_[]<>%^llflfiphrids\303\251\303\241\303\250\303\240"; }
00144
00145 protected:
00146 TextureFont( const Font &font, const std::string &supportedChars, const Format &format );
00147
00148 struct GlyphInfo {
00149 uint8_t mTextureIndex;
00150 Area mTexCoords;
00151 Vec2f mOriginOffset;
00152 };
00153
00154 boost::unordered_map<Font::Glyph, GlyphInfo> mGlyphMap;
00155 std::vector<gl::Texture> mTextures;
00156 Font mFont;
00157 Format mFormat;
00158 };
00159
00160 } }