Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.3 KiB

  1. /*
  2. * mkwidth - Make WIDTHTABLE on ufm for Prop. DBCS device fonts.
  3. *
  4. * TODO:
  5. *
  6. * HISTORY:
  7. *
  8. * 9/4/98 yasuho Created.
  9. */
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <sys\types.h>
  15. #include <sys\stat.h>
  16. #include <windef.h>
  17. #include <wingdi.h>
  18. #include <winddi.h>
  19. #include <prntfont.h>
  20. typedef struct _GLYPHTABLE {
  21. WCHAR wCode;
  22. WORD wCount;
  23. WORD wGID;
  24. } GLYPHTABLE, *PGLYPHTABLE;
  25. DWORD ufmsize, gttsize;
  26. DWORD wtoffset;
  27. DWORD runsize;
  28. PGLYPHTABLE pGlyph;
  29. UNIFM_HDR ufm;
  30. IFIMETRICS IFI;
  31. UNI_GLYPHSETDATA gtt;
  32. WIDTHTABLE WidthTable;
  33. PWIDTHRUN pWidthRun;
  34. DWORD nWidthRun;
  35. DWORD loWidth;
  36. PWORD pWidth;
  37. WORD nWidth;
  38. static int vflag;
  39. static int Vflag;
  40. static char rbuf[256];
  41. static void usage();
  42. static void fatal(char *s);
  43. static void error(char *s);
  44. static void checkufm(char *name)
  45. {
  46. FILE *fp;
  47. struct _stat sb;
  48. if (_stat(name, &sb) < 0)
  49. fatal(name);
  50. ufmsize = sb.st_size;
  51. if ((fp = fopen(name, "rb")) == NULL)
  52. fatal(name);
  53. if (fread(&ufm, sizeof ufm, 1, fp) != 1)
  54. fatal("fread ufmhdr");
  55. if (ufm.dwSize != ufmsize)
  56. error("Invalid ufmsize");
  57. if (ufm.loWidthTable != 0)
  58. error("loWidthTable != 0");
  59. fclose(fp);
  60. }
  61. static void getgtt(char *name)
  62. {
  63. FILE *fp;
  64. WORD GID;
  65. DWORD i;
  66. struct _stat sb;
  67. GLYPHRUN run;
  68. if (_stat(name, &sb) < 0)
  69. fatal(name);
  70. gttsize = sb.st_size;
  71. if ((fp = fopen(name, "rb")) == NULL)
  72. fatal(name);
  73. if (fread(&gtt, sizeof gtt, 1, fp) != 1)
  74. fatal(name);
  75. if (gtt.dwSize != gttsize)
  76. error("Invalid gttsize");
  77. runsize = gtt.dwRunCount * sizeof(GLYPHTABLE);
  78. if ((pGlyph = malloc(runsize)) == NULL)
  79. fatal("GLYPHTABLE");
  80. if (fseek(fp, gtt.loRunOffset, 0) < 0)
  81. fatal("loRunOffset");
  82. GID = 1;
  83. if (vflag) {
  84. printf("=== GTT ===\n");
  85. printf("Code\tCount\tGLYPHID\n");
  86. printf("---\t---\t---\n");
  87. }
  88. for (i = 0; i < gtt.dwRunCount; i++) {
  89. if (fread(&run, sizeof run, 1, fp) != 1)
  90. fatal("GLYPHRUN");
  91. pGlyph[i].wCode = run.wcLow;
  92. pGlyph[i].wCount = run.wGlyphCount;
  93. pGlyph[i].wGID = GID;
  94. GID += run.wGlyphCount;
  95. if (vflag)
  96. printf("0x%04x\t%d\t%ld\n", pGlyph[i].wCode,
  97. pGlyph[i].wCount, pGlyph[i].wGID);
  98. }
  99. fclose(fp);
  100. }
  101. static WORD uni2gid(WORD code)
  102. {
  103. DWORD i;
  104. PGLYPHTABLE p;
  105. for (i = 0, p = pGlyph; i < gtt.dwRunCount; i++, p++) {
  106. if (code >= p->wCode && code < p->wCode + p->wCount)
  107. return p->wGID + (code - p->wCode);
  108. }
  109. return 0;
  110. }
  111. static void getdef(char *name)
  112. {
  113. FILE *fp;
  114. int res;
  115. WORD uni, width, count, v1, v2;
  116. WORD GID, curGID;
  117. DWORD i, j, line;
  118. PWORD p;
  119. char buf[80];
  120. if ((fp = fopen(name, "r")) == NULL)
  121. fatal(name);
  122. curGID = 0;
  123. nWidth = 0;
  124. count = 0;
  125. line = 0;
  126. while (fgets(rbuf, sizeof rbuf, fp)) {
  127. line++;
  128. if (!isxdigit(rbuf[0]))
  129. continue;
  130. if ((res = sscanf(rbuf, "%x %d %d", &uni, &v1, &v2)) != 3) {
  131. sprintf(buf, "sscanf=%d", res);
  132. error(buf);
  133. }
  134. width = Vflag ? -v2 : v1;
  135. if ((GID = uni2gid(uni)) == 0) {
  136. sprintf(buf, "Invalid code: %04x", uni);
  137. error(buf);
  138. }
  139. if (GID <= curGID) {
  140. sprintf(buf, "dup code: Line:%d: Uni=%d,GID=%d",
  141. line, uni, GID);
  142. error(buf);
  143. }
  144. if (curGID == 0 || GID != curGID + 1) {
  145. if (pWidthRun == NULL) {
  146. if ((pWidthRun = malloc(sizeof(*pWidthRun))) ==
  147. NULL)
  148. fatal("malloc pWidthRun");
  149. } else {
  150. pWidthRun[nWidthRun].wGlyphCount = count;
  151. nWidthRun++;
  152. if ((pWidthRun = realloc(pWidthRun,
  153. sizeof(*pWidthRun) * (nWidthRun + 1)))
  154. == NULL)
  155. fatal("realloc pWidthRun");
  156. }
  157. pWidthRun[nWidthRun].wStartGlyph = GID;
  158. count = 0;
  159. }
  160. if (pWidth == NULL) {
  161. if ((pWidth = malloc(sizeof(*pWidth))) == NULL)
  162. fatal("malloc pWidth");
  163. } else {
  164. if ((pWidth = realloc(pWidth, sizeof(*pWidth) *
  165. (nWidth + 1))) == NULL)
  166. fatal("realloc pWidth");
  167. }
  168. pWidth[nWidth] = width;
  169. nWidth++;
  170. count++;
  171. curGID = GID;
  172. }
  173. if (nWidth) {
  174. pWidthRun[nWidthRun].wGlyphCount = count;
  175. nWidthRun++;
  176. }
  177. if (vflag) {
  178. printf("\n=== WIDTHRUN ===\n");
  179. p = pWidth;
  180. for (i = 0; i < nWidthRun; i++) {
  181. count = pWidthRun[i].wGlyphCount;
  182. printf("Glyph=%-5d , Count=%-5d\n",
  183. pWidthRun[i].wStartGlyph, count);
  184. for (j = 0; j < count; j++)
  185. printf("\tWidth[%5d]=%d\n", j, *p++);
  186. }
  187. }
  188. fclose(fp);
  189. }
  190. static void buildufm(char *name)
  191. {
  192. FILE *fp;
  193. DWORD off, size;
  194. DWORD i;
  195. PWIDTHRUN pRun;
  196. if ((fp = fopen(name, "r+b")) == NULL)
  197. fatal(name);
  198. #if 1 // v-masatk Oct/30/98
  199. ufm.loWidthTable = 0;
  200. off = ufmsize;
  201. #else
  202. ufm.loWidthTable = off = ufmsize;
  203. #endif // if 1 else end
  204. if (fwrite(&ufm, sizeof ufm, 1, fp) != 1)
  205. fatal("fwrite ufmhdr");
  206. if (fseek(fp, ufm.loIFIMetrics, 0) < 0)
  207. fatal(name);
  208. if (fread(&IFI, sizeof IFI, 1, fp) != 1)
  209. fatal("fread IFIMETRICS");
  210. IFI.flInfo &= ~(FM_INFO_OPTICALLY_FIXED_PITCH|FM_INFO_DBCS_FIXED_PITCH);
  211. IFI.jWinPitchAndFamily |= VARIABLE_PITCH;
  212. IFI.jWinPitchAndFamily &= ~FIXED_PITCH;
  213. if (fseek(fp, ufm.loIFIMetrics, 0) < 0)
  214. fatal(name);
  215. if (fwrite(&IFI, sizeof IFI, 1, fp) != 1)
  216. fatal("fwrite IFIMETRICS");
  217. #if 0 // v-masatk Oct/30/98
  218. if (fseek(fp, off, 0) < 0)
  219. fatal(name);
  220. WidthTable.dwSize = sizeof(WidthTable) + sizeof(WIDTHRUN) *
  221. (nWidthRun - 1);
  222. WidthTable.dwRunNum = nWidthRun;
  223. size = sizeof(WidthTable) - sizeof(WIDTHRUN);
  224. if (fwrite(&WidthTable, size, 1, fp) != 1)
  225. fatal("fwrite WidthTable");
  226. off = WidthTable.dwSize;
  227. for (i = 0, pRun = pWidthRun; i < nWidthRun; i++, pRun++) {
  228. pRun->loCharWidthOffset = off;
  229. off += sizeof(*pWidth) * pRun->wGlyphCount;
  230. }
  231. if (fwrite(pWidthRun, sizeof(WIDTHRUN) * nWidthRun, 1, fp) != 1)
  232. fatal("fwrite WidthRun");
  233. if (fwrite(pWidth, sizeof(*pWidth) * nWidth, 1, fp) != 1)
  234. fatal("fwrite *pWidth");
  235. #endif // if 0
  236. }
  237. void __cdecl main(int argc, char *argv[])
  238. {
  239. argc--, argv++;
  240. for (; argc && **argv == '-'; argc--, argv++) switch (argv[0][1]) {
  241. case 'v':
  242. vflag++;
  243. break;
  244. case 'V':
  245. Vflag++;
  246. break;
  247. }
  248. #if 0 // v-masatk Oct/30/98
  249. if (argc != 3)
  250. usage();
  251. #endif // if 0
  252. checkufm(argv[0]);
  253. #if 0 // v-masatk Oct/30/98
  254. getgtt(argv[1]);
  255. getdef(argv[2]);
  256. #endif // if 0
  257. buildufm(argv[0]);
  258. exit(0);
  259. }
  260. static char Usage[] = "Usage: %s [-v][-V] ufm-file gtt-file def-file\n";
  261. static char CmdName[] = "mkwidth";
  262. static void usage()
  263. {
  264. fprintf(stderr, Usage, CmdName);
  265. exit(1);
  266. }
  267. static void fatal(char *s)
  268. {
  269. fprintf(stderr, "%s: ", CmdName);
  270. perror(s);
  271. exit(1);
  272. }
  273. static void error(char *s)
  274. {
  275. fprintf(stderr, "%s: %s\n", CmdName, s);
  276. exit(1);
  277. }