blob: 45800a16f60fac9602a771dea597565f4862cd26 [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
105#if defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)
106# define OPT_MBFONT_USECOURIER 0
107# define OPT_MBFONT_ASCII 1
108# define OPT_MBFONT_REGULAR 2
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
152#if (defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
153/*
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;
459#ifdef FEAT_MBYTE
460 int l;
461#endif
462
463 /* Also use the space for the line number. */
464 if (prt_use_number())
465 width += PRINT_NUMBER_WIDTH;
466
467 tbuf = alloc(width + IOSIZE);
468 if (tbuf == NULL)
469 return;
470
471#ifdef FEAT_STL_OPT
472 if (*p_header != NUL)
473 {
474 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000475 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000476
477 /*
478 * Need to (temporarily) set current line number and first/last line
479 * number on the 'window'. Since we don't know how long the page is,
480 * set the first and current line number to the top line, and guess
481 * that the page length is 64.
482 */
483 tmp_lnum = curwin->w_cursor.lnum;
484 tmp_topline = curwin->w_topline;
485 tmp_botline = curwin->w_botline;
486 curwin->w_cursor.lnum = lnum;
487 curwin->w_topline = lnum;
488 curwin->w_botline = lnum + 63;
489 printer_page_num = pagenum;
490
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000491# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000492 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000493# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000494 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000495 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000497
498 /* Reset line numbers */
499 curwin->w_cursor.lnum = tmp_lnum;
500 curwin->w_topline = tmp_topline;
501 curwin->w_botline = tmp_botline;
502 }
503 else
504#endif
505 sprintf((char *)tbuf, _("Page %d"), pagenum);
506
507 prt_set_fg(PRCOLOR_BLACK);
508 prt_set_bg(PRCOLOR_WHITE);
509 prt_set_font(TRUE, FALSE, FALSE);
510
511 /* Use a negative line number to indicate printing in the top margin. */
512 page_line = 0 - prt_header_height();
513 mch_print_start_line(TRUE, page_line);
514 for (p = tbuf; *p != NUL; )
515 {
516 if (mch_print_text_out(p,
517#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000518 (l = (*mb_ptr2len)(p))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000519#else
520 1
521#endif
522 ))
523 {
524 ++page_line;
525 if (page_line >= 0) /* out of room in header */
526 break;
527 mch_print_start_line(TRUE, page_line);
528 }
529#ifdef FEAT_MBYTE
530 p += l;
531#else
532 p++;
533#endif
534 }
535
536 vim_free(tbuf);
537
538#ifdef FEAT_SYN_HL
539 if (psettings->do_syntax)
540 /* Set colors for next character. */
541 current_syn_id = -1;
542 else
543#endif
544 {
545 /* Set colors and font back to normal. */
546 prt_set_fg(PRCOLOR_BLACK);
547 prt_set_bg(PRCOLOR_WHITE);
548 prt_set_font(FALSE, FALSE, FALSE);
549 }
550}
551
552/*
553 * Display a print status message.
554 */
555 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100556prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000557{
558 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100559 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000560 out_flush();
561}
562
563 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100564ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000565{
566 linenr_T lnum;
567 int collated_copies, uncollated_copies;
568 prt_settings_T settings;
569 long_u bytes_to_print = 0;
570 int page_line;
571 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000572
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200573 vim_memset(&settings, 0, sizeof(prt_settings_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000574 settings.has_color = TRUE;
575
576# ifdef FEAT_POSTSCRIPT
577 if (*eap->arg == '>')
578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100579 char *errormsg = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000580
581 /* Expand things like "%.ps". */
582 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
583 {
584 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100585 emsg(errormsg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000586 return;
587 }
588 settings.outfile = skipwhite(eap->arg + 1);
589 }
590 else if (*eap->arg != NUL)
591 settings.arguments = eap->arg;
592# endif
593
594 /*
595 * Initialise for printing. Ask the user for settings, unless forceit is
596 * set.
597 * The mch_print_init() code should set up margins if applicable. (It may
598 * not be a real printer - for example the engine might generate HTML or
599 * PS.)
600 */
601 if (mch_print_init(&settings,
602 curbuf->b_fname == NULL
603 ? (char_u *)buf_spname(curbuf)
604 : curbuf->b_sfname == NULL
605 ? curbuf->b_fname
606 : curbuf->b_sfname,
607 eap->forceit) == FAIL)
608 return;
609
610#ifdef FEAT_SYN_HL
611# ifdef FEAT_GUI
612 if (gui.in_use)
613 settings.modec = 'g';
614 else
615# endif
616 if (t_colors > 1)
617 settings.modec = 'c';
618 else
619 settings.modec = 't';
620
Bram Moolenaar860cae12010-06-05 23:22:07 +0200621 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000622 settings.do_syntax = FALSE;
623 else if (printer_opts[OPT_PRINT_SYNTAX].present
624 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
625 settings.do_syntax =
626 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
627 else
628 settings.do_syntax = settings.has_color;
629#endif
630
631 /* Set up printing attributes for line numbers */
632 settings.number.fg_color = PRCOLOR_BLACK;
633 settings.number.bg_color = PRCOLOR_WHITE;
634 settings.number.bold = FALSE;
635 settings.number.italic = TRUE;
636 settings.number.underline = FALSE;
637#ifdef FEAT_SYN_HL
638 /*
639 * Syntax highlighting of line numbers.
640 */
641 if (prt_use_number() && settings.do_syntax)
642 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000643 int id;
644
Bram Moolenaar81366db2005-07-24 21:16:51 +0000645 id = syn_name2id((char_u *)"LineNr");
646 if (id > 0)
647 id = syn_get_final_id(id);
648
649 prt_get_attr(id, &settings.number, settings.modec);
650 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000651#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000652
653 /*
654 * Estimate the total lines to be printed
655 */
656 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
657 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
658 if (bytes_to_print == 0)
659 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100660 msg(_("No text to be printed"));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000661 goto print_fail_no_begin;
662 }
663
664 /* Set colors and font to normal. */
665 curr_bg = (long_u)0xffffffffL;
666 curr_fg = (long_u)0xffffffffL;
667 curr_italic = MAYBE;
668 curr_bold = MAYBE;
669 curr_underline = MAYBE;
670
671 prt_set_fg(PRCOLOR_BLACK);
672 prt_set_bg(PRCOLOR_WHITE);
673 prt_set_font(FALSE, FALSE, FALSE);
674#ifdef FEAT_SYN_HL
675 current_syn_id = -1;
676#endif
677
678 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
679 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
680
681 if (!mch_print_begin(&settings))
682 goto print_fail_no_begin;
683
684 /*
685 * Loop over collated copies: 1 2 3, 1 2 3, ...
686 */
687 page_count = 0;
688 for (collated_copies = 0;
689 collated_copies < settings.n_collated_copies;
690 collated_copies++)
691 {
692 prt_pos_T prtpos; /* current print position */
693 prt_pos_T page_prtpos; /* print position at page start */
694 int side;
695
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200696 vim_memset(&page_prtpos, 0, sizeof(prt_pos_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000697 page_prtpos.file_line = eap->line1;
698 prtpos = page_prtpos;
699
700 if (jobsplit && collated_copies > 0)
701 {
702 /* Splitting jobs: Stop a previous job and start a new one. */
703 mch_print_end(&settings);
704 if (!mch_print_begin(&settings))
705 goto print_fail_no_begin;
706 }
707
708 /*
709 * Loop over all pages in the print job: 1 2 3 ...
710 */
711 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
712 {
713 /*
714 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
715 * For duplex: 12 12 12 34 34 34, ...
716 */
717 for (uncollated_copies = 0;
718 uncollated_copies < settings.n_uncollated_copies;
719 uncollated_copies++)
720 {
721 /* Set the print position to the start of this page. */
722 prtpos = page_prtpos;
723
724 /*
725 * Do front and rear side of a page.
726 */
727 for (side = 0; side <= settings.duplex; ++side)
728 {
729 /*
730 * Print one page.
731 */
732
733 /* Check for interrupt character every page. */
734 ui_breakcheck();
735 if (got_int || settings.user_abort)
736 goto print_fail;
737
738 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
739 page_count + 1 + side,
740 prtpos.bytes_printed > 1000000
741 ? (int)(prtpos.bytes_printed /
742 (bytes_to_print / 100))
743 : (int)((prtpos.bytes_printed * 100)
744 / bytes_to_print));
745 if (!mch_print_begin_page(IObuff))
746 goto print_fail;
747
748 if (settings.n_collated_copies > 1)
749 sprintf((char *)IObuff + STRLEN(IObuff),
750 _(" Copy %d of %d"),
751 collated_copies + 1,
752 settings.n_collated_copies);
753 prt_message(IObuff);
754
755 /*
756 * Output header if required
757 */
758 if (prt_header_height() > 0)
759 prt_header(&settings, page_count + 1 + side,
760 prtpos.file_line);
761
762 for (page_line = 0; page_line < settings.lines_per_page;
763 ++page_line)
764 {
765 prtpos.column = hardcopy_line(&settings,
766 page_line, &prtpos);
767 if (prtpos.column == 0)
768 {
769 /* finished a file line */
770 prtpos.bytes_printed +=
771 STRLEN(skipwhite(ml_get(prtpos.file_line)));
772 if (++prtpos.file_line > eap->line2)
773 break; /* reached the end */
774 }
775 else if (prtpos.ff)
776 {
777 /* Line had a formfeed in it - start new page but
778 * stay on the current line */
779 break;
780 }
781 }
782
783 if (!mch_print_end_page())
784 goto print_fail;
785 if (prtpos.file_line > eap->line2)
786 break; /* reached the end */
787 }
788
789 /*
790 * Extra blank page for duplexing with odd number of pages and
791 * more copies to come.
792 */
793 if (prtpos.file_line > eap->line2 && settings.duplex
794 && side == 0
795 && uncollated_copies + 1 < settings.n_uncollated_copies)
796 {
797 if (!mch_print_blank_page())
798 goto print_fail;
799 }
800 }
801 if (settings.duplex && prtpos.file_line <= eap->line2)
802 ++page_count;
803
804 /* Remember the position where the next page starts. */
805 page_prtpos = prtpos;
806 }
807
808 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
809 settings.jobname);
810 prt_message(IObuff);
811 }
812
813print_fail:
814 if (got_int || settings.user_abort)
815 {
816 sprintf((char *)IObuff, "%s", _("Printing aborted"));
817 prt_message(IObuff);
818 }
819 mch_print_end(&settings);
820
821print_fail_no_begin:
822 mch_print_cleanup();
823}
824
825/*
826 * Print one page line.
827 * Return the next column to print, or zero if the line is finished.
828 */
829 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100830hardcopy_line(
831 prt_settings_T *psettings,
832 int page_line,
833 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000834{
835 colnr_T col;
836 char_u *line;
837 int need_break = FALSE;
838 int outputlen;
839 int tab_spaces;
840 long_u print_pos;
841#ifdef FEAT_SYN_HL
842 prt_text_attr_T attr;
843 int id;
844#endif
845
846 if (ppos->column == 0 || ppos->ff)
847 {
848 print_pos = 0;
849 tab_spaces = 0;
850 if (!ppos->ff && prt_use_number())
851 prt_line_number(psettings, page_line, ppos->file_line);
852 ppos->ff = FALSE;
853 }
854 else
855 {
856 /* left over from wrap halfway a tab */
857 print_pos = ppos->print_pos;
858 tab_spaces = ppos->lead_spaces;
859 }
860
861 mch_print_start_line(0, page_line);
862 line = ml_get(ppos->file_line);
863
864 /*
865 * Loop over the columns until the end of the file line or right margin.
866 */
867 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
868 {
869 outputlen = 1;
870#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000871 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000872 outputlen = 1;
873#endif
874#ifdef FEAT_SYN_HL
875 /*
876 * syntax highlighting stuff.
877 */
878 if (psettings->do_syntax)
879 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000880 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000881 if (id > 0)
882 id = syn_get_final_id(id);
883 else
884 id = 0;
885 /* Get the line again, a multi-line regexp may invalidate it. */
886 line = ml_get(ppos->file_line);
887
888 if (id != current_syn_id)
889 {
890 current_syn_id = id;
891 prt_get_attr(id, &attr, psettings->modec);
892 prt_set_font(attr.bold, attr.italic, attr.underline);
893 prt_set_fg(attr.fg_color);
894 prt_set_bg(attr.bg_color);
895 }
896 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000897#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000898
899 /*
900 * Appropriately expand any tabs to spaces.
901 */
902 if (line[col] == TAB || tab_spaces != 0)
903 {
904 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200905#ifdef FEAT_VARTABS
906 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
907 curbuf->b_p_vts_array);
908#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000909 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200910#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000911
912 while (tab_spaces > 0)
913 {
914 need_break = mch_print_text_out((char_u *)" ", 1);
915 print_pos++;
916 tab_spaces--;
917 if (need_break)
918 break;
919 }
920 /* Keep the TAB if we didn't finish it. */
921 if (need_break && tab_spaces > 0)
922 break;
923 }
924 else if (line[col] == FF
925 && printer_opts[OPT_PRINT_FORMFEED].present
926 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
927 == 'y')
928 {
929 ppos->ff = TRUE;
930 need_break = 1;
931 }
932 else
933 {
934 need_break = mch_print_text_out(line + col, outputlen);
935#ifdef FEAT_MBYTE
936 if (has_mbyte)
937 print_pos += (*mb_ptr2cells)(line + col);
938 else
939#endif
940 print_pos++;
941 }
942 }
943
944 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000945 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000946
947 /*
948 * Start next line of file if we clip lines, or have reached end of the
949 * line, unless we are doing a formfeed.
950 */
951 if (!ppos->ff
952 && (line[col] == NUL
953 || (printer_opts[OPT_PRINT_WRAP].present
954 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
955 == 'n')))
956 return 0;
957 return col;
958}
959
960# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
961
962/*
963 * PS printer stuff.
964 *
965 * Sources of information to help maintain the PS printing code:
966 *
967 * 1. PostScript Language Reference, 3rd Edition,
968 * Addison-Wesley, 1999, ISBN 0-201-37922-8
969 * 2. PostScript Language Program Design,
970 * Addison-Wesley, 1988, ISBN 0-201-14396-8
971 * 3. PostScript Tutorial and Cookbook,
972 * Addison Wesley, 1985, ISBN 0-201-10179-3
973 * 4. PostScript Language Document Structuring Conventions Specification,
974 * version 3.0,
975 * Adobe Technote 5001, 25th September 1992
976 * 5. PostScript Printer Description File Format Specification, Version 4.3,
977 * Adobe technote 5003, 9th February 1996
978 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
979 * Adobe Technote 5007, 7th October 1998
980 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
981 * Adobe Technote 5014, 8th October 1996
982 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
983 * Adoboe Technote 5094, 8th September, 2001
984 * 9. CJKV Information Processing, 2nd Edition,
985 * O'Reilly, 2002, ISBN 1-56592-224-7
986 *
987 * Some of these documents can be found in PDF form on Adobe's web site -
988 * http://www.adobe.com
989 */
990
991#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
992
993#define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
994#define PRT_PS_DEFAULT_FONTSIZE (10)
995#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
996
997struct prt_mediasize_S
998{
999 char *name;
1000 float width; /* width and height in points for portrait */
1001 float height;
1002};
1003
1004#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
1005
1006static struct prt_mediasize_S prt_mediasize[] =
1007{
1008 {"A4", 595.0, 842.0},
1009 {"letter", 612.0, 792.0},
1010 {"10x14", 720.0, 1008.0},
1011 {"A3", 842.0, 1191.0},
1012 {"A5", 420.0, 595.0},
1013 {"B4", 729.0, 1032.0},
1014 {"B5", 516.0, 729.0},
1015 {"executive", 522.0, 756.0},
1016 {"folio", 595.0, 935.0},
1017 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1018 {"legal", 612.0, 1008.0},
1019 {"quarto", 610.0, 780.0},
1020 {"statement", 396.0, 612.0},
1021 {"tabloid", 792.0, 1224.0}
1022};
1023
1024/* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1025struct prt_ps_font_S
1026{
1027 int wx;
1028 int uline_offset;
1029 int uline_width;
1030 int bbox_min_y;
1031 int bbox_max_y;
1032 char *(ps_fontname[4]);
1033};
1034
1035#define PRT_PS_FONT_ROMAN (0)
1036#define PRT_PS_FONT_BOLD (1)
1037#define PRT_PS_FONT_OBLIQUE (2)
1038#define PRT_PS_FONT_BOLDOBLIQUE (3)
1039
1040/* Standard font metrics for Courier family */
1041static struct prt_ps_font_S prt_ps_courier_font =
1042{
1043 600,
1044 -100, 50,
1045 -250, 805,
1046 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1047};
1048
1049#ifdef FEAT_MBYTE
1050/* Generic font metrics for multi-byte fonts */
1051static struct prt_ps_font_S prt_ps_mb_font =
1052{
1053 1000,
1054 -100, 50,
1055 -250, 805,
1056 {NULL, NULL, NULL, NULL}
1057};
1058#endif
1059
1060/* Pointer to current font set being used */
1061static struct prt_ps_font_S* prt_ps_font;
1062
1063/* Structures to map user named encoding and mapping to PS equivalents for
1064 * building CID font name */
1065struct prt_ps_encoding_S
1066{
1067 char *encoding;
1068 char *cmap_encoding;
1069 int needs_charset;
1070};
1071
1072struct prt_ps_charset_S
1073{
1074 char *charset;
1075 char *cmap_charset;
1076 int has_charset;
1077};
1078
1079#ifdef FEAT_MBYTE
1080
1081#define CS_JIS_C_1978 (0x01)
1082#define CS_JIS_X_1983 (0x02)
1083#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001084#define CS_NEC (0x08)
1085#define CS_MSWINDOWS (0x10)
1086#define CS_CP932 (0x20)
1087#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001088#define CS_KANJITALK7 (0x80)
1089
1090/* Japanese encodings and charsets */
1091static struct prt_ps_encoding_S j_encodings[] =
1092{
1093 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001094 CS_NEC)},
1095 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1096 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1097 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001098 {"cp932", "RKSJ", CS_JIS_X_1983},
1099 {"ucs-2", "UCS2", CS_JIS_X_1990},
1100 {"utf-8", "UTF8" , CS_JIS_X_1990}
1101};
1102static struct prt_ps_charset_S j_charsets[] =
1103{
1104 {"JIS_C_1978", "78", CS_JIS_C_1978},
1105 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1106 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001107 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001108 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1109 {"CP932", "90ms", CS_JIS_X_1983},
1110 {"KANJITALK6", "83pv", CS_KANJITALK6},
1111 {"KANJITALK7", "90pv", CS_KANJITALK7}
1112};
1113
1114#define CS_GB_2312_80 (0x01)
1115#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001116#define CS_GBK2K (0x04)
1117#define CS_SC_MAC (0x08)
1118#define CS_GBT_90_MAC (0x10)
1119#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001120#define CS_SC_ISO10646 (0x40)
1121
1122/* Simplified Chinese encodings and charsets */
1123static struct prt_ps_encoding_S sc_encodings[] =
1124{
1125 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1126 {"gb18030", NULL, CS_GBK2K},
1127 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001128 CS_GBT_90_MAC)},
1129 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001130 {"ucs-2", "UCS2", CS_SC_ISO10646},
1131 {"utf-8", "UTF8", CS_SC_ISO10646}
1132};
1133static struct prt_ps_charset_S sc_charsets[] =
1134{
1135 {"GB_2312-80", "GB", CS_GB_2312_80},
1136 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001137 {"MAC", "GBpc", CS_SC_MAC},
1138 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1139 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001140 {"GB18030", "GBK2K", CS_GBK2K},
1141 {"ISO10646", "UniGB", CS_SC_ISO10646}
1142};
1143
1144#define CS_CNS_PLANE_1 (0x01)
1145#define CS_CNS_PLANE_2 (0x02)
1146#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001147#define CS_B5 (0x08)
1148#define CS_ETEN (0x10)
1149#define CS_HK_GCCS (0x20)
1150#define CS_HK_SCS (0x40)
1151#define CS_HK_SCS_ETEN (0x80)
1152#define CS_MTHKL (0x100)
1153#define CS_MTHKS (0x200)
1154#define CS_DLHKL (0x400)
1155#define CS_DLHKS (0x800)
1156#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001157
1158/* Traditional Chinese encodings and charsets */
1159static struct prt_ps_encoding_S tc_encodings[] =
1160{
1161 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1162 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001163 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1164 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1165 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001166 {"cp950", "B5", CS_B5},
1167 {"ucs-2", "UCS2", CS_TC_ISO10646},
1168 {"utf-8", "UTF8", CS_TC_ISO10646},
1169 {"utf-16", "UTF16", CS_TC_ISO10646},
1170 {"utf-32", "UTF32", CS_TC_ISO10646}
1171};
1172static struct prt_ps_charset_S tc_charsets[] =
1173{
1174 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1175 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1176 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001177 {"BIG5", NULL, CS_B5},
1178 {"CP950", NULL, CS_B5},
1179 {"ETEN", "ETen", CS_ETEN},
1180 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1181 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001182 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1183 {"MTHKL", "HKm471", CS_MTHKL},
1184 {"MTHKS", "HKm314", CS_MTHKS},
1185 {"DLHKL", "HKdla", CS_DLHKL},
1186 {"DLHKS", "HKdlb", CS_DLHKS},
1187 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1188};
1189
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001190#define CS_KR_X_1992 (0x01)
1191#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001192#define CS_KR_X_1992_MS (0x04)
1193#define CS_KR_ISO10646 (0x08)
1194
1195/* Korean encodings and charsets */
1196static struct prt_ps_encoding_S k_encodings[] =
1197{
1198 {"iso-2022-kr", NULL, CS_KR_X_1992},
1199 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1200 {"johab", "Johab", CS_KR_X_1992},
1201 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001202 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001203 {"cp949", "UHC", CS_KR_X_1992_MS},
1204 {"ucs-2", "UCS2", CS_KR_ISO10646},
1205 {"utf-8", "UTF8", CS_KR_ISO10646}
1206};
1207static struct prt_ps_charset_S k_charsets[] =
1208{
1209 {"KS_X_1992", "KSC", CS_KR_X_1992},
1210 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001211 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001212 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1213 {"CP949", "KSCms", CS_KR_X_1992_MS},
1214 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1215 {"ISO10646", "UniKS", CS_KR_ISO10646}
1216};
1217
1218/* Collections of encodings and charsets for multi-byte printing */
1219struct prt_ps_mbfont_S
1220{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001221 int num_encodings;
1222 struct prt_ps_encoding_S *encodings;
1223 int num_charsets;
1224 struct prt_ps_charset_S *charsets;
1225 char *ascii_enc;
1226 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001227};
1228
1229static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1230{
1231 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001232 NUM_ELEMENTS(j_encodings),
1233 j_encodings,
1234 NUM_ELEMENTS(j_charsets),
1235 j_charsets,
1236 "jis_roman",
1237 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001238 },
1239 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001240 NUM_ELEMENTS(sc_encodings),
1241 sc_encodings,
1242 NUM_ELEMENTS(sc_charsets),
1243 sc_charsets,
1244 "gb_roman",
1245 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001246 },
1247 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001248 NUM_ELEMENTS(tc_encodings),
1249 tc_encodings,
1250 NUM_ELEMENTS(tc_charsets),
1251 tc_charsets,
1252 "cns_roman",
1253 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001254 },
1255 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001256 NUM_ELEMENTS(k_encodings),
1257 k_encodings,
1258 NUM_ELEMENTS(k_charsets),
1259 k_charsets,
1260 "ks_roman",
1261 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001262 }
1263};
1264#endif /* FEAT_MBYTE */
1265
1266struct prt_ps_resource_S
1267{
1268 char_u name[64];
1269 char_u filename[MAXPATHL + 1];
1270 int type;
1271 char_u title[256];
1272 char_u version[256];
1273};
1274
1275/* Types of PS resource file currently used */
1276#define PRT_RESOURCE_TYPE_PROCSET (0)
1277#define PRT_RESOURCE_TYPE_ENCODING (1)
1278#define PRT_RESOURCE_TYPE_CMAP (2)
1279
1280/* The PS prolog file version number has to match - if the prolog file is
1281 * updated, increment the number in the file and here. Version checking was
1282 * added as of VIM 6.2.
1283 * The CID prolog file version number behaves as per PS prolog.
1284 * Table of VIM and prolog versions:
1285 *
1286 * VIM Prolog CIDProlog
1287 * 6.2 1.3
1288 * 7.0 1.4 1.0
1289 */
1290#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1291#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1292
1293/* String versions of PS resource types - indexed by constants above so don't
1294 * re-order!
1295 */
1296static char *prt_resource_types[] =
1297{
1298 "procset",
1299 "encoding",
1300 "cmap"
1301};
1302
1303/* Strings to look for in a PS resource file */
1304#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1305#define PRT_RESOURCE_RESOURCE "Resource-"
1306#define PRT_RESOURCE_PROCSET "ProcSet"
1307#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001308#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001309
1310
1311/* Data for table based DSC comment recognition, easy to extend if VIM needs to
1312 * read more comments. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001313#define PRT_DSC_MISC_TYPE (-1)
1314#define PRT_DSC_TITLE_TYPE (1)
1315#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001316#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1317
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001318#define PRT_DSC_TITLE "%%Title:"
1319#define PRT_DSC_VERSION "%%Version:"
1320#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001321
1322struct prt_dsc_comment_S
1323{
1324 char *string;
1325 int len;
1326 int type;
1327};
1328
1329struct prt_dsc_line_S
1330{
1331 int type;
1332 char_u *string;
1333 int len;
1334};
1335
1336
1337#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1338static struct prt_dsc_comment_S prt_dsc_table[] =
1339{
1340 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1341 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1342 PRT_DSC_VERSION_TYPE},
1343 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1344 PRT_DSC_ENDCOMMENTS_TYPE}
1345};
1346
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001347static void prt_write_file_len(char_u *buffer, int bytes);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001348static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001349
1350/*
1351 * Variables for the output PostScript file.
1352 */
1353static FILE *prt_ps_fd;
1354static int prt_file_error;
1355static char_u *prt_ps_file_name = NULL;
1356
1357/*
1358 * Various offsets and dimensions in default PostScript user space (points).
1359 * Used for text positioning calculations
1360 */
1361static float prt_page_width;
1362static float prt_page_height;
1363static float prt_left_margin;
1364static float prt_right_margin;
1365static float prt_top_margin;
1366static float prt_bottom_margin;
1367static float prt_line_height;
1368static float prt_first_line_height;
1369static float prt_char_width;
1370static float prt_number_width;
1371static float prt_bgcol_offset;
1372static float prt_pos_x_moveto = 0.0;
1373static float prt_pos_y_moveto = 0.0;
1374
1375/*
1376 * Various control variables used to decide when and how to change the
1377 * PostScript graphics state.
1378 */
1379static int prt_need_moveto;
1380static int prt_do_moveto;
1381static int prt_need_font;
1382static int prt_font;
1383static int prt_need_underline;
1384static int prt_underline;
1385static int prt_do_underline;
1386static int prt_need_fgcol;
1387static int prt_fgcol;
1388static int prt_need_bgcol;
1389static int prt_do_bgcol;
1390static int prt_bgcol;
1391static int prt_new_bgcol;
1392static int prt_attribute_change;
1393static float prt_text_run;
1394static int prt_page_num;
1395static int prt_bufsiz;
1396
1397/*
1398 * Variables controlling physical printing.
1399 */
1400static int prt_media;
1401static int prt_portrait;
1402static int prt_num_copies;
1403static int prt_duplex;
1404static int prt_tumble;
1405static int prt_collate;
1406
1407/*
1408 * Buffers used when generating PostScript output
1409 */
1410static char_u prt_line_buffer[257];
1411static garray_T prt_ps_buffer;
1412
1413# ifdef FEAT_MBYTE
1414static int prt_do_conv;
1415static vimconv_T prt_conv;
1416
1417static int prt_out_mbyte;
1418static int prt_custom_cmap;
1419static char prt_cmap[80];
1420static int prt_use_courier;
1421static int prt_in_ascii;
1422static int prt_half_width;
1423static char *prt_ascii_encoding;
1424static char_u prt_hexchar[] = "0123456789abcdef";
1425# endif
1426
1427 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001428prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001429{
1430 if (!prt_file_error
1431 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1432 != (size_t)bytes)
1433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001434 emsg(_("E455: Error writing to PostScript output file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001435 prt_file_error = TRUE;
1436 }
1437}
1438
1439 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001440prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001441{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001442 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001443}
1444
1445 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001446prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001447{
1448#ifdef EBCDIC
1449 ebcdic2ascii(buffer, bytes);
1450#endif
1451 prt_write_file_raw_len(buffer, bytes);
1452}
1453
1454/*
1455 * Write a string.
1456 */
1457 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001458prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001459{
1460 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1461 prt_write_file(prt_line_buffer);
1462}
1463
1464/*
1465 * Write an int and a space.
1466 */
1467 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001468prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001469{
1470 sprintf((char *)prt_line_buffer, "%d ", i);
1471 prt_write_file(prt_line_buffer);
1472}
1473
1474/*
1475 * Write a boolean and a space.
1476 */
1477 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001478prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001479{
1480 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1481 prt_write_file(prt_line_buffer);
1482}
1483
1484/*
1485 * Write PostScript to re-encode and define the font.
1486 */
1487 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001488prt_def_font(
1489 char *new_name,
1490 char *encoding,
1491 int height,
1492 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001493{
1494 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1495 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1496 prt_write_file(prt_line_buffer);
1497#ifdef FEAT_MBYTE
1498 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001499 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001500 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1501 else
1502#endif
1503 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1504 "/%s %d /_%s ffs\n", new_name, height, new_name);
1505 prt_write_file(prt_line_buffer);
1506}
1507
1508#ifdef FEAT_MBYTE
1509/*
1510 * Write a line to define the CID font.
1511 */
1512 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001513prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001514{
1515 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1516 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1517 prt_write_file(prt_line_buffer);
1518 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1519 "/%s %d /_%s ffs\n", new_name, height, new_name);
1520 prt_write_file(prt_line_buffer);
1521}
1522
1523/*
1524 * Write a line to define a duplicate of a CID font
1525 */
1526 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001527prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001528{
1529 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1530 "/%s %s d\n", new_name, original_name);
1531 prt_write_file(prt_line_buffer);
1532}
1533#endif
1534
1535/*
1536 * Convert a real value into an integer and fractional part as integers, with
1537 * the fractional part being in the range [0,10^precision). The fractional part
1538 * is also rounded based on the precision + 1'th fractional digit.
1539 */
1540 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001541prt_real_bits(
1542 double real,
1543 int precision,
1544 int *pinteger,
1545 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001546{
1547 int i;
1548 int integer;
1549 float fraction;
1550
1551 integer = (int)real;
1552 fraction = (float)(real - integer);
1553 if (real < (double)integer)
1554 fraction = -fraction;
1555 for (i = 0; i < precision; i++)
1556 fraction *= 10.0;
1557
1558 *pinteger = integer;
1559 *pfraction = (int)(fraction + 0.5);
1560}
1561
1562/*
1563 * Write a real and a space. Save bytes if real value has no fractional part!
1564 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1565 * what decimal point character to use, but PS always requires a '.'.
1566 */
1567 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001568prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001569{
1570 int integer;
1571 int fraction;
1572
1573 prt_real_bits(val, prec, &integer, &fraction);
1574 /* Emit integer part */
1575 sprintf((char *)prt_line_buffer, "%d", integer);
1576 prt_write_file(prt_line_buffer);
1577 /* Only emit fraction if necessary */
1578 if (fraction != 0)
1579 {
1580 /* Remove any trailing zeros */
1581 while ((fraction % 10) == 0)
1582 {
1583 prec--;
1584 fraction /= 10;
1585 }
1586 /* Emit fraction left padded with zeros */
1587 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1588 prt_write_file(prt_line_buffer);
1589 }
1590 sprintf((char *)prt_line_buffer, " ");
1591 prt_write_file(prt_line_buffer);
1592}
1593
1594/*
1595 * Write a line to define a numeric variable.
1596 */
1597 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001598prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001599{
1600 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1601 "/%s ", name);
1602 prt_write_file(prt_line_buffer);
1603 prt_write_real(value, prec);
1604 sprintf((char *)prt_line_buffer, "d\n");
1605 prt_write_file(prt_line_buffer);
1606}
1607
1608/* Convert size from font space to user space at current font scale */
1609#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1610
1611 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001612prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001613{
1614 if (prt_ps_buffer.ga_len > 0)
1615 {
1616 /* Any background color must be drawn first */
1617 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1618 {
1619 int r, g, b;
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
1629 /* Size of rect of background color on which text is printed */
1630 prt_write_real(prt_text_run, 2);
1631 prt_write_real(prt_line_height, 2);
1632
1633 /* Lastly add the color of the background */
1634 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1635 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1636 b = prt_new_bgcol & 0xff;
1637 prt_write_real(r / 255.0, 3);
1638 prt_write_real(g / 255.0, 3);
1639 prt_write_real(b / 255.0, 3);
1640 prt_write_string("bg\n");
1641 }
1642 /* Draw underlines before the text as it makes it slightly easier to
1643 * find the starting point.
1644 */
1645 if (prt_do_underline)
1646 {
1647 if (prt_do_moveto)
1648 {
1649 prt_write_real(prt_pos_x_moveto, 2);
1650 prt_write_real(prt_pos_y_moveto, 2);
1651 prt_write_string("m\n");
1652 prt_do_moveto = FALSE;
1653 }
1654
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001655 /* Underline length of text run */
Bram Moolenaar81366db2005-07-24 21:16:51 +00001656 prt_write_real(prt_text_run, 2);
1657 prt_write_string("ul\n");
1658 }
1659 /* Draw the text
1660 * Note: we write text out raw - EBCDIC conversion is handled in the
1661 * PostScript world via the font encoding vector. */
1662#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001663 if (prt_out_mbyte)
1664 prt_write_string("<");
1665 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001666#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001667 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001668 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1669#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001670 if (prt_out_mbyte)
1671 prt_write_string(">");
1672 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001673#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001674 prt_write_string(")");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001675 /* Add a moveto if need be and use the appropriate show procedure */
1676 if (prt_do_moveto)
1677 {
1678 prt_write_real(prt_pos_x_moveto, 2);
1679 prt_write_real(prt_pos_y_moveto, 2);
1680 /* moveto and a show */
1681 prt_write_string("ms\n");
1682 prt_do_moveto = FALSE;
1683 }
1684 else /* Simple show */
1685 prt_write_string("s\n");
1686
1687 ga_clear(&prt_ps_buffer);
1688 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1689 }
1690}
1691
1692
1693 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001694prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001695{
1696 char_u *resource_filename = cookie;
1697
1698 if (STRLEN(filename) >= MAXPATHL)
1699 *resource_filename = NUL;
1700 else
1701 STRCPY(resource_filename, filename);
1702}
1703
1704 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001705prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001706{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001707 char_u *buffer;
1708 int retval;
1709
1710 buffer = alloc(MAXPATHL + 1);
1711 if (buffer == NULL)
1712 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001713
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001714 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001715 /* Look for named resource file in runtimepath */
1716 STRCPY(buffer, "print");
1717 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001718 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1719 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001720 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001721 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001722 resource->filename)
1723 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001724 vim_free(buffer);
1725 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001726}
1727
1728/* PS CR and LF characters have platform independent values */
1729#define PSLF (0x0a)
1730#define PSCR (0x0d)
1731
1732/* Static buffer to read initial comments in a resource file, some can have a
1733 * couple of KB of comments! */
1734#define PRT_FILE_BUFFER_LEN (2048)
1735struct prt_resfile_buffer_S
1736{
1737 char_u buffer[PRT_FILE_BUFFER_LEN];
1738 int len;
1739 int line_start;
1740 int line_end;
1741};
1742
1743static struct prt_resfile_buffer_S prt_resfile;
1744
1745 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001746prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001747{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001748 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001749
1750 /* Move to start of next line and then find end of line */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001751 idx = prt_resfile.line_end + 1;
1752 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001753 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001754 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001755 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001757 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001759
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001761 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001762 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001763 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001765 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001767
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001769}
1770
1771 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001772prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001773{
1774 /* Force not equal if string is longer than remainder of line */
1775 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001776 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001777
1778 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001779 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001780}
1781
1782 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001783prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001784{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001785 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001786
Bram Moolenaar89d40322006-08-29 15:30:07 +00001787 idx = prt_resfile.line_start + offset;
1788 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001789 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001790 if (isspace(prt_resfile.buffer[idx]))
1791 return idx - prt_resfile.line_start;
1792 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001793 }
1794 return -1;
1795}
1796
1797 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001798prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001799{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001800 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001801
Bram Moolenaar89d40322006-08-29 15:30:07 +00001802 idx = prt_resfile.line_start + offset;
1803 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001804 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001805 if (!isspace(prt_resfile.buffer[idx]))
1806 return idx - prt_resfile.line_start;
1807 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001808 }
1809 return -1;
1810}
1811
1812/* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1813 * if a DSC comment is found, else false */
1814 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001815prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001816{
1817 int comment;
1818 int offset;
1819
1820 /* Move to start of next line */
1821 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001822 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001823
1824 /* DSC comments always start %% */
1825 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001826 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001827
1828 /* Find type of DSC comment */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001829 for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001830 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1831 prt_dsc_table[comment].len) == 0)
1832 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001833
1834 if (comment != NUM_ELEMENTS(prt_dsc_table))
1835 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001836 /* Return type of comment */
1837 p_dsc_line->type = prt_dsc_table[comment].type;
1838 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001839 }
1840 else
1841 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001842 /* Unrecognised DSC comment, skip to ws after comment leader */
1843 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1844 offset = prt_resfile_skip_nonws(0);
1845 if (offset == -1)
1846 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001847 }
1848
1849 /* Skip ws to comment value */
1850 offset = prt_resfile_skip_ws(offset);
1851 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001852 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001853
1854 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1855 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1856
1857 return TRUE;
1858}
1859
1860/* Improved hand crafted parser to get the type, title, and version number of a
1861 * PS resource file so the file details can be added to the DSC header comments.
1862 */
1863 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001864prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001865{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001866 int offset;
1867 int seen_all;
1868 int seen_title;
1869 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001870 FILE *fd_resource;
1871 struct prt_dsc_line_S dsc_line;
1872
1873 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1874 if (fd_resource == NULL)
1875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 semsg(_("E624: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001877 return FALSE;
1878 }
1879 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1880
1881 /* Parse first line to ensure valid resource file */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001882 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001883 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001884 if (ferror(fd_resource))
1885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001886 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001887 resource->filename);
1888 fclose(fd_resource);
1889 return FALSE;
1890 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001891 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001892
1893 prt_resfile.line_end = -1;
1894 prt_resfile.line_start = 0;
1895 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001896 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001897
1898 offset = 0;
1899
1900 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001901 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 semsg(_("E618: file \"%s\" is not a PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001904 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001905 return FALSE;
1906 }
1907
1908 /* Skip over any version numbers and following ws */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001909 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001910 offset = prt_resfile_skip_nonws(offset);
1911 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001912 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001913 offset = prt_resfile_skip_ws(offset);
1914 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001915 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001916
1917 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001918 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001920 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001921 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001922 return FALSE;
1923 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001924 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001925
1926 /* Decide type of resource in the file */
1927 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001928 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001929 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1930 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001931 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001932 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1933 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001934 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001935 resource->type = PRT_RESOURCE_TYPE_CMAP;
1936 else
1937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001938 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001939 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001940 return FALSE;
1941 }
1942
1943 /* Look for title and version of resource */
1944 resource->title[0] = '\0';
1945 resource->version[0] = '\0';
1946 seen_title = FALSE;
1947 seen_version = FALSE;
1948 seen_all = FALSE;
1949 while (!seen_all && prt_next_dsc(&dsc_line))
1950 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001951 switch (dsc_line.type)
1952 {
1953 case PRT_DSC_TITLE_TYPE:
1954 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
1955 seen_title = TRUE;
1956 if (seen_version)
1957 seen_all = TRUE;
1958 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001959
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001960 case PRT_DSC_VERSION_TYPE:
1961 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
1962 seen_version = TRUE;
1963 if (seen_title)
1964 seen_all = TRUE;
1965 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001966
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001967 case PRT_DSC_ENDCOMMENTS_TYPE:
1968 /* Wont find title or resource after this comment, stop searching */
1969 seen_all = TRUE;
1970 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001971
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001972 case PRT_DSC_MISC_TYPE:
1973 /* Not interested in whatever comment this line had */
1974 break;
1975 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001976 }
1977
1978 if (!seen_title || !seen_version)
1979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001981 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001982 return FALSE;
1983 }
1984
Bram Moolenaar81366db2005-07-24 21:16:51 +00001985 return TRUE;
1986}
1987
1988 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001989prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001990{
1991 /* Version number m.n should match, the revision number does not matter */
1992 if (STRNCMP(resource->version, version, STRLEN(version)))
1993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 semsg(_("E621: \"%s\" resource file has wrong version"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001995 resource->name);
1996 return FALSE;
1997 }
1998
1999 /* Other checks to be added as needed */
2000 return TRUE;
2001}
2002
2003 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002004prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002005{
2006 prt_write_string("%!PS-Adobe-3.0\n");
2007}
2008
2009 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002010prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002011{
2012 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2013 "%%%%%s\n", comment);
2014 prt_write_file(prt_line_buffer);
2015}
2016
2017 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002018prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002019{
2020 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2021 "%%%%%s: %s\n", comment, text);
2022 prt_write_file(prt_line_buffer);
2023}
2024
2025 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002026prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002027{
2028 /* TODO - should scan 'text' for any chars needing escaping! */
2029 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2030 "%%%%%s: (%s)\n", comment, text);
2031 prt_write_file(prt_line_buffer);
2032}
2033
2034#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2035
2036 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002037prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002038{
2039 int i;
2040
2041 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2042 "%%%%%s:", comment);
2043 prt_write_file(prt_line_buffer);
2044
2045 for (i = 0; i < count; i++)
2046 {
2047 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2048 prt_write_file(prt_line_buffer);
2049 }
2050
2051 prt_write_string("\n");
2052}
2053
2054 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002055prt_dsc_resources(
2056 char *comment, /* if NULL add to previous */
2057 char *type,
2058 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002059{
2060 if (comment != NULL)
2061 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2062 "%%%%%s: %s", comment, type);
2063 else
2064 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2065 "%%%%+ %s", type);
2066 prt_write_file(prt_line_buffer);
2067
2068 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2069 " %s\n", string);
2070 prt_write_file(prt_line_buffer);
2071}
2072
2073 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002074prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002075{
2076 int i;
2077
2078 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002079 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002080 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002081 if (ps_font->ps_fontname[i] != NULL)
2082 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002083}
2084
2085 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002086prt_dsc_requirements(
2087 int duplex,
2088 int tumble,
2089 int collate,
2090 int color,
2091 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002092{
2093 /* Only output the comment if we need to.
2094 * Note: tumble is ignored if we are not duplexing
2095 */
2096 if (!(duplex || collate || color || (num_copies > 1)))
2097 return;
2098
2099 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2100 prt_write_file(prt_line_buffer);
2101
2102 if (duplex)
2103 {
2104 prt_write_string(" duplex");
2105 if (tumble)
2106 prt_write_string("(tumble)");
2107 }
2108 if (collate)
2109 prt_write_string(" collate");
2110 if (color)
2111 prt_write_string(" color");
2112 if (num_copies > 1)
2113 {
2114 prt_write_string(" numcopies(");
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002115 /* Note: no space wanted so don't use prt_write_int() */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002116 sprintf((char *)prt_line_buffer, "%d", num_copies);
2117 prt_write_file(prt_line_buffer);
2118 prt_write_string(")");
2119 }
2120 prt_write_string("\n");
2121}
2122
2123 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002124prt_dsc_docmedia(
2125 char *paper_name,
2126 double width,
2127 double height,
2128 double weight,
2129 char *colour,
2130 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002131{
2132 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2133 "%%%%DocumentMedia: %s ", paper_name);
2134 prt_write_file(prt_line_buffer);
2135 prt_write_real(width, 2);
2136 prt_write_real(height, 2);
2137 prt_write_real(weight, 2);
2138 if (colour == NULL)
2139 prt_write_string("()");
2140 else
2141 prt_write_string(colour);
2142 prt_write_string(" ");
2143 if (type == NULL)
2144 prt_write_string("()");
2145 else
2146 prt_write_string(type);
2147 prt_write_string("\n");
2148}
2149
2150 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002151mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002152{
2153#ifdef FEAT_MBYTE
2154 if (prt_out_mbyte)
2155 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002156 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002157
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002158 /* Free off all CID font names created, but first clear duplicate
2159 * pointers to the same string (when the same font is used for more than
2160 * one style).
2161 */
2162 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2163 {
2164 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002165 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002166 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002167 }
2168
2169 if (prt_do_conv)
2170 {
2171 convert_setup(&prt_conv, NULL, NULL);
2172 prt_do_conv = FALSE;
2173 }
2174#endif
2175 if (prt_ps_fd != NULL)
2176 {
2177 fclose(prt_ps_fd);
2178 prt_ps_fd = NULL;
2179 prt_file_error = FALSE;
2180 }
2181 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002182 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002183}
2184
2185 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002186to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002187{
2188 float ret;
2189 int u;
2190 int nr;
2191
2192 u = prt_get_unit(idx);
2193 if (u == PRT_UNIT_NONE)
2194 {
2195 u = PRT_UNIT_PERC;
2196 nr = def_number;
2197 }
2198 else
2199 nr = printer_opts[idx].number;
2200
2201 switch (u)
2202 {
2203 case PRT_UNIT_INCH:
2204 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2205 break;
2206 case PRT_UNIT_MM:
2207 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2208 break;
2209 case PRT_UNIT_POINT:
2210 ret = (float)nr;
2211 break;
2212 case PRT_UNIT_PERC:
2213 default:
2214 ret = (float)(physsize * nr) / 100;
2215 break;
2216 }
2217
2218 return ret;
2219}
2220
2221/*
2222 * Calculate margins for given width and height from printoptions settings.
2223 */
2224 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002225prt_page_margins(
2226 double width,
2227 double height,
2228 double *left,
2229 double *right,
2230 double *top,
2231 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002232{
2233 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2234 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2235 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2236 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2237}
2238
2239 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002240prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002241{
2242 prt_line_height = (float)font_scale;
2243 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2244}
2245
2246
2247 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002248prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002249{
2250 if (prt_use_number())
2251 {
2252 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2253#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002254 /* If we are outputting multi-byte characters then line numbers will be
2255 * printed with half width characters
2256 */
2257 if (prt_out_mbyte)
2258 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002259#endif
2260 prt_left_margin += prt_number_width;
2261 }
2262 else
2263 prt_number_width = 0.0;
2264
2265 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2266}
2267
2268#ifdef FEAT_MBYTE
2269 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002270prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002271{
2272 char *fontname;
2273
2274 fontname = (char *)alloc(name_len + 1);
2275 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002276 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002277 vim_strncpy((char_u *)fontname, name, name_len);
2278 prt_ps_mb_font.ps_fontname[font] = fontname;
2279
2280 return TRUE;
2281}
2282#endif
2283
2284/*
2285 * Get number of lines of text that fit on a page (excluding the header).
2286 */
2287 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002288prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002289{
2290 int lpp;
2291
2292 /*
2293 * Calculate offset to lower left corner of background rect based on actual
2294 * font height (based on its bounding box) and the line height, handling the
2295 * case where the font height can exceed the line height.
2296 */
2297 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2298 prt_ps_font->bbox_min_y);
2299 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2300 {
2301 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2302 (1000.0 - (prt_ps_font->bbox_max_y -
2303 prt_ps_font->bbox_min_y)) / 2);
2304 }
2305
2306 /* Get height for topmost line based on background rect offset. */
2307 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2308
2309 /* Calculate lpp */
2310 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2311
2312 /* Adjust top margin if there is a header */
2313 prt_top_margin -= prt_line_height * prt_header_height();
2314
2315 return lpp - prt_header_height();
2316}
2317
2318#ifdef FEAT_MBYTE
2319 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002320prt_match_encoding(
2321 char *p_encoding,
2322 struct prt_ps_mbfont_S *p_cmap,
2323 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002324{
2325 int mbenc;
2326 int enc_len;
2327 struct prt_ps_encoding_S *p_mbenc;
2328
2329 *pp_mbenc = NULL;
2330 /* Look for recognised encoding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002331 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002332 p_mbenc = p_cmap->encodings;
2333 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2334 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002335 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2336 {
2337 *pp_mbenc = p_mbenc;
2338 return TRUE;
2339 }
2340 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002341 }
2342 return FALSE;
2343}
2344
2345 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002346prt_match_charset(
2347 char *p_charset,
2348 struct prt_ps_mbfont_S *p_cmap,
2349 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002350{
2351 int mbchar;
2352 int char_len;
2353 struct prt_ps_charset_S *p_mbchar;
2354
2355 /* Look for recognised character set, using default if one is not given */
2356 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002357 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002358 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002359 p_mbchar = p_cmap->charsets;
2360 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2361 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002362 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2363 {
2364 *pp_mbchar = p_mbchar;
2365 return TRUE;
2366 }
2367 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002368 }
2369 return FALSE;
2370}
2371#endif
2372
Bram Moolenaar81366db2005-07-24 21:16:51 +00002373 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002374mch_print_init(
2375 prt_settings_T *psettings,
2376 char_u *jobname,
2377 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002378{
2379 int i;
2380 char *paper_name;
2381 int paper_strlen;
2382 int fontsize;
2383 char_u *p;
2384 double left;
2385 double right;
2386 double top;
2387 double bottom;
2388#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002389 int props;
2390 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002391 char_u *p_encoding;
2392 struct prt_ps_encoding_S *p_mbenc;
2393 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002394 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002395#endif
2396
2397#if 0
2398 /*
2399 * TODO:
2400 * If "forceit" is false: pop up a dialog to select:
2401 * - printer name
2402 * - copies
2403 * - collated/uncollated
2404 * - duplex off/long side/short side
2405 * - paper size
2406 * - portrait/landscape
2407 * - font size
2408 *
2409 * If "forceit" is true: use the default printer and settings
2410 */
2411 if (forceit)
2412 s_pd.Flags |= PD_RETURNDEFAULT;
2413#endif
2414
2415 /*
2416 * Set up font and encoding.
2417 */
2418#ifdef FEAT_MBYTE
2419 p_encoding = enc_skip(p_penc);
2420 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002421 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002422
Bram Moolenaar14716812006-05-04 21:54:08 +00002423 /* Look for a multi-byte font that matches the encoding and character set.
2424 * Only look if multi-byte character set is defined, or using multi-byte
2425 * encoding other than Unicode. This is because a Unicode encoding does not
2426 * uniquely identify a CJK character set to use. */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002427 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002428 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002429 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002430 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002431 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002432
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002433 p_mbenc_first = NULL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002434 for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002435 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002436 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002437 {
2438 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002439 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002440 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002441 cmap_first = cmap;
2442 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002443 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002444 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002445 break;
2446 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002447
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002448 /* Use first encoding matched if no charset matched */
2449 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002450 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002451 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002452 cmap = cmap_first;
2453 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002454 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002455
2456 prt_out_mbyte = (p_mbenc != NULL);
2457 if (prt_out_mbyte)
2458 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002459 /* Build CMap name - will be same for all multi-byte fonts used */
2460 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002461
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002462 prt_custom_cmap = (p_mbchar == NULL);
2463 if (!prt_custom_cmap)
2464 {
2465 /* Check encoding and character set are compatible */
2466 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002468 emsg(_("E673: Incompatible multi-byte encoding and character set."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002469 return FALSE;
2470 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002471
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002472 /* Add charset name if not empty */
2473 if (p_mbchar->cmap_charset != NULL)
2474 {
2475 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002476 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002477 STRCAT(prt_cmap, "-");
2478 }
2479 }
2480 else
2481 {
2482 /* Add custom CMap character set name */
2483 if (*p_pmcs == NUL)
2484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002485 emsg(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002486 return FALSE;
2487 }
2488 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2489 STRCAT(prt_cmap, "-");
2490 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002491
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002492 /* CMap name ends with (optional) encoding name and -H for horizontal */
2493 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002494 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002495 {
2496 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2497 STRCAT(prt_cmap, "-");
2498 }
2499 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002500
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002501 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002503 emsg(_("E675: No default font specified for multi-byte printing."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002504 return FALSE;
2505 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002506
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002507 /* Derive CID font names with fallbacks if not defined */
2508 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2509 mbfont_opts[OPT_MBFONT_REGULAR].string,
2510 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2511 return FALSE;
2512 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2513 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2514 mbfont_opts[OPT_MBFONT_BOLD].string,
2515 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2516 return FALSE;
2517 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2518 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2519 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2520 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2521 return FALSE;
2522 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2523 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002524 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2525 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002526 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002527
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002528 /* Check if need to use Courier for ASCII code range, and if so pick up
2529 * the encoding to use */
2530 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2531 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2532 if (prt_use_courier)
2533 {
2534 /* Use national ASCII variant unless ASCII wanted */
2535 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2536 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2537 prt_ascii_encoding = "ascii";
2538 else
2539 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2540 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002541
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002542 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002543 }
2544 else
2545#endif
2546 {
2547#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002548 prt_use_courier = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002549#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002550 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002551 }
2552
2553 /*
2554 * Find the size of the paper and set the margins.
2555 */
2556 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2557 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2558 if (printer_opts[OPT_PRINT_PAPER].present)
2559 {
2560 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2561 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2562 }
2563 else
2564 {
2565 paper_name = "A4";
2566 paper_strlen = 2;
2567 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002568 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002569 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2570 && STRNICMP(prt_mediasize[i].name, paper_name,
2571 paper_strlen) == 0)
2572 break;
2573 if (i == PRT_MEDIASIZE_LEN)
2574 i = 0;
2575 prt_media = i;
2576
2577 /*
2578 * Set PS pagesize based on media dimensions and print orientation.
2579 * Note: Media and page sizes have defined meanings in PostScript and should
2580 * be kept distinct. Media is the paper (or transparency, or ...) that is
2581 * printed on, whereas the page size is the area that the PostScript
2582 * interpreter renders into.
2583 */
2584 if (prt_portrait)
2585 {
2586 prt_page_width = prt_mediasize[i].width;
2587 prt_page_height = prt_mediasize[i].height;
2588 }
2589 else
2590 {
2591 prt_page_width = prt_mediasize[i].height;
2592 prt_page_height = prt_mediasize[i].width;
2593 }
2594
2595 /*
2596 * Set PS page margins based on the PS pagesize, not the mediasize - this
2597 * needs to be done before the cpl and lpp are calculated.
2598 */
2599 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2600 &bottom);
2601 prt_left_margin = (float)left;
2602 prt_right_margin = (float)right;
2603 prt_top_margin = (float)top;
2604 prt_bottom_margin = (float)bottom;
2605
2606 /*
2607 * Set up the font size.
2608 */
2609 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2610 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2611 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2612 fontsize = atoi((char *)p + 2);
2613 prt_font_metrics(fontsize);
2614
2615 /*
2616 * Return the number of characters per line, and lines per page for the
2617 * generic print code.
2618 */
2619 psettings->chars_per_line = prt_get_cpl();
2620 psettings->lines_per_page = prt_get_lpp();
2621
2622 /* Catch margin settings that leave no space for output! */
2623 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2624 return FAIL;
2625
2626 /*
2627 * Sort out the number of copies to be printed. PS by default will do
2628 * uncollated copies for you, so once we know how many uncollated copies are
2629 * wanted cache it away and lie to the generic code that we only want one
2630 * uncollated copy.
2631 */
2632 psettings->n_collated_copies = 1;
2633 psettings->n_uncollated_copies = 1;
2634 prt_num_copies = 1;
2635 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2636 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2637 if (prt_collate)
2638 {
2639 /* TODO: Get number of collated copies wanted. */
2640 psettings->n_collated_copies = 1;
2641 }
2642 else
2643 {
2644 /* TODO: Get number of uncollated copies wanted and update the cached
2645 * count.
2646 */
2647 prt_num_copies = 1;
2648 }
2649
2650 psettings->jobname = jobname;
2651
2652 /*
2653 * Set up printer duplex and tumble based on Duplex option setting - default
2654 * is long sided duplex printing (i.e. no tumble).
2655 */
2656 prt_duplex = TRUE;
2657 prt_tumble = FALSE;
2658 psettings->duplex = 1;
2659 if (printer_opts[OPT_PRINT_DUPLEX].present)
2660 {
2661 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2662 {
2663 prt_duplex = FALSE;
2664 psettings->duplex = 0;
2665 }
2666 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2667 == 0)
2668 prt_tumble = TRUE;
2669 }
2670
2671 /* For now user abort not supported */
2672 psettings->user_abort = 0;
2673
2674 /* If the user didn't specify a file name, use a temp file. */
2675 if (psettings->outfile == NULL)
2676 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002677 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002678 if (prt_ps_file_name == NULL)
2679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 emsg(_(e_notmp));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002681 return FAIL;
2682 }
2683 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2684 }
2685 else
2686 {
2687 p = expand_env_save(psettings->outfile);
2688 if (p != NULL)
2689 {
2690 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2691 vim_free(p);
2692 }
2693 }
2694 if (prt_ps_fd == NULL)
2695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002696 emsg(_("E324: Can't open PostScript output file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002697 mch_print_cleanup();
2698 return FAIL;
2699 }
2700
2701 prt_bufsiz = psettings->chars_per_line;
2702#ifdef FEAT_MBYTE
2703 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002704 prt_bufsiz *= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002705#endif
2706 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2707
2708 prt_page_num = 0;
2709
2710 prt_attribute_change = FALSE;
2711 prt_need_moveto = FALSE;
2712 prt_need_font = FALSE;
2713 prt_need_fgcol = FALSE;
2714 prt_need_bgcol = FALSE;
2715 prt_need_underline = FALSE;
2716
2717 prt_file_error = FALSE;
2718
2719 return OK;
2720}
2721
2722 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002723prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002724{
2725 FILE* fd_resource;
2726 char_u resource_buffer[512];
2727 size_t bytes_read;
2728
2729 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2730 if (fd_resource == NULL)
2731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002732 semsg(_("E456: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002733 return FALSE;
2734 }
2735 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2736 (char *)resource->title);
2737
2738 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2739
2740 for (;;)
2741 {
2742 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2743 sizeof(resource_buffer), fd_resource);
2744 if (ferror(fd_resource))
2745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002746 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002747 resource->filename);
2748 fclose(fd_resource);
2749 return FALSE;
2750 }
2751 if (bytes_read == 0)
2752 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002753 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002754 if (prt_file_error)
2755 {
2756 fclose(fd_resource);
2757 return FALSE;
2758 }
2759 }
2760 fclose(fd_resource);
2761
2762 prt_dsc_noarg("EndDocument");
2763
2764 prt_dsc_noarg("EndResource");
2765
2766 return TRUE;
2767}
2768
2769 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002770mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002771{
2772 time_t now;
2773 int bbox[4];
2774 char *p_time;
2775 double left;
2776 double right;
2777 double top;
2778 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002779 struct prt_ps_resource_S *res_prolog;
2780 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002781 char buffer[256];
2782 char_u *p_encoding;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002783 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002784#ifdef FEAT_MBYTE
Bram Moolenaard9462e32011-04-11 21:35:11 +02002785 struct prt_ps_resource_S *res_cidfont;
2786 struct prt_ps_resource_S *res_cmap;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002787#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02002788 int retval = FALSE;
2789
2790 res_prolog = (struct prt_ps_resource_S *)
2791 alloc(sizeof(struct prt_ps_resource_S));
2792 res_encoding = (struct prt_ps_resource_S *)
2793 alloc(sizeof(struct prt_ps_resource_S));
2794#ifdef FEAT_MBYTE
2795 res_cidfont = (struct prt_ps_resource_S *)
2796 alloc(sizeof(struct prt_ps_resource_S));
2797 res_cmap = (struct prt_ps_resource_S *)
2798 alloc(sizeof(struct prt_ps_resource_S));
2799#endif
2800 if (res_prolog == NULL || res_encoding == NULL
2801#ifdef FEAT_MBYTE
2802 || res_cidfont == NULL || res_cmap == NULL
2803#endif
2804 )
2805 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002806
2807 /*
2808 * PS DSC Header comments - no PS code!
2809 */
2810 prt_dsc_start();
2811 prt_dsc_textline("Title", (char *)psettings->jobname);
2812 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002813 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002814 prt_dsc_textline("For", buffer);
2815 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2816 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2817 now = time(NULL);
2818 p_time = ctime(&now);
2819 /* Note: ctime() adds a \n so we have to remove it :-( */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002820 p = vim_strchr((char_u *)p_time, '\n');
2821 if (p != NULL)
2822 *p = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002823 prt_dsc_textline("CreationDate", p_time);
2824 prt_dsc_textline("DocumentData", "Clean8Bit");
2825 prt_dsc_textline("Orientation", "Portrait");
2826 prt_dsc_atend("Pages");
2827 prt_dsc_textline("PageOrder", "Ascend");
2828 /* The bbox does not change with orientation - it is always in the default
2829 * user coordinate system! We have to recalculate right and bottom
2830 * coordinates based on the font metrics for the bbox to be accurate. */
2831 prt_page_margins(prt_mediasize[prt_media].width,
2832 prt_mediasize[prt_media].height,
2833 &left, &right, &top, &bottom);
2834 bbox[0] = (int)left;
2835 if (prt_portrait)
2836 {
2837 /* In portrait printing the fixed point is the top left corner so we
2838 * derive the bbox from that point. We have the expected cpl chars
2839 * across the media and lpp lines down the media.
2840 */
2841 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2842 * prt_line_height);
2843 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2844 + 0.5);
2845 bbox[3] = (int)(top + 0.5);
2846 }
2847 else
2848 {
2849 /* In landscape printing the fixed point is the bottom left corner so we
2850 * derive the bbox from that point. We have lpp chars across the media
2851 * and cpl lines up the media.
2852 */
2853 bbox[1] = (int)bottom;
2854 bbox[2] = (int)(left + ((psettings->lines_per_page
2855 + prt_header_height()) * prt_line_height) + 0.5);
2856 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2857 + 0.5);
2858 }
2859 prt_dsc_ints("BoundingBox", 4, bbox);
2860 /* The media width and height does not change with landscape printing! */
2861 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2862 prt_mediasize[prt_media].width,
2863 prt_mediasize[prt_media].height,
2864 (double)0, NULL, NULL);
2865 /* Define fonts needed */
2866#ifdef FEAT_MBYTE
2867 if (!prt_out_mbyte || prt_use_courier)
2868#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002869 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002870#ifdef FEAT_MBYTE
2871 if (prt_out_mbyte)
2872 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002873 prt_dsc_font_resource((prt_use_courier ? NULL
2874 : "DocumentNeededResources"), &prt_ps_mb_font);
2875 if (!prt_custom_cmap)
2876 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002877 }
2878#endif
2879
2880 /* Search for external resources VIM supplies */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002881 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883 emsg(_("E456: Can't find PostScript resource file \"prolog.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002884 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002885 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002886 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002887 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002888 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002889 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002890#ifdef FEAT_MBYTE
2891 if (prt_out_mbyte)
2892 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002893 /* Look for required version of multi-byte printing procset */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002894 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 emsg(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002897 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002898 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002899 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002900 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002901 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002902 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002903 }
2904#endif
2905
2906 /* Find an encoding to use for printing.
2907 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2908 * that cannot be found then default to "latin1".
2909 * Note: VIM specific encoding header is always skipped.
2910 */
2911#ifdef FEAT_MBYTE
2912 if (!prt_out_mbyte)
2913 {
2914#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002915 p_encoding = enc_skip(p_penc);
2916 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002917 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002918 {
2919 /* 'printencoding' not set or not supported - find alternate */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002920#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002921 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002922
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002923 p_encoding = enc_skip(p_enc);
2924 props = enc_canon_props(p_encoding);
2925 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002926 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002927 /* 8-bit 'encoding' is not supported */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002928#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002929 {
2930 /* Use latin1 as default printing encoding */
2931 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002932 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002934 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002935 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002936 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002937 }
2938 }
2939 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002940 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002941 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002942 /* For the moment there are no checks on encoding resource files to
2943 * perform */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002944#ifdef FEAT_MBYTE
2945 }
2946 else
2947 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002948 p_encoding = enc_skip(p_penc);
2949 if (*p_encoding == NUL)
2950 p_encoding = enc_skip(p_enc);
2951 if (prt_use_courier)
2952 {
2953 /* Include ASCII range encoding vector */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002954 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002957 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002958 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002959 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002960 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002961 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002962 /* For the moment there are no checks on encoding resource files to
2963 * perform */
2964 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002965 }
2966
2967 prt_conv.vc_type = CONV_NONE;
2968 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002969 /* Set up encoding conversion if required */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002970 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
2971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 semsg(_("E620: Unable to convert to print encoding \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002973 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002974 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002975 }
2976 prt_do_conv = TRUE;
2977 }
2978 prt_do_conv = prt_conv.vc_type != CONV_NONE;
2979
2980 if (prt_out_mbyte && prt_custom_cmap)
2981 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002982 /* Find user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002983 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002985 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002986 prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002987 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002988 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002989 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002990 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002991 }
2992#endif
2993
2994 /* List resources supplied */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002995 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002996 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002997 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002998 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
2999#ifdef FEAT_MBYTE
3000 if (prt_out_mbyte)
3001 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003002 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003003 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003004 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003005 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003006
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003007 if (prt_custom_cmap)
3008 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003009 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003010 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003011 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003012 prt_dsc_resources(NULL, "cmap", buffer);
3013 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003014 }
3015 if (!prt_out_mbyte || prt_use_courier)
3016#endif
3017 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003018 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003019 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003020 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003021 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003022 }
3023 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3024#ifdef FEAT_SYN_HL
3025 psettings->do_syntax
3026#else
3027 0
3028#endif
3029 , prt_num_copies);
3030 prt_dsc_noarg("EndComments");
3031
3032 /*
3033 * PS Document page defaults
3034 */
3035 prt_dsc_noarg("BeginDefaults");
3036
3037 /* List font resources most likely common to all pages */
3038#ifdef FEAT_MBYTE
3039 if (!prt_out_mbyte || prt_use_courier)
3040#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003041 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003042#ifdef FEAT_MBYTE
3043 if (prt_out_mbyte)
3044 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003045 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3046 &prt_ps_mb_font);
3047 if (!prt_custom_cmap)
3048 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003049 }
3050#endif
3051
3052 /* Paper will be used for all pages */
3053 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3054
3055 prt_dsc_noarg("EndDefaults");
3056
3057 /*
3058 * PS Document prolog inclusion - all required procsets.
3059 */
3060 prt_dsc_noarg("BeginProlog");
3061
3062 /* Add required procsets - NOTE: order is important! */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003063 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003064 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003065#ifdef FEAT_MBYTE
3066 if (prt_out_mbyte)
3067 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003068 /* Add CID font procset, and any user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003069 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003070 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003071 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003072 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003073 }
3074#endif
3075
3076#ifdef FEAT_MBYTE
3077 if (!prt_out_mbyte || prt_use_courier)
3078#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003079 /* There will be only one Roman font encoding to be included in the PS
3080 * file. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003081 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003082 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003083
3084 prt_dsc_noarg("EndProlog");
3085
3086 /*
3087 * PS Document setup - must appear after the prolog
3088 */
3089 prt_dsc_noarg("BeginSetup");
3090
3091 /* Device setup - page size and number of uncollated copies */
3092 prt_write_int((int)prt_mediasize[prt_media].width);
3093 prt_write_int((int)prt_mediasize[prt_media].height);
3094 prt_write_int(0);
3095 prt_write_string("sps\n");
3096 prt_write_int(prt_num_copies);
3097 prt_write_string("nc\n");
3098 prt_write_boolean(prt_duplex);
3099 prt_write_boolean(prt_tumble);
3100 prt_write_string("dt\n");
3101 prt_write_boolean(prt_collate);
3102 prt_write_string("c\n");
3103
3104 /* Font resource inclusion and definition */
3105#ifdef FEAT_MBYTE
3106 if (!prt_out_mbyte || prt_use_courier)
3107 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003108 /* When using Courier for ASCII range when printing multi-byte, need to
3109 * pick up ASCII encoding to use with it. */
3110 if (prt_use_courier)
3111 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003112#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003113 prt_dsc_resources("IncludeResource", "font",
3114 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3115 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3116 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3117 prt_dsc_resources("IncludeResource", "font",
3118 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3119 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3120 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3121 prt_dsc_resources("IncludeResource", "font",
3122 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3123 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3124 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3125 prt_dsc_resources("IncludeResource", "font",
3126 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3127 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3128 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003129#ifdef FEAT_MBYTE
3130 }
3131 if (prt_out_mbyte)
3132 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003133 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3134 * not have an italic form being a western style, so where no font is
3135 * defined for these faces VIM falls back to an existing face.
3136 * Note: if using Courier for the ASCII range then the printout will
3137 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3138 */
3139 prt_dsc_resources("IncludeResource", "font",
3140 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3141 if (!prt_custom_cmap)
3142 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3143 prt_def_cidfont("CF0", (int)prt_line_height,
3144 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003145
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003146 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3147 {
3148 prt_dsc_resources("IncludeResource", "font",
3149 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3150 if (!prt_custom_cmap)
3151 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3152 prt_def_cidfont("CF1", (int)prt_line_height,
3153 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3154 }
3155 else
3156 /* Use ROMAN for BOLD */
3157 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003158
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003159 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3160 {
3161 prt_dsc_resources("IncludeResource", "font",
3162 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3163 if (!prt_custom_cmap)
3164 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3165 prt_def_cidfont("CF2", (int)prt_line_height,
3166 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3167 }
3168 else
3169 /* Use ROMAN for OBLIQUE */
3170 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003171
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003172 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3173 {
3174 prt_dsc_resources("IncludeResource", "font",
3175 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3176 if (!prt_custom_cmap)
3177 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3178 prt_def_cidfont("CF3", (int)prt_line_height,
3179 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3180 }
3181 else
3182 /* Use BOLD for BOLDOBLIQUE */
3183 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003184 }
3185#endif
3186
3187 /* Misc constant vars used for underlining and background rects */
3188 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3189 prt_ps_font->uline_offset), 2);
3190 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3191 prt_ps_font->uline_width), 2);
3192 prt_def_var("BO", prt_bgcol_offset, 2);
3193
3194 prt_dsc_noarg("EndSetup");
3195
3196 /* Fail if any problems writing out to the PS file */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003197 retval = !prt_file_error;
3198
3199theend:
3200 vim_free(res_prolog);
3201 vim_free(res_encoding);
3202#ifdef FEAT_MBYTE
3203 vim_free(res_cidfont);
3204 vim_free(res_cmap);
3205#endif
3206
3207 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003208}
3209
3210 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003211mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003212{
3213 prt_dsc_noarg("Trailer");
3214
3215 /*
3216 * Output any info we don't know in toto until we finish
3217 */
3218 prt_dsc_ints("Pages", 1, &prt_page_num);
3219
3220 prt_dsc_noarg("EOF");
3221
3222 /* Write CTRL-D to close serial communication link if used.
3223 * NOTHING MUST BE WRITTEN AFTER THIS! */
3224 prt_write_file((char_u *)IF_EB("\004", "\067"));
3225
3226 if (!prt_file_error && psettings->outfile == NULL
3227 && !got_int && !psettings->user_abort)
3228 {
3229 /* Close the file first. */
3230 if (prt_ps_fd != NULL)
3231 {
3232 fclose(prt_ps_fd);
3233 prt_ps_fd = NULL;
3234 }
3235 prt_message((char_u *)_("Sending to printer..."));
3236
3237 /* Not printing to a file: use 'printexpr' to print the file. */
3238 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003239 emsg(_("E365: Failed to print PostScript file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003240 else
3241 prt_message((char_u *)_("Print job sent."));
3242 }
3243
3244 mch_print_cleanup();
3245}
3246
3247 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003248mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003249{
3250 prt_flush_buffer();
3251
3252 prt_write_string("re sp\n");
3253
3254 prt_dsc_noarg("PageTrailer");
3255
3256 return !prt_file_error;
3257}
3258
Bram Moolenaar81366db2005-07-24 21:16:51 +00003259 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003260mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003261{
3262 int page_num[2];
3263
3264 prt_page_num++;
3265
3266 page_num[0] = page_num[1] = prt_page_num;
3267 prt_dsc_ints("Page", 2, page_num);
3268
3269 prt_dsc_noarg("BeginPageSetup");
3270
3271 prt_write_string("sv\n0 g\n");
3272#ifdef FEAT_MBYTE
3273 prt_in_ascii = !prt_out_mbyte;
3274 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003275 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003276 else
3277#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003278 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003279 prt_fgcol = PRCOLOR_BLACK;
3280 prt_bgcol = PRCOLOR_WHITE;
3281 prt_font = PRT_PS_FONT_ROMAN;
3282
3283 /* Set up page transformation for landscape printing. */
3284 if (!prt_portrait)
3285 {
3286 prt_write_int(-((int)prt_mediasize[prt_media].width));
3287 prt_write_string("sl\n");
3288 }
3289
3290 prt_dsc_noarg("EndPageSetup");
3291
3292 /* We have reset the font attributes, force setting them again. */
3293 curr_bg = (long_u)0xffffffff;
3294 curr_fg = (long_u)0xffffffff;
3295 curr_bold = MAYBE;
3296
3297 return !prt_file_error;
3298}
3299
3300 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003301mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003302{
3303 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3304}
3305
3306static float prt_pos_x = 0;
3307static float prt_pos_y = 0;
3308
3309 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003310mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003311{
3312 prt_pos_x = prt_left_margin;
3313 if (margin)
3314 prt_pos_x -= prt_number_width;
3315
3316 prt_pos_y = prt_top_margin - prt_first_line_height -
3317 page_line * prt_line_height;
3318
3319 prt_attribute_change = TRUE;
3320 prt_need_moveto = TRUE;
3321#ifdef FEAT_MBYTE
3322 prt_half_width = FALSE;
3323#endif
3324}
3325
Bram Moolenaar81366db2005-07-24 21:16:51 +00003326 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003327mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003328{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003329 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003330 int need_break;
3331 char_u ch;
3332 char_u ch_buff[8];
3333 float char_width;
3334 float next_pos;
3335#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003336 int in_ascii;
3337 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003338 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003339#endif
3340
3341 char_width = prt_char_width;
3342
3343#ifdef FEAT_MBYTE
3344 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3345 * CJKV font to do what VIM is doing here - use a Roman font for characters
Bram Moolenaarb15c8332007-05-10 18:40:02 +00003346 * in the ASCII range, and the original CID font for everything else.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003347 * The problem is that GhostScript still (as of 8.13) does not support
3348 * rearranged fonts even though they have been documented by Adobe for 7
3349 * years! If they ever do, a lot of this code will disappear.
3350 */
3351 if (prt_use_courier)
3352 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003353 in_ascii = (len == 1 && *p < 0x80);
3354 if (prt_in_ascii)
3355 {
3356 if (!in_ascii)
3357 {
3358 /* No longer in ASCII range - need to switch font */
3359 prt_in_ascii = FALSE;
3360 prt_need_font = TRUE;
3361 prt_attribute_change = TRUE;
3362 }
3363 }
3364 else if (in_ascii)
3365 {
3366 /* Now in ASCII range - need to switch font */
3367 prt_in_ascii = TRUE;
3368 prt_need_font = TRUE;
3369 prt_attribute_change = TRUE;
3370 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003371 }
3372 if (prt_out_mbyte)
3373 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003374 half_width = ((*mb_ptr2cells)(p) == 1);
3375 if (half_width)
3376 char_width /= 2;
3377 if (prt_half_width)
3378 {
3379 if (!half_width)
3380 {
3381 prt_half_width = FALSE;
3382 prt_pos_x += prt_char_width/4;
3383 prt_need_moveto = TRUE;
3384 prt_attribute_change = TRUE;
3385 }
3386 }
3387 else if (half_width)
3388 {
3389 prt_half_width = TRUE;
3390 prt_pos_x += prt_char_width/4;
3391 prt_need_moveto = TRUE;
3392 prt_attribute_change = TRUE;
3393 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003394 }
3395#endif
3396
3397 /* Output any required changes to the graphics state, after flushing any
3398 * text buffered so far.
3399 */
3400 if (prt_attribute_change)
3401 {
3402 prt_flush_buffer();
3403 /* Reset count of number of chars that will be printed */
3404 prt_text_run = 0;
3405
3406 if (prt_need_moveto)
3407 {
3408 prt_pos_x_moveto = prt_pos_x;
3409 prt_pos_y_moveto = prt_pos_y;
3410 prt_do_moveto = TRUE;
3411
3412 prt_need_moveto = FALSE;
3413 }
3414 if (prt_need_font)
3415 {
3416#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003417 if (!prt_in_ascii)
3418 prt_write_string("CF");
3419 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003420#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003421 prt_write_string("F");
3422 prt_write_int(prt_font);
3423 prt_write_string("sf\n");
3424 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003425 }
3426 if (prt_need_fgcol)
3427 {
3428 int r, g, b;
3429 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3430 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3431 b = prt_fgcol & 0xff;
3432
3433 prt_write_real(r / 255.0, 3);
3434 if (r == g && g == b)
3435 prt_write_string("g\n");
3436 else
3437 {
3438 prt_write_real(g / 255.0, 3);
3439 prt_write_real(b / 255.0, 3);
3440 prt_write_string("r\n");
3441 }
3442 prt_need_fgcol = FALSE;
3443 }
3444
3445 if (prt_bgcol != PRCOLOR_WHITE)
3446 {
3447 prt_new_bgcol = prt_bgcol;
3448 if (prt_need_bgcol)
3449 prt_do_bgcol = TRUE;
3450 }
3451 else
3452 prt_do_bgcol = FALSE;
3453 prt_need_bgcol = FALSE;
3454
3455 if (prt_need_underline)
3456 prt_do_underline = prt_underline;
3457 prt_need_underline = FALSE;
3458
3459 prt_attribute_change = FALSE;
3460 }
3461
3462#ifdef FEAT_MBYTE
3463 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003464 {
Bram Moolenaar81366db2005-07-24 21:16:51 +00003465 /* Convert from multi-byte to 8-bit encoding */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003466 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003467 if (p == NULL)
3468 {
3469 p = (char_u *)"";
3470 len = 0;
3471 }
3472 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003473
3474 if (prt_out_mbyte)
3475 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003476 /* Multi-byte character strings are represented more efficiently as hex
3477 * strings when outputting clean 8 bit PS.
3478 */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003479 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003480 {
3481 ch = prt_hexchar[(unsigned)(*p) >> 4];
3482 ga_append(&prt_ps_buffer, ch);
3483 ch = prt_hexchar[(*p) & 0xf];
3484 ga_append(&prt_ps_buffer, ch);
3485 p++;
3486 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003487 }
3488 else
3489#endif
3490 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003491 /* Add next character to buffer of characters to output.
3492 * Note: One printed character may require several PS characters to
3493 * represent it, but we only count them as one printed character.
3494 */
3495 ch = *p;
3496 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3497 {
3498 /* Convert non-printing characters to either their escape or octal
3499 * sequence, ensures PS sent over a serial line does not interfere
3500 * with the comms protocol. Note: For EBCDIC we need to write out
3501 * the escape sequences as ASCII codes!
Bram Moolenaar81366db2005-07-24 21:16:51 +00003502 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3503 */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003504 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3505 switch (ch)
3506 {
3507 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3508 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3509 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3510 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3511 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3512 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3513 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3514 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003515
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003516 default:
3517 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003518#ifdef EBCDIC
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003519 ebcdic2ascii(ch_buff, 3);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003520#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003521 ga_append(&prt_ps_buffer, ch_buff[0]);
3522 ga_append(&prt_ps_buffer, ch_buff[1]);
3523 ga_append(&prt_ps_buffer, ch_buff[2]);
3524 break;
3525 }
3526 }
3527 else
3528 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003529 }
3530
3531#ifdef FEAT_MBYTE
3532 /* Need to free any translated characters */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003533 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003534#endif
3535
3536 prt_text_run += char_width;
3537 prt_pos_x += char_width;
3538
3539 /* The downside of fp - use relative error on right margin check */
3540 next_pos = prt_pos_x + prt_char_width;
3541 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003542 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003543
3544 if (need_break)
3545 prt_flush_buffer();
3546
3547 return need_break;
3548}
3549
3550 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003551mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003552{
3553 int font = 0;
3554
3555 if (iBold)
3556 font |= 0x01;
3557 if (iItalic)
3558 font |= 0x02;
3559
3560 if (font != prt_font)
3561 {
3562 prt_font = font;
3563 prt_attribute_change = TRUE;
3564 prt_need_font = TRUE;
3565 }
3566 if (prt_underline != iUnderline)
3567 {
3568 prt_underline = iUnderline;
3569 prt_attribute_change = TRUE;
3570 prt_need_underline = TRUE;
3571 }
3572}
3573
3574 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003575mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003576{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003577 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003578 prt_attribute_change = TRUE;
3579 prt_need_bgcol = TRUE;
3580}
3581
3582 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003583mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003584{
3585 if (fgcol != (long_u)prt_fgcol)
3586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003587 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003588 prt_attribute_change = TRUE;
3589 prt_need_fgcol = TRUE;
3590 }
3591}
3592
3593# endif /*FEAT_POSTSCRIPT*/
3594#endif /*FEAT_PRINTER*/