Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2647347
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-01-16 21:52:22

freetype2结合bitmap中文显示
 
1. 注意pitch,是显示客户区域的长度,并安4bit对齐
2。单个字向客户显示区域bitblt
3. bitmap.buffer由1byte转换成rgb,0时则跳过
4。由于freetype形成的字体轮廓问题,有的字体当大小不适合时显示中文出现异常
 
//***********************************************************/
#include "stdafx.h"
#include "freetype.h"
#include "commdlg.h"
#include
#include "gl\glu.h"
#include "glut.h"
#include
#include
#include
#include
#include
#pragma comment(lib , "lib/freetype2110.lib")
//#pragma comment(lib , "lib/glut32.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name
int g_nRowsize=0; //pixes per row
// Forward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void drawText(HDC hdc, wchar_t* _strText,int x , int y, int maxW , int h);
LPWSTR AnsiToUnicode(LPCSTR lpcstr) ;
HBITMAP createDIBitmapCommon(wchar_t ch, HDC hdc);
/*****************************************************************/
struct xCharTexture
{
 GLuint  m_texID;
 wchar_t m_chaID;
 int     m_Width;
 int     m_Height;
 int     m_adv_x;
 int     m_adv_y;
 int     m_delta_x;
 int     m_delta_y;
 GLubyte* m_pbuf;
public:
 xCharTexture()
 {
  m_texID  = 0;
  m_chaID  = 0;
  m_Width  = 0;
  m_Height = 0;
 }
}g_TexID[65536];
class xFreeTypeLib
{
 FT_Library m_FT2Lib;
 FT_Face    m_FT_Face;
public:
 int   m_w;
 int   m_h;
 void load(const char* font_file , int _w , int _h);
 GLuint loadChar(wchar_t ch);
 GLuint Paint(HDC hdc, wchar_t ch);
 BOOL xFreeTypeLib::fontPaint(HDC hdc);
};
xFreeTypeLib g_FreeTypeLib;
xCharTexture* getTextChar(wchar_t ch);
wchar_t g_UnicodeString[]=L"AAbb\x4E2D\x6587\x0031\x0032\x0033"; 
const char g_AnsiString[]=
"aaa VB文件格式:\n\
若不明确就标为未知\n\
表演者: 若不明确就标为未知\n\
专辑:  若不明确就标为未知\n\
持续时间:01:01:00超过1小时;\n\
09:09不足小时;00:09不足1分钟\n\
glBindTexture(GL_TEXTURE_2D,pCharTex->m_texID);\n\
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );\n\
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );\n\
glEnable(GL_BLEND);\n\
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)" ;
/******************************************************/
int APIENTRY _tWinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPTSTR    lpCmdLine,
        int       nCmdShow)
{
 // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;
 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_FREETYPE, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);
 g_FreeTypeLib.load("c:\\windows\\fonts\\simhei.ttf",0,24);
 //g_FreeTypeLib.load("c:\\windows\\fonts\\simsun.ttc",0,18);
 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }
 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FREETYPE);
 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }
 return (int) msg.wParam;
}
//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;
 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_FREETYPE);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCTSTR)IDC_FREETYPE;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
 return RegisterClassEx(&wcex);
}
//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 HWND hWnd;
 hInst = hInstance; // Store instance handle in our global variable
 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, 0, 300, 240, NULL, NULL, hInstance, NULL);
 if (!hWnd)
 {
  return FALSE;
 }
 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);
 return TRUE;
}
//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;
 switch (message)
 {
 case WM_COMMAND:
  wmId    = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  // Parse the menu selections:
  switch (wmId)
  {
  case IDM_ABOUT:
   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
   break;
  case IDM_EXIT:
   DestroyWindow(hWnd);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;
 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);
  // TODO: Add any drawing code here...
  wchar_t *wstr;
  //wstr = AnsiToUnicode(g_AnsiString);
  wstr = g_UnicodeString;
  if (g_nRowsize==0)
  {
   int nRowPad = 4;
   RECT r;
   GetClientRect(hWnd, &r);
   g_nRowsize = r.right-r.left;
   g_nRowsize += nRowPad - 1;
   g_nRowsize -= g_nRowsize % nRowPad;
  }
  
  drawText(hdc, wstr, 50, 50, 300,25);
  EndPaint(hWnd, &ps);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}
// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
 case WM_INITDIALOG:
  return TRUE;
 case WM_COMMAND:
  if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  {
   EndDialog(hDlg, LOWORD(wParam));
   return TRUE;
  }
  break;
 }
 return FALSE;
}
/******************************************/
void xFreeTypeLib::load(const char* font_file , int _w , int _h)
{
 FT_Library library;
 if (FT_Init_FreeType( &library) )
  exit(0);
 //加载一个字体,取默认的Face,一般为Regualer
 if (FT_New_Face( library, font_file, 0, &m_FT_Face ))
  exit(0);
 FT_Select_Charmap(m_FT_Face, FT_ENCODING_UNICODE);
 m_w = _w ; m_h = _h;
 m_FT_Face->num_fixed_sizes;
 //大小要乘64.这是规定。照做就可以了。
 //FT_Set_Char_Size( m_FT_Face , 64 << 1 ,  64 << 1 ,  1200 ,  1200 );
 FT_Set_Pixel_Sizes(m_FT_Face,m_w, m_h);

}
GLuint xFreeTypeLib::loadChar(wchar_t ch)
{
 if(g_TexID[ch].m_texID)
  return g_TexID[ch].m_texID;
 if(FT_Load_Char(m_FT_Face, ch, /*FT_LOAD_RENDER|*/FT_LOAD_FORCE_AUTOHINT|
  (TRUE ? FT_LOAD_TARGET_NORMAL : FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO) )   )
 {
  return 0;
 }
 /*if(FT_Load_Glyph( m_FT_Face, FT_Get_Char_Index( m_FT_Face, ch ), FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT|
 (TRUE ? FT_LOAD_TARGET_NORMAL : FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO) ))
 throw std::runtime_error("FT_Load_Glyph failed");*/
 xCharTexture& charTex = g_TexID[ch];
 //得到字模
 FT_Glyph glyph;
 if(FT_Get_Glyph( m_FT_Face->glyph, &glyph ))
  return 0;
 //转化成位图
 BOOL aa=FALSE;
 FT_Render_Glyph( m_FT_Face->glyph,   aa ? ft_render_mode_normal : ft_render_mode_mono );//FT_RENDER_MODE_NORMAL  );
 FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
 FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
 //取道位图数据
 FT_Bitmap& bitmap=bitmap_glyph->bitmap;
 bitmap.pixel_mode =FT_PIXEL_MODE_MONO;
 glGenTextures(1,&charTex.m_texID);
 //把位图数据拷贝自己定义的数据区里.这样旧可以画到需要的东西上面了。
 int width  =  bitmap.width;
 int height =  bitmap.rows;
 m_FT_Face->size->metrics.y_ppem;
 m_FT_Face->glyph->metrics.horiAdvance;
 charTex.m_Width = width;
 charTex.m_Height = height;
 charTex.m_adv_x = m_FT_Face->glyph->advance.x / 64.0f;
 charTex.m_adv_y = m_FT_Face->size->metrics.y_ppem; //m_FT_Face->glyph->metrics.horiBearingY / 64.0f;
 charTex.m_delta_x = (float)bitmap_glyph->left;
 charTex.m_delta_y = (float)bitmap_glyph->top - height;
 
 int nPitch = g_nRowsize * 3;
 charTex.m_pbuf = new GLubyte[nPitch * height];
 memset(charTex.m_pbuf, 0, nPitch * height);
 GLubyte *pbuf = new GLubyte[ width * height];
 GLubyte *p;
 for(int j=0; j  < height ; j++)
 {
  for(int i=0; i < width; i++)
  {
   int n = *bitmap.buffer++;
   if (n>0)
   {
    int nPos = j*nPitch + 3*i;
    charTex.m_pbuf[ nPos ] = 0x00;
    charTex.m_pbuf[nPos+1] = 0x00;
    charTex.m_pbuf[nPos+2] = 0xFF;
   }
  }
 }
 return charTex.m_chaID;
}
/*****************************************************************/
void drawText(HDC hdc, wchar_t* _strText,int x , int y, int maxW , int h)
{
 int sx = x;
 int sy = y;
 int maxH = h;
 size_t nLen = wcslen(_strText);
 for(int i = 0 ; i  {
  if(_strText[i] =='\n')
  {
   sx = x ;
   sy += maxH * 1.2;
   continue;
  }
  xCharTexture* pCharTex = getTextChar(_strText[i]);
  int w = pCharTex->m_Width;
  int h = pCharTex->m_Height;
  int ch_x = sx + pCharTex->m_delta_x;
  int ch_y = sy - h - pCharTex->m_delta_y+20;
  if(maxH < h) maxH = h;
  HBITMAP hbmp = createDIBitmapCommon(_strText[i], hdc);
  HDC bmpDC = CreateCompatibleDC(hdc);
  if (bmpDC) {
   SelectObject(bmpDC, hbmp);
   if(1==2)
    StretchBlt(hdc, sx+1, sx+1, sy-2, sy-2, bmpDC, ch_x, ch_y,ch_x+w, ch_y+h,SRCCOPY);
   else
    BitBlt(hdc, 20+sx, 20, w, h,  bmpDC, 0, 0, SRCCOPY);
   DeleteDC(bmpDC);
  }
  DeleteObject(hbmp);
  sx += pCharTex->m_adv_x;
  if(sx > x + maxW)
  {
   sx = x ;
   sy += maxH +5 ;
  }
  //break;
 }
}
xCharTexture* getTextChar(wchar_t ch)
{
 g_FreeTypeLib.loadChar(ch);
 return &g_TexID[ch];
}
LPWSTR AnsiToUnicode(LPCSTR lpcstr)   //参数lpcstr类型也可是char*
{
 LPWSTR Pwstr;
 int  i;
 i=MultiByteToWideChar(CP_ACP,0,lpcstr,-1,NULL,0);
 Pwstr=new WCHAR[i];
 MultiByteToWideChar(CP_ACP,0,lpcstr,-1,Pwstr,i);
 return (Pwstr);
}
static HBITMAP createDIBitmapCommon(wchar_t ch, HDC hdc)
{
 int bmpDx = g_TexID[ch].m_Width;
 int bmpDy = g_TexID[ch].m_Height;
 int bmpRowSize =bmpDx*bmpDy;
 BITMAPINFOHEADER bmih;
 bmih.biSize = sizeof(bmih);
 bmih.biHeight = -bmpDy;
 bmih.biWidth = g_nRowsize;
 bmih.biPlanes = 1;
 bmih.biBitCount = 24;
 bmih.biCompression = BI_RGB;
 bmih.biSizeImage = bmpRowSize;
 bmih.biXPelsPerMeter = bmih.biYPelsPerMeter = 0;
 bmih.biClrUsed = bmih.biClrImportant = 0;
 unsigned char* bmpData =  g_TexID[ch].m_pbuf;
 HBITMAP hbmp = ::CreateDIBitmap(hdc, &bmih, CBM_INIT, bmpData, (BITMAPINFO *)&bmih , DIB_RGB_COLORS);
 
 return hbmp;
}
 
阅读(2786) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-01-22 23:10:17

http://www.unixresources.net/linux/clf/office/archive/00/00/56/72/567231.html

chinaunix网友2009-01-22 10:30:01

Font of OpenGL :http://www.opengl.org/resources/features/fontsurvey/

chinaunix网友2009-01-20 14:46:01

online reader 叫texterity