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 #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 } } // namespace cinder::gl