read.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* $XdotOrg: xc/programs/fonttosfnt/read.c,v 1.2 2004/04/23 19:54:32 eich Exp $ */
  2. /*
  3. Copyright (c) 2002 by Juliusz Chroboczek
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* $XdotOrg: xc/programs/fonttosfnt/read.c,v 1.2 2004/04/23 19:54:32 eich Exp $ */
  21. /* $XFree86: xc/programs/fonttosfnt/read.c,v 1.5 2003/12/19 02:05:39 dawes Exp $ */
  22. #include <stdio.h>
  23. #include <ft2build.h>
  24. #include FT_FREETYPE_H
  25. #include FT_BDF_H
  26. #include "X11/Xos.h"
  27. #include "fonttosfnt.h"
  28. #include "X11/fonts/fontenc.h"
  29. #define FLOOR2(x, y) ((x) >= 0 ? (x) / (y) : -(((-(x)) + (y) - 1) / (y)))
  30. #define CEIL2(x, y) (FLOOR2((x) + (y) - 1, (y)))
  31. #define FT_Pos_DOWN(x) (FLOOR2((x),64))
  32. #define FT_Pos_UP(x) (CEIL2((x), 64))
  33. static int ft_inited = 0;
  34. static FT_Library ft_library;
  35. static int
  36. FT_Ensure_Inited(void)
  37. {
  38. int rc;
  39. if(ft_inited)
  40. return 0;
  41. rc = FT_Init_FreeType(&ft_library);
  42. if(rc != 0)
  43. return rc;
  44. ft_inited = 1;
  45. return 0;
  46. }
  47. int
  48. readFile(char *filename, FontPtr font)
  49. {
  50. int i, j, k, index;
  51. int rc;
  52. FT_Face face;
  53. StrikePtr strike;
  54. BitmapPtr bitmap;
  55. int symbol = 0;
  56. char *encoding_name = NULL;
  57. FontMapPtr mapping = NULL;
  58. FontMapReversePtr reverse = NULL;
  59. rc = FT_Ensure_Inited();
  60. if(rc != 0)
  61. return rc;
  62. rc = FT_New_Face(ft_library, filename, 0, &face);
  63. if(rc != 0) {
  64. fprintf(stderr, "Couldn't open face %s.\n", filename);
  65. return -1;
  66. }
  67. encoding_name = faceEncoding(face);
  68. if(encoding_name == NULL) {
  69. symbol = 1;
  70. } else if(strcasecmp(encoding_name, "iso10646-1") != 0) {
  71. if(reencode_flag)
  72. mapping = FontEncMapFind(encoding_name,
  73. FONT_ENCODING_UNICODE, 0, 0, NULL);
  74. if(mapping == NULL) {
  75. symbol = 1;
  76. } else {
  77. reverse = FontMapReverse(mapping);
  78. if(reverse == NULL) {
  79. fprintf(stderr, "Couldn't reverse mapping.\n");
  80. return -1;
  81. }
  82. }
  83. }
  84. if(verbose_flag) {
  85. fprintf(stderr, "%s %s %s: %d sizes%s\n",
  86. filename, face->family_name, face->style_name,
  87. face->num_fixed_sizes,
  88. symbol ? " (symbol)" : "");
  89. }
  90. if(font->numNames == 0 && face->style_name && face->family_name) {
  91. char *full_name, *unique_name;
  92. BDF_PropertyRec prop;
  93. int rc, i;
  94. if(strcmp(face->style_name, "Regular") == 0)
  95. full_name = sprintf_alloc("%s", face->family_name);
  96. else
  97. full_name = sprintf_alloc("%s %s",
  98. face->family_name, face->style_name);
  99. /* The unique name doesn't actually need to be globally
  100. unique; it only needs to be unique among all installed fonts on a
  101. Windows system. We don't bother getting it quite right. */
  102. if(face->num_fixed_sizes <= 0)
  103. unique_name = sprintf_alloc("%s "XVENDORNAMESHORT" bitmap",
  104. full_name);
  105. else if(face->available_sizes[0].width ==
  106. face->available_sizes[0].height)
  107. unique_name = sprintf_alloc("%s "XVENDORNAMESHORT
  108. " bitmap size %d",
  109. full_name,
  110. face->available_sizes[0].height);
  111. else
  112. unique_name = sprintf_alloc("%s "XVENDORNAMESHORT
  113. " bitmap size %dx%d",
  114. full_name,
  115. face->available_sizes[0].width,
  116. face->available_sizes[0].height);
  117. font->names = malloc(10 * sizeof(FontNameEntryRec));
  118. if(font->names == NULL) {
  119. fprintf(stderr, "Couldn't allocate names.\n");
  120. return -1;
  121. }
  122. i = 0;
  123. rc = FT_Get_BDF_Property(face, "COPYRIGHT", &prop);
  124. if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) {
  125. font->names[i].nid = 0;
  126. font->names[i].size = 2 * strlen(prop.u.atom);
  127. font->names[i].value = makeUTF16((char*)prop.u.atom);
  128. i++;
  129. }
  130. font->names[i].nid = 1;
  131. font->names[i].size = 2 * strlen(face->family_name);
  132. font->names[i].value = makeUTF16(face->family_name);
  133. i++;
  134. font->names[i].nid = 2;
  135. font->names[i].size = 2 * strlen(face->style_name);
  136. font->names[i].value = makeUTF16(face->style_name);
  137. i++;
  138. font->names[i].nid = 3;
  139. font->names[i].size = 2 * strlen(unique_name);
  140. font->names[i].value = makeUTF16(unique_name);
  141. i++;
  142. font->names[i].nid = 4;
  143. font->names[i].size = 2 * strlen(full_name);
  144. font->names[i].value = makeUTF16(full_name);
  145. i++;
  146. font->names[i].nid = 5;
  147. font->names[i].size = 2 * strlen("Version 0.0");
  148. font->names[i].value = makeUTF16("Version 0.0");
  149. i++;
  150. rc = FT_Get_BDF_Property(face, "FOUNDRY", &prop);
  151. if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) {
  152. font->names[i].nid = 8;
  153. font->names[i].size = 2 * strlen(prop.u.atom);
  154. font->names[i].value = makeUTF16((char*)prop.u.atom);
  155. i++;
  156. }
  157. font->names[i].nid = 10;
  158. font->names[i].size = 2 * strlen(XVENDORNAMESHORT
  159. " converted bitmap font");
  160. font->names[i].value = makeUTF16(XVENDORNAMESHORT
  161. "X converted bitmap font");
  162. i++;
  163. #ifdef __VENDORWEBSUPPORT__
  164. font->names[i].nid = 11;
  165. font->names[i].size = 2 * strlen(__VENDORWEBSUPPORT__);
  166. font->names[i].value = makeUTF16(__VENDORWEBSUPPORT__);
  167. i++;
  168. #endif
  169. font->numNames = i;
  170. font->flags = faceFlags(face) | (symbol ? FACE_SYMBOL : 0);
  171. font->weight = faceWeight(face);
  172. font->width = faceWidth(face);
  173. font->foundry = faceFoundry(face);
  174. font->italicAngle = faceItalicAngle(face);
  175. rc = FT_Get_BDF_Property(face, "UNDERLINE_POSITION", &prop);
  176. if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_INTEGER)
  177. font->underlinePosition =
  178. (double)prop.u.integer / face->available_sizes[0].height *
  179. TWO_SIXTEENTH;
  180. else
  181. font->underlinePosition =
  182. - 1.5 / face->available_sizes[0].height * TWO_SIXTEENTH;
  183. rc = FT_Get_BDF_Property(face, "UNDERLINE_THICKNESS", &prop);
  184. if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_INTEGER)
  185. font->underlineThickness =
  186. (double)prop.u.integer / face->available_sizes[0].height *
  187. TWO_SIXTEENTH;
  188. else
  189. font->underlineThickness =
  190. 1.0 / face->available_sizes[0].height * TWO_SIXTEENTH;
  191. }
  192. if(face->num_fixed_sizes == 0) {
  193. fprintf(stderr, "No bitmaps in face.\n");
  194. return -1;
  195. }
  196. if(!symbol && !mapping)
  197. rc = FT_Select_Charmap(face, ft_encoding_unicode);
  198. else
  199. rc = FT_Select_Charmap(face, ft_encoding_none);
  200. if(rc != 0) {
  201. fprintf(stderr, "Couldn't select character map: %x.\n", rc);
  202. return -1;
  203. }
  204. for(i = 0; i < face->num_fixed_sizes; i++) {
  205. if(verbose_flag)
  206. fprintf(stderr, "size %d: %dx%d\n",
  207. i,
  208. face->available_sizes[i].x_ppem >> 6,
  209. face->available_sizes[i].y_ppem >> 6);
  210. rc = FT_Set_Pixel_Sizes(face,
  211. face->available_sizes[i].x_ppem >> 6,
  212. face->available_sizes[i].y_ppem >> 6);
  213. if(rc != 0) {
  214. fprintf(stderr, "Couldn't set size.\n");
  215. return -1;
  216. }
  217. strike = makeStrike(font,
  218. face->available_sizes[i].x_ppem >> 6,
  219. face->available_sizes[i].y_ppem >> 6);
  220. if(strike == NULL) {
  221. fprintf(stderr, "Couldn't allocate strike.\n");
  222. return -1;
  223. }
  224. for(j = 0; j < FONT_CODES; j++) {
  225. if(mapping)
  226. k = reverse->reverse(j, reverse->data);
  227. else
  228. k = j;
  229. if(k <= 0 && j != 0)
  230. continue;
  231. index = FT_Get_Char_Index(face, k);
  232. if(j != 0 && index == 0)
  233. continue;
  234. rc = FT_Load_Glyph(face, index,
  235. FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
  236. if(rc != 0) {
  237. fprintf(stderr, "Couldn't load glyph for U+%04X (%d)\n",
  238. j, index);
  239. continue;
  240. }
  241. bitmap = makeBitmap(strike, j,
  242. FT_Pos_UP(face->glyph->metrics.horiAdvance),
  243. FT_Pos_DOWN(face->glyph->metrics.horiBearingX),
  244. FT_Pos_DOWN(face->glyph->metrics.horiBearingY),
  245. face->glyph->bitmap.width,
  246. face->glyph->bitmap.rows,
  247. face->glyph->bitmap.pitch,
  248. face->glyph->bitmap.buffer,
  249. crop_flag);
  250. if(bitmap == NULL) {
  251. fprintf(stderr, "Couldn't create bitmap.\n");
  252. return -1;
  253. }
  254. }
  255. }
  256. FT_Done_Face(face);
  257. j = 0;
  258. for(i = 0; i < FONT_CODES; i++) {
  259. int found = 0;
  260. strike = font->strikes;
  261. while(strike) {
  262. bitmap = STRIKE_BITMAP(strike, i);
  263. if(bitmap) {
  264. bitmap->index = j;
  265. found = 1;
  266. } else {
  267. if(i == 0) {
  268. fprintf(stderr,
  269. "Warning: no bitmap for the undefined glyph.\n");
  270. found = 1;
  271. }
  272. }
  273. strike = strike->next;
  274. }
  275. if(found)
  276. j++;
  277. }
  278. return 0;
  279. }