字体信息 TEXTMETRIC 字体结构 GLYPHMETRICS

tmHeight指字符高度(不包括两行字符之间的间距),tmAscent表示字符基线以上部分的高度,tmDescent表示字符基线以下部分的高度。
tmInternalLeading表示字符内预留的间距包含在tmAscent中(主要用于显示重音符号等)。
tmExternalLeading标准两行字符之间的间距,
tmAveCharWidth表示(小写 x)字符的加权平均宽度,
tmMaxCharWidth表示字符的最大宽宽度。

大写字符的平均宽度通常是字符平均宽度的1.5倍。
大写字母平均宽度 = tmMaxCharWith * 1.5 = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 

tmPitchFamily的低位表示字符是变宽字符还是等宽字符。1表示变宽字符,0表示等宽字符。

字符的横向大小由2个值确定:

① tmAveCharWidth,小写字母加权平均宽度。

② tmMaxCharWidth,字体中最宽字符的宽度。

对于等宽字体,tmAveCharWidth和tmMaxCharWidth这两个值相等。

大写字母的平均宽度比较复杂,如果:

① 字体是等宽字体,那么大写字母的平均宽度等于tmAveCharWidth。

② 字体是变宽字体,那么大写字母的平均宽度等于tmAveCharWidth*1.5。

判断字体是否是变宽字体,可以通过TEXTMETRIC结构中的tmPitchAndFamily域的低位判断,如果低位是1,那么是变宽字体,如果是0,那么是等宽字体。

 

  TTextMetric = record
    tmHeight : Longint; { the height of a character }
    tmAscent : Longint; { the ascent of a character }
    tmDescent : Longint; { the descent of a character }
    tmInternalLeading : Longint; { the internal leading }
    tmExternalLeading : Longint; { the external leading }
    tmAveCharWidth : Longint; { the average character width }
    tmMaxCharWidth : Longint; { the maximum character width }
    tmWeight : Longint; { the boldness value }
    tmOverhang : Longint; { the overhang width }
    tmDigitizedAspectX : Longint; { the horizontal aspect }
    tmDigitizedAspectY : Longint; { the vertical aspect }
    tmFirstChar : WORD; { the first character }
    tmLastChar : WORD; { the last character }
    tmDefaultChar : WORD; { the default character }
    tmBreakChar : WORD; { the word break character }
    tmItalic : Byte; { the italics flag }
    tmUnderlined : Byte; { the underlined flag }
    tmStruckOut : Byte; { the strikeout flag }
    tmPitchAndFamily : Byte; { the pitch and family flags }
    tmCharSet : Byte; { the character set }
  end;

GetTextMetrics

 
函数功能:
该函数把程序当前的字体信息,存放到TEXTMETRIC(即MSDN原文中的specified buffer)中。
函数原型:
BOOL GetTextMetrics(HDC hdc, LPTEXTMETRIC lptm);
参数:
hdc:设备环境句柄。
lptm:指向结构TEXTMETRIC的指针,该结构用于获得字体信息。
返回值:
如果函数调用成功,返回值非零,如果函数调用失败,返回值是0。
 

 

  TGlyphMetrics = packed record
    gmBlackBoxX : UINT; { the smallest rectangle width }
    gmBlackBoxY : UINT; { the smallest rectangle height }
    gmptGlyphOrigin : TPoint; { the smallest rectangle origin }
    gmCellIncX : SHORT; { the next character cell horizontal offset }
    gmCellIncY : SHORT; { the next character cell vertical offset }
  end;

GetGlyphOutline

 
函数功能:该函数取得被选进指定设备环境的TrueType字体的字符轮廓或位图。
函数原型:DWORD GetGlyphOutline(HDC hdc, UINT uChar, UINT uFormat, LPGLYPHMETRICS lpgm, DWORD cbBuffer, LPVOID lpvBuffer, CONST MAT2 *lpmat2);
参数:
hdc:设备环境句柄。
uChar:指定被返回其数据的字符。
uFormat:指定函数取得的数据的格式。可用下列值之一:各值含义为:
GGO_METRICS:函数只获得由lpgm指定的GLYPHMETRICS结构。其余缓冲区被忽徊,此值影响函数失败时返回值的含义,参见后面的返回值部分。 
GGO_BITMAP:函数获得字形位图。要得到善于内存分配的信息,参见后面备注部分。
GGO_NATIVE:函数获得光栅器(rasterrizer)的本地格式的曲线数据点,并使用字体的设计单位,当指定了此值,由lpMatrix指定的任何变换都被忽略。
GGO_GRAY2_BITMAP:函数获得含5级灰色的字形位图。
GGO_GRAY4_BITMAP:函数获得含17级灰色的字形位图。
GGO_GRAY8_BITMAP:函数获得含65级灰色的字形位图。
注意:对GGO_GRAYnBITMAP值,函数获得n*n+1级灰色的字形位图。
lpgm:指向结构GLYPHMETRICS的指针,用于描述字表在字符单元的放置。
cbBuffer:指向定缓冲区的大小,该缓冲区用于复制轮廓字符的信息。如果此值为零,函数返回需要的缓冲区大小。
lpvBuffer:指向缓冲区的指针,该缓冲区用于复制轮廓字符的信息,如果此值为NULL,函数返回需要的缓冲区大小。
lpmat2:指向MAT2结构的指针,该结构为字符信息转换矩阵。该函数不能为NULL,即使nFormat已被设置为GGO_NATIVE
返回值:如果指定了GGO_BITMAP, GGO_GRAY2_BITMAP, GGO_GRAY4_BITMAP, GGO_GRAY8_BITMAP,或GGO_NATIVE值且函数调用成功,返回值将大于0,
否则,返回值是GDI_ERROR。如果指定了上述之一值,但缓冲区或地址是0,则返回需要的缓冲区的字节数。
如果GGO_METRICS被指定且函数调用失败,返回值是GDI_ERROR。
Windows NT:若想获得更多错误信息,请调用GetLastError函数。
备注:GetGlyphOutline返回的字形轮廓是一种配有网络的字形(配有网络的字形是指一种被修改的字形,使其点阵图尽可能地与字形的原始设计一致)。
如果一个应用程序需要一种无修改的字形轮廓,应该在那些大小等于字体的em单位的字体中要求一个字符的字形轮廓,
字体的em单位值存在于结构OUTLINETEXTMETRIC的otmEMSquare成员中。
由GetGlyphOutline指定GGO_BITMAP返回的字形位图是一种双字对齐、面向行的单色位图,
当指定GGO_GRAY2_BITMAP,返回的位图是一种双字对齐、面向行的,其值在0-4之间的字节数组。
当GGO_GRAY4_BITMAP指定时,返回的位图是一种双字对齐、面向行的,其值在0-16之间的字节数组。
当指定GGO_GRAY8_BITMAP时,返回的位图是一种双字对齐,面向行的,其值在0-64之间的字节数组。
应用程序可以指定lpMatrix参数里的2-对-2转换矩阵来将以位图格式获得的字符旋转。
// Convert degrees to radians.
//............................
#define RAD(x) ((x) * 3.1415927 / 180)

FIXED FixedFromDouble( double d )
{
  long l;

  l = (long) ( d * 65536L );
  return *(FIXED *) &l;
}

HBITMAP BitmapFromBits( void * lpBits, WORD width, WORD height )
{
  BITMAP bm;

  bm.bmType = 0;
  bm.bmWidth = width;
  bm.bmHeight = height;
  bm.bmWidthBytes = ( ( width + 31 ) >> 5 ) << 2;
  bm.bmPlanes = 1;
  bm.bmBitsPixel = 1;
  bm.bmBits = lpBits;

  return ( CreateBitmapIndirect( &bm ) );
}

void GetGlyphOutlineExample( void )
{
  HDC hDC, hMemDC;
  HFONT hFont, hOldFont;
  GLYPHMETRICS gm;
  MAT2 m2;
  DWORD dwRet;
  LPBYTE lpBuf;
  HBITMAP hBitmap, hOldBmp;

  // Create a Times New Roman font.
  hFont = CreateFont( 32, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, 
    OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, 
    DEFAULT_PITCH | FF_DONTCARE, “Times New Roman” );

  // Retrieve the device context, select the font into it,
  // and create a compatible device context.
  hDC = GetDC( hWnd );
  hMemDC = CreateCompatibleDC( hDC );
  hOldFont = SelectObject( hDC, hFont );

  // Set up the translation matrix to rotate the font 45 degrees.
  m2.eM11 = FixedFromDouble( cos( RAD(45) ) );
  m2.eM12 = FixedFromDouble( sin( RAD(45) ) );
  m2.eM21 = FixedFromDouble( -sin( RAD(45) ) );
  m2.eM22 = FixedFromDouble( cos( RAD(45) ) );

  // Retrieve the size of the bitmap and allocate memory to hold it.
  dwRet = GetGlyphOutline( hDC, 'A’, GGO_BITMAP, & gm, 0, NULL, &m2 );

  lpBuf = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwRet );
  // Retrieve the bitmap for the letter 'A’.
  GetGlyphOutline( hDC, 'A’, GGO_BITMAP, & gm, dwRet, lpBuf, &m2 );

  // Create a HBITMAP and display it.
  hBitmap = BitmapFromBits( lpBuf, (WORD) gm.gmBlackBoxX, (WORD) gm.gmBlackBoxY );

  hOldBmp = SelectObject( hMemDC, hBitmap );

  BitBlt( hDC, 10, 10, gm.gmBlackBoxX, gm.gmBlackBoxY, hMemDC, 0, 0, SRCCOPY );

  // Output a normal 'A’ character.
  TextOut( hDC, 40, 10, “A”, 1 );

  // Clean up.
  SelectObject( hMemDC, hOldBmp );
  DeleteObject( hBitmap );
  HeapFree( GetProcessHeap( ), 0, lpBuf );
  SelectObject( hDC, hOldFont );
  ReleaseDC( hWnd, hDC );
  DeleteObject( hFont );
}

原文地址:https://www.cnblogs.com/shangdawei/p/3066762.html