]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/texttops.c
Merge changes from 1.1 tree.
[thirdparty/cups.git] / filter / texttops.c
CommitLineData
070c29a1 1/*
b5cb0608 2 * "$Id: texttops.c,v 1.34.2.1 2001/05/13 18:38:21 mike Exp $"
070c29a1 3 *
e2a47002 4 * Text to PostScript filter for the Common UNIX Printing System (CUPS).
070c29a1 5 *
d2935a0f 6 * Copyright 1993-2001 by Easy Software Products.
070c29a1 7 *
e2a47002 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:
070c29a1 14 *
e2a47002 15 * Attn: CUPS Licensing Information
16 * Easy Software Products
58ec2a95 17 * 44141 Airport View Drive, Suite 204
e2a47002 18 * Hollywood, Maryland 20636-3111 USA
77bdfb41 19 *
e2a47002 20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
57945030 23 *
e2a47002 24 * Contents:
070c29a1 25 *
2d35d8ee 26 * main() - Main entry for text to PostScript filter.
27 * WriteEpilogue() - Write the PostScript file epilogue.
28 * WritePage() - Write a page of text.
29 * WriteProlog() - Write the PostScript file prolog with options.
30 * write_line() - Write a row of text.
31 * write_string() - Write a string of text.
070c29a1 32 */
33
34/*
35 * Include necessary headers...
36 */
37
2d35d8ee 38#include "textcommon.h"
070c29a1 39
40
41/*
42 * Globals...
43 */
44
3b9a00b0 45char *Glyphs[65536]; /* PostScript glyphs for Unicode */
46int NumFonts; /* Number of fonts to use */
47char *Fonts[256][4]; /* Fonts to use */
48unsigned short Chars[65536]; /* 0xffcc (ff = font, cc = char) */
49unsigned short Codes[65536]; /* Unicode glyph mapping to fonts */
50int Widths[256]; /* Widths of each font */
51int Directions[256];/* Text directions for each font */
52
e2a47002 53
54/*
55 * Local functions...
56 */
57
58ec2a95 58static void write_line(int row, lchar_t *line);
58ec2a95 59static void write_string(int col, int row, int len, lchar_t *s);
b5cb0608 60static void write_text(const char *s);
58ec2a95 61
62
63/*
2d35d8ee 64 * 'main()' - Main entry for text to PostScript filter.
77dbfbdc 65 */
66
2d35d8ee 67int /* O - Exit status */
68main(int argc, /* I - Number of command-line arguments */
69 char *argv[]) /* I - Command-line arguments */
77dbfbdc 70{
2d35d8ee 71 return (TextMain("texttops", argc, argv));
77dbfbdc 72}
e2a47002 73
e2a47002 74
77dbfbdc 75/*
2d35d8ee 76 * 'WriteEpilogue()' - Write the PostScript file epilogue.
77dbfbdc 77 */
e2a47002 78
2d35d8ee 79void
80WriteEpilogue(void)
77dbfbdc 81{
82 puts("%%BeginTrailer");
83 printf("%%%%Pages: %d\n", NumPages);
84 puts("%%EOF");
e2a47002 85
77dbfbdc 86 free(Page[0]);
87 free(Page);
88}
89
90
91/*
2d35d8ee 92 * 'WritePage()' - Write a page of text.
77dbfbdc 93 */
94
2d35d8ee 95void
96WritePage(void)
070c29a1 97{
77dbfbdc 98 int line; /* Current line */
57945030 99
070c29a1 100
77dbfbdc 101 NumPages ++;
102 printf("%%%%Page: %d %d\n", NumPages, NumPages);
070c29a1 103
77dbfbdc 104 puts("gsave");
070c29a1 105
77dbfbdc 106 if (PrettyPrint)
107 printf("%d H\n", NumPages);
070c29a1 108
77dbfbdc 109 for (line = 0; line < SizeLines; line ++)
110 write_line(line, Page[line]);
070c29a1 111
77dbfbdc 112 puts("grestore");
113 puts("showpage");
070c29a1 114
77dbfbdc 115 memset(Page[0], 0, sizeof(lchar_t) * SizeColumns * SizeLines);
116}
070c29a1 117
f6bdbd14 118
77dbfbdc 119/*
2d35d8ee 120 * 'WriteProlog()' - Write the PostScript file prolog with options.
77dbfbdc 121 */
f6bdbd14 122
2d35d8ee 123void
b5cb0608 124WriteProlog(const char *title, /* I - Title of job */
125 const char *user, /* I - Username */
126 const char *classification, /* I - Classification */
127 const char *label, /* I - Page label */
128 ppd_file_t *ppd) /* I - PPD file info */
77dbfbdc 129{
3b9a00b0 130 int i, j, k; /* Looping vars */
77dbfbdc 131 char *charset; /* Character set string */
77dbfbdc 132 char filename[1024]; /* Glyph filenames */
133 FILE *fp; /* Glyph files */
571292aa 134 const char *datadir; /* CUPS_DATADIR environment variable */
3b9a00b0 135 char line[1024], /* Line from file */
136 *lineptr, /* Pointer into line */
137 *valptr; /* Pointer to value in line */
77dbfbdc 138 int ch, unicode; /* Character values */
3b9a00b0 139 int start, end; /* Start and end values for range */
77dbfbdc 140 char glyph[64]; /* Glyph name */
77dbfbdc 141 time_t curtime; /* Current time */
142 struct tm *curtm; /* Current date */
143 char curdate[255]; /* Current date (text format) */
3b9a00b0 144 int num_fonts; /* Number of unique fonts */
145 char *fonts[1024]; /* Unique fonts */
146 static char *names[] = /* Font names */
147 {
148 "cupsNormal",
149 "cupsBold",
150 "cupsItalic"
151 };
152
153
571292aa 154 /*
155 * Get the data directory...
156 */
157
158 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
159 datadir = CUPS_DATADIR;
160
b5cb0608 161 /*
162 * Adjust margins as necessary...
163 */
164
165 if (classification || label)
166 {
167 /*
168 * Leave room for labels...
169 */
170
171 PageBottom += 36;
172 PageTop -= 36;
173 }
174
3b9a00b0 175 /*
176 * Allocate memory for the page...
177 */
178
179 SizeColumns = (PageRight - PageLeft) / 72.0 * CharsPerInch;
180 SizeLines = (PageTop - PageBottom) / 72.0 * LinesPerInch;
181
182 Page = calloc(sizeof(lchar_t *), SizeLines);
183 Page[0] = calloc(sizeof(lchar_t), SizeColumns * SizeLines);
184 for (i = 1; i < SizeLines; i ++)
185 Page[i] = Page[0] + i * SizeColumns;
186
187 if (PageColumns > 1)
188 {
189 ColumnGutter = CharsPerInch / 2;
190 ColumnWidth = (SizeColumns - ColumnGutter * (PageColumns - 1)) /
191 PageColumns;
192 }
193 else
194 ColumnWidth = SizeColumns;
f6bdbd14 195
3b9a00b0 196 /*
197 * Output the DSC header...
198 */
f6bdbd14 199
77dbfbdc 200 curtime = time(NULL);
201 curtm = localtime(&curtime);
8a32fc08 202 strftime(curdate, sizeof(curdate), CUPS_STRFTIME_FORMAT, curtm);
f6bdbd14 203
77dbfbdc 204 puts("%!PS-Adobe-3.0");
205 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,
206 PageRight, PageTop);
207 if (Orientation & 1)
208 puts("%%Orientation: Landscape");
13fd65af 209 puts("%%Creator: texttops/" CUPS_SVERSION);
77dbfbdc 210 printf("%%%%CreationDate: %s\n", curdate);
211 printf("%%%%Title: %s\n", title);
212 printf("%%%%For: %s\n", user);
77dbfbdc 213 puts("%%Pages: (atend)");
f6bdbd14 214
3b9a00b0 215 /*
216 * Initialize globals...
217 */
f6bdbd14 218
3b9a00b0 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));
f6bdbd14 224
3b9a00b0 225 /*
226 * Load the PostScript glyph names and the corresponding character
227 * set definition...
228 */
58ec2a95 229
571292aa 230 snprintf(filename, sizeof(filename), "%s/data/psglyphs", datadir);
231
232 if ((fp = fopen(filename, "r")) != NULL)
f6bdbd14 233 {
3b9a00b0 234 while (fscanf(fp, "%x%63s", &unicode, glyph) == 2)
235 Glyphs[unicode] = strdup(glyph);
236
237 fclose(fp);
f6bdbd14 238 }
77dbfbdc 239 else
3b9a00b0 240 {
571292aa 241 fprintf(stderr, "ERROR: Unable to open \"%s\" - %s\n", filename,
242 strerror(errno));
3b9a00b0 243 exit(1);
244 }
070c29a1 245
246 /*
3b9a00b0 247 * Get the output character set...
070c29a1 248 */
249
77dbfbdc 250 charset = getenv("CHARSET");
251 if (charset != NULL && strcmp(charset, "us-ascii") != 0)
070c29a1 252 {
571292aa 253 snprintf(filename, sizeof(filename), "%s/charsets/%s", datadir, charset);
3b9a00b0 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
58ec2a95 266 /*
3b9a00b0 267 * Opened charset file; now see if this is really a charset file...
58ec2a95 268 */
070c29a1 269
3b9a00b0 270 if (fgets(line, sizeof(line), fp) == NULL)
271 {
272 /*
273 * Bad/empty charset file!
274 */
58ec2a95 275
3b9a00b0 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)
77dbfbdc 282 {
3b9a00b0 283 /*
284 * Bad format/not a charset file!
285 */
58ec2a95 286
77dbfbdc 287 fclose(fp);
3b9a00b0 288 fprintf(stderr, "ERROR: Bad charset file %s\n", filename);
289 exit(1);
77dbfbdc 290 }
f6bdbd14 291
3b9a00b0 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)
77dbfbdc 300 {
3b9a00b0 301 /*
302 * 8-bit text...
303 */
304
305 UTF8 = 0;
3d9e2586 306 NumFonts = 0;
3b9a00b0 307
308 /*
3d9e2586 309 * Read the font description(s)...
3b9a00b0 310 */
311
312 while (fgets(line, sizeof(line), fp) != NULL)
313 {
314 /*
315 * Skip comment and blank lines...
316 */
317
3d9e2586 318 if (line[0] == '#' || line[0] == '\n')
319 continue;
58ec2a95 320
3d9e2586 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);
f6bdbd14 331
3d9e2586 332 while (isspace(*lineptr))
333 lineptr ++;
3b9a00b0 334
3d9e2586 335 if (!*lineptr)
972c5ace 336 break; /* Must be a font mapping */
3b9a00b0 337
3d9e2586 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 }
3b9a00b0 366
3b9a00b0 367 /*
3d9e2586 368 * Got the direction, now get the width...
3b9a00b0 369 */
f6bdbd14 370
3d9e2586 371 while (isspace(*lineptr))
372 lineptr ++;
3b9a00b0 373
3d9e2586 374 valptr = lineptr;
3b9a00b0 375
3d9e2586 376 while (!isspace(*lineptr) && *lineptr)
377 lineptr ++;
3b9a00b0 378
3d9e2586 379 if (!*lineptr)
380 {
381 /*
382 * Can't have a font without all required values...
383 */
3b9a00b0 384
3d9e2586 385 fprintf(stderr, "ERROR: bad font description line: %s\n", valptr);
386 fclose(fp);
387 exit(1);
388 }
3b9a00b0 389
3d9e2586 390 *lineptr++ = '\0';
3b9a00b0 391
3d9e2586 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 }
3b9a00b0 402
3fb23871 403 /*
3d9e2586 404 * Get the fonts...
3fb23871 405 */
406
3d9e2586 407 for (i = 0; *lineptr && i < 4; i ++)
408 {
409 while (isspace(*lineptr))
410 lineptr ++;
3b9a00b0 411
3d9e2586 412 valptr = lineptr;
3b9a00b0 413
3d9e2586 414 while (!isspace(*lineptr) && *lineptr)
415 lineptr ++;
3b9a00b0 416
3d9e2586 417 if (*lineptr)
418 *lineptr++ = '\0';
3b9a00b0 419
3d9e2586 420 if (lineptr > valptr)
421 Fonts[NumFonts][i] = strdup(valptr);
422 }
3b9a00b0 423
3d9e2586 424 /*
425 * Fill in remaining fonts as needed...
426 */
3b9a00b0 427
3d9e2586 428 for (j = i; j < 4; j ++)
429 Fonts[NumFonts][j] = strdup(Fonts[NumFonts][0]);
3b9a00b0 430
3d9e2586 431 /*
432 * Define the character mappings...
433 */
3b9a00b0 434
3d9e2586 435 for (i = start, j = NumFonts * 256; i <= end; i ++, j ++)
436 Chars[i] = j;
3b9a00b0 437
3d9e2586 438 NumFonts ++;
439 }
3b9a00b0 440
441 /*
442 * Read encoding lines...
443 */
444
3d9e2586 445 do
3b9a00b0 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)
3d9e2586 459 Codes[Chars[ch]] = unicode;
3b9a00b0 460 }
3d9e2586 461 while (fgets(line, sizeof(line), fp) != NULL);
3b9a00b0 462
463 fclose(fp);
77dbfbdc 464 }
3b9a00b0 465 else if (strcmp(lineptr, "utf8") == 0)
77dbfbdc 466 {
467 /*
3b9a00b0 468 * UTF-8 (Unicode) text...
77dbfbdc 469 */
f6bdbd14 470
77dbfbdc 471 UTF8 = 1;
f6bdbd14 472
3b9a00b0 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);
77dbfbdc 621 }
3b9a00b0 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;
f6bdbd14 639
77dbfbdc 640 /*
3b9a00b0 641 * Define US-ASCII characters...
77dbfbdc 642 */
58ec2a95 643
3b9a00b0 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 ++)
e2a4ce3e 656 for (j = PrettyPrint ? 2 : 1; j >= 0; j --)
3b9a00b0 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)
85569ef7 679 printf("%%%%DocumentNeededResources: font %s\n", fonts[i]);
3b9a00b0 680 else
85569ef7 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 }
9d13ae31 699 else
700 j = 0;
85569ef7 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 }
3b9a00b0 711
712 puts("%%EndComments");
713
714 puts("%%BeginProlog");
715
85569ef7 716 /*
717 * Download any missing fonts...
718 */
719
720 for (i = 0; i < num_fonts; i ++)
721 {
722 if (ppd != NULL)
9d13ae31 723 {
85569ef7 724 for (j = 0; j < ppd->num_fonts; j ++)
725 if (strcmp(fonts[i], ppd->fonts[j]) == 0)
726 break;
9d13ae31 727 }
728 else
729 j = 0;
85569ef7 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
571292aa 739 snprintf(filename, sizeof(filename), "%s/fonts/%s", datadir, fonts[i]);
85569ef7 740 if ((fp = fopen(filename, "rb")) != NULL)
741 {
e2a4ce3e 742 while ((j = fread(line, 1, sizeof(line), fp)) > 0)
743 fwrite(line, 1, j, stdout);
85569ef7 744
745 fclose(fp);
746 }
747
e2a4ce3e 748 puts("\n%%EndResource");
85569ef7 749 }
750 }
751
3b9a00b0 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);
58ec2a95 761
77dbfbdc 762 for (ch = 0; ch < 256; ch ++)
763 {
3b9a00b0 764 if (Glyphs[Codes[i * 256 + ch]])
765 printf("/%s", Glyphs[Codes[i * 256 + ch]]);
77dbfbdc 766 else
767 printf("/.notdef");
58ec2a95 768
77dbfbdc 769 if ((ch & 7) == 7)
3b9a00b0 770 putchar('\n');
77dbfbdc 771 }
f6bdbd14 772
77dbfbdc 773 puts("] def");
3b9a00b0 774 }
775
776 /*
777 * Create the fonts...
778 */
779
780 if (NumFonts == 1)
781 {
782 /*
783 * Just reencode the named fonts...
784 */
f6bdbd14 785
77dbfbdc 786 puts("% Reencode fonts");
3b9a00b0 787
e2a4ce3e 788 for (i = PrettyPrint ? 2 : 1; i >= 0; i --)
2d35d8ee 789 {
3b9a00b0 790 printf("/%s findfont\n", Fonts[0][i]);
c18f2cf9 791 puts("dup length 1 add dict begin\n"
3b9a00b0 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]);
c18f2cf9 811 printf("dup length 1 add dict begin\n"
3b9a00b0 812 " { 1 index /FID ne { def } { pop pop } ifelse } forall\n"
813 " /Encoding cupsEncoding%02x def\n"
814 " currentdict\n"
815 "end\n", j);
c18f2cf9 816 printf("/%s%02x exch definefont /%s%02x exch def\n", names[i], j,
817 names[i], j);
3b9a00b0 818 }
819
820 /*
821 * Then merge them into composite fonts...
822 */
823
c18f2cf9 824 puts("% Create composite fonts...");
825
3b9a00b0 826 for (i = 1 + PrettyPrint; i >= 0; i --)
827 {
c18f2cf9 828 puts("8 dict begin");
829 puts("/FontType 0 def/FontMatrix[1.0 0 0 1.0 0 0]def/FMapType 2 def/Encoding[");
3b9a00b0 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))
c18f2cf9 840 printf("%s%02x", names[i], j);
3b9a00b0 841 else if ((j & 3) == 3)
c18f2cf9 842 printf("%s%02x\n", names[i], j);
3b9a00b0 843 else
c18f2cf9 844 printf("%s%02x ", names[i], j);
845 puts("]def currentdict end");
846 printf("/%s exch definefont pop\n", names[i]);
2d35d8ee 847 }
77dbfbdc 848 }
f6bdbd14 849
3b9a00b0 850 /*
851 * Output the texttops procset...
852 */
853
854 puts("%%BeginResource: procset texttops 1.1 0");
855
77dbfbdc 856 puts("% Define fonts");
f6bdbd14 857
3b9a00b0 858 printf("/FN /cupsNormal findfont [%.1f 0 0 %.1f 0 0] makefont def\n",
77dbfbdc 859 120.0 / CharsPerInch, 68.0 / LinesPerInch);
3b9a00b0 860 printf("/FB /cupsBold findfont [%.1f 0 0 %.1f 0 0] makefont def\n",
77dbfbdc 861 120.0 / CharsPerInch, 68.0 / LinesPerInch);
862 if (PrettyPrint)
3b9a00b0 863 printf("/FI /cupsItalic findfont [%.1f 0 0 %.1f 0 0] makefont def\n",
77dbfbdc 864 120.0 / CharsPerInch, 68.0 / LinesPerInch);
f6bdbd14 865
77dbfbdc 866 puts("% Common procedures");
f6bdbd14 867
77dbfbdc 868 puts("/N { FN setfont moveto } bind def");
869 puts("/B { FB setfont moveto } bind def");
6f609fd7 870 printf("/U { gsave 0.5 setlinewidth 0 %.2f rmoveto "
871 "0 rlineto stroke grestore } bind def\n", -6.8 / LinesPerInch);
58ec2a95 872
77dbfbdc 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 }
58ec2a95 889
77dbfbdc 890 puts("/I { FI setfont moveto } bind def");
58ec2a95 891
c18f2cf9 892 puts("/n {");
893 puts("\t20 string cvs % convert page number to string");
894 puts("\tdup length % get length");
895 puts("\tdup 2 mul string /P exch def % P = string twice as long");
896 puts("\t0 1 2 index 1 sub { % loop through each character in the page number");
897 puts("\t\tdup 3 index exch get % get character N from the page number");
898 puts("\t\texch 2 mul dup % compute offset in P");
899 puts("\t\tP exch 0 put % font 0");
900 puts("\t\t1 add P exch 2 index put % character");
901 puts("\t\tpop % discard character");
902 puts("\t} for % do for loop");
903 puts("\tpop pop % discard string and length");
904 puts("\tP % put string on stack");
905 puts("} bind def");
58ec2a95 906
c18f2cf9 907 printf("/T");
908 write_text(title);
909 puts("def");
58ec2a95 910
c18f2cf9 911 printf("/D");
912 write_text(curdate);
913 puts("def");
58ec2a95 914
77dbfbdc 915 puts("/H {");
916 puts("gsave");
917 puts("\t0.9 setgray");
070c29a1 918
77dbfbdc 919 if (Duplex)
920 {
921 puts("\tdup 2 mod 0 eq {");
922 printf("\t\t%.1f %.1f translate } {\n",
923 PageWidth - PageRight, PageTop + 72.0f / LinesPerInch);
924 printf("\t\t%.1f %.1f translate } ifelse\n",
925 PageLeft, PageTop + 72.0f / LinesPerInch);
926 }
2d35d8ee 927 else
928 printf("\t%.1f %.1f translate\n",
929 PageLeft, PageTop + 72.0f / LinesPerInch);
f6bdbd14 930
77dbfbdc 931 printf("\t0 0 %.1f %.1f rectfill\n", PageRight - PageLeft,
932 144.0f / LinesPerInch);
f6bdbd14 933
b1ac64aa 934 puts("\tFB setfont");
77dbfbdc 935 puts("\t0 setgray");
f6bdbd14 936
77dbfbdc 937 if (Duplex)
938 {
939 puts("\tdup 2 mod 0 eq {");
940 printf("\t\tT stringwidth pop neg %.1f add %.1f } {\n",
5d4b6bc1 941 PageRight - PageLeft - 36.0f / LinesPerInch,
942 (0.5f + 0.157f) * 72.0f / LinesPerInch);
77dbfbdc 943 printf("\t\t%.1f %.1f } ifelse\n", 36.0f / LinesPerInch,
5d4b6bc1 944 (0.5f + 0.157f) * 72.0f / LinesPerInch);
77dbfbdc 945 }
946 else
947 printf("\t%.1f %.1f\n", 36.0f / LinesPerInch,
5d4b6bc1 948 (0.5f + 0.157f) * 72.0f / LinesPerInch);
f6bdbd14 949
77dbfbdc 950 puts("\tmoveto T show");
f6bdbd14 951
c18f2cf9 952 printf("\tD dup stringwidth pop neg 2 div %.1f add %.1f\n",
5d4b6bc1 953 (PageRight - PageLeft) * 0.5,
954 (0.5f + 0.157f) * 72.0f / LinesPerInch);
77dbfbdc 955 puts("\tmoveto show");
956
957 if (Duplex)
958 {
c18f2cf9 959 puts("\tdup n exch 2 mod 0 eq {");
77dbfbdc 960 printf("\t\t%.1f %.1f } {\n", 36.0f / LinesPerInch,
5d4b6bc1 961 (0.5f + 0.157f) * 72.0f / LinesPerInch);
77dbfbdc 962 printf("\t\tdup stringwidth pop neg %.1f add %.1f } ifelse\n",
963 PageRight - PageLeft - 36.0f / LinesPerInch,
5d4b6bc1 964 (0.5f + 0.157f) * 72.0f / LinesPerInch);
77dbfbdc 965 }
966 else
c18f2cf9 967 printf("\tn dup stringwidth pop neg %.1f add %.1f\n",
77dbfbdc 968 PageRight - PageLeft - 36.0f / LinesPerInch,
5d4b6bc1 969 (0.5f + 0.157f) * 72.0f / LinesPerInch);
f6bdbd14 970
77dbfbdc 971 puts("\tmoveto show");
972 puts("\tgrestore");
973 puts("} bind def");
974 }
975 else
976 puts("/S { show } bind def");
f6bdbd14 977
77dbfbdc 978 puts("%%EndResource");
f6bdbd14 979
77dbfbdc 980 puts("%%EndProlog");
981}
f6bdbd14 982
f6bdbd14 983
2d35d8ee 984/*
985 * 'write_line()' - Write a row of text.
986 */
987
988static void
989write_line(int row, /* I - Row number (0 to N) */
990 lchar_t *line) /* I - Line to print */
991{
3d9e2586 992 int i; /* Looping var */
2d35d8ee 993 int col; /* Current column */
994 int attr; /* Current attribute */
3d9e2586 995 int font, /* Font to use */
2889d62d 996 lastfont, /* Last font */
3d9e2586 997 mono; /* Monospaced? */
2d35d8ee 998 lchar_t *start; /* First character in sequence */
999
c18f2cf9 1000
2d35d8ee 1001 for (col = 0, start = line; col < SizeColumns;)
1002 {
1003 while (col < SizeColumns && (line->ch == ' ' || line->ch == 0))
1004 {
1005 col ++;
1006 line ++;
1007 }
1008
1009 if (col >= SizeColumns)
1010 break;
1011
3d9e2586 1012 if (NumFonts == 1)
1013 {
1014 /*
1015 * All characters in a single font - assume monospaced...
1016 */
1017
1018 attr = line->attr;
1019 start = line;
1020
1021 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1022 {
1023 col ++;
1024 line ++;
1025 }
2d35d8ee 1026
3d9e2586 1027 write_string(col - (line - start), row, line - start, start);
1028 }
1029 else
2d35d8ee 1030 {
3d9e2586 1031 /*
1032 * Multiple fonts; break up based on the font...
1033 */
1034
2889d62d 1035 attr = line->attr;
1036 start = line;
1037 lastfont = Chars[line->ch] / 256;
1038 mono = strncmp(Fonts[lastfont][0], "Courier", 7) == 0;
2d35d8ee 1039 col ++;
1040 line ++;
2d35d8ee 1041
3d9e2586 1042 if (mono)
1043 {
1044 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1045 {
1046 font = Chars[line->ch] / 256;
2889d62d 1047 if (strncmp(Fonts[font][0], "Courier", 7) != 0 ||
1048 font != lastfont)
3d9e2586 1049 break;
1050
1051 col ++;
1052 line ++;
1053 }
1054 }
1055
2889d62d 1056 if (Directions[lastfont] > 0)
3d9e2586 1057 write_string(col - (line - start), row, line - start, start);
1058 else
1059 {
1060 /*
1061 * Do right-to-left text...
1062 */
1063
1064 while (col < SizeColumns && line->ch != 0 && attr == line->attr)
1065 {
1066 if (Directions[Chars[line->ch] / 256] > 0 &&
1067 !ispunct(line->ch) && !isspace(line->ch))
1068 break;
1069
1070 col ++;
1071 line ++;
1072 }
1073
2889d62d 1074 for (i = 1; start < line; i ++, start ++)
3d9e2586 1075 if (!isspace(start->ch))
1076 write_string(col - i, row, 1, start);
1077 }
1078 }
2d35d8ee 1079 }
1080}
1081
1082
77dbfbdc 1083/*
1084 * 'write_string()' - Write a string of text.
1085 */
f6bdbd14 1086
77dbfbdc 1087static void
1088write_string(int col, /* I - Start column */
1089 int row, /* I - Row */
1090 int len, /* I - Number of characters */
1091 lchar_t *s) /* I - String to print */
1092{
3b9a00b0 1093 int ch; /* Current character */
77dbfbdc 1094 float x, y; /* Position of text */
1095 unsigned attr; /* Character attributes */
f6bdbd14 1096
f6bdbd14 1097
77dbfbdc 1098 /*
1099 * Position the text and set the font...
1100 */
58ec2a95 1101
77dbfbdc 1102 if (Duplex && (NumPages & 1) == 0)
1103 {
1104 x = PageWidth - PageRight;
1105 y = PageTop;
1106 }
1107 else
1108 {
1109 x = PageLeft;
1110 y = PageTop;
1111 }
58ec2a95 1112
b1ac64aa 1113 x += (float)col * 72.0f / (float)CharsPerInch;
5d4b6bc1 1114 y -= (float)(row + 0.843) * 72.0f / (float)LinesPerInch;
070c29a1 1115
77dbfbdc 1116 attr = s->attr;
f6bdbd14 1117
77dbfbdc 1118 if (attr & ATTR_RAISED)
1119 y += 36.0 / (float)LinesPerInch;
1120 else if (attr & ATTR_LOWERED)
1121 y -= 36.0 / (float)LinesPerInch;
58ec2a95 1122
77dbfbdc 1123 if (x == (int)x)
1124 printf("%.0f ", x);
1125 else
1126 printf("%.1f ", x);
77bdfb41 1127
77dbfbdc 1128 if (y == (int)y)
1129 printf("%.0f ", y);
1130 else
1131 printf("%.1f ", y);
070c29a1 1132
77dbfbdc 1133 if (attr & ATTR_BOLD)
1134 putchar('B');
1135 else if (attr & ATTR_ITALIC)
1136 putchar('I');
1137 else
1138 putchar('N');
f6bdbd14 1139
77dbfbdc 1140 if (attr & ATTR_UNDERLINE)
1141 printf(" %.1f U", (float)len * 72.0 / (float)CharsPerInch);
f6bdbd14 1142
3d9e2586 1143 if (NumFonts > 1)
c18f2cf9 1144 {
1145 /*
1146 * Write a hex string...
1147 */
f6bdbd14 1148
c18f2cf9 1149 putchar('<');
f6bdbd14 1150
c18f2cf9 1151 while (len > 0)
1152 {
1153 printf("%04x", Chars[s->ch]);
1154
1155 len --;
1156 s ++;
1157 }
f6bdbd14 1158
c18f2cf9 1159 putchar('>');
1160 }
1161 else
77dbfbdc 1162 {
c18f2cf9 1163 /*
1164 * Write a quoted string...
1165 */
1166
1167 putchar('(');
f6bdbd14 1168
c18f2cf9 1169 while (len > 0)
77dbfbdc 1170 {
c18f2cf9 1171 ch = Chars[s->ch];
1172
1173 if (ch < 32 || ch > 126)
1174 {
1175 /*
1176 * Quote 8-bit and control characters...
1177 */
1178
1179 printf("\\%03o", ch);
1180 }
77dbfbdc 1181 else
1182 {
c18f2cf9 1183 /*
1184 * Quote the parenthesis and backslash as needed...
1185 */
1186
3b9a00b0 1187 if (ch == '(' || ch == ')' || ch == '\\')
77dbfbdc 1188 putchar('\\');
070c29a1 1189
c18f2cf9 1190 putchar(ch);
77dbfbdc 1191 }
3b9a00b0 1192
c18f2cf9 1193 len --;
1194 s ++;
3b9a00b0 1195 }
3b9a00b0 1196
c18f2cf9 1197 putchar(')');
b1ac64aa 1198 }
77dbfbdc 1199
b1ac64aa 1200 if (PrettyPrint)
1201 {
77dbfbdc 1202 if (attr & ATTR_RED)
1203 puts("r");
1204 else if (attr & ATTR_GREEN)
1205 puts("g");
1206 else if (attr & ATTR_BLUE)
1207 puts("b");
1208 else
1209 puts("S");
1210 }
b1ac64aa 1211 else
1212 puts("S");
070c29a1 1213}
1214
1215
1216/*
c18f2cf9 1217 * 'write_text()' - Write a text string, quoting/encoding as needed.
1218 */
1219
1220static void
b5cb0608 1221write_text(const char *s) /* I - String to write */
c18f2cf9 1222{
b5cb0608 1223 int ch; /* Actual character value (UTF8) */
1224 const unsigned char *utf8; /* UTF8 text */
c18f2cf9 1225
1226
3d9e2586 1227 if (NumFonts > 1)
c18f2cf9 1228 {
1229 /*
1230 * 8/8 encoding...
1231 */
1232
1233 putchar('<');
1234
b5cb0608 1235 utf8 = (const unsigned char *)s;
c18f2cf9 1236
1237 while (*utf8)
1238 {
3d9e2586 1239 if (*utf8 < 0xc0 || !UTF8)
c18f2cf9 1240 ch = *utf8 ++;
1241 else if ((*utf8 & 0xe0) == 0xc0)
1242 {
1243 /*
1244 * Two byte character...
1245 */
1246
1247 ch = ((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f);
1248 utf8 += 2;
1249 }
1250 else
1251 {
1252 /*
1253 * Three byte character...
1254 */
1255
1256 ch = ((((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f)) << 6) |
1257 (utf8[2] & 0x3f);
1258 utf8 += 3;
1259 }
1260
1261 printf("%04x", Chars[ch]);
1262 }
1263
1264 putchar('>');
1265 }
1266 else
1267 {
1268 /*
1269 * Standard 8-bit encoding...
1270 */
1271
1272 putchar('(');
1273
1274 while (*s)
1275 {
1276 if (*s < 32 || *s > 126)
1277 printf("\\%03o", *s);
1278 else
1279 {
1280 if (*s == '(' || *s == ')' || *s == '\\')
1281 putchar('\\');
1282
1283 putchar(*s);
1284 }
1285
1286 s ++;
1287 }
1288
1289 putchar(')');
1290 }
1291}
1292
1293
1294/*
b5cb0608 1295 * End of "$Id: texttops.c,v 1.34.2.1 2001/05/13 18:38:21 mike Exp $".
070c29a1 1296 */