Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

include/cinder/Font.h

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
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/Shape2d.h"
00027 #include "cinder/Exception.h"
00028 #include "cinder/DataSource.h"
00029 
00030 #include <string>
00031 #include <vector>
00032 
00033 #if defined( CINDER_COCOA )
00034     typedef struct CGFont *CGFontRef;
00035     #if defined( CINDER_COCOA )
00036         typedef const struct __CTFont * CTFontRef;
00037     #endif
00038 #elif defined( CINDER_MSW )
00039     #include <windows.h>
00040 
00041     namespace Gdiplus {
00042         class Font;
00043     }
00044 #endif
00045 
00046 namespace cinder {
00047 
00048 class Font {
00049  public:
00050     typedef uint16_t        Glyph;      
00051 
00053     Font() {}
00056     Font( const std::string &aName, float size );
00059     Font( DataSourceRef dataSource, float size );
00060 
00061     const std::string&      getName() const;
00062     std::string             getFullName() const;
00063     float                   getSize() const;
00064 
00065     float                   getLeading() const;
00066     float                   getAscent() const;
00067     float                   getDescent() const;
00068     size_t                  getNumGlyphs() const;
00069 
00070 #if ! defined( CINDER_COCOA_TOUCH ) 
00071     Glyph                   getGlyphIndex( size_t idx );
00072     Glyph                   getGlyphChar( char utf8Char );
00073     std::vector<Glyph>      getGlyphs( const std::string &utf8String );
00075     Shape2d                 getGlyphShape( Glyph glyphIndex );
00076     
00077     static const std::vector<std::string>&      getNames( bool forceRefresh = false );
00078 #endif
00079 
00080 #if defined( CINDER_COCOA )
00081     CGFontRef               getCgFontRef() const;
00082   #if defined( CINDER_MAC )
00083     CTFontRef               getCtFontRef() const;
00084   #endif
00085 #elif defined( CINDER_MSW )
00086     ::LOGFONT               getLogfont() const { return mObj->mLogFont; }
00087     const Gdiplus::Font*    getGdiplusFont() const { return mObj->mGdiplusFont.get(); }
00088 #endif
00089 
00090  private:
00091     class Obj {
00092      public:
00093         Obj( const std::string &aName, float aSize );
00094         Obj( DataSourceRef dataSource, float size );
00095         ~Obj();
00096         
00097         void        finishSetup();
00098         
00099         
00100         std::string             mName;
00101         float                   mSize;
00102 #if defined( CINDER_COCOA )
00103         CGFontRef               mCGFont;
00104     #if defined( CINDER_MAC )       
00105         const struct __CTFont*  mCTFont;
00106     #endif
00107 #elif defined( CINDER_MSW )
00108         ::TEXTMETRIC                mTextMetric;
00109         ::LOGFONTW                  mLogFont;
00110         ::HFONT                     mHfont;
00111         shared_ptr<Gdiplus::Font>   mGdiplusFont;
00112         std::vector<std::pair<uint16_t,uint16_t> >  mUnicodeRanges;
00113         size_t                  mNumGlyphs;
00114 #endif      
00115     };
00116 
00117     shared_ptr<Obj>         mObj;
00118     
00119   public:
00121 
00122     typedef shared_ptr<Obj> Font::*unspecified_bool_type;
00123     operator unspecified_bool_type() { return ( mObj.get() == 0 ) ? 0 : &Font::mObj; }
00124     void reset() { mObj.reset(); }
00126 };
00127 
00128 class FontInvalidNameExc : public cinder::Exception {
00129 };
00130 
00131 class FontGlyphFailureExc : public cinder::Exception {
00132 };
00133 
00134 } // namespace cinder