]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/texttops.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / filter / texttops.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: texttops.c 7720 2008-07-11 22:46:21Z mike $"
ef416fc2 3 *
4 * Text to PostScript filter for the Common UNIX Printing System (CUPS).
5 *
1f0275e3 6 * Copyright 2007-2008 by Apple Inc.
f7deaa1a 7 * Copyright 1993-2007 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Main entry for text to PostScript filter.
20 * WriteEpilogue() - Write the PostScript file epilogue.
21 * WritePage() - Write a page of text.
22 * WriteProlog() - Write the PostScript file prolog with options.
23 * write_line() - Write a row of text.
24 * write_string() - Write a string of text.
25 */
26
27/*
28 * Include necessary headers...
29 */
30
31#include "textcommon.h"
c0e1af83 32#include <cups/i18n.h>
ef416fc2 33
34
35/*
36 * Globals...
37 */
38
39char *Glyphs[65536]; /* PostScript glyphs for Unicode */
40int NumFonts; /* Number of fonts to use */
41char *Fonts[256][4]; /* Fonts to use */
42unsigned short Chars[65536]; /* 0xffcc (ff = font, cc = char) */
43unsigned short Codes[65536]; /* Unicode glyph mapping to fonts */
44int Widths[256]; /* Widths of each font */
45int Directions[256];/* Text directions for each font */
46
47
48/*
49 * Local functions...
50 */
51
52static void write_line(int row, lchar_t *line);
53static void write_string(int col, int row, int len, lchar_t *s);
54static void write_text(const char *s);
55
56
57/*
58 * 'main()' - Main entry for text to PostScript filter.
59 */
60
61int /* O - Exit status */
62main(int argc, /* I - Number of command-line arguments */
63 char *argv[]) /* I - Command-line arguments */
64{
65 return (TextMain("texttops", argc, argv));
66}
67
68
69/*
70 * 'WriteEpilogue()' - Write the PostScript file epilogue.
71 */
72
73void
74WriteEpilogue(void)
75{
76 puts("%%Trailer");
77 printf("%%%%Pages: %d\n", NumPages);
78 puts("%%EOF");
79
80 free(Page[0]);
81 free(Page);
82}
83
84
85/*
86 * 'WritePage()' - Write a page of text.
87 */
88
89void
90WritePage(void)
91{
92 int line; /* Current line */
93
94
95 NumPages ++;
96 printf("%%%%Page: %d %d\n", NumPages, NumPages);
97
98 puts("gsave");
99
100 if (PrettyPrint)
101 printf("%d H\n", NumPages);
102
103 for (line = 0; line < SizeLines; line ++)
104 write_line(line, Page[line]);
105
106 puts("grestore");
107 puts("showpage");
108
109 memset(Page[0], 0, sizeof(lchar_t) * SizeColumns * SizeLines);
110}
111
112
113/*
114 * 'WriteProlog()' - Write the PostScript file prolog with options.
115 */
116
117void
118WriteProlog(const char *title, /* I - Title of job */
119 const char *user, /* I - Username */
120 const char *classification, /* I - Classification */
121 const char *label, /* I - Page label */
122 ppd_file_t *ppd) /* I - PPD file info */
123{
124 int i, j, k; /* Looping vars */
125 char *charset; /* Character set string */
126 char filename[1024]; /* Glyph filenames */
127 FILE *fp; /* Glyph files */
128 const char *datadir; /* CUPS_DATADIR environment variable */
129 char line[1024], /* Line from file */
130 *lineptr, /* Pointer into line */
131 *valptr; /* Pointer to value in line */
132 int ch, unicode; /* Character values */
133 int start, end; /* Start and end values for range */
134 char glyph[64]; /* Glyph name */
135 time_t curtime; /* Current time */
136 struct tm *curtm; /* Current date */
137 char curdate[255]; /* Current date (text format) */
138 int num_fonts; /* Number of unique fonts */
139 char *fonts[1024]; /* Unique fonts */
140 static char *names[] = /* Font names */
141 {
142 "cupsNormal",
143 "cupsBold",
144 "cupsItalic"
145 };
146
147
148 /*
149 * Get the data directory...
150 */
151
152 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
153 datadir = CUPS_DATADIR;
154
155 /*
156 * Adjust margins as necessary...
157 */
158
159 if (classification || label)
160 {
161 /*
162 * Leave room for labels...
163 */
164
165 PageBottom += 36;
166 PageTop -= 36;
167 }
168
169 /*
170 * Allocate memory for the page...
171 */
172
173 SizeColumns = (PageRight - PageLeft) / 72.0 * CharsPerInch;
174 SizeLines = (PageTop - PageBottom) / 72.0 * LinesPerInch;
175
176 Page = calloc(sizeof(lchar_t *), SizeLines);
177 Page[0] = calloc(sizeof(lchar_t), SizeColumns * SizeLines);
178 for (i = 1; i < SizeLines; i ++)
179 Page[i] = Page[0] + i * SizeColumns;
180
181 if (PageColumns > 1)
182 {
183 ColumnGutter = CharsPerInch / 2;
184 ColumnWidth = (SizeColumns - ColumnGutter * (PageColumns - 1)) /
185 PageColumns;
186 }
187 else
188 ColumnWidth = SizeColumns;
189
190 /*
191 * Output the DSC header...
192 */
193
194 curtime = time(NULL);
195 curtm = localtime(&curtime);
196 strftime(curdate, sizeof(curdate), "%c", curtm);
197
198 puts("%!PS-Adobe-3.0");
199 printf("%%%%BoundingBox: 0 0 %.0f %.0f\n", PageWidth, PageLength);
200 printf("%%cupsRotation: %d\n", (Orientation & 3) * 90);
201 puts("%%Creator: texttops/" CUPS_SVERSION);
202 printf("%%%%CreationDate: %s\n", curdate);
2abf387c 203 WriteTextComment("Title", title);
204 WriteTextComment("For", user);
ef416fc2 205 puts("%%Pages: (atend)");
206
207 /*
208 * Initialize globals...
209 */
210
211 NumFonts = 0;
212 memset(Fonts, 0, sizeof(Fonts));
213 memset(Glyphs, 0, sizeof(Glyphs));
214 memset(Chars, 0, sizeof(Chars));
215 memset(Codes, 0, sizeof(Codes));
216
217 /*
218 * Load the PostScript glyph names and the corresponding character
219 * set definition...
220 */
221
222 snprintf(filename, sizeof(filename), "%s/data/psglyphs", datadir);
223
224 if ((fp = fopen(filename, "r")) != NULL)
225 {
226 while (fscanf(fp, "%x%63s", &unicode, glyph) == 2)
227 Glyphs[unicode] = strdup(glyph);
228
229 fclose(fp);
230 }
231 else
232 {
c0e1af83 233 fprintf(stderr, _("ERROR: Unable to open \"%s\" - %s\n"), filename,
ef416fc2 234 strerror(errno));
235 exit(1);
236 }
237
238 /*
239 * Get the output character set...
240 */
241
242 charset = getenv("CHARSET");
243 if (charset != NULL && strcmp(charset, "us-ascii") != 0)
244 {
245 snprintf(filename, sizeof(filename), "%s/charsets/%s", datadir, charset);
246
247 if ((fp = fopen(filename, "r")) == NULL)
248 {
249 /*
250 * Can't open charset file!
251 */
252
c0e1af83 253 fprintf(stderr, _("ERROR: Unable to open %s: %s\n"), filename,
ef416fc2 254 strerror(errno));
255 exit(1);
256 }
257
258 /*
259 * Opened charset file; now see if this is really a charset file...
260 */
261
262 if (fgets(line, sizeof(line), fp) == NULL)
263 {
264 /*
265 * Bad/empty charset file!
266 */
267
268 fclose(fp);
c0e1af83 269 fprintf(stderr, _("ERROR: Bad charset file %s\n"), filename);
ef416fc2 270 exit(1);
271 }
272
273 if (strncmp(line, "charset", 7) != 0)
274 {
275 /*
276 * Bad format/not a charset file!
277 */
278
279 fclose(fp);
c0e1af83 280 fprintf(stderr, _("ERROR: Bad charset file %s\n"), filename);
ef416fc2 281 exit(1);
282 }
283
284 /*
285 * See if this is an 8-bit or UTF-8 character set file...
286 */
287
288 line[strlen(line) - 1] = '\0'; /* Drop \n */
289 for (lineptr = line + 7; isspace(*lineptr & 255); lineptr ++); /* Skip whitespace */
290
291 if (strcmp(lineptr, "8bit") == 0)
292 {
293 /*
294 * 8-bit text...
295 */
296
297 UTF8 = 0;
298 NumFonts = 0;
299
300 /*
301 * Read the font description(s)...
302 */
303
304 while (fgets(line, sizeof(line), fp) != NULL)
305 {
306 /*
307 * Skip comment and blank lines...
308 */
309
310 if (line[0] == '#' || line[0] == '\n')
311 continue;
312
313 /*
314 * Read the font descriptions that should look like:
315 *
316 * first last direction width normal [bold italic bold-italic]
317 */
318
319 lineptr = line;
320
321 start = strtol(lineptr, &lineptr, 16);
322 end = strtol(lineptr, &lineptr, 16);
323
324 while (isspace(*lineptr & 255))
325 lineptr ++;
326
327 if (!*lineptr)
328 break; /* Must be a font mapping */
329
330 valptr = lineptr;
331
332 while (!isspace(*lineptr & 255) && *lineptr)
333 lineptr ++;
334
335 if (!*lineptr)
336 {
337 /*
338 * Can't have a font without all required values...
339 */
340
c0e1af83 341 fprintf(stderr, _("ERROR: Bad font description line: %s\n"), valptr);
ef416fc2 342 fclose(fp);
343 exit(1);
344 }
345
346 *lineptr++ = '\0';
347
348 if (strcmp(valptr, "ltor") == 0)
349 Directions[NumFonts] = 1;
350 else if (strcmp(valptr, "rtol") == 0)
351 Directions[NumFonts] = -1;
352 else
353 {
c0e1af83 354 fprintf(stderr, _("ERROR: Bad text direction %s\n"), valptr);
ef416fc2 355 fclose(fp);
356 exit(1);
357 }
358
359 /*
360 * Got the direction, now get the width...
361 */
362
363 while (isspace(*lineptr & 255))
364 lineptr ++;
365
366 valptr = lineptr;
367
368 while (!isspace(*lineptr & 255) && *lineptr)
369 lineptr ++;
370
371 if (!*lineptr)
372 {
373 /*
374 * Can't have a font without all required values...
375 */
376
c0e1af83 377 fprintf(stderr, _("ERROR: Bad font description line: %s\n"), valptr);
ef416fc2 378 fclose(fp);
379 exit(1);
380 }
381
382 *lineptr++ = '\0';
383
384 if (strcmp(valptr, "single") == 0)
385 Widths[NumFonts] = 1;
386 else if (strcmp(valptr, "double") == 0)
387 Widths[NumFonts] = 2;
388 else
389 {
c0e1af83 390 fprintf(stderr, _("ERROR: Bad text width %s\n"), valptr);
ef416fc2 391 fclose(fp);
392 exit(1);
393 }
394
395 /*
396 * Get the fonts...
397 */
398
399 for (i = 0; *lineptr && i < 4; i ++)
400 {
401 while (isspace(*lineptr & 255))
402 lineptr ++;
403
404 valptr = lineptr;
405
406 while (!isspace(*lineptr & 255) && *lineptr)
407 lineptr ++;
408
409 if (*lineptr)
410 *lineptr++ = '\0';
411
412 if (lineptr > valptr)
413 Fonts[NumFonts][i] = strdup(valptr);
414 }
415
416 /*
417 * Fill in remaining fonts as needed...
418 */
419
420 for (j = i; j < 4; j ++)
421 Fonts[NumFonts][j] = strdup(Fonts[NumFonts][0]);
422
423 /*
424 * Define the character mappings...
425 */
426
427 for (i = start, j = NumFonts * 256; i <= end; i ++, j ++)
428 Chars[i] = j;
429
430 NumFonts ++;
431 }
432
433 /*
434 * Read encoding lines...
435 */
436
437 do
438 {
439 /*
440 * Skip comment and blank lines...
441 */
442
443 if (line[0] == '#' || line[0] == '\n')
444 continue;
445
446 /*
447 * Grab the character and unicode glyph number.
448 */
449
450 if (sscanf(line, "%x%x", &ch, &unicode) == 2 && ch < 256)
451 Codes[Chars[ch]] = unicode;
452 }
453 while (fgets(line, sizeof(line), fp) != NULL);
454
455 fclose(fp);
456 }
457 else if (strcmp(lineptr, "utf8") == 0)
458 {
459 /*
460 * UTF-8 (Unicode) text...
461 */
462
463 UTF8 = 1;
464
465 /*
466 * Read the font descriptions...
467 */
468
469 NumFonts = 0;
470
471 while (fgets(line, sizeof(line), fp) != NULL)
472 {
473 /*
474 * Skip comment and blank lines...
475 */
476
477 if (line[0] == '#' || line[0] == '\n')
478 continue;
479
480 /*
481 * Read the font descriptions that should look like:
482 *
483 * start end direction width normal [bold italic bold-italic]
484 */
485
486 lineptr = line;
487
488 start = strtol(lineptr, &lineptr, 16);
489 end = strtol(lineptr, &lineptr, 16);
490
491 while (isspace(*lineptr & 255))
492 lineptr ++;
493
494 valptr = lineptr;
495
496 while (!isspace(*lineptr & 255) && *lineptr)
497 lineptr ++;
498
499 if (!*lineptr)
500 {
501 /*
502 * Can't have a font without all required values...
503 */
504
c0e1af83 505 fprintf(stderr, _("ERROR: Bad font description line: %s\n"), valptr);
ef416fc2 506 fclose(fp);
507 exit(1);
508 }
509
510 *lineptr++ = '\0';
511
512 if (strcmp(valptr, "ltor") == 0)
513 Directions[NumFonts] = 1;
514 else if (strcmp(valptr, "rtol") == 0)
515 Directions[NumFonts] = -1;
516 else
517 {
c0e1af83 518 fprintf(stderr, _("ERROR: Bad text direction %s\n"), valptr);
ef416fc2 519 fclose(fp);
520 exit(1);
521 }
522
523 /*
524 * Got the direction, now get the width...
525 */
526
527 while (isspace(*lineptr & 255))
528 lineptr ++;
529
530 valptr = lineptr;
531
532 while (!isspace(*lineptr & 255) && *lineptr)
533 lineptr ++;
534
535 if (!*lineptr)
536 {
537 /*
538 * Can't have a font without all required values...
539 */
540
c0e1af83 541 fprintf(stderr, _("ERROR: Bad font description line: %s\n"), valptr);
ef416fc2 542 fclose(fp);
543 exit(1);
544 }
545
546 *lineptr++ = '\0';
547
548 if (strcmp(valptr, "single") == 0)
549 Widths[NumFonts] = 1;
550 else if (strcmp(valptr, "double") == 0)
551 Widths[NumFonts] = 2;
552 else
553 {
c0e1af83 554 fprintf(stderr, _("ERROR: Bad text width %s\n"), valptr);
ef416fc2 555 fclose(fp);
556 exit(1);
557 }
558
559 /*
560 * Get the fonts...
561 */
562
563 for (i = 0; *lineptr && i < 4; i ++)
564 {
565 while (isspace(*lineptr & 255))
566 lineptr ++;
567
568 valptr = lineptr;
569
570 while (!isspace(*lineptr & 255) && *lineptr)
571 lineptr ++;
572
573 if (*lineptr)
574 *lineptr++ = '\0';
575
576 if (lineptr > valptr)
577 Fonts[NumFonts][i] = strdup(valptr);
578 }
579
580 /*
581 * Fill in remaining fonts as needed...
582 */
583
584 for (j = i; j < 4; j ++)
585 Fonts[NumFonts][j] = strdup(Fonts[NumFonts][0]);
586
587 /*
588 * Define the character mappings...
589 */
590
591 for (i = start, j = NumFonts * 256; i <= end; i ++, j ++)
592 {
593 Chars[i] = j;
594 Codes[j] = i;
595 }
596
597 /*
598 * Move to the next font, stopping if needed...
599 */
600
601 NumFonts ++;
602 if (NumFonts >= 256)
603 break;
604 }
605
606 fclose(fp);
607 }
608 else
609 {
c0e1af83 610 fprintf(stderr, _("ERROR: Bad charset type %s\n"), lineptr);
ef416fc2 611 fclose(fp);
612 exit(1);
613 }
614 }
615 else
616 {
617 /*
618 * Standard ASCII output just uses Courier, Courier-Bold, and
619 * possibly Courier-Oblique.
620 */
621
622 NumFonts = 1;
623
624 Fonts[0][ATTR_NORMAL] = strdup("Courier");
625 Fonts[0][ATTR_BOLD] = strdup("Courier-Bold");
626 Fonts[0][ATTR_ITALIC] = strdup("Courier-Oblique");
627 Fonts[0][ATTR_BOLDITALIC] = strdup("Courier-BoldOblique");
628
629 Widths[0] = 1;
630 Directions[0] = 1;
631
632 /*
633 * Define US-ASCII characters...
634 */
635
636 for (i = 32; i < 127; i ++)
637 {
638 Chars[i] = i;
639 Codes[i] = i;
640 }
641 }
642
643 /*
644 * Generate a list of unique fonts to use...
645 */
646
647 for (i = 0, num_fonts = 0; i < NumFonts; i ++)
648 for (j = PrettyPrint ? 2 : 1; j >= 0; j --)
649 {
650 for (k = 0; k < num_fonts; k ++)
651 if (strcmp(Fonts[i][j], fonts[k]) == 0)
652 break;
653
654 if (k >= num_fonts)
655 {
656 /*
657 * Add new font...
658 */
659
660 fonts[num_fonts] = Fonts[i][j];
661 num_fonts ++;
662 }
663 }
664
665 /*
666 * List the fonts that will be used...
667 */
668
669 for (i = 0; i < num_fonts; i ++)
670 if (i == 0)
671 printf("%%%%DocumentNeededResources: font %s\n", fonts[i]);
672 else
673 printf("%%%%+ font %s\n", fonts[i]);
674
675 puts("%%DocumentSuppliedResources: procset texttops 1.1 0");
676
677 for (i = 0; i < num_fonts; i ++)
678 {
679 if (ppd != NULL)
680 {
681 fprintf(stderr, "DEBUG: ppd->num_fonts = %d\n", ppd->num_fonts);
682
683 for (j = 0; j < ppd->num_fonts; j ++)
684 {
685 fprintf(stderr, "DEBUG: ppd->fonts[%d] = %s\n", j, ppd->fonts[j]);
686
687 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
688 break;
689 }
690 }
691 else
692 j = 0;
693
694 if ((ppd != NULL && j >= ppd->num_fonts) ||
695 strncmp(fonts[i], "Courier", 7) == 0 ||
696 strcmp(fonts[i], "Symbol") == 0)
697 {
698 /*
699 * Need to embed this font...
700 */
701
702 printf("%%%%+ font %s\n", fonts[i]);
703 }
704 }
705
706 puts("%%EndComments");
707
708 puts("%%BeginProlog");
709
710 /*
711 * Download any missing fonts...
712 */
713
714 for (i = 0; i < num_fonts; i ++)
715 {
716 if (ppd != NULL)
717 {
718 for (j = 0; j < ppd->num_fonts; j ++)
719 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
720 break;
721 }
722 else
723 j = 0;
724
725 if ((ppd != NULL && j >= ppd->num_fonts) ||
726 strncmp(fonts[i], "Courier", 7) == 0 ||
727 strcmp(fonts[i], "Symbol") == 0)
728 {
729 /*
730 * Need to embed this font...
731 */
732
733 printf("%%%%BeginResource: font %s\n", fonts[i]);
734
735 /**** MRS: Need to use CUPS_FONTPATH env var! ****/
736 /**** Also look for Fontmap file or name.pfa, name.pfb... ****/
737 snprintf(filename, sizeof(filename), "%s/fonts/%s", datadir, fonts[i]);
738 if ((fp = fopen(filename, "rb")) != NULL)
739 {
740 while ((j = fread(line, 1, sizeof(line), fp)) > 0)
741 fwrite(line, 1, j, stdout);
742
743 fclose(fp);
744 }
745
746 puts("\n%%EndResource");
747 }
748 }
749
750 /*
751 * Write the encoding array(s)...
752 */
753
754 puts("% character encoding(s)");
755
756 for (i = 0; i < NumFonts; i ++)
757 {
758 printf("/cupsEncoding%02x [\n", i);
759
760 for (ch = 0; ch < 256; ch ++)
761 {
762 if (Glyphs[Codes[i * 256 + ch]])
763 printf("/%s", Glyphs[Codes[i * 256 + ch]]);
764 else if (Codes[i * 256 + ch] > 255)
765 printf("/uni%04X", 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
f7deaa1a 807 for (i = PrettyPrint ? 2 : 1; i >= 0; i --)
ef416fc2 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
f7deaa1a 826 for (i = PrettyPrint ? 2 : 1; i >= 0; i --)
ef416fc2 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
995static void
996write_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
1f0275e3 1008 for (col = 0; col < SizeColumns;)
ef416fc2 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 & 255) && !isspace(line->ch & 255))
1075 break;
1076
1077 col ++;
1078 line ++;
1079 }
1080
1081 for (i = 1; start < line; i ++, start ++)
1082 if (!isspace(start->ch & 255))
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
1094static void
1095write_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
1227static void
1228write_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/*
b19ccc9e 1302 * End of "$Id: texttops.c 7720 2008-07-11 22:46:21Z mike $".
ef416fc2 1303 */