blob: 44e9d4fe8444cd078742dc0965f588462cf20ce3 [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 Moolenaar92b8b2d2016-01-29 22:36:45 +0100138static char_u *parse_list_options(char_u *option_str, option_table_T *table, int table_size);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000139
140#ifdef FEAT_SYN_HL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100141static long_u darken_rgb(long_u rgb);
142static long_u prt_get_term_color(int colorindex);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000143#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100144static void prt_set_fg(long_u fg);
145static void prt_set_bg(long_u bg);
146static void prt_set_font(int bold, int italic, int underline);
147static void prt_line_number(prt_settings_T *psettings, int page_line, linenr_T lnum);
148static void prt_header(prt_settings_T *psettings, int pagenum, linenr_T lnum);
149static void prt_message(char_u *s);
150static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T *ppos);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000151#ifdef FEAT_SYN_HL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void prt_get_attr(int hl_id, prt_text_attr_T* pattr, int modec);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000153#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000154
155/*
156 * Parse 'printoptions' and set the flags in "printer_opts".
157 * Returns an error message or NULL;
158 */
159 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100160parse_printoptions(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000161{
162 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
163}
164
165#if (defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
166/*
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200167 * Parse 'printmbfont' and set the flags in "mbfont_opts".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000168 * Returns an error message or NULL;
169 */
170 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100171parse_printmbfont(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000172{
173 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
174}
175#endif
176
177/*
178 * Parse a list of options in the form
179 * option:value,option:value,option:value
180 *
181 * "value" can start with a number which is parsed out, e.g. margin:12mm
182 *
183 * Returns an error message for an illegal option, NULL otherwise.
184 * Only used for the printer at the moment...
185 */
186 static char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100187parse_list_options(
188 char_u *option_str,
189 option_table_T *table,
190 int table_size)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000191{
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200192 option_table_T *old_opts;
193 char_u *ret = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000194 char_u *stringp;
195 char_u *colonp;
196 char_u *commap;
197 char_u *p;
198 int idx = 0; /* init for GCC */
199 int len;
200
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200201 /* Save the old values, so that they can be restored in case of an error. */
202 old_opts = (option_table_T *)alloc(sizeof(option_table_T) * table_size);
203 if (old_opts == NULL)
204 return NULL;
205
Bram Moolenaar81366db2005-07-24 21:16:51 +0000206 for (idx = 0; idx < table_size; ++idx)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200207 {
208 old_opts[idx] = table[idx];
Bram Moolenaar81366db2005-07-24 21:16:51 +0000209 table[idx].present = FALSE;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200210 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000211
212 /*
213 * Repeat for all comma separated parts.
214 */
215 stringp = option_str;
216 while (*stringp)
217 {
218 colonp = vim_strchr(stringp, ':');
219 if (colonp == NULL)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200220 {
221 ret = (char_u *)N_("E550: Missing colon");
222 break;
223 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000224 commap = vim_strchr(stringp, ',');
225 if (commap == NULL)
226 commap = option_str + STRLEN(option_str);
227
228 len = (int)(colonp - stringp);
229
230 for (idx = 0; idx < table_size; ++idx)
231 if (STRNICMP(stringp, table[idx].name, len) == 0)
232 break;
233
234 if (idx == table_size)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200235 {
236 ret = (char_u *)N_("E551: Illegal component");
237 break;
238 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000239 p = colonp + 1;
240 table[idx].present = TRUE;
241
242 if (table[idx].hasnum)
243 {
244 if (!VIM_ISDIGIT(*p))
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200245 {
246 ret = (char_u *)N_("E552: digit expected");
247 break;
248 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000249
250 table[idx].number = getdigits(&p); /*advances p*/
251 }
252
253 table[idx].string = p;
254 table[idx].strlen = (int)(commap - p);
255
256 stringp = commap;
257 if (*stringp == ',')
258 ++stringp;
259 }
260
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200261 if (ret != NULL)
262 {
263 /* Restore old options in case of error */
264 for (idx = 0; idx < table_size; ++idx)
265 table[idx] = old_opts[idx];
266 }
267 vim_free(old_opts);
268 return ret;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000269}
270
271
272#ifdef FEAT_SYN_HL
273/*
274 * If using a dark background, the colors will probably be too bright to show
275 * up well on white paper, so reduce their brightness.
276 */
277 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100278darken_rgb(long_u rgb)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000279{
280 return ((rgb >> 17) << 16)
281 + (((rgb & 0xff00) >> 9) << 8)
282 + ((rgb & 0xff) >> 1);
283}
284
285 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100286prt_get_term_color(int colorindex)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000287{
288 /* TODO: Should check for xterm with 88 or 256 colors. */
289 if (t_colors > 8)
290 return cterm_color_16[colorindex % 16];
291 return cterm_color_8[colorindex % 8];
292}
293
294 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100295prt_get_attr(
296 int hl_id,
297 prt_text_attr_T *pattr,
298 int modec)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000299{
300 int colorindex;
301 long_u fg_color;
302 long_u bg_color;
303 char *color;
304
305 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
306 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
307 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
308 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
309
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200310# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200311 if (USE_24BIT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000312 {
313 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
314 if (bg_color == PRCOLOR_BLACK)
315 bg_color = PRCOLOR_WHITE;
316
317 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
318 }
319 else
320# endif
321 {
322 bg_color = PRCOLOR_WHITE;
323
324 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
325 if (color == NULL)
326 colorindex = 0;
327 else
328 colorindex = atoi(color);
329
330 if (colorindex >= 0 && colorindex < t_colors)
331 fg_color = prt_get_term_color(colorindex);
332 else
333 fg_color = PRCOLOR_BLACK;
334 }
335
336 if (fg_color == PRCOLOR_WHITE)
337 fg_color = PRCOLOR_BLACK;
338 else if (*p_bg == 'd')
339 fg_color = darken_rgb(fg_color);
340
341 pattr->fg_color = fg_color;
342 pattr->bg_color = bg_color;
343}
344#endif /* FEAT_SYN_HL */
345
346 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100347prt_set_fg(long_u fg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000348{
349 if (fg != curr_fg)
350 {
351 curr_fg = fg;
352 mch_print_set_fg(fg);
353 }
354}
355
356 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100357prt_set_bg(long_u bg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000358{
359 if (bg != curr_bg)
360 {
361 curr_bg = bg;
362 mch_print_set_bg(bg);
363 }
364}
365
366 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100367prt_set_font(int bold, int italic, int underline)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000368{
369 if (curr_bold != bold
370 || curr_italic != italic
371 || curr_underline != underline)
372 {
373 curr_underline = underline;
374 curr_italic = italic;
375 curr_bold = bold;
376 mch_print_set_font(bold, italic, underline);
377 }
378}
379
380/*
381 * Print the line number in the left margin.
382 */
383 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100384prt_line_number(
385 prt_settings_T *psettings,
386 int page_line,
387 linenr_T lnum)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000388{
389 int i;
390 char_u tbuf[20];
391
392 prt_set_fg(psettings->number.fg_color);
393 prt_set_bg(psettings->number.bg_color);
394 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
395 mch_print_start_line(TRUE, page_line);
396
397 /* Leave two spaces between the number and the text; depends on
398 * PRINT_NUMBER_WIDTH. */
399 sprintf((char *)tbuf, "%6ld", (long)lnum);
400 for (i = 0; i < 6; i++)
401 (void)mch_print_text_out(&tbuf[i], 1);
402
403#ifdef FEAT_SYN_HL
404 if (psettings->do_syntax)
405 /* Set colors for next character. */
406 current_syn_id = -1;
407 else
408#endif
409 {
410 /* Set colors and font back to normal. */
411 prt_set_fg(PRCOLOR_BLACK);
412 prt_set_bg(PRCOLOR_WHITE);
413 prt_set_font(FALSE, FALSE, FALSE);
414 }
415}
416
Bram Moolenaar81366db2005-07-24 21:16:51 +0000417/*
418 * Get the currently effective header height.
419 */
420 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100421prt_header_height(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000422{
423 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
424 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
425 return 2;
426}
427
428/*
429 * Return TRUE if using a line number for printing.
430 */
431 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100432prt_use_number(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000433{
434 return (printer_opts[OPT_PRINT_NUMBER].present
435 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
436}
437
438/*
439 * Return the unit used in a margin item in 'printoptions'.
440 * Returns PRT_UNIT_NONE if not recognized.
441 */
442 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100443prt_get_unit(int idx)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000444{
445 int u = PRT_UNIT_NONE;
446 int i;
447 static char *(units[4]) = PRT_UNIT_NAMES;
448
449 if (printer_opts[idx].present)
450 for (i = 0; i < 4; ++i)
451 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
452 {
453 u = i;
454 break;
455 }
456 return u;
457}
458
459/*
460 * Print the page header.
461 */
Bram Moolenaar81366db2005-07-24 21:16:51 +0000462 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100463prt_header(
464 prt_settings_T *psettings,
465 int pagenum,
466 linenr_T lnum UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000467{
468 int width = psettings->chars_per_line;
469 int page_line;
470 char_u *tbuf;
471 char_u *p;
472#ifdef FEAT_MBYTE
473 int l;
474#endif
475
476 /* Also use the space for the line number. */
477 if (prt_use_number())
478 width += PRINT_NUMBER_WIDTH;
479
480 tbuf = alloc(width + IOSIZE);
481 if (tbuf == NULL)
482 return;
483
484#ifdef FEAT_STL_OPT
485 if (*p_header != NUL)
486 {
487 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000488 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000489
490 /*
491 * Need to (temporarily) set current line number and first/last line
492 * number on the 'window'. Since we don't know how long the page is,
493 * set the first and current line number to the top line, and guess
494 * that the page length is 64.
495 */
496 tmp_lnum = curwin->w_cursor.lnum;
497 tmp_topline = curwin->w_topline;
498 tmp_botline = curwin->w_botline;
499 curwin->w_cursor.lnum = lnum;
500 curwin->w_topline = lnum;
501 curwin->w_botline = lnum + 63;
502 printer_page_num = pagenum;
503
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000504# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000505 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000506# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000507 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000508 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000509 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000510
511 /* Reset line numbers */
512 curwin->w_cursor.lnum = tmp_lnum;
513 curwin->w_topline = tmp_topline;
514 curwin->w_botline = tmp_botline;
515 }
516 else
517#endif
518 sprintf((char *)tbuf, _("Page %d"), pagenum);
519
520 prt_set_fg(PRCOLOR_BLACK);
521 prt_set_bg(PRCOLOR_WHITE);
522 prt_set_font(TRUE, FALSE, FALSE);
523
524 /* Use a negative line number to indicate printing in the top margin. */
525 page_line = 0 - prt_header_height();
526 mch_print_start_line(TRUE, page_line);
527 for (p = tbuf; *p != NUL; )
528 {
529 if (mch_print_text_out(p,
530#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000531 (l = (*mb_ptr2len)(p))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000532#else
533 1
534#endif
535 ))
536 {
537 ++page_line;
538 if (page_line >= 0) /* out of room in header */
539 break;
540 mch_print_start_line(TRUE, page_line);
541 }
542#ifdef FEAT_MBYTE
543 p += l;
544#else
545 p++;
546#endif
547 }
548
549 vim_free(tbuf);
550
551#ifdef FEAT_SYN_HL
552 if (psettings->do_syntax)
553 /* Set colors for next character. */
554 current_syn_id = -1;
555 else
556#endif
557 {
558 /* Set colors and font back to normal. */
559 prt_set_fg(PRCOLOR_BLACK);
560 prt_set_bg(PRCOLOR_WHITE);
561 prt_set_font(FALSE, FALSE, FALSE);
562 }
563}
564
565/*
566 * Display a print status message.
567 */
568 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100569prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000570{
571 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100572 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000573 out_flush();
574}
575
576 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100577ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000578{
579 linenr_T lnum;
580 int collated_copies, uncollated_copies;
581 prt_settings_T settings;
582 long_u bytes_to_print = 0;
583 int page_line;
584 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000585
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200586 vim_memset(&settings, 0, sizeof(prt_settings_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000587 settings.has_color = TRUE;
588
589# ifdef FEAT_POSTSCRIPT
590 if (*eap->arg == '>')
591 {
592 char_u *errormsg = NULL;
593
594 /* Expand things like "%.ps". */
595 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
596 {
597 if (errormsg != NULL)
598 EMSG(errormsg);
599 return;
600 }
601 settings.outfile = skipwhite(eap->arg + 1);
602 }
603 else if (*eap->arg != NUL)
604 settings.arguments = eap->arg;
605# endif
606
607 /*
608 * Initialise for printing. Ask the user for settings, unless forceit is
609 * set.
610 * The mch_print_init() code should set up margins if applicable. (It may
611 * not be a real printer - for example the engine might generate HTML or
612 * PS.)
613 */
614 if (mch_print_init(&settings,
615 curbuf->b_fname == NULL
616 ? (char_u *)buf_spname(curbuf)
617 : curbuf->b_sfname == NULL
618 ? curbuf->b_fname
619 : curbuf->b_sfname,
620 eap->forceit) == FAIL)
621 return;
622
623#ifdef FEAT_SYN_HL
624# ifdef FEAT_GUI
625 if (gui.in_use)
626 settings.modec = 'g';
627 else
628# endif
629 if (t_colors > 1)
630 settings.modec = 'c';
631 else
632 settings.modec = 't';
633
Bram Moolenaar860cae12010-06-05 23:22:07 +0200634 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000635 settings.do_syntax = FALSE;
636 else if (printer_opts[OPT_PRINT_SYNTAX].present
637 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
638 settings.do_syntax =
639 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
640 else
641 settings.do_syntax = settings.has_color;
642#endif
643
644 /* Set up printing attributes for line numbers */
645 settings.number.fg_color = PRCOLOR_BLACK;
646 settings.number.bg_color = PRCOLOR_WHITE;
647 settings.number.bold = FALSE;
648 settings.number.italic = TRUE;
649 settings.number.underline = FALSE;
650#ifdef FEAT_SYN_HL
651 /*
652 * Syntax highlighting of line numbers.
653 */
654 if (prt_use_number() && settings.do_syntax)
655 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000656 int id;
657
Bram Moolenaar81366db2005-07-24 21:16:51 +0000658 id = syn_name2id((char_u *)"LineNr");
659 if (id > 0)
660 id = syn_get_final_id(id);
661
662 prt_get_attr(id, &settings.number, settings.modec);
663 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000664#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000665
666 /*
667 * Estimate the total lines to be printed
668 */
669 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
670 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
671 if (bytes_to_print == 0)
672 {
673 MSG(_("No text to be printed"));
674 goto print_fail_no_begin;
675 }
676
677 /* Set colors and font to normal. */
678 curr_bg = (long_u)0xffffffffL;
679 curr_fg = (long_u)0xffffffffL;
680 curr_italic = MAYBE;
681 curr_bold = MAYBE;
682 curr_underline = MAYBE;
683
684 prt_set_fg(PRCOLOR_BLACK);
685 prt_set_bg(PRCOLOR_WHITE);
686 prt_set_font(FALSE, FALSE, FALSE);
687#ifdef FEAT_SYN_HL
688 current_syn_id = -1;
689#endif
690
691 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
692 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
693
694 if (!mch_print_begin(&settings))
695 goto print_fail_no_begin;
696
697 /*
698 * Loop over collated copies: 1 2 3, 1 2 3, ...
699 */
700 page_count = 0;
701 for (collated_copies = 0;
702 collated_copies < settings.n_collated_copies;
703 collated_copies++)
704 {
705 prt_pos_T prtpos; /* current print position */
706 prt_pos_T page_prtpos; /* print position at page start */
707 int side;
708
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200709 vim_memset(&page_prtpos, 0, sizeof(prt_pos_T));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000710 page_prtpos.file_line = eap->line1;
711 prtpos = page_prtpos;
712
713 if (jobsplit && collated_copies > 0)
714 {
715 /* Splitting jobs: Stop a previous job and start a new one. */
716 mch_print_end(&settings);
717 if (!mch_print_begin(&settings))
718 goto print_fail_no_begin;
719 }
720
721 /*
722 * Loop over all pages in the print job: 1 2 3 ...
723 */
724 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
725 {
726 /*
727 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
728 * For duplex: 12 12 12 34 34 34, ...
729 */
730 for (uncollated_copies = 0;
731 uncollated_copies < settings.n_uncollated_copies;
732 uncollated_copies++)
733 {
734 /* Set the print position to the start of this page. */
735 prtpos = page_prtpos;
736
737 /*
738 * Do front and rear side of a page.
739 */
740 for (side = 0; side <= settings.duplex; ++side)
741 {
742 /*
743 * Print one page.
744 */
745
746 /* Check for interrupt character every page. */
747 ui_breakcheck();
748 if (got_int || settings.user_abort)
749 goto print_fail;
750
751 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
752 page_count + 1 + side,
753 prtpos.bytes_printed > 1000000
754 ? (int)(prtpos.bytes_printed /
755 (bytes_to_print / 100))
756 : (int)((prtpos.bytes_printed * 100)
757 / bytes_to_print));
758 if (!mch_print_begin_page(IObuff))
759 goto print_fail;
760
761 if (settings.n_collated_copies > 1)
762 sprintf((char *)IObuff + STRLEN(IObuff),
763 _(" Copy %d of %d"),
764 collated_copies + 1,
765 settings.n_collated_copies);
766 prt_message(IObuff);
767
768 /*
769 * Output header if required
770 */
771 if (prt_header_height() > 0)
772 prt_header(&settings, page_count + 1 + side,
773 prtpos.file_line);
774
775 for (page_line = 0; page_line < settings.lines_per_page;
776 ++page_line)
777 {
778 prtpos.column = hardcopy_line(&settings,
779 page_line, &prtpos);
780 if (prtpos.column == 0)
781 {
782 /* finished a file line */
783 prtpos.bytes_printed +=
784 STRLEN(skipwhite(ml_get(prtpos.file_line)));
785 if (++prtpos.file_line > eap->line2)
786 break; /* reached the end */
787 }
788 else if (prtpos.ff)
789 {
790 /* Line had a formfeed in it - start new page but
791 * stay on the current line */
792 break;
793 }
794 }
795
796 if (!mch_print_end_page())
797 goto print_fail;
798 if (prtpos.file_line > eap->line2)
799 break; /* reached the end */
800 }
801
802 /*
803 * Extra blank page for duplexing with odd number of pages and
804 * more copies to come.
805 */
806 if (prtpos.file_line > eap->line2 && settings.duplex
807 && side == 0
808 && uncollated_copies + 1 < settings.n_uncollated_copies)
809 {
810 if (!mch_print_blank_page())
811 goto print_fail;
812 }
813 }
814 if (settings.duplex && prtpos.file_line <= eap->line2)
815 ++page_count;
816
817 /* Remember the position where the next page starts. */
818 page_prtpos = prtpos;
819 }
820
821 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
822 settings.jobname);
823 prt_message(IObuff);
824 }
825
826print_fail:
827 if (got_int || settings.user_abort)
828 {
829 sprintf((char *)IObuff, "%s", _("Printing aborted"));
830 prt_message(IObuff);
831 }
832 mch_print_end(&settings);
833
834print_fail_no_begin:
835 mch_print_cleanup();
836}
837
838/*
839 * Print one page line.
840 * Return the next column to print, or zero if the line is finished.
841 */
842 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100843hardcopy_line(
844 prt_settings_T *psettings,
845 int page_line,
846 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000847{
848 colnr_T col;
849 char_u *line;
850 int need_break = FALSE;
851 int outputlen;
852 int tab_spaces;
853 long_u print_pos;
854#ifdef FEAT_SYN_HL
855 prt_text_attr_T attr;
856 int id;
857#endif
858
859 if (ppos->column == 0 || ppos->ff)
860 {
861 print_pos = 0;
862 tab_spaces = 0;
863 if (!ppos->ff && prt_use_number())
864 prt_line_number(psettings, page_line, ppos->file_line);
865 ppos->ff = FALSE;
866 }
867 else
868 {
869 /* left over from wrap halfway a tab */
870 print_pos = ppos->print_pos;
871 tab_spaces = ppos->lead_spaces;
872 }
873
874 mch_print_start_line(0, page_line);
875 line = ml_get(ppos->file_line);
876
877 /*
878 * Loop over the columns until the end of the file line or right margin.
879 */
880 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
881 {
882 outputlen = 1;
883#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000884 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000885 outputlen = 1;
886#endif
887#ifdef FEAT_SYN_HL
888 /*
889 * syntax highlighting stuff.
890 */
891 if (psettings->do_syntax)
892 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000893 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000894 if (id > 0)
895 id = syn_get_final_id(id);
896 else
897 id = 0;
898 /* Get the line again, a multi-line regexp may invalidate it. */
899 line = ml_get(ppos->file_line);
900
901 if (id != current_syn_id)
902 {
903 current_syn_id = id;
904 prt_get_attr(id, &attr, psettings->modec);
905 prt_set_font(attr.bold, attr.italic, attr.underline);
906 prt_set_fg(attr.fg_color);
907 prt_set_bg(attr.bg_color);
908 }
909 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000910#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000911
912 /*
913 * Appropriately expand any tabs to spaces.
914 */
915 if (line[col] == TAB || tab_spaces != 0)
916 {
917 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200918#ifdef FEAT_VARTABS
919 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
920 curbuf->b_p_vts_array);
921#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000922 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200923#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000924
925 while (tab_spaces > 0)
926 {
927 need_break = mch_print_text_out((char_u *)" ", 1);
928 print_pos++;
929 tab_spaces--;
930 if (need_break)
931 break;
932 }
933 /* Keep the TAB if we didn't finish it. */
934 if (need_break && tab_spaces > 0)
935 break;
936 }
937 else if (line[col] == FF
938 && printer_opts[OPT_PRINT_FORMFEED].present
939 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
940 == 'y')
941 {
942 ppos->ff = TRUE;
943 need_break = 1;
944 }
945 else
946 {
947 need_break = mch_print_text_out(line + col, outputlen);
948#ifdef FEAT_MBYTE
949 if (has_mbyte)
950 print_pos += (*mb_ptr2cells)(line + col);
951 else
952#endif
953 print_pos++;
954 }
955 }
956
957 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000958 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000959
960 /*
961 * Start next line of file if we clip lines, or have reached end of the
962 * line, unless we are doing a formfeed.
963 */
964 if (!ppos->ff
965 && (line[col] == NUL
966 || (printer_opts[OPT_PRINT_WRAP].present
967 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
968 == 'n')))
969 return 0;
970 return col;
971}
972
973# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
974
975/*
976 * PS printer stuff.
977 *
978 * Sources of information to help maintain the PS printing code:
979 *
980 * 1. PostScript Language Reference, 3rd Edition,
981 * Addison-Wesley, 1999, ISBN 0-201-37922-8
982 * 2. PostScript Language Program Design,
983 * Addison-Wesley, 1988, ISBN 0-201-14396-8
984 * 3. PostScript Tutorial and Cookbook,
985 * Addison Wesley, 1985, ISBN 0-201-10179-3
986 * 4. PostScript Language Document Structuring Conventions Specification,
987 * version 3.0,
988 * Adobe Technote 5001, 25th September 1992
989 * 5. PostScript Printer Description File Format Specification, Version 4.3,
990 * Adobe technote 5003, 9th February 1996
991 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
992 * Adobe Technote 5007, 7th October 1998
993 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
994 * Adobe Technote 5014, 8th October 1996
995 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
996 * Adoboe Technote 5094, 8th September, 2001
997 * 9. CJKV Information Processing, 2nd Edition,
998 * O'Reilly, 2002, ISBN 1-56592-224-7
999 *
1000 * Some of these documents can be found in PDF form on Adobe's web site -
1001 * http://www.adobe.com
1002 */
1003
1004#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
1005
1006#define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
1007#define PRT_PS_DEFAULT_FONTSIZE (10)
1008#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
1009
1010struct prt_mediasize_S
1011{
1012 char *name;
1013 float width; /* width and height in points for portrait */
1014 float height;
1015};
1016
1017#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
1018
1019static struct prt_mediasize_S prt_mediasize[] =
1020{
1021 {"A4", 595.0, 842.0},
1022 {"letter", 612.0, 792.0},
1023 {"10x14", 720.0, 1008.0},
1024 {"A3", 842.0, 1191.0},
1025 {"A5", 420.0, 595.0},
1026 {"B4", 729.0, 1032.0},
1027 {"B5", 516.0, 729.0},
1028 {"executive", 522.0, 756.0},
1029 {"folio", 595.0, 935.0},
1030 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1031 {"legal", 612.0, 1008.0},
1032 {"quarto", 610.0, 780.0},
1033 {"statement", 396.0, 612.0},
1034 {"tabloid", 792.0, 1224.0}
1035};
1036
1037/* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1038struct prt_ps_font_S
1039{
1040 int wx;
1041 int uline_offset;
1042 int uline_width;
1043 int bbox_min_y;
1044 int bbox_max_y;
1045 char *(ps_fontname[4]);
1046};
1047
1048#define PRT_PS_FONT_ROMAN (0)
1049#define PRT_PS_FONT_BOLD (1)
1050#define PRT_PS_FONT_OBLIQUE (2)
1051#define PRT_PS_FONT_BOLDOBLIQUE (3)
1052
1053/* Standard font metrics for Courier family */
1054static struct prt_ps_font_S prt_ps_courier_font =
1055{
1056 600,
1057 -100, 50,
1058 -250, 805,
1059 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1060};
1061
1062#ifdef FEAT_MBYTE
1063/* Generic font metrics for multi-byte fonts */
1064static struct prt_ps_font_S prt_ps_mb_font =
1065{
1066 1000,
1067 -100, 50,
1068 -250, 805,
1069 {NULL, NULL, NULL, NULL}
1070};
1071#endif
1072
1073/* Pointer to current font set being used */
1074static struct prt_ps_font_S* prt_ps_font;
1075
1076/* Structures to map user named encoding and mapping to PS equivalents for
1077 * building CID font name */
1078struct prt_ps_encoding_S
1079{
1080 char *encoding;
1081 char *cmap_encoding;
1082 int needs_charset;
1083};
1084
1085struct prt_ps_charset_S
1086{
1087 char *charset;
1088 char *cmap_charset;
1089 int has_charset;
1090};
1091
1092#ifdef FEAT_MBYTE
1093
1094#define CS_JIS_C_1978 (0x01)
1095#define CS_JIS_X_1983 (0x02)
1096#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001097#define CS_NEC (0x08)
1098#define CS_MSWINDOWS (0x10)
1099#define CS_CP932 (0x20)
1100#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001101#define CS_KANJITALK7 (0x80)
1102
1103/* Japanese encodings and charsets */
1104static struct prt_ps_encoding_S j_encodings[] =
1105{
1106 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001107 CS_NEC)},
1108 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1109 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1110 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001111 {"cp932", "RKSJ", CS_JIS_X_1983},
1112 {"ucs-2", "UCS2", CS_JIS_X_1990},
1113 {"utf-8", "UTF8" , CS_JIS_X_1990}
1114};
1115static struct prt_ps_charset_S j_charsets[] =
1116{
1117 {"JIS_C_1978", "78", CS_JIS_C_1978},
1118 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1119 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001120 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001121 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1122 {"CP932", "90ms", CS_JIS_X_1983},
1123 {"KANJITALK6", "83pv", CS_KANJITALK6},
1124 {"KANJITALK7", "90pv", CS_KANJITALK7}
1125};
1126
1127#define CS_GB_2312_80 (0x01)
1128#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001129#define CS_GBK2K (0x04)
1130#define CS_SC_MAC (0x08)
1131#define CS_GBT_90_MAC (0x10)
1132#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001133#define CS_SC_ISO10646 (0x40)
1134
1135/* Simplified Chinese encodings and charsets */
1136static struct prt_ps_encoding_S sc_encodings[] =
1137{
1138 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1139 {"gb18030", NULL, CS_GBK2K},
1140 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001141 CS_GBT_90_MAC)},
1142 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001143 {"ucs-2", "UCS2", CS_SC_ISO10646},
1144 {"utf-8", "UTF8", CS_SC_ISO10646}
1145};
1146static struct prt_ps_charset_S sc_charsets[] =
1147{
1148 {"GB_2312-80", "GB", CS_GB_2312_80},
1149 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001150 {"MAC", "GBpc", CS_SC_MAC},
1151 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1152 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001153 {"GB18030", "GBK2K", CS_GBK2K},
1154 {"ISO10646", "UniGB", CS_SC_ISO10646}
1155};
1156
1157#define CS_CNS_PLANE_1 (0x01)
1158#define CS_CNS_PLANE_2 (0x02)
1159#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001160#define CS_B5 (0x08)
1161#define CS_ETEN (0x10)
1162#define CS_HK_GCCS (0x20)
1163#define CS_HK_SCS (0x40)
1164#define CS_HK_SCS_ETEN (0x80)
1165#define CS_MTHKL (0x100)
1166#define CS_MTHKS (0x200)
1167#define CS_DLHKL (0x400)
1168#define CS_DLHKS (0x800)
1169#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001170
1171/* Traditional Chinese encodings and charsets */
1172static struct prt_ps_encoding_S tc_encodings[] =
1173{
1174 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1175 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001176 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1177 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1178 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001179 {"cp950", "B5", CS_B5},
1180 {"ucs-2", "UCS2", CS_TC_ISO10646},
1181 {"utf-8", "UTF8", CS_TC_ISO10646},
1182 {"utf-16", "UTF16", CS_TC_ISO10646},
1183 {"utf-32", "UTF32", CS_TC_ISO10646}
1184};
1185static struct prt_ps_charset_S tc_charsets[] =
1186{
1187 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1188 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1189 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001190 {"BIG5", NULL, CS_B5},
1191 {"CP950", NULL, CS_B5},
1192 {"ETEN", "ETen", CS_ETEN},
1193 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1194 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001195 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1196 {"MTHKL", "HKm471", CS_MTHKL},
1197 {"MTHKS", "HKm314", CS_MTHKS},
1198 {"DLHKL", "HKdla", CS_DLHKL},
1199 {"DLHKS", "HKdlb", CS_DLHKS},
1200 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1201};
1202
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001203#define CS_KR_X_1992 (0x01)
1204#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001205#define CS_KR_X_1992_MS (0x04)
1206#define CS_KR_ISO10646 (0x08)
1207
1208/* Korean encodings and charsets */
1209static struct prt_ps_encoding_S k_encodings[] =
1210{
1211 {"iso-2022-kr", NULL, CS_KR_X_1992},
1212 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1213 {"johab", "Johab", CS_KR_X_1992},
1214 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001215 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001216 {"cp949", "UHC", CS_KR_X_1992_MS},
1217 {"ucs-2", "UCS2", CS_KR_ISO10646},
1218 {"utf-8", "UTF8", CS_KR_ISO10646}
1219};
1220static struct prt_ps_charset_S k_charsets[] =
1221{
1222 {"KS_X_1992", "KSC", CS_KR_X_1992},
1223 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001224 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001225 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1226 {"CP949", "KSCms", CS_KR_X_1992_MS},
1227 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1228 {"ISO10646", "UniKS", CS_KR_ISO10646}
1229};
1230
1231/* Collections of encodings and charsets for multi-byte printing */
1232struct prt_ps_mbfont_S
1233{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001234 int num_encodings;
1235 struct prt_ps_encoding_S *encodings;
1236 int num_charsets;
1237 struct prt_ps_charset_S *charsets;
1238 char *ascii_enc;
1239 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001240};
1241
1242static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1243{
1244 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001245 NUM_ELEMENTS(j_encodings),
1246 j_encodings,
1247 NUM_ELEMENTS(j_charsets),
1248 j_charsets,
1249 "jis_roman",
1250 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001251 },
1252 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001253 NUM_ELEMENTS(sc_encodings),
1254 sc_encodings,
1255 NUM_ELEMENTS(sc_charsets),
1256 sc_charsets,
1257 "gb_roman",
1258 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001259 },
1260 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001261 NUM_ELEMENTS(tc_encodings),
1262 tc_encodings,
1263 NUM_ELEMENTS(tc_charsets),
1264 tc_charsets,
1265 "cns_roman",
1266 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001267 },
1268 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001269 NUM_ELEMENTS(k_encodings),
1270 k_encodings,
1271 NUM_ELEMENTS(k_charsets),
1272 k_charsets,
1273 "ks_roman",
1274 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001275 }
1276};
1277#endif /* FEAT_MBYTE */
1278
1279struct prt_ps_resource_S
1280{
1281 char_u name[64];
1282 char_u filename[MAXPATHL + 1];
1283 int type;
1284 char_u title[256];
1285 char_u version[256];
1286};
1287
1288/* Types of PS resource file currently used */
1289#define PRT_RESOURCE_TYPE_PROCSET (0)
1290#define PRT_RESOURCE_TYPE_ENCODING (1)
1291#define PRT_RESOURCE_TYPE_CMAP (2)
1292
1293/* The PS prolog file version number has to match - if the prolog file is
1294 * updated, increment the number in the file and here. Version checking was
1295 * added as of VIM 6.2.
1296 * The CID prolog file version number behaves as per PS prolog.
1297 * Table of VIM and prolog versions:
1298 *
1299 * VIM Prolog CIDProlog
1300 * 6.2 1.3
1301 * 7.0 1.4 1.0
1302 */
1303#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1304#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1305
1306/* String versions of PS resource types - indexed by constants above so don't
1307 * re-order!
1308 */
1309static char *prt_resource_types[] =
1310{
1311 "procset",
1312 "encoding",
1313 "cmap"
1314};
1315
1316/* Strings to look for in a PS resource file */
1317#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1318#define PRT_RESOURCE_RESOURCE "Resource-"
1319#define PRT_RESOURCE_PROCSET "ProcSet"
1320#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001321#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001322
1323
1324/* Data for table based DSC comment recognition, easy to extend if VIM needs to
1325 * read more comments. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001326#define PRT_DSC_MISC_TYPE (-1)
1327#define PRT_DSC_TITLE_TYPE (1)
1328#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001329#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1330
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001331#define PRT_DSC_TITLE "%%Title:"
1332#define PRT_DSC_VERSION "%%Version:"
1333#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001334
1335struct prt_dsc_comment_S
1336{
1337 char *string;
1338 int len;
1339 int type;
1340};
1341
1342struct prt_dsc_line_S
1343{
1344 int type;
1345 char_u *string;
1346 int len;
1347};
1348
1349
1350#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1351static struct prt_dsc_comment_S prt_dsc_table[] =
1352{
1353 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1354 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1355 PRT_DSC_VERSION_TYPE},
1356 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1357 PRT_DSC_ENDCOMMENTS_TYPE}
1358};
1359
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001360static void prt_write_file_raw_len(char_u *buffer, int bytes);
1361static void prt_write_file(char_u *buffer);
1362static void prt_write_file_len(char_u *buffer, int bytes);
1363static void prt_write_string(char *s);
1364static void prt_write_int(int i);
1365static void prt_write_boolean(int b);
1366static void prt_def_font(char *new_name, char *encoding, int height, char *font);
1367static void prt_real_bits(double real, int precision, int *pinteger, int *pfraction);
1368static void prt_write_real(double val, int prec);
1369static void prt_def_var(char *name, double value, int prec);
1370static void prt_flush_buffer(void);
1371static void prt_resource_name(char_u *filename, void *cookie);
1372static int prt_find_resource(char *name, struct prt_ps_resource_S *resource);
1373static int prt_open_resource(struct prt_ps_resource_S *resource);
1374static int prt_check_resource(struct prt_ps_resource_S *resource, char_u *version);
1375static void prt_dsc_start(void);
1376static void prt_dsc_noarg(char *comment);
1377static void prt_dsc_textline(char *comment, char *text);
1378static void prt_dsc_text(char *comment, char *text);
1379static void prt_dsc_ints(char *comment, int count, int *ints);
1380static void prt_dsc_requirements(int duplex, int tumble, int collate, int color, int num_copies);
1381static void prt_dsc_docmedia(char *paper_name, double width, double height, double weight, char *colour, char *type);
1382static void prt_dsc_resources(char *comment, char *type, char *strings);
1383static void prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font);
1384static float to_device_units(int idx, double physsize, int def_number);
1385static void prt_page_margins(double width, double height, double *left, double *right, double *top, double *bottom);
1386static void prt_font_metrics(int font_scale);
1387static int prt_get_cpl(void);
1388static int prt_get_lpp(void);
1389static int prt_add_resource(struct prt_ps_resource_S *resource);
1390static int prt_resfile_next_line(void);
1391static int prt_resfile_strncmp(int offset, char *string, int len);
1392static int prt_resfile_skip_nonws(int offset);
1393static int prt_resfile_skip_ws(int offset);
1394static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001395#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001396static int prt_build_cid_fontname(int font, char_u *name, int name_len);
1397static void prt_def_cidfont(char *new_name, int height, char *cidfont);
1398static void prt_dup_cidfont(char *original_name, char *new_name);
1399static int prt_match_encoding(char *p_encoding, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_encoding_S **pp_mbenc);
1400static int prt_match_charset(char *p_charset, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_charset_S **pp_mbchar);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001401#endif
1402
1403/*
1404 * Variables for the output PostScript file.
1405 */
1406static FILE *prt_ps_fd;
1407static int prt_file_error;
1408static char_u *prt_ps_file_name = NULL;
1409
1410/*
1411 * Various offsets and dimensions in default PostScript user space (points).
1412 * Used for text positioning calculations
1413 */
1414static float prt_page_width;
1415static float prt_page_height;
1416static float prt_left_margin;
1417static float prt_right_margin;
1418static float prt_top_margin;
1419static float prt_bottom_margin;
1420static float prt_line_height;
1421static float prt_first_line_height;
1422static float prt_char_width;
1423static float prt_number_width;
1424static float prt_bgcol_offset;
1425static float prt_pos_x_moveto = 0.0;
1426static float prt_pos_y_moveto = 0.0;
1427
1428/*
1429 * Various control variables used to decide when and how to change the
1430 * PostScript graphics state.
1431 */
1432static int prt_need_moveto;
1433static int prt_do_moveto;
1434static int prt_need_font;
1435static int prt_font;
1436static int prt_need_underline;
1437static int prt_underline;
1438static int prt_do_underline;
1439static int prt_need_fgcol;
1440static int prt_fgcol;
1441static int prt_need_bgcol;
1442static int prt_do_bgcol;
1443static int prt_bgcol;
1444static int prt_new_bgcol;
1445static int prt_attribute_change;
1446static float prt_text_run;
1447static int prt_page_num;
1448static int prt_bufsiz;
1449
1450/*
1451 * Variables controlling physical printing.
1452 */
1453static int prt_media;
1454static int prt_portrait;
1455static int prt_num_copies;
1456static int prt_duplex;
1457static int prt_tumble;
1458static int prt_collate;
1459
1460/*
1461 * Buffers used when generating PostScript output
1462 */
1463static char_u prt_line_buffer[257];
1464static garray_T prt_ps_buffer;
1465
1466# ifdef FEAT_MBYTE
1467static int prt_do_conv;
1468static vimconv_T prt_conv;
1469
1470static int prt_out_mbyte;
1471static int prt_custom_cmap;
1472static char prt_cmap[80];
1473static int prt_use_courier;
1474static int prt_in_ascii;
1475static int prt_half_width;
1476static char *prt_ascii_encoding;
1477static char_u prt_hexchar[] = "0123456789abcdef";
1478# endif
1479
1480 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001481prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001482{
1483 if (!prt_file_error
1484 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1485 != (size_t)bytes)
1486 {
1487 EMSG(_("E455: Error writing to PostScript output file"));
1488 prt_file_error = TRUE;
1489 }
1490}
1491
1492 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001493prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001494{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001496}
1497
1498 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001499prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001500{
1501#ifdef EBCDIC
1502 ebcdic2ascii(buffer, bytes);
1503#endif
1504 prt_write_file_raw_len(buffer, bytes);
1505}
1506
1507/*
1508 * Write a string.
1509 */
1510 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001511prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001512{
1513 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1514 prt_write_file(prt_line_buffer);
1515}
1516
1517/*
1518 * Write an int and a space.
1519 */
1520 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001521prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001522{
1523 sprintf((char *)prt_line_buffer, "%d ", i);
1524 prt_write_file(prt_line_buffer);
1525}
1526
1527/*
1528 * Write a boolean and a space.
1529 */
1530 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001531prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001532{
1533 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1534 prt_write_file(prt_line_buffer);
1535}
1536
1537/*
1538 * Write PostScript to re-encode and define the font.
1539 */
1540 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001541prt_def_font(
1542 char *new_name,
1543 char *encoding,
1544 int height,
1545 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001546{
1547 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1548 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1549 prt_write_file(prt_line_buffer);
1550#ifdef FEAT_MBYTE
1551 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001552 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001553 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1554 else
1555#endif
1556 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1557 "/%s %d /_%s ffs\n", new_name, height, new_name);
1558 prt_write_file(prt_line_buffer);
1559}
1560
1561#ifdef FEAT_MBYTE
1562/*
1563 * Write a line to define the CID font.
1564 */
1565 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001566prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001567{
1568 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1569 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1570 prt_write_file(prt_line_buffer);
1571 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1572 "/%s %d /_%s ffs\n", new_name, height, new_name);
1573 prt_write_file(prt_line_buffer);
1574}
1575
1576/*
1577 * Write a line to define a duplicate of a CID font
1578 */
1579 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001580prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001581{
1582 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1583 "/%s %s d\n", new_name, original_name);
1584 prt_write_file(prt_line_buffer);
1585}
1586#endif
1587
1588/*
1589 * Convert a real value into an integer and fractional part as integers, with
1590 * the fractional part being in the range [0,10^precision). The fractional part
1591 * is also rounded based on the precision + 1'th fractional digit.
1592 */
1593 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001594prt_real_bits(
1595 double real,
1596 int precision,
1597 int *pinteger,
1598 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001599{
1600 int i;
1601 int integer;
1602 float fraction;
1603
1604 integer = (int)real;
1605 fraction = (float)(real - integer);
1606 if (real < (double)integer)
1607 fraction = -fraction;
1608 for (i = 0; i < precision; i++)
1609 fraction *= 10.0;
1610
1611 *pinteger = integer;
1612 *pfraction = (int)(fraction + 0.5);
1613}
1614
1615/*
1616 * Write a real and a space. Save bytes if real value has no fractional part!
1617 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1618 * what decimal point character to use, but PS always requires a '.'.
1619 */
1620 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001621prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001622{
1623 int integer;
1624 int fraction;
1625
1626 prt_real_bits(val, prec, &integer, &fraction);
1627 /* Emit integer part */
1628 sprintf((char *)prt_line_buffer, "%d", integer);
1629 prt_write_file(prt_line_buffer);
1630 /* Only emit fraction if necessary */
1631 if (fraction != 0)
1632 {
1633 /* Remove any trailing zeros */
1634 while ((fraction % 10) == 0)
1635 {
1636 prec--;
1637 fraction /= 10;
1638 }
1639 /* Emit fraction left padded with zeros */
1640 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1641 prt_write_file(prt_line_buffer);
1642 }
1643 sprintf((char *)prt_line_buffer, " ");
1644 prt_write_file(prt_line_buffer);
1645}
1646
1647/*
1648 * Write a line to define a numeric variable.
1649 */
1650 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001651prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001652{
1653 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1654 "/%s ", name);
1655 prt_write_file(prt_line_buffer);
1656 prt_write_real(value, prec);
1657 sprintf((char *)prt_line_buffer, "d\n");
1658 prt_write_file(prt_line_buffer);
1659}
1660
1661/* Convert size from font space to user space at current font scale */
1662#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1663
1664 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001665prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001666{
1667 if (prt_ps_buffer.ga_len > 0)
1668 {
1669 /* Any background color must be drawn first */
1670 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1671 {
1672 int r, g, b;
1673
1674 if (prt_do_moveto)
1675 {
1676 prt_write_real(prt_pos_x_moveto, 2);
1677 prt_write_real(prt_pos_y_moveto, 2);
1678 prt_write_string("m\n");
1679 prt_do_moveto = FALSE;
1680 }
1681
1682 /* Size of rect of background color on which text is printed */
1683 prt_write_real(prt_text_run, 2);
1684 prt_write_real(prt_line_height, 2);
1685
1686 /* Lastly add the color of the background */
1687 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1688 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1689 b = prt_new_bgcol & 0xff;
1690 prt_write_real(r / 255.0, 3);
1691 prt_write_real(g / 255.0, 3);
1692 prt_write_real(b / 255.0, 3);
1693 prt_write_string("bg\n");
1694 }
1695 /* Draw underlines before the text as it makes it slightly easier to
1696 * find the starting point.
1697 */
1698 if (prt_do_underline)
1699 {
1700 if (prt_do_moveto)
1701 {
1702 prt_write_real(prt_pos_x_moveto, 2);
1703 prt_write_real(prt_pos_y_moveto, 2);
1704 prt_write_string("m\n");
1705 prt_do_moveto = FALSE;
1706 }
1707
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001708 /* Underline length of text run */
Bram Moolenaar81366db2005-07-24 21:16:51 +00001709 prt_write_real(prt_text_run, 2);
1710 prt_write_string("ul\n");
1711 }
1712 /* Draw the text
1713 * Note: we write text out raw - EBCDIC conversion is handled in the
1714 * PostScript world via the font encoding vector. */
1715#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001716 if (prt_out_mbyte)
1717 prt_write_string("<");
1718 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001719#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001720 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001721 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1722#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001723 if (prt_out_mbyte)
1724 prt_write_string(">");
1725 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001726#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001727 prt_write_string(")");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001728 /* Add a moveto if need be and use the appropriate show procedure */
1729 if (prt_do_moveto)
1730 {
1731 prt_write_real(prt_pos_x_moveto, 2);
1732 prt_write_real(prt_pos_y_moveto, 2);
1733 /* moveto and a show */
1734 prt_write_string("ms\n");
1735 prt_do_moveto = FALSE;
1736 }
1737 else /* Simple show */
1738 prt_write_string("s\n");
1739
1740 ga_clear(&prt_ps_buffer);
1741 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1742 }
1743}
1744
1745
1746 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001747prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001748{
1749 char_u *resource_filename = cookie;
1750
1751 if (STRLEN(filename) >= MAXPATHL)
1752 *resource_filename = NUL;
1753 else
1754 STRCPY(resource_filename, filename);
1755}
1756
1757 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001758prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001759{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001760 char_u *buffer;
1761 int retval;
1762
1763 buffer = alloc(MAXPATHL + 1);
1764 if (buffer == NULL)
1765 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001766
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001767 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001768 /* Look for named resource file in runtimepath */
1769 STRCPY(buffer, "print");
1770 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001771 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1772 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001773 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001774 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001775 resource->filename)
1776 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001777 vim_free(buffer);
1778 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001779}
1780
1781/* PS CR and LF characters have platform independent values */
1782#define PSLF (0x0a)
1783#define PSCR (0x0d)
1784
1785/* Static buffer to read initial comments in a resource file, some can have a
1786 * couple of KB of comments! */
1787#define PRT_FILE_BUFFER_LEN (2048)
1788struct prt_resfile_buffer_S
1789{
1790 char_u buffer[PRT_FILE_BUFFER_LEN];
1791 int len;
1792 int line_start;
1793 int line_end;
1794};
1795
1796static struct prt_resfile_buffer_S prt_resfile;
1797
1798 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001799prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001800{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001801 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001802
1803 /* Move to start of next line and then find end of line */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001804 idx = prt_resfile.line_end + 1;
1805 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001806 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001808 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001809 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001810 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001811 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001812
Bram Moolenaar89d40322006-08-29 15:30:07 +00001813 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001814 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001815 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001816 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001818 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001819 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001820
Bram Moolenaar89d40322006-08-29 15:30:07 +00001821 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001822}
1823
1824 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001825prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001826{
1827 /* Force not equal if string is longer than remainder of line */
1828 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001829 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001830
1831 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001832 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001833}
1834
1835 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001836prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001837{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001838 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001839
Bram Moolenaar89d40322006-08-29 15:30:07 +00001840 idx = prt_resfile.line_start + offset;
1841 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001842 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001843 if (isspace(prt_resfile.buffer[idx]))
1844 return idx - prt_resfile.line_start;
1845 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001846 }
1847 return -1;
1848}
1849
1850 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001851prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001852{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001853 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001854
Bram Moolenaar89d40322006-08-29 15:30:07 +00001855 idx = prt_resfile.line_start + offset;
1856 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001857 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001858 if (!isspace(prt_resfile.buffer[idx]))
1859 return idx - prt_resfile.line_start;
1860 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001861 }
1862 return -1;
1863}
1864
1865/* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1866 * if a DSC comment is found, else false */
1867 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001868prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001869{
1870 int comment;
1871 int offset;
1872
1873 /* Move to start of next line */
1874 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001875 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001876
1877 /* DSC comments always start %% */
1878 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001879 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001880
1881 /* Find type of DSC comment */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001882 for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001883 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1884 prt_dsc_table[comment].len) == 0)
1885 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001886
1887 if (comment != NUM_ELEMENTS(prt_dsc_table))
1888 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001889 /* Return type of comment */
1890 p_dsc_line->type = prt_dsc_table[comment].type;
1891 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001892 }
1893 else
1894 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001895 /* Unrecognised DSC comment, skip to ws after comment leader */
1896 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1897 offset = prt_resfile_skip_nonws(0);
1898 if (offset == -1)
1899 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001900 }
1901
1902 /* Skip ws to comment value */
1903 offset = prt_resfile_skip_ws(offset);
1904 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001905 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001906
1907 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1908 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1909
1910 return TRUE;
1911}
1912
1913/* Improved hand crafted parser to get the type, title, and version number of a
1914 * PS resource file so the file details can be added to the DSC header comments.
1915 */
1916 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001917prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001918{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001919 int offset;
1920 int seen_all;
1921 int seen_title;
1922 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001923 FILE *fd_resource;
1924 struct prt_dsc_line_S dsc_line;
1925
1926 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1927 if (fd_resource == NULL)
1928 {
1929 EMSG2(_("E624: Can't open file \"%s\""), resource->filename);
1930 return FALSE;
1931 }
1932 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1933
1934 /* Parse first line to ensure valid resource file */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001935 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001936 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001937 if (ferror(fd_resource))
1938 {
1939 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1940 resource->filename);
1941 fclose(fd_resource);
1942 return FALSE;
1943 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001944 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001945
1946 prt_resfile.line_end = -1;
1947 prt_resfile.line_start = 0;
1948 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001949 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001950
1951 offset = 0;
1952
1953 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001954 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001955 {
1956 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1957 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001958 return FALSE;
1959 }
1960
1961 /* Skip over any version numbers and following ws */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001962 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001963 offset = prt_resfile_skip_nonws(offset);
1964 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001965 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001966 offset = prt_resfile_skip_ws(offset);
1967 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001968 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001969
1970 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001971 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001972 {
1973 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1974 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001975 return FALSE;
1976 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001977 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001978
1979 /* Decide type of resource in the file */
1980 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001981 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001982 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1983 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001984 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001985 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1986 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001987 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001988 resource->type = PRT_RESOURCE_TYPE_CMAP;
1989 else
1990 {
1991 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1992 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001993 return FALSE;
1994 }
1995
1996 /* Look for title and version of resource */
1997 resource->title[0] = '\0';
1998 resource->version[0] = '\0';
1999 seen_title = FALSE;
2000 seen_version = FALSE;
2001 seen_all = FALSE;
2002 while (!seen_all && prt_next_dsc(&dsc_line))
2003 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002004 switch (dsc_line.type)
2005 {
2006 case PRT_DSC_TITLE_TYPE:
2007 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
2008 seen_title = TRUE;
2009 if (seen_version)
2010 seen_all = TRUE;
2011 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002012
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002013 case PRT_DSC_VERSION_TYPE:
2014 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
2015 seen_version = TRUE;
2016 if (seen_title)
2017 seen_all = TRUE;
2018 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002019
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002020 case PRT_DSC_ENDCOMMENTS_TYPE:
2021 /* Wont find title or resource after this comment, stop searching */
2022 seen_all = TRUE;
2023 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002024
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002025 case PRT_DSC_MISC_TYPE:
2026 /* Not interested in whatever comment this line had */
2027 break;
2028 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002029 }
2030
2031 if (!seen_title || !seen_version)
2032 {
2033 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2034 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002035 return FALSE;
2036 }
2037
Bram Moolenaar81366db2005-07-24 21:16:51 +00002038 return TRUE;
2039}
2040
2041 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002042prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002043{
2044 /* Version number m.n should match, the revision number does not matter */
2045 if (STRNCMP(resource->version, version, STRLEN(version)))
2046 {
2047 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2048 resource->name);
2049 return FALSE;
2050 }
2051
2052 /* Other checks to be added as needed */
2053 return TRUE;
2054}
2055
2056 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002057prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002058{
2059 prt_write_string("%!PS-Adobe-3.0\n");
2060}
2061
2062 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002063prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002064{
2065 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2066 "%%%%%s\n", comment);
2067 prt_write_file(prt_line_buffer);
2068}
2069
2070 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002071prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002072{
2073 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2074 "%%%%%s: %s\n", comment, text);
2075 prt_write_file(prt_line_buffer);
2076}
2077
2078 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002079prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002080{
2081 /* TODO - should scan 'text' for any chars needing escaping! */
2082 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2083 "%%%%%s: (%s)\n", comment, text);
2084 prt_write_file(prt_line_buffer);
2085}
2086
2087#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2088
2089 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002090prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002091{
2092 int i;
2093
2094 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2095 "%%%%%s:", comment);
2096 prt_write_file(prt_line_buffer);
2097
2098 for (i = 0; i < count; i++)
2099 {
2100 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2101 prt_write_file(prt_line_buffer);
2102 }
2103
2104 prt_write_string("\n");
2105}
2106
2107 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002108prt_dsc_resources(
2109 char *comment, /* if NULL add to previous */
2110 char *type,
2111 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002112{
2113 if (comment != NULL)
2114 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2115 "%%%%%s: %s", comment, type);
2116 else
2117 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2118 "%%%%+ %s", type);
2119 prt_write_file(prt_line_buffer);
2120
2121 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2122 " %s\n", string);
2123 prt_write_file(prt_line_buffer);
2124}
2125
2126 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002127prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002128{
2129 int i;
2130
2131 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002132 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002133 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002134 if (ps_font->ps_fontname[i] != NULL)
2135 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002136}
2137
2138 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002139prt_dsc_requirements(
2140 int duplex,
2141 int tumble,
2142 int collate,
2143 int color,
2144 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002145{
2146 /* Only output the comment if we need to.
2147 * Note: tumble is ignored if we are not duplexing
2148 */
2149 if (!(duplex || collate || color || (num_copies > 1)))
2150 return;
2151
2152 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2153 prt_write_file(prt_line_buffer);
2154
2155 if (duplex)
2156 {
2157 prt_write_string(" duplex");
2158 if (tumble)
2159 prt_write_string("(tumble)");
2160 }
2161 if (collate)
2162 prt_write_string(" collate");
2163 if (color)
2164 prt_write_string(" color");
2165 if (num_copies > 1)
2166 {
2167 prt_write_string(" numcopies(");
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002168 /* Note: no space wanted so don't use prt_write_int() */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002169 sprintf((char *)prt_line_buffer, "%d", num_copies);
2170 prt_write_file(prt_line_buffer);
2171 prt_write_string(")");
2172 }
2173 prt_write_string("\n");
2174}
2175
2176 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002177prt_dsc_docmedia(
2178 char *paper_name,
2179 double width,
2180 double height,
2181 double weight,
2182 char *colour,
2183 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002184{
2185 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2186 "%%%%DocumentMedia: %s ", paper_name);
2187 prt_write_file(prt_line_buffer);
2188 prt_write_real(width, 2);
2189 prt_write_real(height, 2);
2190 prt_write_real(weight, 2);
2191 if (colour == NULL)
2192 prt_write_string("()");
2193 else
2194 prt_write_string(colour);
2195 prt_write_string(" ");
2196 if (type == NULL)
2197 prt_write_string("()");
2198 else
2199 prt_write_string(type);
2200 prt_write_string("\n");
2201}
2202
2203 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002204mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002205{
2206#ifdef FEAT_MBYTE
2207 if (prt_out_mbyte)
2208 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002209 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002211 /* Free off all CID font names created, but first clear duplicate
2212 * pointers to the same string (when the same font is used for more than
2213 * one style).
2214 */
2215 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2216 {
2217 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002218 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002219 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002220 }
2221
2222 if (prt_do_conv)
2223 {
2224 convert_setup(&prt_conv, NULL, NULL);
2225 prt_do_conv = FALSE;
2226 }
2227#endif
2228 if (prt_ps_fd != NULL)
2229 {
2230 fclose(prt_ps_fd);
2231 prt_ps_fd = NULL;
2232 prt_file_error = FALSE;
2233 }
2234 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002235 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002236}
2237
2238 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002239to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002240{
2241 float ret;
2242 int u;
2243 int nr;
2244
2245 u = prt_get_unit(idx);
2246 if (u == PRT_UNIT_NONE)
2247 {
2248 u = PRT_UNIT_PERC;
2249 nr = def_number;
2250 }
2251 else
2252 nr = printer_opts[idx].number;
2253
2254 switch (u)
2255 {
2256 case PRT_UNIT_INCH:
2257 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2258 break;
2259 case PRT_UNIT_MM:
2260 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2261 break;
2262 case PRT_UNIT_POINT:
2263 ret = (float)nr;
2264 break;
2265 case PRT_UNIT_PERC:
2266 default:
2267 ret = (float)(physsize * nr) / 100;
2268 break;
2269 }
2270
2271 return ret;
2272}
2273
2274/*
2275 * Calculate margins for given width and height from printoptions settings.
2276 */
2277 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002278prt_page_margins(
2279 double width,
2280 double height,
2281 double *left,
2282 double *right,
2283 double *top,
2284 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002285{
2286 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2287 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2288 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2289 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2290}
2291
2292 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002293prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002294{
2295 prt_line_height = (float)font_scale;
2296 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2297}
2298
2299
2300 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002301prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002302{
2303 if (prt_use_number())
2304 {
2305 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2306#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002307 /* If we are outputting multi-byte characters then line numbers will be
2308 * printed with half width characters
2309 */
2310 if (prt_out_mbyte)
2311 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002312#endif
2313 prt_left_margin += prt_number_width;
2314 }
2315 else
2316 prt_number_width = 0.0;
2317
2318 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2319}
2320
2321#ifdef FEAT_MBYTE
2322 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002323prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002324{
2325 char *fontname;
2326
2327 fontname = (char *)alloc(name_len + 1);
2328 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002329 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002330 vim_strncpy((char_u *)fontname, name, name_len);
2331 prt_ps_mb_font.ps_fontname[font] = fontname;
2332
2333 return TRUE;
2334}
2335#endif
2336
2337/*
2338 * Get number of lines of text that fit on a page (excluding the header).
2339 */
2340 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002341prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002342{
2343 int lpp;
2344
2345 /*
2346 * Calculate offset to lower left corner of background rect based on actual
2347 * font height (based on its bounding box) and the line height, handling the
2348 * case where the font height can exceed the line height.
2349 */
2350 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2351 prt_ps_font->bbox_min_y);
2352 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2353 {
2354 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2355 (1000.0 - (prt_ps_font->bbox_max_y -
2356 prt_ps_font->bbox_min_y)) / 2);
2357 }
2358
2359 /* Get height for topmost line based on background rect offset. */
2360 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2361
2362 /* Calculate lpp */
2363 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2364
2365 /* Adjust top margin if there is a header */
2366 prt_top_margin -= prt_line_height * prt_header_height();
2367
2368 return lpp - prt_header_height();
2369}
2370
2371#ifdef FEAT_MBYTE
2372 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002373prt_match_encoding(
2374 char *p_encoding,
2375 struct prt_ps_mbfont_S *p_cmap,
2376 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002377{
2378 int mbenc;
2379 int enc_len;
2380 struct prt_ps_encoding_S *p_mbenc;
2381
2382 *pp_mbenc = NULL;
2383 /* Look for recognised encoding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002384 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002385 p_mbenc = p_cmap->encodings;
2386 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2387 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002388 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2389 {
2390 *pp_mbenc = p_mbenc;
2391 return TRUE;
2392 }
2393 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002394 }
2395 return FALSE;
2396}
2397
2398 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002399prt_match_charset(
2400 char *p_charset,
2401 struct prt_ps_mbfont_S *p_cmap,
2402 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002403{
2404 int mbchar;
2405 int char_len;
2406 struct prt_ps_charset_S *p_mbchar;
2407
2408 /* Look for recognised character set, using default if one is not given */
2409 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002410 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002412 p_mbchar = p_cmap->charsets;
2413 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2414 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002415 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2416 {
2417 *pp_mbchar = p_mbchar;
2418 return TRUE;
2419 }
2420 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002421 }
2422 return FALSE;
2423}
2424#endif
2425
Bram Moolenaar81366db2005-07-24 21:16:51 +00002426 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002427mch_print_init(
2428 prt_settings_T *psettings,
2429 char_u *jobname,
2430 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002431{
2432 int i;
2433 char *paper_name;
2434 int paper_strlen;
2435 int fontsize;
2436 char_u *p;
2437 double left;
2438 double right;
2439 double top;
2440 double bottom;
2441#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002442 int props;
2443 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002444 char_u *p_encoding;
2445 struct prt_ps_encoding_S *p_mbenc;
2446 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002447 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002448#endif
2449
2450#if 0
2451 /*
2452 * TODO:
2453 * If "forceit" is false: pop up a dialog to select:
2454 * - printer name
2455 * - copies
2456 * - collated/uncollated
2457 * - duplex off/long side/short side
2458 * - paper size
2459 * - portrait/landscape
2460 * - font size
2461 *
2462 * If "forceit" is true: use the default printer and settings
2463 */
2464 if (forceit)
2465 s_pd.Flags |= PD_RETURNDEFAULT;
2466#endif
2467
2468 /*
2469 * Set up font and encoding.
2470 */
2471#ifdef FEAT_MBYTE
2472 p_encoding = enc_skip(p_penc);
2473 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002474 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002475
Bram Moolenaar14716812006-05-04 21:54:08 +00002476 /* Look for a multi-byte font that matches the encoding and character set.
2477 * Only look if multi-byte character set is defined, or using multi-byte
2478 * encoding other than Unicode. This is because a Unicode encoding does not
2479 * uniquely identify a CJK character set to use. */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002480 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002481 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002482 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002483 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002484 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002485
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002486 p_mbenc_first = NULL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002487 for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002488 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002489 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002490 {
2491 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002492 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002493 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002494 cmap_first = cmap;
2495 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002496 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002497 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002498 break;
2499 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002500
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002501 /* Use first encoding matched if no charset matched */
2502 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002503 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002504 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002505 cmap = cmap_first;
2506 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002507 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002508
2509 prt_out_mbyte = (p_mbenc != NULL);
2510 if (prt_out_mbyte)
2511 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002512 /* Build CMap name - will be same for all multi-byte fonts used */
2513 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002514
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002515 prt_custom_cmap = (p_mbchar == NULL);
2516 if (!prt_custom_cmap)
2517 {
2518 /* Check encoding and character set are compatible */
2519 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2520 {
2521 EMSG(_("E673: Incompatible multi-byte encoding and character set."));
2522 return FALSE;
2523 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002524
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002525 /* Add charset name if not empty */
2526 if (p_mbchar->cmap_charset != NULL)
2527 {
2528 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002529 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002530 STRCAT(prt_cmap, "-");
2531 }
2532 }
2533 else
2534 {
2535 /* Add custom CMap character set name */
2536 if (*p_pmcs == NUL)
2537 {
2538 EMSG(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
2539 return FALSE;
2540 }
2541 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2542 STRCAT(prt_cmap, "-");
2543 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002544
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002545 /* CMap name ends with (optional) encoding name and -H for horizontal */
2546 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002547 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002548 {
2549 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2550 STRCAT(prt_cmap, "-");
2551 }
2552 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002553
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002554 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2555 {
2556 EMSG(_("E675: No default font specified for multi-byte printing."));
2557 return FALSE;
2558 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002559
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002560 /* Derive CID font names with fallbacks if not defined */
2561 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2562 mbfont_opts[OPT_MBFONT_REGULAR].string,
2563 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2564 return FALSE;
2565 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2566 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2567 mbfont_opts[OPT_MBFONT_BOLD].string,
2568 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2569 return FALSE;
2570 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2571 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2572 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2573 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2574 return FALSE;
2575 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2576 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002577 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2578 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002579 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002580
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002581 /* Check if need to use Courier for ASCII code range, and if so pick up
2582 * the encoding to use */
2583 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2584 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2585 if (prt_use_courier)
2586 {
2587 /* Use national ASCII variant unless ASCII wanted */
2588 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2589 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2590 prt_ascii_encoding = "ascii";
2591 else
2592 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2593 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002594
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002595 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002596 }
2597 else
2598#endif
2599 {
2600#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002601 prt_use_courier = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002602#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002603 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002604 }
2605
2606 /*
2607 * Find the size of the paper and set the margins.
2608 */
2609 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2610 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2611 if (printer_opts[OPT_PRINT_PAPER].present)
2612 {
2613 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2614 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2615 }
2616 else
2617 {
2618 paper_name = "A4";
2619 paper_strlen = 2;
2620 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002621 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002622 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2623 && STRNICMP(prt_mediasize[i].name, paper_name,
2624 paper_strlen) == 0)
2625 break;
2626 if (i == PRT_MEDIASIZE_LEN)
2627 i = 0;
2628 prt_media = i;
2629
2630 /*
2631 * Set PS pagesize based on media dimensions and print orientation.
2632 * Note: Media and page sizes have defined meanings in PostScript and should
2633 * be kept distinct. Media is the paper (or transparency, or ...) that is
2634 * printed on, whereas the page size is the area that the PostScript
2635 * interpreter renders into.
2636 */
2637 if (prt_portrait)
2638 {
2639 prt_page_width = prt_mediasize[i].width;
2640 prt_page_height = prt_mediasize[i].height;
2641 }
2642 else
2643 {
2644 prt_page_width = prt_mediasize[i].height;
2645 prt_page_height = prt_mediasize[i].width;
2646 }
2647
2648 /*
2649 * Set PS page margins based on the PS pagesize, not the mediasize - this
2650 * needs to be done before the cpl and lpp are calculated.
2651 */
2652 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2653 &bottom);
2654 prt_left_margin = (float)left;
2655 prt_right_margin = (float)right;
2656 prt_top_margin = (float)top;
2657 prt_bottom_margin = (float)bottom;
2658
2659 /*
2660 * Set up the font size.
2661 */
2662 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2663 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2664 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2665 fontsize = atoi((char *)p + 2);
2666 prt_font_metrics(fontsize);
2667
2668 /*
2669 * Return the number of characters per line, and lines per page for the
2670 * generic print code.
2671 */
2672 psettings->chars_per_line = prt_get_cpl();
2673 psettings->lines_per_page = prt_get_lpp();
2674
2675 /* Catch margin settings that leave no space for output! */
2676 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2677 return FAIL;
2678
2679 /*
2680 * Sort out the number of copies to be printed. PS by default will do
2681 * uncollated copies for you, so once we know how many uncollated copies are
2682 * wanted cache it away and lie to the generic code that we only want one
2683 * uncollated copy.
2684 */
2685 psettings->n_collated_copies = 1;
2686 psettings->n_uncollated_copies = 1;
2687 prt_num_copies = 1;
2688 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2689 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2690 if (prt_collate)
2691 {
2692 /* TODO: Get number of collated copies wanted. */
2693 psettings->n_collated_copies = 1;
2694 }
2695 else
2696 {
2697 /* TODO: Get number of uncollated copies wanted and update the cached
2698 * count.
2699 */
2700 prt_num_copies = 1;
2701 }
2702
2703 psettings->jobname = jobname;
2704
2705 /*
2706 * Set up printer duplex and tumble based on Duplex option setting - default
2707 * is long sided duplex printing (i.e. no tumble).
2708 */
2709 prt_duplex = TRUE;
2710 prt_tumble = FALSE;
2711 psettings->duplex = 1;
2712 if (printer_opts[OPT_PRINT_DUPLEX].present)
2713 {
2714 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2715 {
2716 prt_duplex = FALSE;
2717 psettings->duplex = 0;
2718 }
2719 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2720 == 0)
2721 prt_tumble = TRUE;
2722 }
2723
2724 /* For now user abort not supported */
2725 psettings->user_abort = 0;
2726
2727 /* If the user didn't specify a file name, use a temp file. */
2728 if (psettings->outfile == NULL)
2729 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002730 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002731 if (prt_ps_file_name == NULL)
2732 {
2733 EMSG(_(e_notmp));
2734 return FAIL;
2735 }
2736 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2737 }
2738 else
2739 {
2740 p = expand_env_save(psettings->outfile);
2741 if (p != NULL)
2742 {
2743 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2744 vim_free(p);
2745 }
2746 }
2747 if (prt_ps_fd == NULL)
2748 {
2749 EMSG(_("E324: Can't open PostScript output file"));
2750 mch_print_cleanup();
2751 return FAIL;
2752 }
2753
2754 prt_bufsiz = psettings->chars_per_line;
2755#ifdef FEAT_MBYTE
2756 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002757 prt_bufsiz *= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002758#endif
2759 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2760
2761 prt_page_num = 0;
2762
2763 prt_attribute_change = FALSE;
2764 prt_need_moveto = FALSE;
2765 prt_need_font = FALSE;
2766 prt_need_fgcol = FALSE;
2767 prt_need_bgcol = FALSE;
2768 prt_need_underline = FALSE;
2769
2770 prt_file_error = FALSE;
2771
2772 return OK;
2773}
2774
2775 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002776prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002777{
2778 FILE* fd_resource;
2779 char_u resource_buffer[512];
2780 size_t bytes_read;
2781
2782 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2783 if (fd_resource == NULL)
2784 {
2785 EMSG2(_("E456: Can't open file \"%s\""), resource->filename);
2786 return FALSE;
2787 }
2788 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2789 (char *)resource->title);
2790
2791 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2792
2793 for (;;)
2794 {
2795 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2796 sizeof(resource_buffer), fd_resource);
2797 if (ferror(fd_resource))
2798 {
2799 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
2800 resource->filename);
2801 fclose(fd_resource);
2802 return FALSE;
2803 }
2804 if (bytes_read == 0)
2805 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002806 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002807 if (prt_file_error)
2808 {
2809 fclose(fd_resource);
2810 return FALSE;
2811 }
2812 }
2813 fclose(fd_resource);
2814
2815 prt_dsc_noarg("EndDocument");
2816
2817 prt_dsc_noarg("EndResource");
2818
2819 return TRUE;
2820}
2821
2822 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002823mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002824{
2825 time_t now;
2826 int bbox[4];
2827 char *p_time;
2828 double left;
2829 double right;
2830 double top;
2831 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002832 struct prt_ps_resource_S *res_prolog;
2833 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002834 char buffer[256];
2835 char_u *p_encoding;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002836 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002837#ifdef FEAT_MBYTE
Bram Moolenaard9462e32011-04-11 21:35:11 +02002838 struct prt_ps_resource_S *res_cidfont;
2839 struct prt_ps_resource_S *res_cmap;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002840#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02002841 int retval = FALSE;
2842
2843 res_prolog = (struct prt_ps_resource_S *)
2844 alloc(sizeof(struct prt_ps_resource_S));
2845 res_encoding = (struct prt_ps_resource_S *)
2846 alloc(sizeof(struct prt_ps_resource_S));
2847#ifdef FEAT_MBYTE
2848 res_cidfont = (struct prt_ps_resource_S *)
2849 alloc(sizeof(struct prt_ps_resource_S));
2850 res_cmap = (struct prt_ps_resource_S *)
2851 alloc(sizeof(struct prt_ps_resource_S));
2852#endif
2853 if (res_prolog == NULL || res_encoding == NULL
2854#ifdef FEAT_MBYTE
2855 || res_cidfont == NULL || res_cmap == NULL
2856#endif
2857 )
2858 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002859
2860 /*
2861 * PS DSC Header comments - no PS code!
2862 */
2863 prt_dsc_start();
2864 prt_dsc_textline("Title", (char *)psettings->jobname);
2865 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002866 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002867 prt_dsc_textline("For", buffer);
2868 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2869 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2870 now = time(NULL);
2871 p_time = ctime(&now);
2872 /* Note: ctime() adds a \n so we have to remove it :-( */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002873 p = vim_strchr((char_u *)p_time, '\n');
2874 if (p != NULL)
2875 *p = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002876 prt_dsc_textline("CreationDate", p_time);
2877 prt_dsc_textline("DocumentData", "Clean8Bit");
2878 prt_dsc_textline("Orientation", "Portrait");
2879 prt_dsc_atend("Pages");
2880 prt_dsc_textline("PageOrder", "Ascend");
2881 /* The bbox does not change with orientation - it is always in the default
2882 * user coordinate system! We have to recalculate right and bottom
2883 * coordinates based on the font metrics for the bbox to be accurate. */
2884 prt_page_margins(prt_mediasize[prt_media].width,
2885 prt_mediasize[prt_media].height,
2886 &left, &right, &top, &bottom);
2887 bbox[0] = (int)left;
2888 if (prt_portrait)
2889 {
2890 /* In portrait printing the fixed point is the top left corner so we
2891 * derive the bbox from that point. We have the expected cpl chars
2892 * across the media and lpp lines down the media.
2893 */
2894 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2895 * prt_line_height);
2896 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2897 + 0.5);
2898 bbox[3] = (int)(top + 0.5);
2899 }
2900 else
2901 {
2902 /* In landscape printing the fixed point is the bottom left corner so we
2903 * derive the bbox from that point. We have lpp chars across the media
2904 * and cpl lines up the media.
2905 */
2906 bbox[1] = (int)bottom;
2907 bbox[2] = (int)(left + ((psettings->lines_per_page
2908 + prt_header_height()) * prt_line_height) + 0.5);
2909 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2910 + 0.5);
2911 }
2912 prt_dsc_ints("BoundingBox", 4, bbox);
2913 /* The media width and height does not change with landscape printing! */
2914 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2915 prt_mediasize[prt_media].width,
2916 prt_mediasize[prt_media].height,
2917 (double)0, NULL, NULL);
2918 /* Define fonts needed */
2919#ifdef FEAT_MBYTE
2920 if (!prt_out_mbyte || prt_use_courier)
2921#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002922 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002923#ifdef FEAT_MBYTE
2924 if (prt_out_mbyte)
2925 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002926 prt_dsc_font_resource((prt_use_courier ? NULL
2927 : "DocumentNeededResources"), &prt_ps_mb_font);
2928 if (!prt_custom_cmap)
2929 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002930 }
2931#endif
2932
2933 /* Search for external resources VIM supplies */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002934 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002935 {
2936 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002937 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002938 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002939 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002940 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002941 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002942 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002943#ifdef FEAT_MBYTE
2944 if (prt_out_mbyte)
2945 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002946 /* Look for required version of multi-byte printing procset */
Bram Moolenaard9462e32011-04-11 21:35:11 +02002947 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002948 {
2949 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002950 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002951 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002952 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002953 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002954 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002955 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002956 }
2957#endif
2958
2959 /* Find an encoding to use for printing.
2960 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2961 * that cannot be found then default to "latin1".
2962 * Note: VIM specific encoding header is always skipped.
2963 */
2964#ifdef FEAT_MBYTE
2965 if (!prt_out_mbyte)
2966 {
2967#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002968 p_encoding = enc_skip(p_penc);
2969 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002970 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002971 {
2972 /* 'printencoding' not set or not supported - find alternate */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002973#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002974 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002975
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002976 p_encoding = enc_skip(p_enc);
2977 props = enc_canon_props(p_encoding);
2978 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002979 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002980 /* 8-bit 'encoding' is not supported */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002981#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002982 {
2983 /* Use latin1 as default printing encoding */
2984 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002985 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002986 {
2987 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
2988 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002989 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002990 }
2991 }
2992 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002993 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002994 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002995 /* For the moment there are no checks on encoding resource files to
2996 * perform */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002997#ifdef FEAT_MBYTE
2998 }
2999 else
3000 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003001 p_encoding = enc_skip(p_penc);
3002 if (*p_encoding == NUL)
3003 p_encoding = enc_skip(p_enc);
3004 if (prt_use_courier)
3005 {
3006 /* Include ASCII range encoding vector */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003007 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003008 {
3009 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003010 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01003011 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003012 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003013 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003014 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003015 /* For the moment there are no checks on encoding resource files to
3016 * perform */
3017 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003018 }
3019
3020 prt_conv.vc_type = CONV_NONE;
3021 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003022 /* Set up encoding conversion if required */
Bram Moolenaar81366db2005-07-24 21:16:51 +00003023 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
3024 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003025 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003026 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01003027 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003028 }
3029 prt_do_conv = TRUE;
3030 }
3031 prt_do_conv = prt_conv.vc_type != CONV_NONE;
3032
3033 if (prt_out_mbyte && prt_custom_cmap)
3034 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003035 /* Find user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003036 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003037 {
3038 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003039 prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01003040 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003041 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003042 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003043 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003044 }
3045#endif
3046
3047 /* List resources supplied */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003048 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003049 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003050 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003051 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
3052#ifdef FEAT_MBYTE
3053 if (prt_out_mbyte)
3054 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003055 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003056 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003057 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003058 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003059
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003060 if (prt_custom_cmap)
3061 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003062 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003063 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003064 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003065 prt_dsc_resources(NULL, "cmap", buffer);
3066 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003067 }
3068 if (!prt_out_mbyte || prt_use_courier)
3069#endif
3070 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02003071 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003072 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02003073 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003074 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003075 }
3076 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3077#ifdef FEAT_SYN_HL
3078 psettings->do_syntax
3079#else
3080 0
3081#endif
3082 , prt_num_copies);
3083 prt_dsc_noarg("EndComments");
3084
3085 /*
3086 * PS Document page defaults
3087 */
3088 prt_dsc_noarg("BeginDefaults");
3089
3090 /* List font resources most likely common to all pages */
3091#ifdef FEAT_MBYTE
3092 if (!prt_out_mbyte || prt_use_courier)
3093#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003094 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003095#ifdef FEAT_MBYTE
3096 if (prt_out_mbyte)
3097 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003098 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3099 &prt_ps_mb_font);
3100 if (!prt_custom_cmap)
3101 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003102 }
3103#endif
3104
3105 /* Paper will be used for all pages */
3106 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3107
3108 prt_dsc_noarg("EndDefaults");
3109
3110 /*
3111 * PS Document prolog inclusion - all required procsets.
3112 */
3113 prt_dsc_noarg("BeginProlog");
3114
3115 /* Add required procsets - NOTE: order is important! */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003116 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003117 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003118#ifdef FEAT_MBYTE
3119 if (prt_out_mbyte)
3120 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003121 /* Add CID font procset, and any user supplied CMap */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003122 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003123 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003124 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003125 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003126 }
3127#endif
3128
3129#ifdef FEAT_MBYTE
3130 if (!prt_out_mbyte || prt_use_courier)
3131#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003132 /* There will be only one Roman font encoding to be included in the PS
3133 * file. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003134 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01003135 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003136
3137 prt_dsc_noarg("EndProlog");
3138
3139 /*
3140 * PS Document setup - must appear after the prolog
3141 */
3142 prt_dsc_noarg("BeginSetup");
3143
3144 /* Device setup - page size and number of uncollated copies */
3145 prt_write_int((int)prt_mediasize[prt_media].width);
3146 prt_write_int((int)prt_mediasize[prt_media].height);
3147 prt_write_int(0);
3148 prt_write_string("sps\n");
3149 prt_write_int(prt_num_copies);
3150 prt_write_string("nc\n");
3151 prt_write_boolean(prt_duplex);
3152 prt_write_boolean(prt_tumble);
3153 prt_write_string("dt\n");
3154 prt_write_boolean(prt_collate);
3155 prt_write_string("c\n");
3156
3157 /* Font resource inclusion and definition */
3158#ifdef FEAT_MBYTE
3159 if (!prt_out_mbyte || prt_use_courier)
3160 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003161 /* When using Courier for ASCII range when printing multi-byte, need to
3162 * pick up ASCII encoding to use with it. */
3163 if (prt_use_courier)
3164 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003165#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003166 prt_dsc_resources("IncludeResource", "font",
3167 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3168 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3169 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3170 prt_dsc_resources("IncludeResource", "font",
3171 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3172 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3173 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3174 prt_dsc_resources("IncludeResource", "font",
3175 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3176 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3177 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3178 prt_dsc_resources("IncludeResource", "font",
3179 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3180 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3181 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003182#ifdef FEAT_MBYTE
3183 }
3184 if (prt_out_mbyte)
3185 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003186 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3187 * not have an italic form being a western style, so where no font is
3188 * defined for these faces VIM falls back to an existing face.
3189 * Note: if using Courier for the ASCII range then the printout will
3190 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3191 */
3192 prt_dsc_resources("IncludeResource", "font",
3193 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3194 if (!prt_custom_cmap)
3195 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3196 prt_def_cidfont("CF0", (int)prt_line_height,
3197 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003199 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3200 {
3201 prt_dsc_resources("IncludeResource", "font",
3202 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3203 if (!prt_custom_cmap)
3204 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3205 prt_def_cidfont("CF1", (int)prt_line_height,
3206 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3207 }
3208 else
3209 /* Use ROMAN for BOLD */
3210 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003212 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3213 {
3214 prt_dsc_resources("IncludeResource", "font",
3215 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3216 if (!prt_custom_cmap)
3217 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3218 prt_def_cidfont("CF2", (int)prt_line_height,
3219 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3220 }
3221 else
3222 /* Use ROMAN for OBLIQUE */
3223 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003224
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003225 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3226 {
3227 prt_dsc_resources("IncludeResource", "font",
3228 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3229 if (!prt_custom_cmap)
3230 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3231 prt_def_cidfont("CF3", (int)prt_line_height,
3232 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3233 }
3234 else
3235 /* Use BOLD for BOLDOBLIQUE */
3236 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003237 }
3238#endif
3239
3240 /* Misc constant vars used for underlining and background rects */
3241 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3242 prt_ps_font->uline_offset), 2);
3243 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3244 prt_ps_font->uline_width), 2);
3245 prt_def_var("BO", prt_bgcol_offset, 2);
3246
3247 prt_dsc_noarg("EndSetup");
3248
3249 /* Fail if any problems writing out to the PS file */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003250 retval = !prt_file_error;
3251
3252theend:
3253 vim_free(res_prolog);
3254 vim_free(res_encoding);
3255#ifdef FEAT_MBYTE
3256 vim_free(res_cidfont);
3257 vim_free(res_cmap);
3258#endif
3259
3260 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003261}
3262
3263 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003264mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003265{
3266 prt_dsc_noarg("Trailer");
3267
3268 /*
3269 * Output any info we don't know in toto until we finish
3270 */
3271 prt_dsc_ints("Pages", 1, &prt_page_num);
3272
3273 prt_dsc_noarg("EOF");
3274
3275 /* Write CTRL-D to close serial communication link if used.
3276 * NOTHING MUST BE WRITTEN AFTER THIS! */
3277 prt_write_file((char_u *)IF_EB("\004", "\067"));
3278
3279 if (!prt_file_error && psettings->outfile == NULL
3280 && !got_int && !psettings->user_abort)
3281 {
3282 /* Close the file first. */
3283 if (prt_ps_fd != NULL)
3284 {
3285 fclose(prt_ps_fd);
3286 prt_ps_fd = NULL;
3287 }
3288 prt_message((char_u *)_("Sending to printer..."));
3289
3290 /* Not printing to a file: use 'printexpr' to print the file. */
3291 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
3292 EMSG(_("E365: Failed to print PostScript file"));
3293 else
3294 prt_message((char_u *)_("Print job sent."));
3295 }
3296
3297 mch_print_cleanup();
3298}
3299
3300 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003301mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003302{
3303 prt_flush_buffer();
3304
3305 prt_write_string("re sp\n");
3306
3307 prt_dsc_noarg("PageTrailer");
3308
3309 return !prt_file_error;
3310}
3311
Bram Moolenaar81366db2005-07-24 21:16:51 +00003312 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003313mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003314{
3315 int page_num[2];
3316
3317 prt_page_num++;
3318
3319 page_num[0] = page_num[1] = prt_page_num;
3320 prt_dsc_ints("Page", 2, page_num);
3321
3322 prt_dsc_noarg("BeginPageSetup");
3323
3324 prt_write_string("sv\n0 g\n");
3325#ifdef FEAT_MBYTE
3326 prt_in_ascii = !prt_out_mbyte;
3327 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003328 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003329 else
3330#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003331 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003332 prt_fgcol = PRCOLOR_BLACK;
3333 prt_bgcol = PRCOLOR_WHITE;
3334 prt_font = PRT_PS_FONT_ROMAN;
3335
3336 /* Set up page transformation for landscape printing. */
3337 if (!prt_portrait)
3338 {
3339 prt_write_int(-((int)prt_mediasize[prt_media].width));
3340 prt_write_string("sl\n");
3341 }
3342
3343 prt_dsc_noarg("EndPageSetup");
3344
3345 /* We have reset the font attributes, force setting them again. */
3346 curr_bg = (long_u)0xffffffff;
3347 curr_fg = (long_u)0xffffffff;
3348 curr_bold = MAYBE;
3349
3350 return !prt_file_error;
3351}
3352
3353 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003354mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003355{
3356 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3357}
3358
3359static float prt_pos_x = 0;
3360static float prt_pos_y = 0;
3361
3362 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003363mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003364{
3365 prt_pos_x = prt_left_margin;
3366 if (margin)
3367 prt_pos_x -= prt_number_width;
3368
3369 prt_pos_y = prt_top_margin - prt_first_line_height -
3370 page_line * prt_line_height;
3371
3372 prt_attribute_change = TRUE;
3373 prt_need_moveto = TRUE;
3374#ifdef FEAT_MBYTE
3375 prt_half_width = FALSE;
3376#endif
3377}
3378
Bram Moolenaar81366db2005-07-24 21:16:51 +00003379 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003380mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003381{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003382 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003383 int need_break;
3384 char_u ch;
3385 char_u ch_buff[8];
3386 float char_width;
3387 float next_pos;
3388#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003389 int in_ascii;
3390 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003391 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003392#endif
3393
3394 char_width = prt_char_width;
3395
3396#ifdef FEAT_MBYTE
3397 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3398 * CJKV font to do what VIM is doing here - use a Roman font for characters
Bram Moolenaarb15c8332007-05-10 18:40:02 +00003399 * in the ASCII range, and the original CID font for everything else.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003400 * The problem is that GhostScript still (as of 8.13) does not support
3401 * rearranged fonts even though they have been documented by Adobe for 7
3402 * years! If they ever do, a lot of this code will disappear.
3403 */
3404 if (prt_use_courier)
3405 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003406 in_ascii = (len == 1 && *p < 0x80);
3407 if (prt_in_ascii)
3408 {
3409 if (!in_ascii)
3410 {
3411 /* No longer in ASCII range - need to switch font */
3412 prt_in_ascii = FALSE;
3413 prt_need_font = TRUE;
3414 prt_attribute_change = TRUE;
3415 }
3416 }
3417 else if (in_ascii)
3418 {
3419 /* Now in ASCII range - need to switch font */
3420 prt_in_ascii = TRUE;
3421 prt_need_font = TRUE;
3422 prt_attribute_change = TRUE;
3423 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003424 }
3425 if (prt_out_mbyte)
3426 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003427 half_width = ((*mb_ptr2cells)(p) == 1);
3428 if (half_width)
3429 char_width /= 2;
3430 if (prt_half_width)
3431 {
3432 if (!half_width)
3433 {
3434 prt_half_width = FALSE;
3435 prt_pos_x += prt_char_width/4;
3436 prt_need_moveto = TRUE;
3437 prt_attribute_change = TRUE;
3438 }
3439 }
3440 else if (half_width)
3441 {
3442 prt_half_width = TRUE;
3443 prt_pos_x += prt_char_width/4;
3444 prt_need_moveto = TRUE;
3445 prt_attribute_change = TRUE;
3446 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003447 }
3448#endif
3449
3450 /* Output any required changes to the graphics state, after flushing any
3451 * text buffered so far.
3452 */
3453 if (prt_attribute_change)
3454 {
3455 prt_flush_buffer();
3456 /* Reset count of number of chars that will be printed */
3457 prt_text_run = 0;
3458
3459 if (prt_need_moveto)
3460 {
3461 prt_pos_x_moveto = prt_pos_x;
3462 prt_pos_y_moveto = prt_pos_y;
3463 prt_do_moveto = TRUE;
3464
3465 prt_need_moveto = FALSE;
3466 }
3467 if (prt_need_font)
3468 {
3469#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003470 if (!prt_in_ascii)
3471 prt_write_string("CF");
3472 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003473#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003474 prt_write_string("F");
3475 prt_write_int(prt_font);
3476 prt_write_string("sf\n");
3477 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003478 }
3479 if (prt_need_fgcol)
3480 {
3481 int r, g, b;
3482 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3483 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3484 b = prt_fgcol & 0xff;
3485
3486 prt_write_real(r / 255.0, 3);
3487 if (r == g && g == b)
3488 prt_write_string("g\n");
3489 else
3490 {
3491 prt_write_real(g / 255.0, 3);
3492 prt_write_real(b / 255.0, 3);
3493 prt_write_string("r\n");
3494 }
3495 prt_need_fgcol = FALSE;
3496 }
3497
3498 if (prt_bgcol != PRCOLOR_WHITE)
3499 {
3500 prt_new_bgcol = prt_bgcol;
3501 if (prt_need_bgcol)
3502 prt_do_bgcol = TRUE;
3503 }
3504 else
3505 prt_do_bgcol = FALSE;
3506 prt_need_bgcol = FALSE;
3507
3508 if (prt_need_underline)
3509 prt_do_underline = prt_underline;
3510 prt_need_underline = FALSE;
3511
3512 prt_attribute_change = FALSE;
3513 }
3514
3515#ifdef FEAT_MBYTE
3516 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003517 {
Bram Moolenaar81366db2005-07-24 21:16:51 +00003518 /* Convert from multi-byte to 8-bit encoding */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003519 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003520 if (p == NULL)
3521 {
3522 p = (char_u *)"";
3523 len = 0;
3524 }
3525 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003526
3527 if (prt_out_mbyte)
3528 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003529 /* Multi-byte character strings are represented more efficiently as hex
3530 * strings when outputting clean 8 bit PS.
3531 */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003532 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003533 {
3534 ch = prt_hexchar[(unsigned)(*p) >> 4];
3535 ga_append(&prt_ps_buffer, ch);
3536 ch = prt_hexchar[(*p) & 0xf];
3537 ga_append(&prt_ps_buffer, ch);
3538 p++;
3539 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003540 }
3541 else
3542#endif
3543 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003544 /* Add next character to buffer of characters to output.
3545 * Note: One printed character may require several PS characters to
3546 * represent it, but we only count them as one printed character.
3547 */
3548 ch = *p;
3549 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3550 {
3551 /* Convert non-printing characters to either their escape or octal
3552 * sequence, ensures PS sent over a serial line does not interfere
3553 * with the comms protocol. Note: For EBCDIC we need to write out
3554 * the escape sequences as ASCII codes!
Bram Moolenaar81366db2005-07-24 21:16:51 +00003555 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3556 */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003557 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3558 switch (ch)
3559 {
3560 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3561 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3562 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3563 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3564 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3565 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3566 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3567 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003568
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003569 default:
3570 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003571#ifdef EBCDIC
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003572 ebcdic2ascii(ch_buff, 3);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003573#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003574 ga_append(&prt_ps_buffer, ch_buff[0]);
3575 ga_append(&prt_ps_buffer, ch_buff[1]);
3576 ga_append(&prt_ps_buffer, ch_buff[2]);
3577 break;
3578 }
3579 }
3580 else
3581 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003582 }
3583
3584#ifdef FEAT_MBYTE
3585 /* Need to free any translated characters */
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003586 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003587#endif
3588
3589 prt_text_run += char_width;
3590 prt_pos_x += char_width;
3591
3592 /* The downside of fp - use relative error on right margin check */
3593 next_pos = prt_pos_x + prt_char_width;
3594 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003595 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003596
3597 if (need_break)
3598 prt_flush_buffer();
3599
3600 return need_break;
3601}
3602
3603 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003604mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003605{
3606 int font = 0;
3607
3608 if (iBold)
3609 font |= 0x01;
3610 if (iItalic)
3611 font |= 0x02;
3612
3613 if (font != prt_font)
3614 {
3615 prt_font = font;
3616 prt_attribute_change = TRUE;
3617 prt_need_font = TRUE;
3618 }
3619 if (prt_underline != iUnderline)
3620 {
3621 prt_underline = iUnderline;
3622 prt_attribute_change = TRUE;
3623 prt_need_underline = TRUE;
3624 }
3625}
3626
3627 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003628mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003629{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003630 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003631 prt_attribute_change = TRUE;
3632 prt_need_bgcol = TRUE;
3633}
3634
3635 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003636mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003637{
3638 if (fgcol != (long_u)prt_fgcol)
3639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003641 prt_attribute_change = TRUE;
3642 prt_need_fgcol = TRUE;
3643 }
3644}
3645
3646# endif /*FEAT_POSTSCRIPT*/
3647#endif /*FEAT_PRINTER*/