HOWTO: Mix Italic and Regular (Non-Italic) Text

Support for Windows XP has ended

Microsoft ended support for Windows XP on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q244798
This article has been archived. It is offered "as is" and will no longer be updated.
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.
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 + - abc1.abcC - abc2.abcA, y, "ggg",			lstrlen("ggg"));	// Clean up.	SelectObject(hDC, hOldFont);	DeleteObject(hFontItalic);	DeleteObject(hFontRegular);	return;}				
For more information, visit the following MSDN Web sites:

3.51 4.00 KbDSI kbDSupport GdiFnt GdiTt

Article ID: 244798 - Last Review: 01/11/2015 03:10:08 - Revision: 1.6

  • Microsoft Platform Software Development Kit-January 2000 Edition
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
  • kbnosurvey kbarchive kbdswgdi2003swept kbhowto KB244798