blob: 8abfff210737711ffb686122c26d01f16e2c64dd [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 *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000147parse_printoptions(optset_T *args UNUSED)
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 *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000158parse_printmbfont(optset_T *args UNUSED)
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{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000337 if (fg == curr_fg)
338 return;
339
340 curr_fg = fg;
341 mch_print_set_fg(fg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000342}
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{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000347 if (bg == curr_bg)
348 return;
349
350 curr_bg = bg;
351 mch_print_set_bg(bg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000352}
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{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000357 if (curr_bold == bold
358 && curr_italic == italic
359 && curr_underline == underline)
360 return;
361
362 curr_underline = underline;
363 curr_italic = italic;
364 curr_bold = bold;
365 mch_print_set_font(bold, italic, underline);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000366}
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
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000437 if (!printer_opts[idx].present)
438 return PRT_UNIT_NONE;
439
440 for (i = 0; i < 4; ++i)
441 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
442 {
443 u = i;
444 break;
445 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000446 return u;
447}
448
449/*
450 * Print the page header.
451 */
Bram Moolenaar81366db2005-07-24 21:16:51 +0000452 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100453prt_header(
454 prt_settings_T *psettings,
455 int pagenum,
456 linenr_T lnum UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000457{
458 int width = psettings->chars_per_line;
459 int page_line;
460 char_u *tbuf;
461 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000462 int l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000463
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100464 // Also use the space for the line number.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000465 if (prt_use_number())
466 width += PRINT_NUMBER_WIDTH;
467
468 tbuf = alloc(width + IOSIZE);
469 if (tbuf == NULL)
470 return;
471
472#ifdef FEAT_STL_OPT
473 if (*p_header != NUL)
474 {
475 linenr_T tmp_lnum, tmp_topline, tmp_botline;
476
477 /*
478 * Need to (temporarily) set current line number and first/last line
479 * number on the 'window'. Since we don't know how long the page is,
480 * set the first and current line number to the top line, and guess
481 * that the page length is 64.
482 */
483 tmp_lnum = curwin->w_cursor.lnum;
484 tmp_topline = curwin->w_topline;
485 tmp_botline = curwin->w_botline;
486 curwin->w_cursor.lnum = lnum;
487 curwin->w_topline = lnum;
488 curwin->w_botline = lnum + 63;
489 printer_page_num = pagenum;
490
Luuk van Baal7b224fd2022-11-07 12:16:51 +0000491 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE), p_header,
492 (char_u *)"printheader", 0, ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000493
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100494 // Reset line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000495 curwin->w_cursor.lnum = tmp_lnum;
496 curwin->w_topline = tmp_topline;
497 curwin->w_botline = tmp_botline;
498 }
499 else
500#endif
501 sprintf((char *)tbuf, _("Page %d"), pagenum);
502
503 prt_set_fg(PRCOLOR_BLACK);
504 prt_set_bg(PRCOLOR_WHITE);
505 prt_set_font(TRUE, FALSE, FALSE);
506
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100507 // Use a negative line number to indicate printing in the top margin.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000508 page_line = 0 - prt_header_height();
509 mch_print_start_line(TRUE, page_line);
510 for (p = tbuf; *p != NUL; )
511 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100512 if (mch_print_text_out(p, (l = (*mb_ptr2len)(p))))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000513 {
514 ++page_line;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100515 if (page_line >= 0) // out of room in header
Bram Moolenaar81366db2005-07-24 21:16:51 +0000516 break;
517 mch_print_start_line(TRUE, page_line);
518 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000519 p += l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000520 }
521
522 vim_free(tbuf);
523
524#ifdef FEAT_SYN_HL
525 if (psettings->do_syntax)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100526 // Set colors for next character.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000527 current_syn_id = -1;
528 else
529#endif
530 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100531 // Set colors and font back to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000532 prt_set_fg(PRCOLOR_BLACK);
533 prt_set_bg(PRCOLOR_WHITE);
534 prt_set_font(FALSE, FALSE, FALSE);
535 }
536}
537
538/*
539 * Display a print status message.
540 */
541 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100542prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000543{
544 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100545 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000546 out_flush();
547}
548
549 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100550ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000551{
552 linenr_T lnum;
553 int collated_copies, uncollated_copies;
554 prt_settings_T settings;
555 long_u bytes_to_print = 0;
556 int page_line;
557 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000558
Bram Moolenaara80faa82020-04-12 19:37:17 +0200559 CLEAR_FIELD(settings);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000560 settings.has_color = TRUE;
561
562# ifdef FEAT_POSTSCRIPT
563 if (*eap->arg == '>')
564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100565 char *errormsg = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000566
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100567 // Expand things like "%.ps".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000568 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
569 {
570 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100571 emsg(errormsg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000572 return;
573 }
574 settings.outfile = skipwhite(eap->arg + 1);
575 }
576 else if (*eap->arg != NUL)
577 settings.arguments = eap->arg;
578# endif
579
580 /*
581 * Initialise for printing. Ask the user for settings, unless forceit is
582 * set.
583 * The mch_print_init() code should set up margins if applicable. (It may
584 * not be a real printer - for example the engine might generate HTML or
585 * PS.)
586 */
587 if (mch_print_init(&settings,
588 curbuf->b_fname == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100589 ? buf_spname(curbuf)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000590 : curbuf->b_sfname == NULL
591 ? curbuf->b_fname
592 : curbuf->b_sfname,
593 eap->forceit) == FAIL)
594 return;
595
596#ifdef FEAT_SYN_HL
597# ifdef FEAT_GUI
598 if (gui.in_use)
599 settings.modec = 'g';
600 else
601# endif
602 if (t_colors > 1)
603 settings.modec = 'c';
604 else
605 settings.modec = 't';
606
Bram Moolenaar860cae12010-06-05 23:22:07 +0200607 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000608 settings.do_syntax = FALSE;
609 else if (printer_opts[OPT_PRINT_SYNTAX].present
610 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
611 settings.do_syntax =
612 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
613 else
614 settings.do_syntax = settings.has_color;
615#endif
616
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100617 // Set up printing attributes for line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000618 settings.number.fg_color = PRCOLOR_BLACK;
619 settings.number.bg_color = PRCOLOR_WHITE;
620 settings.number.bold = FALSE;
621 settings.number.italic = TRUE;
622 settings.number.underline = FALSE;
623#ifdef FEAT_SYN_HL
624 /*
625 * Syntax highlighting of line numbers.
626 */
627 if (prt_use_number() && settings.do_syntax)
628 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000629 int id;
630
Bram Moolenaar81366db2005-07-24 21:16:51 +0000631 id = syn_name2id((char_u *)"LineNr");
632 if (id > 0)
633 id = syn_get_final_id(id);
634
635 prt_get_attr(id, &settings.number, settings.modec);
636 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000637#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000638
639 /*
640 * Estimate the total lines to be printed
641 */
642 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
643 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
644 if (bytes_to_print == 0)
645 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100646 msg(_("No text to be printed"));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000647 goto print_fail_no_begin;
648 }
649
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100650 // Set colors and font to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000651 curr_bg = (long_u)0xffffffffL;
652 curr_fg = (long_u)0xffffffffL;
653 curr_italic = MAYBE;
654 curr_bold = MAYBE;
655 curr_underline = MAYBE;
656
657 prt_set_fg(PRCOLOR_BLACK);
658 prt_set_bg(PRCOLOR_WHITE);
659 prt_set_font(FALSE, FALSE, FALSE);
660#ifdef FEAT_SYN_HL
661 current_syn_id = -1;
662#endif
663
664 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
665 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
666
667 if (!mch_print_begin(&settings))
668 goto print_fail_no_begin;
669
670 /*
671 * Loop over collated copies: 1 2 3, 1 2 3, ...
672 */
673 page_count = 0;
674 for (collated_copies = 0;
675 collated_copies < settings.n_collated_copies;
676 collated_copies++)
677 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100678 prt_pos_T prtpos; // current print position
679 prt_pos_T page_prtpos; // print position at page start
Bram Moolenaar81366db2005-07-24 21:16:51 +0000680 int side;
681
Bram Moolenaara80faa82020-04-12 19:37:17 +0200682 CLEAR_FIELD(page_prtpos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000683 page_prtpos.file_line = eap->line1;
684 prtpos = page_prtpos;
685
686 if (jobsplit && collated_copies > 0)
687 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100688 // Splitting jobs: Stop a previous job and start a new one.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000689 mch_print_end(&settings);
690 if (!mch_print_begin(&settings))
691 goto print_fail_no_begin;
692 }
693
694 /*
695 * Loop over all pages in the print job: 1 2 3 ...
696 */
697 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
698 {
699 /*
700 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
701 * For duplex: 12 12 12 34 34 34, ...
702 */
703 for (uncollated_copies = 0;
704 uncollated_copies < settings.n_uncollated_copies;
705 uncollated_copies++)
706 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100707 // Set the print position to the start of this page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000708 prtpos = page_prtpos;
709
710 /*
711 * Do front and rear side of a page.
712 */
713 for (side = 0; side <= settings.duplex; ++side)
714 {
715 /*
716 * Print one page.
717 */
718
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100719 // Check for interrupt character every page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000720 ui_breakcheck();
721 if (got_int || settings.user_abort)
722 goto print_fail;
723
724 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
725 page_count + 1 + side,
726 prtpos.bytes_printed > 1000000
727 ? (int)(prtpos.bytes_printed /
728 (bytes_to_print / 100))
729 : (int)((prtpos.bytes_printed * 100)
730 / bytes_to_print));
731 if (!mch_print_begin_page(IObuff))
732 goto print_fail;
733
734 if (settings.n_collated_copies > 1)
735 sprintf((char *)IObuff + STRLEN(IObuff),
736 _(" Copy %d of %d"),
737 collated_copies + 1,
738 settings.n_collated_copies);
739 prt_message(IObuff);
740
741 /*
742 * Output header if required
743 */
744 if (prt_header_height() > 0)
745 prt_header(&settings, page_count + 1 + side,
746 prtpos.file_line);
747
748 for (page_line = 0; page_line < settings.lines_per_page;
749 ++page_line)
750 {
751 prtpos.column = hardcopy_line(&settings,
752 page_line, &prtpos);
753 if (prtpos.column == 0)
754 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100755 // finished a file line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000756 prtpos.bytes_printed +=
757 STRLEN(skipwhite(ml_get(prtpos.file_line)));
758 if (++prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100759 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000760 }
761 else if (prtpos.ff)
762 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100763 // Line had a formfeed in it - start new page but
764 // stay on the current line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000765 break;
766 }
767 }
768
769 if (!mch_print_end_page())
770 goto print_fail;
771 if (prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100772 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000773 }
774
775 /*
776 * Extra blank page for duplexing with odd number of pages and
777 * more copies to come.
778 */
779 if (prtpos.file_line > eap->line2 && settings.duplex
780 && side == 0
781 && uncollated_copies + 1 < settings.n_uncollated_copies)
782 {
783 if (!mch_print_blank_page())
784 goto print_fail;
785 }
786 }
787 if (settings.duplex && prtpos.file_line <= eap->line2)
788 ++page_count;
789
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100790 // Remember the position where the next page starts.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000791 page_prtpos = prtpos;
792 }
793
794 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
795 settings.jobname);
796 prt_message(IObuff);
797 }
798
799print_fail:
800 if (got_int || settings.user_abort)
801 {
802 sprintf((char *)IObuff, "%s", _("Printing aborted"));
803 prt_message(IObuff);
804 }
805 mch_print_end(&settings);
806
807print_fail_no_begin:
808 mch_print_cleanup();
809}
810
811/*
812 * Print one page line.
813 * Return the next column to print, or zero if the line is finished.
814 */
815 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100816hardcopy_line(
817 prt_settings_T *psettings,
818 int page_line,
819 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000820{
821 colnr_T col;
822 char_u *line;
823 int need_break = FALSE;
824 int outputlen;
825 int tab_spaces;
826 long_u print_pos;
827#ifdef FEAT_SYN_HL
828 prt_text_attr_T attr;
829 int id;
830#endif
831
832 if (ppos->column == 0 || ppos->ff)
833 {
834 print_pos = 0;
835 tab_spaces = 0;
836 if (!ppos->ff && prt_use_number())
837 prt_line_number(psettings, page_line, ppos->file_line);
838 ppos->ff = FALSE;
839 }
840 else
841 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100842 // left over from wrap halfway a tab
Bram Moolenaar81366db2005-07-24 21:16:51 +0000843 print_pos = ppos->print_pos;
844 tab_spaces = ppos->lead_spaces;
845 }
846
847 mch_print_start_line(0, page_line);
848 line = ml_get(ppos->file_line);
849
850 /*
851 * Loop over the columns until the end of the file line or right margin.
852 */
853 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
854 {
855 outputlen = 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000856 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000857 outputlen = 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000858#ifdef FEAT_SYN_HL
859 /*
860 * syntax highlighting stuff.
861 */
862 if (psettings->do_syntax)
863 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000864 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000865 if (id > 0)
866 id = syn_get_final_id(id);
867 else
868 id = 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100869 // Get the line again, a multi-line regexp may invalidate it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000870 line = ml_get(ppos->file_line);
871
872 if (id != current_syn_id)
873 {
874 current_syn_id = id;
875 prt_get_attr(id, &attr, psettings->modec);
876 prt_set_font(attr.bold, attr.italic, attr.underline);
877 prt_set_fg(attr.fg_color);
878 prt_set_bg(attr.bg_color);
879 }
880 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000881#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000882
883 /*
884 * Appropriately expand any tabs to spaces.
885 */
886 if (line[col] == TAB || tab_spaces != 0)
887 {
888 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200889#ifdef FEAT_VARTABS
890 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
891 curbuf->b_p_vts_array);
892#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000893 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200894#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000895
896 while (tab_spaces > 0)
897 {
898 need_break = mch_print_text_out((char_u *)" ", 1);
899 print_pos++;
900 tab_spaces--;
901 if (need_break)
902 break;
903 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100904 // Keep the TAB if we didn't finish it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000905 if (need_break && tab_spaces > 0)
906 break;
907 }
908 else if (line[col] == FF
909 && printer_opts[OPT_PRINT_FORMFEED].present
910 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
911 == 'y')
912 {
913 ppos->ff = TRUE;
914 need_break = 1;
915 }
916 else
917 {
918 need_break = mch_print_text_out(line + col, outputlen);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000919 if (has_mbyte)
920 print_pos += (*mb_ptr2cells)(line + col);
921 else
Bram Moolenaar81366db2005-07-24 21:16:51 +0000922 print_pos++;
923 }
924 }
925
926 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000927 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000928
929 /*
930 * Start next line of file if we clip lines, or have reached end of the
931 * line, unless we are doing a formfeed.
932 */
933 if (!ppos->ff
934 && (line[col] == NUL
935 || (printer_opts[OPT_PRINT_WRAP].present
936 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
937 == 'n')))
938 return 0;
939 return col;
940}
941
942# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
943
944/*
945 * PS printer stuff.
946 *
947 * Sources of information to help maintain the PS printing code:
948 *
949 * 1. PostScript Language Reference, 3rd Edition,
950 * Addison-Wesley, 1999, ISBN 0-201-37922-8
951 * 2. PostScript Language Program Design,
952 * Addison-Wesley, 1988, ISBN 0-201-14396-8
953 * 3. PostScript Tutorial and Cookbook,
954 * Addison Wesley, 1985, ISBN 0-201-10179-3
955 * 4. PostScript Language Document Structuring Conventions Specification,
956 * version 3.0,
957 * Adobe Technote 5001, 25th September 1992
958 * 5. PostScript Printer Description File Format Specification, Version 4.3,
959 * Adobe technote 5003, 9th February 1996
960 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
961 * Adobe Technote 5007, 7th October 1998
962 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
963 * Adobe Technote 5014, 8th October 1996
964 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
965 * Adoboe Technote 5094, 8th September, 2001
966 * 9. CJKV Information Processing, 2nd Edition,
967 * O'Reilly, 2002, ISBN 1-56592-224-7
968 *
969 * Some of these documents can be found in PDF form on Adobe's web site -
970 * http://www.adobe.com
971 */
972
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100973#define PRT_PS_DEFAULT_DPI (72) // Default user space resolution
Bram Moolenaar81366db2005-07-24 21:16:51 +0000974#define PRT_PS_DEFAULT_FONTSIZE (10)
975#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
976
977struct prt_mediasize_S
978{
979 char *name;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100980 float width; // width and height in points for portrait
Bram Moolenaar81366db2005-07-24 21:16:51 +0000981 float height;
982};
983
K.Takataeeec2542021-06-02 13:28:16 +0200984#define PRT_MEDIASIZE_LEN ARRAY_LENGTH(prt_mediasize)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000985
986static struct prt_mediasize_S prt_mediasize[] =
987{
988 {"A4", 595.0, 842.0},
989 {"letter", 612.0, 792.0},
990 {"10x14", 720.0, 1008.0},
991 {"A3", 842.0, 1191.0},
992 {"A5", 420.0, 595.0},
993 {"B4", 729.0, 1032.0},
994 {"B5", 516.0, 729.0},
995 {"executive", 522.0, 756.0},
996 {"folio", 595.0, 935.0},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100997 {"ledger", 1224.0, 792.0}, // Yes, it is wider than taller!
Bram Moolenaar81366db2005-07-24 21:16:51 +0000998 {"legal", 612.0, 1008.0},
999 {"quarto", 610.0, 780.0},
1000 {"statement", 396.0, 612.0},
1001 {"tabloid", 792.0, 1224.0}
1002};
1003
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001004// PS font names, must be in Roman, Bold, Italic, Bold-Italic order
Bram Moolenaar81366db2005-07-24 21:16:51 +00001005struct prt_ps_font_S
1006{
1007 int wx;
1008 int uline_offset;
1009 int uline_width;
1010 int bbox_min_y;
1011 int bbox_max_y;
1012 char *(ps_fontname[4]);
1013};
1014
1015#define PRT_PS_FONT_ROMAN (0)
1016#define PRT_PS_FONT_BOLD (1)
1017#define PRT_PS_FONT_OBLIQUE (2)
1018#define PRT_PS_FONT_BOLDOBLIQUE (3)
1019
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001020// Standard font metrics for Courier family
Bram Moolenaar81366db2005-07-24 21:16:51 +00001021static struct prt_ps_font_S prt_ps_courier_font =
1022{
1023 600,
1024 -100, 50,
1025 -250, 805,
1026 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1027};
1028
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001029// Generic font metrics for multi-byte fonts
Bram Moolenaar81366db2005-07-24 21:16:51 +00001030static struct prt_ps_font_S prt_ps_mb_font =
1031{
1032 1000,
1033 -100, 50,
1034 -250, 805,
1035 {NULL, NULL, NULL, NULL}
1036};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001037
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001038// Pointer to current font set being used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001039static struct prt_ps_font_S* prt_ps_font;
1040
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001041// Structures to map user named encoding and mapping to PS equivalents for
1042// building CID font name
Bram Moolenaar81366db2005-07-24 21:16:51 +00001043struct prt_ps_encoding_S
1044{
1045 char *encoding;
1046 char *cmap_encoding;
1047 int needs_charset;
1048};
1049
1050struct prt_ps_charset_S
1051{
1052 char *charset;
1053 char *cmap_charset;
1054 int has_charset;
1055};
1056
Bram Moolenaar81366db2005-07-24 21:16:51 +00001057
1058#define CS_JIS_C_1978 (0x01)
1059#define CS_JIS_X_1983 (0x02)
1060#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001061#define CS_NEC (0x08)
1062#define CS_MSWINDOWS (0x10)
1063#define CS_CP932 (0x20)
1064#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001065#define CS_KANJITALK7 (0x80)
1066
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001067// Japanese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001068static struct prt_ps_encoding_S j_encodings[] =
1069{
1070 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001071 CS_NEC)},
1072 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1073 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1074 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001075 {"cp932", "RKSJ", CS_JIS_X_1983},
1076 {"ucs-2", "UCS2", CS_JIS_X_1990},
1077 {"utf-8", "UTF8" , CS_JIS_X_1990}
1078};
1079static struct prt_ps_charset_S j_charsets[] =
1080{
1081 {"JIS_C_1978", "78", CS_JIS_C_1978},
1082 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1083 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001084 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001085 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1086 {"CP932", "90ms", CS_JIS_X_1983},
1087 {"KANJITALK6", "83pv", CS_KANJITALK6},
1088 {"KANJITALK7", "90pv", CS_KANJITALK7}
1089};
1090
1091#define CS_GB_2312_80 (0x01)
1092#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001093#define CS_GBK2K (0x04)
1094#define CS_SC_MAC (0x08)
1095#define CS_GBT_90_MAC (0x10)
1096#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001097#define CS_SC_ISO10646 (0x40)
1098
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001099// Simplified Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001100static struct prt_ps_encoding_S sc_encodings[] =
1101{
1102 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1103 {"gb18030", NULL, CS_GBK2K},
1104 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001105 CS_GBT_90_MAC)},
1106 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001107 {"ucs-2", "UCS2", CS_SC_ISO10646},
1108 {"utf-8", "UTF8", CS_SC_ISO10646}
1109};
1110static struct prt_ps_charset_S sc_charsets[] =
1111{
1112 {"GB_2312-80", "GB", CS_GB_2312_80},
1113 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001114 {"MAC", "GBpc", CS_SC_MAC},
1115 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1116 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001117 {"GB18030", "GBK2K", CS_GBK2K},
1118 {"ISO10646", "UniGB", CS_SC_ISO10646}
1119};
1120
1121#define CS_CNS_PLANE_1 (0x01)
1122#define CS_CNS_PLANE_2 (0x02)
1123#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001124#define CS_B5 (0x08)
1125#define CS_ETEN (0x10)
1126#define CS_HK_GCCS (0x20)
1127#define CS_HK_SCS (0x40)
1128#define CS_HK_SCS_ETEN (0x80)
1129#define CS_MTHKL (0x100)
1130#define CS_MTHKS (0x200)
1131#define CS_DLHKL (0x400)
1132#define CS_DLHKS (0x800)
1133#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001134
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135// Traditional Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001136static struct prt_ps_encoding_S tc_encodings[] =
1137{
1138 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1139 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001140 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1141 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1142 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001143 {"cp950", "B5", CS_B5},
1144 {"ucs-2", "UCS2", CS_TC_ISO10646},
1145 {"utf-8", "UTF8", CS_TC_ISO10646},
1146 {"utf-16", "UTF16", CS_TC_ISO10646},
1147 {"utf-32", "UTF32", CS_TC_ISO10646}
1148};
1149static struct prt_ps_charset_S tc_charsets[] =
1150{
1151 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1152 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1153 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001154 {"BIG5", NULL, CS_B5},
1155 {"CP950", NULL, CS_B5},
1156 {"ETEN", "ETen", CS_ETEN},
1157 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1158 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001159 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1160 {"MTHKL", "HKm471", CS_MTHKL},
1161 {"MTHKS", "HKm314", CS_MTHKS},
1162 {"DLHKL", "HKdla", CS_DLHKL},
1163 {"DLHKS", "HKdlb", CS_DLHKS},
1164 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1165};
1166
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001167#define CS_KR_X_1992 (0x01)
1168#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001169#define CS_KR_X_1992_MS (0x04)
1170#define CS_KR_ISO10646 (0x08)
1171
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001172// Korean encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001173static struct prt_ps_encoding_S k_encodings[] =
1174{
1175 {"iso-2022-kr", NULL, CS_KR_X_1992},
1176 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1177 {"johab", "Johab", CS_KR_X_1992},
1178 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001179 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001180 {"cp949", "UHC", CS_KR_X_1992_MS},
1181 {"ucs-2", "UCS2", CS_KR_ISO10646},
1182 {"utf-8", "UTF8", CS_KR_ISO10646}
1183};
1184static struct prt_ps_charset_S k_charsets[] =
1185{
1186 {"KS_X_1992", "KSC", CS_KR_X_1992},
1187 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001188 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001189 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1190 {"CP949", "KSCms", CS_KR_X_1992_MS},
1191 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1192 {"ISO10646", "UniKS", CS_KR_ISO10646}
1193};
1194
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001195// Collections of encodings and charsets for multi-byte printing
Bram Moolenaar81366db2005-07-24 21:16:51 +00001196struct prt_ps_mbfont_S
1197{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001198 int num_encodings;
1199 struct prt_ps_encoding_S *encodings;
1200 int num_charsets;
1201 struct prt_ps_charset_S *charsets;
1202 char *ascii_enc;
1203 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001204};
1205
1206static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1207{
1208 {
K.Takataeeec2542021-06-02 13:28:16 +02001209 ARRAY_LENGTH(j_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001210 j_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001211 ARRAY_LENGTH(j_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001212 j_charsets,
1213 "jis_roman",
1214 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001215 },
1216 {
K.Takataeeec2542021-06-02 13:28:16 +02001217 ARRAY_LENGTH(sc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001218 sc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001219 ARRAY_LENGTH(sc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001220 sc_charsets,
1221 "gb_roman",
1222 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001223 },
1224 {
K.Takataeeec2542021-06-02 13:28:16 +02001225 ARRAY_LENGTH(tc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001226 tc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001227 ARRAY_LENGTH(tc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001228 tc_charsets,
1229 "cns_roman",
1230 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001231 },
1232 {
K.Takataeeec2542021-06-02 13:28:16 +02001233 ARRAY_LENGTH(k_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001234 k_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001235 ARRAY_LENGTH(k_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001236 k_charsets,
1237 "ks_roman",
1238 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001239 }
1240};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001241
1242struct prt_ps_resource_S
1243{
1244 char_u name[64];
1245 char_u filename[MAXPATHL + 1];
1246 int type;
1247 char_u title[256];
1248 char_u version[256];
1249};
1250
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001251// Types of PS resource file currently used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001252#define PRT_RESOURCE_TYPE_PROCSET (0)
1253#define PRT_RESOURCE_TYPE_ENCODING (1)
1254#define PRT_RESOURCE_TYPE_CMAP (2)
1255
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001256// The PS prolog file version number has to match - if the prolog file is
1257// updated, increment the number in the file and here. Version checking was
1258// added as of VIM 6.2.
1259// The CID prolog file version number behaves as per PS prolog.
1260// Table of VIM and prolog versions:
1261//
1262// VIM Prolog CIDProlog
1263// 6.2 1.3
1264// 7.0 1.4 1.0
Bram Moolenaar81366db2005-07-24 21:16:51 +00001265#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1266#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1267
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001268// String versions of PS resource types - indexed by constants above so don't
1269// re-order!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001270static char *prt_resource_types[] =
1271{
1272 "procset",
1273 "encoding",
1274 "cmap"
1275};
1276
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001277// Strings to look for in a PS resource file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001278#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1279#define PRT_RESOURCE_RESOURCE "Resource-"
1280#define PRT_RESOURCE_PROCSET "ProcSet"
1281#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001282#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001283
1284
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001285// Data for table based DSC comment recognition, easy to extend if VIM needs to
1286// read more comments.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001287#define PRT_DSC_MISC_TYPE (-1)
1288#define PRT_DSC_TITLE_TYPE (1)
1289#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001290#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1291
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001292#define PRT_DSC_TITLE "%%Title:"
1293#define PRT_DSC_VERSION "%%Version:"
1294#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001295
1296struct prt_dsc_comment_S
1297{
1298 char *string;
1299 int len;
1300 int type;
1301};
1302
1303struct prt_dsc_line_S
1304{
1305 int type;
1306 char_u *string;
1307 int len;
1308};
1309
1310
1311#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1312static struct prt_dsc_comment_S prt_dsc_table[] =
1313{
1314 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1315 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1316 PRT_DSC_VERSION_TYPE},
1317 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1318 PRT_DSC_ENDCOMMENTS_TYPE}
1319};
1320
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001321static void prt_write_file_len(char_u *buffer, int bytes);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001322static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001323
1324/*
1325 * Variables for the output PostScript file.
1326 */
1327static FILE *prt_ps_fd;
1328static int prt_file_error;
1329static char_u *prt_ps_file_name = NULL;
1330
1331/*
1332 * Various offsets and dimensions in default PostScript user space (points).
1333 * Used for text positioning calculations
1334 */
1335static float prt_page_width;
1336static float prt_page_height;
1337static float prt_left_margin;
1338static float prt_right_margin;
1339static float prt_top_margin;
1340static float prt_bottom_margin;
1341static float prt_line_height;
1342static float prt_first_line_height;
1343static float prt_char_width;
1344static float prt_number_width;
1345static float prt_bgcol_offset;
1346static float prt_pos_x_moveto = 0.0;
1347static float prt_pos_y_moveto = 0.0;
1348
1349/*
1350 * Various control variables used to decide when and how to change the
1351 * PostScript graphics state.
1352 */
1353static int prt_need_moveto;
1354static int prt_do_moveto;
1355static int prt_need_font;
1356static int prt_font;
1357static int prt_need_underline;
1358static int prt_underline;
1359static int prt_do_underline;
1360static int prt_need_fgcol;
1361static int prt_fgcol;
1362static int prt_need_bgcol;
1363static int prt_do_bgcol;
1364static int prt_bgcol;
1365static int prt_new_bgcol;
1366static int prt_attribute_change;
1367static float prt_text_run;
1368static int prt_page_num;
1369static int prt_bufsiz;
1370
1371/*
1372 * Variables controlling physical printing.
1373 */
1374static int prt_media;
1375static int prt_portrait;
1376static int prt_num_copies;
1377static int prt_duplex;
1378static int prt_tumble;
1379static int prt_collate;
1380
1381/*
1382 * Buffers used when generating PostScript output
1383 */
1384static char_u prt_line_buffer[257];
1385static garray_T prt_ps_buffer;
1386
Bram Moolenaar81366db2005-07-24 21:16:51 +00001387static int prt_do_conv;
1388static vimconv_T prt_conv;
1389
1390static int prt_out_mbyte;
1391static int prt_custom_cmap;
1392static char prt_cmap[80];
1393static int prt_use_courier;
1394static int prt_in_ascii;
1395static int prt_half_width;
1396static char *prt_ascii_encoding;
1397static char_u prt_hexchar[] = "0123456789abcdef";
Bram Moolenaar81366db2005-07-24 21:16:51 +00001398
1399 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001400prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001401{
1402 if (!prt_file_error
1403 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1404 != (size_t)bytes)
1405 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001406 emsg(_(e_error_writing_to_postscript_output_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001407 prt_file_error = TRUE;
1408 }
1409}
1410
1411 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001412prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001413{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001414 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001415}
1416
1417 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001418prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001419{
Bram Moolenaar81366db2005-07-24 21:16:51 +00001420 prt_write_file_raw_len(buffer, bytes);
1421}
1422
1423/*
1424 * Write a string.
1425 */
1426 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001427prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001428{
1429 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1430 prt_write_file(prt_line_buffer);
1431}
1432
1433/*
1434 * Write an int and a space.
1435 */
1436 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001437prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001438{
1439 sprintf((char *)prt_line_buffer, "%d ", i);
1440 prt_write_file(prt_line_buffer);
1441}
1442
1443/*
1444 * Write a boolean and a space.
1445 */
1446 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001447prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001448{
1449 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1450 prt_write_file(prt_line_buffer);
1451}
1452
1453/*
1454 * Write PostScript to re-encode and define the font.
1455 */
1456 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001457prt_def_font(
1458 char *new_name,
1459 char *encoding,
1460 int height,
1461 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001462{
1463 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1464 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1465 prt_write_file(prt_line_buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001466 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001467 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001468 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1469 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001470 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1471 "/%s %d /_%s ffs\n", new_name, height, new_name);
1472 prt_write_file(prt_line_buffer);
1473}
1474
Bram Moolenaar81366db2005-07-24 21:16:51 +00001475/*
1476 * Write a line to define the CID font.
1477 */
1478 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001479prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001480{
1481 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1482 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1483 prt_write_file(prt_line_buffer);
1484 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1485 "/%s %d /_%s ffs\n", new_name, height, new_name);
1486 prt_write_file(prt_line_buffer);
1487}
1488
1489/*
1490 * Write a line to define a duplicate of a CID font
1491 */
1492 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001493prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001494{
1495 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1496 "/%s %s d\n", new_name, original_name);
1497 prt_write_file(prt_line_buffer);
1498}
Bram Moolenaar81366db2005-07-24 21:16:51 +00001499
1500/*
1501 * Convert a real value into an integer and fractional part as integers, with
1502 * the fractional part being in the range [0,10^precision). The fractional part
1503 * is also rounded based on the precision + 1'th fractional digit.
1504 */
1505 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001506prt_real_bits(
1507 double real,
1508 int precision,
1509 int *pinteger,
1510 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001511{
1512 int i;
1513 int integer;
1514 float fraction;
1515
1516 integer = (int)real;
1517 fraction = (float)(real - integer);
1518 if (real < (double)integer)
1519 fraction = -fraction;
1520 for (i = 0; i < precision; i++)
1521 fraction *= 10.0;
1522
1523 *pinteger = integer;
1524 *pfraction = (int)(fraction + 0.5);
1525}
1526
1527/*
1528 * Write a real and a space. Save bytes if real value has no fractional part!
1529 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1530 * what decimal point character to use, but PS always requires a '.'.
1531 */
1532 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001533prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001534{
1535 int integer;
1536 int fraction;
1537
1538 prt_real_bits(val, prec, &integer, &fraction);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001539 // Emit integer part
Bram Moolenaar81366db2005-07-24 21:16:51 +00001540 sprintf((char *)prt_line_buffer, "%d", integer);
1541 prt_write_file(prt_line_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 // Only emit fraction if necessary
Bram Moolenaar81366db2005-07-24 21:16:51 +00001543 if (fraction != 0)
1544 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001545 // Remove any trailing zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001546 while ((fraction % 10) == 0)
1547 {
1548 prec--;
1549 fraction /= 10;
1550 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001551 // Emit fraction left padded with zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001552 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1553 prt_write_file(prt_line_buffer);
1554 }
1555 sprintf((char *)prt_line_buffer, " ");
1556 prt_write_file(prt_line_buffer);
1557}
1558
1559/*
1560 * Write a line to define a numeric variable.
1561 */
1562 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001563prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001564{
1565 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1566 "/%s ", name);
1567 prt_write_file(prt_line_buffer);
1568 prt_write_real(value, prec);
1569 sprintf((char *)prt_line_buffer, "d\n");
1570 prt_write_file(prt_line_buffer);
1571}
1572
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001573// Convert size from font space to user space at current font scale
Bram Moolenaar81366db2005-07-24 21:16:51 +00001574#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1575
1576 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001577prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001578{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001579 if (prt_ps_buffer.ga_len <= 0)
1580 return;
1581
1582 // Any background color must be drawn first
1583 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
Bram Moolenaar81366db2005-07-24 21:16:51 +00001584 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001585 int r, g, b;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001586
Bram Moolenaar81366db2005-07-24 21:16:51 +00001587 if (prt_do_moveto)
1588 {
1589 prt_write_real(prt_pos_x_moveto, 2);
1590 prt_write_real(prt_pos_y_moveto, 2);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001591 prt_write_string("m\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001592 prt_do_moveto = FALSE;
1593 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001594
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001595 // Size of rect of background color on which text is printed
1596 prt_write_real(prt_text_run, 2);
1597 prt_write_real(prt_line_height, 2);
1598
1599 // Lastly add the color of the background
1600 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1601 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1602 b = prt_new_bgcol & 0xff;
1603 prt_write_real(r / 255.0, 3);
1604 prt_write_real(g / 255.0, 3);
1605 prt_write_real(b / 255.0, 3);
1606 prt_write_string("bg\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001607 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001608 // Draw underlines before the text as it makes it slightly easier to
1609 // find the starting point.
1610 if (prt_do_underline)
1611 {
1612 if (prt_do_moveto)
1613 {
1614 prt_write_real(prt_pos_x_moveto, 2);
1615 prt_write_real(prt_pos_y_moveto, 2);
1616 prt_write_string("m\n");
1617 prt_do_moveto = FALSE;
1618 }
1619
1620 // Underline length of text run
1621 prt_write_real(prt_text_run, 2);
1622 prt_write_string("ul\n");
1623 }
1624 // Draw the text
1625 if (prt_out_mbyte)
1626 prt_write_string("<");
1627 else
1628 prt_write_string("(");
1629 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1630 if (prt_out_mbyte)
1631 prt_write_string(">");
1632 else
1633 prt_write_string(")");
1634 // Add a moveto if need be and use the appropriate show procedure
1635 if (prt_do_moveto)
1636 {
1637 prt_write_real(prt_pos_x_moveto, 2);
1638 prt_write_real(prt_pos_y_moveto, 2);
1639 // moveto and a show
1640 prt_write_string("ms\n");
1641 prt_do_moveto = FALSE;
1642 }
1643 else // Simple show
1644 prt_write_string("s\n");
1645
1646 ga_clear(&prt_ps_buffer);
1647 ga_init2(&prt_ps_buffer, sizeof(char), prt_bufsiz);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001648}
1649
1650
1651 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001652prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001653{
1654 char_u *resource_filename = cookie;
1655
1656 if (STRLEN(filename) >= MAXPATHL)
1657 *resource_filename = NUL;
1658 else
1659 STRCPY(resource_filename, filename);
1660}
1661
1662 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001663prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001664{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001665 char_u *buffer;
1666 int retval;
1667
1668 buffer = alloc(MAXPATHL + 1);
1669 if (buffer == NULL)
1670 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001671
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001672 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001673 // Look for named resource file in runtimepath
Bram Moolenaar81366db2005-07-24 21:16:51 +00001674 STRCPY(buffer, "print");
1675 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001676 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1677 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001678 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001679 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001680 resource->filename)
1681 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001682 vim_free(buffer);
1683 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001684}
1685
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001686// PS CR and LF characters have platform independent values
Bram Moolenaar81366db2005-07-24 21:16:51 +00001687#define PSLF (0x0a)
1688#define PSCR (0x0d)
1689
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001690// Static buffer to read initial comments in a resource file, some can have a
1691// couple of KB of comments!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001692#define PRT_FILE_BUFFER_LEN (2048)
1693struct prt_resfile_buffer_S
1694{
1695 char_u buffer[PRT_FILE_BUFFER_LEN];
1696 int len;
1697 int line_start;
1698 int line_end;
1699};
1700
1701static struct prt_resfile_buffer_S prt_resfile;
1702
1703 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001704prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001705{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001706 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001707
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001708 // Move to start of next line and then find end of line
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 idx = prt_resfile.line_end + 1;
1710 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001711 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001713 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001714 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001715 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001717
Bram Moolenaar89d40322006-08-29 15:30:07 +00001718 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001719 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001720 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001721 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001722 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001723 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001724 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001725
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001727}
1728
1729 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001730prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001731{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001732 // Force not equal if string is longer than remainder of line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001733 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001734 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001735
1736 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001737 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001738}
1739
1740 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001741prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001742{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001743 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001744
Bram Moolenaar89d40322006-08-29 15:30:07 +00001745 idx = prt_resfile.line_start + offset;
1746 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001747 {
Keith Thompson184f71c2024-01-04 21:19:04 +01001748 if (SAFE_isspace(prt_resfile.buffer[idx]))
Bram Moolenaar89d40322006-08-29 15:30:07 +00001749 return idx - prt_resfile.line_start;
1750 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001751 }
1752 return -1;
1753}
1754
1755 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001756prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001757{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001759
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 idx = prt_resfile.line_start + offset;
1761 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001762 {
Keith Thompson184f71c2024-01-04 21:19:04 +01001763 if (!SAFE_isspace(prt_resfile.buffer[idx]))
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 return idx - prt_resfile.line_start;
1765 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001766 }
1767 return -1;
1768}
1769
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001770// prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1771// if a DSC comment is found, else false
Bram Moolenaar81366db2005-07-24 21:16:51 +00001772 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001773prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001774{
1775 int comment;
1776 int offset;
1777
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001778 // Move to start of next line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001779 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001780 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001781
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001782 // DSC comments always start %%
Bram Moolenaar81366db2005-07-24 21:16:51 +00001783 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001784 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001785
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001786 // Find type of DSC comment
K.Takataeeec2542021-06-02 13:28:16 +02001787 for (comment = 0; comment < (int)ARRAY_LENGTH(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001788 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1789 prt_dsc_table[comment].len) == 0)
1790 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001791
K.Takataeeec2542021-06-02 13:28:16 +02001792 if (comment != ARRAY_LENGTH(prt_dsc_table))
Bram Moolenaar81366db2005-07-24 21:16:51 +00001793 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001794 // Return type of comment
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001795 p_dsc_line->type = prt_dsc_table[comment].type;
1796 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001797 }
1798 else
1799 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800 // Unrecognised DSC comment, skip to ws after comment leader
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001801 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1802 offset = prt_resfile_skip_nonws(0);
1803 if (offset == -1)
1804 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001805 }
1806
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001807 // Skip ws to comment value
Bram Moolenaar81366db2005-07-24 21:16:51 +00001808 offset = prt_resfile_skip_ws(offset);
1809 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001810 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001811
1812 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1813 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1814
1815 return TRUE;
1816}
1817
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001818/*
1819 * Improved hand crafted parser to get the type, title, and version number of a
Bram Moolenaar81366db2005-07-24 21:16:51 +00001820 * PS resource file so the file details can be added to the DSC header comments.
1821 */
1822 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001823prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001824{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001825 int offset;
1826 int seen_all;
1827 int seen_title;
1828 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001829 FILE *fd_resource;
1830 struct prt_dsc_line_S dsc_line;
1831
1832 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1833 if (fd_resource == NULL)
1834 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001835 semsg(_(e_cant_open_file_str_3), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001836 return FALSE;
1837 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001838 CLEAR_FIELD(prt_resfile.buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001839
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001840 // Parse first line to ensure valid resource file
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001841 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001842 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001843 if (ferror(fd_resource))
1844 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001845 semsg(_(e_cant_read_postscript_resource_file_str),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001846 resource->filename);
1847 fclose(fd_resource);
1848 return FALSE;
1849 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001850 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001851
1852 prt_resfile.line_end = -1;
1853 prt_resfile.line_start = 0;
1854 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001855 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001856
1857 offset = 0;
1858
1859 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001860 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001861 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001862 semsg(_(e_file_str_is_not_postscript_resource_file),
1863 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001864 return FALSE;
1865 }
1866
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001867 // Skip over any version numbers and following ws
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001868 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001869 offset = prt_resfile_skip_nonws(offset);
1870 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001871 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001872 offset = prt_resfile_skip_ws(offset);
1873 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001874 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001875
1876 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001877 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001878 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001879 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1880 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001881 return FALSE;
1882 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001883 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001884
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001885 // Decide type of resource in the file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001886 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001887 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001888 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1889 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001890 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001891 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1892 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001893 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001894 resource->type = PRT_RESOURCE_TYPE_CMAP;
1895 else
1896 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001897 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1898 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001899 return FALSE;
1900 }
1901
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001902 // Look for title and version of resource
Bram Moolenaar81366db2005-07-24 21:16:51 +00001903 resource->title[0] = '\0';
1904 resource->version[0] = '\0';
1905 seen_title = FALSE;
1906 seen_version = FALSE;
1907 seen_all = FALSE;
1908 while (!seen_all && prt_next_dsc(&dsc_line))
1909 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001910 switch (dsc_line.type)
1911 {
1912 case PRT_DSC_TITLE_TYPE:
1913 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
1914 seen_title = TRUE;
1915 if (seen_version)
1916 seen_all = TRUE;
1917 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001918
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001919 case PRT_DSC_VERSION_TYPE:
1920 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
1921 seen_version = TRUE;
1922 if (seen_title)
1923 seen_all = TRUE;
1924 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001925
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001926 case PRT_DSC_ENDCOMMENTS_TYPE:
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001927 // Won't find title or resource after this comment, stop searching
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001928 seen_all = TRUE;
1929 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001930
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001931 case PRT_DSC_MISC_TYPE:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001932 // Not interested in whatever comment this line had
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001933 break;
1934 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001935 }
1936
1937 if (!seen_title || !seen_version)
1938 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001939 semsg(_(e_file_str_is_not_supported_postscript_resource_file),
1940 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001941 return FALSE;
1942 }
1943
Bram Moolenaar81366db2005-07-24 21:16:51 +00001944 return TRUE;
1945}
1946
1947 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001948prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001949{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001950 // Version number m.n should match, the revision number does not matter
Bram Moolenaar81366db2005-07-24 21:16:51 +00001951 if (STRNCMP(resource->version, version, STRLEN(version)))
1952 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001953 semsg(_(e_str_resource_file_has_wrong_version),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001954 resource->name);
1955 return FALSE;
1956 }
1957
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001958 // Other checks to be added as needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00001959 return TRUE;
1960}
1961
1962 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001963prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001964{
1965 prt_write_string("%!PS-Adobe-3.0\n");
1966}
1967
1968 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001969prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001970{
1971 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1972 "%%%%%s\n", comment);
1973 prt_write_file(prt_line_buffer);
1974}
1975
1976 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001977prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001978{
1979 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1980 "%%%%%s: %s\n", comment, text);
1981 prt_write_file(prt_line_buffer);
1982}
1983
1984 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001985prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001986{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001987 // TODO - should scan 'text' for any chars needing escaping!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001988 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1989 "%%%%%s: (%s)\n", comment, text);
1990 prt_write_file(prt_line_buffer);
1991}
1992
1993#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
1994
1995 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001996prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001997{
1998 int i;
1999
2000 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2001 "%%%%%s:", comment);
2002 prt_write_file(prt_line_buffer);
2003
2004 for (i = 0; i < count; i++)
2005 {
2006 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2007 prt_write_file(prt_line_buffer);
2008 }
2009
2010 prt_write_string("\n");
2011}
2012
2013 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002014prt_dsc_resources(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002015 char *comment, // if NULL add to previous
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002016 char *type,
2017 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002018{
2019 if (comment != NULL)
2020 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2021 "%%%%%s: %s", comment, type);
2022 else
2023 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2024 "%%%%+ %s", type);
2025 prt_write_file(prt_line_buffer);
2026
2027 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2028 " %s\n", string);
2029 prt_write_file(prt_line_buffer);
2030}
2031
2032 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002033prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002034{
2035 int i;
2036
2037 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002038 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002039 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002040 if (ps_font->ps_fontname[i] != NULL)
2041 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002042}
2043
2044 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002045prt_dsc_requirements(
2046 int duplex,
2047 int tumble,
2048 int collate,
2049 int color,
2050 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002051{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002052 // Only output the comment if we need to.
2053 // Note: tumble is ignored if we are not duplexing
Bram Moolenaar81366db2005-07-24 21:16:51 +00002054 if (!(duplex || collate || color || (num_copies > 1)))
2055 return;
2056
2057 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2058 prt_write_file(prt_line_buffer);
2059
2060 if (duplex)
2061 {
2062 prt_write_string(" duplex");
2063 if (tumble)
2064 prt_write_string("(tumble)");
2065 }
2066 if (collate)
2067 prt_write_string(" collate");
2068 if (color)
2069 prt_write_string(" color");
2070 if (num_copies > 1)
2071 {
2072 prt_write_string(" numcopies(");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073 // Note: no space wanted so don't use prt_write_int()
Bram Moolenaar81366db2005-07-24 21:16:51 +00002074 sprintf((char *)prt_line_buffer, "%d", num_copies);
2075 prt_write_file(prt_line_buffer);
2076 prt_write_string(")");
2077 }
2078 prt_write_string("\n");
2079}
2080
2081 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002082prt_dsc_docmedia(
2083 char *paper_name,
2084 double width,
2085 double height,
2086 double weight,
2087 char *colour,
2088 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002089{
2090 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2091 "%%%%DocumentMedia: %s ", paper_name);
2092 prt_write_file(prt_line_buffer);
2093 prt_write_real(width, 2);
2094 prt_write_real(height, 2);
2095 prt_write_real(weight, 2);
2096 if (colour == NULL)
2097 prt_write_string("()");
2098 else
2099 prt_write_string(colour);
2100 prt_write_string(" ");
2101 if (type == NULL)
2102 prt_write_string("()");
2103 else
2104 prt_write_string(type);
2105 prt_write_string("\n");
2106}
2107
2108 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002109mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002110{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002111 if (prt_out_mbyte)
2112 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002113 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002114
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002115 // Free off all CID font names created, but first clear duplicate
2116 // pointers to the same string (when the same font is used for more than
2117 // one style).
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002118 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2119 {
2120 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002121 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002122 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002123 }
2124
2125 if (prt_do_conv)
2126 {
2127 convert_setup(&prt_conv, NULL, NULL);
2128 prt_do_conv = FALSE;
2129 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002130 if (prt_ps_fd != NULL)
2131 {
2132 fclose(prt_ps_fd);
2133 prt_ps_fd = NULL;
2134 prt_file_error = FALSE;
2135 }
2136 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002137 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002138}
2139
2140 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002141to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002142{
2143 float ret;
2144 int u;
2145 int nr;
2146
2147 u = prt_get_unit(idx);
2148 if (u == PRT_UNIT_NONE)
2149 {
2150 u = PRT_UNIT_PERC;
2151 nr = def_number;
2152 }
2153 else
2154 nr = printer_opts[idx].number;
2155
2156 switch (u)
2157 {
2158 case PRT_UNIT_INCH:
2159 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2160 break;
2161 case PRT_UNIT_MM:
2162 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2163 break;
2164 case PRT_UNIT_POINT:
2165 ret = (float)nr;
2166 break;
2167 case PRT_UNIT_PERC:
2168 default:
2169 ret = (float)(physsize * nr) / 100;
2170 break;
2171 }
2172
2173 return ret;
2174}
2175
2176/*
2177 * Calculate margins for given width and height from printoptions settings.
2178 */
2179 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002180prt_page_margins(
2181 double width,
2182 double height,
2183 double *left,
2184 double *right,
2185 double *top,
2186 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002187{
2188 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2189 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2190 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2191 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2192}
2193
2194 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002195prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002196{
2197 prt_line_height = (float)font_scale;
2198 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2199}
2200
2201
2202 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002203prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002204{
2205 if (prt_use_number())
2206 {
2207 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002208 // If we are outputting multi-byte characters then line numbers will be
2209 // printed with half width characters
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002210 if (prt_out_mbyte)
2211 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002212 prt_left_margin += prt_number_width;
2213 }
2214 else
2215 prt_number_width = 0.0;
2216
2217 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2218}
2219
Bram Moolenaar81366db2005-07-24 21:16:51 +00002220 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002221prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002222{
2223 char *fontname;
2224
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002225 fontname = alloc(name_len + 1);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002226 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002227 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002228 vim_strncpy((char_u *)fontname, name, name_len);
2229 prt_ps_mb_font.ps_fontname[font] = fontname;
2230
2231 return TRUE;
2232}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002233
2234/*
2235 * Get number of lines of text that fit on a page (excluding the header).
2236 */
2237 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002238prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002239{
2240 int lpp;
2241
2242 /*
2243 * Calculate offset to lower left corner of background rect based on actual
2244 * font height (based on its bounding box) and the line height, handling the
2245 * case where the font height can exceed the line height.
2246 */
2247 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2248 prt_ps_font->bbox_min_y);
2249 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2250 {
2251 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2252 (1000.0 - (prt_ps_font->bbox_max_y -
2253 prt_ps_font->bbox_min_y)) / 2);
2254 }
2255
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002256 // Get height for topmost line based on background rect offset.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002257 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2258
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002259 // Calculate lpp
Bram Moolenaar81366db2005-07-24 21:16:51 +00002260 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2261
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002262 // Adjust top margin if there is a header
Bram Moolenaar81366db2005-07-24 21:16:51 +00002263 prt_top_margin -= prt_line_height * prt_header_height();
2264
2265 return lpp - prt_header_height();
2266}
2267
Bram Moolenaar81366db2005-07-24 21:16:51 +00002268 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002269prt_match_encoding(
2270 char *p_encoding,
2271 struct prt_ps_mbfont_S *p_cmap,
2272 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002273{
2274 int mbenc;
2275 int enc_len;
2276 struct prt_ps_encoding_S *p_mbenc;
2277
2278 *pp_mbenc = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002279 // Look for recognised encoding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002280 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002281 p_mbenc = p_cmap->encodings;
2282 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2283 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002284 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2285 {
2286 *pp_mbenc = p_mbenc;
2287 return TRUE;
2288 }
2289 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002290 }
2291 return FALSE;
2292}
2293
2294 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002295prt_match_charset(
2296 char *p_charset,
2297 struct prt_ps_mbfont_S *p_cmap,
2298 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002299{
2300 int mbchar;
2301 int char_len;
2302 struct prt_ps_charset_S *p_mbchar;
2303
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002304 // Look for recognised character set, using default if one is not given
Bram Moolenaar81366db2005-07-24 21:16:51 +00002305 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002306 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002307 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002308 p_mbchar = p_cmap->charsets;
2309 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2310 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002311 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2312 {
2313 *pp_mbchar = p_mbchar;
2314 return TRUE;
2315 }
2316 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002317 }
2318 return FALSE;
2319}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002320
Bram Moolenaar81366db2005-07-24 21:16:51 +00002321 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002322mch_print_init(
2323 prt_settings_T *psettings,
2324 char_u *jobname,
2325 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002326{
2327 int i;
2328 char *paper_name;
2329 int paper_strlen;
2330 int fontsize;
2331 char_u *p;
2332 double left;
2333 double right;
2334 double top;
2335 double bottom;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002336 int props;
2337 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002338 char_u *p_encoding;
2339 struct prt_ps_encoding_S *p_mbenc;
2340 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002341 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002342
2343#if 0
2344 /*
2345 * TODO:
2346 * If "forceit" is false: pop up a dialog to select:
2347 * - printer name
2348 * - copies
2349 * - collated/uncollated
2350 * - duplex off/long side/short side
2351 * - paper size
2352 * - portrait/landscape
2353 * - font size
2354 *
2355 * If "forceit" is true: use the default printer and settings
2356 */
2357 if (forceit)
2358 s_pd.Flags |= PD_RETURNDEFAULT;
2359#endif
2360
2361 /*
2362 * Set up font and encoding.
2363 */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002364 p_encoding = enc_skip(p_penc);
2365 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002366 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002367
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002368 // Look for a multi-byte font that matches the encoding and character set.
2369 // Only look if multi-byte character set is defined, or using multi-byte
2370 // encoding other than Unicode. This is because a Unicode encoding does not
2371 // uniquely identify a CJK character set to use.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002372 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002373 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002374 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002375 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002376 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002377
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002378 p_mbenc_first = NULL;
K.Takataeeec2542021-06-02 13:28:16 +02002379 for (cmap = 0; cmap < (int)ARRAY_LENGTH(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002380 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002381 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002382 {
2383 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002384 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002385 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002386 cmap_first = cmap;
2387 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002388 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002389 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002390 break;
2391 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002392
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002393 // Use first encoding matched if no charset matched
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002394 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002395 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002396 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002397 cmap = cmap_first;
2398 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002399 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002400
2401 prt_out_mbyte = (p_mbenc != NULL);
2402 if (prt_out_mbyte)
2403 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002404 // Build CMap name - will be same for all multi-byte fonts used
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002405 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002406
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002407 prt_custom_cmap = (p_mbchar == NULL);
2408 if (!prt_custom_cmap)
2409 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002410 // Check encoding and character set are compatible
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002411 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2412 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002413 emsg(_(e_incompatible_multi_byte_encoding_and_character_set));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002414 return FALSE;
2415 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002416
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002417 // Add charset name if not empty
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002418 if (p_mbchar->cmap_charset != NULL)
2419 {
2420 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002421 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002422 STRCAT(prt_cmap, "-");
2423 }
2424 }
2425 else
2426 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002427 // Add custom CMap character set name
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002428 if (*p_pmcs == NUL)
2429 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002430 emsg(_(e_printmbcharset_cannot_be_empty_with_multi_byte_encoding));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002431 return FALSE;
2432 }
2433 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2434 STRCAT(prt_cmap, "-");
2435 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002436
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002437 // CMap name ends with (optional) encoding name and -H for horizontal
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002438 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002439 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002440 {
2441 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2442 STRCAT(prt_cmap, "-");
2443 }
2444 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002445
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002446 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2447 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002448 emsg(_(e_no_default_font_specified_for_multi_byte_printing));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002449 return FALSE;
2450 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002451
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002452 // Derive CID font names with fallbacks if not defined
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002453 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2454 mbfont_opts[OPT_MBFONT_REGULAR].string,
2455 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2456 return FALSE;
2457 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2458 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2459 mbfont_opts[OPT_MBFONT_BOLD].string,
2460 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2461 return FALSE;
2462 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2463 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2464 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2465 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2466 return FALSE;
2467 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2468 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002469 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2470 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002471 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002472
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002473 // Check if need to use Courier for ASCII code range, and if so pick up
2474 // the encoding to use
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002475 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2476 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2477 if (prt_use_courier)
2478 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002479 // Use national ASCII variant unless ASCII wanted
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002480 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2481 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2482 prt_ascii_encoding = "ascii";
2483 else
2484 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2485 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002486
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002487 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002488 }
2489 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00002490 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002491 prt_use_courier = FALSE;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002492 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002493 }
2494
2495 /*
2496 * Find the size of the paper and set the margins.
2497 */
2498 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2499 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2500 if (printer_opts[OPT_PRINT_PAPER].present)
2501 {
2502 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2503 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2504 }
2505 else
2506 {
2507 paper_name = "A4";
2508 paper_strlen = 2;
2509 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002510 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002511 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2512 && STRNICMP(prt_mediasize[i].name, paper_name,
2513 paper_strlen) == 0)
2514 break;
2515 if (i == PRT_MEDIASIZE_LEN)
2516 i = 0;
2517 prt_media = i;
2518
2519 /*
2520 * Set PS pagesize based on media dimensions and print orientation.
2521 * Note: Media and page sizes have defined meanings in PostScript and should
2522 * be kept distinct. Media is the paper (or transparency, or ...) that is
2523 * printed on, whereas the page size is the area that the PostScript
2524 * interpreter renders into.
2525 */
2526 if (prt_portrait)
2527 {
2528 prt_page_width = prt_mediasize[i].width;
2529 prt_page_height = prt_mediasize[i].height;
2530 }
2531 else
2532 {
2533 prt_page_width = prt_mediasize[i].height;
2534 prt_page_height = prt_mediasize[i].width;
2535 }
2536
2537 /*
2538 * Set PS page margins based on the PS pagesize, not the mediasize - this
2539 * needs to be done before the cpl and lpp are calculated.
2540 */
2541 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2542 &bottom);
2543 prt_left_margin = (float)left;
2544 prt_right_margin = (float)right;
2545 prt_top_margin = (float)top;
2546 prt_bottom_margin = (float)bottom;
2547
2548 /*
2549 * Set up the font size.
2550 */
2551 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2552 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2553 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2554 fontsize = atoi((char *)p + 2);
2555 prt_font_metrics(fontsize);
2556
2557 /*
2558 * Return the number of characters per line, and lines per page for the
2559 * generic print code.
2560 */
2561 psettings->chars_per_line = prt_get_cpl();
2562 psettings->lines_per_page = prt_get_lpp();
2563
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002564 // Catch margin settings that leave no space for output!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002565 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2566 return FAIL;
2567
2568 /*
2569 * Sort out the number of copies to be printed. PS by default will do
2570 * uncollated copies for you, so once we know how many uncollated copies are
2571 * wanted cache it away and lie to the generic code that we only want one
2572 * uncollated copy.
2573 */
2574 psettings->n_collated_copies = 1;
2575 psettings->n_uncollated_copies = 1;
2576 prt_num_copies = 1;
2577 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2578 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2579 if (prt_collate)
2580 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002581 // TODO: Get number of collated copies wanted.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002582 psettings->n_collated_copies = 1;
2583 }
2584 else
2585 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002586 // TODO: Get number of uncollated copies wanted and update the cached
2587 // count.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002588 prt_num_copies = 1;
2589 }
2590
2591 psettings->jobname = jobname;
2592
2593 /*
2594 * Set up printer duplex and tumble based on Duplex option setting - default
2595 * is long sided duplex printing (i.e. no tumble).
2596 */
2597 prt_duplex = TRUE;
2598 prt_tumble = FALSE;
2599 psettings->duplex = 1;
2600 if (printer_opts[OPT_PRINT_DUPLEX].present)
2601 {
2602 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2603 {
2604 prt_duplex = FALSE;
2605 psettings->duplex = 0;
2606 }
2607 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2608 == 0)
2609 prt_tumble = TRUE;
2610 }
2611
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002612 // For now user abort not supported
Bram Moolenaar81366db2005-07-24 21:16:51 +00002613 psettings->user_abort = 0;
2614
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002615 // If the user didn't specify a file name, use a temp file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002616 if (psettings->outfile == NULL)
2617 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002618 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002619 if (prt_ps_file_name == NULL)
2620 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002621 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002622 return FAIL;
2623 }
2624 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2625 }
2626 else
2627 {
2628 p = expand_env_save(psettings->outfile);
2629 if (p != NULL)
2630 {
2631 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2632 vim_free(p);
2633 }
2634 }
2635 if (prt_ps_fd == NULL)
2636 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00002637 emsg(_(e_cant_open_postscript_output_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002638 mch_print_cleanup();
2639 return FAIL;
2640 }
2641
2642 prt_bufsiz = psettings->chars_per_line;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002643 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002644 prt_bufsiz *= 2;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002645 ga_init2(&prt_ps_buffer, sizeof(char), prt_bufsiz);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002646
2647 prt_page_num = 0;
2648
2649 prt_attribute_change = FALSE;
2650 prt_need_moveto = FALSE;
2651 prt_need_font = FALSE;
2652 prt_need_fgcol = FALSE;
2653 prt_need_bgcol = FALSE;
2654 prt_need_underline = FALSE;
2655
2656 prt_file_error = FALSE;
2657
2658 return OK;
2659}
2660
2661 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002662prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002663{
2664 FILE* fd_resource;
2665 char_u resource_buffer[512];
2666 size_t bytes_read;
2667
2668 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2669 if (fd_resource == NULL)
2670 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002671 semsg(_(e_cant_open_file_str_2), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002672 return FALSE;
2673 }
2674 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2675 (char *)resource->title);
2676
2677 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2678
2679 for (;;)
2680 {
2681 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2682 sizeof(resource_buffer), fd_resource);
2683 if (ferror(fd_resource))
2684 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002685 semsg(_(e_cant_read_postscript_resource_file_str),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002686 resource->filename);
2687 fclose(fd_resource);
2688 return FALSE;
2689 }
2690 if (bytes_read == 0)
2691 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002692 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002693 if (prt_file_error)
2694 {
2695 fclose(fd_resource);
2696 return FALSE;
2697 }
2698 }
2699 fclose(fd_resource);
2700
2701 prt_dsc_noarg("EndDocument");
2702
2703 prt_dsc_noarg("EndResource");
2704
2705 return TRUE;
2706}
2707
2708 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002709mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002710{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002711 int bbox[4];
Bram Moolenaar81366db2005-07-24 21:16:51 +00002712 double left;
2713 double right;
2714 double top;
2715 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002716 struct prt_ps_resource_S *res_prolog;
2717 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002718 char buffer[256];
2719 char_u *p_encoding;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002720 struct prt_ps_resource_S *res_cidfont;
2721 struct prt_ps_resource_S *res_cmap;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002722 int retval = FALSE;
2723
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002724 res_prolog = ALLOC_ONE(struct prt_ps_resource_S);
2725 res_encoding = ALLOC_ONE(struct prt_ps_resource_S);
2726 res_cidfont = ALLOC_ONE(struct prt_ps_resource_S);
2727 res_cmap = ALLOC_ONE(struct prt_ps_resource_S);
Bram Moolenaard9462e32011-04-11 21:35:11 +02002728 if (res_prolog == NULL || res_encoding == NULL
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002729 || res_cidfont == NULL || res_cmap == NULL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002730 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002731
2732 /*
2733 * PS DSC Header comments - no PS code!
2734 */
2735 prt_dsc_start();
2736 prt_dsc_textline("Title", (char *)psettings->jobname);
2737 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002738 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002739 prt_dsc_textline("For", buffer);
2740 prt_dsc_textline("Creator", VIM_VERSION_LONG);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002741 // Note: to ensure Clean8bit I don't think we can use LC_TIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02002742
2743 prt_dsc_textline("CreationDate", get_ctime(time(NULL), FALSE));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002744 prt_dsc_textline("DocumentData", "Clean8Bit");
2745 prt_dsc_textline("Orientation", "Portrait");
2746 prt_dsc_atend("Pages");
2747 prt_dsc_textline("PageOrder", "Ascend");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002748 // The bbox does not change with orientation - it is always in the default
2749 // user coordinate system! We have to recalculate right and bottom
2750 // coordinates based on the font metrics for the bbox to be accurate.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002751 prt_page_margins(prt_mediasize[prt_media].width,
2752 prt_mediasize[prt_media].height,
2753 &left, &right, &top, &bottom);
2754 bbox[0] = (int)left;
2755 if (prt_portrait)
2756 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002757 // In portrait printing the fixed point is the top left corner so we
2758 // derive the bbox from that point. We have the expected cpl chars
2759 // across the media and lpp lines down the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002760 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002761 * (double)prt_line_height);
2762 bbox[2] = (int)(left + psettings->chars_per_line
2763 * (double)prt_char_width + 0.5);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002764 bbox[3] = (int)(top + 0.5);
2765 }
2766 else
2767 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002768 // In landscape printing the fixed point is the bottom left corner so we
2769 // derive the bbox from that point. We have lpp chars across the media
2770 // and cpl lines up the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002771 bbox[1] = (int)bottom;
2772 bbox[2] = (int)(left + ((psettings->lines_per_page
2773 + prt_header_height()) * prt_line_height) + 0.5);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002774 bbox[3] = (int)(bottom + psettings->chars_per_line
2775 * (double)prt_char_width + 0.5);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002776 }
2777 prt_dsc_ints("BoundingBox", 4, bbox);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002778 // The media width and height does not change with landscape printing!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002779 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2780 prt_mediasize[prt_media].width,
2781 prt_mediasize[prt_media].height,
2782 (double)0, NULL, NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002783 // Define fonts needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00002784 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002785 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002786 if (prt_out_mbyte)
2787 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002788 prt_dsc_font_resource((prt_use_courier ? NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002789 : "DocumentNeededResources"), &prt_ps_mb_font);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002790 if (!prt_custom_cmap)
2791 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002792 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002793
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002794 // Search for external resources VIM supplies
Bram Moolenaard9462e32011-04-11 21:35:11 +02002795 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002796 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002797 semsg(_(e_cant_find_postscript_resource_file_str_ps), "prolog");
Bram Moolenaar0a383962014-11-27 17:37:57 +01002798 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002799 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002800 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002801 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002802 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002803 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002804 if (prt_out_mbyte)
2805 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002806 // Look for required version of multi-byte printing procset
Bram Moolenaard9462e32011-04-11 21:35:11 +02002807 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002808 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002809 semsg(_(e_cant_find_postscript_resource_file_str_ps), "cidfont");
Bram Moolenaar0a383962014-11-27 17:37:57 +01002810 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002811 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002812 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002813 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002814 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002815 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002816 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002817
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002818 // Find an encoding to use for printing.
2819 // Check 'printencoding'. If not set or not found, then use 'encoding'. If
2820 // that cannot be found then default to "latin1".
2821 // Note: VIM specific encoding header is always skipped.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002822 if (!prt_out_mbyte)
2823 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002824 p_encoding = enc_skip(p_penc);
2825 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002826 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002827 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002828 // 'printencoding' not set or not supported - find alternate
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002829 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002830
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002831 p_encoding = enc_skip(p_enc);
2832 props = enc_canon_props(p_encoding);
2833 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002834 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002835 // 8-bit 'encoding' is not supported
2836 {
2837 // Use latin1 as default printing encoding
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002838 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002839 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002840 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002841 semsg(_(e_cant_find_postscript_resource_file_str_ps),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002842 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002843 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002844 }
2845 }
2846 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002847 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002848 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002849 // For the moment there are no checks on encoding resource files to
2850 // perform
Bram Moolenaar81366db2005-07-24 21:16:51 +00002851 }
2852 else
2853 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002854 p_encoding = enc_skip(p_penc);
2855 if (*p_encoding == NUL)
2856 p_encoding = enc_skip(p_enc);
2857 if (prt_use_courier)
2858 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002859 // Include ASCII range encoding vector
Bram Moolenaard9462e32011-04-11 21:35:11 +02002860 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002861 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002862 semsg(_(e_cant_find_postscript_resource_file_str_ps),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002863 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002864 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002865 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002866 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002867 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002868 // For the moment there are no checks on encoding resource files to
2869 // perform
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002870 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002871 }
2872
2873 prt_conv.vc_type = CONV_NONE;
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002874 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT))
2875 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002876 // Set up encoding conversion if required
Bram Moolenaar81366db2005-07-24 21:16:51 +00002877 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
2878 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002879 semsg(_(e_unable_to_convert_to_print_encoding_str), p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002880 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002881 }
2882 prt_do_conv = TRUE;
2883 }
2884 prt_do_conv = prt_conv.vc_type != CONV_NONE;
2885
2886 if (prt_out_mbyte && prt_custom_cmap)
2887 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002888 // Find user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002889 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002890 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002891 semsg(_(e_cant_find_postscript_resource_file_str_ps), prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002892 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002893 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002894 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002895 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002896 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002897
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002898 // List resources supplied
Bram Moolenaard9462e32011-04-11 21:35:11 +02002899 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002900 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002901 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002902 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002903 if (prt_out_mbyte)
2904 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002905 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002906 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002907 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002908 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002909
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002910 if (prt_custom_cmap)
2911 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002912 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002913 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002914 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002915 prt_dsc_resources(NULL, "cmap", buffer);
2916 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002917 }
2918 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002919 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002920 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002921 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002922 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002923 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002924 }
2925 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
2926#ifdef FEAT_SYN_HL
2927 psettings->do_syntax
2928#else
2929 0
2930#endif
2931 , prt_num_copies);
2932 prt_dsc_noarg("EndComments");
2933
2934 /*
2935 * PS Document page defaults
2936 */
2937 prt_dsc_noarg("BeginDefaults");
2938
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002939 // List font resources most likely common to all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002940 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002941 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002942 if (prt_out_mbyte)
2943 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002944 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
2945 &prt_ps_mb_font);
2946 if (!prt_custom_cmap)
2947 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002948 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002949
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002950 // Paper will be used for all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002951 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
2952
2953 prt_dsc_noarg("EndDefaults");
2954
2955 /*
2956 * PS Document prolog inclusion - all required procsets.
2957 */
2958 prt_dsc_noarg("BeginProlog");
2959
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002960 // Add required procsets - NOTE: order is important!
Bram Moolenaard9462e32011-04-11 21:35:11 +02002961 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002962 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002963 if (prt_out_mbyte)
2964 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002965 // Add CID font procset, and any user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002966 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002967 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002968 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002969 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002970 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002971
Bram Moolenaar81366db2005-07-24 21:16:51 +00002972 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002973 // There will be only one Roman font encoding to be included in the PS
2974 // file.
Bram Moolenaard9462e32011-04-11 21:35:11 +02002975 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002976 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002977
2978 prt_dsc_noarg("EndProlog");
2979
2980 /*
2981 * PS Document setup - must appear after the prolog
2982 */
2983 prt_dsc_noarg("BeginSetup");
2984
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002985 // Device setup - page size and number of uncollated copies
Bram Moolenaar81366db2005-07-24 21:16:51 +00002986 prt_write_int((int)prt_mediasize[prt_media].width);
2987 prt_write_int((int)prt_mediasize[prt_media].height);
2988 prt_write_int(0);
2989 prt_write_string("sps\n");
2990 prt_write_int(prt_num_copies);
2991 prt_write_string("nc\n");
2992 prt_write_boolean(prt_duplex);
2993 prt_write_boolean(prt_tumble);
2994 prt_write_string("dt\n");
2995 prt_write_boolean(prt_collate);
2996 prt_write_string("c\n");
2997
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002998 // Font resource inclusion and definition
Bram Moolenaar81366db2005-07-24 21:16:51 +00002999 if (!prt_out_mbyte || prt_use_courier)
3000 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003001 // When using Courier for ASCII range when printing multi-byte, need to
3002 // pick up ASCII encoding to use with it.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003003 if (prt_use_courier)
3004 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003005 prt_dsc_resources("IncludeResource", "font",
3006 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3007 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3008 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3009 prt_dsc_resources("IncludeResource", "font",
3010 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3011 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3012 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3013 prt_dsc_resources("IncludeResource", "font",
3014 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3015 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3016 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3017 prt_dsc_resources("IncludeResource", "font",
3018 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3019 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3020 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003021 }
3022 if (prt_out_mbyte)
3023 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003024 // Define the CID fonts to be used in the job. Typically CJKV fonts do
3025 // not have an italic form being a western style, so where no font is
3026 // defined for these faces VIM falls back to an existing face.
3027 // Note: if using Courier for the ASCII range then the printout will
3028 // have bold/italic/bolditalic regardless of the setting of printmbfont.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003029 prt_dsc_resources("IncludeResource", "font",
3030 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3031 if (!prt_custom_cmap)
3032 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3033 prt_def_cidfont("CF0", (int)prt_line_height,
3034 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003035
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003036 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3037 {
3038 prt_dsc_resources("IncludeResource", "font",
3039 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3040 if (!prt_custom_cmap)
3041 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3042 prt_def_cidfont("CF1", (int)prt_line_height,
3043 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3044 }
3045 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003046 // Use ROMAN for BOLD
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003047 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003048
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003049 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3050 {
3051 prt_dsc_resources("IncludeResource", "font",
3052 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3053 if (!prt_custom_cmap)
3054 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3055 prt_def_cidfont("CF2", (int)prt_line_height,
3056 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3057 }
3058 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003059 // Use ROMAN for OBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003060 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003061
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003062 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3063 {
3064 prt_dsc_resources("IncludeResource", "font",
3065 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3066 if (!prt_custom_cmap)
3067 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3068 prt_def_cidfont("CF3", (int)prt_line_height,
3069 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3070 }
3071 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003072 // Use BOLD for BOLDOBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003073 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003074 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003075
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003076 // Misc constant vars used for underlining and background rects
Bram Moolenaar81366db2005-07-24 21:16:51 +00003077 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3078 prt_ps_font->uline_offset), 2);
3079 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3080 prt_ps_font->uline_width), 2);
3081 prt_def_var("BO", prt_bgcol_offset, 2);
3082
3083 prt_dsc_noarg("EndSetup");
3084
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003085 // Fail if any problems writing out to the PS file
Bram Moolenaard9462e32011-04-11 21:35:11 +02003086 retval = !prt_file_error;
3087
3088theend:
3089 vim_free(res_prolog);
3090 vim_free(res_encoding);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003091 vim_free(res_cidfont);
3092 vim_free(res_cmap);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003093
3094 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003095}
3096
3097 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003098mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003099{
3100 prt_dsc_noarg("Trailer");
3101
3102 /*
3103 * Output any info we don't know in toto until we finish
3104 */
3105 prt_dsc_ints("Pages", 1, &prt_page_num);
3106
3107 prt_dsc_noarg("EOF");
3108
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003109 // Write CTRL-D to close serial communication link if used.
3110 // NOTHING MUST BE WRITTEN AFTER THIS!
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003111 prt_write_file((char_u *)"\004");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003112
3113 if (!prt_file_error && psettings->outfile == NULL
3114 && !got_int && !psettings->user_abort)
3115 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003116 // Close the file first.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003117 if (prt_ps_fd != NULL)
3118 {
3119 fclose(prt_ps_fd);
3120 prt_ps_fd = NULL;
3121 }
3122 prt_message((char_u *)_("Sending to printer..."));
3123
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003124 // Not printing to a file: use 'printexpr' to print the file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003125 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003126 emsg(_(e_failed_to_print_postscript_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003127 else
3128 prt_message((char_u *)_("Print job sent."));
3129 }
3130
3131 mch_print_cleanup();
3132}
3133
3134 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003135mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003136{
3137 prt_flush_buffer();
3138
3139 prt_write_string("re sp\n");
3140
3141 prt_dsc_noarg("PageTrailer");
3142
3143 return !prt_file_error;
3144}
3145
Bram Moolenaar81366db2005-07-24 21:16:51 +00003146 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003147mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003148{
3149 int page_num[2];
3150
3151 prt_page_num++;
3152
3153 page_num[0] = page_num[1] = prt_page_num;
3154 prt_dsc_ints("Page", 2, page_num);
3155
3156 prt_dsc_noarg("BeginPageSetup");
3157
3158 prt_write_string("sv\n0 g\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003159 prt_in_ascii = !prt_out_mbyte;
3160 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003161 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003162 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003163 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003164 prt_fgcol = PRCOLOR_BLACK;
3165 prt_bgcol = PRCOLOR_WHITE;
3166 prt_font = PRT_PS_FONT_ROMAN;
3167
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003168 // Set up page transformation for landscape printing.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003169 if (!prt_portrait)
3170 {
3171 prt_write_int(-((int)prt_mediasize[prt_media].width));
3172 prt_write_string("sl\n");
3173 }
3174
3175 prt_dsc_noarg("EndPageSetup");
3176
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003177 // We have reset the font attributes, force setting them again.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003178 curr_bg = (long_u)0xffffffff;
3179 curr_fg = (long_u)0xffffffff;
3180 curr_bold = MAYBE;
3181
3182 return !prt_file_error;
3183}
3184
3185 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003186mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003187{
3188 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3189}
3190
3191static float prt_pos_x = 0;
3192static float prt_pos_y = 0;
3193
3194 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003195mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003196{
3197 prt_pos_x = prt_left_margin;
3198 if (margin)
3199 prt_pos_x -= prt_number_width;
3200
3201 prt_pos_y = prt_top_margin - prt_first_line_height -
3202 page_line * prt_line_height;
3203
3204 prt_attribute_change = TRUE;
3205 prt_need_moveto = TRUE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003206 prt_half_width = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003207}
3208
Bram Moolenaar81366db2005-07-24 21:16:51 +00003209 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003210mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003211{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003212 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003213 int need_break;
3214 char_u ch;
3215 char_u ch_buff[8];
3216 float char_width;
3217 float next_pos;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003218 int in_ascii;
3219 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003220 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003221
3222 char_width = prt_char_width;
3223
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003224 // Ideally VIM would create a rearranged CID font to combine a Roman and
3225 // CJKV font to do what VIM is doing here - use a Roman font for characters
3226 // in the ASCII range, and the original CID font for everything else.
3227 // The problem is that GhostScript still (as of 8.13) does not support
3228 // rearranged fonts even though they have been documented by Adobe for 7
3229 // years! If they ever do, a lot of this code will disappear.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003230 if (prt_use_courier)
3231 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003232 in_ascii = (len == 1 && *p < 0x80);
3233 if (prt_in_ascii)
3234 {
3235 if (!in_ascii)
3236 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003237 // No longer in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003238 prt_in_ascii = FALSE;
3239 prt_need_font = TRUE;
3240 prt_attribute_change = TRUE;
3241 }
3242 }
3243 else if (in_ascii)
3244 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003245 // Now in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003246 prt_in_ascii = TRUE;
3247 prt_need_font = TRUE;
3248 prt_attribute_change = TRUE;
3249 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003250 }
3251 if (prt_out_mbyte)
3252 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003253 half_width = ((*mb_ptr2cells)(p) == 1);
3254 if (half_width)
3255 char_width /= 2;
3256 if (prt_half_width)
3257 {
3258 if (!half_width)
3259 {
3260 prt_half_width = FALSE;
3261 prt_pos_x += prt_char_width/4;
3262 prt_need_moveto = TRUE;
3263 prt_attribute_change = TRUE;
3264 }
3265 }
3266 else if (half_width)
3267 {
3268 prt_half_width = TRUE;
3269 prt_pos_x += prt_char_width/4;
3270 prt_need_moveto = TRUE;
3271 prt_attribute_change = TRUE;
3272 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003273 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003274
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003275 // Output any required changes to the graphics state, after flushing any
3276 // text buffered so far.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003277 if (prt_attribute_change)
3278 {
3279 prt_flush_buffer();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003280 // Reset count of number of chars that will be printed
Bram Moolenaar81366db2005-07-24 21:16:51 +00003281 prt_text_run = 0;
3282
3283 if (prt_need_moveto)
3284 {
3285 prt_pos_x_moveto = prt_pos_x;
3286 prt_pos_y_moveto = prt_pos_y;
3287 prt_do_moveto = TRUE;
3288
3289 prt_need_moveto = FALSE;
3290 }
3291 if (prt_need_font)
3292 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003293 if (!prt_in_ascii)
3294 prt_write_string("CF");
3295 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003296 prt_write_string("F");
3297 prt_write_int(prt_font);
3298 prt_write_string("sf\n");
3299 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003300 }
3301 if (prt_need_fgcol)
3302 {
3303 int r, g, b;
3304 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3305 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3306 b = prt_fgcol & 0xff;
3307
3308 prt_write_real(r / 255.0, 3);
3309 if (r == g && g == b)
3310 prt_write_string("g\n");
3311 else
3312 {
3313 prt_write_real(g / 255.0, 3);
3314 prt_write_real(b / 255.0, 3);
3315 prt_write_string("r\n");
3316 }
3317 prt_need_fgcol = FALSE;
3318 }
3319
3320 if (prt_bgcol != PRCOLOR_WHITE)
3321 {
3322 prt_new_bgcol = prt_bgcol;
3323 if (prt_need_bgcol)
3324 prt_do_bgcol = TRUE;
3325 }
3326 else
3327 prt_do_bgcol = FALSE;
3328 prt_need_bgcol = FALSE;
3329
3330 if (prt_need_underline)
3331 prt_do_underline = prt_underline;
3332 prt_need_underline = FALSE;
3333
3334 prt_attribute_change = FALSE;
3335 }
3336
Bram Moolenaar81366db2005-07-24 21:16:51 +00003337 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003338 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003339 // Convert from multi-byte to 8-bit encoding
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003340 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003341 if (p == NULL)
3342 {
3343 p = (char_u *)"";
3344 len = 0;
3345 }
3346 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003347
3348 if (prt_out_mbyte)
3349 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003350 // Multi-byte character strings are represented more efficiently as hex
3351 // strings when outputting clean 8 bit PS.
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003352 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003353 {
3354 ch = prt_hexchar[(unsigned)(*p) >> 4];
3355 ga_append(&prt_ps_buffer, ch);
3356 ch = prt_hexchar[(*p) & 0xf];
3357 ga_append(&prt_ps_buffer, ch);
3358 p++;
3359 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003360 }
3361 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003362 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003363 // Add next character to buffer of characters to output.
3364 // Note: One printed character may require several PS characters to
3365 // represent it, but we only count them as one printed character.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003366 ch = *p;
3367 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3368 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003369 // Convert non-printing characters to either their escape or octal
3370 // sequence, ensures PS sent over a serial line does not interfere
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003371 // with the comms protocol.
3372 ga_append(&prt_ps_buffer, '\\');
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003373 switch (ch)
3374 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003375 case BS: ga_append(&prt_ps_buffer, 'b'); break;
3376 case TAB: ga_append(&prt_ps_buffer, 't'); break;
3377 case NL: ga_append(&prt_ps_buffer, 'n'); break;
3378 case FF: ga_append(&prt_ps_buffer, 'f'); break;
3379 case CAR: ga_append(&prt_ps_buffer, 'r'); break;
3380 case '(': ga_append(&prt_ps_buffer, '('); break;
3381 case ')': ga_append(&prt_ps_buffer, ')'); break;
3382 case '\\': ga_append(&prt_ps_buffer, '\\'); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003383
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003384 default:
3385 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003386 ga_append(&prt_ps_buffer, ch_buff[0]);
3387 ga_append(&prt_ps_buffer, ch_buff[1]);
3388 ga_append(&prt_ps_buffer, ch_buff[2]);
3389 break;
3390 }
3391 }
3392 else
3393 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003394 }
3395
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003396 // Need to free any translated characters
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003397 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003398
3399 prt_text_run += char_width;
3400 prt_pos_x += char_width;
3401
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003402 // The downside of fp - use relative error on right margin check
Bram Moolenaar81366db2005-07-24 21:16:51 +00003403 next_pos = prt_pos_x + prt_char_width;
3404 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003405 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003406
3407 if (need_break)
3408 prt_flush_buffer();
3409
3410 return need_break;
3411}
3412
3413 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003414mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003415{
3416 int font = 0;
3417
3418 if (iBold)
3419 font |= 0x01;
3420 if (iItalic)
3421 font |= 0x02;
3422
3423 if (font != prt_font)
3424 {
3425 prt_font = font;
3426 prt_attribute_change = TRUE;
3427 prt_need_font = TRUE;
3428 }
3429 if (prt_underline != iUnderline)
3430 {
3431 prt_underline = iUnderline;
3432 prt_attribute_change = TRUE;
3433 prt_need_underline = TRUE;
3434 }
3435}
3436
3437 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003438mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003439{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003440 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003441 prt_attribute_change = TRUE;
3442 prt_need_bgcol = TRUE;
3443}
3444
3445 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003446mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003447{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003448 if (fgcol == (long_u)prt_fgcol)
3449 return;
3450
3451 prt_fgcol = (int)fgcol;
3452 prt_attribute_change = TRUE;
3453 prt_need_fgcol = TRUE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003454}
3455
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003456# endif //FEAT_POSTSCRIPT
3457#endif //FEAT_PRINTER