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 "nstream/nstream.h"
00039 #include "threadsafe/smart_ptr.h"
00040 
00041 
00042 namespace glut {
00043 
00044 ////////////////////////////////////////////////////////////////////////////////
00045 ///
00046 /// \ingroup wave_glut
00047 /// \defgroup glut_font Glut Font Library
00048 ///
00049 /// A font helper library.  Keeps other code abstracted from the details of
00050 /// font implementation.  Currently this is a wrapper on top of the FTGL
00051 /// library.
00052 ///
00053 /// \b WARNING: this library is used for 2D font rendering.  That is, it assumes
00054 /// that we are in 2D rendering mode (see the glut::push2D() and pop2D()
00055 /// methods).  This library uses the wavepacket convention for 2D coordinates:
00056 /// y=0 is the TOP of the screen, and y=height is the BOTTOM of the screen.
00057 /// That is a bit unusual: OpenGL flips that convention (and OpenGL's convention
00058 /// is probably better).  But for legacy reasons, this library uses the
00059 /// wavepacket convention.
00060 ///
00061 /// This means, for instance, that it is *very* awkward to attempt to use these
00062 /// routines to render while in 3D rendering mode.
00063 ///
00064 ////////////////////////////////////////////////////////////////////////////////
00065 
00066 /*@{*/
00067 
00068 
00069 /// standard 2D pixel rect
00070 typedef rect2d_t<int> pixel_rect_t;
00071 
00072 
00073 
00074 /// font sizing information
00075 struct font_rect_t {
00076         // constructor, manipulators
00077         font_rect_t(void) throw() { this->clear(); }
00078         void clear(void) throw() {
00079                         lead = trail = drop = rise = 0;
00080                 }
00081         void getPixelRect(OUT pixel_rect_t& pr) const throw() {
00082                         pr.left = -lead;
00083                         pr.right = trail;
00084                         pr.top = -rise;
00085                         pr.bottom = drop;
00086                 }
00087 
00088         // data fields
00089         int     lead;           ///< pixels leading (to left of) draw origin
00090         int     trail;          ///< pixels trailing (to right of) draw origin
00091         int     drop;           ///< pixels falling below draw origin
00092         int     rise;           ///< pixels above draw origin
00093 };
00094 
00095 
00096 
00097 ////////////////////////////////////////////////////////////////////////////////
00098 //
00099 //      public API
00100 //
00101 ////////////////////////////////////////////////////////////////////////////////
00102 
00103 /// a basic font class.  Mostly delegates to the actual FTGL implementation
00104 ///     (see \ref ftgl)
00105 class Font {
00106 public:
00107         // virtual destructor --------------------------------------------------
00108         virtual ~Font(void) throw();
00109 
00110         // glut::Font class interface methods ----------------------------------
00111         virtual const char * getName(void) const throw() = 0;
00112         virtual font_rect_t getBoundingRect(IN const char * text) = 0;
00113         virtual void display(IN float x, IN float y, IN float z,
00114                                 IN const char * text) = 0;
00115         virtual bool canScale(void) const throw() = 0;
00116         virtual bool setFaceSize(IN float pointSize) = 0;
00117         virtual float getFaceSize(void) const throw() = 0;
00118         virtual int getLineHeight(void) const throw() = 0;
00119 };
00120 
00121 
00122 
00123 /// the font manager
00124 class FontManager {
00125 public:
00126         // virtual destructor --------------------------------------------------
00127         virtual ~FontManager(void) throw();
00128 
00129         // glut::FontManager class interface methods ---------------------------
00130 
00131         /// retrieve (and create if necessary) a font from the given true type
00132         ///     font file (.ttf) stream.  If stream is null, a default font will
00133         ///     be used (if available).  Always returns non-null (throws an
00134         ///     exception on any error).
00135         virtual smart_ptr<Font> getFont(IN nstream::Stream * stream) = 0;
00136 
00137         // static factory methods ----------------------------------------------
00138         static smart_ptr<FontManager> create(void);
00139 };
00140 
00141 
00142 
00143 /// retrieve the default font (throws if there isn't one)
00144 smart_ptr<Font> getDefaultFont(void);
00145 
00146 
00147 /// scale the given font to the specified height in pixels.  Returns false if
00148 ///     this was not possible (for instance, the font isn't scalable).  Caller
00149 ///     must specify what text they are scaling for (capital M is common)
00150 bool scaleFontToPixelHeight(IO Font * font,
00151                         IN const char * textToScale,
00152                         IN int desiredPixelHeight);
00153 
00154 
00155 };      // glut namespace
00156 
00157 #endif  // WAVE_GLUT_GLUT_FONT_H__
00158