/* Include necessary headers... */ #include "textcommon.h" /* Local functions... */ static void write_line(int row, lchar_t *line); static void write_string(int col, int row, int len, lchar_t *s); /* Local globals... */ static char file_date[255], file_title[255]; static lchar_t file_notice[255]; /* Class + caveat */ /* * 'main()' - Main entry for text to HP-PCL filter. */ int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { /* Print stuff. */ return (TextMain("texttohp", argc, argv)); } /* * 'WriteEpilogue()' - Write the HP-PCL file epilogue. */ void WriteEpilogue(void) { printf("\033E"); free(Page[0]); free(Page); } /* * 'WritePage()' - Write a page of text. */ void WritePage(void) { float left, /* Left margin */ width; /* Width of page */ int line; /* Current line */ char page[255]; /* Page number */ NumPages ++; fprintf(stderr, "PAGE: %d %d\n", NumPages, Copies); /* Print the shaded header as needed. */ if (PrettyPrint) { sprintf(page, "%d", NumPages); if (!Duplex || (NumPages & 1)) left = PageLeft * 10.0f; else left = (PageWidth - PageRight) * 10.0f; width = (PageRight - PageLeft) * 10.0f; printf("\033(s3B"); printf("\033&a%.0fH\033&a360V", left); printf("\033*c%.0fH\033*c%.0fV", width, 720.0f * 2.0f / LinesPerInch); printf("\033*c10G\033*c2P"); printf("\033&a%.0fV", 360.0f + 1.343f * 720.0f / LinesPerInch); if (!Duplex || (NumPages & 1)) { printf("\033&a%.0fH%s", left + 720.0f / CharsPerInch, file_title); printf("\033&a%.0fH%s", left + 0.5 * (width - 720.0f / CharsPerInch * strlen(file_date)), file_date); printf("\033&a%.0fH%s", left + width - 720.0f / CharsPerInch * (strlen(page) + 1), page); } else { printf("\033&a%.0fH%s", left + 720.0f / CharsPerInch, page); printf("\033&a%.0fH%s", left + 0.5 * (width - 720.0f / CharsPerInch * strlen(file_date)), file_date); printf("\033&a%.0fH%s", left + width - 720.0f / CharsPerInch * (strlen(file_title) + 1), file_title); } } /* Classification/label stuff... */ write_line(-2, file_notice); /* Then all of the lines. */ for (line = 0; line < SizeLines; line ++) write_line(line, Page[line]); /* Classification/label stuff. */ write_line(SizeLines + 1, file_notice); /* Eject the page. */ putchar(0x0c); /* Clear the page. */ memset(Page[0], 0, sizeof(lchar_t) * SizeColumns * SizeLines); } /* * 'WriteProlog()' - Write the HP-PCL file prolog with options. */ void WriteProlog(const char *title, /* I - Title of job */ const char *user, /* I - Username */ const char *classification, /* I - Classification */ const char *label, /* I - Page label */ ppd_file_t *ppd) /* I - PPD file info */ { int line; /* Current output line */ char *charset; /* Character set string */ time_t curtime; /* Current time */ struct tm *curtm; /* Current date */ ppd_choice_t *input_slot; /* InputSlot option (if any) */ char *temp; /* MediaPosition string (if any) */ int pos; /* MediaPosition value (if any) */ char s[255], /* Classification/label string */ *ptr; /* Pointer into string */ lchar_t *lptr; /* Pointer into line */ /* We don't use the user argument. */ (void)user; /* Adjust margins as necessary for page labels. */ if (classification || label) { /* Leave room for labels. */ PageBottom += 144.0 / LinesPerInch; PageTop -= 144.0 / LinesPerInch; } /* Set the printing time. */ curtime = time(NULL); curtm = localtime(&curtime); strftime(file_date, sizeof(file_date), "%c", curtm); /* Set the title... */ strncpy(file_title, title, sizeof(file_title) - 1); file_title[sizeof(file_title) - 1] = '\0'; /* Figure out the page size. */ SizeColumns = (PageRight - PageLeft) / 72.0 * CharsPerInch; SizeLines = (PageTop - PageBottom) / 72.0 * LinesPerInch; /* Allocate memory for the page buffer */ Page = calloc(sizeof(lchar_t *), SizeLines); Page[0] = calloc(sizeof(lchar_t), SizeColumns * SizeLines); for (line = 1; line < SizeLines; line ++) Page[line] = Page[0] + line * SizeColumns; if (PageColumns > 1) { ColumnGutter = CharsPerInch / 2; ColumnWidth = (SizeColumns - ColumnGutter * (PageColumns - 1)) / PageColumns; } else ColumnWidth = SizeColumns; /* Set any classification/label strings... */ memset(file_notice, 0, sizeof(file_notice)); if (classification || label) { if (classification) { if (strcmp(classification, "confidential") == 0) classification = "CONFIDENTIAL"; else if (strcmp(classification, "classified") == 0) classification = "CLASSIFIED"; else if (strcmp(classification, "secret") == 0) classification = "SECRET"; else if (strcmp(classification, "topsecret") == 0) classification = "TOP SECRET"; else if (strcmp(classification, "unclassified") == 0) classification = "UNCLASSIFIED"; } if (classification && label) snprintf(s, sizeof(s), "%s - %s", classification, label); else if (classification) { strncpy(s, classification, sizeof(s) - 1); s[sizeof(s) - 1] = '\0'; } else { strncpy(s, label, sizeof(s) - 1); s[sizeof(s) - 1] = '\0'; } for (ptr = s, lptr = file_notice + (SizeColumns - strlen(s)) / 2; *ptr; ptr ++, lptr ++) { lptr->ch = *ptr; lptr->attr = ATTR_BOLD; } } /* Set the actual media size and character sizes... */ printf("\033E"); printf("\033&l%dX", Copies); printf("\033&l%dO", Orientation); switch ((int)(PageLength + 0.5f)) { case 540 : /* Monarch Envelope */ printf("\033&l80A"); break; case 624 : /* DL Envelope */ printf("\033&l90A"); break; case 649 : /* C5 Envelope */ printf("\033&l91A"); break; case 684 : /* COM-10 Envelope */ printf("\033&l81A"); break; case 709 : /* B5 Envelope */ printf("\033&l100A"); break; case 756 : /* Executive */ printf("\033&l1A"); break; case 792 : /* Letter */ printf("\033&l2A"); break; case 842 : /* A4 */ printf("\033&l26A"); break; case 1008 : /* Legal */ printf("\033&l3A"); break; case 1191 : /* A3 */ printf("\033&l27A"); break; case 1224 : /* Tabloid */ printf("\033&l6A"); break; default : /* Set page length */ printf("\033&l%.2fP", PageLength / 12.0); break; } printf("\033&l0L"); /* Turn off perforation skip */ printf("\033&l0E"); /* Reset top margin to 0 */ if ((input_slot = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL) { if ((temp = strstr(input_slot->code, "/cupsMediaPosition")) != NULL) if ((pos = atoi(temp + 19)) != 0) printf("\033&l%dH", pos); } printf("\033&l%dS", Duplex + (Orientation & 1)); printf("\033(s%dH", CharsPerInch); printf("\033(s%.2fV", 72.0f / (float)LinesPerInch); if (ColorDevice) printf("\033*r3U"); /* * Get the output character set; if it is undefined or "us-ascii", do * nothing because we can use the default encoding... */ charset = getenv("CHARSET"); if (charset != NULL && strcmp(charset, "us-ascii") != 0) { /* Set the HP-PCL character set to ISO-8859-1. */ printf("\033(0N"); } } /* * 'write_line()' - Write a row of text. */ static void write_line(int row, /* I - Row number (0 to N) */ lchar_t *line) /* I - Line to print */ { int col; /* Current column */ int attr; /* Current attribute */ lchar_t *start; /* First character in sequence */ for (col = 0, start = line; col < SizeColumns;) { while (col < SizeColumns && (line->ch == ' ' || line->ch == 0)) { col ++; line ++; } if (col >= SizeColumns) break; attr = line->attr; start = line; while (col < SizeColumns && line->ch != 0 && attr == line->attr) { col ++; line ++; } write_string(col - (line - start), row, line - start, start); } } /* * 'write_string()' - Write a string of text. */ static void write_string(int col, /* I - Start column */ int row, /* I - Row */ int len, /* I - Number of characters */ lchar_t *s) /* I - String to print */ { float x, y; /* Position of text */ unsigned attr; /* Character attributes */ /* Position the text and set the font. */ if (Duplex && (NumPages & 1) == 0) { x = (PageWidth - PageRight) * 10.0; y = (PageLength - PageTop) * 10.0; } else { x = PageLeft * 10.0; y = (PageLength - PageTop) * 10.0; } if (PrettyPrint) y += 3.0f * 720.0f / (float)LinesPerInch; x += (float)col * 720.0f / (float)CharsPerInch; y += (float)(row + 0.843f) * 720.0f / (float)LinesPerInch; attr = s->attr; if (attr & ATTR_RAISED) y -= 360.0 / (float)LinesPerInch; else if (attr & ATTR_LOWERED) y += 360.0 / (float)LinesPerInch; printf("\033&a%.0fH", x); printf("\033&a%.0fV", y); if (attr & ATTR_BOLD) printf("\033(s3B\033(s0S"); else if (attr & ATTR_ITALIC) printf("\033(s0B\033(s1S"); else printf("\033(s0B\033(s0S"); if (attr & ATTR_UNDERLINE) printf("\033&d0D"); else printf("\033&d@"); if (ColorDevice && PrettyPrint) { if (attr & ATTR_BOLD) printf("\033*v4S"); else if (attr & ATTR_ITALIC) printf("\033*v2S"); else printf("\033*v0S"); } /* Write the string. */ while (len > 0) { putchar(s->ch); len --; s ++; } }