Debian Codegraph @

texk/makejvf/tfmread.c

raw
1 #include <kpathsea/kpathsea.h>
2 #include "makejvf.h"
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 int nt,nl,unit,zh,zw,jfm_id,rightamount;
9 int *width,*height,*depth,*italic,*kern,*glue,*param;
10 unsigned char *header,*char_type,*char_info,*glue_kern;
11
12 int jfmread(int kcode)
13 {
14 int i,ctype=0,w_ind,w,ll=0,rr=0,tag,gk_ind,gk2_ind;
15
16 for (i = 0 ; i < nt ; i++) {
17 /* support new JFM spec by texjporg */
18 if (upair(&char_type[i*4])+char_type[i*4+2]*65536 == kcode) {
19 ctype = char_type[i*4+3];
20 break;
21 }
22 }
23
24 /* get character width of <kcode> */
25 w_ind = char_info[ctype*4];
26 w = width[w_ind];
27
28 rightamount = 0;
29 if (enhanced && w != zw && ctype > 0) {
30 /* get natural length of JFM glue between <type0> and <type of kcode> */
31 tag = char_info[0*4+2] % 4;
32 if (tag == 1) {
33 gk_ind = char_info[0*4+3]; /* remainder for <type0> */
34 if (glue_kern[gk_ind*4] > 128) /* huge gluekern table rearranged */
35 gk_ind = upair(&glue_kern[gk_ind*4+2]);
36 for (i = 0 ; i < nl-gk_ind ; i++) {
37 /* if rearrangement already handled ... */
38 if (glue_kern[(gk_ind+i)*4] > 128) break; /* ... skip loop */
39 if (glue_kern[(gk_ind+i)*4+1] == ctype) {
40 if (glue_kern[(gk_ind+i)*4+2] >= 128) {
41 gk2_ind = (glue_kern[(gk_ind+i)*4+2]-128)*256+glue_kern[(gk_ind+i)*4+3];
42 ll = kern[gk2_ind];
43 }
44 else {
45 gk2_ind = glue_kern[(gk_ind+i)*4+2]*256+glue_kern[(gk_ind+i)*4+3];
46 ll = glue[3*gk2_ind];
47 }
48 break;
49 }
50 if (glue_kern[(gk_ind+i)*4] >= 128) /* end of program */
51 break;
52 else /* SKIP */
53 i += glue_kern[(gk_ind+i)*4];
54 }
55 }
56 /* get natural length of JFM glue between <type of kcode> and <type0> */
57 tag = char_info[ctype*4+2] % 4;
58 if (tag == 1) {
59 gk_ind = char_info[ctype*4+3]; /* remainder for <type of kcode> */
60 if (glue_kern[gk_ind*4] > 128) /* huge gluekern table rearranged */
61 gk_ind = upair(&glue_kern[gk_ind*4+2]);
62 for (i = 0 ; i < nl-gk_ind ; i++) {
63 /* if rearrangement already handled ... */
64 if (glue_kern[(gk_ind+i)*4] > 128) break; /* ... skip loop */
65 if (glue_kern[(gk_ind+i)*4+1] == 0) {
66 if (glue_kern[(gk_ind+i)*4+2] >= 128) {
67 gk2_ind = (glue_kern[(gk_ind+i)*4+2]-128)*256+glue_kern[(gk_ind+i)*4+3];
68 rr = kern[gk2_ind];
69 }
70 else {
71 gk2_ind = glue_kern[(gk_ind+i)*4+2]*256+glue_kern[(gk_ind+i)*4+3];
72 rr = glue[3*gk2_ind];
73 }
74 break;
75 }
76 if (glue_kern[(gk_ind+i)*4] >= 128) /* end of program */
77 break;
78 else /* SKIP */
79 i += glue_kern[(gk_ind+i)*4];
80 }
81 }
82 if (abs(zw - ll - w - rr) <= 1) /* allow round-off error */
83 /* character width is truncated,
84 and metric glue/kern is inserted as a substitute to fill zenkaku */
85 rightamount = ll;
86 else
87 /* character width is actually truncated */
88 rightamount = (zw - w)/2;
89 }
90
91 return(w);
92 }
93
94 int tfmget(char *name)
95 {
96 char *nbuff;
97 FILE *fp;
98
99 nbuff = xmalloc(strlen(name)+4+1);
100 strcpy(nbuff,name);
101 strcat(nbuff,".tfm");
102 fp = fopen(nbuff,"rb");
103 if (fp == NULL) {
104 fp = fopen(name,"rb"); /* just in case ... */
105 if (fp == NULL) {
106 fprintf(stderr,"Cannot open %s for input.\n",nbuff);
107 exit(1);
108 }
109 }
110
111 tfmidx(fp);
112
113 fclose(fp);
114
115 free(nbuff);
116
117 return 0;
118 }
119
120 int tfmidx(FILE *fp)
121 {
122 int i;
123 int lh,ec,nw,nh,nd,ni,nk,ng,np;
124
125 jfm_id = fpair(fp);
126
127 if ((jfm_id == 9) || (jfm_id == 11)) {
128 nt = ufpair(fp);
129 fpair(fp);
130 lh = ufpair(fp);
131 fpair(fp);
132 ec = ufpair(fp);
133 nw = ufpair(fp);
134 nh = ufpair(fp);
135 nd = ufpair(fp);
136 ni = ufpair(fp);
137 nl = ufpair(fp);
138 nk = ufpair(fp);
139 ng = ufpair(fp);
140 np = ufpair(fp);
141
142 header = xmalloc(lh*4);
143 for (i = 0 ; i < lh*4 ; i++) {
144 header[i] = (char)fgetc(fp);
145 }
146 char_type = xmalloc(nt*4);
147 for (i = 0 ; i < nt*4 ; i++) {
148 char_type[i] = (char)fgetc(fp);
149 }
150 char_info = xmalloc((ec+1)*4);
151 for (i = 0 ; i < (ec+1)*4 ; i++) {
152 char_info[i] = (char)fgetc(fp);
153 }
154 width = xmalloc(nw*sizeof(int));
155 for (i = 0 ; i < nw ; i++) {
156 width[i] = fquad(fp);
157 }
158 height = xmalloc(nh*sizeof(int));
159 for (i = 0 ; i < nh ; i++) {
160 height[i] = fquad(fp);
161 }
162 depth = xmalloc(nd*sizeof(int));
163 for (i = 0 ; i < nd ; i++) {
164 depth[i] = fquad(fp);
165 }
166 italic = xmalloc(ni*sizeof(int));
167 for (i = 0 ; i < ni ; i++) {
168 italic[i] = fquad(fp);
169 }
170 glue_kern = xmalloc(nl*4);
171 for (i = 0 ; i < nl*4 ; i++) {
172 glue_kern[i] = (char)fgetc(fp);
173 }
174 kern = xmalloc(nk*sizeof(int));
175 for (i = 0 ; i < nk ; i++) {
176 kern[i] = fquad(fp);
177 }
178 glue = xmalloc(ng*sizeof(int));
179 for (i = 0 ; i < ng ; i++) {
180 glue[i] = fquad(fp);
181 }
182 param = xmalloc(np*sizeof(int));
183 for (i = 0 ; i < np ; i++) {
184 param[i] = fquad(fp);
185 }
186 unit = mquad(&header[4]);
187 zh = param[4];
188 zw = param[5];
189
190 if (baseshift)
191 baseshift = (int)(zh*baseshift/1000.0+0.5);
192 }
193 else {
194 fprintf(stderr,"This TFM is not for Japanese.\n");
195 exit(100);
196 }
197
198 return 0;
199 }