📄 postscript.c
字号:
#include <config.h>#include <stdio.h>#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#if TIME_WITH_SYS_TIME#include <sys/time.h>#include <time.h>#else#if HAVE_SYS_TIME_H#include <sys/time.h>#else#include <time.h>#endif#endif#ifdef HAVE_STAT_H#include <stat.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include "mad.h"static void zfree (unsigned char *s){ memset (s, 0, strlen ((char *) s)); free (s);}/* Some configuration constants, meassured in points (1/72 inch) */int postscript_option_page_top = 842; /* A4 = 297mm x 210mm = 842pt x 595pt */int postscript_option_page_right_edge = 595;/* Set to 1 if your printer doesn't have iso encoding builtin. */int isoencoding_not_builtin = 0;int postscript_option_landscape = 0;int postscript_option_footline_in_landscape = 0;int postscript_option_line_numbers = 0;int postscript_option_font_size = 0;int postscript_option_left_margin = 80;int postscript_option_right_margin = 40;int postscript_option_top_margin = 0;int postscript_option_bottom_margin = 0;char *postscript_option_font = "Courier";char *postscript_option_header_font = "Helvetica-Bold";int postscript_option_header_font_size = 12;char *postscript_option_line_number_font = "Helvetica";int postscript_option_line_number_size = 5;int postscript_option_col_separation = 30;int postscript_option_header_height = 30;int postscript_option_show_header = 1;int postscript_option_wrap_lines = 1;int postscript_option_columns = 1;int postscript_option_chars_per_line = 0;int postscript_option_plain_text = 0;unsigned char *postscript_option_title = 0;unsigned char *postscript_option_file = 0;unsigned char *postscript_option_pipe = 0;unsigned char *option_continuation_string = 0;int postscript_option_tab_size = 8;void postscript_clean (void){ if (postscript_option_title) free (postscript_option_title); if (postscript_option_file) free (postscript_option_file); if (postscript_option_pipe) free (postscript_option_pipe); if (option_continuation_string) free (option_continuation_string);}void (*postscript_dialog_cannot_open) (unsigned char *) = 0;int (*postscript_dialog_exists) (unsigned char *) = 0;unsigned char *(*postscript_get_next_line) (unsigned char *) = 0;/* utility functions */unsigned char *ps_string (unsigned char *text){ unsigned char *r, *p, *q; int i = 0; for (i = 0, p = text; *p; p++, i++) { if ((char *) strchr ("\\()", (int) *p)) i++; else if (*p >= 0177 || *p <= 037) i += 3; } r = (unsigned char *) malloc (i + 1); for (p = text, q = r; *p; p++, q++) { if (strchr ("\\()", *p)) { *q++ = '\\'; *q = *p; } else if (*p >= 0177 || *p <= 037) { sprintf ((char *) q, "\\%03o", (int) *p); q += 3; } else { *q = *p; } } *q = 0; return r;}static unsigned char *get_spaces (unsigned char *old, int l){ if (old) zfree (old); if (l < 0) l = 0; old = (unsigned char *) malloc (l + 1); memset (old, ' ', l); old[l] = '\0'; return old;}static unsigned char *expand_tabs (unsigned char *old){ int i; unsigned char *p, *r, *q, *spaces; spaces = get_spaces (0, postscript_option_tab_size); for (i = 0, p = old; *p; p++, i++) if (*p == '\t') i += 7; r = (unsigned char *) malloc (i + 1); for (i = 0, q = r, p = old; *p; p++, i++) if (*p == '\t') { int n = postscript_option_tab_size - (i % postscript_option_tab_size); strncpy (q, spaces, n); q += n; i += n - 1; } else *q++ = *p; *q = '\0'; if (old) zfree (old); zfree (spaces); return r;}/* globals */unsigned char filedate[80];int page_no = 0, line_no = 0, cur_col = 100, total_pages = 0;double cur_pos = -1;double top = 0, right_edge = 0, no_columns = 0, home_pos = 0, bottom_pos = 0;double sep_bar_pos = 0;unsigned char *box_format;double font_size = 0.0, line_height = 0.0, char_width = 0.0;double col_width = 0.0;int chars_per_line = 0;/* The next few entries are from the AFM file for Adobe's postscript_option_font Courier */double cour_char_width = 600; /* The width of each unsigned char in 1000x1000 square *//* sub underline_position { -82; } # Where underline goes relative to baseline *//* sub underline_thickness { 40; } # and it's thickness */static FILE *outfile;unsigned char *top_box, *bot_box;unsigned char *header = 0, *title = 0;int lineNoOut = 0;static void portrait_header (void){ fprintf (outfile, "%s", top_box); fprintf (outfile, "%s", bot_box); /* Then the banner or the filename */ fprintf (outfile, "F3 SF\n"); if (header) { fprintf (outfile, "%.1f %.1f M(%s)dup stringwidth pop neg 0 rmoveto show\n", (double) right_edge - postscript_option_right_margin, (double) top - postscript_option_top_margin - postscript_option_header_font_size, header); } /* Then print date and page number */ fprintf (outfile, "(%s)%.1f %.1f S\n", filedate, (double) postscript_option_left_margin, (double) postscript_option_bottom_margin + postscript_option_header_font_size * 0.3); fprintf (outfile, "%.1f %.1f M(%d)dup stringwidth pop neg 0 rmoveto show\n", (double) right_edge - postscript_option_right_margin, (double) postscript_option_bottom_margin + postscript_option_header_font_size * 0.3, page_no);}static void landscape_header (void){ double y; if (postscript_option_footline_in_landscape) { fprintf (outfile, "%s", bot_box); y = (double) postscript_option_bottom_margin + postscript_option_header_font_size * 0.3; } else { fprintf (outfile, "%s", top_box); y = (double) top - postscript_option_top_margin - postscript_option_header_font_size; } fprintf (outfile, "F3 SF\n"); if (header) { fprintf (outfile, "%.1f %.1f M(%s)dup stringwidth pop 2 div neg 0 rmoveto show\n", (double) (postscript_option_left_margin + right_edge - postscript_option_right_margin) / 2, y, header); } fprintf (outfile, "(%s)%.1f %.1f S\n", filedate, (double) postscript_option_left_margin, (double) y); fprintf (outfile, "%.1f %.1f M(%d)dup stringwidth pop neg 0 rmoveto show\n", (double) right_edge - (double) postscript_option_right_margin, (double) y, page_no);}static void end_page (void){ if (total_pages) { fprintf (outfile, "page_save restore\n"); fprintf (outfile, "showpage\n"); }}static void new_page (void){ end_page (); page_no++; total_pages++; fprintf (outfile, "%%%%Page: %d %d\n", page_no, page_no); fprintf (outfile, "%%%%BeginPageSetup\n"); fprintf (outfile, "/page_save save def\n"); if (postscript_option_landscape) fprintf (outfile, "90 rotate 0 -%d translate %% landscape mode\n", postscript_option_page_right_edge); if (postscript_option_show_header) fprintf (outfile, "0.15 setlinewidth\n"); fprintf (outfile, "%%%%EndPageSetup\n"); if (postscript_option_show_header) { if (postscript_option_landscape) landscape_header (); else portrait_header (); fprintf (outfile, "F1 SF\n"); } if (no_columns > 1) { int c; for (c = 1; c < no_columns; c++) { double x; x = (double) postscript_option_left_margin + (double) c *sep_bar_pos; fprintf (outfile, "%.1f %.1f M %.1f %.1f L stroke\n", (double) x, (double) bottom_pos - 10, (double) x, (double) home_pos + 10); } }}static void printLine (unsigned char *p){ double x = 0; if (cur_pos < bottom_pos) { cur_pos = home_pos; if (cur_col < no_columns) { cur_col++; } else { cur_col = 1; new_page (); } } cur_pos -= line_height; if (*p) { /* no work for empty lines */ double indent = 0; while (*p == ' ') { p++; indent++; } indent *= char_width; p = ps_string (p); x = (double) postscript_option_left_margin + (double) (cur_col - 1) * ((double) col_width + postscript_option_col_separation); fprintf (outfile, "(%s)%.1f %.1f S\n", p, (double) x + indent, (double) cur_pos); zfree (p); } if (lineNoOut) { fprintf (outfile, "F2 SF(%d)%.1f %.1f S F1 SF\n", line_no, (double) x + col_width + 5, (double) cur_pos); lineNoOut = 0; }}static void printLongLines (unsigned char *p){ int maxLength; int lineLength = 0; int rightMargin = 0; unsigned char *spaces = 0; unsigned char *temp = 0; unsigned char *c; int right_space = 0; c = option_continuation_string; if (!c) c = (unsigned char *) "~"; maxLength = chars_per_line - strlen ((char *) c); while (*p) { unsigned char *q; if (strlen ((char *) p) > maxLength && postscript_option_wrap_lines) { char *t; for (lineLength = maxLength - 1, q = p + maxLength - 1; q >= p && *q != ' '; q--, lineLength--); if (lineLength < maxLength / 2) lineLength = maxLength; temp = p + lineLength; t = (char *) malloc (maxLength + right_space + strlen ((char *) c) + 1); strncpy (t, p, lineLength); if (!right_space) right_space = maxLength - lineLength;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -