HOWTO: Modify the Intercharacter Spacing Array for a String (263536)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional
  • Microsoft Win32 Software Development Kit (SDK) 4.0
  • Microsoft Windows 95
  • Microsoft Windows 98
  • Microsoft Windows 98 Second Edition
  • Microsoft Windows NT Server 4.0
  • Microsoft Windows NT Workstation 4.0
  • Microsoft Windows XP Home Edition
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition

This article was previously published under Q263536

SUMMARY

You can use the TextOut and ExtTextOut functions with a NULL intercharacter spacing (ICS) array to produce text with a default spacing. When it becomes necessary to change that default spacing, it is convenient to start with the original default spacing and modify it to suit the current need. This article describes how to obtain a default ICS array, and how to modify it to alter the spacing within a string.

MORE INFORMATION

The code below demonstrates how to use the GetCharABCWidths function to determine the default intercharacter spacing array for a string, and how to modify that array to alter the spacing of the text.

This code was designed to work with 8-bit ANSI strings or, when compiling for Unicode, with Unicode strings. Double-byte character set (DBCS) strings require more processing. Also, new applications should use Uniscribe, which is described in the MSJ article in the "References" section.
BOOL ModifiedTextOut( HDC hDC, int nX, int nY, LPTSTR szString )
{
	int		i, nStringLength;
	BOOL	bResult;
	int		*pDx;

	// How long is the string - we need this a couple times below
	nStringLength = lstrlen( szString );

	// Allocate enough memory for the intercharacter spacing (ICS).array
	pDx = (int *)malloc( sizeof(int) * nStringLength );
	// Initialize the ICS array with "default" values
	for(i=0;i<nStringLength;i++)
	{
		ABC		abc;
		if( ! GetCharABCWidths( hDC, szString[i], szString[i], &abc ) )
		{
			free( pDx );
			return FALSE;
		}
		pDx[i] = abc.abcA + abc.abcB + abc.abcC;
	}

   // To demonstrate the modification of the ICS array, just add
   // one logical unit to the spacing for each character. This is where
   // you'd do things like implement justification, etc. by modifying
   // this array to cause the string to have the extent you desire.
        for(i=0;i<nStringLength;i++)
	{
		pDx[i] += 1;
	}
	// ExtTextOut() draws our text with our ICS array.
	bResult = ExtTextOut( hDC, nX, nY, ETO_OPAQUE, NULL, szString, nStringLength, pDx );
	// clean up.
	free( pDx );
	return bResult;
}
				

REFERENCES

"Developing International Software for Windows 95 and Windows NT" by Nadine Kano

"Supporting Multilanguage Text Layout and Complex Scripts with Windows NT 5.0," MSJ, November 1998

Modification Type:MinorLast Reviewed:5/10/2006
Keywords:kbDSWGDI2003Swept kbFont kbGDI kbhowto KB263536