大熊的口袋

 

ttf字符索引到字符的转换

bool GetCharByGlyphIndexes(const LPCWSTR fontName, const WORD* lpGlyphs, UINT count, BSTR* ppChars)
{
    HDC hdcScreen 
= CreateDC("DISPLAY", NULL, NULL, NULL); 
    HDC hdcCompatible 
= CreateCompatibleDC(hdcScreen);
    KLogFont logFont(fontName);
    HFONT hFont 
= logFont.CreateHFONT(); 
    HFONT hOldFont 
= (HFONT)::SelectObject(hdcCompatible, hFont);

    DWORD fsize;
    LPGLYPHSET lgp;

    fsize 
= GetFontUnicodeRanges(hdcCompatible, NULL);
    lgp 
= (LPGLYPHSET)malloc(fsize);
    GetFontUnicodeRanges(hdcCompatible,lgp);
    std::vector
<wchar_t> vecGlyphs(count, 0);
    
for(int i = 0; i < lgp->cRanges; i++)
    
{
        
for(int j = 0; j < lgp->ranges[i].cGlyphs; j++)
        
{
            wchar_t ch 
= j + lgp->ranges[i].wcLow;//这就是包含的字符
            WORD gp = 0;
            GetGlyphIndicesW(hdcCompatible, 
&ch, 1&gp, 1);
            
for (int k = 0; k < count; ++k)
            
{
                
if (gp == lpGlyphs[k])
                
{
                    vecGlyphs[k] 
= ch;
                    
break;
                }

            }

        }

    }

    free(lgp);
    hFont 
= (HFONT)::SelectObject(hdcCompatible, hOldFont);
    ::DeleteObject(hFont);
    ::DeleteDC(hdcCompatible);
    ::DeleteDC(hdcScreen);

    
for (int i = 0; i < count; ++i)
    
{
        
if (vecGlyphs[i] == 0)
        
{
            
return false;
        }

    }


    
*ppChars = SysAllocStringLen(&vecGlyphs[0], count);
    
return true;
}

posted on 2009-11-04 19:46 大熊的口袋 阅读(767) 评论(1)  编辑 收藏 引用 所属分类: win32

评论

# re: ttf字符索引到字符的转换[未登录] 2009-11-04 22:01 kk

有使用的例子吗  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

公告

常用链接

留言簿(2)

随笔分类

随笔档案

win32 & debug

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜