关闭

FreeType 调试

849人阅读 评论(0) 收藏 举报
分类:

不良习惯害死人啊!


下面代码对吗?

unsigned char maskData = glyphData.pGlyphData[j * glyphData.pitch + k >> 3];


随手写的,却坑了自己2小时。反复看了好几次文档,也没搞明白哪错了。

正确写法:

unsigned char maskData = glyphData.pGlyphData[j * glyphData.pitch + (k >> 3)];  // + 比 >> 优先级高


附上自己封装的FreeType给大家参考:

#include <ft2build.h> 
#include FT_FREETYPE_H 
#include <windows.h>
#include <tchar.h>

typedef struct stGFX2D_Font_Impl {
GFX2D_UB8    width;
GFX2D_UB8    height;
GFX2D_UB8    weight;
GFX2D_ENUM   face;
GFX2D_ENUM   charSet;
FT_Face      pFTFace; 
PFONTCALL    fontcall;
PCHARSETOPS  charsetops;
} GFX2D_Font_Impl, *GFX2D_Font_Impl_Handle;


static FT_Library   s_pFTLib;
const char fontpath[] = "c:\\WINDOWS\\fonts\\msmincho.ttc";


GFX2D_STAT FontLib_FreeType_Init ()
{
GFX2D_STAT rtn = GFX2D_SUCCESS;
FT_Init_FreeType(&s_pFTLib);
return rtn;
}


GFX2D_STAT FontLib_FreeType_Exit ()
{
GFX2D_STAT rtn = GFX2D_SUCCESS;
FT_Done_FreeType(s_pFTLib);
s_pFTLib = NULL;
return rtn;
}


GFX2D_STAT FontLib_FreeType_Font_Create(
GFX2D_FONT_INFO* info,
GFX2D_Font_Handle *gf
)
{
GFX2D_STAT rtn = GFX2D_SUCCESS;
GFX2D_Font_Impl_Handle pf = (GFX2D_Font_Impl_Handle)MALLOC(sizeof(GFX2D_Font_Impl));
memset(pf, 0, sizeof(GFX2D_Font_Impl));
pf->face        = info->face;
pf->height      = info->height;
pf->charSet     = info->charSet;
pf->weight      = info->weight;
pf->width       = info->width;


FT_New_Face(s_pFTLib, "C:\\WINDOWS\\Fonts\\times.ttf", 0, &(pf->pFTFace));
FT_Select_Charmap(pf->pFTFace,FT_ENCODING_UNICODE);
FT_Set_Pixel_Sizes(pf->pFTFace, info->width, info->height);


*gf = pf;
return rtn;
}


GFX2D_STAT FontLib_FreeType_Font_Destroy(
GFX2D_Font_Handle gf
)
{
GFX2D_STAT rtn = GFX2D_SUCCESS;
GFX2D_Font_Impl_Handle pf = (GFX2D_Font_Impl_Handle)gf;
FT_Done_Face(pf->pFTFace);
FREE(pf);
return rtn;
}

typedef struct stGFX2D_Font_GlypData {
char *pGlyphData;
int pitch;
int width;
int height;
int left;
int top;
int advanceX;
} GFX2D_Font_GlypData;


GFX2D_STAT FontLib_FreeType_Font_GetGlpy(
GFX2D_Font_Handle gf,
void* str,
GFX2D_Font_GlypData *pGlyphData
)
{
GFX2D_STAT rtn = GFX2D_SUCCESS;
GFX2D_Font_Impl_Handle pf = (GFX2D_Font_Impl_Handle)gf;
FT_Face      pFTFace = pf->pFTFace;
int bError;
TCHAR newChar = *(TCHAR*)str;


bError = FT_Load_Char( pFTFace, newChar, FT_LOAD_MONOCHROME);
assert(bError == 0);
bError = FT_Render_Glyph( pFTFace->glyph, FT_RENDER_MODE_MONO);
assert(bError == 0);
pGlyphData->pGlyphData = pFTFace->glyph->bitmap.buffer;
pGlyphData->pitch = pFTFace->glyph->bitmap.pitch;
pGlyphData->width = pFTFace->glyph->bitmap.width;
pGlyphData->height = pFTFace->glyph->bitmap.rows;
pGlyphData->advanceX = pFTFace->glyph->advance.x/64;
pGlyphData->left = pFTFace->glyph->bitmap_left;
pGlyphData->top = pFTFace->glyph->bitmap_top;
return rtn;
}


描画函数:(HBitmap 32位)

typedef struct stSurfaceInfoImpl {
unsigned int  w;
unsigned int  h;
GFX2D_COLOR_SYSTEM fmt;
int bitCount;
HDC hDC;
BITMAPINFOHEADER bitmapInfoHeader;
HBITMAP hBmp;
void* pPixelBuf;
int lpitch;
GFX2D_Font_Handle gf;
} SurfaceInfoImpl, *SurfaceHandleImpl;

{

SurfaceHandleImpl handle = (SurfaceHandleImpl)pGFX2D_t;


TCHAR *pChar = (TCHAR *)str;
int i, j, k;
GFX2D_Font_GlypData glyphData;
GFX2D_Font_Handle gf = handle->gf;
int startX, startY;


startX = pos.x;
startY = pos.y;
for (i = 0; i < lenth; i ++) {
int drawGlyphX,drawGlyphY;
FontLib_FreeType_Font_GetGlpy(gf, &(pChar[i]), &glyphData);
drawGlyphX = startX + glyphData.left;
drawGlyphY = startY;
for (j = 0; j < glyphData.height; j++)
{
unsigned char *pDrawStart = (unsigned char *)handle->pPixelBuf + drawGlyphY * handle->lpitch + drawGlyphX * 4;
for(k = 0; k < glyphData.width; k++ )
{
unsigned char maskData = glyphData.pGlyphData[j * glyphData.pitch + (k >> 3)];
int bitIndex = k % 8;

if ((maskData<< bitIndex) & 0x80) {
*pDrawStart++ = 0xFF;
*pDrawStart++ = 0xFF;
*pDrawStart++ = 0xFF;
*pDrawStart++ = 0xFF;
} else {
*pDrawStart++ = 0;
*pDrawStart++ = 0;
*pDrawStart++ = 0;
*pDrawStart++ = 0;
}
}
drawGlyphY++;
}
startX += glyphData.advanceX;
}

}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:133448次
    • 积分:2510
    • 等级:
    • 排名:第14715名
    • 原创:121篇
    • 转载:1篇
    • 译文:0篇
    • 评论:59条
    博客专栏
    最新评论