t11.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "tdef.h"
  2. #include "fns.h"
  3. #include "ext.h"
  4. #define MAXCH NCHARS /* maximum number of global char names */
  5. char *chnames[MAXCH]; /* chnames[n-ALPHABET] -> name of char n */
  6. int nchnames; /* number of Cxy names currently seen */
  7. #define MAXPS 100 /* max number of point sizes */
  8. int pstab[MAXPS]; /* point sizes */
  9. int nsizes; /* number in DESC */
  10. Font fonts[MAXFONTS+1]; /* font info + ptr to width info */
  11. #define skipline(f) while (getc(f) != '\n')
  12. #define eq(s1, s2) (strcmp(s1, s2) == 0)
  13. getdesc(char *name)
  14. {
  15. FILE *fin;
  16. char cmd[100], s[100];
  17. int i, v;
  18. if ((fin = fopen(name, "r")) == NULL)
  19. return -1;
  20. while (fscanf(fin, "%s", cmd) != EOF) {
  21. if (strcmp(cmd, "res") == 0) {
  22. fscanf(fin, "%d", &Inch);
  23. } else if (strcmp(cmd, "hor") == 0) {
  24. fscanf(fin, "%d", &Hor);
  25. } else if (strcmp(cmd, "vert") == 0) {
  26. fscanf(fin, "%d", &Vert);
  27. } else if (strcmp(cmd, "unitwidth") == 0) {
  28. fscanf(fin, "%d", &Unitwidth);
  29. } else if (strcmp(cmd, "sizes") == 0) {
  30. nsizes = 0;
  31. while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
  32. pstab[nsizes++] = v;
  33. } else if (strcmp(cmd, "fonts") == 0) {
  34. fscanf(fin, "%d", &nfonts);
  35. for (i = 1; i <= nfonts; i++) {
  36. fscanf(fin, "%s", s);
  37. fontlab[i] = PAIR(s[0], s[1]);
  38. }
  39. } else if (strcmp(cmd, "charset") == 0) { /* add any names */
  40. while (fscanf(fin, "%s", s) != EOF)
  41. chadd(s, Troffchar, Install);
  42. break;
  43. }
  44. /* else
  45. just skip anything else */
  46. skipline(fin);
  47. }
  48. fclose(fin);
  49. return 1;
  50. }
  51. static int checkfont(char *name)
  52. { /* in case it's not really a font description file */
  53. /* really paranoid, but consider \f. */
  54. FILE *fp;
  55. char buf[300], buf2[300];
  56. int i, status = -1;
  57. if ((fp = fopen(name, "r")) == NULL)
  58. return -1;
  59. for (i = 1; i <= 10; i++) {
  60. if (fgets(buf, sizeof buf, fp) == NULL)
  61. break;
  62. sscanf(buf, "%s", buf2);
  63. if (buf2[0] == '#') {
  64. i--;
  65. continue;
  66. }
  67. if (eq(buf2, "name") || eq(buf2, "fontname") ||
  68. eq(buf2, "special") || eq(buf2, "charset")) {
  69. status = 1;
  70. break;
  71. }
  72. }
  73. fclose(fp);
  74. return status;
  75. }
  76. getfont(char *name, int pos) /* create width tab for font */
  77. {
  78. FILE *fin;
  79. Font *ftemp = &fonts[pos];
  80. Chwid chtemp[MAXCH];
  81. static Chwid chinit;
  82. int i, nw, n, wid, kern, code, type;
  83. char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
  84. /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
  85. if (checkfont(name) == -1)
  86. return -1;
  87. if ((fin = fopen(name, "r")) == NULL)
  88. return -1;
  89. for (i = 0; i < ALPHABET; i++)
  90. chtemp[i] = chinit; /* zero out to begin with */
  91. ftemp->specfont = ftemp->ligfont = 0;
  92. ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
  93. while (fscanf(fin, "%s", cmd) != EOF) {
  94. if (strcmp(cmd, "name") == 0)
  95. fscanf(fin, "%s", ftemp->longname);
  96. else if (strcmp(cmd, "special") == 0)
  97. ftemp->specfont = 1;
  98. else if (strcmp(cmd, "ligatures") == 0) {
  99. ftemp->ligfont = getlig(fin);
  100. } else if (strcmp(cmd, "spacewidth") == 0) {
  101. fscanf(fin, "%d", &ftemp->spacewidth);
  102. } else if (strcmp(cmd, "defaultwidth") == 0) {
  103. fscanf(fin, "%d", &ftemp->defaultwidth);
  104. } else if (strcmp(cmd, "charset") == 0) {
  105. wchar_t wc;
  106. skipline(fin);
  107. nw = ALPHABET;
  108. while (fgets(buf, sizeof buf, fin) != NULL) {
  109. sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
  110. if (s1[0] != '"') { /* genuine new character */
  111. sscanf(s1, "%d", &wid);
  112. sscanf(s2, "%d", &kern);
  113. code = strtol(s3, 0, 0); /* dec/oct/hex */
  114. }
  115. /* otherwise it's a synonym for prev character, */
  116. /* so leave previous values intact */
  117. /* decide what kind of alphabet it might come from here */
  118. if (strlen(ch) == 1) { /* it's ascii */
  119. n = ch[0]; /* origin includes non-graphics */
  120. chtemp[n].num = ch[0];
  121. } else if (ch[0] == '\\' && ch[1] == '0') {
  122. n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
  123. chtemp[n].num = n;
  124. #ifdef UNICODE
  125. } else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
  126. chtemp[nw].num = chadd(ch, MBchar, Install);
  127. n = nw;
  128. nw++;
  129. #endif /*UNICODE*/
  130. } else {
  131. if (strcmp(ch, "---") == 0) { /* no name */
  132. sprintf(ch, "%d", code);
  133. type = Number;
  134. } else
  135. type = Troffchar;
  136. chtemp[nw].num = chadd(ch, type, Install);
  137. n = nw;
  138. nw++;
  139. }
  140. chtemp[n].wid = wid;
  141. chtemp[n].kern = kern;
  142. chtemp[n].code = code;
  143. /*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
  144. ftemp->longname, ch, n, wid, code);
  145. */
  146. }
  147. break;
  148. }
  149. skipline(fin);
  150. }
  151. fclose(fin);
  152. chtemp[' '].wid = ftemp->spacewidth; /* width of space on this font */
  153. ftemp->nchars = nw;
  154. if (ftemp->wp)
  155. free(ftemp->wp); /* god help us if this wasn't allocated */
  156. ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
  157. if (ftemp->wp == NULL)
  158. return -1;
  159. for (i = 0; i < nw; i++)
  160. ftemp->wp[i] = chtemp[i];
  161. /*
  162. * printf("%d chars: ", nw);
  163. * for (i = 0; i < nw; i++)
  164. * if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
  165. * printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
  166. * else if (i >= ALPHABET)
  167. * printf("%d (%s) %d ", ftemp->wp[i].num,
  168. * chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
  169. * }
  170. * printf("\n");
  171. */
  172. return 1;
  173. }
  174. chadd(char *s, int type, int install) /* add s to global character name table; */
  175. { /* or just look it up */
  176. /* a temporary kludge: store the "type" as the first character */
  177. /* of the string, so we can remember from whence it came */
  178. char *p;
  179. int i;
  180. /* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
  181. for (i = 0; i < nchnames; i++)
  182. if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
  183. break;
  184. /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
  185. if (i < nchnames) /* found same type and bytes at position i */
  186. return ALPHABET + i;
  187. else if (install == Lookup) /* not found, and we were just looking */
  188. return -1;
  189. chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1); /* type + \0 */
  190. if (p == NULL) {
  191. ERROR "out of space adding character %s", s WARN;
  192. return LEFTHAND;
  193. }
  194. if (nchnames >= NCHARS - ALPHABET) {
  195. ERROR "out of table space adding character %s", s WARN;
  196. return LEFTHAND;
  197. }
  198. strcpy(chnames[nchnames]+1, s);
  199. chnames[nchnames][0] = type;
  200. /* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
  201. return nchnames++ + ALPHABET;
  202. }
  203. char *chname(int n) /* return string for char with index n */
  204. { /* includes type char at front, to be peeled off elsewhere */
  205. if (n >= ALPHABET && n < nchnames + ALPHABET)
  206. return chnames[n-ALPHABET];
  207. else
  208. return "";
  209. }
  210. getlig(FILE *fin) /* pick up ligature list */
  211. {
  212. int lig;
  213. char temp[200];
  214. lig = 0;
  215. while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
  216. if (strcmp(temp, "fi") == 0)
  217. lig |= LFI;
  218. else if (strcmp(temp, "fl") == 0)
  219. lig |= LFL;
  220. else if (strcmp(temp, "ff") == 0)
  221. lig |= LFF;
  222. else if (strcmp(temp, "ffi") == 0)
  223. lig |= LFFI;
  224. else if (strcmp(temp, "ffl") == 0)
  225. lig |= LFFL;
  226. else
  227. fprintf(stderr, "illegal ligature %s ignored\n", temp);
  228. }
  229. return lig;
  230. }