Re: [Q]How to display Tahoma italic font
- From: "John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx>
- Date: Tue, 16 May 2006 17:18:14 +1000
"Johnny" <johnny194@xxxxxxxxxxx> wrote in message
news:1147751450.865990.234670@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
I can't display Tahoma font in italic properly using
GetTextExtentPoint32W(), do you have any ideas about it? here is a
brief view of the code:
You don't say how the display is improper, but I take it that your concern
is that the middle italic letters overlap the right hand normal letters.
This is hardly surprising. The italic letters lean right so if you put them
alongside upright text with no intervening spaces, then they are likely to
collide. GetTextExtentPoint32 is intended to get the separation right on the
assumption that the letters lean the same way. You can't reliably get
correct separation both at the baseline and higher up if you are mixing
fonts in a single word. In normal typesetting, you don't switch between
fonts within a single word, partly for this reason.
If you must mix italic and normal fonts as in your example, then you can
generally avoid a collision by SUBTRACTING the C width of the last italic
character from the horizontal position, i.e., after drawing the italic
string and incrementing XIncrement, you should do a second increment as
follows:
ABC abc;
GetCharABCWidthsW(hdc, wString[stringLen-1], wString[stringLen-1], &abc);
XIncrement -= abc.abcC;
(you could, in addition, subtract the non-italic A width of the first
character of the non-italic string that follows).
Some additional comments are inline.
void demoFunc(HDC hdc)
{
int XIncrement;
int YStart;
HFONT hfntDefault;
HFONT hfntItalic;
HFONT hfntNormal;
LPSTR lpszString = "dd";
int stringLen = strlen(lpszString);
LPWSTR wString = (LPWSTR)malloc(WCHAR_STRING_LENGTH * sizeof(LPWSTR));
So what is WCHAR_STRING_LENGTH? This doesn't make sense on several levels.
First, I don't know why you are converting from ANSI to Unicode instead of
starting out with Unicode. Second sizeof(LPWSTR) is just the size of a
pointer which has nothing to do with how much space you need for the string.
Third, you have not made any allowance for the terminating NUL. The
following makes more sense:
LPWSTR wString = (LPWSTR)malloc(sizeof(WCHAR) * (stringLen+1));
SIZE sz;
// Create a normal and an italic logical font
hfntItalic = MyCreateFont(ITALIC_FONT);
hfntNormal = MyCreateFont(NORMAL_FONT);
// translate the LPSTR string to LPWSTR string
MultiByteToWideChar(CP_ACP, 0, lpszString, stringLen, wString,
WCHAR_STRING_LENGTH);
Change WCHAR_STRING_LENGTH to stringLen+1.
// Select the normal font and draw the first string
// beginning at the specified point (XIncrement, YStart).
XIncrement = 10;
YStart = 50;
hfntDefault = (HFONT)SelectObject(hdc, hfntNormal);
TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);
Make that TextOutW (unless UNICODE is #defined, in which cas you don't need
all your other Ws).
GetTextExtentPoint32W(hdc, (LPCWSTR)wString, stringLen, &sz);
XIncrement += sz.cx;
// Select an italic font and draw the second string
// beginning at the point (XIncrement, YStart).
hfntNormal = (HFONT)SelectObject(hdc, hfntItalic);
TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);
TextOutW
// Compute the length of the second string and add
// this value to the x-increment that is used for the
// text-output operation.
GetTextExtentPoint32W(hdc, (LPCWSTR)wString, stringLen, &sz);
XIncrement += sz.cx;
// Reselect the normal font and draw the third string
// beginning at the point (XIncrement, YStart).
SelectObject(hdc, hfntNormal);
TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);
TextOutW
// Reselect the original font.
SelectObject(hdc, hfntDefault);
// Delete the bold and italic fonts.
DeleteObject(hfntItalic);
DeleteObject(hfntNormal);
free(wString);
}
HFONT MyCreateFont(FontType ftype)
{
LOGFONT logfont = {0};
wcscpy(logfont.lfFaceName, L"Tahoma");
switch (ftype)
{
case ITALIC_FONT:
logfont.lfItalic = TRUE;
break;
case BOLD_FONT:
logfont.lfWeight = FW_BOLD;
break;
case NORMAL_FONT:
break;
default:
assert(0);
break;
}
return CreateFontIndirect(&logfont);
}
I've been searching for a solution for weeks, please help me,
thanks.
Regards,
Johnny
.
- Follow-Ups:
- Re: [Q]How to display Tahoma italic font
- From: Johnny
- Re: [Q]How to display Tahoma italic font
- References:
- [Q]How to display Tahoma italic font
- From: Johnny
- [Q]How to display Tahoma italic font
- Prev by Date: [Q]How to display Tahoma italic font
- Next by Date: Re: [Q]How to display Tahoma italic font
- Previous by thread: [Q]How to display Tahoma italic font
- Next by thread: Re: [Q]How to display Tahoma italic font
- Index(es):
Relevant Pages
|