blob: b00322d47329eaa07f17e2733ceb19c29dabdf69 [file] [log] [blame]
Bram Moolenaar81366db2005-07-24 21:16:51 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * hardcopy.c: printing to paper
12 */
13
14#include "vim.h"
15#include "version.h"
16
17#if defined(FEAT_PRINTER) || defined(PROTO)
18/*
19 * To implement printing on a platform, the following functions must be
20 * defined:
21 *
22 * int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
23 * Called once. Code should display printer dialogue (if appropriate) and
24 * determine printer font and margin settings. Reset has_color if the printer
25 * doesn't support colors at all.
26 * Returns FAIL to abort.
27 *
28 * int mch_print_begin(prt_settings_T *settings)
29 * Called to start the print job.
30 * Return FALSE to abort.
31 *
32 * int mch_print_begin_page(char_u *msg)
33 * Called at the start of each page.
34 * "msg" indicates the progress of the print job, can be NULL.
35 * Return FALSE to abort.
36 *
37 * int mch_print_end_page()
38 * Called at the end of each page.
39 * Return FALSE to abort.
40 *
41 * int mch_print_blank_page()
42 * Called to generate a blank page for collated, duplex, multiple copy
43 * document. Return FALSE to abort.
44 *
45 * void mch_print_end(prt_settings_T *psettings)
46 * Called at normal end of print job.
47 *
48 * void mch_print_cleanup()
49 * Called if print job ends normally or is abandoned. Free any memory, close
50 * devices and handles. Also called when mch_print_begin() fails, but not
51 * when mch_print_init() fails.
52 *
53 * void mch_print_set_font(int Bold, int Italic, int Underline);
54 * Called whenever the font style changes.
55 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000056 * void mch_print_set_bg(long_u bgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000057 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
59 *
Bram Moolenaar551dbcc2006-04-25 22:13:59 +000060 * void mch_print_set_fg(long_u fgcol);
Bram Moolenaar81366db2005-07-24 21:16:51 +000061 * Called to set the foreground color for the following text. Parameter is an
62 * RGB value.
63 *
64 * mch_print_start_line(int margin, int page_line)
65 * Sets the current position at the start of line "page_line".
66 * If margin is TRUE start in the left margin (for header and line number).
67 *
68 * int mch_print_text_out(char_u *p, int len);
69 * Output one character of text p[len] at the current position.
70 * Return TRUE if there is no room for another character in the same line.
71 *
72 * Note that the generic code has no idea of margins. The machine code should
73 * simply make the page look smaller! The header and the line numbers are
74 * printed in the margin.
75 */
76
77#ifdef FEAT_SYN_HL
78static const long_u cterm_color_8[8] =
79{
80 (long_u)0x000000L, (long_u)0xff0000L, (long_u)0x00ff00L, (long_u)0xffff00L,
81 (long_u)0x0000ffL, (long_u)0xff00ffL, (long_u)0x00ffffL, (long_u)0xffffffL
82};
83
84static const long_u cterm_color_16[16] =
85{
86 (long_u)0x000000L, (long_u)0x0000c0L, (long_u)0x008000L, (long_u)0x004080L,
87 (long_u)0xc00000L, (long_u)0xc000c0L, (long_u)0x808000L, (long_u)0xc0c0c0L,
88 (long_u)0x808080L, (long_u)0x6060ffL, (long_u)0x00ff00L, (long_u)0x00ffffL,
89 (long_u)0xff8080L, (long_u)0xff40ffL, (long_u)0xffff00L, (long_u)0xffffffL
90};
91
92static int current_syn_id;
93#endif
94
95#define PRCOLOR_BLACK (long_u)0
96#define PRCOLOR_WHITE (long_u)0xFFFFFFL
97
98static int curr_italic;
99static int curr_bold;
100static int curr_underline;
101static long_u curr_bg;
102static long_u curr_fg;
103static int page_count;
104
105#if defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)
106# define OPT_MBFONT_USECOURIER 0
107# define OPT_MBFONT_ASCII 1
108# define OPT_MBFONT_REGULAR 2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000109# define OPT_MBFONT_BOLD 3
Bram Moolenaar81366db2005-07-24 21:16:51 +0000110# define OPT_MBFONT_OBLIQUE 4
111# define OPT_MBFONT_BOLDOBLIQUE 5
112# define OPT_MBFONT_NUM_OPTIONS 6
113
114static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] =
115{
116 {"c", FALSE, 0, NULL, 0, FALSE},
117 {"a", FALSE, 0, NULL, 0, FALSE},
118 {"r", FALSE, 0, NULL, 0, FALSE},
119 {"b", FALSE, 0, NULL, 0, FALSE},
120 {"i", FALSE, 0, NULL, 0, FALSE},
121 {"o", FALSE, 0, NULL, 0, FALSE},
122};
123#endif
124
125/*
126 * These values determine the print position on a page.
127 */
128typedef struct
129{
130 int lead_spaces; /* remaining spaces for a TAB */
131 int print_pos; /* virtual column for computing TABs */
132 colnr_T column; /* byte column */
133 linenr_T file_line; /* line nr in the buffer */
134 long_u bytes_printed; /* bytes printed so far */
135 int ff; /* seen form feed character */
136} prt_pos_T;
137
138static char_u *parse_list_options __ARGS((char_u *option_str, option_table_T *table, int table_size));
139
140#ifdef FEAT_SYN_HL
141static long_u darken_rgb __ARGS((long_u rgb));
142static long_u prt_get_term_color __ARGS((int colorindex));
143#endif
144static void prt_set_fg __ARGS((long_u fg));
145static void prt_set_bg __ARGS((long_u bg));
146static void prt_set_font __ARGS((int bold, int italic, int underline));
147static void prt_line_number __ARGS((prt_settings_T *psettings, int page_line, linenr_T lnum));
148static void prt_header __ARGS((prt_settings_T *psettings, int pagenum, linenr_T lnum));
149static void prt_message __ARGS((char_u *s));
150static colnr_T hardcopy_line __ARGS((prt_settings_T *psettings, int page_line, prt_pos_T *ppos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000151#ifdef FEAT_SYN_HL
Bram Moolenaar81366db2005-07-24 21:16:51 +0000152static void prt_get_attr __ARGS((int hl_id, prt_text_attr_T* pattr, int modec));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000153#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000154
155/*
156 * Parse 'printoptions' and set the flags in "printer_opts".
157 * Returns an error message or NULL;
158 */
159 char_u *
160parse_printoptions()
161{
162 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
163}
164
165#if (defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
166/*
167 * Parse 'printoptions' and set the flags in "printer_opts".
168 * Returns an error message or NULL;
169 */
170 char_u *
171parse_printmbfont()
172{
173 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
174}
175#endif
176
177/*
178 * Parse a list of options in the form
179 * option:value,option:value,option:value
180 *
181 * "value" can start with a number which is parsed out, e.g. margin:12mm
182 *
183 * Returns an error message for an illegal option, NULL otherwise.
184 * Only used for the printer at the moment...
185 */
186 static char_u *
187parse_list_options(option_str, table, table_size)
188 char_u *option_str;
189 option_table_T *table;
190 int table_size;
191{
192 char_u *stringp;
193 char_u *colonp;
194 char_u *commap;
195 char_u *p;
196 int idx = 0; /* init for GCC */
197 int len;
198
199 for (idx = 0; idx < table_size; ++idx)
200 table[idx].present = FALSE;
201
202 /*
203 * Repeat for all comma separated parts.
204 */
205 stringp = option_str;
206 while (*stringp)
207 {
208 colonp = vim_strchr(stringp, ':');
209 if (colonp == NULL)
210 return (char_u *)N_("E550: Missing colon");
211 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)
222 return (char_u *)N_("E551: Illegal component");
223
224 p = colonp + 1;
225 table[idx].present = TRUE;
226
227 if (table[idx].hasnum)
228 {
229 if (!VIM_ISDIGIT(*p))
230 return (char_u *)N_("E552: digit expected");
231
232 table[idx].number = getdigits(&p); /*advances p*/
233 }
234
235 table[idx].string = p;
236 table[idx].strlen = (int)(commap - p);
237
238 stringp = commap;
239 if (*stringp == ',')
240 ++stringp;
241 }
242
243 return NULL;
244}
245
246
247#ifdef FEAT_SYN_HL
248/*
249 * If using a dark background, the colors will probably be too bright to show
250 * up well on white paper, so reduce their brightness.
251 */
252 static long_u
253darken_rgb(rgb)
254 long_u rgb;
255{
256 return ((rgb >> 17) << 16)
257 + (((rgb & 0xff00) >> 9) << 8)
258 + ((rgb & 0xff) >> 1);
259}
260
261 static long_u
262prt_get_term_color(colorindex)
263 int colorindex;
264{
265 /* TODO: Should check for xterm with 88 or 256 colors. */
266 if (t_colors > 8)
267 return cterm_color_16[colorindex % 16];
268 return cterm_color_8[colorindex % 8];
269}
270
271 static void
272prt_get_attr(hl_id, pattr, modec)
273 int hl_id;
274 prt_text_attr_T *pattr;
275 int modec;
276{
277 int colorindex;
278 long_u fg_color;
279 long_u bg_color;
280 char *color;
281
282 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
283 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
284 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
285 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
286
287# ifdef FEAT_GUI
288 if (gui.in_use)
289 {
290 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
291 if (bg_color == PRCOLOR_BLACK)
292 bg_color = PRCOLOR_WHITE;
293
294 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
295 }
296 else
297# endif
298 {
299 bg_color = PRCOLOR_WHITE;
300
301 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
302 if (color == NULL)
303 colorindex = 0;
304 else
305 colorindex = atoi(color);
306
307 if (colorindex >= 0 && colorindex < t_colors)
308 fg_color = prt_get_term_color(colorindex);
309 else
310 fg_color = PRCOLOR_BLACK;
311 }
312
313 if (fg_color == PRCOLOR_WHITE)
314 fg_color = PRCOLOR_BLACK;
315 else if (*p_bg == 'd')
316 fg_color = darken_rgb(fg_color);
317
318 pattr->fg_color = fg_color;
319 pattr->bg_color = bg_color;
320}
321#endif /* FEAT_SYN_HL */
322
323 static void
324prt_set_fg(fg)
325 long_u fg;
326{
327 if (fg != curr_fg)
328 {
329 curr_fg = fg;
330 mch_print_set_fg(fg);
331 }
332}
333
334 static void
335prt_set_bg(bg)
336 long_u bg;
337{
338 if (bg != curr_bg)
339 {
340 curr_bg = bg;
341 mch_print_set_bg(bg);
342 }
343}
344
345 static void
346prt_set_font(bold, italic, underline)
347 int bold;
348 int italic;
349 int underline;
350{
351 if (curr_bold != bold
352 || curr_italic != italic
353 || curr_underline != underline)
354 {
355 curr_underline = underline;
356 curr_italic = italic;
357 curr_bold = bold;
358 mch_print_set_font(bold, italic, underline);
359 }
360}
361
362/*
363 * Print the line number in the left margin.
364 */
365 static void
366prt_line_number(psettings, page_line, lnum)
367 prt_settings_T *psettings;
368 int page_line;
369 linenr_T lnum;
370{
371 int i;
372 char_u tbuf[20];
373
374 prt_set_fg(psettings->number.fg_color);
375 prt_set_bg(psettings->number.bg_color);
376 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
377 mch_print_start_line(TRUE, page_line);
378
379 /* Leave two spaces between the number and the text; depends on
380 * PRINT_NUMBER_WIDTH. */
381 sprintf((char *)tbuf, "%6ld", (long)lnum);
382 for (i = 0; i < 6; i++)
383 (void)mch_print_text_out(&tbuf[i], 1);
384
385#ifdef FEAT_SYN_HL
386 if (psettings->do_syntax)
387 /* Set colors for next character. */
388 current_syn_id = -1;
389 else
390#endif
391 {
392 /* Set colors and font back to normal. */
393 prt_set_fg(PRCOLOR_BLACK);
394 prt_set_bg(PRCOLOR_WHITE);
395 prt_set_font(FALSE, FALSE, FALSE);
396 }
397}
398
Bram Moolenaar81366db2005-07-24 21:16:51 +0000399/*
400 * Get the currently effective header height.
401 */
402 int
403prt_header_height()
404{
405 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
406 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
407 return 2;
408}
409
410/*
411 * Return TRUE if using a line number for printing.
412 */
413 int
414prt_use_number()
415{
416 return (printer_opts[OPT_PRINT_NUMBER].present
417 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
418}
419
420/*
421 * Return the unit used in a margin item in 'printoptions'.
422 * Returns PRT_UNIT_NONE if not recognized.
423 */
424 int
425prt_get_unit(idx)
426 int idx;
427{
428 int u = PRT_UNIT_NONE;
429 int i;
430 static char *(units[4]) = PRT_UNIT_NAMES;
431
432 if (printer_opts[idx].present)
433 for (i = 0; i < 4; ++i)
434 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
435 {
436 u = i;
437 break;
438 }
439 return u;
440}
441
442/*
443 * Print the page header.
444 */
445/*ARGSUSED*/
446 static void
447prt_header(psettings, pagenum, lnum)
448 prt_settings_T *psettings;
449 int pagenum;
450 linenr_T lnum;
451{
452 int width = psettings->chars_per_line;
453 int page_line;
454 char_u *tbuf;
455 char_u *p;
456#ifdef FEAT_MBYTE
457 int l;
458#endif
459
460 /* Also use the space for the line number. */
461 if (prt_use_number())
462 width += PRINT_NUMBER_WIDTH;
463
464 tbuf = alloc(width + IOSIZE);
465 if (tbuf == NULL)
466 return;
467
468#ifdef FEAT_STL_OPT
469 if (*p_header != NUL)
470 {
471 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000472 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000473
474 /*
475 * Need to (temporarily) set current line number and first/last line
476 * number on the 'window'. Since we don't know how long the page is,
477 * set the first and current line number to the top line, and guess
478 * that the page length is 64.
479 */
480 tmp_lnum = curwin->w_cursor.lnum;
481 tmp_topline = curwin->w_topline;
482 tmp_botline = curwin->w_botline;
483 curwin->w_cursor.lnum = lnum;
484 curwin->w_topline = lnum;
485 curwin->w_botline = lnum + 63;
486 printer_page_num = pagenum;
487
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000488# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000489 use_sandbox = was_set_insecurely((char_u *)"printheader", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000490# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000491 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000492 p_header, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000493 ' ', width, NULL, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000494
495 /* Reset line numbers */
496 curwin->w_cursor.lnum = tmp_lnum;
497 curwin->w_topline = tmp_topline;
498 curwin->w_botline = tmp_botline;
499 }
500 else
501#endif
502 sprintf((char *)tbuf, _("Page %d"), pagenum);
503
504 prt_set_fg(PRCOLOR_BLACK);
505 prt_set_bg(PRCOLOR_WHITE);
506 prt_set_font(TRUE, FALSE, FALSE);
507
508 /* Use a negative line number to indicate printing in the top margin. */
509 page_line = 0 - prt_header_height();
510 mch_print_start_line(TRUE, page_line);
511 for (p = tbuf; *p != NUL; )
512 {
513 if (mch_print_text_out(p,
514#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000515 (l = (*mb_ptr2len)(p))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000516#else
517 1
518#endif
519 ))
520 {
521 ++page_line;
522 if (page_line >= 0) /* out of room in header */
523 break;
524 mch_print_start_line(TRUE, page_line);
525 }
526#ifdef FEAT_MBYTE
527 p += l;
528#else
529 p++;
530#endif
531 }
532
533 vim_free(tbuf);
534
535#ifdef FEAT_SYN_HL
536 if (psettings->do_syntax)
537 /* Set colors for next character. */
538 current_syn_id = -1;
539 else
540#endif
541 {
542 /* Set colors and font back to normal. */
543 prt_set_fg(PRCOLOR_BLACK);
544 prt_set_bg(PRCOLOR_WHITE);
545 prt_set_font(FALSE, FALSE, FALSE);
546 }
547}
548
549/*
550 * Display a print status message.
551 */
552 static void
553prt_message(s)
554 char_u *s;
555{
556 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
557 screen_puts(s, (int)Rows - 1, 0, hl_attr(HLF_R));
558 out_flush();
559}
560
561 void
562ex_hardcopy(eap)
563 exarg_T *eap;
564{
565 linenr_T lnum;
566 int collated_copies, uncollated_copies;
567 prt_settings_T settings;
568 long_u bytes_to_print = 0;
569 int page_line;
570 int jobsplit;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000571
572 memset(&settings, 0, sizeof(prt_settings_T));
573 settings.has_color = TRUE;
574
575# ifdef FEAT_POSTSCRIPT
576 if (*eap->arg == '>')
577 {
578 char_u *errormsg = NULL;
579
580 /* Expand things like "%.ps". */
581 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
582 {
583 if (errormsg != NULL)
584 EMSG(errormsg);
585 return;
586 }
587 settings.outfile = skipwhite(eap->arg + 1);
588 }
589 else if (*eap->arg != NUL)
590 settings.arguments = eap->arg;
591# endif
592
593 /*
594 * Initialise for printing. Ask the user for settings, unless forceit is
595 * set.
596 * The mch_print_init() code should set up margins if applicable. (It may
597 * not be a real printer - for example the engine might generate HTML or
598 * PS.)
599 */
600 if (mch_print_init(&settings,
601 curbuf->b_fname == NULL
602 ? (char_u *)buf_spname(curbuf)
603 : curbuf->b_sfname == NULL
604 ? curbuf->b_fname
605 : curbuf->b_sfname,
606 eap->forceit) == FAIL)
607 return;
608
609#ifdef FEAT_SYN_HL
610# ifdef FEAT_GUI
611 if (gui.in_use)
612 settings.modec = 'g';
613 else
614# endif
615 if (t_colors > 1)
616 settings.modec = 'c';
617 else
618 settings.modec = 't';
619
620 if (!syntax_present(curbuf))
621 settings.do_syntax = FALSE;
622 else if (printer_opts[OPT_PRINT_SYNTAX].present
623 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
624 settings.do_syntax =
625 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
626 else
627 settings.do_syntax = settings.has_color;
628#endif
629
630 /* Set up printing attributes for line numbers */
631 settings.number.fg_color = PRCOLOR_BLACK;
632 settings.number.bg_color = PRCOLOR_WHITE;
633 settings.number.bold = FALSE;
634 settings.number.italic = TRUE;
635 settings.number.underline = FALSE;
636#ifdef FEAT_SYN_HL
637 /*
638 * Syntax highlighting of line numbers.
639 */
640 if (prt_use_number() && settings.do_syntax)
641 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000642 int id;
643
Bram Moolenaar81366db2005-07-24 21:16:51 +0000644 id = syn_name2id((char_u *)"LineNr");
645 if (id > 0)
646 id = syn_get_final_id(id);
647
648 prt_get_attr(id, &settings.number, settings.modec);
649 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000650#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000651
652 /*
653 * Estimate the total lines to be printed
654 */
655 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
656 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
657 if (bytes_to_print == 0)
658 {
659 MSG(_("No text to be printed"));
660 goto print_fail_no_begin;
661 }
662
663 /* Set colors and font to normal. */
664 curr_bg = (long_u)0xffffffffL;
665 curr_fg = (long_u)0xffffffffL;
666 curr_italic = MAYBE;
667 curr_bold = MAYBE;
668 curr_underline = MAYBE;
669
670 prt_set_fg(PRCOLOR_BLACK);
671 prt_set_bg(PRCOLOR_WHITE);
672 prt_set_font(FALSE, FALSE, FALSE);
673#ifdef FEAT_SYN_HL
674 current_syn_id = -1;
675#endif
676
677 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
678 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
679
680 if (!mch_print_begin(&settings))
681 goto print_fail_no_begin;
682
683 /*
684 * Loop over collated copies: 1 2 3, 1 2 3, ...
685 */
686 page_count = 0;
687 for (collated_copies = 0;
688 collated_copies < settings.n_collated_copies;
689 collated_copies++)
690 {
691 prt_pos_T prtpos; /* current print position */
692 prt_pos_T page_prtpos; /* print position at page start */
693 int side;
694
695 memset(&page_prtpos, 0, sizeof(prt_pos_T));
696 page_prtpos.file_line = eap->line1;
697 prtpos = page_prtpos;
698
699 if (jobsplit && collated_copies > 0)
700 {
701 /* Splitting jobs: Stop a previous job and start a new one. */
702 mch_print_end(&settings);
703 if (!mch_print_begin(&settings))
704 goto print_fail_no_begin;
705 }
706
707 /*
708 * Loop over all pages in the print job: 1 2 3 ...
709 */
710 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
711 {
712 /*
713 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
714 * For duplex: 12 12 12 34 34 34, ...
715 */
716 for (uncollated_copies = 0;
717 uncollated_copies < settings.n_uncollated_copies;
718 uncollated_copies++)
719 {
720 /* Set the print position to the start of this page. */
721 prtpos = page_prtpos;
722
723 /*
724 * Do front and rear side of a page.
725 */
726 for (side = 0; side <= settings.duplex; ++side)
727 {
728 /*
729 * Print one page.
730 */
731
732 /* Check for interrupt character every page. */
733 ui_breakcheck();
734 if (got_int || settings.user_abort)
735 goto print_fail;
736
737 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
738 page_count + 1 + side,
739 prtpos.bytes_printed > 1000000
740 ? (int)(prtpos.bytes_printed /
741 (bytes_to_print / 100))
742 : (int)((prtpos.bytes_printed * 100)
743 / bytes_to_print));
744 if (!mch_print_begin_page(IObuff))
745 goto print_fail;
746
747 if (settings.n_collated_copies > 1)
748 sprintf((char *)IObuff + STRLEN(IObuff),
749 _(" Copy %d of %d"),
750 collated_copies + 1,
751 settings.n_collated_copies);
752 prt_message(IObuff);
753
754 /*
755 * Output header if required
756 */
757 if (prt_header_height() > 0)
758 prt_header(&settings, page_count + 1 + side,
759 prtpos.file_line);
760
761 for (page_line = 0; page_line < settings.lines_per_page;
762 ++page_line)
763 {
764 prtpos.column = hardcopy_line(&settings,
765 page_line, &prtpos);
766 if (prtpos.column == 0)
767 {
768 /* finished a file line */
769 prtpos.bytes_printed +=
770 STRLEN(skipwhite(ml_get(prtpos.file_line)));
771 if (++prtpos.file_line > eap->line2)
772 break; /* reached the end */
773 }
774 else if (prtpos.ff)
775 {
776 /* Line had a formfeed in it - start new page but
777 * stay on the current line */
778 break;
779 }
780 }
781
782 if (!mch_print_end_page())
783 goto print_fail;
784 if (prtpos.file_line > eap->line2)
785 break; /* reached the end */
786 }
787
788 /*
789 * Extra blank page for duplexing with odd number of pages and
790 * more copies to come.
791 */
792 if (prtpos.file_line > eap->line2 && settings.duplex
793 && side == 0
794 && uncollated_copies + 1 < settings.n_uncollated_copies)
795 {
796 if (!mch_print_blank_page())
797 goto print_fail;
798 }
799 }
800 if (settings.duplex && prtpos.file_line <= eap->line2)
801 ++page_count;
802
803 /* Remember the position where the next page starts. */
804 page_prtpos = prtpos;
805 }
806
807 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
808 settings.jobname);
809 prt_message(IObuff);
810 }
811
812print_fail:
813 if (got_int || settings.user_abort)
814 {
815 sprintf((char *)IObuff, "%s", _("Printing aborted"));
816 prt_message(IObuff);
817 }
818 mch_print_end(&settings);
819
820print_fail_no_begin:
821 mch_print_cleanup();
822}
823
824/*
825 * Print one page line.
826 * Return the next column to print, or zero if the line is finished.
827 */
828 static colnr_T
829hardcopy_line(psettings, page_line, ppos)
830 prt_settings_T *psettings;
831 int page_line;
832 prt_pos_T *ppos;
833{
834 colnr_T col;
835 char_u *line;
836 int need_break = FALSE;
837 int outputlen;
838 int tab_spaces;
839 long_u print_pos;
840#ifdef FEAT_SYN_HL
841 prt_text_attr_T attr;
842 int id;
843#endif
844
845 if (ppos->column == 0 || ppos->ff)
846 {
847 print_pos = 0;
848 tab_spaces = 0;
849 if (!ppos->ff && prt_use_number())
850 prt_line_number(psettings, page_line, ppos->file_line);
851 ppos->ff = FALSE;
852 }
853 else
854 {
855 /* left over from wrap halfway a tab */
856 print_pos = ppos->print_pos;
857 tab_spaces = ppos->lead_spaces;
858 }
859
860 mch_print_start_line(0, page_line);
861 line = ml_get(ppos->file_line);
862
863 /*
864 * Loop over the columns until the end of the file line or right margin.
865 */
866 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
867 {
868 outputlen = 1;
869#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000870 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000871 outputlen = 1;
872#endif
873#ifdef FEAT_SYN_HL
874 /*
875 * syntax highlighting stuff.
876 */
877 if (psettings->do_syntax)
878 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +0000879 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000880 if (id > 0)
881 id = syn_get_final_id(id);
882 else
883 id = 0;
884 /* Get the line again, a multi-line regexp may invalidate it. */
885 line = ml_get(ppos->file_line);
886
887 if (id != current_syn_id)
888 {
889 current_syn_id = id;
890 prt_get_attr(id, &attr, psettings->modec);
891 prt_set_font(attr.bold, attr.italic, attr.underline);
892 prt_set_fg(attr.fg_color);
893 prt_set_bg(attr.bg_color);
894 }
895 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000896#endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000897
898 /*
899 * Appropriately expand any tabs to spaces.
900 */
901 if (line[col] == TAB || tab_spaces != 0)
902 {
903 if (tab_spaces == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000904 tab_spaces = (int)(curbuf->b_p_ts - (print_pos % curbuf->b_p_ts));
Bram Moolenaar81366db2005-07-24 21:16:51 +0000905
906 while (tab_spaces > 0)
907 {
908 need_break = mch_print_text_out((char_u *)" ", 1);
909 print_pos++;
910 tab_spaces--;
911 if (need_break)
912 break;
913 }
914 /* Keep the TAB if we didn't finish it. */
915 if (need_break && tab_spaces > 0)
916 break;
917 }
918 else if (line[col] == FF
919 && printer_opts[OPT_PRINT_FORMFEED].present
920 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
921 == 'y')
922 {
923 ppos->ff = TRUE;
924 need_break = 1;
925 }
926 else
927 {
928 need_break = mch_print_text_out(line + col, outputlen);
929#ifdef FEAT_MBYTE
930 if (has_mbyte)
931 print_pos += (*mb_ptr2cells)(line + col);
932 else
933#endif
934 print_pos++;
935 }
936 }
937
938 ppos->lead_spaces = tab_spaces;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000939 ppos->print_pos = (int)print_pos;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000940
941 /*
942 * Start next line of file if we clip lines, or have reached end of the
943 * line, unless we are doing a formfeed.
944 */
945 if (!ppos->ff
946 && (line[col] == NUL
947 || (printer_opts[OPT_PRINT_WRAP].present
948 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
949 == 'n')))
950 return 0;
951 return col;
952}
953
954# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
955
956/*
957 * PS printer stuff.
958 *
959 * Sources of information to help maintain the PS printing code:
960 *
961 * 1. PostScript Language Reference, 3rd Edition,
962 * Addison-Wesley, 1999, ISBN 0-201-37922-8
963 * 2. PostScript Language Program Design,
964 * Addison-Wesley, 1988, ISBN 0-201-14396-8
965 * 3. PostScript Tutorial and Cookbook,
966 * Addison Wesley, 1985, ISBN 0-201-10179-3
967 * 4. PostScript Language Document Structuring Conventions Specification,
968 * version 3.0,
969 * Adobe Technote 5001, 25th September 1992
970 * 5. PostScript Printer Description File Format Specification, Version 4.3,
971 * Adobe technote 5003, 9th February 1996
972 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
973 * Adobe Technote 5007, 7th October 1998
974 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
975 * Adobe Technote 5014, 8th October 1996
976 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
977 * Adoboe Technote 5094, 8th September, 2001
978 * 9. CJKV Information Processing, 2nd Edition,
979 * O'Reilly, 2002, ISBN 1-56592-224-7
980 *
981 * Some of these documents can be found in PDF form on Adobe's web site -
982 * http://www.adobe.com
983 */
984
985#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
986
987#define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
988#define PRT_PS_DEFAULT_FONTSIZE (10)
989#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
990
991struct prt_mediasize_S
992{
993 char *name;
994 float width; /* width and height in points for portrait */
995 float height;
996};
997
998#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
999
1000static struct prt_mediasize_S prt_mediasize[] =
1001{
1002 {"A4", 595.0, 842.0},
1003 {"letter", 612.0, 792.0},
1004 {"10x14", 720.0, 1008.0},
1005 {"A3", 842.0, 1191.0},
1006 {"A5", 420.0, 595.0},
1007 {"B4", 729.0, 1032.0},
1008 {"B5", 516.0, 729.0},
1009 {"executive", 522.0, 756.0},
1010 {"folio", 595.0, 935.0},
1011 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1012 {"legal", 612.0, 1008.0},
1013 {"quarto", 610.0, 780.0},
1014 {"statement", 396.0, 612.0},
1015 {"tabloid", 792.0, 1224.0}
1016};
1017
1018/* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1019struct prt_ps_font_S
1020{
1021 int wx;
1022 int uline_offset;
1023 int uline_width;
1024 int bbox_min_y;
1025 int bbox_max_y;
1026 char *(ps_fontname[4]);
1027};
1028
1029#define PRT_PS_FONT_ROMAN (0)
1030#define PRT_PS_FONT_BOLD (1)
1031#define PRT_PS_FONT_OBLIQUE (2)
1032#define PRT_PS_FONT_BOLDOBLIQUE (3)
1033
1034/* Standard font metrics for Courier family */
1035static struct prt_ps_font_S prt_ps_courier_font =
1036{
1037 600,
1038 -100, 50,
1039 -250, 805,
1040 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1041};
1042
1043#ifdef FEAT_MBYTE
1044/* Generic font metrics for multi-byte fonts */
1045static struct prt_ps_font_S prt_ps_mb_font =
1046{
1047 1000,
1048 -100, 50,
1049 -250, 805,
1050 {NULL, NULL, NULL, NULL}
1051};
1052#endif
1053
1054/* Pointer to current font set being used */
1055static struct prt_ps_font_S* prt_ps_font;
1056
1057/* Structures to map user named encoding and mapping to PS equivalents for
1058 * building CID font name */
1059struct prt_ps_encoding_S
1060{
1061 char *encoding;
1062 char *cmap_encoding;
1063 int needs_charset;
1064};
1065
1066struct prt_ps_charset_S
1067{
1068 char *charset;
1069 char *cmap_charset;
1070 int has_charset;
1071};
1072
1073#ifdef FEAT_MBYTE
1074
1075#define CS_JIS_C_1978 (0x01)
1076#define CS_JIS_X_1983 (0x02)
1077#define CS_JIS_X_1990 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001078#define CS_NEC (0x08)
1079#define CS_MSWINDOWS (0x10)
1080#define CS_CP932 (0x20)
1081#define CS_KANJITALK6 (0x40)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001082#define CS_KANJITALK7 (0x80)
1083
1084/* Japanese encodings and charsets */
1085static struct prt_ps_encoding_S j_encodings[] =
1086{
1087 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001088 CS_NEC)},
1089 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1090 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1091 CS_KANJITALK6|CS_KANJITALK7)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001092 {"cp932", "RKSJ", CS_JIS_X_1983},
1093 {"ucs-2", "UCS2", CS_JIS_X_1990},
1094 {"utf-8", "UTF8" , CS_JIS_X_1990}
1095};
1096static struct prt_ps_charset_S j_charsets[] =
1097{
1098 {"JIS_C_1978", "78", CS_JIS_C_1978},
1099 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1100 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001101 {"NEC", "Ext", CS_NEC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001102 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1103 {"CP932", "90ms", CS_JIS_X_1983},
1104 {"KANJITALK6", "83pv", CS_KANJITALK6},
1105 {"KANJITALK7", "90pv", CS_KANJITALK7}
1106};
1107
1108#define CS_GB_2312_80 (0x01)
1109#define CS_GBT_12345_90 (0x02)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001110#define CS_GBK2K (0x04)
1111#define CS_SC_MAC (0x08)
1112#define CS_GBT_90_MAC (0x10)
1113#define CS_GBK (0x20)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001114#define CS_SC_ISO10646 (0x40)
1115
1116/* Simplified Chinese encodings and charsets */
1117static struct prt_ps_encoding_S sc_encodings[] =
1118{
1119 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1120 {"gb18030", NULL, CS_GBK2K},
1121 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001122 CS_GBT_90_MAC)},
1123 {"gbk", "EUC", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001124 {"ucs-2", "UCS2", CS_SC_ISO10646},
1125 {"utf-8", "UTF8", CS_SC_ISO10646}
1126};
1127static struct prt_ps_charset_S sc_charsets[] =
1128{
1129 {"GB_2312-80", "GB", CS_GB_2312_80},
1130 {"GBT_12345-90","GBT", CS_GBT_12345_90},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001131 {"MAC", "GBpc", CS_SC_MAC},
1132 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1133 {"GBK", "GBK", CS_GBK},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001134 {"GB18030", "GBK2K", CS_GBK2K},
1135 {"ISO10646", "UniGB", CS_SC_ISO10646}
1136};
1137
1138#define CS_CNS_PLANE_1 (0x01)
1139#define CS_CNS_PLANE_2 (0x02)
1140#define CS_CNS_PLANE_1_2 (0x04)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001141#define CS_B5 (0x08)
1142#define CS_ETEN (0x10)
1143#define CS_HK_GCCS (0x20)
1144#define CS_HK_SCS (0x40)
1145#define CS_HK_SCS_ETEN (0x80)
1146#define CS_MTHKL (0x100)
1147#define CS_MTHKS (0x200)
1148#define CS_DLHKL (0x400)
1149#define CS_DLHKS (0x800)
1150#define CS_TC_ISO10646 (0x1000)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001151
1152/* Traditional Chinese encodings and charsets */
1153static struct prt_ps_encoding_S tc_encodings[] =
1154{
1155 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1156 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001157 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1158 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1159 CS_DLHKS)},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001160 {"cp950", "B5", CS_B5},
1161 {"ucs-2", "UCS2", CS_TC_ISO10646},
1162 {"utf-8", "UTF8", CS_TC_ISO10646},
1163 {"utf-16", "UTF16", CS_TC_ISO10646},
1164 {"utf-32", "UTF32", CS_TC_ISO10646}
1165};
1166static struct prt_ps_charset_S tc_charsets[] =
1167{
1168 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1169 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1170 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001171 {"BIG5", NULL, CS_B5},
1172 {"CP950", NULL, CS_B5},
1173 {"ETEN", "ETen", CS_ETEN},
1174 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1175 {"SCS", "HKscs", CS_HK_SCS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001176 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1177 {"MTHKL", "HKm471", CS_MTHKL},
1178 {"MTHKS", "HKm314", CS_MTHKS},
1179 {"DLHKL", "HKdla", CS_DLHKL},
1180 {"DLHKS", "HKdlb", CS_DLHKS},
1181 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1182};
1183
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001184#define CS_KR_X_1992 (0x01)
1185#define CS_KR_MAC (0x02)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001186#define CS_KR_X_1992_MS (0x04)
1187#define CS_KR_ISO10646 (0x08)
1188
1189/* Korean encodings and charsets */
1190static struct prt_ps_encoding_S k_encodings[] =
1191{
1192 {"iso-2022-kr", NULL, CS_KR_X_1992},
1193 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1194 {"johab", "Johab", CS_KR_X_1992},
1195 {"cp1361", "Johab", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001196 {"uhc", "UHC", CS_KR_X_1992_MS},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001197 {"cp949", "UHC", CS_KR_X_1992_MS},
1198 {"ucs-2", "UCS2", CS_KR_ISO10646},
1199 {"utf-8", "UTF8", CS_KR_ISO10646}
1200};
1201static struct prt_ps_charset_S k_charsets[] =
1202{
1203 {"KS_X_1992", "KSC", CS_KR_X_1992},
1204 {"CP1361", "KSC", CS_KR_X_1992},
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001205 {"MAC", "KSCpc", CS_KR_MAC},
Bram Moolenaar81366db2005-07-24 21:16:51 +00001206 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1207 {"CP949", "KSCms", CS_KR_X_1992_MS},
1208 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1209 {"ISO10646", "UniKS", CS_KR_ISO10646}
1210};
1211
1212/* Collections of encodings and charsets for multi-byte printing */
1213struct prt_ps_mbfont_S
1214{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001215 int num_encodings;
1216 struct prt_ps_encoding_S *encodings;
1217 int num_charsets;
1218 struct prt_ps_charset_S *charsets;
1219 char *ascii_enc;
1220 char *defcs;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001221};
1222
1223static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1224{
1225 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001226 NUM_ELEMENTS(j_encodings),
1227 j_encodings,
1228 NUM_ELEMENTS(j_charsets),
1229 j_charsets,
1230 "jis_roman",
1231 "JIS_X_1983"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001232 },
1233 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001234 NUM_ELEMENTS(sc_encodings),
1235 sc_encodings,
1236 NUM_ELEMENTS(sc_charsets),
1237 sc_charsets,
1238 "gb_roman",
1239 "GB_2312-80"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001240 },
1241 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001242 NUM_ELEMENTS(tc_encodings),
1243 tc_encodings,
1244 NUM_ELEMENTS(tc_charsets),
1245 tc_charsets,
1246 "cns_roman",
1247 "BIG5"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001248 },
1249 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001250 NUM_ELEMENTS(k_encodings),
1251 k_encodings,
1252 NUM_ELEMENTS(k_charsets),
1253 k_charsets,
1254 "ks_roman",
1255 "KS_X_1992"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001256 }
1257};
1258#endif /* FEAT_MBYTE */
1259
1260struct prt_ps_resource_S
1261{
1262 char_u name[64];
1263 char_u filename[MAXPATHL + 1];
1264 int type;
1265 char_u title[256];
1266 char_u version[256];
1267};
1268
1269/* Types of PS resource file currently used */
1270#define PRT_RESOURCE_TYPE_PROCSET (0)
1271#define PRT_RESOURCE_TYPE_ENCODING (1)
1272#define PRT_RESOURCE_TYPE_CMAP (2)
1273
1274/* The PS prolog file version number has to match - if the prolog file is
1275 * updated, increment the number in the file and here. Version checking was
1276 * added as of VIM 6.2.
1277 * The CID prolog file version number behaves as per PS prolog.
1278 * Table of VIM and prolog versions:
1279 *
1280 * VIM Prolog CIDProlog
1281 * 6.2 1.3
1282 * 7.0 1.4 1.0
1283 */
1284#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1285#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1286
1287/* String versions of PS resource types - indexed by constants above so don't
1288 * re-order!
1289 */
1290static char *prt_resource_types[] =
1291{
1292 "procset",
1293 "encoding",
1294 "cmap"
1295};
1296
1297/* Strings to look for in a PS resource file */
1298#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1299#define PRT_RESOURCE_RESOURCE "Resource-"
1300#define PRT_RESOURCE_PROCSET "ProcSet"
1301#define PRT_RESOURCE_ENCODING "Encoding"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001302#define PRT_RESOURCE_CMAP "CMap"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001303
1304
1305/* Data for table based DSC comment recognition, easy to extend if VIM needs to
1306 * read more comments. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001307#define PRT_DSC_MISC_TYPE (-1)
1308#define PRT_DSC_TITLE_TYPE (1)
1309#define PRT_DSC_VERSION_TYPE (2)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001310#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1311
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001312#define PRT_DSC_TITLE "%%Title:"
1313#define PRT_DSC_VERSION "%%Version:"
1314#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
Bram Moolenaar81366db2005-07-24 21:16:51 +00001315
1316struct prt_dsc_comment_S
1317{
1318 char *string;
1319 int len;
1320 int type;
1321};
1322
1323struct prt_dsc_line_S
1324{
1325 int type;
1326 char_u *string;
1327 int len;
1328};
1329
1330
1331#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1332static struct prt_dsc_comment_S prt_dsc_table[] =
1333{
1334 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1335 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1336 PRT_DSC_VERSION_TYPE},
1337 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1338 PRT_DSC_ENDCOMMENTS_TYPE}
1339};
1340
1341static void prt_write_file_raw_len __ARGS((char_u *buffer, int bytes));
1342static void prt_write_file __ARGS((char_u *buffer));
1343static void prt_write_file_len __ARGS((char_u *buffer, int bytes));
1344static void prt_write_string __ARGS((char *s));
1345static void prt_write_int __ARGS((int i));
1346static void prt_write_boolean __ARGS((int b));
1347static void prt_def_font __ARGS((char *new_name, char *encoding, int height, char *font));
1348static void prt_real_bits __ARGS((double real, int precision, int *pinteger, int *pfraction));
1349static void prt_write_real __ARGS((double val, int prec));
1350static void prt_def_var __ARGS((char *name, double value, int prec));
1351static void prt_flush_buffer __ARGS((void));
1352static void prt_resource_name __ARGS((char_u *filename, void *cookie));
1353static int prt_find_resource __ARGS((char *name, struct prt_ps_resource_S *resource));
1354static int prt_open_resource __ARGS((struct prt_ps_resource_S *resource));
1355static int prt_check_resource __ARGS((struct prt_ps_resource_S *resource, char_u *version));
1356static void prt_dsc_start __ARGS((void));
1357static void prt_dsc_noarg __ARGS((char *comment));
1358static void prt_dsc_textline __ARGS((char *comment, char *text));
1359static void prt_dsc_text __ARGS((char *comment, char *text));
1360static void prt_dsc_ints __ARGS((char *comment, int count, int *ints));
1361static void prt_dsc_requirements __ARGS((int duplex, int tumble, int collate, int color, int num_copies));
1362static void prt_dsc_docmedia __ARGS((char *paper_name, double width, double height, double weight, char *colour, char *type));
1363static void prt_dsc_resources __ARGS((char *comment, char *type, char *strings));
1364static void prt_dsc_font_resource __ARGS((char *resource, struct prt_ps_font_S *ps_font));
1365static float to_device_units __ARGS((int idx, double physsize, int def_number));
1366static void prt_page_margins __ARGS((double width, double height, double *left, double *right, double *top, double *bottom));
1367static void prt_font_metrics __ARGS((int font_scale));
1368static int prt_get_cpl __ARGS((void));
1369static int prt_get_lpp __ARGS((void));
1370static int prt_add_resource __ARGS((struct prt_ps_resource_S *resource));
1371static int prt_resfile_next_line __ARGS((void));
1372static int prt_resfile_strncmp __ARGS((int offset, char *string, int len));
1373static int prt_resfile_skip_nonws __ARGS((int offset));
1374static int prt_resfile_skip_ws __ARGS((int offset));
1375static int prt_next_dsc __ARGS((struct prt_dsc_line_S *p_dsc_line));
1376#ifdef FEAT_MBYTE
1377static int prt_build_cid_fontname __ARGS((int font, char_u *name, int name_len));
1378static void prt_def_cidfont __ARGS((char *new_name, int height, char *cidfont));
1379static void prt_dup_cidfont __ARGS((char *original_name, char *new_name));
1380static int prt_match_encoding __ARGS((char *p_encoding, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_encoding_S **pp_mbenc));
1381static int prt_match_charset __ARGS((char *p_charset, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_charset_S **pp_mbchar));
1382#endif
1383
1384/*
1385 * Variables for the output PostScript file.
1386 */
1387static FILE *prt_ps_fd;
1388static int prt_file_error;
1389static char_u *prt_ps_file_name = NULL;
1390
1391/*
1392 * Various offsets and dimensions in default PostScript user space (points).
1393 * Used for text positioning calculations
1394 */
1395static float prt_page_width;
1396static float prt_page_height;
1397static float prt_left_margin;
1398static float prt_right_margin;
1399static float prt_top_margin;
1400static float prt_bottom_margin;
1401static float prt_line_height;
1402static float prt_first_line_height;
1403static float prt_char_width;
1404static float prt_number_width;
1405static float prt_bgcol_offset;
1406static float prt_pos_x_moveto = 0.0;
1407static float prt_pos_y_moveto = 0.0;
1408
1409/*
1410 * Various control variables used to decide when and how to change the
1411 * PostScript graphics state.
1412 */
1413static int prt_need_moveto;
1414static int prt_do_moveto;
1415static int prt_need_font;
1416static int prt_font;
1417static int prt_need_underline;
1418static int prt_underline;
1419static int prt_do_underline;
1420static int prt_need_fgcol;
1421static int prt_fgcol;
1422static int prt_need_bgcol;
1423static int prt_do_bgcol;
1424static int prt_bgcol;
1425static int prt_new_bgcol;
1426static int prt_attribute_change;
1427static float prt_text_run;
1428static int prt_page_num;
1429static int prt_bufsiz;
1430
1431/*
1432 * Variables controlling physical printing.
1433 */
1434static int prt_media;
1435static int prt_portrait;
1436static int prt_num_copies;
1437static int prt_duplex;
1438static int prt_tumble;
1439static int prt_collate;
1440
1441/*
1442 * Buffers used when generating PostScript output
1443 */
1444static char_u prt_line_buffer[257];
1445static garray_T prt_ps_buffer;
1446
1447# ifdef FEAT_MBYTE
1448static int prt_do_conv;
1449static vimconv_T prt_conv;
1450
1451static int prt_out_mbyte;
1452static int prt_custom_cmap;
1453static char prt_cmap[80];
1454static int prt_use_courier;
1455static int prt_in_ascii;
1456static int prt_half_width;
1457static char *prt_ascii_encoding;
1458static char_u prt_hexchar[] = "0123456789abcdef";
1459# endif
1460
1461 static void
1462prt_write_file_raw_len(buffer, bytes)
1463 char_u *buffer;
1464 int bytes;
1465{
1466 if (!prt_file_error
1467 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1468 != (size_t)bytes)
1469 {
1470 EMSG(_("E455: Error writing to PostScript output file"));
1471 prt_file_error = TRUE;
1472 }
1473}
1474
1475 static void
1476prt_write_file(buffer)
1477 char_u *buffer;
1478{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001479 prt_write_file_len(buffer, (int)STRLEN(buffer));
Bram Moolenaar81366db2005-07-24 21:16:51 +00001480}
1481
1482 static void
1483prt_write_file_len(buffer, bytes)
1484 char_u *buffer;
1485 int bytes;
1486{
1487#ifdef EBCDIC
1488 ebcdic2ascii(buffer, bytes);
1489#endif
1490 prt_write_file_raw_len(buffer, bytes);
1491}
1492
1493/*
1494 * Write a string.
1495 */
1496 static void
1497prt_write_string(s)
1498 char *s;
1499{
1500 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1501 prt_write_file(prt_line_buffer);
1502}
1503
1504/*
1505 * Write an int and a space.
1506 */
1507 static void
1508prt_write_int(i)
1509 int i;
1510{
1511 sprintf((char *)prt_line_buffer, "%d ", i);
1512 prt_write_file(prt_line_buffer);
1513}
1514
1515/*
1516 * Write a boolean and a space.
1517 */
1518 static void
1519prt_write_boolean(b)
1520 int b;
1521{
1522 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1523 prt_write_file(prt_line_buffer);
1524}
1525
1526/*
1527 * Write PostScript to re-encode and define the font.
1528 */
1529 static void
1530prt_def_font(new_name, encoding, height, font)
1531 char *new_name;
1532 char *encoding;
1533 int height;
1534 char *font;
1535{
1536 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1537 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1538 prt_write_file(prt_line_buffer);
1539#ifdef FEAT_MBYTE
1540 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001541 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
Bram Moolenaar81366db2005-07-24 21:16:51 +00001542 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1543 else
1544#endif
1545 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1546 "/%s %d /_%s ffs\n", new_name, height, new_name);
1547 prt_write_file(prt_line_buffer);
1548}
1549
1550#ifdef FEAT_MBYTE
1551/*
1552 * Write a line to define the CID font.
1553 */
1554 static void
1555prt_def_cidfont(new_name, height, cidfont)
1556 char *new_name;
1557 int height;
1558 char *cidfont;
1559{
1560 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1561 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1562 prt_write_file(prt_line_buffer);
1563 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1564 "/%s %d /_%s ffs\n", new_name, height, new_name);
1565 prt_write_file(prt_line_buffer);
1566}
1567
1568/*
1569 * Write a line to define a duplicate of a CID font
1570 */
1571 static void
1572prt_dup_cidfont(original_name, new_name)
1573 char *original_name;
1574 char *new_name;
1575{
1576 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1577 "/%s %s d\n", new_name, original_name);
1578 prt_write_file(prt_line_buffer);
1579}
1580#endif
1581
1582/*
1583 * Convert a real value into an integer and fractional part as integers, with
1584 * the fractional part being in the range [0,10^precision). The fractional part
1585 * is also rounded based on the precision + 1'th fractional digit.
1586 */
1587 static void
1588prt_real_bits(real, precision, pinteger, pfraction)
1589 double real;
1590 int precision;
1591 int *pinteger;
1592 int *pfraction;
1593{
1594 int i;
1595 int integer;
1596 float fraction;
1597
1598 integer = (int)real;
1599 fraction = (float)(real - integer);
1600 if (real < (double)integer)
1601 fraction = -fraction;
1602 for (i = 0; i < precision; i++)
1603 fraction *= 10.0;
1604
1605 *pinteger = integer;
1606 *pfraction = (int)(fraction + 0.5);
1607}
1608
1609/*
1610 * Write a real and a space. Save bytes if real value has no fractional part!
1611 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1612 * what decimal point character to use, but PS always requires a '.'.
1613 */
1614 static void
1615prt_write_real(val, prec)
1616 double val;
1617 int prec;
1618{
1619 int integer;
1620 int fraction;
1621
1622 prt_real_bits(val, prec, &integer, &fraction);
1623 /* Emit integer part */
1624 sprintf((char *)prt_line_buffer, "%d", integer);
1625 prt_write_file(prt_line_buffer);
1626 /* Only emit fraction if necessary */
1627 if (fraction != 0)
1628 {
1629 /* Remove any trailing zeros */
1630 while ((fraction % 10) == 0)
1631 {
1632 prec--;
1633 fraction /= 10;
1634 }
1635 /* Emit fraction left padded with zeros */
1636 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1637 prt_write_file(prt_line_buffer);
1638 }
1639 sprintf((char *)prt_line_buffer, " ");
1640 prt_write_file(prt_line_buffer);
1641}
1642
1643/*
1644 * Write a line to define a numeric variable.
1645 */
1646 static void
1647prt_def_var(name, value, prec)
1648 char *name;
1649 double value;
1650 int prec;
1651{
1652 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1653 "/%s ", name);
1654 prt_write_file(prt_line_buffer);
1655 prt_write_real(value, prec);
1656 sprintf((char *)prt_line_buffer, "d\n");
1657 prt_write_file(prt_line_buffer);
1658}
1659
1660/* Convert size from font space to user space at current font scale */
1661#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1662
1663 static void
1664prt_flush_buffer()
1665{
1666 if (prt_ps_buffer.ga_len > 0)
1667 {
1668 /* Any background color must be drawn first */
1669 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1670 {
1671 int r, g, b;
1672
1673 if (prt_do_moveto)
1674 {
1675 prt_write_real(prt_pos_x_moveto, 2);
1676 prt_write_real(prt_pos_y_moveto, 2);
1677 prt_write_string("m\n");
1678 prt_do_moveto = FALSE;
1679 }
1680
1681 /* Size of rect of background color on which text is printed */
1682 prt_write_real(prt_text_run, 2);
1683 prt_write_real(prt_line_height, 2);
1684
1685 /* Lastly add the color of the background */
1686 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1687 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1688 b = prt_new_bgcol & 0xff;
1689 prt_write_real(r / 255.0, 3);
1690 prt_write_real(g / 255.0, 3);
1691 prt_write_real(b / 255.0, 3);
1692 prt_write_string("bg\n");
1693 }
1694 /* Draw underlines before the text as it makes it slightly easier to
1695 * find the starting point.
1696 */
1697 if (prt_do_underline)
1698 {
1699 if (prt_do_moveto)
1700 {
1701 prt_write_real(prt_pos_x_moveto, 2);
1702 prt_write_real(prt_pos_y_moveto, 2);
1703 prt_write_string("m\n");
1704 prt_do_moveto = FALSE;
1705 }
1706
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001707 /* Underline length of text run */
Bram Moolenaar81366db2005-07-24 21:16:51 +00001708 prt_write_real(prt_text_run, 2);
1709 prt_write_string("ul\n");
1710 }
1711 /* Draw the text
1712 * Note: we write text out raw - EBCDIC conversion is handled in the
1713 * PostScript world via the font encoding vector. */
1714#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001715 if (prt_out_mbyte)
1716 prt_write_string("<");
1717 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001718#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001719 prt_write_string("(");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001720 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1721#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001722 if (prt_out_mbyte)
1723 prt_write_string(">");
1724 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00001725#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001726 prt_write_string(")");
Bram Moolenaar81366db2005-07-24 21:16:51 +00001727 /* Add a moveto if need be and use the appropriate show procedure */
1728 if (prt_do_moveto)
1729 {
1730 prt_write_real(prt_pos_x_moveto, 2);
1731 prt_write_real(prt_pos_y_moveto, 2);
1732 /* moveto and a show */
1733 prt_write_string("ms\n");
1734 prt_do_moveto = FALSE;
1735 }
1736 else /* Simple show */
1737 prt_write_string("s\n");
1738
1739 ga_clear(&prt_ps_buffer);
1740 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1741 }
1742}
1743
1744
1745 static void
1746prt_resource_name(filename, cookie)
1747 char_u *filename;
1748 void *cookie;
1749{
1750 char_u *resource_filename = cookie;
1751
1752 if (STRLEN(filename) >= MAXPATHL)
1753 *resource_filename = NUL;
1754 else
1755 STRCPY(resource_filename, filename);
1756}
1757
1758 static int
1759prt_find_resource(name, resource)
1760 char *name;
1761 struct prt_ps_resource_S *resource;
1762{
1763 char_u buffer[MAXPATHL + 1];
1764
1765 STRCPY(resource->name, name);
1766 /* Look for named resource file in runtimepath */
1767 STRCPY(buffer, "print");
1768 add_pathsep(buffer);
1769 STRCAT(buffer, name);
1770 STRCAT(buffer, ".ps");
1771 resource->filename[0] = NUL;
1772 return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
1773 resource->filename)
1774 && resource->filename[0] != NUL);
1775}
1776
1777/* PS CR and LF characters have platform independent values */
1778#define PSLF (0x0a)
1779#define PSCR (0x0d)
1780
1781/* Static buffer to read initial comments in a resource file, some can have a
1782 * couple of KB of comments! */
1783#define PRT_FILE_BUFFER_LEN (2048)
1784struct prt_resfile_buffer_S
1785{
1786 char_u buffer[PRT_FILE_BUFFER_LEN];
1787 int len;
1788 int line_start;
1789 int line_end;
1790};
1791
1792static struct prt_resfile_buffer_S prt_resfile;
1793
1794 static int
1795prt_resfile_next_line()
1796{
1797 int index;
1798
1799 /* Move to start of next line and then find end of line */
1800 index = prt_resfile.line_end + 1;
1801 while (index < prt_resfile.len)
1802 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001803 if (prt_resfile.buffer[index] != PSLF && prt_resfile.buffer[index]
1804 != PSCR)
1805 break;
1806 index++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001807 }
1808 prt_resfile.line_start = index;
1809
1810 while (index < prt_resfile.len)
1811 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001812 if (prt_resfile.buffer[index] == PSLF || prt_resfile.buffer[index]
1813 == PSCR)
1814 break;
1815 index++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001816 }
1817 prt_resfile.line_end = index;
1818
1819 return (index < prt_resfile.len);
1820}
1821
1822 static int
1823prt_resfile_strncmp(offset, string, len)
1824 int offset;
1825 char *string;
1826 int len;
1827{
1828 /* Force not equal if string is longer than remainder of line */
1829 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001830 return 1;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001831
1832 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001833 string, len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001834}
1835
1836 static int
1837prt_resfile_skip_nonws(offset)
1838 int offset;
1839{
1840 int index;
1841
1842 index = prt_resfile.line_start + offset;
1843 while (index < prt_resfile.line_end)
1844 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001845 if (isspace(prt_resfile.buffer[index]))
1846 return index - prt_resfile.line_start;
1847 index++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001848 }
1849 return -1;
1850}
1851
1852 static int
1853prt_resfile_skip_ws(offset)
1854 int offset;
1855{
1856 int index;
1857
1858 index = prt_resfile.line_start + offset;
1859 while (index < prt_resfile.line_end)
1860 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001861 if (!isspace(prt_resfile.buffer[index]))
1862 return index - prt_resfile.line_start;
1863 index++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001864 }
1865 return -1;
1866}
1867
1868/* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1869 * if a DSC comment is found, else false */
1870 static int
1871prt_next_dsc(p_dsc_line)
1872 struct prt_dsc_line_S *p_dsc_line;
1873{
1874 int comment;
1875 int offset;
1876
1877 /* Move to start of next line */
1878 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001879 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001880
1881 /* DSC comments always start %% */
1882 if (prt_resfile_strncmp(0, "%%", 2) != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001883 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001884
1885 /* Find type of DSC comment */
1886 for (comment = 0; comment < NUM_ELEMENTS(prt_dsc_table); comment++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001887 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1888 prt_dsc_table[comment].len) == 0)
1889 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001890
1891 if (comment != NUM_ELEMENTS(prt_dsc_table))
1892 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001893 /* Return type of comment */
1894 p_dsc_line->type = prt_dsc_table[comment].type;
1895 offset = prt_dsc_table[comment].len;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001896 }
1897 else
1898 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001899 /* Unrecognised DSC comment, skip to ws after comment leader */
1900 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1901 offset = prt_resfile_skip_nonws(0);
1902 if (offset == -1)
1903 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001904 }
1905
1906 /* Skip ws to comment value */
1907 offset = prt_resfile_skip_ws(offset);
1908 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001909 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001910
1911 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1912 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1913
1914 return TRUE;
1915}
1916
1917/* Improved hand crafted parser to get the type, title, and version number of a
1918 * PS resource file so the file details can be added to the DSC header comments.
1919 */
1920 static int
1921prt_open_resource(resource)
1922 struct prt_ps_resource_S *resource;
1923{
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001924 int offset;
1925 int seen_all;
1926 int seen_title;
1927 int seen_version;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001928 FILE *fd_resource;
1929 struct prt_dsc_line_S dsc_line;
1930
1931 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1932 if (fd_resource == NULL)
1933 {
1934 EMSG2(_("E624: Can't open file \"%s\""), resource->filename);
1935 return FALSE;
1936 }
1937 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1938
1939 /* Parse first line to ensure valid resource file */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001940 prt_resfile.len = (int)fread((char *)prt_resfile.buffer, sizeof(char_u),
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001941 PRT_FILE_BUFFER_LEN, fd_resource);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001942 if (ferror(fd_resource))
1943 {
1944 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1945 resource->filename);
1946 fclose(fd_resource);
1947 return FALSE;
1948 }
1949
1950 prt_resfile.line_end = -1;
1951 prt_resfile.line_start = 0;
1952 if (!prt_resfile_next_line())
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001953 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001954
1955 offset = 0;
1956
1957 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001958 (int)STRLEN(PRT_RESOURCE_HEADER)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001959 {
1960 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1961 resource->filename);
1962 fclose(fd_resource);
1963 return FALSE;
1964 }
1965
1966 /* Skip over any version numbers and following ws */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001967 offset += (int)STRLEN(PRT_RESOURCE_HEADER);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001968 offset = prt_resfile_skip_nonws(offset);
1969 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001970 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001971 offset = prt_resfile_skip_ws(offset);
1972 if (offset == -1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001973 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00001974
1975 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001976 (int)STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001977 {
1978 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1979 resource->filename);
1980 fclose(fd_resource);
1981 return FALSE;
1982 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 offset += (int)STRLEN(PRT_RESOURCE_RESOURCE);
Bram Moolenaar81366db2005-07-24 21:16:51 +00001984
1985 /* Decide type of resource in the file */
1986 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001987 (int)STRLEN(PRT_RESOURCE_PROCSET)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001988 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1989 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001990 (int)STRLEN(PRT_RESOURCE_ENCODING)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001991 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1992 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001993 (int)STRLEN(PRT_RESOURCE_CMAP)) == 0)
Bram Moolenaar81366db2005-07-24 21:16:51 +00001994 resource->type = PRT_RESOURCE_TYPE_CMAP;
1995 else
1996 {
1997 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1998 resource->filename);
1999 fclose(fd_resource);
2000 return FALSE;
2001 }
2002
2003 /* Look for title and version of resource */
2004 resource->title[0] = '\0';
2005 resource->version[0] = '\0';
2006 seen_title = FALSE;
2007 seen_version = FALSE;
2008 seen_all = FALSE;
2009 while (!seen_all && prt_next_dsc(&dsc_line))
2010 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002011 switch (dsc_line.type)
2012 {
2013 case PRT_DSC_TITLE_TYPE:
2014 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
2015 seen_title = TRUE;
2016 if (seen_version)
2017 seen_all = TRUE;
2018 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002019
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002020 case PRT_DSC_VERSION_TYPE:
2021 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
2022 seen_version = TRUE;
2023 if (seen_title)
2024 seen_all = TRUE;
2025 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002026
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002027 case PRT_DSC_ENDCOMMENTS_TYPE:
2028 /* Wont find title or resource after this comment, stop searching */
2029 seen_all = TRUE;
2030 break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002031
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002032 case PRT_DSC_MISC_TYPE:
2033 /* Not interested in whatever comment this line had */
2034 break;
2035 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002036 }
2037
2038 if (!seen_title || !seen_version)
2039 {
2040 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2041 resource->filename);
2042 fclose(fd_resource);
2043 return FALSE;
2044 }
2045
2046 fclose(fd_resource);
2047
2048 return TRUE;
2049}
2050
2051 static int
2052prt_check_resource(resource, version)
2053 struct prt_ps_resource_S *resource;
2054 char_u *version;
2055{
2056 /* Version number m.n should match, the revision number does not matter */
2057 if (STRNCMP(resource->version, version, STRLEN(version)))
2058 {
2059 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2060 resource->name);
2061 return FALSE;
2062 }
2063
2064 /* Other checks to be added as needed */
2065 return TRUE;
2066}
2067
2068 static void
2069prt_dsc_start()
2070{
2071 prt_write_string("%!PS-Adobe-3.0\n");
2072}
2073
2074 static void
2075prt_dsc_noarg(comment)
2076 char *comment;
2077{
2078 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2079 "%%%%%s\n", comment);
2080 prt_write_file(prt_line_buffer);
2081}
2082
2083 static void
2084prt_dsc_textline(comment, text)
2085 char *comment;
2086 char *text;
2087{
2088 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2089 "%%%%%s: %s\n", comment, text);
2090 prt_write_file(prt_line_buffer);
2091}
2092
2093 static void
2094prt_dsc_text(comment, text)
2095 char *comment;
2096 char *text;
2097{
2098 /* TODO - should scan 'text' for any chars needing escaping! */
2099 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2100 "%%%%%s: (%s)\n", comment, text);
2101 prt_write_file(prt_line_buffer);
2102}
2103
2104#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2105
2106 static void
2107prt_dsc_ints(comment, count, ints)
2108 char *comment;
2109 int count;
2110 int *ints;
2111{
2112 int i;
2113
2114 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2115 "%%%%%s:", comment);
2116 prt_write_file(prt_line_buffer);
2117
2118 for (i = 0; i < count; i++)
2119 {
2120 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2121 prt_write_file(prt_line_buffer);
2122 }
2123
2124 prt_write_string("\n");
2125}
2126
2127 static void
2128prt_dsc_resources(comment, type, string)
2129 char *comment; /* if NULL add to previous */
2130 char *type;
2131 char *string;
2132{
2133 if (comment != NULL)
2134 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2135 "%%%%%s: %s", comment, type);
2136 else
2137 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2138 "%%%%+ %s", type);
2139 prt_write_file(prt_line_buffer);
2140
2141 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2142 " %s\n", string);
2143 prt_write_file(prt_line_buffer);
2144}
2145
2146 static void
2147prt_dsc_font_resource(resource, ps_font)
2148 char *resource;
2149 struct prt_ps_font_S *ps_font;
2150{
2151 int i;
2152
2153 prt_dsc_resources(resource, "font",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002154 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002155 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002156 if (ps_font->ps_fontname[i] != NULL)
2157 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002158}
2159
2160 static void
2161prt_dsc_requirements(duplex, tumble, collate, color, num_copies)
2162 int duplex;
2163 int tumble;
2164 int collate;
2165 int color;
2166 int num_copies;
2167{
2168 /* Only output the comment if we need to.
2169 * Note: tumble is ignored if we are not duplexing
2170 */
2171 if (!(duplex || collate || color || (num_copies > 1)))
2172 return;
2173
2174 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2175 prt_write_file(prt_line_buffer);
2176
2177 if (duplex)
2178 {
2179 prt_write_string(" duplex");
2180 if (tumble)
2181 prt_write_string("(tumble)");
2182 }
2183 if (collate)
2184 prt_write_string(" collate");
2185 if (color)
2186 prt_write_string(" color");
2187 if (num_copies > 1)
2188 {
2189 prt_write_string(" numcopies(");
2190 /* Note: no space wanted so dont use prt_write_int() */
2191 sprintf((char *)prt_line_buffer, "%d", num_copies);
2192 prt_write_file(prt_line_buffer);
2193 prt_write_string(")");
2194 }
2195 prt_write_string("\n");
2196}
2197
2198 static void
2199prt_dsc_docmedia(paper_name, width, height, weight, colour, type)
2200 char *paper_name;
2201 double width;
2202 double height;
2203 double weight;
2204 char *colour;
2205 char *type;
2206{
2207 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2208 "%%%%DocumentMedia: %s ", paper_name);
2209 prt_write_file(prt_line_buffer);
2210 prt_write_real(width, 2);
2211 prt_write_real(height, 2);
2212 prt_write_real(weight, 2);
2213 if (colour == NULL)
2214 prt_write_string("()");
2215 else
2216 prt_write_string(colour);
2217 prt_write_string(" ");
2218 if (type == NULL)
2219 prt_write_string("()");
2220 else
2221 prt_write_string(type);
2222 prt_write_string("\n");
2223}
2224
2225 void
2226mch_print_cleanup()
2227{
2228#ifdef FEAT_MBYTE
2229 if (prt_out_mbyte)
2230 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002231 int i;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002232
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002233 /* Free off all CID font names created, but first clear duplicate
2234 * pointers to the same string (when the same font is used for more than
2235 * one style).
2236 */
2237 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2238 {
2239 if (prt_ps_mb_font.ps_fontname[i] != NULL)
2240 vim_free(prt_ps_mb_font.ps_fontname[i]);
2241 prt_ps_mb_font.ps_fontname[i] = NULL;
2242 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002243 }
2244
2245 if (prt_do_conv)
2246 {
2247 convert_setup(&prt_conv, NULL, NULL);
2248 prt_do_conv = FALSE;
2249 }
2250#endif
2251 if (prt_ps_fd != NULL)
2252 {
2253 fclose(prt_ps_fd);
2254 prt_ps_fd = NULL;
2255 prt_file_error = FALSE;
2256 }
2257 if (prt_ps_file_name != NULL)
2258 {
2259 vim_free(prt_ps_file_name);
2260 prt_ps_file_name = NULL;
2261 }
2262}
2263
2264 static float
2265to_device_units(idx, physsize, def_number)
2266 int idx;
2267 double physsize;
2268 int def_number;
2269{
2270 float ret;
2271 int u;
2272 int nr;
2273
2274 u = prt_get_unit(idx);
2275 if (u == PRT_UNIT_NONE)
2276 {
2277 u = PRT_UNIT_PERC;
2278 nr = def_number;
2279 }
2280 else
2281 nr = printer_opts[idx].number;
2282
2283 switch (u)
2284 {
2285 case PRT_UNIT_INCH:
2286 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2287 break;
2288 case PRT_UNIT_MM:
2289 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2290 break;
2291 case PRT_UNIT_POINT:
2292 ret = (float)nr;
2293 break;
2294 case PRT_UNIT_PERC:
2295 default:
2296 ret = (float)(physsize * nr) / 100;
2297 break;
2298 }
2299
2300 return ret;
2301}
2302
2303/*
2304 * Calculate margins for given width and height from printoptions settings.
2305 */
2306 static void
2307prt_page_margins(width, height, left, right, top, bottom)
2308 double width;
2309 double height;
2310 double *left;
2311 double *right;
2312 double *top;
2313 double *bottom;
2314{
2315 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2316 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2317 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2318 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2319}
2320
2321 static void
2322prt_font_metrics(font_scale)
2323 int font_scale;
2324{
2325 prt_line_height = (float)font_scale;
2326 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2327}
2328
2329
2330 static int
2331prt_get_cpl()
2332{
2333 if (prt_use_number())
2334 {
2335 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2336#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002337 /* If we are outputting multi-byte characters then line numbers will be
2338 * printed with half width characters
2339 */
2340 if (prt_out_mbyte)
2341 prt_number_width /= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002342#endif
2343 prt_left_margin += prt_number_width;
2344 }
2345 else
2346 prt_number_width = 0.0;
2347
2348 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2349}
2350
2351#ifdef FEAT_MBYTE
2352 static int
2353prt_build_cid_fontname(font, name, name_len)
2354 int font;
2355 char_u *name;
2356 int name_len;
2357{
2358 char *fontname;
2359
2360 fontname = (char *)alloc(name_len + 1);
2361 if (fontname == NULL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002362 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002363 vim_strncpy((char_u *)fontname, name, name_len);
2364 prt_ps_mb_font.ps_fontname[font] = fontname;
2365
2366 return TRUE;
2367}
2368#endif
2369
2370/*
2371 * Get number of lines of text that fit on a page (excluding the header).
2372 */
2373 static int
2374prt_get_lpp()
2375{
2376 int lpp;
2377
2378 /*
2379 * Calculate offset to lower left corner of background rect based on actual
2380 * font height (based on its bounding box) and the line height, handling the
2381 * case where the font height can exceed the line height.
2382 */
2383 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2384 prt_ps_font->bbox_min_y);
2385 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2386 {
2387 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2388 (1000.0 - (prt_ps_font->bbox_max_y -
2389 prt_ps_font->bbox_min_y)) / 2);
2390 }
2391
2392 /* Get height for topmost line based on background rect offset. */
2393 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2394
2395 /* Calculate lpp */
2396 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2397
2398 /* Adjust top margin if there is a header */
2399 prt_top_margin -= prt_line_height * prt_header_height();
2400
2401 return lpp - prt_header_height();
2402}
2403
2404#ifdef FEAT_MBYTE
2405 static int
2406prt_match_encoding(p_encoding, p_cmap, pp_mbenc)
2407 char *p_encoding;
2408 struct prt_ps_mbfont_S *p_cmap;
2409 struct prt_ps_encoding_S **pp_mbenc;
2410{
2411 int mbenc;
2412 int enc_len;
2413 struct prt_ps_encoding_S *p_mbenc;
2414
2415 *pp_mbenc = NULL;
2416 /* Look for recognised encoding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002417 enc_len = (int)STRLEN(p_encoding);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002418 p_mbenc = p_cmap->encodings;
2419 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2420 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002421 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2422 {
2423 *pp_mbenc = p_mbenc;
2424 return TRUE;
2425 }
2426 p_mbenc++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002427 }
2428 return FALSE;
2429}
2430
2431 static int
2432prt_match_charset(p_charset, p_cmap, pp_mbchar)
2433 char *p_charset;
2434 struct prt_ps_mbfont_S *p_cmap;
2435 struct prt_ps_charset_S **pp_mbchar;
2436{
2437 int mbchar;
2438 int char_len;
2439 struct prt_ps_charset_S *p_mbchar;
2440
2441 /* Look for recognised character set, using default if one is not given */
2442 if (*p_charset == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002443 p_charset = p_cmap->defcs;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002444 char_len = (int)STRLEN(p_charset);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002445 p_mbchar = p_cmap->charsets;
2446 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2447 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002448 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2449 {
2450 *pp_mbchar = p_mbchar;
2451 return TRUE;
2452 }
2453 p_mbchar++;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002454 }
2455 return FALSE;
2456}
2457#endif
2458
2459/*ARGSUSED*/
2460 int
2461mch_print_init(psettings, jobname, forceit)
2462 prt_settings_T *psettings;
2463 char_u *jobname;
2464 int forceit;
2465{
2466 int i;
2467 char *paper_name;
2468 int paper_strlen;
2469 int fontsize;
2470 char_u *p;
2471 double left;
2472 double right;
2473 double top;
2474 double bottom;
2475#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002476 int props;
2477 int cmap = 0;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002478 char_u *p_encoding;
2479 struct prt_ps_encoding_S *p_mbenc;
2480 struct prt_ps_encoding_S *p_mbenc_first;
2481 struct prt_ps_charset_S *p_mbchar;
2482#endif
2483
2484#if 0
2485 /*
2486 * TODO:
2487 * If "forceit" is false: pop up a dialog to select:
2488 * - printer name
2489 * - copies
2490 * - collated/uncollated
2491 * - duplex off/long side/short side
2492 * - paper size
2493 * - portrait/landscape
2494 * - font size
2495 *
2496 * If "forceit" is true: use the default printer and settings
2497 */
2498 if (forceit)
2499 s_pd.Flags |= PD_RETURNDEFAULT;
2500#endif
2501
2502 /*
2503 * Set up font and encoding.
2504 */
2505#ifdef FEAT_MBYTE
2506 p_encoding = enc_skip(p_penc);
2507 if (*p_encoding == NUL)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002508 p_encoding = enc_skip(p_enc);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002509
Bram Moolenaar14716812006-05-04 21:54:08 +00002510 /* Look for a multi-byte font that matches the encoding and character set.
2511 * Only look if multi-byte character set is defined, or using multi-byte
2512 * encoding other than Unicode. This is because a Unicode encoding does not
2513 * uniquely identify a CJK character set to use. */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002514 p_mbenc = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002515 props = enc_canon_props(p_encoding);
Bram Moolenaar14716812006-05-04 21:54:08 +00002516 if (!(props & ENC_8BIT) && ((*p_pmcs != NUL) || !(props & ENC_UNICODE)))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002517 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002518 p_mbenc_first = NULL;
2519 p_mbchar = NULL;
2520 for (cmap = 0; cmap < NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
2521 if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002522 &p_mbenc))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002523 {
2524 if (p_mbenc_first == NULL)
2525 p_mbenc_first = p_mbenc;
2526 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
Bram Moolenaar81366db2005-07-24 21:16:51 +00002527 &p_mbchar))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002528 break;
2529 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002530
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002531 /* Use first encoding matched if no charset matched */
2532 if (p_mbchar == NULL && p_mbenc_first != NULL)
2533 p_mbenc = p_mbenc_first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002534 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002535
2536 prt_out_mbyte = (p_mbenc != NULL);
2537 if (prt_out_mbyte)
2538 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002539 /* Build CMap name - will be same for all multi-byte fonts used */
2540 prt_cmap[0] = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002541
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002542 prt_custom_cmap = (p_mbchar == NULL);
2543 if (!prt_custom_cmap)
2544 {
2545 /* Check encoding and character set are compatible */
2546 if ((p_mbenc->needs_charset & p_mbchar->has_charset) == 0)
2547 {
2548 EMSG(_("E673: Incompatible multi-byte encoding and character set."));
2549 return FALSE;
2550 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002551
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002552 /* Add charset name if not empty */
2553 if (p_mbchar->cmap_charset != NULL)
2554 {
2555 vim_strncpy((char_u *)prt_cmap,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002556 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002557 STRCAT(prt_cmap, "-");
2558 }
2559 }
2560 else
2561 {
2562 /* Add custom CMap character set name */
2563 if (*p_pmcs == NUL)
2564 {
2565 EMSG(_("E674: printmbcharset cannot be empty with multi-byte encoding."));
2566 return FALSE;
2567 }
2568 vim_strncpy((char_u *)prt_cmap, p_pmcs, sizeof(prt_cmap) - 3);
2569 STRCAT(prt_cmap, "-");
2570 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002571
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002572 /* CMap name ends with (optional) encoding name and -H for horizontal */
2573 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
Bram Moolenaar81366db2005-07-24 21:16:51 +00002574 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002575 {
2576 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2577 STRCAT(prt_cmap, "-");
2578 }
2579 STRCAT(prt_cmap, "H");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002580
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002581 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2582 {
2583 EMSG(_("E675: No default font specified for multi-byte printing."));
2584 return FALSE;
2585 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002586
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002587 /* Derive CID font names with fallbacks if not defined */
2588 if (!prt_build_cid_fontname(PRT_PS_FONT_ROMAN,
2589 mbfont_opts[OPT_MBFONT_REGULAR].string,
2590 mbfont_opts[OPT_MBFONT_REGULAR].strlen))
2591 return FALSE;
2592 if (mbfont_opts[OPT_MBFONT_BOLD].present)
2593 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLD,
2594 mbfont_opts[OPT_MBFONT_BOLD].string,
2595 mbfont_opts[OPT_MBFONT_BOLD].strlen))
2596 return FALSE;
2597 if (mbfont_opts[OPT_MBFONT_OBLIQUE].present)
2598 if (!prt_build_cid_fontname(PRT_PS_FONT_OBLIQUE,
2599 mbfont_opts[OPT_MBFONT_OBLIQUE].string,
2600 mbfont_opts[OPT_MBFONT_OBLIQUE].strlen))
2601 return FALSE;
2602 if (mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].present)
2603 if (!prt_build_cid_fontname(PRT_PS_FONT_BOLDOBLIQUE,
Bram Moolenaar81366db2005-07-24 21:16:51 +00002604 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2605 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002606 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002607
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002608 /* Check if need to use Courier for ASCII code range, and if so pick up
2609 * the encoding to use */
2610 prt_use_courier = mbfont_opts[OPT_MBFONT_USECOURIER].present &&
2611 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_USECOURIER].string[0]) == 'y');
2612 if (prt_use_courier)
2613 {
2614 /* Use national ASCII variant unless ASCII wanted */
2615 if (mbfont_opts[OPT_MBFONT_ASCII].present &&
2616 (TOLOWER_ASC(mbfont_opts[OPT_MBFONT_ASCII].string[0]) == 'y'))
2617 prt_ascii_encoding = "ascii";
2618 else
2619 prt_ascii_encoding = prt_ps_mbfonts[cmap].ascii_enc;
2620 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00002621
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002622 prt_ps_font = &prt_ps_mb_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002623 }
2624 else
2625#endif
2626 {
2627#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002628 prt_use_courier = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002629#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002630 prt_ps_font = &prt_ps_courier_font;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002631 }
2632
2633 /*
2634 * Find the size of the paper and set the margins.
2635 */
2636 prt_portrait = (!printer_opts[OPT_PRINT_PORTRAIT].present
2637 || TOLOWER_ASC(printer_opts[OPT_PRINT_PORTRAIT].string[0]) == 'y');
2638 if (printer_opts[OPT_PRINT_PAPER].present)
2639 {
2640 paper_name = (char *)printer_opts[OPT_PRINT_PAPER].string;
2641 paper_strlen = printer_opts[OPT_PRINT_PAPER].strlen;
2642 }
2643 else
2644 {
2645 paper_name = "A4";
2646 paper_strlen = 2;
2647 }
2648 for (i = 0; i < PRT_MEDIASIZE_LEN; ++i)
2649 if (STRLEN(prt_mediasize[i].name) == (unsigned)paper_strlen
2650 && STRNICMP(prt_mediasize[i].name, paper_name,
2651 paper_strlen) == 0)
2652 break;
2653 if (i == PRT_MEDIASIZE_LEN)
2654 i = 0;
2655 prt_media = i;
2656
2657 /*
2658 * Set PS pagesize based on media dimensions and print orientation.
2659 * Note: Media and page sizes have defined meanings in PostScript and should
2660 * be kept distinct. Media is the paper (or transparency, or ...) that is
2661 * printed on, whereas the page size is the area that the PostScript
2662 * interpreter renders into.
2663 */
2664 if (prt_portrait)
2665 {
2666 prt_page_width = prt_mediasize[i].width;
2667 prt_page_height = prt_mediasize[i].height;
2668 }
2669 else
2670 {
2671 prt_page_width = prt_mediasize[i].height;
2672 prt_page_height = prt_mediasize[i].width;
2673 }
2674
2675 /*
2676 * Set PS page margins based on the PS pagesize, not the mediasize - this
2677 * needs to be done before the cpl and lpp are calculated.
2678 */
2679 prt_page_margins(prt_page_width, prt_page_height, &left, &right, &top,
2680 &bottom);
2681 prt_left_margin = (float)left;
2682 prt_right_margin = (float)right;
2683 prt_top_margin = (float)top;
2684 prt_bottom_margin = (float)bottom;
2685
2686 /*
2687 * Set up the font size.
2688 */
2689 fontsize = PRT_PS_DEFAULT_FONTSIZE;
2690 for (p = p_pfn; (p = vim_strchr(p, ':')) != NULL; ++p)
2691 if (p[1] == 'h' && VIM_ISDIGIT(p[2]))
2692 fontsize = atoi((char *)p + 2);
2693 prt_font_metrics(fontsize);
2694
2695 /*
2696 * Return the number of characters per line, and lines per page for the
2697 * generic print code.
2698 */
2699 psettings->chars_per_line = prt_get_cpl();
2700 psettings->lines_per_page = prt_get_lpp();
2701
2702 /* Catch margin settings that leave no space for output! */
2703 if (psettings->chars_per_line <= 0 || psettings->lines_per_page <= 0)
2704 return FAIL;
2705
2706 /*
2707 * Sort out the number of copies to be printed. PS by default will do
2708 * uncollated copies for you, so once we know how many uncollated copies are
2709 * wanted cache it away and lie to the generic code that we only want one
2710 * uncollated copy.
2711 */
2712 psettings->n_collated_copies = 1;
2713 psettings->n_uncollated_copies = 1;
2714 prt_num_copies = 1;
2715 prt_collate = (!printer_opts[OPT_PRINT_COLLATE].present
2716 || TOLOWER_ASC(printer_opts[OPT_PRINT_COLLATE].string[0]) == 'y');
2717 if (prt_collate)
2718 {
2719 /* TODO: Get number of collated copies wanted. */
2720 psettings->n_collated_copies = 1;
2721 }
2722 else
2723 {
2724 /* TODO: Get number of uncollated copies wanted and update the cached
2725 * count.
2726 */
2727 prt_num_copies = 1;
2728 }
2729
2730 psettings->jobname = jobname;
2731
2732 /*
2733 * Set up printer duplex and tumble based on Duplex option setting - default
2734 * is long sided duplex printing (i.e. no tumble).
2735 */
2736 prt_duplex = TRUE;
2737 prt_tumble = FALSE;
2738 psettings->duplex = 1;
2739 if (printer_opts[OPT_PRINT_DUPLEX].present)
2740 {
2741 if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "off", 3) == 0)
2742 {
2743 prt_duplex = FALSE;
2744 psettings->duplex = 0;
2745 }
2746 else if (STRNICMP(printer_opts[OPT_PRINT_DUPLEX].string, "short", 5)
2747 == 0)
2748 prt_tumble = TRUE;
2749 }
2750
2751 /* For now user abort not supported */
2752 psettings->user_abort = 0;
2753
2754 /* If the user didn't specify a file name, use a temp file. */
2755 if (psettings->outfile == NULL)
2756 {
2757 prt_ps_file_name = vim_tempname('p');
2758 if (prt_ps_file_name == NULL)
2759 {
2760 EMSG(_(e_notmp));
2761 return FAIL;
2762 }
2763 prt_ps_fd = mch_fopen((char *)prt_ps_file_name, WRITEBIN);
2764 }
2765 else
2766 {
2767 p = expand_env_save(psettings->outfile);
2768 if (p != NULL)
2769 {
2770 prt_ps_fd = mch_fopen((char *)p, WRITEBIN);
2771 vim_free(p);
2772 }
2773 }
2774 if (prt_ps_fd == NULL)
2775 {
2776 EMSG(_("E324: Can't open PostScript output file"));
2777 mch_print_cleanup();
2778 return FAIL;
2779 }
2780
2781 prt_bufsiz = psettings->chars_per_line;
2782#ifdef FEAT_MBYTE
2783 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002784 prt_bufsiz *= 2;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002785#endif
2786 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
2787
2788 prt_page_num = 0;
2789
2790 prt_attribute_change = FALSE;
2791 prt_need_moveto = FALSE;
2792 prt_need_font = FALSE;
2793 prt_need_fgcol = FALSE;
2794 prt_need_bgcol = FALSE;
2795 prt_need_underline = FALSE;
2796
2797 prt_file_error = FALSE;
2798
2799 return OK;
2800}
2801
2802 static int
2803prt_add_resource(resource)
2804 struct prt_ps_resource_S *resource;
2805{
2806 FILE* fd_resource;
2807 char_u resource_buffer[512];
2808 size_t bytes_read;
2809
2810 fd_resource = mch_fopen((char *)resource->filename, READBIN);
2811 if (fd_resource == NULL)
2812 {
2813 EMSG2(_("E456: Can't open file \"%s\""), resource->filename);
2814 return FALSE;
2815 }
2816 prt_dsc_resources("BeginResource", prt_resource_types[resource->type],
2817 (char *)resource->title);
2818
2819 prt_dsc_textline("BeginDocument", (char *)resource->filename);
2820
2821 for (;;)
2822 {
2823 bytes_read = fread((char *)resource_buffer, sizeof(char_u),
2824 sizeof(resource_buffer), fd_resource);
2825 if (ferror(fd_resource))
2826 {
2827 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
2828 resource->filename);
2829 fclose(fd_resource);
2830 return FALSE;
2831 }
2832 if (bytes_read == 0)
2833 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002834 prt_write_file_raw_len(resource_buffer, (int)bytes_read);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002835 if (prt_file_error)
2836 {
2837 fclose(fd_resource);
2838 return FALSE;
2839 }
2840 }
2841 fclose(fd_resource);
2842
2843 prt_dsc_noarg("EndDocument");
2844
2845 prt_dsc_noarg("EndResource");
2846
2847 return TRUE;
2848}
2849
2850 int
2851mch_print_begin(psettings)
2852 prt_settings_T *psettings;
2853{
2854 time_t now;
2855 int bbox[4];
2856 char *p_time;
2857 double left;
2858 double right;
2859 double top;
2860 double bottom;
2861 struct prt_ps_resource_S res_prolog;
2862 struct prt_ps_resource_S res_encoding;
2863 char buffer[256];
2864 char_u *p_encoding;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002865 char_u *p;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002866#ifdef FEAT_MBYTE
2867 struct prt_ps_resource_S res_cidfont;
2868 struct prt_ps_resource_S res_cmap;
2869#endif
2870
2871 /*
2872 * PS DSC Header comments - no PS code!
2873 */
2874 prt_dsc_start();
2875 prt_dsc_textline("Title", (char *)psettings->jobname);
2876 if (!get_user_name((char_u *)buffer, 256))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002877 STRCPY(buffer, "Unknown");
Bram Moolenaar81366db2005-07-24 21:16:51 +00002878 prt_dsc_textline("For", buffer);
2879 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2880 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2881 now = time(NULL);
2882 p_time = ctime(&now);
2883 /* Note: ctime() adds a \n so we have to remove it :-( */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002884 p = vim_strchr((char_u *)p_time, '\n');
2885 if (p != NULL)
2886 *p = NUL;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002887 prt_dsc_textline("CreationDate", p_time);
2888 prt_dsc_textline("DocumentData", "Clean8Bit");
2889 prt_dsc_textline("Orientation", "Portrait");
2890 prt_dsc_atend("Pages");
2891 prt_dsc_textline("PageOrder", "Ascend");
2892 /* The bbox does not change with orientation - it is always in the default
2893 * user coordinate system! We have to recalculate right and bottom
2894 * coordinates based on the font metrics for the bbox to be accurate. */
2895 prt_page_margins(prt_mediasize[prt_media].width,
2896 prt_mediasize[prt_media].height,
2897 &left, &right, &top, &bottom);
2898 bbox[0] = (int)left;
2899 if (prt_portrait)
2900 {
2901 /* In portrait printing the fixed point is the top left corner so we
2902 * derive the bbox from that point. We have the expected cpl chars
2903 * across the media and lpp lines down the media.
2904 */
2905 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2906 * prt_line_height);
2907 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2908 + 0.5);
2909 bbox[3] = (int)(top + 0.5);
2910 }
2911 else
2912 {
2913 /* In landscape printing the fixed point is the bottom left corner so we
2914 * derive the bbox from that point. We have lpp chars across the media
2915 * and cpl lines up the media.
2916 */
2917 bbox[1] = (int)bottom;
2918 bbox[2] = (int)(left + ((psettings->lines_per_page
2919 + prt_header_height()) * prt_line_height) + 0.5);
2920 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2921 + 0.5);
2922 }
2923 prt_dsc_ints("BoundingBox", 4, bbox);
2924 /* The media width and height does not change with landscape printing! */
2925 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2926 prt_mediasize[prt_media].width,
2927 prt_mediasize[prt_media].height,
2928 (double)0, NULL, NULL);
2929 /* Define fonts needed */
2930#ifdef FEAT_MBYTE
2931 if (!prt_out_mbyte || prt_use_courier)
2932#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002933 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002934#ifdef FEAT_MBYTE
2935 if (prt_out_mbyte)
2936 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002937 prt_dsc_font_resource((prt_use_courier ? NULL
2938 : "DocumentNeededResources"), &prt_ps_mb_font);
2939 if (!prt_custom_cmap)
2940 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00002941 }
2942#endif
2943
2944 /* Search for external resources VIM supplies */
2945 if (!prt_find_resource("prolog", &res_prolog))
2946 {
2947 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
2948 return FALSE;
2949 }
2950 if (!prt_open_resource(&res_prolog))
2951 return FALSE;
2952 if (!prt_check_resource(&res_prolog, PRT_PROLOG_VERSION))
2953 return FALSE;
2954#ifdef FEAT_MBYTE
2955 if (prt_out_mbyte)
2956 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002957 /* Look for required version of multi-byte printing procset */
2958 if (!prt_find_resource("cidfont", &res_cidfont))
2959 {
2960 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
2961 return FALSE;
2962 }
2963 if (!prt_open_resource(&res_cidfont))
2964 return FALSE;
2965 if (!prt_check_resource(&res_cidfont, PRT_CID_PROLOG_VERSION))
2966 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002967 }
2968#endif
2969
2970 /* Find an encoding to use for printing.
2971 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2972 * that cannot be found then default to "latin1".
2973 * Note: VIM specific encoding header is always skipped.
2974 */
2975#ifdef FEAT_MBYTE
2976 if (!prt_out_mbyte)
2977 {
2978#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002979 p_encoding = enc_skip(p_penc);
2980 if (*p_encoding == NUL
2981 || !prt_find_resource((char *)p_encoding, &res_encoding))
2982 {
2983 /* 'printencoding' not set or not supported - find alternate */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002984#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002985 int props;
Bram Moolenaar81366db2005-07-24 21:16:51 +00002986
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002987 p_encoding = enc_skip(p_enc);
2988 props = enc_canon_props(p_encoding);
2989 if (!(props & ENC_8BIT)
2990 || !prt_find_resource((char *)p_encoding, &res_encoding))
2991 /* 8-bit 'encoding' is not supported */
Bram Moolenaar81366db2005-07-24 21:16:51 +00002992#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002993 {
2994 /* Use latin1 as default printing encoding */
2995 p_encoding = (char_u *)"latin1";
2996 if (!prt_find_resource((char *)p_encoding, &res_encoding))
2997 {
2998 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
2999 p_encoding);
3000 return FALSE;
3001 }
3002 }
3003 }
3004 if (!prt_open_resource(&res_encoding))
3005 return FALSE;
3006 /* For the moment there are no checks on encoding resource files to
3007 * perform */
Bram Moolenaar81366db2005-07-24 21:16:51 +00003008#ifdef FEAT_MBYTE
3009 }
3010 else
3011 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003012 p_encoding = enc_skip(p_penc);
3013 if (*p_encoding == NUL)
3014 p_encoding = enc_skip(p_enc);
3015 if (prt_use_courier)
3016 {
3017 /* Include ASCII range encoding vector */
3018 if (!prt_find_resource(prt_ascii_encoding, &res_encoding))
3019 {
3020 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003021 prt_ascii_encoding);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003022 return FALSE;
3023 }
3024 if (!prt_open_resource(&res_encoding))
3025 return FALSE;
3026 /* For the moment there are no checks on encoding resource files to
3027 * perform */
3028 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003029 }
3030
3031 prt_conv.vc_type = CONV_NONE;
3032 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003033 /* Set up encoding conversion if required */
Bram Moolenaar81366db2005-07-24 21:16:51 +00003034 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
3035 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003036 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003037 p_encoding);
3038 return FALSE;
3039 }
3040 prt_do_conv = TRUE;
3041 }
3042 prt_do_conv = prt_conv.vc_type != CONV_NONE;
3043
3044 if (prt_out_mbyte && prt_custom_cmap)
3045 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003046 /* Find user supplied CMap */
3047 if (!prt_find_resource(prt_cmap, &res_cmap))
3048 {
3049 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
Bram Moolenaar81366db2005-07-24 21:16:51 +00003050 prt_cmap);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003051 return FALSE;
3052 }
3053 if (!prt_open_resource(&res_cmap))
3054 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003055 }
3056#endif
3057
3058 /* List resources supplied */
3059 STRCPY(buffer, res_prolog.title);
3060 STRCAT(buffer, " ");
3061 STRCAT(buffer, res_prolog.version);
3062 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
3063#ifdef FEAT_MBYTE
3064 if (prt_out_mbyte)
3065 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003066 STRCPY(buffer, res_cidfont.title);
3067 STRCAT(buffer, " ");
3068 STRCAT(buffer, res_cidfont.version);
3069 prt_dsc_resources(NULL, "procset", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003070
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003071 if (prt_custom_cmap)
3072 {
3073 STRCPY(buffer, res_cmap.title);
3074 STRCAT(buffer, " ");
3075 STRCAT(buffer, res_cmap.version);
3076 prt_dsc_resources(NULL, "cmap", buffer);
3077 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003078 }
3079 if (!prt_out_mbyte || prt_use_courier)
3080#endif
3081 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003082 STRCPY(buffer, res_encoding.title);
3083 STRCAT(buffer, " ");
3084 STRCAT(buffer, res_encoding.version);
3085 prt_dsc_resources(NULL, "encoding", buffer);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003086 }
3087 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3088#ifdef FEAT_SYN_HL
3089 psettings->do_syntax
3090#else
3091 0
3092#endif
3093 , prt_num_copies);
3094 prt_dsc_noarg("EndComments");
3095
3096 /*
3097 * PS Document page defaults
3098 */
3099 prt_dsc_noarg("BeginDefaults");
3100
3101 /* List font resources most likely common to all pages */
3102#ifdef FEAT_MBYTE
3103 if (!prt_out_mbyte || prt_use_courier)
3104#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003105 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003106#ifdef FEAT_MBYTE
3107 if (prt_out_mbyte)
3108 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003109 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3110 &prt_ps_mb_font);
3111 if (!prt_custom_cmap)
3112 prt_dsc_resources(NULL, "cmap", prt_cmap);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003113 }
3114#endif
3115
3116 /* Paper will be used for all pages */
3117 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3118
3119 prt_dsc_noarg("EndDefaults");
3120
3121 /*
3122 * PS Document prolog inclusion - all required procsets.
3123 */
3124 prt_dsc_noarg("BeginProlog");
3125
3126 /* Add required procsets - NOTE: order is important! */
3127 if (!prt_add_resource(&res_prolog))
3128 return FALSE;
3129#ifdef FEAT_MBYTE
3130 if (prt_out_mbyte)
3131 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003132 /* Add CID font procset, and any user supplied CMap */
3133 if (!prt_add_resource(&res_cidfont))
3134 return FALSE;
3135 if (prt_custom_cmap && !prt_add_resource(&res_cmap))
3136 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003137 }
3138#endif
3139
3140#ifdef FEAT_MBYTE
3141 if (!prt_out_mbyte || prt_use_courier)
3142#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003143 /* There will be only one Roman font encoding to be included in the PS
3144 * file. */
3145 if (!prt_add_resource(&res_encoding))
3146 return FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003147
3148 prt_dsc_noarg("EndProlog");
3149
3150 /*
3151 * PS Document setup - must appear after the prolog
3152 */
3153 prt_dsc_noarg("BeginSetup");
3154
3155 /* Device setup - page size and number of uncollated copies */
3156 prt_write_int((int)prt_mediasize[prt_media].width);
3157 prt_write_int((int)prt_mediasize[prt_media].height);
3158 prt_write_int(0);
3159 prt_write_string("sps\n");
3160 prt_write_int(prt_num_copies);
3161 prt_write_string("nc\n");
3162 prt_write_boolean(prt_duplex);
3163 prt_write_boolean(prt_tumble);
3164 prt_write_string("dt\n");
3165 prt_write_boolean(prt_collate);
3166 prt_write_string("c\n");
3167
3168 /* Font resource inclusion and definition */
3169#ifdef FEAT_MBYTE
3170 if (!prt_out_mbyte || prt_use_courier)
3171 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003172 /* When using Courier for ASCII range when printing multi-byte, need to
3173 * pick up ASCII encoding to use with it. */
3174 if (prt_use_courier)
3175 p_encoding = (char_u *)prt_ascii_encoding;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003176#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003177 prt_dsc_resources("IncludeResource", "font",
3178 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3179 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3180 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3181 prt_dsc_resources("IncludeResource", "font",
3182 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3183 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3184 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3185 prt_dsc_resources("IncludeResource", "font",
3186 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3187 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3188 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3189 prt_dsc_resources("IncludeResource", "font",
3190 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3191 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3192 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003193#ifdef FEAT_MBYTE
3194 }
3195 if (prt_out_mbyte)
3196 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003197 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3198 * not have an italic form being a western style, so where no font is
3199 * defined for these faces VIM falls back to an existing face.
3200 * Note: if using Courier for the ASCII range then the printout will
3201 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3202 */
3203 prt_dsc_resources("IncludeResource", "font",
3204 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3205 if (!prt_custom_cmap)
3206 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3207 prt_def_cidfont("CF0", (int)prt_line_height,
3208 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003209
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003210 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3211 {
3212 prt_dsc_resources("IncludeResource", "font",
3213 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3214 if (!prt_custom_cmap)
3215 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3216 prt_def_cidfont("CF1", (int)prt_line_height,
3217 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3218 }
3219 else
3220 /* Use ROMAN for BOLD */
3221 prt_dup_cidfont("CF0", "CF1");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003222
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003223 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3224 {
3225 prt_dsc_resources("IncludeResource", "font",
3226 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3227 if (!prt_custom_cmap)
3228 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3229 prt_def_cidfont("CF2", (int)prt_line_height,
3230 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3231 }
3232 else
3233 /* Use ROMAN for OBLIQUE */
3234 prt_dup_cidfont("CF0", "CF2");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003235
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003236 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3237 {
3238 prt_dsc_resources("IncludeResource", "font",
3239 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3240 if (!prt_custom_cmap)
3241 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3242 prt_def_cidfont("CF3", (int)prt_line_height,
3243 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3244 }
3245 else
3246 /* Use BOLD for BOLDOBLIQUE */
3247 prt_dup_cidfont("CF1", "CF3");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003248 }
3249#endif
3250
3251 /* Misc constant vars used for underlining and background rects */
3252 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3253 prt_ps_font->uline_offset), 2);
3254 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3255 prt_ps_font->uline_width), 2);
3256 prt_def_var("BO", prt_bgcol_offset, 2);
3257
3258 prt_dsc_noarg("EndSetup");
3259
3260 /* Fail if any problems writing out to the PS file */
3261 return !prt_file_error;
3262}
3263
3264 void
3265mch_print_end(psettings)
3266 prt_settings_T *psettings;
3267{
3268 prt_dsc_noarg("Trailer");
3269
3270 /*
3271 * Output any info we don't know in toto until we finish
3272 */
3273 prt_dsc_ints("Pages", 1, &prt_page_num);
3274
3275 prt_dsc_noarg("EOF");
3276
3277 /* Write CTRL-D to close serial communication link if used.
3278 * NOTHING MUST BE WRITTEN AFTER THIS! */
3279 prt_write_file((char_u *)IF_EB("\004", "\067"));
3280
3281 if (!prt_file_error && psettings->outfile == NULL
3282 && !got_int && !psettings->user_abort)
3283 {
3284 /* Close the file first. */
3285 if (prt_ps_fd != NULL)
3286 {
3287 fclose(prt_ps_fd);
3288 prt_ps_fd = NULL;
3289 }
3290 prt_message((char_u *)_("Sending to printer..."));
3291
3292 /* Not printing to a file: use 'printexpr' to print the file. */
3293 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
3294 EMSG(_("E365: Failed to print PostScript file"));
3295 else
3296 prt_message((char_u *)_("Print job sent."));
3297 }
3298
3299 mch_print_cleanup();
3300}
3301
3302 int
3303mch_print_end_page()
3304{
3305 prt_flush_buffer();
3306
3307 prt_write_string("re sp\n");
3308
3309 prt_dsc_noarg("PageTrailer");
3310
3311 return !prt_file_error;
3312}
3313
3314/*ARGSUSED*/
3315 int
3316mch_print_begin_page(str)
3317 char_u *str;
3318{
3319 int page_num[2];
3320
3321 prt_page_num++;
3322
3323 page_num[0] = page_num[1] = prt_page_num;
3324 prt_dsc_ints("Page", 2, page_num);
3325
3326 prt_dsc_noarg("BeginPageSetup");
3327
3328 prt_write_string("sv\n0 g\n");
3329#ifdef FEAT_MBYTE
3330 prt_in_ascii = !prt_out_mbyte;
3331 if (prt_out_mbyte)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003332 prt_write_string("CF0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003333 else
3334#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003335 prt_write_string("F0 sf\n");
Bram Moolenaar81366db2005-07-24 21:16:51 +00003336 prt_fgcol = PRCOLOR_BLACK;
3337 prt_bgcol = PRCOLOR_WHITE;
3338 prt_font = PRT_PS_FONT_ROMAN;
3339
3340 /* Set up page transformation for landscape printing. */
3341 if (!prt_portrait)
3342 {
3343 prt_write_int(-((int)prt_mediasize[prt_media].width));
3344 prt_write_string("sl\n");
3345 }
3346
3347 prt_dsc_noarg("EndPageSetup");
3348
3349 /* We have reset the font attributes, force setting them again. */
3350 curr_bg = (long_u)0xffffffff;
3351 curr_fg = (long_u)0xffffffff;
3352 curr_bold = MAYBE;
3353
3354 return !prt_file_error;
3355}
3356
3357 int
3358mch_print_blank_page()
3359{
3360 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3361}
3362
3363static float prt_pos_x = 0;
3364static float prt_pos_y = 0;
3365
3366 void
3367mch_print_start_line(margin, page_line)
3368 int margin;
3369 int page_line;
3370{
3371 prt_pos_x = prt_left_margin;
3372 if (margin)
3373 prt_pos_x -= prt_number_width;
3374
3375 prt_pos_y = prt_top_margin - prt_first_line_height -
3376 page_line * prt_line_height;
3377
3378 prt_attribute_change = TRUE;
3379 prt_need_moveto = TRUE;
3380#ifdef FEAT_MBYTE
3381 prt_half_width = FALSE;
3382#endif
3383}
3384
3385/*ARGSUSED*/
3386 int
3387mch_print_text_out(p, len)
3388 char_u *p;
3389 int len;
3390{
3391 int need_break;
3392 char_u ch;
3393 char_u ch_buff[8];
3394 float char_width;
3395 float next_pos;
3396#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003397 int in_ascii;
3398 int half_width;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003399#endif
3400
3401 char_width = prt_char_width;
3402
3403#ifdef FEAT_MBYTE
3404 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3405 * CJKV font to do what VIM is doing here - use a Roman font for characters
3406 * in the ASCII range, and the origingal CID font for everything else.
3407 * The problem is that GhostScript still (as of 8.13) does not support
3408 * rearranged fonts even though they have been documented by Adobe for 7
3409 * years! If they ever do, a lot of this code will disappear.
3410 */
3411 if (prt_use_courier)
3412 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003413 in_ascii = (len == 1 && *p < 0x80);
3414 if (prt_in_ascii)
3415 {
3416 if (!in_ascii)
3417 {
3418 /* No longer in ASCII range - need to switch font */
3419 prt_in_ascii = FALSE;
3420 prt_need_font = TRUE;
3421 prt_attribute_change = TRUE;
3422 }
3423 }
3424 else if (in_ascii)
3425 {
3426 /* Now in ASCII range - need to switch font */
3427 prt_in_ascii = TRUE;
3428 prt_need_font = TRUE;
3429 prt_attribute_change = TRUE;
3430 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003431 }
3432 if (prt_out_mbyte)
3433 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003434 half_width = ((*mb_ptr2cells)(p) == 1);
3435 if (half_width)
3436 char_width /= 2;
3437 if (prt_half_width)
3438 {
3439 if (!half_width)
3440 {
3441 prt_half_width = FALSE;
3442 prt_pos_x += prt_char_width/4;
3443 prt_need_moveto = TRUE;
3444 prt_attribute_change = TRUE;
3445 }
3446 }
3447 else if (half_width)
3448 {
3449 prt_half_width = TRUE;
3450 prt_pos_x += prt_char_width/4;
3451 prt_need_moveto = TRUE;
3452 prt_attribute_change = TRUE;
3453 }
Bram Moolenaar81366db2005-07-24 21:16:51 +00003454 }
3455#endif
3456
3457 /* Output any required changes to the graphics state, after flushing any
3458 * text buffered so far.
3459 */
3460 if (prt_attribute_change)
3461 {
3462 prt_flush_buffer();
3463 /* Reset count of number of chars that will be printed */
3464 prt_text_run = 0;
3465
3466 if (prt_need_moveto)
3467 {
3468 prt_pos_x_moveto = prt_pos_x;
3469 prt_pos_y_moveto = prt_pos_y;
3470 prt_do_moveto = TRUE;
3471
3472 prt_need_moveto = FALSE;
3473 }
3474 if (prt_need_font)
3475 {
3476#ifdef FEAT_MBYTE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003477 if (!prt_in_ascii)
3478 prt_write_string("CF");
3479 else
Bram Moolenaar81366db2005-07-24 21:16:51 +00003480#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003481 prt_write_string("F");
3482 prt_write_int(prt_font);
3483 prt_write_string("sf\n");
3484 prt_need_font = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003485 }
3486 if (prt_need_fgcol)
3487 {
3488 int r, g, b;
3489 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3490 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3491 b = prt_fgcol & 0xff;
3492
3493 prt_write_real(r / 255.0, 3);
3494 if (r == g && g == b)
3495 prt_write_string("g\n");
3496 else
3497 {
3498 prt_write_real(g / 255.0, 3);
3499 prt_write_real(b / 255.0, 3);
3500 prt_write_string("r\n");
3501 }
3502 prt_need_fgcol = FALSE;
3503 }
3504
3505 if (prt_bgcol != PRCOLOR_WHITE)
3506 {
3507 prt_new_bgcol = prt_bgcol;
3508 if (prt_need_bgcol)
3509 prt_do_bgcol = TRUE;
3510 }
3511 else
3512 prt_do_bgcol = FALSE;
3513 prt_need_bgcol = FALSE;
3514
3515 if (prt_need_underline)
3516 prt_do_underline = prt_underline;
3517 prt_need_underline = FALSE;
3518
3519 prt_attribute_change = FALSE;
3520 }
3521
3522#ifdef FEAT_MBYTE
3523 if (prt_do_conv)
3524 {
3525 /* Convert from multi-byte to 8-bit encoding */
3526 p = string_convert(&prt_conv, p, &len);
3527 if (p == NULL)
3528 p = (char_u *)"";
3529 }
3530
3531 if (prt_out_mbyte)
3532 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003533 /* Multi-byte character strings are represented more efficiently as hex
3534 * strings when outputting clean 8 bit PS.
3535 */
3536 do
3537 {
3538 ch = prt_hexchar[(unsigned)(*p) >> 4];
3539 ga_append(&prt_ps_buffer, ch);
3540 ch = prt_hexchar[(*p) & 0xf];
3541 ga_append(&prt_ps_buffer, ch);
3542 p++;
3543 }
3544 while (--len);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003545 }
3546 else
3547#endif
3548 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003549 /* Add next character to buffer of characters to output.
3550 * Note: One printed character may require several PS characters to
3551 * represent it, but we only count them as one printed character.
3552 */
3553 ch = *p;
3554 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3555 {
3556 /* Convert non-printing characters to either their escape or octal
3557 * sequence, ensures PS sent over a serial line does not interfere
3558 * with the comms protocol. Note: For EBCDIC we need to write out
3559 * the escape sequences as ASCII codes!
Bram Moolenaar81366db2005-07-24 21:16:51 +00003560 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3561 */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003562 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3563 switch (ch)
3564 {
3565 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3566 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3567 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3568 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3569 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3570 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3571 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3572 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003573
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003574 default:
3575 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003576#ifdef EBCDIC
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003577 ebcdic2ascii(ch_buff, 3);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003578#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003579 ga_append(&prt_ps_buffer, ch_buff[0]);
3580 ga_append(&prt_ps_buffer, ch_buff[1]);
3581 ga_append(&prt_ps_buffer, ch_buff[2]);
3582 break;
3583 }
3584 }
3585 else
3586 ga_append(&prt_ps_buffer, ch);
Bram Moolenaar81366db2005-07-24 21:16:51 +00003587 }
3588
3589#ifdef FEAT_MBYTE
3590 /* Need to free any translated characters */
3591 if (prt_do_conv && (*p != NUL))
3592 vim_free(p);
3593#endif
3594
3595 prt_text_run += char_width;
3596 prt_pos_x += char_width;
3597
3598 /* The downside of fp - use relative error on right margin check */
3599 next_pos = prt_pos_x + prt_char_width;
3600 need_break = (next_pos > prt_right_margin) &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003601 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
Bram Moolenaar81366db2005-07-24 21:16:51 +00003602
3603 if (need_break)
3604 prt_flush_buffer();
3605
3606 return need_break;
3607}
3608
3609 void
3610mch_print_set_font(iBold, iItalic, iUnderline)
3611 int iBold;
3612 int iItalic;
3613 int iUnderline;
3614{
3615 int font = 0;
3616
3617 if (iBold)
3618 font |= 0x01;
3619 if (iItalic)
3620 font |= 0x02;
3621
3622 if (font != prt_font)
3623 {
3624 prt_font = font;
3625 prt_attribute_change = TRUE;
3626 prt_need_font = TRUE;
3627 }
3628 if (prt_underline != iUnderline)
3629 {
3630 prt_underline = iUnderline;
3631 prt_attribute_change = TRUE;
3632 prt_need_underline = TRUE;
3633 }
3634}
3635
3636 void
3637mch_print_set_bg(bgcol)
3638 long_u bgcol;
3639{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 prt_bgcol = (int)bgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003641 prt_attribute_change = TRUE;
3642 prt_need_bgcol = TRUE;
3643}
3644
3645 void
3646mch_print_set_fg(fgcol)
3647 long_u fgcol;
3648{
3649 if (fgcol != (long_u)prt_fgcol)
3650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003651 prt_fgcol = (int)fgcol;
Bram Moolenaar81366db2005-07-24 21:16:51 +00003652 prt_attribute_change = TRUE;
3653 prt_need_fgcol = TRUE;
3654 }
3655}
3656
3657# endif /*FEAT_POSTSCRIPT*/
3658#endif /*FEAT_PRINTER*/