PageRenderTime 50ms CodeModel.GetById 36ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/fontextract/fontextract.c

http://ftk.googlecode.com/
C | 144 lines | 119 code | 25 blank | 0 comment | 21 complexity | 13d3a0023eab034ed11a146221f05c34 MD5 | raw file
  1#include <unistd.h>
  2#include <sys/types.h>
  3#include <sys/stat.h>
  4#include "fontdata.h"
  5#include <ft2build.h>
  6#include FT_GLYPH_H
  7
  8void show_usage(int argc, char* argv[])
  9{
 10	if(argc != 5)
 11	{
 12		printf("%s fontfile size input_file output_file\n", argv[0]);
 13		printf("input_file: contains the chars you need.\n");
 14		printf("output_file: contains the final fonts\n");
 15
 16		exit(0);
 17	}
 18
 19	return;
 20}
 21
 22void verify_font_data(FT_Face face, FILE* fp, const char* font_data_file)
 23{
 24	Glyph glyph = {0};
 25	unsigned short ch = 0;
 26	FontData* data = font_data_load_file(font_data_file);
 27
 28	printf("verifing...\n");
 29	fseek(fp, SEEK_SET, 0);
 30	while(fread(&ch, 1, sizeof(ch), fp) > 0 && data != NULL)
 31	{
 32		int index = FT_Get_Char_Index(face, ch);
 33		FT_Error err = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_RENDER);
 34
 35		if(err == 0)
 36		{
 37			printf("code=%04x width=%d height=%d\n", ch, face->glyph->bitmap.width, face->glyph->bitmap.rows);
 38
 39			memset(&glyph, 0x00, sizeof(Glyph));
 40			assert(font_data_get_glyph(data, ch, &glyph) == RET_OK);
 41
 42			assert(glyph.code == ch);
 43			assert(glyph.x == face->glyph->bitmap_left);
 44			assert(glyph.y == face->glyph->bitmap_top);
 45			assert(glyph.w == face->glyph->bitmap.width);
 46			assert(glyph.h == face->glyph->bitmap.rows);
 47			assert(memcmp(glyph.data, face->glyph->bitmap.buffer, glyph.w * glyph.h) == 0);
 48		}
 49		else
 50		{
 51			printf("not found code=%04x\n", ch);
 52		}
 53	}
 54
 55	return;
 56}
 57
 58void extract(FT_Face face, const char* input_file, const char* output_file, int size)
 59{
 60	Glyph glyph = {0};
 61	struct stat st = {0};
 62	FontData* data = NULL;
 63	unsigned short ch = 0;
 64	FILE* fp = fopen(input_file, "rb");
 65
 66	if(fp == NULL)
 67	{
 68		printf("open %s failed\n", input_file);
 69		return;
 70	}
 71
 72	printf("extracting...\n");
 73	stat(input_file, &st);
 74	data = font_data_create(st.st_size>>1, ENC_UTF16);
 75	font_data_set_size(data, size, size);
 76	while(fread(&ch, 1, sizeof(ch), fp) > 0 && data != NULL)
 77	{
 78		int index = FT_Get_Char_Index(face, ch);
 79		FT_Error err = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_RENDER);
 80
 81		if(err == 0)
 82		{
 83			glyph.code = ch;
 84			glyph.x = face->glyph->bitmap_left;
 85			glyph.y = face->glyph->bitmap_top;
 86			glyph.w = face->glyph->bitmap.width;
 87			glyph.h = face->glyph->bitmap.rows;
 88			glyph.data = face->glyph->bitmap.buffer;
 89			
 90			font_data_add_glyph(data, &glyph);
 91			printf("code=%04x width=%d height=%d\n", ch, face->glyph->bitmap.width, face->glyph->bitmap.rows);
 92		}
 93		else
 94		{
 95			printf("not found code=%04x\n", ch);
 96		}
 97	}
 98	font_data_save(data, output_file);
 99	font_data_destroy(data);
100	verify_font_data(face, fp, output_file);
101	fclose(fp);
102
103	return;
104}
105
106int main(int argc, char* argv[])
107{
108	int size = 0;
109	FT_Face     face = {0};
110	FT_Library  library = {0};
111	const char* input_file  = NULL;
112	const char* output_file = NULL;
113	const char* font_file   = NULL;
114	FT_Error err = FT_Init_FreeType( &library );
115
116	show_usage(argc, argv);
117
118	font_file = argv[1];
119	size = atoi(argv[2]);
120	input_file = argv[3];
121	output_file = argv[4];
122
123	if((err = FT_New_Face( library, font_file, 0, &face)))
124	{
125		printf("load %s failed.\n", font_file);
126		return 0;
127	}
128
129	err = FT_Select_Charmap( face, ft_encoding_unicode);
130	
131	if(err)
132	{
133		err = FT_Select_Charmap( face, ft_encoding_latin_1 );
134	}
135	err = FT_Set_Pixel_Sizes(face, 0, size);
136
137	extract(face, input_file, output_file, size);
138
139	FT_Done_Face( face );
140	FT_Done_FreeType( library );
141
142	return 0;
143}
144