⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 按C51格式生成汉字字模的.c及.h的windows C源码
💻 C
📖 第 1 页 / 共 3 页
字号:
		       }
		   }
		   else if( (strncmp(wordBuffer,"else",4)==0) ) {
		       if( _i_SkipAllIfMacro ) {
			     _if_ParseStatus = PARSE_MACRO_IFDEF_WAIT_ENDIF_MASK ;
		       } else
		       {
			    if( !findDefineTok(_strDefineName) ) {
				 Process_IfMacro() ;
			    }
			    else
			    {
				 _i_SkipAllIfMacro = 1 ;
			    }
			    _if_ParseStatus = PARSE_MACRO_IFDEF_WAIT_ENDIF_MASK ;
		       }
		   }
		   else if( (strncmp(wordBuffer,"elif",4)==0) ) {
			Process_IfMacro() ;
			 _if_ParseStatus = PARSE_MACRO_IFDEF_WAIT_ENDIF_MASK ;
		   }
		   else if( strncmp(wordBuffer,"define",6)==0) {
			_if_ParseStatus = PARSE_MACRO_DEFINE_MASK  ;
		   }
		   else if( strncmp(wordBuffer,"endif",5)==0) {
		       _iNest_if_macro-- ;
		       if( _iNest_if_macro == 0 ) {
			   return 1 ;
		       }
		       _if_ParseStatus = PARSE_MACRO_IFDEF_WAIT_ENDIF_MASK ;
		   }
		   else if( strncmp(wordBuffer,"include",7)==0) {
			_if_ParseStatus = PARSE_MACRO_INCLUDE_FILE_MASK;
		   }
		   break ;
	   } ;
    }
}

int ProcessConfigFile()
{
      struct _IF_MARCO_PARAM   _if_macro_param ;

   while(1) {
      if( _head_configfile[_iFileCounter].leadOfconfigBuffer >= _head_configfile[_iFileCounter].sizeOfconfigBuffer ) {
	   if( !GetConfigFile() ) return 0 ;
      }
      if( !GetWordFromConfigFile() ) {
	    return 0 ;
      }


      switch( _iParseStatus ) {
	 case PARSE_NULL :
		   if( strncmp(wordBuffer,"#",1)==0) {
		       _iParseStatus = PARSE_MACRO_MASK ;
		   }
		   break ;
	 case PARSE_MACRO_MASK :
		   if( strncmp(wordBuffer,"if",2)==0) {
			Process_IfMacro() ;
			_iParseStatus = PARSE_NULL ;
		   }
		   else if( strncmp(wordBuffer,"define",6)==0) {
			_iParseStatus = PARSE_MACRO_DEFINE_MASK ;
		   }
		   else if( strncmp(wordBuffer,"include",7)==0) {
			_iParseStatus = PARSE_MACRO_INCLUDE_FILE_MASK;
		   }
		   break ;
	  case PARSE_MACRO_INCLUDE_FILE_MASK:
		   _iFileCounter++ ;
		   ProcessFileName(wordBuffer) ;
		   _head_configfile[_iFileCounter].configFile = fopen(wordBuffer,"r");
		   _iParseStatus = PARSE_NULL ;
		   break ;
	  case PARSE_MACRO_DEFINE_MASK :
		   if( !findDefineTok(wordBuffer) ) {
			 _defineToks[defineTokLast].Value = -1 ;
			 strcpy(_defineToks[defineTokLast].Name,wordBuffer) ;
			 pLastDefineTok = &_defineToks[defineTokLast] ;
			 defineTokLast++ ;
		   }
		   _iParseStatus = PARSE_MACRO_DEFINE_VALUE_MASK ;
		   break ;
	 case PARSE_MACRO_DEFINE_VALUE_MASK :
		   if( strncmp(wordBuffer,"#",1)==0) {
		       _iParseStatus = PARSE_MACRO_MASK ;
		       break ;
		   }
		   else if( wordBuffer[0] != 0 ) {
			pLastDefineTok->Value = atoi(wordBuffer) ;
			_iParseStatus = PARSE_NULL ;
		   }
		   break ;
      }

   }
}

unsigned char _Char2Hex(char p)
{
      if( p >= '0' && p <= '9' ) {
	   return p - '0' ;
      }
      if( p >= 'A' && p <= 'F' ) {
	   return p - 'A' + 0x0A ;
      }
      if( p >= 'a' && p <= 'f' ) {
	   return p - 'a' + 0x0A ;
      }
      return -1 ;
}

#if defined(GBK_DOT_SIZE_16) || defined(GBK_DOT_SIZE_12)
long int HZK_offset(int _hzCode)
{
      int _hzHigh ;
      int _hzLow  ;
      int  X,Y,Z,M ;
      long int offset ;
      int  _hzM ;

      _hzHigh = _hzCode & 0xFF00 ;
      _hzHigh >>= 8 ;
      _hzHigh &= 0xFF ;
      _hzLow  = _hzCode & 0x00FF ;

      if( _hzLow > 0xA0 ) {
	    M = 0x5E ;
	    Y = _hzLow - 0xA1 ;
	    if( _hzHigh > 0xA0 ) {
		    X = _hzHigh - 0xA1 ;
		    Z = 0 ;
	    }
	    else {
		    X = _hzHigh - 0x81 ;
		    Z = 0x2284 ;
	    }
      } else {
	    M = 0x60 ;
	    if( _hzLow > 0x7F )
		  Y = _hzLow - 0x41 ;
	    else
		  Y = _hzLow - 0x40 ;

	    if( _hzHigh > 0xA0 ) {
		  X = _hzHigh - 0xA1 ;
		  Z = 0x3a44 ;
	    }
	    else {
		  X = _hzHigh - 0x81 ;
		  Z = 0x2e44 ;
	    }
      }

      offset = X*M + Y + Z ;
      offset *= HZK_DOT_WIDTH ;
      return offset ;
}
#endif // defined(GBK_DOT_SIZE_16)

#if defined(HZK_DOT_SIZE_16)
long int HZK_offset(int _hzCode)
{
      int _hzHigh ;
      long int offset ;

      _hzCode -= 0xA1A1 ;
      _hzHigh = _hzCode & 0xFF00 ;
      _hzHigh >>= 8 ;
      _hzHigh *= 94 ;
      _hzCode &= 0xFF ;
      _hzCode += _hzHigh ;
      offset = _hzCode ;
      offset *= HZK_DOT_WIDTH ;
      return offset ;
}
#endif // defined(HZK_DOT_SIZE_16)

#if defined(HZK_DOT_SIZE_12)
long int HZK_offset(int _hzCode)
{
      int _hzHigh ;
      long int offset ;

      _hzCode -= 0xA1A1 ;
      _hzHigh = _hzCode & 0xFF00 ;
      _hzHigh >>= 8 ;
      _hzHigh *= 94 ;
      _hzCode &= 0xFF ;
      _hzCode += _hzHigh ;
      offset = _hzCode ;
      offset *= HZK_DOT_WIDTH ;
      return offset ;
}
#endif // defined(HZK_DOT_SIZE_12)

#if defined(FREETYPE2_DOT_SIZE_9)
void OutOneHZ(FT_Face *face,unsigned int unicode,
              FILE *_c_hz_code_file,
              FILE *_h_hz_code_file
             )  // using freetype2 to get bitmap
{
    FT_UInt glyph_index;
    int row, pixel;
    int bi,bj , bMask ;
    char    _hzk_code[200] ;
    char    _buffer[128] ;
    int     _hzTemp,_hzNdx ;
    int     _hzByteSize ;
    int     _hzBitIndex ;


    glyph_index = FT_Get_Char_Index((*face), unicode);
    if(glyph_index == 0) {
        printf("freetype2 font can not found char! unicode as .%d.",unicode) ;
        return ;
    }
    if(FT_Load_Glyph((*face),glyph_index,FT_LOAD_DEFAULT)){
        printf("freetype2 font can not load glyph ! unicode as .%d.",unicode) ;
        return ;
    }
//    if(FT_Render_Glyph((*face)->glyph,FT_RENDER_MODE_MONO)){
//        printf("freetype2 font can not load render glyph ! unicode as .%d.",unicode) ;
//        return ;
//    }

            _hzByteSize = (*face)->glyph->bitmap.width / 8 ;
            if( (*face)->glyph->bitmap.width % 8 ) {
                 _hzByteSize++ ;
            }
            
            for(bi=0;bi<HZK_DOT_WIDTH;bi++) _hzk_code[bi] = 0 ;

            bi    = 0 ;
            bMask = 0x80 ;
            _hzBitIndex = 0 ;

            for(row = 0; row < (*face)->glyph->bitmap.rows; ++row){
               for(pixel = 0; pixel < (*face)->glyph->bitmap_left; ++pixel) {
                   printf("_");
                   bMask >>= 1 ;
                   if( bMask == 0 ) {
                     bMask = 0x80 ;
                     bi++ ;
                   }
                   _hzBitIndex++ ;
                   if( _hzBitIndex >= (*face)->glyph->bitmap.width ) {
                      bi++ ;
                      bMask = 0x80 ;
                      _hzBitIndex = 0 ;
                   }

               }
               for(pixel = 0; pixel < (*face)->glyph->bitmap.width; ++pixel){
                   if( (*face)->glyph->bitmap.buffer
                             [row * (*face)->glyph->bitmap.pitch +
                             pixel/8] & (0xC0 >> (pixel % 8))) {
                       _hzk_code[bi] |= bMask ;
                   }
                   bMask >>= 1 ;
                   if( bMask == 0 ) {
                        bMask = 0x80 ;
                        bi++ ;
                   }
                   _hzBitIndex++ ;
                   if( _hzBitIndex >= (*face)->glyph->bitmap.width ) {
                      bi++ ;
                      bMask = 0x80 ;
                      _hzBitIndex = 0 ;
                   }
                   //printf("%c", ((*face)->glyph->bitmap.buffer
                   //          [row * (*face)->glyph->bitmap.pitch +
                   //          pixel/8] & (0xC0 >> (pixel % 8)))?'#':'_');
               }
               //printf("\n");
            }

	      sprintf(_buffer,"{")  ;
	      fwrite(_buffer, 1, strlen(_buffer), _c_hz_code_file);
	      _hzNdx=0 ;
	      while(_hzNdx<bi) {
		      _hzTemp = _hzk_code[_hzNdx++] & 0xFF ;
		      sprintf(_buffer,"0x%2X,",_hzTemp)  ;
		      if( _buffer[2] == ' ' ) _buffer[2] = '0' ;
		      fwrite(_buffer, 1, strlen(_buffer), _c_hz_code_file);
                      // printf("%s",_buffer) ;
		      if( _hzNdx % 8 == 0 ) {
			    sprintf(_buffer,"\n ")  ;
			    fwrite(_buffer, 1, strlen(_buffer), _c_hz_code_file);
                            // printf("%s",_buffer) ;
		      }
	      }
}

/*
所谓“utf-8”只是UCS Transformation Format,只是UNICODE的一种表现形式,不等同于UNICODE,一般汉字在UNICODE中为两个(双)字节表示,而我们看到实际保存的文档确是三个字节表示一个汉字的,看看下表:

U-00000000 - U-0000007F:  0xxxxxxx
U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx

U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

UTF-8是一种变长度的表达方式,一般UNICODE为双字节(指UCS2)但为了与以前的ASCII码兼容,ASCII为一个字节,于是就想出了这种方法,在ASCII码的范围用一个字节表示,超出ASCII码的范围就用多字节表示,这就形成了我们上面看到的UTF-8的表示方法,这样的好处是当UNICODE文档中只有ASCII码时,保存的文档都为一个字节,所以就是普通的ASCII文档无异,读入的时候也是如此,所以能与以前的ASCII 文档兼容。

至于大于ASCII码的,就会由上面的第一字节的前几位表示该unicode字符的长度,比如110xxxxxx前三位的二进制表示告诉我们这是个 2BYTE的UNICODE字符;1110xxxx是个三位的UNICODE字符,依此类推,而首字节后面的字节都是以10开头,见上面这是为了与 ASCII码开头的0区分告诉我们这是个多字节UTF-8编码的后续位。看上面的编码,我们将上面的x部分重新连起来组成的数值就是实际的UNICODE 码值了(排除10组成的标志位)。

下面是个我写的从UTF-8转换到UNICODE真实值的程序,
编译方法:
gcc utf82unicode.cpp -o utf82unicode -lstdc++

使用方法:
比如一个汉字‘新’字,它的UTF-8编码为:E696B0,为了知道他的实际UNICODE编码,执行如下程序,
 ./utf82unicode E696B0
unicode: 65B0
上面程序的输出结果告诉我们UTF8:E696B0 对应UNICODE:65B0。
*/

int Utf8ToUSC2( char *src_utf8, char *dest_usc2)
{
     if( (src_utf8[0] & 0xF0) == 0xE0 ) // utf8 has 3 bytes
     {   // U-0800 - U-FFFF:  1110xxxx 10xxxxxx 10xxxxxx
          dest_usc2[0]  = (src_utf8[0] & 0x0F) << 4 ;
          dest_usc2[0] |= (src_utf8[1] & 0x3C) >> 2 ;
          dest_usc2[1]  = (src_utf8[1] & 0x03) << 6 ;
          dest_usc2[1] |= (src_utf8[2] & 0x3F) ;
          // UartDump(dest_usc2,2) ;
          return 3 ;
     }
     else if( (src_utf8[0] & 0xE0) == 0xC0 ) // utf8 has 2 bytes
     {   // U-0080 - U-07FF:  110xxxxx 10xxxxxx
          dest_usc2[1]   = (src_utf8[1] & 0x3F) ;
          dest_usc2[1]  |= (src_utf8[0] & 0x03) << 6 ;
          dest_usc2[0]   = (src_utf8[0] & 0x1C) >> 2 ;
          // UartDump(dest_usc2,2) ;
          return 2 ;
     }
     else {  // U-0000 - U-007F:  0xxxxxxx
          dest_usc2[0]  = (src_utf8[0] & 0x7F) ;
          dest_usc2[1]  = 0x00 ;
          // UartDump(dest_usc2,2) ;
          return 1 ;
     }
}

unsigned int convertUnicode(iconv_t _hzIconv,unsigned int gbkCode)
{
      char inbuf[5] ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -