基本上查到了两种方法:一种是用creatfont,然后TextOut画出来再用GetPixel截取,这个办法比较笨拙另一种就是用GetGlyphOutline()函数,想用这种。但是对其具体的实现不太明白,哪位大虾能给我清楚地讲解一下么,多谢多谢

解决方案 »

  1.   

    You can use the GetGlyphOutline function to retrieve the outline of a glyph from a TrueType font. The glyph outline returned by the GetGlyphOutline function is for a grid-fitted glyph. (A grid-fitted glyph has been modified so that its bitmap image conforms as closely as possible to the original design of the glyph.) If your application requires an unmodified glyph outline, request the glyph outline for a character in a font whose size is equal to the font's em units. (To create a font with this size, set the lfHeight member of the LOGFONT structure to the negative of the value of the ntmSizeEM member of the NEWTEXTMETRIC structure.) GetGlyphOutline returns the outline as a bitmap or as a series of polylines and splines. When an application retrieves a glyph outline as a series of polylines and splines, the information is returned in a TTPOLYGONHEADER structure followed by as many TTPOLYCURVE structures as required to describe the glyph. All points are returned as POINTFX structures and represent absolute positions, not relative moves. The starting point specified by the pfxStart member of the TTPOLYGONHEADER structure is the point where the outline for a contour begins. The TTPOLYCURVE structures that follow can be either polyline records or spline records. To render a TrueType character outline, you must use both the polyline and the spline records. The system can render both polylines and splines easily. Each polyline and spline record contains as many sequential points as possible, to minimize the number of records returned. The starting point specified in the TTPOLYGONHEADER structure is always on the outline of the glyph. The specified point serves as both the starting and ending points for the contour. 
      

  2.   

    谢pioneer_public() ^^嗯,在网上还找到一段:
    **********我的开始符*************************************简单一些,我的程序现在只能预览一个汉字的不同字体的点阵表达。界面很简单: 一个输出点阵大小的选择列表(8x8,16x16,24x24等),一个系统中已有的字体名称列表,一个预览按钮,一块画图显示区域。得到字体列表的方法:(作者称这一段是用来取回系统的字体,然后添加到下拉框中)
       //取字体名称列表的回调函数,使用前要声明一下该方法
       int CALLBACK MyEnumFontProc(ENUMLOGFONTEX* lpelf,NEWTEXTMETRICEX* lpntm,DWORD nFontType,long lParam) 
       { 
            CFontPeekerDlg* pWnd=(CFontPeekerDlg*) lParam; 
            if(pWnd) 
            { 
          if( pWnd->m_combo_sfont.FindString(0, lpelf->elfLogFont.lfFaceName) <0 )
                 pWnd->m_combo_sfont.AddString(lpelf->elfLogFont.lfFaceName); 
                return 1; 
            } 
            return 0; 
       } 
       //说明:CFontPeekerDlg 是我的dialog的类名, m_combo_sfont是列表名称下拉combobox关联的control变量
       
       
       //调用的地方  (******问题1:下面那个&lf怎么得到呢)
       {
       ::EnumFontFamiliesEx((HDC) dc,&lf, (FONTENUMPROC)MyEnumFontProc,(LPARAM) this,0);    
       m_combo_sfont.SetCurSel(0);
       } 字体预览:
      如果点阵大小选择16,显示的时候就画出16x16个方格。自定义一个类CMyStatic继承自CStatic,用来画图。在CMyStatic的OnPaint()函数中计算并显示。
      
    取得字体:
      常用的方法:用CreateFont创建字体,把字TextOut再用GetPixel()取点存入数组。 缺点:必须把字TextOut出来,能在屏幕上看见,不爽。
      
      我的方法,用这个函数:GetGlyphOutline(),可以得到一个字的轮廓矢量或者位图。可以不用textout到屏幕,直接取得字模信息
      函数原型如下:
        DWORD GetGlyphOutline(
            HDC hdc,          //画图设备句柄
            UINT uChar,        //将要读取的字符/汉字
            UINT uFormat,      //返回数据的格式(字的外形轮廓还是字的位图)
           LPGLYPHMETRICS lpgm,  // GLYPHMETRICS结构地址,输出参数
            DWORD cbBuffer,   //输出数据缓冲区的大小
            LPVOID lpvBuffer,  //输出数据缓冲区的地址
            CONST MAT2 *lpmat2 //转置矩阵的地址
       );       说明:
        uChar字符需要判断是否是汉字还是英文字符。中文占2个字节长度。
      
        lpgm是输出函数,调用GetGlyphOutline()是无须给lpgm 赋值。
        
        lpmat2如果不需要转置,将 eM11.value=1; eM22.value=1; 即可。
        
        cbBuffer缓冲区的大小,可以先通过调用GetGlyphOutline(。lpgm, 0, NULL, mat); 来取得,然后动态分配lpvBuffer,再一次调用GetGlyphOutline,将信息存到lpvBuffer。 使用完毕后再释放lpvBuffer。
        
      程序示例:(***问题2:用这段程序,我获取的字符点阵总都是一样的,不管什么字)
         。前面部分省略。
        GLYPHMETRICS glyph;            
        MAT2 m2; 
        
        memset(&m2, 0, sizeof(MAT2));
        m2.eM11.value = 1;
        m2.eM22.value = 1;   
        
        //取得buffer的大小    
        DWORD cbBuf = dc.GetGlyphOutline( nChar, GGO_BITMAP, &glyph, 0L, NULL, &m2);
        
        BYTE* pBuf=NULL;
        
        //返回GDI_ERROR表示失败。
        if( cbBuf != GDI_ERROR )
        {
         pBuf = new BYTE[cbBuf];
         
         //输出位图GGO_BITMAP 的信息。输出信息4字节(DWORD)对齐
         dc.GetGlyphOutline( nChar, GGO_BITMAP, &glyph, cbBuf, pBuf, &m2);
        }
        else
        {
         if(m_pFont!=NULL) 
          delete m_pFont;
         return;
        }     编程中遇到问题:
          一开始,GetGlyphOutline总是返回-1,getLastError显示是“无法完成的功能”,后来发现是因为调用之前没有给hdc设置Font.
          
          后来能取得pBuf信息后,又开始郁闷,因为不太明白bitmap的结果是按什么排列的。后来跟踪汉字“一”来调试(这个字简单),注意到了glyph.gmBlackBoxX 其实就是输出位图的宽度,glyph.gmBlackBoxY就是高度。如果gmBlackBoxX=15,glyph.gmBlackBoxY=2,表示输出的pBuf中有这些信息:位图有2行信息,每一行使用15 bit来存储信息。
          
          例如:我读取“一”:glyph.gmBlackBoxX = 0x0e,glyph.gmBlackBoxY=0x2;  pBuf长度cbBuf=8 字节
                pBuf信息:   00 08 00 00 ff fc 00 00
                字符宽度 0x0e=14  则 第一行信息为:     0000 0000 0000 100   (只取到前14位)
                第二行根据4字节对齐的规则,从0xff开始    1111 1111 1111 110
                看出“一”字了吗?呵呵
                
          直到他的存储之后就可以动手解析输出的信息了。
          
          我定义了一个宏#define BIT(n)  (1<<(n))  用来比较每一个位信息时使用
          
          后来又遇到了一个问题,就是小头和大头的问题了。在我的机器上是little endian的形式,如果我用
                  unsigned long *lptr = (unsigned long*)pBuf;
                  //j from 0 to 15
                  if( *lptr & BIT(j) )
                { 
                  //这时候如果想用j来表示写1的位数,就错了          
                } 
                
                因为从字节数组中转化成unsigned long型的时候,数值已经经过转化了,像上例中,实际上是0x0800 在同BIT(j)比较.
                
                不多说了,比较之前转化一下就可以了if( htonl(*lptr) & BIT(j) )
                
          
     ********************我的结束语********************
    问题在上面,大虾们有时间帮看一下
      

  3.   

    取得字体:
      常用的方法:用CreateFont创建字体,把字TextOut再用GetPixel()取点存入数组。 缺点:必须把字TextOut出来,能在屏幕上看见,不爽。
    ===================================================================================
    什么意思呢?不想Out出来完全可以在内存中转换完成,并且,完全不必GetPixel()...