Mercurial > hg > xemacs-beta
comparison lib-src/mule/m2ps.c @ 70:131b0175ea99 r20-0b30
Import from CVS: tag r20-0b30
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:02:59 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
69:804d1389bcd6 | 70:131b0175ea99 |
---|---|
1 /* m2ps Ver.2.2 -- Convert multilingual text to PostScript | |
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.8 written by K.Handa <handa@etl.go.jp> */ | |
22 /* 92.11.3 modified for Mule Ver.0.9.7 | |
23 by T.Matsuzawa <mzw_t@hpujisa.yhp.co.jp> | |
24 Support NONSPACING characters and send smaller bitmap. */ | |
25 /* 92.11.6 modified for Mule Ver.0.9.7 by K.Shibata <shibata@sgi.co.jp> | |
26 Modified for ANSI-C. */ | |
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.14 modified for Mule Ver.0.9.7 by K.Handa <handa@etl.go.jp> | |
33 Support composite character. */ | |
34 /* 93.5.7 modified for Mule Ver.0.9.8 by K.Handa <handa@etl.go.jp> | |
35 Bug in handling missing font fixed. | |
36 Support Big5. */ | |
37 /* 93.5.29 modified for Mule Ver.0.9.8 by Y.Niibe <gniibe@mri.co.jp> | |
38 Include config.h in src directory */ | |
39 /* 93.6.4 modified for Mule Ver.0.9.8 by K.Shibata <shibata@sgi.co.jp> | |
40 Modified for ANSI-C. */ | |
41 /* 93.12.25 modified for Mule Ver.1.0 by K.Handa <handa@etl.go.jp> | |
42 In textmode(), bug of right-to-left handling fixed */ | |
43 /* 94.3.8 modified for Mule Ver.1.1 by K.Handa <handa@etl.go.jp> | |
44 Bug in find_encoding() fixed. */ | |
45 /* See `mule/ChangeLog.Mule' for the recent change log */ | |
46 | |
47 #include <stdio.h> | |
48 #include <fcntl.h> | |
49 #include <sys/types.h> | |
50 #include <sys/file.h> | |
51 #include "mulelib.h" | |
52 | |
53 static char *m2ps_version = "2.2"; | |
54 | |
55 #ifndef PSHeader | |
56 #define PSHeader "m2ps.ps" | |
57 #endif | |
58 | |
59 #ifndef DPI | |
60 #define DPI 300 | |
61 #endif | |
62 | |
63 #ifndef FontScale | |
64 #define FontScale 10 | |
65 #endif | |
66 | |
67 #ifndef LinePitch | |
68 #define LinePitch 14 | |
69 #endif | |
70 | |
71 /* Parameters for A4 paper */ | |
72 #define A4_MaxLine 56 | |
73 #define A4_TopMargin 800 | |
74 | |
75 /* Parameters for US-LETTER paper */ | |
76 #define US_MaxLine 50 | |
77 #define US_TopMargin 746 | |
78 | |
79 #ifndef USLETTER | |
80 | |
81 #ifndef MaxLine | |
82 #define MaxLine A4_MaxLine | |
83 #endif | |
84 | |
85 #ifndef TopMargin | |
86 #define TopMargin A4_TopMargin | |
87 #endif | |
88 | |
89 #else /* US-LETTER */ | |
90 | |
91 #ifndef MaxLine | |
92 #define MaxLine US_MaxLine | |
93 #endif | |
94 | |
95 #ifndef TopMargin | |
96 #define TopMargin US_TopMargin | |
97 #endif | |
98 | |
99 #endif /* US-LETTER */ | |
100 | |
101 #ifndef LeftMargin | |
102 #define LeftMargin 30 /* To widen left margin, specify larger value. */ | |
103 #endif | |
104 | |
105 #ifndef ShortMemory | |
106 #define ShortMemory 0 | |
107 #endif | |
108 | |
109 char *psheader = PSHeader; | |
110 int dpi = DPI; | |
111 int fontscale = FontScale; | |
112 int linepitch = LinePitch; | |
113 int maxline = MaxLine; | |
114 int topmargin = TopMargin; | |
115 int leftmargin = LeftMargin; | |
116 int shortmemory = ShortMemory; | |
117 | |
118 /***************/ | |
119 /* Main staffs */ | |
120 /***************/ | |
121 | |
122 /* GLOBAL VARIABLES */ | |
123 int clm, row, current_lc; | |
124 | |
125 set_font(lc) | |
126 int lc; | |
127 { /* 93.5.7, 94.11.29 by K.Handa -- Big change */ | |
128 if (FONT_LOADED (lc) == 0) { | |
129 if (bdf_load_font(lc)) | |
130 ps_newfont(lc); | |
131 else | |
132 lc = 0; | |
133 } else if (FONT_LOADED (lc) < 0) | |
134 lc = 0; | |
135 if (lc != current_lc) { | |
136 ps_setfont(lc); | |
137 current_lc = lc; | |
138 } | |
139 } | |
140 | |
141 renew_font(lc) | |
142 int lc; | |
143 { | |
144 bzero(((font_extra *)font[lc & 0x7F].extra)->new, 256 * (sizeof (char))); | |
145 } | |
146 | |
147 /* Load specified glyph, then return the index to glyph. Note the index | |
148 is within the range of [0,255]. Return -1 if error. */ | |
149 set_glyph1(lc, c) | |
150 int lc, c; | |
151 { | |
152 if (!DEFINED1(lc, c)) { | |
153 if (bdf_load_glyph(lc, c, &glyph)) { | |
154 ps_newglyph(c, &glyph); | |
155 DEFINE1(lc, c); | |
156 } else | |
157 return -1; | |
158 } | |
159 return c; | |
160 } | |
161 | |
162 find_encoding(fontp, lc, c) | |
163 font_struct *fontp; | |
164 int lc, c; | |
165 { | |
166 font_extra *ext = (font_extra *)fontp->extra; | |
167 int i, min_count = ext->count[0], j = 0; | |
168 | |
169 if (min_count > 0) { | |
170 min_count = 0x7FFF; /* 94.3.8 by K.Handa */ | |
171 for (i = 0; i < 256; i++) { | |
172 if (ext->count[i] <= min_count && !ext->new[i]) { | |
173 min_count = ext->count[i], j = i; | |
174 if (min_count == 0) break; | |
175 } | |
176 } | |
177 } | |
178 if (min_count > 0) | |
179 UNDEFINE2(lc, ext->code[j]); | |
180 | |
181 ext->count[j] = ext->new[j] = 1; | |
182 ext->code[j] = c; | |
183 DEFINE2(lc, ext->code[j]); | |
184 return j; | |
185 } | |
186 | |
187 /* Load specified glyph, replacing previously loaded glyph if necessary, | |
188 then return the index to glyph. Note the index is within the range | |
189 of [0,255]. Return -1 if error. */ | |
190 set_glyph2(lc, c) | |
191 int lc, c; | |
192 { | |
193 int code; | |
194 font_struct *fontp = &font[lc & 0x7F]; | |
195 font_extra *ext = (font_extra *)fontp->extra; | |
196 | |
197 if (!DEFINED2(lc, c)) { | |
198 if (bdf_load_glyph(lc, c, &glyph)) { | |
199 code = find_encoding(fontp, lc, c); | |
200 ps_newglyph(code, &glyph); | |
201 } else | |
202 return -1; | |
203 } else { | |
204 for (code = 0; code < 256; code++) { | |
205 if (ext->code[code] == c) break; | |
206 } | |
207 ext->new[code] = 1; | |
208 ext->count[code]++; | |
209 } | |
210 return code; | |
211 } | |
212 | |
213 swap_buf(buf, from, to) | |
214 unsigned char *buf; | |
215 int from, to; | |
216 { | |
217 int i, j; | |
218 unsigned char buf2[1024], *p = buf2; | |
219 | |
220 i = j = to; | |
221 while (--i >= from) { | |
222 if (NONASCII_P(buf[i])) continue; | |
223 bcopy(buf + i, p, j - i); | |
224 p += j - i; | |
225 j = i; | |
226 } | |
227 bcopy(buf2, buf + from, to - from); | |
228 } | |
229 | |
230 textmode() | |
231 { | |
232 register int i, j, k, c, lc; | |
233 char buf[1024]; /* 92.11.6 by K.Shibata */ | |
234 unsigned char line[1024], *p; | |
235 int cmp_flag = 0, len, r2l_chars; | |
236 | |
237 ps_bot(); ps_bop(); | |
238 clm = row = 0; current_lc = -1; | |
239 /* set_font(0); */ | |
240 while ((len = get_line(line, 1024, stdin)) >= 0) { /* 93.6.4 by K.Shibata */ | |
241 /* We should process line by line to handle r2l direction. */ | |
242 j = 0; r2l_chars = 0; | |
243 | |
244 /* At first, re-order characters of r2l direction. */ | |
245 while (j < len) { | |
246 p = line + j; | |
247 if (*p <= 0x7F) { | |
248 lc = 0; | |
249 p++; | |
250 } else { | |
251 lc = *p++; | |
252 if (lc == LCCMP) lc = *p++ - 0x20; | |
253 if (lc >= LCPRV11) lc = *p++; | |
254 } | |
255 while (NONASCII_P(*p)) p++; | |
256 if (char_direction[lc]) { | |
257 if (!r2l_chars) k = j; | |
258 r2l_chars++; | |
259 } else { | |
260 if (r2l_chars > 1) | |
261 swap_buf(line, k, j); | |
262 r2l_chars = 0; | |
263 } | |
264 j = p - line; | |
265 } | |
266 if (r2l_chars > 1) swap_buf(line, k, j); /* 93.12.25 by K.Handa */ | |
267 | |
268 /* Then output characters. */ | |
269 for (j = 0; j < len;) { | |
270 c = line[j]; | |
271 | |
272 if (c == LCCMP) { /* start of composite characters */ | |
273 cmp_flag = 1; | |
274 j++; | |
275 continue; | |
276 } | |
277 if (cmp_flag) { /* composed characters have special encoding */ | |
278 if (c == 0xA0) c = line[++j] & 0x7F; | |
279 else if (c > 0xA0) c -= 0x20; | |
280 else cmp_flag = 0; | |
281 } | |
282 | |
283 if (c < 0x20 || c == 0x7F) { /* Control code */ | |
284 switch (c) { | |
285 case '\t': | |
286 i = 8 - (clm % 8); | |
287 clm += i; | |
288 set_font(0); | |
289 if (set_glyph1(0, ' ') >= 0) { | |
290 putchar('('); | |
291 while (i--) putchar(' '); | |
292 printf(") s "); | |
293 } | |
294 break; | |
295 case '\f': | |
296 ps_eop(), ps_bop(); | |
297 break; | |
298 default: | |
299 if (c < ' ') control_char(c); | |
300 else invalid_char(c); | |
301 } | |
302 j++; | |
303 } else if (c < 0x7F) { /* ASCII */ | |
304 set_font(0); | |
305 i = 0; | |
306 do { | |
307 if ((c = set_glyph1(0, c)) >= 0) { | |
308 if (c == '\\' || c == '(' || c == ')') | |
309 buf[i++] = '\\', buf[i++] = c; | |
310 else | |
311 buf[i++] = c; | |
312 clm++; | |
313 } | |
314 c = line[++j]; | |
315 } while (!cmp_flag /* process composite character one by one */ | |
316 && 0x20 <= c && c < 0x7F); | |
317 buf[i] = 0; | |
318 switch (cmp_flag) { | |
319 case 0: /* not composite character */ | |
320 printf("(%s) s ", buf); | |
321 break; | |
322 case 1: /* first of composite character */ | |
323 printf("(%s) cs1 ", buf); | |
324 cmp_flag = 2; | |
325 break; | |
326 default: /* case 2: rest of composite character*/ | |
327 if (font[0].relative_compose) | |
328 printf("(%s) cs2 ", buf); | |
329 else | |
330 printf("(%s) cs3 ", buf); | |
331 } | |
332 } else if (c < LCINV) { /* Multilingual character */ | |
333 int cur_lc = c < LCPRV11 ? c : line[j + 1]; | |
334 CCL_PROGRAM *ccl = x_ccl_programs[cur_lc & 0x7F]; | |
335 unsigned char c1, c2; | |
336 | |
337 set_font(cur_lc); | |
338 i = 0, lc = cur_lc; | |
339 while (1) { | |
340 if (lc >= LCPRV11) lc = line[j + 1]; | |
341 if (lc != cur_lc) | |
342 break; | |
343 if (lc < LCJPOLD) c1 = 0, c2 = line[j + 1], j += 2; | |
344 else if (lc < LCPRV11) c1 = line[j + 1], c2 = line[j + 2], j += 3; | |
345 else if (lc < LCPRV21EXT) c1 = 0, c2 = line[j + 2], j += 3; | |
346 else c1 = line[j + 2], c2 = line[j + 3], j += 4; | |
347 if (!ccl) { | |
348 c = c1 * 256 + c2; | |
349 } else { | |
350 ccl->reg[0] = c1; ccl->reg[1] = c2; | |
351 ccl_driver (ccl, NULL, NULL, 0, 0); | |
352 c = ccl->reg[0] * 256 + ccl->reg[1]; | |
353 } | |
354 if (current_lc != lc) { | |
355 /* Font is not found. Use ASCII to write the code. */ | |
356 c &= 0x7F; | |
357 if (c >= ' ' && c <= 126) | |
358 c = set_glyph1(0, c & 0x7F); | |
359 else | |
360 c = set_glyph1(0, ' '); | |
361 } else if (char_type[lc] < TYPE94N) | |
362 c = set_glyph1(lc, c); | |
363 else | |
364 c = set_glyph2(lc, c); | |
365 if (c >= 0) { | |
366 if (c == '\\' || c == '(' || c == ')') | |
367 buf[i++] = '\\', buf[i++] = c; | |
368 else if (c >= ' ' && c < 127) | |
369 buf[i++] = c; | |
370 else | |
371 sprintf(buf + i, "\\%03o", c), i += 4; | |
372 clm += lc < 0x90 || (lc >= LCPRV11EXT && lc < LCPRV21EXT) ? 1 : 2; | |
373 } | |
374 if (cmp_flag) break; | |
375 lc = line[j]; | |
376 } | |
377 buf[i] = 0; | |
378 switch (cmp_flag) { | |
379 case 0: | |
380 printf("(%s) s ", buf); | |
381 break; | |
382 case 1: | |
383 printf("(%s) cs1 ", buf); | |
384 cmp_flag = 2; | |
385 break; | |
386 case 2: | |
387 default: | |
388 if (font[lc & 0x7F].relative_compose) | |
389 printf("(%s) cs2 ", buf); | |
390 else | |
391 printf("(%s) cs3 ", buf); | |
392 } | |
393 renew_font(cur_lc); | |
394 } else { | |
395 invalid_char(c); | |
396 } | |
397 } | |
398 printf("n\n"); | |
399 row++, clm = 0; | |
400 if (row >= maxline) ps_eop(), ps_bop(); | |
401 } | |
402 | |
403 ps_eop(); | |
404 ps_eot(); | |
405 } | |
406 | |
407 control_char(c) | |
408 int c; | |
409 { | |
410 c += '@'; | |
411 set_font(0); | |
412 if ((set_glyph1(0, '^') >= 0) && (set_glyph1(0, c) >= 0)) { | |
413 printf("(^%c) s ", c); | |
414 clm += 2; | |
415 } | |
416 } | |
417 | |
418 invalid_char(c) | |
419 int c; | |
420 { | |
421 int i; | |
422 | |
423 set_font(0); | |
424 if (set_glyph1(0, '\\') >= 0) { | |
425 for (i = '0'; i <= '9'; i++) { | |
426 if (set_glyph1(0, i) < 0) break; | |
427 } | |
428 if (i > '9') { | |
429 printf("(\\\\%03o) s ", c); | |
430 clm += 4; | |
431 } | |
432 } | |
433 } | |
434 | |
435 main(argc, argv) | |
436 int argc; | |
437 char *argv[]; | |
438 { | |
439 register int i = 1; | |
440 char *bdf_path = NULL, *charsets = NULL; | |
441 | |
442 while (i < argc) { | |
443 if (!strcmp(argv[i], "-ps") && (i + 1) < argc) { | |
444 psheader = argv[i + 1]; | |
445 i += 2; | |
446 } else if (!strcmp(argv[i], "-fp") && (i + 1) < argc) { | |
447 bdf_path = argv[i + 1]; | |
448 i += 2; | |
449 } else if (!strcmp(argv[i], "-cs") && (i + 1) < argc) { | |
450 charsets = argv[i + 1]; | |
451 i += 2; | |
452 } else if (!strcmp(argv[i], "-dpi") && (i + 1) < argc) { | |
453 dpi = atoi(argv[i + 1]); | |
454 i += 2; | |
455 } else if (!strcmp(argv[i], "-fs") && (i + 1) < argc) { | |
456 fontscale = atoi(argv[i + 1]); | |
457 i += 2; | |
458 } else if (!strcmp(argv[i], "-lp") && (i + 1) < argc) { | |
459 linepitch = atoi(argv[i + 1]); | |
460 i += 2; | |
461 } else if (!strcmp(argv[i], "-ml") && (i + 1) < argc) { | |
462 maxline = atoi(argv[i + 1]); | |
463 i += 2; | |
464 } else if (!strcmp(argv[i], "-tm") && (i + 1) < argc) { | |
465 topmargin = atoi(argv[i + 1]); | |
466 i += 2; | |
467 } else if (!strcmp(argv[i], "-lm") && (i + 1) < argc) { | |
468 leftmargin = atoi(argv[i + 1]); | |
469 i += 2; | |
470 } else if (!strcmp(argv[i], "-sm")) { | |
471 shortmemory = 1; | |
472 i++; | |
473 } else if (!strcmp(argv[i], "-bm")) { | |
474 shortmemory = 0; | |
475 i++; | |
476 } else if (!strcmp(argv[i], "-a4") || !strcmp(argv[i], "-A4")) { | |
477 maxline = A4_MaxLine; | |
478 topmargin = A4_TopMargin; | |
479 i++; | |
480 } else if (!strcmp(argv[i], "-us") || !strcmp(argv[i], "-US")) { | |
481 maxline = US_MaxLine; | |
482 topmargin = US_TopMargin; | |
483 i++; | |
484 } else if (!strncmp(argv[i], "-ver", 4)) { | |
485 printf("m2ps Ver.%s\n", m2ps_version); | |
486 exit(0); | |
487 } else { | |
488 fprintf(stderr, "%s: Invalid argument: %s\n", argv[0], argv[i]); | |
489 exit(1); | |
490 } | |
491 } | |
492 | |
493 mulelib_initialize(argc, argv, charsets, NULL); | |
494 bdf_initialize(bdf_path, 0); | |
495 textmode(); | |
496 exit(0); | |
497 } | |
498 | |
499 /*********************/ | |
500 /* PostScript staffs */ | |
501 /*********************/ | |
502 | |
503 ps_bot() | |
504 { | |
505 int c; | |
506 FILE *fp; | |
507 | |
508 if ((fp = open_file(PATH_DATA, psheader)) == NULL) | |
509 fatal1("PostScript header file not found."); | |
510 | |
511 while ((c = getc(fp)) != EOF) putchar(c); | |
512 fclose(fp); | |
513 printf("Mydict begin\n"); | |
514 printf("/DPI %d def\n", dpi); | |
515 printf("/FontScale %d def\n", fontscale); | |
516 printf("/LinePitch %d def\n", linepitch); | |
517 printf("/TopMargin %d def\n", topmargin); | |
518 printf("/LeftMargin %d def\n", leftmargin); | |
519 printf("/ShortMemory %s def\n", (shortmemory ? "true" : "false")); | |
520 } | |
521 | |
522 ps_eot() | |
523 { | |
524 printf("end\n"); | |
525 } | |
526 | |
527 /* Define new PS font for a leading char LC. | |
528 No_cache flag is for the fonts be modified (replacing the glyphs, etc.) | |
529 at execution time. */ | |
530 ps_newfont(lc) | |
531 int lc; | |
532 { | |
533 font_struct *fontp = &font[lc & 0x7F]; | |
534 | |
535 printf("/F%02x /FF%02x %d [%d %d %d %d] %d %s nf\n", | |
536 lc, lc, ((font_extra *)fontp->extra)->fs, | |
537 fontp->llx, fontp->lly - fontp->yoffset, | |
538 fontp->urx, fontp->ury - fontp->yoffset, | |
539 fontp->relative_compose, | |
540 (fontp->bytes == 1 ? "true" : "false")); | |
541 } | |
542 | |
543 ps_setfont(lc) | |
544 int lc; | |
545 { | |
546 printf("F%02x f\n", lc); | |
547 } | |
548 | |
549 ps_newglyph(encoding, glyph) | |
550 int encoding; | |
551 glyph_struct *glyph; | |
552 { | |
553 char *bitmap = glyph->bitmap; | |
554 | |
555 if (*bitmap == '\0') | |
556 bitmap = "00"; | |
557 | |
558 printf("/C%d%s[ %d %d %d %d %d %d %d %d %d <%s> ] g\n", | |
559 encoding, (encoding < 10 ? "XX " : encoding < 100 ? "X " : " "), | |
560 glyph->dwidth, | |
561 glyph->bbox, glyph->bboy, | |
562 glyph->bbw + glyph->bbox, glyph->bbh + glyph->bboy, | |
563 glyph->bbw, glyph->bbh, | |
564 glyph->bbox, glyph->bbh + glyph->bboy, | |
565 bitmap); | |
566 } | |
567 | |
568 ps_bop() | |
569 { | |
570 int lc; | |
571 | |
572 row = clm = 0; | |
573 printf("bp\n"); | |
574 if (shortmemory) { | |
575 current_lc = -1; | |
576 for (lc = 0; lc < 256; lc++) { /* 93.5.7 by K.Handa */ | |
577 if (FONT_LOADED (lc) == 1) FONT_LOADED (lc) = 0; | |
578 } | |
579 } | |
580 } | |
581 | |
582 ps_eop() | |
583 { | |
584 printf("ep\n"); | |
585 } |