t11.c 7.3 KB

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