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

Article translations Article translations
Article ID: 244798 - View products that this article applies to.
This article was previously published under Q244798
Expand all | Collapse all

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:

Windows GDI - Fonts and Text
http://msdn.microsoft.com/en-us/library/ms534210.aspx
How to Fit Text in a Rectangle
http://support.microsoft.com/default.aspx?scid=kb;[LN];Q200262

Properties

Article ID: 244798 - Last Review: February 12, 2007 - Revision: 1.6
APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
Keywords: 
kbdswgdi2003swept kbhowto KB244798

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com