]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/texttops.c
Mirror 1.1.x changes.
[thirdparty/cups.git] / filter / texttops.c
1 /*
2 * "$Id: texttops.c,v 1.34.2.11 2002/09/24 18:33:44 mike Exp $"
3 *
4 * Text to PostScript filter for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2002 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Main entry for text to PostScript filter.
29 * WriteEpilogue() - Write the PostScript file epilogue.
30 * WritePage() - Write a page of text.
31 * WriteProlog() - Write the PostScript file prolog with options.
32 * write_line() - Write a row of text.
33 * write_string() - Write a string of text.
34 */
35
36 /*
37 * Include necessary headers...
38 */
39
40 #include "textcommon.h"
41
42
43 /*
44 * Globals...
45 */
46
47 char *Glyphs[65536]; /* PostScript glyphs for Unicode */
48 int NumFonts; /* Number of fonts to use */
49 char *Fonts[256][4]; /* Fonts to use */
50 unsigned short Chars[65536]; /* 0xffcc (ff = font, cc = char) */
51 unsigned short Codes[65536]; /* Unicode glyph mapping to fonts */
52 int Widths[256]; /* Widths of each font */
53 int Directions[256];/* Text directions for each font */
54
55
56 /*
57 * Local functions...
58 */
59
60 static void write_line(int row, lchar_t *line);
61 static void write_string(int col, int row, int len, lchar_t *s);
62 static void write_text(const char *s);
63
64
65 /*
66 * 'main()' - Main entry for text to PostScript filter.
67 */
68
69 int /* O - Exit status */
70 main(int argc, /* I - Number of command-line arguments */
71 char *argv[]) /* I - Command-line arguments */
72 {
73 return (TextMain("texttops", argc, argv));
74 }
75
76
77 /*
78 * 'WriteEpilogue()' - Write the PostScript file epilogue.
79 */
80
81 void
82 WriteEpilogue(void)
83 {
84 puts("%%Trailer");
85 printf("%%%%Pages: %d\n", NumPages);
86 puts("%%EOF");
87
88 free(Page[0]);
89 free(Page);
90 }
91
92
93 /*
94 * 'WritePage()' - Write a page of text.
95 */
96
97 void
98 WritePage(void)
99 {
100 int line; /* Current line */
101
102
103 NumPages ++;
104 printf("%%%%Page: %d %d\n", NumPages, NumPages);
105
106 puts("gsave");
107
108 if (PrettyPrint)
109 printf("%d H\n", NumPages);
110
111 for (line = 0; line < SizeLines; line ++)
112 write_line(line, Page[line]);
113
114 puts("grestore");
115 puts("showpage");
116
117 memset(Page[0], 0, sizeof(lchar_t) * SizeColumns * SizeLines);
118 }
119
120
121 /*
122 * 'WriteProlog()' - Write the PostScript file prolog with options.
123 */
124
125 void
126 WriteProlog(const char *title, /* I - Title of job */
127 const char *user, /* I - Username */
128 const char *classification, /* I - Classification */
129 const char *label, /* I - Page label */
130 ppd_file_t *ppd) /* I - PPD file info */
131 {
132 int i, j, k; /* Looping vars */
133 char *charset; /* Character set string */
134 char filename[1024]; /* Glyph filenames */
135 FILE *fp; /* Glyph files */
136 const char *datadir; /* CUPS_DATADIR environment variable */
137 char line[1024], /* Line from file */
138 *lineptr, /* Pointer into line */
139 *valptr; /* Pointer to value in line */
140 int ch, unicode; /* Character values */
141 int start, end; /* Start and end values for range */
142 char glyph[64]; /* Glyph name */
143 time_t curtime; /* Current time */
144 struct tm *curtm; /* Current date */
145 char curdate[255]; /* Current date (text format) */
146 int num_fonts; /* Number of unique fonts */
147 char *fonts[1024]; /* Unique fonts */
148 static char *names[] = /* Font names */
149 {
150 "cupsNormal",
151 "cupsBold",
152 "cupsItalic"
153 };
154
155
156 /*
157 * Get the data directory...
158 */
159
160 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
161 datadir = CUPS_DATADIR;
162
163 /*
164 * Adjust margins as necessary...
165 */
166
167 if (classification || label)
168 {
169 /*
170 * Leave room for labels...
171 */
172
173 PageBottom += 36;
174 PageTop -= 36;
175 }
176
177 /*
178 * Allocate memory for the page...
179 */
180
181 SizeColumns = (PageRight - PageLeft) / 72.0 * CharsPerInch;
182 SizeLines = (PageTop - PageBottom) / 72.0 * LinesPerInch;
183
184 Page = calloc(sizeof(lchar_t *), SizeLines);
185 Page[0] = calloc(sizeof(lchar_t), SizeColumns * SizeLines);
186 for (i = 1; i < SizeLines; i ++)
187 Page[i] = Page[0] + i * SizeColumns;
188
189 if (PageColumns > 1)
190 {
191 ColumnGutter = CharsPerInch / 2;
192 ColumnWidth = (SizeColumns - ColumnGutter * (PageColumns - 1)) /
193 PageColumns;
194 }
195 else
196 ColumnWidth = SizeColumns;
197
198 /*
199 * Output the DSC header...
200 */
201
202 curtime = time(NULL);
203 curtm = localtime(&curtime);
204 strftime(curdate, sizeof(curdate), CUPS_STRFTIME_FORMAT, curtm);
205
206 puts("%!PS-Adobe-3.0");
207 printf("%%%%BoundingBox: 0 0 %.0f %.0f\n", PageWidth, PageLength);
208 printf("%%cupsRotation: %d\n", (Orientation & 3) * 90);
209 puts("%%Creator: texttops/" CUPS_SVERSION);
210 printf("%%%%CreationDate: %s\n", curdate);
211 printf("%%%%Title: %s\n", title);
212 printf("%%%%For: %s\n", user);
213 puts("%%Pages: (atend)");
214
215 /*
216 * Initialize globals...
217 */
218
219 NumFonts = 0;
220 memset(Fonts, 0, sizeof(Fonts));
221 memset(Glyphs, 0, sizeof(Glyphs));
222 memset(Chars, 0, sizeof(Chars));
223 memset(Codes, 0, sizeof(Codes));
224
225 /*
226 * Load the PostScript glyph names and the corresponding character
227 * set definition...
228 */
229
230 snprintf(filename, sizeof(filename), "%s/data/psglyphs", datadir);
231
232 if ((fp = fopen(filename, "r")) != NULL)
233 {
234 while (fscanf(fp, "%x%63s", &unicode, glyph) == 2)
235 Glyphs[unicode] = strdup(glyph);
236
237 fclose(fp);
238 }
239 else
240 {
241 fprintf(stderr, "ERROR: Unable to open \"%s\" - %s\n", filename,
242 strerror(errno));
243 exit(1);
244 }
245
246 /*
247 * Get the output character set...
248 */
249
250 charset = getenv("CHARSET");
251 if (charset != NULL && strcmp(charset, "us-ascii") != 0)
252 {
253 snprintf(filename, sizeof(filename), "%s/charsets/%s", datadir, charset);
254
255 if ((fp = fopen(filename, "r")) == NULL)
256 {
257 /*
258 * Can't open charset file!
259 */
260
261 fprintf(stderr, "ERROR: Unable to open %s: %s\n", filename,
262 strerror(errno));
263 exit(1);
264 }
265
266 /*
267 * Opened charset file; now see if this is really a charset file...
268 */
269
270 if (fgets(line, sizeof(line), fp) == NULL)
271 {
272 /*
273 * Bad/empty charset file!
274 */
275
276 fclose(fp);
277 fprintf(stderr, "ERROR: Bad/empty charset file %s\n", filename);
278 exit(1);
279 }
280
281 if (strncmp(line, "charset", 7) != 0)
282 {
283 /*
284 * Bad format/not a charset file!
285 */
286
287 fclose(fp);
288 fprintf(stderr, "ERROR: Bad charset file %s\n", filename);
289 exit(1);
290 }
291
292 /*
293 * See if this is an 8-bit or UTF-8 character set file...
294 */
295
296 line[strlen(line) - 1] = '\0'; /* Drop \n */
297 for (lineptr = line + 7; isspace(*lineptr); lineptr ++); /* Skip whitespace */
298
299 if (strcmp(lineptr, "8bit") == 0)
300 {
301 /*
302 * 8-bit text...
303 */
304
305 UTF8 = 0;
306 NumFonts = 0;
307
308 /*
309 * Read the font description(s)...
310 */
311
312 while (fgets(line, sizeof(line), fp) != NULL)
313 {
314 /*
315 * Skip comment and blank lines...
316 */
317
318 if (line[0] == '#' || line[0] == '\n')
319 continue;
320
321 /*
322 * Read the font descriptions that should look like:
323 *
324 * first last direction width normal [bold italic bold-italic]
325 */
326
327 lineptr = line;
328
329 start = strtol(lineptr, &lineptr, 16);
330 end = strtol(lineptr, &lineptr, 16);
331
332 while (isspace(*lineptr))
333 lineptr ++;
334
335 if (!*lineptr)
336 break; /* Must be a font mapping */
337
338 valptr = lineptr;
339
340 while (!isspace(*lineptr) && *lineptr)
341 lineptr ++;
342
343 if (!*lineptr)
344 {
345 /*
346 * Can't have a font without all required values...
347 */
348
349 fprintf(stderr, "ERROR: bad font description line: %s\n", valptr);
350 fclose(fp);
351 exit(1);
352 }
353
354 *lineptr++ = '\0';
355
356 if (strcmp(valptr, "ltor") == 0)
357 Directions[NumFonts] = 1;
358 else if (strcmp(valptr, "rtol") == 0)
359 Directions[NumFonts] = -1;
360 else
361 {
362 fprintf(stderr, "ERROR: Bad text direction %s\n", valptr);
363 fclose(fp);
364 exit(1);
365 }
366
367 /*
368 * Got the direction, now get the width...
369 */
370
371 while (isspace(*lineptr))
372 lineptr ++;
373
374 valptr = lineptr;
375
376 while (!isspace(*lineptr) && *lineptr)
377 lineptr ++;
378
379 if (!*lineptr)
380 {
381 /*
382 * Can't have a font without all required values...
383 */
384
385 fprintf(stderr, "ERROR: bad font description line: %s\n", valptr);
386 fclose(fp);
387 exit(1);
388 }
389
390 *lineptr++ = '\0';
391
392 if (strcmp(valptr, "single") == 0)
393 Widths[NumFonts] = 1;
394 else if (strcmp(valptr, "double") == 0)
395 Widths[NumFonts] = 2;
396 else
397 {
398 fprintf(stderr, "ERROR: Bad text width %s\n", valptr);
399 fclose(fp);
400 exit(1);
401 }
402
403 /*
404 * Get the fonts...
405 */
406
407 for (i = 0; *lineptr && i < 4; i ++)
408 {
409 while (isspace(*lineptr))
410 lineptr ++;
411
412 valptr = lineptr;
413
414 while (!isspace(*lineptr) && *lineptr)
415 lineptr ++;
416
417 if (*lineptr)
418 *lineptr++ = '\0';
419
420 if (lineptr > valptr)
421 Fonts[NumFonts][i] = strdup(valptr);
422 }
423
424 /*
425 * Fill in remaining fonts as needed...
426 */
427
428 for (j = i; j < 4; j ++)
429 Fonts[NumFonts][j] = strdup(Fonts[NumFonts][0]);
430
431 /*
432 * Define the character mappings...
433 */
434
435 for (i = start, j = NumFonts * 256; i <= end; i ++, j ++)
436 Chars[i] = j;
437
438 NumFonts ++;
439 }
440
441 /*
442 * Read encoding lines...
443 */
444
445 do
446 {
447 /*
448 * Skip comment and blank lines...
449 */
450
451 if (line[0] == '#' || line[0] == '\n')
452 continue;
453
454 /*
455 * Grab the character and unicode glyph number.
456 */
457
458 if (sscanf(line, "%x%x", &ch, &unicode) == 2 && ch < 256)
459 Codes[Chars[ch]] = unicode;
460 }
461 while (fgets(line, sizeof(line), fp) != NULL);
462
463 fclose(fp);
464 }
465 else if (strcmp(lineptr, "utf8") == 0)
466 {
467 /*
468 * UTF-8 (Unicode) text...
469 */
470
471 UTF8 = 1;
472
473 /*
474 * Read the font descriptions...
475 */
476
477 NumFonts = 0;
478
479 while (fgets(line, sizeof(line), fp) != NULL)
480 {
481 /*
482 * Skip comment and blank lines...
483 */
484
485 if (line[0] == '#' || line[0] == '\n')
486 continue;
487
488 /*
489 * Read the font descriptions that should look like:
490 *
491 * start end direction width normal [bold italic bold-italic]
492 */
493
494 lineptr = line;
495
496 start = strtol(lineptr, &lineptr, 16);
497 end = strtol(lineptr, &lineptr, 16);
498
499 while (isspace(*lineptr))
500 lineptr ++;
501
502 valptr = lineptr;
503
504 while (!isspace(*lineptr) && *lineptr)
505 lineptr ++;
506
507 if (!*lineptr)
508 {
509 /*
510 * Can't have a font without all required values...
511 */
512
513 fprintf(stderr, "ERROR: bad font description line: %s\n", valptr);
514 fclose(fp);
515 exit(1);
516 }
517
518 *lineptr++ = '\0';
519
520 if (strcmp(valptr, "ltor") == 0)
521 Directions[NumFonts] = 1;
522 else if (strcmp(valptr, "rtol") == 0)
523 Directions[NumFonts] = -1;
524 else
525 {
526 fprintf(stderr, "ERROR: Bad text direction %s\n", valptr);
527 fclose(fp);
528 exit(1);
529 }
530
531 /*
532 * Got the direction, now get the width...
533 */
534
535 while (isspace(*lineptr))
536 lineptr ++;
537
538 valptr = lineptr;
539
540 while (!isspace(*lineptr) && *lineptr)
541 lineptr ++;
542
543 if (!*lineptr)
544 {
545 /*
546 * Can't have a font without all required values...
547 */
548
549 fprintf(stderr, "ERROR: bad font description line: %s\n", valptr);
550 fclose(fp);
551 exit(1);
552 }
553
554 *lineptr++ = '\0';
555
556 if (strcmp(valptr, "single") == 0)
557 Widths[NumFonts] = 1;
558 else if (strcmp(valptr, "double") == 0)
559 Widths[NumFonts] = 2;
560 else
561 {
562 fprintf(stderr, "ERROR: Bad text width %s\n", valptr);
563 fclose(fp);
564 exit(1);
565 }
566
567 /*
568 * Get the fonts...
569 */
570
571 for (i = 0; *lineptr && i < 4; i ++)
572 {
573 while (isspace(*lineptr))
574 lineptr ++;
575
576 valptr = lineptr;
577
578 while (!isspace(*lineptr) && *lineptr)
579 lineptr ++;
580
581 if (*lineptr)
582 *lineptr++ = '\0';
583
584 if (lineptr > valptr)
585 Fonts[NumFonts][i] = strdup(valptr);
586 }
587
588 /*
589 * Fill in remaining fonts as needed...
590 */
591
592 for (j = i; j < 4; j ++)
593 Fonts[NumFonts][j] = strdup(Fonts[NumFonts][0]);
594
595 /*
596 * Define the character mappings...
597 */
598
599 for (i = start, j = NumFonts * 256; i <= end; i ++, j ++)
600 {
601 Chars[i] = j;
602 Codes[j] = i;
603 }
604
605 /*
606 * Move to the next font, stopping if needed...
607 */
608
609 NumFonts ++;
610 if (NumFonts >= 256)
611 break;
612 }
613
614 fclose(fp);
615 }
616 else
617 {
618 fprintf(stderr, "ERROR: Bad charset type %s\n", lineptr);
619 fclose(fp);
620 exit(1);
621 }
622 }
623 else
624 {
625 /*
626 * Standard ASCII output just uses Courier, Courier-Bold, and
627 * possibly Courier-Oblique.
628 */
629
630 NumFonts = 1;
631
632 Fonts[0][ATTR_NORMAL] = strdup("Courier");
633 Fonts[0][ATTR_BOLD] = strdup("Courier-Bold");
634 Fonts[0][ATTR_ITALIC] = strdup("Courier-Oblique");
635 Fonts[0][ATTR_BOLDITALIC] = strdup("Courier-BoldOblique");
636
637 Widths[0] = 1;
638 Directions[0] = 1;
639
640 /*
641 * Define US-ASCII characters...
642 */
643
644 for (i = 32; i < 127; i ++)
645 {
646 Chars[i] = i;
647 Codes[i] = i;
648 }
649 }
650
651 /*
652 * Generate a list of unique fonts to use...
653 */
654
655 for (i = 0, num_fonts = 0; i < NumFonts; i ++)
656 for (j = PrettyPrint ? 2 : 1; j >= 0; j --)
657 {
658 for (k = 0; k < num_fonts; k ++)
659 if (strcmp(Fonts[i][j], fonts[k]) == 0)
660 break;
661
662 if (k >= num_fonts)
663 {
664 /*
665 * Add new font...
666 */
667
668 fonts[num_fonts] = Fonts[i][j];
669 num_fonts ++;
670 }
671 }
672
673 /*
674 * List the fonts that will be used...
675 */
676
677 for (i = 0; i < num_fonts; i ++)
678 if (i == 0)
679 printf("%%%%DocumentNeededResources: font %s\n", fonts[i]);
680 else
681 printf("%%%%+ font %s\n", fonts[i]);
682
683 puts("%%DocumentSuppliedResources: procset texttops 1.1 0");
684
685 for (i = 0; i < num_fonts; i ++)
686 {
687 if (ppd != NULL)
688 {
689 fprintf(stderr, "DEBUG: ppd->num_fonts = %d\n", ppd->num_fonts);
690
691 for (j = 0; j < ppd->num_fonts; j ++)
692 {
693 fprintf(stderr, "DEBUG: ppd->fonts[%d] = %s\n", j, ppd->fonts[j]);
694
695 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
696 break;
697 }
698 }
699 else
700 j = 0;
701
702 if (ppd != NULL && j >= ppd->num_fonts)
703 {
704 /*
705 * Need to embed this font...
706 */
707
708 printf("%%%%+ font %s\n", fonts[i]);
709 }
710 }
711
712 puts("%%EndComments");
713
714 puts("%%BeginProlog");
715
716 /*
717 * Download any missing fonts...
718 */
719
720 for (i = 0; i < num_fonts; i ++)
721 {
722 if (ppd != NULL)
723 {
724 for (j = 0; j < ppd->num_fonts; j ++)
725 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
726 break;
727 }
728 else
729 j = 0;
730
731 if (ppd != NULL && j >= ppd->num_fonts)
732 {
733 /*
734 * Need to embed this font...
735 */
736
737 printf("%%%%BeginResource: font %s\n", fonts[i]);
738
739 snprintf(filename, sizeof(filename), "%s/fonts/%s", datadir, fonts[i]);
740 if ((fp = fopen(filename, "rb")) != NULL)
741 {
742 while ((j = fread(line, 1, sizeof(line), fp)) > 0)
743 fwrite(line, 1, j, stdout);
744
745 fclose(fp);
746 }
747
748 puts("\n%%EndResource");
749 }
750 }
751
752 /*
753 * Write the encoding array(s)...
754 */
755
756 puts("% character encoding(s)");
757
758 for (i = 0; i < NumFonts; i ++)
759 {
760 printf("/cupsEncoding%02x [\n", i);
761
762 for (ch = 0; ch < 256; ch ++)
763 {
764 if (Glyphs[Codes[i * 256 + ch]])
765 printf("/%s", Glyphs[Codes[i * 256 + ch]]);
766 else
767 printf("/.notdef");
768
769 if ((ch & 7) == 7)
770 putchar('\n');
771 }
772
773 puts("] def");
774 }
775
776 /*
777 * Create the fonts...
778 */
779
780 if (NumFonts == 1)
781 {
782 /*
783 * Just reencode the named fonts...
784 */
785
786 puts("% Reencode fonts");
787
788 for (i = PrettyPrint ? 2 : 1; i >= 0; i --)
789 {
790 printf("/%s findfont\n", Fonts[0][i]);
791 puts("dup length 1 add dict begin\n"
792 " { 1 index /FID ne { def } { pop pop } ifelse } forall\n"
793 " /Encoding cupsEncoding00 def\n"
794 " currentdict\n"
795 "end");
796 printf("/%s exch definefont pop\n", names[i]);
797 }
798 }
799 else
800 {
801 /*
802 * Construct composite fonts... Start by reencoding the base fonts...
803 */
804
805 puts("% Reencode base fonts");
806
807 for (i = 1 + PrettyPrint; i >= 0; i --)
808 for (j = 0; j < NumFonts; j ++)
809 {
810 printf("/%s findfont\n", Fonts[j][i]);
811 printf("dup length 1 add dict begin\n"
812 " { 1 index /FID ne { def } { pop pop } ifelse } forall\n"
813 " /Encoding cupsEncoding%02x def\n"
814 " currentdict\n"
815 "end\n", j);
816 printf("/%s%02x exch definefont /%s%02x exch def\n", names[i], j,
817 names[i], j);
818 }
819
820 /*
821 * Then merge them into composite fonts...
822 */
823
824 puts("% Create composite fonts...");
825
826 for (i = 1 + PrettyPrint; i >= 0; i --)
827 {
828 puts("8 dict begin");
829 puts("/FontType 0 def/FontMatrix[1.0 0 0 1.0 0 0]def/FMapType 2 def/Encoding[");
830 for (j = 0; j < NumFonts; j ++)
831 if (j == (NumFonts - 1))
832 printf("%d", j);
833 else if ((j & 15) == 15)
834 printf("%d\n", j);
835 else
836 printf("%d ", j);
837 puts("]def/FDepVector[");
838 for (j = 0; j < NumFonts; j ++)
839 if (j == (NumFonts - 1))
840 printf("%s%02x", names[i], j);
841 else if ((j & 3) == 3)
842 printf("%s%02x\n", names[i], j);
843 else
844 printf("%s%02x ", names[i], j);
845 puts("]def currentdict end");
846 printf("/%s exch definefont pop\n", names[i]);
847 }
848 }
849
850 /*
851 * Output the texttops procset...
852 */
853
854 puts("%%BeginResource: procset texttops 1.1 0");
855
856 puts("% Define fonts");
857
858 printf("/FN /cupsNormal findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
859 120.0 / CharsPerInch, 68.0 / LinesPerInch);
860 printf("/FB /cupsBold findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
861 120.0 / CharsPerInch, 68.0 / LinesPerInch);
862 if (PrettyPrint)
863 printf("/FI /cupsItalic findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
864 120.0 / CharsPerInch, 68.0 / LinesPerInch);
865
866 puts("% Common procedures");
867
868 puts("/N { FN setfont moveto } bind def");
869 puts("/B { FB setfont moveto } bind def");
870 printf("/U { gsave 0.5 setlinewidth 0 %.3f rmoveto "
871 "0 rlineto stroke grestore } bind def\n", -6.8 / LinesPerInch);
872
873 if (PrettyPrint)
874 {
875 if (ColorDevice)
876 {
877 puts("/S { 0.0 setgray show } bind def");
878 puts("/r { 0.5 0.0 0.0 setrgbcolor show } bind def");
879 puts("/g { 0.0 0.5 0.0 setrgbcolor show } bind def");
880 puts("/b { 0.0 0.0 0.5 setrgbcolor show } bind def");
881 }
882 else
883 {
884 puts("/S { 0.0 setgray show } bind def");
885 puts("/r { 0.2 setgray show } bind def");
886 puts("/g { 0.2 setgray show } bind def");
887 puts("/b { 0.2 setgray show } bind def");
888 }
889
890 puts("/I { FI setfont moveto } bind def");
891
892 puts("/n {");
893 puts("\t20 string cvs % convert page number to string");
894 if (NumFonts > 1)
895 {
896 /*
897 * Convert a number to double-byte chars...
898 */
899
900 puts("\tdup length % get length");
901 puts("\tdup 2 mul string /P exch def % P = string twice as long");
902 puts("\t0 1 2 index 1 sub { % loop through each character in the page number");
903 puts("\t\tdup 3 index exch get % get character N from the page number");
904 puts("\t\texch 2 mul dup % compute offset in P");
905 puts("\t\tP exch 0 put % font 0");
906 puts("\t\t1 add P exch 2 index put % character");
907 puts("\t\tpop % discard character");
908 puts("\t} for % do for loop");
909 puts("\tpop pop % discard string and length");
910 puts("\tP % put string on stack");
911 }
912 puts("} bind def");
913
914 printf("/T");
915 write_text(title);
916 puts("def");
917
918 printf("/D");
919 write_text(curdate);
920 puts("def");
921
922 puts("/H {");
923 puts("\tgsave");
924 puts("\t0.9 setgray");
925
926 if (Duplex)
927 {
928 puts("\tdup 2 mod 0 eq {");
929 printf("\t\t%.3f %.3f translate } {\n",
930 PageWidth - PageRight, PageTop + 72.0f / LinesPerInch);
931 printf("\t\t%.3f %.3f translate } ifelse\n",
932 PageLeft, PageTop + 72.0f / LinesPerInch);
933 }
934 else
935 printf("\t%.3f %.3f translate\n",
936 PageLeft, PageTop + 72.0f / LinesPerInch);
937
938 printf("\t0 0 %.3f %.3f rectfill\n", PageRight - PageLeft,
939 144.0f / LinesPerInch);
940
941 puts("\tFB setfont");
942 puts("\t0 setgray");
943
944 if (Duplex)
945 {
946 puts("\tdup 2 mod 0 eq {");
947 printf("\t\tT stringwidth pop neg %.3f add %.3f } {\n",
948 PageRight - PageLeft - 36.0f / LinesPerInch,
949 (0.5f + 0.157f) * 72.0f / LinesPerInch);
950 printf("\t\t%.3f %.3f } ifelse\n", 36.0f / LinesPerInch,
951 (0.5f + 0.157f) * 72.0f / LinesPerInch);
952 }
953 else
954 printf("\t%.3f %.3f\n", 36.0f / LinesPerInch,
955 (0.5f + 0.157f) * 72.0f / LinesPerInch);
956
957 puts("\tmoveto T show");
958
959 printf("\tD dup stringwidth pop neg 2 div %.3f add %.3f\n",
960 (PageRight - PageLeft) * 0.5,
961 (0.5f + 0.157f) * 72.0f / LinesPerInch);
962 puts("\tmoveto show");
963
964 if (Duplex)
965 {
966 puts("\tdup n exch 2 mod 0 eq {");
967 printf("\t\t%.3f %.3f } {\n", 36.0f / LinesPerInch,
968 (0.5f + 0.157f) * 72.0f / LinesPerInch);
969 printf("\t\tdup stringwidth pop neg %.3f add %.3f } ifelse\n",
970 PageRight - PageLeft - 36.0f / LinesPerInch,
971 (0.5f + 0.157f) * 72.0f / LinesPerInch);
972 }
973 else
974 printf("\tn dup stringwidth pop neg %.3f add %.3f\n",
975 PageRight - PageLeft - 36.0f / LinesPerInch,
976 (0.5f + 0.157f) * 72.0f / LinesPerInch);
977
978 puts("\tmoveto show");
979 puts("\tgrestore");
980 puts("} bind def");
981 }
982 else
983 puts("/S { show } bind def");
984
985 puts("%%EndResource");
986
987 puts("%%EndProlog");
988 }
989
990
991 /*
992 * 'write_line()' - Write a row of text.
993 */
994
995 static void
996 write_line(int row, /* I - Row number (0 to N) */
997 lchar_t *line) /* I - Line to print */
998 {
999 int i; /* Looping var */
1000 int col; /* Current column */
1001 int attr; /* Current attribute */
1002 int font, /* Font to use */
1003 lastfont, /* Last font */
1004 mono; /* Monospaced? */
1005 lchar_t *start; /* First character in sequence */
1006
1007
1008 for (col = 0, start = line; col < SizeColumns;)
1009 {
1010 while (col < SizeColumns && (line->ch == ' ' || line->ch == 0))
1011 {
1012 col ++;
1013 line ++;
1014 }
1015
1016 if (col >= SizeColumns)
1017 break;
1018
1019 if (NumFonts == 1)
1020 {
1021 /*
1022 * All characters in a single font - assume monospaced...
1023 */
1024
1025 attr = line->attr;
1026 start = line;
1027
1028 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1029 {
1030 col ++;
1031 line ++;
1032 }
1033
1034 write_string(col - (line - start), row, line - start, start);
1035 }
1036 else
1037 {
1038 /*
1039 * Multiple fonts; break up based on the font...
1040 */
1041
1042 attr = line->attr;
1043 start = line;
1044 lastfont = Chars[line->ch] / 256;
1045 mono = strncmp(Fonts[lastfont][0], "Courier", 7) == 0;
1046 col ++;
1047 line ++;
1048
1049 if (mono)
1050 {
1051 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1052 {
1053 font = Chars[line->ch] / 256;
1054 if (strncmp(Fonts[font][0], "Courier", 7) != 0 ||
1055 font != lastfont)
1056 break;
1057
1058 col ++;
1059 line ++;
1060 }
1061 }
1062
1063 if (Directions[lastfont] > 0)
1064 write_string(col - (line - start), row, line - start, start);
1065 else
1066 {
1067 /*
1068 * Do right-to-left text...
1069 */
1070
1071 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1072 {
1073 if (Directions[Chars[line->ch] / 256] > 0 &&
1074 !ispunct(line->ch) && !isspace(line->ch))
1075 break;
1076
1077 col ++;
1078 line ++;
1079 }
1080
1081 for (i = 1; start < line; i ++, start ++)
1082 if (!isspace(start->ch))
1083 write_string(col - i, row, 1, start);
1084 }
1085 }
1086 }
1087 }
1088
1089
1090 /*
1091 * 'write_string()' - Write a string of text.
1092 */
1093
1094 static void
1095 write_string(int col, /* I - Start column */
1096 int row, /* I - Row */
1097 int len, /* I - Number of characters */
1098 lchar_t *s) /* I - String to print */
1099 {
1100 int ch; /* Current character */
1101 float x, y; /* Position of text */
1102 unsigned attr; /* Character attributes */
1103
1104
1105 /*
1106 * Position the text and set the font...
1107 */
1108
1109 if (Duplex && (NumPages & 1) == 0)
1110 {
1111 x = PageWidth - PageRight;
1112 y = PageTop;
1113 }
1114 else
1115 {
1116 x = PageLeft;
1117 y = PageTop;
1118 }
1119
1120 x += (float)col * 72.0f / (float)CharsPerInch;
1121 y -= (float)(row + 0.843) * 72.0f / (float)LinesPerInch;
1122
1123 attr = s->attr;
1124
1125 if (attr & ATTR_RAISED)
1126 y += 36.0 / (float)LinesPerInch;
1127 else if (attr & ATTR_LOWERED)
1128 y -= 36.0 / (float)LinesPerInch;
1129
1130 if (x == (int)x)
1131 printf("%.0f ", x);
1132 else
1133 printf("%.3f ", x);
1134
1135 if (y == (int)y)
1136 printf("%.0f ", y);
1137 else
1138 printf("%.3f ", y);
1139
1140 if (attr & ATTR_BOLD)
1141 putchar('B');
1142 else if (attr & ATTR_ITALIC)
1143 putchar('I');
1144 else
1145 putchar('N');
1146
1147 if (attr & ATTR_UNDERLINE)
1148 printf(" %.3f U", (float)len * 72.0 / (float)CharsPerInch);
1149
1150 if (NumFonts > 1)
1151 {
1152 /*
1153 * Write a hex string...
1154 */
1155
1156 putchar('<');
1157
1158 while (len > 0)
1159 {
1160 printf("%04x", Chars[s->ch]);
1161
1162 len --;
1163 s ++;
1164 }
1165
1166 putchar('>');
1167 }
1168 else
1169 {
1170 /*
1171 * Write a quoted string...
1172 */
1173
1174 putchar('(');
1175
1176 while (len > 0)
1177 {
1178 ch = Chars[s->ch];
1179
1180 if (ch < 32 || ch > 126)
1181 {
1182 /*
1183 * Quote 8-bit and control characters...
1184 */
1185
1186 printf("\\%03o", ch);
1187 }
1188 else
1189 {
1190 /*
1191 * Quote the parenthesis and backslash as needed...
1192 */
1193
1194 if (ch == '(' || ch == ')' || ch == '\\')
1195 putchar('\\');
1196
1197 putchar(ch);
1198 }
1199
1200 len --;
1201 s ++;
1202 }
1203
1204 putchar(')');
1205 }
1206
1207 if (PrettyPrint)
1208 {
1209 if (attr & ATTR_RED)
1210 puts("r");
1211 else if (attr & ATTR_GREEN)
1212 puts("g");
1213 else if (attr & ATTR_BLUE)
1214 puts("b");
1215 else
1216 puts("S");
1217 }
1218 else
1219 puts("S");
1220 }
1221
1222
1223 /*
1224 * 'write_text()' - Write a text string, quoting/encoding as needed.
1225 */
1226
1227 static void
1228 write_text(const char *s) /* I - String to write */
1229 {
1230 int ch; /* Actual character value (UTF8) */
1231 const unsigned char *utf8; /* UTF8 text */
1232
1233
1234 if (NumFonts > 1)
1235 {
1236 /*
1237 * 8/8 encoding...
1238 */
1239
1240 putchar('<');
1241
1242 utf8 = (const unsigned char *)s;
1243
1244 while (*utf8)
1245 {
1246 if (*utf8 < 0xc0 || !UTF8)
1247 ch = *utf8 ++;
1248 else if ((*utf8 & 0xe0) == 0xc0)
1249 {
1250 /*
1251 * Two byte character...
1252 */
1253
1254 ch = ((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f);
1255 utf8 += 2;
1256 }
1257 else
1258 {
1259 /*
1260 * Three byte character...
1261 */
1262
1263 ch = ((((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f)) << 6) |
1264 (utf8[2] & 0x3f);
1265 utf8 += 3;
1266 }
1267
1268 printf("%04x", Chars[ch]);
1269 }
1270
1271 putchar('>');
1272 }
1273 else
1274 {
1275 /*
1276 * Standard 8-bit encoding...
1277 */
1278
1279 putchar('(');
1280
1281 while (*s)
1282 {
1283 if (*s < 32 || *s > 126)
1284 printf("\\%03o", *s);
1285 else
1286 {
1287 if (*s == '(' || *s == ')' || *s == '\\')
1288 putchar('\\');
1289
1290 putchar(*s);
1291 }
1292
1293 s ++;
1294 }
1295
1296 putchar(')');
1297 }
1298 }
1299
1300
1301 /*
1302 * End of "$Id: texttops.c,v 1.34.2.11 2002/09/24 18:33:44 mike Exp $".
1303 */