汉字笔画计算代码
通过提取楷体的字形轮廓可以获得汉字笔画。只不过选用的字体必须是楷体。其他字体不行。
这功能没什么用途吧,我只是好玩而已。
int GetCharStrokes(UINT ch)
{
HFONT hFont;
{ // create font;
LOGFONT lfFont;
memset(&lfFont, 0, sizeof(lfFont));
lstrcpy(lfFont.lfFaceName, "楷体_GB2312");
lfFont.lfHeight = 72;
lfFont.lfWeight = FW_NORMAL;
lfFont.lfItalic = FALSE;
lfFont.lfStrikeOut = FALSE;
lfFont.lfCharSet = DEFAULT_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 0;
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));
DWORD dwSize = GetGlyphOutline(hDC,ch,GGO_NATIVE,&gm,0,NULL,&mat);
int cnt = 0;
LPVOID pBuff = NULL;
if (dwSize>0)
{
pBuff = malloc(dwSize);
memset(&gm,0,sizeof(gm));
DWORD res = GetGlyphOutline(hDC,ch,GGO_NATIVE,&gm,dwSize,pBuff,&mat);
if (res!=dwSize)
{
free(pBuff);
pBuff = NULL;
}
}
if (pBuff)
{
BYTE * ptr = (BYTE *)pBuff;
while (dwSize>0)
{
TTPOLYGONHEADER * header = (TTPOLYGONHEADER *)ptr;
cnt ++;
ptr += header->cb;
dwSize -= header->cb;
}
free(pBuff);
}
SelectObject(hDC,hOldFont);
ReleaseDC(NULL,hDC);
return cnt;
}
测试:
union {
UINT ch;
char szText[4];
} a;
char * szText = "疆";
a.ch = 0;
a.szText[1] = szText[0];
a.szText[0] = szText[1];
int cnt = GetCharStrokes(a.ch);
TRACE("\"%s\"的笔画%d\n",szText,cnt);
运行结果:
"疆"的笔画19
这功能没什么用途吧,我只是好玩而已。
int GetCharStrokes(UINT ch)
{
HFONT hFont;
{ // create font;
LOGFONT lfFont;
memset(&lfFont, 0, sizeof(lfFont));
lstrcpy(lfFont.lfFaceName, "楷体_GB2312");
lfFont.lfHeight = 72;
lfFont.lfWeight = FW_NORMAL;
lfFont.lfItalic = FALSE;
lfFont.lfStrikeOut = FALSE;
lfFont.lfCharSet = DEFAULT_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 0;
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));
DWORD dwSize = GetGlyphOutline(hDC,ch,GGO_NATIVE,&gm,0,NULL,&mat);
int cnt = 0;
LPVOID pBuff = NULL;
if (dwSize>0)
{
pBuff = malloc(dwSize);
memset(&gm,0,sizeof(gm));
DWORD res = GetGlyphOutline(hDC,ch,GGO_NATIVE,&gm,dwSize,pBuff,&mat);
if (res!=dwSize)
{
free(pBuff);
pBuff = NULL;
}
}
if (pBuff)
{
BYTE * ptr = (BYTE *)pBuff;
while (dwSize>0)
{
TTPOLYGONHEADER * header = (TTPOLYGONHEADER *)ptr;
cnt ++;
ptr += header->cb;
dwSize -= header->cb;
}
free(pBuff);
}
SelectObject(hDC,hOldFont);
ReleaseDC(NULL,hDC);
return cnt;
}
测试:
union {
UINT ch;
char szText[4];
} a;
char * szText = "疆";
a.ch = 0;
a.szText[1] = szText[0];
a.szText[0] = szText[1];
int cnt = GetCharStrokes(a.ch);
TRACE("\"%s\"的笔画%d\n",szText,cnt);
运行结果:
"疆"的笔画19
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论