glut-font.h

Go to the documentation of this file.
00001 /*
00002  * glut-font.h
00003  *
00004  * Copyright (C) 2008,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  */
00031 
00032 #ifndef WAVE_GLUT_GLUT_FONT_H__
00033 #define WAVE_GLUT_GLUT_FONT_H__
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "common/common.h"
00037 #include "geometry/geometry_2d.h"
00038 #include "threadsafe/smart_ptr.h"
00039 
00040 
00041 namespace glut {
00042 
00043 ////////////////////////////////////////////////////////////////////////////////
00044 ///
00045 /// \ingroup wave_glut
00046 /// \defgroup glut_font Glut Font Library
00047 ///
00048 /// A font helper library.  Keeps other code abstracted from the details of
00049 /// font implementation.  Currently this is a wrapper on top of the FTGL
00050 /// library.
00051 ///
00052 /// \b WARNING: this library is used for 2D font rendering.  That is, it assumes
00053 /// that we are in 2D rendering mode (see the glut::push2D() and pop2D()
00054 /// methods).  This library uses the wavepacket convention for 2D coordinates:
00055 /// y=0 is the TOP of the screen, and y=height is the BOTTOM of the screen.
00056 /// That is a bit unusual: OpenGL flips that convention (and OpenGL's convention
00057 /// is probably better).  But for legacy reasons, this library uses the
00058 /// wavepacket convention.
00059 ///
00060 /// This means, for instance, that it is *very* awkward to attempt to use these
00061 /// routines to render while in 3D rendering mode.
00062 ///
00063 ////////////////////////////////////////////////////////////////////////////////
00064 
00065 /*@{*/
00066 
00067 
00068 /// standard 2D pixel rect
00069 typedef rect2d_t<int> pixel_rect_t;
00070 
00071 
00072 
00073 /// font sizing information
00074 struct font_rect_t {
00075         // constructor, manipulators
00076         font_rect_t(void) throw() { this->clear(); }
00077         void clear(void) throw() {
00078                         lead = trail = drop = rise = 0;
00079                 }
00080         void getPixelRect(OUT pixel_rect_t& pr) const throw() {
00081                         pr.left = -lead;
00082                         pr.right = trail;
00083                         pr.top = -rise;
00084                         pr.bottom = drop;
00085                 }
00086 
00087         // data fields
00088         int     lead;           ///< pixels leading (to left of) draw origin
00089         int     trail;          ///< pixels trailing (to right of) draw origin
00090         int     drop;           ///< pixels falling below draw origin
00091         int     rise;           ///< pixels above draw origin
00092 };
00093 
00094 
00095 
00096 ////////////////////////////////////////////////////////////////////////////////
00097 //
00098 //      public API
00099 //
00100 ////////////////////////////////////////////////////////////////////////////////
00101 
00102 /// a basic font class.  Mostly delegates to the actual FTGL implementation
00103 ///     (see \ref ftgl)
00104 class Font {
00105 public:
00106         // virtual destructor --------------------------------------------------
00107         virtual ~Font(void) throw();
00108 
00109         // glut::Font class interface methods ----------------------------------
00110         virtual const char * getName(void) const throw() = 0;
00111         virtual font_rect_t getBoundingRect(IN const char * text) = 0;
00112         virtual void display(IN float x, IN float y, IN float z,
00113                                 IN const char * text) = 0;
00114         virtual bool canScale(void) const throw() = 0;
00115         virtual bool setFaceSize(IN float pointSize) = 0;
00116         virtual float getFaceSize(void) const throw() = 0;
00117         virtual int getLineHeight(void) const throw() = 0;
00118 };
00119 
00120 
00121 
00122 /// the font manager
00123 class FontManager {
00124 public:
00125         // virtual destructor --------------------------------------------------
00126         virtual ~FontManager(void) throw();
00127 
00128         // glut::FontManager class interface methods ---------------------------
00129 
00130         /// retrieve (and create if necessary) a font from the given true type
00131         ///     font file (.ttf).  If path is null, a default font will be used
00132         ///     (if available).  Always returns non-null (throws an exception on
00133         ///     any error).
00134         virtual smart_ptr<Font> getFont(IN const char * path) = 0;
00135 
00136         // static factory methods ----------------------------------------------
00137         static smart_ptr<FontManager> create(void);
00138 };
00139 
00140 
00141 
00142 /// retrieve the default font (throws if there isn't one)
00143 smart_ptr<Font> getDefaultFont(void);
00144 
00145 
00146 /// scale the given font to the specified height in pixels.  Returns false if
00147 ///     this was not possible (for instance, the font isn't scalable).  Caller
00148 ///     must specify what text they are scaling for (capital M is common)
00149 bool scaleFontToPixelHeight(IO Font * font,
00150                         IN const char * textToScale,
00151                         IN int desiredPixelHeight);
00152 
00153 
00154 };      // glut namespace
00155 
00156 #endif  // WAVE_GLUT_GLUT_FONT_H__
00157