]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/texttops.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / texttops.c
1 /*
2 * "$Id: texttops.c 4680 2005-09-21 09:28:39Z mike $"
3 *
4 * Text to PostScript filter for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2005 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 USA
19 *
20 * Voice: (301) 373-9600
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), "%c", 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 & 255); 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 & 255))
333 lineptr ++;
334
335 if (!*lineptr)
336 break; /* Must be a font mapping */
337
338 valptr = lineptr;
339
340 while (!isspace(*lineptr & 255) && *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 & 255))
372 lineptr ++;
373
374 valptr = lineptr;
375
376 while (!isspace(*lineptr & 255) && *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 & 255))
410 lineptr ++;
411
412 valptr = lineptr;
413
414 while (!isspace(*lineptr & 255) && *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 & 255))
500 lineptr ++;
501
502 valptr = lineptr;
503
504 while (!isspace(*lineptr & 255) && *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 & 255))
536 lineptr ++;
537
538 valptr = lineptr;
539
540 while (!isspace(*lineptr & 255) && *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 & 255))
574 lineptr ++;
575
576 valptr = lineptr;
577
578 while (!isspace(*lineptr & 255) && *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 strncmp(fonts[i], "Courier", 7) == 0 ||
704 strcmp(fonts[i], "Symbol") == 0)
705 {
706 /*
707 * Need to embed this font...
708 */
709
710 printf("%%%%+ font %s\n", fonts[i]);
711 }
712 }
713
714 puts("%%EndComments");
715
716 puts("%%BeginProlog");
717
718 /*
719 * Download any missing fonts...
720 */
721
722 for (i = 0; i < num_fonts; i ++)
723 {
724 if (ppd != NULL)
725 {
726 for (j = 0; j < ppd->num_fonts; j ++)
727 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
728 break;
729 }
730 else
731 j = 0;
732
733 if ((ppd != NULL && j >= ppd->num_fonts) ||
734 strncmp(fonts[i], "Courier", 7) == 0 ||
735 strcmp(fonts[i], "Symbol") == 0)
736 {
737 /*
738 * Need to embed this font...
739 */
740
741 printf("%%%%BeginResource: font %s\n", fonts[i]);
742
743 /**** MRS: Need to use CUPS_FONTPATH env var! ****/
744 /**** Also look for Fontmap file or name.pfa, name.pfb... ****/
745 snprintf(filename, sizeof(filename), "%s/fonts/%s", datadir, fonts[i]);
746 if ((fp = fopen(filename, "rb")) != NULL)
747 {
748 while ((j = fread(line, 1, sizeof(line), fp)) > 0)
749 fwrite(line, 1, j, stdout);
750
751 fclose(fp);
752 }
753
754 puts("\n%%EndResource");
755 }
756 }
757
758 /*
759 * Write the encoding array(s)...
760 */
761
762 puts("% character encoding(s)");
763
764 for (i = 0; i < NumFonts; i ++)
765 {
766 printf("/cupsEncoding%02x [\n", i);
767
768 for (ch = 0; ch < 256; ch ++)
769 {
770 if (Glyphs[Codes[i * 256 + ch]])
771 printf("/%s", Glyphs[Codes[i * 256 + ch]]);
772 else if (Codes[i * 256 + ch] > 255)
773 printf("/uni%04X", Codes[i * 256 + ch]);
774 else
775 printf("/.notdef");
776
777 if ((ch & 7) == 7)
778 putchar('\n');
779 }
780
781 puts("] def");
782 }
783
784 /*
785 * Create the fonts...
786 */
787
788 if (NumFonts == 1)
789 {
790 /*
791 * Just reencode the named fonts...
792 */
793
794 puts("% Reencode fonts");
795
796 for (i = PrettyPrint ? 2 : 1; i >= 0; i --)
797 {
798 printf("/%s findfont\n", Fonts[0][i]);
799 puts("dup length 1 add dict begin\n"
800 " { 1 index /FID ne { def } { pop pop } ifelse } forall\n"
801 " /Encoding cupsEncoding00 def\n"
802 " currentdict\n"
803 "end");
804 printf("/%s exch definefont pop\n", names[i]);
805 }
806 }
807 else
808 {
809 /*
810 * Construct composite fonts... Start by reencoding the base fonts...
811 */
812
813 puts("% Reencode base fonts");
814
815 for (i = 1 + PrettyPrint; i >= 0; i --)
816 for (j = 0; j < NumFonts; j ++)
817 {
818 printf("/%s findfont\n", Fonts[j][i]);
819 printf("dup length 1 add dict begin\n"
820 " { 1 index /FID ne { def } { pop pop } ifelse } forall\n"
821 " /Encoding cupsEncoding%02x def\n"
822 " currentdict\n"
823 "end\n", j);
824 printf("/%s%02x exch definefont /%s%02x exch def\n", names[i], j,
825 names[i], j);
826 }
827
828 /*
829 * Then merge them into composite fonts...
830 */
831
832 puts("% Create composite fonts...");
833
834 for (i = 1 + PrettyPrint; i >= 0; i --)
835 {
836 puts("8 dict begin");
837 puts("/FontType 0 def/FontMatrix[1.0 0 0 1.0 0 0]def/FMapType 2 def/Encoding[");
838 for (j = 0; j < NumFonts; j ++)
839 if (j == (NumFonts - 1))
840 printf("%d", j);
841 else if ((j & 15) == 15)
842 printf("%d\n", j);
843 else
844 printf("%d ", j);
845 puts("]def/FDepVector[");
846 for (j = 0; j < NumFonts; j ++)
847 if (j == (NumFonts - 1))
848 printf("%s%02x", names[i], j);
849 else if ((j & 3) == 3)
850 printf("%s%02x\n", names[i], j);
851 else
852 printf("%s%02x ", names[i], j);
853 puts("]def currentdict end");
854 printf("/%s exch definefont pop\n", names[i]);
855 }
856 }
857
858 /*
859 * Output the texttops procset...
860 */
861
862 puts("%%BeginResource: procset texttops 1.1 0");
863
864 puts("% Define fonts");
865
866 printf("/FN /cupsNormal findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
867 120.0 / CharsPerInch, 68.0 / LinesPerInch);
868 printf("/FB /cupsBold findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
869 120.0 / CharsPerInch, 68.0 / LinesPerInch);
870 if (PrettyPrint)
871 printf("/FI /cupsItalic findfont [%.3f 0 0 %.3f 0 0] makefont def\n",
872 120.0 / CharsPerInch, 68.0 / LinesPerInch);
873
874 puts("% Common procedures");
875
876 puts("/N { FN setfont moveto } bind def");
877 puts("/B { FB setfont moveto } bind def");
878 printf("/U { gsave 0.5 setlinewidth 0 %.3f rmoveto "
879 "0 rlineto stroke grestore } bind def\n", -6.8 / LinesPerInch);
880
881 if (PrettyPrint)
882 {
883 if (ColorDevice)
884 {
885 puts("/S { 0.0 setgray show } bind def");
886 puts("/r { 0.5 0.0 0.0 setrgbcolor show } bind def");
887 puts("/g { 0.0 0.5 0.0 setrgbcolor show } bind def");
888 puts("/b { 0.0 0.0 0.5 setrgbcolor show } bind def");
889 }
890 else
891 {
892 puts("/S { 0.0 setgray show } bind def");
893 puts("/r { 0.2 setgray show } bind def");
894 puts("/g { 0.2 setgray show } bind def");
895 puts("/b { 0.2 setgray show } bind def");
896 }
897
898 puts("/I { FI setfont moveto } bind def");
899
900 puts("/n {");
901 puts("\t20 string cvs % convert page number to string");
902 if (NumFonts > 1)
903 {
904 /*
905 * Convert a number to double-byte chars...
906 */
907
908 puts("\tdup length % get length");
909 puts("\tdup 2 mul string /P exch def % P = string twice as long");
910 puts("\t0 1 2 index 1 sub { % loop through each character in the page number");
911 puts("\t\tdup 3 index exch get % get character N from the page number");
912 puts("\t\texch 2 mul dup % compute offset in P");
913 puts("\t\tP exch 0 put % font 0");
914 puts("\t\t1 add P exch 2 index put % character");
915 puts("\t\tpop % discard character");
916 puts("\t} for % do for loop");
917 puts("\tpop pop % discard string and length");
918 puts("\tP % put string on stack");
919 }
920 puts("} bind def");
921
922 printf("/T");
923 write_text(title);
924 puts("def");
925
926 printf("/D");
927 write_text(curdate);
928 puts("def");
929
930 puts("/H {");
931 puts("\tgsave");
932 puts("\t0.9 setgray");
933
934 if (Duplex)
935 {
936 puts("\tdup 2 mod 0 eq {");
937 printf("\t\t%.3f %.3f translate } {\n",
938 PageWidth - PageRight, PageTop + 72.0f / LinesPerInch);
939 printf("\t\t%.3f %.3f translate } ifelse\n",
940 PageLeft, PageTop + 72.0f / LinesPerInch);
941 }
942 else
943 printf("\t%.3f %.3f translate\n",
944 PageLeft, PageTop + 72.0f / LinesPerInch);
945
946 printf("\t0 0 %.3f %.3f rectfill\n", PageRight - PageLeft,
947 144.0f / LinesPerInch);
948
949 puts("\tFB setfont");
950 puts("\t0 setgray");
951
952 if (Duplex)
953 {
954 puts("\tdup 2 mod 0 eq {");
955 printf("\t\tT stringwidth pop neg %.3f add %.3f } {\n",
956 PageRight - PageLeft - 36.0f / LinesPerInch,
957 (0.5f + 0.157f) * 72.0f / LinesPerInch);
958 printf("\t\t%.3f %.3f } ifelse\n", 36.0f / LinesPerInch,
959 (0.5f + 0.157f) * 72.0f / LinesPerInch);
960 }
961 else
962 printf("\t%.3f %.3f\n", 36.0f / LinesPerInch,
963 (0.5f + 0.157f) * 72.0f / LinesPerInch);
964
965 puts("\tmoveto T show");
966
967 printf("\tD dup stringwidth pop neg 2 div %.3f add %.3f\n",
968 (PageRight - PageLeft) * 0.5,
969 (0.5f + 0.157f) * 72.0f / LinesPerInch);
970 puts("\tmoveto show");
971
972 if (Duplex)
973 {
974 puts("\tdup n exch 2 mod 0 eq {");
975 printf("\t\t%.3f %.3f } {\n", 36.0f / LinesPerInch,
976 (0.5f + 0.157f) * 72.0f / LinesPerInch);
977 printf("\t\tdup stringwidth pop neg %.3f add %.3f } ifelse\n",
978 PageRight - PageLeft - 36.0f / LinesPerInch,
979 (0.5f + 0.157f) * 72.0f / LinesPerInch);
980 }
981 else
982 printf("\tn dup stringwidth pop neg %.3f add %.3f\n",
983 PageRight - PageLeft - 36.0f / LinesPerInch,
984 (0.5f + 0.157f) * 72.0f / LinesPerInch);
985
986 puts("\tmoveto show");
987 puts("\tgrestore");
988 puts("} bind def");
989 }
990 else
991 puts("/S { show } bind def");
992
993 puts("%%EndResource");
994
995 puts("%%EndProlog");
996 }
997
998
999 /*
1000 * 'write_line()' - Write a row of text.
1001 */
1002
1003 static void
1004 write_line(int row, /* I - Row number (0 to N) */
1005 lchar_t *line) /* I - Line to print */
1006 {
1007 int i; /* Looping var */
1008 int col; /* Current column */
1009 int attr; /* Current attribute */
1010 int font, /* Font to use */
1011 lastfont, /* Last font */
1012 mono; /* Monospaced? */
1013 lchar_t *start; /* First character in sequence */
1014
1015
1016 for (col = 0, start = line; col < SizeColumns;)
1017 {
1018 while (col < SizeColumns && (line->ch == ' ' || line->ch == 0))
1019 {
1020 col ++;
1021 line ++;
1022 }
1023
1024 if (col >= SizeColumns)
1025 break;
1026
1027 if (NumFonts == 1)
1028 {
1029 /*
1030 * All characters in a single font - assume monospaced...
1031 */
1032
1033 attr = line->attr;
1034 start = line;
1035
1036 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1037 {
1038 col ++;
1039 line ++;
1040 }
1041
1042 write_string(col - (line - start), row, line - start, start);
1043 }
1044 else
1045 {
1046 /*
1047 * Multiple fonts; break up based on the font...
1048 */
1049
1050 attr = line->attr;
1051 start = line;
1052 lastfont = Chars[line->ch] / 256;
1053 mono = strncmp(Fonts[lastfont][0], "Courier", 7) == 0;
1054 col ++;
1055 line ++;
1056
1057 if (mono)
1058 {
1059 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1060 {
1061 font = Chars[line->ch] / 256;
1062 if (strncmp(Fonts[font][0], "Courier", 7) != 0 ||
1063 font != lastfont)
1064 break;
1065
1066 col ++;
1067 line ++;
1068 }
1069 }
1070
1071 if (Directions[lastfont] > 0)
1072 write_string(col - (line - start), row, line - start, start);
1073 else
1074 {
1075 /*
1076 * Do right-to-left text...
1077 */
1078
1079 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1080 {
1081 if (Directions[Chars[line->ch] / 256] > 0 &&
1082 !ispunct(line->ch & 255) && !isspace(line->ch & 255))
1083 break;
1084
1085 col ++;
1086 line ++;
1087 }
1088
1089 for (i = 1; start < line; i ++, start ++)
1090 if (!isspace(start->ch & 255))
1091 write_string(col - i, row, 1, start);
1092 }
1093 }
1094 }
1095 }
1096
1097
1098 /*
1099 * 'write_string()' - Write a string of text.
1100 */
1101
1102 static void
1103 write_string(int col, /* I - Start column */
1104 int row, /* I - Row */
1105 int len, /* I - Number of characters */
1106 lchar_t *s) /* I - String to print */
1107 {
1108 int ch; /* Current character */
1109 float x, y; /* Position of text */
1110 unsigned attr; /* Character attributes */
1111
1112
1113 /*
1114 * Position the text and set the font...
1115 */
1116
1117 if (Duplex && (NumPages & 1) == 0)
1118 {
1119 x = PageWidth - PageRight;
1120 y = PageTop;
1121 }
1122 else
1123 {
1124 x = PageLeft;
1125 y = PageTop;
1126 }
1127
1128 x += (float)col * 72.0f / (float)CharsPerInch;
1129 y -= (float)(row + 0.843) * 72.0f / (float)LinesPerInch;
1130
1131 attr = s->attr;
1132
1133 if (attr & ATTR_RAISED)
1134 y += 36.0 / (float)LinesPerInch;
1135 else if (attr & ATTR_LOWERED)
1136 y -= 36.0 / (float)LinesPerInch;
1137
1138 if (x == (int)x)
1139 printf("%.0f ", x);
1140 else
1141 printf("%.3f ", x);
1142
1143 if (y == (int)y)
1144 printf("%.0f ", y);
1145 else
1146 printf("%.3f ", y);
1147
1148 if (attr & ATTR_BOLD)
1149 putchar('B');
1150 else if (attr & ATTR_ITALIC)
1151 putchar('I');
1152 else
1153 putchar('N');
1154
1155 if (attr & ATTR_UNDERLINE)
1156 printf(" %.3f U", (float)len * 72.0 / (float)CharsPerInch);
1157
1158 if (NumFonts > 1)
1159 {
1160 /*
1161 * Write a hex string...
1162 */
1163
1164 putchar('<');
1165
1166 while (len > 0)
1167 {
1168 printf("%04x", Chars[s->ch]);
1169
1170 len --;
1171 s ++;
1172 }
1173
1174 putchar('>');
1175 }
1176 else
1177 {
1178 /*
1179 * Write a quoted string...
1180 */
1181
1182 putchar('(');
1183
1184 while (len > 0)
1185 {
1186 ch = Chars[s->ch];
1187
1188 if (ch < 32 || ch > 126)
1189 {
1190 /*
1191 * Quote 8-bit and control characters...
1192 */
1193
1194 printf("\\%03o", ch);
1195 }
1196 else
1197 {
1198 /*
1199 * Quote the parenthesis and backslash as needed...
1200 */
1201
1202 if (ch == '(' || ch == ')' || ch == '\\')
1203 putchar('\\');
1204
1205 putchar(ch);
1206 }
1207
1208 len --;
1209 s ++;
1210 }
1211
1212 putchar(')');
1213 }
1214
1215 if (PrettyPrint)
1216 {
1217 if (attr & ATTR_RED)
1218 puts("r");
1219 else if (attr & ATTR_GREEN)
1220 puts("g");
1221 else if (attr & ATTR_BLUE)
1222 puts("b");
1223 else
1224 puts("S");
1225 }
1226 else
1227 puts("S");
1228 }
1229
1230
1231 /*
1232 * 'write_text()' - Write a text string, quoting/encoding as needed.
1233 */
1234
1235 static void
1236 write_text(const char *s) /* I - String to write */
1237 {
1238 int ch; /* Actual character value (UTF8) */
1239 const unsigned char *utf8; /* UTF8 text */
1240
1241
1242 if (NumFonts > 1)
1243 {
1244 /*
1245 * 8/8 encoding...
1246 */
1247
1248 putchar('<');
1249
1250 utf8 = (const unsigned char *)s;
1251
1252 while (*utf8)
1253 {
1254 if (*utf8 < 0xc0 || !UTF8)
1255 ch = *utf8 ++;
1256 else if ((*utf8 & 0xe0) == 0xc0)
1257 {
1258 /*
1259 * Two byte character...
1260 */
1261
1262 ch = ((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f);
1263 utf8 += 2;
1264 }
1265 else
1266 {
1267 /*
1268 * Three byte character...
1269 */
1270
1271 ch = ((((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f)) << 6) |
1272 (utf8[2] & 0x3f);
1273 utf8 += 3;
1274 }
1275
1276 printf("%04x", Chars[ch]);
1277 }
1278
1279 putchar('>');
1280 }
1281 else
1282 {
1283 /*
1284 * Standard 8-bit encoding...
1285 */
1286
1287 putchar('(');
1288
1289 while (*s)
1290 {
1291 if (*s < 32 || *s > 126)
1292 printf("\\%03o", *s);
1293 else
1294 {
1295 if (*s == '(' || *s == ')' || *s == '\\')
1296 putchar('\\');
1297
1298 putchar(*s);
1299 }
1300
1301 s ++;
1302 }
1303
1304 putchar(')');
1305 }
1306 }
1307
1308
1309 /*
1310 * End of "$Id: texttops.c 4680 2005-09-21 09:28:39Z mike $".
1311 */