blob: 61905b0a7a6268b7fdbd2785293d1346e01e0eca [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 *
56 * void mch_print_set_bg(long bgcol);
57 * Called to set the background color for the following text. Parameter is an
58 * RGB value.
59 *
60 * void mch_print_set_fg(long fgcol);
61 * 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
109# define OPT_MBFONT_BOLD 3
110# 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));
151static void prt_get_attr __ARGS((int hl_id, prt_text_attr_T* pattr, int modec));
152
153/*
154 * Parse 'printoptions' and set the flags in "printer_opts".
155 * Returns an error message or NULL;
156 */
157 char_u *
158parse_printoptions()
159{
160 return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
161}
162
163#if (defined(FEAT_MBYTE) && defined(FEAT_POSTSCRIPT)) || defined(PROTO)
164/*
165 * Parse 'printoptions' and set the flags in "printer_opts".
166 * Returns an error message or NULL;
167 */
168 char_u *
169parse_printmbfont()
170{
171 return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS);
172}
173#endif
174
175/*
176 * Parse a list of options in the form
177 * option:value,option:value,option:value
178 *
179 * "value" can start with a number which is parsed out, e.g. margin:12mm
180 *
181 * Returns an error message for an illegal option, NULL otherwise.
182 * Only used for the printer at the moment...
183 */
184 static char_u *
185parse_list_options(option_str, table, table_size)
186 char_u *option_str;
187 option_table_T *table;
188 int table_size;
189{
190 char_u *stringp;
191 char_u *colonp;
192 char_u *commap;
193 char_u *p;
194 int idx = 0; /* init for GCC */
195 int len;
196
197 for (idx = 0; idx < table_size; ++idx)
198 table[idx].present = FALSE;
199
200 /*
201 * Repeat for all comma separated parts.
202 */
203 stringp = option_str;
204 while (*stringp)
205 {
206 colonp = vim_strchr(stringp, ':');
207 if (colonp == NULL)
208 return (char_u *)N_("E550: Missing colon");
209 commap = vim_strchr(stringp, ',');
210 if (commap == NULL)
211 commap = option_str + STRLEN(option_str);
212
213 len = (int)(colonp - stringp);
214
215 for (idx = 0; idx < table_size; ++idx)
216 if (STRNICMP(stringp, table[idx].name, len) == 0)
217 break;
218
219 if (idx == table_size)
220 return (char_u *)N_("E551: Illegal component");
221
222 p = colonp + 1;
223 table[idx].present = TRUE;
224
225 if (table[idx].hasnum)
226 {
227 if (!VIM_ISDIGIT(*p))
228 return (char_u *)N_("E552: digit expected");
229
230 table[idx].number = getdigits(&p); /*advances p*/
231 }
232
233 table[idx].string = p;
234 table[idx].strlen = (int)(commap - p);
235
236 stringp = commap;
237 if (*stringp == ',')
238 ++stringp;
239 }
240
241 return NULL;
242}
243
244
245#ifdef FEAT_SYN_HL
246/*
247 * If using a dark background, the colors will probably be too bright to show
248 * up well on white paper, so reduce their brightness.
249 */
250 static long_u
251darken_rgb(rgb)
252 long_u rgb;
253{
254 return ((rgb >> 17) << 16)
255 + (((rgb & 0xff00) >> 9) << 8)
256 + ((rgb & 0xff) >> 1);
257}
258
259 static long_u
260prt_get_term_color(colorindex)
261 int colorindex;
262{
263 /* TODO: Should check for xterm with 88 or 256 colors. */
264 if (t_colors > 8)
265 return cterm_color_16[colorindex % 16];
266 return cterm_color_8[colorindex % 8];
267}
268
269 static void
270prt_get_attr(hl_id, pattr, modec)
271 int hl_id;
272 prt_text_attr_T *pattr;
273 int modec;
274{
275 int colorindex;
276 long_u fg_color;
277 long_u bg_color;
278 char *color;
279
280 pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL);
281 pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL);
282 pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL);
283 pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL);
284
285# ifdef FEAT_GUI
286 if (gui.in_use)
287 {
288 bg_color = highlight_gui_color_rgb(hl_id, FALSE);
289 if (bg_color == PRCOLOR_BLACK)
290 bg_color = PRCOLOR_WHITE;
291
292 fg_color = highlight_gui_color_rgb(hl_id, TRUE);
293 }
294 else
295# endif
296 {
297 bg_color = PRCOLOR_WHITE;
298
299 color = (char *)highlight_color(hl_id, (char_u *)"fg", modec);
300 if (color == NULL)
301 colorindex = 0;
302 else
303 colorindex = atoi(color);
304
305 if (colorindex >= 0 && colorindex < t_colors)
306 fg_color = prt_get_term_color(colorindex);
307 else
308 fg_color = PRCOLOR_BLACK;
309 }
310
311 if (fg_color == PRCOLOR_WHITE)
312 fg_color = PRCOLOR_BLACK;
313 else if (*p_bg == 'd')
314 fg_color = darken_rgb(fg_color);
315
316 pattr->fg_color = fg_color;
317 pattr->bg_color = bg_color;
318}
319#endif /* FEAT_SYN_HL */
320
321 static void
322prt_set_fg(fg)
323 long_u fg;
324{
325 if (fg != curr_fg)
326 {
327 curr_fg = fg;
328 mch_print_set_fg(fg);
329 }
330}
331
332 static void
333prt_set_bg(bg)
334 long_u bg;
335{
336 if (bg != curr_bg)
337 {
338 curr_bg = bg;
339 mch_print_set_bg(bg);
340 }
341}
342
343 static void
344prt_set_font(bold, italic, underline)
345 int bold;
346 int italic;
347 int underline;
348{
349 if (curr_bold != bold
350 || curr_italic != italic
351 || curr_underline != underline)
352 {
353 curr_underline = underline;
354 curr_italic = italic;
355 curr_bold = bold;
356 mch_print_set_font(bold, italic, underline);
357 }
358}
359
360/*
361 * Print the line number in the left margin.
362 */
363 static void
364prt_line_number(psettings, page_line, lnum)
365 prt_settings_T *psettings;
366 int page_line;
367 linenr_T lnum;
368{
369 int i;
370 char_u tbuf[20];
371
372 prt_set_fg(psettings->number.fg_color);
373 prt_set_bg(psettings->number.bg_color);
374 prt_set_font(psettings->number.bold, psettings->number.italic, psettings->number.underline);
375 mch_print_start_line(TRUE, page_line);
376
377 /* Leave two spaces between the number and the text; depends on
378 * PRINT_NUMBER_WIDTH. */
379 sprintf((char *)tbuf, "%6ld", (long)lnum);
380 for (i = 0; i < 6; i++)
381 (void)mch_print_text_out(&tbuf[i], 1);
382
383#ifdef FEAT_SYN_HL
384 if (psettings->do_syntax)
385 /* Set colors for next character. */
386 current_syn_id = -1;
387 else
388#endif
389 {
390 /* Set colors and font back to normal. */
391 prt_set_fg(PRCOLOR_BLACK);
392 prt_set_bg(PRCOLOR_WHITE);
393 prt_set_font(FALSE, FALSE, FALSE);
394 }
395}
396
397static linenr_T printer_page_num;
398
399 int
400get_printer_page_num()
401{
402 return printer_page_num;
403}
404
405/*
406 * Get the currently effective header height.
407 */
408 int
409prt_header_height()
410{
411 if (printer_opts[OPT_PRINT_HEADERHEIGHT].present)
412 return printer_opts[OPT_PRINT_HEADERHEIGHT].number;
413 return 2;
414}
415
416/*
417 * Return TRUE if using a line number for printing.
418 */
419 int
420prt_use_number()
421{
422 return (printer_opts[OPT_PRINT_NUMBER].present
423 && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y');
424}
425
426/*
427 * Return the unit used in a margin item in 'printoptions'.
428 * Returns PRT_UNIT_NONE if not recognized.
429 */
430 int
431prt_get_unit(idx)
432 int idx;
433{
434 int u = PRT_UNIT_NONE;
435 int i;
436 static char *(units[4]) = PRT_UNIT_NAMES;
437
438 if (printer_opts[idx].present)
439 for (i = 0; i < 4; ++i)
440 if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0)
441 {
442 u = i;
443 break;
444 }
445 return u;
446}
447
448/*
449 * Print the page header.
450 */
451/*ARGSUSED*/
452 static void
453prt_header(psettings, pagenum, lnum)
454 prt_settings_T *psettings;
455 int pagenum;
456 linenr_T lnum;
457{
458 int width = psettings->chars_per_line;
459 int page_line;
460 char_u *tbuf;
461 char_u *p;
462#ifdef FEAT_MBYTE
463 int l;
464#endif
465
466 /* Also use the space for the line number. */
467 if (prt_use_number())
468 width += PRINT_NUMBER_WIDTH;
469
470 tbuf = alloc(width + IOSIZE);
471 if (tbuf == NULL)
472 return;
473
474#ifdef FEAT_STL_OPT
475 if (*p_header != NUL)
476 {
477 linenr_T tmp_lnum, tmp_topline, tmp_botline;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000478 int use_sandbox = FALSE;
Bram Moolenaar81366db2005-07-24 21:16:51 +0000479
480 /*
481 * Need to (temporarily) set current line number and first/last line
482 * number on the 'window'. Since we don't know how long the page is,
483 * set the first and current line number to the top line, and guess
484 * that the page length is 64.
485 */
486 tmp_lnum = curwin->w_cursor.lnum;
487 tmp_topline = curwin->w_topline;
488 tmp_botline = curwin->w_botline;
489 curwin->w_cursor.lnum = lnum;
490 curwin->w_topline = lnum;
491 curwin->w_botline = lnum + 63;
492 printer_page_num = pagenum;
493
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000494# ifdef FEAT_EVAL
495 use_sandbox = was_set_insecurely((char_u *)"printheader");
496# endif
Bram Moolenaar81366db2005-07-24 21:16:51 +0000497 build_stl_str_hl(curwin, tbuf, (size_t)(width + IOSIZE),
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000498 p_header, use_sandbox,
499 ' ', width, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000500
501 /* Reset line numbers */
502 curwin->w_cursor.lnum = tmp_lnum;
503 curwin->w_topline = tmp_topline;
504 curwin->w_botline = tmp_botline;
505 }
506 else
507#endif
508 sprintf((char *)tbuf, _("Page %d"), pagenum);
509
510 prt_set_fg(PRCOLOR_BLACK);
511 prt_set_bg(PRCOLOR_WHITE);
512 prt_set_font(TRUE, FALSE, FALSE);
513
514 /* Use a negative line number to indicate printing in the top margin. */
515 page_line = 0 - prt_header_height();
516 mch_print_start_line(TRUE, page_line);
517 for (p = tbuf; *p != NUL; )
518 {
519 if (mch_print_text_out(p,
520#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000521 (l = (*mb_ptr2len)(p))
Bram Moolenaar81366db2005-07-24 21:16:51 +0000522#else
523 1
524#endif
525 ))
526 {
527 ++page_line;
528 if (page_line >= 0) /* out of room in header */
529 break;
530 mch_print_start_line(TRUE, page_line);
531 }
532#ifdef FEAT_MBYTE
533 p += l;
534#else
535 p++;
536#endif
537 }
538
539 vim_free(tbuf);
540
541#ifdef FEAT_SYN_HL
542 if (psettings->do_syntax)
543 /* Set colors for next character. */
544 current_syn_id = -1;
545 else
546#endif
547 {
548 /* Set colors and font back to normal. */
549 prt_set_fg(PRCOLOR_BLACK);
550 prt_set_bg(PRCOLOR_WHITE);
551 prt_set_font(FALSE, FALSE, FALSE);
552 }
553}
554
555/*
556 * Display a print status message.
557 */
558 static void
559prt_message(s)
560 char_u *s;
561{
562 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
563 screen_puts(s, (int)Rows - 1, 0, hl_attr(HLF_R));
564 out_flush();
565}
566
567 void
568ex_hardcopy(eap)
569 exarg_T *eap;
570{
571 linenr_T lnum;
572 int collated_copies, uncollated_copies;
573 prt_settings_T settings;
574 long_u bytes_to_print = 0;
575 int page_line;
576 int jobsplit;
577 int id;
578
579 memset(&settings, 0, sizeof(prt_settings_T));
580 settings.has_color = TRUE;
581
582# ifdef FEAT_POSTSCRIPT
583 if (*eap->arg == '>')
584 {
585 char_u *errormsg = NULL;
586
587 /* Expand things like "%.ps". */
588 if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL)
589 {
590 if (errormsg != NULL)
591 EMSG(errormsg);
592 return;
593 }
594 settings.outfile = skipwhite(eap->arg + 1);
595 }
596 else if (*eap->arg != NUL)
597 settings.arguments = eap->arg;
598# endif
599
600 /*
601 * Initialise for printing. Ask the user for settings, unless forceit is
602 * set.
603 * The mch_print_init() code should set up margins if applicable. (It may
604 * not be a real printer - for example the engine might generate HTML or
605 * PS.)
606 */
607 if (mch_print_init(&settings,
608 curbuf->b_fname == NULL
609 ? (char_u *)buf_spname(curbuf)
610 : curbuf->b_sfname == NULL
611 ? curbuf->b_fname
612 : curbuf->b_sfname,
613 eap->forceit) == FAIL)
614 return;
615
616#ifdef FEAT_SYN_HL
617# ifdef FEAT_GUI
618 if (gui.in_use)
619 settings.modec = 'g';
620 else
621# endif
622 if (t_colors > 1)
623 settings.modec = 'c';
624 else
625 settings.modec = 't';
626
627 if (!syntax_present(curbuf))
628 settings.do_syntax = FALSE;
629 else if (printer_opts[OPT_PRINT_SYNTAX].present
630 && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a')
631 settings.do_syntax =
632 (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y');
633 else
634 settings.do_syntax = settings.has_color;
635#endif
636
637 /* Set up printing attributes for line numbers */
638 settings.number.fg_color = PRCOLOR_BLACK;
639 settings.number.bg_color = PRCOLOR_WHITE;
640 settings.number.bold = FALSE;
641 settings.number.italic = TRUE;
642 settings.number.underline = FALSE;
643#ifdef FEAT_SYN_HL
644 /*
645 * Syntax highlighting of line numbers.
646 */
647 if (prt_use_number() && settings.do_syntax)
648 {
649 id = syn_name2id((char_u *)"LineNr");
650 if (id > 0)
651 id = syn_get_final_id(id);
652
653 prt_get_attr(id, &settings.number, settings.modec);
654 }
655#endif /* FEAT_SYN_HL */
656
657 /*
658 * Estimate the total lines to be printed
659 */
660 for (lnum = eap->line1; lnum <= eap->line2; lnum++)
661 bytes_to_print += (long_u)STRLEN(skipwhite(ml_get(lnum)));
662 if (bytes_to_print == 0)
663 {
664 MSG(_("No text to be printed"));
665 goto print_fail_no_begin;
666 }
667
668 /* Set colors and font to normal. */
669 curr_bg = (long_u)0xffffffffL;
670 curr_fg = (long_u)0xffffffffL;
671 curr_italic = MAYBE;
672 curr_bold = MAYBE;
673 curr_underline = MAYBE;
674
675 prt_set_fg(PRCOLOR_BLACK);
676 prt_set_bg(PRCOLOR_WHITE);
677 prt_set_font(FALSE, FALSE, FALSE);
678#ifdef FEAT_SYN_HL
679 current_syn_id = -1;
680#endif
681
682 jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
683 && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y');
684
685 if (!mch_print_begin(&settings))
686 goto print_fail_no_begin;
687
688 /*
689 * Loop over collated copies: 1 2 3, 1 2 3, ...
690 */
691 page_count = 0;
692 for (collated_copies = 0;
693 collated_copies < settings.n_collated_copies;
694 collated_copies++)
695 {
696 prt_pos_T prtpos; /* current print position */
697 prt_pos_T page_prtpos; /* print position at page start */
698 int side;
699
700 memset(&page_prtpos, 0, sizeof(prt_pos_T));
701 page_prtpos.file_line = eap->line1;
702 prtpos = page_prtpos;
703
704 if (jobsplit && collated_copies > 0)
705 {
706 /* Splitting jobs: Stop a previous job and start a new one. */
707 mch_print_end(&settings);
708 if (!mch_print_begin(&settings))
709 goto print_fail_no_begin;
710 }
711
712 /*
713 * Loop over all pages in the print job: 1 2 3 ...
714 */
715 for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count)
716 {
717 /*
718 * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ...
719 * For duplex: 12 12 12 34 34 34, ...
720 */
721 for (uncollated_copies = 0;
722 uncollated_copies < settings.n_uncollated_copies;
723 uncollated_copies++)
724 {
725 /* Set the print position to the start of this page. */
726 prtpos = page_prtpos;
727
728 /*
729 * Do front and rear side of a page.
730 */
731 for (side = 0; side <= settings.duplex; ++side)
732 {
733 /*
734 * Print one page.
735 */
736
737 /* Check for interrupt character every page. */
738 ui_breakcheck();
739 if (got_int || settings.user_abort)
740 goto print_fail;
741
742 sprintf((char *)IObuff, _("Printing page %d (%d%%)"),
743 page_count + 1 + side,
744 prtpos.bytes_printed > 1000000
745 ? (int)(prtpos.bytes_printed /
746 (bytes_to_print / 100))
747 : (int)((prtpos.bytes_printed * 100)
748 / bytes_to_print));
749 if (!mch_print_begin_page(IObuff))
750 goto print_fail;
751
752 if (settings.n_collated_copies > 1)
753 sprintf((char *)IObuff + STRLEN(IObuff),
754 _(" Copy %d of %d"),
755 collated_copies + 1,
756 settings.n_collated_copies);
757 prt_message(IObuff);
758
759 /*
760 * Output header if required
761 */
762 if (prt_header_height() > 0)
763 prt_header(&settings, page_count + 1 + side,
764 prtpos.file_line);
765
766 for (page_line = 0; page_line < settings.lines_per_page;
767 ++page_line)
768 {
769 prtpos.column = hardcopy_line(&settings,
770 page_line, &prtpos);
771 if (prtpos.column == 0)
772 {
773 /* finished a file line */
774 prtpos.bytes_printed +=
775 STRLEN(skipwhite(ml_get(prtpos.file_line)));
776 if (++prtpos.file_line > eap->line2)
777 break; /* reached the end */
778 }
779 else if (prtpos.ff)
780 {
781 /* Line had a formfeed in it - start new page but
782 * stay on the current line */
783 break;
784 }
785 }
786
787 if (!mch_print_end_page())
788 goto print_fail;
789 if (prtpos.file_line > eap->line2)
790 break; /* reached the end */
791 }
792
793 /*
794 * Extra blank page for duplexing with odd number of pages and
795 * more copies to come.
796 */
797 if (prtpos.file_line > eap->line2 && settings.duplex
798 && side == 0
799 && uncollated_copies + 1 < settings.n_uncollated_copies)
800 {
801 if (!mch_print_blank_page())
802 goto print_fail;
803 }
804 }
805 if (settings.duplex && prtpos.file_line <= eap->line2)
806 ++page_count;
807
808 /* Remember the position where the next page starts. */
809 page_prtpos = prtpos;
810 }
811
812 vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"),
813 settings.jobname);
814 prt_message(IObuff);
815 }
816
817print_fail:
818 if (got_int || settings.user_abort)
819 {
820 sprintf((char *)IObuff, "%s", _("Printing aborted"));
821 prt_message(IObuff);
822 }
823 mch_print_end(&settings);
824
825print_fail_no_begin:
826 mch_print_cleanup();
827}
828
829/*
830 * Print one page line.
831 * Return the next column to print, or zero if the line is finished.
832 */
833 static colnr_T
834hardcopy_line(psettings, page_line, ppos)
835 prt_settings_T *psettings;
836 int page_line;
837 prt_pos_T *ppos;
838{
839 colnr_T col;
840 char_u *line;
841 int need_break = FALSE;
842 int outputlen;
843 int tab_spaces;
844 long_u print_pos;
845#ifdef FEAT_SYN_HL
846 prt_text_attr_T attr;
847 int id;
848#endif
849
850 if (ppos->column == 0 || ppos->ff)
851 {
852 print_pos = 0;
853 tab_spaces = 0;
854 if (!ppos->ff && prt_use_number())
855 prt_line_number(psettings, page_line, ppos->file_line);
856 ppos->ff = FALSE;
857 }
858 else
859 {
860 /* left over from wrap halfway a tab */
861 print_pos = ppos->print_pos;
862 tab_spaces = ppos->lead_spaces;
863 }
864
865 mch_print_start_line(0, page_line);
866 line = ml_get(ppos->file_line);
867
868 /*
869 * Loop over the columns until the end of the file line or right margin.
870 */
871 for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen)
872 {
873 outputlen = 1;
874#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000875 if (has_mbyte && (outputlen = (*mb_ptr2len)(line + col)) < 1)
Bram Moolenaar81366db2005-07-24 21:16:51 +0000876 outputlen = 1;
877#endif
878#ifdef FEAT_SYN_HL
879 /*
880 * syntax highlighting stuff.
881 */
882 if (psettings->do_syntax)
883 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +0000884 id = syn_get_id(curwin, ppos->file_line, col, 1, NULL);
Bram Moolenaar81366db2005-07-24 21:16:51 +0000885 if (id > 0)
886 id = syn_get_final_id(id);
887 else
888 id = 0;
889 /* Get the line again, a multi-line regexp may invalidate it. */
890 line = ml_get(ppos->file_line);
891
892 if (id != current_syn_id)
893 {
894 current_syn_id = id;
895 prt_get_attr(id, &attr, psettings->modec);
896 prt_set_font(attr.bold, attr.italic, attr.underline);
897 prt_set_fg(attr.fg_color);
898 prt_set_bg(attr.bg_color);
899 }
900 }
901#endif /* FEAT_SYN_HL */
902
903 /*
904 * Appropriately expand any tabs to spaces.
905 */
906 if (line[col] == TAB || tab_spaces != 0)
907 {
908 if (tab_spaces == 0)
909 tab_spaces = curbuf->b_p_ts - (print_pos % curbuf->b_p_ts);
910
911 while (tab_spaces > 0)
912 {
913 need_break = mch_print_text_out((char_u *)" ", 1);
914 print_pos++;
915 tab_spaces--;
916 if (need_break)
917 break;
918 }
919 /* Keep the TAB if we didn't finish it. */
920 if (need_break && tab_spaces > 0)
921 break;
922 }
923 else if (line[col] == FF
924 && printer_opts[OPT_PRINT_FORMFEED].present
925 && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0])
926 == 'y')
927 {
928 ppos->ff = TRUE;
929 need_break = 1;
930 }
931 else
932 {
933 need_break = mch_print_text_out(line + col, outputlen);
934#ifdef FEAT_MBYTE
935 if (has_mbyte)
936 print_pos += (*mb_ptr2cells)(line + col);
937 else
938#endif
939 print_pos++;
940 }
941 }
942
943 ppos->lead_spaces = tab_spaces;
944 ppos->print_pos = print_pos;
945
946 /*
947 * Start next line of file if we clip lines, or have reached end of the
948 * line, unless we are doing a formfeed.
949 */
950 if (!ppos->ff
951 && (line[col] == NUL
952 || (printer_opts[OPT_PRINT_WRAP].present
953 && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0])
954 == 'n')))
955 return 0;
956 return col;
957}
958
959# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
960
961/*
962 * PS printer stuff.
963 *
964 * Sources of information to help maintain the PS printing code:
965 *
966 * 1. PostScript Language Reference, 3rd Edition,
967 * Addison-Wesley, 1999, ISBN 0-201-37922-8
968 * 2. PostScript Language Program Design,
969 * Addison-Wesley, 1988, ISBN 0-201-14396-8
970 * 3. PostScript Tutorial and Cookbook,
971 * Addison Wesley, 1985, ISBN 0-201-10179-3
972 * 4. PostScript Language Document Structuring Conventions Specification,
973 * version 3.0,
974 * Adobe Technote 5001, 25th September 1992
975 * 5. PostScript Printer Description File Format Specification, Version 4.3,
976 * Adobe technote 5003, 9th February 1996
977 * 6. Adobe Font Metrics File Format Specification, Version 4.1,
978 * Adobe Technote 5007, 7th October 1998
979 * 7. Adobe CMap and CIDFont Files Specification, Version 1.0,
980 * Adobe Technote 5014, 8th October 1996
981 * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts,
982 * Adoboe Technote 5094, 8th September, 2001
983 * 9. CJKV Information Processing, 2nd Edition,
984 * O'Reilly, 2002, ISBN 1-56592-224-7
985 *
986 * Some of these documents can be found in PDF form on Adobe's web site -
987 * http://www.adobe.com
988 */
989
990#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
991
992#define PRT_PS_DEFAULT_DPI (72) /* Default user space resolution */
993#define PRT_PS_DEFAULT_FONTSIZE (10)
994#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
995
996struct prt_mediasize_S
997{
998 char *name;
999 float width; /* width and height in points for portrait */
1000 float height;
1001};
1002
1003#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
1004
1005static struct prt_mediasize_S prt_mediasize[] =
1006{
1007 {"A4", 595.0, 842.0},
1008 {"letter", 612.0, 792.0},
1009 {"10x14", 720.0, 1008.0},
1010 {"A3", 842.0, 1191.0},
1011 {"A5", 420.0, 595.0},
1012 {"B4", 729.0, 1032.0},
1013 {"B5", 516.0, 729.0},
1014 {"executive", 522.0, 756.0},
1015 {"folio", 595.0, 935.0},
1016 {"ledger", 1224.0, 792.0}, /* Yes, it is wider than taller! */
1017 {"legal", 612.0, 1008.0},
1018 {"quarto", 610.0, 780.0},
1019 {"statement", 396.0, 612.0},
1020 {"tabloid", 792.0, 1224.0}
1021};
1022
1023/* PS font names, must be in Roman, Bold, Italic, Bold-Italic order */
1024struct prt_ps_font_S
1025{
1026 int wx;
1027 int uline_offset;
1028 int uline_width;
1029 int bbox_min_y;
1030 int bbox_max_y;
1031 char *(ps_fontname[4]);
1032};
1033
1034#define PRT_PS_FONT_ROMAN (0)
1035#define PRT_PS_FONT_BOLD (1)
1036#define PRT_PS_FONT_OBLIQUE (2)
1037#define PRT_PS_FONT_BOLDOBLIQUE (3)
1038
1039/* Standard font metrics for Courier family */
1040static struct prt_ps_font_S prt_ps_courier_font =
1041{
1042 600,
1043 -100, 50,
1044 -250, 805,
1045 {"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"}
1046};
1047
1048#ifdef FEAT_MBYTE
1049/* Generic font metrics for multi-byte fonts */
1050static struct prt_ps_font_S prt_ps_mb_font =
1051{
1052 1000,
1053 -100, 50,
1054 -250, 805,
1055 {NULL, NULL, NULL, NULL}
1056};
1057#endif
1058
1059/* Pointer to current font set being used */
1060static struct prt_ps_font_S* prt_ps_font;
1061
1062/* Structures to map user named encoding and mapping to PS equivalents for
1063 * building CID font name */
1064struct prt_ps_encoding_S
1065{
1066 char *encoding;
1067 char *cmap_encoding;
1068 int needs_charset;
1069};
1070
1071struct prt_ps_charset_S
1072{
1073 char *charset;
1074 char *cmap_charset;
1075 int has_charset;
1076};
1077
1078#ifdef FEAT_MBYTE
1079
1080#define CS_JIS_C_1978 (0x01)
1081#define CS_JIS_X_1983 (0x02)
1082#define CS_JIS_X_1990 (0x04)
1083#define CS_NEC (0x08)
1084#define CS_MSWINDOWS (0x10)
1085#define CS_CP932 (0x20)
1086#define CS_KANJITALK6 (0x40)
1087#define CS_KANJITALK7 (0x80)
1088
1089/* Japanese encodings and charsets */
1090static struct prt_ps_encoding_S j_encodings[] =
1091{
1092 {"iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990|
1093 CS_NEC)},
1094 {"euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990)},
1095 {"sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS|
1096 CS_KANJITALK6|CS_KANJITALK7)},
1097 {"cp932", "RKSJ", CS_JIS_X_1983},
1098 {"ucs-2", "UCS2", CS_JIS_X_1990},
1099 {"utf-8", "UTF8" , CS_JIS_X_1990}
1100};
1101static struct prt_ps_charset_S j_charsets[] =
1102{
1103 {"JIS_C_1978", "78", CS_JIS_C_1978},
1104 {"JIS_X_1983", NULL, CS_JIS_X_1983},
1105 {"JIS_X_1990", "Hojo", CS_JIS_X_1990},
1106 {"NEC", "Ext", CS_NEC},
1107 {"MSWINDOWS", "90ms", CS_MSWINDOWS},
1108 {"CP932", "90ms", CS_JIS_X_1983},
1109 {"KANJITALK6", "83pv", CS_KANJITALK6},
1110 {"KANJITALK7", "90pv", CS_KANJITALK7}
1111};
1112
1113#define CS_GB_2312_80 (0x01)
1114#define CS_GBT_12345_90 (0x02)
1115#define CS_GBK2K (0x04)
1116#define CS_SC_MAC (0x08)
1117#define CS_GBT_90_MAC (0x10)
1118#define CS_GBK (0x20)
1119#define CS_SC_ISO10646 (0x40)
1120
1121/* Simplified Chinese encodings and charsets */
1122static struct prt_ps_encoding_S sc_encodings[] =
1123{
1124 {"iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90)},
1125 {"gb18030", NULL, CS_GBK2K},
1126 {"euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC|
1127 CS_GBT_90_MAC)},
1128 {"gbk", "EUC", CS_GBK},
1129 {"ucs-2", "UCS2", CS_SC_ISO10646},
1130 {"utf-8", "UTF8", CS_SC_ISO10646}
1131};
1132static struct prt_ps_charset_S sc_charsets[] =
1133{
1134 {"GB_2312-80", "GB", CS_GB_2312_80},
1135 {"GBT_12345-90","GBT", CS_GBT_12345_90},
1136 {"MAC", "GBpc", CS_SC_MAC},
1137 {"GBT-90_MAC", "GBTpc", CS_GBT_90_MAC},
1138 {"GBK", "GBK", CS_GBK},
1139 {"GB18030", "GBK2K", CS_GBK2K},
1140 {"ISO10646", "UniGB", CS_SC_ISO10646}
1141};
1142
1143#define CS_CNS_PLANE_1 (0x01)
1144#define CS_CNS_PLANE_2 (0x02)
1145#define CS_CNS_PLANE_1_2 (0x04)
1146#define CS_B5 (0x08)
1147#define CS_ETEN (0x10)
1148#define CS_HK_GCCS (0x20)
1149#define CS_HK_SCS (0x40)
1150#define CS_HK_SCS_ETEN (0x80)
1151#define CS_MTHKL (0x100)
1152#define CS_MTHKS (0x200)
1153#define CS_DLHKL (0x400)
1154#define CS_DLHKS (0x800)
1155#define CS_TC_ISO10646 (0x1000)
1156
1157/* Traditional Chinese encodings and charsets */
1158static struct prt_ps_encoding_S tc_encodings[] =
1159{
1160 {"iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2)},
1161 {"euc-tw", "EUC", CS_CNS_PLANE_1_2},
1162 {"big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS|
1163 CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL|
1164 CS_DLHKS)},
1165 {"cp950", "B5", CS_B5},
1166 {"ucs-2", "UCS2", CS_TC_ISO10646},
1167 {"utf-8", "UTF8", CS_TC_ISO10646},
1168 {"utf-16", "UTF16", CS_TC_ISO10646},
1169 {"utf-32", "UTF32", CS_TC_ISO10646}
1170};
1171static struct prt_ps_charset_S tc_charsets[] =
1172{
1173 {"CNS_1992_1", "CNS1", CS_CNS_PLANE_1},
1174 {"CNS_1992_2", "CNS2", CS_CNS_PLANE_2},
1175 {"CNS_1993", "CNS", CS_CNS_PLANE_1_2},
1176 {"BIG5", NULL, CS_B5},
1177 {"CP950", NULL, CS_B5},
1178 {"ETEN", "ETen", CS_ETEN},
1179 {"HK_GCCS", "HKgccs", CS_HK_GCCS},
1180 {"SCS", "HKscs", CS_HK_SCS},
1181 {"SCS_ETEN", "ETHK", CS_HK_SCS_ETEN},
1182 {"MTHKL", "HKm471", CS_MTHKL},
1183 {"MTHKS", "HKm314", CS_MTHKS},
1184 {"DLHKL", "HKdla", CS_DLHKL},
1185 {"DLHKS", "HKdlb", CS_DLHKS},
1186 {"ISO10646", "UniCNS", CS_TC_ISO10646}
1187};
1188
1189#define CS_KR_X_1992 (0x01)
1190#define CS_KR_MAC (0x02)
1191#define CS_KR_X_1992_MS (0x04)
1192#define CS_KR_ISO10646 (0x08)
1193
1194/* Korean encodings and charsets */
1195static struct prt_ps_encoding_S k_encodings[] =
1196{
1197 {"iso-2022-kr", NULL, CS_KR_X_1992},
1198 {"euc-kr", "EUC", (CS_KR_X_1992|CS_KR_MAC)},
1199 {"johab", "Johab", CS_KR_X_1992},
1200 {"cp1361", "Johab", CS_KR_X_1992},
1201 {"uhc", "UHC", CS_KR_X_1992_MS},
1202 {"cp949", "UHC", CS_KR_X_1992_MS},
1203 {"ucs-2", "UCS2", CS_KR_ISO10646},
1204 {"utf-8", "UTF8", CS_KR_ISO10646}
1205};
1206static struct prt_ps_charset_S k_charsets[] =
1207{
1208 {"KS_X_1992", "KSC", CS_KR_X_1992},
1209 {"CP1361", "KSC", CS_KR_X_1992},
1210 {"MAC", "KSCpc", CS_KR_MAC},
1211 {"MSWINDOWS", "KSCms", CS_KR_X_1992_MS},
1212 {"CP949", "KSCms", CS_KR_X_1992_MS},
1213 {"WANSUNG", "KSCms", CS_KR_X_1992_MS},
1214 {"ISO10646", "UniKS", CS_KR_ISO10646}
1215};
1216
1217/* Collections of encodings and charsets for multi-byte printing */
1218struct prt_ps_mbfont_S
1219{
1220 int num_encodings;
1221 struct prt_ps_encoding_S *encodings;
1222 int num_charsets;
1223 struct prt_ps_charset_S *charsets;
1224 char *ascii_enc;
1225 char *defcs;
1226};
1227
1228static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
1229{
1230 {
1231 NUM_ELEMENTS(j_encodings),
1232 j_encodings,
1233 NUM_ELEMENTS(j_charsets),
1234 j_charsets,
1235 "jis_roman",
1236 "JIS_X_1983"
1237 },
1238 {
1239 NUM_ELEMENTS(sc_encodings),
1240 sc_encodings,
1241 NUM_ELEMENTS(sc_charsets),
1242 sc_charsets,
1243 "gb_roman",
1244 "GB_2312-80"
1245 },
1246 {
1247 NUM_ELEMENTS(tc_encodings),
1248 tc_encodings,
1249 NUM_ELEMENTS(tc_charsets),
1250 tc_charsets,
1251 "cns_roman",
1252 "BIG5"
1253 },
1254 {
1255 NUM_ELEMENTS(k_encodings),
1256 k_encodings,
1257 NUM_ELEMENTS(k_charsets),
1258 k_charsets,
1259 "ks_roman",
1260 "KS_X_1992"
1261 }
1262};
1263#endif /* FEAT_MBYTE */
1264
1265struct prt_ps_resource_S
1266{
1267 char_u name[64];
1268 char_u filename[MAXPATHL + 1];
1269 int type;
1270 char_u title[256];
1271 char_u version[256];
1272};
1273
1274/* Types of PS resource file currently used */
1275#define PRT_RESOURCE_TYPE_PROCSET (0)
1276#define PRT_RESOURCE_TYPE_ENCODING (1)
1277#define PRT_RESOURCE_TYPE_CMAP (2)
1278
1279/* The PS prolog file version number has to match - if the prolog file is
1280 * updated, increment the number in the file and here. Version checking was
1281 * added as of VIM 6.2.
1282 * The CID prolog file version number behaves as per PS prolog.
1283 * Table of VIM and prolog versions:
1284 *
1285 * VIM Prolog CIDProlog
1286 * 6.2 1.3
1287 * 7.0 1.4 1.0
1288 */
1289#define PRT_PROLOG_VERSION ((char_u *)"1.4")
1290#define PRT_CID_PROLOG_VERSION ((char_u *)"1.0")
1291
1292/* String versions of PS resource types - indexed by constants above so don't
1293 * re-order!
1294 */
1295static char *prt_resource_types[] =
1296{
1297 "procset",
1298 "encoding",
1299 "cmap"
1300};
1301
1302/* Strings to look for in a PS resource file */
1303#define PRT_RESOURCE_HEADER "%!PS-Adobe-"
1304#define PRT_RESOURCE_RESOURCE "Resource-"
1305#define PRT_RESOURCE_PROCSET "ProcSet"
1306#define PRT_RESOURCE_ENCODING "Encoding"
1307#define PRT_RESOURCE_CMAP "CMap"
1308
1309
1310/* Data for table based DSC comment recognition, easy to extend if VIM needs to
1311 * read more comments. */
1312#define PRT_DSC_MISC_TYPE (-1)
1313#define PRT_DSC_TITLE_TYPE (1)
1314#define PRT_DSC_VERSION_TYPE (2)
1315#define PRT_DSC_ENDCOMMENTS_TYPE (3)
1316
1317#define PRT_DSC_TITLE "%%Title:"
1318#define PRT_DSC_VERSION "%%Version:"
1319#define PRT_DSC_ENDCOMMENTS "%%EndComments:"
1320
1321struct prt_dsc_comment_S
1322{
1323 char *string;
1324 int len;
1325 int type;
1326};
1327
1328struct prt_dsc_line_S
1329{
1330 int type;
1331 char_u *string;
1332 int len;
1333};
1334
1335
1336#define SIZEOF_CSTR(s) (sizeof(s) - 1)
1337static struct prt_dsc_comment_S prt_dsc_table[] =
1338{
1339 {PRT_DSC_TITLE, SIZEOF_CSTR(PRT_DSC_TITLE), PRT_DSC_TITLE_TYPE},
1340 {PRT_DSC_VERSION, SIZEOF_CSTR(PRT_DSC_VERSION),
1341 PRT_DSC_VERSION_TYPE},
1342 {PRT_DSC_ENDCOMMENTS, SIZEOF_CSTR(PRT_DSC_ENDCOMMENTS),
1343 PRT_DSC_ENDCOMMENTS_TYPE}
1344};
1345
1346static void prt_write_file_raw_len __ARGS((char_u *buffer, int bytes));
1347static void prt_write_file __ARGS((char_u *buffer));
1348static void prt_write_file_len __ARGS((char_u *buffer, int bytes));
1349static void prt_write_string __ARGS((char *s));
1350static void prt_write_int __ARGS((int i));
1351static void prt_write_boolean __ARGS((int b));
1352static void prt_def_font __ARGS((char *new_name, char *encoding, int height, char *font));
1353static void prt_real_bits __ARGS((double real, int precision, int *pinteger, int *pfraction));
1354static void prt_write_real __ARGS((double val, int prec));
1355static void prt_def_var __ARGS((char *name, double value, int prec));
1356static void prt_flush_buffer __ARGS((void));
1357static void prt_resource_name __ARGS((char_u *filename, void *cookie));
1358static int prt_find_resource __ARGS((char *name, struct prt_ps_resource_S *resource));
1359static int prt_open_resource __ARGS((struct prt_ps_resource_S *resource));
1360static int prt_check_resource __ARGS((struct prt_ps_resource_S *resource, char_u *version));
1361static void prt_dsc_start __ARGS((void));
1362static void prt_dsc_noarg __ARGS((char *comment));
1363static void prt_dsc_textline __ARGS((char *comment, char *text));
1364static void prt_dsc_text __ARGS((char *comment, char *text));
1365static void prt_dsc_ints __ARGS((char *comment, int count, int *ints));
1366static void prt_dsc_requirements __ARGS((int duplex, int tumble, int collate, int color, int num_copies));
1367static void prt_dsc_docmedia __ARGS((char *paper_name, double width, double height, double weight, char *colour, char *type));
1368static void prt_dsc_resources __ARGS((char *comment, char *type, char *strings));
1369static void prt_dsc_font_resource __ARGS((char *resource, struct prt_ps_font_S *ps_font));
1370static float to_device_units __ARGS((int idx, double physsize, int def_number));
1371static void prt_page_margins __ARGS((double width, double height, double *left, double *right, double *top, double *bottom));
1372static void prt_font_metrics __ARGS((int font_scale));
1373static int prt_get_cpl __ARGS((void));
1374static int prt_get_lpp __ARGS((void));
1375static int prt_add_resource __ARGS((struct prt_ps_resource_S *resource));
1376static int prt_resfile_next_line __ARGS((void));
1377static int prt_resfile_strncmp __ARGS((int offset, char *string, int len));
1378static int prt_resfile_skip_nonws __ARGS((int offset));
1379static int prt_resfile_skip_ws __ARGS((int offset));
1380static int prt_next_dsc __ARGS((struct prt_dsc_line_S *p_dsc_line));
1381#ifdef FEAT_MBYTE
1382static int prt_build_cid_fontname __ARGS((int font, char_u *name, int name_len));
1383static void prt_def_cidfont __ARGS((char *new_name, int height, char *cidfont));
1384static void prt_dup_cidfont __ARGS((char *original_name, char *new_name));
1385static int prt_match_encoding __ARGS((char *p_encoding, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_encoding_S **pp_mbenc));
1386static int prt_match_charset __ARGS((char *p_charset, struct prt_ps_mbfont_S *p_cmap, struct prt_ps_charset_S **pp_mbchar));
1387#endif
1388
1389/*
1390 * Variables for the output PostScript file.
1391 */
1392static FILE *prt_ps_fd;
1393static int prt_file_error;
1394static char_u *prt_ps_file_name = NULL;
1395
1396/*
1397 * Various offsets and dimensions in default PostScript user space (points).
1398 * Used for text positioning calculations
1399 */
1400static float prt_page_width;
1401static float prt_page_height;
1402static float prt_left_margin;
1403static float prt_right_margin;
1404static float prt_top_margin;
1405static float prt_bottom_margin;
1406static float prt_line_height;
1407static float prt_first_line_height;
1408static float prt_char_width;
1409static float prt_number_width;
1410static float prt_bgcol_offset;
1411static float prt_pos_x_moveto = 0.0;
1412static float prt_pos_y_moveto = 0.0;
1413
1414/*
1415 * Various control variables used to decide when and how to change the
1416 * PostScript graphics state.
1417 */
1418static int prt_need_moveto;
1419static int prt_do_moveto;
1420static int prt_need_font;
1421static int prt_font;
1422static int prt_need_underline;
1423static int prt_underline;
1424static int prt_do_underline;
1425static int prt_need_fgcol;
1426static int prt_fgcol;
1427static int prt_need_bgcol;
1428static int prt_do_bgcol;
1429static int prt_bgcol;
1430static int prt_new_bgcol;
1431static int prt_attribute_change;
1432static float prt_text_run;
1433static int prt_page_num;
1434static int prt_bufsiz;
1435
1436/*
1437 * Variables controlling physical printing.
1438 */
1439static int prt_media;
1440static int prt_portrait;
1441static int prt_num_copies;
1442static int prt_duplex;
1443static int prt_tumble;
1444static int prt_collate;
1445
1446/*
1447 * Buffers used when generating PostScript output
1448 */
1449static char_u prt_line_buffer[257];
1450static garray_T prt_ps_buffer;
1451
1452# ifdef FEAT_MBYTE
1453static int prt_do_conv;
1454static vimconv_T prt_conv;
1455
1456static int prt_out_mbyte;
1457static int prt_custom_cmap;
1458static char prt_cmap[80];
1459static int prt_use_courier;
1460static int prt_in_ascii;
1461static int prt_half_width;
1462static char *prt_ascii_encoding;
1463static char_u prt_hexchar[] = "0123456789abcdef";
1464# endif
1465
1466 static void
1467prt_write_file_raw_len(buffer, bytes)
1468 char_u *buffer;
1469 int bytes;
1470{
1471 if (!prt_file_error
1472 && fwrite(buffer, sizeof(char_u), bytes, prt_ps_fd)
1473 != (size_t)bytes)
1474 {
1475 EMSG(_("E455: Error writing to PostScript output file"));
1476 prt_file_error = TRUE;
1477 }
1478}
1479
1480 static void
1481prt_write_file(buffer)
1482 char_u *buffer;
1483{
1484 prt_write_file_len(buffer, STRLEN(buffer));
1485}
1486
1487 static void
1488prt_write_file_len(buffer, bytes)
1489 char_u *buffer;
1490 int bytes;
1491{
1492#ifdef EBCDIC
1493 ebcdic2ascii(buffer, bytes);
1494#endif
1495 prt_write_file_raw_len(buffer, bytes);
1496}
1497
1498/*
1499 * Write a string.
1500 */
1501 static void
1502prt_write_string(s)
1503 char *s;
1504{
1505 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer), "%s", s);
1506 prt_write_file(prt_line_buffer);
1507}
1508
1509/*
1510 * Write an int and a space.
1511 */
1512 static void
1513prt_write_int(i)
1514 int i;
1515{
1516 sprintf((char *)prt_line_buffer, "%d ", i);
1517 prt_write_file(prt_line_buffer);
1518}
1519
1520/*
1521 * Write a boolean and a space.
1522 */
1523 static void
1524prt_write_boolean(b)
1525 int b;
1526{
1527 sprintf((char *)prt_line_buffer, "%s ", (b ? "T" : "F"));
1528 prt_write_file(prt_line_buffer);
1529}
1530
1531/*
1532 * Write PostScript to re-encode and define the font.
1533 */
1534 static void
1535prt_def_font(new_name, encoding, height, font)
1536 char *new_name;
1537 char *encoding;
1538 int height;
1539 char *font;
1540{
1541 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1542 "/_%s /VIM-%s /%s ref\n", new_name, encoding, font);
1543 prt_write_file(prt_line_buffer);
1544#ifdef FEAT_MBYTE
1545 if (prt_out_mbyte)
1546 sprintf((char *)prt_line_buffer, "/%s %d %f /_%s sffs\n",
1547 new_name, height, 500./prt_ps_courier_font.wx, new_name);
1548 else
1549#endif
1550 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1551 "/%s %d /_%s ffs\n", new_name, height, new_name);
1552 prt_write_file(prt_line_buffer);
1553}
1554
1555#ifdef FEAT_MBYTE
1556/*
1557 * Write a line to define the CID font.
1558 */
1559 static void
1560prt_def_cidfont(new_name, height, cidfont)
1561 char *new_name;
1562 int height;
1563 char *cidfont;
1564{
1565 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1566 "/_%s /%s[/%s] vim_composefont\n", new_name, prt_cmap, cidfont);
1567 prt_write_file(prt_line_buffer);
1568 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1569 "/%s %d /_%s ffs\n", new_name, height, new_name);
1570 prt_write_file(prt_line_buffer);
1571}
1572
1573/*
1574 * Write a line to define a duplicate of a CID font
1575 */
1576 static void
1577prt_dup_cidfont(original_name, new_name)
1578 char *original_name;
1579 char *new_name;
1580{
1581 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1582 "/%s %s d\n", new_name, original_name);
1583 prt_write_file(prt_line_buffer);
1584}
1585#endif
1586
1587/*
1588 * Convert a real value into an integer and fractional part as integers, with
1589 * the fractional part being in the range [0,10^precision). The fractional part
1590 * is also rounded based on the precision + 1'th fractional digit.
1591 */
1592 static void
1593prt_real_bits(real, precision, pinteger, pfraction)
1594 double real;
1595 int precision;
1596 int *pinteger;
1597 int *pfraction;
1598{
1599 int i;
1600 int integer;
1601 float fraction;
1602
1603 integer = (int)real;
1604 fraction = (float)(real - integer);
1605 if (real < (double)integer)
1606 fraction = -fraction;
1607 for (i = 0; i < precision; i++)
1608 fraction *= 10.0;
1609
1610 *pinteger = integer;
1611 *pfraction = (int)(fraction + 0.5);
1612}
1613
1614/*
1615 * Write a real and a space. Save bytes if real value has no fractional part!
1616 * We use prt_real_bits() as %f in sprintf uses the locale setting to decide
1617 * what decimal point character to use, but PS always requires a '.'.
1618 */
1619 static void
1620prt_write_real(val, prec)
1621 double val;
1622 int prec;
1623{
1624 int integer;
1625 int fraction;
1626
1627 prt_real_bits(val, prec, &integer, &fraction);
1628 /* Emit integer part */
1629 sprintf((char *)prt_line_buffer, "%d", integer);
1630 prt_write_file(prt_line_buffer);
1631 /* Only emit fraction if necessary */
1632 if (fraction != 0)
1633 {
1634 /* Remove any trailing zeros */
1635 while ((fraction % 10) == 0)
1636 {
1637 prec--;
1638 fraction /= 10;
1639 }
1640 /* Emit fraction left padded with zeros */
1641 sprintf((char *)prt_line_buffer, ".%0*d", prec, fraction);
1642 prt_write_file(prt_line_buffer);
1643 }
1644 sprintf((char *)prt_line_buffer, " ");
1645 prt_write_file(prt_line_buffer);
1646}
1647
1648/*
1649 * Write a line to define a numeric variable.
1650 */
1651 static void
1652prt_def_var(name, value, prec)
1653 char *name;
1654 double value;
1655 int prec;
1656{
1657 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
1658 "/%s ", name);
1659 prt_write_file(prt_line_buffer);
1660 prt_write_real(value, prec);
1661 sprintf((char *)prt_line_buffer, "d\n");
1662 prt_write_file(prt_line_buffer);
1663}
1664
1665/* Convert size from font space to user space at current font scale */
1666#define PRT_PS_FONT_TO_USER(scale, size) ((size) * ((scale)/1000.0))
1667
1668 static void
1669prt_flush_buffer()
1670{
1671 if (prt_ps_buffer.ga_len > 0)
1672 {
1673 /* Any background color must be drawn first */
1674 if (prt_do_bgcol && (prt_new_bgcol != PRCOLOR_WHITE))
1675 {
1676 int r, g, b;
1677
1678 if (prt_do_moveto)
1679 {
1680 prt_write_real(prt_pos_x_moveto, 2);
1681 prt_write_real(prt_pos_y_moveto, 2);
1682 prt_write_string("m\n");
1683 prt_do_moveto = FALSE;
1684 }
1685
1686 /* Size of rect of background color on which text is printed */
1687 prt_write_real(prt_text_run, 2);
1688 prt_write_real(prt_line_height, 2);
1689
1690 /* Lastly add the color of the background */
1691 r = ((unsigned)prt_new_bgcol & 0xff0000) >> 16;
1692 g = ((unsigned)prt_new_bgcol & 0xff00) >> 8;
1693 b = prt_new_bgcol & 0xff;
1694 prt_write_real(r / 255.0, 3);
1695 prt_write_real(g / 255.0, 3);
1696 prt_write_real(b / 255.0, 3);
1697 prt_write_string("bg\n");
1698 }
1699 /* Draw underlines before the text as it makes it slightly easier to
1700 * find the starting point.
1701 */
1702 if (prt_do_underline)
1703 {
1704 if (prt_do_moveto)
1705 {
1706 prt_write_real(prt_pos_x_moveto, 2);
1707 prt_write_real(prt_pos_y_moveto, 2);
1708 prt_write_string("m\n");
1709 prt_do_moveto = FALSE;
1710 }
1711
1712 /* Underline length of text run */
1713 prt_write_real(prt_text_run, 2);
1714 prt_write_string("ul\n");
1715 }
1716 /* Draw the text
1717 * Note: we write text out raw - EBCDIC conversion is handled in the
1718 * PostScript world via the font encoding vector. */
1719#ifdef FEAT_MBYTE
1720 if (prt_out_mbyte)
1721 prt_write_string("<");
1722 else
1723#endif
1724 prt_write_string("(");
1725 prt_write_file_raw_len(prt_ps_buffer.ga_data, prt_ps_buffer.ga_len);
1726#ifdef FEAT_MBYTE
1727 if (prt_out_mbyte)
1728 prt_write_string(">");
1729 else
1730#endif
1731 prt_write_string(")");
1732 /* Add a moveto if need be and use the appropriate show procedure */
1733 if (prt_do_moveto)
1734 {
1735 prt_write_real(prt_pos_x_moveto, 2);
1736 prt_write_real(prt_pos_y_moveto, 2);
1737 /* moveto and a show */
1738 prt_write_string("ms\n");
1739 prt_do_moveto = FALSE;
1740 }
1741 else /* Simple show */
1742 prt_write_string("s\n");
1743
1744 ga_clear(&prt_ps_buffer);
1745 ga_init2(&prt_ps_buffer, (int)sizeof(char), prt_bufsiz);
1746 }
1747}
1748
1749
1750 static void
1751prt_resource_name(filename, cookie)
1752 char_u *filename;
1753 void *cookie;
1754{
1755 char_u *resource_filename = cookie;
1756
1757 if (STRLEN(filename) >= MAXPATHL)
1758 *resource_filename = NUL;
1759 else
1760 STRCPY(resource_filename, filename);
1761}
1762
1763 static int
1764prt_find_resource(name, resource)
1765 char *name;
1766 struct prt_ps_resource_S *resource;
1767{
1768 char_u buffer[MAXPATHL + 1];
1769
1770 STRCPY(resource->name, name);
1771 /* Look for named resource file in runtimepath */
1772 STRCPY(buffer, "print");
1773 add_pathsep(buffer);
1774 STRCAT(buffer, name);
1775 STRCAT(buffer, ".ps");
1776 resource->filename[0] = NUL;
1777 return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
1778 resource->filename)
1779 && resource->filename[0] != NUL);
1780}
1781
1782/* PS CR and LF characters have platform independent values */
1783#define PSLF (0x0a)
1784#define PSCR (0x0d)
1785
1786/* Static buffer to read initial comments in a resource file, some can have a
1787 * couple of KB of comments! */
1788#define PRT_FILE_BUFFER_LEN (2048)
1789struct prt_resfile_buffer_S
1790{
1791 char_u buffer[PRT_FILE_BUFFER_LEN];
1792 int len;
1793 int line_start;
1794 int line_end;
1795};
1796
1797static struct prt_resfile_buffer_S prt_resfile;
1798
1799 static int
1800prt_resfile_next_line()
1801{
1802 int index;
1803
1804 /* Move to start of next line and then find end of line */
1805 index = prt_resfile.line_end + 1;
1806 while (index < prt_resfile.len)
1807 {
1808 if (prt_resfile.buffer[index] != PSLF && prt_resfile.buffer[index]
1809 != PSCR)
1810 break;
1811 index++;
1812 }
1813 prt_resfile.line_start = index;
1814
1815 while (index < prt_resfile.len)
1816 {
1817 if (prt_resfile.buffer[index] == PSLF || prt_resfile.buffer[index]
1818 == PSCR)
1819 break;
1820 index++;
1821 }
1822 prt_resfile.line_end = index;
1823
1824 return (index < prt_resfile.len);
1825}
1826
1827 static int
1828prt_resfile_strncmp(offset, string, len)
1829 int offset;
1830 char *string;
1831 int len;
1832{
1833 /* Force not equal if string is longer than remainder of line */
1834 if (len > (prt_resfile.line_end - (prt_resfile.line_start + offset)))
1835 return 1;
1836
1837 return STRNCMP(&prt_resfile.buffer[prt_resfile.line_start + offset],
1838 string, len);
1839}
1840
1841 static int
1842prt_resfile_skip_nonws(offset)
1843 int offset;
1844{
1845 int index;
1846
1847 index = prt_resfile.line_start + offset;
1848 while (index < prt_resfile.line_end)
1849 {
1850 if (isspace(prt_resfile.buffer[index]))
1851 return index - prt_resfile.line_start;
1852 index++;
1853 }
1854 return -1;
1855}
1856
1857 static int
1858prt_resfile_skip_ws(offset)
1859 int offset;
1860{
1861 int index;
1862
1863 index = prt_resfile.line_start + offset;
1864 while (index < prt_resfile.line_end)
1865 {
1866 if (!isspace(prt_resfile.buffer[index]))
1867 return index - prt_resfile.line_start;
1868 index++;
1869 }
1870 return -1;
1871}
1872
1873/* prt_next_dsc() - returns detail on next DSC comment line found. Returns true
1874 * if a DSC comment is found, else false */
1875 static int
1876prt_next_dsc(p_dsc_line)
1877 struct prt_dsc_line_S *p_dsc_line;
1878{
1879 int comment;
1880 int offset;
1881
1882 /* Move to start of next line */
1883 if (!prt_resfile_next_line())
1884 return FALSE;
1885
1886 /* DSC comments always start %% */
1887 if (prt_resfile_strncmp(0, "%%", 2) != 0)
1888 return FALSE;
1889
1890 /* Find type of DSC comment */
1891 for (comment = 0; comment < NUM_ELEMENTS(prt_dsc_table); comment++)
1892 if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
1893 prt_dsc_table[comment].len) == 0)
1894 break;
1895
1896 if (comment != NUM_ELEMENTS(prt_dsc_table))
1897 {
1898 /* Return type of comment */
1899 p_dsc_line->type = prt_dsc_table[comment].type;
1900 offset = prt_dsc_table[comment].len;
1901 }
1902 else
1903 {
1904 /* Unrecognised DSC comment, skip to ws after comment leader */
1905 p_dsc_line->type = PRT_DSC_MISC_TYPE;
1906 offset = prt_resfile_skip_nonws(0);
1907 if (offset == -1)
1908 return FALSE;
1909 }
1910
1911 /* Skip ws to comment value */
1912 offset = prt_resfile_skip_ws(offset);
1913 if (offset == -1)
1914 return FALSE;
1915
1916 p_dsc_line->string = &prt_resfile.buffer[prt_resfile.line_start + offset];
1917 p_dsc_line->len = prt_resfile.line_end - (prt_resfile.line_start + offset);
1918
1919 return TRUE;
1920}
1921
1922/* Improved hand crafted parser to get the type, title, and version number of a
1923 * PS resource file so the file details can be added to the DSC header comments.
1924 */
1925 static int
1926prt_open_resource(resource)
1927 struct prt_ps_resource_S *resource;
1928{
1929 int offset;
1930 int seen_all;
1931 int seen_title;
1932 int seen_version;
1933 FILE *fd_resource;
1934 struct prt_dsc_line_S dsc_line;
1935
1936 fd_resource = mch_fopen((char *)resource->filename, READBIN);
1937 if (fd_resource == NULL)
1938 {
1939 EMSG2(_("E624: Can't open file \"%s\""), resource->filename);
1940 return FALSE;
1941 }
1942 vim_memset(prt_resfile.buffer, NUL, PRT_FILE_BUFFER_LEN);
1943
1944 /* Parse first line to ensure valid resource file */
1945 prt_resfile.len = fread((char *)prt_resfile.buffer, sizeof(char_u),
1946 PRT_FILE_BUFFER_LEN, fd_resource);
1947 if (ferror(fd_resource))
1948 {
1949 EMSG2(_("E457: Can't read PostScript resource file \"%s\""),
1950 resource->filename);
1951 fclose(fd_resource);
1952 return FALSE;
1953 }
1954
1955 prt_resfile.line_end = -1;
1956 prt_resfile.line_start = 0;
1957 if (!prt_resfile_next_line())
1958 return FALSE;
1959
1960 offset = 0;
1961
1962 if (prt_resfile_strncmp(offset, PRT_RESOURCE_HEADER,
1963 STRLEN(PRT_RESOURCE_HEADER)) != 0)
1964 {
1965 EMSG2(_("E618: file \"%s\" is not a PostScript resource file"),
1966 resource->filename);
1967 fclose(fd_resource);
1968 return FALSE;
1969 }
1970
1971 /* Skip over any version numbers and following ws */
1972 offset += STRLEN(PRT_RESOURCE_HEADER);
1973 offset = prt_resfile_skip_nonws(offset);
1974 if (offset == -1)
1975 return FALSE;
1976 offset = prt_resfile_skip_ws(offset);
1977 if (offset == -1)
1978 return FALSE;
1979
1980 if (prt_resfile_strncmp(offset, PRT_RESOURCE_RESOURCE,
1981 STRLEN(PRT_RESOURCE_RESOURCE)) != 0)
1982 {
1983 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
1984 resource->filename);
1985 fclose(fd_resource);
1986 return FALSE;
1987 }
1988 offset += STRLEN(PRT_RESOURCE_RESOURCE);
1989
1990 /* Decide type of resource in the file */
1991 if (prt_resfile_strncmp(offset, PRT_RESOURCE_PROCSET,
1992 STRLEN(PRT_RESOURCE_PROCSET)) == 0)
1993 resource->type = PRT_RESOURCE_TYPE_PROCSET;
1994 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_ENCODING,
1995 STRLEN(PRT_RESOURCE_ENCODING)) == 0)
1996 resource->type = PRT_RESOURCE_TYPE_ENCODING;
1997 else if (prt_resfile_strncmp(offset, PRT_RESOURCE_CMAP,
1998 STRLEN(PRT_RESOURCE_CMAP)) == 0)
1999 resource->type = PRT_RESOURCE_TYPE_CMAP;
2000 else
2001 {
2002 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2003 resource->filename);
2004 fclose(fd_resource);
2005 return FALSE;
2006 }
2007
2008 /* Look for title and version of resource */
2009 resource->title[0] = '\0';
2010 resource->version[0] = '\0';
2011 seen_title = FALSE;
2012 seen_version = FALSE;
2013 seen_all = FALSE;
2014 while (!seen_all && prt_next_dsc(&dsc_line))
2015 {
2016 switch (dsc_line.type)
2017 {
2018 case PRT_DSC_TITLE_TYPE:
2019 vim_strncpy(resource->title, dsc_line.string, dsc_line.len);
2020 seen_title = TRUE;
2021 if (seen_version)
2022 seen_all = TRUE;
2023 break;
2024
2025 case PRT_DSC_VERSION_TYPE:
2026 vim_strncpy(resource->version, dsc_line.string, dsc_line.len);
2027 seen_version = TRUE;
2028 if (seen_title)
2029 seen_all = TRUE;
2030 break;
2031
2032 case PRT_DSC_ENDCOMMENTS_TYPE:
2033 /* Wont find title or resource after this comment, stop searching */
2034 seen_all = TRUE;
2035 break;
2036
2037 case PRT_DSC_MISC_TYPE:
2038 /* Not interested in whatever comment this line had */
2039 break;
2040 }
2041 }
2042
2043 if (!seen_title || !seen_version)
2044 {
2045 EMSG2(_("E619: file \"%s\" is not a supported PostScript resource file"),
2046 resource->filename);
2047 fclose(fd_resource);
2048 return FALSE;
2049 }
2050
2051 fclose(fd_resource);
2052
2053 return TRUE;
2054}
2055
2056 static int
2057prt_check_resource(resource, version)
2058 struct prt_ps_resource_S *resource;
2059 char_u *version;
2060{
2061 /* Version number m.n should match, the revision number does not matter */
2062 if (STRNCMP(resource->version, version, STRLEN(version)))
2063 {
2064 EMSG2(_("E621: \"%s\" resource file has wrong version"),
2065 resource->name);
2066 return FALSE;
2067 }
2068
2069 /* Other checks to be added as needed */
2070 return TRUE;
2071}
2072
2073 static void
2074prt_dsc_start()
2075{
2076 prt_write_string("%!PS-Adobe-3.0\n");
2077}
2078
2079 static void
2080prt_dsc_noarg(comment)
2081 char *comment;
2082{
2083 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2084 "%%%%%s\n", comment);
2085 prt_write_file(prt_line_buffer);
2086}
2087
2088 static void
2089prt_dsc_textline(comment, text)
2090 char *comment;
2091 char *text;
2092{
2093 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2094 "%%%%%s: %s\n", comment, text);
2095 prt_write_file(prt_line_buffer);
2096}
2097
2098 static void
2099prt_dsc_text(comment, text)
2100 char *comment;
2101 char *text;
2102{
2103 /* TODO - should scan 'text' for any chars needing escaping! */
2104 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2105 "%%%%%s: (%s)\n", comment, text);
2106 prt_write_file(prt_line_buffer);
2107}
2108
2109#define prt_dsc_atend(c) prt_dsc_text((c), "atend")
2110
2111 static void
2112prt_dsc_ints(comment, count, ints)
2113 char *comment;
2114 int count;
2115 int *ints;
2116{
2117 int i;
2118
2119 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2120 "%%%%%s:", comment);
2121 prt_write_file(prt_line_buffer);
2122
2123 for (i = 0; i < count; i++)
2124 {
2125 sprintf((char *)prt_line_buffer, " %d", ints[i]);
2126 prt_write_file(prt_line_buffer);
2127 }
2128
2129 prt_write_string("\n");
2130}
2131
2132 static void
2133prt_dsc_resources(comment, type, string)
2134 char *comment; /* if NULL add to previous */
2135 char *type;
2136 char *string;
2137{
2138 if (comment != NULL)
2139 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2140 "%%%%%s: %s", comment, type);
2141 else
2142 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2143 "%%%%+ %s", type);
2144 prt_write_file(prt_line_buffer);
2145
2146 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2147 " %s\n", string);
2148 prt_write_file(prt_line_buffer);
2149}
2150
2151 static void
2152prt_dsc_font_resource(resource, ps_font)
2153 char *resource;
2154 struct prt_ps_font_S *ps_font;
2155{
2156 int i;
2157
2158 prt_dsc_resources(resource, "font",
2159 ps_font->ps_fontname[PRT_PS_FONT_ROMAN]);
2160 for (i = PRT_PS_FONT_BOLD ; i <= PRT_PS_FONT_BOLDOBLIQUE ; i++)
2161 if (ps_font->ps_fontname[i] != NULL)
2162 prt_dsc_resources(NULL, "font", ps_font->ps_fontname[i]);
2163}
2164
2165 static void
2166prt_dsc_requirements(duplex, tumble, collate, color, num_copies)
2167 int duplex;
2168 int tumble;
2169 int collate;
2170 int color;
2171 int num_copies;
2172{
2173 /* Only output the comment if we need to.
2174 * Note: tumble is ignored if we are not duplexing
2175 */
2176 if (!(duplex || collate || color || (num_copies > 1)))
2177 return;
2178
2179 sprintf((char *)prt_line_buffer, "%%%%Requirements:");
2180 prt_write_file(prt_line_buffer);
2181
2182 if (duplex)
2183 {
2184 prt_write_string(" duplex");
2185 if (tumble)
2186 prt_write_string("(tumble)");
2187 }
2188 if (collate)
2189 prt_write_string(" collate");
2190 if (color)
2191 prt_write_string(" color");
2192 if (num_copies > 1)
2193 {
2194 prt_write_string(" numcopies(");
2195 /* Note: no space wanted so dont use prt_write_int() */
2196 sprintf((char *)prt_line_buffer, "%d", num_copies);
2197 prt_write_file(prt_line_buffer);
2198 prt_write_string(")");
2199 }
2200 prt_write_string("\n");
2201}
2202
2203 static void
2204prt_dsc_docmedia(paper_name, width, height, weight, colour, type)
2205 char *paper_name;
2206 double width;
2207 double height;
2208 double weight;
2209 char *colour;
2210 char *type;
2211{
2212 vim_snprintf((char *)prt_line_buffer, sizeof(prt_line_buffer),
2213 "%%%%DocumentMedia: %s ", paper_name);
2214 prt_write_file(prt_line_buffer);
2215 prt_write_real(width, 2);
2216 prt_write_real(height, 2);
2217 prt_write_real(weight, 2);
2218 if (colour == NULL)
2219 prt_write_string("()");
2220 else
2221 prt_write_string(colour);
2222 prt_write_string(" ");
2223 if (type == NULL)
2224 prt_write_string("()");
2225 else
2226 prt_write_string(type);
2227 prt_write_string("\n");
2228}
2229
2230 void
2231mch_print_cleanup()
2232{
2233#ifdef FEAT_MBYTE
2234 if (prt_out_mbyte)
2235 {
2236 int i;
2237
2238 /* Free off all CID font names created, but first clear duplicate
2239 * pointers to the same string (when the same font is used for more than
2240 * one style).
2241 */
2242 for (i = PRT_PS_FONT_ROMAN; i <= PRT_PS_FONT_BOLDOBLIQUE; i++)
2243 {
2244 if (prt_ps_mb_font.ps_fontname[i] != NULL)
2245 vim_free(prt_ps_mb_font.ps_fontname[i]);
2246 prt_ps_mb_font.ps_fontname[i] = NULL;
2247 }
2248 }
2249
2250 if (prt_do_conv)
2251 {
2252 convert_setup(&prt_conv, NULL, NULL);
2253 prt_do_conv = FALSE;
2254 }
2255#endif
2256 if (prt_ps_fd != NULL)
2257 {
2258 fclose(prt_ps_fd);
2259 prt_ps_fd = NULL;
2260 prt_file_error = FALSE;
2261 }
2262 if (prt_ps_file_name != NULL)
2263 {
2264 vim_free(prt_ps_file_name);
2265 prt_ps_file_name = NULL;
2266 }
2267}
2268
2269 static float
2270to_device_units(idx, physsize, def_number)
2271 int idx;
2272 double physsize;
2273 int def_number;
2274{
2275 float ret;
2276 int u;
2277 int nr;
2278
2279 u = prt_get_unit(idx);
2280 if (u == PRT_UNIT_NONE)
2281 {
2282 u = PRT_UNIT_PERC;
2283 nr = def_number;
2284 }
2285 else
2286 nr = printer_opts[idx].number;
2287
2288 switch (u)
2289 {
2290 case PRT_UNIT_INCH:
2291 ret = (float)(nr * PRT_PS_DEFAULT_DPI);
2292 break;
2293 case PRT_UNIT_MM:
2294 ret = (float)(nr * PRT_PS_DEFAULT_DPI) / (float)25.4;
2295 break;
2296 case PRT_UNIT_POINT:
2297 ret = (float)nr;
2298 break;
2299 case PRT_UNIT_PERC:
2300 default:
2301 ret = (float)(physsize * nr) / 100;
2302 break;
2303 }
2304
2305 return ret;
2306}
2307
2308/*
2309 * Calculate margins for given width and height from printoptions settings.
2310 */
2311 static void
2312prt_page_margins(width, height, left, right, top, bottom)
2313 double width;
2314 double height;
2315 double *left;
2316 double *right;
2317 double *top;
2318 double *bottom;
2319{
2320 *left = to_device_units(OPT_PRINT_LEFT, width, 10);
2321 *right = width - to_device_units(OPT_PRINT_RIGHT, width, 5);
2322 *top = height - to_device_units(OPT_PRINT_TOP, height, 5);
2323 *bottom = to_device_units(OPT_PRINT_BOT, height, 5);
2324}
2325
2326 static void
2327prt_font_metrics(font_scale)
2328 int font_scale;
2329{
2330 prt_line_height = (float)font_scale;
2331 prt_char_width = (float)PRT_PS_FONT_TO_USER(font_scale, prt_ps_font->wx);
2332}
2333
2334
2335 static int
2336prt_get_cpl()
2337{
2338 if (prt_use_number())
2339 {
2340 prt_number_width = PRINT_NUMBER_WIDTH * prt_char_width;
2341#ifdef FEAT_MBYTE
2342 /* If we are outputting multi-byte characters then line numbers will be
2343 * printed with half width characters
2344 */
2345 if (prt_out_mbyte)
2346 prt_number_width /= 2;
2347#endif
2348 prt_left_margin += prt_number_width;
2349 }
2350 else
2351 prt_number_width = 0.0;
2352
2353 return (int)((prt_right_margin - prt_left_margin) / prt_char_width);
2354}
2355
2356#ifdef FEAT_MBYTE
2357 static int
2358prt_build_cid_fontname(font, name, name_len)
2359 int font;
2360 char_u *name;
2361 int name_len;
2362{
2363 char *fontname;
2364
2365 fontname = (char *)alloc(name_len + 1);
2366 if (fontname == NULL)
2367 return FALSE;
2368 vim_strncpy((char_u *)fontname, name, name_len);
2369 prt_ps_mb_font.ps_fontname[font] = fontname;
2370
2371 return TRUE;
2372}
2373#endif
2374
2375/*
2376 * Get number of lines of text that fit on a page (excluding the header).
2377 */
2378 static int
2379prt_get_lpp()
2380{
2381 int lpp;
2382
2383 /*
2384 * Calculate offset to lower left corner of background rect based on actual
2385 * font height (based on its bounding box) and the line height, handling the
2386 * case where the font height can exceed the line height.
2387 */
2388 prt_bgcol_offset = (float)PRT_PS_FONT_TO_USER(prt_line_height,
2389 prt_ps_font->bbox_min_y);
2390 if ((prt_ps_font->bbox_max_y - prt_ps_font->bbox_min_y) < 1000.0)
2391 {
2392 prt_bgcol_offset -= (float)PRT_PS_FONT_TO_USER(prt_line_height,
2393 (1000.0 - (prt_ps_font->bbox_max_y -
2394 prt_ps_font->bbox_min_y)) / 2);
2395 }
2396
2397 /* Get height for topmost line based on background rect offset. */
2398 prt_first_line_height = prt_line_height + prt_bgcol_offset;
2399
2400 /* Calculate lpp */
2401 lpp = (int)((prt_top_margin - prt_bottom_margin) / prt_line_height);
2402
2403 /* Adjust top margin if there is a header */
2404 prt_top_margin -= prt_line_height * prt_header_height();
2405
2406 return lpp - prt_header_height();
2407}
2408
2409#ifdef FEAT_MBYTE
2410 static int
2411prt_match_encoding(p_encoding, p_cmap, pp_mbenc)
2412 char *p_encoding;
2413 struct prt_ps_mbfont_S *p_cmap;
2414 struct prt_ps_encoding_S **pp_mbenc;
2415{
2416 int mbenc;
2417 int enc_len;
2418 struct prt_ps_encoding_S *p_mbenc;
2419
2420 *pp_mbenc = NULL;
2421 /* Look for recognised encoding */
2422 enc_len = STRLEN(p_encoding);
2423 p_mbenc = p_cmap->encodings;
2424 for (mbenc = 0; mbenc < p_cmap->num_encodings; mbenc++)
2425 {
2426 if (STRNICMP(p_mbenc->encoding, p_encoding, enc_len) == 0)
2427 {
2428 *pp_mbenc = p_mbenc;
2429 return TRUE;
2430 }
2431 p_mbenc++;
2432 }
2433 return FALSE;
2434}
2435
2436 static int
2437prt_match_charset(p_charset, p_cmap, pp_mbchar)
2438 char *p_charset;
2439 struct prt_ps_mbfont_S *p_cmap;
2440 struct prt_ps_charset_S **pp_mbchar;
2441{
2442 int mbchar;
2443 int char_len;
2444 struct prt_ps_charset_S *p_mbchar;
2445
2446 /* Look for recognised character set, using default if one is not given */
2447 if (*p_charset == NUL)
2448 p_charset = p_cmap->defcs;
2449 char_len = STRLEN(p_charset);
2450 p_mbchar = p_cmap->charsets;
2451 for (mbchar = 0; mbchar < p_cmap->num_charsets; mbchar++)
2452 {
2453 if (STRNICMP(p_mbchar->charset, p_charset, char_len) == 0)
2454 {
2455 *pp_mbchar = p_mbchar;
2456 return TRUE;
2457 }
2458 p_mbchar++;
2459 }
2460 return FALSE;
2461}
2462#endif
2463
2464/*ARGSUSED*/
2465 int
2466mch_print_init(psettings, jobname, forceit)
2467 prt_settings_T *psettings;
2468 char_u *jobname;
2469 int forceit;
2470{
2471 int i;
2472 char *paper_name;
2473 int paper_strlen;
2474 int fontsize;
2475 char_u *p;
2476 double left;
2477 double right;
2478 double top;
2479 double bottom;
2480#ifdef FEAT_MBYTE
2481 int cmap;
2482 char_u *p_encoding;
2483 struct prt_ps_encoding_S *p_mbenc;
2484 struct prt_ps_encoding_S *p_mbenc_first;
2485 struct prt_ps_charset_S *p_mbchar;
2486#endif
2487
2488#if 0
2489 /*
2490 * TODO:
2491 * If "forceit" is false: pop up a dialog to select:
2492 * - printer name
2493 * - copies
2494 * - collated/uncollated
2495 * - duplex off/long side/short side
2496 * - paper size
2497 * - portrait/landscape
2498 * - font size
2499 *
2500 * If "forceit" is true: use the default printer and settings
2501 */
2502 if (forceit)
2503 s_pd.Flags |= PD_RETURNDEFAULT;
2504#endif
2505
2506 /*
2507 * Set up font and encoding.
2508 */
2509#ifdef FEAT_MBYTE
2510 p_encoding = enc_skip(p_penc);
2511 if (*p_encoding == NUL)
2512 p_encoding = enc_skip(p_enc);
2513
2514 /* Look for recognised multi-byte coding, and if the charset is recognised.
2515 * This is to cope with the fact that various unicode encodings are
2516 * supported in more than one of CJK. */
2517 p_mbenc = NULL;
2518 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],
2522 &p_mbenc))
2523 {
2524 if (p_mbenc_first == NULL)
2525 p_mbenc_first = p_mbenc;
2526 if (prt_match_charset((char *)p_pmcs, &prt_ps_mbfonts[cmap],
2527 &p_mbchar))
2528 break;
2529 }
2530
2531 /* Use first encoding matched if no charset matched */
2532 if (p_mbchar == NULL && p_mbenc_first != NULL)
2533 p_mbenc = p_mbenc_first;
2534
2535 prt_out_mbyte = (p_mbenc != NULL);
2536 if (prt_out_mbyte)
2537 {
2538 /* Build CMap name - will be same for all multi-byte fonts used */
2539 prt_cmap[0] = NUL;
2540
2541 prt_custom_cmap = prt_out_mbyte && p_mbchar == NULL;
2542
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 }
2551
2552 /* Add charset name if not empty */
2553 if (p_mbchar->cmap_charset != NULL)
2554 {
2555 vim_strncpy((char_u *)prt_cmap,
2556 (char_u *)p_mbchar->cmap_charset, sizeof(prt_cmap) - 3);
2557 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 }
2571
2572 /* CMap name ends with (optional) encoding name and -H for horizontal */
2573 if (p_mbenc->cmap_encoding != NULL && STRLEN(prt_cmap)
2574 + STRLEN(p_mbenc->cmap_encoding) + 3 < sizeof(prt_cmap))
2575 {
2576 STRCAT(prt_cmap, p_mbenc->cmap_encoding);
2577 STRCAT(prt_cmap, "-");
2578 }
2579 STRCAT(prt_cmap, "H");
2580
2581 if (!mbfont_opts[OPT_MBFONT_REGULAR].present)
2582 {
2583 EMSG(_("E675: No default font specified for multi-byte printing."));
2584 return FALSE;
2585 }
2586
2587 /* 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,
2604 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].string,
2605 mbfont_opts[OPT_MBFONT_BOLDOBLIQUE].strlen))
2606 return FALSE;
2607
2608 /* 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 }
2621
2622 prt_ps_font = &prt_ps_mb_font;
2623 }
2624 else
2625#endif
2626 {
2627#ifdef FEAT_MBYTE
2628 prt_use_courier = FALSE;
2629#endif
2630 prt_ps_font = &prt_ps_courier_font;
2631 }
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)
2784 prt_bufsiz *= 2;
2785#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;
2834 prt_write_file_raw_len(resource_buffer, bytes_read);
2835 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;
2865#ifdef FEAT_MBYTE
2866 struct prt_ps_resource_S res_cidfont;
2867 struct prt_ps_resource_S res_cmap;
2868#endif
2869
2870 /*
2871 * PS DSC Header comments - no PS code!
2872 */
2873 prt_dsc_start();
2874 prt_dsc_textline("Title", (char *)psettings->jobname);
2875 if (!get_user_name((char_u *)buffer, 256))
2876 STRCPY(buffer, "Unknown");
2877 prt_dsc_textline("For", buffer);
2878 prt_dsc_textline("Creator", VIM_VERSION_LONG);
2879 /* Note: to ensure Clean8bit I don't think we can use LC_TIME */
2880 now = time(NULL);
2881 p_time = ctime(&now);
2882 /* Note: ctime() adds a \n so we have to remove it :-( */
2883 *(vim_strchr((char_u *)p_time, '\n')) = '\0';
2884 prt_dsc_textline("CreationDate", p_time);
2885 prt_dsc_textline("DocumentData", "Clean8Bit");
2886 prt_dsc_textline("Orientation", "Portrait");
2887 prt_dsc_atend("Pages");
2888 prt_dsc_textline("PageOrder", "Ascend");
2889 /* The bbox does not change with orientation - it is always in the default
2890 * user coordinate system! We have to recalculate right and bottom
2891 * coordinates based on the font metrics for the bbox to be accurate. */
2892 prt_page_margins(prt_mediasize[prt_media].width,
2893 prt_mediasize[prt_media].height,
2894 &left, &right, &top, &bottom);
2895 bbox[0] = (int)left;
2896 if (prt_portrait)
2897 {
2898 /* In portrait printing the fixed point is the top left corner so we
2899 * derive the bbox from that point. We have the expected cpl chars
2900 * across the media and lpp lines down the media.
2901 */
2902 bbox[1] = (int)(top - (psettings->lines_per_page + prt_header_height())
2903 * prt_line_height);
2904 bbox[2] = (int)(left + psettings->chars_per_line * prt_char_width
2905 + 0.5);
2906 bbox[3] = (int)(top + 0.5);
2907 }
2908 else
2909 {
2910 /* In landscape printing the fixed point is the bottom left corner so we
2911 * derive the bbox from that point. We have lpp chars across the media
2912 * and cpl lines up the media.
2913 */
2914 bbox[1] = (int)bottom;
2915 bbox[2] = (int)(left + ((psettings->lines_per_page
2916 + prt_header_height()) * prt_line_height) + 0.5);
2917 bbox[3] = (int)(bottom + psettings->chars_per_line * prt_char_width
2918 + 0.5);
2919 }
2920 prt_dsc_ints("BoundingBox", 4, bbox);
2921 /* The media width and height does not change with landscape printing! */
2922 prt_dsc_docmedia(prt_mediasize[prt_media].name,
2923 prt_mediasize[prt_media].width,
2924 prt_mediasize[prt_media].height,
2925 (double)0, NULL, NULL);
2926 /* Define fonts needed */
2927#ifdef FEAT_MBYTE
2928 if (!prt_out_mbyte || prt_use_courier)
2929#endif
2930 prt_dsc_font_resource("DocumentNeededResources", &prt_ps_courier_font);
2931#ifdef FEAT_MBYTE
2932 if (prt_out_mbyte)
2933 {
2934 prt_dsc_font_resource((prt_use_courier ? NULL
2935 : "DocumentNeededResources"), &prt_ps_mb_font);
2936 if (!prt_custom_cmap)
2937 prt_dsc_resources(NULL, "cmap", prt_cmap);
2938 }
2939#endif
2940
2941 /* Search for external resources VIM supplies */
2942 if (!prt_find_resource("prolog", &res_prolog))
2943 {
2944 EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
2945 return FALSE;
2946 }
2947 if (!prt_open_resource(&res_prolog))
2948 return FALSE;
2949 if (!prt_check_resource(&res_prolog, PRT_PROLOG_VERSION))
2950 return FALSE;
2951#ifdef FEAT_MBYTE
2952 if (prt_out_mbyte)
2953 {
2954 /* Look for required version of multi-byte printing procset */
2955 if (!prt_find_resource("cidfont", &res_cidfont))
2956 {
2957 EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
2958 return FALSE;
2959 }
2960 if (!prt_open_resource(&res_cidfont))
2961 return FALSE;
2962 if (!prt_check_resource(&res_cidfont, PRT_CID_PROLOG_VERSION))
2963 return FALSE;
2964 }
2965#endif
2966
2967 /* Find an encoding to use for printing.
2968 * Check 'printencoding'. If not set or not found, then use 'encoding'. If
2969 * that cannot be found then default to "latin1".
2970 * Note: VIM specific encoding header is always skipped.
2971 */
2972#ifdef FEAT_MBYTE
2973 if (!prt_out_mbyte)
2974 {
2975#endif
2976 p_encoding = enc_skip(p_penc);
2977 if (*p_encoding == NUL
2978 || !prt_find_resource((char *)p_encoding, &res_encoding))
2979 {
2980 /* 'printencoding' not set or not supported - find alternate */
2981#ifdef FEAT_MBYTE
2982 int props;
2983
2984 p_encoding = enc_skip(p_enc);
2985 props = enc_canon_props(p_encoding);
2986 if (!(props & ENC_8BIT)
2987 || !prt_find_resource((char *)p_encoding, &res_encoding))
2988 /* 8-bit 'encoding' is not supported */
2989#endif
2990 {
2991 /* Use latin1 as default printing encoding */
2992 p_encoding = (char_u *)"latin1";
2993 if (!prt_find_resource((char *)p_encoding, &res_encoding))
2994 {
2995 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
2996 p_encoding);
2997 return FALSE;
2998 }
2999 }
3000 }
3001 if (!prt_open_resource(&res_encoding))
3002 return FALSE;
3003 /* For the moment there are no checks on encoding resource files to
3004 * perform */
3005#ifdef FEAT_MBYTE
3006 }
3007 else
3008 {
3009 p_encoding = enc_skip(p_penc);
3010 if (*p_encoding == NUL)
3011 p_encoding = enc_skip(p_enc);
3012 if (prt_use_courier)
3013 {
3014 /* Include ASCII range encoding vector */
3015 if (!prt_find_resource(prt_ascii_encoding, &res_encoding))
3016 {
3017 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3018 prt_ascii_encoding);
3019 return FALSE;
3020 }
3021 if (!prt_open_resource(&res_encoding))
3022 return FALSE;
3023 /* For the moment there are no checks on encoding resource files to
3024 * perform */
3025 }
3026 }
3027
3028 prt_conv.vc_type = CONV_NONE;
3029 if (!(enc_canon_props(p_enc) & enc_canon_props(p_encoding) & ENC_8BIT)) {
3030 /* Set up encoding conversion if required */
3031 if (FAIL == convert_setup(&prt_conv, p_enc, p_encoding))
3032 {
3033 EMSG2(_("E620: Unable to convert to print encoding \"%s\""),
3034 p_encoding);
3035 return FALSE;
3036 }
3037 prt_do_conv = TRUE;
3038 }
3039 prt_do_conv = prt_conv.vc_type != CONV_NONE;
3040
3041 if (prt_out_mbyte && prt_custom_cmap)
3042 {
3043 /* Find user supplied CMap */
3044 if (!prt_find_resource(prt_cmap, &res_cmap))
3045 {
3046 EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
3047 prt_cmap);
3048 return FALSE;
3049 }
3050 if (!prt_open_resource(&res_cmap))
3051 return FALSE;
3052 }
3053#endif
3054
3055 /* List resources supplied */
3056 STRCPY(buffer, res_prolog.title);
3057 STRCAT(buffer, " ");
3058 STRCAT(buffer, res_prolog.version);
3059 prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
3060#ifdef FEAT_MBYTE
3061 if (prt_out_mbyte)
3062 {
3063 STRCPY(buffer, res_cidfont.title);
3064 STRCAT(buffer, " ");
3065 STRCAT(buffer, res_cidfont.version);
3066 prt_dsc_resources(NULL, "procset", buffer);
3067
3068 if (prt_custom_cmap)
3069 {
3070 STRCPY(buffer, res_cmap.title);
3071 STRCAT(buffer, " ");
3072 STRCAT(buffer, res_cmap.version);
3073 prt_dsc_resources(NULL, "cmap", buffer);
3074 }
3075 }
3076 if (!prt_out_mbyte || prt_use_courier)
3077#endif
3078 {
3079 STRCPY(buffer, res_encoding.title);
3080 STRCAT(buffer, " ");
3081 STRCAT(buffer, res_encoding.version);
3082 prt_dsc_resources(NULL, "encoding", buffer);
3083 }
3084 prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
3085#ifdef FEAT_SYN_HL
3086 psettings->do_syntax
3087#else
3088 0
3089#endif
3090 , prt_num_copies);
3091 prt_dsc_noarg("EndComments");
3092
3093 /*
3094 * PS Document page defaults
3095 */
3096 prt_dsc_noarg("BeginDefaults");
3097
3098 /* List font resources most likely common to all pages */
3099#ifdef FEAT_MBYTE
3100 if (!prt_out_mbyte || prt_use_courier)
3101#endif
3102 prt_dsc_font_resource("PageResources", &prt_ps_courier_font);
3103#ifdef FEAT_MBYTE
3104 if (prt_out_mbyte)
3105 {
3106 prt_dsc_font_resource((prt_use_courier ? NULL : "PageResources"),
3107 &prt_ps_mb_font);
3108 if (!prt_custom_cmap)
3109 prt_dsc_resources(NULL, "cmap", prt_cmap);
3110 }
3111#endif
3112
3113 /* Paper will be used for all pages */
3114 prt_dsc_textline("PageMedia", prt_mediasize[prt_media].name);
3115
3116 prt_dsc_noarg("EndDefaults");
3117
3118 /*
3119 * PS Document prolog inclusion - all required procsets.
3120 */
3121 prt_dsc_noarg("BeginProlog");
3122
3123 /* Add required procsets - NOTE: order is important! */
3124 if (!prt_add_resource(&res_prolog))
3125 return FALSE;
3126#ifdef FEAT_MBYTE
3127 if (prt_out_mbyte)
3128 {
3129 /* Add CID font procset, and any user supplied CMap */
3130 if (!prt_add_resource(&res_cidfont))
3131 return FALSE;
3132 if (prt_custom_cmap && !prt_add_resource(&res_cmap))
3133 return FALSE;
3134 }
3135#endif
3136
3137#ifdef FEAT_MBYTE
3138 if (!prt_out_mbyte || prt_use_courier)
3139#endif
3140 /* There will be only one Roman font encoding to be included in the PS
3141 * file. */
3142 if (!prt_add_resource(&res_encoding))
3143 return FALSE;
3144
3145 prt_dsc_noarg("EndProlog");
3146
3147 /*
3148 * PS Document setup - must appear after the prolog
3149 */
3150 prt_dsc_noarg("BeginSetup");
3151
3152 /* Device setup - page size and number of uncollated copies */
3153 prt_write_int((int)prt_mediasize[prt_media].width);
3154 prt_write_int((int)prt_mediasize[prt_media].height);
3155 prt_write_int(0);
3156 prt_write_string("sps\n");
3157 prt_write_int(prt_num_copies);
3158 prt_write_string("nc\n");
3159 prt_write_boolean(prt_duplex);
3160 prt_write_boolean(prt_tumble);
3161 prt_write_string("dt\n");
3162 prt_write_boolean(prt_collate);
3163 prt_write_string("c\n");
3164
3165 /* Font resource inclusion and definition */
3166#ifdef FEAT_MBYTE
3167 if (!prt_out_mbyte || prt_use_courier)
3168 {
3169 /* When using Courier for ASCII range when printing multi-byte, need to
3170 * pick up ASCII encoding to use with it. */
3171 if (prt_use_courier)
3172 p_encoding = (char_u *)prt_ascii_encoding;
3173#endif
3174 prt_dsc_resources("IncludeResource", "font",
3175 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3176 prt_def_font("F0", (char *)p_encoding, (int)prt_line_height,
3177 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3178 prt_dsc_resources("IncludeResource", "font",
3179 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3180 prt_def_font("F1", (char *)p_encoding, (int)prt_line_height,
3181 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLD]);
3182 prt_dsc_resources("IncludeResource", "font",
3183 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3184 prt_def_font("F2", (char *)p_encoding, (int)prt_line_height,
3185 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3186 prt_dsc_resources("IncludeResource", "font",
3187 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3188 prt_def_font("F3", (char *)p_encoding, (int)prt_line_height,
3189 prt_ps_courier_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3190#ifdef FEAT_MBYTE
3191 }
3192 if (prt_out_mbyte)
3193 {
3194 /* Define the CID fonts to be used in the job. Typically CJKV fonts do
3195 * not have an italic form being a western style, so where no font is
3196 * defined for these faces VIM falls back to an existing face.
3197 * Note: if using Courier for the ASCII range then the printout will
3198 * have bold/italic/bolditalic regardless of the setting of printmbfont.
3199 */
3200 prt_dsc_resources("IncludeResource", "font",
3201 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3202 if (!prt_custom_cmap)
3203 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3204 prt_def_cidfont("CF0", (int)prt_line_height,
3205 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_ROMAN]);
3206
3207 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD] != NULL)
3208 {
3209 prt_dsc_resources("IncludeResource", "font",
3210 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3211 if (!prt_custom_cmap)
3212 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3213 prt_def_cidfont("CF1", (int)prt_line_height,
3214 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLD]);
3215 }
3216 else
3217 /* Use ROMAN for BOLD */
3218 prt_dup_cidfont("CF0", "CF1");
3219
3220 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE] != NULL)
3221 {
3222 prt_dsc_resources("IncludeResource", "font",
3223 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3224 if (!prt_custom_cmap)
3225 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3226 prt_def_cidfont("CF2", (int)prt_line_height,
3227 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_OBLIQUE]);
3228 }
3229 else
3230 /* Use ROMAN for OBLIQUE */
3231 prt_dup_cidfont("CF0", "CF2");
3232
3233 if (prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE] != NULL)
3234 {
3235 prt_dsc_resources("IncludeResource", "font",
3236 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3237 if (!prt_custom_cmap)
3238 prt_dsc_resources("IncludeResource", "cmap", prt_cmap);
3239 prt_def_cidfont("CF3", (int)prt_line_height,
3240 prt_ps_mb_font.ps_fontname[PRT_PS_FONT_BOLDOBLIQUE]);
3241 }
3242 else
3243 /* Use BOLD for BOLDOBLIQUE */
3244 prt_dup_cidfont("CF1", "CF3");
3245 }
3246#endif
3247
3248 /* Misc constant vars used for underlining and background rects */
3249 prt_def_var("UO", PRT_PS_FONT_TO_USER(prt_line_height,
3250 prt_ps_font->uline_offset), 2);
3251 prt_def_var("UW", PRT_PS_FONT_TO_USER(prt_line_height,
3252 prt_ps_font->uline_width), 2);
3253 prt_def_var("BO", prt_bgcol_offset, 2);
3254
3255 prt_dsc_noarg("EndSetup");
3256
3257 /* Fail if any problems writing out to the PS file */
3258 return !prt_file_error;
3259}
3260
3261 void
3262mch_print_end(psettings)
3263 prt_settings_T *psettings;
3264{
3265 prt_dsc_noarg("Trailer");
3266
3267 /*
3268 * Output any info we don't know in toto until we finish
3269 */
3270 prt_dsc_ints("Pages", 1, &prt_page_num);
3271
3272 prt_dsc_noarg("EOF");
3273
3274 /* Write CTRL-D to close serial communication link if used.
3275 * NOTHING MUST BE WRITTEN AFTER THIS! */
3276 prt_write_file((char_u *)IF_EB("\004", "\067"));
3277
3278 if (!prt_file_error && psettings->outfile == NULL
3279 && !got_int && !psettings->user_abort)
3280 {
3281 /* Close the file first. */
3282 if (prt_ps_fd != NULL)
3283 {
3284 fclose(prt_ps_fd);
3285 prt_ps_fd = NULL;
3286 }
3287 prt_message((char_u *)_("Sending to printer..."));
3288
3289 /* Not printing to a file: use 'printexpr' to print the file. */
3290 if (eval_printexpr(prt_ps_file_name, psettings->arguments) == FAIL)
3291 EMSG(_("E365: Failed to print PostScript file"));
3292 else
3293 prt_message((char_u *)_("Print job sent."));
3294 }
3295
3296 mch_print_cleanup();
3297}
3298
3299 int
3300mch_print_end_page()
3301{
3302 prt_flush_buffer();
3303
3304 prt_write_string("re sp\n");
3305
3306 prt_dsc_noarg("PageTrailer");
3307
3308 return !prt_file_error;
3309}
3310
3311/*ARGSUSED*/
3312 int
3313mch_print_begin_page(str)
3314 char_u *str;
3315{
3316 int page_num[2];
3317
3318 prt_page_num++;
3319
3320 page_num[0] = page_num[1] = prt_page_num;
3321 prt_dsc_ints("Page", 2, page_num);
3322
3323 prt_dsc_noarg("BeginPageSetup");
3324
3325 prt_write_string("sv\n0 g\n");
3326#ifdef FEAT_MBYTE
3327 prt_in_ascii = !prt_out_mbyte;
3328 if (prt_out_mbyte)
3329 prt_write_string("CF0 sf\n");
3330 else
3331#endif
3332 prt_write_string("F0 sf\n");
3333 prt_fgcol = PRCOLOR_BLACK;
3334 prt_bgcol = PRCOLOR_WHITE;
3335 prt_font = PRT_PS_FONT_ROMAN;
3336
3337 /* Set up page transformation for landscape printing. */
3338 if (!prt_portrait)
3339 {
3340 prt_write_int(-((int)prt_mediasize[prt_media].width));
3341 prt_write_string("sl\n");
3342 }
3343
3344 prt_dsc_noarg("EndPageSetup");
3345
3346 /* We have reset the font attributes, force setting them again. */
3347 curr_bg = (long_u)0xffffffff;
3348 curr_fg = (long_u)0xffffffff;
3349 curr_bold = MAYBE;
3350
3351 return !prt_file_error;
3352}
3353
3354 int
3355mch_print_blank_page()
3356{
3357 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
3358}
3359
3360static float prt_pos_x = 0;
3361static float prt_pos_y = 0;
3362
3363 void
3364mch_print_start_line(margin, page_line)
3365 int margin;
3366 int page_line;
3367{
3368 prt_pos_x = prt_left_margin;
3369 if (margin)
3370 prt_pos_x -= prt_number_width;
3371
3372 prt_pos_y = prt_top_margin - prt_first_line_height -
3373 page_line * prt_line_height;
3374
3375 prt_attribute_change = TRUE;
3376 prt_need_moveto = TRUE;
3377#ifdef FEAT_MBYTE
3378 prt_half_width = FALSE;
3379#endif
3380}
3381
3382/*ARGSUSED*/
3383 int
3384mch_print_text_out(p, len)
3385 char_u *p;
3386 int len;
3387{
3388 int need_break;
3389 char_u ch;
3390 char_u ch_buff[8];
3391 float char_width;
3392 float next_pos;
3393#ifdef FEAT_MBYTE
3394 int in_ascii;
3395 int half_width;
3396#endif
3397
3398 char_width = prt_char_width;
3399
3400#ifdef FEAT_MBYTE
3401 /* Ideally VIM would create a rearranged CID font to combine a Roman and
3402 * CJKV font to do what VIM is doing here - use a Roman font for characters
3403 * in the ASCII range, and the origingal CID font for everything else.
3404 * The problem is that GhostScript still (as of 8.13) does not support
3405 * rearranged fonts even though they have been documented by Adobe for 7
3406 * years! If they ever do, a lot of this code will disappear.
3407 */
3408 if (prt_use_courier)
3409 {
3410 in_ascii = (len == 1 && *p < 0x80);
3411 if (prt_in_ascii)
3412 {
3413 if (!in_ascii)
3414 {
3415 /* No longer in ASCII range - need to switch font */
3416 prt_in_ascii = FALSE;
3417 prt_need_font = TRUE;
3418 prt_attribute_change = TRUE;
3419 }
3420 }
3421 else if (in_ascii)
3422 {
3423 /* Now in ASCII range - need to switch font */
3424 prt_in_ascii = TRUE;
3425 prt_need_font = TRUE;
3426 prt_attribute_change = TRUE;
3427 }
3428 }
3429 if (prt_out_mbyte)
3430 {
3431 half_width = ((*mb_ptr2cells)(p) == 1);
3432 if (half_width)
3433 char_width /= 2;
3434 if (prt_half_width)
3435 {
3436 if (!half_width)
3437 {
3438 prt_half_width = FALSE;
3439 prt_pos_x += prt_char_width/4;
3440 prt_need_moveto = TRUE;
3441 prt_attribute_change = TRUE;
3442 }
3443 }
3444 else if (half_width)
3445 {
3446 prt_half_width = TRUE;
3447 prt_pos_x += prt_char_width/4;
3448 prt_need_moveto = TRUE;
3449 prt_attribute_change = TRUE;
3450 }
3451 }
3452#endif
3453
3454 /* Output any required changes to the graphics state, after flushing any
3455 * text buffered so far.
3456 */
3457 if (prt_attribute_change)
3458 {
3459 prt_flush_buffer();
3460 /* Reset count of number of chars that will be printed */
3461 prt_text_run = 0;
3462
3463 if (prt_need_moveto)
3464 {
3465 prt_pos_x_moveto = prt_pos_x;
3466 prt_pos_y_moveto = prt_pos_y;
3467 prt_do_moveto = TRUE;
3468
3469 prt_need_moveto = FALSE;
3470 }
3471 if (prt_need_font)
3472 {
3473#ifdef FEAT_MBYTE
3474 if (!prt_in_ascii)
3475 prt_write_string("CF");
3476 else
3477#endif
3478 prt_write_string("F");
3479 prt_write_int(prt_font);
3480 prt_write_string("sf\n");
3481 prt_need_font = FALSE;
3482 }
3483 if (prt_need_fgcol)
3484 {
3485 int r, g, b;
3486 r = ((unsigned)prt_fgcol & 0xff0000) >> 16;
3487 g = ((unsigned)prt_fgcol & 0xff00) >> 8;
3488 b = prt_fgcol & 0xff;
3489
3490 prt_write_real(r / 255.0, 3);
3491 if (r == g && g == b)
3492 prt_write_string("g\n");
3493 else
3494 {
3495 prt_write_real(g / 255.0, 3);
3496 prt_write_real(b / 255.0, 3);
3497 prt_write_string("r\n");
3498 }
3499 prt_need_fgcol = FALSE;
3500 }
3501
3502 if (prt_bgcol != PRCOLOR_WHITE)
3503 {
3504 prt_new_bgcol = prt_bgcol;
3505 if (prt_need_bgcol)
3506 prt_do_bgcol = TRUE;
3507 }
3508 else
3509 prt_do_bgcol = FALSE;
3510 prt_need_bgcol = FALSE;
3511
3512 if (prt_need_underline)
3513 prt_do_underline = prt_underline;
3514 prt_need_underline = FALSE;
3515
3516 prt_attribute_change = FALSE;
3517 }
3518
3519#ifdef FEAT_MBYTE
3520 if (prt_do_conv)
3521 {
3522 /* Convert from multi-byte to 8-bit encoding */
3523 p = string_convert(&prt_conv, p, &len);
3524 if (p == NULL)
3525 p = (char_u *)"";
3526 }
3527
3528 if (prt_out_mbyte)
3529 {
3530 /* Multi-byte character strings are represented more efficiently as hex
3531 * strings when outputting clean 8 bit PS.
3532 */
3533 do
3534 {
3535 ch = prt_hexchar[(unsigned)(*p) >> 4];
3536 ga_append(&prt_ps_buffer, ch);
3537 ch = prt_hexchar[(*p) & 0xf];
3538 ga_append(&prt_ps_buffer, ch);
3539 p++;
3540 }
3541 while (--len);
3542 }
3543 else
3544#endif
3545 {
3546 /* Add next character to buffer of characters to output.
3547 * Note: One printed character may require several PS characters to
3548 * represent it, but we only count them as one printed character.
3549 */
3550 ch = *p;
3551 if (ch < 32 || ch == '(' || ch == ')' || ch == '\\')
3552 {
3553 /* Convert non-printing characters to either their escape or octal
3554 * sequence, ensures PS sent over a serial line does not interfere
3555 * with the comms protocol. Note: For EBCDIC we need to write out
3556 * the escape sequences as ASCII codes!
3557 * Note 2: Char codes < 32 are identical in EBCDIC and ASCII AFAIK!
3558 */
3559 ga_append(&prt_ps_buffer, IF_EB('\\', 0134));
3560 switch (ch)
3561 {
3562 case BS: ga_append(&prt_ps_buffer, IF_EB('b', 0142)); break;
3563 case TAB: ga_append(&prt_ps_buffer, IF_EB('t', 0164)); break;
3564 case NL: ga_append(&prt_ps_buffer, IF_EB('n', 0156)); break;
3565 case FF: ga_append(&prt_ps_buffer, IF_EB('f', 0146)); break;
3566 case CAR: ga_append(&prt_ps_buffer, IF_EB('r', 0162)); break;
3567 case '(': ga_append(&prt_ps_buffer, IF_EB('(', 0050)); break;
3568 case ')': ga_append(&prt_ps_buffer, IF_EB(')', 0051)); break;
3569 case '\\': ga_append(&prt_ps_buffer, IF_EB('\\', 0134)); break;
3570
3571 default:
3572 sprintf((char *)ch_buff, "%03o", (unsigned int)ch);
3573#ifdef EBCDIC
3574 ebcdic2ascii(ch_buff, 3);
3575#endif
3576 ga_append(&prt_ps_buffer, ch_buff[0]);
3577 ga_append(&prt_ps_buffer, ch_buff[1]);
3578 ga_append(&prt_ps_buffer, ch_buff[2]);
3579 break;
3580 }
3581 }
3582 else
3583 ga_append(&prt_ps_buffer, ch);
3584 }
3585
3586#ifdef FEAT_MBYTE
3587 /* Need to free any translated characters */
3588 if (prt_do_conv && (*p != NUL))
3589 vim_free(p);
3590#endif
3591
3592 prt_text_run += char_width;
3593 prt_pos_x += char_width;
3594
3595 /* The downside of fp - use relative error on right margin check */
3596 next_pos = prt_pos_x + prt_char_width;
3597 need_break = (next_pos > prt_right_margin) &&
3598 ((next_pos - prt_right_margin) > (prt_right_margin*1e-5));
3599
3600 if (need_break)
3601 prt_flush_buffer();
3602
3603 return need_break;
3604}
3605
3606 void
3607mch_print_set_font(iBold, iItalic, iUnderline)
3608 int iBold;
3609 int iItalic;
3610 int iUnderline;
3611{
3612 int font = 0;
3613
3614 if (iBold)
3615 font |= 0x01;
3616 if (iItalic)
3617 font |= 0x02;
3618
3619 if (font != prt_font)
3620 {
3621 prt_font = font;
3622 prt_attribute_change = TRUE;
3623 prt_need_font = TRUE;
3624 }
3625 if (prt_underline != iUnderline)
3626 {
3627 prt_underline = iUnderline;
3628 prt_attribute_change = TRUE;
3629 prt_need_underline = TRUE;
3630 }
3631}
3632
3633 void
3634mch_print_set_bg(bgcol)
3635 long_u bgcol;
3636{
3637 prt_bgcol = bgcol;
3638 prt_attribute_change = TRUE;
3639 prt_need_bgcol = TRUE;
3640}
3641
3642 void
3643mch_print_set_fg(fgcol)
3644 long_u fgcol;
3645{
3646 if (fgcol != (long_u)prt_fgcol)
3647 {
3648 prt_fgcol = fgcol;
3649 prt_attribute_change = TRUE;
3650 prt_need_fgcol = TRUE;
3651 }
3652}
3653
3654# endif /*FEAT_POSTSCRIPT*/
3655#endif /*FEAT_PRINTER*/