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

📄 gbtobitmap.txt

📁 linux环境下利用freetype将汉字转换成位图
💻 TXT
字号:
int utf8_to_unicode(const char* chars, int len, unsigned short *result, int len_out)
{
	unsigned short uc=0;
	int need=0, i, p=0;

	for (i=0; i<len; i++) {
		unsigned char ch = chars[i];
		if (need) {
			if ( (ch&0xc0) == 0x80 ) {
				uc = (uc << 6) | (ch & 0x3f);
				need--;
				if ( !need ) {
					if(p >= (len_out -1))
						break;
					result[p++] = uc;
				}
			} else {
				result [p++] = 0xfffd;
				need = 0;
			}
		} else {
			if ( ch < 128 ) {
				result[p++] = ch;
			} else if ( (ch&0xe0) == 0xc0 ) {
				uc = ch &0x1f;
				need = 1;
			} else if ( (ch&0xf0) == 0xe0 ) {
				uc = ch &0x0f;
				need = 2;
			}
		}
	}
	
	return p;
}

int gb_to_unicode(gchar *gb_text, unsigned short *unicode_text)
{
	int len = 0;
	int i;
	int unicode_len = 0;
	unsigned char *utf8_text; 

	len = strlen(gb_text);
        printf( "gb_text length i   s  %d\n", len );
	unicode_len = len;
        utf8_text = g_convert( gb_text, strlen(gb_text), "UTF-8", "GB18030", NULL, NULL, NULL );
	printf("gb to utf8 success!\n");
	len = strlen( utf8_text );
	printf( "text length is %d\n", len );
        len = utf8_to_unicode( utf8_text, strlen(utf8_text), unicode_text, unicode_len );
	printf("utf8 to unicode success!\n");
        printf( "unicode_text length is %d\n", len );
	for (i=0; i<len; i++){
		printf("unicode %d is %d\n", i, *(unicode_text + i));
	}
	i = wcslen((wchar_t*)unicode_text);
	printf("unicode=i=%d\n", i);
	
	return len;
}


void unicode_to_bitmap(unsigned short *unicode_text, int len, unsigned char *bitmap_point, int font_size)
{
        int i, j, n;
        FT_Library pFTLib = NULL;
        FT_Face pFTFace = NULL;
        FT_Error error = 0;
        int glyph_index = 0;
        FT_GlyphSlot slot = 0;	
	int a_bitmap_len;

	for (i=0; i<len; i++){
		printf("unicode %d is %d\n", i, *(unicode_text+i));
	}

	//make a character into ImageBuffer
	error = FT_Init_FreeType(&pFTLib);
	if (error){
		pFTLib = 0;
		printf("Init library error!\n");
	//	return -1;
	}
	else{
		printf("Init library success!\n");
	}

	error = FT_New_Face(pFTLib, "/usr/X11R6/lib/X11/fonts/TTF/gbsn00lp.ttf", 0, &pFTFace);
	printf("new face error is %d\n", error);
	
	if(error){
		printf("font success or fail donot know!\n");
	}

	error = FT_Set_Char_Size(pFTFace, font_size<<6, font_size<<6, 300, 300);// RESOLVE, RESOLVE);
	if (error){
		printf("set dot size error!\n");
	}
	a_bitmap_len = A_BITMAP_LEN;
	
	for(n=0; n<len; n++){
		glyph_index = FT_Get_Char_Index(pFTFace,*(unicode_text+n));
		error = FT_Load_Glyph(pFTFace, glyph_index, FT_LOAD_DEFAULT);
		if (error){
			printf("load glyph error!\n");
		}
		error = FT_Render_Glyph(pFTFace->glyph, FT_RENDER_MODE_NORMAL);
		
		if(error){
			printf("render glyph error!\n");
		}
		else{
			slot = pFTFace->glyph;
			printf("row is %d, width is %d, get character\n", slot->bitmap.rows, slot->bitmap.width);
				
			for (i=0; i<slot->bitmap.rows; i++){
				for (j=0; j<slot->bitmap.width; j++){
					*(bitmap_point+n*a_bitmap_len+i*FONT_DOT_RATIO(font_size)+j+BITMAP_OFFSET) = 
                                          ((255 - slot->bitmap.buffer[i*(slot->bitmap.width)+j])>220)?
						255 : (255 - slot->bitmap.buffer[i*(slot->bitmap.width)+j]);
				}
				
			}
			
					
			*(bitmap_point + n * a_bitmap_len) = slot->bitmap.width;
			*(bitmap_point + n * a_bitmap_len + 1) = slot->bitmap.rows;
			if (*(unicode_text+n) == 19968)
				*(bitmap_point + n * a_bitmap_len + 1) = slot->bitmap.rows + FONT_DOT_RATIO(font_size)/3;
			else if ((*(unicode_text+n)==8220)||(*(unicode_text+n)==8221)
                                ||(*(unicode_text+n)==8216)||(*(unicode_text+n)==8217))
				*(bitmap_point + n * a_bitmap_len + 1) = slot->bitmap.rows + FONT_DOT_RATIO(font_size)/2;
			printf("width: %d  ", *(bitmap_point + n * a_bitmap_len));
			printf("rows: %d\n", *(bitmap_point + n * a_bitmap_len + 1));
		 		
		}
		
	}
	
	FT_Done_Face(pFTFace);
	pFTFace = NULL;
	FT_Done_FreeType(pFTLib);
	pFTLib = NULL;
	printf("unicode to bitmap end!\n");
}

⌨️ 快捷键说明

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