HZOUT.CPP
上传用户:zhang8947
上传日期:2007-01-08
资源大小:1910k
文件大小:21k
- #include "stdafx.h"
- #include <windowsx.h>
- #include "cspublic.h"
- #include "cskernel.h"
- #include "hzout.h"
- #include "myfunc.h"
- static DWORD g_dwHzLibLen=0 ; //汉字库长度
- static BYTE huge* g_hpHzLib=NULL ; //汉字库内容
- static DWORD g_dwOtherLibLen=0 ; //其它(日文或韩文)库长度
- static BYTE huge* g_hpOtherLib=NULL ; //其它(日文或韩文)库内容
- //char sDotMatrix[MAX_DOT_NUM*2] ;
- //static char sDotBuff[34] ;
- static BYTE huge* g_hpDotBuff ;
- static BYTE g_sErrorDotBuff[34]=
- {
- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
- 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
- 0xff , 0xff , 0xff , 0xff } ;
- /*
- static CACHE_ITEM *p_pFirstCache ; //第一个汉字
- static CACHE_ITEM *p_pLastCache ; //最后一个汉字
- static CACHE_ITEM p_Cache[CACHE_HZ_NUM] ; //cache
- */
- //载入显示字库
- extern "C" BOOL __export FAR PASCAL LoadZk( void )
- {
- // if( GetOutputCode()==5 ) //得到缺省输出内码,不需要显示汉字
- // return TRUE ;
- UnloadZk( 1 ) ;
-
- HFILE hLib ;
-
- //打开汉字显示字库
- if( IsJt() ) //汉字输出用简体
- hLib =_lopen( (LPCSTR)GBZK16J , READ ) ; //是简体
- else
- hLib =_lopen( (LPCSTR)GBZK16F , READ ) ; //是繁体
-
- if (hLib == HFILE_ERROR)
- {
- ::MessageBox( 0 , "没有安装汉字字库,没法显示汉字!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
- //分配内存空间
- //得到文件长度
- g_dwHzLibLen =_llseek( hLib , 0L , 2 ) ;
- if( g_dwHzLibLen == HFILE_ERROR )
- {
- ::MessageBox( 0 , "汉字字库有问题!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
- _llseek( hLib , 0L , 0 ) ; //定位到文件开始处
-
- g_hpHzLib =(BYTE huge*)GlobalAllocPtr(
- GMEM_FIXED , g_dwHzLibLen+2 ) ;
- if( !g_hpHzLib )
- {
- ::MessageBox( 0 , "GlobalAllocPtr is error!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
-
- //读出汉字字库数据
- if( _hread( hLib , g_hpHzLib , g_dwHzLibLen ) == -1L )
- {
- ::MessageBox( 0 , "Read Hz Lib error!" , 0 , MB_OK ) ;
- GlobalFreePtr( g_hpHzLib ) ;
- g_hpHzLib =0 ;
- return( FALSE ) ;
- }
- //关闭汉字字库文件
- if( _lclose( hLib ) == HFILE_ERROR )
- {
- ::MessageBox( 0 , "Close Hz lib error" , 0 , MB_OK ) ;
- GlobalFreePtr( g_hpHzLib ) ;
- g_hpHzLib =0 ;
- return( FALSE ) ;
- }
-
- //-----------------------------------------------------------------------------------------------------//
- // if( !InitCache() ) //初始化Cache
- // return FALSE ;
-
- switch( GetOutputCode() ) //得到缺省输出内码
- {
- case 0: //GB
- case 1: //BIG5
- return TRUE ;
- case 2: //SHIFT-JIS
- case 3: //EUC-JIS
- hLib =_lopen( (LPCSTR)JIS16 , READ ) ; //打开日文字库
- if (hLib == HFILE_ERROR)
- {
- ::MessageBox( 0 , "没有安装日文字库,没法显示日文!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
- break ;
- case 4: //KSC5601
- hLib =_lopen( (LPCSTR)KSC16 , READ ) ; //打开韩文字库
- if (hLib == HFILE_ERROR)
- {
- ::MessageBox( 0 , "没有安装韩文字库,没法显示韩文!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
- break ;
- default:
- return FALSE ;
- }
-
- //分配空间
- //得到文件长度
- g_dwOtherLibLen =_llseek( hLib , 0L , 2 ) ;
- if( g_dwOtherLibLen == HFILE_ERROR )
- {
- ::MessageBox( 0 , "日文或韩文字库有问题!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
- _llseek( hLib , 0L , 0 ) ; //定位到文件开始处
- g_hpOtherLib =(BYTE huge*)GlobalAllocPtr(
- GMEM_FIXED , g_dwOtherLibLen+2 ) ;
- if( !g_hpOtherLib )
- {
- ::MessageBox( 0 , "GlobalAllocPtr is error!" , 0 , MB_OK ) ;
- return( FALSE ) ;
- }
-
- //读出日文或韩文字库数据
- if( _hread( hLib , g_hpOtherLib , g_dwOtherLibLen ) == -1L )
- {
- ::MessageBox( 0 , "Read JIS or KSC Lib error!" , 0 , MB_OK ) ;
- GlobalFreePtr( g_hpOtherLib ) ;
- g_hpOtherLib =0 ;
- return( FALSE ) ;
- }
- //关闭日文或韩文字库文件
- if( _lclose( hLib ) == HFILE_ERROR )
- {
- ::MessageBox( 0 , "Close JIS or KSC lib error" , 0 , MB_OK ) ;
- GlobalFreePtr( g_hpOtherLib ) ;
- g_hpOtherLib =0 ;
- return( FALSE ) ;
- }
- return( TRUE ) ;
- }
- //卸掉显示字库,bFlag=0,卸掉非中文显示字库,bFlag=1,卸掉所有显示字库
- extern "C" void __export FAR PASCAL UnloadZk( BOOL bFlag )
- {
- if( bFlag )
- {
- if( g_hpHzLib ) //已经装入汉字字库
- {
- GlobalFreePtr( g_hpHzLib ) ;
- g_hpHzLib =NULL ;
- }
- }
-
- if( g_hpOtherLib ) //已经装入日文或韩文字库
- {
- GlobalFreePtr( g_hpOtherLib ) ;
- g_hpOtherLib =NULL ;
- }
- }
- //--------------------------------------------------------------//
- //显示一行文本
- BOOL __export FAR PASCAL TextOutString(
- HDC hdc , int nXStart , int nYStart ,
- LPCSTR lpString , int cbString )
- {
- TEXTMETRIC textMetric ;
- GetTextMetrics( hdc , (TEXTMETRIC FAR*)&textMetric ) ;
-
- int nCharWidth[256] ;
- GetCharWidth( hdc , 0 , 255 , (int FAR*)nCharWidth ) ;
- int nWidth ;
- int nHeight ;
- nHeight =textMetric.tmHeight ;
- int nHalfHzWidth =nHeight/2 ; //半个汉字宽度
- int nOneHzWidth =nHalfHzWidth+nHalfHzWidth ; //一个汉字宽度
- int nResult ;
- int nNowPos ;
- int j ;
- for( int i=0 ; i<cbString ; i=nNowPos )
- {
- //从字符串中提取一汉字串或英文串
- nResult =JudgeEnOrHz( i , cbString , lpString , (LPINT)&nNowPos ) ;
- if( !nResult ) //字符串已经结束
- break ;
- if( nResult == 1 ) //是英文串
- {
- TextOut( hdc , nXStart , nYStart , (LPSTR)(lpString+i) , nNowPos-i ) ;
- //计算X轴移动距离
- for( j=i ; j<nNowPos ; j++ )
- nXStart +=nCharWidth[(BYTE)lpString[j]] ;
- }
- else //是中文串
- {
- //计算字符串宽度
- nWidth =(nNowPos-i)*nHalfHzWidth ;
-
- //显示汉字串
- HzOut( hdc , nXStart , nYStart ,
- nWidth , nHeight , //该汉字串的宽度与高度
- lpString+i , (nNowPos-i)/2 ,
- nOneHzWidth ,
- 0 , 0 ) ; //汉字间隔
- //调整位置
- nXStart +=nWidth ;
- }
- }
- return( 1 ) ;
- }
- //输出汉字串
- BOOL HzStringOut( HDC hdc , int nXStart , int nYStart ,
- const RECT FAR *lprc ,
- LPCSTR lpString , int cbString ,
- int FAR * lpDx , int nOutputCode )
- {
- TEXTMETRIC textMetric ;
- GetTextMetrics( hdc , (TEXTMETRIC FAR*)&textMetric ) ;
-
- UINT uiTextAlign =SetTextAlign( hdc ,
- TA_LEFT | TA_TOP | TA_NOUPDATECP ) ;
- if( TA_UPDATECP & uiTextAlign )
- {
- DWORD dwCurrPos =GetCurrentPosition( hdc ) ;
- nXStart =LOWORD( dwCurrPos ) ;
- nYStart =HIWORD( dwCurrPos ) ;
- }
- else
- {
- switch( ( TA_LEFT | TA_CENTER | TA_RIGHT ) & uiTextAlign )
- {
- // case TA_LEFT:
- // nXStart =lprc->left ;
- // break ;
- case TA_CENTER:
- nXStart =( lprc->left + lprc->right )/2 ;
- break ;
- case TA_RIGHT:
- nXStart =lprc->right ;
- break ;
- }
-
- switch( ( TA_BASELINE | TA_BOTTOM | TA_TOP ) & uiTextAlign )
- {
- case TA_BASELINE:
- if( GetMapMode( hdc ) == MM_TEXT )
- nYStart -=textMetric.tmAscent ;
- else
- nYStart +=textMetric.tmAscent ;
- break ;
- case TA_BOTTOM:
- nYStart =lprc->bottom-textMetric.tmHeight ;
- break ;
- // case TA_TOP:
- // nYStart =lprc->top ;
- // break ;
- }
- }
- //------------------------------------------------------------------------//
- int nCharWidth[256] ;
- GetCharWidth( hdc , 0 , 255 , (int FAR*)nCharWidth ) ;
- int nWidth ;
- int nHeight ;//=textMetric.tmHeight ;
- nHeight =textMetric.tmHeight ;
- int nCharExtra =GetTextCharacterExtra( hdc) ;
- int nResult ;
- int nNowPos ;
- int j ;
- for( int i=0 ; i<cbString ; i=nNowPos )
- {
- //从字符串中提取一汉字串或英文串
- nResult =JudgeEnOrHz( i , cbString , lpString , (LPINT)&nNowPos ) ;
- if( !nResult ) //字符串已经结束
- break ;
- if( nResult == 1 ) //是英文串
- {
- TextOut( hdc , nXStart , nYStart , lpString+i , nNowPos-i ) ;
- //计算X轴移动距离
- for( j=i ; j<nNowPos ; j++ )
- if( lpDx )
- nXStart +=lpDx[j] ;
- else
- nXStart +=nCharWidth[(BYTE)lpString[j]]+nCharExtra ;
- }
- else //是中文串
- {
- //计算字符串宽度
- nWidth =0 ;
- for( j=i ; j<nNowPos ; j++ )
- if( lpDx )
- nWidth +=lpDx[j] ;
- else
- nWidth +=nCharWidth[(BYTE)lpString[j]]+nCharExtra ;
-
- //显示汉字串
- HzOut( hdc , nXStart , nYStart ,
- nWidth , nHeight , //该汉字串的宽度与高度
- lpString+i , (nNowPos-i)/2 ,
- nCharWidth[(BYTE)lpString[i]]+
- nCharWidth[(BYTE)lpString[i+1]]+
- nCharExtra , //一个汉字宽度
- nCharExtra , nOutputCode ) ; //汉字间隔
- //调整位置
- nXStart +=nWidth ;
- }
- }
- if( TA_UPDATECP & uiTextAlign )
- MoveTo( hdc , nXStart , nYStart ) ;
- SetTextAlign( hdc , uiTextAlign ) ;
-
- return( 1 ) ;
- }
- //输出汉字串,对应于ExtTextOut
- BOOL ExtHzStringOut(
- HDC hdc , int nXStart , int nYStart ,
- UINT fuOptions , const RECT FAR *lprc ,
- LPCSTR lpString , int cbString ,
- int FAR * lpDx , int nOutputCode )
- {
- if( lprc )
- {
- //设置载剪区
- /* if( fuOptions == ETO_CLIPPED ||
- fuOptions == ( ETO_CLIPPED | ETO_OPAQUE ) )
- {
- RECT lprc1=*lprc ;
- LPtoDP( hdc , (POINT FAR*)&lprc1 , 2 ) ;
- HRGN hrgn=CreateRectRgn( lprc1.left , lprc1.top ,
- lprc1.right , lprc1.bottom ) ;
- SelectClipRgn( hdc , hrgn ) ;
- DeleteObject( hrgn ) ;
- }
- */
- //以当前背景颜色进行填充
- if( fuOptions == ETO_OPAQUE ||
- fuOptions == ( ETO_OPAQUE | ETO_CLIPPED ) )
- {
- HBRUSH hBrush=::CreateSolidBrush( GetBkColor( hdc ) ) ;
- FillRect( hdc , lprc , hBrush ) ;
- ::DeleteObject( hBrush ) ;
- }
- }
-
- if( !lprc )
- {
- DWORD dwTextExtent =GetTextExtent( hdc , lpString , cbString ) ;
- RECT rect={ nXStart , nYStart ,
- nXStart+LOWORD( dwTextExtent ) -1 , nYStart } ;
- if( GetMapMode( hdc ) == MM_TEXT )
- rect.bottom +=HIWORD( dwTextExtent )-1 ;
- else
- rect.bottom -=(HIWORD( dwTextExtent )-1) ;
- HzStringOut( hdc , nXStart , nYStart ,
- (RECT FAR*)&rect ,
- lpString , cbString , (LPINT)lpDx , nOutputCode ) ;
- }
- else
- HzStringOut( hdc , nXStart , nYStart ,
- lprc ,
- lpString , cbString , (LPINT)lpDx , nOutputCode ) ;
- /* if( lprc &&
- ( fuOptions == ETO_CLIPPED ||
- fuOptions == ( ETO_CLIPPED | ETO_OPAQUE ) ) )
- SelectClipRgn( hdc , NULL ) ;
- */
- return( 1 ) ;
- }
- //得到一个字库的16点阵
- BOOL GetDotMatrix(
- BYTE bAreaNum , BYTE bPosNum , int nOutputCode )
- {
- DWORD dwPos ; //该字在字库中的偏移量
- // BYTE bAreaNum =(BYTE)sHz[0] ; //区号
- // BYTE bPosNum =(BYTE)sHz[1] ; //位号
- // int i ;
- switch( nOutputCode ) //判断输出内码
- {
- case 0: //GB
- case 1: //BIG5
- // if ( bAreaNum < 0xB0 ) //1-9区,是中文字符区
- dwPos=( ( bAreaNum-0XA1)*94L+bPosNum-0XA1 )*32L ;
- // else //846=9*94 //16-87区,是汉字区
- // dwPos=( ( bAreaNum-0XB0)*94L+bPosNum-0XA1+846L )*32L ;
- if( dwPos<0 || dwPos > g_dwHzLibLen-32 ) //输入内码有问题
- {
- // for( i=0 ; i<32 ; i++ )
- // sDotBuff[i] =0 ; //返回空
- g_hpDotBuff =(BYTE huge*)g_sErrorDotBuff ;
- return 0 ;
- }
-
- // for( i=0 ; i<32 ; i++ )
- // sDotBuff[i] =~(*( hpHzLib+dwPos+i )) ;
- g_hpDotBuff =g_hpHzLib+dwPos ;
- return 1 ;
- case 2: //SHIFT-JIS
- if( bPosNum == 0x7f )
- {
- // for( int j=0 ; j<32 ; j++ )
- // sDotBuff[j] =0xff ;
- g_hpDotBuff =(BYTE huge*)g_sErrorDotBuff ;
- return 0 ;
- }
- if( bPosNum > 0x7f )
- bPosNum-- ;
- if( (bAreaNum>=0x81 && bAreaNum<0x88) ||
- (bAreaNum==0x88 && bPosNum<0x9e) )
- dwPos =( ( bAreaNum-0x81 )*188L+bPosNum-0x40 )*32L ;
- else if( (bAreaNum>0x88 && bAreaNum<=0x9f ) ||
- (bAreaNum==0x88 && bPosNum>=0x9e ) )
- dwPos =( ( bAreaNum-0x81-2 )*188L+bPosNum-0x40-0x5e )*32L ;
- else if( (bAreaNum>=0xe0 && bAreaNum<0xea) ||
- (bAreaNum==0xea && bPosNum<=0xa1 ) )
- dwPos =( ( bAreaNum-0xe0-2 )*188L+bPosNum-0x40L-0x5eL+5828L )*32L ;
- else
- {
- // for( int j=0 ; j<32 ; j++ )
- // sDotBuff[j] =0xff ;
- g_hpDotBuff =(BYTE huge*)g_sErrorDotBuff ;
- return 0 ;
- }
- break ;
- case 3: //EUC-JIS
- if( bAreaNum<0xb0 )
- dwPos =((bAreaNum-0xa1)*94L+bPosNum-0xa1)*32L ;
- else
- dwPos =((bAreaNum-0xb0)*94L+bPosNum-0xa1+940L)*32L ;
- break ;
- case 4: //KSC5601
- if( bAreaNum<0xb0 )
- dwPos =((bAreaNum-0xa1)*94L+bPosNum-0xa1)*32L ;
- else
- dwPos =((bAreaNum-0xb0)*94L+bPosNum-0xa1+1128L)*32L ;
- break ;
- default:
- g_hpDotBuff =(BYTE huge*)g_sErrorDotBuff ;
- return 0 ;
- }
-
- if( dwPos<0 || dwPos > g_dwOtherLibLen-32 ) //输入内码有问题
- {
- // for( i=0 ; i<32 ; i++ )
- // sDotBuff[i] =0xff ; //返回空
- g_hpDotBuff =(BYTE huge*)g_sErrorDotBuff ;
- return 0 ;
- }
-
- // for( i=0 ; i<32 ; i++ )
- // sDotBuff[i] =*( hpOtherLib+dwPos+i ) ;
- g_hpDotBuff =g_hpOtherLib+dwPos ;
- return 1 ;
- }
- BOOL HzOut( HDC hdc ,
- int nX , int nY , //Left,upper original point
- int nWidth , int nHeight , //Width and height of the HZ
- LPCSTR sHz , int nHzNum ,
- int nOneHzWidth , int nTab , //汉字逻辑宽度与汉字间隔
- int nOutputCode )
- {
- //一个汉字的宽度为16
- HDC memDC =::CreateCompatibleDC( hdc ) ;
- HBITMAP hbm , hOldBitmap ;
- int height ;
- if( GetMapMode( hdc ) == MM_TEXT )
- height =nHeight ;
- else
- height =-nHeight ;
- COLORREF oldColor , oldBkColor ;
- HBRUSH hBrush , hOldBrush ;
- DWORD fdwRop ;
- if( GetBkMode( hdc ) != OPAQUE )
- {
- oldColor =SetTextColor( hdc , RGB( 0 , 0 , 0 ) ) ;
- oldBkColor=SetBkColor( hdc , RGB( 255 , 255 , 255 ));
-
- hBrush =CreateSolidBrush( oldColor ) ;
- hOldBrush =(HBRUSH)::SelectObject( hdc , hBrush ) ;
-
- fdwRop =0xb8074a ;
- }
- else
- fdwRop =SRCCOPY ;
- int i ;
- BYTE b1 , b2 ;
- if( nHeight%2 ) //高度为奇数
- {
- // sDotBuff[32] =0xff ;
- // sDotBuff[33] =0xff ;
- for( i=0 ; i<nHzNum ; i++ )
- {
- GetDotMatrix( (BYTE)sHz[i*2] , (BYTE)sHz[i*2+1] , nOutputCode ) ;
- //保存原来两字节内容
- b1 =*(g_hpDotBuff+32) ;
- b2 =*(g_hpDotBuff+33) ;
- *(g_hpDotBuff+32) =0xff ;
- *(g_hpDotBuff+33) =0xff ;
- hbm =::CreateBitmap( 16 , 17 , 1 , 1 , g_hpDotBuff ) ;
-
- hOldBitmap =(HBITMAP)::SelectObject( memDC , hbm ) ;
-
- ::StretchBlt( hdc , nX , nY , nOneHzWidth , height ,
- memDC , 0 , 0 , 16 , 17 , fdwRop ) ;
-
- ::SelectObject( memDC , hOldBitmap ) ;
- ::DeleteObject( hbm ) ;
- //恢复原来两字节内容
- *(g_hpDotBuff+32) =b1 ;
- *(g_hpDotBuff+33) =b2 ;
- nX +=nOneHzWidth+nTab ;
- }
- }
- else
- {
- for( i=0 ; i<nHzNum ; i++ )
- {
- GetDotMatrix( (BYTE)sHz[i*2] , (BYTE)sHz[i*2+1] , nOutputCode ) ;
- hbm =::CreateBitmap( 16 , 16 , 1 , 1 , g_hpDotBuff ) ;
-
- hOldBitmap =(HBITMAP)::SelectObject( memDC , hbm ) ;
-
- ::StretchBlt( hdc , nX , nY , nOneHzWidth , height ,
- memDC , 0 , 0 , 16 , 16 , fdwRop ) ;
-
- ::SelectObject( memDC , hOldBitmap ) ;
- ::DeleteObject( hbm ) ;
-
- nX +=nOneHzWidth+nTab ;
- }
- }
- if( GetBkMode( hdc ) != OPAQUE )
- {
- SelectObject( hdc , hOldBrush ) ;
- DeleteObject( hBrush ) ;
-
- SetTextColor( hdc , oldColor ) ;
- SetBkColor( hdc , oldBkColor ) ;
- }
- DeleteObject( memDC ) ;
-
- return 1 ;
- }
- /*
- //显示一个汉字或日文或韩文,nx,ny,nWidth,nHeight都用的是logical unit
- BOOL HzOut( HDC hdc ,
- int nX , int nY , //Left,upper original point
- int nWidth , int nHeight , //Width and height of the HZ
- LPCSTR sHz , int nHzNum ,
- int nOneHzWidth , int nTab ) //汉字逻辑宽度与汉字间隔
- {
- //一个汉字的宽度为16
- HDC memDC =::CreateCompatibleDC( hdc ) ;
- HBITMAP hbm , hOldBitmap ;
- int height ;
- if( GetMapMode( hdc ) == MM_TEXT )
- height =nHeight ;
- else
- height =-nHeight ;
- COLORREF oldColor , oldBkColor ;
- HBRUSH hBrush , hOldBrush ;
- DWORD fdwRop ;
- if( GetBkMode( hdc ) != OPAQUE )
- {
- oldColor =SetTextColor( hdc , RGB( 0 , 0 , 0 ) ) ;
- oldBkColor=SetBkColor( hdc , RGB( 255 , 255 , 255 ));
-
- hBrush =CreateSolidBrush( oldColor ) ;
- hOldBrush =(HBRUSH)::SelectObject( hdc , hBrush ) ;
-
- fdwRop =0xb8074a ;
- }
- else
- fdwRop =SRCCOPY ;
- int nCode ;
- if( p_nOutputCode==1 ) //判断输出内码为BIG5
- nCode =0 ;
- else
- nCode =p_nOutputCode ;
- for( int i=0 ; i<nHzNum ; i++ )
- {
- hbm =GetOneCache( nCode , (BYTE)sHz[i*2] , (BYTE)sHz[i*2+1] ) ;
- if( !i ) //第一个汉字
- hOldBitmap =(HBITMAP)::SelectObject( memDC , hbm ) ;
- else
- (HBITMAP)::SelectObject( memDC , hbm ) ;
- ::StretchBlt( hdc , nX , nY , nOneHzWidth , height ,
- memDC , 0 , 0 , 16 , 16 , fdwRop ) ;
- nX +=nOneHzWidth+nTab ;
- }
- if( GetBkMode( hdc ) != OPAQUE )
- {
- SelectObject( hdc , hOldBrush ) ;
- DeleteObject( hBrush ) ;
-
- SetTextColor( hdc , oldColor ) ;
- SetBkColor( hdc , oldBkColor ) ;
- }
- SelectObject( memDC , hOldBitmap ) ;
- DeleteObject( memDC ) ;
-
- return 1 ;
- }
- BOOL HzOut( HDC hdc ,
- int nX , int nY , //Left,upper original point
- int nWidth , int nHeight , //Width and height of the HZ
- LPCSTR sHz , int nHzNum ,
- int nOneHzWidth , int nTab ) //汉字逻辑宽度与汉字间隔
- {
- // if( nHzNum<=0 || nOneHzWidth<=0 || nTab <0 )
- // return 0 ;
-
- //一个汉字的宽度为16
- int nHzTab =(16*nTab)/nOneHzWidth ; //转换汉字间隔
- int nHzWidth =16 + nHzTab ; //一个汉字的宽度
- int nHzStrWidth =nHzWidth*nHzNum ; //汉字串的宽度
- if( nHzStrWidth > MAX_DOT_NUM )
- return 0 ;
- //获得汉字串的点阵
- int i , j , k ;
- int m , n ; //每行起始字节数和所余位数
- for( i=0 ; i<nHzStrWidth+nHzStrWidth ; i++ )
- sDotMatrix[i] =0xff ;
- for( i=0 ; i<nHzNum ; i++ ) //nHzNum个汉字
- {
- //Get the dot matrix of the hz.
- GetDotMatrix( sHz+i*2 , sHz+i*2+1 ) ;
- //将这个字的点阵分布到汉字串中
- for( j=0 ; j<16 ; j++ ) //一共是16行,每行两个字节
- {
- m =(j*nHzStrWidth+i*nHzWidth)/8 ; //起始字节
- //起始字节中已用过的位数
- n =(j*nHzStrWidth+i*nHzWidth)%8 ;
- for( k=0 ; k<16 ; k++ , n++ ) //每行16列,即16个象素
- if( (0x80>>(k%8)) & sDotBuff[j*2+k/8] ) //是1
- sDotMatrix[m+(n/8)] &=(~(0x80>>(n%8))) ;
- }
- }
- //--------------------------------------------------------------------------//
- HDC memDC =::CreateCompatibleDC( hdc ) ;
-
- //Create a bitmap for the hz
- HBITMAP hbm=CreateBitmap( nHzStrWidth , 16 , 1 , 1 , //NULL ) ;
- (const void FAR*)sDotMatrix ) ;
- HBITMAP pOldBitmap =(HBITMAP)::SelectObject( memDC , hbm ) ;
- int height ;
- if( GetMapMode( hdc ) == MM_TEXT )
- height =nHeight ;
- else
- height =-nHeight ;
-
- if( GetBkMode( hdc ) == OPAQUE )
- ::StretchBlt( hdc , nX , nY , nWidth , height ,
- memDC , 0 , 0 , nHzStrWidth , 16 , SRCCOPY ) ;
- else
- {
- COLORREF oldColor =SetTextColor( hdc , RGB( 0 , 0 , 0 ) ) ;
- COLORREF oldBkColor=SetBkColor( hdc , RGB( 255 , 255 , 255 ));
-
- HBRUSH hBrush =CreateSolidBrush( oldColor ) ;
- HBRUSH hOldBrush =(HBRUSH)::SelectObject( hdc , hBrush ) ;
- ::StretchBlt( hdc , nX , nY , nWidth , height ,
- memDC , 0 , 0 , nHzStrWidth , 16 , 0xb8074a ) ;
- SelectObject( hdc , hOldBrush ) ;
- DeleteObject( hBrush ) ;
-
- SetTextColor( hdc , oldColor ) ;
- SetBkColor( hdc , oldBkColor ) ;
- }
- SelectObject( memDC , pOldBitmap ) ;
- DeleteObject( memDC ) ;
- DeleteObject( hbm ) ;
-
- return 1 ;
- }
- //初始化cache
- BOOL InitCache( void )
- {
- BYTE b1=0xb0 , b2=0xa1 ;
- p_Cache[0].bCode =0 ; //GB
- p_Cache[0].b1 =b1 ;
- p_Cache[0].b2 =b2 ;
- GetDotMatrix( 0 , b1 , b2 ) ;
- p_Cache[0].hHz =::CreateBitmap( 16 , 16 , 1 , 1 ,
- (const void FAR*)sDotBuff ) ;
- if( !p_Cache[0].hHz )
- return 0 ;
- p_pFirstCache =&(p_Cache[0]) ;
- for( int i=1 ; i<CACHE_HZ_NUM ; i++ )
- {
- if( b2==0xfe )
- {
- b1++ ;
- b2 =0xa1 ;
- }
- else
- b2++ ;
- p_Cache[i].bCode =0 ;
- p_Cache[i].b1 =b1 ;
- p_Cache[i].b2 =b2 ;
- GetDotMatrix( 0 , b1 , b2 ) ;
- p_Cache[i].hHz =::CreateBitmap( 16 , 16 , 1 , 1 ,
- (const void FAR*)sDotBuff ) ;
- if( !p_Cache[i].hHz )
- return 0 ;
- p_Cache[i].pPrior =&(p_Cache[i-1]) ;
- p_Cache[i-1].pNext =&(p_Cache[i]) ;
- }
- p_pLastCache =&(p_Cache[CACHE_HZ_NUM-1]) ;
- p_Cache[CACHE_HZ_NUM-1].pNext =p_pFirstCache ;
- p_Cache[0].pPrior =p_pLastCache ;
-
- return 1 ;
- }
- //结束Cache
- void ExitCache( void )
- {
- for( int i=0 ; i<CACHE_HZ_NUM ; i++ )
- {
- DeleteObject( p_pFirstCache->hHz ) ;
- p_pFirstCache =p_pFirstCache->pNext ;
- }
- }
- //得到一个汉字的位图句柄
- HBITMAP GetOneCache( int bCode , BYTE b1 , BYTE b2 )
- {
- CACHE_ITEM *pItem ;
- pItem =p_pFirstCache ;
- for( int i=0 ; i<CACHE_HZ_NUM ; i++ )
- {
- if( bCode==pItem->bCode &&
- b1==pItem->b1 && b2==pItem->b2 )
- return pItem->hHz ;
- pItem =pItem->pNext ;
- }
- //找不着,以这个汉字冲掉最后一个汉字
- p_pLastCache->bCode =bCode ;
- p_pLastCache->b1 =b1 ;
- p_pLastCache->b2 =b2 ;
- DeleteObject( p_pLastCache->hHz ) ; //删除原来的
- GetDotMatrix( p_nOutputCode , b1 , b2 ) ;
- p_pLastCache->hHz =::CreateBitmap( 16 , 16 , 1 , 1 ,
- (const void FAR*)sDotBuff ) ;
- p_pFirstCache =p_pLastCache ;
- p_pLastCache =p_pLastCache->pPrior ;
-
- return p_pFirstCache->hHz ;
- }
- */