Merge branch 'vim-with-runtime' into feat/tagfunc
[vim_extended.git] / src / hardcopy.c
blob0e7ee4ddd8fbc0a68135b57edad8c897b95ea504
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * hardcopy.c: printing to paper
14 #include "vim.h"
15 #include "version.h"
17 #if defined(FEAT_PRINTER) || defined(PROTO)
19 * To implement printing on a platform, the following functions must be
20 * defined:
22 * int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
23 * Called once. Code should display printer dialogue (if appropriate) and
24 * determine printer font and margin settings. Reset has_color if the printer
25 * doesn't support colors at all.
26 * Returns FAIL to abort.
28 * int mch_print_begin(prt_settings_T *settings)
29 * Called to start the print job.
30 * Return FALSE to abort.
32 * int mch_print_begin_page(char_u *msg)
33 * Called at the start of each page.
34 * "msg" indicates the progress of the print job, can be NULL.
35 * Return FALSE to abort.
37 * int mch_print_end_page()
38 * Called at the end of each page.
39 * Return FALSE to abort.
41 * int mch_print_blank_page()
42 * Called to generate a blank page for collated, duplex, multiple copy
43 * document. Return FALSE to abort.
45 * void mch_print_end(prt_settings_T *psettings)
46 * Called at normal end of print job.
48 * void mch_print_cleanup()
49 * Called if print job ends normally or is abandoned. Free any memory, close
50 * devices and handles. Also called when mch_print_begin() fails, but not
51 * when mch_print_init() fails.
53 * void mch_print_set_font(int Bold, int Italic, int Underline);
54 * Called whenever the font style changes.
56 * void mch_print_set_bg(long_u bgcol);
57 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
60 * void mch_print_set_fg(long_u fgcol);
61 * Called to set the foreground color for the following text. Parameter is an
62 * RGB value.
64 * mch_print_start_line(int margin, int page_line)
65 * Sets the current position at the start of line "page_line".
66 * If margin is TRUE start in the left margin (for header and line number).
68 * int mch_print_text_out(char_u *p, int len);
69 * Output one character of text p[len] at the current position.
70 * Return TRUE if there is no room for another character in the same line.
72 * Note that the generic code has no idea of margins. The machine code should
73 * simply make the page look smaller! The header and the line numbers are
74 * printed in the margin.
77 #ifdef FEAT_SYN_HL
78 static const long_u cterm_color_8[8] =
80 (long_u)0x000000L, (long_u)0xff0000L, (long_u)0x00ff00L, (long_u)0xffff00L,
81 (long_u)0x0000ffL, (long_u)0xff00ffL, (long_u)0x00ffffL, (long_u)0xffffffL
84 static const long_u cterm_color_16[16] =
86 (long_u)0x000000L, (long_u)0x0000c0L, (long_u)0x008000L, (long_u)0x004080L,
87 (long_u)0xc00000L, (long_u)0xc000c0L, (long_u)0x808000L, (long_u)0xc0c0c0L,
88 (long_u)0x808080L, (long_u)0x6060ffL, (long_u)0x00ff00L, (long_u)0x00ffffL,
89 (long_u)0xff8080L, (long_u)0xff40ffL, (long_u)0xffff00L, (long_u)0xffffffL
92 static int current_syn_id;
93 #endif
95 #define PRCOLOR_BLACK (long_u)0
96 #define PRCOLOR_WHITE (long_u)0xFFFFFFL
98 static int curr_italic;
99 static int curr_bold;
100 static int curr_underline;
101 static long_u curr_bg;
102 static long_u curr_fg;
103 static int page_count;
105 #if defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)
106 # define OPT_MBFONT_USECOURIER 0
107 # define OPT_MBFONT_ASCII 1
108 # define OPT_MBFONT_REGULAR 2
109 # define OPT_MBFONT_BOLD 3
110 # define OPT_MBFONT_OBLIQUE 4
111 # define OPT_MBFONT_BOLDOBLIQUE 5
112 # define OPT_MBFONT_NUM_OPTIONS 6
114 static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] =
116 {"c", FALSE, 0, NULL, 0, FALSE},
117 {"a", FALSE, 0, NULL, 0, FALSE},
118 {"r", FALSE, 0, NULL, 0, FALSE},
119 {"b", FALSE, 0, NULL, 0, FALSE},
120 {"i", FALSE, 0, NULL, 0, FALSE},
121 {"o", FALSE, 0, NULL, 0, FALSE},
123 #endif
126 * These values determine the print position on a page.
128 typedef struct
130 int lead_spaces; /* remaining spaces for a TAB */
131 int print_pos; /* virtual column for computing TABs */
132 colnr_T column; /* byte column */
133 linenr_T file_line; /* line nr in the buffer */
134 long_u bytes_printed; /* bytes printed so far */
135 int ff; /* seen form feed character */
136 } prt_pos_T;
138 static char_u *parse_list_options __ARGS((char_u *option_str, option_table_T *table, int table_size));
140 #ifdef FEAT_SYN_HL
141 static long_u darken_rgb __ARGS((long_u rgb));
142 static long_u prt_get_term_color __ARGS((int colorindex));
143 #endif
144 static void prt_set_fg __ARGS((long_u fg));
145 static void prt_set_bg __ARGS((long_u bg));
146 static void prt_set_font __ARGS((int bold, int italic, int underline));
147 static void prt_line_number __ARGS((prt_settings_T *psettings, int page_line, linenr_T lnum));
148 static void prt_header __ARGS((prt_settings_T *psettings, int pagenum, linenr_T lnum));
149 static void prt_message __ARGS((char_u *s));
150 static colnr_T hardcopy_line __ARGS((prt_settings_T *psettings, int page_line, prt_pos_T *ppos));
151 #ifdef FEAT_SYN_HL
152 static void prt_get_attr __ARGS((int hl_id, prt_text_attr_T* pattr, int modec));
153 #endif
156 * Parse 'printoptions' and set the flags in "printer_opts".
157 * Returns an error message or NULL;
159 char_u *
160 parse_printoptions()
162 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
165 #if (defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
167 * Parse 'printoptions' and set the flags in "printer_opts".
168 * Returns an error message or NULL;
170 char_u *
171 parse_printmbfont()
173 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
175 #endif
178 * Parse a list of options in the form
179 * option:value,option:value,option:value
181 * "value" can start with a number which is parsed out, e.g. margin:12mm
183 * Returns an error message for an illegal option, NULL otherwise.
184 * Only used for the printer at the moment...
186 static char_u *
187 parse_list_options(option_str, table, table_size)
188 char_u *option_str;
189 option_table_T *table;
190 int table_size;
192 char_u *stringp;
193 char_u *colonp;
194 char_u *commap;
195 char_u *p;
196 int idx = 0; /* init for GCC */
197 int len;
199 for (idx = 0; idx < table_size; ++idx)
200 table[idx].present = FALSE;
203 * Repeat for all comma separated parts.
205 stringp = option_str;
206 while (*stringp)
208 colonp = vim_strchr(stringp, ':');
209 if (colonp == NULL)
210 return (char_u *)N_("E550: Missing colon");
211 commap = vim_strchr(stringp, ',');
212 if (commap == NULL)
213 commap = option_str + STRLEN(option_str);
215 len = (int)(colonp - stringp);
217 for (idx = 0; idx < table_size; ++idx)
218 if (STRNICMP(stringp, table[idx].name, len) == 0)
219 break;
221 if (idx == table_size)
222 return (char_u *)N_("E551: Illegal component");
224 p = colonp + 1;
225 table[idx].present = TRUE;
227 if (table[idx].hasnum)
229 if (!VIM_ISDIGIT(*p))
230 return (char_u *)N_("E552: digit expected");
232 table[idx].number = getdigits(&p); /*advances p*/
235 table[idx].string = p;
236 table[idx].strlen = (int)(commap - p);
238 stringp = commap;
239 if (*stringp == ',')
240 ++stringp;
243 return NULL;
247 #ifdef FEAT_SYN_HL
249 * If using a dark background, the colors will probably be too bright to show
250 * up well on white paper, so reduce their brightness.
252 static long_u
253 darken_rgb(rgb)
254 long_u rgb;
256 return ((rgb >> 17) << 16)
257 + (((rgb & 0xff00) >> 9) << 8)
258 + ((rgb & 0xff) >> 1);
261 static long_u
262 prt_get_term_color(colorindex)
263 int colorindex;
265 /* TODO: Should check for xterm with 88 or 256 colors. */
266 if (t_colors > 8)
267 return cterm_color_16[colorindex % 16];
268 return cterm_color_8[colorindex % 8];
271 static void
272 prt_get_attr(hl_id, pattr, modec)
273 int hl_id;
274 prt_text_attr_T *pattr;
275 int modec;
277 int colorindex;
278 long_u fg_color;
279 long_u bg_color;
280 char *color;
282 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
283 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
284 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
285 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
287 # ifdef FEAT_GUI
288 if (gui.in_use)
290 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
291 if (bg_color == PRCOLOR_BLACK)
292 bg_color = PRCOLOR_WHITE;
294 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
296 else
297 # endif
299 bg_color = PRCOLOR_WHITE;
301 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
302 if (color == NULL)
303 colorindex = 0;
304 else
305 colorindex = atoi(color);
307 if (colorindex >= 0 && colorindex < t_colors)
308 fg_color = prt_get_term_color(colorindex);
309 else
310 fg_color = PRCOLOR_BLACK;
313 if (fg_color == PRCOLOR_WHITE)
314 fg_color = PRCOLOR_BLACK;
315 else if (*p_bg == 'd')
316 fg_color = darken_rgb(fg_color);
318 pattr->fg_color = fg_color;
319 pattr->bg_color = bg_color;
321 #endif /* FEAT_SYN_HL */
323 static void
324 prt_set_fg(fg)
325 long_u fg;
327 if (fg != curr_fg)
329 curr_fg = fg;
330 mch_print_set_fg(fg);
334 static void
335 prt_set_bg(bg)
336 long_u bg;
338 if (bg != curr_bg)
340 curr_bg = bg;
341 mch_print_set_bg(bg);
345 static void
346 prt_set_font(bold, italic, underline)
347 int bold;
348 int italic;
349 int underline;
351 if (curr_bold != bold
352 || curr_italic != italic
353 || curr_underline != underline)
355 curr_underline = underline;
356 curr_italic = italic;
357 curr_bold = bold;
358 mch_print_set_font(bold, italic, underline);
363 * Print the line number in the left margin.
365 static void
366 prt_line_number(psettings, page_line, lnum)
367 prt_settings_T *psettings;
368 int page_line;
369 linenr_T lnum;
371 int i;
372 char_u tbuf[20];
374 prt_set_fg(psettings->number.fg_color);
375 prt_set_bg(psettings->number.bg_color);
376 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
377 mch_print_start_line(TRUE, page_line);
379 /* Leave two spaces between the number and the text; depends on
380 * PRINT_NUMBER_WIDTH. */
381 sprintf((char *)tbuf, "%6ld", (long)lnum);
382 for (i = 0; i < 6; i++)
383 (void)mch_print_text_out(&tbuf[i], 1);
385 #ifdef FEAT_SYN_HL
386 if (psettings->do_syntax)
387 /* Set colors for next character. */
388 current_syn_id = -1;
389 else
390 #endif
392 /* Set colors and font back to normal. */
393 prt_set_fg(PRCOLOR_BLACK);
394 prt_set_bg(PRCOLOR_WHITE);
395 prt_set_font(FALSE, FALSE, FALSE);
400 * Get the currently effective header height.
403 prt_header_height()
405 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
406 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
407 return 2;
411 * Return TRUE if using a line number for printing.
414 prt_use_number()
416 return (printer_opts[OPT_PRINT_NUMBER].present
417 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
421 * Return the unit used in a margin item in 'printoptions'.
422 * Returns PRT_UNIT_NONE if not recognized.
425 prt_get_unit(idx)
426 int idx;
428 int u = PRT_UNIT_NONE;
429 int i;
430 static char *(units[4]) = PRT_UNIT_NAMES;
432 if (printer_opts[idx].present)
433 for (i = 0; i < 4; ++i)
434 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
436 u = i;
437 break;
439 return u;
443 * Print the page header.
445 static void
446 prt_header(psettings, pagenum, lnum)
447 prt_settings_T *psettings;
448 int pagenum;
449 linenr_T lnum UNUSED;
451 int width = psettings->chars_per_line;
452 int page_line;
453 char_u *tbuf;
454 char_u *p;
455 #ifdef FEAT_MBYTE
456 int l;
457 #endif
459 /* Also use the space for the line number. */
460 if (prt_use_number())
461 width += PRINT_NUMBER_WIDTH;
463 tbuf = alloc(width + IOSIZE);
464 if (tbuf == NULL)
465 return;
467 #ifdef FEAT_STL_OPT
468 if (*p_header != NUL)
470 linenr_T tmp_lnum, tmp_topline, tmp_botline;
471 int use_sandbox = FALSE;
474 * Need to (temporarily) set current line number and first/last line
475 * number on the 'window'. Since we don't know how long the page is,
476 * set the first and current line number to the top line, and guess
477 * that the page length is 64.
479 tmp_lnum = curwin->w_cursor.lnum;
480 tmp_topline = curwin->w_topline;
481 tmp_botline = curwin->w_botline;
482 curwin->w_cursor.lnum = lnum;
483 curwin->w_topline = lnum;
484 curwin->w_botline = lnum + 63;
485 printer_page_num = pagenum;
487 # ifdef FEAT_EVAL
488 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
489 # endif
490 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
491 p_header, use_sandbox,
492 ' ', width, NULL, NULL);
494 /* Reset line numbers */
495 curwin->w_cursor.lnum = tmp_lnum;
496 curwin->w_topline = tmp_topline;
497 curwin->w_botline = tmp_botline;
499 else
500 #endif
501 sprintf((char *)tbuf, _("Page %d"), pagenum);
503 prt_set_fg(PRCOLOR_BLACK);
504 prt_set_bg(PRCOLOR_WHITE);
505 prt_set_font(TRUE, FALSE, FALSE);
507 /* Use a negative line number to indicate printing in the top margin. */
508 page_line = 0 - prt_header_height();
509 mch_print_start_line(TRUE, page_line);
510 for (p = tbuf; *p != NUL; )
512 if (mch_print_text_out(p,
513 #ifdef FEAT_MBYTE
514 (l = (*mb_ptr2len)(p))
515 #else
517 #endif
520 ++page_line;
521 if (page_line >= 0) /* out of room in header */
522 break;
523 mch_print_start_line(TRUE, page_line);
525 #ifdef FEAT_MBYTE
526 p += l;
527 #else
528 p++;
529 #endif
532 vim_free(tbuf);
534 #ifdef FEAT_SYN_HL
535 if (psettings->do_syntax)
536 /* Set colors for next character. */
537 current_syn_id = -1;
538 else
539 #endif
541 /* Set colors and font back to normal. */
542 prt_set_fg(PRCOLOR_BLACK);
543 prt_set_bg(PRCOLOR_WHITE);
544 prt_set_font(FALSE, FALSE, FALSE);
549 * Display a print status message.
551 static void
552 prt_message(s)
553 char_u *s;
555 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
556 screen_puts(s, (int)Rows - 1, 0, hl_attr(HLF_R));
557 out_flush();
560 void
561 ex_hardcopy(eap)
562 exarg_T *eap;
564 linenr_T lnum;
565 int collated_copies, uncollated_copies;
566 prt_settings_T settings;
567 long_u bytes_to_print = 0;
568 int page_line;
569 int jobsplit;
571 memset(&settings, 0, sizeof(prt_settings_T));
572 settings.has_color = TRUE;
574 # ifdef FEAT_POSTSCRIPT
575 if (*eap->arg == '>')
577 char_u *errormsg = NULL;
579 /* Expand things like "%.ps". */
580 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
582 if (errormsg != NULL)
583 EMSG(errormsg);
584 return;
586 settings.outfile = skipwhite(eap->arg + 1);
588 else if (*eap->arg != NUL)
589 settings.arguments = eap->arg;
590 # endif
593 * Initialise for printing. Ask the user for settings, unless forceit is
594 * set.
595 * The mch_print_init() code should set up margins if applicable. (It may
596 * not be a real printer - for example the engine might generate HTML or
597 * PS.)
599 if (mch_print_init(&settings,
600 curbuf->b_fname == NULL
601 ? (char_u *)buf_spname(curbuf)
602 : curbuf->b_sfname == NULL
603 ? curbuf->b_fname
604 : curbuf->b_sfname,
605 eap->forceit) == FAIL)
606 return;
608 #ifdef FEAT_SYN_HL
609 # ifdef FEAT_GUI
610 if (gui.in_use)
611 settings.modec = 'g';
612 else
613 # endif
614 if (t_colors > 1)
615 settings.modec = 'c';
616 else
617 settings.modec = 't';
619 if (!syntax_present(curbuf))
620 settings.do_syntax = FALSE;
621 else if (printer_opts[OPT_PRINT_SYNTAX].present
622 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
623 settings.do_syntax =
624 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
625 else
626 settings.do_syntax = settings.has_color;
627 #endif
629 /* Set up printing attributes for line numbers */
630 settings.number.fg_color = PRCOLOR_BLACK;
631 settings.number.bg_color = PRCOLOR_WHITE;
632 settings.number.bold = FALSE;
633 settings.number.italic = TRUE;
634 settings.number.underline = FALSE;
635 #ifdef FEAT_SYN_HL
637 * Syntax highlighting of line numbers.
639 if (prt_use_number() && settings.do_syntax)
641 int id;
643 id = syn_name2id((char_u *)"LineNr");
644 if (id > 0)
645 id = syn_get_final_id(id);
647 prt_get_attr(id, &settings.number, settings.modec);
649 #endif
652 * Estimate the total lines to be printed
654 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
655 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
656 if (bytes_to_print == 0)
658 MSG(_("No text to be printed"));
659 goto print_fail_no_begin;
662 /* Set colors and font to normal. */
663 curr_bg = (long_u)0xffffffffL;
664 curr_fg = (long_u)0xffffffffL;
665 curr_italic = MAYBE;
666 curr_bold = MAYBE;
667 curr_underline = MAYBE;
669 prt_set_fg(PRCOLOR_BLACK);
670 prt_set_bg(PRCOLOR_WHITE);
671 prt_set_font(FALSE, FALSE, FALSE);
672 #ifdef FEAT_SYN_HL
673 current_syn_id = -1;
674 #endif
676 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
677 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
679 if (!mch_print_begin(&settings))
680 goto print_fail_no_begin;
683 * Loop over collated copies: 1 2 3, 1 2 3, ...
685 page_count = 0;
686 for (collated_copies = 0;
687 collated_copies < settings.n_collated_copies;
688 collated_copies++)
690 prt_pos_T prtpos; /* current print position */
691 prt_pos_T page_prtpos; /* print position at page start */
692 int side;
694 memset(&page_prtpos, 0, sizeof(prt_pos_T));
695 page_prtpos.file_line = eap->line1;
696 prtpos = page_prtpos;
698 if (jobsplit && collated_copies > 0)
700 /* Splitting jobs: Stop a previous job and start a new one. */
701 mch_print_end(&settings);
702 if (!mch_print_begin(&settings))
703 goto print_fail_no_begin;
707 * Loop over all pages in the print job: 1 2 3 ...
709 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
712 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
713 * For duplex: 12 12 12 34 34 34, ...
715 for (uncollated_copies = 0;
716 uncollated_copies < settings.n_uncollated_copies;
717 uncollated_copies++)
719 /* Set the print position to the start of this page. */
720 prtpos = page_prtpos;
723 * Do front and rear side of a page.
725 for (side = 0; side <= settings.duplex; ++side)
728 * Print one page.
731 /* Check for interrupt character every page. */
732 ui_breakcheck();
733 if (got_int || settings.user_abort)
734 goto print_fail;
736 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
737 page_count + 1 + side,
738 prtpos.bytes_printed > 1000000
739 ? (int)(prtpos.bytes_printed /
740 (bytes_to_print / 100))
741 : (int)((prtpos.bytes_printed * 100)
742 / bytes_to_print));
743 if (!mch_print_begin_page(IObuff))
744 goto print_fail;
746 if (settings.n_collated_copies > 1)
747 sprintf((char *)IObuff + STRLEN(IObuff),
748 _(" Copy %d of %d"),
749 collated_copies + 1,
750 settings.n_collated_copies);
751 prt_message(IObuff);
754 * Output header if required
756 if (prt_header_height() > 0)
757 prt_header(&settings, page_count + 1 + side,
758 prtpos.file_line);
760 for (page_line = 0; page_line < settings.lines_per_page;
761 ++page_line)
763 prtpos.column = hardcopy_line(&settings,
764 page_line, &prtpos);
765 if (prtpos.column == 0)
767 /* finished a file line */
768 prtpos.bytes_printed +=
769 STRLEN(skipwhite(ml_get(prtpos.file_line)));
770 if (++prtpos.file_line > eap->line2)
771 break; /* reached the end */
773 else if (prtpos.ff)
775 /* Line had a formfeed in it - start new page but
776 * stay on the current line */
777 break;
781 if (!mch_print_end_page())
782 goto print_fail;
783 if (prtpos.file_line > eap->line2)
784 break; /* reached the end */
788 * Extra blank page for duplexing with odd number of pages and
789 * more copies to come.
791 if (prtpos.file_line > eap->line2 && settings.duplex
792 && side == 0
793 && uncollated_copies + 1 < settings.n_uncollated_copies)
795 if (!mch_print_blank_page())
796 goto print_fail;
799 if (settings.duplex && prtpos.file_line <= eap->line2)
800 ++page_count;
802 /* Remember the position where the next page starts. */
803 page_prtpos = prtpos;
806 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
807 settings.jobname);
808 prt_message(IObuff);
811 print_fail:
812 if (got_int || settings.user_abort)
814 sprintf((char *)IObuff, "%s", _("Printing aborted"));
815 prt_message(IObuff);
817 mch_print_end(&settings);
819 print_fail_no_begin:
820 mch_print_cleanup();
824 * Print one page line.
825 * Return the next column to print, or zero if the line is finished.
827 static colnr_T
828 hardcopy_line(psettings, page_line, ppos)
829 prt_settings_T *psettings;
830 int page_line;
831 prt_pos_T *ppos;
833 colnr_T col;
834 char_u *line;
835 int need_break = FALSE;
836 int outputlen;
837 int tab_spaces;
838 long_u print_pos;
839 #ifdef FEAT_SYN_HL
840 prt_text_attr_T attr;
841 int id;
842 #endif
844 if (ppos->column == 0 || ppos->ff)
846 print_pos = 0;
847 tab_spaces = 0;
848 if (!ppos->ff && prt_use_number())
849 prt_line_number(psettings, page_line, ppos->file_line);
850 ppos->ff = FALSE;
852 else
854 /* left over from wrap halfway a tab */
855 print_pos = ppos->print_pos;
856 tab_spaces = ppos->lead_spaces;
859 mch_print_start_line(0, page_line);
860 line = ml_get(ppos->file_line);
863 * Loop over the columns until the end of the file line or right margin.
865 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
867 outputlen = 1;
868 #ifdef FEAT_MBYTE
869 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
870 outputlen = 1;
871 #endif
872 #ifdef FEAT_SYN_HL
874 * syntax highlighting stuff.
876 if (psettings->do_syntax)
878 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
879 if (id > 0)
880 id = syn_get_final_id(id);
881 else
882 id = 0;
883 /* Get the line again, a multi-line regexp may invalidate it. */
884 line = ml_get(ppos->file_line);
886 if (id != current_syn_id)
888 current_syn_id = id;
889 prt_get_attr(id, &attr, psettings->modec);
890 prt_set_font(attr.bold, attr.italic, attr.underline);
891 prt_set_fg(attr.fg_color);
892 prt_set_bg(attr.bg_color);
895 #endif
898 * Appropriately expand any tabs to spaces.
900 if (line[col] == TAB || tab_spaces != 0)
902 if (tab_spaces == 0)
903 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
905 while (tab_spaces > 0)
907 need_break = mch_print_text_out((char_u *)" ", 1);
908 print_pos++;
909 tab_spaces--;
910 if (need_break)
911 break;
913 /* Keep the TAB if we didn't finish it. */
914 if (need_break && tab_spaces > 0)
915 break;
917 else if (line[col] == FF
918 && printer_opts[OPT_PRINT_FORMFEED].present
919 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
920 == 'y')
922 ppos->ff = TRUE;
923 need_break = 1;
925 else
927 need_break = mch_print_text_out(line + col, outputlen);
928 #ifdef FEAT_MBYTE
929 if (has_mbyte)
930 print_pos += (*mb_ptr2cells)(line + col);
931 else
932 #endif
933 print_pos++;
937 ppos->lead_spaces = tab_spaces;
938 ppos->print_pos = (int)print_pos;
941 * Start next line of file if we clip lines, or have reached end of the
942 * line, unless we are doing a formfeed.
944 if (!ppos->ff
945 && (line[col] == NUL
946 || (printer_opts[OPT_PRINT_WRAP].present
947 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
948 == 'n')))
949 return 0;
950 return col;
953 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
956 * PS printer stuff.
958 * Sources of information to help maintain the PS printing code:
960 * 1. PostScript Language Reference, 3rd Edition,
961 * Addison-Wesley, 1999, ISBN 0-201-37922-8
962 * 2. PostScript Language Program Design,
963 * Addison-Wesley, 1988, ISBN 0-201-14396-8
964 * 3. PostScript Tutorial and Cookbook,
965 * Addison Wesley, 1985, ISBN 0-201-10179-3
966 * 4. PostScript Language Document Structuring Conventions Specification,
967 * version 3.0,
968 * Adobe Technote 5001, 25th September 1992
969 * 5. PostScript Printer Description File Format Specification, Version 4.3,
970 * Adobe technote 5003, 9th February 1996
971 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
972 * Adobe Technote 5007, 7th October 1998
973 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
974 * Adobe Technote 5014, 8th October 1996
975 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
976 * Adoboe Technote 5094, 8th September, 2001
977 * 9. CJKV Information Processing, 2nd Edition,
978 * O'Reilly, 2002, ISBN 1-56592-224-7
980 * Some of these documents can be found in PDF form on Adobe's web site -
981 * http://www.adobe.com
984 #define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
986 #define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
987 #define PRT_PS_DEFAULT_FONTSIZE (10)
988 #define PRT_PS_DEFAULT_BUFFER_SIZE (80)
990 struct prt_mediasize_S
992 char *name;
993 float width; /* width and height in points for portrait */
994 float height;
997 #define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
999 static struct prt_mediasize_S prt_mediasize[] =
1001 {"A4", 595.0, 842.0},
1002 {"letter", 612.0, 792.0},
1003 {"10x14", 720.0, 1008.0},
1004 {"A3", 842.0, 1191.0},
1005 {"A5", 420.0, 595.0},
1006 {"B4", 729.0, 1032.0},
1007 {"B5", 516.0, 729.0},
1008 {"executive", 522.0, 756.0},
1009 {"folio", 595.0, 935.0},
1010 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1011 {"legal", 612.0, 1008.0},
1012 {"quarto", 610.0, 780.0},
1013 {"statement", 396.0, 612.0},
1014 {"tabloid", 792.0, 1224.0}
1017 /* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1018 struct prt_ps_font_S
1020 int wx;
1021 int uline_offset;
1022 int uline_width;
1023 int bbox_min_y;
1024 int bbox_max_y;
1025 char *(ps_fontname[4]);
1028 #define PRT_PS_FONT_ROMAN (0)
1029 #define PRT_PS_FONT_BOLD (1)
1030 #define PRT_PS_FONT_OBLIQUE (2)
1031 #define PRT_PS_FONT_BOLDOBLIQUE (3)
1033 /* Standard font metrics for Courier family */
1034 static struct prt_ps_font_S prt_ps_courier_font =
1036 600,
1037 -100, 50,
1038 -250, 805,
1039 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1042 #ifdef FEAT_MBYTE
1043 /* Generic font metrics for multi-byte fonts */
1044 static struct prt_ps_font_S prt_ps_mb_font =
1046 1000,
1047 -100, 50,
1048 -250, 805,
1049 {NULL, NULL, NULL, NULL}
1051 #endif
1053 /* Pointer to current font set being used */
1054 static struct prt_ps_font_S* prt_ps_font;
1056 /* Structures to map user named encoding and mapping to PS equivalents for
1057 * building CID font name */
1058 struct prt_ps_encoding_S
1060 char *encoding;
1061 char *cmap_encoding;
1062 int needs_charset;
1065 struct prt_ps_charset_S
1067 char *charset;
1068 char *cmap_charset;
1069 int has_charset;
1072 #ifdef FEAT_MBYTE
1074 #define CS_JIS_C_1978 (0x01)
1075 #define CS_JIS_X_1983 (0x02)
1076 #define CS_JIS_X_1990 (0x04)
1077 #define CS_NEC (0x08)
1078 #define CS_MSWINDOWS (0x10)
1079 #define CS_CP932 (0x20)
1080 #define CS_KANJITALK6 (0x40)
1081 #define CS_KANJITALK7 (0x80)
1083 /* Japanese encodings and charsets */
1084 static struct prt_ps_encoding_S j_encodings[] =
1086 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
1087 CS_NEC)},
1088 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1089 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1090 CS_KANJITALK6|CS_KANJITALK7)},
1091 {"cp932", "RKSJ", CS_JIS_X_1983},
1092 {"ucs-2", "UCS2", CS_JIS_X_1990},
1093 {"utf-8", "UTF8" , CS_JIS_X_1990}
1095 static struct prt_ps_charset_S j_charsets[] =
1097 {"JIS_C_1978", "78", CS_JIS_C_1978},
1098 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1099 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
1100 {"NEC", "Ext", CS_NEC},
1101 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1102 {"CP932", "90ms", CS_JIS_X_1983},
1103 {"KANJITALK6", "83pv", CS_KANJITALK6},
1104 {"KANJITALK7", "90pv", CS_KANJITALK7}
1107 #define CS_GB_2312_80 (0x01)
1108 #define CS_GBT_12345_90 (0x02)
1109 #define CS_GBK2K (0x04)
1110 #define CS_SC_MAC (0x08)
1111 #define CS_GBT_90_MAC (0x10)
1112 #define CS_GBK (0x20)
1113 #define CS_SC_ISO10646 (0x40)
1115 /* Simplified Chinese encodings and charsets */
1116 static struct prt_ps_encoding_S sc_encodings[] =
1118 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1119 {"gb18030", NULL, CS_GBK2K},
1120 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
1121 CS_GBT_90_MAC)},
1122 {"gbk", "EUC", CS_GBK},
1123 {"ucs-2", "UCS2", CS_SC_ISO10646},
1124 {"utf-8", "UTF8", CS_SC_ISO10646}
1126 static struct prt_ps_charset_S sc_charsets[] =
1128 {"GB_2312-80", "GB", CS_GB_2312_80},
1129 {"GBT_12345-90","GBT", CS_GBT_12345_90},
1130 {"MAC", "GBpc", CS_SC_MAC},
1131 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1132 {"GBK", "GBK", CS_GBK},
1133 {"GB18030", "GBK2K", CS_GBK2K},
1134 {"ISO10646", "UniGB", CS_SC_ISO10646}
1137 #define CS_CNS_PLANE_1 (0x01)
1138 #define CS_CNS_PLANE_2 (0x02)
1139 #define CS_CNS_PLANE_1_2 (0x04)
1140 #define CS_B5 (0x08)
1141 #define CS_ETEN (0x10)
1142 #define CS_HK_GCCS (0x20)
1143 #define CS_HK_SCS (0x40)
1144 #define CS_HK_SCS_ETEN (0x80)
1145 #define CS_MTHKL (0x100)
1146 #define CS_MTHKS (0x200)
1147 #define CS_DLHKL (0x400)
1148 #define CS_DLHKS (0x800)
1149 #define CS_TC_ISO10646 (0x1000)
1151 /* Traditional Chinese encodings and charsets */
1152 static struct prt_ps_encoding_S tc_encodings[] =
1154 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1155 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
1156 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1157 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1158 CS_DLHKS)},
1159 {"cp950", "B5", CS_B5},
1160 {"ucs-2", "UCS2", CS_TC_ISO10646},
1161 {"utf-8", "UTF8", CS_TC_ISO10646},
1162 {"utf-16", "UTF16", CS_TC_ISO10646},
1163 {"utf-32", "UTF32", CS_TC_ISO10646}
1165 static struct prt_ps_charset_S tc_charsets[] =
1167 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1168 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1169 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
1170 {"BIG5", NULL, CS_B5},
1171 {"CP950", NULL, CS_B5},
1172 {"ETEN", "ETen", CS_ETEN},
1173 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1174 {"SCS", "HKscs", CS_HK_SCS},
1175 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1176 {"MTHKL", "HKm471", CS_MTHKL},
1177 {"MTHKS", "HKm314", CS_MTHKS},
1178 {"DLHKL", "HKdla", CS_DLHKL},
1179 {"DLHKS", "HKdlb", CS_DLHKS},
1180 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1183 #define CS_KR_X_1992 (0x01)
1184 #define CS_KR_MAC (0x02)
1185 #define CS_KR_X_1992_MS (0x04)
1186 #define CS_KR_ISO10646 (0x08)
1188 /* Korean encodings and charsets */
1189 static struct prt_ps_encoding_S k_encodings[] =
1191 {"iso-2022-kr", NULL, CS_KR_X_1992},
1192 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1193 {"johab", "Johab", CS_KR_X_1992},
1194 {"cp1361", "Johab", CS_KR_X_1992},
1195 {"uhc", "UHC", CS_KR_X_1992_MS},
1196 {"cp949", "UHC", CS_KR_X_1992_MS},
1197 {"ucs-2", "UCS2", CS_KR_ISO10646},
1198 {"utf-8", "UTF8", CS_KR_ISO10646}
1200 static struct prt_ps_charset_S k_charsets[] =
1202 {"KS_X_1992", "KSC", CS_KR_X_1992},
1203 {"CP1361", "KSC", CS_KR_X_1992},
1204 {"MAC", "KSCpc", CS_KR_MAC},
1205 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1206 {"CP949", "KSCms", CS_KR_X_1992_MS},
1207 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1208 {"ISO10646", "UniKS", CS_KR_ISO10646}
1211 /* Collections of encodings and charsets for multi-byte printing */
1212 struct prt_ps_mbfont_S
1214 int num_encodings;
1215 struct prt_ps_encoding_S *encodings;
1216 int num_charsets;
1217 struct prt_ps_charset_S *charsets;
1218 char *ascii_enc;
1219 char *defcs;
1222 static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1225 NUM_ELEMENTS(j_encodings),
1226 j_encodings,
1227 NUM_ELEMENTS(j_charsets),
1228 j_charsets,
1229 "jis_roman",
1230 "JIS_X_1983"
1233 NUM_ELEMENTS(sc_encodings),
1234 sc_encodings,
1235 NUM_ELEMENTS(sc_charsets),
1236 sc_charsets,
1237 "gb_roman",
1238 "GB_2312-80"
1241 NUM_ELEMENTS(tc_encodings),
1242 tc_encodings,
1243 NUM_ELEMENTS(tc_charsets),
1244 tc_charsets,
1245 "cns_roman",
1246 "BIG5"
1249 NUM_ELEMENTS(k_encodings),
1250 k_encodings,
1251 NUM_ELEMENTS(k_charsets),
1252 k_charsets,
1253 "ks_roman",
1254 "KS_X_1992"
1257 #endif /* FEAT_MBYTE */
1259 struct prt_ps_resource_S
1261 char_u name[64];
1262 char_u filename[MAXPATHL + 1];
1263 int type;
1264 char_u title[256];
1265 char_u version[256];
1268 /* Types of PS resource file currently used */
1269 #define PRT_RESOURCE_TYPE_PROCSET (0)
1270 #define PRT_RESOURCE_TYPE_ENCODING (1)
1271 #define PRT_RESOURCE_TYPE_CMAP (2)
1273 /* The PS prolog file version number has to match - if the prolog file is
1274 * updated, increment the number in the file and here. Version checking was
1275 * added as of VIM 6.2.
1276 * The CID prolog file version number behaves as per PS prolog.
1277 * Table of VIM and prolog versions:
1279 * VIM Prolog CIDProlog
1280 * 6.2 1.3
1281 * 7.0 1.4 1.0
1283 #define PRT_PROLOG_VERSION ((char_u *)"1.4")
1284 #define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1286 /* String versions of PS resource types - indexed by constants above so don't
1287 * re-order!
1289 static char *prt_resource_types[] =
1291 "procset",
1292 "encoding",
1293 "cmap"
1296 /* Strings to look for in a PS resource file */
1297 #define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1298 #define PRT_RESOURCE_RESOURCE "Resource-"
1299 #define PRT_RESOURCE_PROCSET "ProcSet"
1300 #define PRT_RESOURCE_ENCODING "Encoding"
1301 #define PRT_RESOURCE_CMAP "CMap"
1304 /* Data for table based DSC comment recognition, easy to extend if VIM needs to
1305 * read more comments. */
1306 #define PRT_DSC_MISC_TYPE (-1)
1307 #define PRT_DSC_TITLE_TYPE (1)
1308 #define PRT_DSC_VERSION_TYPE (2)
1309 #define PRT_DSC_ENDCOMMENTS_TYPE (3)
1311 #define PRT_DSC_TITLE "%%Title:"
1312 #define PRT_DSC_VERSION "%%Version:"
1313 #define PRT_DSC_ENDCOMMENTS "%%EndComments:"
1315 struct prt_dsc_comment_S
1317 char *string;
1318 int len;
1319 int type;
1322 struct prt_dsc_line_S
1324 int type;
1325 char_u *string;
1326 int len;
1330 #define SIZEOF_CSTR(s) (sizeof(s) - 1)
1331 static struct prt_dsc_comment_S prt_dsc_table[] =
1333 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1334 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1335 PRT_DSC_VERSION_TYPE},
1336 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1337 PRT_DSC_ENDCOMMENTS_TYPE}
1340 static void prt_write_file_raw_len __ARGS((char_u *buffer, int bytes));
1341 static void prt_write_file __ARGS((char_u *buffer));
1342 static void prt_write_file_len __ARGS((char_u *buffer, int bytes));
1343 static void prt_write_string __ARGS((char *s));
1344 static void prt_write_int __ARGS((int i));
1345 static void prt_write_boolean __ARGS((int b));
1346 static void prt_def_font __ARGS((char *new_name, char *encoding, int height, char *font));
1347 static void prt_real_bits __ARGS((double real, int precision, int *pinteger, int *pfraction));
1348 static void prt_write_real __ARGS((double val, int prec));
1349 static void prt_def_var __ARGS((char *name, double value, int prec));
1350 static void prt_flush_buffer __ARGS((void));
1351 static void prt_resource_name __ARGS((char_u *filename, void *cookie));
1352 static int prt_find_resource __ARGS((char *name, struct prt_ps_resource_S *resource));
1353 static int prt_open_resource __ARGS((struct prt_ps_resource_S *resource));
1354 static int prt_check_resource __ARGS((struct prt_ps_resource_S *resource, char_u *version));
1355 static void prt_dsc_start __ARGS((void));
1356 static void prt_dsc_noarg __ARGS((char *comment));
1357 static void prt_dsc_textline __ARGS((char *comment, char *text));
1358 static void prt_dsc_text __ARGS((char *comment, char *text));
1359 static void prt_dsc_ints __ARGS((char *comment, int count, int *ints));
1360 static void prt_dsc_requirements __ARGS((int duplex, int tumble, int collate, int color, int num_copies));
1361 static void prt_dsc_docmedia __ARGS((char *paper_name, double width, double height, double weight, char *colour, char *type));
1362 static void prt_dsc_resources __ARGS((char *comment, char *type, char *strings));
1363 static void prt_dsc_font_resource __ARGS((char *resource, struct prt_ps_font_S *ps_font));
1364 static float to_device_units __ARGS((int idx, double physsize, int def_number));
1365 static void prt_page_margins __ARGS((double width, double height, double *left, double *right, double *top, double *bottom));
1366 static void prt_font_metrics __ARGS((int font_scale));
1367 static int prt_get_cpl __ARGS((void));
1368 static int prt_get_lpp __ARGS((void));
1369 static int prt_add_resource __ARGS((struct prt_ps_resource_S *resource));
1370 static int prt_resfile_next_line __ARGS((void));
1371 static int prt_resfile_strncmp __ARGS((int offset, char *string, int len));
1372 static int prt_resfile_skip_nonws __ARGS((int offset));
1373 static int prt_resfile_skip_ws __ARGS((int offset));
1374 static int prt_next_dsc __ARGS((struct prt_dsc_line_S *p_dsc_line));
1375 #ifdef FEAT_MBYTE
1376 static int prt_build_cid_fontname __ARGS((int font, char_u *name, int name_len));
1377 static void prt_def_cidfont __ARGS((char *new_name, int height, char *cidfont));
1378 static void prt_dup_cidfont __ARGS((char *original_name, char *new_name));
1379 static int prt_match_encoding __ARGS((char *p_encoding, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_encoding_S **pp_mbenc));
1380 static int prt_match_charset __ARGS((char *p_charset, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_charset_S **pp_mbchar));
1381 #endif
1384 * Variables for the output PostScript file.
1386 static FILE *prt_ps_fd;
1387 static int prt_file_error;
1388 static char_u *prt_ps_file_name = NULL;
1391 * Various offsets and dimensions in default PostScript user space (points).
1392 * Used for text positioning calculations
1394 static float prt_page_width;
1395 static float prt_page_height;
1396 static float prt_left_margin;
1397 static float prt_right_margin;
1398 static float prt_top_margin;
1399 static float prt_bottom_margin;
1400 static float prt_line_height;
1401 static float prt_first_line_height;
1402 static float prt_char_width;
1403 static float prt_number_width;
1404 static float prt_bgcol_offset;
1405 static float prt_pos_x_moveto = 0.0;
1406 static float prt_pos_y_moveto = 0.0;
1409 * Various control variables used to decide when and how to change the
1410 * PostScript graphics state.
1412 static int prt_need_moveto;
1413 static int prt_do_moveto;
1414 static int prt_need_font;
1415 static int prt_font;
1416 static int prt_need_underline;
1417 static int prt_underline;
1418 static int prt_do_underline;
1419 static int prt_need_fgcol;
1420 static int prt_fgcol;
1421 static int prt_need_bgcol;
1422 static int prt_do_bgcol;
1423 static int prt_bgcol;
1424 static int prt_new_bgcol;
1425 static int prt_attribute_change;
1426 static float prt_text_run;
1427 static int prt_page_num;
1428 static int prt_bufsiz;
1431 * Variables controlling physical printing.
1433 static int prt_media;
1434 static int prt_portrait;
1435 static int prt_num_copies;
1436 static int prt_duplex;
1437 static int prt_tumble;
1438 static int prt_collate;
1441 * Buffers used when generating PostScript output
1443 static char_u prt_line_buffer[257];
1444 static garray_T prt_ps_buffer;
1446 # ifdef FEAT_MBYTE
1447 static int prt_do_conv;
1448 static vimconv_T prt_conv;
1450 static int prt_out_mbyte;
1451 static int prt_custom_cmap;
1452 static char prt_cmap[80];
1453 static int prt_use_courier;
1454 static int prt_in_ascii;
1455 static int prt_half_width;
1456 static char *prt_ascii_encoding;
1457 static char_u prt_hexchar[] = "0123456789abcdef";
1458 # endif
1460 static void
1461 prt_write_file_raw_len(buffer, bytes)
1462 char_u *buffer;
1463 int bytes;
1465 if (!prt_file_error
1466 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1467 != (size_t)bytes)
1469 EMSG(_("E455: Error writing to PostScript output file"));
1470 prt_file_error = TRUE;
1474 static void
1475 prt_write_file(buffer)
1476 char_u *buffer;
1478 prt_write_file_len(buffer, (int)STRLEN(buffer));
1481 static void
1482 prt_write_file_len(buffer, bytes)
1483 char_u *buffer;
1484 int bytes;
1486 #ifdef EBCDIC
1487 ebcdic2ascii(buffer, bytes);
1488 #endif
1489 prt_write_file_raw_len(buffer, bytes);
1493 * Write a string.
1495 static void
1496 prt_write_string(s)
1497 char *s;
1499 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1500 prt_write_file(prt_line_buffer);
1504 * Write an int and a space.
1506 static void
1507 prt_write_int(i)
1508 int i;
1510 sprintf((char *)prt_line_buffer, "%d ", i);
1511 prt_write_file(prt_line_buffer);
1515 * Write a boolean and a space.
1517 static void
1518 prt_write_boolean(b)
1519 int b;
1521 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1522 prt_write_file(prt_line_buffer);
1526 * Write PostScript to re-encode and define the font.
1528 static void
1529 prt_def_font(new_name, encoding, height, font)
1530 char *new_name;
1531 char *encoding;
1532 int height;
1533 char *font;
1535 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1536 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1537 prt_write_file(prt_line_buffer);
1538 #ifdef FEAT_MBYTE
1539 if (prt_out_mbyte)
1540 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
1541 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1542 else
1543 #endif
1544 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1545 "/%s %d /_%s ffs\n", new_name, height, new_name);
1546 prt_write_file(prt_line_buffer);
1549 #ifdef FEAT_MBYTE
1551 * Write a line to define the CID font.
1553 static void
1554 prt_def_cidfont(new_name, height, cidfont)
1555 char *new_name;
1556 int height;
1557 char *cidfont;
1559 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1560 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1561 prt_write_file(prt_line_buffer);
1562 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1563 "/%s %d /_%s ffs\n", new_name, height, new_name);
1564 prt_write_file(prt_line_buffer);
1568 * Write a line to define a duplicate of a CID font
1570 static void
1571 prt_dup_cidfont(original_name, new_name)
1572 char *original_name;
1573 char *new_name;
1575 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1576 "/%s %s d\n", new_name, original_name);
1577 prt_write_file(prt_line_buffer);
1579 #endif
1582 * Convert a real value into an integer and fractional part as integers, with
1583 * the fractional part being in the range [0,10^precision). The fractional part
1584 * is also rounded based on the precision + 1'th fractional digit.
1586 static void
1587 prt_real_bits(real, precision, pinteger, pfraction)
1588 double real;
1589 int precision;
1590 int *pinteger;
1591 int *pfraction;
1593 int i;
1594 int integer;
1595 float fraction;
1597 integer = (int)real;
1598 fraction = (float)(real - integer);
1599 if (real < (double)integer)
1600 fraction = -fraction;
1601 for (i = 0; i < precision; i++)
1602 fraction *= 10.0;
1604 *pinteger = integer;
1605 *pfraction = (int)(fraction + 0.5);
1609 * Write a real and a space. Save bytes if real value has no fractional part!
1610 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1611 * what decimal point character to use, but PS always requires a '.'.
1613 static void
1614 prt_write_real(val, prec)
1615 double val;
1616 int prec;
1618 int integer;
1619 int fraction;
1621 prt_real_bits(val, prec, &integer, &fraction);
1622 /* Emit integer part */
1623 sprintf((char *)prt_line_buffer, "%d", integer);
1624 prt_write_file(prt_line_buffer);
1625 /* Only emit fraction if necessary */
1626 if (fraction != 0)
1628 /* Remove any trailing zeros */
1629 while ((fraction % 10) == 0)
1631 prec--;
1632 fraction /= 10;
1634 /* Emit fraction left padded with zeros */
1635 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1636 prt_write_file(prt_line_buffer);
1638 sprintf((char *)prt_line_buffer, " ");
1639 prt_write_file(prt_line_buffer);
1643 * Write a line to define a numeric variable.
1645 static void
1646 prt_def_var(name, value, prec)
1647 char *name;
1648 double value;
1649 int prec;
1651 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1652 "/%s ", name);
1653 prt_write_file(prt_line_buffer);
1654 prt_write_real(value, prec);
1655 sprintf((char *)prt_line_buffer, "d\n");
1656 prt_write_file(prt_line_buffer);
1659 /* Convert size from font space to user space at current font scale */
1660 #define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1662 static void
1663 prt_flush_buffer()
1665 if (prt_ps_buffer.ga_len > 0)
1667 /* Any background color must be drawn first */
1668 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1670 int r, g, b;
1672 if (prt_do_moveto)
1674 prt_write_real(prt_pos_x_moveto, 2);
1675 prt_write_real(prt_pos_y_moveto, 2);
1676 prt_write_string("m\n");
1677 prt_do_moveto = FALSE;
1680 /* Size of rect of background color on which text is printed */
1681 prt_write_real(prt_text_run, 2);
1682 prt_write_real(prt_line_height, 2);
1684 /* Lastly add the color of the background */
1685 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1686 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1687 b = prt_new_bgcol & 0xff;
1688 prt_write_real(r / 255.0, 3);
1689 prt_write_real(g / 255.0, 3);
1690 prt_write_real(b / 255.0, 3);
1691 prt_write_string("bg\n");
1693 /* Draw underlines before the text as it makes it slightly easier to
1694 * find the starting point.
1696 if (prt_do_underline)
1698 if (prt_do_moveto)
1700 prt_write_real(prt_pos_x_moveto, 2);
1701 prt_write_real(prt_pos_y_moveto, 2);
1702 prt_write_string("m\n");
1703 prt_do_moveto = FALSE;
1706 /* Underline length of text run */
1707 prt_write_real(prt_text_run, 2);
1708 prt_write_string("ul\n");
1710 /* Draw the text
1711 * Note: we write text out raw - EBCDIC conversion is handled in the
1712 * PostScript world via the font encoding vector. */
1713 #ifdef FEAT_MBYTE
1714 if (prt_out_mbyte)
1715 prt_write_string("<");
1716 else
1717 #endif
1718 prt_write_string("(");
1719 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1720 #ifdef FEAT_MBYTE
1721 if (prt_out_mbyte)
1722 prt_write_string(">");
1723 else
1724 #endif
1725 prt_write_string(")");
1726 /* Add a moveto if need be and use the appropriate show procedure */
1727 if (prt_do_moveto)
1729 prt_write_real(prt_pos_x_moveto, 2);
1730 prt_write_real(prt_pos_y_moveto, 2);
1731 /* moveto and a show */
1732 prt_write_string("ms\n");
1733 prt_do_moveto = FALSE;
1735 else /* Simple show */
1736 prt_write_string("s\n");
1738 ga_clear(&prt_ps_buffer);
1739 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1744 static void
1745 prt_resource_name(filename, cookie)
1746 char_u *filename;
1747 void *cookie;
1749 char_u *resource_filename = cookie;
1751 if (STRLEN(filename) >= MAXPATHL)
1752 *resource_filename = NUL;
1753 else
1754 STRCPY(resource_filename, filename);
1757 static int
1758 prt_find_resource(name, resource)
1759 char *name;
1760 struct prt_ps_resource_S *resource;
1762 char_u buffer[MAXPATHL + 1];
1764 STRCPY(resource->name, name);
1765 /* Look for named resource file in runtimepath */
1766 STRCPY(buffer, "print");
1767 add_pathsep(buffer);
1768 STRCAT(buffer, name);
1769 STRCAT(buffer, ".ps");
1770 resource->filename[0] = NUL;
1771 return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
1772 resource->filename)
1773 && resource->filename[0] != NUL);
1776 /* PS CR and LF characters have platform independent values */
1777 #define PSLF (0x0a)
1778 #define PSCR (0x0d)
1780 /* Static buffer to read initial comments in a resource file, some can have a
1781 * couple of KB of comments! */
1782 #define PRT_FILE_BUFFER_LEN (2048)
1783 struct prt_resfile_buffer_S
1785 char_u buffer[PRT_FILE_BUFFER_LEN];
1786 int len;
1787 int line_start;
1788 int line_end;
1791 static struct prt_resfile_buffer_S prt_resfile;
1793 static int
1794 prt_resfile_next_line()
1796 int idx;
1798 /* Move to start of next line and then find end of line */
1799 idx = prt_resfile.line_end + 1;
1800 while (idx < prt_resfile.len)
1802 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
1803 break;
1804 idx++;
1806 prt_resfile.line_start = idx;
1808 while (idx < prt_resfile.len)
1810 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
1811 break;
1812 idx++;
1814 prt_resfile.line_end = idx;
1816 return (idx < prt_resfile.len);
1819 static int
1820 prt_resfile_strncmp(offset, string, len)
1821 int offset;
1822 char *string;
1823 int len;
1825 /* Force not equal if string is longer than remainder of line */
1826 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
1827 return 1;
1829 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
1830 string, len);
1833 static int
1834 prt_resfile_skip_nonws(offset)
1835 int offset;
1837 int idx;
1839 idx = prt_resfile.line_start + offset;
1840 while (idx < prt_resfile.line_end)
1842 if (isspace(prt_resfile.buffer[idx]))
1843 return idx - prt_resfile.line_start;
1844 idx++;
1846 return -1;
1849 static int
1850 prt_resfile_skip_ws(offset)
1851 int offset;
1853 int idx;
1855 idx = prt_resfile.line_start + offset;
1856 while (idx < prt_resfile.line_end)
1858 if (!isspace(prt_resfile.buffer[idx]))
1859 return idx - prt_resfile.line_start;
1860 idx++;
1862 return -1;
1865 /* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1866 * if a DSC comment is found, else false */
1867 static int
1868 prt_next_dsc(p_dsc_line)
1869 struct prt_dsc_line_S *p_dsc_line;
1871 int comment;
1872 int offset;
1874 /* Move to start of next line */
1875 if (!prt_resfile_next_line())
1876 return FALSE;
1878 /* DSC comments always start %% */
1879 if (prt_resfile_strncmp(0, "%%", 2) != 0)
1880 return FALSE;
1882 /* Find type of DSC comment */
1883 for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
1884 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1885 prt_dsc_table[comment].len) == 0)
1886 break;
1888 if (comment != NUM_ELEMENTS(prt_dsc_table))
1890 /* Return type of comment */
1891 p_dsc_line->type = prt_dsc_table[comment].type;
1892 offset = prt_dsc_table[comment].len;
1894 else
1896 /* Unrecognised DSC comment, skip to ws after comment leader */
1897 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1898 offset = prt_resfile_skip_nonws(0);
1899 if (offset == -1)
1900 return FALSE;
1903 /* Skip ws to comment value */
1904 offset = prt_resfile_skip_ws(offset);
1905 if (offset == -1)
1906 return FALSE;
1908 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1909 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1911 return TRUE;
1914 /* Improved hand crafted parser to get the type, title, and version number of a
1915 * PS resource file so the file details can be added to the DSC header comments.
1917 static int
1918 prt_open_resource(resource)
1919 struct prt_ps_resource_S *resource;
1921 int offset;
1922 int seen_all;
1923 int seen_title;
1924 int seen_version;
1925 FILE *fd_resource;
1926 struct prt_dsc_line_S dsc_line;
1928 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1929 if (fd_resource == NULL)
1931 EMSG2(_("E624: Can't open file \"%s\""), resource->filename);
1932 return FALSE;
1934 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1936 /* Parse first line to ensure valid resource file */
1937 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
1938 PRT_FILE_BUFFER_LEN, fd_resource);
1939 if (ferror(fd_resource))
1941 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1942 resource->filename);
1943 fclose(fd_resource);
1944 return FALSE;
1947 prt_resfile.line_end = -1;
1948 prt_resfile.line_start = 0;
1949 if (!prt_resfile_next_line())
1950 return FALSE;
1952 offset = 0;
1954 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
1955 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
1957 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1958 resource->filename);
1959 fclose(fd_resource);
1960 return FALSE;
1963 /* Skip over any version numbers and following ws */
1964 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
1965 offset = prt_resfile_skip_nonws(offset);
1966 if (offset == -1)
1967 return FALSE;
1968 offset = prt_resfile_skip_ws(offset);
1969 if (offset == -1)
1970 return FALSE;
1972 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
1973 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
1975 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1976 resource->filename);
1977 fclose(fd_resource);
1978 return FALSE;
1980 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
1982 /* Decide type of resource in the file */
1983 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
1984 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
1985 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1986 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
1987 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
1988 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1989 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
1990 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
1991 resource->type = PRT_RESOURCE_TYPE_CMAP;
1992 else
1994 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1995 resource->filename);
1996 fclose(fd_resource);
1997 return FALSE;
2000 /* Look for title and version of resource */
2001 resource->title[0] = '\0';
2002 resource->version[0] = '\0';
2003 seen_title = FALSE;
2004 seen_version = FALSE;
2005 seen_all = FALSE;
2006 while (!seen_all && prt_next_dsc(&dsc_line))
2008 switch (dsc_line.type)
2010 case PRT_DSC_TITLE_TYPE:
2011 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
2012 seen_title = TRUE;
2013 if (seen_version)
2014 seen_all = TRUE;
2015 break;
2017 case PRT_DSC_VERSION_TYPE:
2018 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
2019 seen_version = TRUE;
2020 if (seen_title)
2021 seen_all = TRUE;
2022 break;
2024 case PRT_DSC_ENDCOMMENTS_TYPE:
2025 /* Wont find title or resource after this comment, stop searching */
2026 seen_all = TRUE;
2027 break;
2029 case PRT_DSC_MISC_TYPE:
2030 /* Not interested in whatever comment this line had */
2031 break;
2035 if (!seen_title || !seen_version)
2037 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2038 resource->filename);
2039 fclose(fd_resource);
2040 return FALSE;
2043 fclose(fd_resource);
2045 return TRUE;
2048 static int
2049 prt_check_resource(resource, version)
2050 struct prt_ps_resource_S *resource;
2051 char_u *version;
2053 /* Version number m.n should match, the revision number does not matter */
2054 if (STRNCMP(resource->version, version, STRLEN(version)))
2056 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2057 resource->name);
2058 return FALSE;
2061 /* Other checks to be added as needed */
2062 return TRUE;
2065 static void
2066 prt_dsc_start()
2068 prt_write_string("%!PS-Adobe-3.0\n");
2071 static void
2072 prt_dsc_noarg(comment)
2073 char *comment;
2075 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2076 "%%%%%s\n", comment);
2077 prt_write_file(prt_line_buffer);
2080 static void
2081 prt_dsc_textline(comment, text)
2082 char *comment;
2083 char *text;
2085 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2086 "%%%%%s: %s\n", comment, text);
2087 prt_write_file(prt_line_buffer);
2090 static void
2091 prt_dsc_text(comment, text)
2092 char *comment;
2093 char *text;
2095 /* TODO - should scan 'text' for any chars needing escaping! */
2096 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2097 "%%%%%s: (%s)\n", comment, text);
2098 prt_write_file(prt_line_buffer);
2101 #define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2103 static void
2104 prt_dsc_ints(comment, count, ints)
2105 char *comment;
2106 int count;
2107 int *ints;
2109 int i;
2111 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2112 "%%%%%s:", comment);
2113 prt_write_file(prt_line_buffer);
2115 for (i = 0; i < count; i++)
2117 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2118 prt_write_file(prt_line_buffer);
2121 prt_write_string("\n");
2124 static void
2125 prt_dsc_resources(comment, type, string)
2126 char *comment; /* if NULL add to previous */
2127 char *type;
2128 char *string;
2130 if (comment != NULL)
2131 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2132 "%%%%%s: %s", comment, type);
2133 else
2134 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2135 "%%%%+ %s", type);
2136 prt_write_file(prt_line_buffer);
2138 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2139 " %s\n", string);
2140 prt_write_file(prt_line_buffer);
2143 static void
2144 prt_dsc_font_resource(resource, ps_font)
2145 char *resource;
2146 struct prt_ps_font_S *ps_font;
2148 int i;
2150 prt_dsc_resources(resource, "font",
2151 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
2152 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
2153 if (ps_font->ps_fontname[i] != NULL)
2154 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
2157 static void
2158 prt_dsc_requirements(duplex, tumble, collate, color, num_copies)
2159 int duplex;
2160 int tumble;
2161 int collate;
2162 int color;
2163 int num_copies;
2165 /* Only output the comment if we need to.
2166 * Note: tumble is ignored if we are not duplexing
2168 if (!(duplex || collate || color || (num_copies > 1)))
2169 return;
2171 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2172 prt_write_file(prt_line_buffer);
2174 if (duplex)
2176 prt_write_string(" duplex");
2177 if (tumble)
2178 prt_write_string("(tumble)");
2180 if (collate)
2181 prt_write_string(" collate");
2182 if (color)
2183 prt_write_string(" color");
2184 if (num_copies > 1)
2186 prt_write_string(" numcopies(");
2187 /* Note: no space wanted so dont use prt_write_int() */
2188 sprintf((char *)prt_line_buffer, "%d", num_copies);
2189 prt_write_file(prt_line_buffer);
2190 prt_write_string(")");
2192 prt_write_string("\n");
2195 static void
2196 prt_dsc_docmedia(paper_name, width, height, weight, colour, type)
2197 char *paper_name;
2198 double width;
2199 double height;
2200 double weight;
2201 char *colour;
2202 char *type;
2204 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2205 "%%%%DocumentMedia: %s ", paper_name);
2206 prt_write_file(prt_line_buffer);
2207 prt_write_real(width, 2);
2208 prt_write_real(height, 2);
2209 prt_write_real(weight, 2);
2210 if (colour == NULL)
2211 prt_write_string("()");
2212 else
2213 prt_write_string(colour);
2214 prt_write_string(" ");
2215 if (type == NULL)
2216 prt_write_string("()");
2217 else
2218 prt_write_string(type);
2219 prt_write_string("\n");
2222 void
2223 mch_print_cleanup()
2225 #ifdef FEAT_MBYTE
2226 if (prt_out_mbyte)
2228 int i;
2230 /* Free off all CID font names created, but first clear duplicate
2231 * pointers to the same string (when the same font is used for more than
2232 * one style).
2234 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2236 if (prt_ps_mb_font.ps_fontname[i] != NULL)
2237 vim_free(prt_ps_mb_font.ps_fontname[i]);
2238 prt_ps_mb_font.ps_fontname[i] = NULL;
2242 if (prt_do_conv)
2244 convert_setup(&prt_conv, NULL, NULL);
2245 prt_do_conv = FALSE;
2247 #endif
2248 if (prt_ps_fd != NULL)
2250 fclose(prt_ps_fd);
2251 prt_ps_fd = NULL;
2252 prt_file_error = FALSE;
2254 if (prt_ps_file_name != NULL)
2256 vim_free(prt_ps_file_name);
2257 prt_ps_file_name = NULL;
2261 static float
2262 to_device_units(idx, physsize, def_number)
2263 int idx;
2264 double physsize;
2265 int def_number;
2267 float ret;
2268 int u;
2269 int nr;
2271 u = prt_get_unit(idx);
2272 if (u == PRT_UNIT_NONE)
2274 u = PRT_UNIT_PERC;
2275 nr = def_number;
2277 else
2278 nr = printer_opts[idx].number;
2280 switch (u)
2282 case PRT_UNIT_INCH:
2283 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2284 break;
2285 case PRT_UNIT_MM:
2286 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2287 break;
2288 case PRT_UNIT_POINT:
2289 ret = (float)nr;
2290 break;
2291 case PRT_UNIT_PERC:
2292 default:
2293 ret = (float)(physsize * nr) / 100;
2294 break;
2297 return ret;
2301 * Calculate margins for given width and height from printoptions settings.
2303 static void
2304 prt_page_margins(width, height, left, right, top, bottom)
2305 double width;
2306 double height;
2307 double *left;
2308 double *right;
2309 double *top;
2310 double *bottom;
2312 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2313 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2314 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2315 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2318 static void
2319 prt_font_metrics(font_scale)
2320 int font_scale;
2322 prt_line_height = (float)font_scale;
2323 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2327 static int
2328 prt_get_cpl()
2330 if (prt_use_number())
2332 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2333 #ifdef FEAT_MBYTE
2334 /* If we are outputting multi-byte characters then line numbers will be
2335 * printed with half width characters
2337 if (prt_out_mbyte)
2338 prt_number_width /= 2;
2339 #endif
2340 prt_left_margin += prt_number_width;
2342 else
2343 prt_number_width = 0.0;
2345 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2348 #ifdef FEAT_MBYTE
2349 static int
2350 prt_build_cid_fontname(font, name, name_len)
2351 int font;
2352 char_u *name;
2353 int name_len;
2355 char *fontname;
2357 fontname = (char *)alloc(name_len + 1);
2358 if (fontname == NULL)
2359 return FALSE;
2360 vim_strncpy((char_u *)fontname, name, name_len);
2361 prt_ps_mb_font.ps_fontname[font] = fontname;
2363 return TRUE;
2365 #endif
2368 * Get number of lines of text that fit on a page (excluding the header).
2370 static int
2371 prt_get_lpp()
2373 int lpp;
2376 * Calculate offset to lower left corner of background rect based on actual
2377 * font height (based on its bounding box) and the line height, handling the
2378 * case where the font height can exceed the line height.
2380 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2381 prt_ps_font->bbox_min_y);
2382 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2384 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2385 (1000.0 - (prt_ps_font->bbox_max_y -
2386 prt_ps_font->bbox_min_y)) / 2);
2389 /* Get height for topmost line based on background rect offset. */
2390 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2392 /* Calculate lpp */
2393 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2395 /* Adjust top margin if there is a header */
2396 prt_top_margin -= prt_line_height * prt_header_height();
2398 return lpp - prt_header_height();
2401 #ifdef FEAT_MBYTE
2402 static int
2403 prt_match_encoding(p_encoding, p_cmap, pp_mbenc)
2404 char *p_encoding;
2405 struct prt_ps_mbfont_S *p_cmap;
2406 struct prt_ps_encoding_S **pp_mbenc;
2408 int mbenc;
2409 int enc_len;
2410 struct prt_ps_encoding_S *p_mbenc;
2412 *pp_mbenc = NULL;
2413 /* Look for recognised encoding */
2414 enc_len = (int)STRLEN(p_encoding);
2415 p_mbenc = p_cmap->encodings;
2416 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2418 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2420 *pp_mbenc = p_mbenc;
2421 return TRUE;
2423 p_mbenc++;
2425 return FALSE;
2428 static int
2429 prt_match_charset(p_charset, p_cmap, pp_mbchar)
2430 char *p_charset;
2431 struct prt_ps_mbfont_S *p_cmap;
2432 struct prt_ps_charset_S **pp_mbchar;
2434 int mbchar;
2435 int char_len;
2436 struct prt_ps_charset_S *p_mbchar;
2438 /* Look for recognised character set, using default if one is not given */
2439 if (*p_charset == NUL)
2440 p_charset = p_cmap->defcs;
2441 char_len = (int)STRLEN(p_charset);
2442 p_mbchar = p_cmap->charsets;
2443 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2445 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2447 *pp_mbchar = p_mbchar;
2448 return TRUE;
2450 p_mbchar++;
2452 return FALSE;
2454 #endif
2457 mch_print_init(psettings, jobname, forceit)
2458 prt_settings_T *psettings;
2459 char_u *jobname;
2460 int forceit UNUSED;
2462 int i;
2463 char *paper_name;
2464 int paper_strlen;
2465 int fontsize;
2466 char_u *p;
2467 double left;
2468 double right;
2469 double top;
2470 double bottom;
2471 #ifdef FEAT_MBYTE
2472 int props;
2473 int cmap = 0;
2474 char_u *p_encoding;
2475 struct prt_ps_encoding_S *p_mbenc;
2476 struct prt_ps_encoding_S *p_mbenc_first;
2477 struct prt_ps_charset_S *p_mbchar = NULL;
2478 #endif
2480 #if 0
2482 * TODO:
2483 * If "forceit" is false: pop up a dialog to select:
2484 * - printer name
2485 * - copies
2486 * - collated/uncollated
2487 * - duplex off/long side/short side
2488 * - paper size
2489 * - portrait/landscape
2490 * - font size
2492 * If "forceit" is true: use the default printer and settings
2494 if (forceit)
2495 s_pd.Flags |= PD_RETURNDEFAULT;
2496 #endif
2499 * Set up font and encoding.
2501 #ifdef FEAT_MBYTE
2502 p_encoding = enc_skip(p_penc);
2503 if (*p_encoding == NUL)
2504 p_encoding = enc_skip(p_enc);
2506 /* Look for a multi-byte font that matches the encoding and character set.
2507 * Only look if multi-byte character set is defined, or using multi-byte
2508 * encoding other than Unicode. This is because a Unicode encoding does not
2509 * uniquely identify a CJK character set to use. */
2510 p_mbenc = NULL;
2511 props = enc_canon_props(p_encoding);
2512 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
2514 p_mbenc_first = NULL;
2515 for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
2516 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
2517 &p_mbenc))
2519 if (p_mbenc_first == NULL)
2520 p_mbenc_first = p_mbenc;
2521 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
2522 &p_mbchar))
2523 break;
2526 /* Use first encoding matched if no charset matched */
2527 if (p_mbchar == NULL && p_mbenc_first != NULL)
2528 p_mbenc = p_mbenc_first;
2531 prt_out_mbyte = (p_mbenc != NULL);
2532 if (prt_out_mbyte)
2534 /* Build CMap name - will be same for all multi-byte fonts used */
2535 prt_cmap[0] = NUL;
2537 prt_custom_cmap = (p_mbchar == NULL);
2538 if (!prt_custom_cmap)
2540 /* Check encoding and character set are compatible */
2541 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2543 EMSG(_("E673: Incompatible multi-byte encoding and character set."));
2544 return FALSE;
2547 /* Add charset name if not empty */
2548 if (p_mbchar->cmap_charset != NULL)
2550 vim_strncpy((char_u *)prt_cmap,
2551 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
2552 STRCAT(prt_cmap, "-");
2555 else
2557 /* Add custom CMap character set name */
2558 if (*p_pmcs == NUL)
2560 EMSG(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
2561 return FALSE;
2563 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2564 STRCAT(prt_cmap, "-");
2567 /* CMap name ends with (optional) encoding name and -H for horizontal */
2568 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
2569 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
2571 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2572 STRCAT(prt_cmap, "-");
2574 STRCAT(prt_cmap, "H");
2576 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2578 EMSG(_("E675: No default font specified for multi-byte printing."));
2579 return FALSE;
2582 /* Derive CID font names with fallbacks if not defined */
2583 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2584 mbfont_opts[OPT_MBFONT_REGULAR].string,
2585 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2586 return FALSE;
2587 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2588 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2589 mbfont_opts[OPT_MBFONT_BOLD].string,
2590 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2591 return FALSE;
2592 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2593 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2594 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2595 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2596 return FALSE;
2597 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2598 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
2599 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2600 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
2601 return FALSE;
2603 /* Check if need to use Courier for ASCII code range, and if so pick up
2604 * the encoding to use */
2605 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2606 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2607 if (prt_use_courier)
2609 /* Use national ASCII variant unless ASCII wanted */
2610 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2611 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2612 prt_ascii_encoding = "ascii";
2613 else
2614 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2617 prt_ps_font = &prt_ps_mb_font;
2619 else
2620 #endif
2622 #ifdef FEAT_MBYTE
2623 prt_use_courier = FALSE;
2624 #endif
2625 prt_ps_font = &prt_ps_courier_font;
2629 * Find the size of the paper and set the margins.
2631 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2632 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2633 if (printer_opts[OPT_PRINT_PAPER].present)
2635 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2636 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2638 else
2640 paper_name = "A4";
2641 paper_strlen = 2;
2643 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
2644 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2645 && STRNICMP(prt_mediasize[i].name, paper_name,
2646 paper_strlen) == 0)
2647 break;
2648 if (i == PRT_MEDIASIZE_LEN)
2649 i = 0;
2650 prt_media = i;
2653 * Set PS pagesize based on media dimensions and print orientation.
2654 * Note: Media and page sizes have defined meanings in PostScript and should
2655 * be kept distinct. Media is the paper (or transparency, or ...) that is
2656 * printed on, whereas the page size is the area that the PostScript
2657 * interpreter renders into.
2659 if (prt_portrait)
2661 prt_page_width = prt_mediasize[i].width;
2662 prt_page_height = prt_mediasize[i].height;
2664 else
2666 prt_page_width = prt_mediasize[i].height;
2667 prt_page_height = prt_mediasize[i].width;
2671 * Set PS page margins based on the PS pagesize, not the mediasize - this
2672 * needs to be done before the cpl and lpp are calculated.
2674 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2675 &bottom);
2676 prt_left_margin = (float)left;
2677 prt_right_margin = (float)right;
2678 prt_top_margin = (float)top;
2679 prt_bottom_margin = (float)bottom;
2682 * Set up the font size.
2684 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2685 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2686 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2687 fontsize = atoi((char *)p + 2);
2688 prt_font_metrics(fontsize);
2691 * Return the number of characters per line, and lines per page for the
2692 * generic print code.
2694 psettings->chars_per_line = prt_get_cpl();
2695 psettings->lines_per_page = prt_get_lpp();
2697 /* Catch margin settings that leave no space for output! */
2698 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2699 return FAIL;
2702 * Sort out the number of copies to be printed. PS by default will do
2703 * uncollated copies for you, so once we know how many uncollated copies are
2704 * wanted cache it away and lie to the generic code that we only want one
2705 * uncollated copy.
2707 psettings->n_collated_copies = 1;
2708 psettings->n_uncollated_copies = 1;
2709 prt_num_copies = 1;
2710 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2711 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2712 if (prt_collate)
2714 /* TODO: Get number of collated copies wanted. */
2715 psettings->n_collated_copies = 1;
2717 else
2719 /* TODO: Get number of uncollated copies wanted and update the cached
2720 * count.
2722 prt_num_copies = 1;
2725 psettings->jobname = jobname;
2728 * Set up printer duplex and tumble based on Duplex option setting - default
2729 * is long sided duplex printing (i.e. no tumble).
2731 prt_duplex = TRUE;
2732 prt_tumble = FALSE;
2733 psettings->duplex = 1;
2734 if (printer_opts[OPT_PRINT_DUPLEX].present)
2736 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2738 prt_duplex = FALSE;
2739 psettings->duplex = 0;
2741 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2742 == 0)
2743 prt_tumble = TRUE;
2746 /* For now user abort not supported */
2747 psettings->user_abort = 0;
2749 /* If the user didn't specify a file name, use a temp file. */
2750 if (psettings->outfile == NULL)
2752 prt_ps_file_name = vim_tempname('p');
2753 if (prt_ps_file_name == NULL)
2755 EMSG(_(e_notmp));
2756 return FAIL;
2758 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2760 else
2762 p = expand_env_save(psettings->outfile);
2763 if (p != NULL)
2765 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2766 vim_free(p);
2769 if (prt_ps_fd == NULL)
2771 EMSG(_("E324: Can't open PostScript output file"));
2772 mch_print_cleanup();
2773 return FAIL;
2776 prt_bufsiz = psettings->chars_per_line;
2777 #ifdef FEAT_MBYTE
2778 if (prt_out_mbyte)
2779 prt_bufsiz *= 2;
2780 #endif
2781 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2783 prt_page_num = 0;
2785 prt_attribute_change = FALSE;
2786 prt_need_moveto = FALSE;
2787 prt_need_font = FALSE;
2788 prt_need_fgcol = FALSE;
2789 prt_need_bgcol = FALSE;
2790 prt_need_underline = FALSE;
2792 prt_file_error = FALSE;
2794 return OK;
2797 static int
2798 prt_add_resource(resource)
2799 struct prt_ps_resource_S *resource;
2801 FILE* fd_resource;
2802 char_u resource_buffer[512];
2803 size_t bytes_read;
2805 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2806 if (fd_resource == NULL)
2808 EMSG2(_("E456: Can't open file \"%s\""), resource->filename);
2809 return FALSE;
2811 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2812 (char *)resource->title);
2814 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2816 for (;;)
2818 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2819 sizeof(resource_buffer), fd_resource);
2820 if (ferror(fd_resource))
2822 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
2823 resource->filename);
2824 fclose(fd_resource);
2825 return FALSE;
2827 if (bytes_read == 0)
2828 break;
2829 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
2830 if (prt_file_error)
2832 fclose(fd_resource);
2833 return FALSE;
2836 fclose(fd_resource);
2838 prt_dsc_noarg("EndDocument");
2840 prt_dsc_noarg("EndResource");
2842 return TRUE;
2846 mch_print_begin(psettings)
2847 prt_settings_T *psettings;
2849 time_t now;
2850 int bbox[4];
2851 char *p_time;
2852 double left;
2853 double right;
2854 double top;
2855 double bottom;
2856 struct prt_ps_resource_S res_prolog;
2857 struct prt_ps_resource_S res_encoding;
2858 char buffer[256];
2859 char_u *p_encoding;
2860 char_u *p;
2861 #ifdef FEAT_MBYTE
2862 struct prt_ps_resource_S res_cidfont;
2863 struct prt_ps_resource_S res_cmap;
2864 #endif
2867 * PS DSC Header comments - no PS code!
2869 prt_dsc_start();
2870 prt_dsc_textline("Title", (char *)psettings->jobname);
2871 if (!get_user_name((char_u *)buffer, 256))
2872 STRCPY(buffer, "Unknown");
2873 prt_dsc_textline("For", buffer);
2874 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2875 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2876 now = time(NULL);
2877 p_time = ctime(&now);
2878 /* Note: ctime() adds a \n so we have to remove it :-( */
2879 p = vim_strchr((char_u *)p_time, '\n');
2880 if (p != NULL)
2881 *p = NUL;
2882 prt_dsc_textline("CreationDate", p_time);
2883 prt_dsc_textline("DocumentData", "Clean8Bit");
2884 prt_dsc_textline("Orientation", "Portrait");
2885 prt_dsc_atend("Pages");
2886 prt_dsc_textline("PageOrder", "Ascend");
2887 /* The bbox does not change with orientation - it is always in the default
2888 * user coordinate system! We have to recalculate right and bottom
2889 * coordinates based on the font metrics for the bbox to be accurate. */
2890 prt_page_margins(prt_mediasize[prt_media].width,
2891 prt_mediasize[prt_media].height,
2892 &left, &right, &top, &bottom);
2893 bbox[0] = (int)left;
2894 if (prt_portrait)
2896 /* In portrait printing the fixed point is the top left corner so we
2897 * derive the bbox from that point. We have the expected cpl chars
2898 * across the media and lpp lines down the media.
2900 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2901 * prt_line_height);
2902 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2903 + 0.5);
2904 bbox[3] = (int)(top + 0.5);
2906 else
2908 /* In landscape printing the fixed point is the bottom left corner so we
2909 * derive the bbox from that point. We have lpp chars across the media
2910 * and cpl lines up the media.
2912 bbox[1] = (int)bottom;
2913 bbox[2] = (int)(left + ((psettings->lines_per_page
2914 + prt_header_height()) * prt_line_height) + 0.5);
2915 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2916 + 0.5);
2918 prt_dsc_ints("BoundingBox", 4, bbox);
2919 /* The media width and height does not change with landscape printing! */
2920 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2921 prt_mediasize[prt_media].width,
2922 prt_mediasize[prt_media].height,
2923 (double)0, NULL, NULL);
2924 /* Define fonts needed */
2925 #ifdef FEAT_MBYTE
2926 if (!prt_out_mbyte || prt_use_courier)
2927 #endif
2928 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
2929 #ifdef FEAT_MBYTE
2930 if (prt_out_mbyte)
2932 prt_dsc_font_resource((prt_use_courier ? NULL
2933 : "DocumentNeededResources"), &prt_ps_mb_font);
2934 if (!prt_custom_cmap)
2935 prt_dsc_resources(NULL, "cmap", prt_cmap);
2937 #endif
2939 /* Search for external resources VIM supplies */
2940 if (!prt_find_resource("prolog", &res_prolog))
2942 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
2943 return FALSE;
2945 if (!prt_open_resource(&res_prolog))
2946 return FALSE;
2947 if (!prt_check_resource(&res_prolog, PRT_PROLOG_VERSION))
2948 return FALSE;
2949 #ifdef FEAT_MBYTE
2950 if (prt_out_mbyte)
2952 /* Look for required version of multi-byte printing procset */
2953 if (!prt_find_resource("cidfont", &res_cidfont))
2955 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
2956 return FALSE;
2958 if (!prt_open_resource(&res_cidfont))
2959 return FALSE;
2960 if (!prt_check_resource(&res_cidfont, PRT_CID_PROLOG_VERSION))
2961 return FALSE;
2963 #endif
2965 /* Find an encoding to use for printing.
2966 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2967 * that cannot be found then default to "latin1".
2968 * Note: VIM specific encoding header is always skipped.
2970 #ifdef FEAT_MBYTE
2971 if (!prt_out_mbyte)
2973 #endif
2974 p_encoding = enc_skip(p_penc);
2975 if (*p_encoding == NUL
2976 || !prt_find_resource((char *)p_encoding, &res_encoding))
2978 /* 'printencoding' not set or not supported - find alternate */
2979 #ifdef FEAT_MBYTE
2980 int props;
2982 p_encoding = enc_skip(p_enc);
2983 props = enc_canon_props(p_encoding);
2984 if (!(props & ENC_8BIT)
2985 || !prt_find_resource((char *)p_encoding, &res_encoding))
2986 /* 8-bit 'encoding' is not supported */
2987 #endif
2989 /* Use latin1 as default printing encoding */
2990 p_encoding = (char_u *)"latin1";
2991 if (!prt_find_resource((char *)p_encoding, &res_encoding))
2993 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
2994 p_encoding);
2995 return FALSE;
2999 if (!prt_open_resource(&res_encoding))
3000 return FALSE;
3001 /* For the moment there are no checks on encoding resource files to
3002 * perform */
3003 #ifdef FEAT_MBYTE
3005 else
3007 p_encoding = enc_skip(p_penc);
3008 if (*p_encoding == NUL)
3009 p_encoding = enc_skip(p_enc);
3010 if (prt_use_courier)
3012 /* Include ASCII range encoding vector */
3013 if (!prt_find_resource(prt_ascii_encoding, &res_encoding))
3015 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3016 prt_ascii_encoding);
3017 return FALSE;
3019 if (!prt_open_resource(&res_encoding))
3020 return FALSE;
3021 /* For the moment there are no checks on encoding resource files to
3022 * perform */
3026 prt_conv.vc_type = CONV_NONE;
3027 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
3028 /* Set up encoding conversion if required */
3029 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
3031 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
3032 p_encoding);
3033 return FALSE;
3035 prt_do_conv = TRUE;
3037 prt_do_conv = prt_conv.vc_type != CONV_NONE;
3039 if (prt_out_mbyte && prt_custom_cmap)
3041 /* Find user supplied CMap */
3042 if (!prt_find_resource(prt_cmap, &res_cmap))
3044 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3045 prt_cmap);
3046 return FALSE;
3048 if (!prt_open_resource(&res_cmap))
3049 return FALSE;
3051 #endif
3053 /* List resources supplied */
3054 STRCPY(buffer, res_prolog.title);
3055 STRCAT(buffer, " ");
3056 STRCAT(buffer, res_prolog.version);
3057 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
3058 #ifdef FEAT_MBYTE
3059 if (prt_out_mbyte)
3061 STRCPY(buffer, res_cidfont.title);
3062 STRCAT(buffer, " ");
3063 STRCAT(buffer, res_cidfont.version);
3064 prt_dsc_resources(NULL, "procset", buffer);
3066 if (prt_custom_cmap)
3068 STRCPY(buffer, res_cmap.title);
3069 STRCAT(buffer, " ");
3070 STRCAT(buffer, res_cmap.version);
3071 prt_dsc_resources(NULL, "cmap", buffer);
3074 if (!prt_out_mbyte || prt_use_courier)
3075 #endif
3077 STRCPY(buffer, res_encoding.title);
3078 STRCAT(buffer, " ");
3079 STRCAT(buffer, res_encoding.version);
3080 prt_dsc_resources(NULL, "encoding", buffer);
3082 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3083 #ifdef FEAT_SYN_HL
3084 psettings->do_syntax
3085 #else
3087 #endif
3088 , prt_num_copies);
3089 prt_dsc_noarg("EndComments");
3092 * PS Document page defaults
3094 prt_dsc_noarg("BeginDefaults");
3096 /* List font resources most likely common to all pages */
3097 #ifdef FEAT_MBYTE
3098 if (!prt_out_mbyte || prt_use_courier)
3099 #endif
3100 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
3101 #ifdef FEAT_MBYTE
3102 if (prt_out_mbyte)
3104 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3105 &prt_ps_mb_font);
3106 if (!prt_custom_cmap)
3107 prt_dsc_resources(NULL, "cmap", prt_cmap);
3109 #endif
3111 /* Paper will be used for all pages */
3112 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3114 prt_dsc_noarg("EndDefaults");
3117 * PS Document prolog inclusion - all required procsets.
3119 prt_dsc_noarg("BeginProlog");
3121 /* Add required procsets - NOTE: order is important! */
3122 if (!prt_add_resource(&res_prolog))
3123 return FALSE;
3124 #ifdef FEAT_MBYTE
3125 if (prt_out_mbyte)
3127 /* Add CID font procset, and any user supplied CMap */
3128 if (!prt_add_resource(&res_cidfont))
3129 return FALSE;
3130 if (prt_custom_cmap && !prt_add_resource(&res_cmap))
3131 return FALSE;
3133 #endif
3135 #ifdef FEAT_MBYTE
3136 if (!prt_out_mbyte || prt_use_courier)
3137 #endif
3138 /* There will be only one Roman font encoding to be included in the PS
3139 * file. */
3140 if (!prt_add_resource(&res_encoding))
3141 return FALSE;
3143 prt_dsc_noarg("EndProlog");
3146 * PS Document setup - must appear after the prolog
3148 prt_dsc_noarg("BeginSetup");
3150 /* Device setup - page size and number of uncollated copies */
3151 prt_write_int((int)prt_mediasize[prt_media].width);
3152 prt_write_int((int)prt_mediasize[prt_media].height);
3153 prt_write_int(0);
3154 prt_write_string("sps\n");
3155 prt_write_int(prt_num_copies);
3156 prt_write_string("nc\n");
3157 prt_write_boolean(prt_duplex);
3158 prt_write_boolean(prt_tumble);
3159 prt_write_string("dt\n");
3160 prt_write_boolean(prt_collate);
3161 prt_write_string("c\n");
3163 /* Font resource inclusion and definition */
3164 #ifdef FEAT_MBYTE
3165 if (!prt_out_mbyte || prt_use_courier)
3167 /* When using Courier for ASCII range when printing multi-byte, need to
3168 * pick up ASCII encoding to use with it. */
3169 if (prt_use_courier)
3170 p_encoding = (char_u *)prt_ascii_encoding;
3171 #endif
3172 prt_dsc_resources("IncludeResource", "font",
3173 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3174 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3175 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3176 prt_dsc_resources("IncludeResource", "font",
3177 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3178 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3179 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3180 prt_dsc_resources("IncludeResource", "font",
3181 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3182 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3183 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3184 prt_dsc_resources("IncludeResource", "font",
3185 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3186 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3187 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3188 #ifdef FEAT_MBYTE
3190 if (prt_out_mbyte)
3192 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3193 * not have an italic form being a western style, so where no font is
3194 * defined for these faces VIM falls back to an existing face.
3195 * Note: if using Courier for the ASCII range then the printout will
3196 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3198 prt_dsc_resources("IncludeResource", "font",
3199 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3200 if (!prt_custom_cmap)
3201 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3202 prt_def_cidfont("CF0", (int)prt_line_height,
3203 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3205 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3207 prt_dsc_resources("IncludeResource", "font",
3208 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3209 if (!prt_custom_cmap)
3210 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3211 prt_def_cidfont("CF1", (int)prt_line_height,
3212 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3214 else
3215 /* Use ROMAN for BOLD */
3216 prt_dup_cidfont("CF0", "CF1");
3218 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3220 prt_dsc_resources("IncludeResource", "font",
3221 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3222 if (!prt_custom_cmap)
3223 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3224 prt_def_cidfont("CF2", (int)prt_line_height,
3225 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3227 else
3228 /* Use ROMAN for OBLIQUE */
3229 prt_dup_cidfont("CF0", "CF2");
3231 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3233 prt_dsc_resources("IncludeResource", "font",
3234 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3235 if (!prt_custom_cmap)
3236 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3237 prt_def_cidfont("CF3", (int)prt_line_height,
3238 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3240 else
3241 /* Use BOLD for BOLDOBLIQUE */
3242 prt_dup_cidfont("CF1", "CF3");
3244 #endif
3246 /* Misc constant vars used for underlining and background rects */
3247 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3248 prt_ps_font->uline_offset), 2);
3249 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3250 prt_ps_font->uline_width), 2);
3251 prt_def_var("BO", prt_bgcol_offset, 2);
3253 prt_dsc_noarg("EndSetup");
3255 /* Fail if any problems writing out to the PS file */
3256 return !prt_file_error;
3259 void
3260 mch_print_end(psettings)
3261 prt_settings_T *psettings;
3263 prt_dsc_noarg("Trailer");
3266 * Output any info we don't know in toto until we finish
3268 prt_dsc_ints("Pages", 1, &prt_page_num);
3270 prt_dsc_noarg("EOF");
3272 /* Write CTRL-D to close serial communication link if used.
3273 * NOTHING MUST BE WRITTEN AFTER THIS! */
3274 prt_write_file((char_u *)IF_EB("\004", "\067"));
3276 if (!prt_file_error && psettings->outfile == NULL
3277 && !got_int && !psettings->user_abort)
3279 /* Close the file first. */
3280 if (prt_ps_fd != NULL)
3282 fclose(prt_ps_fd);
3283 prt_ps_fd = NULL;
3285 prt_message((char_u *)_("Sending to printer..."));
3287 /* Not printing to a file: use 'printexpr' to print the file. */
3288 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
3289 EMSG(_("E365: Failed to print PostScript file"));
3290 else
3291 prt_message((char_u *)_("Print job sent."));
3294 mch_print_cleanup();
3298 mch_print_end_page()
3300 prt_flush_buffer();
3302 prt_write_string("re sp\n");
3304 prt_dsc_noarg("PageTrailer");
3306 return !prt_file_error;
3310 mch_print_begin_page(str)
3311 char_u *str UNUSED;
3313 int page_num[2];
3315 prt_page_num++;
3317 page_num[0] = page_num[1] = prt_page_num;
3318 prt_dsc_ints("Page", 2, page_num);
3320 prt_dsc_noarg("BeginPageSetup");
3322 prt_write_string("sv\n0 g\n");
3323 #ifdef FEAT_MBYTE
3324 prt_in_ascii = !prt_out_mbyte;
3325 if (prt_out_mbyte)
3326 prt_write_string("CF0 sf\n");
3327 else
3328 #endif
3329 prt_write_string("F0 sf\n");
3330 prt_fgcol = PRCOLOR_BLACK;
3331 prt_bgcol = PRCOLOR_WHITE;
3332 prt_font = PRT_PS_FONT_ROMAN;
3334 /* Set up page transformation for landscape printing. */
3335 if (!prt_portrait)
3337 prt_write_int(-((int)prt_mediasize[prt_media].width));
3338 prt_write_string("sl\n");
3341 prt_dsc_noarg("EndPageSetup");
3343 /* We have reset the font attributes, force setting them again. */
3344 curr_bg = (long_u)0xffffffff;
3345 curr_fg = (long_u)0xffffffff;
3346 curr_bold = MAYBE;
3348 return !prt_file_error;
3352 mch_print_blank_page()
3354 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3357 static float prt_pos_x = 0;
3358 static float prt_pos_y = 0;
3360 void
3361 mch_print_start_line(margin, page_line)
3362 int margin;
3363 int page_line;
3365 prt_pos_x = prt_left_margin;
3366 if (margin)
3367 prt_pos_x -= prt_number_width;
3369 prt_pos_y = prt_top_margin - prt_first_line_height -
3370 page_line * prt_line_height;
3372 prt_attribute_change = TRUE;
3373 prt_need_moveto = TRUE;
3374 #ifdef FEAT_MBYTE
3375 prt_half_width = FALSE;
3376 #endif
3380 mch_print_text_out(p, len)
3381 char_u *p;
3382 int len UNUSED;
3384 int need_break;
3385 char_u ch;
3386 char_u ch_buff[8];
3387 float char_width;
3388 float next_pos;
3389 #ifdef FEAT_MBYTE
3390 int in_ascii;
3391 int half_width;
3392 #endif
3394 char_width = prt_char_width;
3396 #ifdef FEAT_MBYTE
3397 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3398 * CJKV font to do what VIM is doing here - use a Roman font for characters
3399 * in the ASCII range, and the original CID font for everything else.
3400 * The problem is that GhostScript still (as of 8.13) does not support
3401 * rearranged fonts even though they have been documented by Adobe for 7
3402 * years! If they ever do, a lot of this code will disappear.
3404 if (prt_use_courier)
3406 in_ascii = (len == 1 && *p < 0x80);
3407 if (prt_in_ascii)
3409 if (!in_ascii)
3411 /* No longer in ASCII range - need to switch font */
3412 prt_in_ascii = FALSE;
3413 prt_need_font = TRUE;
3414 prt_attribute_change = TRUE;
3417 else if (in_ascii)
3419 /* Now in ASCII range - need to switch font */
3420 prt_in_ascii = TRUE;
3421 prt_need_font = TRUE;
3422 prt_attribute_change = TRUE;
3425 if (prt_out_mbyte)
3427 half_width = ((*mb_ptr2cells)(p) == 1);
3428 if (half_width)
3429 char_width /= 2;
3430 if (prt_half_width)
3432 if (!half_width)
3434 prt_half_width = FALSE;
3435 prt_pos_x += prt_char_width/4;
3436 prt_need_moveto = TRUE;
3437 prt_attribute_change = TRUE;
3440 else if (half_width)
3442 prt_half_width = TRUE;
3443 prt_pos_x += prt_char_width/4;
3444 prt_need_moveto = TRUE;
3445 prt_attribute_change = TRUE;
3448 #endif
3450 /* Output any required changes to the graphics state, after flushing any
3451 * text buffered so far.
3453 if (prt_attribute_change)
3455 prt_flush_buffer();
3456 /* Reset count of number of chars that will be printed */
3457 prt_text_run = 0;
3459 if (prt_need_moveto)
3461 prt_pos_x_moveto = prt_pos_x;
3462 prt_pos_y_moveto = prt_pos_y;
3463 prt_do_moveto = TRUE;
3465 prt_need_moveto = FALSE;
3467 if (prt_need_font)
3469 #ifdef FEAT_MBYTE
3470 if (!prt_in_ascii)
3471 prt_write_string("CF");
3472 else
3473 #endif
3474 prt_write_string("F");
3475 prt_write_int(prt_font);
3476 prt_write_string("sf\n");
3477 prt_need_font = FALSE;
3479 if (prt_need_fgcol)
3481 int r, g, b;
3482 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3483 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3484 b = prt_fgcol & 0xff;
3486 prt_write_real(r / 255.0, 3);
3487 if (r == g && g == b)
3488 prt_write_string("g\n");
3489 else
3491 prt_write_real(g / 255.0, 3);
3492 prt_write_real(b / 255.0, 3);
3493 prt_write_string("r\n");
3495 prt_need_fgcol = FALSE;
3498 if (prt_bgcol != PRCOLOR_WHITE)
3500 prt_new_bgcol = prt_bgcol;
3501 if (prt_need_bgcol)
3502 prt_do_bgcol = TRUE;
3504 else
3505 prt_do_bgcol = FALSE;
3506 prt_need_bgcol = FALSE;
3508 if (prt_need_underline)
3509 prt_do_underline = prt_underline;
3510 prt_need_underline = FALSE;
3512 prt_attribute_change = FALSE;
3515 #ifdef FEAT_MBYTE
3516 if (prt_do_conv)
3518 /* Convert from multi-byte to 8-bit encoding */
3519 p = string_convert(&prt_conv, p, &len);
3520 if (p == NULL)
3521 p = (char_u *)"";
3524 if (prt_out_mbyte)
3526 /* Multi-byte character strings are represented more efficiently as hex
3527 * strings when outputting clean 8 bit PS.
3531 ch = prt_hexchar[(unsigned)(*p) >> 4];
3532 ga_append(&prt_ps_buffer, ch);
3533 ch = prt_hexchar[(*p) & 0xf];
3534 ga_append(&prt_ps_buffer, ch);
3535 p++;
3537 while (--len);
3539 else
3540 #endif
3542 /* Add next character to buffer of characters to output.
3543 * Note: One printed character may require several PS characters to
3544 * represent it, but we only count them as one printed character.
3546 ch = *p;
3547 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3549 /* Convert non-printing characters to either their escape or octal
3550 * sequence, ensures PS sent over a serial line does not interfere
3551 * with the comms protocol. Note: For EBCDIC we need to write out
3552 * the escape sequences as ASCII codes!
3553 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3555 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3556 switch (ch)
3558 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3559 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3560 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3561 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3562 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3563 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3564 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3565 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
3567 default:
3568 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
3569 #ifdef EBCDIC
3570 ebcdic2ascii(ch_buff, 3);
3571 #endif
3572 ga_append(&prt_ps_buffer, ch_buff[0]);
3573 ga_append(&prt_ps_buffer, ch_buff[1]);
3574 ga_append(&prt_ps_buffer, ch_buff[2]);
3575 break;
3578 else
3579 ga_append(&prt_ps_buffer, ch);
3582 #ifdef FEAT_MBYTE
3583 /* Need to free any translated characters */
3584 if (prt_do_conv && (*p != NUL))
3585 vim_free(p);
3586 #endif
3588 prt_text_run += char_width;
3589 prt_pos_x += char_width;
3591 /* The downside of fp - use relative error on right margin check */
3592 next_pos = prt_pos_x + prt_char_width;
3593 need_break = (next_pos > prt_right_margin) &&
3594 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
3596 if (need_break)
3597 prt_flush_buffer();
3599 return need_break;
3602 void
3603 mch_print_set_font(iBold, iItalic, iUnderline)
3604 int iBold;
3605 int iItalic;
3606 int iUnderline;
3608 int font = 0;
3610 if (iBold)
3611 font |= 0x01;
3612 if (iItalic)
3613 font |= 0x02;
3615 if (font != prt_font)
3617 prt_font = font;
3618 prt_attribute_change = TRUE;
3619 prt_need_font = TRUE;
3621 if (prt_underline != iUnderline)
3623 prt_underline = iUnderline;
3624 prt_attribute_change = TRUE;
3625 prt_need_underline = TRUE;
3629 void
3630 mch_print_set_bg(bgcol)
3631 long_u bgcol;
3633 prt_bgcol = (int)bgcol;
3634 prt_attribute_change = TRUE;
3635 prt_need_bgcol = TRUE;
3638 void
3639 mch_print_set_fg(fgcol)
3640 long_u fgcol;
3642 if (fgcol != (long_u)prt_fgcol)
3644 prt_fgcol = (int)fgcol;
3645 prt_attribute_change = TRUE;
3646 prt_need_fgcol = TRUE;
3650 # endif /*FEAT_POSTSCRIPT*/
3651 #endif /*FEAT_PRINTER*/