sfnt2fon.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * sfnt2fon. Bitmap-only ttf to Windows font file converter
  3. *
  4. * Copyright 2004 Huw Davies
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #ifdef HAVE_FREETYPE
  28. #ifdef HAVE_FT2BUILD_H
  29. #include <ft2build.h>
  30. #endif
  31. #include FT_FREETYPE_H
  32. #include FT_SFNT_NAMES_H
  33. #include FT_TRUETYPE_TABLES_H
  34. #include FT_TRUETYPE_TAGS_H
  35. #include "windef.h"
  36. #include "winbase.h"
  37. #include "wingdi.h"
  38. #include "basetsd.h"
  39. #include "../tools.h"
  40. #include "pshpack1.h"
  41. typedef struct
  42. {
  43. INT16 dfType;
  44. INT16 dfPoints;
  45. INT16 dfVertRes;
  46. INT16 dfHorizRes;
  47. INT16 dfAscent;
  48. INT16 dfInternalLeading;
  49. INT16 dfExternalLeading;
  50. BYTE dfItalic;
  51. BYTE dfUnderline;
  52. BYTE dfStrikeOut;
  53. INT16 dfWeight;
  54. BYTE dfCharSet;
  55. INT16 dfPixWidth;
  56. INT16 dfPixHeight;
  57. BYTE dfPitchAndFamily;
  58. INT16 dfAvgWidth;
  59. INT16 dfMaxWidth;
  60. BYTE dfFirstChar;
  61. BYTE dfLastChar;
  62. BYTE dfDefaultChar;
  63. BYTE dfBreakChar;
  64. INT16 dfWidthBytes;
  65. LONG dfDevice;
  66. LONG dfFace;
  67. LONG dfBitsPointer;
  68. LONG dfBitsOffset;
  69. BYTE dfReserved;
  70. LONG dfFlags;
  71. INT16 dfAspace;
  72. INT16 dfBspace;
  73. INT16 dfCspace;
  74. LONG dfColorPointer;
  75. LONG dfReserved1[4];
  76. } FONTINFO16;
  77. typedef struct
  78. {
  79. WORD dfVersion;
  80. DWORD dfSize;
  81. char dfCopyright[60];
  82. FONTINFO16 fi;
  83. } FNT_HEADER;
  84. typedef struct {
  85. WORD width;
  86. DWORD offset;
  87. } CHAR_TABLE_ENTRY;
  88. typedef struct {
  89. DWORD version;
  90. ULONG numSizes;
  91. } eblcHeader_t;
  92. typedef struct {
  93. CHAR ascender;
  94. CHAR descender;
  95. BYTE widthMax;
  96. CHAR caretSlopeNumerator;
  97. CHAR caretSlopeDenominator;
  98. CHAR caretOffset;
  99. CHAR minOriginSB;
  100. CHAR minAdvanceSB;
  101. CHAR maxBeforeBL;
  102. CHAR maxAfterBL;
  103. CHAR pad1;
  104. CHAR pad2;
  105. } sbitLineMetrics_t;
  106. typedef struct {
  107. ULONG indexSubTableArrayOffset;
  108. ULONG indexTableSize;
  109. ULONG numberOfIndexSubTables;
  110. ULONG colorRef;
  111. sbitLineMetrics_t hori;
  112. sbitLineMetrics_t vert;
  113. USHORT startGlyphIndex;
  114. USHORT endGlyphIndex;
  115. BYTE ppemX;
  116. BYTE ppemY;
  117. BYTE bitDepth;
  118. CHAR flags;
  119. } bitmapSizeTable_t;
  120. typedef struct
  121. {
  122. FT_Int major;
  123. FT_Int minor;
  124. FT_Int patch;
  125. } FT_Version_t;
  126. static FT_Version_t FT_Version;
  127. #include "poppack.h"
  128. unsigned char *output_buffer = NULL;
  129. size_t output_buffer_pos = 0;
  130. size_t output_buffer_size = 0;
  131. #define GET_BE_WORD(ptr) MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
  132. #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
  133. GET_BE_WORD(&((WORD *)(ptr))[0]) ))
  134. struct fontinfo
  135. {
  136. FNT_HEADER hdr;
  137. CHAR_TABLE_ENTRY dfCharTable[258];
  138. BYTE *data;
  139. };
  140. static const BYTE MZ_hdr[] =
  141. {
  142. 'M', 'Z', 0x0d, 0x01, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  143. 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  146. 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 'T', 'h',
  147. 'i', 's', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 'c', 'a', 'n', 'n', 'o',
  148. 't', ' ', 'b', 'e', ' ', 'r', 'u', 'n', ' ', 'i', 'n', ' ', 'D', 'O', 'S', ' ',
  149. 'm', 'o', 'd', 'e', 0x0d, 0x0a, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  150. };
  151. static const WCHAR encoding_1250[128] =
  152. {
  153. 0x20ac, 0x0081, 0x201a, 0x0083, 0x201e, 0x2026, 0x2020, 0x2021,
  154. 0x0088, 0x2030, 0x0160, 0x2039, 0x015a, 0x0164, 0x017d, 0x0179,
  155. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  156. 0x0098, 0x2122, 0x0161, 0x203a, 0x015b, 0x0165, 0x017e, 0x017a,
  157. 0x00a0, 0x02c7, 0x02d8, 0x0141, 0x00a4, 0x0104, 0x00a6, 0x00a7,
  158. 0x00a8, 0x00a9, 0x015e, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x017b,
  159. 0x00b0, 0x00b1, 0x02db, 0x0142, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  160. 0x00b8, 0x0105, 0x015f, 0x00bb, 0x013d, 0x02dd, 0x013e, 0x017c,
  161. 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7,
  162. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e,
  163. 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7,
  164. 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df,
  165. 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7,
  166. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f,
  167. 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7,
  168. 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9
  169. };
  170. static const WCHAR encoding_1251[128] =
  171. {
  172. 0x0402, 0x0403, 0x201a, 0x0453, 0x201e, 0x2026, 0x2020, 0x2021,
  173. 0x20ac, 0x2030, 0x0409, 0x2039, 0x040a, 0x040c, 0x040b, 0x040f,
  174. 0x0452, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  175. 0x0098, 0x2122, 0x0459, 0x203a, 0x045a, 0x045c, 0x045b, 0x045f,
  176. 0x00a0, 0x040e, 0x045e, 0x0408, 0x00a4, 0x0490, 0x00a6, 0x00a7,
  177. 0x0401, 0x00a9, 0x0404, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x0407,
  178. 0x00b0, 0x00b1, 0x0406, 0x0456, 0x0491, 0x00b5, 0x00b6, 0x00b7,
  179. 0x0451, 0x2116, 0x0454, 0x00bb, 0x0458, 0x0405, 0x0455, 0x0457,
  180. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
  181. 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
  182. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
  183. 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
  184. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
  185. 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
  186. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
  187. 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f
  188. };
  189. static const WCHAR encoding_1252[128] =
  190. {
  191. 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  192. 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x017d, 0x008f,
  193. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  194. 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x017e, 0x0178,
  195. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
  196. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  197. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  198. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
  199. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
  200. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
  201. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
  202. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
  203. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
  204. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
  205. 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
  206. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
  207. };
  208. static const WCHAR encoding_1253[128] =
  209. {
  210. 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  211. 0x0088, 0x2030, 0x008a, 0x2039, 0x008c, 0x008d, 0x008e, 0x008f,
  212. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  213. 0x0098, 0x2122, 0x009a, 0x203a, 0x009c, 0x009d, 0x009e, 0x009f,
  214. 0x00a0, 0x0385, 0x0386, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
  215. 0x00a8, 0x00a9, 0xf8f9, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x2015,
  216. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x00b5, 0x00b6, 0x00b7,
  217. 0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f,
  218. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
  219. 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f,
  220. 0x03a0, 0x03a1, 0xf8fa, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
  221. 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
  222. 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
  223. 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
  224. 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7,
  225. 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0xf8fb
  226. };
  227. static const WCHAR encoding_1254[128] =
  228. {
  229. 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  230. 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x008e, 0x008f,
  231. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  232. 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x009e, 0x0178,
  233. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
  234. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  235. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  236. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
  237. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
  238. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
  239. 0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
  240. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df,
  241. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
  242. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
  243. 0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
  244. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff
  245. };
  246. static const WCHAR encoding_1255[128] =
  247. {
  248. 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  249. 0x02c6, 0x2030, 0x008a, 0x2039, 0x008c, 0x008d, 0x008e, 0x008f,
  250. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  251. 0x02dc, 0x2122, 0x009a, 0x203a, 0x009c, 0x009d, 0x009e, 0x009f,
  252. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x20aa, 0x00a5, 0x00a6, 0x00a7,
  253. 0x00a8, 0x00a9, 0x00d7, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  254. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  255. 0x00b8, 0x00b9, 0x00f7, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
  256. 0x05b0, 0x05b1, 0x05b2, 0x05b3, 0x05b4, 0x05b5, 0x05b6, 0x05b7,
  257. 0x05b8, 0x05b9, 0x05ba, 0x05bb, 0x05bc, 0x05bd, 0x05be, 0x05bf,
  258. 0x05c0, 0x05c1, 0x05c2, 0x05c3, 0x05f0, 0x05f1, 0x05f2, 0x05f3,
  259. 0x05f4, 0xf88d, 0xf88e, 0xf88f, 0xf890, 0xf891, 0xf892, 0xf893,
  260. 0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7,
  261. 0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df,
  262. 0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7,
  263. 0x05e8, 0x05e9, 0x05ea, 0xf894, 0xf895, 0x200e, 0x200f, 0xf896
  264. };
  265. static const WCHAR encoding_1256[128] =
  266. {
  267. 0x20ac, 0x067e, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  268. 0x02c6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
  269. 0x06af, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  270. 0x06a9, 0x2122, 0x0691, 0x203a, 0x0153, 0x200c, 0x200d, 0x06ba,
  271. 0x00a0, 0x060c, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
  272. 0x00a8, 0x00a9, 0x06be, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  273. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  274. 0x00b8, 0x00b9, 0x061b, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x061f,
  275. 0x06c1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
  276. 0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f,
  277. 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00d7,
  278. 0x0637, 0x0638, 0x0639, 0x063a, 0x0640, 0x0641, 0x0642, 0x0643,
  279. 0x00e0, 0x0644, 0x00e2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00e7,
  280. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x0649, 0x064a, 0x00ee, 0x00ef,
  281. 0x064b, 0x064c, 0x064d, 0x064e, 0x00f4, 0x064f, 0x0650, 0x00f7,
  282. 0x0651, 0x00f9, 0x0652, 0x00fb, 0x00fc, 0x200e, 0x200f, 0x06d2
  283. };
  284. static const WCHAR encoding_1257[128] =
  285. {
  286. 0x20ac, 0x0081, 0x201a, 0x0083, 0x201e, 0x2026, 0x2020, 0x2021,
  287. 0x0088, 0x2030, 0x008a, 0x2039, 0x008c, 0x00a8, 0x02c7, 0x00b8,
  288. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  289. 0x0098, 0x2122, 0x009a, 0x203a, 0x009c, 0x00af, 0x02db, 0x009f,
  290. 0x00a0, 0xf8fc, 0x00a2, 0x00a3, 0x00a4, 0xf8fd, 0x00a6, 0x00a7,
  291. 0x00d8, 0x00a9, 0x0156, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00c6,
  292. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  293. 0x00f8, 0x00b9, 0x0157, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00e6,
  294. 0x0104, 0x012e, 0x0100, 0x0106, 0x00c4, 0x00c5, 0x0118, 0x0112,
  295. 0x010c, 0x00c9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012a, 0x013b,
  296. 0x0160, 0x0143, 0x0145, 0x00d3, 0x014c, 0x00d5, 0x00d6, 0x00d7,
  297. 0x0172, 0x0141, 0x015a, 0x016a, 0x00dc, 0x017b, 0x017d, 0x00df,
  298. 0x0105, 0x012f, 0x0101, 0x0107, 0x00e4, 0x00e5, 0x0119, 0x0113,
  299. 0x010d, 0x00e9, 0x017a, 0x0117, 0x0123, 0x0137, 0x012b, 0x013c,
  300. 0x0161, 0x0144, 0x0146, 0x00f3, 0x014d, 0x00f5, 0x00f6, 0x00f7,
  301. 0x0173, 0x0142, 0x015b, 0x016b, 0x00fc, 0x017c, 0x017e, 0x02d9
  302. };
  303. static const WCHAR encoding_874[128] =
  304. {
  305. 0x20ac, 0x0081, 0x0082, 0x0083, 0x0084, 0x2026, 0x0086, 0x0087,
  306. 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
  307. 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  308. 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
  309. 0x00a0, 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07,
  310. 0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f,
  311. 0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17,
  312. 0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f,
  313. 0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27,
  314. 0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f,
  315. 0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37,
  316. 0x0e38, 0x0e39, 0x0e3a, 0xf8c1, 0xf8c2, 0xf8c3, 0xf8c4, 0x0e3f,
  317. 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47,
  318. 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0e4e, 0x0e4f,
  319. 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57,
  320. 0x0e58, 0x0e59, 0x0e5a, 0x0e5b, 0xf8c5, 0xf8c6, 0xf8c7, 0xf8c8
  321. };
  322. static const struct { int codepage; const WCHAR *table; } encodings[] =
  323. {
  324. { 874, encoding_874 },
  325. { 1250, encoding_1250 },
  326. { 1251, encoding_1251 },
  327. { 1252, encoding_1252 },
  328. { 1253, encoding_1253 },
  329. { 1254, encoding_1254 },
  330. { 1255, encoding_1255 },
  331. { 1256, encoding_1256 },
  332. { 1257, encoding_1257 },
  333. { 0, encoding_1252 }, /* default encoding */
  334. };
  335. static int option_defchar = ' ';
  336. static int option_dpi = 96;
  337. static int option_fnt_mode = 0;
  338. static int option_quiet = 0;
  339. static const char *output_name;
  340. static FT_Library ft_library;
  341. static const char *argv0;
  342. static void usage(void)
  343. {
  344. fprintf(stderr, "%s [options] input.ttf ppem,enc,avg_width ...\n", argv0);
  345. fprintf(stderr, "Options:\n");
  346. fprintf(stderr, " -h Display help\n" );
  347. fprintf(stderr, " -d char Set the font default char\n" );
  348. fprintf(stderr, " -o file Set output file name\n" );
  349. fprintf(stderr, " -q Quiet mode\n" );
  350. fprintf(stderr, " -r dpi Set resolution in DPI (default: 96)\n" );
  351. fprintf(stderr, " -s Single .fnt file mode\n" );
  352. }
  353. /* atexit handler to cleanup files */
  354. static void cleanup(void)
  355. {
  356. if (output_name) unlink( output_name );
  357. }
  358. static void exit_on_signal( int sig )
  359. {
  360. exit(1); /* this will call the atexit functions */
  361. }
  362. static void error(const char *s, ...) __attribute__((format (printf, 1, 2)));
  363. static void error(const char *s, ...)
  364. {
  365. va_list ap;
  366. va_start(ap, s);
  367. fprintf(stderr, "Error: ");
  368. vfprintf(stderr, s, ap);
  369. va_end(ap);
  370. exit(1);
  371. }
  372. static const char *get_face_name( const struct fontinfo *info )
  373. {
  374. return (const char *)info->data + info->hdr.fi.dfFace - info->hdr.fi.dfBitsOffset;
  375. }
  376. static int lookup_charset(int enc)
  377. {
  378. /* FIXME: make winelib app and use TranslateCharsetInfo */
  379. switch(enc) {
  380. case 1250:
  381. return EE_CHARSET;
  382. case 1251:
  383. return RUSSIAN_CHARSET;
  384. case 1252:
  385. return ANSI_CHARSET;
  386. case 1253:
  387. return GREEK_CHARSET;
  388. case 1254:
  389. return TURKISH_CHARSET;
  390. case 1255:
  391. return HEBREW_CHARSET;
  392. case 1256:
  393. return ARABIC_CHARSET;
  394. case 1257:
  395. return BALTIC_CHARSET;
  396. case 1258:
  397. return VIETNAMESE_CHARSET;
  398. case 437:
  399. case 737:
  400. case 775:
  401. case 850:
  402. case 852:
  403. case 855:
  404. case 857:
  405. case 860:
  406. case 861:
  407. case 862:
  408. case 863:
  409. case 864:
  410. case 865:
  411. case 866:
  412. case 869:
  413. return OEM_CHARSET;
  414. case 874:
  415. return THAI_CHARSET;
  416. case 932:
  417. return SHIFTJIS_CHARSET;
  418. case 936:
  419. return GB2312_CHARSET;
  420. case 949:
  421. return HANGUL_CHARSET;
  422. case 950:
  423. return CHINESEBIG5_CHARSET;
  424. }
  425. fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
  426. return OEM_CHARSET;
  427. }
  428. static void get_char_table(int enc, WCHAR tableW[0x100])
  429. {
  430. unsigned int i;
  431. for (i = 0; i < 128; i++) tableW[i] = i;
  432. for (i = 0; encodings[i].codepage; i++) if (encodings[i].codepage == enc) break;
  433. memcpy( tableW + 128, encodings[i].table, 128 * sizeof(WCHAR) );
  434. /* Korean has the Won sign in place of '\\' */
  435. if (enc == 949) tableW['\\'] = 0x20a9;
  436. }
  437. static struct fontinfo *fill_fontinfo( const char *face_name, int ppem, int enc, int dpi,
  438. unsigned char def_char, int avg_width )
  439. {
  440. FT_Face face;
  441. int ascent = 0, il, el, width_bytes = 0, space_size, max_width = 0;
  442. BYTE left_byte, right_byte, byte;
  443. DWORD start;
  444. int i, x, y, x_off, x_end, first_char;
  445. FT_UInt gi;
  446. int num_names;
  447. FT_SfntName sfntname;
  448. TT_OS2 *os2;
  449. FT_ULong needed;
  450. eblcHeader_t *eblc;
  451. bitmapSizeTable_t *size_table;
  452. int num_sizes;
  453. struct fontinfo *info;
  454. size_t data_pos;
  455. WCHAR table[0x100];
  456. if (FT_New_Face(ft_library, face_name, 0, &face)) error( "Cannot open face %s\n", face_name );
  457. if (FT_Set_Pixel_Sizes(face, ppem, ppem)) error( "cannot set face size to %u\n", ppem );
  458. assert( face->size->metrics.y_ppem == ppem );
  459. get_char_table( enc, table );
  460. needed = 0;
  461. if (FT_Load_Sfnt_Table(face, TTAG_EBLC, 0, NULL, &needed))
  462. fprintf(stderr,"Can't find EBLC table\n");
  463. else
  464. {
  465. eblc = xmalloc(needed);
  466. FT_Load_Sfnt_Table(face, TTAG_EBLC, 0, (FT_Byte *)eblc, &needed);
  467. num_sizes = GET_BE_DWORD(&eblc->numSizes);
  468. size_table = (bitmapSizeTable_t *)(eblc + 1);
  469. for(i = 0; i < num_sizes; i++)
  470. {
  471. if( (signed char)size_table->hori.ascender - (signed char)size_table->hori.descender == ppem)
  472. {
  473. ascent = size_table->hori.ascender;
  474. break;
  475. }
  476. size_table++;
  477. }
  478. free(eblc);
  479. }
  480. /* Versions of fontforge prior to early 2006 have incorrect
  481. ascender values in the eblc table, so we won't find the
  482. correct bitmapSizeTable. In this case use the height of
  483. the Aring glyph instead. */
  484. if(ascent == 0)
  485. {
  486. if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT))
  487. error("Can't find Aring\n");
  488. ascent = face->glyph->metrics.horiBearingY >> 6;
  489. }
  490. start = sizeof(FNT_HEADER);
  491. if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT))
  492. error("Can't find M\n");
  493. il = ascent - (face->glyph->metrics.height >> 6);
  494. /* Hack: Courier has no internal leading, nor do any Chinese or Japanese fonts */
  495. if(!strcmp(face->family_name, "Courier") || enc == 936 || enc == 950 || enc == 932)
  496. il = 0;
  497. else if (!strcmp(face->family_name, "Fixedsys"))
  498. il = 3;
  499. /* Japanese System font has an external leading */
  500. if (!strcmp(face->family_name, "System") && enc == 932)
  501. el = 2;
  502. else
  503. el = 0;
  504. first_char = FT_Get_First_Char(face, &gi);
  505. if(first_char < 0x20) /* Ignore glyphs below 0x20 */
  506. first_char = 0x20; /* FT_Get_Next_Char for some reason returns too high
  507. number in this case */
  508. info = calloc( 1, sizeof(*info) );
  509. info->hdr.fi.dfFirstChar = first_char;
  510. info->hdr.fi.dfLastChar = 0xff;
  511. start += ((unsigned char)info->hdr.fi.dfLastChar - (unsigned char)info->hdr.fi.dfFirstChar + 3 ) * sizeof(*info->dfCharTable);
  512. num_names = FT_Get_Sfnt_Name_Count(face);
  513. for(i = 0; i <num_names; i++) {
  514. FT_Get_Sfnt_Name(face, i, &sfntname);
  515. if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 &&
  516. sfntname.language_id == 0 && sfntname.name_id == 0) {
  517. size_t len = min( sfntname.string_len, sizeof(info->hdr.dfCopyright)-1 );
  518. memcpy(info->hdr.dfCopyright, sfntname.string, len);
  519. info->hdr.dfCopyright[len] = 0;
  520. }
  521. }
  522. os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
  523. for(i = first_char; i < 0x100; i++) {
  524. gi = FT_Get_Char_Index(face, table[i]);
  525. if(gi == 0 && !option_quiet)
  526. fprintf(stderr, "warning: %s %u: missing glyph for char %04x\n",
  527. face->family_name, ppem, table[i]);
  528. if(FT_Load_Char(face, table[i], FT_LOAD_DEFAULT)) {
  529. fprintf(stderr, "error loading char %d - bad news!\n", i);
  530. continue;
  531. }
  532. info->dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
  533. info->dfCharTable[i].offset = start + (width_bytes * ppem);
  534. width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
  535. if(max_width < (face->glyph->metrics.horiAdvance >> 6))
  536. max_width = face->glyph->metrics.horiAdvance >> 6;
  537. }
  538. /* space */
  539. space_size = (ppem + 3) / 4;
  540. info->dfCharTable[i].width = space_size;
  541. info->dfCharTable[i].offset = start + (width_bytes * ppem);
  542. width_bytes += (space_size + 7) >> 3;
  543. /* sentinel */
  544. info->dfCharTable[++i].width = 0;
  545. info->dfCharTable[i].offset = start + (width_bytes * ppem);
  546. info->hdr.fi.dfType = 0;
  547. info->hdr.fi.dfPoints = ((ppem - il - el) * 72 + dpi/2) / dpi;
  548. info->hdr.fi.dfVertRes = dpi;
  549. info->hdr.fi.dfHorizRes = dpi;
  550. info->hdr.fi.dfAscent = ascent;
  551. info->hdr.fi.dfInternalLeading = il;
  552. info->hdr.fi.dfExternalLeading = el;
  553. info->hdr.fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
  554. info->hdr.fi.dfUnderline = 0;
  555. info->hdr.fi.dfStrikeOut = 0;
  556. info->hdr.fi.dfWeight = os2->usWeightClass;
  557. info->hdr.fi.dfCharSet = lookup_charset(enc);
  558. info->hdr.fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ? avg_width : 0;
  559. info->hdr.fi.dfPixHeight = ppem;
  560. info->hdr.fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
  561. switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
  562. case PAN_FAMILY_SCRIPT:
  563. info->hdr.fi.dfPitchAndFamily |= FF_SCRIPT;
  564. break;
  565. case PAN_FAMILY_DECORATIVE:
  566. case PAN_FAMILY_PICTORIAL:
  567. info->hdr.fi.dfPitchAndFamily |= FF_DECORATIVE;
  568. break;
  569. case PAN_FAMILY_TEXT_DISPLAY:
  570. if(info->hdr.fi.dfPitchAndFamily == 0) /* fixed */
  571. info->hdr.fi.dfPitchAndFamily = FF_MODERN;
  572. else {
  573. switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
  574. case PAN_SERIF_NORMAL_SANS:
  575. case PAN_SERIF_OBTUSE_SANS:
  576. case PAN_SERIF_PERP_SANS:
  577. info->hdr.fi.dfPitchAndFamily |= FF_SWISS;
  578. break;
  579. default:
  580. info->hdr.fi.dfPitchAndFamily |= FF_ROMAN;
  581. }
  582. }
  583. break;
  584. default:
  585. info->hdr.fi.dfPitchAndFamily |= FF_DONTCARE;
  586. }
  587. info->hdr.fi.dfAvgWidth = avg_width;
  588. info->hdr.fi.dfMaxWidth = (enc == 932) ? avg_width * 2 : max_width;
  589. info->hdr.fi.dfDefaultChar = def_char - info->hdr.fi.dfFirstChar;
  590. info->hdr.fi.dfBreakChar = ' ' - info->hdr.fi.dfFirstChar;
  591. info->hdr.fi.dfWidthBytes = (width_bytes + 1) & ~1;
  592. info->hdr.fi.dfFace = start + info->hdr.fi.dfWidthBytes * ppem;
  593. info->hdr.fi.dfBitsOffset = start;
  594. info->hdr.fi.dfFlags = 0x10; /* DFF_1COLOR */
  595. info->hdr.fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
  596. info->hdr.dfVersion = 0x300;
  597. info->hdr.dfSize = start + info->hdr.fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
  598. info->data = calloc( info->hdr.dfSize - start, 1 );
  599. data_pos = 0;
  600. for(i = first_char; i < 0x100; i++) {
  601. if(FT_Load_Char(face, table[i], FT_LOAD_DEFAULT)) {
  602. continue;
  603. }
  604. assert(info->dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
  605. for(x = 0; x < ((info->dfCharTable[i].width + 7) / 8); x++) {
  606. for(y = 0; y < ppem; y++) {
  607. if(y < ascent - face->glyph->bitmap_top ||
  608. y >= (int)face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
  609. info->data[data_pos++] = 0;
  610. continue;
  611. }
  612. x_off = face->glyph->bitmap_left / 8;
  613. x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
  614. if(x < x_off || x > x_end) {
  615. info->data[data_pos++] = 0;
  616. continue;
  617. }
  618. if(x == x_off)
  619. left_byte = 0;
  620. else
  621. left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
  622. /* On the last non-trivial output byte (x == x_end) have we got one or two input bytes */
  623. if(x == x_end && (face->glyph->bitmap_left % 8 != 0) && ((face->glyph->bitmap.width % 8 == 0) || (x != (((face->glyph->bitmap.width) & ~0x7) + face->glyph->bitmap_left) / 8)))
  624. right_byte = 0;
  625. else
  626. right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
  627. byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
  628. byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
  629. info->data[data_pos++] = byte;
  630. }
  631. }
  632. }
  633. data_pos += ((space_size + 7) / 8) * ppem;
  634. if (width_bytes & 1) data_pos += ppem;
  635. memcpy( info->data + data_pos, face->family_name, strlen( face->family_name ));
  636. data_pos += strlen( face->family_name ) + 1;
  637. assert( start + data_pos == info->hdr.dfSize );
  638. FT_Done_Face( face );
  639. return info;
  640. }
  641. static void put_fontdir( const struct fontinfo *info )
  642. {
  643. const char *name = get_face_name( info );
  644. put_word( info->hdr.dfVersion );
  645. put_dword( info->hdr.dfSize );
  646. put_data( info->hdr.dfCopyright, sizeof(info->hdr.dfCopyright) );
  647. put_word( info->hdr.fi.dfType );
  648. put_word( info->hdr.fi.dfPoints );
  649. put_word( info->hdr.fi.dfVertRes );
  650. put_word( info->hdr.fi.dfHorizRes );
  651. put_word( info->hdr.fi.dfAscent );
  652. put_word( info->hdr.fi.dfInternalLeading );
  653. put_word( info->hdr.fi.dfExternalLeading );
  654. put_byte( info->hdr.fi.dfItalic );
  655. put_byte( info->hdr.fi.dfUnderline );
  656. put_byte( info->hdr.fi.dfStrikeOut );
  657. put_word( info->hdr.fi.dfWeight );
  658. put_byte( info->hdr.fi.dfCharSet );
  659. put_word( info->hdr.fi.dfPixWidth );
  660. put_word( info->hdr.fi.dfPixHeight );
  661. put_byte( info->hdr.fi.dfPitchAndFamily );
  662. put_word( info->hdr.fi.dfAvgWidth );
  663. put_word( info->hdr.fi.dfMaxWidth );
  664. put_byte( info->hdr.fi.dfFirstChar );
  665. put_byte( info->hdr.fi.dfLastChar );
  666. put_byte( info->hdr.fi.dfDefaultChar );
  667. put_byte( info->hdr.fi.dfBreakChar );
  668. put_word( info->hdr.fi.dfWidthBytes );
  669. put_dword( info->hdr.fi.dfDevice );
  670. put_dword( info->hdr.fi.dfFace );
  671. put_dword( 0 ); /* dfReserved */
  672. put_byte( 0 ); /* szDeviceName */
  673. put_data( name, strlen(name) + 1 ); /* szFaceName */
  674. }
  675. static void put_font( const struct fontinfo *info )
  676. {
  677. int num_chars, i;
  678. put_word( info->hdr.dfVersion );
  679. put_dword( info->hdr.dfSize );
  680. put_data( info->hdr.dfCopyright, sizeof(info->hdr.dfCopyright) );
  681. put_word( info->hdr.fi.dfType );
  682. put_word( info->hdr.fi.dfPoints );
  683. put_word( info->hdr.fi.dfVertRes );
  684. put_word( info->hdr.fi.dfHorizRes );
  685. put_word( info->hdr.fi.dfAscent );
  686. put_word( info->hdr.fi.dfInternalLeading );
  687. put_word( info->hdr.fi.dfExternalLeading );
  688. put_byte( info->hdr.fi.dfItalic );
  689. put_byte( info->hdr.fi.dfUnderline );
  690. put_byte( info->hdr.fi.dfStrikeOut );
  691. put_word( info->hdr.fi.dfWeight );
  692. put_byte( info->hdr.fi.dfCharSet );
  693. put_word( info->hdr.fi.dfPixWidth );
  694. put_word( info->hdr.fi.dfPixHeight );
  695. put_byte( info->hdr.fi.dfPitchAndFamily );
  696. put_word( info->hdr.fi.dfAvgWidth );
  697. put_word( info->hdr.fi.dfMaxWidth );
  698. put_byte( info->hdr.fi.dfFirstChar );
  699. put_byte( info->hdr.fi.dfLastChar );
  700. put_byte( info->hdr.fi.dfDefaultChar );
  701. put_byte( info->hdr.fi.dfBreakChar );
  702. put_word( info->hdr.fi.dfWidthBytes );
  703. put_dword( info->hdr.fi.dfDevice );
  704. put_dword( info->hdr.fi.dfFace );
  705. put_dword( info->hdr.fi.dfBitsPointer );
  706. put_dword( info->hdr.fi.dfBitsOffset );
  707. put_byte( info->hdr.fi.dfReserved );
  708. put_dword( info->hdr.fi.dfFlags );
  709. put_word( info->hdr.fi.dfAspace );
  710. put_word( info->hdr.fi.dfBspace );
  711. put_word( info->hdr.fi.dfCspace );
  712. put_dword( info->hdr.fi.dfColorPointer );
  713. put_dword( info->hdr.fi.dfReserved1[0] );
  714. put_dword( info->hdr.fi.dfReserved1[1] );
  715. put_dword( info->hdr.fi.dfReserved1[2] );
  716. put_dword( info->hdr.fi.dfReserved1[3] );
  717. num_chars = ((unsigned char)info->hdr.fi.dfLastChar - (unsigned char)info->hdr.fi.dfFirstChar) + 3;
  718. for (i = 0; i < num_chars; i++)
  719. {
  720. put_word( info->dfCharTable[info->hdr.fi.dfFirstChar + i].width );
  721. put_dword( info->dfCharTable[info->hdr.fi.dfFirstChar + i].offset );
  722. }
  723. put_data( info->data, info->hdr.dfSize - info->hdr.fi.dfBitsOffset );
  724. }
  725. static void option_callback( int optc, char *optarg )
  726. {
  727. switch(optc)
  728. {
  729. case 'd':
  730. option_defchar = atoi( optarg );
  731. break;
  732. case 'o':
  733. output_name = xstrdup( optarg );
  734. break;
  735. case 'q':
  736. option_quiet = 1;
  737. break;
  738. case 'r':
  739. option_dpi = atoi( optarg );
  740. break;
  741. case 's':
  742. option_fnt_mode = 1;
  743. break;
  744. case 'h':
  745. usage();
  746. exit(0);
  747. case '?':
  748. fprintf( stderr, "%s: %s\n\n", argv0, optarg );
  749. usage();
  750. exit(1);
  751. }
  752. }
  753. int main(int argc, char **argv)
  754. {
  755. int i, num_files;
  756. const int typeinfo_size = 4 * sizeof(WORD);
  757. const int nameinfo_size = 6 * sizeof(WORD);
  758. int resource_table_len, non_resident_name_len, resident_name_len;
  759. unsigned short resource_table_off, resident_name_off, module_ref_off, non_resident_name_off, fontdir_off, font_off;
  760. char resident_name[200];
  761. int fontdir_len = 2;
  762. char non_resident_name[200];
  763. unsigned short first_res = 0x0050, res;
  764. struct fontinfo **info;
  765. const char *input_file;
  766. struct strarray args;
  767. argv0 = argv[0];
  768. args = parse_options( argc, argv, "d:ho:qr:s", NULL, 0, option_callback );
  769. if (!args.count)
  770. {
  771. usage();
  772. exit(1);
  773. }
  774. input_file = args.str[0];
  775. if(FT_Init_FreeType(&ft_library))
  776. error("ft init failure\n");
  777. FT_Version.major=FT_Version.minor=FT_Version.patch=-1;
  778. FT_Library_Version(ft_library,&FT_Version.major,&FT_Version.minor,&FT_Version.patch);
  779. num_files = args.count - 1;
  780. if (option_fnt_mode && num_files > 1)
  781. error( "can only specify one font in .fnt mode\n" );
  782. info = xmalloc( num_files * sizeof(*info) );
  783. for (i = 0; i < num_files; i++)
  784. {
  785. int ppem, enc, avg_width;
  786. const char *name;
  787. if (sscanf( args.str[i + 1], "%d,%d,%d", &ppem, &enc, &avg_width ) != 3)
  788. {
  789. usage();
  790. exit(1);
  791. }
  792. if (!(info[i] = fill_fontinfo( input_file, ppem, enc, option_dpi, option_defchar, avg_width )))
  793. exit(1);
  794. name = get_face_name( info[i] );
  795. fontdir_len += 0x74 + strlen(name) + 1;
  796. if(i == 0) {
  797. sprintf(non_resident_name, "FONTRES 100,%d,%d : %s %d",
  798. info[i]->hdr.fi.dfVertRes, info[i]->hdr.fi.dfHorizRes,
  799. name, info[i]->hdr.fi.dfPoints );
  800. strcpy(resident_name, name);
  801. } else {
  802. sprintf(non_resident_name + strlen(non_resident_name), ",%d", info[i]->hdr.fi.dfPoints );
  803. }
  804. }
  805. if (option_dpi <= 108)
  806. strcat(non_resident_name, " (VGA res)");
  807. else
  808. strcat(non_resident_name, " (8514 res)");
  809. non_resident_name_len = strlen(non_resident_name) + 4;
  810. /* shift count + fontdir entry + num_files of font + nul type + \007FONTDIR */
  811. resource_table_len = sizeof(WORD) /* align */ + sizeof("FONTDIR") +
  812. typeinfo_size + nameinfo_size +
  813. typeinfo_size + nameinfo_size * num_files +
  814. typeinfo_size;
  815. resource_table_off = sizeof(IMAGE_OS2_HEADER);
  816. resident_name_off = resource_table_off + resource_table_len;
  817. resident_name_len = strlen(resident_name) + 4;
  818. module_ref_off = resident_name_off + resident_name_len;
  819. non_resident_name_off = sizeof(MZ_hdr) + module_ref_off + sizeof(WORD) /* align */;
  820. fontdir_off = (non_resident_name_off + non_resident_name_len + 15) & ~0xf;
  821. font_off = (fontdir_off + fontdir_len + 15) & ~0x0f;
  822. atexit( cleanup );
  823. signal( SIGTERM, exit_on_signal );
  824. signal( SIGINT, exit_on_signal );
  825. #ifdef SIGHUP
  826. signal( SIGHUP, exit_on_signal );
  827. #endif
  828. if (!output_name) /* build a default output name */
  829. output_name = strmake( "%s%s", get_basename_noext( input_file ),
  830. option_fnt_mode ? ".fnt" : ".fon" );
  831. init_output_buffer();
  832. if (option_fnt_mode)
  833. {
  834. put_font( info[0] );
  835. goto done;
  836. }
  837. put_data(MZ_hdr, sizeof(MZ_hdr));
  838. /* NE header */
  839. put_word( 0x454e ); /* ne_magic */
  840. put_byte( 5 ); /* ne_ver */
  841. put_byte( 1 ); /* ne_rev */
  842. put_word( module_ref_off ); /* ne_enttab */
  843. put_word( 0 ); /* ne_cbenttab */
  844. put_dword( 0 ); /* ne_crc */
  845. put_word( 0x8300 ); /* ne_flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_GUI */
  846. put_word( 0 ); /* ne_autodata */
  847. put_word( 0 ); /* ne_heap */
  848. put_word( 0 ); /* ne_stack */
  849. put_dword( 0 ); /* ne_csip */
  850. put_dword( 0 ); /* ne_sssp */
  851. put_word( 0 ); /* ne_cseg */
  852. put_word( 0 ); /* ne_cmod */
  853. put_word( non_resident_name_len ); /* ne_cbnrestab */
  854. put_word( sizeof(IMAGE_OS2_HEADER) ); /* ne_segtab */
  855. put_word( sizeof(IMAGE_OS2_HEADER) ); /* ne_rsrctab */
  856. put_word( resident_name_off ); /* ne_restab */
  857. put_word( module_ref_off ); /* ne_modtab */
  858. put_word( module_ref_off ); /* ne_imptab */
  859. put_dword( non_resident_name_off ); /* ne_nrestab */
  860. put_word( 0 ); /* ne_cmovent */
  861. put_word( 4 ); /* ne_align */
  862. put_word( 0 ); /* ne_cres */
  863. put_byte( 2 ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
  864. put_byte( 0 ); /* ne_flagsothers */
  865. put_word( 0 ); /* ne_pretthunks */
  866. put_word( 0 ); /* ne_psegrefbytes */
  867. put_word( 0 ); /* ne_swaparea */
  868. put_word( 0x400 ); /* ne_expver */
  869. put_word( 4 ); /* align */
  870. /* resources */
  871. put_word( 0x8007 ); /* type_id = NE_RSCTYPE_FONTDIR */
  872. put_word( 1 ); /* count */
  873. put_dword( 0 ); /* resloader */
  874. put_word( fontdir_off >> 4 ); /* offset */
  875. put_word( (fontdir_len + 15) >> 4 ); /* length */
  876. put_word( 0x0050 ); /* flags = NE_SEGFLAGS_MOVEABLE | NE_SEGFLAGS_PRELOAD */
  877. put_word( resident_name_off - sizeof("FONTDIR") - sizeof(IMAGE_OS2_HEADER) ); /* id */
  878. put_word( 0 ); /* handle */
  879. put_word( 0 ); /* usage */
  880. put_word( 0x8008 ); /* type_id = NE_RSCTYPE_FONT */
  881. put_word( num_files ); /* count */
  882. put_dword( 0 ); /* resloader */
  883. for(res = first_res | 0x8000, i = 0; i < num_files; i++, res++) {
  884. int len = (info[i]->hdr.dfSize + 15) & ~0xf;
  885. put_word( font_off >> 4 ); /* offset */
  886. put_word( len >> 4 ); /* length */
  887. put_word( 0x1030 ); /* flags = NE_SEGFLAGS_MOVEABLE|NE_SEGFLAGS_SHAREABLE|NE_SEGFLAGS_DISCARDABLE */
  888. put_word( res ); /* id */
  889. put_word( 0 ); /* handle */
  890. put_word( 0 ); /* usage */
  891. font_off += len;
  892. }
  893. put_word( 0 ); /* type_id */
  894. put_word( 0 ); /* count */
  895. put_dword( 0 ); /* resloader */
  896. put_byte( strlen("FONTDIR") );
  897. put_data( "FONTDIR", strlen("FONTDIR") );
  898. put_byte( strlen(resident_name) );
  899. put_data( resident_name, strlen(resident_name) );
  900. put_byte( 0 );
  901. put_byte( 0 );
  902. put_byte( 0 );
  903. put_byte( 0 );
  904. put_byte( 0 );
  905. put_byte( strlen(non_resident_name) );
  906. put_data( non_resident_name, strlen(non_resident_name) );
  907. put_byte( 0 );
  908. /* empty ne_modtab and ne_imptab */
  909. put_byte( 0 );
  910. put_byte( 0 );
  911. align_output( 16 );
  912. /* FONTDIR resource */
  913. put_word( num_files );
  914. for (i = 0; i < num_files; i++)
  915. {
  916. put_word( first_res + i );
  917. put_fontdir( info[i] );
  918. }
  919. align_output( 16 );
  920. for(i = 0; i < num_files; i++)
  921. {
  922. put_font( info[i] );
  923. align_output( 16 );
  924. }
  925. done:
  926. flush_output_buffer( output_name );
  927. output_name = NULL;
  928. exit(0);
  929. }
  930. #else /* HAVE_FREETYPE */
  931. int main(int argc, char **argv)
  932. {
  933. fprintf( stderr, "%s needs to be built with FreeType support\n", argv[0] );
  934. exit(1);
  935. }
  936. #endif /* HAVE_FREETYPE */