70
|
1 /* bdfstaff Ver.2.2 -- BDF utilities
|
|
2 Copyright (C) 1992 Free Software Foundation, Inc. */
|
|
3
|
|
4 /* This file is part of Mule (MULtilingual Enhancement of GNU Emacs).
|
|
5
|
|
6 Mule is free software distributed in the form of patches to GNU Emacs.
|
|
7 You can redistribute it and/or modify
|
|
8 it under the terms of the GNU General Public License as published by
|
|
9 the Free Software Foundation; either version 1, or (at your option)
|
|
10 any later version.
|
|
11
|
|
12 Mule is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with GNU Emacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
20
|
|
21 /* 92.10.14 written by K.Handa <handa@etl.go.jp> */
|
|
22 /* 92.10.21 modified for Mule 0.9.6 with DOS support
|
|
23 by M.Higashida <manabu@sigmath.osaka-u.ac.jp> */
|
|
24 /* 92.11.3 modified for Mule Ver.0.9.7
|
|
25 by T.Matsuzawa <mzw_t@hpujisa.yhp.co.jp>
|
|
26 Support NONSPACING characters and send smaller bitmap. */
|
|
27 /* 92.11.10 modified for Mule Ver.0.9.7
|
|
28 by K.Sakaeda <saka@tomorose.trad.pfu.fujitsu.co.jp>
|
|
29 Modified for SystemV. */
|
|
30 /* 92.11.24 modified for Mule Ver.0.9.7 by K.Handa <handa@etl.go.jp>
|
|
31 Modified to reduce memory. */
|
|
32 /* 92.12.15 modified for Mule Ver.0.9.7 by K.Handa <handa@etl.go.jp>
|
|
33 Look FONTPROPERTIES. */
|
|
34 /* 93.1.13 modified for Mule Ver.0.9.7.1
|
|
35 by T.Furuhata <furuhata@fujita3.iis.u-tokyo.ac.jp>
|
|
36 Modified for AIX. */
|
|
37 /* 93.2.10 modified for Mule Ver.0.9.7.1 by K.Handa <handa@etl.go.jp>
|
|
38 In bdf_fopen(), declaration of 'path' is changed. */
|
|
39 /* 93.3.15 modified for Mule Ver.0.9.7.1 by K.Handa <handa@etl.go.jp>
|
|
40 For AIX, now we don't need "#pragma alloca". */
|
|
41 /* 93.5.7 modified for Mule Ver.0.9.8 by K.Handa <handa@etl.go.jp>
|
|
42 In bdf_load_font(), bug fixed in handling private char-set. */
|
|
43 /* 93.5.7 modified for Mule Ver.0.9.8 by K.Handa <handa@etl.go.jp>
|
|
44 In bdf_load_font(), use value of PIXEL_SIZE instead of BBH.
|
|
45 Support right-to-left character.
|
|
46 Support Big5. */
|
|
47 /* 93.7.19 modified for Mule Ver.0.9.8 by K.Handa <handa@etl.go.jp>
|
|
48 Stop including "codeconv.h". */
|
|
49 /* 94.3.9 modified for Mule Ver.1.1 by Y.Niibe <gniibe@oz.etl.go.jp>
|
|
50 In bdf_load_font(), fontp->extra->fs should not be set in emacs. */
|
|
51
|
|
52 #include <stdio.h>
|
|
53 #include <sys/param.h>
|
|
54 #include <sys/types.h>
|
|
55 #include <fcntl.h>
|
|
56 #include <sys/file.h>
|
|
57 #include "mulelib.h"
|
|
58 #ifdef USG
|
|
59 #include <string.h>
|
|
60 #else
|
|
61 #include <strings.h>
|
|
62 #endif
|
|
63
|
|
64 static int in_emacs;
|
|
65
|
|
66 char *bdf_path;
|
|
67
|
|
68 font_struct *font;
|
|
69 glyph_struct glyph;
|
|
70 static char *line, *dummy;
|
|
71 #ifndef LINE_BUF_SIZE
|
|
72 #define LINE_BUF_SIZE 256
|
|
73 #endif
|
|
74
|
|
75 bdf_reset_font(fontp)
|
|
76 font_struct *fontp;
|
|
77 {
|
|
78 int size1, size2;
|
|
79 font_extra *ext;
|
|
80
|
|
81 if (fontp->bytes == 1) size1 = size2 = 256;
|
|
82 else size1 = 0x10000, size2 = size1 / 32;
|
|
83
|
|
84 if (fontp->offset == NULL) {
|
|
85 fontp->offset = (long *)malloc((sizeof (long)) * size1);
|
|
86 bzero(fontp->offset, (sizeof (long)) * size1);
|
|
87 if (in_emacs) {
|
|
88 fontp->extra = (char **)malloc((sizeof (char *)) * size1);
|
|
89 bzero(fontp->extra, (sizeof (char *)) * size1);
|
|
90 } else {
|
|
91 fontp->extra = (char **)malloc(sizeof *ext);
|
|
92 ext = (font_extra *)(fontp->extra);
|
|
93 ext->defined = (unsigned int *)malloc((sizeof (int)) * size2);
|
|
94 }
|
|
95 }
|
|
96 if (!in_emacs) {
|
|
97 bzero(ext->defined, (sizeof (int)) * size2);
|
|
98 bzero(ext->code, (sizeof (int)) * 256);
|
|
99 bzero(ext->count, (sizeof (int)) * 256);
|
|
100 bzero(ext->new, (sizeof (char)) * 256);
|
|
101 }
|
|
102 }
|
|
103
|
|
104 bdf_init_font()
|
|
105 {
|
|
106 int i, lc;
|
|
107
|
|
108 font = (font_struct *)malloc((sizeof (font_struct)) * 128);
|
|
109 for (i = 0; i < 128; i++) {
|
|
110 font[i].fp = NULL;
|
|
111 font[i].loaded = 0;
|
|
112 font[i].offset = NULL;
|
|
113 font[i].bytes = char_type[i == 0 ? i : i + 128] < TYPE94N ? 1 : 2;
|
|
114 font[i].filename = font_name[i];
|
|
115 font[i].encoding = font_encoding[i];
|
|
116 lc = (i == 0) ? i : i | 0x80;
|
|
117 }
|
|
118 }
|
|
119
|
|
120 bdf_init_glyph(glyphp)
|
|
121 glyph_struct *glyphp;
|
|
122 {
|
|
123 glyphp->bitmap_size = 100;
|
|
124 glyphp->bitmap = (char *)malloc(glyph.bitmap_size * (sizeof (char)));
|
|
125 }
|
|
126
|
|
127 bdf_initialize(bp, _emacs)
|
|
128 char *bp;
|
|
129 int _emacs;
|
|
130 {
|
|
131 in_emacs = _emacs;
|
|
132 line = (char *)malloc(LINE_BUF_SIZE);
|
|
133 dummy = (char *)malloc(LINE_BUF_SIZE);
|
|
134 bdf_path = (bp != NULL ? bp : getenv("BDFPATH"));
|
|
135 if (bdf_path == NULL) bdf_path = BDF_PATH;
|
|
136 bdf_init_font();
|
|
137 bdf_init_glyph(&glyph);
|
|
138 }
|
|
139
|
|
140 bdf_proceed_line(fp, str)
|
|
141 FILE *fp;
|
|
142 char *str;
|
|
143 {
|
|
144 int len = strlen(str);
|
|
145 do {
|
|
146 if (fgets(line, LINE_BUF_SIZE, fp) == NULL)
|
|
147 return 0;
|
|
148 } while (strncmp(line, str, len));
|
|
149 return 1;
|
|
150 }
|
|
151
|
|
152 bdf_proceed_line2(fp, str, stop)
|
|
153 FILE *fp;
|
|
154 char *str, *stop;
|
|
155 {
|
|
156 int len1 = strlen(str), len2 = strlen(stop);
|
|
157 do {
|
|
158 if (fgets(line, LINE_BUF_SIZE, fp) == NULL
|
|
159 || !strncmp(line, stop, len2))
|
|
160 return 0;
|
|
161 } while (strncmp(line, str, len1));
|
|
162 return 1;
|
|
163 }
|
|
164
|
|
165 bdf_load_font(lc)
|
|
166 int lc;
|
|
167 {
|
|
168 font_struct *fontp = &font[lc & 0x7F];
|
|
169 int i, j, bbw, bbh, bbox, bboy;
|
|
170 unsigned int idx;
|
|
171
|
|
172 if (fontp->filename == NULL
|
|
173 || (fontp->fp = open_file(bdf_path, fontp->filename)) == NULL) {
|
|
174 if (lc == 0)
|
|
175 fatal1("Font for ASCII not found.\n");
|
|
176 warning2("Font(%d:%s) not found. Substituted by ASCII font.\n",
|
|
177 lc, fontp->filename);
|
|
178 *fontp = font[0];
|
|
179 fontp->loaded = -1;
|
|
180 return 0;
|
|
181 }
|
|
182
|
|
183 bdf_reset_font(fontp);
|
|
184
|
|
185 bdf_proceed_line(fontp->fp, "FONTBOUNDINGBOX ");
|
|
186 sscanf(line, "%s %d %d %d %d", dummy, &bbw, &bbh, &bbox, &bboy);
|
|
187 fontp->llx = bbox, fontp->lly = bboy;
|
|
188 fontp->urx = bbw + bbox, fontp->ury = bbh + bboy;
|
|
189 fontp->yoffset = 0;
|
|
190 fontp->relative_compose = 0;
|
|
191 if (!in_emacs)
|
|
192 ((font_extra *)fontp->extra)->fs = bbh;
|
|
193 if (bdf_proceed_line2(fontp->fp, "STARTPROPERTIES ", "CHARS ")) {
|
|
194 do {
|
|
195 /* If there are properties of PIXEL_SIZE, FONT_ASCENT, FONT_DESCENT,
|
|
196 we believe them rather than FONTBOUNDINGBOX.
|
|
197 In addition, we also look private properties _MULE_BASELINE_OFFSET
|
|
198 and _MULE_RELATIVE_COMPOSE. */
|
|
199 if (fgets(line, LINE_BUF_SIZE, fontp->fp) == NULL)
|
|
200 return 0;
|
|
201 if (!strncmp(line, "PIXEL_SIZE ", 11)) { /* 93.5.7 by K.Handa */
|
|
202 if (!in_emacs) { /* 94.3.9 by Y.Niibe */
|
|
203 sscanf(line, "%s %d", dummy, &i);
|
|
204 ((font_extra *)fontp->extra)->fs = i;
|
|
205 }
|
|
206 } else if (!strncmp(line, "FONT_ASCENT ", 12)) {
|
|
207 sscanf(line, "%s %d", dummy, &fontp->ury);
|
|
208 } else if (!strncmp(line, "FONT_DESCENT ", 13)) {
|
|
209 sscanf(line, "%s %d", dummy, &i);
|
|
210 fontp->lly = - i;
|
|
211 } else if (!strncmp(line, "_MULE_BASELINE_OFFSET ", 22)) {
|
|
212 sscanf(line, "%s %d", dummy, &i);
|
|
213 fontp->yoffset = - i;
|
|
214 } else if (!strncmp(line, "_MULE_RELATIVE_COMPOSE ", 23)) {
|
|
215 sscanf(line, "%s %d", dummy, &fontp->relative_compose);
|
|
216 }
|
|
217 } while (strncmp(line, "ENDPROPERTIES", 13));
|
|
218 bdf_proceed_line(fontp->fp, "CHARS ");
|
|
219 }
|
|
220 sscanf(line, "%s %d", dummy, &(fontp->chars));
|
|
221 fontp->last_offset = ftell(fontp->fp);
|
|
222 fontp->loaded = 1;
|
|
223 if (fontp->bytes == 2) {
|
|
224 fontp->yoffset
|
|
225 = (((fontp->ury + fontp->lly) - (font[0].ury + font[0].lly - font[0].yoffset * 2)) / 2)
|
|
226 * ((font_extra *)font[0].extra)->fs / ((font_extra *)fontp->extra)->fs;
|
|
227 printf("%%yoffset of %d(%d) is %d\n", lc, ((font_extra *)font[0].extra)->fs, fontp->yoffset);
|
|
228
|
|
229 }
|
|
230 return 1;
|
|
231 }
|
|
232
|
|
233 bdf_load_glyph(lc, idx, glyph)
|
|
234 int lc, idx;
|
|
235 glyph_struct *glyph;
|
|
236 {
|
|
237 font_struct *fontp = &font[lc & 0x7F];
|
|
238 int i, j;
|
|
239 int width, size;
|
|
240 int h0, h1, k;
|
|
241
|
|
242 if (fontp->fp == NULL) goto glyph_not_found;
|
|
243
|
|
244 if (fontp->offset[idx]) {
|
|
245 fseek(fontp->fp, fontp->offset[idx], 0);
|
|
246 } else {
|
|
247 fseek(fontp->fp, fontp->last_offset, 0);
|
|
248 i = 0;
|
|
249 while (fontp->chars-- > 0) {
|
|
250 bdf_proceed_line(fontp->fp, "ENCODING ");
|
|
251 sscanf(line, "%s %d", dummy, &i);
|
|
252 fontp->offset[i] = fontp->last_offset = ftell(fontp->fp);
|
|
253 if (i == idx) break;
|
|
254 }
|
|
255 if (i != idx) {
|
|
256 if (fontp->offset[idx & 0x7F7F]) {
|
|
257 fontp->offset[idx] = fontp->offset[idx & 0x7F7F];
|
|
258 fseek(fontp->fp, fontp->offset[idx], 0);
|
|
259 } else
|
|
260 goto glyph_not_found;
|
|
261 }
|
|
262 }
|
|
263
|
|
264 bdf_proceed_line(fontp->fp, "DWIDTH ");
|
|
265 sscanf(line, "%s %d", dummy, &(glyph->dwidth));
|
|
266 bdf_proceed_line(fontp->fp, "BBX ");
|
|
267 sscanf(line, "%s %d %d %d %d",
|
|
268 dummy, &(glyph->bbw), &(glyph->bbh), &(glyph->bbox), &(glyph->bboy));
|
|
269 bdf_proceed_line(fontp->fp, "BITMAP");
|
|
270 width = ((glyph->bbw + 7) / 8) * 2;
|
|
271 size = width * glyph->bbh + 1;
|
|
272 if (glyph->bitmap_size < size)
|
|
273 glyph->bitmap_size = size, glyph->bitmap = (char *)realloc(glyph->bitmap, size);
|
|
274
|
|
275 h0 = h1 = -1;
|
|
276 j = 0;
|
|
277 for ( i = 0; i < glyph->bbh; i++ ) {
|
|
278 if (!fgets(line, LINE_BUF_SIZE, fontp->fp)) goto glyph_not_found;
|
|
279 line[width] = 0;
|
|
280 for ( k = 0; k < width; k++ ) {
|
|
281 if ( line[k] != '0' ) {
|
|
282 if ( h0 < 0 ) {
|
|
283 h0 = i;
|
|
284 }
|
|
285 if ( h1 < i ) {
|
|
286 h1 = i;
|
|
287 }
|
|
288 break;
|
|
289 }
|
|
290 }
|
|
291 if ( h0 < 0 ) {
|
|
292 continue;
|
|
293 }
|
|
294 sprintf(glyph->bitmap + j, "%s", line);
|
|
295 j += width;
|
|
296 }
|
|
297 glyph->bitmap[j] = 0;
|
|
298 if ( h0 < 0 && h1 < 0 ) {
|
|
299 h0 = 0;
|
|
300 }
|
|
301 glyph->bboy += ( glyph->bbh - h1 - 1) - fontp->yoffset;
|
|
302 glyph->bbh = ( h1 - h0 + 1 );
|
|
303 glyph->bitmap_size = width * glyph->bbh + 1;
|
|
304 glyph->bitmap = (char *)realloc(glyph->bitmap, size );
|
|
305 glyph->bitmap[glyph->bitmap_size - 1] = 0;
|
|
306 return 1;
|
|
307
|
|
308 glyph_not_found:
|
|
309 warning3("Glyph of char(%d) for font(%d:%s) not found.\n",
|
|
310 idx, lc, fontp->filename);
|
|
311 return 0;
|
|
312 }
|