blob: 3993f4d58410f219601457a9d09eed0537d618c7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar81366db2005-07-24 21:16:51 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * hardcopy.c: printing to paper
12 */
13
14#include "vim.h"
15#include "version.h"
16
17#if defined(FEAT_PRINTER) || defined(PROTO)
18/*
19 * To implement printing on a platform, the following functions must be
20 * defined:
21 *
22 * int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
23 * Called once. Code should display printer dialogue (if appropriate) and
24 * determine printer font and margin settings. Reset has_color if the printer
25 * doesn't support colors at all.
26 * Returns FAIL to abort.
27 *
28 * int mch_print_begin(prt_settings_T *settings)
29 * Called to start the print job.
30 * Return FALSE to abort.
31 *
32 * int mch_print_begin_page(char_u *msg)
33 * Called at the start of each page.
34 * "msg" indicates the progress of the print job, can be NULL.
35 * Return FALSE to abort.
36 *
37 * int mch_print_end_page()
38 * Called at the end of each page.
39 * Return FALSE to abort.
40 *
41 * int mch_print_blank_page()
42 * Called to generate a blank page for collated, duplex, multiple copy
43 * document. Return FALSE to abort.
44 *
45 * void mch_print_end(prt_settings_T *psettings)
46 * Called at normal end of print job.
47 *
48 * void mch_print_cleanup()
49 * Called if print job ends normally or is abandoned. Free any memory, close
50 * devices and handles. Also called when mch_print_begin() fails, but not
51 * when mch_print_init() fails.
52 *
53 * void mch_print_set_font(int Bold, int Italic, int Underline);
54 * Called whenever the font style changes.
55 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000056 * void mch_print_set_bg(long_u bgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000057 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
59 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000060 * void mch_print_set_fg(long_u fgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000061 * Called to set the foreground color for the following text. Parameter is an
62 * RGB value.
63 *
64 * mch_print_start_line(int margin, int page_line)
65 * Sets the current position at the start of line "page_line".
66 * If margin is TRUE start in the left margin (for header and line number).
67 *
68 * int mch_print_text_out(char_u *p, int len);
69 * Output one character of text p[len] at the current position.
70 * Return TRUE if there is no room for another character in the same line.
71 *
72 * Note that the generic code has no idea of margins. The machine code should
73 * simply make the page look smaller! The header and the line numbers are
74 * printed in the margin.
75 */
76
77#ifdef FEAT_SYN_HL
78static const long_u cterm_color_8[8] =
79{
80 (long_u)0x000000L, (long_u)0xff0000L, (long_u)0x00ff00L, (long_u)0xffff00L,
81 (long_u)0x0000ffL, (long_u)0xff00ffL, (long_u)0x00ffffL, (long_u)0xffffffL
82};
83
84static const long_u cterm_color_16[16] =
85{
86 (long_u)0x000000L, (long_u)0x0000c0L, (long_u)0x008000L, (long_u)0x004080L,
87 (long_u)0xc00000L, (long_u)0xc000c0L, (long_u)0x808000L, (long_u)0xc0c0c0L,
88 (long_u)0x808080L, (long_u)0x6060ffL, (long_u)0x00ff00L, (long_u)0x00ffffL,
89 (long_u)0xff8080L, (long_u)0xff40ffL, (long_u)0xffff00L, (long_u)0xffffffL
90};
91
92static int current_syn_id;
93#endif
94
95#define PRCOLOR_BLACK (long_u)0
96#define PRCOLOR_WHITE (long_u)0xFFFFFFL
97
98static int curr_italic;
99static int curr_bold;
100static int curr_underline;
101static long_u curr_bg;
102static long_u curr_fg;
103static int page_count;
104
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100105#if defined(FEAT_POSTSCRIPT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000106# define OPT_MBFONT_USECOURIER 0
107# define OPT_MBFONT_ASCII 1
108# define OPT_MBFONT_REGULAR 2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000109# define OPT_MBFONT_BOLD 3
Bram Moolenaar81366db2005-07-24 21:16:51 +0000110# define OPT_MBFONT_OBLIQUE 4
111# define OPT_MBFONT_BOLDOBLIQUE 5
112# define OPT_MBFONT_NUM_OPTIONS 6
113
114static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] =
115{
116 {"c", FALSE, 0, NULL, 0, FALSE},
117 {"a", FALSE, 0, NULL, 0, FALSE},
118 {"r", FALSE, 0, NULL, 0, FALSE},
119 {"b", FALSE, 0, NULL, 0, FALSE},
120 {"i", FALSE, 0, NULL, 0, FALSE},
121 {"o", FALSE, 0, NULL, 0, FALSE},
122};
123#endif
124
125/*
126 * These values determine the print position on a page.
127 */
128typedef struct
129{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100130 int lead_spaces; // remaining spaces for a TAB
131 int print_pos; // virtual column for computing TABs
132 colnr_T column; // byte column
133 linenr_T file_line; // line nr in the buffer
134 long_u bytes_printed; // bytes printed so far
135 int ff; // seen form feed character
Bram Moolenaar81366db2005-07-24 21:16:51 +0000136} prt_pos_T;
137
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138static char *parse_list_options(char_u *option_str, option_table_T *table, int table_size);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000139
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T *ppos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000141
142/*
143 * Parse 'printoptions' and set the flags in "printer_opts".
144 * Returns an error message or NULL;
145 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100147parse_printoptions(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000148{
149 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
150}
151
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100152#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000153/*
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200154 * Parse 'printmbfont' and set the flags in "mbfont_opts".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000155 * Returns an error message or NULL;
156 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100157 char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100158parse_printmbfont(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000159{
160 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
161}
162#endif
163
164/*
165 * Parse a list of options in the form
166 * option:value,option:value,option:value
167 *
168 * "value" can start with a number which is parsed out, e.g. margin:12mm
169 *
170 * Returns an error message for an illegal option, NULL otherwise.
171 * Only used for the printer at the moment...
172 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100173 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100174parse_list_options(
175 char_u *option_str,
176 option_table_T *table,
177 int table_size)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000178{
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200179 option_table_T *old_opts;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100180 char *ret = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000181 char_u *stringp;
182 char_u *colonp;
183 char_u *commap;
184 char_u *p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100185 int idx = 0; // init for GCC
Bram Moolenaar81366db2005-07-24 21:16:51 +0000186 int len;
187
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100188 // Save the old values, so that they can be restored in case of an error.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200189 old_opts = ALLOC_MULT(option_table_T, table_size);
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200190 if (old_opts == NULL)
191 return NULL;
192
Bram Moolenaar81366db2005-07-24 21:16:51 +0000193 for (idx = 0; idx < table_size; ++idx)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200194 {
195 old_opts[idx] = table[idx];
Bram Moolenaar81366db2005-07-24 21:16:51 +0000196 table[idx].present = FALSE;
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200197 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000198
199 /*
200 * Repeat for all comma separated parts.
201 */
202 stringp = option_str;
203 while (*stringp)
204 {
205 colonp = vim_strchr(stringp, ':');
206 if (colonp == NULL)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100208 ret = N_("E550: Missing colon");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200209 break;
210 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000211 commap = vim_strchr(stringp, ',');
212 if (commap == NULL)
213 commap = option_str + STRLEN(option_str);
214
215 len = (int)(colonp - stringp);
216
217 for (idx = 0; idx < table_size; ++idx)
218 if (STRNICMP(stringp, table[idx].name, len) == 0)
219 break;
220
221 if (idx == table_size)
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100223 ret = N_("E551: Illegal component");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200224 break;
225 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000226 p = colonp + 1;
227 table[idx].present = TRUE;
228
229 if (table[idx].hasnum)
230 {
231 if (!VIM_ISDIGIT(*p))
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100233 ret = N_("E552: digit expected");
Bram Moolenaar4afc7c52016-04-03 14:56:52 +0200234 break;
235 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000236
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);
296
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200297# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200298 if (USE_24BIT)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000299 {
300 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
301 if (bg_color == PRCOLOR_BLACK)
302 bg_color = PRCOLOR_WHITE;
303
304 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
305 }
306 else
307# endif
308 {
309 bg_color = PRCOLOR_WHITE;
310
311 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
312 if (color == NULL)
313 colorindex = 0;
314 else
315 colorindex = atoi(color);
316
317 if (colorindex >= 0 && colorindex < t_colors)
318 fg_color = prt_get_term_color(colorindex);
319 else
320 fg_color = PRCOLOR_BLACK;
321 }
322
323 if (fg_color == PRCOLOR_WHITE)
324 fg_color = PRCOLOR_BLACK;
325 else if (*p_bg == 'd')
326 fg_color = darken_rgb(fg_color);
327
328 pattr->fg_color = fg_color;
329 pattr->bg_color = bg_color;
330}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100331#endif // FEAT_SYN_HL
Bram Moolenaar81366db2005-07-24 21:16:51 +0000332
333 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100334prt_set_fg(long_u fg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000335{
336 if (fg != curr_fg)
337 {
338 curr_fg = fg;
339 mch_print_set_fg(fg);
340 }
341}
342
343 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100344prt_set_bg(long_u bg)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000345{
346 if (bg != curr_bg)
347 {
348 curr_bg = bg;
349 mch_print_set_bg(bg);
350 }
351}
352
353 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100354prt_set_font(int bold, int italic, int underline)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000355{
356 if (curr_bold != bold
357 || curr_italic != italic
358 || curr_underline != underline)
359 {
360 curr_underline = underline;
361 curr_italic = italic;
362 curr_bold = bold;
363 mch_print_set_font(bold, italic, underline);
364 }
365}
366
367/*
368 * Print the line number in the left margin.
369 */
370 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100371prt_line_number(
372 prt_settings_T *psettings,
373 int page_line,
374 linenr_T lnum)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000375{
376 int i;
377 char_u tbuf[20];
378
379 prt_set_fg(psettings->number.fg_color);
380 prt_set_bg(psettings->number.bg_color);
381 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
382 mch_print_start_line(TRUE, page_line);
383
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100384 // Leave two spaces between the number and the text; depends on
385 // PRINT_NUMBER_WIDTH.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000386 sprintf((char *)tbuf, "%6ld", (long)lnum);
387 for (i = 0; i < 6; i++)
388 (void)mch_print_text_out(&tbuf[i], 1);
389
390#ifdef FEAT_SYN_HL
391 if (psettings->do_syntax)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100392 // Set colors for next character.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000393 current_syn_id = -1;
394 else
395#endif
396 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100397 // Set colors and font back to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000398 prt_set_fg(PRCOLOR_BLACK);
399 prt_set_bg(PRCOLOR_WHITE);
400 prt_set_font(FALSE, FALSE, FALSE);
401 }
402}
403
Bram Moolenaar81366db2005-07-24 21:16:51 +0000404/*
405 * Get the currently effective header height.
406 */
407 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100408prt_header_height(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000409{
410 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
411 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
412 return 2;
413}
414
415/*
416 * Return TRUE if using a line number for printing.
417 */
418 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100419prt_use_number(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000420{
421 return (printer_opts[OPT_PRINT_NUMBER].present
422 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
423}
424
425/*
426 * Return the unit used in a margin item in 'printoptions'.
427 * Returns PRT_UNIT_NONE if not recognized.
428 */
429 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100430prt_get_unit(int idx)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000431{
432 int u = PRT_UNIT_NONE;
433 int i;
434 static char *(units[4]) = PRT_UNIT_NAMES;
435
436 if (printer_opts[idx].present)
437 for (i = 0; i < 4; ++i)
438 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
439 {
440 u = i;
441 break;
442 }
443 return u;
444}
445
446/*
447 * Print the page header.
448 */
Bram Moolenaar81366db2005-07-24 21:16:51 +0000449 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100450prt_header(
451 prt_settings_T *psettings,
452 int pagenum,
453 linenr_T lnum UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000454{
455 int width = psettings->chars_per_line;
456 int page_line;
457 char_u *tbuf;
458 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000459 int l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000460
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100461 // Also use the space for the line number.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000462 if (prt_use_number())
463 width += PRINT_NUMBER_WIDTH;
464
465 tbuf = alloc(width + IOSIZE);
466 if (tbuf == NULL)
467 return;
468
469#ifdef FEAT_STL_OPT
470 if (*p_header != NUL)
471 {
472 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000473 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000474
475 /*
476 * Need to (temporarily) set current line number and first/last line
477 * number on the 'window'. Since we don't know how long the page is,
478 * set the first and current line number to the top line, and guess
479 * that the page length is 64.
480 */
481 tmp_lnum = curwin->w_cursor.lnum;
482 tmp_topline = curwin->w_topline;
483 tmp_botline = curwin->w_botline;
484 curwin->w_cursor.lnum = lnum;
485 curwin->w_topline = lnum;
486 curwin->w_botline = lnum + 63;
487 printer_page_num = pagenum;
488
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000489# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000490 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000491# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000492 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000493 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000494 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000495
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100496 // Reset line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000497 curwin->w_cursor.lnum = tmp_lnum;
498 curwin->w_topline = tmp_topline;
499 curwin->w_botline = tmp_botline;
500 }
501 else
502#endif
503 sprintf((char *)tbuf, _("Page %d"), pagenum);
504
505 prt_set_fg(PRCOLOR_BLACK);
506 prt_set_bg(PRCOLOR_WHITE);
507 prt_set_font(TRUE, FALSE, FALSE);
508
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100509 // Use a negative line number to indicate printing in the top margin.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000510 page_line = 0 - prt_header_height();
511 mch_print_start_line(TRUE, page_line);
512 for (p = tbuf; *p != NUL; )
513 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100514 if (mch_print_text_out(p, (l = (*mb_ptr2len)(p))))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000515 {
516 ++page_line;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100517 if (page_line >= 0) // out of room in header
Bram Moolenaar81366db2005-07-24 21:16:51 +0000518 break;
519 mch_print_start_line(TRUE, page_line);
520 }
Bram Moolenaar81366db2005-07-24 21:16:51 +0000521 p += l;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000522 }
523
524 vim_free(tbuf);
525
526#ifdef FEAT_SYN_HL
527 if (psettings->do_syntax)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100528 // Set colors for next character.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000529 current_syn_id = -1;
530 else
531#endif
532 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100533 // Set colors and font back to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000534 prt_set_fg(PRCOLOR_BLACK);
535 prt_set_bg(PRCOLOR_WHITE);
536 prt_set_font(FALSE, FALSE, FALSE);
537 }
538}
539
540/*
541 * Display a print status message.
542 */
543 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100544prt_message(char_u *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000545{
546 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
Bram Moolenaar8820b482017-03-16 17:23:31 +0100547 screen_puts(s, (int)Rows - 1, 0, HL_ATTR(HLF_R));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000548 out_flush();
549}
550
551 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100552ex_hardcopy(exarg_T *eap)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000553{
554 linenr_T lnum;
555 int collated_copies, uncollated_copies;
556 prt_settings_T settings;
557 long_u bytes_to_print = 0;
558 int page_line;
559 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000560
Bram Moolenaara80faa82020-04-12 19:37:17 +0200561 CLEAR_FIELD(settings);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000562 settings.has_color = TRUE;
563
564# ifdef FEAT_POSTSCRIPT
565 if (*eap->arg == '>')
566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 char *errormsg = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000568
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100569 // Expand things like "%.ps".
Bram Moolenaar81366db2005-07-24 21:16:51 +0000570 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
571 {
572 if (errormsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100573 emsg(errormsg);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000574 return;
575 }
576 settings.outfile = skipwhite(eap->arg + 1);
577 }
578 else if (*eap->arg != NUL)
579 settings.arguments = eap->arg;
580# endif
581
582 /*
583 * Initialise for printing. Ask the user for settings, unless forceit is
584 * set.
585 * The mch_print_init() code should set up margins if applicable. (It may
586 * not be a real printer - for example the engine might generate HTML or
587 * PS.)
588 */
589 if (mch_print_init(&settings,
590 curbuf->b_fname == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100591 ? buf_spname(curbuf)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000592 : curbuf->b_sfname == NULL
593 ? curbuf->b_fname
594 : curbuf->b_sfname,
595 eap->forceit) == FAIL)
596 return;
597
598#ifdef FEAT_SYN_HL
599# ifdef FEAT_GUI
600 if (gui.in_use)
601 settings.modec = 'g';
602 else
603# endif
604 if (t_colors > 1)
605 settings.modec = 'c';
606 else
607 settings.modec = 't';
608
Bram Moolenaar860cae12010-06-05 23:22:07 +0200609 if (!syntax_present(curwin))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000610 settings.do_syntax = FALSE;
611 else if (printer_opts[OPT_PRINT_SYNTAX].present
612 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
613 settings.do_syntax =
614 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
615 else
616 settings.do_syntax = settings.has_color;
617#endif
618
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100619 // Set up printing attributes for line numbers
Bram Moolenaar81366db2005-07-24 21:16:51 +0000620 settings.number.fg_color = PRCOLOR_BLACK;
621 settings.number.bg_color = PRCOLOR_WHITE;
622 settings.number.bold = FALSE;
623 settings.number.italic = TRUE;
624 settings.number.underline = FALSE;
625#ifdef FEAT_SYN_HL
626 /*
627 * Syntax highlighting of line numbers.
628 */
629 if (prt_use_number() && settings.do_syntax)
630 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000631 int id;
632
Bram Moolenaar81366db2005-07-24 21:16:51 +0000633 id = syn_name2id((char_u *)"LineNr");
634 if (id > 0)
635 id = syn_get_final_id(id);
636
637 prt_get_attr(id, &settings.number, settings.modec);
638 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000639#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000640
641 /*
642 * Estimate the total lines to be printed
643 */
644 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
645 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
646 if (bytes_to_print == 0)
647 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100648 msg(_("No text to be printed"));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000649 goto print_fail_no_begin;
650 }
651
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100652 // Set colors and font to normal.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000653 curr_bg = (long_u)0xffffffffL;
654 curr_fg = (long_u)0xffffffffL;
655 curr_italic = MAYBE;
656 curr_bold = MAYBE;
657 curr_underline = MAYBE;
658
659 prt_set_fg(PRCOLOR_BLACK);
660 prt_set_bg(PRCOLOR_WHITE);
661 prt_set_font(FALSE, FALSE, FALSE);
662#ifdef FEAT_SYN_HL
663 current_syn_id = -1;
664#endif
665
666 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
667 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
668
669 if (!mch_print_begin(&settings))
670 goto print_fail_no_begin;
671
672 /*
673 * Loop over collated copies: 1 2 3, 1 2 3, ...
674 */
675 page_count = 0;
676 for (collated_copies = 0;
677 collated_copies < settings.n_collated_copies;
678 collated_copies++)
679 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100680 prt_pos_T prtpos; // current print position
681 prt_pos_T page_prtpos; // print position at page start
Bram Moolenaar81366db2005-07-24 21:16:51 +0000682 int side;
683
Bram Moolenaara80faa82020-04-12 19:37:17 +0200684 CLEAR_FIELD(page_prtpos);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000685 page_prtpos.file_line = eap->line1;
686 prtpos = page_prtpos;
687
688 if (jobsplit && collated_copies > 0)
689 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100690 // Splitting jobs: Stop a previous job and start a new one.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000691 mch_print_end(&settings);
692 if (!mch_print_begin(&settings))
693 goto print_fail_no_begin;
694 }
695
696 /*
697 * Loop over all pages in the print job: 1 2 3 ...
698 */
699 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
700 {
701 /*
702 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
703 * For duplex: 12 12 12 34 34 34, ...
704 */
705 for (uncollated_copies = 0;
706 uncollated_copies < settings.n_uncollated_copies;
707 uncollated_copies++)
708 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100709 // Set the print position to the start of this page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000710 prtpos = page_prtpos;
711
712 /*
713 * Do front and rear side of a page.
714 */
715 for (side = 0; side <= settings.duplex; ++side)
716 {
717 /*
718 * Print one page.
719 */
720
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100721 // Check for interrupt character every page.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000722 ui_breakcheck();
723 if (got_int || settings.user_abort)
724 goto print_fail;
725
726 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
727 page_count + 1 + side,
728 prtpos.bytes_printed > 1000000
729 ? (int)(prtpos.bytes_printed /
730 (bytes_to_print / 100))
731 : (int)((prtpos.bytes_printed * 100)
732 / bytes_to_print));
733 if (!mch_print_begin_page(IObuff))
734 goto print_fail;
735
736 if (settings.n_collated_copies > 1)
737 sprintf((char *)IObuff + STRLEN(IObuff),
738 _(" Copy %d of %d"),
739 collated_copies + 1,
740 settings.n_collated_copies);
741 prt_message(IObuff);
742
743 /*
744 * Output header if required
745 */
746 if (prt_header_height() > 0)
747 prt_header(&settings, page_count + 1 + side,
748 prtpos.file_line);
749
750 for (page_line = 0; page_line < settings.lines_per_page;
751 ++page_line)
752 {
753 prtpos.column = hardcopy_line(&settings,
754 page_line, &prtpos);
755 if (prtpos.column == 0)
756 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100757 // finished a file line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000758 prtpos.bytes_printed +=
759 STRLEN(skipwhite(ml_get(prtpos.file_line)));
760 if (++prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100761 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000762 }
763 else if (prtpos.ff)
764 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100765 // Line had a formfeed in it - start new page but
766 // stay on the current line
Bram Moolenaar81366db2005-07-24 21:16:51 +0000767 break;
768 }
769 }
770
771 if (!mch_print_end_page())
772 goto print_fail;
773 if (prtpos.file_line > eap->line2)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100774 break; // reached the end
Bram Moolenaar81366db2005-07-24 21:16:51 +0000775 }
776
777 /*
778 * Extra blank page for duplexing with odd number of pages and
779 * more copies to come.
780 */
781 if (prtpos.file_line > eap->line2 && settings.duplex
782 && side == 0
783 && uncollated_copies + 1 < settings.n_uncollated_copies)
784 {
785 if (!mch_print_blank_page())
786 goto print_fail;
787 }
788 }
789 if (settings.duplex && prtpos.file_line <= eap->line2)
790 ++page_count;
791
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100792 // Remember the position where the next page starts.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000793 page_prtpos = prtpos;
794 }
795
796 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
797 settings.jobname);
798 prt_message(IObuff);
799 }
800
801print_fail:
802 if (got_int || settings.user_abort)
803 {
804 sprintf((char *)IObuff, "%s", _("Printing aborted"));
805 prt_message(IObuff);
806 }
807 mch_print_end(&settings);
808
809print_fail_no_begin:
810 mch_print_cleanup();
811}
812
813/*
814 * Print one page line.
815 * Return the next column to print, or zero if the line is finished.
816 */
817 static colnr_T
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100818hardcopy_line(
819 prt_settings_T *psettings,
820 int page_line,
821 prt_pos_T *ppos)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000822{
823 colnr_T col;
824 char_u *line;
825 int need_break = FALSE;
826 int outputlen;
827 int tab_spaces;
828 long_u print_pos;
829#ifdef FEAT_SYN_HL
830 prt_text_attr_T attr;
831 int id;
832#endif
833
834 if (ppos->column == 0 || ppos->ff)
835 {
836 print_pos = 0;
837 tab_spaces = 0;
838 if (!ppos->ff && prt_use_number())
839 prt_line_number(psettings, page_line, ppos->file_line);
840 ppos->ff = FALSE;
841 }
842 else
843 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100844 // left over from wrap halfway a tab
Bram Moolenaar81366db2005-07-24 21:16:51 +0000845 print_pos = ppos->print_pos;
846 tab_spaces = ppos->lead_spaces;
847 }
848
849 mch_print_start_line(0, page_line);
850 line = ml_get(ppos->file_line);
851
852 /*
853 * Loop over the columns until the end of the file line or right margin.
854 */
855 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
856 {
857 outputlen = 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000858 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000859 outputlen = 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000860#ifdef FEAT_SYN_HL
861 /*
862 * syntax highlighting stuff.
863 */
864 if (psettings->do_syntax)
865 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +0000866 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, FALSE);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000867 if (id > 0)
868 id = syn_get_final_id(id);
869 else
870 id = 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100871 // Get the line again, a multi-line regexp may invalidate it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000872 line = ml_get(ppos->file_line);
873
874 if (id != current_syn_id)
875 {
876 current_syn_id = id;
877 prt_get_attr(id, &attr, psettings->modec);
878 prt_set_font(attr.bold, attr.italic, attr.underline);
879 prt_set_fg(attr.fg_color);
880 prt_set_bg(attr.bg_color);
881 }
882 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000883#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000884
885 /*
886 * Appropriately expand any tabs to spaces.
887 */
888 if (line[col] == TAB || tab_spaces != 0)
889 {
890 if (tab_spaces == 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200891#ifdef FEAT_VARTABS
892 tab_spaces = tabstop_padding(print_pos, curbuf->b_p_ts,
893 curbuf->b_p_vts_array);
894#else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000895 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200896#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000897
898 while (tab_spaces > 0)
899 {
900 need_break = mch_print_text_out((char_u *)" ", 1);
901 print_pos++;
902 tab_spaces--;
903 if (need_break)
904 break;
905 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100906 // Keep the TAB if we didn't finish it.
Bram Moolenaar81366db2005-07-24 21:16:51 +0000907 if (need_break && tab_spaces > 0)
908 break;
909 }
910 else if (line[col] == FF
911 && printer_opts[OPT_PRINT_FORMFEED].present
912 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
913 == 'y')
914 {
915 ppos->ff = TRUE;
916 need_break = 1;
917 }
918 else
919 {
920 need_break = mch_print_text_out(line + col, outputlen);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000921 if (has_mbyte)
922 print_pos += (*mb_ptr2cells)(line + col);
923 else
Bram Moolenaar81366db2005-07-24 21:16:51 +0000924 print_pos++;
925 }
926 }
927
928 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000929 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000930
931 /*
932 * Start next line of file if we clip lines, or have reached end of the
933 * line, unless we are doing a formfeed.
934 */
935 if (!ppos->ff
936 && (line[col] == NUL
937 || (printer_opts[OPT_PRINT_WRAP].present
938 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
939 == 'n')))
940 return 0;
941 return col;
942}
943
944# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
945
946/*
947 * PS printer stuff.
948 *
949 * Sources of information to help maintain the PS printing code:
950 *
951 * 1. PostScript Language Reference, 3rd Edition,
952 * Addison-Wesley, 1999, ISBN 0-201-37922-8
953 * 2. PostScript Language Program Design,
954 * Addison-Wesley, 1988, ISBN 0-201-14396-8
955 * 3. PostScript Tutorial and Cookbook,
956 * Addison Wesley, 1985, ISBN 0-201-10179-3
957 * 4. PostScript Language Document Structuring Conventions Specification,
958 * version 3.0,
959 * Adobe Technote 5001, 25th September 1992
960 * 5. PostScript Printer Description File Format Specification, Version 4.3,
961 * Adobe technote 5003, 9th February 1996
962 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
963 * Adobe Technote 5007, 7th October 1998
964 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
965 * Adobe Technote 5014, 8th October 1996
966 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
967 * Adoboe Technote 5094, 8th September, 2001
968 * 9. CJKV Information Processing, 2nd Edition,
969 * O'Reilly, 2002, ISBN 1-56592-224-7
970 *
971 * Some of these documents can be found in PDF form on Adobe's web site -
972 * http://www.adobe.com
973 */
974
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975#define PRT_PS_DEFAULT_DPI (72) // Default user space resolution
Bram Moolenaar81366db2005-07-24 21:16:51 +0000976#define PRT_PS_DEFAULT_FONTSIZE (10)
977#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
978
979struct prt_mediasize_S
980{
981 char *name;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100982 float width; // width and height in points for portrait
Bram Moolenaar81366db2005-07-24 21:16:51 +0000983 float height;
984};
985
K.Takataeeec2542021-06-02 13:28:16 +0200986#define PRT_MEDIASIZE_LEN ARRAY_LENGTH(prt_mediasize)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000987
988static struct prt_mediasize_S prt_mediasize[] =
989{
990 {"A4", 595.0, 842.0},
991 {"letter", 612.0, 792.0},
992 {"10x14", 720.0, 1008.0},
993 {"A3", 842.0, 1191.0},
994 {"A5", 420.0, 595.0},
995 {"B4", 729.0, 1032.0},
996 {"B5", 516.0, 729.0},
997 {"executive", 522.0, 756.0},
998 {"folio", 595.0, 935.0},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100999 {"ledger", 1224.0, 792.0}, // Yes, it is wider than taller!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001000 {"legal", 612.0, 1008.0},
1001 {"quarto", 610.0, 780.0},
1002 {"statement", 396.0, 612.0},
1003 {"tabloid", 792.0, 1224.0}
1004};
1005
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001006// PS font names, must be in Roman, Bold, Italic, Bold-Italic order
Bram Moolenaar81366db2005-07-24 21:16:51 +00001007struct prt_ps_font_S
1008{
1009 int wx;
1010 int uline_offset;
1011 int uline_width;
1012 int bbox_min_y;
1013 int bbox_max_y;
1014 char *(ps_fontname[4]);
1015};
1016
1017#define PRT_PS_FONT_ROMAN (0)
1018#define PRT_PS_FONT_BOLD (1)
1019#define PRT_PS_FONT_OBLIQUE (2)
1020#define PRT_PS_FONT_BOLDOBLIQUE (3)
1021
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001022// Standard font metrics for Courier family
Bram Moolenaar81366db2005-07-24 21:16:51 +00001023static struct prt_ps_font_S prt_ps_courier_font =
1024{
1025 600,
1026 -100, 50,
1027 -250, 805,
1028 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1029};
1030
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001031// Generic font metrics for multi-byte fonts
Bram Moolenaar81366db2005-07-24 21:16:51 +00001032static struct prt_ps_font_S prt_ps_mb_font =
1033{
1034 1000,
1035 -100, 50,
1036 -250, 805,
1037 {NULL, NULL, NULL, NULL}
1038};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001039
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001040// Pointer to current font set being used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001041static struct prt_ps_font_S* prt_ps_font;
1042
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001043// Structures to map user named encoding and mapping to PS equivalents for
1044// building CID font name
Bram Moolenaar81366db2005-07-24 21:16:51 +00001045struct prt_ps_encoding_S
1046{
1047 char *encoding;
1048 char *cmap_encoding;
1049 int needs_charset;
1050};
1051
1052struct prt_ps_charset_S
1053{
1054 char *charset;
1055 char *cmap_charset;
1056 int has_charset;
1057};
1058
Bram Moolenaar81366db2005-07-24 21:16:51 +00001059
1060#define CS_JIS_C_1978 (0x01)
1061#define CS_JIS_X_1983 (0x02)
1062#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001063#define CS_NEC (0x08)
1064#define CS_MSWINDOWS (0x10)
1065#define CS_CP932 (0x20)
1066#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001067#define CS_KANJITALK7 (0x80)
1068
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001069// Japanese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001070static struct prt_ps_encoding_S j_encodings[] =
1071{
1072 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001073 CS_NEC)},
1074 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1075 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1076 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001077 {"cp932", "RKSJ", CS_JIS_X_1983},
1078 {"ucs-2", "UCS2", CS_JIS_X_1990},
1079 {"utf-8", "UTF8" , CS_JIS_X_1990}
1080};
1081static struct prt_ps_charset_S j_charsets[] =
1082{
1083 {"JIS_C_1978", "78", CS_JIS_C_1978},
1084 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1085 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001086 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001087 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1088 {"CP932", "90ms", CS_JIS_X_1983},
1089 {"KANJITALK6", "83pv", CS_KANJITALK6},
1090 {"KANJITALK7", "90pv", CS_KANJITALK7}
1091};
1092
1093#define CS_GB_2312_80 (0x01)
1094#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001095#define CS_GBK2K (0x04)
1096#define CS_SC_MAC (0x08)
1097#define CS_GBT_90_MAC (0x10)
1098#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001099#define CS_SC_ISO10646 (0x40)
1100
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001101// Simplified Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001102static struct prt_ps_encoding_S sc_encodings[] =
1103{
1104 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1105 {"gb18030", NULL, CS_GBK2K},
1106 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001107 CS_GBT_90_MAC)},
1108 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001109 {"ucs-2", "UCS2", CS_SC_ISO10646},
1110 {"utf-8", "UTF8", CS_SC_ISO10646}
1111};
1112static struct prt_ps_charset_S sc_charsets[] =
1113{
1114 {"GB_2312-80", "GB", CS_GB_2312_80},
1115 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001116 {"MAC", "GBpc", CS_SC_MAC},
1117 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1118 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001119 {"GB18030", "GBK2K", CS_GBK2K},
1120 {"ISO10646", "UniGB", CS_SC_ISO10646}
1121};
1122
1123#define CS_CNS_PLANE_1 (0x01)
1124#define CS_CNS_PLANE_2 (0x02)
1125#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001126#define CS_B5 (0x08)
1127#define CS_ETEN (0x10)
1128#define CS_HK_GCCS (0x20)
1129#define CS_HK_SCS (0x40)
1130#define CS_HK_SCS_ETEN (0x80)
1131#define CS_MTHKL (0x100)
1132#define CS_MTHKS (0x200)
1133#define CS_DLHKL (0x400)
1134#define CS_DLHKS (0x800)
1135#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001136
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001137// Traditional Chinese encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001138static struct prt_ps_encoding_S tc_encodings[] =
1139{
1140 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1141 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001142 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1143 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1144 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001145 {"cp950", "B5", CS_B5},
1146 {"ucs-2", "UCS2", CS_TC_ISO10646},
1147 {"utf-8", "UTF8", CS_TC_ISO10646},
1148 {"utf-16", "UTF16", CS_TC_ISO10646},
1149 {"utf-32", "UTF32", CS_TC_ISO10646}
1150};
1151static struct prt_ps_charset_S tc_charsets[] =
1152{
1153 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1154 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1155 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001156 {"BIG5", NULL, CS_B5},
1157 {"CP950", NULL, CS_B5},
1158 {"ETEN", "ETen", CS_ETEN},
1159 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1160 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001161 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1162 {"MTHKL", "HKm471", CS_MTHKL},
1163 {"MTHKS", "HKm314", CS_MTHKS},
1164 {"DLHKL", "HKdla", CS_DLHKL},
1165 {"DLHKS", "HKdlb", CS_DLHKS},
1166 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1167};
1168
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001169#define CS_KR_X_1992 (0x01)
1170#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001171#define CS_KR_X_1992_MS (0x04)
1172#define CS_KR_ISO10646 (0x08)
1173
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001174// Korean encodings and charsets
Bram Moolenaar81366db2005-07-24 21:16:51 +00001175static struct prt_ps_encoding_S k_encodings[] =
1176{
1177 {"iso-2022-kr", NULL, CS_KR_X_1992},
1178 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1179 {"johab", "Johab", CS_KR_X_1992},
1180 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001181 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001182 {"cp949", "UHC", CS_KR_X_1992_MS},
1183 {"ucs-2", "UCS2", CS_KR_ISO10646},
1184 {"utf-8", "UTF8", CS_KR_ISO10646}
1185};
1186static struct prt_ps_charset_S k_charsets[] =
1187{
1188 {"KS_X_1992", "KSC", CS_KR_X_1992},
1189 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001190 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001191 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1192 {"CP949", "KSCms", CS_KR_X_1992_MS},
1193 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1194 {"ISO10646", "UniKS", CS_KR_ISO10646}
1195};
1196
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001197// Collections of encodings and charsets for multi-byte printing
Bram Moolenaar81366db2005-07-24 21:16:51 +00001198struct prt_ps_mbfont_S
1199{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001200 int num_encodings;
1201 struct prt_ps_encoding_S *encodings;
1202 int num_charsets;
1203 struct prt_ps_charset_S *charsets;
1204 char *ascii_enc;
1205 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001206};
1207
1208static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1209{
1210 {
K.Takataeeec2542021-06-02 13:28:16 +02001211 ARRAY_LENGTH(j_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001212 j_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001213 ARRAY_LENGTH(j_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001214 j_charsets,
1215 "jis_roman",
1216 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001217 },
1218 {
K.Takataeeec2542021-06-02 13:28:16 +02001219 ARRAY_LENGTH(sc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001220 sc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001221 ARRAY_LENGTH(sc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001222 sc_charsets,
1223 "gb_roman",
1224 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001225 },
1226 {
K.Takataeeec2542021-06-02 13:28:16 +02001227 ARRAY_LENGTH(tc_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001228 tc_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001229 ARRAY_LENGTH(tc_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001230 tc_charsets,
1231 "cns_roman",
1232 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001233 },
1234 {
K.Takataeeec2542021-06-02 13:28:16 +02001235 ARRAY_LENGTH(k_encodings),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001236 k_encodings,
K.Takataeeec2542021-06-02 13:28:16 +02001237 ARRAY_LENGTH(k_charsets),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001238 k_charsets,
1239 "ks_roman",
1240 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001241 }
1242};
Bram Moolenaar81366db2005-07-24 21:16:51 +00001243
1244struct prt_ps_resource_S
1245{
1246 char_u name[64];
1247 char_u filename[MAXPATHL + 1];
1248 int type;
1249 char_u title[256];
1250 char_u version[256];
1251};
1252
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001253// Types of PS resource file currently used
Bram Moolenaar81366db2005-07-24 21:16:51 +00001254#define PRT_RESOURCE_TYPE_PROCSET (0)
1255#define PRT_RESOURCE_TYPE_ENCODING (1)
1256#define PRT_RESOURCE_TYPE_CMAP (2)
1257
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001258// The PS prolog file version number has to match - if the prolog file is
1259// updated, increment the number in the file and here. Version checking was
1260// added as of VIM 6.2.
1261// The CID prolog file version number behaves as per PS prolog.
1262// Table of VIM and prolog versions:
1263//
1264// VIM Prolog CIDProlog
1265// 6.2 1.3
1266// 7.0 1.4 1.0
Bram Moolenaar81366db2005-07-24 21:16:51 +00001267#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1268#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1269
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001270// String versions of PS resource types - indexed by constants above so don't
1271// re-order!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001272static char *prt_resource_types[] =
1273{
1274 "procset",
1275 "encoding",
1276 "cmap"
1277};
1278
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001279// Strings to look for in a PS resource file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001280#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1281#define PRT_RESOURCE_RESOURCE "Resource-"
1282#define PRT_RESOURCE_PROCSET "ProcSet"
1283#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001284#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001285
1286
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001287// Data for table based DSC comment recognition, easy to extend if VIM needs to
1288// read more comments.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001289#define PRT_DSC_MISC_TYPE (-1)
1290#define PRT_DSC_TITLE_TYPE (1)
1291#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001292#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1293
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001294#define PRT_DSC_TITLE "%%Title:"
1295#define PRT_DSC_VERSION "%%Version:"
1296#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001297
1298struct prt_dsc_comment_S
1299{
1300 char *string;
1301 int len;
1302 int type;
1303};
1304
1305struct prt_dsc_line_S
1306{
1307 int type;
1308 char_u *string;
1309 int len;
1310};
1311
1312
1313#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1314static struct prt_dsc_comment_S prt_dsc_table[] =
1315{
1316 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1317 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1318 PRT_DSC_VERSION_TYPE},
1319 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1320 PRT_DSC_ENDCOMMENTS_TYPE}
1321};
1322
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001323static void prt_write_file_len(char_u *buffer, int bytes);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001324static int prt_next_dsc(struct prt_dsc_line_S *p_dsc_line);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001325
1326/*
1327 * Variables for the output PostScript file.
1328 */
1329static FILE *prt_ps_fd;
1330static int prt_file_error;
1331static char_u *prt_ps_file_name = NULL;
1332
1333/*
1334 * Various offsets and dimensions in default PostScript user space (points).
1335 * Used for text positioning calculations
1336 */
1337static float prt_page_width;
1338static float prt_page_height;
1339static float prt_left_margin;
1340static float prt_right_margin;
1341static float prt_top_margin;
1342static float prt_bottom_margin;
1343static float prt_line_height;
1344static float prt_first_line_height;
1345static float prt_char_width;
1346static float prt_number_width;
1347static float prt_bgcol_offset;
1348static float prt_pos_x_moveto = 0.0;
1349static float prt_pos_y_moveto = 0.0;
1350
1351/*
1352 * Various control variables used to decide when and how to change the
1353 * PostScript graphics state.
1354 */
1355static int prt_need_moveto;
1356static int prt_do_moveto;
1357static int prt_need_font;
1358static int prt_font;
1359static int prt_need_underline;
1360static int prt_underline;
1361static int prt_do_underline;
1362static int prt_need_fgcol;
1363static int prt_fgcol;
1364static int prt_need_bgcol;
1365static int prt_do_bgcol;
1366static int prt_bgcol;
1367static int prt_new_bgcol;
1368static int prt_attribute_change;
1369static float prt_text_run;
1370static int prt_page_num;
1371static int prt_bufsiz;
1372
1373/*
1374 * Variables controlling physical printing.
1375 */
1376static int prt_media;
1377static int prt_portrait;
1378static int prt_num_copies;
1379static int prt_duplex;
1380static int prt_tumble;
1381static int prt_collate;
1382
1383/*
1384 * Buffers used when generating PostScript output
1385 */
1386static char_u prt_line_buffer[257];
1387static garray_T prt_ps_buffer;
1388
Bram Moolenaar81366db2005-07-24 21:16:51 +00001389static int prt_do_conv;
1390static vimconv_T prt_conv;
1391
1392static int prt_out_mbyte;
1393static int prt_custom_cmap;
1394static char prt_cmap[80];
1395static int prt_use_courier;
1396static int prt_in_ascii;
1397static int prt_half_width;
1398static char *prt_ascii_encoding;
1399static char_u prt_hexchar[] = "0123456789abcdef";
Bram Moolenaar81366db2005-07-24 21:16:51 +00001400
1401 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001402prt_write_file_raw_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001403{
1404 if (!prt_file_error
1405 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1406 != (size_t)bytes)
1407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001408 emsg(_("E455: Error writing to PostScript output file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001409 prt_file_error = TRUE;
1410 }
1411}
1412
1413 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001414prt_write_file(char_u *buffer)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001415{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001416 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001417}
1418
1419 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001420prt_write_file_len(char_u *buffer, int bytes)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001421{
1422#ifdef EBCDIC
1423 ebcdic2ascii(buffer, bytes);
1424#endif
1425 prt_write_file_raw_len(buffer, bytes);
1426}
1427
1428/*
1429 * Write a string.
1430 */
1431 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001432prt_write_string(char *s)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001433{
1434 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1435 prt_write_file(prt_line_buffer);
1436}
1437
1438/*
1439 * Write an int and a space.
1440 */
1441 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001442prt_write_int(int i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001443{
1444 sprintf((char *)prt_line_buffer, "%d ", i);
1445 prt_write_file(prt_line_buffer);
1446}
1447
1448/*
1449 * Write a boolean and a space.
1450 */
1451 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001452prt_write_boolean(int b)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001453{
1454 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1455 prt_write_file(prt_line_buffer);
1456}
1457
1458/*
1459 * Write PostScript to re-encode and define the font.
1460 */
1461 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001462prt_def_font(
1463 char *new_name,
1464 char *encoding,
1465 int height,
1466 char *font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001467{
1468 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1469 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1470 prt_write_file(prt_line_buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001471 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001472 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001473 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1474 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001475 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1476 "/%s %d /_%s ffs\n", new_name, height, new_name);
1477 prt_write_file(prt_line_buffer);
1478}
1479
Bram Moolenaar81366db2005-07-24 21:16:51 +00001480/*
1481 * Write a line to define the CID font.
1482 */
1483 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001484prt_def_cidfont(char *new_name, int height, char *cidfont)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001485{
1486 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1487 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1488 prt_write_file(prt_line_buffer);
1489 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1490 "/%s %d /_%s ffs\n", new_name, height, new_name);
1491 prt_write_file(prt_line_buffer);
1492}
1493
1494/*
1495 * Write a line to define a duplicate of a CID font
1496 */
1497 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001498prt_dup_cidfont(char *original_name, char *new_name)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001499{
1500 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1501 "/%s %s d\n", new_name, original_name);
1502 prt_write_file(prt_line_buffer);
1503}
Bram Moolenaar81366db2005-07-24 21:16:51 +00001504
1505/*
1506 * Convert a real value into an integer and fractional part as integers, with
1507 * the fractional part being in the range [0,10^precision). The fractional part
1508 * is also rounded based on the precision + 1'th fractional digit.
1509 */
1510 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001511prt_real_bits(
1512 double real,
1513 int precision,
1514 int *pinteger,
1515 int *pfraction)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001516{
1517 int i;
1518 int integer;
1519 float fraction;
1520
1521 integer = (int)real;
1522 fraction = (float)(real - integer);
1523 if (real < (double)integer)
1524 fraction = -fraction;
1525 for (i = 0; i < precision; i++)
1526 fraction *= 10.0;
1527
1528 *pinteger = integer;
1529 *pfraction = (int)(fraction + 0.5);
1530}
1531
1532/*
1533 * Write a real and a space. Save bytes if real value has no fractional part!
1534 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1535 * what decimal point character to use, but PS always requires a '.'.
1536 */
1537 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001538prt_write_real(double val, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001539{
1540 int integer;
1541 int fraction;
1542
1543 prt_real_bits(val, prec, &integer, &fraction);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001544 // Emit integer part
Bram Moolenaar81366db2005-07-24 21:16:51 +00001545 sprintf((char *)prt_line_buffer, "%d", integer);
1546 prt_write_file(prt_line_buffer);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001547 // Only emit fraction if necessary
Bram Moolenaar81366db2005-07-24 21:16:51 +00001548 if (fraction != 0)
1549 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001550 // Remove any trailing zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001551 while ((fraction % 10) == 0)
1552 {
1553 prec--;
1554 fraction /= 10;
1555 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001556 // Emit fraction left padded with zeros
Bram Moolenaar81366db2005-07-24 21:16:51 +00001557 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1558 prt_write_file(prt_line_buffer);
1559 }
1560 sprintf((char *)prt_line_buffer, " ");
1561 prt_write_file(prt_line_buffer);
1562}
1563
1564/*
1565 * Write a line to define a numeric variable.
1566 */
1567 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001568prt_def_var(char *name, double value, int prec)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001569{
1570 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1571 "/%s ", name);
1572 prt_write_file(prt_line_buffer);
1573 prt_write_real(value, prec);
1574 sprintf((char *)prt_line_buffer, "d\n");
1575 prt_write_file(prt_line_buffer);
1576}
1577
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001578// Convert size from font space to user space at current font scale
Bram Moolenaar81366db2005-07-24 21:16:51 +00001579#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1580
1581 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001582prt_flush_buffer(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001583{
1584 if (prt_ps_buffer.ga_len > 0)
1585 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001586 // Any background color must be drawn first
Bram Moolenaar81366db2005-07-24 21:16:51 +00001587 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1588 {
1589 int r, g, b;
1590
1591 if (prt_do_moveto)
1592 {
1593 prt_write_real(prt_pos_x_moveto, 2);
1594 prt_write_real(prt_pos_y_moveto, 2);
1595 prt_write_string("m\n");
1596 prt_do_moveto = FALSE;
1597 }
1598
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001599 // Size of rect of background color on which text is printed
Bram Moolenaar81366db2005-07-24 21:16:51 +00001600 prt_write_real(prt_text_run, 2);
1601 prt_write_real(prt_line_height, 2);
1602
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001603 // Lastly add the color of the background
Bram Moolenaar81366db2005-07-24 21:16:51 +00001604 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1605 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1606 b = prt_new_bgcol & 0xff;
1607 prt_write_real(r / 255.0, 3);
1608 prt_write_real(g / 255.0, 3);
1609 prt_write_real(b / 255.0, 3);
1610 prt_write_string("bg\n");
1611 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001612 // Draw underlines before the text as it makes it slightly easier to
1613 // find the starting point.
Bram Moolenaar81366db2005-07-24 21:16:51 +00001614 if (prt_do_underline)
1615 {
1616 if (prt_do_moveto)
1617 {
1618 prt_write_real(prt_pos_x_moveto, 2);
1619 prt_write_real(prt_pos_y_moveto, 2);
1620 prt_write_string("m\n");
1621 prt_do_moveto = FALSE;
1622 }
1623
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001624 // Underline length of text run
Bram Moolenaar81366db2005-07-24 21:16:51 +00001625 prt_write_real(prt_text_run, 2);
1626 prt_write_string("ul\n");
1627 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001628 // Draw the text
1629 // Note: we write text out raw - EBCDIC conversion is handled in the
1630 // PostScript world via the font encoding vector.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001631 if (prt_out_mbyte)
1632 prt_write_string("<");
1633 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001634 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001635 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001636 if (prt_out_mbyte)
1637 prt_write_string(">");
1638 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001639 prt_write_string(")");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001640 // Add a moveto if need be and use the appropriate show procedure
Bram Moolenaar81366db2005-07-24 21:16:51 +00001641 if (prt_do_moveto)
1642 {
1643 prt_write_real(prt_pos_x_moveto, 2);
1644 prt_write_real(prt_pos_y_moveto, 2);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001645 // moveto and a show
Bram Moolenaar81366db2005-07-24 21:16:51 +00001646 prt_write_string("ms\n");
1647 prt_do_moveto = FALSE;
1648 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001649 else // Simple show
Bram Moolenaar81366db2005-07-24 21:16:51 +00001650 prt_write_string("s\n");
1651
1652 ga_clear(&prt_ps_buffer);
1653 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1654 }
1655}
1656
1657
1658 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001659prt_resource_name(char_u *filename, void *cookie)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001660{
1661 char_u *resource_filename = cookie;
1662
1663 if (STRLEN(filename) >= MAXPATHL)
1664 *resource_filename = NUL;
1665 else
1666 STRCPY(resource_filename, filename);
1667}
1668
1669 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001670prt_find_resource(char *name, struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001671{
Bram Moolenaard9462e32011-04-11 21:35:11 +02001672 char_u *buffer;
1673 int retval;
1674
1675 buffer = alloc(MAXPATHL + 1);
1676 if (buffer == NULL)
1677 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001678
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001679 vim_strncpy(resource->name, (char_u *)name, 63);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001680 // Look for named resource file in runtimepath
Bram Moolenaar81366db2005-07-24 21:16:51 +00001681 STRCPY(buffer, "print");
1682 add_pathsep(buffer);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001683 vim_strcat(buffer, (char_u *)name, MAXPATHL);
1684 vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001685 resource->filename[0] = NUL;
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001686 retval = (do_in_runtimepath(buffer, 0, prt_resource_name,
Bram Moolenaar81366db2005-07-24 21:16:51 +00001687 resource->filename)
1688 && resource->filename[0] != NUL);
Bram Moolenaard9462e32011-04-11 21:35:11 +02001689 vim_free(buffer);
1690 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001691}
1692
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001693// PS CR and LF characters have platform independent values
Bram Moolenaar81366db2005-07-24 21:16:51 +00001694#define PSLF (0x0a)
1695#define PSCR (0x0d)
1696
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001697// Static buffer to read initial comments in a resource file, some can have a
1698// couple of KB of comments!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001699#define PRT_FILE_BUFFER_LEN (2048)
1700struct prt_resfile_buffer_S
1701{
1702 char_u buffer[PRT_FILE_BUFFER_LEN];
1703 int len;
1704 int line_start;
1705 int line_end;
1706};
1707
1708static struct prt_resfile_buffer_S prt_resfile;
1709
1710 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001711prt_resfile_next_line(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001712{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001714
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001715 // Move to start of next line and then find end of line
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 idx = prt_resfile.line_end + 1;
1717 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001718 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001719 if (prt_resfile.buffer[idx] != PSLF && prt_resfile.buffer[idx] != PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001720 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001722 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001723 prt_resfile.line_start = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001724
Bram Moolenaar89d40322006-08-29 15:30:07 +00001725 while (idx < prt_resfile.len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001726 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001727 if (prt_resfile.buffer[idx] == PSLF || prt_resfile.buffer[idx] == PSCR)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001728 break;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001729 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001730 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001731 prt_resfile.line_end = idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001732
Bram Moolenaar89d40322006-08-29 15:30:07 +00001733 return (idx < prt_resfile.len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001734}
1735
1736 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001737prt_resfile_strncmp(int offset, char *string, int len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001738{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001739 // Force not equal if string is longer than remainder of line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001740 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001741 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001742
1743 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001744 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001745}
1746
1747 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001748prt_resfile_skip_nonws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001749{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001750 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001751
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 idx = prt_resfile.line_start + offset;
1753 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001754 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 if (isspace(prt_resfile.buffer[idx]))
1756 return idx - prt_resfile.line_start;
1757 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001758 }
1759 return -1;
1760}
1761
1762 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001763prt_resfile_skip_ws(int offset)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001764{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 int idx;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001766
Bram Moolenaar89d40322006-08-29 15:30:07 +00001767 idx = prt_resfile.line_start + offset;
1768 while (idx < prt_resfile.line_end)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001769 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 if (!isspace(prt_resfile.buffer[idx]))
1771 return idx - prt_resfile.line_start;
1772 idx++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001773 }
1774 return -1;
1775}
1776
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001777// prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1778// if a DSC comment is found, else false
Bram Moolenaar81366db2005-07-24 21:16:51 +00001779 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001780prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001781{
1782 int comment;
1783 int offset;
1784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785 // Move to start of next line
Bram Moolenaar81366db2005-07-24 21:16:51 +00001786 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001787 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001788
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001789 // DSC comments always start %%
Bram Moolenaar81366db2005-07-24 21:16:51 +00001790 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001791 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001792
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001793 // Find type of DSC comment
K.Takataeeec2542021-06-02 13:28:16 +02001794 for (comment = 0; comment < (int)ARRAY_LENGTH(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001795 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1796 prt_dsc_table[comment].len) == 0)
1797 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001798
K.Takataeeec2542021-06-02 13:28:16 +02001799 if (comment != ARRAY_LENGTH(prt_dsc_table))
Bram Moolenaar81366db2005-07-24 21:16:51 +00001800 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001801 // Return type of comment
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001802 p_dsc_line->type = prt_dsc_table[comment].type;
1803 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001804 }
1805 else
1806 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001807 // Unrecognised DSC comment, skip to ws after comment leader
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001808 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1809 offset = prt_resfile_skip_nonws(0);
1810 if (offset == -1)
1811 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001812 }
1813
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001814 // Skip ws to comment value
Bram Moolenaar81366db2005-07-24 21:16:51 +00001815 offset = prt_resfile_skip_ws(offset);
1816 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001817 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001818
1819 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1820 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1821
1822 return TRUE;
1823}
1824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001825/*
1826 * Improved hand crafted parser to get the type, title, and version number of a
Bram Moolenaar81366db2005-07-24 21:16:51 +00001827 * PS resource file so the file details can be added to the DSC header comments.
1828 */
1829 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001830prt_open_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001831{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001832 int offset;
1833 int seen_all;
1834 int seen_title;
1835 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001836 FILE *fd_resource;
1837 struct prt_dsc_line_S dsc_line;
1838
1839 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1840 if (fd_resource == NULL)
1841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001842 semsg(_("E624: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001843 return FALSE;
1844 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001845 CLEAR_FIELD(prt_resfile.buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001846
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001847 // Parse first line to ensure valid resource file
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001848 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001849 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001850 if (ferror(fd_resource))
1851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001852 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001853 resource->filename);
1854 fclose(fd_resource);
1855 return FALSE;
1856 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001857 fclose(fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001858
1859 prt_resfile.line_end = -1;
1860 prt_resfile.line_start = 0;
1861 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001862 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001863
1864 offset = 0;
1865
1866 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001867 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001869 semsg(_("E618: file \"%s\" is not a PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001870 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001871 return FALSE;
1872 }
1873
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001874 // Skip over any version numbers and following ws
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001875 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001876 offset = prt_resfile_skip_nonws(offset);
1877 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001878 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001879 offset = prt_resfile_skip_ws(offset);
1880 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001881 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001882
1883 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001884 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001886 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001887 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001888 return FALSE;
1889 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001890 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001891
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001892 // Decide type of resource in the file
Bram Moolenaar81366db2005-07-24 21:16:51 +00001893 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001894 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001895 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1896 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001897 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001898 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1899 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001900 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001901 resource->type = PRT_RESOURCE_TYPE_CMAP;
1902 else
1903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001905 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001906 return FALSE;
1907 }
1908
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001909 // Look for title and version of resource
Bram Moolenaar81366db2005-07-24 21:16:51 +00001910 resource->title[0] = '\0';
1911 resource->version[0] = '\0';
1912 seen_title = FALSE;
1913 seen_version = FALSE;
1914 seen_all = FALSE;
1915 while (!seen_all && prt_next_dsc(&dsc_line))
1916 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001917 switch (dsc_line.type)
1918 {
1919 case PRT_DSC_TITLE_TYPE:
1920 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
1921 seen_title = TRUE;
1922 if (seen_version)
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_VERSION_TYPE:
1927 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
1928 seen_version = TRUE;
1929 if (seen_title)
1930 seen_all = TRUE;
1931 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001932
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001933 case PRT_DSC_ENDCOMMENTS_TYPE:
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001934 // Won't find title or resource after this comment, stop searching
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001935 seen_all = TRUE;
1936 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001937
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001938 case PRT_DSC_MISC_TYPE:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001939 // Not interested in whatever comment this line had
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001940 break;
1941 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00001942 }
1943
1944 if (!seen_title || !seen_version)
1945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001946 semsg(_("E619: file \"%s\" is not a supported PostScript resource file"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001947 resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001948 return FALSE;
1949 }
1950
Bram Moolenaar81366db2005-07-24 21:16:51 +00001951 return TRUE;
1952}
1953
1954 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001955prt_check_resource(struct prt_ps_resource_S *resource, char_u *version)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001956{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001957 // Version number m.n should match, the revision number does not matter
Bram Moolenaar81366db2005-07-24 21:16:51 +00001958 if (STRNCMP(resource->version, version, STRLEN(version)))
1959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001960 semsg(_("E621: \"%s\" resource file has wrong version"),
Bram Moolenaar81366db2005-07-24 21:16:51 +00001961 resource->name);
1962 return FALSE;
1963 }
1964
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001965 // Other checks to be added as needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00001966 return TRUE;
1967}
1968
1969 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001970prt_dsc_start(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001971{
1972 prt_write_string("%!PS-Adobe-3.0\n");
1973}
1974
1975 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001976prt_dsc_noarg(char *comment)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001977{
1978 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1979 "%%%%%s\n", comment);
1980 prt_write_file(prt_line_buffer);
1981}
1982
1983 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001984prt_dsc_textline(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001985{
1986 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1987 "%%%%%s: %s\n", comment, text);
1988 prt_write_file(prt_line_buffer);
1989}
1990
1991 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001992prt_dsc_text(char *comment, char *text)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001993{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001994 // TODO - should scan 'text' for any chars needing escaping!
Bram Moolenaar81366db2005-07-24 21:16:51 +00001995 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1996 "%%%%%s: (%s)\n", comment, text);
1997 prt_write_file(prt_line_buffer);
1998}
1999
2000#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2001
2002 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002003prt_dsc_ints(char *comment, int count, int *ints)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002004{
2005 int i;
2006
2007 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2008 "%%%%%s:", comment);
2009 prt_write_file(prt_line_buffer);
2010
2011 for (i = 0; i < count; i++)
2012 {
2013 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2014 prt_write_file(prt_line_buffer);
2015 }
2016
2017 prt_write_string("\n");
2018}
2019
2020 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002021prt_dsc_resources(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022 char *comment, // if NULL add to previous
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002023 char *type,
2024 char *string)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002025{
2026 if (comment != NULL)
2027 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2028 "%%%%%s: %s", comment, type);
2029 else
2030 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2031 "%%%%+ %s", type);
2032 prt_write_file(prt_line_buffer);
2033
2034 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2035 " %s\n", string);
2036 prt_write_file(prt_line_buffer);
2037}
2038
2039 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002040prt_dsc_font_resource(char *resource, struct prt_ps_font_S *ps_font)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002041{
2042 int i;
2043
2044 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002045 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002046 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002047 if (ps_font->ps_fontname[i] != NULL)
2048 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002049}
2050
2051 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002052prt_dsc_requirements(
2053 int duplex,
2054 int tumble,
2055 int collate,
2056 int color,
2057 int num_copies)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002058{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002059 // Only output the comment if we need to.
2060 // Note: tumble is ignored if we are not duplexing
Bram Moolenaar81366db2005-07-24 21:16:51 +00002061 if (!(duplex || collate || color || (num_copies > 1)))
2062 return;
2063
2064 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2065 prt_write_file(prt_line_buffer);
2066
2067 if (duplex)
2068 {
2069 prt_write_string(" duplex");
2070 if (tumble)
2071 prt_write_string("(tumble)");
2072 }
2073 if (collate)
2074 prt_write_string(" collate");
2075 if (color)
2076 prt_write_string(" color");
2077 if (num_copies > 1)
2078 {
2079 prt_write_string(" numcopies(");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002080 // Note: no space wanted so don't use prt_write_int()
Bram Moolenaar81366db2005-07-24 21:16:51 +00002081 sprintf((char *)prt_line_buffer, "%d", num_copies);
2082 prt_write_file(prt_line_buffer);
2083 prt_write_string(")");
2084 }
2085 prt_write_string("\n");
2086}
2087
2088 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002089prt_dsc_docmedia(
2090 char *paper_name,
2091 double width,
2092 double height,
2093 double weight,
2094 char *colour,
2095 char *type)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002096{
2097 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2098 "%%%%DocumentMedia: %s ", paper_name);
2099 prt_write_file(prt_line_buffer);
2100 prt_write_real(width, 2);
2101 prt_write_real(height, 2);
2102 prt_write_real(weight, 2);
2103 if (colour == NULL)
2104 prt_write_string("()");
2105 else
2106 prt_write_string(colour);
2107 prt_write_string(" ");
2108 if (type == NULL)
2109 prt_write_string("()");
2110 else
2111 prt_write_string(type);
2112 prt_write_string("\n");
2113}
2114
2115 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002116mch_print_cleanup(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002117{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002118 if (prt_out_mbyte)
2119 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002120 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002121
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 // Free off all CID font names created, but first clear duplicate
2123 // pointers to the same string (when the same font is used for more than
2124 // one style).
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002125 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2126 {
2127 if (prt_ps_mb_font.ps_fontname[i] != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002128 VIM_CLEAR(prt_ps_mb_font.ps_fontname[i]);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002129 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002130 }
2131
2132 if (prt_do_conv)
2133 {
2134 convert_setup(&prt_conv, NULL, NULL);
2135 prt_do_conv = FALSE;
2136 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002137 if (prt_ps_fd != NULL)
2138 {
2139 fclose(prt_ps_fd);
2140 prt_ps_fd = NULL;
2141 prt_file_error = FALSE;
2142 }
2143 if (prt_ps_file_name != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002144 VIM_CLEAR(prt_ps_file_name);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002145}
2146
2147 static float
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002148to_device_units(int idx, double physsize, int def_number)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002149{
2150 float ret;
2151 int u;
2152 int nr;
2153
2154 u = prt_get_unit(idx);
2155 if (u == PRT_UNIT_NONE)
2156 {
2157 u = PRT_UNIT_PERC;
2158 nr = def_number;
2159 }
2160 else
2161 nr = printer_opts[idx].number;
2162
2163 switch (u)
2164 {
2165 case PRT_UNIT_INCH:
2166 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2167 break;
2168 case PRT_UNIT_MM:
2169 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2170 break;
2171 case PRT_UNIT_POINT:
2172 ret = (float)nr;
2173 break;
2174 case PRT_UNIT_PERC:
2175 default:
2176 ret = (float)(physsize * nr) / 100;
2177 break;
2178 }
2179
2180 return ret;
2181}
2182
2183/*
2184 * Calculate margins for given width and height from printoptions settings.
2185 */
2186 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002187prt_page_margins(
2188 double width,
2189 double height,
2190 double *left,
2191 double *right,
2192 double *top,
2193 double *bottom)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002194{
2195 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2196 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2197 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2198 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2199}
2200
2201 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002202prt_font_metrics(int font_scale)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002203{
2204 prt_line_height = (float)font_scale;
2205 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2206}
2207
2208
2209 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002210prt_get_cpl(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002211{
2212 if (prt_use_number())
2213 {
2214 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002215 // If we are outputting multi-byte characters then line numbers will be
2216 // printed with half width characters
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002217 if (prt_out_mbyte)
2218 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002219 prt_left_margin += prt_number_width;
2220 }
2221 else
2222 prt_number_width = 0.0;
2223
2224 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2225}
2226
Bram Moolenaar81366db2005-07-24 21:16:51 +00002227 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002228prt_build_cid_fontname(int font, char_u *name, int name_len)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002229{
2230 char *fontname;
2231
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002232 fontname = alloc(name_len + 1);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002233 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002234 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002235 vim_strncpy((char_u *)fontname, name, name_len);
2236 prt_ps_mb_font.ps_fontname[font] = fontname;
2237
2238 return TRUE;
2239}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002240
2241/*
2242 * Get number of lines of text that fit on a page (excluding the header).
2243 */
2244 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002245prt_get_lpp(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002246{
2247 int lpp;
2248
2249 /*
2250 * Calculate offset to lower left corner of background rect based on actual
2251 * font height (based on its bounding box) and the line height, handling the
2252 * case where the font height can exceed the line height.
2253 */
2254 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2255 prt_ps_font->bbox_min_y);
2256 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2257 {
2258 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2259 (1000.0 - (prt_ps_font->bbox_max_y -
2260 prt_ps_font->bbox_min_y)) / 2);
2261 }
2262
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002263 // Get height for topmost line based on background rect offset.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002264 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2265
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002266 // Calculate lpp
Bram Moolenaar81366db2005-07-24 21:16:51 +00002267 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2268
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002269 // Adjust top margin if there is a header
Bram Moolenaar81366db2005-07-24 21:16:51 +00002270 prt_top_margin -= prt_line_height * prt_header_height();
2271
2272 return lpp - prt_header_height();
2273}
2274
Bram Moolenaar81366db2005-07-24 21:16:51 +00002275 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002276prt_match_encoding(
2277 char *p_encoding,
2278 struct prt_ps_mbfont_S *p_cmap,
2279 struct prt_ps_encoding_S **pp_mbenc)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002280{
2281 int mbenc;
2282 int enc_len;
2283 struct prt_ps_encoding_S *p_mbenc;
2284
2285 *pp_mbenc = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002286 // Look for recognised encoding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002287 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002288 p_mbenc = p_cmap->encodings;
2289 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2290 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002291 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2292 {
2293 *pp_mbenc = p_mbenc;
2294 return TRUE;
2295 }
2296 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002297 }
2298 return FALSE;
2299}
2300
2301 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002302prt_match_charset(
2303 char *p_charset,
2304 struct prt_ps_mbfont_S *p_cmap,
2305 struct prt_ps_charset_S **pp_mbchar)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002306{
2307 int mbchar;
2308 int char_len;
2309 struct prt_ps_charset_S *p_mbchar;
2310
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002311 // Look for recognised character set, using default if one is not given
Bram Moolenaar81366db2005-07-24 21:16:51 +00002312 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002313 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002314 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002315 p_mbchar = p_cmap->charsets;
2316 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2317 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002318 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2319 {
2320 *pp_mbchar = p_mbchar;
2321 return TRUE;
2322 }
2323 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002324 }
2325 return FALSE;
2326}
Bram Moolenaar81366db2005-07-24 21:16:51 +00002327
Bram Moolenaar81366db2005-07-24 21:16:51 +00002328 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002329mch_print_init(
2330 prt_settings_T *psettings,
2331 char_u *jobname,
2332 int forceit UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002333{
2334 int i;
2335 char *paper_name;
2336 int paper_strlen;
2337 int fontsize;
2338 char_u *p;
2339 double left;
2340 double right;
2341 double top;
2342 double bottom;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002343 int props;
2344 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002345 char_u *p_encoding;
2346 struct prt_ps_encoding_S *p_mbenc;
2347 struct prt_ps_encoding_S *p_mbenc_first;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002348 struct prt_ps_charset_S *p_mbchar = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002349
2350#if 0
2351 /*
2352 * TODO:
2353 * If "forceit" is false: pop up a dialog to select:
2354 * - printer name
2355 * - copies
2356 * - collated/uncollated
2357 * - duplex off/long side/short side
2358 * - paper size
2359 * - portrait/landscape
2360 * - font size
2361 *
2362 * If "forceit" is true: use the default printer and settings
2363 */
2364 if (forceit)
2365 s_pd.Flags |= PD_RETURNDEFAULT;
2366#endif
2367
2368 /*
2369 * Set up font and encoding.
2370 */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002371 p_encoding = enc_skip(p_penc);
2372 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002373 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002374
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002375 // Look for a multi-byte font that matches the encoding and character set.
2376 // Only look if multi-byte character set is defined, or using multi-byte
2377 // encoding other than Unicode. This is because a Unicode encoding does not
2378 // uniquely identify a CJK character set to use.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002379 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002380 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002381 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002382 {
Bram Moolenaarec45c4a2015-04-15 14:27:49 +02002383 int cmap_first = 0;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002384
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002385 p_mbenc_first = NULL;
K.Takataeeec2542021-06-02 13:28:16 +02002386 for (cmap = 0; cmap < (int)ARRAY_LENGTH(prt_ps_mbfonts); cmap++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002387 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002388 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002389 {
2390 if (p_mbenc_first == NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002391 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002392 p_mbenc_first = p_mbenc;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002393 cmap_first = cmap;
2394 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002395 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002396 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002397 break;
2398 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002399
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002400 // Use first encoding matched if no charset matched
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002401 if (p_mbchar == NULL && p_mbenc_first != NULL)
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002402 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002403 p_mbenc = p_mbenc_first;
Bram Moolenaar7c94ce92015-04-13 14:45:27 +02002404 cmap = cmap_first;
2405 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002406 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002407
2408 prt_out_mbyte = (p_mbenc != NULL);
2409 if (prt_out_mbyte)
2410 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002411 // Build CMap name - will be same for all multi-byte fonts used
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002412 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002413
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002414 prt_custom_cmap = (p_mbchar == NULL);
2415 if (!prt_custom_cmap)
2416 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002417 // Check encoding and character set are compatible
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002418 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 emsg(_("E673: Incompatible multi-byte encoding and character set."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002421 return FALSE;
2422 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002423
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002424 // Add charset name if not empty
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002425 if (p_mbchar->cmap_charset != NULL)
2426 {
2427 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002428 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002429 STRCAT(prt_cmap, "-");
2430 }
2431 }
2432 else
2433 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002434 // Add custom CMap character set name
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002435 if (*p_pmcs == NUL)
2436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002437 emsg(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002438 return FALSE;
2439 }
2440 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2441 STRCAT(prt_cmap, "-");
2442 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002443
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002444 // CMap name ends with (optional) encoding name and -H for horizontal
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002445 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002446 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002447 {
2448 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2449 STRCAT(prt_cmap, "-");
2450 }
2451 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002452
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002453 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002455 emsg(_("E675: No default font specified for multi-byte printing."));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002456 return FALSE;
2457 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002458
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002459 // Derive CID font names with fallbacks if not defined
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002460 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2461 mbfont_opts[OPT_MBFONT_REGULAR].string,
2462 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2463 return FALSE;
2464 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2465 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2466 mbfont_opts[OPT_MBFONT_BOLD].string,
2467 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2468 return FALSE;
2469 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2470 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2471 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2472 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2473 return FALSE;
2474 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2475 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002476 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2477 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002478 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002479
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002480 // Check if need to use Courier for ASCII code range, and if so pick up
2481 // the encoding to use
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002482 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2483 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2484 if (prt_use_courier)
2485 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002486 // Use national ASCII variant unless ASCII wanted
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002487 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2488 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2489 prt_ascii_encoding = "ascii";
2490 else
2491 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2492 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002493
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002494 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002495 }
2496 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00002497 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002498 prt_use_courier = FALSE;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002499 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002500 }
2501
2502 /*
2503 * Find the size of the paper and set the margins.
2504 */
2505 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2506 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2507 if (printer_opts[OPT_PRINT_PAPER].present)
2508 {
2509 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2510 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2511 }
2512 else
2513 {
2514 paper_name = "A4";
2515 paper_strlen = 2;
2516 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002517 for (i = 0; i < (int)PRT_MEDIASIZE_LEN; ++i)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002518 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2519 && STRNICMP(prt_mediasize[i].name, paper_name,
2520 paper_strlen) == 0)
2521 break;
2522 if (i == PRT_MEDIASIZE_LEN)
2523 i = 0;
2524 prt_media = i;
2525
2526 /*
2527 * Set PS pagesize based on media dimensions and print orientation.
2528 * Note: Media and page sizes have defined meanings in PostScript and should
2529 * be kept distinct. Media is the paper (or transparency, or ...) that is
2530 * printed on, whereas the page size is the area that the PostScript
2531 * interpreter renders into.
2532 */
2533 if (prt_portrait)
2534 {
2535 prt_page_width = prt_mediasize[i].width;
2536 prt_page_height = prt_mediasize[i].height;
2537 }
2538 else
2539 {
2540 prt_page_width = prt_mediasize[i].height;
2541 prt_page_height = prt_mediasize[i].width;
2542 }
2543
2544 /*
2545 * Set PS page margins based on the PS pagesize, not the mediasize - this
2546 * needs to be done before the cpl and lpp are calculated.
2547 */
2548 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2549 &bottom);
2550 prt_left_margin = (float)left;
2551 prt_right_margin = (float)right;
2552 prt_top_margin = (float)top;
2553 prt_bottom_margin = (float)bottom;
2554
2555 /*
2556 * Set up the font size.
2557 */
2558 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2559 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2560 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2561 fontsize = atoi((char *)p + 2);
2562 prt_font_metrics(fontsize);
2563
2564 /*
2565 * Return the number of characters per line, and lines per page for the
2566 * generic print code.
2567 */
2568 psettings->chars_per_line = prt_get_cpl();
2569 psettings->lines_per_page = prt_get_lpp();
2570
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002571 // Catch margin settings that leave no space for output!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002572 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2573 return FAIL;
2574
2575 /*
2576 * Sort out the number of copies to be printed. PS by default will do
2577 * uncollated copies for you, so once we know how many uncollated copies are
2578 * wanted cache it away and lie to the generic code that we only want one
2579 * uncollated copy.
2580 */
2581 psettings->n_collated_copies = 1;
2582 psettings->n_uncollated_copies = 1;
2583 prt_num_copies = 1;
2584 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2585 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2586 if (prt_collate)
2587 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002588 // TODO: Get number of collated copies wanted.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002589 psettings->n_collated_copies = 1;
2590 }
2591 else
2592 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002593 // TODO: Get number of uncollated copies wanted and update the cached
2594 // count.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002595 prt_num_copies = 1;
2596 }
2597
2598 psettings->jobname = jobname;
2599
2600 /*
2601 * Set up printer duplex and tumble based on Duplex option setting - default
2602 * is long sided duplex printing (i.e. no tumble).
2603 */
2604 prt_duplex = TRUE;
2605 prt_tumble = FALSE;
2606 psettings->duplex = 1;
2607 if (printer_opts[OPT_PRINT_DUPLEX].present)
2608 {
2609 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2610 {
2611 prt_duplex = FALSE;
2612 psettings->duplex = 0;
2613 }
2614 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2615 == 0)
2616 prt_tumble = TRUE;
2617 }
2618
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002619 // For now user abort not supported
Bram Moolenaar81366db2005-07-24 21:16:51 +00002620 psettings->user_abort = 0;
2621
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002622 // If the user didn't specify a file name, use a temp file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002623 if (psettings->outfile == NULL)
2624 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002625 prt_ps_file_name = vim_tempname('p', TRUE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002626 if (prt_ps_file_name == NULL)
2627 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002628 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002629 return FAIL;
2630 }
2631 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2632 }
2633 else
2634 {
2635 p = expand_env_save(psettings->outfile);
2636 if (p != NULL)
2637 {
2638 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2639 vim_free(p);
2640 }
2641 }
2642 if (prt_ps_fd == NULL)
2643 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00002644 emsg(_(e_cant_open_postscript_output_file));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002645 mch_print_cleanup();
2646 return FAIL;
2647 }
2648
2649 prt_bufsiz = psettings->chars_per_line;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002650 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002651 prt_bufsiz *= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002652 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2653
2654 prt_page_num = 0;
2655
2656 prt_attribute_change = FALSE;
2657 prt_need_moveto = FALSE;
2658 prt_need_font = FALSE;
2659 prt_need_fgcol = FALSE;
2660 prt_need_bgcol = FALSE;
2661 prt_need_underline = FALSE;
2662
2663 prt_file_error = FALSE;
2664
2665 return OK;
2666}
2667
2668 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002669prt_add_resource(struct prt_ps_resource_S *resource)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002670{
2671 FILE* fd_resource;
2672 char_u resource_buffer[512];
2673 size_t bytes_read;
2674
2675 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2676 if (fd_resource == NULL)
2677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 semsg(_("E456: Can't open file \"%s\""), resource->filename);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002679 return FALSE;
2680 }
2681 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2682 (char *)resource->title);
2683
2684 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2685
2686 for (;;)
2687 {
2688 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2689 sizeof(resource_buffer), fd_resource);
2690 if (ferror(fd_resource))
2691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002692 semsg(_("E457: Can't read PostScript resource file \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002693 resource->filename);
2694 fclose(fd_resource);
2695 return FALSE;
2696 }
2697 if (bytes_read == 0)
2698 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002699 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002700 if (prt_file_error)
2701 {
2702 fclose(fd_resource);
2703 return FALSE;
2704 }
2705 }
2706 fclose(fd_resource);
2707
2708 prt_dsc_noarg("EndDocument");
2709
2710 prt_dsc_noarg("EndResource");
2711
2712 return TRUE;
2713}
2714
2715 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002716mch_print_begin(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002717{
Bram Moolenaar81366db2005-07-24 21:16:51 +00002718 int bbox[4];
Bram Moolenaar81366db2005-07-24 21:16:51 +00002719 double left;
2720 double right;
2721 double top;
2722 double bottom;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002723 struct prt_ps_resource_S *res_prolog;
2724 struct prt_ps_resource_S *res_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002725 char buffer[256];
2726 char_u *p_encoding;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002727 struct prt_ps_resource_S *res_cidfont;
2728 struct prt_ps_resource_S *res_cmap;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002729 int retval = FALSE;
2730
2731 res_prolog = (struct prt_ps_resource_S *)
2732 alloc(sizeof(struct prt_ps_resource_S));
2733 res_encoding = (struct prt_ps_resource_S *)
2734 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002735 res_cidfont = (struct prt_ps_resource_S *)
2736 alloc(sizeof(struct prt_ps_resource_S));
2737 res_cmap = (struct prt_ps_resource_S *)
2738 alloc(sizeof(struct prt_ps_resource_S));
Bram Moolenaard9462e32011-04-11 21:35:11 +02002739 if (res_prolog == NULL || res_encoding == NULL
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002740 || res_cidfont == NULL || res_cmap == NULL)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002741 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002742
2743 /*
2744 * PS DSC Header comments - no PS code!
2745 */
2746 prt_dsc_start();
2747 prt_dsc_textline("Title", (char *)psettings->jobname);
2748 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002749 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002750 prt_dsc_textline("For", buffer);
2751 prt_dsc_textline("Creator", VIM_VERSION_LONG);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002752 // Note: to ensure Clean8bit I don't think we can use LC_TIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02002753
2754 prt_dsc_textline("CreationDate", get_ctime(time(NULL), FALSE));
Bram Moolenaar81366db2005-07-24 21:16:51 +00002755 prt_dsc_textline("DocumentData", "Clean8Bit");
2756 prt_dsc_textline("Orientation", "Portrait");
2757 prt_dsc_atend("Pages");
2758 prt_dsc_textline("PageOrder", "Ascend");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002759 // The bbox does not change with orientation - it is always in the default
2760 // user coordinate system! We have to recalculate right and bottom
2761 // coordinates based on the font metrics for the bbox to be accurate.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002762 prt_page_margins(prt_mediasize[prt_media].width,
2763 prt_mediasize[prt_media].height,
2764 &left, &right, &top, &bottom);
2765 bbox[0] = (int)left;
2766 if (prt_portrait)
2767 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002768 // In portrait printing the fixed point is the top left corner so we
2769 // derive the bbox from that point. We have the expected cpl chars
2770 // across the media and lpp lines down the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002771 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2772 * prt_line_height);
2773 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2774 + 0.5);
2775 bbox[3] = (int)(top + 0.5);
2776 }
2777 else
2778 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002779 // In landscape printing the fixed point is the bottom left corner so we
2780 // derive the bbox from that point. We have lpp chars across the media
2781 // and cpl lines up the media.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002782 bbox[1] = (int)bottom;
2783 bbox[2] = (int)(left + ((psettings->lines_per_page
2784 + prt_header_height()) * prt_line_height) + 0.5);
2785 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2786 + 0.5);
2787 }
2788 prt_dsc_ints("BoundingBox", 4, bbox);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002789 // The media width and height does not change with landscape printing!
Bram Moolenaar81366db2005-07-24 21:16:51 +00002790 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2791 prt_mediasize[prt_media].width,
2792 prt_mediasize[prt_media].height,
2793 (double)0, NULL, NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002794 // Define fonts needed
Bram Moolenaar81366db2005-07-24 21:16:51 +00002795 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002796 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002797 if (prt_out_mbyte)
2798 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002799 prt_dsc_font_resource((prt_use_courier ? NULL
2800 : "DocumentNeededResources"), &prt_ps_mb_font);
2801 if (!prt_custom_cmap)
2802 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002803 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002804
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002805 // Search for external resources VIM supplies
Bram Moolenaard9462e32011-04-11 21:35:11 +02002806 if (!prt_find_resource("prolog", res_prolog))
Bram Moolenaar81366db2005-07-24 21:16:51 +00002807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002808 emsg(_("E456: Can't find PostScript resource file \"prolog.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002809 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002810 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002811 if (!prt_open_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002812 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002813 if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002814 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002815 if (prt_out_mbyte)
2816 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002817 // Look for required version of multi-byte printing procset
Bram Moolenaard9462e32011-04-11 21:35:11 +02002818 if (!prt_find_resource("cidfont", res_cidfont))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002820 emsg(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
Bram Moolenaar0a383962014-11-27 17:37:57 +01002821 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002822 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002823 if (!prt_open_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002824 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002825 if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002826 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002827 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002828
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002829 // Find an encoding to use for printing.
2830 // Check 'printencoding'. If not set or not found, then use 'encoding'. If
2831 // that cannot be found then default to "latin1".
2832 // Note: VIM specific encoding header is always skipped.
Bram Moolenaar81366db2005-07-24 21:16:51 +00002833 if (!prt_out_mbyte)
2834 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002835 p_encoding = enc_skip(p_penc);
2836 if (*p_encoding == NUL
Bram Moolenaard9462e32011-04-11 21:35:11 +02002837 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002838 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002839 // 'printencoding' not set or not supported - find alternate
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002840 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002841
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002842 p_encoding = enc_skip(p_enc);
2843 props = enc_canon_props(p_encoding);
2844 if (!(props & ENC_8BIT)
Bram Moolenaard9462e32011-04-11 21:35:11 +02002845 || !prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002846 // 8-bit 'encoding' is not supported
2847 {
2848 // Use latin1 as default printing encoding
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002849 p_encoding = (char_u *)"latin1";
Bram Moolenaard9462e32011-04-11 21:35:11 +02002850 if (!prt_find_resource((char *)p_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002852 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002853 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002854 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002855 }
2856 }
2857 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002858 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002859 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002860 // For the moment there are no checks on encoding resource files to
2861 // perform
Bram Moolenaar81366db2005-07-24 21:16:51 +00002862 }
2863 else
2864 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002865 p_encoding = enc_skip(p_penc);
2866 if (*p_encoding == NUL)
2867 p_encoding = enc_skip(p_enc);
2868 if (prt_use_courier)
2869 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002870 // Include ASCII range encoding vector
Bram Moolenaard9462e32011-04-11 21:35:11 +02002871 if (!prt_find_resource(prt_ascii_encoding, res_encoding))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002874 prt_ascii_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002875 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002876 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002877 if (!prt_open_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002878 goto theend;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002879 // For the moment there are no checks on encoding resource files to
2880 // perform
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002881 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002882 }
2883
2884 prt_conv.vc_type = CONV_NONE;
2885 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002886 // Set up encoding conversion if required
Bram Moolenaar81366db2005-07-24 21:16:51 +00002887 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
2888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002889 semsg(_("E620: Unable to convert to print encoding \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002890 p_encoding);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002891 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002892 }
2893 prt_do_conv = TRUE;
2894 }
2895 prt_do_conv = prt_conv.vc_type != CONV_NONE;
2896
2897 if (prt_out_mbyte && prt_custom_cmap)
2898 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002899 // Find user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002900 if (!prt_find_resource(prt_cmap, res_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002902 semsg(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00002903 prt_cmap);
Bram Moolenaar0a383962014-11-27 17:37:57 +01002904 goto theend;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002905 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02002906 if (!prt_open_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002907 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002908 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002909
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002910 // List resources supplied
Bram Moolenaard9462e32011-04-11 21:35:11 +02002911 STRCPY(buffer, res_prolog->title);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002912 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002913 STRCAT(buffer, res_prolog->version);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002914 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002915 if (prt_out_mbyte)
2916 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002917 STRCPY(buffer, res_cidfont->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002918 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002919 STRCAT(buffer, res_cidfont->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002920 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002921
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002922 if (prt_custom_cmap)
2923 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002924 STRCPY(buffer, res_cmap->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002925 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002926 STRCAT(buffer, res_cmap->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002927 prt_dsc_resources(NULL, "cmap", buffer);
2928 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002929 }
2930 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002931 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002932 STRCPY(buffer, res_encoding->title);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002933 STRCAT(buffer, " ");
Bram Moolenaard9462e32011-04-11 21:35:11 +02002934 STRCAT(buffer, res_encoding->version);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002935 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002936 }
2937 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
2938#ifdef FEAT_SYN_HL
2939 psettings->do_syntax
2940#else
2941 0
2942#endif
2943 , prt_num_copies);
2944 prt_dsc_noarg("EndComments");
2945
2946 /*
2947 * PS Document page defaults
2948 */
2949 prt_dsc_noarg("BeginDefaults");
2950
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002951 // List font resources most likely common to all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002952 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002953 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002954 if (prt_out_mbyte)
2955 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002956 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
2957 &prt_ps_mb_font);
2958 if (!prt_custom_cmap)
2959 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002960 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002961
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002962 // Paper will be used for all pages
Bram Moolenaar81366db2005-07-24 21:16:51 +00002963 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
2964
2965 prt_dsc_noarg("EndDefaults");
2966
2967 /*
2968 * PS Document prolog inclusion - all required procsets.
2969 */
2970 prt_dsc_noarg("BeginProlog");
2971
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002972 // Add required procsets - NOTE: order is important!
Bram Moolenaard9462e32011-04-11 21:35:11 +02002973 if (!prt_add_resource(res_prolog))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002974 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002975 if (prt_out_mbyte)
2976 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002977 // Add CID font procset, and any user supplied CMap
Bram Moolenaard9462e32011-04-11 21:35:11 +02002978 if (!prt_add_resource(res_cidfont))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002979 goto theend;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002980 if (prt_custom_cmap && !prt_add_resource(res_cmap))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002981 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002982 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002983
Bram Moolenaar81366db2005-07-24 21:16:51 +00002984 if (!prt_out_mbyte || prt_use_courier)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002985 // There will be only one Roman font encoding to be included in the PS
2986 // file.
Bram Moolenaard9462e32011-04-11 21:35:11 +02002987 if (!prt_add_resource(res_encoding))
Bram Moolenaar0a383962014-11-27 17:37:57 +01002988 goto theend;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002989
2990 prt_dsc_noarg("EndProlog");
2991
2992 /*
2993 * PS Document setup - must appear after the prolog
2994 */
2995 prt_dsc_noarg("BeginSetup");
2996
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002997 // Device setup - page size and number of uncollated copies
Bram Moolenaar81366db2005-07-24 21:16:51 +00002998 prt_write_int((int)prt_mediasize[prt_media].width);
2999 prt_write_int((int)prt_mediasize[prt_media].height);
3000 prt_write_int(0);
3001 prt_write_string("sps\n");
3002 prt_write_int(prt_num_copies);
3003 prt_write_string("nc\n");
3004 prt_write_boolean(prt_duplex);
3005 prt_write_boolean(prt_tumble);
3006 prt_write_string("dt\n");
3007 prt_write_boolean(prt_collate);
3008 prt_write_string("c\n");
3009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003010 // Font resource inclusion and definition
Bram Moolenaar81366db2005-07-24 21:16:51 +00003011 if (!prt_out_mbyte || prt_use_courier)
3012 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003013 // When using Courier for ASCII range when printing multi-byte, need to
3014 // pick up ASCII encoding to use with it.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003015 if (prt_use_courier)
3016 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003017 prt_dsc_resources("IncludeResource", "font",
3018 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3019 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3020 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3021 prt_dsc_resources("IncludeResource", "font",
3022 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3023 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3024 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3025 prt_dsc_resources("IncludeResource", "font",
3026 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3027 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3028 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3029 prt_dsc_resources("IncludeResource", "font",
3030 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3031 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3032 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003033 }
3034 if (prt_out_mbyte)
3035 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003036 // Define the CID fonts to be used in the job. Typically CJKV fonts do
3037 // not have an italic form being a western style, so where no font is
3038 // defined for these faces VIM falls back to an existing face.
3039 // Note: if using Courier for the ASCII range then the printout will
3040 // have bold/italic/bolditalic regardless of the setting of printmbfont.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003041 prt_dsc_resources("IncludeResource", "font",
3042 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3043 if (!prt_custom_cmap)
3044 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3045 prt_def_cidfont("CF0", (int)prt_line_height,
3046 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003047
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003048 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3049 {
3050 prt_dsc_resources("IncludeResource", "font",
3051 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3052 if (!prt_custom_cmap)
3053 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3054 prt_def_cidfont("CF1", (int)prt_line_height,
3055 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3056 }
3057 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003058 // Use ROMAN for BOLD
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003059 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003060
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003061 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3062 {
3063 prt_dsc_resources("IncludeResource", "font",
3064 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3065 if (!prt_custom_cmap)
3066 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3067 prt_def_cidfont("CF2", (int)prt_line_height,
3068 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3069 }
3070 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003071 // Use ROMAN for OBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003072 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003073
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003074 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3075 {
3076 prt_dsc_resources("IncludeResource", "font",
3077 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3078 if (!prt_custom_cmap)
3079 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3080 prt_def_cidfont("CF3", (int)prt_line_height,
3081 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3082 }
3083 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003084 // Use BOLD for BOLDOBLIQUE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003085 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003086 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003087
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003088 // Misc constant vars used for underlining and background rects
Bram Moolenaar81366db2005-07-24 21:16:51 +00003089 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3090 prt_ps_font->uline_offset), 2);
3091 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3092 prt_ps_font->uline_width), 2);
3093 prt_def_var("BO", prt_bgcol_offset, 2);
3094
3095 prt_dsc_noarg("EndSetup");
3096
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003097 // Fail if any problems writing out to the PS file
Bram Moolenaard9462e32011-04-11 21:35:11 +02003098 retval = !prt_file_error;
3099
3100theend:
3101 vim_free(res_prolog);
3102 vim_free(res_encoding);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003103 vim_free(res_cidfont);
3104 vim_free(res_cmap);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003105
3106 return retval;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003107}
3108
3109 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003110mch_print_end(prt_settings_T *psettings)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003111{
3112 prt_dsc_noarg("Trailer");
3113
3114 /*
3115 * Output any info we don't know in toto until we finish
3116 */
3117 prt_dsc_ints("Pages", 1, &prt_page_num);
3118
3119 prt_dsc_noarg("EOF");
3120
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003121 // Write CTRL-D to close serial communication link if used.
3122 // NOTHING MUST BE WRITTEN AFTER THIS!
Bram Moolenaar81366db2005-07-24 21:16:51 +00003123 prt_write_file((char_u *)IF_EB("\004", "\067"));
3124
3125 if (!prt_file_error && psettings->outfile == NULL
3126 && !got_int && !psettings->user_abort)
3127 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003128 // Close the file first.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003129 if (prt_ps_fd != NULL)
3130 {
3131 fclose(prt_ps_fd);
3132 prt_ps_fd = NULL;
3133 }
3134 prt_message((char_u *)_("Sending to printer..."));
3135
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003136 // Not printing to a file: use 'printexpr' to print the file.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003137 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003138 emsg(_("E365: Failed to print PostScript file"));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003139 else
3140 prt_message((char_u *)_("Print job sent."));
3141 }
3142
3143 mch_print_cleanup();
3144}
3145
3146 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003147mch_print_end_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003148{
3149 prt_flush_buffer();
3150
3151 prt_write_string("re sp\n");
3152
3153 prt_dsc_noarg("PageTrailer");
3154
3155 return !prt_file_error;
3156}
3157
Bram Moolenaar81366db2005-07-24 21:16:51 +00003158 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003159mch_print_begin_page(char_u *str UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003160{
3161 int page_num[2];
3162
3163 prt_page_num++;
3164
3165 page_num[0] = page_num[1] = prt_page_num;
3166 prt_dsc_ints("Page", 2, page_num);
3167
3168 prt_dsc_noarg("BeginPageSetup");
3169
3170 prt_write_string("sv\n0 g\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003171 prt_in_ascii = !prt_out_mbyte;
3172 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003173 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003174 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003175 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003176 prt_fgcol = PRCOLOR_BLACK;
3177 prt_bgcol = PRCOLOR_WHITE;
3178 prt_font = PRT_PS_FONT_ROMAN;
3179
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003180 // Set up page transformation for landscape printing.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003181 if (!prt_portrait)
3182 {
3183 prt_write_int(-((int)prt_mediasize[prt_media].width));
3184 prt_write_string("sl\n");
3185 }
3186
3187 prt_dsc_noarg("EndPageSetup");
3188
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003189 // We have reset the font attributes, force setting them again.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003190 curr_bg = (long_u)0xffffffff;
3191 curr_fg = (long_u)0xffffffff;
3192 curr_bold = MAYBE;
3193
3194 return !prt_file_error;
3195}
3196
3197 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003198mch_print_blank_page(void)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003199{
3200 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3201}
3202
3203static float prt_pos_x = 0;
3204static float prt_pos_y = 0;
3205
3206 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003207mch_print_start_line(int margin, int page_line)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003208{
3209 prt_pos_x = prt_left_margin;
3210 if (margin)
3211 prt_pos_x -= prt_number_width;
3212
3213 prt_pos_y = prt_top_margin - prt_first_line_height -
3214 page_line * prt_line_height;
3215
3216 prt_attribute_change = TRUE;
3217 prt_need_moveto = TRUE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003218 prt_half_width = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003219}
3220
Bram Moolenaar81366db2005-07-24 21:16:51 +00003221 int
Bram Moolenaar43dee182018-06-16 14:44:11 +02003222mch_print_text_out(char_u *textp, int len UNUSED)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003223{
Bram Moolenaar43dee182018-06-16 14:44:11 +02003224 char_u *p = textp;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003225 int need_break;
3226 char_u ch;
3227 char_u ch_buff[8];
3228 float char_width;
3229 float next_pos;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003230 int in_ascii;
3231 int half_width;
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003232 char_u *tofree = NULL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003233
3234 char_width = prt_char_width;
3235
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003236 // Ideally VIM would create a rearranged CID font to combine a Roman and
3237 // CJKV font to do what VIM is doing here - use a Roman font for characters
3238 // in the ASCII range, and the original CID font for everything else.
3239 // The problem is that GhostScript still (as of 8.13) does not support
3240 // rearranged fonts even though they have been documented by Adobe for 7
3241 // years! If they ever do, a lot of this code will disappear.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003242 if (prt_use_courier)
3243 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003244 in_ascii = (len == 1 && *p < 0x80);
3245 if (prt_in_ascii)
3246 {
3247 if (!in_ascii)
3248 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003249 // No longer in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003250 prt_in_ascii = FALSE;
3251 prt_need_font = TRUE;
3252 prt_attribute_change = TRUE;
3253 }
3254 }
3255 else if (in_ascii)
3256 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003257 // Now in ASCII range - need to switch font
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003258 prt_in_ascii = TRUE;
3259 prt_need_font = TRUE;
3260 prt_attribute_change = TRUE;
3261 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003262 }
3263 if (prt_out_mbyte)
3264 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003265 half_width = ((*mb_ptr2cells)(p) == 1);
3266 if (half_width)
3267 char_width /= 2;
3268 if (prt_half_width)
3269 {
3270 if (!half_width)
3271 {
3272 prt_half_width = FALSE;
3273 prt_pos_x += prt_char_width/4;
3274 prt_need_moveto = TRUE;
3275 prt_attribute_change = TRUE;
3276 }
3277 }
3278 else if (half_width)
3279 {
3280 prt_half_width = TRUE;
3281 prt_pos_x += prt_char_width/4;
3282 prt_need_moveto = TRUE;
3283 prt_attribute_change = TRUE;
3284 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003285 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003286
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003287 // Output any required changes to the graphics state, after flushing any
3288 // text buffered so far.
Bram Moolenaar81366db2005-07-24 21:16:51 +00003289 if (prt_attribute_change)
3290 {
3291 prt_flush_buffer();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003292 // Reset count of number of chars that will be printed
Bram Moolenaar81366db2005-07-24 21:16:51 +00003293 prt_text_run = 0;
3294
3295 if (prt_need_moveto)
3296 {
3297 prt_pos_x_moveto = prt_pos_x;
3298 prt_pos_y_moveto = prt_pos_y;
3299 prt_do_moveto = TRUE;
3300
3301 prt_need_moveto = FALSE;
3302 }
3303 if (prt_need_font)
3304 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003305 if (!prt_in_ascii)
3306 prt_write_string("CF");
3307 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003308 prt_write_string("F");
3309 prt_write_int(prt_font);
3310 prt_write_string("sf\n");
3311 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003312 }
3313 if (prt_need_fgcol)
3314 {
3315 int r, g, b;
3316 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3317 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3318 b = prt_fgcol & 0xff;
3319
3320 prt_write_real(r / 255.0, 3);
3321 if (r == g && g == b)
3322 prt_write_string("g\n");
3323 else
3324 {
3325 prt_write_real(g / 255.0, 3);
3326 prt_write_real(b / 255.0, 3);
3327 prt_write_string("r\n");
3328 }
3329 prt_need_fgcol = FALSE;
3330 }
3331
3332 if (prt_bgcol != PRCOLOR_WHITE)
3333 {
3334 prt_new_bgcol = prt_bgcol;
3335 if (prt_need_bgcol)
3336 prt_do_bgcol = TRUE;
3337 }
3338 else
3339 prt_do_bgcol = FALSE;
3340 prt_need_bgcol = FALSE;
3341
3342 if (prt_need_underline)
3343 prt_do_underline = prt_underline;
3344 prt_need_underline = FALSE;
3345
3346 prt_attribute_change = FALSE;
3347 }
3348
Bram Moolenaar81366db2005-07-24 21:16:51 +00003349 if (prt_do_conv)
Bram Moolenaar43dee182018-06-16 14:44:11 +02003350 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003351 // Convert from multi-byte to 8-bit encoding
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003352 tofree = p = string_convert(&prt_conv, p, &len);
Bram Moolenaar43dee182018-06-16 14:44:11 +02003353 if (p == NULL)
3354 {
3355 p = (char_u *)"";
3356 len = 0;
3357 }
3358 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003359
3360 if (prt_out_mbyte)
3361 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003362 // Multi-byte character strings are represented more efficiently as hex
3363 // strings when outputting clean 8 bit PS.
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003364 while (len-- > 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003365 {
3366 ch = prt_hexchar[(unsigned)(*p) >> 4];
3367 ga_append(&prt_ps_buffer, ch);
3368 ch = prt_hexchar[(*p) & 0xf];
3369 ga_append(&prt_ps_buffer, ch);
3370 p++;
3371 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003372 }
3373 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003374 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003375 // Add next character to buffer of characters to output.
3376 // Note: One printed character may require several PS characters to
3377 // represent it, but we only count them as one printed character.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003378 ch = *p;
3379 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3380 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003381 // Convert non-printing characters to either their escape or octal
3382 // sequence, ensures PS sent over a serial line does not interfere
3383 // with the comms protocol. Note: For EBCDIC we need to write out
3384 // the escape sequences as ASCII codes!
3385 // Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003386 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3387 switch (ch)
3388 {
3389 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3390 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3391 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3392 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3393 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3394 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3395 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3396 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003397
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003398 default:
3399 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003400#ifdef EBCDIC
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003401 ebcdic2ascii(ch_buff, 3);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003402#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003403 ga_append(&prt_ps_buffer, ch_buff[0]);
3404 ga_append(&prt_ps_buffer, ch_buff[1]);
3405 ga_append(&prt_ps_buffer, ch_buff[2]);
3406 break;
3407 }
3408 }
3409 else
3410 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003411 }
3412
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003413 // Need to free any translated characters
Bram Moolenaarcdd09aa2018-02-11 15:38:40 +01003414 vim_free(tofree);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003415
3416 prt_text_run += char_width;
3417 prt_pos_x += char_width;
3418
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003419 // The downside of fp - use relative error on right margin check
Bram Moolenaar81366db2005-07-24 21:16:51 +00003420 next_pos = prt_pos_x + prt_char_width;
3421 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003422 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003423
3424 if (need_break)
3425 prt_flush_buffer();
3426
3427 return need_break;
3428}
3429
3430 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003431mch_print_set_font(int iBold, int iItalic, int iUnderline)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003432{
3433 int font = 0;
3434
3435 if (iBold)
3436 font |= 0x01;
3437 if (iItalic)
3438 font |= 0x02;
3439
3440 if (font != prt_font)
3441 {
3442 prt_font = font;
3443 prt_attribute_change = TRUE;
3444 prt_need_font = TRUE;
3445 }
3446 if (prt_underline != iUnderline)
3447 {
3448 prt_underline = iUnderline;
3449 prt_attribute_change = TRUE;
3450 prt_need_underline = TRUE;
3451 }
3452}
3453
3454 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003455mch_print_set_bg(long_u bgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003456{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003458 prt_attribute_change = TRUE;
3459 prt_need_bgcol = TRUE;
3460}
3461
3462 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01003463mch_print_set_fg(long_u fgcol)
Bram Moolenaar81366db2005-07-24 21:16:51 +00003464{
3465 if (fgcol != (long_u)prt_fgcol)
3466 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003468 prt_attribute_change = TRUE;
3469 prt_need_fgcol = TRUE;
3470 }
3471}
3472
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01003473# endif //FEAT_POSTSCRIPT
3474#endif //FEAT_PRINTER