Path: blob/master/libmupen64plus/mupen64plus-core/src/osd/OGLFT.h
2 views
// -*- c++ -*-1/*2* OGLFT: A library for drawing text with OpenGL using the FreeType library3* Copyright (C) 2002 lignum Computing, Inc. <[email protected]>4* $Id: OGLFT.h,v 1.15 2003/10/01 14:41:09 allen Exp $5*6* This library is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* version 2.1 of the License, or (at your option) any later version.10*11* This library is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with this library; if not, write18* Free Software Foundation, Inc.,19* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.20*/2122#ifndef OGLFT_H23#define OGLFT_H2425#include <cmath>26#include <map>27#include <list>28#include <vector>29#include <wchar.h>3031#define GL_GLEXT_PROTOTYPES32#include <SDL_opengl.h>33#if defined(__MACOSX__)34#include <OpenGL/glu.h>35#elif defined(__MACOS__)36#include <glu.h>37#else38#include <GL/glu.h>39#endif4041#include <ft2build.h>42#include FT_FREETYPE_H43#include FT_GLYPH_H44#include FT_OUTLINE_H45#include FT_TRIGONOMETRY_H4647namespace OGLFT48{49enum Coordinates50{51X, Y, Z, W52};5354enum ColorSpace55{56R, G, B, A57};5859// global library functions60bool Init_FT(void);61bool Uninit_FT(void);6263struct Advance64{65float dx_;66float dy_;6768Advance ( float dx = 0, float dy = 0 ) : dx_( dx ), dy_( dy )69{70return;71}7273Advance ( FT_Vector v )74{75dx_ = (float) (v.x / 64.);76dy_ = (float) (v.y / 64.);77}7879Advance& operator+= ( const FT_Vector v )80{81dx_ += (float) (v.x / 64.);82dy_ += (float) (v.y / 64.);83return *this;84}85};8687struct BBox88{89float x_min_;90float y_min_;91float x_max_;92float y_max_;93Advance advance_;9495BBox () : x_min_( 0 ), y_min_( 0 ), x_max_( 0 ), y_max_( 0 )96{97return;98}99BBox ( FT_BBox ft_bbox )100{101x_min_ = (float) (ft_bbox.xMin / 64.);102y_min_ = (float) (ft_bbox.yMin / 64.);103x_max_ = (float) (ft_bbox.xMax / 64.);104y_max_ = (float) (ft_bbox.yMax / 64.);105}106BBox& operator*= ( double k )107{108x_min_ *= (float) k;109y_min_ *= (float) k;110x_max_ *= (float) k;111y_max_ *= (float) k;112advance_.dx_ *= (float) k;113advance_.dy_ *= (float) k;114115return *this;116}117BBox& operator+= ( const BBox& b )118{119float new_value;120121new_value = b.x_min_ + advance_.dx_;122if ( new_value < x_min_ ) x_min_ = new_value;123new_value = b.y_min_ + advance_.dy_;124if ( new_value < y_min_ ) y_min_ = new_value;125new_value = b.x_max_ + advance_.dx_;126if ( new_value > x_max_ ) x_max_ = new_value;127new_value = b.y_max_ + advance_.dy_;128if ( new_value > y_max_ ) y_max_ = new_value;129130advance_.dx_ += b.advance_.dx_;131advance_.dy_ += b.advance_.dy_;132133return *this;134}135};136typedef std::vector<GLuint> DisplayLists;137typedef DisplayLists::const_iterator DLCI;138typedef DisplayLists::iterator DLI;139class Face140{141public:142enum HorizontalJustification143{144LEFT,145ORIGIN,146CENTER,147RIGHT148};149150enum VerticalJustification151{152BOTTOM,153BASELINE,154MIDDLE,155TOP156};157158enum GlyphCompileMode159{160COMPILE,161IMMEDIATE162};163164private:165struct FaceData166{167FT_Face face_;168bool free_on_exit_;169FaceData ( FT_Face face, bool free_on_exit = true )170: face_( face ), free_on_exit_( free_on_exit )171{172return;173}174};175176protected:177std::vector< FaceData > faces_;178bool valid_;179enum GlyphCompileMode compile_mode_;180float point_size_;181FT_UInt resolution_;182bool advance_;183GLfloat foreground_color_[4];184GLfloat background_color_[4];185enum HorizontalJustification horizontal_justification_;186enum VerticalJustification vertical_justification_;187GLfloat string_rotation_;188FT_UInt rotation_reference_glyph_;189FT_Face rotation_reference_face_;190GLfloat rotation_offset_y_;191typedef std::map< FT_UInt, GLuint > GlyphDLists;192typedef GlyphDLists::const_iterator GDLCI;193typedef GlyphDLists::iterator GDLI;194GlyphDLists glyph_dlists_;195DisplayLists character_display_lists_;196197public:198Face ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );199Face ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );200virtual ~Face ( void );201bool isValid ( void ) const { return valid_; }202bool addAuxiliaryFace ( const char* filename );203bool addAuxiliaryFace ( FT_Face face );204void setCompileMode ( enum GlyphCompileMode compile_mode ) { compile_mode_ = compile_mode; }205enum GlyphCompileMode compileMode ( void ) const { return compile_mode_; }206void setPointSize ( float point_size );207float pointSize ( void ) { return point_size_; }208void setResolution ( FT_UInt resolution );209FT_UInt resolution ( void ) { return resolution_; }210void setAdvance ( bool advance ) { advance_ = advance; }211bool advance ( void ) const { return advance_; }212void setForegroundColor ( GLfloat red = 0.0, GLfloat green = 0.0, GLfloat blue = 0.0, GLfloat alpha = 1.0 );213void setForegroundColor ( const GLfloat foreground_color[4] );214GLfloat foregroundRed ( void ) const { return foreground_color_[R]; }215GLfloat foregroundGreen ( void ) const { return foreground_color_[G]; }216GLfloat foregroundBlue ( void ) const { return foreground_color_[B]; }217GLfloat foregroundAlpha ( void ) const { return foreground_color_[A]; }218void setBackgroundColor ( GLfloat red = 1.0, GLfloat green = 1.0, GLfloat blue = 1.0, GLfloat alpha = 0.0 );219void setBackgroundColor ( const GLfloat background_color[4] );220GLfloat backgroundRed ( void ) const { return background_color_[R]; }221GLfloat backgroundGreen ( void ) const { return background_color_[G]; }222GLfloat backgroundBlue ( void ) const { return background_color_[B]; }223GLfloat backgroundAlpha ( void ) const { return background_color_[A]; }224virtual void setCharacterRotationZ ( GLfloat character_rotation_z ) = 0;225virtual GLfloat characterRotationZ ( void ) const = 0;226void setCharacterRotationReference ( unsigned char c );227void setStringRotation ( GLfloat string_rotation );228GLfloat stringRotation ( void ) const { return string_rotation_; }229void setHorizontalJustification ( enum HorizontalJustification horizontal_justification )230{231horizontal_justification_ = horizontal_justification;232}233enum HorizontalJustification horizontalJustification ( void ) const { return horizontal_justification_; }234void setVerticalJustification ( enum VerticalJustification vertical_justification )235{236vertical_justification_ = vertical_justification;237}238239enum VerticalJustification verticaljustification ( void ) const { return vertical_justification_; }240void setCharacterDisplayLists ( const DisplayLists& character_display_lists ) { character_display_lists_ = character_display_lists; }241DisplayLists& characterDisplayLists ( void ) { return character_display_lists_; }242virtual double height ( void ) const = 0;243virtual BBox measure ( unsigned char c ) = 0;244virtual BBox measure ( wchar_t c ) = 0;245virtual BBox measure ( const char* s );246virtual BBox measureRaw ( const char* s );247virtual BBox measure ( const wchar_t* s );248virtual BBox measure ( const wchar_t* format, double number );249virtual BBox measureRaw ( const wchar_t* s );250GLuint compile ( unsigned char c );251GLuint compile ( const wchar_t c );252void draw ( const char* s );253void draw ( const wchar_t* s );254void draw ( unsigned char c );255void draw ( const wchar_t c );256void draw ( GLfloat x, GLfloat y, unsigned char c );257void draw ( GLfloat x, GLfloat y, GLfloat z, unsigned char c );258void draw ( GLfloat x, GLfloat y, wchar_t c );259void draw ( GLfloat x, GLfloat y, GLfloat z, wchar_t c );260void draw ( GLfloat x, GLfloat y, const char* s, float *sizebox );261void draw ( GLfloat x, GLfloat y, GLfloat z, const char* s );262void draw ( GLfloat x, GLfloat y, const wchar_t* s );263void draw ( GLfloat x, GLfloat y, GLfloat z, const wchar_t* s );264int ascender ( void ) { return faces_.front().face_->ascender; }265int descender ( void ) { return faces_.front().face_->descender; }266BBox measure_nominal ( const char* s );267BBox measure_nominal ( const wchar_t* s );268269protected:270virtual GLuint compileGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;271virtual void renderGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;272virtual void setCharSize ( void ) = 0;273virtual void clearCaches ( void ) = 0;274virtual void setRotationOffset ( void ) = 0;275276private:277void init ( void );278};279280class Raster : public Face281{282protected:283GLfloat character_rotation_z_;284public:285Raster ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );286Raster ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );287virtual ~Raster ( void );288void setCharacterRotationZ ( GLfloat character_rotation_z );289GLfloat characterRotationZ ( void ) const { return character_rotation_z_; }290double height ( void ) const;291BBox measure ( unsigned char c );292BBox measure ( wchar_t c );293BBox measure ( const char* s ) { return Face::measure( s ); }294BBox measure ( const wchar_t* format, double number ) { return Face::measure( format, number ); }295296private:297void init ( void );298GLuint compileGlyph ( FT_Face face, FT_UInt glyph_index );299void setCharSize ( void );300void setRotationOffset ( void );301void clearCaches ( void );302};303class Monochrome : public Raster304{305public:306Monochrome ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );307Monochrome ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );308~Monochrome ( void );309310private:311GLubyte* invertBitmap ( const FT_Bitmap& bitmap );312void renderGlyph ( FT_Face face, FT_UInt glyph_index );313};314315}316#endif /* OGLFT_H */317318319320