GetGlyphOutline如何获取汉字的点阵数据?

fulltimeangel 2012-04-29 11:18:35
最近的工作要用到windows api取汉字的点阵数据,我查阅了一些资料,能去除英文字母的点阵数据,但无法正确地取出汉字的点阵数据。

核心代码段如下:

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
//hdc=GetDC(hwnd);
//hFont=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
hFont = EzCreateFont (hdc, TEXT ("楷体_GB2312"), 1440, 0, 0, TRUE) ;
oldFont=(HFONT)SelectObject(hdc,hFont);

//hFont=GetFont();
//oldFont=(HFONT)SelectObject(hdc,hfont);

MAT2 mat2;
mat2.eM11 = FixedFromDouble(2);
mat2.eM12 = FixedFromDouble(0);
mat2.eM21 = FixedFromDouble(0);
mat2.eM22 = FixedFromDouble(2);

GLYPHMETRICS gm;
//chText=L'W';
//chText =L'A';
chText=L'泰';
//ch=str[0] < <8|(str[1]&0xff);






dwBufSize=GetGlyphOutline(hdc,chText,GGO_BITMAP,&gm,0,NULL,&mat2);
lpszFunction="GetGlyphOutline";

//dw = GetLastError();
//if(dw!=0)
//{
// sprintf(szBuf, "%s failed: GetLastError returned %u\n",
// lpszFunction, dw);
//
// MessageBox(NULL, szBuf, "Error", MB_OK);
// ExitProcess(dw);
// }
if(dwBufSize!=GDI_ERROR)
{
if(dwBufSize>0 && dwBufSize<0xFFFF)
{
//char* lpBuf=NULL;
//lpBuf=(char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufSize);
//板卡控制
// rtn=GT_Open(0);
//rtn=GT_Reset();
// rtn=GT_ScanCrdClear(0);


LPBYTE lpBuf=(LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufSize);


if(lpBuf)
{

GetGlyphOutline(hdc,chText,GGO_BITMAP,&gm,dwBufSize,lpBuf,&mat2);
...


我个人认为在代码:
chText=L'泰';//此处还应该针对汉字做进一步处理

dwBufSize=GetGlyphOutline(hdc,chText,GGO_BITMAP,&gm,0,NULL,&mat2);//此处实际调用的是GetGlyphOutlineW
...全文
851 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fulltimeangel 2012-05-03
  • 打赏
  • 举报
回复
感谢10楼的回复,问题总算解决了。
schlafenhamster 2012-04-29
  • 打赏
  • 举报
回复

//#define UPSIDE_DOWN
void CTtfShowDlg::DrawContour(CDC *pDC,int X, int Y,char* theChar)
{
GLYPHMETRICS gm;
MAT2 mat;
/*
MAT2 mat= {{ 0, 1, },
{ 0, 0, },
{ 0, 0, },
{ 0, 1, }};
*/
mat.eM11=Double2Fixed(1);
mat.eM12=Double2Fixed(0);
mat.eM21=Double2Fixed(0);
#ifdef UPSIDE_DOWN
mat.eM22=Double2Fixed(1);
#else // normal
mat.eM22=Double2Fixed(-1);
#endif
POINT Origin={X,Y};
WCHAR szText[40];
// Copy the string
MultiByteToWideChar(CP_ACP, 0, theChar, -1, (LPWSTR) szText, 40);
// actually only 1 char.
for(UINT i=0;szText[i];i++)
{/* Returns the curve data points in the rasterizer’s native format, using device units. */
DWORD BufferLen=::GetGlyphOutlineW(pDC->m_hDC,szText[i],GGO_NATIVE,&gm,0,0,&mat);
if(BufferLen != GDI_ERROR) // 0xFFFFFFFFL
{//OK
BYTE *pBuffer=new BYTE[BufferLen];
DWORD Result=::GetGlyphOutlineW(pDC->m_hDC,szText[i],GGO_NATIVE,&gm,BufferLen,pBuffer,&mat);
// OutLine
DrawCurve(pDC,pBuffer,BufferLen,gm,Origin);
// if save
if(m_bSaveToFile)
{
m_bSaveToFile=FALSE;
// + size theApp.m_Ascent
char Ascent[10];
sprintf(Ascent,"%d",theApp.m_Ascent);
strcat(theChar,Ascent);
SaveGlyphInfo(theChar,pBuffer,BufferLen);
}
Origin.x += gm.gmCellIncX;
Origin.y += gm.gmCellIncY;
delete[] pBuffer;
}
}
}

fulltimeangel 2012-04-29
  • 打赏
  • 举报
回复
大家帮忙解决一下吧,这问题很困扰我。
kaizi65535 2012-04-29
  • 打赏
  • 举报
回复
加入预编译指令:
#define NUM 1000
#ifdef UNICODE
#define GetGlyphOutline GetGlyphOutlineW
#else
#define GetGlyphOutline GetGlyphOutlineA
#endif

然后从“工程”-“C\C++”-“预处理程序定义”加入“UNICODE”即可
schlafenhamster 2012-04-29
  • 打赏
  • 举报
回复
//这是“用GetGlyphOutline取字体点阵”内容:
void CtestdlgDlg::OnBnClickedButton6()

{

CDC * pDC;

CString cStr;

CHAR c;

GLYPHMETRICS GpMtic;

BYTE * pBuffer;

DWORD dwBufSize;

MAT2 mat;

CFont font;

CFont * pOldFont;

LOGFONT lf;

DWORD dwRetVal;

int x, y;

BOOL bBit;



c = 'X';

cStr = c;

pDC = this->GetWindowDC();



memset(&lf,0,sizeof(LOGFONT));



lf.lfHeight=180;

lstrcpy(lf.lfFaceName,_T("Times New Roman"));

font.CreateFontIndirect(&lf);



pOldFont = pDC->SelectObject(&font);

pDC->TextOut(0, 0, cStr);



NSys::IdleSleep(200);



memset(&GpMtic, 0, sizeof(GpMtic));

memset(&mat, 0, sizeof(mat));

mat.eM11.value = 1;

mat.eM22.value = 1;



pBuffer = NULL;

dwBufSize = 0;

dwRetVal = pDC->GetGlyphOutline(c, GGO_BITMAP, &GpMtic, dwBufSize, pBuffer, &mat);

if(dwRetVal == -1)

{

NMsg::MsgErr();

goto Done;

}



dwBufSize = dwRetVal;

pBuffer = new BYTE[dwBufSize];

memset(pBuffer, 0, dwBufSize);

dwRetVal = pDC->GetGlyphOutline(c, GGO_BITMAP, &GpMtic, dwBufSize, pBuffer, &mat);

if(dwRetVal == -1)

{

delete [] pBuffer;

goto Done;

}



int nWidth, nHeight;

BYTE *pData;

DWORD dwOneLine;

int dx, dy;



nWidth = GpMtic.gmBlackBoxX;

nHeight = GpMtic.gmBlackBoxY;





TEXTMETRIC TxtMtic;

pDC->GetTextMetrics(&TxtMtic);



dx = GpMtic.gmptGlyphOrigin.x;

dy = TxtMtic.tmAscent - GpMtic.gmptGlyphOrigin.y;



dwOneLine = (nWidth/8 + 3) / 4 * 4;

for(y=0; y<nHeight; y++)

{

for(x=0; x<nWidth; x++)

{

pData = pBuffer + dwOneLine*y + x/8;

bBit = *pData & (1 << 7-(x%8));



if(bBit)

pDC->SetPixel(x+dx, y+dy, RGB(255, 0, 0));

// else

// pDC->SetPixel(x+dx, y+dy, RGB(0, 255, 0));



}

}



delete [] pBuffer;

Done:

pDC->SelectObject(pOldFont);

this->ReleaseDC(pDC);

}

fulltimeangel 2012-04-29
  • 打赏
  • 举报
回复
请问谁知道chText=L'泰';之后应该再做何种处理?
schlafenhamster 2012-04-29
  • 打赏
  • 举报
回复
那就看看“用GetGlyphOutline取字体点阵 - zzz3265的专栏 - CSDN博客zzz3265的专栏”
kaizi65535 2012-04-29
  • 打赏
  • 举报
回复
可以考虑使用逻辑字体,并指定编码格式
fulltimeangel 2012-04-29
  • 打赏
  • 举报
回复
感谢2楼的回复,我仍然未能理解。

19,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧