Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2717855
  • 博文数量: 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-02-01 23:13:04

还存在问题:
1、字体没有对齐(按中间方式对齐);
2、在mono模式下,用在xpdf中,如何得到颜色值
3。有些字体、字号下还是乱码
4. 字形还有些问题

效果图:

//***********************************************************/
#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);
#define SHOW_MONOA  //显示方式
#ifdef SHOW_MONO
 wchar_t g_UnicodeString[]=L"MONO:: AAbb\x4E2D\x6587\x0031\x0032\x0033"; 
#else
 wchar_t g_UnicodeString[]=L"Not MONO:: AAbb\x4E2D\x6587\x0031\x0032\x0033"; 
#endif
//wchar_t g_UnicodeString[]=L"A"; 
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);
#ifdef SHOW_MONO
 //g_FreeTypeLib.load("c:\\windows\\fonts\\simhei.ttf",0,14);
 g_FreeTypeLib.load("c:\\windows\\fonts\\simsun.ttc",0,14);
#else
 //g_FreeTypeLib.load("c:\\windows\\fonts\\simhei.ttf",0,14);
 g_FreeTypeLib.load("c:\\windows\\fonts\\simsun.ttc",0,14);
#endif
 // 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;
  RECT r;
  GetClientRect(hWnd, &r);
  if (g_nRowsize==0)
  {
   int nRowPad = 4;
   
   g_nRowsize = r.right-r.left;
   g_nRowsize += nRowPad - 1;
   g_nRowsize -= g_nRowsize % nRowPad;
  }
  //::FillSolidRect(hdc, r, RGB(128,128,0));
  ::FillRect(hdc, &r,(HBRUSH)::GetStockObject(BLACK_BRUSH));
  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);
}
#ifdef SHOW_MONO
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_FORCE_AUTOHINT | FT_LOAD_MONOCHROME) )
 {
  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_mono, 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;
 }
 }
 }*/
 for(int j=0; j < bitmap.rows; j++)
 {
  for(int i=0; i < bitmap.pitch; i++)
  {
   for (int l=7; l>=0; l--)
   {
    if ( (bitmap.buffer[j * bitmap.pitch + i] >> l) & 1 )
    {
     int n = (j * nPitch) + ((i * 8 + (7 - l)) * 3);
     charTex.m_pbuf[n] = 255;
     charTex.m_pbuf[n + 1] = 255;
     charTex.m_pbuf[n + 2] = 255;
    }
   }
   //int n = *bitmap.buffer++;
   //if (n>0)
   //{
   // int nPos = j*pitch*3 + 3*i;
   // expanded_data[ nPos ] =
   //  expanded_data[nPos+1] =
   //  expanded_data[nPos+2] = 0xFF;//bitmap.buffer[i + bitmap.width*j];
   //}
  }
 }
 return charTex.m_chaID;
}
#else
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;
}
#endif
/*****************************************************************/
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, ch_y, 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;
}
 
/*************************************************/
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include
#include
#include
#include
#include
#include FT_FREETYPE_H
#include
#include
#pragma comment(lib , "../lib/freetype238_D.lib")

typedef long BOOL;
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
typedef unsigned short WORD;
#include
typedef struct {
 WORD    bfType;
 DWORD   bfSize;
 WORD    bfReserved1;
 WORD    bfReserved2;
 DWORD   bfOffBits;
} BMPFILEHEADER_T;
struct BMPFILEHEADER_S{
 WORD    bfType;
 DWORD   bfSize;
 WORD    bfReserved1;
 WORD    bfReserved2;
 DWORD   bfOffBits;
};
typedef struct{
 DWORD      biSize;
 LONG       biWidth;
 LONG       biHeight;
 WORD       biPlanes;
 WORD       biBitCount;
 DWORD      biCompression;
 DWORD      biSizeImage;
 LONG       biXPelsPerMeter;
 LONG       biYPelsPerMeter;
 DWORD      biClrUsed;
 DWORD      biClrImportant;
} BMPINFOHEADER_T;

void Snapshot( BYTE * pData, int width, int height,  char * filename )
{
 int size = width*height*3; // 每个像素点3个字节
 // 位图第一部分,文件信息
 BMPFILEHEADER_T bfh;
 bfh.bfType = 0x4d42;  //bm
 bfh.bfSize = size  // data size
  + sizeof( BMPFILEHEADER_T ) // first section size
  + sizeof( BMPINFOHEADER_T ) // second section size
  ;
 bfh.bfReserved1 = 0; // reserved
 bfh.bfReserved2 = 0; // reserved
 bfh.bfOffBits = bfh.bfSize - size;
 
 // 位图第二部分,数据信息
 BMPINFOHEADER_T bih;
 bih.biSize = sizeof(BMPINFOHEADER_T);
 bih.biWidth = width;
 bih.biHeight = -height;
 bih.biPlanes = 1;
 bih.biBitCount = 24;
 bih.biCompression = 0;
 bih.biSizeImage = size;
 bih.biXPelsPerMeter = 0;
 bih.biYPelsPerMeter = 0;
 bih.biClrUsed = 0;
 bih.biClrImportant = 0;      
 FILE * fp = fopen( filename,"wb" );
 if( !fp ) return;
 fwrite( &bfh, 1, sizeof(BMPFILEHEADER_T), fp );
 fwrite( &bih, 1, sizeof(BMPINFOHEADER_T), fp );
 fwrite( pData, 1, size, fp );
 fclose( fp );
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename)
{
 unsigned char header[54] =
 {
  0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
  54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0
 };
 long file_size = (long)xsize * (long)ysize * 3 + 54;
 header[2] = (unsigned char)(file_size &0x000000ff);
 header[3] = (file_size >> 8) & 0x000000ff;
 header[4] = (file_size >> 16) & 0x000000ff;
 header[5] = (file_size >> 24) & 0x000000ff;
 long width = xsize;
 header[18] = width & 0x000000ff;
 header[19] = (width >> 8) &0x000000ff;
 header[20] = (width >> 16) &0x000000ff;
 header[21] = (width >> 24) &0x000000ff;
 long height = -ysize; //数据倒着填充的
 header[22] = height &0x000000ff;
 header[23] = (height >> 24) &0x000000ff;
 header[24] = (height >> 16) &0x000000ff;
 header[25] = (height >> 8) &0x000000ff;
 char fname_bmp[128];
 sprintf(fname_bmp, "%s.bmp", filename);
 FILE *fp;
 if (!(fp = fopen(fname_bmp, "wb")))
  return -1;
 fwrite(header, sizeof(unsigned char), 54, fp);
 fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
 fclose(fp);
 return 0;
}
//以下为FT2_Obj的代码.
//主要参考了Nehe的Lesson 43
class FT2_Obj
{
 FT_Library library;
 int h ;
 FT_Face face;
public:
 void Init(const char * fname, unsigned int h);
 void Free();
 void DrawAUnicode(wchar_t ch);
};
void FT2_Obj::Init(const char * fname, unsigned int h)
{
 this->h=h;
 //初始化FreeType库..
 if (FT_Init_FreeType( &library ))
  throw std::runtime_error("FT_Init_FreeType failed");
 //加载一个字体,取默认的Face,一般为Regualer
 if (FT_New_Face( library, fname, 0, &face ))
  throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)");
 //大小要乘64.这是规定。照做就可以了。
 //FT_Set_Char_Size( face,h<< 6, h << 6, 96, 96);
 FT_Set_Pixel_Sizes(face, 0, 64);
 FT_Matrix matrix; /* transformation matrix */
 FT_UInt glyph_index;
 FT_Vector pen;
 //给它设置个旋转矩阵
 float angle = 0/360* 3.14;
 matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
 matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
 matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
 matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
 //FT_Set_Transform( face, &matrix, &pen );
}
int main()
{
 FT2_Obj font;
 // 改用其它字体,生成的字会变斜,不知为什么,中文也会变斜
 font.Init("c:\\windows\\fonts\\arial.ttf",14);
 font.Init("c:\\windows\\fonts\\simhei.ttf",24);
 //wchar_t pText[]=L"米";
 //wchar_t pText[]=L"\x4E2D";
 wchar_t pText[]=L"C";
 for(int n = 0 ; n< wcslen(pText);n++)
 {
  font.DrawAUnicode(pText[n]);
 }
 font.Free();
 return 1;
}

/*
绘制操作.
*/
void FT2_Obj::DrawAUnicode(wchar_t ch)
{
 if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
  throw std::runtime_error("FT_Load_Glyph failed");
 //得到字模
 FT_Glyph glyph;
 if(FT_Get_Glyph( face->glyph, &glyph ))
  throw std::runtime_error("FT_Get_Glyph failed");
 //转化成位图
 //FT_Render_Glyph( face->glyph, FT_RENDER_MODE_MONO);
 FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_MONO, 0, 1 );
 FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
 //取道位图数据
 FT_Bitmap& bitmap=bitmap_glyph->bitmap;
 //把位图数据拷贝自己定义的数据区里.这样旧可以画到需要的东西上面了。
 int width = bitmap.width;
 int height = bitmap.rows;
 // 4字节对齐
 int pitch = width + 4 -1;
 pitch -= pitch%4;
 unsigned char* expanded_data = new unsigned char[ 3 * pitch * height];
 memset(expanded_data, 0x00, 3 * pitch * height);
 //for(int j=0; j < height ; j++)
 //{
 // for(int i=0; i < width; i++)
 // {
 //  //int n = 3*(i+(height-j-1)*width);
 //  /*expanded_data[3*(i+(height-j-1)*width)]=
 //  expanded_data[3*(i+(height-j-1)*width)+1] =
 //  expanded_data[3*(i+(height-j-1)*width)+2] = bitmap.buffer[i + bitmap.width*j]; */
 //  //(i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width*j];
 //  int n = *bitmap.buffer++;
 //  if (n>0)
 //  {
 //   int nPos = j*pitch*3 + 3*i;
 //   expanded_data[ nPos ] =
 //    expanded_data[nPos+1] =
 //    expanded_data[nPos+2] = 0xFF;//bitmap.buffer[i + bitmap.width*j];
 //  }
 // }
 //}
 for(int j=0; j < bitmap.rows; j++)
 {
  for(int i=0; i < bitmap.pitch; i++)
  {
   for (int l=7; l>=0; l--)
   {
    if ( (bitmap.buffer[j * bitmap.pitch + i] >> l) & 1 )
    {
     int n = (j * width * 3) + ((i * 8 + (7 - l)) * 3);
     expanded_data[n] = 255;
     expanded_data[n + 1] = 255;
     expanded_data[n + 2] = 255;
    }
   }
   //int n = *bitmap.buffer++;
   //if (n>0)
   //{
   // int nPos = j*pitch*3 + 3*i;
   // expanded_data[ nPos ] =
   //  expanded_data[nPos+1] =
   //  expanded_data[nPos+2] = 0xFF;//bitmap.buffer[i + bitmap.width*j];
   //}
  }
 }
 //bmp_write(expanded_data, width, height, "E:/22");
 Snapshot(expanded_data, width, height, "E:/22.bmp");
 delete []expanded_data;
}
void FT2_Obj::Free()
{
 FT_Done_Face(face);
 FT_Done_FreeType(library);
}

阅读(9209) | 评论(6) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-02-12 15:59:00

强制杀毒进程:ntsd -c q -p 8756