可怜的VTK5.2中居然还是不支持中文,提取字符点集的方法效率太低了。已经写了一个大类,支持各种字体,单行多行等,再也没有更好的办法了,比如修改VTK内核之类。
下面是一段显示中文字符的代码,抛砖引玉,希望有牛人能够从根上解决这个问题。
void CMIGVTKChineseChar::SingleGenerateData(TCHAR chText, int xBegin)
{
USES_CONVERSION;
HFONT hFont;
LOGFONT lfFont;
memset(&lfFont, 0, sizeof(lfFont));
lstrcpy(lfFont.lfFaceName, _T("宋体"));
lfFont.lfHeight = 72;
lfFont.lfWeight = FW_THIN;
lfFont.lfItalic = FALSE;
lfFont.lfStrikeOut = FALSE;
lfFont.lfCharSet = CHINESEBIG5_CHARSET;
lfFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
lfFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lfFont.lfQuality = DRAFT_QUALITY;
lfFont.lfPitchAndFamily = DEFAULT_PITCH;
hFont = CreateFontIndirect(&lfFont);
HDC hDC = GetDC(NULL);
if (hDC==NULL) return;
HGDIOBJ hOldFont = SelectObject(hDC,hFont);
MAT2 mat;
memset(&mat,0,sizeof(mat));
mat.eM11.value = 1;
mat.eM22.value = -1;
GLYPHMETRICS gm;
memset(&gm,0,sizeof(gm));
TEXTMETRIC tm;
GetTextMetrics(hDC, &tm);
int nFontHeight = tm.tmHeight;
int nFontWidth = tm.tmWeight;
int nExternal = tm.tmExternalLeading;
DWORD dwNeedSize = GetGlyphOutline(hDC,chText,GGO_BITMAP,&gm,0,NULL,&mat);
if (dwNeedSize > 0 && dwNeedSize < 0xFFFF)
{
LPBYTE lpBuf = (LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwNeedSize);
if (lpBuf)
{
GetGlyphOutline(hDC,chText,GGO_BITMAP,&gm,dwNeedSize,lpBuf,&mat);
int nByteCount = ((gm.gmBlackBoxX +31) >> 5) << 2;
for (int i = 0; i < gm.gmBlackBoxY; i++)
{
for (int j = 0; j < nByteCount; j++)
{
BYTE btCode = lpBuf[i* nByteCount + j];
for (int k = 0; k < 8; k++)
{
if (btCode & (0x80>>k))
{
m_chData->SetScalarComponentFromFloat( j*8+k + xBegin,i, 0,0,254);
}
else
{
m_chData->SetScalarComponentFromFloat( j*8+k + xBegin,i,0,0,0);
}
}
}
}
HeapFree(GetProcessHeap(),0,lpBuf);
}
}
SelectObject(hDC,hOldFont);
DeleteObject(hFont);
}