include/cinder/gl/TextureFont.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2011, The Cinder Project: http://libcinder.org All rights reserved.
00003  This code is intended for use with the Cinder C++ library: http://libcinder.org
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
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 #if defined( _MSC_VER ) && ( _MSC_VER >= 1600 ) || defined( _LIBCPP_VERSION )
00032     #include <unordered_map>
00033 #else
00034     #include <boost/unordered_map.hpp>
00035 #endif
00036 
00037 namespace cinder { namespace gl {
00038 
00039 typedef std::shared_ptr<class TextureFont>  TextureFontRef;
00040 
00041 class TextureFont {
00042   public:
00043     class Format {
00044       public:
00045         Format() : mTextureWidth( 1024 ), mTextureHeight( 1024 ), mPremultiply( false ), mMipmapping( false )
00046         {}
00047         
00049         Format&     textureWidth( int32_t textureWidth ) { mTextureWidth = textureWidth; return *this; }
00051         int32_t     getTextureWidth() const { return mTextureWidth; }
00053         Format&     textureHeight( int32_t textureHeight ) { mTextureHeight = textureHeight; return *this; }
00055         int32_t     getTextureHeight() const { return mTextureHeight; }
00056         
00058         Format&     premultiply( bool premult = true ) { mPremultiply = premult; return *this; }
00060         bool        getPremultiply() const { return mPremultiply; }
00061         
00063         Format&     enableMipmapping( bool enable = true ) { mMipmapping = enable; return *this; }
00065         bool        hasMipmapping() const { return mMipmapping; }
00066         
00067       protected:
00068         int32_t     mTextureWidth, mTextureHeight;
00069         bool        mPremultiply;
00070         bool        mMipmapping;
00071     };
00072 
00073     struct DrawOptions {
00074         DrawOptions() : mClipHorizontal( true ), mClipVertical( true ), mPixelSnap( true ), mLigate( false ), mScale( 1 ) {}
00075 
00077         bool            getClipHorizontal() const { return mClipHorizontal; }       
00079         DrawOptions&    clipHorizontal( bool clipH = true ) { mClipHorizontal = clipH; return *this; }
00080 
00082         bool            getClipVertical() const { return mClipVertical; }       
00084         DrawOptions&    clipVertical( bool clipV = true ) { mClipVertical = clipV; return *this; }
00085 
00087         bool            getPixelSnap() const { return mPixelSnap; }     
00089         DrawOptions&    pixelSnap( bool pixelSnap = true ) { mPixelSnap = pixelSnap; return *this; }
00090 
00092         bool            getLigate() const { return mLigate; }
00094         DrawOptions&    ligate( bool useLigatures = true ) { mLigate = useLigatures; return *this; }
00095 
00097         float           getScale() const { return mScale; }
00099         DrawOptions&    scale( float sc ) { mScale = sc; return *this; }
00100 
00101 
00102       protected:
00103         bool        mClipHorizontal, mClipVertical, mPixelSnap, mLigate;
00104         float       mScale;
00105     };
00106 
00108     static TextureFontRef       create( const Font &font, const Format &format = Format(), const std::string &supportedChars = TextureFont::defaultChars() )
00109     { return TextureFontRef( new TextureFont( font, supportedChars, format ) ); }
00110     
00112     void    drawString( const std::string &str, const Vec2f &baseline, const DrawOptions &options = DrawOptions() );
00114     void    drawString( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
00116     void    drawStringWrapped( const std::string &str, const Rectf &fitRect, const Vec2f &offset = Vec2f::zero(), const DrawOptions &options = DrawOptions() );
00118     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>() );
00120     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>() );
00121 
00123     Vec2f   measureString( const std::string &str, const DrawOptions &options = DrawOptions() ) const;
00124 #if defined( CINDER_COCOA )
00125 
00126     Vec2f   measureStringWrapped( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
00127 #endif
00128     
00130     std::vector<std::pair<uint16_t,Vec2f> >     getGlyphPlacements( const std::string &str, const DrawOptions &options = DrawOptions() ) const;
00132     std::vector<std::pair<uint16_t,Vec2f> >     getGlyphPlacements( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
00134     std::vector<std::pair<uint16_t,Vec2f> >     getGlyphPlacementsWrapped( const std::string &str, const Rectf &fitRect, const DrawOptions &options = DrawOptions() ) const;
00135 
00137     const Font&     getFont() const { return mFont; }
00139     std::string getName() const { return mFont.getName(); }
00141     float   getAscent() const { return mFont.getAscent(); }
00143     float   getDescent() const { return mFont.getDescent(); }
00145     bool    isPremultiplied() const { return mFormat.getPremultiply(); }
00146 
00149     static std::string      defaultChars() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890().?!,:;'\"&*=+-/\\@#_[]<>%^llflfiphrids\303\251\303\241\303\250\303\240"; }
00150 
00151   protected:
00152     TextureFont( const Font &font, const std::string &supportedChars, const Format &format );
00153 
00154     struct GlyphInfo {
00155         uint8_t     mTextureIndex;
00156         Area        mTexCoords;
00157         Vec2f       mOriginOffset;
00158     };
00159     
00160 #if defined( _MSC_VER ) && ( _MSC_VER >= 1600 ) || defined( _LIBCPP_VERSION )
00161     std::unordered_map<Font::Glyph, GlyphInfo>      mGlyphMap;
00162 #else
00163     boost::unordered_map<Font::Glyph, GlyphInfo>    mGlyphMap;
00164 #endif
00165     std::vector<gl::Texture>                        mTextures;
00166     Font                                            mFont;
00167     Format                                          mFormat;
00168 };
00169 
00170 } } // namespace cinder::gl