TrueType字体中的字符轮廓由直线和二次贝塞尔曲线片段所构成。Windows系统提供了API函数GetGlyphOutline()来获取字符的轮廓。函数通过字符Code确定一个字符在TTF字库中的结构,并取出字符Code所对应的数据。下面简单说明下函数的结构和字符轮廓获取的实现代码。
GetGlyphOutline()函数结构:
DWORD GetGlyphOutline(UINT nChar, //需要获取字符
UINT nFormat, //获取字符的格式
LPGLYPHMETRICS lpgm,//获取字符的相关信息
DWORD cbBuffer, //保存字符数据缓冲区的大小
LPVOID lpBuffer, //保存字符数据的缓冲区
const MAT2* lpmat2 //变换矩阵
)const;
使用GetGlyphOutline()函数获取字符轮廓实现:
HDC hDC = pDC->GetSafeHdc();
//创建字体
CFont font;
VERIFY(font.CreateFont(m_iFontHeight, 0, 0, 0,FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS,m_sFontFaceName));
CFont *pOldFont = pDC->SelectObject(&font);
//定义并初始化变换矩阵
MAT2 mat2;
memset(&mat2, 0, sizeof(mat2));
mat2.eM11 = 1;
mat2.eM22 = 1;
GLYPHMETRICS metrics; //保存字符相关信息
DWORD dwDataSize = 0; //初始化字符数据缓冲区大小
//……
//通过函数GetGlyphOutline()确定存储字符结构的空间
dwDataSize = pDC->GetGlyphOutline(nChar, GGO_NATIVE, &metrics, 0, NULL, &mat2);
if ((dwDataSize != 0) && (dwDataSize != GDI_ERROR))
{
//创建保存字符数据缓冲区大小
LPBYTE pPixels = new BYTE[dwDataSize];
ASSERT( pPixels != NULL );
TTPOLYGONHEADER *pHeader = (TTPOLYGONHEADER*)pPixels;
dwDataSize = pDC->GetGlyphOutline(nChar,GGO_NATIVE,&metrics, dwDataSize, pPixels, &mat2);
while(dwDataSize > 0)
{
//计算字符轮廓的起点,转换坐标
int xOld = MapFXY(pHeader->pfxStart.x);
int yOld = MapFXY(pHeader->pfxStart.y);
//根据TTF字体结构获取字符轮廓
::MoveToEx(hDC,iXpos + xOld,iYpos - yOld,NULL);
TTPOLYCURVE *pCurrentCurve = (TTPOLYCURVE*)(pHeader + 1);
int remainByte = pHeader->cb - sizeof(TTPOLYGONHEADER);
while (remainByte > 0)
{
CPoint lpPoint[1000];
CPoint bezi[2];
int index;
for (index = 0; index < pCurrentCurve->cpfx; ++index)
{
lpPoint[index].x = iXpos + MapFXY(pCurrentCurve->apfx[index].x);
lpPoint[index].y = iYpos - MapFXY(pCurrentCurve->apfx[index].y);
}
switch (pCurrentCurve->wType)
{
case TT_PRIM_LINE:
case TT_PRIM_QSPLINE:
for (index =0; index < pCurrentCurve->cpfx; index++)
{
::LineTo(hDC,lpPoint[index].x,lpPoint[index].y);
}
break;
default:
MessageBox(_T("字体不支持"));
break;
}
int count = sizeof(TTPOLYCURVE) + (pCurrentCurve->cpfx -1)*sizeof(POINTFX);
pCurrentCurve = (TTPOLYCURVE*)((char*)pCurrentCurve + count);
remainByte -= count;
}
::LineTo(hDC,iXpos + xOld, iYpos - yOld);
dwDataSize -= pHeader->cb;
pHeader = (TTPOLYGONHEADER*)((char*)pHeader + pHeader->cb);
}
delete [] pPixels;
}
}