blob: dd1f2e071e14b130b6a322bcfd09a344a1a82de4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar81366db2005-07-24 21:16:51 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * hardcopy.c: printing to paper
12 */
13
14#include "vim.h"
15#include "version.h"
16
17#if defined(FEAT_PRINTER) || defined(PROTO)
18/*
19 * To implement printing on a platform, the following functions must be
20 * defined:
21 *
22 * int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
23 * Called once. Code should display printer dialogue (if appropriate) and
24 * determine printer font and margin settings. Reset has_color if the printer
25 * doesn't support colors at all.
26 * Returns FAIL to abort.
27 *
28 * int mch_print_begin(prt_settings_T *settings)
29 * Called to start the print job.
30 * Return FALSE to abort.
31 *
32 * int mch_print_begin_page(char_u *msg)
33 * Called at the start of each page.
34 * "msg" indicates the progress of the print job, can be NULL.
35 * Return FALSE to abort.
36 *
37 * int mch_print_end_page()
38 * Called at the end of each page.
39 * Return FALSE to abort.
40 *
41 * int mch_print_blank_page()
42 * Called to generate a blank page for collated, duplex, multiple copy
43 * document. Return FALSE to abort.
44 *
45 * void mch_print_end(prt_settings_T *psettings)
46 * Called at normal end of print job.
47 *
48 * void mch_print_cleanup()
49 * Called if print job ends normally or is abandoned. Free any memory, close
50 * devices and handles. Also called when mch_print_begin() fails, but not
51 * when mch_print_init() fails.
52 *
53 * void mch_print_set_font(int Bold, int Italic, int Underline);
54 * Called whenever the font style changes.
55 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000056 * void mch_print_set_bg(long_u bgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000057 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
59 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000060 * void mch_print_set_fg(long_u fgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000061 * Called to set the foreground color for the following text. Parameter is an
62 * RGB value.
63 *
64 * mch_print_start_line(int margin, int page_line)
65 * Sets the current position at the start of line "page_line".
66 * If margin is TRUE start in the left margin (for header and line number).
67 *
68 * int mch_print_text_out(char_u *p, int len);
69 * Output one character of text p[len] at the current position.
70 * Return TRUE if there is no room for another character in the same line.
71 *
72 * Note that the generic code has no idea of margins. The machine code should
73 * simply make the page look smaller! The header and the line numbers are
74 * printed in the margin.
75 */
76
77#ifdef FEAT_SYN_HL
78static const long_u cterm_color_8[8] =
79{
80 (long_u)0x000000L, (long_u)0xff0000L, (long_u)0x00ff00L, (long_u)0xffff00L,
81 (long_u)0x0000ffL, (long_u)0xff00ffL, (long_u)0x00ffffL, (long_u)0xffffffL
82};
83
84static const long_u cterm_color_16[16] =
85{
86 (long_u)0x000000L, (long_u)0x0000c0L, (long_u)0x008000L, (long_u)0x004080L,
87 (long_u)0xc00000L, (long_u)0xc000c0L, (long_u)0x808000L, (long_u)0xc0c0c0L,
88 (long_u)0x808080L, (long_u)0x6060ffL, (long_u)0x00ff00L, (long_u)0x00ffffL,
89 (long_u)0xff8080L, (long_u)0xff40ffL, (long_u)0xffff00L, (long_u)0xffffffL
90};
91
92static int current_syn_id;
93#endif
94
95#define PRCOLOR_BLACK (long_u)0
96#define PRCOLOR_WHITE (long_u)0xFFFFFFL
97
98static int curr_italic;
99static int curr_bold;
100static int curr_underline;
101static long_u curr_bg;
102static long_u curr_fg;
103static int page_count;
104
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100105#if defined(FEAT_POSTSCRIPT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000106# define OPT_MBFONT_USECOURIER 0
107# define OPT_MBFONT_ASCII 1
108# define OPT_MBFONT_REGULAR 2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000109# define OPT_MBFONT_BOLD 3
Bram Moolenaar81366db2005-07-24 21:16:51 +0000110# define OPT_MBFONT_OBLIQUE 4
111# define OPT_MBFONT_BOLDOBLIQUE 5
112# define OPT_MBFONT_NUM_OPTIONS 6
113
114static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] =
115{
116 {"c", FALSE, 0, NULL, 0, FALSE},
117 {"a", FALSE, 0, NULL, 0, FALSE},
118 {"r", FALSE, 0, NULL, 0, FALSE},
119 {"b", FALSE, 0, NULL, 0, FALSE},
120 {"i", FALSE, 0, NULL, 0, FALSE},
121 {"o", FALSE, 0, NULL, 0, FALSE},
122};
123#endif
124
125/*
126 * These values determine the print position on a page.
127 */
128typedef struct
129{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100130 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
Bram Moolenaar81366db2005-07-24 21:16:51 +0000136} prt_pos_T;
137
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138static char *parse_list_options(char_u *option_str, option_table_T *table, int table_size);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000139
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T *ppos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000141
142/*
143 * Parse 'printoptions' and set the flags in "printer_opts".
144 * Returns an error message or NULL;
145 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100147parse_printoptions(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000148{
149 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
150}
151
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100152#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000153/*
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200154 * Parse 'printmbfont' and set the flags in "mbfont_opts".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000155 * Returns an error message or NULL;
156 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100157 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100158parse_printmbfont(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000159{
160 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
161}
162#endif
163
164/*
165 * Parse a list of options in the form
166 * option:value,option:value,option:value
167 *
168 * "value" can start with a number which is parsed out, e.g. margin:12mm
169 *
170 * Returns an error message for an illegal option, NULL otherwise.
171 * Only used for the printer at the moment...
172 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100173 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100174parse_list_options(
175 char_u *option_str,
176 option_table_T *table,
177 int table_size)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000178{
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200179 option_table_T *old_opts;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100180 char *ret = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000181 char_u *stringp;
182 char_u *colonp;
183 char_u *commap;
184 char_u *p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100185 int idx = 0; // init for GCC
Bram Moolenaar81366db2005-07-24 21:16:51 +0000186 int len;
187
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100188 // Save the old values, so that they can be restored in case of an error.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200189 old_opts = ALLOC_MULT(option_table_T, table_size);
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200190 if (old_opts == NULL)
191 return NULL;
192
Bram Moolenaar81366db2005-07-24 21:16:51 +0000193 for (idx = 0; idx < table_size; ++idx)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200194 {
195 old_opts[idx] = table[idx];
Bram Moolenaar81366db2005-07-24 21:16:51 +0000196 table[idx].present = FALSE;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200197 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000198
199 /*
200 * Repeat for all comma separated parts.
201 */
202 stringp = option_str;
203 while (*stringp)
204 {
205 colonp = vim_strchr(stringp, ':');
206 if (colonp == NULL)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200207 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000208 ret = e_missing_colon_3;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200209 break;
210 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000211 commap = vim_strchr(stringp, ',');
212 if (commap == NULL)
213 commap = option_str + STRLEN(option_str);
214
215 len = (int)(colonp - stringp);
216
217 for (idx = 0; idx < table_size; ++idx)
218 if (STRNICMP(stringp, table[idx].name, len) == 0)
219 break;
220
221 if (idx == table_size)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200222 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000223 ret = e_illegal_component;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200224 break;
225 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000226 p = colonp + 1;
227 table[idx].present = TRUE;
228
229 if (table[idx].hasnum)
230 {
231 if (!VIM_ISDIGIT(*p))
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200232 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000233 ret = e_digit_expected_2;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200234 break;
235 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000236
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100237 table[idx].number = getdigits(&p); // advances p
Bram Moolenaar81366db2005-07-24 21:16:51 +0000238 }
239
240 table[idx].string = p;
241 table[idx].strlen = (int)(commap - p);
242
243 stringp = commap;
244 if (*stringp == ',')
245 ++stringp;
246 }
247
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200248 if (ret != NULL)
249 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100250 // Restore old options in case of error
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200251 for (idx = 0; idx < table_size; ++idx)
252 table[idx] = old_opts[idx];
253 }
254 vim_free(old_opts);
255 return ret;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000256}
257
258
259#ifdef FEAT_SYN_HL
260/*
261 * If using a dark background, the colors will probably be too bright to show
262 * up well on white paper, so reduce their brightness.
263 */
264 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100265darken_rgb(long_u rgb)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000266{
267 return ((rgb >> 17) << 16)
268 + (((rgb & 0xff00) >> 9) << 8)
269 + ((rgb & 0xff) >> 1);
270}
271
272 static long_u
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100273prt_get_term_color(int colorindex)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000274{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100275 // TODO: Should check for xterm with 88 or 256 colors.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000276 if (t_colors > 8)
277 return cterm_color_16[colorindex % 16];
278 return cterm_color_8[colorindex % 8];
279}
280
281 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100282prt_get_attr(
283 int hl_id,
284 prt_text_attr_T *pattr,
285 int modec)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000286{
287 int colorindex;
288 long_u fg_color;
289 long_u bg_color;
290 char *color;
291
292 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
293 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
294 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
295 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
Bram Moolenaar84f54632022-06-29 18:39:11 +0100296 // TODO: HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED
Bram Moolenaar81366db2005-07-24 21:16:51 +0000297
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200298# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200299 if (USE_24BIT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000300 {
301 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
302 if (bg_color == PRCOLOR_BLACK)
303 bg_color = PRCOLOR_WHITE;
304
305 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
306 }
307 else
308# endif
309 {
310 bg_color = PRCOLOR_WHITE;
311
312 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
313 if (color == NULL)
314 colorindex = 0;
315 else
316 colorindex = atoi(color);
317
318 if (colorindex >= 0 && colorindex < t_colors)
319 fg_color = prt_get_term_color(colorindex);
320 else
321 fg_color = PRCOLOR_BLACK;
322 }
323
324 if (fg_color == PRCOLOR_WHITE)
325 fg_color = PRCOLOR_BLACK;
326 else if (*p_bg == 'd')
327 fg_color = darken_rgb(fg_color);
328
329 pattr->fg_color = fg_color;
330 pattr->bg_color = bg_color;
331}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100332#endif // FEAT_SYN_HL
Bram Moolenaar81366db2005-07-24 21:16:51 +0000333
334 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100335prt_set_fg(long_u fg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000336{
337 if (fg != curr_fg)
338 {
339 curr_fg = fg;
340 mch_print_set_fg(fg);
341 }
342}
343
344 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100345prt_set_bg(long_u bg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000346{
347 if (bg != curr_bg)
348 {
349 curr_bg = bg;
350 mch_print_set_bg(bg);
351 }
352}
353
354 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100355prt_set_font(int bold, int italic, int underline)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000356{
357 if (curr_bold != bold
358 || curr_italic != italic
359 || curr_underline != underline)
360 {
361 curr_underline = underline;
362 curr_italic = italic;
363 curr_bold = bold;
364 mch_print_set_font(bold, italic, underline);
365 }
366}
367
368/*
369 * Print the line number in the left margin.
370 */
371 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100372prt_line_number(
373 prt_settings_T *psettings,
374 int page_line,
375 linenr_T lnum)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000376{
377 int i;
378 char_u tbuf[20];
379
380 prt_set_fg(psettings->number.fg_color);
381 prt_set_bg(psettings->number.bg_color);
382 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
383 mch_print_start_line(TRUE, page_line);
384
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100385 // Leave two spaces between the number and the text; depends on
386 // PRINT_NUMBER_WIDTH.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000387 sprintf((char *)tbuf, "%6ld", (long)lnum);
388 for (i = 0; i < 6; i++)
389 (void)mch_print_text_out(&tbuf[i], 1);
390
391#ifdef FEAT_SYN_HL
392 if (psettings->do_syntax)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100393 // Set colors for next character.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000394 current_syn_id = -1;
395 else
396#endif
397 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100398 // Set colors and font back to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000399 prt_set_fg(PRCOLOR_BLACK);
400 prt_set_bg(PRCOLOR_WHITE);
401 prt_set_font(FALSE, FALSE, FALSE);
402 }
403}
404
Bram Moolenaar81366db2005-07-24 21:16:51 +0000405/*
406 * Get the currently effective header height.
407 */
408 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100409prt_header_height(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000410{
411 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
412 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
413 return 2;
414}
415
416/*
417 * Return TRUE if using a line number for printing.
418 */
419 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100420prt_use_number(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000421{
422 return (printer_opts[OPT_PRINT_NUMBER].present
423 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
424}
425
426/*
427 * Return the unit used in a margin item in 'printoptions'.
428 * Returns PRT_UNIT_NONE if not recognized.
429 */
430 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100431prt_get_unit(int idx)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000432{
433 int u = PRT_UNIT_NONE;
434 int i;
435 static char *(units[4]) = PRT_UNIT_NAMES;
436
437 if (printer_opts[idx].present)
438 for (i = 0; i < 4; ++i)
439 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
440 {
441 u = i;
442 break;
443 }
444 return u;
445}
446
447/*
448 * Print the page header.
449 */
Bram Moolenaar81366db2005-07-24 21:16:51 +0000450 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100451prt_header(
452 prt_settings_T *psettings,
453 int pagenum,
454 linenr_T lnum UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000455{
456 int width = psettings->chars_per_line;
457 int page_line;
458 char_u *tbuf;
459 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000460 int l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000461
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100462 // Also use the space for the line number.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000463 if (prt_use_number())
464 width += PRINT_NUMBER_WIDTH;
465
466 tbuf = alloc(width + IOSIZE);
467 if (tbuf == NULL)
468 return;
469
470#ifdef FEAT_STL_OPT
471 if (*p_header != NUL)
472 {
473 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000474 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000475
476 /*
477 * Need to (temporarily) set current line number and first/last line
478 * number on the 'window'. Since we don't know how long the page is,
479 * set the first and current line number to the top line, and guess
480 * that the page length is 64.
481 */
482 tmp_lnum = curwin->w_cursor.lnum;
483 tmp_topline = curwin->w_topline;
484 tmp_botline = curwin->w_botline;
485 curwin->w_cursor.lnum = lnum;
486 curwin->w_topline = lnum;
487 curwin->w_botline = lnum + 63;
488 printer_page_num = pagenum;
489
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000490# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000491 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000492# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000493 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000494 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000495 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000496
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100497 // Reset line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000498 curwin->w_cursor.lnum = tmp_lnum;
499 curwin->w_topline = tmp_topline;
500 curwin->w_botline = tmp_botline;
501 }
502 else
503#endif
504 sprintf((char *)tbuf, _("Page %d"), pagenum);
505
506 prt_set_fg(PRCOLOR_BLACK);
507 prt_set_bg(PRCOLOR_WHITE);
508 prt_set_font(TRUE, FALSE, FALSE);
509
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100510 // Use a negative line number to indicate printing in the top margin.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000511 page_line = 0 - prt_header_height();
512 mch_print_start_line(TRUE, page_line);
513 for (p = tbuf; *p != NUL; )
514 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100515 if (mch_print_text_out(p, (l = (*mb_ptr2len)(p))))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000516 {
517 ++page_line;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100518 if (page_line >= 0) // out of room in header
Bram Moolenaar81366db2005-07-24 21:16:51 +0000519 break;
520 mch_print_start_line(TRUE, page_line);
521 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000522 p += l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000523 }
524
525 vim_free(tbuf);
526
527#ifdef FEAT_SYN_HL
528 if (psettings->do_syntax)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100529 // Set colors for next character.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000530 current_syn_id = -1;
531 else
532#endif
533 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100534 // Set colors and font back to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000535 prt_set_fg(PRCOLOR_BLACK);
536 prt_set_bg(PRCOLOR_WHITE);
537 prt_set_font(FALSE, FALSE, FALSE);
538 }
539}
540
541/*
542 * Display a print status message.
543 */
544 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100545prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000546{
547 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100548 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000549 out_flush();
550}
551
552 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100553ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000554{
555 linenr_T lnum;
556 int collated_copies, uncollated_copies;
557 prt_settings_T settings;
558 long_u bytes_to_print = 0;
559 int page_line;
560 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000561
Bram Moolenaara80faa82020-04-12 19:37:17 +0200562 CLEAR_FIELD(settings);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000563 settings.has_color = TRUE;
564
565# ifdef FEAT_POSTSCRIPT
566 if (*eap->arg == '>')
567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 char *errormsg = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000569
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100570 // Expand things like "%.ps".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000571 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
572 {
573 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100574 emsg(errormsg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000575 return;
576 }
577 settings.outfile = skipwhite(eap->arg + 1);
578 }
579 else if (*eap->arg != NUL)
580 settings.arguments = eap->arg;
581# endif
582
583 /*
584 * Initialise for printing. Ask the user for settings, unless forceit is
585 * set.
586 * The mch_print_init() code should set up margins if applicable. (It may
587 * not be a real printer - for example the engine might generate HTML or
588 * PS.)
589 */
590 if (mch_print_init(&settings,
591 curbuf->b_fname == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100592 ? buf_spname(curbuf)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000593 : curbuf->b_sfname == NULL
594 ? curbuf->b_fname
595 : curbuf->b_sfname,
596 eap->forceit) == FAIL)
597 return;
598
599#ifdef FEAT_SYN_HL
600# ifdef FEAT_GUI
601 if (gui.in_use)
602 settings.modec = 'g';
603 else
604# endif
605 if (t_colors > 1)
606 settings.modec = 'c';
607 else
608 settings.modec = 't';
609
Bram Moolenaar860cae12010-06-05 23:22:07 +0200610 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000611 settings.do_syntax = FALSE;
612 else if (printer_opts[OPT_PRINT_SYNTAX].present
613 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
614 settings.do_syntax =
615 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
616 else
617 settings.do_syntax = settings.has_color;
618#endif
619
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100620 // Set up printing attributes for line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000621 settings.number.fg_color = PRCOLOR_BLACK;
622 settings.number.bg_color = PRCOLOR_WHITE;
623 settings.number.bold = FALSE;
624 settings.number.italic = TRUE;
625 settings.number.underline = FALSE;
626#ifdef FEAT_SYN_HL
627 /*
628 * Syntax highlighting of line numbers.
629 */
630 if (prt_use_number() && settings.do_syntax)
631 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000632 int id;
633
Bram Moolenaar81366db2005-07-24 21:16:51 +0000634 id = syn_name2id((char_u *)"LineNr");
635 if (id > 0)
636 id = syn_get_final_id(id);
637
638 prt_get_attr(id, &settings.number, settings.modec);
639 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000640#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000641
642 /*
643 * Estimate the total lines to be printed
644 */
645 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
646 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
647 if (bytes_to_print == 0)
648 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100649 msg(_("No text to be printed"));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000650 goto print_fail_no_begin;
651 }
652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100653 // Set colors and font to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000654 curr_bg = (long_u)0xffffffffL;
655 curr_fg = (long_u)0xffffffffL;
656 curr_italic = MAYBE;
657 curr_bold = MAYBE;
658 curr_underline = MAYBE;
659
660 prt_set_fg(PRCOLOR_BLACK);
661 prt_set_bg(PRCOLOR_WHITE);
662 prt_set_font(FALSE, FALSE, FALSE);
663#ifdef FEAT_SYN_HL
664 current_syn_id = -1;
665#endif
666
667 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
668 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
669
670 if (!mch_print_begin(&settings))
671 goto print_fail_no_begin;
672
673 /*
674 * Loop over collated copies: 1 2 3, 1 2 3, ...
675 */
676 page_count = 0;
677 for (collated_copies = 0;
678 collated_copies < settings.n_collated_copies;
679 collated_copies++)
680 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100681 prt_pos_T prtpos; // current print position
682 prt_pos_T page_prtpos; // print position at page start
Bram Moolenaar81366db2005-07-24 21:16:51 +0000683 int side;
684
Bram Moolenaara80faa82020-04-12 19:37:17 +0200685 CLEAR_FIELD(page_prtpos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000686 page_prtpos.file_line = eap->line1;
687 prtpos = page_prtpos;
688
689 if (jobsplit && collated_copies > 0)
690 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100691 // Splitting jobs: Stop a previous job and start a new one.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000692 mch_print_end(&settings);
693 if (!mch_print_begin(&settings))
694 goto print_fail_no_begin;
695 }
696
697 /*
698 * Loop over all pages in the print job: 1 2 3 ...
699 */
700 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
701 {
702 /*
703 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
704 * For duplex: 12 12 12 34 34 34, ...
705 */
706 for (uncollated_copies = 0;
707 uncollated_copies < settings.n_uncollated_copies;
708 uncollated_copies++)
709 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100710 // Set the print position to the start of this page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000711 prtpos = page_prtpos;
712
713 /*
714 * Do front and rear side of a page.
715 */
716 for (side = 0; side <= settings.duplex; ++side)
717 {
718 /*
719 * Print one page.
720 */
721
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100722 // Check for interrupt character every page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000723 ui_breakcheck();
724 if (got_int || settings.user_abort)
725 goto print_fail;
726
727 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
728 page_count + 1 + side,
729 prtpos.bytes_printed > 1000000
730 ? (int)(prtpos.bytes_printed /
731 (bytes_to_print / 100))
732 : (int)((prtpos.bytes_printed * 100)
733 / bytes_to_print));
734 if (!mch_print_begin_page(IObuff))
735 goto print_fail;
736
737 if (settings.n_collated_copies > 1)
738 sprintf((char *)IObuff + STRLEN(IObuff),
739 _(" Copy %d of %d"),
740 collated_copies + 1,
741 settings.n_collated_copies);
742 prt_message(IObuff);
743
744 /*
745 * Output header if required
746 */
747 if (prt_header_height() > 0)
748 prt_header(&settings, page_count + 1 + side,
749 prtpos.file_line);
750
751 for (page_line = 0; page_line < settings.lines_per_page;
752 ++page_line)
753 {
754 prtpos.column = hardcopy_line(&settings,
755 page_line, &prtpos);
756 if (prtpos.column == 0)
757 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100758 // finished a file line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000759 prtpos.bytes_printed +=
760 STRLEN(skipwhite(ml_get(prtpos.file_line)));
761 if (++prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100762 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000763 }
764 else if (prtpos.ff)
765 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100766 // Line had a formfeed in it - start new page but
767 // stay on the current line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000768 break;
769 }
770 }
771
772 if (!mch_print_end_page())
773 goto print_fail;
774 if (prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100775 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000776 }
777
778 /*
779 * Extra blank page for duplexing with odd number of pages and
780 * more copies to come.
781 */
782 if (prtpos.file_line > eap->line2 && settings.duplex
783 && side == 0
784 && uncollated_copies + 1 < settings.n_uncollated_copies)
785 {
786 if (!mch_print_blank_page())
787 goto print_fail;
788 }
789 }
790 if (settings.duplex && prtpos.file_line <= eap->line2)
791 ++page_count;
792
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100793 // Remember the position where the next page starts.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000794 page_prtpos = prtpos;
795 }
796
797 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
798 settings.jobname);
799 prt_message(IObuff);
800 }
801
802print_fail:
803 if (got_int || settings.user_abort)
804 {
805 sprintf((char *)IObuff, "%s", _("Printing aborted"));
806 prt_message(IObuff);
807 }
808 mch_print_end(&settings);
809
810print_fail_no_begin:
811 mch_print_cleanup();
812}
813
814/*
815 * Print one page line.
816 * Return the next column to print, or zero if the line is finished.
817 */
818 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100819hardcopy_line(
820 prt_settings_T *psettings,
821 int page_line,
822 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000823{
824 colnr_T col;
825 char_u *line;
826 int need_break = FALSE;
827 int outputlen;
828 int tab_spaces;
829 long_u print_pos;
830#ifdef FEAT_SYN_HL
831 prt_text_attr_T attr;
832 int id;
833#endif
834
835 if (ppos->column == 0 || ppos->ff)
836 {
837 print_pos = 0;
838 tab_spaces = 0;
839 if (!ppos->ff && prt_use_number())
840 prt_line_number(psettings, page_line, ppos->file_line);
841 ppos->ff = FALSE;
842 }
843 else
844 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100845 // left over from wrap halfway a tab
Bram Moolenaar81366db2005-07-24 21:16:51 +0000846 print_pos = ppos->print_pos;
847 tab_spaces = ppos->lead_spaces;
848 }
849
850 mch_print_start_line(0, page_line);
851 line = ml_get(ppos->file_line);
852
853 /*
854 * Loop over the columns until the end of the file line or right margin.
855 */
856 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
857 {
858 outputlen = 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000859 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000860 outputlen = 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000861#ifdef FEAT_SYN_HL
862 /*
863 * syntax highlighting stuff.
864 */
865 if (psettings->do_syntax)
866 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000867 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000868 if (id > 0)
869 id = syn_get_final_id(id);
870 else
871 id = 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100872 // Get the line again, a multi-line regexp may invalidate it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000873 line = ml_get(ppos->file_line);
874
875 if (id != current_syn_id)
876 {
877 current_syn_id = id;
878 prt_get_attr(id, &attr, psettings->modec);
879 prt_set_font(attr.bold, attr.italic, attr.underline);
880 prt_set_fg(attr.fg_color);
881 prt_set_bg(attr.bg_color);
882 }
883 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000884#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000885
886 /*
887 * Appropriately expand any tabs to spaces.
888 */
889 if (line[col] == TAB || tab_spaces != 0)
890 {
891 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200892#ifdef FEAT_VARTABS
893 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
894 curbuf->b_p_vts_array);
895#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000896 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200897#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000898
899 while (tab_spaces > 0)
900 {
901 need_break = mch_print_text_out((char_u *)" ", 1);
902 print_pos++;
903 tab_spaces--;
904 if (need_break)
905 break;
906 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100907 // Keep the TAB if we didn't finish it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000908 if (need_break && tab_spaces > 0)
909 break;
910 }
911 else if (line[col] == FF
912 && printer_opts[OPT_PRINT_FORMFEED].present
913 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
914 == 'y')
915 {
916 ppos->ff = TRUE;
917 need_break = 1;
918 }
919 else
920 {
921 need_break = mch_print_text_out(line + col, outputlen);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000922 if (has_mbyte)
923 print_pos += (*mb_ptr2cells)(line + col);
924 else
Bram Moolenaar81366db2005-07-24 21:16:51 +0000925 print_pos++;
926 }
927 }
928
929 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000930 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000931
932 /*
933 * Start next line of file if we clip lines, or have reached end of the
934 * line, unless we are doing a formfeed.
935 */
936 if (!ppos->ff
937 && (line[col] == NUL
938 || (printer_opts[OPT_PRINT_WRAP].present
939 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
940 == 'n')))
941 return 0;
942 return col;
943}
944
945# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
946
947/*
948 * PS printer stuff.
949 *
950 * Sources of information to help maintain the PS printing code:
951 *
952 * 1. PostScript Language Reference, 3rd Edition,
953 * Addison-Wesley, 1999, ISBN 0-201-37922-8
954 * 2. PostScript Language Program Design,
955 * Addison-Wesley, 1988, ISBN 0-201-14396-8
956 * 3. PostScript Tutorial and Cookbook,
957 * Addison Wesley, 1985, ISBN 0-201-10179-3
958 * 4. PostScript Language Document Structuring Conventions Specification,
959 * version 3.0,
960 * Adobe Technote 5001, 25th September 1992
961 * 5. PostScript Printer Description File Format Specification, Version 4.3,
962 * Adobe technote 5003, 9th February 1996
963 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
964 * Adobe Technote 5007, 7th October 1998
965 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
966 * Adobe Technote 5014, 8th October 1996
967 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
968 * Adoboe Technote 5094, 8th September, 2001
969 * 9. CJKV Information Processing, 2nd Edition,
970 * O'Reilly, 2002, ISBN 1-56592-224-7
971 *
972 * Some of these documents can be found in PDF form on Adobe's web site -
973 * http://www.adobe.com
974 */
975
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100976#define PRT_PS_DEFAULT_DPI (72) // Default user space resolution
Bram Moolenaar81366db2005-07-24 21:16:51 +0000977#define PRT_PS_DEFAULT_FONTSIZE (10)
978#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
979
980struct prt_mediasize_S
981{
982 char *name;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100983 float width; // width and height in points for portrait
Bram Moolenaar81366db2005-07-24 21:16:51 +0000984 float height;
985};
986
K.Takataeeec2542021-06-02 13:28:16 +0200987#define PRT_MEDIASIZE_LEN ARRAY_LENGTH(prt_mediasize)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000988
989static struct prt_mediasize_S prt_mediasize[] =
990{
991 {"A4", 595.0, 842.0},
992 {"letter", 612.0, 792.0},
993 {"10x14", 720.0, 1008.0},
994 {"A3", 842.0, 1191.0},
995 {"A5", 420.0, 595.0},
996 {"B4", 729.0, 1032.0},
997 {"B5", 516.0, 729.0},
998 {"executive", 522.0, 756.0},
999 {"folio", 595.0, 935.0},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001000 {"ledger", 1224.0, 792.0}, // Yes, it is wider than taller!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001001 {"legal", 612.0, 1008.0},
1002 {"quarto", 610.0, 780.0},
1003 {"statement", 396.0, 612.0},
1004 {"tabloid", 792.0, 1224.0}
1005};
1006
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001007// PS font names, must be in Roman, Bold, Italic, Bold-Italic order
Bram Moolenaar81366db2005-07-24 21:16:51 +00001008struct prt_ps_font_S
1009{
1010 int wx;
1011 int uline_offset;
1012 int uline_width;
1013 int bbox_min_y;
1014 int bbox_max_y;
1015 char *(ps_fontname[4]);
1016};
1017
1018#define PRT_PS_FONT_ROMAN (0)
1019#define PRT_PS_FONT_BOLD (1)
1020#define PRT_PS_FONT_OBLIQUE (2)
1021#define PRT_PS_FONT_BOLDOBLIQUE (3)
1022
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001023// Standard font metrics for Courier family
Bram Moolenaar81366db2005-07-24 21:16:51 +00001024static struct prt_ps_font_S prt_ps_courier_font =
1025{
1026 600,
1027 -100, 50,
1028 -250, 805,
1029 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1030};
1031
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001032// Generic font metrics for multi-byte fonts
Bram Moolenaar81366db2005-07-24 21:16:51 +00001033static struct prt_ps_font_S prt_ps_mb_font =
1034{
1035 1000,
1036 -100, 50,
1037 -250, 805,
1038 {NULL, NULL, NULL, NULL}
1039};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001040
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001041// Pointer to current font set being used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001042static struct prt_ps_font_S* prt_ps_font;
1043
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001044// Structures to map user named encoding and mapping to PS equivalents for
1045// building CID font name
Bram Moolenaar81366db2005-07-24 21:16:51 +00001046struct prt_ps_encoding_S
1047{
1048 char *encoding;
1049 char *cmap_encoding;
1050 int needs_charset;
1051};
1052
1053struct prt_ps_charset_S
1054{
1055 char *charset;
1056 char *cmap_charset;
1057 int has_charset;
1058};
1059
Bram Moolenaar81366db2005-07-24 21:16:51 +00001060
1061#define CS_JIS_C_1978 (0x01)
1062#define CS_JIS_X_1983 (0x02)
1063#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001064#define CS_NEC (0x08)
1065#define CS_MSWINDOWS (0x10)
1066#define CS_CP932 (0x20)
1067#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001068#define CS_KANJITALK7 (0x80)
1069
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001070// Japanese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001071static struct prt_ps_encoding_S j_encodings[] =
1072{
1073 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001074 CS_NEC)},
1075 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1076 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1077 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001078 {"cp932", "RKSJ", CS_JIS_X_1983},
1079 {"ucs-2", "UCS2", CS_JIS_X_1990},
1080 {"utf-8", "UTF8" , CS_JIS_X_1990}
1081};
1082static struct prt_ps_charset_S j_charsets[] =
1083{
1084 {"JIS_C_1978", "78", CS_JIS_C_1978},
1085 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1086 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001087 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001088 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1089 {"CP932", "90ms", CS_JIS_X_1983},
1090 {"KANJITALK6", "83pv", CS_KANJITALK6},
1091 {"KANJITALK7", "90pv", CS_KANJITALK7}
1092};
1093
1094#define CS_GB_2312_80 (0x01)
1095#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001096#define CS_GBK2K (0x04)
1097#define CS_SC_MAC (0x08)
1098#define CS_GBT_90_MAC (0x10)
1099#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001100#define CS_SC_ISO10646 (0x40)
1101
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001102// Simplified Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001103static struct prt_ps_encoding_S sc_encodings[] =
1104{
1105 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1106 {"gb18030", NULL, CS_GBK2K},
1107 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001108 CS_GBT_90_MAC)},
1109 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001110 {"ucs-2", "UCS2", CS_SC_ISO10646},
1111 {"utf-8", "UTF8", CS_SC_ISO10646}
1112};
1113static struct prt_ps_charset_S sc_charsets[] =
1114{
1115 {"GB_2312-80", "GB", CS_GB_2312_80},
1116 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001117 {"MAC", "GBpc", CS_SC_MAC},
1118 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1119 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001120 {"GB18030", "GBK2K", CS_GBK2K},
1121 {"ISO10646", "UniGB", CS_SC_ISO10646}
1122};
1123
1124#define CS_CNS_PLANE_1 (0x01)
1125#define CS_CNS_PLANE_2 (0x02)
1126#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001127#define CS_B5 (0x08)
1128#define CS_ETEN (0x10)
1129#define CS_HK_GCCS (0x20)
1130#define CS_HK_SCS (0x40)
1131#define CS_HK_SCS_ETEN (0x80)
1132#define CS_MTHKL (0x100)
1133#define CS_MTHKS (0x200)
1134#define CS_DLHKL (0x400)
1135#define CS_DLHKS (0x800)
1136#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001137
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138// Traditional Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001139static struct prt_ps_encoding_S tc_encodings[] =
1140{
1141 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1142 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001143 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1144 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1145 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001146 {"cp950", "B5", CS_B5},
1147 {"ucs-2", "UCS2", CS_TC_ISO10646},
1148 {"utf-8", "UTF8", CS_TC_ISO10646},
1149 {"utf-16", "UTF16", CS_TC_ISO10646},
1150 {"utf-32", "UTF32", CS_TC_ISO10646}
1151};
1152static struct prt_ps_charset_S tc_charsets[] =
1153{
1154 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1155 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1156 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001157 {"BIG5", NULL, CS_B5},
1158 {"CP950", NULL, CS_B5},
1159 {"ETEN", "ETen", CS_ETEN},
1160 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1161 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001162 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1163 {"MTHKL", "HKm471", CS_MTHKL},
1164 {"MTHKS", "HKm314", CS_MTHKS},
1165 {"DLHKL", "HKdla", CS_DLHKL},
1166 {"DLHKS", "HKdlb", CS_DLHKS},
1167 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1168};
1169
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001170#define CS_KR_X_1992 (0x01)
1171#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001172#define CS_KR_X_1992_MS (0x04)
1173#define CS_KR_ISO10646 (0x08)
1174
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001175// Korean encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001176static struct prt_ps_encoding_S k_encodings[] =
1177{
1178 {"iso-2022-kr", NULL, CS_KR_X_1992},
1179 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1180 {"johab", "Johab", CS_KR_X_1992},
1181 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001182 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001183 {"cp949", "UHC", CS_KR_X_1992_MS},
1184 {"ucs-2", "UCS2", CS_KR_ISO10646},
1185 {"utf-8", "UTF8", CS_KR_ISO10646}
1186};
1187static struct prt_ps_charset_S k_charsets[] =
1188{
1189 {"KS_X_1992", "KSC", CS_KR_X_1992},
1190 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001191 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001192 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1193 {"CP949", "KSCms", CS_KR_X_1992_MS},
1194 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1195 {"ISO10646", "UniKS", CS_KR_ISO10646}
1196};
1197
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001198// Collections of encodings and charsets for multi-byte printing
Bram Moolenaar81366db2005-07-24 21:16:51 +00001199struct prt_ps_mbfont_S
1200{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001201 int num_encodings;
1202 struct prt_ps_encoding_S *encodings;
1203 int num_charsets;
1204 struct prt_ps_charset_S *charsets;
1205 char *ascii_enc;
1206 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001207};
1208
1209static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1210{
1211 {
K.Takataeeec2542021-06-02 13:28:16 +02001212 ARRAY_LENGTH(j_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001213 j_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001214 ARRAY_LENGTH(j_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001215 j_charsets,
1216 "jis_roman",
1217 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001218 },
1219 {
K.Takataeeec2542021-06-02 13:28:16 +02001220 ARRAY_LENGTH(sc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001221 sc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001222 ARRAY_LENGTH(sc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001223 sc_charsets,
1224 "gb_roman",
1225 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001226 },
1227 {
K.Takataeeec2542021-06-02 13:28:16 +02001228 ARRAY_LENGTH(tc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001229 tc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001230 ARRAY_LENGTH(tc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001231 tc_charsets,
1232 "cns_roman",
1233 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001234 },
1235 {
K.Takataeeec2542021-06-02 13:28:16 +02001236 ARRAY_LENGTH(k_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001237 k_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001238 ARRAY_LENGTH(k_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001239 k_charsets,
1240 "ks_roman",
1241 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001242 }
1243};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001244
1245struct prt_ps_resource_S
1246{
1247 char_u name[64];
1248 char_u filename[MAXPATHL + 1];
1249 int type;
1250 char_u title[256];
1251 char_u version[256];
1252};
1253
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001254// Types of PS resource file currently used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001255#define PRT_RESOURCE_TYPE_PROCSET (0)
1256#define PRT_RESOURCE_TYPE_ENCODING (1)
1257#define PRT_RESOURCE_TYPE_CMAP (2)
1258
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001259// The PS prolog file version number has to match - if the prolog file is
1260// updated, increment the number in the file and here. Version checking was
1261// added as of VIM 6.2.
1262// The CID prolog file version number behaves as per PS prolog.
1263// Table of VIM and prolog versions:
1264//
1265// VIM Prolog CIDProlog
1266// 6.2 1.3
1267// 7.0 1.4 1.0
Bram Moolenaar81366db2005-07-24 21:16:51 +00001268#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1269#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1270
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001271// String versions of PS resource types - indexed by constants above so don't
1272// re-order!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001273static char *prt_resource_types[] =
1274{
1275 "procset",
1276 "encoding",
1277 "cmap"
1278};
1279
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001280// Strings to look for in a PS resource file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001281#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1282#define PRT_RESOURCE_RESOURCE "Resource-"
1283#define PRT_RESOURCE_PROCSET "ProcSet"
1284#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001285#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001286
1287
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001288// Data for table based DSC comment recognition, easy to extend if VIM needs to
1289// read more comments.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001290#define PRT_DSC_MISC_TYPE (-1)
1291#define PRT_DSC_TITLE_TYPE (1)
1292#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001293#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1294
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001295#define PRT_DSC_TITLE "%%Title:"
1296#define PRT_DSC_VERSION "%%Version:"
1297#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001298
1299struct prt_dsc_comment_S
1300{
1301 char *string;
1302 int len;
1303 int type;
1304};
1305
1306struct prt_dsc_line_S
1307{
1308 int type;
1309 char_u *string;
1310 int len;
1311};
1312
1313
1314#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1315static struct prt_dsc_comment_S prt_dsc_table[] =
1316{
1317 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1318 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1319 PRT_DSC_VERSION_TYPE},
1320 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1321 PRT_DSC_ENDCOMMENTS_TYPE}
1322};
1323
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001324static void prt_write_file_len(char_u *buffer, int bytes);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001325static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001326
1327/*
1328 * Variables for the output PostScript file.
1329 */
1330static FILE *prt_ps_fd;
1331static int prt_file_error;
1332static char_u *prt_ps_file_name = NULL;
1333
1334/*
1335 * Various offsets and dimensions in default PostScript user space (points).
1336 * Used for text positioning calculations
1337 */
1338static float prt_page_width;
1339static float prt_page_height;
1340static float prt_left_margin;
1341static float prt_right_margin;
1342static float prt_top_margin;
1343static float prt_bottom_margin;
1344static float prt_line_height;
1345static float prt_first_line_height;
1346static float prt_char_width;
1347static float prt_number_width;
1348static float prt_bgcol_offset;
1349static float prt_pos_x_moveto = 0.0;
1350static float prt_pos_y_moveto = 0.0;
1351
1352/*
1353 * Various control variables used to decide when and how to change the
1354 * PostScript graphics state.
1355 */
1356static int prt_need_moveto;
1357static int prt_do_moveto;
1358static int prt_need_font;
1359static int prt_font;
1360static int prt_need_underline;
1361static int prt_underline;
1362static int prt_do_underline;
1363static int prt_need_fgcol;
1364static int prt_fgcol;
1365static int prt_need_bgcol;
1366static int prt_do_bgcol;
1367static int prt_bgcol;
1368static int prt_new_bgcol;
1369static int prt_attribute_change;
1370static float prt_text_run;
1371static int prt_page_num;
1372static int prt_bufsiz;
1373
1374/*
1375 * Variables controlling physical printing.
1376 */
1377static int prt_media;
1378static int prt_portrait;
1379static int prt_num_copies;
1380static int prt_duplex;
1381static int prt_tumble;
1382static int prt_collate;
1383
1384/*
1385 * Buffers used when generating PostScript output
1386 */
1387static char_u prt_line_buffer[257];
1388static garray_T prt_ps_buffer;
1389
Bram Moolenaar81366db2005-07-24 21:16:51 +00001390static int prt_do_conv;
1391static vimconv_T prt_conv;
1392
1393static int prt_out_mbyte;
1394static int prt_custom_cmap;
1395static char prt_cmap[80];
1396static int prt_use_courier;
1397static int prt_in_ascii;
1398static int prt_half_width;
1399static char *prt_ascii_encoding;
1400static char_u prt_hexchar[] = "0123456789abcdef";
Bram Moolenaar81366db2005-07-24 21:16:51 +00001401
1402 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001403prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001404{
1405 if (!prt_file_error
1406 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1407 != (size_t)bytes)
1408 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001409 emsg(_(e_error_writing_to_postscript_output_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001410 prt_file_error = TRUE;
1411 }
1412}
1413
1414 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001415prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001416{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001417 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001418}
1419
1420 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001421prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001422{
Bram Moolenaar81366db2005-07-24 21:16:51 +00001423 prt_write_file_raw_len(buffer, bytes);
1424}
1425
1426/*
1427 * Write a string.
1428 */
1429 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001430prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001431{
1432 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1433 prt_write_file(prt_line_buffer);
1434}
1435
1436/*
1437 * Write an int and a space.
1438 */
1439 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001440prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001441{
1442 sprintf((char *)prt_line_buffer, "%d ", i);
1443 prt_write_file(prt_line_buffer);
1444}
1445
1446/*
1447 * Write a boolean and a space.
1448 */
1449 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001450prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001451{
1452 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1453 prt_write_file(prt_line_buffer);
1454}
1455
1456/*
1457 * Write PostScript to re-encode and define the font.
1458 */
1459 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001460prt_def_font(
1461 char *new_name,
1462 char *encoding,
1463 int height,
1464 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001465{
1466 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1467 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1468 prt_write_file(prt_line_buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001469 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001470 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001471 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1472 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001473 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1474 "/%s %d /_%s ffs\n", new_name, height, new_name);
1475 prt_write_file(prt_line_buffer);
1476}
1477
Bram Moolenaar81366db2005-07-24 21:16:51 +00001478/*
1479 * Write a line to define the CID font.
1480 */
1481 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001482prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001483{
1484 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1485 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1486 prt_write_file(prt_line_buffer);
1487 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1488 "/%s %d /_%s ffs\n", new_name, height, new_name);
1489 prt_write_file(prt_line_buffer);
1490}
1491
1492/*
1493 * Write a line to define a duplicate of a CID font
1494 */
1495 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001496prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001497{
1498 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1499 "/%s %s d\n", new_name, original_name);
1500 prt_write_file(prt_line_buffer);
1501}
Bram Moolenaar81366db2005-07-24 21:16:51 +00001502
1503/*
1504 * Convert a real value into an integer and fractional part as integers, with
1505 * the fractional part being in the range [0,10^precision). The fractional part
1506 * is also rounded based on the precision + 1'th fractional digit.
1507 */
1508 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001509prt_real_bits(
1510 double real,
1511 int precision,
1512 int *pinteger,
1513 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001514{
1515 int i;
1516 int integer;
1517 float fraction;
1518
1519 integer = (int)real;
1520 fraction = (float)(real - integer);
1521 if (real < (double)integer)
1522 fraction = -fraction;
1523 for (i = 0; i < precision; i++)
1524 fraction *= 10.0;
1525
1526 *pinteger = integer;
1527 *pfraction = (int)(fraction + 0.5);
1528}
1529
1530/*
1531 * Write a real and a space. Save bytes if real value has no fractional part!
1532 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1533 * what decimal point character to use, but PS always requires a '.'.
1534 */
1535 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001536prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001537{
1538 int integer;
1539 int fraction;
1540
1541 prt_real_bits(val, prec, &integer, &fraction);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 // Emit integer part
Bram Moolenaar81366db2005-07-24 21:16:51 +00001543 sprintf((char *)prt_line_buffer, "%d", integer);
1544 prt_write_file(prt_line_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001545 // Only emit fraction if necessary
Bram Moolenaar81366db2005-07-24 21:16:51 +00001546 if (fraction != 0)
1547 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001548 // Remove any trailing zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001549 while ((fraction % 10) == 0)
1550 {
1551 prec--;
1552 fraction /= 10;
1553 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001554 // Emit fraction left padded with zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001555 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1556 prt_write_file(prt_line_buffer);
1557 }
1558 sprintf((char *)prt_line_buffer, " ");
1559 prt_write_file(prt_line_buffer);
1560}
1561
1562/*
1563 * Write a line to define a numeric variable.
1564 */
1565 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001566prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001567{
1568 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1569 "/%s ", name);
1570 prt_write_file(prt_line_buffer);
1571 prt_write_real(value, prec);
1572 sprintf((char *)prt_line_buffer, "d\n");
1573 prt_write_file(prt_line_buffer);
1574}
1575
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576// Convert size from font space to user space at current font scale
Bram Moolenaar81366db2005-07-24 21:16:51 +00001577#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1578
1579 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001580prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001581{
1582 if (prt_ps_buffer.ga_len > 0)
1583 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584 // Any background color must be drawn first
Bram Moolenaar81366db2005-07-24 21:16:51 +00001585 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1586 {
1587 int r, g, b;
1588
1589 if (prt_do_moveto)
1590 {
1591 prt_write_real(prt_pos_x_moveto, 2);
1592 prt_write_real(prt_pos_y_moveto, 2);
1593 prt_write_string("m\n");
1594 prt_do_moveto = FALSE;
1595 }
1596
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001597 // Size of rect of background color on which text is printed
Bram Moolenaar81366db2005-07-24 21:16:51 +00001598 prt_write_real(prt_text_run, 2);
1599 prt_write_real(prt_line_height, 2);
1600
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001601 // Lastly add the color of the background
Bram Moolenaar81366db2005-07-24 21:16:51 +00001602 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1603 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1604 b = prt_new_bgcol & 0xff;
1605 prt_write_real(r / 255.0, 3);
1606 prt_write_real(g / 255.0, 3);
1607 prt_write_real(b / 255.0, 3);
1608 prt_write_string("bg\n");
1609 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001610 // Draw underlines before the text as it makes it slightly easier to
1611 // find the starting point.
Bram Moolenaar81366db2005-07-24 21:16:51 +00001612 if (prt_do_underline)
1613 {
1614 if (prt_do_moveto)
1615 {
1616 prt_write_real(prt_pos_x_moveto, 2);
1617 prt_write_real(prt_pos_y_moveto, 2);
1618 prt_write_string("m\n");
1619 prt_do_moveto = FALSE;
1620 }
1621
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001622 // Underline length of text run
Bram Moolenaar81366db2005-07-24 21:16:51 +00001623 prt_write_real(prt_text_run, 2);
1624 prt_write_string("ul\n");
1625 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001626 // Draw the text
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001627 if (prt_out_mbyte)
1628 prt_write_string("<");
1629 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001630 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001631 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001632 if (prt_out_mbyte)
1633 prt_write_string(">");
1634 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001635 prt_write_string(")");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001636 // Add a moveto if need be and use the appropriate show procedure
Bram Moolenaar81366db2005-07-24 21:16:51 +00001637 if (prt_do_moveto)
1638 {
1639 prt_write_real(prt_pos_x_moveto, 2);
1640 prt_write_real(prt_pos_y_moveto, 2);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001641 // moveto and a show
Bram Moolenaar81366db2005-07-24 21:16:51 +00001642 prt_write_string("ms\n");
1643 prt_do_moveto = FALSE;
1644 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001645 else // Simple show
Bram Moolenaar81366db2005-07-24 21:16:51 +00001646 prt_write_string("s\n");
1647
1648 ga_clear(&prt_ps_buffer);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001649 ga_init2(&prt_ps_buffer, sizeof(char), prt_bufsiz);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001650 }
1651}
1652
1653
1654 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001655prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001656{
1657 char_u *resource_filename = cookie;
1658
1659 if (STRLEN(filename) >= MAXPATHL)
1660 *resource_filename = NUL;
1661 else
1662 STRCPY(resource_filename, filename);
1663}
1664
1665 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001666prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001667{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001668 char_u *buffer;
1669 int retval;
1670
1671 buffer = alloc(MAXPATHL + 1);
1672 if (buffer == NULL)
1673 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001674
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001675 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001676 // Look for named resource file in runtimepath
Bram Moolenaar81366db2005-07-24 21:16:51 +00001677 STRCPY(buffer, "print");
1678 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001679 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1680 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001681 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001682 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001683 resource->filename)
1684 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001685 vim_free(buffer);
1686 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001687}
1688
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001689// PS CR and LF characters have platform independent values
Bram Moolenaar81366db2005-07-24 21:16:51 +00001690#define PSLF (0x0a)
1691#define PSCR (0x0d)
1692
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001693// Static buffer to read initial comments in a resource file, some can have a
1694// couple of KB of comments!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001695#define PRT_FILE_BUFFER_LEN (2048)
1696struct prt_resfile_buffer_S
1697{
1698 char_u buffer[PRT_FILE_BUFFER_LEN];
1699 int len;
1700 int line_start;
1701 int line_end;
1702};
1703
1704static struct prt_resfile_buffer_S prt_resfile;
1705
1706 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001707prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001708{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001710
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001711 // Move to start of next line and then find end of line
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 idx = prt_resfile.line_end + 1;
1713 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001714 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001716 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001717 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001718 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001719 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001720
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001722 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001723 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001724 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001725 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001726 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001728
Bram Moolenaar89d40322006-08-29 15:30:07 +00001729 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001730}
1731
1732 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001733prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001734{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001735 // Force not equal if string is longer than remainder of line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001736 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001737 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001738
1739 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001740 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001741}
1742
1743 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001744prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001745{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001746 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001747
Bram Moolenaar89d40322006-08-29 15:30:07 +00001748 idx = prt_resfile.line_start + offset;
1749 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001750 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001751 if (isspace(prt_resfile.buffer[idx]))
1752 return idx - prt_resfile.line_start;
1753 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001754 }
1755 return -1;
1756}
1757
1758 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001759prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001760{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001762
Bram Moolenaar89d40322006-08-29 15:30:07 +00001763 idx = prt_resfile.line_start + offset;
1764 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001765 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 if (!isspace(prt_resfile.buffer[idx]))
1767 return idx - prt_resfile.line_start;
1768 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001769 }
1770 return -1;
1771}
1772
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001773// prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1774// if a DSC comment is found, else false
Bram Moolenaar81366db2005-07-24 21:16:51 +00001775 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001776prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001777{
1778 int comment;
1779 int offset;
1780
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001781 // Move to start of next line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001782 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001783 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785 // DSC comments always start %%
Bram Moolenaar81366db2005-07-24 21:16:51 +00001786 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001787 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001788
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001789 // Find type of DSC comment
K.Takataeeec2542021-06-02 13:28:16 +02001790 for (comment = 0; comment < (int)ARRAY_LENGTH(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001791 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1792 prt_dsc_table[comment].len) == 0)
1793 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001794
K.Takataeeec2542021-06-02 13:28:16 +02001795 if (comment != ARRAY_LENGTH(prt_dsc_table))
Bram Moolenaar81366db2005-07-24 21:16:51 +00001796 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001797 // Return type of comment
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001798 p_dsc_line->type = prt_dsc_table[comment].type;
1799 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001800 }
1801 else
1802 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001803 // Unrecognised DSC comment, skip to ws after comment leader
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001804 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1805 offset = prt_resfile_skip_nonws(0);
1806 if (offset == -1)
1807 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001808 }
1809
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810 // Skip ws to comment value
Bram Moolenaar81366db2005-07-24 21:16:51 +00001811 offset = prt_resfile_skip_ws(offset);
1812 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001813 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001814
1815 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1816 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1817
1818 return TRUE;
1819}
1820
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001821/*
1822 * Improved hand crafted parser to get the type, title, and version number of a
Bram Moolenaar81366db2005-07-24 21:16:51 +00001823 * PS resource file so the file details can be added to the DSC header comments.
1824 */
1825 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001826prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001827{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001828 int offset;
1829 int seen_all;
1830 int seen_title;
1831 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001832 FILE *fd_resource;
1833 struct prt_dsc_line_S dsc_line;
1834
1835 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1836 if (fd_resource == NULL)
1837 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001838 semsg(_(e_cant_open_file_str_3), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001839 return FALSE;
1840 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001841 CLEAR_FIELD(prt_resfile.buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001842
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843 // Parse first line to ensure valid resource file
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001844 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001845 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001846 if (ferror(fd_resource))
1847 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001848 semsg(_(e_cant_read_postscript_resource_file_str),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001849 resource->filename);
1850 fclose(fd_resource);
1851 return FALSE;
1852 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001853 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001854
1855 prt_resfile.line_end = -1;
1856 prt_resfile.line_start = 0;
1857 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001858 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001859
1860 offset = 0;
1861
1862 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001863 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001864 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001865 semsg(_(e_file_str_is_not_postscript_resource_file),
1866 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001867 return FALSE;
1868 }
1869
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001870 // Skip over any version numbers and following ws
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001871 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001872 offset = prt_resfile_skip_nonws(offset);
1873 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001874 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001875 offset = prt_resfile_skip_ws(offset);
1876 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001877 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001878
1879 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001880 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001881 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001882 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1883 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001884 return FALSE;
1885 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001886 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001887
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001888 // Decide type of resource in the file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001889 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001890 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001891 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1892 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001893 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001894 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1895 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001896 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001897 resource->type = PRT_RESOURCE_TYPE_CMAP;
1898 else
1899 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001900 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1901 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001902 return FALSE;
1903 }
1904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001905 // Look for title and version of resource
Bram Moolenaar81366db2005-07-24 21:16:51 +00001906 resource->title[0] = '\0';
1907 resource->version[0] = '\0';
1908 seen_title = FALSE;
1909 seen_version = FALSE;
1910 seen_all = FALSE;
1911 while (!seen_all && prt_next_dsc(&dsc_line))
1912 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001913 switch (dsc_line.type)
1914 {
1915 case PRT_DSC_TITLE_TYPE:
1916 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
1917 seen_title = TRUE;
1918 if (seen_version)
1919 seen_all = TRUE;
1920 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001921
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001922 case PRT_DSC_VERSION_TYPE:
1923 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
1924 seen_version = TRUE;
1925 if (seen_title)
1926 seen_all = TRUE;
1927 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001928
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001929 case PRT_DSC_ENDCOMMENTS_TYPE:
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001930 // Won't find title or resource after this comment, stop searching
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001931 seen_all = TRUE;
1932 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001933
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001934 case PRT_DSC_MISC_TYPE:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001935 // Not interested in whatever comment this line had
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001936 break;
1937 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001938 }
1939
1940 if (!seen_title || !seen_version)
1941 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001942 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1943 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001944 return FALSE;
1945 }
1946
Bram Moolenaar81366db2005-07-24 21:16:51 +00001947 return TRUE;
1948}
1949
1950 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001951prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001952{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001953 // Version number m.n should match, the revision number does not matter
Bram Moolenaar81366db2005-07-24 21:16:51 +00001954 if (STRNCMP(resource->version, version, STRLEN(version)))
1955 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001956 semsg(_(e_str_resource_file_has_wrong_version),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001957 resource->name);
1958 return FALSE;
1959 }
1960
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001961 // Other checks to be added as needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00001962 return TRUE;
1963}
1964
1965 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001966prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001967{
1968 prt_write_string("%!PS-Adobe-3.0\n");
1969}
1970
1971 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001972prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001973{
1974 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1975 "%%%%%s\n", comment);
1976 prt_write_file(prt_line_buffer);
1977}
1978
1979 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001980prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001981{
1982 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1983 "%%%%%s: %s\n", comment, text);
1984 prt_write_file(prt_line_buffer);
1985}
1986
1987 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001988prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001989{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001990 // TODO - should scan 'text' for any chars needing escaping!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001991 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1992 "%%%%%s: (%s)\n", comment, text);
1993 prt_write_file(prt_line_buffer);
1994}
1995
1996#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
1997
1998 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001999prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002000{
2001 int i;
2002
2003 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2004 "%%%%%s:", comment);
2005 prt_write_file(prt_line_buffer);
2006
2007 for (i = 0; i < count; i++)
2008 {
2009 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2010 prt_write_file(prt_line_buffer);
2011 }
2012
2013 prt_write_string("\n");
2014}
2015
2016 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002017prt_dsc_resources(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002018 char *comment, // if NULL add to previous
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002019 char *type,
2020 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002021{
2022 if (comment != NULL)
2023 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2024 "%%%%%s: %s", comment, type);
2025 else
2026 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2027 "%%%%+ %s", type);
2028 prt_write_file(prt_line_buffer);
2029
2030 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2031 " %s\n", string);
2032 prt_write_file(prt_line_buffer);
2033}
2034
2035 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002036prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002037{
2038 int i;
2039
2040 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002041 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002042 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002043 if (ps_font->ps_fontname[i] != NULL)
2044 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002045}
2046
2047 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002048prt_dsc_requirements(
2049 int duplex,
2050 int tumble,
2051 int collate,
2052 int color,
2053 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002054{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002055 // Only output the comment if we need to.
2056 // Note: tumble is ignored if we are not duplexing
Bram Moolenaar81366db2005-07-24 21:16:51 +00002057 if (!(duplex || collate || color || (num_copies > 1)))
2058 return;
2059
2060 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2061 prt_write_file(prt_line_buffer);
2062
2063 if (duplex)
2064 {
2065 prt_write_string(" duplex");
2066 if (tumble)
2067 prt_write_string("(tumble)");
2068 }
2069 if (collate)
2070 prt_write_string(" collate");
2071 if (color)
2072 prt_write_string(" color");
2073 if (num_copies > 1)
2074 {
2075 prt_write_string(" numcopies(");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002076 // Note: no space wanted so don't use prt_write_int()
Bram Moolenaar81366db2005-07-24 21:16:51 +00002077 sprintf((char *)prt_line_buffer, "%d", num_copies);
2078 prt_write_file(prt_line_buffer);
2079 prt_write_string(")");
2080 }
2081 prt_write_string("\n");
2082}
2083
2084 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002085prt_dsc_docmedia(
2086 char *paper_name,
2087 double width,
2088 double height,
2089 double weight,
2090 char *colour,
2091 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002092{
2093 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2094 "%%%%DocumentMedia: %s ", paper_name);
2095 prt_write_file(prt_line_buffer);
2096 prt_write_real(width, 2);
2097 prt_write_real(height, 2);
2098 prt_write_real(weight, 2);
2099 if (colour == NULL)
2100 prt_write_string("()");
2101 else
2102 prt_write_string(colour);
2103 prt_write_string(" ");
2104 if (type == NULL)
2105 prt_write_string("()");
2106 else
2107 prt_write_string(type);
2108 prt_write_string("\n");
2109}
2110
2111 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002112mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002113{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002114 if (prt_out_mbyte)
2115 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002116 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002117
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002118 // Free off all CID font names created, but first clear duplicate
2119 // pointers to the same string (when the same font is used for more than
2120 // one style).
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002121 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2122 {
2123 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002124 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002125 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002126 }
2127
2128 if (prt_do_conv)
2129 {
2130 convert_setup(&prt_conv, NULL, NULL);
2131 prt_do_conv = FALSE;
2132 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002133 if (prt_ps_fd != NULL)
2134 {
2135 fclose(prt_ps_fd);
2136 prt_ps_fd = NULL;
2137 prt_file_error = FALSE;
2138 }
2139 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002140 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002141}
2142
2143 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002144to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002145{
2146 float ret;
2147 int u;
2148 int nr;
2149
2150 u = prt_get_unit(idx);
2151 if (u == PRT_UNIT_NONE)
2152 {
2153 u = PRT_UNIT_PERC;
2154 nr = def_number;
2155 }
2156 else
2157 nr = printer_opts[idx].number;
2158
2159 switch (u)
2160 {
2161 case PRT_UNIT_INCH:
2162 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2163 break;
2164 case PRT_UNIT_MM:
2165 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2166 break;
2167 case PRT_UNIT_POINT:
2168 ret = (float)nr;
2169 break;
2170 case PRT_UNIT_PERC:
2171 default:
2172 ret = (float)(physsize * nr) / 100;
2173 break;
2174 }
2175
2176 return ret;
2177}
2178
2179/*
2180 * Calculate margins for given width and height from printoptions settings.
2181 */
2182 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002183prt_page_margins(
2184 double width,
2185 double height,
2186 double *left,
2187 double *right,
2188 double *top,
2189 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002190{
2191 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2192 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2193 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2194 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2195}
2196
2197 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002198prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002199{
2200 prt_line_height = (float)font_scale;
2201 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2202}
2203
2204
2205 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002206prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002207{
2208 if (prt_use_number())
2209 {
2210 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002211 // If we are outputting multi-byte characters then line numbers will be
2212 // printed with half width characters
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002213 if (prt_out_mbyte)
2214 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002215 prt_left_margin += prt_number_width;
2216 }
2217 else
2218 prt_number_width = 0.0;
2219
2220 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2221}
2222
Bram Moolenaar81366db2005-07-24 21:16:51 +00002223 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002224prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002225{
2226 char *fontname;
2227
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002228 fontname = alloc(name_len + 1);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002229 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002230 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002231 vim_strncpy((char_u *)fontname, name, name_len);
2232 prt_ps_mb_font.ps_fontname[font] = fontname;
2233
2234 return TRUE;
2235}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002236
2237/*
2238 * Get number of lines of text that fit on a page (excluding the header).
2239 */
2240 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002241prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002242{
2243 int lpp;
2244
2245 /*
2246 * Calculate offset to lower left corner of background rect based on actual
2247 * font height (based on its bounding box) and the line height, handling the
2248 * case where the font height can exceed the line height.
2249 */
2250 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2251 prt_ps_font->bbox_min_y);
2252 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2253 {
2254 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2255 (1000.0 - (prt_ps_font->bbox_max_y -
2256 prt_ps_font->bbox_min_y)) / 2);
2257 }
2258
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002259 // Get height for topmost line based on background rect offset.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002260 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2261
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002262 // Calculate lpp
Bram Moolenaar81366db2005-07-24 21:16:51 +00002263 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2264
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002265 // Adjust top margin if there is a header
Bram Moolenaar81366db2005-07-24 21:16:51 +00002266 prt_top_margin -= prt_line_height * prt_header_height();
2267
2268 return lpp - prt_header_height();
2269}
2270
Bram Moolenaar81366db2005-07-24 21:16:51 +00002271 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002272prt_match_encoding(
2273 char *p_encoding,
2274 struct prt_ps_mbfont_S *p_cmap,
2275 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002276{
2277 int mbenc;
2278 int enc_len;
2279 struct prt_ps_encoding_S *p_mbenc;
2280
2281 *pp_mbenc = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002282 // Look for recognised encoding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002283 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002284 p_mbenc = p_cmap->encodings;
2285 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2286 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002287 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2288 {
2289 *pp_mbenc = p_mbenc;
2290 return TRUE;
2291 }
2292 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002293 }
2294 return FALSE;
2295}
2296
2297 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002298prt_match_charset(
2299 char *p_charset,
2300 struct prt_ps_mbfont_S *p_cmap,
2301 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002302{
2303 int mbchar;
2304 int char_len;
2305 struct prt_ps_charset_S *p_mbchar;
2306
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002307 // Look for recognised character set, using default if one is not given
Bram Moolenaar81366db2005-07-24 21:16:51 +00002308 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002309 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002310 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002311 p_mbchar = p_cmap->charsets;
2312 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2313 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002314 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2315 {
2316 *pp_mbchar = p_mbchar;
2317 return TRUE;
2318 }
2319 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002320 }
2321 return FALSE;
2322}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002323
Bram Moolenaar81366db2005-07-24 21:16:51 +00002324 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002325mch_print_init(
2326 prt_settings_T *psettings,
2327 char_u *jobname,
2328 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002329{
2330 int i;
2331 char *paper_name;
2332 int paper_strlen;
2333 int fontsize;
2334 char_u *p;
2335 double left;
2336 double right;
2337 double top;
2338 double bottom;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002339 int props;
2340 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002341 char_u *p_encoding;
2342 struct prt_ps_encoding_S *p_mbenc;
2343 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002344 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002345
2346#if 0
2347 /*
2348 * TODO:
2349 * If "forceit" is false: pop up a dialog to select:
2350 * - printer name
2351 * - copies
2352 * - collated/uncollated
2353 * - duplex off/long side/short side
2354 * - paper size
2355 * - portrait/landscape
2356 * - font size
2357 *
2358 * If "forceit" is true: use the default printer and settings
2359 */
2360 if (forceit)
2361 s_pd.Flags |= PD_RETURNDEFAULT;
2362#endif
2363
2364 /*
2365 * Set up font and encoding.
2366 */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002367 p_encoding = enc_skip(p_penc);
2368 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002369 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002370
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002371 // Look for a multi-byte font that matches the encoding and character set.
2372 // Only look if multi-byte character set is defined, or using multi-byte
2373 // encoding other than Unicode. This is because a Unicode encoding does not
2374 // uniquely identify a CJK character set to use.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002375 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002376 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002377 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002378 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002379 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002380
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002381 p_mbenc_first = NULL;
K.Takataeeec2542021-06-02 13:28:16 +02002382 for (cmap = 0; cmap < (int)ARRAY_LENGTH(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002383 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002384 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002385 {
2386 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002387 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002388 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002389 cmap_first = cmap;
2390 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002391 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002392 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002393 break;
2394 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002395
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002396 // Use first encoding matched if no charset matched
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002397 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002398 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002399 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002400 cmap = cmap_first;
2401 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002402 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002403
2404 prt_out_mbyte = (p_mbenc != NULL);
2405 if (prt_out_mbyte)
2406 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002407 // Build CMap name - will be same for all multi-byte fonts used
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002408 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002409
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002410 prt_custom_cmap = (p_mbchar == NULL);
2411 if (!prt_custom_cmap)
2412 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002413 // Check encoding and character set are compatible
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002414 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2415 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002416 emsg(_(e_incompatible_multi_byte_encoding_and_character_set));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002417 return FALSE;
2418 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002419
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002420 // Add charset name if not empty
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002421 if (p_mbchar->cmap_charset != NULL)
2422 {
2423 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002424 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002425 STRCAT(prt_cmap, "-");
2426 }
2427 }
2428 else
2429 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002430 // Add custom CMap character set name
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002431 if (*p_pmcs == NUL)
2432 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002433 emsg(_(e_printmbcharset_cannot_be_empty_with_multi_byte_encoding));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002434 return FALSE;
2435 }
2436 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2437 STRCAT(prt_cmap, "-");
2438 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002439
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002440 // CMap name ends with (optional) encoding name and -H for horizontal
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002441 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002442 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002443 {
2444 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2445 STRCAT(prt_cmap, "-");
2446 }
2447 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002448
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002449 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2450 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002451 emsg(_(e_no_default_font_specified_for_multi_byte_printing));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002452 return FALSE;
2453 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002454
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002455 // Derive CID font names with fallbacks if not defined
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002456 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2457 mbfont_opts[OPT_MBFONT_REGULAR].string,
2458 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2459 return FALSE;
2460 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2461 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2462 mbfont_opts[OPT_MBFONT_BOLD].string,
2463 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2464 return FALSE;
2465 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2466 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2467 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2468 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2469 return FALSE;
2470 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2471 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002472 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2473 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002474 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002475
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002476 // Check if need to use Courier for ASCII code range, and if so pick up
2477 // the encoding to use
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002478 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2479 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2480 if (prt_use_courier)
2481 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002482 // Use national ASCII variant unless ASCII wanted
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002483 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2484 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2485 prt_ascii_encoding = "ascii";
2486 else
2487 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2488 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002489
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002490 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002491 }
2492 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00002493 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002494 prt_use_courier = FALSE;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002495 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002496 }
2497
2498 /*
2499 * Find the size of the paper and set the margins.
2500 */
2501 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2502 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2503 if (printer_opts[OPT_PRINT_PAPER].present)
2504 {
2505 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2506 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2507 }
2508 else
2509 {
2510 paper_name = "A4";
2511 paper_strlen = 2;
2512 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002513 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002514 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2515 && STRNICMP(prt_mediasize[i].name, paper_name,
2516 paper_strlen) == 0)
2517 break;
2518 if (i == PRT_MEDIASIZE_LEN)
2519 i = 0;
2520 prt_media = i;
2521
2522 /*
2523 * Set PS pagesize based on media dimensions and print orientation.
2524 * Note: Media and page sizes have defined meanings in PostScript and should
2525 * be kept distinct. Media is the paper (or transparency, or ...) that is
2526 * printed on, whereas the page size is the area that the PostScript
2527 * interpreter renders into.
2528 */
2529 if (prt_portrait)
2530 {
2531 prt_page_width = prt_mediasize[i].width;
2532 prt_page_height = prt_mediasize[i].height;
2533 }
2534 else
2535 {
2536 prt_page_width = prt_mediasize[i].height;
2537 prt_page_height = prt_mediasize[i].width;
2538 }
2539
2540 /*
2541 * Set PS page margins based on the PS pagesize, not the mediasize - this
2542 * needs to be done before the cpl and lpp are calculated.
2543 */
2544 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2545 &bottom);
2546 prt_left_margin = (float)left;
2547 prt_right_margin = (float)right;
2548 prt_top_margin = (float)top;
2549 prt_bottom_margin = (float)bottom;
2550
2551 /*
2552 * Set up the font size.
2553 */
2554 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2555 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2556 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2557 fontsize = atoi((char *)p + 2);
2558 prt_font_metrics(fontsize);
2559
2560 /*
2561 * Return the number of characters per line, and lines per page for the
2562 * generic print code.
2563 */
2564 psettings->chars_per_line = prt_get_cpl();
2565 psettings->lines_per_page = prt_get_lpp();
2566
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002567 // Catch margin settings that leave no space for output!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002568 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2569 return FAIL;
2570
2571 /*
2572 * Sort out the number of copies to be printed. PS by default will do
2573 * uncollated copies for you, so once we know how many uncollated copies are
2574 * wanted cache it away and lie to the generic code that we only want one
2575 * uncollated copy.
2576 */
2577 psettings->n_collated_copies = 1;
2578 psettings->n_uncollated_copies = 1;
2579 prt_num_copies = 1;
2580 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2581 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2582 if (prt_collate)
2583 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002584 // TODO: Get number of collated copies wanted.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002585 psettings->n_collated_copies = 1;
2586 }
2587 else
2588 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002589 // TODO: Get number of uncollated copies wanted and update the cached
2590 // count.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002591 prt_num_copies = 1;
2592 }
2593
2594 psettings->jobname = jobname;
2595
2596 /*
2597 * Set up printer duplex and tumble based on Duplex option setting - default
2598 * is long sided duplex printing (i.e. no tumble).
2599 */
2600 prt_duplex = TRUE;
2601 prt_tumble = FALSE;
2602 psettings->duplex = 1;
2603 if (printer_opts[OPT_PRINT_DUPLEX].present)
2604 {
2605 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2606 {
2607 prt_duplex = FALSE;
2608 psettings->duplex = 0;
2609 }
2610 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2611 == 0)
2612 prt_tumble = TRUE;
2613 }
2614
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002615 // For now user abort not supported
Bram Moolenaar81366db2005-07-24 21:16:51 +00002616 psettings->user_abort = 0;
2617
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002618 // If the user didn't specify a file name, use a temp file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002619 if (psettings->outfile == NULL)
2620 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002621 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002622 if (prt_ps_file_name == NULL)
2623 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002624 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002625 return FAIL;
2626 }
2627 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2628 }
2629 else
2630 {
2631 p = expand_env_save(psettings->outfile);
2632 if (p != NULL)
2633 {
2634 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2635 vim_free(p);
2636 }
2637 }
2638 if (prt_ps_fd == NULL)
2639 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00002640 emsg(_(e_cant_open_postscript_output_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002641 mch_print_cleanup();
2642 return FAIL;
2643 }
2644
2645 prt_bufsiz = psettings->chars_per_line;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002646 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002647 prt_bufsiz *= 2;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002648 ga_init2(&prt_ps_buffer, sizeof(char), prt_bufsiz);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002649
2650 prt_page_num = 0;
2651
2652 prt_attribute_change = FALSE;
2653 prt_need_moveto = FALSE;
2654 prt_need_font = FALSE;
2655 prt_need_fgcol = FALSE;
2656 prt_need_bgcol = FALSE;
2657 prt_need_underline = FALSE;
2658
2659 prt_file_error = FALSE;
2660
2661 return OK;
2662}
2663
2664 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002665prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002666{
2667 FILE* fd_resource;
2668 char_u resource_buffer[512];
2669 size_t bytes_read;
2670
2671 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2672 if (fd_resource == NULL)
2673 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002674 semsg(_(e_cant_open_file_str_2), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002675 return FALSE;
2676 }
2677 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2678 (char *)resource->title);
2679
2680 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2681
2682 for (;;)
2683 {
2684 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2685 sizeof(resource_buffer), fd_resource);
2686 if (ferror(fd_resource))
2687 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002688 semsg(_(e_cant_read_postscript_resource_file_str),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002689 resource->filename);
2690 fclose(fd_resource);
2691 return FALSE;
2692 }
2693 if (bytes_read == 0)
2694 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002695 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002696 if (prt_file_error)
2697 {
2698 fclose(fd_resource);
2699 return FALSE;
2700 }
2701 }
2702 fclose(fd_resource);
2703
2704 prt_dsc_noarg("EndDocument");
2705
2706 prt_dsc_noarg("EndResource");
2707
2708 return TRUE;
2709}
2710
2711 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002712mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002713{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002714 int bbox[4];
Bram Moolenaar81366db2005-07-24 21:16:51 +00002715 double left;
2716 double right;
2717 double top;
2718 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002719 struct prt_ps_resource_S *res_prolog;
2720 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002721 char buffer[256];
2722 char_u *p_encoding;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002723 struct prt_ps_resource_S *res_cidfont;
2724 struct prt_ps_resource_S *res_cmap;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002725 int retval = FALSE;
2726
2727 res_prolog = (struct prt_ps_resource_S *)
2728 alloc(sizeof(struct prt_ps_resource_S));
2729 res_encoding = (struct prt_ps_resource_S *)
2730 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002731 res_cidfont = (struct prt_ps_resource_S *)
2732 alloc(sizeof(struct prt_ps_resource_S));
2733 res_cmap = (struct prt_ps_resource_S *)
2734 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002735 if (res_prolog == NULL || res_encoding == NULL
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002736 || res_cidfont == NULL || res_cmap == NULL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002737 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002738
2739 /*
2740 * PS DSC Header comments - no PS code!
2741 */
2742 prt_dsc_start();
2743 prt_dsc_textline("Title", (char *)psettings->jobname);
2744 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002745 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002746 prt_dsc_textline("For", buffer);
2747 prt_dsc_textline("Creator", VIM_VERSION_LONG);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002748 // Note: to ensure Clean8bit I don't think we can use LC_TIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02002749
2750 prt_dsc_textline("CreationDate", get_ctime(time(NULL), FALSE));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002751 prt_dsc_textline("DocumentData", "Clean8Bit");
2752 prt_dsc_textline("Orientation", "Portrait");
2753 prt_dsc_atend("Pages");
2754 prt_dsc_textline("PageOrder", "Ascend");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002755 // The bbox does not change with orientation - it is always in the default
2756 // user coordinate system! We have to recalculate right and bottom
2757 // coordinates based on the font metrics for the bbox to be accurate.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002758 prt_page_margins(prt_mediasize[prt_media].width,
2759 prt_mediasize[prt_media].height,
2760 &left, &right, &top, &bottom);
2761 bbox[0] = (int)left;
2762 if (prt_portrait)
2763 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002764 // In portrait printing the fixed point is the top left corner so we
2765 // derive the bbox from that point. We have the expected cpl chars
2766 // across the media and lpp lines down the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002767 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002768 * (double)prt_line_height);
2769 bbox[2] = (int)(left + psettings->chars_per_line
2770 * (double)prt_char_width + 0.5);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002771 bbox[3] = (int)(top + 0.5);
2772 }
2773 else
2774 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002775 // In landscape printing the fixed point is the bottom left corner so we
2776 // derive the bbox from that point. We have lpp chars across the media
2777 // and cpl lines up the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002778 bbox[1] = (int)bottom;
2779 bbox[2] = (int)(left + ((psettings->lines_per_page
2780 + prt_header_height()) * prt_line_height) + 0.5);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002781 bbox[3] = (int)(bottom + psettings->chars_per_line
2782 * (double)prt_char_width + 0.5);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002783 }
2784 prt_dsc_ints("BoundingBox", 4, bbox);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002785 // The media width and height does not change with landscape printing!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002786 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2787 prt_mediasize[prt_media].width,
2788 prt_mediasize[prt_media].height,
2789 (double)0, NULL, NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002790 // Define fonts needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00002791 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002792 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002793 if (prt_out_mbyte)
2794 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002795 prt_dsc_font_resource((prt_use_courier ? NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002796 : "DocumentNeededResources"), &prt_ps_mb_font);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002797 if (!prt_custom_cmap)
2798 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002799 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002800
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002801 // Search for external resources VIM supplies
Bram Moolenaard9462e32011-04-11 21:35:11 +02002802 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002803 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002804 semsg(_(e_cant_find_postscript_resource_file_str_ps), "prolog");
Bram Moolenaar0a383962014-11-27 17:37:57 +01002805 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002806 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002807 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002808 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002809 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002810 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002811 if (prt_out_mbyte)
2812 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002813 // Look for required version of multi-byte printing procset
Bram Moolenaard9462e32011-04-11 21:35:11 +02002814 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002815 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002816 semsg(_(e_cant_find_postscript_resource_file_str_ps), "cidfont");
Bram Moolenaar0a383962014-11-27 17:37:57 +01002817 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002818 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002819 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002820 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002821 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002822 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002823 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002825 // Find an encoding to use for printing.
2826 // Check 'printencoding'. If not set or not found, then use 'encoding'. If
2827 // that cannot be found then default to "latin1".
2828 // Note: VIM specific encoding header is always skipped.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002829 if (!prt_out_mbyte)
2830 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002831 p_encoding = enc_skip(p_penc);
2832 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002833 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002834 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002835 // 'printencoding' not set or not supported - find alternate
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002836 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002837
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002838 p_encoding = enc_skip(p_enc);
2839 props = enc_canon_props(p_encoding);
2840 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002841 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002842 // 8-bit 'encoding' is not supported
2843 {
2844 // Use latin1 as default printing encoding
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002845 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002846 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002847 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002848 semsg(_(e_cant_find_postscript_resource_file_str_ps),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002849 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002850 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002851 }
2852 }
2853 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002854 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002855 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002856 // For the moment there are no checks on encoding resource files to
2857 // perform
Bram Moolenaar81366db2005-07-24 21:16:51 +00002858 }
2859 else
2860 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002861 p_encoding = enc_skip(p_penc);
2862 if (*p_encoding == NUL)
2863 p_encoding = enc_skip(p_enc);
2864 if (prt_use_courier)
2865 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002866 // Include ASCII range encoding vector
Bram Moolenaard9462e32011-04-11 21:35:11 +02002867 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002868 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002869 semsg(_(e_cant_find_postscript_resource_file_str_ps),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002870 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002871 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002872 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002873 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002874 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002875 // For the moment there are no checks on encoding resource files to
2876 // perform
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002877 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002878 }
2879
2880 prt_conv.vc_type = CONV_NONE;
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002881 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT))
2882 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002883 // Set up encoding conversion if required
Bram Moolenaar81366db2005-07-24 21:16:51 +00002884 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
2885 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002886 semsg(_(e_unable_to_convert_to_print_encoding_str), p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002887 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002888 }
2889 prt_do_conv = TRUE;
2890 }
2891 prt_do_conv = prt_conv.vc_type != CONV_NONE;
2892
2893 if (prt_out_mbyte && prt_custom_cmap)
2894 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002895 // Find user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002896 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002897 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002898 semsg(_(e_cant_find_postscript_resource_file_str_ps), prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002899 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002900 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002901 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002902 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002903 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002905 // List resources supplied
Bram Moolenaard9462e32011-04-11 21:35:11 +02002906 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002907 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002908 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002909 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002910 if (prt_out_mbyte)
2911 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002912 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002913 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002914 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002915 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002916
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002917 if (prt_custom_cmap)
2918 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002919 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002920 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002921 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002922 prt_dsc_resources(NULL, "cmap", buffer);
2923 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002924 }
2925 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002926 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002927 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002928 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002929 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002930 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002931 }
2932 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
2933#ifdef FEAT_SYN_HL
2934 psettings->do_syntax
2935#else
2936 0
2937#endif
2938 , prt_num_copies);
2939 prt_dsc_noarg("EndComments");
2940
2941 /*
2942 * PS Document page defaults
2943 */
2944 prt_dsc_noarg("BeginDefaults");
2945
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002946 // List font resources most likely common to all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002947 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002948 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002949 if (prt_out_mbyte)
2950 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002951 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
2952 &prt_ps_mb_font);
2953 if (!prt_custom_cmap)
2954 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002955 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002956
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002957 // Paper will be used for all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002958 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
2959
2960 prt_dsc_noarg("EndDefaults");
2961
2962 /*
2963 * PS Document prolog inclusion - all required procsets.
2964 */
2965 prt_dsc_noarg("BeginProlog");
2966
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002967 // Add required procsets - NOTE: order is important!
Bram Moolenaard9462e32011-04-11 21:35:11 +02002968 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002969 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002970 if (prt_out_mbyte)
2971 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002972 // Add CID font procset, and any user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002973 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002974 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002975 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002976 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002977 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002978
Bram Moolenaar81366db2005-07-24 21:16:51 +00002979 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002980 // There will be only one Roman font encoding to be included in the PS
2981 // file.
Bram Moolenaard9462e32011-04-11 21:35:11 +02002982 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002983 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002984
2985 prt_dsc_noarg("EndProlog");
2986
2987 /*
2988 * PS Document setup - must appear after the prolog
2989 */
2990 prt_dsc_noarg("BeginSetup");
2991
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002992 // Device setup - page size and number of uncollated copies
Bram Moolenaar81366db2005-07-24 21:16:51 +00002993 prt_write_int((int)prt_mediasize[prt_media].width);
2994 prt_write_int((int)prt_mediasize[prt_media].height);
2995 prt_write_int(0);
2996 prt_write_string("sps\n");
2997 prt_write_int(prt_num_copies);
2998 prt_write_string("nc\n");
2999 prt_write_boolean(prt_duplex);
3000 prt_write_boolean(prt_tumble);
3001 prt_write_string("dt\n");
3002 prt_write_boolean(prt_collate);
3003 prt_write_string("c\n");
3004
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003005 // Font resource inclusion and definition
Bram Moolenaar81366db2005-07-24 21:16:51 +00003006 if (!prt_out_mbyte || prt_use_courier)
3007 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003008 // When using Courier for ASCII range when printing multi-byte, need to
3009 // pick up ASCII encoding to use with it.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003010 if (prt_use_courier)
3011 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003012 prt_dsc_resources("IncludeResource", "font",
3013 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3014 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3015 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3016 prt_dsc_resources("IncludeResource", "font",
3017 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3018 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3019 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3020 prt_dsc_resources("IncludeResource", "font",
3021 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3022 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3023 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3024 prt_dsc_resources("IncludeResource", "font",
3025 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3026 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3027 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003028 }
3029 if (prt_out_mbyte)
3030 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003031 // Define the CID fonts to be used in the job. Typically CJKV fonts do
3032 // not have an italic form being a western style, so where no font is
3033 // defined for these faces VIM falls back to an existing face.
3034 // Note: if using Courier for the ASCII range then the printout will
3035 // have bold/italic/bolditalic regardless of the setting of printmbfont.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003036 prt_dsc_resources("IncludeResource", "font",
3037 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3038 if (!prt_custom_cmap)
3039 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3040 prt_def_cidfont("CF0", (int)prt_line_height,
3041 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003042
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003043 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3044 {
3045 prt_dsc_resources("IncludeResource", "font",
3046 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3047 if (!prt_custom_cmap)
3048 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3049 prt_def_cidfont("CF1", (int)prt_line_height,
3050 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3051 }
3052 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003053 // Use ROMAN for BOLD
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003054 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003055
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003056 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3057 {
3058 prt_dsc_resources("IncludeResource", "font",
3059 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3060 if (!prt_custom_cmap)
3061 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3062 prt_def_cidfont("CF2", (int)prt_line_height,
3063 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3064 }
3065 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003066 // Use ROMAN for OBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003067 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003068
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003069 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3070 {
3071 prt_dsc_resources("IncludeResource", "font",
3072 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3073 if (!prt_custom_cmap)
3074 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3075 prt_def_cidfont("CF3", (int)prt_line_height,
3076 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3077 }
3078 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003079 // Use BOLD for BOLDOBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003080 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003081 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003082
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003083 // Misc constant vars used for underlining and background rects
Bram Moolenaar81366db2005-07-24 21:16:51 +00003084 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3085 prt_ps_font->uline_offset), 2);
3086 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3087 prt_ps_font->uline_width), 2);
3088 prt_def_var("BO", prt_bgcol_offset, 2);
3089
3090 prt_dsc_noarg("EndSetup");
3091
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003092 // Fail if any problems writing out to the PS file
Bram Moolenaard9462e32011-04-11 21:35:11 +02003093 retval = !prt_file_error;
3094
3095theend:
3096 vim_free(res_prolog);
3097 vim_free(res_encoding);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003098 vim_free(res_cidfont);
3099 vim_free(res_cmap);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003100
3101 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003102}
3103
3104 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003105mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003106{
3107 prt_dsc_noarg("Trailer");
3108
3109 /*
3110 * Output any info we don't know in toto until we finish
3111 */
3112 prt_dsc_ints("Pages", 1, &prt_page_num);
3113
3114 prt_dsc_noarg("EOF");
3115
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003116 // Write CTRL-D to close serial communication link if used.
3117 // NOTHING MUST BE WRITTEN AFTER THIS!
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003118 prt_write_file((char_u *)"\004");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003119
3120 if (!prt_file_error && psettings->outfile == NULL
3121 && !got_int && !psettings->user_abort)
3122 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003123 // Close the file first.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003124 if (prt_ps_fd != NULL)
3125 {
3126 fclose(prt_ps_fd);
3127 prt_ps_fd = NULL;
3128 }
3129 prt_message((char_u *)_("Sending to printer..."));
3130
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003131 // Not printing to a file: use 'printexpr' to print the file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003132 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003133 emsg(_(e_failed_to_print_postscript_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003134 else
3135 prt_message((char_u *)_("Print job sent."));
3136 }
3137
3138 mch_print_cleanup();
3139}
3140
3141 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003142mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003143{
3144 prt_flush_buffer();
3145
3146 prt_write_string("re sp\n");
3147
3148 prt_dsc_noarg("PageTrailer");
3149
3150 return !prt_file_error;
3151}
3152
Bram Moolenaar81366db2005-07-24 21:16:51 +00003153 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003154mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003155{
3156 int page_num[2];
3157
3158 prt_page_num++;
3159
3160 page_num[0] = page_num[1] = prt_page_num;
3161 prt_dsc_ints("Page", 2, page_num);
3162
3163 prt_dsc_noarg("BeginPageSetup");
3164
3165 prt_write_string("sv\n0 g\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003166 prt_in_ascii = !prt_out_mbyte;
3167 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003168 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003169 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003170 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003171 prt_fgcol = PRCOLOR_BLACK;
3172 prt_bgcol = PRCOLOR_WHITE;
3173 prt_font = PRT_PS_FONT_ROMAN;
3174
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003175 // Set up page transformation for landscape printing.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003176 if (!prt_portrait)
3177 {
3178 prt_write_int(-((int)prt_mediasize[prt_media].width));
3179 prt_write_string("sl\n");
3180 }
3181
3182 prt_dsc_noarg("EndPageSetup");
3183
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003184 // We have reset the font attributes, force setting them again.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003185 curr_bg = (long_u)0xffffffff;
3186 curr_fg = (long_u)0xffffffff;
3187 curr_bold = MAYBE;
3188
3189 return !prt_file_error;
3190}
3191
3192 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003193mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003194{
3195 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3196}
3197
3198static float prt_pos_x = 0;
3199static float prt_pos_y = 0;
3200
3201 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003202mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003203{
3204 prt_pos_x = prt_left_margin;
3205 if (margin)
3206 prt_pos_x -= prt_number_width;
3207
3208 prt_pos_y = prt_top_margin - prt_first_line_height -
3209 page_line * prt_line_height;
3210
3211 prt_attribute_change = TRUE;
3212 prt_need_moveto = TRUE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003213 prt_half_width = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003214}
3215
Bram Moolenaar81366db2005-07-24 21:16:51 +00003216 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003217mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003218{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003219 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003220 int need_break;
3221 char_u ch;
3222 char_u ch_buff[8];
3223 float char_width;
3224 float next_pos;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003225 int in_ascii;
3226 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003227 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003228
3229 char_width = prt_char_width;
3230
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003231 // Ideally VIM would create a rearranged CID font to combine a Roman and
3232 // CJKV font to do what VIM is doing here - use a Roman font for characters
3233 // in the ASCII range, and the original CID font for everything else.
3234 // The problem is that GhostScript still (as of 8.13) does not support
3235 // rearranged fonts even though they have been documented by Adobe for 7
3236 // years! If they ever do, a lot of this code will disappear.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003237 if (prt_use_courier)
3238 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003239 in_ascii = (len == 1 && *p < 0x80);
3240 if (prt_in_ascii)
3241 {
3242 if (!in_ascii)
3243 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003244 // No longer in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003245 prt_in_ascii = FALSE;
3246 prt_need_font = TRUE;
3247 prt_attribute_change = TRUE;
3248 }
3249 }
3250 else if (in_ascii)
3251 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003252 // Now in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003253 prt_in_ascii = TRUE;
3254 prt_need_font = TRUE;
3255 prt_attribute_change = TRUE;
3256 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003257 }
3258 if (prt_out_mbyte)
3259 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003260 half_width = ((*mb_ptr2cells)(p) == 1);
3261 if (half_width)
3262 char_width /= 2;
3263 if (prt_half_width)
3264 {
3265 if (!half_width)
3266 {
3267 prt_half_width = FALSE;
3268 prt_pos_x += prt_char_width/4;
3269 prt_need_moveto = TRUE;
3270 prt_attribute_change = TRUE;
3271 }
3272 }
3273 else if (half_width)
3274 {
3275 prt_half_width = TRUE;
3276 prt_pos_x += prt_char_width/4;
3277 prt_need_moveto = TRUE;
3278 prt_attribute_change = TRUE;
3279 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003280 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003281
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003282 // Output any required changes to the graphics state, after flushing any
3283 // text buffered so far.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003284 if (prt_attribute_change)
3285 {
3286 prt_flush_buffer();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003287 // Reset count of number of chars that will be printed
Bram Moolenaar81366db2005-07-24 21:16:51 +00003288 prt_text_run = 0;
3289
3290 if (prt_need_moveto)
3291 {
3292 prt_pos_x_moveto = prt_pos_x;
3293 prt_pos_y_moveto = prt_pos_y;
3294 prt_do_moveto = TRUE;
3295
3296 prt_need_moveto = FALSE;
3297 }
3298 if (prt_need_font)
3299 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003300 if (!prt_in_ascii)
3301 prt_write_string("CF");
3302 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003303 prt_write_string("F");
3304 prt_write_int(prt_font);
3305 prt_write_string("sf\n");
3306 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003307 }
3308 if (prt_need_fgcol)
3309 {
3310 int r, g, b;
3311 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3312 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3313 b = prt_fgcol & 0xff;
3314
3315 prt_write_real(r / 255.0, 3);
3316 if (r == g && g == b)
3317 prt_write_string("g\n");
3318 else
3319 {
3320 prt_write_real(g / 255.0, 3);
3321 prt_write_real(b / 255.0, 3);
3322 prt_write_string("r\n");
3323 }
3324 prt_need_fgcol = FALSE;
3325 }
3326
3327 if (prt_bgcol != PRCOLOR_WHITE)
3328 {
3329 prt_new_bgcol = prt_bgcol;
3330 if (prt_need_bgcol)
3331 prt_do_bgcol = TRUE;
3332 }
3333 else
3334 prt_do_bgcol = FALSE;
3335 prt_need_bgcol = FALSE;
3336
3337 if (prt_need_underline)
3338 prt_do_underline = prt_underline;
3339 prt_need_underline = FALSE;
3340
3341 prt_attribute_change = FALSE;
3342 }
3343
Bram Moolenaar81366db2005-07-24 21:16:51 +00003344 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003345 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003346 // Convert from multi-byte to 8-bit encoding
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003347 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003348 if (p == NULL)
3349 {
3350 p = (char_u *)"";
3351 len = 0;
3352 }
3353 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003354
3355 if (prt_out_mbyte)
3356 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003357 // Multi-byte character strings are represented more efficiently as hex
3358 // strings when outputting clean 8 bit PS.
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003359 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003360 {
3361 ch = prt_hexchar[(unsigned)(*p) >> 4];
3362 ga_append(&prt_ps_buffer, ch);
3363 ch = prt_hexchar[(*p) & 0xf];
3364 ga_append(&prt_ps_buffer, ch);
3365 p++;
3366 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003367 }
3368 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003369 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003370 // Add next character to buffer of characters to output.
3371 // Note: One printed character may require several PS characters to
3372 // represent it, but we only count them as one printed character.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003373 ch = *p;
3374 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3375 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003376 // Convert non-printing characters to either their escape or octal
3377 // sequence, ensures PS sent over a serial line does not interfere
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003378 // with the comms protocol.
3379 ga_append(&prt_ps_buffer, '\\');
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003380 switch (ch)
3381 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003382 case BS: ga_append(&prt_ps_buffer, 'b'); break;
3383 case TAB: ga_append(&prt_ps_buffer, 't'); break;
3384 case NL: ga_append(&prt_ps_buffer, 'n'); break;
3385 case FF: ga_append(&prt_ps_buffer, 'f'); break;
3386 case CAR: ga_append(&prt_ps_buffer, 'r'); break;
3387 case '(': ga_append(&prt_ps_buffer, '('); break;
3388 case ')': ga_append(&prt_ps_buffer, ')'); break;
3389 case '\\': ga_append(&prt_ps_buffer, '\\'); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003390
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003391 default:
3392 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003393 ga_append(&prt_ps_buffer, ch_buff[0]);
3394 ga_append(&prt_ps_buffer, ch_buff[1]);
3395 ga_append(&prt_ps_buffer, ch_buff[2]);
3396 break;
3397 }
3398 }
3399 else
3400 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003401 }
3402
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003403 // Need to free any translated characters
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003404 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003405
3406 prt_text_run += char_width;
3407 prt_pos_x += char_width;
3408
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003409 // The downside of fp - use relative error on right margin check
Bram Moolenaar81366db2005-07-24 21:16:51 +00003410 next_pos = prt_pos_x + prt_char_width;
3411 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003412 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003413
3414 if (need_break)
3415 prt_flush_buffer();
3416
3417 return need_break;
3418}
3419
3420 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003421mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003422{
3423 int font = 0;
3424
3425 if (iBold)
3426 font |= 0x01;
3427 if (iItalic)
3428 font |= 0x02;
3429
3430 if (font != prt_font)
3431 {
3432 prt_font = font;
3433 prt_attribute_change = TRUE;
3434 prt_need_font = TRUE;
3435 }
3436 if (prt_underline != iUnderline)
3437 {
3438 prt_underline = iUnderline;
3439 prt_attribute_change = TRUE;
3440 prt_need_underline = TRUE;
3441 }
3442}
3443
3444 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003445mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003446{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003447 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003448 prt_attribute_change = TRUE;
3449 prt_need_bgcol = TRUE;
3450}
3451
3452 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003453mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003454{
3455 if (fgcol != (long_u)prt_fgcol)
3456 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003458 prt_attribute_change = TRUE;
3459 prt_need_fgcol = TRUE;
3460 }
3461}
3462
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003463# endif //FEAT_POSTSCRIPT
3464#endif //FEAT_PRINTER