PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Wrapping with DrawText

Hi, I'm currently working on a code that renders a text in a given window with text wrapping. The only problem is that since we don't have access to the internal font database that GD builds when CreateFont is called and contains the kerning and width values we need to use DrawText function with CALCWIDTH function quite intensively to determine where a line wrap should take place. Johan I'm just wondering if it will change in 2.02. Suggestions are welcome. Thanks.
Hi! Please feel email me any suggestions you might have regarding function names and data return types at gapidraw@viktoria.se
I was just thinking about creating a wrapper for simple text processing that would parse the input string for \n and simply 'chop' it up and draw the text on separate lines. Warlords does a TON of text rendering from a file-based string table and this feature would be wonderful to have in GapiDraw 2.02

Another nice text feature (I'm just dreaming here) would be to pass DrawText a string with basic format commands (such as %d) along with an argument list and have the method insert the embedded text. Many of our text strings are currently broken up into multiple segments that have to be read in separately and have a value inserted in them, then drawn out. For example :

read in : You have
embed : (some number)
read in : cities
merge them together and DrawText

It would be nice if I could :

read in : You have %d cities
pass DrawText the string and some number in an arglist.

Just a thought =)
Thanks for the answer.
Actually I would need two things to speed up my text wrapper. Firstly, it would be nice to have a function that returns the width of a specified character of a bitmap font.

Code:

DWORD CGapiBitmapFont::GetCharacterWidth (TCHAR character);


Secondly, another function that returns the space in pixels (positive or negative value) between two characters of a bitmap font.

Code:

int CGapiBitmapFont::GetSpacing (TCHAR character1, TCHAR character2);


These are two things I can think of. On the other hand it would be more efficient to get access through a pointer to the internal font structure of GapiDraw although I don't know how it looks like and how complicated it is. The two functions above would do.

Please consider these things.
After re-reading my post above I realized I must have been half asleep at the time. While I still would like to see auto wrapping (or at least CR/LF at specified locations within the string), my request for string format handling was, well, a bit off.

Text format handling is easy to do :

1) load the text into a TCHAR string
2) use _stprintf to fill in the format info
3) draw the text

Consider you have a text string "value = %d":

Code:

TCHAR * format = _T("value = %d");
TCHAR str[128];
INT num = 123;
_stprintf(str, format, num);


This will use the format info to create a string to pass to DrawText. Sorry for the confusion...
You are right even the GapiDraw examples are using the _sprintf method when printing the framerate value :) .
Nevertheless it would be good to have access to bitmap fonts' width and spacing values to be able to do quick multi-line text wrapper routines. What I'm doing now is a kind of precalculation. I precalcalculate the points where the string should be clipped and store these values as pointers to the first character of each line. When the window size is changed the recalc is needed to be done again but until then everything runs at full speed. all I gotta do is dump the text according to the precalculated clipping points. The speed of the precalculation could be improved big time if we had access to the font data somehow. Btw, I'm working on a basic widget framwork for GapiDraw that's why I need all this.
cottrelld · 08 Jul 2003, 04:16 UTC — Agreed
I would also like access to that info. I'm currently making a rough estimate of what fits in a line and verifying with calcwidth. If I'm over i chop off a word and add it to the next line. This way I have less calls to calcwidth, but its still not optimal.

-David
Hi Cottrelld, it's good to know that I'm not alone with this request :) Could you tell me how do you do that 'rough estimate'? Actually I go word by word treating space as word boundary, I calculate the actual width of the sentence with DrawText and when it gets too long I insert a clipping point to the previous word which did not exceed the line width. The actual problem starts when a word is too long to fit in one line because I need to go character by character to determine the clipping point and it is really slow.
I'm new to GD but can I make a suggestion? Create a function that takes a whole string of characters and then returns the dimensions used up by the string - Width & Height in pixels.

For a fixedsize 8x8 font:
"ace" will give us a dimension of Width = 24 and Height = 6 (lowercase).
"ACE" will give us a dimension of Width = 24 and Height = 8 (uppercase).

GD will then do the calculation natively which will be a big performance boost, rather than making multiple function calls for every character in the a string which will take longer.

Uses for this function for example will allow one to create a destination surface according to the dimensions, blit the string to that surface, then do all sorts of extra effects like scaling and rotation...

Like I said, just a suggestion - use it don't use it. :)

[EDIT - a few minute later]

I see that CGapiSurface::DrawText has support for GDDRAWTEXT_CALCWIDTH, but doesn't have a GDDRAWTEXT_CALCHEIGHT, which would be a nice to have. But CGapiSurface::RenderSystemFont does not have the same ability.
Hi 8Bit, actually I don't think there is a need for CALC_HEIGHT since you create the font so you probably know its dimensions or you can get it by determining the height of the bitmap you create the font from. As for the system fonts according to the GapiDraw documentation:

The system font is of variable width (1-6 pixels + 1 pixel white space), and fixed height (7 pixels with border, 5 pixels without).
If it was really important you could try to gather that info as soon as you load the font, like sending each character in a string alone and then saving the width. I see no reason why it would ever change.

This works unless you modify the kerning ala:
Code:

Font.SetKerning(TEXT('_'), TEXT('_'), -1);
Font.SetKerning(TEXT('t'), TEXT('t'), -2);
...


In cases where you use that it becomes a bit more complex.
Pejo Software - Per · 09 Jul 2003, 07:54 UTC
This might be a little off topic...
When doing the help section of my game (smartphone version)I wanted to use different font sizes, bold and italic typefaces. Being lazy I used GDI for this.
I thought I will provide the source code here if anyone is interested (it is not the most beautiful code but it works).
Header file (GDITextSurface.h):
Code:
// GDITextSurface.h: interface for the CGDITextSurface class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_GDITEXTSURFACE_H__33F7CA46_081D_48CB_90D3_2E3E4AB054EC__INCLUDED_)
#define AFX_GDITEXTSURFACE_H__33F7CA46_081D_48CB_90D3_2E3E4AB054EC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <gapidraw.h>

class CGDITextSurface : public CGapiSurface
{
public:
CGDITextSurface();
virtual ~CGDITextSurface();
HRESULT CreateSurface(DWORD dwWidth, DWORD dwHeight,TCHAR* text,COLORREF textcolor,LOGFONT *pLF=0);

};

#endif // !defined(AFX_GDITEXTSURFACE_H__33F7CA46_081D_48CB_90D3_2E3E4AB054EC__INCLUDED_)

implementation file (GDITextSurface.cpp):
Code:
// GDITextSurface.cpp: implementation of the CGDITextSurface class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "beshaped.h"
#include "GDITextSurface.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGDITextSurface::CGDITextSurface()
{

}

CGDITextSurface::~CGDITextSurface()
{

}

HRESULT CGDITextSurface::CreateSurface(DWORD dwWidth, DWORD dwHeight,TCHAR* text,COLORREF textcolor,LOGFONT *pLF)
{
RECT r={0,0,0,0};
r.right=dwWidth;
r.bottom=dwHeight;
HRESULT hr;

HDC hdc;
int h=0;
LOGFONT lf;
if(pLF==0)
{

lf.lfHeight = 12; // character height in pixels
lf.lfWidth = 4; // character width in pixels
lf.lfEscapement = 0; // orientation of next character
lf.lfOrientation = 0; // orientation of first character
lf.lfWeight = FW_NORMAL; // normal thickness
lf.lfItalic = FALSE; // not italic characters
lf.lfUnderline = FALSE; // not underlined characters
lf.lfStrikeOut = FALSE; // not strikeout characters
lf.lfCharSet = OEM_CHARSET; // OEM character set
lf.lfOutPrecision = 0; // default output precision
lf.lfClipPrecision = 0; // default clipping precision
lf.lfQuality = DEFAULT_QUALITY; // default scaling quality
lf.lfPitchAndFamily = FF_DONTCARE; // default pitch, script font family
lstrcpy(lf.lfFaceName,_T("")); // Nina typeface
pLF=&lf;
}

HFONT hFont = CreateFontIndirect(pLF);
if(dwWidth==0)
{
hr=CGapiSurface::CreateSurface(0, 1, 1);
hr=GetDC(&hdc);
SelectObject(hdc,hFont);
h=::DrawText(hdc, text, -1, &r, DT_LEFT|DT_CALCRECT);
ReleaseDC(hdc);
hr=CGapiSurface::CreateSurface(GDSURFACE_CLEAR, r.right, r.bottom);
SetColorKey(RGB(0,0,0));

hr=GetDC(&hdc);
SelectObject(hdc,hFont);
SetTextColor(hdc,textcolor);
SetBkColor(hdc,RGB(0,0,0));

h=::DrawText(hdc, text, -1, &r, DT_LEFT);
ReleaseDC(hdc);
DeleteObject(hFont);
return hr;
}
if(dwHeight==0)
{
hr=CGapiSurface::CreateSurface(0, 1, 1);
GetDC(&hdc);
SelectObject(hdc,hFont);
h=::DrawText(hdc, text, -1, &r, DT_LEFT|DT_WORDBREAK|DT_CALCRECT);
ReleaseDC(hdc);
}

hr=CGapiSurface::CreateSurface(GDSURFACE_CLEAR, r.right, r.bottom);
SetColorKey(RGB(0,0,0));

GetDC(&hdc);
SelectObject(hdc,hFont);
SetTextColor(hdc,textcolor);
SetBkColor(hdc,RGB(0,0,0));

h=::DrawText(hdc, text, -1, &r, DT_LEFT|DT_WORDBREAK);
ReleaseDC(hdc);
DeleteObject(hFont);
return hr;
}


You can then use this class to create a surface that holds the text you want to display. CreateSurface will break lines at words and the height of the surface will be set automaticly if you pass height=0. if you pass width=0 a single line surface will be created.

This code also suffers from problems if you have words that are longer than a line but you can pass \r\n to break the line.
Johan, what a nice solution could be is the following function:

Code:
HRESULT DrawTextMultiLine(DWORD dwX,
DWORD dwY,
const TCHAR* pString,
DWORD dwTextFlags,
DWORD dwHeight);


In this function GD draws the string at x, y. If it encounters a '\n' it returns back to x and increases y with dwHeight.
Hi,

Unfortunately I don't have the time to revamp the DrawText function.. But I will add the following functions in 2.02:

// Returns the width in pixels needed to draw the string pString
HRESULT GetStringWidth(const TCHAR* pString, DWORD* pWidth);

// Returns the width in pixels for the specified character
HRESULT GetCharWidth(TCHAR tcChar, DWORD* pWidth);

// Returns the white space in pixels between the two characters
HRESULT GetSpacing(TCHAR tcChar1, TCHAR tcChar2, DWORD* pSpacing);

Hope it's enough for most uses. It would involve too much work to add the individual height of each character so unfortunately this will not be added.

I will however probably change the DrawText function to support features such as drawing text in columns etc in the future, hope it's not too much pain to change all the DrawText calls (The last parameter DWORD* pWidth would be changed to GDDRAWTEXTFX* pFX)...
There are some other advantages of not using CALCWIDTH with DrawText as well - now you can for example first get the string width from CGapiBitmapFont, and later create a surface the exact width after that.
Johan thanks for the info. I very much look forward to GD 2.02. Do you have an estimated release date yet?