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