Font / Character Outlines

Benoit JACQUIER b.jacquier at amesys.fr
Wed Dec 5 23:43:06 PST 2007


Hi,

Here is a class that can load any font present in your system and render it
as an OpenGL list.

Using it is rather straightforward :
- Create your GLFont object, giving it a wxGLCanvas to use
- Load a font declared as a wxFont object (including font face, weight,
style, underlined)
- Draw any wxString using OpenGL

Unfortunately, this is only for Win32.

 Hope it helps

Beno=EEt JACQUIER

 =


-----Message d'origine-----
De=A0: Stephan Rose [mailto:kermos at somrek.net] =

Envoy=E9=A0: mercredi 5 d=E9cembre 2007 21:41
=C0=A0: wx-users at lists.wxwidgets.org
Objet=A0: Font / Character Outlines

Hey all,

having mostly finished my unicode string class and having taught it to
interact smoothly with wxString when necessary, I kind of realized that
at this particular moment most of my Unicode support is pretty much a
moot point in some areas of my software.

Reason being is that I have text objects on my OpenGL canvas that
currently cannot represent anything outside ASCII.

I can't use the standard bitmap/texture approaches commonly used to
render text in OpenGL as images cannot be represented by the data files
I ultimately need to generate that these texts are a part of. So for
that reason, I currently use my own vector font that renders characters
composed of line segments which can then later be correctly represented
in the output data format.

However, this does limit me to 7 bit ascii as I'm not about to go ahead
and create a vector based definition for every unicode code point. :)

Also, it doesn't give the user the ability to choose a different font if
they like.

So I either need some method of taking a font character and creating
geometrical data from it. Either in the form of line segments or
triangulated polygons. Line segments representation is more suitable but
polygons are fine too.

I've looked around a bit on google on the subject but haven't really
found anything all that terribly useful on the subject yet.

Anyone have any experience with this? Any help / suggestions appreciated
it!

Thanks,

Stephan


---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe at lists.wxwidgets.org
For additional commands, e-mail: wx-users-help at lists.wxwidgets.org
-------------- next part --------------

#include "wx/wx.h"

#include "gl/glu.h"
#include "glut/glut.h"

#include "GLFont.h"


//######################################################################
// GLFont
//######################################################################

/*(@!**********************************************************************=
***
Nom : GLFont
Role : constructeur de la classe GLFont
---------------------------------------------------------------------------=
---
Contraintes :
Donnees en entree : =

Donnees en sortie : =

Donnees globales modifiees :
---------------------------------------------------------------------------=
---
Pseudo code :
**************************************************************************@=
!)*/
GLFont::GLFont(wxGLCanvas* glCanvas)
		: wxObject()
{
	m_pGLCanvas	=3D glCanvas;
	m_bLoaded	=3D false;
}
//----------------------------------------------------------------------


/*(@!**********************************************************************=
***
Nom : LoadFont
Role : =

---------------------------------------------------------------------------=
---
Contraintes :
Donnees en entree : =

Donnees en sortie : =

Donnees globales modifiees :
---------------------------------------------------------------------------=
--- =

Pseudo code :
**************************************************************************@=
!)*/
bool GLFont::LoadFont(const wxFont& font)
{
	if (m_bLoaded)
	{
		DeleteObject(m_hFont);			=

		glDeleteLists(m_unFontBase, 224);
	}

	m_nSize		=3D font.GetPointSize();
	m_bLoaded	=3D false;

	if (!m_pGLCanvas)
		return m_bLoaded;

	if (!m_pGLCanvas->IsShownOnScreen())
		return m_bLoaded;

	HFONT	oldfont;									// Used For Good House Keeping
	m_hDC =3D GetDC((HWND)m_pGLCanvas->GetHandle());

	m_unFontBase =3D glGenLists(224);						// Storage For 224 Characters

	m_hFont =3D CreateFont(	-m_nSize,						// Height Of Font
							0,								// Width Of Font
							0,								// Angle Of Escapement
							0,								// Orientation Angle
							font.GetWeight() =3D=3D wxFONTWEIGHT_BOLD ? FW_BOLD : FW_NORMAL,	// =
Font Weight
							font.GetStyle() =3D=3D wxFONTSTYLE_ITALIC ? TRUE : FALSE,			// Italic
							font.GetUnderlined() ? TRUE : FALSE,							// Underline
							FALSE,							// Strikeout
							ANSI_CHARSET,					// Character Set Identifier
							OUT_TT_PRECIS,					// Output Precision
							CLIP_DEFAULT_PRECIS,			// Clipping Precision
							ANTIALIASED_QUALITY,			// Output Quality
							FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
							font.GetFaceName().c_str());	// Font Name

	if (!m_hFont)
	{
		return m_bLoaded;
	}

	oldfont =3D (HFONT)SelectObject(m_hDC, m_hFont);           // Selects The =
Font We Want
	if (!wglUseFontBitmaps(m_hDC, 32, 224, m_unFontBase))	// Builds 224 Charac=
ters Starting At Character 32
	{
		return m_bLoaded;
	}
				=

	SelectObject(m_hDC, oldfont);							// Selects The Font We Want

	m_bLoaded =3D true;
	return m_bLoaded;
}
//----------------------------------------------------------------------


/*(@!**********************************************************************=
***
Nom : ~GLFont
Role : destructeur de la classe GLFont
---------------------------------------------------------------------------=
---
Contraintes :
Donnees en entree : =

Donnees en sortie : =

Donnees globales modifiees :
---------------------------------------------------------------------------=
---
Pseudo code :
**************************************************************************@=
!)*/
GLFont::~GLFont()
{
	if (m_bLoaded)
	{
		DeleteObject(m_hFont);			=

		glDeleteLists(m_unFontBase, 224);
	}
}

//----------------------------------------------------------------------

//*************
// INTERFACE **
//*************


/*(@!**********************************************************************=
***
Nom : Draw
Role : =

---------------------------------------------------------------------------=
---
Contraintes : =

Donnees en entree : =

Donnees en sortie : =

Donnees globales modifiees :
---------------------------------------------------------------------------=
---
Pseudo code :
**************************************************************************@=
!)*/
void GLFont::Draw(wxString& str)
{
	if (m_bLoaded)
	{
		glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
			glListBase(m_unFontBase - 32);								// Sets The Base Character to 32
			glCallLists((int)str.Length(), GL_UNSIGNED_BYTE, str.c_str());	// Draws =
The Display List Text
		glPopAttrib();										// Pops The Display List Bits
	}
}


/*(@!**********************************************************************=
***
Nom : GetSize
Role : =

---------------------------------------------------------------------------=
---
Contraintes : =

Donnees en entree : =

Donnees en sortie : =

Donnees globales modifiees :
---------------------------------------------------------------------------=
---
Pseudo code :
**************************************************************************@=
!)*/
wxSize GLFont::GetSize(wxString& str)
{
	HFONT			oldfont;										// Used For Good House Keeping
	SIZE			size;

	if (!m_bLoaded)
	{
		return wxSize(-1,-1);
	}

	oldfont =3D (HFONT)SelectObject(m_hDC, m_hFont);          // Selects The F=
ont We Want

	GetTextExtentPoint32(
		m_hDC,				// handle to DC
		str.c_str(),		// text string
		(int)str.Length(),		// characters in string
		&size				// string size
	);


	SelectObject(m_hDC, oldfont);							// Selects The Font We Want

	return wxSize(size.cx+1, size.cy-1);
}
-------------- next part --------------


#ifndef __GLFONT_H
#define __GLFONT_H

#include "wx/wx.h"
#include "wx/glcanvas.h"

//######################################################################
// C L A S S   D E F I N I T I O N S
//######################################################################

//*****************
// CAffichageBase
//*****************

class GLFont : public wxObject
{
public:
	GLFont(wxGLCanvas* parent);
	virtual ~GLFont();

	bool	LoadFont(const wxFont& font);
	void	Draw(wxString& str);
	wxSize	GetSize(wxString& str);
	bool	IsLoaded()				{return m_bLoaded;};

private:
	wxGLCanvas*				m_pGLCanvas;
	unsigned int			m_unFontBase;
	int						m_nSize;
	bool					m_bLoaded;

	HFONT					m_hFont;
	HDC						m_hDC;
};
//######################################################################

#endif


More information about the wx-users mailing list