Merge branch 'vim-with-runtime' into feat/quickfix-title
[vim_extended.git] / src / hardcopy.c
blob83ab74f7f01a133e74d3a07aee0109898c4a73de
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;
570 char_u *bname = NULL;
571 int do_return = FALSE;
573 memset(&settings, 0, sizeof(prt_settings_T));
574 settings.has_color = TRUE;
576 # ifdef FEAT_POSTSCRIPT
577 if (*eap->arg == '>')
579 char_u *errormsg = NULL;
581 /* Expand things like "%.ps". */
582 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
584 if (errormsg != NULL)
585 EMSG(errormsg);
586 return;
588 settings.outfile = skipwhite(eap->arg + 1);
590 else if (*eap->arg != NUL)
591 settings.arguments = eap->arg;
592 # endif
595 * Initialise for printing. Ask the user for settings, unless forceit is
596 * set.
597 * The mch_print_init() code should set up margins if applicable. (It may
598 * not be a real printer - for example the engine might generate HTML or
599 * PS.)
601 if (mch_print_init(&settings,
602 curbuf->b_fname == NULL
603 ? (bname = (char_u *)buf_spname(curbuf))
604 : curbuf->b_sfname == NULL
605 ? curbuf->b_fname
606 : curbuf->b_sfname,
607 eap->forceit) == FAIL)
608 do_return = TRUE;
610 vim_free(bname);
611 if (do_return)
612 return;
614 #ifdef FEAT_SYN_HL
615 # ifdef FEAT_GUI
616 if (gui.in_use)
617 settings.modec = 'g';
618 else
619 # endif
620 if (t_colors > 1)
621 settings.modec = 'c';
622 else
623 settings.modec = 't';
625 if (!syntax_present(curbuf))
626 settings.do_syntax = FALSE;
627 else if (printer_opts[OPT_PRINT_SYNTAX].present
628 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
629 settings.do_syntax =
630 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
631 else
632 settings.do_syntax = settings.has_color;
633 #endif
635 /* Set up printing attributes for line numbers */
636 settings.number.fg_color = PRCOLOR_BLACK;
637 settings.number.bg_color = PRCOLOR_WHITE;
638 settings.number.bold = FALSE;
639 settings.number.italic = TRUE;
640 settings.number.underline = FALSE;
641 #ifdef FEAT_SYN_HL
643 * Syntax highlighting of line numbers.
645 if (prt_use_number() && settings.do_syntax)
647 int id;
649 id = syn_name2id((char_u *)"LineNr");
650 if (id > 0)
651 id = syn_get_final_id(id);
653 prt_get_attr(id, &settings.number, settings.modec);
655 #endif
658 * Estimate the total lines to be printed
660 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
661 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
662 if (bytes_to_print == 0)
664 MSG(_("No text to be printed"));
665 goto print_fail_no_begin;
668 /* Set colors and font to normal. */
669 curr_bg = (long_u)0xffffffffL;
670 curr_fg = (long_u)0xffffffffL;
671 curr_italic = MAYBE;
672 curr_bold = MAYBE;
673 curr_underline = MAYBE;
675 prt_set_fg(PRCOLOR_BLACK);
676 prt_set_bg(PRCOLOR_WHITE);
677 prt_set_font(FALSE, FALSE, FALSE);
678 #ifdef FEAT_SYN_HL
679 current_syn_id = -1;
680 #endif
682 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
683 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
685 if (!mch_print_begin(&settings))
686 goto print_fail_no_begin;
689 * Loop over collated copies: 1 2 3, 1 2 3, ...
691 page_count = 0;
692 for (collated_copies = 0;
693 collated_copies < settings.n_collated_copies;
694 collated_copies++)
696 prt_pos_T prtpos; /* current print position */
697 prt_pos_T page_prtpos; /* print position at page start */
698 int side;
700 memset(&page_prtpos, 0, sizeof(prt_pos_T));
701 page_prtpos.file_line = eap->line1;
702 prtpos = page_prtpos;
704 if (jobsplit && collated_copies > 0)
706 /* Splitting jobs: Stop a previous job and start a new one. */
707 mch_print_end(&settings);
708 if (!mch_print_begin(&settings))
709 goto print_fail_no_begin;
713 * Loop over all pages in the print job: 1 2 3 ...
715 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
718 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
719 * For duplex: 12 12 12 34 34 34, ...
721 for (uncollated_copies = 0;
722 uncollated_copies < settings.n_uncollated_copies;
723 uncollated_copies++)
725 /* Set the print position to the start of this page. */
726 prtpos = page_prtpos;
729 * Do front and rear side of a page.
731 for (side = 0; side <= settings.duplex; ++side)
734 * Print one page.
737 /* Check for interrupt character every page. */
738 ui_breakcheck();
739 if (got_int || settings.user_abort)
740 goto print_fail;
742 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
743 page_count + 1 + side,
744 prtpos.bytes_printed > 1000000
745 ? (int)(prtpos.bytes_printed /
746 (bytes_to_print / 100))
747 : (int)((prtpos.bytes_printed * 100)
748 / bytes_to_print));
749 if (!mch_print_begin_page(IObuff))
750 goto print_fail;
752 if (settings.n_collated_copies > 1)
753 sprintf((char *)IObuff + STRLEN(IObuff),
754 _(" Copy %d of %d"),
755 collated_copies + 1,
756 settings.n_collated_copies);
757 prt_message(IObuff);
760 * Output header if required
762 if (prt_header_height() > 0)
763 prt_header(&settings, page_count + 1 + side,
764 prtpos.file_line);
766 for (page_line = 0; page_line < settings.lines_per_page;
767 ++page_line)
769 prtpos.column = hardcopy_line(&settings,
770 page_line, &prtpos);
771 if (prtpos.column == 0)
773 /* finished a file line */
774 prtpos.bytes_printed +=
775 STRLEN(skipwhite(ml_get(prtpos.file_line)));
776 if (++prtpos.file_line > eap->line2)
777 break; /* reached the end */
779 else if (prtpos.ff)
781 /* Line had a formfeed in it - start new page but
782 * stay on the current line */
783 break;
787 if (!mch_print_end_page())
788 goto print_fail;
789 if (prtpos.file_line > eap->line2)
790 break; /* reached the end */
794 * Extra blank page for duplexing with odd number of pages and
795 * more copies to come.
797 if (prtpos.file_line > eap->line2 && settings.duplex
798 && side == 0
799 && uncollated_copies + 1 < settings.n_uncollated_copies)
801 if (!mch_print_blank_page())
802 goto print_fail;
805 if (settings.duplex && prtpos.file_line <= eap->line2)
806 ++page_count;
808 /* Remember the position where the next page starts. */
809 page_prtpos = prtpos;
812 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
813 settings.jobname);
814 prt_message(IObuff);
817 print_fail:
818 if (got_int || settings.user_abort)
820 sprintf((char *)IObuff, "%s", _("Printing aborted"));
821 prt_message(IObuff);
823 mch_print_end(&settings);
825 print_fail_no_begin:
826 mch_print_cleanup();
830 * Print one page line.
831 * Return the next column to print, or zero if the line is finished.
833 static colnr_T
834 hardcopy_line(psettings, page_line, ppos)
835 prt_settings_T *psettings;
836 int page_line;
837 prt_pos_T *ppos;
839 colnr_T col;
840 char_u *line;
841 int need_break = FALSE;
842 int outputlen;
843 int tab_spaces;
844 long_u print_pos;
845 #ifdef FEAT_SYN_HL
846 prt_text_attr_T attr;
847 int id;
848 #endif
850 if (ppos->column == 0 || ppos->ff)
852 print_pos = 0;
853 tab_spaces = 0;
854 if (!ppos->ff && prt_use_number())
855 prt_line_number(psettings, page_line, ppos->file_line);
856 ppos->ff = FALSE;
858 else
860 /* left over from wrap halfway a tab */
861 print_pos = ppos->print_pos;
862 tab_spaces = ppos->lead_spaces;
865 mch_print_start_line(0, page_line);
866 line = ml_get(ppos->file_line);
869 * Loop over the columns until the end of the file line or right margin.
871 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
873 outputlen = 1;
874 #ifdef FEAT_MBYTE
875 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
876 outputlen = 1;
877 #endif
878 #ifdef FEAT_SYN_HL
880 * syntax highlighting stuff.
882 if (psettings->do_syntax)
884 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
885 if (id > 0)
886 id = syn_get_final_id(id);
887 else
888 id = 0;
889 /* Get the line again, a multi-line regexp may invalidate it. */
890 line = ml_get(ppos->file_line);
892 if (id != current_syn_id)
894 current_syn_id = id;
895 prt_get_attr(id, &attr, psettings->modec);
896 prt_set_font(attr.bold, attr.italic, attr.underline);
897 prt_set_fg(attr.fg_color);
898 prt_set_bg(attr.bg_color);
901 #endif
904 * Appropriately expand any tabs to spaces.
906 if (line[col] == TAB || tab_spaces != 0)
908 if (tab_spaces == 0)
909 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
911 while (tab_spaces > 0)
913 need_break = mch_print_text_out((char_u *)" ", 1);
914 print_pos++;
915 tab_spaces--;
916 if (need_break)
917 break;
919 /* Keep the TAB if we didn't finish it. */
920 if (need_break && tab_spaces > 0)
921 break;
923 else if (line[col] == FF
924 && printer_opts[OPT_PRINT_FORMFEED].present
925 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
926 == 'y')
928 ppos->ff = TRUE;
929 need_break = 1;
931 else
933 need_break = mch_print_text_out(line + col, outputlen);
934 #ifdef FEAT_MBYTE
935 if (has_mbyte)
936 print_pos += (*mb_ptr2cells)(line + col);
937 else
938 #endif
939 print_pos++;
943 ppos->lead_spaces = tab_spaces;
944 ppos->print_pos = (int)print_pos;
947 * Start next line of file if we clip lines, or have reached end of the
948 * line, unless we are doing a formfeed.
950 if (!ppos->ff
951 && (line[col] == NUL
952 || (printer_opts[OPT_PRINT_WRAP].present
953 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
954 == 'n')))
955 return 0;
956 return col;
959 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
962 * PS printer stuff.
964 * Sources of information to help maintain the PS printing code:
966 * 1. PostScript Language Reference, 3rd Edition,
967 * Addison-Wesley, 1999, ISBN 0-201-37922-8
968 * 2. PostScript Language Program Design,
969 * Addison-Wesley, 1988, ISBN 0-201-14396-8
970 * 3. PostScript Tutorial and Cookbook,
971 * Addison Wesley, 1985, ISBN 0-201-10179-3
972 * 4. PostScript Language Document Structuring Conventions Specification,
973 * version 3.0,
974 * Adobe Technote 5001, 25th September 1992
975 * 5. PostScript Printer Description File Format Specification, Version 4.3,
976 * Adobe technote 5003, 9th February 1996
977 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
978 * Adobe Technote 5007, 7th October 1998
979 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
980 * Adobe Technote 5014, 8th October 1996
981 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
982 * Adoboe Technote 5094, 8th September, 2001
983 * 9. CJKV Information Processing, 2nd Edition,
984 * O'Reilly, 2002, ISBN 1-56592-224-7
986 * Some of these documents can be found in PDF form on Adobe's web site -
987 * http://www.adobe.com
990 #define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
992 #define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
993 #define PRT_PS_DEFAULT_FONTSIZE (10)
994 #define PRT_PS_DEFAULT_BUFFER_SIZE (80)
996 struct prt_mediasize_S
998 char *name;
999 float width; /* width and height in points for portrait */
1000 float height;
1003 #define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
1005 static struct prt_mediasize_S prt_mediasize[] =
1007 {"A4", 595.0, 842.0},
1008 {"letter", 612.0, 792.0},
1009 {"10x14", 720.0, 1008.0},
1010 {"A3", 842.0, 1191.0},
1011 {"A5", 420.0, 595.0},
1012 {"B4", 729.0, 1032.0},
1013 {"B5", 516.0, 729.0},
1014 {"executive", 522.0, 756.0},
1015 {"folio", 595.0, 935.0},
1016 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1017 {"legal", 612.0, 1008.0},
1018 {"quarto", 610.0, 780.0},
1019 {"statement", 396.0, 612.0},
1020 {"tabloid", 792.0, 1224.0}
1023 /* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1024 struct prt_ps_font_S
1026 int wx;
1027 int uline_offset;
1028 int uline_width;
1029 int bbox_min_y;
1030 int bbox_max_y;
1031 char *(ps_fontname[4]);
1034 #define PRT_PS_FONT_ROMAN (0)
1035 #define PRT_PS_FONT_BOLD (1)
1036 #define PRT_PS_FONT_OBLIQUE (2)
1037 #define PRT_PS_FONT_BOLDOBLIQUE (3)
1039 /* Standard font metrics for Courier family */
1040 static struct prt_ps_font_S prt_ps_courier_font =
1042 600,
1043 -100, 50,
1044 -250, 805,
1045 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1048 #ifdef FEAT_MBYTE
1049 /* Generic font metrics for multi-byte fonts */
1050 static struct prt_ps_font_S prt_ps_mb_font =
1052 1000,
1053 -100, 50,
1054 -250, 805,
1055 {NULL, NULL, NULL, NULL}
1057 #endif
1059 /* Pointer to current font set being used */
1060 static struct prt_ps_font_S* prt_ps_font;
1062 /* Structures to map user named encoding and mapping to PS equivalents for
1063 * building CID font name */
1064 struct prt_ps_encoding_S
1066 char *encoding;
1067 char *cmap_encoding;
1068 int needs_charset;
1071 struct prt_ps_charset_S
1073 char *charset;
1074 char *cmap_charset;
1075 int has_charset;
1078 #ifdef FEAT_MBYTE
1080 #define CS_JIS_C_1978 (0x01)
1081 #define CS_JIS_X_1983 (0x02)
1082 #define CS_JIS_X_1990 (0x04)
1083 #define CS_NEC (0x08)
1084 #define CS_MSWINDOWS (0x10)
1085 #define CS_CP932 (0x20)
1086 #define CS_KANJITALK6 (0x40)
1087 #define CS_KANJITALK7 (0x80)
1089 /* Japanese encodings and charsets */
1090 static struct prt_ps_encoding_S j_encodings[] =
1092 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
1093 CS_NEC)},
1094 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1095 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1096 CS_KANJITALK6|CS_KANJITALK7)},
1097 {"cp932", "RKSJ", CS_JIS_X_1983},
1098 {"ucs-2", "UCS2", CS_JIS_X_1990},
1099 {"utf-8", "UTF8" , CS_JIS_X_1990}
1101 static struct prt_ps_charset_S j_charsets[] =
1103 {"JIS_C_1978", "78", CS_JIS_C_1978},
1104 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1105 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
1106 {"NEC", "Ext", CS_NEC},
1107 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1108 {"CP932", "90ms", CS_JIS_X_1983},
1109 {"KANJITALK6", "83pv", CS_KANJITALK6},
1110 {"KANJITALK7", "90pv", CS_KANJITALK7}
1113 #define CS_GB_2312_80 (0x01)
1114 #define CS_GBT_12345_90 (0x02)
1115 #define CS_GBK2K (0x04)
1116 #define CS_SC_MAC (0x08)
1117 #define CS_GBT_90_MAC (0x10)
1118 #define CS_GBK (0x20)
1119 #define CS_SC_ISO10646 (0x40)
1121 /* Simplified Chinese encodings and charsets */
1122 static struct prt_ps_encoding_S sc_encodings[] =
1124 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1125 {"gb18030", NULL, CS_GBK2K},
1126 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
1127 CS_GBT_90_MAC)},
1128 {"gbk", "EUC", CS_GBK},
1129 {"ucs-2", "UCS2", CS_SC_ISO10646},
1130 {"utf-8", "UTF8", CS_SC_ISO10646}
1132 static struct prt_ps_charset_S sc_charsets[] =
1134 {"GB_2312-80", "GB", CS_GB_2312_80},
1135 {"GBT_12345-90","GBT", CS_GBT_12345_90},
1136 {"MAC", "GBpc", CS_SC_MAC},
1137 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1138 {"GBK", "GBK", CS_GBK},
1139 {"GB18030", "GBK2K", CS_GBK2K},
1140 {"ISO10646", "UniGB", CS_SC_ISO10646}
1143 #define CS_CNS_PLANE_1 (0x01)
1144 #define CS_CNS_PLANE_2 (0x02)
1145 #define CS_CNS_PLANE_1_2 (0x04)
1146 #define CS_B5 (0x08)
1147 #define CS_ETEN (0x10)
1148 #define CS_HK_GCCS (0x20)
1149 #define CS_HK_SCS (0x40)
1150 #define CS_HK_SCS_ETEN (0x80)
1151 #define CS_MTHKL (0x100)
1152 #define CS_MTHKS (0x200)
1153 #define CS_DLHKL (0x400)
1154 #define CS_DLHKS (0x800)
1155 #define CS_TC_ISO10646 (0x1000)
1157 /* Traditional Chinese encodings and charsets */
1158 static struct prt_ps_encoding_S tc_encodings[] =
1160 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1161 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
1162 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1163 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1164 CS_DLHKS)},
1165 {"cp950", "B5", CS_B5},
1166 {"ucs-2", "UCS2", CS_TC_ISO10646},
1167 {"utf-8", "UTF8", CS_TC_ISO10646},
1168 {"utf-16", "UTF16", CS_TC_ISO10646},
1169 {"utf-32", "UTF32", CS_TC_ISO10646}
1171 static struct prt_ps_charset_S tc_charsets[] =
1173 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1174 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1175 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
1176 {"BIG5", NULL, CS_B5},
1177 {"CP950", NULL, CS_B5},
1178 {"ETEN", "ETen", CS_ETEN},
1179 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1180 {"SCS", "HKscs", CS_HK_SCS},
1181 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1182 {"MTHKL", "HKm471", CS_MTHKL},
1183 {"MTHKS", "HKm314", CS_MTHKS},
1184 {"DLHKL", "HKdla", CS_DLHKL},
1185 {"DLHKS", "HKdlb", CS_DLHKS},
1186 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1189 #define CS_KR_X_1992 (0x01)
1190 #define CS_KR_MAC (0x02)
1191 #define CS_KR_X_1992_MS (0x04)
1192 #define CS_KR_ISO10646 (0x08)
1194 /* Korean encodings and charsets */
1195 static struct prt_ps_encoding_S k_encodings[] =
1197 {"iso-2022-kr", NULL, CS_KR_X_1992},
1198 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1199 {"johab", "Johab", CS_KR_X_1992},
1200 {"cp1361", "Johab", CS_KR_X_1992},
1201 {"uhc", "UHC", CS_KR_X_1992_MS},
1202 {"cp949", "UHC", CS_KR_X_1992_MS},
1203 {"ucs-2", "UCS2", CS_KR_ISO10646},
1204 {"utf-8", "UTF8", CS_KR_ISO10646}
1206 static struct prt_ps_charset_S k_charsets[] =
1208 {"KS_X_1992", "KSC", CS_KR_X_1992},
1209 {"CP1361", "KSC", CS_KR_X_1992},
1210 {"MAC", "KSCpc", CS_KR_MAC},
1211 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1212 {"CP949", "KSCms", CS_KR_X_1992_MS},
1213 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1214 {"ISO10646", "UniKS", CS_KR_ISO10646}
1217 /* Collections of encodings and charsets for multi-byte printing */
1218 struct prt_ps_mbfont_S
1220 int num_encodings;
1221 struct prt_ps_encoding_S *encodings;
1222 int num_charsets;
1223 struct prt_ps_charset_S *charsets;
1224 char *ascii_enc;
1225 char *defcs;
1228 static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1231 NUM_ELEMENTS(j_encodings),
1232 j_encodings,
1233 NUM_ELEMENTS(j_charsets),
1234 j_charsets,
1235 "jis_roman",
1236 "JIS_X_1983"
1239 NUM_ELEMENTS(sc_encodings),
1240 sc_encodings,
1241 NUM_ELEMENTS(sc_charsets),
1242 sc_charsets,
1243 "gb_roman",
1244 "GB_2312-80"
1247 NUM_ELEMENTS(tc_encodings),
1248 tc_encodings,
1249 NUM_ELEMENTS(tc_charsets),
1250 tc_charsets,
1251 "cns_roman",
1252 "BIG5"
1255 NUM_ELEMENTS(k_encodings),
1256 k_encodings,
1257 NUM_ELEMENTS(k_charsets),
1258 k_charsets,
1259 "ks_roman",
1260 "KS_X_1992"
1263 #endif /* FEAT_MBYTE */
1265 struct prt_ps_resource_S
1267 char_u name[64];
1268 char_u filename[MAXPATHL + 1];
1269 int type;
1270 char_u title[256];
1271 char_u version[256];
1274 /* Types of PS resource file currently used */
1275 #define PRT_RESOURCE_TYPE_PROCSET (0)
1276 #define PRT_RESOURCE_TYPE_ENCODING (1)
1277 #define PRT_RESOURCE_TYPE_CMAP (2)
1279 /* The PS prolog file version number has to match - if the prolog file is
1280 * updated, increment the number in the file and here. Version checking was
1281 * added as of VIM 6.2.
1282 * The CID prolog file version number behaves as per PS prolog.
1283 * Table of VIM and prolog versions:
1285 * VIM Prolog CIDProlog
1286 * 6.2 1.3
1287 * 7.0 1.4 1.0
1289 #define PRT_PROLOG_VERSION ((char_u *)"1.4")
1290 #define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1292 /* String versions of PS resource types - indexed by constants above so don't
1293 * re-order!
1295 static char *prt_resource_types[] =
1297 "procset",
1298 "encoding",
1299 "cmap"
1302 /* Strings to look for in a PS resource file */
1303 #define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1304 #define PRT_RESOURCE_RESOURCE "Resource-"
1305 #define PRT_RESOURCE_PROCSET "ProcSet"
1306 #define PRT_RESOURCE_ENCODING "Encoding"
1307 #define PRT_RESOURCE_CMAP "CMap"
1310 /* Data for table based DSC comment recognition, easy to extend if VIM needs to
1311 * read more comments. */
1312 #define PRT_DSC_MISC_TYPE (-1)
1313 #define PRT_DSC_TITLE_TYPE (1)
1314 #define PRT_DSC_VERSION_TYPE (2)
1315 #define PRT_DSC_ENDCOMMENTS_TYPE (3)
1317 #define PRT_DSC_TITLE "%%Title:"
1318 #define PRT_DSC_VERSION "%%Version:"
1319 #define PRT_DSC_ENDCOMMENTS "%%EndComments:"
1321 struct prt_dsc_comment_S
1323 char *string;
1324 int len;
1325 int type;
1328 struct prt_dsc_line_S
1330 int type;
1331 char_u *string;
1332 int len;
1336 #define SIZEOF_CSTR(s) (sizeof(s) - 1)
1337 static struct prt_dsc_comment_S prt_dsc_table[] =
1339 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1340 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1341 PRT_DSC_VERSION_TYPE},
1342 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1343 PRT_DSC_ENDCOMMENTS_TYPE}
1346 static void prt_write_file_raw_len __ARGS((char_u *buffer, int bytes));
1347 static void prt_write_file __ARGS((char_u *buffer));
1348 static void prt_write_file_len __ARGS((char_u *buffer, int bytes));
1349 static void prt_write_string __ARGS((char *s));
1350 static void prt_write_int __ARGS((int i));
1351 static void prt_write_boolean __ARGS((int b));
1352 static void prt_def_font __ARGS((char *new_name, char *encoding, int height, char *font));
1353 static void prt_real_bits __ARGS((double real, int precision, int *pinteger, int *pfraction));
1354 static void prt_write_real __ARGS((double val, int prec));
1355 static void prt_def_var __ARGS((char *name, double value, int prec));
1356 static void prt_flush_buffer __ARGS((void));
1357 static void prt_resource_name __ARGS((char_u *filename, void *cookie));
1358 static int prt_find_resource __ARGS((char *name, struct prt_ps_resource_S *resource));
1359 static int prt_open_resource __ARGS((struct prt_ps_resource_S *resource));
1360 static int prt_check_resource __ARGS((struct prt_ps_resource_S *resource, char_u *version));
1361 static void prt_dsc_start __ARGS((void));
1362 static void prt_dsc_noarg __ARGS((char *comment));
1363 static void prt_dsc_textline __ARGS((char *comment, char *text));
1364 static void prt_dsc_text __ARGS((char *comment, char *text));
1365 static void prt_dsc_ints __ARGS((char *comment, int count, int *ints));
1366 static void prt_dsc_requirements __ARGS((int duplex, int tumble, int collate, int color, int num_copies));
1367 static void prt_dsc_docmedia __ARGS((char *paper_name, double width, double height, double weight, char *colour, char *type));
1368 static void prt_dsc_resources __ARGS((char *comment, char *type, char *strings));
1369 static void prt_dsc_font_resource __ARGS((char *resource, struct prt_ps_font_S *ps_font));
1370 static float to_device_units __ARGS((int idx, double physsize, int def_number));
1371 static void prt_page_margins __ARGS((double width, double height, double *left, double *right, double *top, double *bottom));
1372 static void prt_font_metrics __ARGS((int font_scale));
1373 static int prt_get_cpl __ARGS((void));
1374 static int prt_get_lpp __ARGS((void));
1375 static int prt_add_resource __ARGS((struct prt_ps_resource_S *resource));
1376 static int prt_resfile_next_line __ARGS((void));
1377 static int prt_resfile_strncmp __ARGS((int offset, char *string, int len));
1378 static int prt_resfile_skip_nonws __ARGS((int offset));
1379 static int prt_resfile_skip_ws __ARGS((int offset));
1380 static int prt_next_dsc __ARGS((struct prt_dsc_line_S *p_dsc_line));
1381 #ifdef FEAT_MBYTE
1382 static int prt_build_cid_fontname __ARGS((int font, char_u *name, int name_len));
1383 static void prt_def_cidfont __ARGS((char *new_name, int height, char *cidfont));
1384 static void prt_dup_cidfont __ARGS((char *original_name, char *new_name));
1385 static int prt_match_encoding __ARGS((char *p_encoding, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_encoding_S **pp_mbenc));
1386 static int prt_match_charset __ARGS((char *p_charset, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_charset_S **pp_mbchar));
1387 #endif
1390 * Variables for the output PostScript file.
1392 static FILE *prt_ps_fd;
1393 static int prt_file_error;
1394 static char_u *prt_ps_file_name = NULL;
1397 * Various offsets and dimensions in default PostScript user space (points).
1398 * Used for text positioning calculations
1400 static float prt_page_width;
1401 static float prt_page_height;
1402 static float prt_left_margin;
1403 static float prt_right_margin;
1404 static float prt_top_margin;
1405 static float prt_bottom_margin;
1406 static float prt_line_height;
1407 static float prt_first_line_height;
1408 static float prt_char_width;
1409 static float prt_number_width;
1410 static float prt_bgcol_offset;
1411 static float prt_pos_x_moveto = 0.0;
1412 static float prt_pos_y_moveto = 0.0;
1415 * Various control variables used to decide when and how to change the
1416 * PostScript graphics state.
1418 static int prt_need_moveto;
1419 static int prt_do_moveto;
1420 static int prt_need_font;
1421 static int prt_font;
1422 static int prt_need_underline;
1423 static int prt_underline;
1424 static int prt_do_underline;
1425 static int prt_need_fgcol;
1426 static int prt_fgcol;
1427 static int prt_need_bgcol;
1428 static int prt_do_bgcol;
1429 static int prt_bgcol;
1430 static int prt_new_bgcol;
1431 static int prt_attribute_change;
1432 static float prt_text_run;
1433 static int prt_page_num;
1434 static int prt_bufsiz;
1437 * Variables controlling physical printing.
1439 static int prt_media;
1440 static int prt_portrait;
1441 static int prt_num_copies;
1442 static int prt_duplex;
1443 static int prt_tumble;
1444 static int prt_collate;
1447 * Buffers used when generating PostScript output
1449 static char_u prt_line_buffer[257];
1450 static garray_T prt_ps_buffer;
1452 # ifdef FEAT_MBYTE
1453 static int prt_do_conv;
1454 static vimconv_T prt_conv;
1456 static int prt_out_mbyte;
1457 static int prt_custom_cmap;
1458 static char prt_cmap[80];
1459 static int prt_use_courier;
1460 static int prt_in_ascii;
1461 static int prt_half_width;
1462 static char *prt_ascii_encoding;
1463 static char_u prt_hexchar[] = "0123456789abcdef";
1464 # endif
1466 static void
1467 prt_write_file_raw_len(buffer, bytes)
1468 char_u *buffer;
1469 int bytes;
1471 if (!prt_file_error
1472 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1473 != (size_t)bytes)
1475 EMSG(_("E455: Error writing to PostScript output file"));
1476 prt_file_error = TRUE;
1480 static void
1481 prt_write_file(buffer)
1482 char_u *buffer;
1484 prt_write_file_len(buffer, (int)STRLEN(buffer));
1487 static void
1488 prt_write_file_len(buffer, bytes)
1489 char_u *buffer;
1490 int bytes;
1492 #ifdef EBCDIC
1493 ebcdic2ascii(buffer, bytes);
1494 #endif
1495 prt_write_file_raw_len(buffer, bytes);
1499 * Write a string.
1501 static void
1502 prt_write_string(s)
1503 char *s;
1505 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1506 prt_write_file(prt_line_buffer);
1510 * Write an int and a space.
1512 static void
1513 prt_write_int(i)
1514 int i;
1516 sprintf((char *)prt_line_buffer, "%d ", i);
1517 prt_write_file(prt_line_buffer);
1521 * Write a boolean and a space.
1523 static void
1524 prt_write_boolean(b)
1525 int b;
1527 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1528 prt_write_file(prt_line_buffer);
1532 * Write PostScript to re-encode and define the font.
1534 static void
1535 prt_def_font(new_name, encoding, height, font)
1536 char *new_name;
1537 char *encoding;
1538 int height;
1539 char *font;
1541 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1542 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1543 prt_write_file(prt_line_buffer);
1544 #ifdef FEAT_MBYTE
1545 if (prt_out_mbyte)
1546 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
1547 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1548 else
1549 #endif
1550 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1551 "/%s %d /_%s ffs\n", new_name, height, new_name);
1552 prt_write_file(prt_line_buffer);
1555 #ifdef FEAT_MBYTE
1557 * Write a line to define the CID font.
1559 static void
1560 prt_def_cidfont(new_name, height, cidfont)
1561 char *new_name;
1562 int height;
1563 char *cidfont;
1565 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1566 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1567 prt_write_file(prt_line_buffer);
1568 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1569 "/%s %d /_%s ffs\n", new_name, height, new_name);
1570 prt_write_file(prt_line_buffer);
1574 * Write a line to define a duplicate of a CID font
1576 static void
1577 prt_dup_cidfont(original_name, new_name)
1578 char *original_name;
1579 char *new_name;
1581 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1582 "/%s %s d\n", new_name, original_name);
1583 prt_write_file(prt_line_buffer);
1585 #endif
1588 * Convert a real value into an integer and fractional part as integers, with
1589 * the fractional part being in the range [0,10^precision). The fractional part
1590 * is also rounded based on the precision + 1'th fractional digit.
1592 static void
1593 prt_real_bits(real, precision, pinteger, pfraction)
1594 double real;
1595 int precision;
1596 int *pinteger;
1597 int *pfraction;
1599 int i;
1600 int integer;
1601 float fraction;
1603 integer = (int)real;
1604 fraction = (float)(real - integer);
1605 if (real < (double)integer)
1606 fraction = -fraction;
1607 for (i = 0; i < precision; i++)
1608 fraction *= 10.0;
1610 *pinteger = integer;
1611 *pfraction = (int)(fraction + 0.5);
1615 * Write a real and a space. Save bytes if real value has no fractional part!
1616 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1617 * what decimal point character to use, but PS always requires a '.'.
1619 static void
1620 prt_write_real(val, prec)
1621 double val;
1622 int prec;
1624 int integer;
1625 int fraction;
1627 prt_real_bits(val, prec, &integer, &fraction);
1628 /* Emit integer part */
1629 sprintf((char *)prt_line_buffer, "%d", integer);
1630 prt_write_file(prt_line_buffer);
1631 /* Only emit fraction if necessary */
1632 if (fraction != 0)
1634 /* Remove any trailing zeros */
1635 while ((fraction % 10) == 0)
1637 prec--;
1638 fraction /= 10;
1640 /* Emit fraction left padded with zeros */
1641 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1642 prt_write_file(prt_line_buffer);
1644 sprintf((char *)prt_line_buffer, " ");
1645 prt_write_file(prt_line_buffer);
1649 * Write a line to define a numeric variable.
1651 static void
1652 prt_def_var(name, value, prec)
1653 char *name;
1654 double value;
1655 int prec;
1657 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1658 "/%s ", name);
1659 prt_write_file(prt_line_buffer);
1660 prt_write_real(value, prec);
1661 sprintf((char *)prt_line_buffer, "d\n");
1662 prt_write_file(prt_line_buffer);
1665 /* Convert size from font space to user space at current font scale */
1666 #define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1668 static void
1669 prt_flush_buffer()
1671 if (prt_ps_buffer.ga_len > 0)
1673 /* Any background color must be drawn first */
1674 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1676 int r, g, b;
1678 if (prt_do_moveto)
1680 prt_write_real(prt_pos_x_moveto, 2);
1681 prt_write_real(prt_pos_y_moveto, 2);
1682 prt_write_string("m\n");
1683 prt_do_moveto = FALSE;
1686 /* Size of rect of background color on which text is printed */
1687 prt_write_real(prt_text_run, 2);
1688 prt_write_real(prt_line_height, 2);
1690 /* Lastly add the color of the background */
1691 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1692 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1693 b = prt_new_bgcol & 0xff;
1694 prt_write_real(r / 255.0, 3);
1695 prt_write_real(g / 255.0, 3);
1696 prt_write_real(b / 255.0, 3);
1697 prt_write_string("bg\n");
1699 /* Draw underlines before the text as it makes it slightly easier to
1700 * find the starting point.
1702 if (prt_do_underline)
1704 if (prt_do_moveto)
1706 prt_write_real(prt_pos_x_moveto, 2);
1707 prt_write_real(prt_pos_y_moveto, 2);
1708 prt_write_string("m\n");
1709 prt_do_moveto = FALSE;
1712 /* Underline length of text run */
1713 prt_write_real(prt_text_run, 2);
1714 prt_write_string("ul\n");
1716 /* Draw the text
1717 * Note: we write text out raw - EBCDIC conversion is handled in the
1718 * PostScript world via the font encoding vector. */
1719 #ifdef FEAT_MBYTE
1720 if (prt_out_mbyte)
1721 prt_write_string("<");
1722 else
1723 #endif
1724 prt_write_string("(");
1725 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1726 #ifdef FEAT_MBYTE
1727 if (prt_out_mbyte)
1728 prt_write_string(">");
1729 else
1730 #endif
1731 prt_write_string(")");
1732 /* Add a moveto if need be and use the appropriate show procedure */
1733 if (prt_do_moveto)
1735 prt_write_real(prt_pos_x_moveto, 2);
1736 prt_write_real(prt_pos_y_moveto, 2);
1737 /* moveto and a show */
1738 prt_write_string("ms\n");
1739 prt_do_moveto = FALSE;
1741 else /* Simple show */
1742 prt_write_string("s\n");
1744 ga_clear(&prt_ps_buffer);
1745 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1750 static void
1751 prt_resource_name(filename, cookie)
1752 char_u *filename;
1753 void *cookie;
1755 char_u *resource_filename = cookie;
1757 if (STRLEN(filename) >= MAXPATHL)
1758 *resource_filename = NUL;
1759 else
1760 STRCPY(resource_filename, filename);
1763 static int
1764 prt_find_resource(name, resource)
1765 char *name;
1766 struct prt_ps_resource_S *resource;
1768 char_u buffer[MAXPATHL + 1];
1770 STRCPY(resource->name, name);
1771 /* Look for named resource file in runtimepath */
1772 STRCPY(buffer, "print");
1773 add_pathsep(buffer);
1774 STRCAT(buffer, name);
1775 STRCAT(buffer, ".ps");
1776 resource->filename[0] = NUL;
1777 return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
1778 resource->filename)
1779 && resource->filename[0] != NUL);
1782 /* PS CR and LF characters have platform independent values */
1783 #define PSLF (0x0a)
1784 #define PSCR (0x0d)
1786 /* Static buffer to read initial comments in a resource file, some can have a
1787 * couple of KB of comments! */
1788 #define PRT_FILE_BUFFER_LEN (2048)
1789 struct prt_resfile_buffer_S
1791 char_u buffer[PRT_FILE_BUFFER_LEN];
1792 int len;
1793 int line_start;
1794 int line_end;
1797 static struct prt_resfile_buffer_S prt_resfile;
1799 static int
1800 prt_resfile_next_line()
1802 int idx;
1804 /* Move to start of next line and then find end of line */
1805 idx = prt_resfile.line_end + 1;
1806 while (idx < prt_resfile.len)
1808 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
1809 break;
1810 idx++;
1812 prt_resfile.line_start = idx;
1814 while (idx < prt_resfile.len)
1816 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
1817 break;
1818 idx++;
1820 prt_resfile.line_end = idx;
1822 return (idx < prt_resfile.len);
1825 static int
1826 prt_resfile_strncmp(offset, string, len)
1827 int offset;
1828 char *string;
1829 int len;
1831 /* Force not equal if string is longer than remainder of line */
1832 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
1833 return 1;
1835 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
1836 string, len);
1839 static int
1840 prt_resfile_skip_nonws(offset)
1841 int offset;
1843 int idx;
1845 idx = prt_resfile.line_start + offset;
1846 while (idx < prt_resfile.line_end)
1848 if (isspace(prt_resfile.buffer[idx]))
1849 return idx - prt_resfile.line_start;
1850 idx++;
1852 return -1;
1855 static int
1856 prt_resfile_skip_ws(offset)
1857 int offset;
1859 int idx;
1861 idx = prt_resfile.line_start + offset;
1862 while (idx < prt_resfile.line_end)
1864 if (!isspace(prt_resfile.buffer[idx]))
1865 return idx - prt_resfile.line_start;
1866 idx++;
1868 return -1;
1871 /* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1872 * if a DSC comment is found, else false */
1873 static int
1874 prt_next_dsc(p_dsc_line)
1875 struct prt_dsc_line_S *p_dsc_line;
1877 int comment;
1878 int offset;
1880 /* Move to start of next line */
1881 if (!prt_resfile_next_line())
1882 return FALSE;
1884 /* DSC comments always start %% */
1885 if (prt_resfile_strncmp(0, "%%", 2) != 0)
1886 return FALSE;
1888 /* Find type of DSC comment */
1889 for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
1890 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1891 prt_dsc_table[comment].len) == 0)
1892 break;
1894 if (comment != NUM_ELEMENTS(prt_dsc_table))
1896 /* Return type of comment */
1897 p_dsc_line->type = prt_dsc_table[comment].type;
1898 offset = prt_dsc_table[comment].len;
1900 else
1902 /* Unrecognised DSC comment, skip to ws after comment leader */
1903 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1904 offset = prt_resfile_skip_nonws(0);
1905 if (offset == -1)
1906 return FALSE;
1909 /* Skip ws to comment value */
1910 offset = prt_resfile_skip_ws(offset);
1911 if (offset == -1)
1912 return FALSE;
1914 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1915 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1917 return TRUE;
1920 /* Improved hand crafted parser to get the type, title, and version number of a
1921 * PS resource file so the file details can be added to the DSC header comments.
1923 static int
1924 prt_open_resource(resource)
1925 struct prt_ps_resource_S *resource;
1927 int offset;
1928 int seen_all;
1929 int seen_title;
1930 int seen_version;
1931 FILE *fd_resource;
1932 struct prt_dsc_line_S dsc_line;
1934 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1935 if (fd_resource == NULL)
1937 EMSG2(_("E624: Can't open file \"%s\""), resource->filename);
1938 return FALSE;
1940 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1942 /* Parse first line to ensure valid resource file */
1943 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
1944 PRT_FILE_BUFFER_LEN, fd_resource);
1945 if (ferror(fd_resource))
1947 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1948 resource->filename);
1949 fclose(fd_resource);
1950 return FALSE;
1953 prt_resfile.line_end = -1;
1954 prt_resfile.line_start = 0;
1955 if (!prt_resfile_next_line())
1956 return FALSE;
1958 offset = 0;
1960 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
1961 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
1963 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1964 resource->filename);
1965 fclose(fd_resource);
1966 return FALSE;
1969 /* Skip over any version numbers and following ws */
1970 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
1971 offset = prt_resfile_skip_nonws(offset);
1972 if (offset == -1)
1973 return FALSE;
1974 offset = prt_resfile_skip_ws(offset);
1975 if (offset == -1)
1976 return FALSE;
1978 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
1979 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
1981 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1982 resource->filename);
1983 fclose(fd_resource);
1984 return FALSE;
1986 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
1988 /* Decide type of resource in the file */
1989 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
1990 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
1991 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1992 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
1993 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
1994 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1995 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
1996 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
1997 resource->type = PRT_RESOURCE_TYPE_CMAP;
1998 else
2000 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2001 resource->filename);
2002 fclose(fd_resource);
2003 return FALSE;
2006 /* Look for title and version of resource */
2007 resource->title[0] = '\0';
2008 resource->version[0] = '\0';
2009 seen_title = FALSE;
2010 seen_version = FALSE;
2011 seen_all = FALSE;
2012 while (!seen_all && prt_next_dsc(&dsc_line))
2014 switch (dsc_line.type)
2016 case PRT_DSC_TITLE_TYPE:
2017 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
2018 seen_title = TRUE;
2019 if (seen_version)
2020 seen_all = TRUE;
2021 break;
2023 case PRT_DSC_VERSION_TYPE:
2024 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
2025 seen_version = TRUE;
2026 if (seen_title)
2027 seen_all = TRUE;
2028 break;
2030 case PRT_DSC_ENDCOMMENTS_TYPE:
2031 /* Wont find title or resource after this comment, stop searching */
2032 seen_all = TRUE;
2033 break;
2035 case PRT_DSC_MISC_TYPE:
2036 /* Not interested in whatever comment this line had */
2037 break;
2041 if (!seen_title || !seen_version)
2043 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2044 resource->filename);
2045 fclose(fd_resource);
2046 return FALSE;
2049 fclose(fd_resource);
2051 return TRUE;
2054 static int
2055 prt_check_resource(resource, version)
2056 struct prt_ps_resource_S *resource;
2057 char_u *version;
2059 /* Version number m.n should match, the revision number does not matter */
2060 if (STRNCMP(resource->version, version, STRLEN(version)))
2062 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2063 resource->name);
2064 return FALSE;
2067 /* Other checks to be added as needed */
2068 return TRUE;
2071 static void
2072 prt_dsc_start()
2074 prt_write_string("%!PS-Adobe-3.0\n");
2077 static void
2078 prt_dsc_noarg(comment)
2079 char *comment;
2081 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2082 "%%%%%s\n", comment);
2083 prt_write_file(prt_line_buffer);
2086 static void
2087 prt_dsc_textline(comment, text)
2088 char *comment;
2089 char *text;
2091 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2092 "%%%%%s: %s\n", comment, text);
2093 prt_write_file(prt_line_buffer);
2096 static void
2097 prt_dsc_text(comment, text)
2098 char *comment;
2099 char *text;
2101 /* TODO - should scan 'text' for any chars needing escaping! */
2102 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2103 "%%%%%s: (%s)\n", comment, text);
2104 prt_write_file(prt_line_buffer);
2107 #define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2109 static void
2110 prt_dsc_ints(comment, count, ints)
2111 char *comment;
2112 int count;
2113 int *ints;
2115 int i;
2117 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2118 "%%%%%s:", comment);
2119 prt_write_file(prt_line_buffer);
2121 for (i = 0; i < count; i++)
2123 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2124 prt_write_file(prt_line_buffer);
2127 prt_write_string("\n");
2130 static void
2131 prt_dsc_resources(comment, type, string)
2132 char *comment; /* if NULL add to previous */
2133 char *type;
2134 char *string;
2136 if (comment != NULL)
2137 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2138 "%%%%%s: %s", comment, type);
2139 else
2140 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2141 "%%%%+ %s", type);
2142 prt_write_file(prt_line_buffer);
2144 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2145 " %s\n", string);
2146 prt_write_file(prt_line_buffer);
2149 static void
2150 prt_dsc_font_resource(resource, ps_font)
2151 char *resource;
2152 struct prt_ps_font_S *ps_font;
2154 int i;
2156 prt_dsc_resources(resource, "font",
2157 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
2158 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
2159 if (ps_font->ps_fontname[i] != NULL)
2160 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
2163 static void
2164 prt_dsc_requirements(duplex, tumble, collate, color, num_copies)
2165 int duplex;
2166 int tumble;
2167 int collate;
2168 int color;
2169 int num_copies;
2171 /* Only output the comment if we need to.
2172 * Note: tumble is ignored if we are not duplexing
2174 if (!(duplex || collate || color || (num_copies > 1)))
2175 return;
2177 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2178 prt_write_file(prt_line_buffer);
2180 if (duplex)
2182 prt_write_string(" duplex");
2183 if (tumble)
2184 prt_write_string("(tumble)");
2186 if (collate)
2187 prt_write_string(" collate");
2188 if (color)
2189 prt_write_string(" color");
2190 if (num_copies > 1)
2192 prt_write_string(" numcopies(");
2193 /* Note: no space wanted so dont use prt_write_int() */
2194 sprintf((char *)prt_line_buffer, "%d", num_copies);
2195 prt_write_file(prt_line_buffer);
2196 prt_write_string(")");
2198 prt_write_string("\n");
2201 static void
2202 prt_dsc_docmedia(paper_name, width, height, weight, colour, type)
2203 char *paper_name;
2204 double width;
2205 double height;
2206 double weight;
2207 char *colour;
2208 char *type;
2210 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2211 "%%%%DocumentMedia: %s ", paper_name);
2212 prt_write_file(prt_line_buffer);
2213 prt_write_real(width, 2);
2214 prt_write_real(height, 2);
2215 prt_write_real(weight, 2);
2216 if (colour == NULL)
2217 prt_write_string("()");
2218 else
2219 prt_write_string(colour);
2220 prt_write_string(" ");
2221 if (type == NULL)
2222 prt_write_string("()");
2223 else
2224 prt_write_string(type);
2225 prt_write_string("\n");
2228 void
2229 mch_print_cleanup()
2231 #ifdef FEAT_MBYTE
2232 if (prt_out_mbyte)
2234 int i;
2236 /* Free off all CID font names created, but first clear duplicate
2237 * pointers to the same string (when the same font is used for more than
2238 * one style).
2240 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2242 if (prt_ps_mb_font.ps_fontname[i] != NULL)
2243 vim_free(prt_ps_mb_font.ps_fontname[i]);
2244 prt_ps_mb_font.ps_fontname[i] = NULL;
2248 if (prt_do_conv)
2250 convert_setup(&prt_conv, NULL, NULL);
2251 prt_do_conv = FALSE;
2253 #endif
2254 if (prt_ps_fd != NULL)
2256 fclose(prt_ps_fd);
2257 prt_ps_fd = NULL;
2258 prt_file_error = FALSE;
2260 if (prt_ps_file_name != NULL)
2262 vim_free(prt_ps_file_name);
2263 prt_ps_file_name = NULL;
2267 static float
2268 to_device_units(idx, physsize, def_number)
2269 int idx;
2270 double physsize;
2271 int def_number;
2273 float ret;
2274 int u;
2275 int nr;
2277 u = prt_get_unit(idx);
2278 if (u == PRT_UNIT_NONE)
2280 u = PRT_UNIT_PERC;
2281 nr = def_number;
2283 else
2284 nr = printer_opts[idx].number;
2286 switch (u)
2288 case PRT_UNIT_INCH:
2289 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2290 break;
2291 case PRT_UNIT_MM:
2292 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2293 break;
2294 case PRT_UNIT_POINT:
2295 ret = (float)nr;
2296 break;
2297 case PRT_UNIT_PERC:
2298 default:
2299 ret = (float)(physsize * nr) / 100;
2300 break;
2303 return ret;
2307 * Calculate margins for given width and height from printoptions settings.
2309 static void
2310 prt_page_margins(width, height, left, right, top, bottom)
2311 double width;
2312 double height;
2313 double *left;
2314 double *right;
2315 double *top;
2316 double *bottom;
2318 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2319 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2320 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2321 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2324 static void
2325 prt_font_metrics(font_scale)
2326 int font_scale;
2328 prt_line_height = (float)font_scale;
2329 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2333 static int
2334 prt_get_cpl()
2336 if (prt_use_number())
2338 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2339 #ifdef FEAT_MBYTE
2340 /* If we are outputting multi-byte characters then line numbers will be
2341 * printed with half width characters
2343 if (prt_out_mbyte)
2344 prt_number_width /= 2;
2345 #endif
2346 prt_left_margin += prt_number_width;
2348 else
2349 prt_number_width = 0.0;
2351 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2354 #ifdef FEAT_MBYTE
2355 static int
2356 prt_build_cid_fontname(font, name, name_len)
2357 int font;
2358 char_u *name;
2359 int name_len;
2361 char *fontname;
2363 fontname = (char *)alloc(name_len + 1);
2364 if (fontname == NULL)
2365 return FALSE;
2366 vim_strncpy((char_u *)fontname, name, name_len);
2367 prt_ps_mb_font.ps_fontname[font] = fontname;
2369 return TRUE;
2371 #endif
2374 * Get number of lines of text that fit on a page (excluding the header).
2376 static int
2377 prt_get_lpp()
2379 int lpp;
2382 * Calculate offset to lower left corner of background rect based on actual
2383 * font height (based on its bounding box) and the line height, handling the
2384 * case where the font height can exceed the line height.
2386 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2387 prt_ps_font->bbox_min_y);
2388 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2390 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2391 (1000.0 - (prt_ps_font->bbox_max_y -
2392 prt_ps_font->bbox_min_y)) / 2);
2395 /* Get height for topmost line based on background rect offset. */
2396 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2398 /* Calculate lpp */
2399 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2401 /* Adjust top margin if there is a header */
2402 prt_top_margin -= prt_line_height * prt_header_height();
2404 return lpp - prt_header_height();
2407 #ifdef FEAT_MBYTE
2408 static int
2409 prt_match_encoding(p_encoding, p_cmap, pp_mbenc)
2410 char *p_encoding;
2411 struct prt_ps_mbfont_S *p_cmap;
2412 struct prt_ps_encoding_S **pp_mbenc;
2414 int mbenc;
2415 int enc_len;
2416 struct prt_ps_encoding_S *p_mbenc;
2418 *pp_mbenc = NULL;
2419 /* Look for recognised encoding */
2420 enc_len = (int)STRLEN(p_encoding);
2421 p_mbenc = p_cmap->encodings;
2422 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2424 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2426 *pp_mbenc = p_mbenc;
2427 return TRUE;
2429 p_mbenc++;
2431 return FALSE;
2434 static int
2435 prt_match_charset(p_charset, p_cmap, pp_mbchar)
2436 char *p_charset;
2437 struct prt_ps_mbfont_S *p_cmap;
2438 struct prt_ps_charset_S **pp_mbchar;
2440 int mbchar;
2441 int char_len;
2442 struct prt_ps_charset_S *p_mbchar;
2444 /* Look for recognised character set, using default if one is not given */
2445 if (*p_charset == NUL)
2446 p_charset = p_cmap->defcs;
2447 char_len = (int)STRLEN(p_charset);
2448 p_mbchar = p_cmap->charsets;
2449 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2451 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2453 *pp_mbchar = p_mbchar;
2454 return TRUE;
2456 p_mbchar++;
2458 return FALSE;
2460 #endif
2463 mch_print_init(psettings, jobname, forceit)
2464 prt_settings_T *psettings;
2465 char_u *jobname;
2466 int forceit UNUSED;
2468 int i;
2469 char *paper_name;
2470 int paper_strlen;
2471 int fontsize;
2472 char_u *p;
2473 double left;
2474 double right;
2475 double top;
2476 double bottom;
2477 #ifdef FEAT_MBYTE
2478 int props;
2479 int cmap = 0;
2480 char_u *p_encoding;
2481 struct prt_ps_encoding_S *p_mbenc;
2482 struct prt_ps_encoding_S *p_mbenc_first;
2483 struct prt_ps_charset_S *p_mbchar = NULL;
2484 #endif
2486 #if 0
2488 * TODO:
2489 * If "forceit" is false: pop up a dialog to select:
2490 * - printer name
2491 * - copies
2492 * - collated/uncollated
2493 * - duplex off/long side/short side
2494 * - paper size
2495 * - portrait/landscape
2496 * - font size
2498 * If "forceit" is true: use the default printer and settings
2500 if (forceit)
2501 s_pd.Flags |= PD_RETURNDEFAULT;
2502 #endif
2505 * Set up font and encoding.
2507 #ifdef FEAT_MBYTE
2508 p_encoding = enc_skip(p_penc);
2509 if (*p_encoding == NUL)
2510 p_encoding = enc_skip(p_enc);
2512 /* Look for a multi-byte font that matches the encoding and character set.
2513 * Only look if multi-byte character set is defined, or using multi-byte
2514 * encoding other than Unicode. This is because a Unicode encoding does not
2515 * uniquely identify a CJK character set to use. */
2516 p_mbenc = NULL;
2517 props = enc_canon_props(p_encoding);
2518 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
2520 p_mbenc_first = NULL;
2521 for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
2522 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
2523 &p_mbenc))
2525 if (p_mbenc_first == NULL)
2526 p_mbenc_first = p_mbenc;
2527 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
2528 &p_mbchar))
2529 break;
2532 /* Use first encoding matched if no charset matched */
2533 if (p_mbchar == NULL && p_mbenc_first != NULL)
2534 p_mbenc = p_mbenc_first;
2537 prt_out_mbyte = (p_mbenc != NULL);
2538 if (prt_out_mbyte)
2540 /* Build CMap name - will be same for all multi-byte fonts used */
2541 prt_cmap[0] = NUL;
2543 prt_custom_cmap = (p_mbchar == NULL);
2544 if (!prt_custom_cmap)
2546 /* Check encoding and character set are compatible */
2547 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2549 EMSG(_("E673: Incompatible multi-byte encoding and character set."));
2550 return FALSE;
2553 /* Add charset name if not empty */
2554 if (p_mbchar->cmap_charset != NULL)
2556 vim_strncpy((char_u *)prt_cmap,
2557 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
2558 STRCAT(prt_cmap, "-");
2561 else
2563 /* Add custom CMap character set name */
2564 if (*p_pmcs == NUL)
2566 EMSG(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
2567 return FALSE;
2569 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2570 STRCAT(prt_cmap, "-");
2573 /* CMap name ends with (optional) encoding name and -H for horizontal */
2574 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
2575 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
2577 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2578 STRCAT(prt_cmap, "-");
2580 STRCAT(prt_cmap, "H");
2582 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2584 EMSG(_("E675: No default font specified for multi-byte printing."));
2585 return FALSE;
2588 /* Derive CID font names with fallbacks if not defined */
2589 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2590 mbfont_opts[OPT_MBFONT_REGULAR].string,
2591 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2592 return FALSE;
2593 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2594 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2595 mbfont_opts[OPT_MBFONT_BOLD].string,
2596 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2597 return FALSE;
2598 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2599 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2600 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2601 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2602 return FALSE;
2603 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2604 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
2605 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2606 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
2607 return FALSE;
2609 /* Check if need to use Courier for ASCII code range, and if so pick up
2610 * the encoding to use */
2611 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2612 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2613 if (prt_use_courier)
2615 /* Use national ASCII variant unless ASCII wanted */
2616 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2617 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2618 prt_ascii_encoding = "ascii";
2619 else
2620 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2623 prt_ps_font = &prt_ps_mb_font;
2625 else
2626 #endif
2628 #ifdef FEAT_MBYTE
2629 prt_use_courier = FALSE;
2630 #endif
2631 prt_ps_font = &prt_ps_courier_font;
2635 * Find the size of the paper and set the margins.
2637 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2638 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2639 if (printer_opts[OPT_PRINT_PAPER].present)
2641 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2642 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2644 else
2646 paper_name = "A4";
2647 paper_strlen = 2;
2649 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
2650 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2651 && STRNICMP(prt_mediasize[i].name, paper_name,
2652 paper_strlen) == 0)
2653 break;
2654 if (i == PRT_MEDIASIZE_LEN)
2655 i = 0;
2656 prt_media = i;
2659 * Set PS pagesize based on media dimensions and print orientation.
2660 * Note: Media and page sizes have defined meanings in PostScript and should
2661 * be kept distinct. Media is the paper (or transparency, or ...) that is
2662 * printed on, whereas the page size is the area that the PostScript
2663 * interpreter renders into.
2665 if (prt_portrait)
2667 prt_page_width = prt_mediasize[i].width;
2668 prt_page_height = prt_mediasize[i].height;
2670 else
2672 prt_page_width = prt_mediasize[i].height;
2673 prt_page_height = prt_mediasize[i].width;
2677 * Set PS page margins based on the PS pagesize, not the mediasize - this
2678 * needs to be done before the cpl and lpp are calculated.
2680 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2681 &bottom);
2682 prt_left_margin = (float)left;
2683 prt_right_margin = (float)right;
2684 prt_top_margin = (float)top;
2685 prt_bottom_margin = (float)bottom;
2688 * Set up the font size.
2690 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2691 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2692 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2693 fontsize = atoi((char *)p + 2);
2694 prt_font_metrics(fontsize);
2697 * Return the number of characters per line, and lines per page for the
2698 * generic print code.
2700 psettings->chars_per_line = prt_get_cpl();
2701 psettings->lines_per_page = prt_get_lpp();
2703 /* Catch margin settings that leave no space for output! */
2704 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2705 return FAIL;
2708 * Sort out the number of copies to be printed. PS by default will do
2709 * uncollated copies for you, so once we know how many uncollated copies are
2710 * wanted cache it away and lie to the generic code that we only want one
2711 * uncollated copy.
2713 psettings->n_collated_copies = 1;
2714 psettings->n_uncollated_copies = 1;
2715 prt_num_copies = 1;
2716 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2717 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2718 if (prt_collate)
2720 /* TODO: Get number of collated copies wanted. */
2721 psettings->n_collated_copies = 1;
2723 else
2725 /* TODO: Get number of uncollated copies wanted and update the cached
2726 * count.
2728 prt_num_copies = 1;
2731 psettings->jobname = jobname;
2734 * Set up printer duplex and tumble based on Duplex option setting - default
2735 * is long sided duplex printing (i.e. no tumble).
2737 prt_duplex = TRUE;
2738 prt_tumble = FALSE;
2739 psettings->duplex = 1;
2740 if (printer_opts[OPT_PRINT_DUPLEX].present)
2742 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2744 prt_duplex = FALSE;
2745 psettings->duplex = 0;
2747 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2748 == 0)
2749 prt_tumble = TRUE;
2752 /* For now user abort not supported */
2753 psettings->user_abort = 0;
2755 /* If the user didn't specify a file name, use a temp file. */
2756 if (psettings->outfile == NULL)
2758 prt_ps_file_name = vim_tempname('p');
2759 if (prt_ps_file_name == NULL)
2761 EMSG(_(e_notmp));
2762 return FAIL;
2764 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2766 else
2768 p = expand_env_save(psettings->outfile);
2769 if (p != NULL)
2771 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2772 vim_free(p);
2775 if (prt_ps_fd == NULL)
2777 EMSG(_("E324: Can't open PostScript output file"));
2778 mch_print_cleanup();
2779 return FAIL;
2782 prt_bufsiz = psettings->chars_per_line;
2783 #ifdef FEAT_MBYTE
2784 if (prt_out_mbyte)
2785 prt_bufsiz *= 2;
2786 #endif
2787 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2789 prt_page_num = 0;
2791 prt_attribute_change = FALSE;
2792 prt_need_moveto = FALSE;
2793 prt_need_font = FALSE;
2794 prt_need_fgcol = FALSE;
2795 prt_need_bgcol = FALSE;
2796 prt_need_underline = FALSE;
2798 prt_file_error = FALSE;
2800 return OK;
2803 static int
2804 prt_add_resource(resource)
2805 struct prt_ps_resource_S *resource;
2807 FILE* fd_resource;
2808 char_u resource_buffer[512];
2809 size_t bytes_read;
2811 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2812 if (fd_resource == NULL)
2814 EMSG2(_("E456: Can't open file \"%s\""), resource->filename);
2815 return FALSE;
2817 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2818 (char *)resource->title);
2820 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2822 for (;;)
2824 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2825 sizeof(resource_buffer), fd_resource);
2826 if (ferror(fd_resource))
2828 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
2829 resource->filename);
2830 fclose(fd_resource);
2831 return FALSE;
2833 if (bytes_read == 0)
2834 break;
2835 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
2836 if (prt_file_error)
2838 fclose(fd_resource);
2839 return FALSE;
2842 fclose(fd_resource);
2844 prt_dsc_noarg("EndDocument");
2846 prt_dsc_noarg("EndResource");
2848 return TRUE;
2852 mch_print_begin(psettings)
2853 prt_settings_T *psettings;
2855 time_t now;
2856 int bbox[4];
2857 char *p_time;
2858 double left;
2859 double right;
2860 double top;
2861 double bottom;
2862 struct prt_ps_resource_S res_prolog;
2863 struct prt_ps_resource_S res_encoding;
2864 char buffer[256];
2865 char_u *p_encoding;
2866 char_u *p;
2867 #ifdef FEAT_MBYTE
2868 struct prt_ps_resource_S res_cidfont;
2869 struct prt_ps_resource_S res_cmap;
2870 #endif
2873 * PS DSC Header comments - no PS code!
2875 prt_dsc_start();
2876 prt_dsc_textline("Title", (char *)psettings->jobname);
2877 if (!get_user_name((char_u *)buffer, 256))
2878 STRCPY(buffer, "Unknown");
2879 prt_dsc_textline("For", buffer);
2880 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2881 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2882 now = time(NULL);
2883 p_time = ctime(&now);
2884 /* Note: ctime() adds a \n so we have to remove it :-( */
2885 p = vim_strchr((char_u *)p_time, '\n');
2886 if (p != NULL)
2887 *p = NUL;
2888 prt_dsc_textline("CreationDate", p_time);
2889 prt_dsc_textline("DocumentData", "Clean8Bit");
2890 prt_dsc_textline("Orientation", "Portrait");
2891 prt_dsc_atend("Pages");
2892 prt_dsc_textline("PageOrder", "Ascend");
2893 /* The bbox does not change with orientation - it is always in the default
2894 * user coordinate system! We have to recalculate right and bottom
2895 * coordinates based on the font metrics for the bbox to be accurate. */
2896 prt_page_margins(prt_mediasize[prt_media].width,
2897 prt_mediasize[prt_media].height,
2898 &left, &right, &top, &bottom);
2899 bbox[0] = (int)left;
2900 if (prt_portrait)
2902 /* In portrait printing the fixed point is the top left corner so we
2903 * derive the bbox from that point. We have the expected cpl chars
2904 * across the media and lpp lines down the media.
2906 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2907 * prt_line_height);
2908 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2909 + 0.5);
2910 bbox[3] = (int)(top + 0.5);
2912 else
2914 /* In landscape printing the fixed point is the bottom left corner so we
2915 * derive the bbox from that point. We have lpp chars across the media
2916 * and cpl lines up the media.
2918 bbox[1] = (int)bottom;
2919 bbox[2] = (int)(left + ((psettings->lines_per_page
2920 + prt_header_height()) * prt_line_height) + 0.5);
2921 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2922 + 0.5);
2924 prt_dsc_ints("BoundingBox", 4, bbox);
2925 /* The media width and height does not change with landscape printing! */
2926 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2927 prt_mediasize[prt_media].width,
2928 prt_mediasize[prt_media].height,
2929 (double)0, NULL, NULL);
2930 /* Define fonts needed */
2931 #ifdef FEAT_MBYTE
2932 if (!prt_out_mbyte || prt_use_courier)
2933 #endif
2934 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
2935 #ifdef FEAT_MBYTE
2936 if (prt_out_mbyte)
2938 prt_dsc_font_resource((prt_use_courier ? NULL
2939 : "DocumentNeededResources"), &prt_ps_mb_font);
2940 if (!prt_custom_cmap)
2941 prt_dsc_resources(NULL, "cmap", prt_cmap);
2943 #endif
2945 /* Search for external resources VIM supplies */
2946 if (!prt_find_resource("prolog", &res_prolog))
2948 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
2949 return FALSE;
2951 if (!prt_open_resource(&res_prolog))
2952 return FALSE;
2953 if (!prt_check_resource(&res_prolog, PRT_PROLOG_VERSION))
2954 return FALSE;
2955 #ifdef FEAT_MBYTE
2956 if (prt_out_mbyte)
2958 /* Look for required version of multi-byte printing procset */
2959 if (!prt_find_resource("cidfont", &res_cidfont))
2961 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
2962 return FALSE;
2964 if (!prt_open_resource(&res_cidfont))
2965 return FALSE;
2966 if (!prt_check_resource(&res_cidfont, PRT_CID_PROLOG_VERSION))
2967 return FALSE;
2969 #endif
2971 /* Find an encoding to use for printing.
2972 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2973 * that cannot be found then default to "latin1".
2974 * Note: VIM specific encoding header is always skipped.
2976 #ifdef FEAT_MBYTE
2977 if (!prt_out_mbyte)
2979 #endif
2980 p_encoding = enc_skip(p_penc);
2981 if (*p_encoding == NUL
2982 || !prt_find_resource((char *)p_encoding, &res_encoding))
2984 /* 'printencoding' not set or not supported - find alternate */
2985 #ifdef FEAT_MBYTE
2986 int props;
2988 p_encoding = enc_skip(p_enc);
2989 props = enc_canon_props(p_encoding);
2990 if (!(props & ENC_8BIT)
2991 || !prt_find_resource((char *)p_encoding, &res_encoding))
2992 /* 8-bit 'encoding' is not supported */
2993 #endif
2995 /* Use latin1 as default printing encoding */
2996 p_encoding = (char_u *)"latin1";
2997 if (!prt_find_resource((char *)p_encoding, &res_encoding))
2999 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3000 p_encoding);
3001 return FALSE;
3005 if (!prt_open_resource(&res_encoding))
3006 return FALSE;
3007 /* For the moment there are no checks on encoding resource files to
3008 * perform */
3009 #ifdef FEAT_MBYTE
3011 else
3013 p_encoding = enc_skip(p_penc);
3014 if (*p_encoding == NUL)
3015 p_encoding = enc_skip(p_enc);
3016 if (prt_use_courier)
3018 /* Include ASCII range encoding vector */
3019 if (!prt_find_resource(prt_ascii_encoding, &res_encoding))
3021 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3022 prt_ascii_encoding);
3023 return FALSE;
3025 if (!prt_open_resource(&res_encoding))
3026 return FALSE;
3027 /* For the moment there are no checks on encoding resource files to
3028 * perform */
3032 prt_conv.vc_type = CONV_NONE;
3033 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
3034 /* Set up encoding conversion if required */
3035 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
3037 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
3038 p_encoding);
3039 return FALSE;
3041 prt_do_conv = TRUE;
3043 prt_do_conv = prt_conv.vc_type != CONV_NONE;
3045 if (prt_out_mbyte && prt_custom_cmap)
3047 /* Find user supplied CMap */
3048 if (!prt_find_resource(prt_cmap, &res_cmap))
3050 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3051 prt_cmap);
3052 return FALSE;
3054 if (!prt_open_resource(&res_cmap))
3055 return FALSE;
3057 #endif
3059 /* List resources supplied */
3060 STRCPY(buffer, res_prolog.title);
3061 STRCAT(buffer, " ");
3062 STRCAT(buffer, res_prolog.version);
3063 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
3064 #ifdef FEAT_MBYTE
3065 if (prt_out_mbyte)
3067 STRCPY(buffer, res_cidfont.title);
3068 STRCAT(buffer, " ");
3069 STRCAT(buffer, res_cidfont.version);
3070 prt_dsc_resources(NULL, "procset", buffer);
3072 if (prt_custom_cmap)
3074 STRCPY(buffer, res_cmap.title);
3075 STRCAT(buffer, " ");
3076 STRCAT(buffer, res_cmap.version);
3077 prt_dsc_resources(NULL, "cmap", buffer);
3080 if (!prt_out_mbyte || prt_use_courier)
3081 #endif
3083 STRCPY(buffer, res_encoding.title);
3084 STRCAT(buffer, " ");
3085 STRCAT(buffer, res_encoding.version);
3086 prt_dsc_resources(NULL, "encoding", buffer);
3088 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3089 #ifdef FEAT_SYN_HL
3090 psettings->do_syntax
3091 #else
3093 #endif
3094 , prt_num_copies);
3095 prt_dsc_noarg("EndComments");
3098 * PS Document page defaults
3100 prt_dsc_noarg("BeginDefaults");
3102 /* List font resources most likely common to all pages */
3103 #ifdef FEAT_MBYTE
3104 if (!prt_out_mbyte || prt_use_courier)
3105 #endif
3106 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
3107 #ifdef FEAT_MBYTE
3108 if (prt_out_mbyte)
3110 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3111 &prt_ps_mb_font);
3112 if (!prt_custom_cmap)
3113 prt_dsc_resources(NULL, "cmap", prt_cmap);
3115 #endif
3117 /* Paper will be used for all pages */
3118 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3120 prt_dsc_noarg("EndDefaults");
3123 * PS Document prolog inclusion - all required procsets.
3125 prt_dsc_noarg("BeginProlog");
3127 /* Add required procsets - NOTE: order is important! */
3128 if (!prt_add_resource(&res_prolog))
3129 return FALSE;
3130 #ifdef FEAT_MBYTE
3131 if (prt_out_mbyte)
3133 /* Add CID font procset, and any user supplied CMap */
3134 if (!prt_add_resource(&res_cidfont))
3135 return FALSE;
3136 if (prt_custom_cmap && !prt_add_resource(&res_cmap))
3137 return FALSE;
3139 #endif
3141 #ifdef FEAT_MBYTE
3142 if (!prt_out_mbyte || prt_use_courier)
3143 #endif
3144 /* There will be only one Roman font encoding to be included in the PS
3145 * file. */
3146 if (!prt_add_resource(&res_encoding))
3147 return FALSE;
3149 prt_dsc_noarg("EndProlog");
3152 * PS Document setup - must appear after the prolog
3154 prt_dsc_noarg("BeginSetup");
3156 /* Device setup - page size and number of uncollated copies */
3157 prt_write_int((int)prt_mediasize[prt_media].width);
3158 prt_write_int((int)prt_mediasize[prt_media].height);
3159 prt_write_int(0);
3160 prt_write_string("sps\n");
3161 prt_write_int(prt_num_copies);
3162 prt_write_string("nc\n");
3163 prt_write_boolean(prt_duplex);
3164 prt_write_boolean(prt_tumble);
3165 prt_write_string("dt\n");
3166 prt_write_boolean(prt_collate);
3167 prt_write_string("c\n");
3169 /* Font resource inclusion and definition */
3170 #ifdef FEAT_MBYTE
3171 if (!prt_out_mbyte || prt_use_courier)
3173 /* When using Courier for ASCII range when printing multi-byte, need to
3174 * pick up ASCII encoding to use with it. */
3175 if (prt_use_courier)
3176 p_encoding = (char_u *)prt_ascii_encoding;
3177 #endif
3178 prt_dsc_resources("IncludeResource", "font",
3179 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3180 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3181 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3182 prt_dsc_resources("IncludeResource", "font",
3183 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3184 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3185 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3186 prt_dsc_resources("IncludeResource", "font",
3187 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3188 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3189 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3190 prt_dsc_resources("IncludeResource", "font",
3191 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3192 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3193 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3194 #ifdef FEAT_MBYTE
3196 if (prt_out_mbyte)
3198 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3199 * not have an italic form being a western style, so where no font is
3200 * defined for these faces VIM falls back to an existing face.
3201 * Note: if using Courier for the ASCII range then the printout will
3202 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3204 prt_dsc_resources("IncludeResource", "font",
3205 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3206 if (!prt_custom_cmap)
3207 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3208 prt_def_cidfont("CF0", (int)prt_line_height,
3209 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3211 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3213 prt_dsc_resources("IncludeResource", "font",
3214 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3215 if (!prt_custom_cmap)
3216 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3217 prt_def_cidfont("CF1", (int)prt_line_height,
3218 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3220 else
3221 /* Use ROMAN for BOLD */
3222 prt_dup_cidfont("CF0", "CF1");
3224 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3226 prt_dsc_resources("IncludeResource", "font",
3227 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3228 if (!prt_custom_cmap)
3229 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3230 prt_def_cidfont("CF2", (int)prt_line_height,
3231 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3233 else
3234 /* Use ROMAN for OBLIQUE */
3235 prt_dup_cidfont("CF0", "CF2");
3237 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3239 prt_dsc_resources("IncludeResource", "font",
3240 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3241 if (!prt_custom_cmap)
3242 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3243 prt_def_cidfont("CF3", (int)prt_line_height,
3244 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3246 else
3247 /* Use BOLD for BOLDOBLIQUE */
3248 prt_dup_cidfont("CF1", "CF3");
3250 #endif
3252 /* Misc constant vars used for underlining and background rects */
3253 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3254 prt_ps_font->uline_offset), 2);
3255 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3256 prt_ps_font->uline_width), 2);
3257 prt_def_var("BO", prt_bgcol_offset, 2);
3259 prt_dsc_noarg("EndSetup");
3261 /* Fail if any problems writing out to the PS file */
3262 return !prt_file_error;
3265 void
3266 mch_print_end(psettings)
3267 prt_settings_T *psettings;
3269 prt_dsc_noarg("Trailer");
3272 * Output any info we don't know in toto until we finish
3274 prt_dsc_ints("Pages", 1, &prt_page_num);
3276 prt_dsc_noarg("EOF");
3278 /* Write CTRL-D to close serial communication link if used.
3279 * NOTHING MUST BE WRITTEN AFTER THIS! */
3280 prt_write_file((char_u *)IF_EB("\004", "\067"));
3282 if (!prt_file_error && psettings->outfile == NULL
3283 && !got_int && !psettings->user_abort)
3285 /* Close the file first. */
3286 if (prt_ps_fd != NULL)
3288 fclose(prt_ps_fd);
3289 prt_ps_fd = NULL;
3291 prt_message((char_u *)_("Sending to printer..."));
3293 /* Not printing to a file: use 'printexpr' to print the file. */
3294 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
3295 EMSG(_("E365: Failed to print PostScript file"));
3296 else
3297 prt_message((char_u *)_("Print job sent."));
3300 mch_print_cleanup();
3304 mch_print_end_page()
3306 prt_flush_buffer();
3308 prt_write_string("re sp\n");
3310 prt_dsc_noarg("PageTrailer");
3312 return !prt_file_error;
3316 mch_print_begin_page(str)
3317 char_u *str UNUSED;
3319 int page_num[2];
3321 prt_page_num++;
3323 page_num[0] = page_num[1] = prt_page_num;
3324 prt_dsc_ints("Page", 2, page_num);
3326 prt_dsc_noarg("BeginPageSetup");
3328 prt_write_string("sv\n0 g\n");
3329 #ifdef FEAT_MBYTE
3330 prt_in_ascii = !prt_out_mbyte;
3331 if (prt_out_mbyte)
3332 prt_write_string("CF0 sf\n");
3333 else
3334 #endif
3335 prt_write_string("F0 sf\n");
3336 prt_fgcol = PRCOLOR_BLACK;
3337 prt_bgcol = PRCOLOR_WHITE;
3338 prt_font = PRT_PS_FONT_ROMAN;
3340 /* Set up page transformation for landscape printing. */
3341 if (!prt_portrait)
3343 prt_write_int(-((int)prt_mediasize[prt_media].width));
3344 prt_write_string("sl\n");
3347 prt_dsc_noarg("EndPageSetup");
3349 /* We have reset the font attributes, force setting them again. */
3350 curr_bg = (long_u)0xffffffff;
3351 curr_fg = (long_u)0xffffffff;
3352 curr_bold = MAYBE;
3354 return !prt_file_error;
3358 mch_print_blank_page()
3360 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3363 static float prt_pos_x = 0;
3364 static float prt_pos_y = 0;
3366 void
3367 mch_print_start_line(margin, page_line)
3368 int margin;
3369 int page_line;
3371 prt_pos_x = prt_left_margin;
3372 if (margin)
3373 prt_pos_x -= prt_number_width;
3375 prt_pos_y = prt_top_margin - prt_first_line_height -
3376 page_line * prt_line_height;
3378 prt_attribute_change = TRUE;
3379 prt_need_moveto = TRUE;
3380 #ifdef FEAT_MBYTE
3381 prt_half_width = FALSE;
3382 #endif
3386 mch_print_text_out(p, len)
3387 char_u *p;
3388 int len UNUSED;
3390 int need_break;
3391 char_u ch;
3392 char_u ch_buff[8];
3393 float char_width;
3394 float next_pos;
3395 #ifdef FEAT_MBYTE
3396 int in_ascii;
3397 int half_width;
3398 #endif
3400 char_width = prt_char_width;
3402 #ifdef FEAT_MBYTE
3403 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3404 * CJKV font to do what VIM is doing here - use a Roman font for characters
3405 * in the ASCII range, and the original CID font for everything else.
3406 * The problem is that GhostScript still (as of 8.13) does not support
3407 * rearranged fonts even though they have been documented by Adobe for 7
3408 * years! If they ever do, a lot of this code will disappear.
3410 if (prt_use_courier)
3412 in_ascii = (len == 1 && *p < 0x80);
3413 if (prt_in_ascii)
3415 if (!in_ascii)
3417 /* No longer in ASCII range - need to switch font */
3418 prt_in_ascii = FALSE;
3419 prt_need_font = TRUE;
3420 prt_attribute_change = TRUE;
3423 else if (in_ascii)
3425 /* Now in ASCII range - need to switch font */
3426 prt_in_ascii = TRUE;
3427 prt_need_font = TRUE;
3428 prt_attribute_change = TRUE;
3431 if (prt_out_mbyte)
3433 half_width = ((*mb_ptr2cells)(p) == 1);
3434 if (half_width)
3435 char_width /= 2;
3436 if (prt_half_width)
3438 if (!half_width)
3440 prt_half_width = FALSE;
3441 prt_pos_x += prt_char_width/4;
3442 prt_need_moveto = TRUE;
3443 prt_attribute_change = TRUE;
3446 else if (half_width)
3448 prt_half_width = TRUE;
3449 prt_pos_x += prt_char_width/4;
3450 prt_need_moveto = TRUE;
3451 prt_attribute_change = TRUE;
3454 #endif
3456 /* Output any required changes to the graphics state, after flushing any
3457 * text buffered so far.
3459 if (prt_attribute_change)
3461 prt_flush_buffer();
3462 /* Reset count of number of chars that will be printed */
3463 prt_text_run = 0;
3465 if (prt_need_moveto)
3467 prt_pos_x_moveto = prt_pos_x;
3468 prt_pos_y_moveto = prt_pos_y;
3469 prt_do_moveto = TRUE;
3471 prt_need_moveto = FALSE;
3473 if (prt_need_font)
3475 #ifdef FEAT_MBYTE
3476 if (!prt_in_ascii)
3477 prt_write_string("CF");
3478 else
3479 #endif
3480 prt_write_string("F");
3481 prt_write_int(prt_font);
3482 prt_write_string("sf\n");
3483 prt_need_font = FALSE;
3485 if (prt_need_fgcol)
3487 int r, g, b;
3488 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3489 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3490 b = prt_fgcol & 0xff;
3492 prt_write_real(r / 255.0, 3);
3493 if (r == g && g == b)
3494 prt_write_string("g\n");
3495 else
3497 prt_write_real(g / 255.0, 3);
3498 prt_write_real(b / 255.0, 3);
3499 prt_write_string("r\n");
3501 prt_need_fgcol = FALSE;
3504 if (prt_bgcol != PRCOLOR_WHITE)
3506 prt_new_bgcol = prt_bgcol;
3507 if (prt_need_bgcol)
3508 prt_do_bgcol = TRUE;
3510 else
3511 prt_do_bgcol = FALSE;
3512 prt_need_bgcol = FALSE;
3514 if (prt_need_underline)
3515 prt_do_underline = prt_underline;
3516 prt_need_underline = FALSE;
3518 prt_attribute_change = FALSE;
3521 #ifdef FEAT_MBYTE
3522 if (prt_do_conv)
3524 /* Convert from multi-byte to 8-bit encoding */
3525 p = string_convert(&prt_conv, p, &len);
3526 if (p == NULL)
3527 p = (char_u *)"";
3530 if (prt_out_mbyte)
3532 /* Multi-byte character strings are represented more efficiently as hex
3533 * strings when outputting clean 8 bit PS.
3537 ch = prt_hexchar[(unsigned)(*p) >> 4];
3538 ga_append(&prt_ps_buffer, ch);
3539 ch = prt_hexchar[(*p) & 0xf];
3540 ga_append(&prt_ps_buffer, ch);
3541 p++;
3543 while (--len);
3545 else
3546 #endif
3548 /* Add next character to buffer of characters to output.
3549 * Note: One printed character may require several PS characters to
3550 * represent it, but we only count them as one printed character.
3552 ch = *p;
3553 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3555 /* Convert non-printing characters to either their escape or octal
3556 * sequence, ensures PS sent over a serial line does not interfere
3557 * with the comms protocol. Note: For EBCDIC we need to write out
3558 * the escape sequences as ASCII codes!
3559 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3561 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3562 switch (ch)
3564 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3565 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3566 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3567 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3568 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3569 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3570 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3571 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
3573 default:
3574 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
3575 #ifdef EBCDIC
3576 ebcdic2ascii(ch_buff, 3);
3577 #endif
3578 ga_append(&prt_ps_buffer, ch_buff[0]);
3579 ga_append(&prt_ps_buffer, ch_buff[1]);
3580 ga_append(&prt_ps_buffer, ch_buff[2]);
3581 break;
3584 else
3585 ga_append(&prt_ps_buffer, ch);
3588 #ifdef FEAT_MBYTE
3589 /* Need to free any translated characters */
3590 if (prt_do_conv && (*p != NUL))
3591 vim_free(p);
3592 #endif
3594 prt_text_run += char_width;
3595 prt_pos_x += char_width;
3597 /* The downside of fp - use relative error on right margin check */
3598 next_pos = prt_pos_x + prt_char_width;
3599 need_break = (next_pos > prt_right_margin) &&
3600 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
3602 if (need_break)
3603 prt_flush_buffer();
3605 return need_break;
3608 void
3609 mch_print_set_font(iBold, iItalic, iUnderline)
3610 int iBold;
3611 int iItalic;
3612 int iUnderline;
3614 int font = 0;
3616 if (iBold)
3617 font |= 0x01;
3618 if (iItalic)
3619 font |= 0x02;
3621 if (font != prt_font)
3623 prt_font = font;
3624 prt_attribute_change = TRUE;
3625 prt_need_font = TRUE;
3627 if (prt_underline != iUnderline)
3629 prt_underline = iUnderline;
3630 prt_attribute_change = TRUE;
3631 prt_need_underline = TRUE;
3635 void
3636 mch_print_set_bg(bgcol)
3637 long_u bgcol;
3639 prt_bgcol = (int)bgcol;
3640 prt_attribute_change = TRUE;
3641 prt_need_bgcol = TRUE;
3644 void
3645 mch_print_set_fg(fgcol)
3646 long_u fgcol;
3648 if (fgcol != (long_u)prt_fgcol)
3650 prt_fgcol = (int)fgcol;
3651 prt_attribute_change = TRUE;
3652 prt_need_fgcol = TRUE;
3656 # endif /*FEAT_POSTSCRIPT*/
3657 #endif /*FEAT_PRINTER*/