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.
11 * hardcopy.c: printing to paper
17 #if defined(FEAT_PRINTER) || defined(PROTO)
19 * To implement printing on a platform, the following functions must be
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
60 * void mch_print_set_fg(long_u fgcol);
61 * Called to set the foreground color for the following text. Parameter is an
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.
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
;
95 #define PRCOLOR_BLACK (long_u)0
96 #define PRCOLOR_WHITE (long_u)0xFFFFFFL
98 static int curr_italic
;
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
},
126 * These values determine the print position on a page.
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 */
138 static char_u
*parse_list_options
__ARGS((char_u
*option_str
, option_table_T
*table
, int table_size
));
141 static long_u darken_rgb
__ARGS((long_u rgb
));
142 static long_u prt_get_term_color
__ARGS((int colorindex
));
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
));
152 static void prt_get_attr
__ARGS((int hl_id
, prt_text_attr_T
* pattr
, int modec
));
156 * Parse 'printoptions' and set the flags in "printer_opts".
157 * Returns an error message or NULL;
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;
173 return parse_list_options(p_pmfn
, mbfont_opts
, OPT_MBFONT_NUM_OPTIONS
);
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...
187 parse_list_options(option_str
, table
, table_size
)
189 option_table_T
*table
;
196 int idx
= 0; /* init for GCC */
199 for (idx
= 0; idx
< table_size
; ++idx
)
200 table
[idx
].present
= FALSE
;
203 * Repeat for all comma separated parts.
205 stringp
= option_str
;
208 colonp
= vim_strchr(stringp
, ':');
210 return (char_u
*)N_("E550: Missing colon");
211 commap
= vim_strchr(stringp
, ',');
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)
221 if (idx
== table_size
)
222 return (char_u
*)N_("E551: Illegal component");
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
);
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.
256 return ((rgb
>> 17) << 16)
257 + (((rgb
& 0xff00) >> 9) << 8)
258 + ((rgb
& 0xff) >> 1);
262 prt_get_term_color(colorindex
)
265 /* TODO: Should check for xterm with 88 or 256 colors. */
267 return cterm_color_16
[colorindex
% 16];
268 return cterm_color_8
[colorindex
% 8];
272 prt_get_attr(hl_id
, pattr
, modec
)
274 prt_text_attr_T
*pattr
;
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
);
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
);
299 bg_color
= PRCOLOR_WHITE
;
301 color
= (char *)highlight_color(hl_id
, (char_u
*)"fg", modec
);
305 colorindex
= atoi(color
);
307 if (colorindex
>= 0 && colorindex
< t_colors
)
308 fg_color
= prt_get_term_color(colorindex
);
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 */
330 mch_print_set_fg(fg
);
341 mch_print_set_bg(bg
);
346 prt_set_font(bold
, italic
, underline
)
351 if (curr_bold
!= bold
352 || curr_italic
!= italic
353 || curr_underline
!= underline
)
355 curr_underline
= underline
;
356 curr_italic
= italic
;
358 mch_print_set_font(bold
, italic
, underline
);
363 * Print the line number in the left margin.
366 prt_line_number(psettings
, page_line
, lnum
)
367 prt_settings_T
*psettings
;
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);
386 if (psettings
->do_syntax
)
387 /* Set colors for next character. */
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.
405 if (printer_opts
[OPT_PRINT_HEADERHEIGHT
].present
)
406 return printer_opts
[OPT_PRINT_HEADERHEIGHT
].number
;
411 * Return TRUE if using a line number for printing.
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.
428 int u
= PRT_UNIT_NONE
;
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)
443 * Print the page header.
447 prt_header(psettings
, pagenum
, lnum
)
448 prt_settings_T
*psettings
;
452 int width
= psettings
->chars_per_line
;
460 /* Also use the space for the line number. */
461 if (prt_use_number())
462 width
+= PRINT_NUMBER_WIDTH
;
464 tbuf
= alloc(width
+ IOSIZE
);
469 if (*p_header
!= NUL
)
471 linenr_T tmp_lnum
, tmp_topline
, tmp_botline
;
472 int use_sandbox
= FALSE
;
475 * Need to (temporarily) set current line number and first/last line
476 * number on the 'window'. Since we don't know how long the page is,
477 * set the first and current line number to the top line, and guess
478 * that the page length is 64.
480 tmp_lnum
= curwin
->w_cursor
.lnum
;
481 tmp_topline
= curwin
->w_topline
;
482 tmp_botline
= curwin
->w_botline
;
483 curwin
->w_cursor
.lnum
= lnum
;
484 curwin
->w_topline
= lnum
;
485 curwin
->w_botline
= lnum
+ 63;
486 printer_page_num
= pagenum
;
489 use_sandbox
= was_set_insecurely((char_u
*)"printheader", 0);
491 build_stl_str_hl(curwin
, tbuf
, (size_t)(width
+ IOSIZE
),
492 p_header
, use_sandbox
,
493 ' ', width
, NULL
, NULL
);
495 /* Reset line numbers */
496 curwin
->w_cursor
.lnum
= tmp_lnum
;
497 curwin
->w_topline
= tmp_topline
;
498 curwin
->w_botline
= tmp_botline
;
502 sprintf((char *)tbuf
, _("Page %d"), pagenum
);
504 prt_set_fg(PRCOLOR_BLACK
);
505 prt_set_bg(PRCOLOR_WHITE
);
506 prt_set_font(TRUE
, FALSE
, FALSE
);
508 /* Use a negative line number to indicate printing in the top margin. */
509 page_line
= 0 - prt_header_height();
510 mch_print_start_line(TRUE
, page_line
);
511 for (p
= tbuf
; *p
!= NUL
; )
513 if (mch_print_text_out(p
,
515 (l
= (*mb_ptr2len
)(p
))
522 if (page_line
>= 0) /* out of room in header */
524 mch_print_start_line(TRUE
, page_line
);
536 if (psettings
->do_syntax
)
537 /* Set colors for next character. */
542 /* Set colors and font back to normal. */
543 prt_set_fg(PRCOLOR_BLACK
);
544 prt_set_bg(PRCOLOR_WHITE
);
545 prt_set_font(FALSE
, FALSE
, FALSE
);
550 * Display a print status message.
556 screen_fill((int)Rows
- 1, (int)Rows
, 0, (int)Columns
, ' ', ' ', 0);
557 screen_puts(s
, (int)Rows
- 1, 0, hl_attr(HLF_R
));
566 int collated_copies
, uncollated_copies
;
567 prt_settings_T settings
;
568 long_u bytes_to_print
= 0;
572 memset(&settings
, 0, sizeof(prt_settings_T
));
573 settings
.has_color
= TRUE
;
575 # ifdef FEAT_POSTSCRIPT
576 if (*eap
->arg
== '>')
578 char_u
*errormsg
= NULL
;
580 /* Expand things like "%.ps". */
581 if (expand_filename(eap
, eap
->cmdlinep
, &errormsg
) == FAIL
)
583 if (errormsg
!= NULL
)
587 settings
.outfile
= skipwhite(eap
->arg
+ 1);
589 else if (*eap
->arg
!= NUL
)
590 settings
.arguments
= eap
->arg
;
594 * Initialise for printing. Ask the user for settings, unless forceit is
596 * The mch_print_init() code should set up margins if applicable. (It may
597 * not be a real printer - for example the engine might generate HTML or
600 if (mch_print_init(&settings
,
601 curbuf
->b_fname
== NULL
602 ? (char_u
*)buf_spname(curbuf
)
603 : curbuf
->b_sfname
== NULL
606 eap
->forceit
) == FAIL
)
612 settings
.modec
= 'g';
616 settings
.modec
= 'c';
618 settings
.modec
= 't';
620 if (!syntax_present(curbuf
))
621 settings
.do_syntax
= FALSE
;
622 else if (printer_opts
[OPT_PRINT_SYNTAX
].present
623 && TOLOWER_ASC(printer_opts
[OPT_PRINT_SYNTAX
].string
[0]) != 'a')
625 (TOLOWER_ASC(printer_opts
[OPT_PRINT_SYNTAX
].string
[0]) == 'y');
627 settings
.do_syntax
= settings
.has_color
;
630 /* Set up printing attributes for line numbers */
631 settings
.number
.fg_color
= PRCOLOR_BLACK
;
632 settings
.number
.bg_color
= PRCOLOR_WHITE
;
633 settings
.number
.bold
= FALSE
;
634 settings
.number
.italic
= TRUE
;
635 settings
.number
.underline
= FALSE
;
638 * Syntax highlighting of line numbers.
640 if (prt_use_number() && settings
.do_syntax
)
644 id
= syn_name2id((char_u
*)"LineNr");
646 id
= syn_get_final_id(id
);
648 prt_get_attr(id
, &settings
.number
, settings
.modec
);
653 * Estimate the total lines to be printed
655 for (lnum
= eap
->line1
; lnum
<= eap
->line2
; lnum
++)
656 bytes_to_print
+= (long_u
)STRLEN(skipwhite(ml_get(lnum
)));
657 if (bytes_to_print
== 0)
659 MSG(_("No text to be printed"));
660 goto print_fail_no_begin
;
663 /* Set colors and font to normal. */
664 curr_bg
= (long_u
)0xffffffffL
;
665 curr_fg
= (long_u
)0xffffffffL
;
668 curr_underline
= MAYBE
;
670 prt_set_fg(PRCOLOR_BLACK
);
671 prt_set_bg(PRCOLOR_WHITE
);
672 prt_set_font(FALSE
, FALSE
, FALSE
);
677 jobsplit
= (printer_opts
[OPT_PRINT_JOBSPLIT
].present
678 && TOLOWER_ASC(printer_opts
[OPT_PRINT_JOBSPLIT
].string
[0]) == 'y');
680 if (!mch_print_begin(&settings
))
681 goto print_fail_no_begin
;
684 * Loop over collated copies: 1 2 3, 1 2 3, ...
687 for (collated_copies
= 0;
688 collated_copies
< settings
.n_collated_copies
;
691 prt_pos_T prtpos
; /* current print position */
692 prt_pos_T page_prtpos
; /* print position at page start */
695 memset(&page_prtpos
, 0, sizeof(prt_pos_T
));
696 page_prtpos
.file_line
= eap
->line1
;
697 prtpos
= page_prtpos
;
699 if (jobsplit
&& collated_copies
> 0)
701 /* Splitting jobs: Stop a previous job and start a new one. */
702 mch_print_end(&settings
);
703 if (!mch_print_begin(&settings
))
704 goto print_fail_no_begin
;
708 * Loop over all pages in the print job: 1 2 3 ...
710 for (page_count
= 0; prtpos
.file_line
<= eap
->line2
; ++page_count
)
713 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
714 * For duplex: 12 12 12 34 34 34, ...
716 for (uncollated_copies
= 0;
717 uncollated_copies
< settings
.n_uncollated_copies
;
720 /* Set the print position to the start of this page. */
721 prtpos
= page_prtpos
;
724 * Do front and rear side of a page.
726 for (side
= 0; side
<= settings
.duplex
; ++side
)
732 /* Check for interrupt character every page. */
734 if (got_int
|| settings
.user_abort
)
737 sprintf((char *)IObuff
, _("Printing page %d (%d%%)"),
738 page_count
+ 1 + side
,
739 prtpos
.bytes_printed
> 1000000
740 ? (int)(prtpos
.bytes_printed
/
741 (bytes_to_print
/ 100))
742 : (int)((prtpos
.bytes_printed
* 100)
744 if (!mch_print_begin_page(IObuff
))
747 if (settings
.n_collated_copies
> 1)
748 sprintf((char *)IObuff
+ STRLEN(IObuff
),
751 settings
.n_collated_copies
);
755 * Output header if required
757 if (prt_header_height() > 0)
758 prt_header(&settings
, page_count
+ 1 + side
,
761 for (page_line
= 0; page_line
< settings
.lines_per_page
;
764 prtpos
.column
= hardcopy_line(&settings
,
766 if (prtpos
.column
== 0)
768 /* finished a file line */
769 prtpos
.bytes_printed
+=
770 STRLEN(skipwhite(ml_get(prtpos
.file_line
)));
771 if (++prtpos
.file_line
> eap
->line2
)
772 break; /* reached the end */
776 /* Line had a formfeed in it - start new page but
777 * stay on the current line */
782 if (!mch_print_end_page())
784 if (prtpos
.file_line
> eap
->line2
)
785 break; /* reached the end */
789 * Extra blank page for duplexing with odd number of pages and
790 * more copies to come.
792 if (prtpos
.file_line
> eap
->line2
&& settings
.duplex
794 && uncollated_copies
+ 1 < settings
.n_uncollated_copies
)
796 if (!mch_print_blank_page())
800 if (settings
.duplex
&& prtpos
.file_line
<= eap
->line2
)
803 /* Remember the position where the next page starts. */
804 page_prtpos
= prtpos
;
807 vim_snprintf((char *)IObuff
, IOSIZE
, _("Printed: %s"),
813 if (got_int
|| settings
.user_abort
)
815 sprintf((char *)IObuff
, "%s", _("Printing aborted"));
818 mch_print_end(&settings
);
825 * Print one page line.
826 * Return the next column to print, or zero if the line is finished.
829 hardcopy_line(psettings
, page_line
, ppos
)
830 prt_settings_T
*psettings
;
836 int need_break
= FALSE
;
841 prt_text_attr_T attr
;
845 if (ppos
->column
== 0 || ppos
->ff
)
849 if (!ppos
->ff
&& prt_use_number())
850 prt_line_number(psettings
, page_line
, ppos
->file_line
);
855 /* left over from wrap halfway a tab */
856 print_pos
= ppos
->print_pos
;
857 tab_spaces
= ppos
->lead_spaces
;
860 mch_print_start_line(0, page_line
);
861 line
= ml_get(ppos
->file_line
);
864 * Loop over the columns until the end of the file line or right margin.
866 for (col
= ppos
->column
; line
[col
] != NUL
&& !need_break
; col
+= outputlen
)
870 if (has_mbyte
&& (outputlen
= (*mb_ptr2len
)(line
+ col
)) < 1)
875 * syntax highlighting stuff.
877 if (psettings
->do_syntax
)
879 id
= syn_get_id(curwin
, ppos
->file_line
, col
, 1, NULL
, FALSE
);
881 id
= syn_get_final_id(id
);
884 /* Get the line again, a multi-line regexp may invalidate it. */
885 line
= ml_get(ppos
->file_line
);
887 if (id
!= current_syn_id
)
890 prt_get_attr(id
, &attr
, psettings
->modec
);
891 prt_set_font(attr
.bold
, attr
.italic
, attr
.underline
);
892 prt_set_fg(attr
.fg_color
);
893 prt_set_bg(attr
.bg_color
);
899 * Appropriately expand any tabs to spaces.
901 if (line
[col
] == TAB
|| tab_spaces
!= 0)
904 tab_spaces
= (int)(curbuf
->b_p_ts
- (print_pos
% curbuf
->b_p_ts
));
906 while (tab_spaces
> 0)
908 need_break
= mch_print_text_out((char_u
*)" ", 1);
914 /* Keep the TAB if we didn't finish it. */
915 if (need_break
&& tab_spaces
> 0)
918 else if (line
[col
] == FF
919 && printer_opts
[OPT_PRINT_FORMFEED
].present
920 && TOLOWER_ASC(printer_opts
[OPT_PRINT_FORMFEED
].string
[0])
928 need_break
= mch_print_text_out(line
+ col
, outputlen
);
931 print_pos
+= (*mb_ptr2cells
)(line
+ col
);
938 ppos
->lead_spaces
= tab_spaces
;
939 ppos
->print_pos
= (int)print_pos
;
942 * Start next line of file if we clip lines, or have reached end of the
943 * line, unless we are doing a formfeed.
947 || (printer_opts
[OPT_PRINT_WRAP
].present
948 && TOLOWER_ASC(printer_opts
[OPT_PRINT_WRAP
].string
[0])
954 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
959 * Sources of information to help maintain the PS printing code:
961 * 1. PostScript Language Reference, 3rd Edition,
962 * Addison-Wesley, 1999, ISBN 0-201-37922-8
963 * 2. PostScript Language Program Design,
964 * Addison-Wesley, 1988, ISBN 0-201-14396-8
965 * 3. PostScript Tutorial and Cookbook,
966 * Addison Wesley, 1985, ISBN 0-201-10179-3
967 * 4. PostScript Language Document Structuring Conventions Specification,
969 * Adobe Technote 5001, 25th September 1992
970 * 5. PostScript Printer Description File Format Specification, Version 4.3,
971 * Adobe technote 5003, 9th February 1996
972 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
973 * Adobe Technote 5007, 7th October 1998
974 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
975 * Adobe Technote 5014, 8th October 1996
976 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
977 * Adoboe Technote 5094, 8th September, 2001
978 * 9. CJKV Information Processing, 2nd Edition,
979 * O'Reilly, 2002, ISBN 1-56592-224-7
981 * Some of these documents can be found in PDF form on Adobe's web site -
982 * http://www.adobe.com
985 #define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
987 #define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
988 #define PRT_PS_DEFAULT_FONTSIZE (10)
989 #define PRT_PS_DEFAULT_BUFFER_SIZE (80)
991 struct prt_mediasize_S
994 float width
; /* width and height in points for portrait */
998 #define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
1000 static struct prt_mediasize_S prt_mediasize
[] =
1002 {"A4", 595.0, 842.0},
1003 {"letter", 612.0, 792.0},
1004 {"10x14", 720.0, 1008.0},
1005 {"A3", 842.0, 1191.0},
1006 {"A5", 420.0, 595.0},
1007 {"B4", 729.0, 1032.0},
1008 {"B5", 516.0, 729.0},
1009 {"executive", 522.0, 756.0},
1010 {"folio", 595.0, 935.0},
1011 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1012 {"legal", 612.0, 1008.0},
1013 {"quarto", 610.0, 780.0},
1014 {"statement", 396.0, 612.0},
1015 {"tabloid", 792.0, 1224.0}
1018 /* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1019 struct prt_ps_font_S
1026 char *(ps_fontname
[4]);
1029 #define PRT_PS_FONT_ROMAN (0)
1030 #define PRT_PS_FONT_BOLD (1)
1031 #define PRT_PS_FONT_OBLIQUE (2)
1032 #define PRT_PS_FONT_BOLDOBLIQUE (3)
1034 /* Standard font metrics for Courier family */
1035 static struct prt_ps_font_S prt_ps_courier_font
=
1040 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1044 /* Generic font metrics for multi-byte fonts */
1045 static struct prt_ps_font_S prt_ps_mb_font
=
1050 {NULL
, NULL
, NULL
, NULL
}
1054 /* Pointer to current font set being used */
1055 static struct prt_ps_font_S
* prt_ps_font
;
1057 /* Structures to map user named encoding and mapping to PS equivalents for
1058 * building CID font name */
1059 struct prt_ps_encoding_S
1062 char *cmap_encoding
;
1066 struct prt_ps_charset_S
1075 #define CS_JIS_C_1978 (0x01)
1076 #define CS_JIS_X_1983 (0x02)
1077 #define CS_JIS_X_1990 (0x04)
1078 #define CS_NEC (0x08)
1079 #define CS_MSWINDOWS (0x10)
1080 #define CS_CP932 (0x20)
1081 #define CS_KANJITALK6 (0x40)
1082 #define CS_KANJITALK7 (0x80)
1084 /* Japanese encodings and charsets */
1085 static struct prt_ps_encoding_S j_encodings
[] =
1087 {"iso-2022-jp", NULL
, (CS_JIS_C_1978
|CS_JIS_X_1983
|CS_JIS_X_1990
|
1089 {"euc-jp", "EUC", (CS_JIS_C_1978
|CS_JIS_X_1983
|CS_JIS_X_1990
)},
1090 {"sjis", "RKSJ", (CS_JIS_C_1978
|CS_JIS_X_1983
|CS_MSWINDOWS
|
1091 CS_KANJITALK6
|CS_KANJITALK7
)},
1092 {"cp932", "RKSJ", CS_JIS_X_1983
},
1093 {"ucs-2", "UCS2", CS_JIS_X_1990
},
1094 {"utf-8", "UTF8" , CS_JIS_X_1990
}
1096 static struct prt_ps_charset_S j_charsets
[] =
1098 {"JIS_C_1978", "78", CS_JIS_C_1978
},
1099 {"JIS_X_1983", NULL
, CS_JIS_X_1983
},
1100 {"JIS_X_1990", "Hojo", CS_JIS_X_1990
},
1101 {"NEC", "Ext", CS_NEC
},
1102 {"MSWINDOWS", "90ms", CS_MSWINDOWS
},
1103 {"CP932", "90ms", CS_JIS_X_1983
},
1104 {"KANJITALK6", "83pv", CS_KANJITALK6
},
1105 {"KANJITALK7", "90pv", CS_KANJITALK7
}
1108 #define CS_GB_2312_80 (0x01)
1109 #define CS_GBT_12345_90 (0x02)
1110 #define CS_GBK2K (0x04)
1111 #define CS_SC_MAC (0x08)
1112 #define CS_GBT_90_MAC (0x10)
1113 #define CS_GBK (0x20)
1114 #define CS_SC_ISO10646 (0x40)
1116 /* Simplified Chinese encodings and charsets */
1117 static struct prt_ps_encoding_S sc_encodings
[] =
1119 {"iso-2022", NULL
, (CS_GB_2312_80
|CS_GBT_12345_90
)},
1120 {"gb18030", NULL
, CS_GBK2K
},
1121 {"euc-cn", "EUC", (CS_GB_2312_80
|CS_GBT_12345_90
|CS_SC_MAC
|
1123 {"gbk", "EUC", CS_GBK
},
1124 {"ucs-2", "UCS2", CS_SC_ISO10646
},
1125 {"utf-8", "UTF8", CS_SC_ISO10646
}
1127 static struct prt_ps_charset_S sc_charsets
[] =
1129 {"GB_2312-80", "GB", CS_GB_2312_80
},
1130 {"GBT_12345-90","GBT", CS_GBT_12345_90
},
1131 {"MAC", "GBpc", CS_SC_MAC
},
1132 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC
},
1133 {"GBK", "GBK", CS_GBK
},
1134 {"GB18030", "GBK2K", CS_GBK2K
},
1135 {"ISO10646", "UniGB", CS_SC_ISO10646
}
1138 #define CS_CNS_PLANE_1 (0x01)
1139 #define CS_CNS_PLANE_2 (0x02)
1140 #define CS_CNS_PLANE_1_2 (0x04)
1141 #define CS_B5 (0x08)
1142 #define CS_ETEN (0x10)
1143 #define CS_HK_GCCS (0x20)
1144 #define CS_HK_SCS (0x40)
1145 #define CS_HK_SCS_ETEN (0x80)
1146 #define CS_MTHKL (0x100)
1147 #define CS_MTHKS (0x200)
1148 #define CS_DLHKL (0x400)
1149 #define CS_DLHKS (0x800)
1150 #define CS_TC_ISO10646 (0x1000)
1152 /* Traditional Chinese encodings and charsets */
1153 static struct prt_ps_encoding_S tc_encodings
[] =
1155 {"iso-2022", NULL
, (CS_CNS_PLANE_1
|CS_CNS_PLANE_2
)},
1156 {"euc-tw", "EUC", CS_CNS_PLANE_1_2
},
1157 {"big5", "B5", (CS_B5
|CS_ETEN
|CS_HK_GCCS
|CS_HK_SCS
|
1158 CS_HK_SCS_ETEN
|CS_MTHKL
|CS_MTHKS
|CS_DLHKL
|
1160 {"cp950", "B5", CS_B5
},
1161 {"ucs-2", "UCS2", CS_TC_ISO10646
},
1162 {"utf-8", "UTF8", CS_TC_ISO10646
},
1163 {"utf-16", "UTF16", CS_TC_ISO10646
},
1164 {"utf-32", "UTF32", CS_TC_ISO10646
}
1166 static struct prt_ps_charset_S tc_charsets
[] =
1168 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1
},
1169 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2
},
1170 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2
},
1171 {"BIG5", NULL
, CS_B5
},
1172 {"CP950", NULL
, CS_B5
},
1173 {"ETEN", "ETen", CS_ETEN
},
1174 {"HK_GCCS", "HKgccs", CS_HK_GCCS
},
1175 {"SCS", "HKscs", CS_HK_SCS
},
1176 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN
},
1177 {"MTHKL", "HKm471", CS_MTHKL
},
1178 {"MTHKS", "HKm314", CS_MTHKS
},
1179 {"DLHKL", "HKdla", CS_DLHKL
},
1180 {"DLHKS", "HKdlb", CS_DLHKS
},
1181 {"ISO10646", "UniCNS", CS_TC_ISO10646
}
1184 #define CS_KR_X_1992 (0x01)
1185 #define CS_KR_MAC (0x02)
1186 #define CS_KR_X_1992_MS (0x04)
1187 #define CS_KR_ISO10646 (0x08)
1189 /* Korean encodings and charsets */
1190 static struct prt_ps_encoding_S k_encodings
[] =
1192 {"iso-2022-kr", NULL
, CS_KR_X_1992
},
1193 {"euc-kr", "EUC", (CS_KR_X_1992
|CS_KR_MAC
)},
1194 {"johab", "Johab", CS_KR_X_1992
},
1195 {"cp1361", "Johab", CS_KR_X_1992
},
1196 {"uhc", "UHC", CS_KR_X_1992_MS
},
1197 {"cp949", "UHC", CS_KR_X_1992_MS
},
1198 {"ucs-2", "UCS2", CS_KR_ISO10646
},
1199 {"utf-8", "UTF8", CS_KR_ISO10646
}
1201 static struct prt_ps_charset_S k_charsets
[] =
1203 {"KS_X_1992", "KSC", CS_KR_X_1992
},
1204 {"CP1361", "KSC", CS_KR_X_1992
},
1205 {"MAC", "KSCpc", CS_KR_MAC
},
1206 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS
},
1207 {"CP949", "KSCms", CS_KR_X_1992_MS
},
1208 {"WANSUNG", "KSCms", CS_KR_X_1992_MS
},
1209 {"ISO10646", "UniKS", CS_KR_ISO10646
}
1212 /* Collections of encodings and charsets for multi-byte printing */
1213 struct prt_ps_mbfont_S
1216 struct prt_ps_encoding_S
*encodings
;
1218 struct prt_ps_charset_S
*charsets
;
1223 static struct prt_ps_mbfont_S prt_ps_mbfonts
[] =
1226 NUM_ELEMENTS(j_encodings
),
1228 NUM_ELEMENTS(j_charsets
),
1234 NUM_ELEMENTS(sc_encodings
),
1236 NUM_ELEMENTS(sc_charsets
),
1242 NUM_ELEMENTS(tc_encodings
),
1244 NUM_ELEMENTS(tc_charsets
),
1250 NUM_ELEMENTS(k_encodings
),
1252 NUM_ELEMENTS(k_charsets
),
1258 #endif /* FEAT_MBYTE */
1260 struct prt_ps_resource_S
1263 char_u filename
[MAXPATHL
+ 1];
1266 char_u version
[256];
1269 /* Types of PS resource file currently used */
1270 #define PRT_RESOURCE_TYPE_PROCSET (0)
1271 #define PRT_RESOURCE_TYPE_ENCODING (1)
1272 #define PRT_RESOURCE_TYPE_CMAP (2)
1274 /* The PS prolog file version number has to match - if the prolog file is
1275 * updated, increment the number in the file and here. Version checking was
1276 * added as of VIM 6.2.
1277 * The CID prolog file version number behaves as per PS prolog.
1278 * Table of VIM and prolog versions:
1280 * VIM Prolog CIDProlog
1284 #define PRT_PROLOG_VERSION ((char_u *)"1.4")
1285 #define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1287 /* String versions of PS resource types - indexed by constants above so don't
1290 static char *prt_resource_types
[] =
1297 /* Strings to look for in a PS resource file */
1298 #define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1299 #define PRT_RESOURCE_RESOURCE "Resource-"
1300 #define PRT_RESOURCE_PROCSET "ProcSet"
1301 #define PRT_RESOURCE_ENCODING "Encoding"
1302 #define PRT_RESOURCE_CMAP "CMap"
1305 /* Data for table based DSC comment recognition, easy to extend if VIM needs to
1306 * read more comments. */
1307 #define PRT_DSC_MISC_TYPE (-1)
1308 #define PRT_DSC_TITLE_TYPE (1)
1309 #define PRT_DSC_VERSION_TYPE (2)
1310 #define PRT_DSC_ENDCOMMENTS_TYPE (3)
1312 #define PRT_DSC_TITLE "%%Title:"
1313 #define PRT_DSC_VERSION "%%Version:"
1314 #define PRT_DSC_ENDCOMMENTS "%%EndComments:"
1316 struct prt_dsc_comment_S
1323 struct prt_dsc_line_S
1331 #define SIZEOF_CSTR(s) (sizeof(s) - 1)
1332 static struct prt_dsc_comment_S prt_dsc_table
[] =
1334 {PRT_DSC_TITLE
, SIZEOF_CSTR(PRT_DSC_TITLE
), PRT_DSC_TITLE_TYPE
},
1335 {PRT_DSC_VERSION
, SIZEOF_CSTR(PRT_DSC_VERSION
),
1336 PRT_DSC_VERSION_TYPE
},
1337 {PRT_DSC_ENDCOMMENTS
, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS
),
1338 PRT_DSC_ENDCOMMENTS_TYPE
}
1341 static void prt_write_file_raw_len
__ARGS((char_u
*buffer
, int bytes
));
1342 static void prt_write_file
__ARGS((char_u
*buffer
));
1343 static void prt_write_file_len
__ARGS((char_u
*buffer
, int bytes
));
1344 static void prt_write_string
__ARGS((char *s
));
1345 static void prt_write_int
__ARGS((int i
));
1346 static void prt_write_boolean
__ARGS((int b
));
1347 static void prt_def_font
__ARGS((char *new_name
, char *encoding
, int height
, char *font
));
1348 static void prt_real_bits
__ARGS((double real
, int precision
, int *pinteger
, int *pfraction
));
1349 static void prt_write_real
__ARGS((double val
, int prec
));
1350 static void prt_def_var
__ARGS((char *name
, double value
, int prec
));
1351 static void prt_flush_buffer
__ARGS((void));
1352 static void prt_resource_name
__ARGS((char_u
*filename
, void *cookie
));
1353 static int prt_find_resource
__ARGS((char *name
, struct prt_ps_resource_S
*resource
));
1354 static int prt_open_resource
__ARGS((struct prt_ps_resource_S
*resource
));
1355 static int prt_check_resource
__ARGS((struct prt_ps_resource_S
*resource
, char_u
*version
));
1356 static void prt_dsc_start
__ARGS((void));
1357 static void prt_dsc_noarg
__ARGS((char *comment
));
1358 static void prt_dsc_textline
__ARGS((char *comment
, char *text
));
1359 static void prt_dsc_text
__ARGS((char *comment
, char *text
));
1360 static void prt_dsc_ints
__ARGS((char *comment
, int count
, int *ints
));
1361 static void prt_dsc_requirements
__ARGS((int duplex
, int tumble
, int collate
, int color
, int num_copies
));
1362 static void prt_dsc_docmedia
__ARGS((char *paper_name
, double width
, double height
, double weight
, char *colour
, char *type
));
1363 static void prt_dsc_resources
__ARGS((char *comment
, char *type
, char *strings
));
1364 static void prt_dsc_font_resource
__ARGS((char *resource
, struct prt_ps_font_S
*ps_font
));
1365 static float to_device_units
__ARGS((int idx
, double physsize
, int def_number
));
1366 static void prt_page_margins
__ARGS((double width
, double height
, double *left
, double *right
, double *top
, double *bottom
));
1367 static void prt_font_metrics
__ARGS((int font_scale
));
1368 static int prt_get_cpl
__ARGS((void));
1369 static int prt_get_lpp
__ARGS((void));
1370 static int prt_add_resource
__ARGS((struct prt_ps_resource_S
*resource
));
1371 static int prt_resfile_next_line
__ARGS((void));
1372 static int prt_resfile_strncmp
__ARGS((int offset
, char *string
, int len
));
1373 static int prt_resfile_skip_nonws
__ARGS((int offset
));
1374 static int prt_resfile_skip_ws
__ARGS((int offset
));
1375 static int prt_next_dsc
__ARGS((struct prt_dsc_line_S
*p_dsc_line
));
1377 static int prt_build_cid_fontname
__ARGS((int font
, char_u
*name
, int name_len
));
1378 static void prt_def_cidfont
__ARGS((char *new_name
, int height
, char *cidfont
));
1379 static void prt_dup_cidfont
__ARGS((char *original_name
, char *new_name
));
1380 static int prt_match_encoding
__ARGS((char *p_encoding
, struct prt_ps_mbfont_S
*p_cmap
, struct prt_ps_encoding_S
**pp_mbenc
));
1381 static int prt_match_charset
__ARGS((char *p_charset
, struct prt_ps_mbfont_S
*p_cmap
, struct prt_ps_charset_S
**pp_mbchar
));
1385 * Variables for the output PostScript file.
1387 static FILE *prt_ps_fd
;
1388 static int prt_file_error
;
1389 static char_u
*prt_ps_file_name
= NULL
;
1392 * Various offsets and dimensions in default PostScript user space (points).
1393 * Used for text positioning calculations
1395 static float prt_page_width
;
1396 static float prt_page_height
;
1397 static float prt_left_margin
;
1398 static float prt_right_margin
;
1399 static float prt_top_margin
;
1400 static float prt_bottom_margin
;
1401 static float prt_line_height
;
1402 static float prt_first_line_height
;
1403 static float prt_char_width
;
1404 static float prt_number_width
;
1405 static float prt_bgcol_offset
;
1406 static float prt_pos_x_moveto
= 0.0;
1407 static float prt_pos_y_moveto
= 0.0;
1410 * Various control variables used to decide when and how to change the
1411 * PostScript graphics state.
1413 static int prt_need_moveto
;
1414 static int prt_do_moveto
;
1415 static int prt_need_font
;
1416 static int prt_font
;
1417 static int prt_need_underline
;
1418 static int prt_underline
;
1419 static int prt_do_underline
;
1420 static int prt_need_fgcol
;
1421 static int prt_fgcol
;
1422 static int prt_need_bgcol
;
1423 static int prt_do_bgcol
;
1424 static int prt_bgcol
;
1425 static int prt_new_bgcol
;
1426 static int prt_attribute_change
;
1427 static float prt_text_run
;
1428 static int prt_page_num
;
1429 static int prt_bufsiz
;
1432 * Variables controlling physical printing.
1434 static int prt_media
;
1435 static int prt_portrait
;
1436 static int prt_num_copies
;
1437 static int prt_duplex
;
1438 static int prt_tumble
;
1439 static int prt_collate
;
1442 * Buffers used when generating PostScript output
1444 static char_u prt_line_buffer
[257];
1445 static garray_T prt_ps_buffer
;
1448 static int prt_do_conv
;
1449 static vimconv_T prt_conv
;
1451 static int prt_out_mbyte
;
1452 static int prt_custom_cmap
;
1453 static char prt_cmap
[80];
1454 static int prt_use_courier
;
1455 static int prt_in_ascii
;
1456 static int prt_half_width
;
1457 static char *prt_ascii_encoding
;
1458 static char_u prt_hexchar
[] = "0123456789abcdef";
1462 prt_write_file_raw_len(buffer
, bytes
)
1467 && fwrite(buffer
, sizeof(char_u
), bytes
, prt_ps_fd
)
1470 EMSG(_("E455: Error writing to PostScript output file"));
1471 prt_file_error
= TRUE
;
1476 prt_write_file(buffer
)
1479 prt_write_file_len(buffer
, (int)STRLEN(buffer
));
1483 prt_write_file_len(buffer
, bytes
)
1488 ebcdic2ascii(buffer
, bytes
);
1490 prt_write_file_raw_len(buffer
, bytes
);
1500 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
), "%s", s
);
1501 prt_write_file(prt_line_buffer
);
1505 * Write an int and a space.
1511 sprintf((char *)prt_line_buffer
, "%d ", i
);
1512 prt_write_file(prt_line_buffer
);
1516 * Write a boolean and a space.
1519 prt_write_boolean(b
)
1522 sprintf((char *)prt_line_buffer
, "%s ", (b
? "T" : "F"));
1523 prt_write_file(prt_line_buffer
);
1527 * Write PostScript to re-encode and define the font.
1530 prt_def_font(new_name
, encoding
, height
, font
)
1536 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1537 "/_%s /VIM-%s /%s ref\n", new_name
, encoding
, font
);
1538 prt_write_file(prt_line_buffer
);
1541 sprintf((char *)prt_line_buffer
, "/%s %d %f /_%s sffs\n",
1542 new_name
, height
, 500./prt_ps_courier_font
.wx
, new_name
);
1545 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1546 "/%s %d /_%s ffs\n", new_name
, height
, new_name
);
1547 prt_write_file(prt_line_buffer
);
1552 * Write a line to define the CID font.
1555 prt_def_cidfont(new_name
, height
, cidfont
)
1560 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1561 "/_%s /%s[/%s] vim_composefont\n", new_name
, prt_cmap
, cidfont
);
1562 prt_write_file(prt_line_buffer
);
1563 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1564 "/%s %d /_%s ffs\n", new_name
, height
, new_name
);
1565 prt_write_file(prt_line_buffer
);
1569 * Write a line to define a duplicate of a CID font
1572 prt_dup_cidfont(original_name
, new_name
)
1573 char *original_name
;
1576 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1577 "/%s %s d\n", new_name
, original_name
);
1578 prt_write_file(prt_line_buffer
);
1583 * Convert a real value into an integer and fractional part as integers, with
1584 * the fractional part being in the range [0,10^precision). The fractional part
1585 * is also rounded based on the precision + 1'th fractional digit.
1588 prt_real_bits(real
, precision
, pinteger
, pfraction
)
1598 integer
= (int)real
;
1599 fraction
= (float)(real
- integer
);
1600 if (real
< (double)integer
)
1601 fraction
= -fraction
;
1602 for (i
= 0; i
< precision
; i
++)
1605 *pinteger
= integer
;
1606 *pfraction
= (int)(fraction
+ 0.5);
1610 * Write a real and a space. Save bytes if real value has no fractional part!
1611 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1612 * what decimal point character to use, but PS always requires a '.'.
1615 prt_write_real(val
, prec
)
1622 prt_real_bits(val
, prec
, &integer
, &fraction
);
1623 /* Emit integer part */
1624 sprintf((char *)prt_line_buffer
, "%d", integer
);
1625 prt_write_file(prt_line_buffer
);
1626 /* Only emit fraction if necessary */
1629 /* Remove any trailing zeros */
1630 while ((fraction
% 10) == 0)
1635 /* Emit fraction left padded with zeros */
1636 sprintf((char *)prt_line_buffer
, ".%0*d", prec
, fraction
);
1637 prt_write_file(prt_line_buffer
);
1639 sprintf((char *)prt_line_buffer
, " ");
1640 prt_write_file(prt_line_buffer
);
1644 * Write a line to define a numeric variable.
1647 prt_def_var(name
, value
, prec
)
1652 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
1654 prt_write_file(prt_line_buffer
);
1655 prt_write_real(value
, prec
);
1656 sprintf((char *)prt_line_buffer
, "d\n");
1657 prt_write_file(prt_line_buffer
);
1660 /* Convert size from font space to user space at current font scale */
1661 #define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1666 if (prt_ps_buffer
.ga_len
> 0)
1668 /* Any background color must be drawn first */
1669 if (prt_do_bgcol
&& (prt_new_bgcol
!= PRCOLOR_WHITE
))
1675 prt_write_real(prt_pos_x_moveto
, 2);
1676 prt_write_real(prt_pos_y_moveto
, 2);
1677 prt_write_string("m\n");
1678 prt_do_moveto
= FALSE
;
1681 /* Size of rect of background color on which text is printed */
1682 prt_write_real(prt_text_run
, 2);
1683 prt_write_real(prt_line_height
, 2);
1685 /* Lastly add the color of the background */
1686 r
= ((unsigned)prt_new_bgcol
& 0xff0000) >> 16;
1687 g
= ((unsigned)prt_new_bgcol
& 0xff00) >> 8;
1688 b
= prt_new_bgcol
& 0xff;
1689 prt_write_real(r
/ 255.0, 3);
1690 prt_write_real(g
/ 255.0, 3);
1691 prt_write_real(b
/ 255.0, 3);
1692 prt_write_string("bg\n");
1694 /* Draw underlines before the text as it makes it slightly easier to
1695 * find the starting point.
1697 if (prt_do_underline
)
1701 prt_write_real(prt_pos_x_moveto
, 2);
1702 prt_write_real(prt_pos_y_moveto
, 2);
1703 prt_write_string("m\n");
1704 prt_do_moveto
= FALSE
;
1707 /* Underline length of text run */
1708 prt_write_real(prt_text_run
, 2);
1709 prt_write_string("ul\n");
1712 * Note: we write text out raw - EBCDIC conversion is handled in the
1713 * PostScript world via the font encoding vector. */
1716 prt_write_string("<");
1719 prt_write_string("(");
1720 prt_write_file_raw_len(prt_ps_buffer
.ga_data
, prt_ps_buffer
.ga_len
);
1723 prt_write_string(">");
1726 prt_write_string(")");
1727 /* Add a moveto if need be and use the appropriate show procedure */
1730 prt_write_real(prt_pos_x_moveto
, 2);
1731 prt_write_real(prt_pos_y_moveto
, 2);
1732 /* moveto and a show */
1733 prt_write_string("ms\n");
1734 prt_do_moveto
= FALSE
;
1736 else /* Simple show */
1737 prt_write_string("s\n");
1739 ga_clear(&prt_ps_buffer
);
1740 ga_init2(&prt_ps_buffer
, (int)sizeof(char), prt_bufsiz
);
1746 prt_resource_name(filename
, cookie
)
1750 char_u
*resource_filename
= cookie
;
1752 if (STRLEN(filename
) >= MAXPATHL
)
1753 *resource_filename
= NUL
;
1755 STRCPY(resource_filename
, filename
);
1759 prt_find_resource(name
, resource
)
1761 struct prt_ps_resource_S
*resource
;
1763 char_u buffer
[MAXPATHL
+ 1];
1765 STRCPY(resource
->name
, name
);
1766 /* Look for named resource file in runtimepath */
1767 STRCPY(buffer
, "print");
1768 add_pathsep(buffer
);
1769 STRCAT(buffer
, name
);
1770 STRCAT(buffer
, ".ps");
1771 resource
->filename
[0] = NUL
;
1772 return (do_in_runtimepath(buffer
, FALSE
, prt_resource_name
,
1774 && resource
->filename
[0] != NUL
);
1777 /* PS CR and LF characters have platform independent values */
1781 /* Static buffer to read initial comments in a resource file, some can have a
1782 * couple of KB of comments! */
1783 #define PRT_FILE_BUFFER_LEN (2048)
1784 struct prt_resfile_buffer_S
1786 char_u buffer
[PRT_FILE_BUFFER_LEN
];
1792 static struct prt_resfile_buffer_S prt_resfile
;
1795 prt_resfile_next_line()
1799 /* Move to start of next line and then find end of line */
1800 idx
= prt_resfile
.line_end
+ 1;
1801 while (idx
< prt_resfile
.len
)
1803 if (prt_resfile
.buffer
[idx
] != PSLF
&& prt_resfile
.buffer
[idx
] != PSCR
)
1807 prt_resfile
.line_start
= idx
;
1809 while (idx
< prt_resfile
.len
)
1811 if (prt_resfile
.buffer
[idx
] == PSLF
|| prt_resfile
.buffer
[idx
] == PSCR
)
1815 prt_resfile
.line_end
= idx
;
1817 return (idx
< prt_resfile
.len
);
1821 prt_resfile_strncmp(offset
, string
, len
)
1826 /* Force not equal if string is longer than remainder of line */
1827 if (len
> (prt_resfile
.line_end
- (prt_resfile
.line_start
+ offset
)))
1830 return STRNCMP(&prt_resfile
.buffer
[prt_resfile
.line_start
+ offset
],
1835 prt_resfile_skip_nonws(offset
)
1840 idx
= prt_resfile
.line_start
+ offset
;
1841 while (idx
< prt_resfile
.line_end
)
1843 if (isspace(prt_resfile
.buffer
[idx
]))
1844 return idx
- prt_resfile
.line_start
;
1851 prt_resfile_skip_ws(offset
)
1856 idx
= prt_resfile
.line_start
+ offset
;
1857 while (idx
< prt_resfile
.line_end
)
1859 if (!isspace(prt_resfile
.buffer
[idx
]))
1860 return idx
- prt_resfile
.line_start
;
1866 /* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1867 * if a DSC comment is found, else false */
1869 prt_next_dsc(p_dsc_line
)
1870 struct prt_dsc_line_S
*p_dsc_line
;
1875 /* Move to start of next line */
1876 if (!prt_resfile_next_line())
1879 /* DSC comments always start %% */
1880 if (prt_resfile_strncmp(0, "%%", 2) != 0)
1883 /* Find type of DSC comment */
1884 for (comment
= 0; comment
< NUM_ELEMENTS(prt_dsc_table
); comment
++)
1885 if (prt_resfile_strncmp(0, prt_dsc_table
[comment
].string
,
1886 prt_dsc_table
[comment
].len
) == 0)
1889 if (comment
!= NUM_ELEMENTS(prt_dsc_table
))
1891 /* Return type of comment */
1892 p_dsc_line
->type
= prt_dsc_table
[comment
].type
;
1893 offset
= prt_dsc_table
[comment
].len
;
1897 /* Unrecognised DSC comment, skip to ws after comment leader */
1898 p_dsc_line
->type
= PRT_DSC_MISC_TYPE
;
1899 offset
= prt_resfile_skip_nonws(0);
1904 /* Skip ws to comment value */
1905 offset
= prt_resfile_skip_ws(offset
);
1909 p_dsc_line
->string
= &prt_resfile
.buffer
[prt_resfile
.line_start
+ offset
];
1910 p_dsc_line
->len
= prt_resfile
.line_end
- (prt_resfile
.line_start
+ offset
);
1915 /* Improved hand crafted parser to get the type, title, and version number of a
1916 * PS resource file so the file details can be added to the DSC header comments.
1919 prt_open_resource(resource
)
1920 struct prt_ps_resource_S
*resource
;
1927 struct prt_dsc_line_S dsc_line
;
1929 fd_resource
= mch_fopen((char *)resource
->filename
, READBIN
);
1930 if (fd_resource
== NULL
)
1932 EMSG2(_("E624: Can't open file \"%s\""), resource
->filename
);
1935 vim_memset(prt_resfile
.buffer
, NUL
, PRT_FILE_BUFFER_LEN
);
1937 /* Parse first line to ensure valid resource file */
1938 prt_resfile
.len
= (int)fread((char *)prt_resfile
.buffer
, sizeof(char_u
),
1939 PRT_FILE_BUFFER_LEN
, fd_resource
);
1940 if (ferror(fd_resource
))
1942 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1943 resource
->filename
);
1944 fclose(fd_resource
);
1948 prt_resfile
.line_end
= -1;
1949 prt_resfile
.line_start
= 0;
1950 if (!prt_resfile_next_line())
1955 if (prt_resfile_strncmp(offset
, PRT_RESOURCE_HEADER
,
1956 (int)STRLEN(PRT_RESOURCE_HEADER
)) != 0)
1958 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1959 resource
->filename
);
1960 fclose(fd_resource
);
1964 /* Skip over any version numbers and following ws */
1965 offset
+= (int)STRLEN(PRT_RESOURCE_HEADER
);
1966 offset
= prt_resfile_skip_nonws(offset
);
1969 offset
= prt_resfile_skip_ws(offset
);
1973 if (prt_resfile_strncmp(offset
, PRT_RESOURCE_RESOURCE
,
1974 (int)STRLEN(PRT_RESOURCE_RESOURCE
)) != 0)
1976 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1977 resource
->filename
);
1978 fclose(fd_resource
);
1981 offset
+= (int)STRLEN(PRT_RESOURCE_RESOURCE
);
1983 /* Decide type of resource in the file */
1984 if (prt_resfile_strncmp(offset
, PRT_RESOURCE_PROCSET
,
1985 (int)STRLEN(PRT_RESOURCE_PROCSET
)) == 0)
1986 resource
->type
= PRT_RESOURCE_TYPE_PROCSET
;
1987 else if (prt_resfile_strncmp(offset
, PRT_RESOURCE_ENCODING
,
1988 (int)STRLEN(PRT_RESOURCE_ENCODING
)) == 0)
1989 resource
->type
= PRT_RESOURCE_TYPE_ENCODING
;
1990 else if (prt_resfile_strncmp(offset
, PRT_RESOURCE_CMAP
,
1991 (int)STRLEN(PRT_RESOURCE_CMAP
)) == 0)
1992 resource
->type
= PRT_RESOURCE_TYPE_CMAP
;
1995 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1996 resource
->filename
);
1997 fclose(fd_resource
);
2001 /* Look for title and version of resource */
2002 resource
->title
[0] = '\0';
2003 resource
->version
[0] = '\0';
2005 seen_version
= FALSE
;
2007 while (!seen_all
&& prt_next_dsc(&dsc_line
))
2009 switch (dsc_line
.type
)
2011 case PRT_DSC_TITLE_TYPE
:
2012 vim_strncpy(resource
->title
, dsc_line
.string
, dsc_line
.len
);
2018 case PRT_DSC_VERSION_TYPE
:
2019 vim_strncpy(resource
->version
, dsc_line
.string
, dsc_line
.len
);
2020 seen_version
= TRUE
;
2025 case PRT_DSC_ENDCOMMENTS_TYPE
:
2026 /* Wont find title or resource after this comment, stop searching */
2030 case PRT_DSC_MISC_TYPE
:
2031 /* Not interested in whatever comment this line had */
2036 if (!seen_title
|| !seen_version
)
2038 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2039 resource
->filename
);
2040 fclose(fd_resource
);
2044 fclose(fd_resource
);
2050 prt_check_resource(resource
, version
)
2051 struct prt_ps_resource_S
*resource
;
2054 /* Version number m.n should match, the revision number does not matter */
2055 if (STRNCMP(resource
->version
, version
, STRLEN(version
)))
2057 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2062 /* Other checks to be added as needed */
2069 prt_write_string("%!PS-Adobe-3.0\n");
2073 prt_dsc_noarg(comment
)
2076 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2077 "%%%%%s\n", comment
);
2078 prt_write_file(prt_line_buffer
);
2082 prt_dsc_textline(comment
, text
)
2086 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2087 "%%%%%s: %s\n", comment
, text
);
2088 prt_write_file(prt_line_buffer
);
2092 prt_dsc_text(comment
, text
)
2096 /* TODO - should scan 'text' for any chars needing escaping! */
2097 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2098 "%%%%%s: (%s)\n", comment
, text
);
2099 prt_write_file(prt_line_buffer
);
2102 #define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2105 prt_dsc_ints(comment
, count
, ints
)
2112 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2113 "%%%%%s:", comment
);
2114 prt_write_file(prt_line_buffer
);
2116 for (i
= 0; i
< count
; i
++)
2118 sprintf((char *)prt_line_buffer
, " %d", ints
[i
]);
2119 prt_write_file(prt_line_buffer
);
2122 prt_write_string("\n");
2126 prt_dsc_resources(comment
, type
, string
)
2127 char *comment
; /* if NULL add to previous */
2131 if (comment
!= NULL
)
2132 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2133 "%%%%%s: %s", comment
, type
);
2135 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2137 prt_write_file(prt_line_buffer
);
2139 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2141 prt_write_file(prt_line_buffer
);
2145 prt_dsc_font_resource(resource
, ps_font
)
2147 struct prt_ps_font_S
*ps_font
;
2151 prt_dsc_resources(resource
, "font",
2152 ps_font
->ps_fontname
[PRT_PS_FONT_ROMAN
]);
2153 for (i
= PRT_PS_FONT_BOLD
; i
<= PRT_PS_FONT_BOLDOBLIQUE
; i
++)
2154 if (ps_font
->ps_fontname
[i
] != NULL
)
2155 prt_dsc_resources(NULL
, "font", ps_font
->ps_fontname
[i
]);
2159 prt_dsc_requirements(duplex
, tumble
, collate
, color
, num_copies
)
2166 /* Only output the comment if we need to.
2167 * Note: tumble is ignored if we are not duplexing
2169 if (!(duplex
|| collate
|| color
|| (num_copies
> 1)))
2172 sprintf((char *)prt_line_buffer
, "%%%%Requirements:");
2173 prt_write_file(prt_line_buffer
);
2177 prt_write_string(" duplex");
2179 prt_write_string("(tumble)");
2182 prt_write_string(" collate");
2184 prt_write_string(" color");
2187 prt_write_string(" numcopies(");
2188 /* Note: no space wanted so dont use prt_write_int() */
2189 sprintf((char *)prt_line_buffer
, "%d", num_copies
);
2190 prt_write_file(prt_line_buffer
);
2191 prt_write_string(")");
2193 prt_write_string("\n");
2197 prt_dsc_docmedia(paper_name
, width
, height
, weight
, colour
, type
)
2205 vim_snprintf((char *)prt_line_buffer
, sizeof(prt_line_buffer
),
2206 "%%%%DocumentMedia: %s ", paper_name
);
2207 prt_write_file(prt_line_buffer
);
2208 prt_write_real(width
, 2);
2209 prt_write_real(height
, 2);
2210 prt_write_real(weight
, 2);
2212 prt_write_string("()");
2214 prt_write_string(colour
);
2215 prt_write_string(" ");
2217 prt_write_string("()");
2219 prt_write_string(type
);
2220 prt_write_string("\n");
2231 /* Free off all CID font names created, but first clear duplicate
2232 * pointers to the same string (when the same font is used for more than
2235 for (i
= PRT_PS_FONT_ROMAN
; i
<= PRT_PS_FONT_BOLDOBLIQUE
; i
++)
2237 if (prt_ps_mb_font
.ps_fontname
[i
] != NULL
)
2238 vim_free(prt_ps_mb_font
.ps_fontname
[i
]);
2239 prt_ps_mb_font
.ps_fontname
[i
] = NULL
;
2245 convert_setup(&prt_conv
, NULL
, NULL
);
2246 prt_do_conv
= FALSE
;
2249 if (prt_ps_fd
!= NULL
)
2253 prt_file_error
= FALSE
;
2255 if (prt_ps_file_name
!= NULL
)
2257 vim_free(prt_ps_file_name
);
2258 prt_ps_file_name
= NULL
;
2263 to_device_units(idx
, physsize
, def_number
)
2272 u
= prt_get_unit(idx
);
2273 if (u
== PRT_UNIT_NONE
)
2279 nr
= printer_opts
[idx
].number
;
2284 ret
= (float)(nr
* PRT_PS_DEFAULT_DPI
);
2287 ret
= (float)(nr
* PRT_PS_DEFAULT_DPI
) / (float)25.4;
2289 case PRT_UNIT_POINT
:
2294 ret
= (float)(physsize
* nr
) / 100;
2302 * Calculate margins for given width and height from printoptions settings.
2305 prt_page_margins(width
, height
, left
, right
, top
, bottom
)
2313 *left
= to_device_units(OPT_PRINT_LEFT
, width
, 10);
2314 *right
= width
- to_device_units(OPT_PRINT_RIGHT
, width
, 5);
2315 *top
= height
- to_device_units(OPT_PRINT_TOP
, height
, 5);
2316 *bottom
= to_device_units(OPT_PRINT_BOT
, height
, 5);
2320 prt_font_metrics(font_scale
)
2323 prt_line_height
= (float)font_scale
;
2324 prt_char_width
= (float)PRT_PS_FONT_TO_USER(font_scale
, prt_ps_font
->wx
);
2331 if (prt_use_number())
2333 prt_number_width
= PRINT_NUMBER_WIDTH
* prt_char_width
;
2335 /* If we are outputting multi-byte characters then line numbers will be
2336 * printed with half width characters
2339 prt_number_width
/= 2;
2341 prt_left_margin
+= prt_number_width
;
2344 prt_number_width
= 0.0;
2346 return (int)((prt_right_margin
- prt_left_margin
) / prt_char_width
);
2351 prt_build_cid_fontname(font
, name
, name_len
)
2358 fontname
= (char *)alloc(name_len
+ 1);
2359 if (fontname
== NULL
)
2361 vim_strncpy((char_u
*)fontname
, name
, name_len
);
2362 prt_ps_mb_font
.ps_fontname
[font
] = fontname
;
2369 * Get number of lines of text that fit on a page (excluding the header).
2377 * Calculate offset to lower left corner of background rect based on actual
2378 * font height (based on its bounding box) and the line height, handling the
2379 * case where the font height can exceed the line height.
2381 prt_bgcol_offset
= (float)PRT_PS_FONT_TO_USER(prt_line_height
,
2382 prt_ps_font
->bbox_min_y
);
2383 if ((prt_ps_font
->bbox_max_y
- prt_ps_font
->bbox_min_y
) < 1000.0)
2385 prt_bgcol_offset
-= (float)PRT_PS_FONT_TO_USER(prt_line_height
,
2386 (1000.0 - (prt_ps_font
->bbox_max_y
-
2387 prt_ps_font
->bbox_min_y
)) / 2);
2390 /* Get height for topmost line based on background rect offset. */
2391 prt_first_line_height
= prt_line_height
+ prt_bgcol_offset
;
2394 lpp
= (int)((prt_top_margin
- prt_bottom_margin
) / prt_line_height
);
2396 /* Adjust top margin if there is a header */
2397 prt_top_margin
-= prt_line_height
* prt_header_height();
2399 return lpp
- prt_header_height();
2404 prt_match_encoding(p_encoding
, p_cmap
, pp_mbenc
)
2406 struct prt_ps_mbfont_S
*p_cmap
;
2407 struct prt_ps_encoding_S
**pp_mbenc
;
2411 struct prt_ps_encoding_S
*p_mbenc
;
2414 /* Look for recognised encoding */
2415 enc_len
= (int)STRLEN(p_encoding
);
2416 p_mbenc
= p_cmap
->encodings
;
2417 for (mbenc
= 0; mbenc
< p_cmap
->num_encodings
; mbenc
++)
2419 if (STRNICMP(p_mbenc
->encoding
, p_encoding
, enc_len
) == 0)
2421 *pp_mbenc
= p_mbenc
;
2430 prt_match_charset(p_charset
, p_cmap
, pp_mbchar
)
2432 struct prt_ps_mbfont_S
*p_cmap
;
2433 struct prt_ps_charset_S
**pp_mbchar
;
2437 struct prt_ps_charset_S
*p_mbchar
;
2439 /* Look for recognised character set, using default if one is not given */
2440 if (*p_charset
== NUL
)
2441 p_charset
= p_cmap
->defcs
;
2442 char_len
= (int)STRLEN(p_charset
);
2443 p_mbchar
= p_cmap
->charsets
;
2444 for (mbchar
= 0; mbchar
< p_cmap
->num_charsets
; mbchar
++)
2446 if (STRNICMP(p_mbchar
->charset
, p_charset
, char_len
) == 0)
2448 *pp_mbchar
= p_mbchar
;
2459 mch_print_init(psettings
, jobname
, forceit
)
2460 prt_settings_T
*psettings
;
2477 struct prt_ps_encoding_S
*p_mbenc
;
2478 struct prt_ps_encoding_S
*p_mbenc_first
;
2479 struct prt_ps_charset_S
*p_mbchar
= NULL
;
2485 * If "forceit" is false: pop up a dialog to select:
2488 * - collated/uncollated
2489 * - duplex off/long side/short side
2491 * - portrait/landscape
2494 * If "forceit" is true: use the default printer and settings
2497 s_pd
.Flags
|= PD_RETURNDEFAULT
;
2501 * Set up font and encoding.
2504 p_encoding
= enc_skip(p_penc
);
2505 if (*p_encoding
== NUL
)
2506 p_encoding
= enc_skip(p_enc
);
2508 /* Look for a multi-byte font that matches the encoding and character set.
2509 * Only look if multi-byte character set is defined, or using multi-byte
2510 * encoding other than Unicode. This is because a Unicode encoding does not
2511 * uniquely identify a CJK character set to use. */
2513 props
= enc_canon_props(p_encoding
);
2514 if (!(props
& ENC_8BIT
) && ((*p_pmcs
!= NUL
) || !(props
& ENC_UNICODE
)))
2516 p_mbenc_first
= NULL
;
2517 for (cmap
= 0; cmap
< NUM_ELEMENTS(prt_ps_mbfonts
); cmap
++)
2518 if (prt_match_encoding((char *)p_encoding
, &prt_ps_mbfonts
[cmap
],
2521 if (p_mbenc_first
== NULL
)
2522 p_mbenc_first
= p_mbenc
;
2523 if (prt_match_charset((char *)p_pmcs
, &prt_ps_mbfonts
[cmap
],
2528 /* Use first encoding matched if no charset matched */
2529 if (p_mbchar
== NULL
&& p_mbenc_first
!= NULL
)
2530 p_mbenc
= p_mbenc_first
;
2533 prt_out_mbyte
= (p_mbenc
!= NULL
);
2536 /* Build CMap name - will be same for all multi-byte fonts used */
2539 prt_custom_cmap
= (p_mbchar
== NULL
);
2540 if (!prt_custom_cmap
)
2542 /* Check encoding and character set are compatible */
2543 if ((p_mbenc
->needs_charset
& p_mbchar
->has_charset
) == 0)
2545 EMSG(_("E673: Incompatible multi-byte encoding and character set."));
2549 /* Add charset name if not empty */
2550 if (p_mbchar
->cmap_charset
!= NULL
)
2552 vim_strncpy((char_u
*)prt_cmap
,
2553 (char_u
*)p_mbchar
->cmap_charset
, sizeof(prt_cmap
) - 3);
2554 STRCAT(prt_cmap
, "-");
2559 /* Add custom CMap character set name */
2562 EMSG(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
2565 vim_strncpy((char_u
*)prt_cmap
, p_pmcs
, sizeof(prt_cmap
) - 3);
2566 STRCAT(prt_cmap
, "-");
2569 /* CMap name ends with (optional) encoding name and -H for horizontal */
2570 if (p_mbenc
->cmap_encoding
!= NULL
&& STRLEN(prt_cmap
)
2571 + STRLEN(p_mbenc
->cmap_encoding
) + 3 < sizeof(prt_cmap
))
2573 STRCAT(prt_cmap
, p_mbenc
->cmap_encoding
);
2574 STRCAT(prt_cmap
, "-");
2576 STRCAT(prt_cmap
, "H");
2578 if (!mbfont_opts
[OPT_MBFONT_REGULAR
].present
)
2580 EMSG(_("E675: No default font specified for multi-byte printing."));
2584 /* Derive CID font names with fallbacks if not defined */
2585 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN
,
2586 mbfont_opts
[OPT_MBFONT_REGULAR
].string
,
2587 mbfont_opts
[OPT_MBFONT_REGULAR
].strlen
))
2589 if (mbfont_opts
[OPT_MBFONT_BOLD
].present
)
2590 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD
,
2591 mbfont_opts
[OPT_MBFONT_BOLD
].string
,
2592 mbfont_opts
[OPT_MBFONT_BOLD
].strlen
))
2594 if (mbfont_opts
[OPT_MBFONT_OBLIQUE
].present
)
2595 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE
,
2596 mbfont_opts
[OPT_MBFONT_OBLIQUE
].string
,
2597 mbfont_opts
[OPT_MBFONT_OBLIQUE
].strlen
))
2599 if (mbfont_opts
[OPT_MBFONT_BOLDOBLIQUE
].present
)
2600 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE
,
2601 mbfont_opts
[OPT_MBFONT_BOLDOBLIQUE
].string
,
2602 mbfont_opts
[OPT_MBFONT_BOLDOBLIQUE
].strlen
))
2605 /* Check if need to use Courier for ASCII code range, and if so pick up
2606 * the encoding to use */
2607 prt_use_courier
= mbfont_opts
[OPT_MBFONT_USECOURIER
].present
&&
2608 (TOLOWER_ASC(mbfont_opts
[OPT_MBFONT_USECOURIER
].string
[0]) == 'y');
2609 if (prt_use_courier
)
2611 /* Use national ASCII variant unless ASCII wanted */
2612 if (mbfont_opts
[OPT_MBFONT_ASCII
].present
&&
2613 (TOLOWER_ASC(mbfont_opts
[OPT_MBFONT_ASCII
].string
[0]) == 'y'))
2614 prt_ascii_encoding
= "ascii";
2616 prt_ascii_encoding
= prt_ps_mbfonts
[cmap
].ascii_enc
;
2619 prt_ps_font
= &prt_ps_mb_font
;
2625 prt_use_courier
= FALSE
;
2627 prt_ps_font
= &prt_ps_courier_font
;
2631 * Find the size of the paper and set the margins.
2633 prt_portrait
= (!printer_opts
[OPT_PRINT_PORTRAIT
].present
2634 || TOLOWER_ASC(printer_opts
[OPT_PRINT_PORTRAIT
].string
[0]) == 'y');
2635 if (printer_opts
[OPT_PRINT_PAPER
].present
)
2637 paper_name
= (char *)printer_opts
[OPT_PRINT_PAPER
].string
;
2638 paper_strlen
= printer_opts
[OPT_PRINT_PAPER
].strlen
;
2645 for (i
= 0; i
< PRT_MEDIASIZE_LEN
; ++i
)
2646 if (STRLEN(prt_mediasize
[i
].name
) == (unsigned)paper_strlen
2647 && STRNICMP(prt_mediasize
[i
].name
, paper_name
,
2650 if (i
== PRT_MEDIASIZE_LEN
)
2655 * Set PS pagesize based on media dimensions and print orientation.
2656 * Note: Media and page sizes have defined meanings in PostScript and should
2657 * be kept distinct. Media is the paper (or transparency, or ...) that is
2658 * printed on, whereas the page size is the area that the PostScript
2659 * interpreter renders into.
2663 prt_page_width
= prt_mediasize
[i
].width
;
2664 prt_page_height
= prt_mediasize
[i
].height
;
2668 prt_page_width
= prt_mediasize
[i
].height
;
2669 prt_page_height
= prt_mediasize
[i
].width
;
2673 * Set PS page margins based on the PS pagesize, not the mediasize - this
2674 * needs to be done before the cpl and lpp are calculated.
2676 prt_page_margins(prt_page_width
, prt_page_height
, &left
, &right
, &top
,
2678 prt_left_margin
= (float)left
;
2679 prt_right_margin
= (float)right
;
2680 prt_top_margin
= (float)top
;
2681 prt_bottom_margin
= (float)bottom
;
2684 * Set up the font size.
2686 fontsize
= PRT_PS_DEFAULT_FONTSIZE
;
2687 for (p
= p_pfn
; (p
= vim_strchr(p
, ':')) != NULL
; ++p
)
2688 if (p
[1] == 'h' && VIM_ISDIGIT(p
[2]))
2689 fontsize
= atoi((char *)p
+ 2);
2690 prt_font_metrics(fontsize
);
2693 * Return the number of characters per line, and lines per page for the
2694 * generic print code.
2696 psettings
->chars_per_line
= prt_get_cpl();
2697 psettings
->lines_per_page
= prt_get_lpp();
2699 /* Catch margin settings that leave no space for output! */
2700 if (psettings
->chars_per_line
<= 0 || psettings
->lines_per_page
<= 0)
2704 * Sort out the number of copies to be printed. PS by default will do
2705 * uncollated copies for you, so once we know how many uncollated copies are
2706 * wanted cache it away and lie to the generic code that we only want one
2709 psettings
->n_collated_copies
= 1;
2710 psettings
->n_uncollated_copies
= 1;
2712 prt_collate
= (!printer_opts
[OPT_PRINT_COLLATE
].present
2713 || TOLOWER_ASC(printer_opts
[OPT_PRINT_COLLATE
].string
[0]) == 'y');
2716 /* TODO: Get number of collated copies wanted. */
2717 psettings
->n_collated_copies
= 1;
2721 /* TODO: Get number of uncollated copies wanted and update the cached
2727 psettings
->jobname
= jobname
;
2730 * Set up printer duplex and tumble based on Duplex option setting - default
2731 * is long sided duplex printing (i.e. no tumble).
2735 psettings
->duplex
= 1;
2736 if (printer_opts
[OPT_PRINT_DUPLEX
].present
)
2738 if (STRNICMP(printer_opts
[OPT_PRINT_DUPLEX
].string
, "off", 3) == 0)
2741 psettings
->duplex
= 0;
2743 else if (STRNICMP(printer_opts
[OPT_PRINT_DUPLEX
].string
, "short", 5)
2748 /* For now user abort not supported */
2749 psettings
->user_abort
= 0;
2751 /* If the user didn't specify a file name, use a temp file. */
2752 if (psettings
->outfile
== NULL
)
2754 prt_ps_file_name
= vim_tempname('p');
2755 if (prt_ps_file_name
== NULL
)
2760 prt_ps_fd
= mch_fopen((char *)prt_ps_file_name
, WRITEBIN
);
2764 p
= expand_env_save(psettings
->outfile
);
2767 prt_ps_fd
= mch_fopen((char *)p
, WRITEBIN
);
2771 if (prt_ps_fd
== NULL
)
2773 EMSG(_("E324: Can't open PostScript output file"));
2774 mch_print_cleanup();
2778 prt_bufsiz
= psettings
->chars_per_line
;
2783 ga_init2(&prt_ps_buffer
, (int)sizeof(char), prt_bufsiz
);
2787 prt_attribute_change
= FALSE
;
2788 prt_need_moveto
= FALSE
;
2789 prt_need_font
= FALSE
;
2790 prt_need_fgcol
= FALSE
;
2791 prt_need_bgcol
= FALSE
;
2792 prt_need_underline
= FALSE
;
2794 prt_file_error
= FALSE
;
2800 prt_add_resource(resource
)
2801 struct prt_ps_resource_S
*resource
;
2804 char_u resource_buffer
[512];
2807 fd_resource
= mch_fopen((char *)resource
->filename
, READBIN
);
2808 if (fd_resource
== NULL
)
2810 EMSG2(_("E456: Can't open file \"%s\""), resource
->filename
);
2813 prt_dsc_resources("BeginResource", prt_resource_types
[resource
->type
],
2814 (char *)resource
->title
);
2816 prt_dsc_textline("BeginDocument", (char *)resource
->filename
);
2820 bytes_read
= fread((char *)resource_buffer
, sizeof(char_u
),
2821 sizeof(resource_buffer
), fd_resource
);
2822 if (ferror(fd_resource
))
2824 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
2825 resource
->filename
);
2826 fclose(fd_resource
);
2829 if (bytes_read
== 0)
2831 prt_write_file_raw_len(resource_buffer
, (int)bytes_read
);
2834 fclose(fd_resource
);
2838 fclose(fd_resource
);
2840 prt_dsc_noarg("EndDocument");
2842 prt_dsc_noarg("EndResource");
2848 mch_print_begin(psettings
)
2849 prt_settings_T
*psettings
;
2858 struct prt_ps_resource_S res_prolog
;
2859 struct prt_ps_resource_S res_encoding
;
2864 struct prt_ps_resource_S res_cidfont
;
2865 struct prt_ps_resource_S res_cmap
;
2869 * PS DSC Header comments - no PS code!
2872 prt_dsc_textline("Title", (char *)psettings
->jobname
);
2873 if (!get_user_name((char_u
*)buffer
, 256))
2874 STRCPY(buffer
, "Unknown");
2875 prt_dsc_textline("For", buffer
);
2876 prt_dsc_textline("Creator", VIM_VERSION_LONG
);
2877 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2879 p_time
= ctime(&now
);
2880 /* Note: ctime() adds a \n so we have to remove it :-( */
2881 p
= vim_strchr((char_u
*)p_time
, '\n');
2884 prt_dsc_textline("CreationDate", p_time
);
2885 prt_dsc_textline("DocumentData", "Clean8Bit");
2886 prt_dsc_textline("Orientation", "Portrait");
2887 prt_dsc_atend("Pages");
2888 prt_dsc_textline("PageOrder", "Ascend");
2889 /* The bbox does not change with orientation - it is always in the default
2890 * user coordinate system! We have to recalculate right and bottom
2891 * coordinates based on the font metrics for the bbox to be accurate. */
2892 prt_page_margins(prt_mediasize
[prt_media
].width
,
2893 prt_mediasize
[prt_media
].height
,
2894 &left
, &right
, &top
, &bottom
);
2895 bbox
[0] = (int)left
;
2898 /* In portrait printing the fixed point is the top left corner so we
2899 * derive the bbox from that point. We have the expected cpl chars
2900 * across the media and lpp lines down the media.
2902 bbox
[1] = (int)(top
- (psettings
->lines_per_page
+ prt_header_height())
2904 bbox
[2] = (int)(left
+ psettings
->chars_per_line
* prt_char_width
2906 bbox
[3] = (int)(top
+ 0.5);
2910 /* In landscape printing the fixed point is the bottom left corner so we
2911 * derive the bbox from that point. We have lpp chars across the media
2912 * and cpl lines up the media.
2914 bbox
[1] = (int)bottom
;
2915 bbox
[2] = (int)(left
+ ((psettings
->lines_per_page
2916 + prt_header_height()) * prt_line_height
) + 0.5);
2917 bbox
[3] = (int)(bottom
+ psettings
->chars_per_line
* prt_char_width
2920 prt_dsc_ints("BoundingBox", 4, bbox
);
2921 /* The media width and height does not change with landscape printing! */
2922 prt_dsc_docmedia(prt_mediasize
[prt_media
].name
,
2923 prt_mediasize
[prt_media
].width
,
2924 prt_mediasize
[prt_media
].height
,
2925 (double)0, NULL
, NULL
);
2926 /* Define fonts needed */
2928 if (!prt_out_mbyte
|| prt_use_courier
)
2930 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font
);
2934 prt_dsc_font_resource((prt_use_courier
? NULL
2935 : "DocumentNeededResources"), &prt_ps_mb_font
);
2936 if (!prt_custom_cmap
)
2937 prt_dsc_resources(NULL
, "cmap", prt_cmap
);
2941 /* Search for external resources VIM supplies */
2942 if (!prt_find_resource("prolog", &res_prolog
))
2944 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
2947 if (!prt_open_resource(&res_prolog
))
2949 if (!prt_check_resource(&res_prolog
, PRT_PROLOG_VERSION
))
2954 /* Look for required version of multi-byte printing procset */
2955 if (!prt_find_resource("cidfont", &res_cidfont
))
2957 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
2960 if (!prt_open_resource(&res_cidfont
))
2962 if (!prt_check_resource(&res_cidfont
, PRT_CID_PROLOG_VERSION
))
2967 /* Find an encoding to use for printing.
2968 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2969 * that cannot be found then default to "latin1".
2970 * Note: VIM specific encoding header is always skipped.
2976 p_encoding
= enc_skip(p_penc
);
2977 if (*p_encoding
== NUL
2978 || !prt_find_resource((char *)p_encoding
, &res_encoding
))
2980 /* 'printencoding' not set or not supported - find alternate */
2984 p_encoding
= enc_skip(p_enc
);
2985 props
= enc_canon_props(p_encoding
);
2986 if (!(props
& ENC_8BIT
)
2987 || !prt_find_resource((char *)p_encoding
, &res_encoding
))
2988 /* 8-bit 'encoding' is not supported */
2991 /* Use latin1 as default printing encoding */
2992 p_encoding
= (char_u
*)"latin1";
2993 if (!prt_find_resource((char *)p_encoding
, &res_encoding
))
2995 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3001 if (!prt_open_resource(&res_encoding
))
3003 /* For the moment there are no checks on encoding resource files to
3009 p_encoding
= enc_skip(p_penc
);
3010 if (*p_encoding
== NUL
)
3011 p_encoding
= enc_skip(p_enc
);
3012 if (prt_use_courier
)
3014 /* Include ASCII range encoding vector */
3015 if (!prt_find_resource(prt_ascii_encoding
, &res_encoding
))
3017 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3018 prt_ascii_encoding
);
3021 if (!prt_open_resource(&res_encoding
))
3023 /* For the moment there are no checks on encoding resource files to
3028 prt_conv
.vc_type
= CONV_NONE
;
3029 if (!(enc_canon_props(p_enc
) & enc_canon_props(p_encoding
) & ENC_8BIT
)) {
3030 /* Set up encoding conversion if required */
3031 if (FAIL
== convert_setup(&prt_conv
, p_enc
, p_encoding
))
3033 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
3039 prt_do_conv
= prt_conv
.vc_type
!= CONV_NONE
;
3041 if (prt_out_mbyte
&& prt_custom_cmap
)
3043 /* Find user supplied CMap */
3044 if (!prt_find_resource(prt_cmap
, &res_cmap
))
3046 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3050 if (!prt_open_resource(&res_cmap
))
3055 /* List resources supplied */
3056 STRCPY(buffer
, res_prolog
.title
);
3057 STRCAT(buffer
, " ");
3058 STRCAT(buffer
, res_prolog
.version
);
3059 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer
);
3063 STRCPY(buffer
, res_cidfont
.title
);
3064 STRCAT(buffer
, " ");
3065 STRCAT(buffer
, res_cidfont
.version
);
3066 prt_dsc_resources(NULL
, "procset", buffer
);
3068 if (prt_custom_cmap
)
3070 STRCPY(buffer
, res_cmap
.title
);
3071 STRCAT(buffer
, " ");
3072 STRCAT(buffer
, res_cmap
.version
);
3073 prt_dsc_resources(NULL
, "cmap", buffer
);
3076 if (!prt_out_mbyte
|| prt_use_courier
)
3079 STRCPY(buffer
, res_encoding
.title
);
3080 STRCAT(buffer
, " ");
3081 STRCAT(buffer
, res_encoding
.version
);
3082 prt_dsc_resources(NULL
, "encoding", buffer
);
3084 prt_dsc_requirements(prt_duplex
, prt_tumble
, prt_collate
,
3086 psettings
->do_syntax
3091 prt_dsc_noarg("EndComments");
3094 * PS Document page defaults
3096 prt_dsc_noarg("BeginDefaults");
3098 /* List font resources most likely common to all pages */
3100 if (!prt_out_mbyte
|| prt_use_courier
)
3102 prt_dsc_font_resource("PageResources", &prt_ps_courier_font
);
3106 prt_dsc_font_resource((prt_use_courier
? NULL
: "PageResources"),
3108 if (!prt_custom_cmap
)
3109 prt_dsc_resources(NULL
, "cmap", prt_cmap
);
3113 /* Paper will be used for all pages */
3114 prt_dsc_textline("PageMedia", prt_mediasize
[prt_media
].name
);
3116 prt_dsc_noarg("EndDefaults");
3119 * PS Document prolog inclusion - all required procsets.
3121 prt_dsc_noarg("BeginProlog");
3123 /* Add required procsets - NOTE: order is important! */
3124 if (!prt_add_resource(&res_prolog
))
3129 /* Add CID font procset, and any user supplied CMap */
3130 if (!prt_add_resource(&res_cidfont
))
3132 if (prt_custom_cmap
&& !prt_add_resource(&res_cmap
))
3138 if (!prt_out_mbyte
|| prt_use_courier
)
3140 /* There will be only one Roman font encoding to be included in the PS
3142 if (!prt_add_resource(&res_encoding
))
3145 prt_dsc_noarg("EndProlog");
3148 * PS Document setup - must appear after the prolog
3150 prt_dsc_noarg("BeginSetup");
3152 /* Device setup - page size and number of uncollated copies */
3153 prt_write_int((int)prt_mediasize
[prt_media
].width
);
3154 prt_write_int((int)prt_mediasize
[prt_media
].height
);
3156 prt_write_string("sps\n");
3157 prt_write_int(prt_num_copies
);
3158 prt_write_string("nc\n");
3159 prt_write_boolean(prt_duplex
);
3160 prt_write_boolean(prt_tumble
);
3161 prt_write_string("dt\n");
3162 prt_write_boolean(prt_collate
);
3163 prt_write_string("c\n");
3165 /* Font resource inclusion and definition */
3167 if (!prt_out_mbyte
|| prt_use_courier
)
3169 /* When using Courier for ASCII range when printing multi-byte, need to
3170 * pick up ASCII encoding to use with it. */
3171 if (prt_use_courier
)
3172 p_encoding
= (char_u
*)prt_ascii_encoding
;
3174 prt_dsc_resources("IncludeResource", "font",
3175 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_ROMAN
]);
3176 prt_def_font("F0", (char *)p_encoding
, (int)prt_line_height
,
3177 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_ROMAN
]);
3178 prt_dsc_resources("IncludeResource", "font",
3179 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_BOLD
]);
3180 prt_def_font("F1", (char *)p_encoding
, (int)prt_line_height
,
3181 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_BOLD
]);
3182 prt_dsc_resources("IncludeResource", "font",
3183 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_OBLIQUE
]);
3184 prt_def_font("F2", (char *)p_encoding
, (int)prt_line_height
,
3185 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_OBLIQUE
]);
3186 prt_dsc_resources("IncludeResource", "font",
3187 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_BOLDOBLIQUE
]);
3188 prt_def_font("F3", (char *)p_encoding
, (int)prt_line_height
,
3189 prt_ps_courier_font
.ps_fontname
[PRT_PS_FONT_BOLDOBLIQUE
]);
3194 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3195 * not have an italic form being a western style, so where no font is
3196 * defined for these faces VIM falls back to an existing face.
3197 * Note: if using Courier for the ASCII range then the printout will
3198 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3200 prt_dsc_resources("IncludeResource", "font",
3201 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_ROMAN
]);
3202 if (!prt_custom_cmap
)
3203 prt_dsc_resources("IncludeResource", "cmap", prt_cmap
);
3204 prt_def_cidfont("CF0", (int)prt_line_height
,
3205 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_ROMAN
]);
3207 if (prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLD
] != NULL
)
3209 prt_dsc_resources("IncludeResource", "font",
3210 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLD
]);
3211 if (!prt_custom_cmap
)
3212 prt_dsc_resources("IncludeResource", "cmap", prt_cmap
);
3213 prt_def_cidfont("CF1", (int)prt_line_height
,
3214 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLD
]);
3217 /* Use ROMAN for BOLD */
3218 prt_dup_cidfont("CF0", "CF1");
3220 if (prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_OBLIQUE
] != NULL
)
3222 prt_dsc_resources("IncludeResource", "font",
3223 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_OBLIQUE
]);
3224 if (!prt_custom_cmap
)
3225 prt_dsc_resources("IncludeResource", "cmap", prt_cmap
);
3226 prt_def_cidfont("CF2", (int)prt_line_height
,
3227 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_OBLIQUE
]);
3230 /* Use ROMAN for OBLIQUE */
3231 prt_dup_cidfont("CF0", "CF2");
3233 if (prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLDOBLIQUE
] != NULL
)
3235 prt_dsc_resources("IncludeResource", "font",
3236 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLDOBLIQUE
]);
3237 if (!prt_custom_cmap
)
3238 prt_dsc_resources("IncludeResource", "cmap", prt_cmap
);
3239 prt_def_cidfont("CF3", (int)prt_line_height
,
3240 prt_ps_mb_font
.ps_fontname
[PRT_PS_FONT_BOLDOBLIQUE
]);
3243 /* Use BOLD for BOLDOBLIQUE */
3244 prt_dup_cidfont("CF1", "CF3");
3248 /* Misc constant vars used for underlining and background rects */
3249 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height
,
3250 prt_ps_font
->uline_offset
), 2);
3251 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height
,
3252 prt_ps_font
->uline_width
), 2);
3253 prt_def_var("BO", prt_bgcol_offset
, 2);
3255 prt_dsc_noarg("EndSetup");
3257 /* Fail if any problems writing out to the PS file */
3258 return !prt_file_error
;
3262 mch_print_end(psettings
)
3263 prt_settings_T
*psettings
;
3265 prt_dsc_noarg("Trailer");
3268 * Output any info we don't know in toto until we finish
3270 prt_dsc_ints("Pages", 1, &prt_page_num
);
3272 prt_dsc_noarg("EOF");
3274 /* Write CTRL-D to close serial communication link if used.
3275 * NOTHING MUST BE WRITTEN AFTER THIS! */
3276 prt_write_file((char_u
*)IF_EB("\004", "\067"));
3278 if (!prt_file_error
&& psettings
->outfile
== NULL
3279 && !got_int
&& !psettings
->user_abort
)
3281 /* Close the file first. */
3282 if (prt_ps_fd
!= NULL
)
3287 prt_message((char_u
*)_("Sending to printer..."));
3289 /* Not printing to a file: use 'printexpr' to print the file. */
3290 if (eval_printexpr(prt_ps_file_name
, psettings
->arguments
) == FAIL
)
3291 EMSG(_("E365: Failed to print PostScript file"));
3293 prt_message((char_u
*)_("Print job sent."));
3296 mch_print_cleanup();
3300 mch_print_end_page()
3304 prt_write_string("re sp\n");
3306 prt_dsc_noarg("PageTrailer");
3308 return !prt_file_error
;
3313 mch_print_begin_page(str
)
3320 page_num
[0] = page_num
[1] = prt_page_num
;
3321 prt_dsc_ints("Page", 2, page_num
);
3323 prt_dsc_noarg("BeginPageSetup");
3325 prt_write_string("sv\n0 g\n");
3327 prt_in_ascii
= !prt_out_mbyte
;
3329 prt_write_string("CF0 sf\n");
3332 prt_write_string("F0 sf\n");
3333 prt_fgcol
= PRCOLOR_BLACK
;
3334 prt_bgcol
= PRCOLOR_WHITE
;
3335 prt_font
= PRT_PS_FONT_ROMAN
;
3337 /* Set up page transformation for landscape printing. */
3340 prt_write_int(-((int)prt_mediasize
[prt_media
].width
));
3341 prt_write_string("sl\n");
3344 prt_dsc_noarg("EndPageSetup");
3346 /* We have reset the font attributes, force setting them again. */
3347 curr_bg
= (long_u
)0xffffffff;
3348 curr_fg
= (long_u
)0xffffffff;
3351 return !prt_file_error
;
3355 mch_print_blank_page()
3357 return (mch_print_begin_page(NULL
) ? (mch_print_end_page()) : FALSE
);
3360 static float prt_pos_x
= 0;
3361 static float prt_pos_y
= 0;
3364 mch_print_start_line(margin
, page_line
)
3368 prt_pos_x
= prt_left_margin
;
3370 prt_pos_x
-= prt_number_width
;
3372 prt_pos_y
= prt_top_margin
- prt_first_line_height
-
3373 page_line
* prt_line_height
;
3375 prt_attribute_change
= TRUE
;
3376 prt_need_moveto
= TRUE
;
3378 prt_half_width
= FALSE
;
3384 mch_print_text_out(p
, len
)
3398 char_width
= prt_char_width
;
3401 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3402 * CJKV font to do what VIM is doing here - use a Roman font for characters
3403 * in the ASCII range, and the original CID font for everything else.
3404 * The problem is that GhostScript still (as of 8.13) does not support
3405 * rearranged fonts even though they have been documented by Adobe for 7
3406 * years! If they ever do, a lot of this code will disappear.
3408 if (prt_use_courier
)
3410 in_ascii
= (len
== 1 && *p
< 0x80);
3415 /* No longer in ASCII range - need to switch font */
3416 prt_in_ascii
= FALSE
;
3417 prt_need_font
= TRUE
;
3418 prt_attribute_change
= TRUE
;
3423 /* Now in ASCII range - need to switch font */
3424 prt_in_ascii
= TRUE
;
3425 prt_need_font
= TRUE
;
3426 prt_attribute_change
= TRUE
;
3431 half_width
= ((*mb_ptr2cells
)(p
) == 1);
3438 prt_half_width
= FALSE
;
3439 prt_pos_x
+= prt_char_width
/4;
3440 prt_need_moveto
= TRUE
;
3441 prt_attribute_change
= TRUE
;
3444 else if (half_width
)
3446 prt_half_width
= TRUE
;
3447 prt_pos_x
+= prt_char_width
/4;
3448 prt_need_moveto
= TRUE
;
3449 prt_attribute_change
= TRUE
;
3454 /* Output any required changes to the graphics state, after flushing any
3455 * text buffered so far.
3457 if (prt_attribute_change
)
3460 /* Reset count of number of chars that will be printed */
3463 if (prt_need_moveto
)
3465 prt_pos_x_moveto
= prt_pos_x
;
3466 prt_pos_y_moveto
= prt_pos_y
;
3467 prt_do_moveto
= TRUE
;
3469 prt_need_moveto
= FALSE
;
3475 prt_write_string("CF");
3478 prt_write_string("F");
3479 prt_write_int(prt_font
);
3480 prt_write_string("sf\n");
3481 prt_need_font
= FALSE
;
3486 r
= ((unsigned)prt_fgcol
& 0xff0000) >> 16;
3487 g
= ((unsigned)prt_fgcol
& 0xff00) >> 8;
3488 b
= prt_fgcol
& 0xff;
3490 prt_write_real(r
/ 255.0, 3);
3491 if (r
== g
&& g
== b
)
3492 prt_write_string("g\n");
3495 prt_write_real(g
/ 255.0, 3);
3496 prt_write_real(b
/ 255.0, 3);
3497 prt_write_string("r\n");
3499 prt_need_fgcol
= FALSE
;
3502 if (prt_bgcol
!= PRCOLOR_WHITE
)
3504 prt_new_bgcol
= prt_bgcol
;
3506 prt_do_bgcol
= TRUE
;
3509 prt_do_bgcol
= FALSE
;
3510 prt_need_bgcol
= FALSE
;
3512 if (prt_need_underline
)
3513 prt_do_underline
= prt_underline
;
3514 prt_need_underline
= FALSE
;
3516 prt_attribute_change
= FALSE
;
3522 /* Convert from multi-byte to 8-bit encoding */
3523 p
= string_convert(&prt_conv
, p
, &len
);
3530 /* Multi-byte character strings are represented more efficiently as hex
3531 * strings when outputting clean 8 bit PS.
3535 ch
= prt_hexchar
[(unsigned)(*p
) >> 4];
3536 ga_append(&prt_ps_buffer
, ch
);
3537 ch
= prt_hexchar
[(*p
) & 0xf];
3538 ga_append(&prt_ps_buffer
, ch
);
3546 /* Add next character to buffer of characters to output.
3547 * Note: One printed character may require several PS characters to
3548 * represent it, but we only count them as one printed character.
3551 if (ch
< 32 || ch
== '(' || ch
== ')' || ch
== '\\')
3553 /* Convert non-printing characters to either their escape or octal
3554 * sequence, ensures PS sent over a serial line does not interfere
3555 * with the comms protocol. Note: For EBCDIC we need to write out
3556 * the escape sequences as ASCII codes!
3557 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3559 ga_append(&prt_ps_buffer
, IF_EB('\\', 0134));
3562 case BS
: ga_append(&prt_ps_buffer
, IF_EB('b', 0142)); break;
3563 case TAB
: ga_append(&prt_ps_buffer
, IF_EB('t', 0164)); break;
3564 case NL
: ga_append(&prt_ps_buffer
, IF_EB('n', 0156)); break;
3565 case FF
: ga_append(&prt_ps_buffer
, IF_EB('f', 0146)); break;
3566 case CAR
: ga_append(&prt_ps_buffer
, IF_EB('r', 0162)); break;
3567 case '(': ga_append(&prt_ps_buffer
, IF_EB('(', 0050)); break;
3568 case ')': ga_append(&prt_ps_buffer
, IF_EB(')', 0051)); break;
3569 case '\\': ga_append(&prt_ps_buffer
, IF_EB('\\', 0134)); break;
3572 sprintf((char *)ch_buff
, "%03o", (unsigned int)ch
);
3574 ebcdic2ascii(ch_buff
, 3);
3576 ga_append(&prt_ps_buffer
, ch_buff
[0]);
3577 ga_append(&prt_ps_buffer
, ch_buff
[1]);
3578 ga_append(&prt_ps_buffer
, ch_buff
[2]);
3583 ga_append(&prt_ps_buffer
, ch
);
3587 /* Need to free any translated characters */
3588 if (prt_do_conv
&& (*p
!= NUL
))
3592 prt_text_run
+= char_width
;
3593 prt_pos_x
+= char_width
;
3595 /* The downside of fp - use relative error on right margin check */
3596 next_pos
= prt_pos_x
+ prt_char_width
;
3597 need_break
= (next_pos
> prt_right_margin
) &&
3598 ((next_pos
- prt_right_margin
) > (prt_right_margin
*1e-5));
3607 mch_print_set_font(iBold
, iItalic
, iUnderline
)
3619 if (font
!= prt_font
)
3622 prt_attribute_change
= TRUE
;
3623 prt_need_font
= TRUE
;
3625 if (prt_underline
!= iUnderline
)
3627 prt_underline
= iUnderline
;
3628 prt_attribute_change
= TRUE
;
3629 prt_need_underline
= TRUE
;
3634 mch_print_set_bg(bgcol
)
3637 prt_bgcol
= (int)bgcol
;
3638 prt_attribute_change
= TRUE
;
3639 prt_need_bgcol
= TRUE
;
3643 mch_print_set_fg(fgcol
)
3646 if (fgcol
!= (long_u
)prt_fgcol
)
3648 prt_fgcol
= (int)fgcol
;
3649 prt_attribute_change
= TRUE
;
3650 prt_need_fgcol
= TRUE
;
3654 # endif /*FEAT_POSTSCRIPT*/
3655 #endif /*FEAT_PRINTER*/