blob: a3b1c0e1f07ff99a85d6b738fc400eb0aa0e49b4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar81366db2005-07-24 21:16:51 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 */
9
10/*
11 * hardcopy.c: printing to paper
12 */
13
14#include "vim.h"
15#include "version.h"
16
17#if defined(FEAT_PRINTER) || defined(PROTO)
18/*
19 * To implement printing on a platform, the following functions must be
20 * defined:
21 *
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.
27 *
28 * int mch_print_begin(prt_settings_T *settings)
29 * Called to start the print job.
30 * Return FALSE to abort.
31 *
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.
36 *
37 * int mch_print_end_page()
38 * Called at the end of each page.
39 * Return FALSE to abort.
40 *
41 * int mch_print_blank_page()
42 * Called to generate a blank page for collated, duplex, multiple copy
43 * document. Return FALSE to abort.
44 *
45 * void mch_print_end(prt_settings_T *psettings)
46 * Called at normal end of print job.
47 *
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.
52 *
53 * void mch_print_set_font(int Bold, int Italic, int Underline);
54 * Called whenever the font style changes.
55 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000056 * void mch_print_set_bg(long_u bgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000057 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
59 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000060 * void mch_print_set_fg(long_u fgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000061 * Called to set the foreground color for the following text. Parameter is an
62 * RGB value.
63 *
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).
67 *
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.
71 *
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.
75 */
76
77#ifdef FEAT_SYN_HL
78static const long_u cterm_color_8[8] =
79{
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
82};
83
84static const long_u cterm_color_16[16] =
85{
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
90};
91
92static int current_syn_id;
93#endif
94
95#define PRCOLOR_BLACK (long_u)0
96#define PRCOLOR_WHITE (long_u)0xFFFFFFL
97
98static int curr_italic;
99static int curr_bold;
100static int curr_underline;
101static long_u curr_bg;
102static long_u curr_fg;
103static int page_count;
104
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100105#if defined(FEAT_POSTSCRIPT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000106# define OPT_MBFONT_USECOURIER 0
107# define OPT_MBFONT_ASCII 1
108# define OPT_MBFONT_REGULAR 2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000109# define OPT_MBFONT_BOLD 3
Bram Moolenaar81366db2005-07-24 21:16:51 +0000110# define OPT_MBFONT_OBLIQUE 4
111# define OPT_MBFONT_BOLDOBLIQUE 5
112# define OPT_MBFONT_NUM_OPTIONS 6
113
114static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] =
115{
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},
122};
123#endif
124
125/*
126 * These values determine the print position on a page.
127 */
128typedef struct
129{
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;
137
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138static char *parse_list_options(char_u *option_str, option_table_T *table, int table_size);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000139
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T *ppos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000141
142/*
143 * Parse 'printoptions' and set the flags in "printer_opts".
144 * Returns an error message or NULL;
145 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100147parse_printoptions(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000148{
149 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
150}
151
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100152#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000153/*
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200154 * Parse 'printmbfont' and set the flags in "mbfont_opts".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000155 * Returns an error message or NULL;
156 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100157 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100158parse_printmbfont(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000159{
160 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
161}
162#endif
163
164/*
165 * Parse a list of options in the form
166 * option:value,option:value,option:value
167 *
168 * "value" can start with a number which is parsed out, e.g. margin:12mm
169 *
170 * Returns an error message for an illegal option, NULL otherwise.
171 * Only used for the printer at the moment...
172 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100173 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100174parse_list_options(
175 char_u *option_str,
176 option_table_T *table,
177 int table_size)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000178{
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200179 option_table_T *old_opts;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100180 char *ret = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000181 char_u *stringp;
182 char_u *colonp;
183 char_u *commap;
184 char_u *p;
185 int idx = 0; /* init for GCC */
186 int len;
187
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200188 /* Save the old values, so that they can be restored in case of an error. */
189 old_opts = (option_table_T *)alloc(sizeof(option_table_T) * table_size);
190 if (old_opts == NULL)
191 return NULL;
192
Bram Moolenaar81366db2005-07-24 21:16:51 +0000193 for (idx = 0; idx < table_size; ++idx)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200194 {
195 old_opts[idx] = table[idx];
Bram Moolenaar81366db2005-07-24 21:16:51 +0000196 table[idx].present = FALSE;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200197 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000198
199 /*
200 * Repeat for all comma separated parts.
201 */
202 stringp = option_str;
203 while (*stringp)
204 {
205 colonp = vim_strchr(stringp, ':');
206 if (colonp == NULL)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100208 ret = N_("E550: Missing colon");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200209 break;
210 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000211 commap = vim_strchr(stringp, ',');
212 if (commap == NULL)
213 commap = option_str + STRLEN(option_str);
214
215 len = (int)(colonp - stringp);
216
217 for (idx = 0; idx < table_size; ++idx)
218 if (STRNICMP(stringp, table[idx].name, len) == 0)
219 break;
220
221 if (idx == table_size)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100223 ret = N_("E551: Illegal component");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200224 break;
225 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000226 p = colonp + 1;
227 table[idx].present = TRUE;
228
229 if (table[idx].hasnum)
230 {
231 if (!VIM_ISDIGIT(*p))
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100233 ret = N_("E552: digit expected");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200234 break;
235 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000236
237 table[idx].number = getdigits(&p); /*advances p*/
238 }
239
240 table[idx].string = p;
241 table[idx].strlen = (int)(commap - p);
242
243 stringp = commap;
244 if (*stringp == ',')
245 ++stringp;
246 }
247
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200248 if (ret != NULL)
249 {
250 /* Restore old options in case of error */
251 for (idx = 0; idx < table_size; ++idx)
252 table[idx] = old_opts[idx];
253 }
254 vim_free(old_opts);
255 return ret;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000256}
257
258
259#ifdef FEAT_SYN_HL
260/*
261 * If using a dark background, the colors will probably be too bright to show
262 * up well on white paper, so reduce their brightness.
263 */
264 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100265darken_rgb(long_u rgb)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000266{
267 return ((rgb >> 17) << 16)
268 + (((rgb & 0xff00) >> 9) << 8)
269 + ((rgb & 0xff) >> 1);
270}
271
272 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100273prt_get_term_color(int colorindex)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000274{
275 /* TODO: Should check for xterm with 88 or 256 colors. */
276 if (t_colors > 8)
277 return cterm_color_16[colorindex % 16];
278 return cterm_color_8[colorindex % 8];
279}
280
281 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100282prt_get_attr(
283 int hl_id,
284 prt_text_attr_T *pattr,
285 int modec)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000286{
287 int colorindex;
288 long_u fg_color;
289 long_u bg_color;
290 char *color;
291
292 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
293 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
294 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
295 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
296
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200297# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200298 if (USE_24BIT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000299 {
300 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
301 if (bg_color == PRCOLOR_BLACK)
302 bg_color = PRCOLOR_WHITE;
303
304 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
305 }
306 else
307# endif
308 {
309 bg_color = PRCOLOR_WHITE;
310
311 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
312 if (color == NULL)
313 colorindex = 0;
314 else
315 colorindex = atoi(color);
316
317 if (colorindex >= 0 && colorindex < t_colors)
318 fg_color = prt_get_term_color(colorindex);
319 else
320 fg_color = PRCOLOR_BLACK;
321 }
322
323 if (fg_color == PRCOLOR_WHITE)
324 fg_color = PRCOLOR_BLACK;
325 else if (*p_bg == 'd')
326 fg_color = darken_rgb(fg_color);
327
328 pattr->fg_color = fg_color;
329 pattr->bg_color = bg_color;
330}
331#endif /* FEAT_SYN_HL */
332
333 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100334prt_set_fg(long_u fg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000335{
336 if (fg != curr_fg)
337 {
338 curr_fg = fg;
339 mch_print_set_fg(fg);
340 }
341}
342
343 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100344prt_set_bg(long_u bg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000345{
346 if (bg != curr_bg)
347 {
348 curr_bg = bg;
349 mch_print_set_bg(bg);
350 }
351}
352
353 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100354prt_set_font(int bold, int italic, int underline)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000355{
356 if (curr_bold != bold
357 || curr_italic != italic
358 || curr_underline != underline)
359 {
360 curr_underline = underline;
361 curr_italic = italic;
362 curr_bold = bold;
363 mch_print_set_font(bold, italic, underline);
364 }
365}
366
367/*
368 * Print the line number in the left margin.
369 */
370 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100371prt_line_number(
372 prt_settings_T *psettings,
373 int page_line,
374 linenr_T lnum)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000375{
376 int i;
377 char_u tbuf[20];
378
379 prt_set_fg(psettings->number.fg_color);
380 prt_set_bg(psettings->number.bg_color);
381 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
382 mch_print_start_line(TRUE, page_line);
383
384 /* Leave two spaces between the number and the text; depends on
385 * PRINT_NUMBER_WIDTH. */
386 sprintf((char *)tbuf, "%6ld", (long)lnum);
387 for (i = 0; i < 6; i++)
388 (void)mch_print_text_out(&tbuf[i], 1);
389
390#ifdef FEAT_SYN_HL
391 if (psettings->do_syntax)
392 /* Set colors for next character. */
393 current_syn_id = -1;
394 else
395#endif
396 {
397 /* Set colors and font back to normal. */
398 prt_set_fg(PRCOLOR_BLACK);
399 prt_set_bg(PRCOLOR_WHITE);
400 prt_set_font(FALSE, FALSE, FALSE);
401 }
402}
403
Bram Moolenaar81366db2005-07-24 21:16:51 +0000404/*
405 * Get the currently effective header height.
406 */
407 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100408prt_header_height(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000409{
410 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
411 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
412 return 2;
413}
414
415/*
416 * Return TRUE if using a line number for printing.
417 */
418 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100419prt_use_number(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000420{
421 return (printer_opts[OPT_PRINT_NUMBER].present
422 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
423}
424
425/*
426 * Return the unit used in a margin item in 'printoptions'.
427 * Returns PRT_UNIT_NONE if not recognized.
428 */
429 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100430prt_get_unit(int idx)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000431{
432 int u = PRT_UNIT_NONE;
433 int i;
434 static char *(units[4]) = PRT_UNIT_NAMES;
435
436 if (printer_opts[idx].present)
437 for (i = 0; i < 4; ++i)
438 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
439 {
440 u = i;
441 break;
442 }
443 return u;
444}
445
446/*
447 * Print the page header.
448 */
Bram Moolenaar81366db2005-07-24 21:16:51 +0000449 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100450prt_header(
451 prt_settings_T *psettings,
452 int pagenum,
453 linenr_T lnum UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000454{
455 int width = psettings->chars_per_line;
456 int page_line;
457 char_u *tbuf;
458 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000459 int l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000460
461 /* Also use the space for the line number. */
462 if (prt_use_number())
463 width += PRINT_NUMBER_WIDTH;
464
465 tbuf = alloc(width + IOSIZE);
466 if (tbuf == NULL)
467 return;
468
469#ifdef FEAT_STL_OPT
470 if (*p_header != NUL)
471 {
472 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000473 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000474
475 /*
476 * Need to (temporarily) set current line number and first/last line
477 * number on the 'window'. Since we don't know how long the page is,
478 * set the first and current line number to the top line, and guess
479 * that the page length is 64.
480 */
481 tmp_lnum = curwin->w_cursor.lnum;
482 tmp_topline = curwin->w_topline;
483 tmp_botline = curwin->w_botline;
484 curwin->w_cursor.lnum = lnum;
485 curwin->w_topline = lnum;
486 curwin->w_botline = lnum + 63;
487 printer_page_num = pagenum;
488
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000489# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000490 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000491# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000492 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000493 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000494 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000495
496 /* Reset line numbers */
497 curwin->w_cursor.lnum = tmp_lnum;
498 curwin->w_topline = tmp_topline;
499 curwin->w_botline = tmp_botline;
500 }
501 else
502#endif
503 sprintf((char *)tbuf, _("Page %d"), pagenum);
504
505 prt_set_fg(PRCOLOR_BLACK);
506 prt_set_bg(PRCOLOR_WHITE);
507 prt_set_font(TRUE, FALSE, FALSE);
508
509 /* Use a negative line number to indicate printing in the top margin. */
510 page_line = 0 - prt_header_height();
511 mch_print_start_line(TRUE, page_line);
512 for (p = tbuf; *p != NUL; )
513 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100514 if (mch_print_text_out(p, (l = (*mb_ptr2len)(p))))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000515 {
516 ++page_line;
517 if (page_line >= 0) /* out of room in header */
518 break;
519 mch_print_start_line(TRUE, page_line);
520 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000521 p += l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000522 }
523
524 vim_free(tbuf);
525
526#ifdef FEAT_SYN_HL
527 if (psettings->do_syntax)
528 /* Set colors for next character. */
529 current_syn_id = -1;
530 else
531#endif
532 {
533 /* Set colors and font back to normal. */
534 prt_set_fg(PRCOLOR_BLACK);
535 prt_set_bg(PRCOLOR_WHITE);
536 prt_set_font(FALSE, FALSE, FALSE);
537 }
538}
539
540/*
541 * Display a print status message.
542 */
543 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100544prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000545{
546 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100547 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000548 out_flush();
549}
550
551 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100552ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000553{
554 linenr_T lnum;
555 int collated_copies, uncollated_copies;
556 prt_settings_T settings;
557 long_u bytes_to_print = 0;
558 int page_line;
559 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000560
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200561 vim_memset(&settings, 0, sizeof(prt_settings_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000562 settings.has_color = TRUE;
563
564# ifdef FEAT_POSTSCRIPT
565 if (*eap->arg == '>')
566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 char *errormsg = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000568
569 /* Expand things like "%.ps". */
570 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
571 {
572 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100573 emsg(errormsg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000574 return;
575 }
576 settings.outfile = skipwhite(eap->arg + 1);
577 }
578 else if (*eap->arg != NUL)
579 settings.arguments = eap->arg;
580# endif
581
582 /*
583 * Initialise for printing. Ask the user for settings, unless forceit is
584 * set.
585 * The mch_print_init() code should set up margins if applicable. (It may
586 * not be a real printer - for example the engine might generate HTML or
587 * PS.)
588 */
589 if (mch_print_init(&settings,
590 curbuf->b_fname == NULL
591 ? (char_u *)buf_spname(curbuf)
592 : curbuf->b_sfname == NULL
593 ? curbuf->b_fname
594 : curbuf->b_sfname,
595 eap->forceit) == FAIL)
596 return;
597
598#ifdef FEAT_SYN_HL
599# ifdef FEAT_GUI
600 if (gui.in_use)
601 settings.modec = 'g';
602 else
603# endif
604 if (t_colors > 1)
605 settings.modec = 'c';
606 else
607 settings.modec = 't';
608
Bram Moolenaar860cae12010-06-05 23:22:07 +0200609 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000610 settings.do_syntax = FALSE;
611 else if (printer_opts[OPT_PRINT_SYNTAX].present
612 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
613 settings.do_syntax =
614 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
615 else
616 settings.do_syntax = settings.has_color;
617#endif
618
619 /* Set up printing attributes for line numbers */
620 settings.number.fg_color = PRCOLOR_BLACK;
621 settings.number.bg_color = PRCOLOR_WHITE;
622 settings.number.bold = FALSE;
623 settings.number.italic = TRUE;
624 settings.number.underline = FALSE;
625#ifdef FEAT_SYN_HL
626 /*
627 * Syntax highlighting of line numbers.
628 */
629 if (prt_use_number() && settings.do_syntax)
630 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000631 int id;
632
Bram Moolenaar81366db2005-07-24 21:16:51 +0000633 id = syn_name2id((char_u *)"LineNr");
634 if (id > 0)
635 id = syn_get_final_id(id);
636
637 prt_get_attr(id, &settings.number, settings.modec);
638 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000639#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000640
641 /*
642 * Estimate the total lines to be printed
643 */
644 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
645 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
646 if (bytes_to_print == 0)
647 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100648 msg(_("No text to be printed"));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000649 goto print_fail_no_begin;
650 }
651
652 /* Set colors and font to normal. */
653 curr_bg = (long_u)0xffffffffL;
654 curr_fg = (long_u)0xffffffffL;
655 curr_italic = MAYBE;
656 curr_bold = MAYBE;
657 curr_underline = MAYBE;
658
659 prt_set_fg(PRCOLOR_BLACK);
660 prt_set_bg(PRCOLOR_WHITE);
661 prt_set_font(FALSE, FALSE, FALSE);
662#ifdef FEAT_SYN_HL
663 current_syn_id = -1;
664#endif
665
666 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
667 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
668
669 if (!mch_print_begin(&settings))
670 goto print_fail_no_begin;
671
672 /*
673 * Loop over collated copies: 1 2 3, 1 2 3, ...
674 */
675 page_count = 0;
676 for (collated_copies = 0;
677 collated_copies < settings.n_collated_copies;
678 collated_copies++)
679 {
680 prt_pos_T prtpos; /* current print position */
681 prt_pos_T page_prtpos; /* print position at page start */
682 int side;
683
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200684 vim_memset(&page_prtpos, 0, sizeof(prt_pos_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000685 page_prtpos.file_line = eap->line1;
686 prtpos = page_prtpos;
687
688 if (jobsplit && collated_copies > 0)
689 {
690 /* Splitting jobs: Stop a previous job and start a new one. */
691 mch_print_end(&settings);
692 if (!mch_print_begin(&settings))
693 goto print_fail_no_begin;
694 }
695
696 /*
697 * Loop over all pages in the print job: 1 2 3 ...
698 */
699 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
700 {
701 /*
702 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
703 * For duplex: 12 12 12 34 34 34, ...
704 */
705 for (uncollated_copies = 0;
706 uncollated_copies < settings.n_uncollated_copies;
707 uncollated_copies++)
708 {
709 /* Set the print position to the start of this page. */
710 prtpos = page_prtpos;
711
712 /*
713 * Do front and rear side of a page.
714 */
715 for (side = 0; side <= settings.duplex; ++side)
716 {
717 /*
718 * Print one page.
719 */
720
721 /* Check for interrupt character every page. */
722 ui_breakcheck();
723 if (got_int || settings.user_abort)
724 goto print_fail;
725
726 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
727 page_count + 1 + side,
728 prtpos.bytes_printed > 1000000
729 ? (int)(prtpos.bytes_printed /
730 (bytes_to_print / 100))
731 : (int)((prtpos.bytes_printed * 100)
732 / bytes_to_print));
733 if (!mch_print_begin_page(IObuff))
734 goto print_fail;
735
736 if (settings.n_collated_copies > 1)
737 sprintf((char *)IObuff + STRLEN(IObuff),
738 _(" Copy %d of %d"),
739 collated_copies + 1,
740 settings.n_collated_copies);
741 prt_message(IObuff);
742
743 /*
744 * Output header if required
745 */
746 if (prt_header_height() > 0)
747 prt_header(&settings, page_count + 1 + side,
748 prtpos.file_line);
749
750 for (page_line = 0; page_line < settings.lines_per_page;
751 ++page_line)
752 {
753 prtpos.column = hardcopy_line(&settings,
754 page_line, &prtpos);
755 if (prtpos.column == 0)
756 {
757 /* finished a file line */
758 prtpos.bytes_printed +=
759 STRLEN(skipwhite(ml_get(prtpos.file_line)));
760 if (++prtpos.file_line > eap->line2)
761 break; /* reached the end */
762 }
763 else if (prtpos.ff)
764 {
765 /* Line had a formfeed in it - start new page but
766 * stay on the current line */
767 break;
768 }
769 }
770
771 if (!mch_print_end_page())
772 goto print_fail;
773 if (prtpos.file_line > eap->line2)
774 break; /* reached the end */
775 }
776
777 /*
778 * Extra blank page for duplexing with odd number of pages and
779 * more copies to come.
780 */
781 if (prtpos.file_line > eap->line2 && settings.duplex
782 && side == 0
783 && uncollated_copies + 1 < settings.n_uncollated_copies)
784 {
785 if (!mch_print_blank_page())
786 goto print_fail;
787 }
788 }
789 if (settings.duplex && prtpos.file_line <= eap->line2)
790 ++page_count;
791
792 /* Remember the position where the next page starts. */
793 page_prtpos = prtpos;
794 }
795
796 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
797 settings.jobname);
798 prt_message(IObuff);
799 }
800
801print_fail:
802 if (got_int || settings.user_abort)
803 {
804 sprintf((char *)IObuff, "%s", _("Printing aborted"));
805 prt_message(IObuff);
806 }
807 mch_print_end(&settings);
808
809print_fail_no_begin:
810 mch_print_cleanup();
811}
812
813/*
814 * Print one page line.
815 * Return the next column to print, or zero if the line is finished.
816 */
817 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100818hardcopy_line(
819 prt_settings_T *psettings,
820 int page_line,
821 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000822{
823 colnr_T col;
824 char_u *line;
825 int need_break = FALSE;
826 int outputlen;
827 int tab_spaces;
828 long_u print_pos;
829#ifdef FEAT_SYN_HL
830 prt_text_attr_T attr;
831 int id;
832#endif
833
834 if (ppos->column == 0 || ppos->ff)
835 {
836 print_pos = 0;
837 tab_spaces = 0;
838 if (!ppos->ff && prt_use_number())
839 prt_line_number(psettings, page_line, ppos->file_line);
840 ppos->ff = FALSE;
841 }
842 else
843 {
844 /* left over from wrap halfway a tab */
845 print_pos = ppos->print_pos;
846 tab_spaces = ppos->lead_spaces;
847 }
848
849 mch_print_start_line(0, page_line);
850 line = ml_get(ppos->file_line);
851
852 /*
853 * Loop over the columns until the end of the file line or right margin.
854 */
855 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
856 {
857 outputlen = 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000858 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000859 outputlen = 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000860#ifdef FEAT_SYN_HL
861 /*
862 * syntax highlighting stuff.
863 */
864 if (psettings->do_syntax)
865 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000866 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000867 if (id > 0)
868 id = syn_get_final_id(id);
869 else
870 id = 0;
871 /* Get the line again, a multi-line regexp may invalidate it. */
872 line = ml_get(ppos->file_line);
873
874 if (id != current_syn_id)
875 {
876 current_syn_id = id;
877 prt_get_attr(id, &attr, psettings->modec);
878 prt_set_font(attr.bold, attr.italic, attr.underline);
879 prt_set_fg(attr.fg_color);
880 prt_set_bg(attr.bg_color);
881 }
882 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000883#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000884
885 /*
886 * Appropriately expand any tabs to spaces.
887 */
888 if (line[col] == TAB || tab_spaces != 0)
889 {
890 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200891#ifdef FEAT_VARTABS
892 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
893 curbuf->b_p_vts_array);
894#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000895 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200896#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000897
898 while (tab_spaces > 0)
899 {
900 need_break = mch_print_text_out((char_u *)" ", 1);
901 print_pos++;
902 tab_spaces--;
903 if (need_break)
904 break;
905 }
906 /* Keep the TAB if we didn't finish it. */
907 if (need_break && tab_spaces > 0)
908 break;
909 }
910 else if (line[col] == FF
911 && printer_opts[OPT_PRINT_FORMFEED].present
912 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
913 == 'y')
914 {
915 ppos->ff = TRUE;
916 need_break = 1;
917 }
918 else
919 {
920 need_break = mch_print_text_out(line + col, outputlen);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000921 if (has_mbyte)
922 print_pos += (*mb_ptr2cells)(line + col);
923 else
Bram Moolenaar81366db2005-07-24 21:16:51 +0000924 print_pos++;
925 }
926 }
927
928 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000929 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000930
931 /*
932 * Start next line of file if we clip lines, or have reached end of the
933 * line, unless we are doing a formfeed.
934 */
935 if (!ppos->ff
936 && (line[col] == NUL
937 || (printer_opts[OPT_PRINT_WRAP].present
938 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
939 == 'n')))
940 return 0;
941 return col;
942}
943
944# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
945
946/*
947 * PS printer stuff.
948 *
949 * Sources of information to help maintain the PS printing code:
950 *
951 * 1. PostScript Language Reference, 3rd Edition,
952 * Addison-Wesley, 1999, ISBN 0-201-37922-8
953 * 2. PostScript Language Program Design,
954 * Addison-Wesley, 1988, ISBN 0-201-14396-8
955 * 3. PostScript Tutorial and Cookbook,
956 * Addison Wesley, 1985, ISBN 0-201-10179-3
957 * 4. PostScript Language Document Structuring Conventions Specification,
958 * version 3.0,
959 * Adobe Technote 5001, 25th September 1992
960 * 5. PostScript Printer Description File Format Specification, Version 4.3,
961 * Adobe technote 5003, 9th February 1996
962 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
963 * Adobe Technote 5007, 7th October 1998
964 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
965 * Adobe Technote 5014, 8th October 1996
966 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
967 * Adoboe Technote 5094, 8th September, 2001
968 * 9. CJKV Information Processing, 2nd Edition,
969 * O'Reilly, 2002, ISBN 1-56592-224-7
970 *
971 * Some of these documents can be found in PDF form on Adobe's web site -
972 * http://www.adobe.com
973 */
974
975#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
976
977#define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
978#define PRT_PS_DEFAULT_FONTSIZE (10)
979#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
980
981struct prt_mediasize_S
982{
983 char *name;
984 float width; /* width and height in points for portrait */
985 float height;
986};
987
988#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
989
990static struct prt_mediasize_S prt_mediasize[] =
991{
992 {"A4", 595.0, 842.0},
993 {"letter", 612.0, 792.0},
994 {"10x14", 720.0, 1008.0},
995 {"A3", 842.0, 1191.0},
996 {"A5", 420.0, 595.0},
997 {"B4", 729.0, 1032.0},
998 {"B5", 516.0, 729.0},
999 {"executive", 522.0, 756.0},
1000 {"folio", 595.0, 935.0},
1001 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1002 {"legal", 612.0, 1008.0},
1003 {"quarto", 610.0, 780.0},
1004 {"statement", 396.0, 612.0},
1005 {"tabloid", 792.0, 1224.0}
1006};
1007
1008/* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1009struct prt_ps_font_S
1010{
1011 int wx;
1012 int uline_offset;
1013 int uline_width;
1014 int bbox_min_y;
1015 int bbox_max_y;
1016 char *(ps_fontname[4]);
1017};
1018
1019#define PRT_PS_FONT_ROMAN (0)
1020#define PRT_PS_FONT_BOLD (1)
1021#define PRT_PS_FONT_OBLIQUE (2)
1022#define PRT_PS_FONT_BOLDOBLIQUE (3)
1023
1024/* Standard font metrics for Courier family */
1025static struct prt_ps_font_S prt_ps_courier_font =
1026{
1027 600,
1028 -100, 50,
1029 -250, 805,
1030 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1031};
1032
Bram Moolenaar81366db2005-07-24 21:16:51 +00001033/* Generic font metrics for multi-byte fonts */
1034static struct prt_ps_font_S prt_ps_mb_font =
1035{
1036 1000,
1037 -100, 50,
1038 -250, 805,
1039 {NULL, NULL, NULL, NULL}
1040};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001041
1042/* Pointer to current font set being used */
1043static struct prt_ps_font_S* prt_ps_font;
1044
1045/* Structures to map user named encoding and mapping to PS equivalents for
1046 * building CID font name */
1047struct prt_ps_encoding_S
1048{
1049 char *encoding;
1050 char *cmap_encoding;
1051 int needs_charset;
1052};
1053
1054struct prt_ps_charset_S
1055{
1056 char *charset;
1057 char *cmap_charset;
1058 int has_charset;
1059};
1060
Bram Moolenaar81366db2005-07-24 21:16:51 +00001061
1062#define CS_JIS_C_1978 (0x01)
1063#define CS_JIS_X_1983 (0x02)
1064#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001065#define CS_NEC (0x08)
1066#define CS_MSWINDOWS (0x10)
1067#define CS_CP932 (0x20)
1068#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001069#define CS_KANJITALK7 (0x80)
1070
1071/* Japanese encodings and charsets */
1072static struct prt_ps_encoding_S j_encodings[] =
1073{
1074 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001075 CS_NEC)},
1076 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1077 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1078 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001079 {"cp932", "RKSJ", CS_JIS_X_1983},
1080 {"ucs-2", "UCS2", CS_JIS_X_1990},
1081 {"utf-8", "UTF8" , CS_JIS_X_1990}
1082};
1083static struct prt_ps_charset_S j_charsets[] =
1084{
1085 {"JIS_C_1978", "78", CS_JIS_C_1978},
1086 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1087 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001088 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001089 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1090 {"CP932", "90ms", CS_JIS_X_1983},
1091 {"KANJITALK6", "83pv", CS_KANJITALK6},
1092 {"KANJITALK7", "90pv", CS_KANJITALK7}
1093};
1094
1095#define CS_GB_2312_80 (0x01)
1096#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001097#define CS_GBK2K (0x04)
1098#define CS_SC_MAC (0x08)
1099#define CS_GBT_90_MAC (0x10)
1100#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001101#define CS_SC_ISO10646 (0x40)
1102
1103/* Simplified Chinese encodings and charsets */
1104static struct prt_ps_encoding_S sc_encodings[] =
1105{
1106 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1107 {"gb18030", NULL, CS_GBK2K},
1108 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001109 CS_GBT_90_MAC)},
1110 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001111 {"ucs-2", "UCS2", CS_SC_ISO10646},
1112 {"utf-8", "UTF8", CS_SC_ISO10646}
1113};
1114static struct prt_ps_charset_S sc_charsets[] =
1115{
1116 {"GB_2312-80", "GB", CS_GB_2312_80},
1117 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001118 {"MAC", "GBpc", CS_SC_MAC},
1119 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1120 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001121 {"GB18030", "GBK2K", CS_GBK2K},
1122 {"ISO10646", "UniGB", CS_SC_ISO10646}
1123};
1124
1125#define CS_CNS_PLANE_1 (0x01)
1126#define CS_CNS_PLANE_2 (0x02)
1127#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001128#define CS_B5 (0x08)
1129#define CS_ETEN (0x10)
1130#define CS_HK_GCCS (0x20)
1131#define CS_HK_SCS (0x40)
1132#define CS_HK_SCS_ETEN (0x80)
1133#define CS_MTHKL (0x100)
1134#define CS_MTHKS (0x200)
1135#define CS_DLHKL (0x400)
1136#define CS_DLHKS (0x800)
1137#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001138
1139/* Traditional Chinese encodings and charsets */
1140static struct prt_ps_encoding_S tc_encodings[] =
1141{
1142 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1143 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001144 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1145 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1146 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001147 {"cp950", "B5", CS_B5},
1148 {"ucs-2", "UCS2", CS_TC_ISO10646},
1149 {"utf-8", "UTF8", CS_TC_ISO10646},
1150 {"utf-16", "UTF16", CS_TC_ISO10646},
1151 {"utf-32", "UTF32", CS_TC_ISO10646}
1152};
1153static struct prt_ps_charset_S tc_charsets[] =
1154{
1155 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1156 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1157 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001158 {"BIG5", NULL, CS_B5},
1159 {"CP950", NULL, CS_B5},
1160 {"ETEN", "ETen", CS_ETEN},
1161 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1162 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001163 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1164 {"MTHKL", "HKm471", CS_MTHKL},
1165 {"MTHKS", "HKm314", CS_MTHKS},
1166 {"DLHKL", "HKdla", CS_DLHKL},
1167 {"DLHKS", "HKdlb", CS_DLHKS},
1168 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1169};
1170
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001171#define CS_KR_X_1992 (0x01)
1172#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001173#define CS_KR_X_1992_MS (0x04)
1174#define CS_KR_ISO10646 (0x08)
1175
1176/* Korean encodings and charsets */
1177static struct prt_ps_encoding_S k_encodings[] =
1178{
1179 {"iso-2022-kr", NULL, CS_KR_X_1992},
1180 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1181 {"johab", "Johab", CS_KR_X_1992},
1182 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001183 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001184 {"cp949", "UHC", CS_KR_X_1992_MS},
1185 {"ucs-2", "UCS2", CS_KR_ISO10646},
1186 {"utf-8", "UTF8", CS_KR_ISO10646}
1187};
1188static struct prt_ps_charset_S k_charsets[] =
1189{
1190 {"KS_X_1992", "KSC", CS_KR_X_1992},
1191 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001192 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001193 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1194 {"CP949", "KSCms", CS_KR_X_1992_MS},
1195 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1196 {"ISO10646", "UniKS", CS_KR_ISO10646}
1197};
1198
1199/* Collections of encodings and charsets for multi-byte printing */
1200struct prt_ps_mbfont_S
1201{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001202 int num_encodings;
1203 struct prt_ps_encoding_S *encodings;
1204 int num_charsets;
1205 struct prt_ps_charset_S *charsets;
1206 char *ascii_enc;
1207 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001208};
1209
1210static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1211{
1212 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001213 NUM_ELEMENTS(j_encodings),
1214 j_encodings,
1215 NUM_ELEMENTS(j_charsets),
1216 j_charsets,
1217 "jis_roman",
1218 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001219 },
1220 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001221 NUM_ELEMENTS(sc_encodings),
1222 sc_encodings,
1223 NUM_ELEMENTS(sc_charsets),
1224 sc_charsets,
1225 "gb_roman",
1226 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001227 },
1228 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001229 NUM_ELEMENTS(tc_encodings),
1230 tc_encodings,
1231 NUM_ELEMENTS(tc_charsets),
1232 tc_charsets,
1233 "cns_roman",
1234 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001235 },
1236 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001237 NUM_ELEMENTS(k_encodings),
1238 k_encodings,
1239 NUM_ELEMENTS(k_charsets),
1240 k_charsets,
1241 "ks_roman",
1242 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001243 }
1244};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001245
1246struct prt_ps_resource_S
1247{
1248 char_u name[64];
1249 char_u filename[MAXPATHL + 1];
1250 int type;
1251 char_u title[256];
1252 char_u version[256];
1253};
1254
1255/* Types of PS resource file currently used */
1256#define PRT_RESOURCE_TYPE_PROCSET (0)
1257#define PRT_RESOURCE_TYPE_ENCODING (1)
1258#define PRT_RESOURCE_TYPE_CMAP (2)
1259
1260/* The PS prolog file version number has to match - if the prolog file is
1261 * updated, increment the number in the file and here. Version checking was
1262 * added as of VIM 6.2.
1263 * The CID prolog file version number behaves as per PS prolog.
1264 * Table of VIM and prolog versions:
1265 *
1266 * VIM Prolog CIDProlog
1267 * 6.2 1.3
1268 * 7.0 1.4 1.0
1269 */
1270#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1271#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1272
1273/* String versions of PS resource types - indexed by constants above so don't
1274 * re-order!
1275 */
1276static char *prt_resource_types[] =
1277{
1278 "procset",
1279 "encoding",
1280 "cmap"
1281};
1282
1283/* Strings to look for in a PS resource file */
1284#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1285#define PRT_RESOURCE_RESOURCE "Resource-"
1286#define PRT_RESOURCE_PROCSET "ProcSet"
1287#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001288#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001289
1290
1291/* Data for table based DSC comment recognition, easy to extend if VIM needs to
1292 * read more comments. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001293#define PRT_DSC_MISC_TYPE (-1)
1294#define PRT_DSC_TITLE_TYPE (1)
1295#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001296#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1297
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001298#define PRT_DSC_TITLE "%%Title:"
1299#define PRT_DSC_VERSION "%%Version:"
1300#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001301
1302struct prt_dsc_comment_S
1303{
1304 char *string;
1305 int len;
1306 int type;
1307};
1308
1309struct prt_dsc_line_S
1310{
1311 int type;
1312 char_u *string;
1313 int len;
1314};
1315
1316
1317#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1318static struct prt_dsc_comment_S prt_dsc_table[] =
1319{
1320 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1321 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1322 PRT_DSC_VERSION_TYPE},
1323 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1324 PRT_DSC_ENDCOMMENTS_TYPE}
1325};
1326
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001327static void prt_write_file_len(char_u *buffer, int bytes);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001328static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001329
1330/*
1331 * Variables for the output PostScript file.
1332 */
1333static FILE *prt_ps_fd;
1334static int prt_file_error;
1335static char_u *prt_ps_file_name = NULL;
1336
1337/*
1338 * Various offsets and dimensions in default PostScript user space (points).
1339 * Used for text positioning calculations
1340 */
1341static float prt_page_width;
1342static float prt_page_height;
1343static float prt_left_margin;
1344static float prt_right_margin;
1345static float prt_top_margin;
1346static float prt_bottom_margin;
1347static float prt_line_height;
1348static float prt_first_line_height;
1349static float prt_char_width;
1350static float prt_number_width;
1351static float prt_bgcol_offset;
1352static float prt_pos_x_moveto = 0.0;
1353static float prt_pos_y_moveto = 0.0;
1354
1355/*
1356 * Various control variables used to decide when and how to change the
1357 * PostScript graphics state.
1358 */
1359static int prt_need_moveto;
1360static int prt_do_moveto;
1361static int prt_need_font;
1362static int prt_font;
1363static int prt_need_underline;
1364static int prt_underline;
1365static int prt_do_underline;
1366static int prt_need_fgcol;
1367static int prt_fgcol;
1368static int prt_need_bgcol;
1369static int prt_do_bgcol;
1370static int prt_bgcol;
1371static int prt_new_bgcol;
1372static int prt_attribute_change;
1373static float prt_text_run;
1374static int prt_page_num;
1375static int prt_bufsiz;
1376
1377/*
1378 * Variables controlling physical printing.
1379 */
1380static int prt_media;
1381static int prt_portrait;
1382static int prt_num_copies;
1383static int prt_duplex;
1384static int prt_tumble;
1385static int prt_collate;
1386
1387/*
1388 * Buffers used when generating PostScript output
1389 */
1390static char_u prt_line_buffer[257];
1391static garray_T prt_ps_buffer;
1392
Bram Moolenaar81366db2005-07-24 21:16:51 +00001393static int prt_do_conv;
1394static vimconv_T prt_conv;
1395
1396static int prt_out_mbyte;
1397static int prt_custom_cmap;
1398static char prt_cmap[80];
1399static int prt_use_courier;
1400static int prt_in_ascii;
1401static int prt_half_width;
1402static char *prt_ascii_encoding;
1403static char_u prt_hexchar[] = "0123456789abcdef";
Bram Moolenaar81366db2005-07-24 21:16:51 +00001404
1405 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001406prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001407{
1408 if (!prt_file_error
1409 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1410 != (size_t)bytes)
1411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001412 emsg(_("E455: Error writing to PostScript output file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001413 prt_file_error = TRUE;
1414 }
1415}
1416
1417 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001418prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001419{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001420 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001421}
1422
1423 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001424prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001425{
1426#ifdef EBCDIC
1427 ebcdic2ascii(buffer, bytes);
1428#endif
1429 prt_write_file_raw_len(buffer, bytes);
1430}
1431
1432/*
1433 * Write a string.
1434 */
1435 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001436prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001437{
1438 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1439 prt_write_file(prt_line_buffer);
1440}
1441
1442/*
1443 * Write an int and a space.
1444 */
1445 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001446prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001447{
1448 sprintf((char *)prt_line_buffer, "%d ", i);
1449 prt_write_file(prt_line_buffer);
1450}
1451
1452/*
1453 * Write a boolean and a space.
1454 */
1455 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001456prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001457{
1458 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1459 prt_write_file(prt_line_buffer);
1460}
1461
1462/*
1463 * Write PostScript to re-encode and define the font.
1464 */
1465 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001466prt_def_font(
1467 char *new_name,
1468 char *encoding,
1469 int height,
1470 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001471{
1472 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1473 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1474 prt_write_file(prt_line_buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001475 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001476 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001477 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1478 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001479 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1480 "/%s %d /_%s ffs\n", new_name, height, new_name);
1481 prt_write_file(prt_line_buffer);
1482}
1483
Bram Moolenaar81366db2005-07-24 21:16:51 +00001484/*
1485 * Write a line to define the CID font.
1486 */
1487 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001488prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001489{
1490 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1491 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1492 prt_write_file(prt_line_buffer);
1493 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1494 "/%s %d /_%s ffs\n", new_name, height, new_name);
1495 prt_write_file(prt_line_buffer);
1496}
1497
1498/*
1499 * Write a line to define a duplicate of a CID font
1500 */
1501 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001502prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001503{
1504 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1505 "/%s %s d\n", new_name, original_name);
1506 prt_write_file(prt_line_buffer);
1507}
Bram Moolenaar81366db2005-07-24 21:16:51 +00001508
1509/*
1510 * Convert a real value into an integer and fractional part as integers, with
1511 * the fractional part being in the range [0,10^precision). The fractional part
1512 * is also rounded based on the precision + 1'th fractional digit.
1513 */
1514 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001515prt_real_bits(
1516 double real,
1517 int precision,
1518 int *pinteger,
1519 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001520{
1521 int i;
1522 int integer;
1523 float fraction;
1524
1525 integer = (int)real;
1526 fraction = (float)(real - integer);
1527 if (real < (double)integer)
1528 fraction = -fraction;
1529 for (i = 0; i < precision; i++)
1530 fraction *= 10.0;
1531
1532 *pinteger = integer;
1533 *pfraction = (int)(fraction + 0.5);
1534}
1535
1536/*
1537 * Write a real and a space. Save bytes if real value has no fractional part!
1538 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1539 * what decimal point character to use, but PS always requires a '.'.
1540 */
1541 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001542prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001543{
1544 int integer;
1545 int fraction;
1546
1547 prt_real_bits(val, prec, &integer, &fraction);
1548 /* Emit integer part */
1549 sprintf((char *)prt_line_buffer, "%d", integer);
1550 prt_write_file(prt_line_buffer);
1551 /* Only emit fraction if necessary */
1552 if (fraction != 0)
1553 {
1554 /* Remove any trailing zeros */
1555 while ((fraction % 10) == 0)
1556 {
1557 prec--;
1558 fraction /= 10;
1559 }
1560 /* Emit fraction left padded with zeros */
1561 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1562 prt_write_file(prt_line_buffer);
1563 }
1564 sprintf((char *)prt_line_buffer, " ");
1565 prt_write_file(prt_line_buffer);
1566}
1567
1568/*
1569 * Write a line to define a numeric variable.
1570 */
1571 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001572prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001573{
1574 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1575 "/%s ", name);
1576 prt_write_file(prt_line_buffer);
1577 prt_write_real(value, prec);
1578 sprintf((char *)prt_line_buffer, "d\n");
1579 prt_write_file(prt_line_buffer);
1580}
1581
1582/* Convert size from font space to user space at current font scale */
1583#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1584
1585 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001586prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001587{
1588 if (prt_ps_buffer.ga_len > 0)
1589 {
1590 /* Any background color must be drawn first */
1591 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1592 {
1593 int r, g, b;
1594
1595 if (prt_do_moveto)
1596 {
1597 prt_write_real(prt_pos_x_moveto, 2);
1598 prt_write_real(prt_pos_y_moveto, 2);
1599 prt_write_string("m\n");
1600 prt_do_moveto = FALSE;
1601 }
1602
1603 /* Size of rect of background color on which text is printed */
1604 prt_write_real(prt_text_run, 2);
1605 prt_write_real(prt_line_height, 2);
1606
1607 /* Lastly add the color of the background */
1608 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1609 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1610 b = prt_new_bgcol & 0xff;
1611 prt_write_real(r / 255.0, 3);
1612 prt_write_real(g / 255.0, 3);
1613 prt_write_real(b / 255.0, 3);
1614 prt_write_string("bg\n");
1615 }
1616 /* Draw underlines before the text as it makes it slightly easier to
1617 * find the starting point.
1618 */
1619 if (prt_do_underline)
1620 {
1621 if (prt_do_moveto)
1622 {
1623 prt_write_real(prt_pos_x_moveto, 2);
1624 prt_write_real(prt_pos_y_moveto, 2);
1625 prt_write_string("m\n");
1626 prt_do_moveto = FALSE;
1627 }
1628
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001629 /* Underline length of text run */
Bram Moolenaar81366db2005-07-24 21:16:51 +00001630 prt_write_real(prt_text_run, 2);
1631 prt_write_string("ul\n");
1632 }
1633 /* Draw the text
1634 * Note: we write text out raw - EBCDIC conversion is handled in the
1635 * PostScript world via the font encoding vector. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001636 if (prt_out_mbyte)
1637 prt_write_string("<");
1638 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001639 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001640 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001641 if (prt_out_mbyte)
1642 prt_write_string(">");
1643 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001644 prt_write_string(")");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001645 /* Add a moveto if need be and use the appropriate show procedure */
1646 if (prt_do_moveto)
1647 {
1648 prt_write_real(prt_pos_x_moveto, 2);
1649 prt_write_real(prt_pos_y_moveto, 2);
1650 /* moveto and a show */
1651 prt_write_string("ms\n");
1652 prt_do_moveto = FALSE;
1653 }
1654 else /* Simple show */
1655 prt_write_string("s\n");
1656
1657 ga_clear(&prt_ps_buffer);
1658 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1659 }
1660}
1661
1662
1663 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001664prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001665{
1666 char_u *resource_filename = cookie;
1667
1668 if (STRLEN(filename) >= MAXPATHL)
1669 *resource_filename = NUL;
1670 else
1671 STRCPY(resource_filename, filename);
1672}
1673
1674 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001675prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001676{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001677 char_u *buffer;
1678 int retval;
1679
1680 buffer = alloc(MAXPATHL + 1);
1681 if (buffer == NULL)
1682 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001683
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001684 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001685 /* Look for named resource file in runtimepath */
1686 STRCPY(buffer, "print");
1687 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001688 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1689 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001690 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001691 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001692 resource->filename)
1693 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001694 vim_free(buffer);
1695 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001696}
1697
1698/* PS CR and LF characters have platform independent values */
1699#define PSLF (0x0a)
1700#define PSCR (0x0d)
1701
1702/* Static buffer to read initial comments in a resource file, some can have a
1703 * couple of KB of comments! */
1704#define PRT_FILE_BUFFER_LEN (2048)
1705struct prt_resfile_buffer_S
1706{
1707 char_u buffer[PRT_FILE_BUFFER_LEN];
1708 int len;
1709 int line_start;
1710 int line_end;
1711};
1712
1713static struct prt_resfile_buffer_S prt_resfile;
1714
1715 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001716prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001717{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001718 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001719
1720 /* Move to start of next line and then find end of line */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 idx = prt_resfile.line_end + 1;
1722 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001723 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001724 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001725 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001727 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001729
Bram Moolenaar89d40322006-08-29 15:30:07 +00001730 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001731 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001732 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001733 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001735 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001737
Bram Moolenaar89d40322006-08-29 15:30:07 +00001738 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001739}
1740
1741 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001742prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001743{
1744 /* Force not equal if string is longer than remainder of line */
1745 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001746 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001747
1748 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001749 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001750}
1751
1752 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001753prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001754{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001756
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 idx = prt_resfile.line_start + offset;
1758 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001759 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 if (isspace(prt_resfile.buffer[idx]))
1761 return idx - prt_resfile.line_start;
1762 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001763 }
1764 return -1;
1765}
1766
1767 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001768prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001769{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001771
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 idx = prt_resfile.line_start + offset;
1773 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001774 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001775 if (!isspace(prt_resfile.buffer[idx]))
1776 return idx - prt_resfile.line_start;
1777 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001778 }
1779 return -1;
1780}
1781
1782/* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1783 * if a DSC comment is found, else false */
1784 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001785prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001786{
1787 int comment;
1788 int offset;
1789
1790 /* Move to start of next line */
1791 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001792 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001793
1794 /* DSC comments always start %% */
1795 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001796 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001797
1798 /* Find type of DSC comment */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001799 for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001800 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1801 prt_dsc_table[comment].len) == 0)
1802 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001803
1804 if (comment != NUM_ELEMENTS(prt_dsc_table))
1805 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001806 /* Return type of comment */
1807 p_dsc_line->type = prt_dsc_table[comment].type;
1808 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001809 }
1810 else
1811 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001812 /* Unrecognised DSC comment, skip to ws after comment leader */
1813 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1814 offset = prt_resfile_skip_nonws(0);
1815 if (offset == -1)
1816 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001817 }
1818
1819 /* Skip ws to comment value */
1820 offset = prt_resfile_skip_ws(offset);
1821 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001822 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001823
1824 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1825 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1826
1827 return TRUE;
1828}
1829
1830/* Improved hand crafted parser to get the type, title, and version number of a
1831 * PS resource file so the file details can be added to the DSC header comments.
1832 */
1833 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001834prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001835{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001836 int offset;
1837 int seen_all;
1838 int seen_title;
1839 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001840 FILE *fd_resource;
1841 struct prt_dsc_line_S dsc_line;
1842
1843 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1844 if (fd_resource == NULL)
1845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846 semsg(_("E624: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001847 return FALSE;
1848 }
1849 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1850
1851 /* Parse first line to ensure valid resource file */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001852 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001853 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001854 if (ferror(fd_resource))
1855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001856 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001857 resource->filename);
1858 fclose(fd_resource);
1859 return FALSE;
1860 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001861 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001862
1863 prt_resfile.line_end = -1;
1864 prt_resfile.line_start = 0;
1865 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001866 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001867
1868 offset = 0;
1869
1870 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001871 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001873 semsg(_("E618: file \"%s\" is not a PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001874 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001875 return FALSE;
1876 }
1877
1878 /* Skip over any version numbers and following ws */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001879 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001880 offset = prt_resfile_skip_nonws(offset);
1881 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001882 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001883 offset = prt_resfile_skip_ws(offset);
1884 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001885 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001886
1887 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001888 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001890 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001891 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001892 return FALSE;
1893 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001894 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001895
1896 /* Decide type of resource in the file */
1897 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001898 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001899 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1900 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001901 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001902 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1903 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001904 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001905 resource->type = PRT_RESOURCE_TYPE_CMAP;
1906 else
1907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001908 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001909 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001910 return FALSE;
1911 }
1912
1913 /* Look for title and version of resource */
1914 resource->title[0] = '\0';
1915 resource->version[0] = '\0';
1916 seen_title = FALSE;
1917 seen_version = FALSE;
1918 seen_all = FALSE;
1919 while (!seen_all && prt_next_dsc(&dsc_line))
1920 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001921 switch (dsc_line.type)
1922 {
1923 case PRT_DSC_TITLE_TYPE:
1924 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
1925 seen_title = TRUE;
1926 if (seen_version)
1927 seen_all = TRUE;
1928 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001929
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001930 case PRT_DSC_VERSION_TYPE:
1931 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
1932 seen_version = TRUE;
1933 if (seen_title)
1934 seen_all = TRUE;
1935 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001936
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001937 case PRT_DSC_ENDCOMMENTS_TYPE:
1938 /* Wont find title or resource after this comment, stop searching */
1939 seen_all = TRUE;
1940 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001941
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001942 case PRT_DSC_MISC_TYPE:
1943 /* Not interested in whatever comment this line had */
1944 break;
1945 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001946 }
1947
1948 if (!seen_title || !seen_version)
1949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001951 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001952 return FALSE;
1953 }
1954
Bram Moolenaar81366db2005-07-24 21:16:51 +00001955 return TRUE;
1956}
1957
1958 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001959prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001960{
1961 /* Version number m.n should match, the revision number does not matter */
1962 if (STRNCMP(resource->version, version, STRLEN(version)))
1963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001964 semsg(_("E621: \"%s\" resource file has wrong version"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001965 resource->name);
1966 return FALSE;
1967 }
1968
1969 /* Other checks to be added as needed */
1970 return TRUE;
1971}
1972
1973 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001974prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001975{
1976 prt_write_string("%!PS-Adobe-3.0\n");
1977}
1978
1979 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001980prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001981{
1982 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1983 "%%%%%s\n", comment);
1984 prt_write_file(prt_line_buffer);
1985}
1986
1987 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001988prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001989{
1990 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1991 "%%%%%s: %s\n", comment, text);
1992 prt_write_file(prt_line_buffer);
1993}
1994
1995 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001996prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001997{
1998 /* TODO - should scan 'text' for any chars needing escaping! */
1999 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2000 "%%%%%s: (%s)\n", comment, text);
2001 prt_write_file(prt_line_buffer);
2002}
2003
2004#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2005
2006 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002007prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002008{
2009 int i;
2010
2011 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2012 "%%%%%s:", comment);
2013 prt_write_file(prt_line_buffer);
2014
2015 for (i = 0; i < count; i++)
2016 {
2017 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2018 prt_write_file(prt_line_buffer);
2019 }
2020
2021 prt_write_string("\n");
2022}
2023
2024 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002025prt_dsc_resources(
2026 char *comment, /* if NULL add to previous */
2027 char *type,
2028 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002029{
2030 if (comment != NULL)
2031 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2032 "%%%%%s: %s", comment, type);
2033 else
2034 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2035 "%%%%+ %s", type);
2036 prt_write_file(prt_line_buffer);
2037
2038 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2039 " %s\n", string);
2040 prt_write_file(prt_line_buffer);
2041}
2042
2043 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002044prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002045{
2046 int i;
2047
2048 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002049 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002050 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002051 if (ps_font->ps_fontname[i] != NULL)
2052 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002053}
2054
2055 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002056prt_dsc_requirements(
2057 int duplex,
2058 int tumble,
2059 int collate,
2060 int color,
2061 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002062{
2063 /* Only output the comment if we need to.
2064 * Note: tumble is ignored if we are not duplexing
2065 */
2066 if (!(duplex || collate || color || (num_copies > 1)))
2067 return;
2068
2069 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2070 prt_write_file(prt_line_buffer);
2071
2072 if (duplex)
2073 {
2074 prt_write_string(" duplex");
2075 if (tumble)
2076 prt_write_string("(tumble)");
2077 }
2078 if (collate)
2079 prt_write_string(" collate");
2080 if (color)
2081 prt_write_string(" color");
2082 if (num_copies > 1)
2083 {
2084 prt_write_string(" numcopies(");
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002085 /* Note: no space wanted so don't use prt_write_int() */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002086 sprintf((char *)prt_line_buffer, "%d", num_copies);
2087 prt_write_file(prt_line_buffer);
2088 prt_write_string(")");
2089 }
2090 prt_write_string("\n");
2091}
2092
2093 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002094prt_dsc_docmedia(
2095 char *paper_name,
2096 double width,
2097 double height,
2098 double weight,
2099 char *colour,
2100 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002101{
2102 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2103 "%%%%DocumentMedia: %s ", paper_name);
2104 prt_write_file(prt_line_buffer);
2105 prt_write_real(width, 2);
2106 prt_write_real(height, 2);
2107 prt_write_real(weight, 2);
2108 if (colour == NULL)
2109 prt_write_string("()");
2110 else
2111 prt_write_string(colour);
2112 prt_write_string(" ");
2113 if (type == NULL)
2114 prt_write_string("()");
2115 else
2116 prt_write_string(type);
2117 prt_write_string("\n");
2118}
2119
2120 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002121mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002122{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002123 if (prt_out_mbyte)
2124 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002125 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002126
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002127 /* Free off all CID font names created, but first clear duplicate
2128 * pointers to the same string (when the same font is used for more than
2129 * one style).
2130 */
2131 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2132 {
2133 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002134 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002135 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002136 }
2137
2138 if (prt_do_conv)
2139 {
2140 convert_setup(&prt_conv, NULL, NULL);
2141 prt_do_conv = FALSE;
2142 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002143 if (prt_ps_fd != NULL)
2144 {
2145 fclose(prt_ps_fd);
2146 prt_ps_fd = NULL;
2147 prt_file_error = FALSE;
2148 }
2149 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002150 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002151}
2152
2153 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002154to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002155{
2156 float ret;
2157 int u;
2158 int nr;
2159
2160 u = prt_get_unit(idx);
2161 if (u == PRT_UNIT_NONE)
2162 {
2163 u = PRT_UNIT_PERC;
2164 nr = def_number;
2165 }
2166 else
2167 nr = printer_opts[idx].number;
2168
2169 switch (u)
2170 {
2171 case PRT_UNIT_INCH:
2172 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2173 break;
2174 case PRT_UNIT_MM:
2175 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2176 break;
2177 case PRT_UNIT_POINT:
2178 ret = (float)nr;
2179 break;
2180 case PRT_UNIT_PERC:
2181 default:
2182 ret = (float)(physsize * nr) / 100;
2183 break;
2184 }
2185
2186 return ret;
2187}
2188
2189/*
2190 * Calculate margins for given width and height from printoptions settings.
2191 */
2192 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002193prt_page_margins(
2194 double width,
2195 double height,
2196 double *left,
2197 double *right,
2198 double *top,
2199 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002200{
2201 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2202 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2203 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2204 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2205}
2206
2207 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002208prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002209{
2210 prt_line_height = (float)font_scale;
2211 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2212}
2213
2214
2215 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002216prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002217{
2218 if (prt_use_number())
2219 {
2220 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002221 /* If we are outputting multi-byte characters then line numbers will be
2222 * printed with half width characters
2223 */
2224 if (prt_out_mbyte)
2225 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002226 prt_left_margin += prt_number_width;
2227 }
2228 else
2229 prt_number_width = 0.0;
2230
2231 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2232}
2233
Bram Moolenaar81366db2005-07-24 21:16:51 +00002234 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002235prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002236{
2237 char *fontname;
2238
2239 fontname = (char *)alloc(name_len + 1);
2240 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002241 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002242 vim_strncpy((char_u *)fontname, name, name_len);
2243 prt_ps_mb_font.ps_fontname[font] = fontname;
2244
2245 return TRUE;
2246}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002247
2248/*
2249 * Get number of lines of text that fit on a page (excluding the header).
2250 */
2251 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002252prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002253{
2254 int lpp;
2255
2256 /*
2257 * Calculate offset to lower left corner of background rect based on actual
2258 * font height (based on its bounding box) and the line height, handling the
2259 * case where the font height can exceed the line height.
2260 */
2261 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2262 prt_ps_font->bbox_min_y);
2263 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2264 {
2265 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2266 (1000.0 - (prt_ps_font->bbox_max_y -
2267 prt_ps_font->bbox_min_y)) / 2);
2268 }
2269
2270 /* Get height for topmost line based on background rect offset. */
2271 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2272
2273 /* Calculate lpp */
2274 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2275
2276 /* Adjust top margin if there is a header */
2277 prt_top_margin -= prt_line_height * prt_header_height();
2278
2279 return lpp - prt_header_height();
2280}
2281
Bram Moolenaar81366db2005-07-24 21:16:51 +00002282 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002283prt_match_encoding(
2284 char *p_encoding,
2285 struct prt_ps_mbfont_S *p_cmap,
2286 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002287{
2288 int mbenc;
2289 int enc_len;
2290 struct prt_ps_encoding_S *p_mbenc;
2291
2292 *pp_mbenc = NULL;
2293 /* Look for recognised encoding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002294 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002295 p_mbenc = p_cmap->encodings;
2296 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2297 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002298 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2299 {
2300 *pp_mbenc = p_mbenc;
2301 return TRUE;
2302 }
2303 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002304 }
2305 return FALSE;
2306}
2307
2308 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002309prt_match_charset(
2310 char *p_charset,
2311 struct prt_ps_mbfont_S *p_cmap,
2312 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002313{
2314 int mbchar;
2315 int char_len;
2316 struct prt_ps_charset_S *p_mbchar;
2317
2318 /* Look for recognised character set, using default if one is not given */
2319 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002320 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002321 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002322 p_mbchar = p_cmap->charsets;
2323 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2324 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002325 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2326 {
2327 *pp_mbchar = p_mbchar;
2328 return TRUE;
2329 }
2330 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002331 }
2332 return FALSE;
2333}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002334
Bram Moolenaar81366db2005-07-24 21:16:51 +00002335 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002336mch_print_init(
2337 prt_settings_T *psettings,
2338 char_u *jobname,
2339 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002340{
2341 int i;
2342 char *paper_name;
2343 int paper_strlen;
2344 int fontsize;
2345 char_u *p;
2346 double left;
2347 double right;
2348 double top;
2349 double bottom;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002350 int props;
2351 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002352 char_u *p_encoding;
2353 struct prt_ps_encoding_S *p_mbenc;
2354 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002355 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002356
2357#if 0
2358 /*
2359 * TODO:
2360 * If "forceit" is false: pop up a dialog to select:
2361 * - printer name
2362 * - copies
2363 * - collated/uncollated
2364 * - duplex off/long side/short side
2365 * - paper size
2366 * - portrait/landscape
2367 * - font size
2368 *
2369 * If "forceit" is true: use the default printer and settings
2370 */
2371 if (forceit)
2372 s_pd.Flags |= PD_RETURNDEFAULT;
2373#endif
2374
2375 /*
2376 * Set up font and encoding.
2377 */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002378 p_encoding = enc_skip(p_penc);
2379 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002380 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002381
Bram Moolenaar14716812006-05-04 21:54:08 +00002382 /* Look for a multi-byte font that matches the encoding and character set.
2383 * Only look if multi-byte character set is defined, or using multi-byte
2384 * encoding other than Unicode. This is because a Unicode encoding does not
2385 * uniquely identify a CJK character set to use. */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002386 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002387 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002388 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002389 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002390 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002391
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002392 p_mbenc_first = NULL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002393 for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002394 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002395 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002396 {
2397 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002398 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002399 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002400 cmap_first = cmap;
2401 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002402 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002403 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002404 break;
2405 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002406
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002407 /* Use first encoding matched if no charset matched */
2408 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002409 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002410 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002411 cmap = cmap_first;
2412 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002413 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002414
2415 prt_out_mbyte = (p_mbenc != NULL);
2416 if (prt_out_mbyte)
2417 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002418 /* Build CMap name - will be same for all multi-byte fonts used */
2419 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002420
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002421 prt_custom_cmap = (p_mbchar == NULL);
2422 if (!prt_custom_cmap)
2423 {
2424 /* Check encoding and character set are compatible */
2425 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002427 emsg(_("E673: Incompatible multi-byte encoding and character set."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002428 return FALSE;
2429 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002430
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002431 /* Add charset name if not empty */
2432 if (p_mbchar->cmap_charset != NULL)
2433 {
2434 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002435 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002436 STRCAT(prt_cmap, "-");
2437 }
2438 }
2439 else
2440 {
2441 /* Add custom CMap character set name */
2442 if (*p_pmcs == NUL)
2443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002444 emsg(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002445 return FALSE;
2446 }
2447 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2448 STRCAT(prt_cmap, "-");
2449 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002450
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002451 /* CMap name ends with (optional) encoding name and -H for horizontal */
2452 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002453 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002454 {
2455 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2456 STRCAT(prt_cmap, "-");
2457 }
2458 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002459
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002460 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002462 emsg(_("E675: No default font specified for multi-byte printing."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002463 return FALSE;
2464 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002465
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002466 /* Derive CID font names with fallbacks if not defined */
2467 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2468 mbfont_opts[OPT_MBFONT_REGULAR].string,
2469 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2470 return FALSE;
2471 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2472 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2473 mbfont_opts[OPT_MBFONT_BOLD].string,
2474 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2475 return FALSE;
2476 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2477 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2478 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2479 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2480 return FALSE;
2481 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2482 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002483 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2484 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002485 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002486
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002487 /* Check if need to use Courier for ASCII code range, and if so pick up
2488 * the encoding to use */
2489 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2490 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2491 if (prt_use_courier)
2492 {
2493 /* Use national ASCII variant unless ASCII wanted */
2494 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2495 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2496 prt_ascii_encoding = "ascii";
2497 else
2498 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2499 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002500
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002501 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002502 }
2503 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00002504 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002505 prt_use_courier = FALSE;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002506 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002507 }
2508
2509 /*
2510 * Find the size of the paper and set the margins.
2511 */
2512 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2513 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2514 if (printer_opts[OPT_PRINT_PAPER].present)
2515 {
2516 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2517 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2518 }
2519 else
2520 {
2521 paper_name = "A4";
2522 paper_strlen = 2;
2523 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002524 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002525 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2526 && STRNICMP(prt_mediasize[i].name, paper_name,
2527 paper_strlen) == 0)
2528 break;
2529 if (i == PRT_MEDIASIZE_LEN)
2530 i = 0;
2531 prt_media = i;
2532
2533 /*
2534 * Set PS pagesize based on media dimensions and print orientation.
2535 * Note: Media and page sizes have defined meanings in PostScript and should
2536 * be kept distinct. Media is the paper (or transparency, or ...) that is
2537 * printed on, whereas the page size is the area that the PostScript
2538 * interpreter renders into.
2539 */
2540 if (prt_portrait)
2541 {
2542 prt_page_width = prt_mediasize[i].width;
2543 prt_page_height = prt_mediasize[i].height;
2544 }
2545 else
2546 {
2547 prt_page_width = prt_mediasize[i].height;
2548 prt_page_height = prt_mediasize[i].width;
2549 }
2550
2551 /*
2552 * Set PS page margins based on the PS pagesize, not the mediasize - this
2553 * needs to be done before the cpl and lpp are calculated.
2554 */
2555 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2556 &bottom);
2557 prt_left_margin = (float)left;
2558 prt_right_margin = (float)right;
2559 prt_top_margin = (float)top;
2560 prt_bottom_margin = (float)bottom;
2561
2562 /*
2563 * Set up the font size.
2564 */
2565 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2566 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2567 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2568 fontsize = atoi((char *)p + 2);
2569 prt_font_metrics(fontsize);
2570
2571 /*
2572 * Return the number of characters per line, and lines per page for the
2573 * generic print code.
2574 */
2575 psettings->chars_per_line = prt_get_cpl();
2576 psettings->lines_per_page = prt_get_lpp();
2577
2578 /* Catch margin settings that leave no space for output! */
2579 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2580 return FAIL;
2581
2582 /*
2583 * Sort out the number of copies to be printed. PS by default will do
2584 * uncollated copies for you, so once we know how many uncollated copies are
2585 * wanted cache it away and lie to the generic code that we only want one
2586 * uncollated copy.
2587 */
2588 psettings->n_collated_copies = 1;
2589 psettings->n_uncollated_copies = 1;
2590 prt_num_copies = 1;
2591 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2592 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2593 if (prt_collate)
2594 {
2595 /* TODO: Get number of collated copies wanted. */
2596 psettings->n_collated_copies = 1;
2597 }
2598 else
2599 {
2600 /* TODO: Get number of uncollated copies wanted and update the cached
2601 * count.
2602 */
2603 prt_num_copies = 1;
2604 }
2605
2606 psettings->jobname = jobname;
2607
2608 /*
2609 * Set up printer duplex and tumble based on Duplex option setting - default
2610 * is long sided duplex printing (i.e. no tumble).
2611 */
2612 prt_duplex = TRUE;
2613 prt_tumble = FALSE;
2614 psettings->duplex = 1;
2615 if (printer_opts[OPT_PRINT_DUPLEX].present)
2616 {
2617 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2618 {
2619 prt_duplex = FALSE;
2620 psettings->duplex = 0;
2621 }
2622 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2623 == 0)
2624 prt_tumble = TRUE;
2625 }
2626
2627 /* For now user abort not supported */
2628 psettings->user_abort = 0;
2629
2630 /* If the user didn't specify a file name, use a temp file. */
2631 if (psettings->outfile == NULL)
2632 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002633 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002634 if (prt_ps_file_name == NULL)
2635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 emsg(_(e_notmp));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002637 return FAIL;
2638 }
2639 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2640 }
2641 else
2642 {
2643 p = expand_env_save(psettings->outfile);
2644 if (p != NULL)
2645 {
2646 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2647 vim_free(p);
2648 }
2649 }
2650 if (prt_ps_fd == NULL)
2651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002652 emsg(_("E324: Can't open PostScript output file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002653 mch_print_cleanup();
2654 return FAIL;
2655 }
2656
2657 prt_bufsiz = psettings->chars_per_line;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002658 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002659 prt_bufsiz *= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002660 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2661
2662 prt_page_num = 0;
2663
2664 prt_attribute_change = FALSE;
2665 prt_need_moveto = FALSE;
2666 prt_need_font = FALSE;
2667 prt_need_fgcol = FALSE;
2668 prt_need_bgcol = FALSE;
2669 prt_need_underline = FALSE;
2670
2671 prt_file_error = FALSE;
2672
2673 return OK;
2674}
2675
2676 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002677prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002678{
2679 FILE* fd_resource;
2680 char_u resource_buffer[512];
2681 size_t bytes_read;
2682
2683 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2684 if (fd_resource == NULL)
2685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002686 semsg(_("E456: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002687 return FALSE;
2688 }
2689 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2690 (char *)resource->title);
2691
2692 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2693
2694 for (;;)
2695 {
2696 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2697 sizeof(resource_buffer), fd_resource);
2698 if (ferror(fd_resource))
2699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002700 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002701 resource->filename);
2702 fclose(fd_resource);
2703 return FALSE;
2704 }
2705 if (bytes_read == 0)
2706 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002707 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002708 if (prt_file_error)
2709 {
2710 fclose(fd_resource);
2711 return FALSE;
2712 }
2713 }
2714 fclose(fd_resource);
2715
2716 prt_dsc_noarg("EndDocument");
2717
2718 prt_dsc_noarg("EndResource");
2719
2720 return TRUE;
2721}
2722
2723 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002724mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002725{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002726 int bbox[4];
Bram Moolenaar81366db2005-07-24 21:16:51 +00002727 double left;
2728 double right;
2729 double top;
2730 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002731 struct prt_ps_resource_S *res_prolog;
2732 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002733 char buffer[256];
2734 char_u *p_encoding;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002735 struct prt_ps_resource_S *res_cidfont;
2736 struct prt_ps_resource_S *res_cmap;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002737 int retval = FALSE;
2738
2739 res_prolog = (struct prt_ps_resource_S *)
2740 alloc(sizeof(struct prt_ps_resource_S));
2741 res_encoding = (struct prt_ps_resource_S *)
2742 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002743 res_cidfont = (struct prt_ps_resource_S *)
2744 alloc(sizeof(struct prt_ps_resource_S));
2745 res_cmap = (struct prt_ps_resource_S *)
2746 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002747 if (res_prolog == NULL || res_encoding == NULL
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002748 || res_cidfont == NULL || res_cmap == NULL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002749 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002750
2751 /*
2752 * PS DSC Header comments - no PS code!
2753 */
2754 prt_dsc_start();
2755 prt_dsc_textline("Title", (char *)psettings->jobname);
2756 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002757 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002758 prt_dsc_textline("For", buffer);
2759 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2760 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
Bram Moolenaar63d25552019-05-10 21:28:38 +02002761
2762 prt_dsc_textline("CreationDate", get_ctime(time(NULL), FALSE));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002763 prt_dsc_textline("DocumentData", "Clean8Bit");
2764 prt_dsc_textline("Orientation", "Portrait");
2765 prt_dsc_atend("Pages");
2766 prt_dsc_textline("PageOrder", "Ascend");
2767 /* The bbox does not change with orientation - it is always in the default
2768 * user coordinate system! We have to recalculate right and bottom
2769 * coordinates based on the font metrics for the bbox to be accurate. */
2770 prt_page_margins(prt_mediasize[prt_media].width,
2771 prt_mediasize[prt_media].height,
2772 &left, &right, &top, &bottom);
2773 bbox[0] = (int)left;
2774 if (prt_portrait)
2775 {
2776 /* In portrait printing the fixed point is the top left corner so we
2777 * derive the bbox from that point. We have the expected cpl chars
2778 * across the media and lpp lines down the media.
2779 */
2780 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2781 * prt_line_height);
2782 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2783 + 0.5);
2784 bbox[3] = (int)(top + 0.5);
2785 }
2786 else
2787 {
2788 /* In landscape printing the fixed point is the bottom left corner so we
2789 * derive the bbox from that point. We have lpp chars across the media
2790 * and cpl lines up the media.
2791 */
2792 bbox[1] = (int)bottom;
2793 bbox[2] = (int)(left + ((psettings->lines_per_page
2794 + prt_header_height()) * prt_line_height) + 0.5);
2795 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2796 + 0.5);
2797 }
2798 prt_dsc_ints("BoundingBox", 4, bbox);
2799 /* The media width and height does not change with landscape printing! */
2800 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2801 prt_mediasize[prt_media].width,
2802 prt_mediasize[prt_media].height,
2803 (double)0, NULL, NULL);
2804 /* Define fonts needed */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002805 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002806 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002807 if (prt_out_mbyte)
2808 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002809 prt_dsc_font_resource((prt_use_courier ? NULL
2810 : "DocumentNeededResources"), &prt_ps_mb_font);
2811 if (!prt_custom_cmap)
2812 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002813 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002814
2815 /* Search for external resources VIM supplies */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002816 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002818 emsg(_("E456: Can't find PostScript resource file \"prolog.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002819 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002820 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002821 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002822 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002823 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002824 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002825 if (prt_out_mbyte)
2826 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002827 /* Look for required version of multi-byte printing procset */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002828 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002830 emsg(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002831 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002832 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002833 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002834 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002835 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002836 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002837 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002838
2839 /* Find an encoding to use for printing.
2840 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2841 * that cannot be found then default to "latin1".
2842 * Note: VIM specific encoding header is always skipped.
2843 */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002844 if (!prt_out_mbyte)
2845 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002846 p_encoding = enc_skip(p_penc);
2847 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002848 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002849 {
2850 /* 'printencoding' not set or not supported - find alternate */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002851 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002852
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002853 p_encoding = enc_skip(p_enc);
2854 props = enc_canon_props(p_encoding);
2855 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002856 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002857 /* 8-bit 'encoding' is not supported */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002858 {
2859 /* Use latin1 as default printing encoding */
2860 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002861 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002863 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002864 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002865 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002866 }
2867 }
2868 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002869 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002870 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002871 /* For the moment there are no checks on encoding resource files to
2872 * perform */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002873 }
2874 else
2875 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002876 p_encoding = enc_skip(p_penc);
2877 if (*p_encoding == NUL)
2878 p_encoding = enc_skip(p_enc);
2879 if (prt_use_courier)
2880 {
2881 /* Include ASCII range encoding vector */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002882 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002884 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002885 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002886 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002887 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002888 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002889 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002890 /* For the moment there are no checks on encoding resource files to
2891 * perform */
2892 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002893 }
2894
2895 prt_conv.vc_type = CONV_NONE;
2896 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002897 /* Set up encoding conversion if required */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002898 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
2899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002900 semsg(_("E620: Unable to convert to print encoding \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002901 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002902 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002903 }
2904 prt_do_conv = TRUE;
2905 }
2906 prt_do_conv = prt_conv.vc_type != CONV_NONE;
2907
2908 if (prt_out_mbyte && prt_custom_cmap)
2909 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002910 /* Find user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002911 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002913 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002914 prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002915 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002916 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002917 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002918 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002919 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002920
2921 /* List resources supplied */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002922 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002923 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002924 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002925 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002926 if (prt_out_mbyte)
2927 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002928 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002929 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002930 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002931 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002932
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002933 if (prt_custom_cmap)
2934 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002935 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002936 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002937 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002938 prt_dsc_resources(NULL, "cmap", buffer);
2939 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002940 }
2941 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002942 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002943 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002944 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002945 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002946 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002947 }
2948 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
2949#ifdef FEAT_SYN_HL
2950 psettings->do_syntax
2951#else
2952 0
2953#endif
2954 , prt_num_copies);
2955 prt_dsc_noarg("EndComments");
2956
2957 /*
2958 * PS Document page defaults
2959 */
2960 prt_dsc_noarg("BeginDefaults");
2961
2962 /* List font resources most likely common to all pages */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002963 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002964 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002965 if (prt_out_mbyte)
2966 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002967 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
2968 &prt_ps_mb_font);
2969 if (!prt_custom_cmap)
2970 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002971 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002972
2973 /* Paper will be used for all pages */
2974 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
2975
2976 prt_dsc_noarg("EndDefaults");
2977
2978 /*
2979 * PS Document prolog inclusion - all required procsets.
2980 */
2981 prt_dsc_noarg("BeginProlog");
2982
2983 /* Add required procsets - NOTE: order is important! */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002984 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002985 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002986 if (prt_out_mbyte)
2987 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002988 /* Add CID font procset, and any user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002989 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002990 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002991 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002992 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002993 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002994
Bram Moolenaar81366db2005-07-24 21:16:51 +00002995 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002996 /* There will be only one Roman font encoding to be included in the PS
2997 * file. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002998 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002999 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003000
3001 prt_dsc_noarg("EndProlog");
3002
3003 /*
3004 * PS Document setup - must appear after the prolog
3005 */
3006 prt_dsc_noarg("BeginSetup");
3007
3008 /* Device setup - page size and number of uncollated copies */
3009 prt_write_int((int)prt_mediasize[prt_media].width);
3010 prt_write_int((int)prt_mediasize[prt_media].height);
3011 prt_write_int(0);
3012 prt_write_string("sps\n");
3013 prt_write_int(prt_num_copies);
3014 prt_write_string("nc\n");
3015 prt_write_boolean(prt_duplex);
3016 prt_write_boolean(prt_tumble);
3017 prt_write_string("dt\n");
3018 prt_write_boolean(prt_collate);
3019 prt_write_string("c\n");
3020
3021 /* Font resource inclusion and definition */
Bram Moolenaar81366db2005-07-24 21:16:51 +00003022 if (!prt_out_mbyte || prt_use_courier)
3023 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003024 /* When using Courier for ASCII range when printing multi-byte, need to
3025 * pick up ASCII encoding to use with it. */
3026 if (prt_use_courier)
3027 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003028 prt_dsc_resources("IncludeResource", "font",
3029 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3030 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3031 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3032 prt_dsc_resources("IncludeResource", "font",
3033 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3034 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3035 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3036 prt_dsc_resources("IncludeResource", "font",
3037 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3038 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3039 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3040 prt_dsc_resources("IncludeResource", "font",
3041 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3042 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3043 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003044 }
3045 if (prt_out_mbyte)
3046 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003047 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3048 * not have an italic form being a western style, so where no font is
3049 * defined for these faces VIM falls back to an existing face.
3050 * Note: if using Courier for the ASCII range then the printout will
3051 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3052 */
3053 prt_dsc_resources("IncludeResource", "font",
3054 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3055 if (!prt_custom_cmap)
3056 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3057 prt_def_cidfont("CF0", (int)prt_line_height,
3058 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003059
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003060 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3061 {
3062 prt_dsc_resources("IncludeResource", "font",
3063 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3064 if (!prt_custom_cmap)
3065 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3066 prt_def_cidfont("CF1", (int)prt_line_height,
3067 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3068 }
3069 else
3070 /* Use ROMAN for BOLD */
3071 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003072
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003073 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3074 {
3075 prt_dsc_resources("IncludeResource", "font",
3076 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3077 if (!prt_custom_cmap)
3078 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3079 prt_def_cidfont("CF2", (int)prt_line_height,
3080 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3081 }
3082 else
3083 /* Use ROMAN for OBLIQUE */
3084 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003086 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3087 {
3088 prt_dsc_resources("IncludeResource", "font",
3089 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3090 if (!prt_custom_cmap)
3091 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3092 prt_def_cidfont("CF3", (int)prt_line_height,
3093 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3094 }
3095 else
3096 /* Use BOLD for BOLDOBLIQUE */
3097 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003098 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003099
3100 /* Misc constant vars used for underlining and background rects */
3101 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3102 prt_ps_font->uline_offset), 2);
3103 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3104 prt_ps_font->uline_width), 2);
3105 prt_def_var("BO", prt_bgcol_offset, 2);
3106
3107 prt_dsc_noarg("EndSetup");
3108
3109 /* Fail if any problems writing out to the PS file */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003110 retval = !prt_file_error;
3111
3112theend:
3113 vim_free(res_prolog);
3114 vim_free(res_encoding);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003115 vim_free(res_cidfont);
3116 vim_free(res_cmap);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003117
3118 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003119}
3120
3121 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003122mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003123{
3124 prt_dsc_noarg("Trailer");
3125
3126 /*
3127 * Output any info we don't know in toto until we finish
3128 */
3129 prt_dsc_ints("Pages", 1, &prt_page_num);
3130
3131 prt_dsc_noarg("EOF");
3132
3133 /* Write CTRL-D to close serial communication link if used.
3134 * NOTHING MUST BE WRITTEN AFTER THIS! */
3135 prt_write_file((char_u *)IF_EB("\004", "\067"));
3136
3137 if (!prt_file_error && psettings->outfile == NULL
3138 && !got_int && !psettings->user_abort)
3139 {
3140 /* Close the file first. */
3141 if (prt_ps_fd != NULL)
3142 {
3143 fclose(prt_ps_fd);
3144 prt_ps_fd = NULL;
3145 }
3146 prt_message((char_u *)_("Sending to printer..."));
3147
3148 /* Not printing to a file: use 'printexpr' to print the file. */
3149 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003150 emsg(_("E365: Failed to print PostScript file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003151 else
3152 prt_message((char_u *)_("Print job sent."));
3153 }
3154
3155 mch_print_cleanup();
3156}
3157
3158 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003159mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003160{
3161 prt_flush_buffer();
3162
3163 prt_write_string("re sp\n");
3164
3165 prt_dsc_noarg("PageTrailer");
3166
3167 return !prt_file_error;
3168}
3169
Bram Moolenaar81366db2005-07-24 21:16:51 +00003170 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003171mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003172{
3173 int page_num[2];
3174
3175 prt_page_num++;
3176
3177 page_num[0] = page_num[1] = prt_page_num;
3178 prt_dsc_ints("Page", 2, page_num);
3179
3180 prt_dsc_noarg("BeginPageSetup");
3181
3182 prt_write_string("sv\n0 g\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003183 prt_in_ascii = !prt_out_mbyte;
3184 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003185 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003186 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003187 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003188 prt_fgcol = PRCOLOR_BLACK;
3189 prt_bgcol = PRCOLOR_WHITE;
3190 prt_font = PRT_PS_FONT_ROMAN;
3191
3192 /* Set up page transformation for landscape printing. */
3193 if (!prt_portrait)
3194 {
3195 prt_write_int(-((int)prt_mediasize[prt_media].width));
3196 prt_write_string("sl\n");
3197 }
3198
3199 prt_dsc_noarg("EndPageSetup");
3200
3201 /* We have reset the font attributes, force setting them again. */
3202 curr_bg = (long_u)0xffffffff;
3203 curr_fg = (long_u)0xffffffff;
3204 curr_bold = MAYBE;
3205
3206 return !prt_file_error;
3207}
3208
3209 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003210mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003211{
3212 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3213}
3214
3215static float prt_pos_x = 0;
3216static float prt_pos_y = 0;
3217
3218 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003219mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003220{
3221 prt_pos_x = prt_left_margin;
3222 if (margin)
3223 prt_pos_x -= prt_number_width;
3224
3225 prt_pos_y = prt_top_margin - prt_first_line_height -
3226 page_line * prt_line_height;
3227
3228 prt_attribute_change = TRUE;
3229 prt_need_moveto = TRUE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003230 prt_half_width = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003231}
3232
Bram Moolenaar81366db2005-07-24 21:16:51 +00003233 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003234mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003235{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003236 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003237 int need_break;
3238 char_u ch;
3239 char_u ch_buff[8];
3240 float char_width;
3241 float next_pos;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003242 int in_ascii;
3243 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003244 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003245
3246 char_width = prt_char_width;
3247
Bram Moolenaar81366db2005-07-24 21:16:51 +00003248 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3249 * CJKV font to do what VIM is doing here - use a Roman font for characters
Bram Moolenaarb15c8332007-05-10 18:40:02 +00003250 * in the ASCII range, and the original CID font for everything else.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003251 * The problem is that GhostScript still (as of 8.13) does not support
3252 * rearranged fonts even though they have been documented by Adobe for 7
3253 * years! If they ever do, a lot of this code will disappear.
3254 */
3255 if (prt_use_courier)
3256 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003257 in_ascii = (len == 1 && *p < 0x80);
3258 if (prt_in_ascii)
3259 {
3260 if (!in_ascii)
3261 {
3262 /* No longer in ASCII range - need to switch font */
3263 prt_in_ascii = FALSE;
3264 prt_need_font = TRUE;
3265 prt_attribute_change = TRUE;
3266 }
3267 }
3268 else if (in_ascii)
3269 {
3270 /* Now in ASCII range - need to switch font */
3271 prt_in_ascii = TRUE;
3272 prt_need_font = TRUE;
3273 prt_attribute_change = TRUE;
3274 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003275 }
3276 if (prt_out_mbyte)
3277 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003278 half_width = ((*mb_ptr2cells)(p) == 1);
3279 if (half_width)
3280 char_width /= 2;
3281 if (prt_half_width)
3282 {
3283 if (!half_width)
3284 {
3285 prt_half_width = FALSE;
3286 prt_pos_x += prt_char_width/4;
3287 prt_need_moveto = TRUE;
3288 prt_attribute_change = TRUE;
3289 }
3290 }
3291 else if (half_width)
3292 {
3293 prt_half_width = TRUE;
3294 prt_pos_x += prt_char_width/4;
3295 prt_need_moveto = TRUE;
3296 prt_attribute_change = TRUE;
3297 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003298 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003299
3300 /* Output any required changes to the graphics state, after flushing any
3301 * text buffered so far.
3302 */
3303 if (prt_attribute_change)
3304 {
3305 prt_flush_buffer();
3306 /* Reset count of number of chars that will be printed */
3307 prt_text_run = 0;
3308
3309 if (prt_need_moveto)
3310 {
3311 prt_pos_x_moveto = prt_pos_x;
3312 prt_pos_y_moveto = prt_pos_y;
3313 prt_do_moveto = TRUE;
3314
3315 prt_need_moveto = FALSE;
3316 }
3317 if (prt_need_font)
3318 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003319 if (!prt_in_ascii)
3320 prt_write_string("CF");
3321 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003322 prt_write_string("F");
3323 prt_write_int(prt_font);
3324 prt_write_string("sf\n");
3325 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003326 }
3327 if (prt_need_fgcol)
3328 {
3329 int r, g, b;
3330 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3331 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3332 b = prt_fgcol & 0xff;
3333
3334 prt_write_real(r / 255.0, 3);
3335 if (r == g && g == b)
3336 prt_write_string("g\n");
3337 else
3338 {
3339 prt_write_real(g / 255.0, 3);
3340 prt_write_real(b / 255.0, 3);
3341 prt_write_string("r\n");
3342 }
3343 prt_need_fgcol = FALSE;
3344 }
3345
3346 if (prt_bgcol != PRCOLOR_WHITE)
3347 {
3348 prt_new_bgcol = prt_bgcol;
3349 if (prt_need_bgcol)
3350 prt_do_bgcol = TRUE;
3351 }
3352 else
3353 prt_do_bgcol = FALSE;
3354 prt_need_bgcol = FALSE;
3355
3356 if (prt_need_underline)
3357 prt_do_underline = prt_underline;
3358 prt_need_underline = FALSE;
3359
3360 prt_attribute_change = FALSE;
3361 }
3362
Bram Moolenaar81366db2005-07-24 21:16:51 +00003363 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003364 {
Bram Moolenaar81366db2005-07-24 21:16:51 +00003365 /* Convert from multi-byte to 8-bit encoding */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003366 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003367 if (p == NULL)
3368 {
3369 p = (char_u *)"";
3370 len = 0;
3371 }
3372 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003373
3374 if (prt_out_mbyte)
3375 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003376 /* Multi-byte character strings are represented more efficiently as hex
3377 * strings when outputting clean 8 bit PS.
3378 */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003379 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003380 {
3381 ch = prt_hexchar[(unsigned)(*p) >> 4];
3382 ga_append(&prt_ps_buffer, ch);
3383 ch = prt_hexchar[(*p) & 0xf];
3384 ga_append(&prt_ps_buffer, ch);
3385 p++;
3386 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003387 }
3388 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003389 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003390 /* Add next character to buffer of characters to output.
3391 * Note: One printed character may require several PS characters to
3392 * represent it, but we only count them as one printed character.
3393 */
3394 ch = *p;
3395 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3396 {
3397 /* Convert non-printing characters to either their escape or octal
3398 * sequence, ensures PS sent over a serial line does not interfere
3399 * with the comms protocol. Note: For EBCDIC we need to write out
3400 * the escape sequences as ASCII codes!
Bram Moolenaar81366db2005-07-24 21:16:51 +00003401 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3402 */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003403 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3404 switch (ch)
3405 {
3406 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3407 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3408 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3409 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3410 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3411 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3412 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3413 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003414
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003415 default:
3416 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003417#ifdef EBCDIC
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003418 ebcdic2ascii(ch_buff, 3);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003419#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003420 ga_append(&prt_ps_buffer, ch_buff[0]);
3421 ga_append(&prt_ps_buffer, ch_buff[1]);
3422 ga_append(&prt_ps_buffer, ch_buff[2]);
3423 break;
3424 }
3425 }
3426 else
3427 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003428 }
3429
Bram Moolenaar81366db2005-07-24 21:16:51 +00003430 /* Need to free any translated characters */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003431 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003432
3433 prt_text_run += char_width;
3434 prt_pos_x += char_width;
3435
3436 /* The downside of fp - use relative error on right margin check */
3437 next_pos = prt_pos_x + prt_char_width;
3438 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003439 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003440
3441 if (need_break)
3442 prt_flush_buffer();
3443
3444 return need_break;
3445}
3446
3447 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003448mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003449{
3450 int font = 0;
3451
3452 if (iBold)
3453 font |= 0x01;
3454 if (iItalic)
3455 font |= 0x02;
3456
3457 if (font != prt_font)
3458 {
3459 prt_font = font;
3460 prt_attribute_change = TRUE;
3461 prt_need_font = TRUE;
3462 }
3463 if (prt_underline != iUnderline)
3464 {
3465 prt_underline = iUnderline;
3466 prt_attribute_change = TRUE;
3467 prt_need_underline = TRUE;
3468 }
3469}
3470
3471 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003472mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003473{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003475 prt_attribute_change = TRUE;
3476 prt_need_bgcol = TRUE;
3477}
3478
3479 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003480mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003481{
3482 if (fgcol != (long_u)prt_fgcol)
3483 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003485 prt_attribute_change = TRUE;
3486 prt_need_fgcol = TRUE;
3487 }
3488}
3489
3490# endif /*FEAT_POSTSCRIPT*/
3491#endif /*FEAT_PRINTER*/