/tools/fontextract/fontextract.c

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