sfnt2fon.c 39 KB

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