# OpenGL unicode fonts

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I wanted to support unicode in my game, but I'm thinking how to get it to draw on opengl. Can I generate bitmap fonts for all the chars in unicode? (I think it'll use too much video memory) If I create a texture for each string on demand, and upload it to the video card, will it be too slow? or you guys have other ways of dealing it? thanks

##### Share on other sites
hey ,

i think loading all maps in VRAM will be kinda expensive so try to load them in RAM and bind them to VRAM when u need it and free it when u done from it.
cya,

##### Share on other sites
There are a lot of glyphs in Unicode. Trying to create a bitmap for each one would not be a good idea. If you plan to support arbitrary Unicode text then you should consider rendering as needed from a font.

##### Share on other sites

ohboy this question was for me
As already pointed out you probably don't want to do this with texture fonts. Of course you could have a default 16x16 glyph size, and generate 1-bit textures (say 256 glyphs on a 256x256 texture) for every character in the basic multilingual plane, at a cost of 256 textures, or a little over 2MB disk space. I almost did this because it would mean the unicode rendering is completely self contained and portable.

However, apart from being stuck with only one (aliased) font, the resulting text would be quite fugly having commas the same width as 'w' for example. Also when you show characters larger than the size they are in the texture, it would be fuglier than fugly. There would probably be a marginal speed hit too, unless you did something clever to reduce the number of texture binds necessary per frame. I still dont think its such a bad idea, but in the end I went with using wglUseFontOutlines to make a display list for each character. This function also gives you the dimensions of the glyph which you can use for a bunch of things: checking if it should be rendered or will need to have a line break, checking if mouse is over it, rendering text as 'selected' by the user etc. Since the display list for the character is made up of lines/polygons, you can do all kinds of 3D rendering with them which you cant do with texture fonts.

Anyway, allow me to contribute some code that I lost a lot of hair making. Here are a couple of functions from my UnicodeString class, with relevant support functions for them. buildDisplayLists takes a wstring and iterates over the characters in it to create a display list for each one, and puts them in a map. The map makes sure you dont create any redundant lists. The result is stored as an array of GLuints in the class, these are the ids of the display lists which need to be called to draw the whole string. Each characters display list includes a glTranslate to position ready for the next character, so you can see the render function just does glCallLists.
Admittedly the characters do look kinda crusty when displayed very small, but they scale up nicely.

Note:
You can't just pop in some Thai/Arabic/Big5 text and expect it to work. Most fonts will only contain a subset of unicode glyphs. For example, the font I used here is "MS UI Gothic". As far as I know this is the font windows uses by default for all its menus and on my pc this includes Japanese glyphs but not Korean, so I need to change the font to something else to view Korean. On your pc you may not have either of these. Also within a font there can be more than one character set. This may also need changing in order to see what you are expecting to see, but so far I havent found it necessary.

HFONT oldfont = NULL, newfont = NULL;void selectFont() {	int nHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);	LOGFONT logfont;    logfont.lfHeight        = nHeight;    logfont.lfWidth         = 0;    logfont.lfEscapement    = 0;    logfont.lfOrientation   = 0;    logfont.lfWeight        = FW_NORMAL;    logfont.lfItalic        = FALSE;    logfont.lfUnderline     = FALSE;    logfont.lfStrikeOut     = FALSE;    logfont.lfCharSet       = DEFAULT_CHARSET;    logfont.lfOutPrecision  = OUT_DEFAULT_PRECIS;    logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;    logfont.lfQuality       = DEFAULT_QUALITY;    logfont.lfPitchAndFamily= FF_DONTCARE|DEFAULT_PITCH;	lstrcpy( logfont.lfFaceName, "MS UI Gothic" );	//lstrcpy( logfont.lfFaceName, "Comic Sans MS" );	newfont = CreateFontIndirect( &logfont );	if (!newfont)        fileLog.log(LL_ERROR, "CreateFontIndirect failed");    oldfont = (HFONT)SelectObject( hDC, newfont );	if (!oldfont)        fileLog.log(LL_ERROR, "SelectObject failed");    assert( oldfont );}void releaseFont() {	assert(oldfont);	if (!oldfont)        fileLog.log(LL_ERROR, "SelectObject failed (%s)", __FUNCTION__);	SelectObject(hDC, oldfont);}//these are members of UnicodeString classint length;GLuint* lists;GLYPHMETRICSFLOAT* glyphMetrics;BoundingBox boundingBox;static map<wchar_t, characterData*> characterCache;void UnicodeString::buildDisplayLists(wstring _text){	length = (int)_text.length();	lists = new GLuint[length];	glyphMetrics = new GLYPHMETRICSFLOAT[length];	selectFont();	for (int i = 0; i < length; i++) {		map<unsigned short, characterData*>::iterator it = characterCache.find(text);		if (it != characterCache.end()) {			characterData* cd = (*it).second;			lists = cd->listId;			glyphMetrics = cd->glyphMetrics;			continue;		}		lists = glGenLists(1);		assert(lists);		int e = glGetError();		if (!lists) {			fileLog.log(LL_ERROR, "glGenLists failed, glGetError = %d", e);			continue;// or... ?		}		BOOL ok = wglUseFontOutlinesW( hDC, text, 1, lists, 0, 0, WGL_FONT_POLYGONS, &glyphMetrics);		e = glGetError();		assert(ok || !e);		if (!ok)			fileLog.log(LL_ERROR, "wglUseFontOutlines failed, glGetError = %d", e);		characterData* cd = new characterData;		cd->listId = lists;		cd->glyphMetrics = glyphMetrics;		characterCache[text] = cd;	}	releaseFont();		boundingBox = BoundingBox();	float baseX = 0, baseY = 0;    for (int i = 0; i < length; i++) {		GLYPHMETRICSFLOAT g = glyphMetrics;		boundingBox.expandToCover(baseX + g.gmfptGlyphOrigin.x, -0.15f);		boundingBox.expandToCover(baseX + g.gmfptGlyphOrigin.x+g.gmfBlackBoxX, -0.15f);		boundingBox.expandToCover(baseX + g.gmfptGlyphOrigin.x+g.gmfBlackBoxX, 0.88f);		boundingBox.expandToCover(baseX + g.gmfptGlyphOrigin.x, 0.85f);		baseX += g.gmfCellIncX;		baseY += g.gmfCellIncY;	}}void UnicodeString::renderText(){	//set color,translation,rotation,scale etc here	//scale should be roughly the height in pixels that you want the character to be	glCallLists(length, GL_UNSIGNED_INT, lists);	}

Some screens of this.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015617
×