HOWTO: Mix Italic and Regular (Non-Italic) Text (244798)
The information in this article applies to:
- Microsoft Platform Software Development Kit (SDK) 1.0
- Microsoft Windows XP Professional
- the operating system: Microsoft Windows XP 64-Bit Edition
This article was previously published under Q244798 SUMMARY
When a sequence of italic text is immediately followed by regular text, and if the placement of the text is not correct, the italic characters can overlap the other trailing text.
To resolve this issue, compensate for both the overhang of the trailing character of the first substring and for the underhang of the lead character of second substring. For TrueType fonts, use GetCharABCWidths to retrieve these values.
MORE INFORMATION
The sample code that follows outputs the string "fff" in italic text, and immediately follows it with the string "ggg" in regular (non-italic) text. If you do not compensate for the overhang and underhang values of the trailing italic character and leading non-italic character, these two substrings typically overlap.
// Sample call to the contrived example below:
ItalicAndRegular(hDC, 0, 0, 12);
// Outputs the italic string "fff" immediately followed by the non-italic
// string "ggg" at position x, y. The fonts are created at the specified
// point size and using the Times New Roman font for this contrived
// example.
void ItalicAndRegular(HDC hDC, int x, int y, int iPointSize)
{
LOGFONT lf;
HFONT hOldFont;
HFONT hFontRegular = NULL;
HFONT hFontItalic = NULL;
SIZE size;
int lpy;
ABC abc1;
ABC abc2;
// Get the vertical DPI of the device.
lpy = GetDeviceCaps(hDC, LOGPIXELSY);
// Create a regular (non-italic) font.
ZeroMemory(&lf, sizeof(lf));
lf.lfHeight = -MulDiv(iPointSize, lpy, 72 );
lf.lfItalic = FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lstrcpy(lf.lfFaceName, TEXT("Times New Roman"));
hFontRegular = CreateFontIndirect(&lf);
// Create an italic font.
ZeroMemory(&lf, sizeof(lf));
lf.lfHeight = -MulDiv(iPointSize, lpy, 72 );
lf.lfItalic = TRUE;
lf.lfCharSet = DEFAULT_CHARSET;
lstrcpy(lf.lfFaceName, TEXT("Times New Roman"));
hFontItalic = CreateFontIndirect(&lf);
// Output an italic string.
hOldFont = (HFONT)SelectObject(hDC, hFontItalic);
TextOut(hDC, x, y, "fff", lstrlen("fff"));
// Get the base width of the italic string *and* the overhang of
// the last italic character if any (abc1.abcC).
GetTextExtentPoint32(hDC, "fff", lstrlen("fff"), &size);
GetCharABCWidths(hDC, 'f', 'f', &abc1);
// Immediately follow the italic string with a non-italic string,
// taking into account the base width of the italic string, the
// overhang of the trailing italic character if any (abc1.abcC),
// and the underhang of the leading non-italic character if any
// (abc2.abcA).
SelectObject(hDC, hFontRegular);
GetCharABCWidths(hDC, 'g', 'g', &abc2);
TextOut(hDC, x + size.cx - abc1.abcC - abc2.abcA, y, "ggg",
lstrlen("ggg"));
// Clean up.
SelectObject(hDC, hOldFont);
DeleteObject(hFontItalic);
DeleteObject(hFontRegular);
return;
}
REFERENCES
For more information, visit the following MSDN Web sites:
Modification Type: | Minor | Last Reviewed: | 5/10/2006 |
---|
Keywords: | kbDSWGDI2003Swept kbhowto KB244798 |
---|
|