# 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     Glyph                   getGlyphIndex( size_t idx );
    00071     Glyph                   getGlyphChar( char utf8Char );
    00072     std::vector<Glyph>      getGlyphs( const std::string &utf8String );
    00074     Shape2d                 getGlyphShape( Glyph glyphIndex );
    00075
    00076     static const std::vector<std::string>&      getNames( bool forceRefresh = false );
    00077
    00078 #if defined( CINDER_COCOA )
    00079     CGFontRef               getCgFontRef() const;
    00080   #if defined( CINDER_MAC )
    00081     CTFontRef               getCtFontRef() const;
    00082   #endif
    00083 #elif defined( CINDER_MSW )
    00084     ::LOGFONT               getLogfont() const { return mObj->mLogFont; }
    00085     const Gdiplus::Font*    getGdiplusFont() const { return mObj->mGdiplusFont.get(); }
    00086 #endif
    00087
    00088  private:
    00089     class Obj {
    00090      public:
    00091         Obj( const std::string &aName, float aSize );
    00092         Obj( DataSourceRef dataSource, float size );
    00093         ~Obj();
    00094
    00095         void        finishSetup();
    00096
    00097
    00098         std::string             mName;
    00099         float                   mSize;
    00100 #if defined( CINDER_COCOA )
    00101         CGFontRef               mCGFont;
    00102     #if defined( CINDER_MAC )
    00103         const struct __CTFont*  mCTFont;
    00104     #endif
    00105 #elif defined( CINDER_MSW )
    00106         ::TEXTMETRIC                mTextMetric;
    00107         ::LOGFONTW                  mLogFont;
    00108         ::HFONT                     mHfont;
    00109         shared_ptr<Gdiplus::Font>   mGdiplusFont;
    00110         std::vector<std::pair<uint16_t,uint16_t> >  mUnicodeRanges;
    00111         size_t                  mNumGlyphs;
    00112 #endif
    00113     };
    00114
    00115     shared_ptr<Obj>         mObj;
    00116
    00117   public:
    00119
    00120     typedef shared_ptr<Obj> Font::*unspecified_bool_type;
    00121     operator unspecified_bool_type() { return ( mObj.get() == 0 ) ? 0 : &Font::mObj; }
    00122     void reset() { mObj.reset(); }
    00124 };
    00125
    00126 class FontInvalidNameExc : public cinder::Exception {
    00127 };
    00128
    00129 class FontGlyphFailureExc : public cinder::Exception {
    00130 };
    00131
    00132 } // namespace cinder