diff options
Diffstat (limited to 'src/nvim/hardcopy.c')
| -rw-r--r-- | src/nvim/hardcopy.c | 3192 |
1 files changed, 0 insertions, 3192 deletions
diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c deleted file mode 100644 index e8410d1ee7..0000000000 --- a/src/nvim/hardcopy.c +++ /dev/null @@ -1,3192 +0,0 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - -/* - * hardcopy.c: printing to paper - */ - -#include <assert.h> -#include <inttypes.h> -#include <string.h> - -#include "nvim/ascii.h" -#include "nvim/vim.h" -#ifdef HAVE_LOCALE_H -# include <locale.h> -#endif -#include "nvim/buffer.h" -#include "nvim/charset.h" -#include "nvim/eval.h" -#include "nvim/ex_docmd.h" -#include "nvim/fileio.h" -#include "nvim/garray.h" -#include "nvim/grid.h" -#include "nvim/hardcopy.h" -#include "nvim/highlight_group.h" -#include "nvim/indent.h" -#include "nvim/mbyte.h" -#include "nvim/memline.h" -#include "nvim/memory.h" -#include "nvim/message.h" -#include "nvim/option.h" -#include "nvim/os/input.h" -#include "nvim/os/os.h" -#include "nvim/path.h" -#include "nvim/runtime.h" -#include "nvim/statusline.h" -#include "nvim/strings.h" -#include "nvim/syntax.h" -#include "nvim/ui.h" -#include "nvim/version.h" - -/* - * To implement printing on a platform, the following functions must be - * defined: - * - * int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit) - * Called once. Code should display printer dialogue (if appropriate) and - * determine printer font and margin settings. Reset has_color if the printer - * doesn't support colors at all. - * Returns FAIL to abort. - * - * int mch_print_begin(prt_settings_T *settings) - * Called to start the print job. - * Return FALSE to abort. - * - * int mch_print_begin_page(char_u *msg) - * Called at the start of each page. - * "msg" indicates the progress of the print job, can be NULL. - * Return FALSE to abort. - * - * int mch_print_end_page() - * Called at the end of each page. - * Return FALSE to abort. - * - * int mch_print_blank_page() - * Called to generate a blank page for collated, duplex, multiple copy - * document. Return FALSE to abort. - * - * void mch_print_end(prt_settings_T *psettings) - * Called at normal end of print job. - * - * void mch_print_cleanup() - * Called if print job ends normally or is abandoned. Free any memory, close - * devices and handles. Also called when mch_print_begin() fails, but not - * when mch_print_init() fails. - * - * void mch_print_set_font(int Bold, int Italic, int Underline); - * Called whenever the font style changes. - * - * void mch_print_set_bg(uint32_t bgcol); - * Called to set the background color for the following text. Parameter is an - * RGB value. - * - * void mch_print_set_fg(uint32_t fgcol); - * Called to set the foreground color for the following text. Parameter is an - * RGB value. - * - * mch_print_start_line(int margin, int page_line) - * Sets the current position at the start of line "page_line". - * If margin is TRUE start in the left margin (for header and line number). - * - * int mch_print_text_out(char_u *p, size_t len); - * Output one character of text p[len] at the current position. - * Return TRUE if there is no room for another character in the same line. - * - * Note that the generic code has no idea of margins. The machine code should - * simply make the page look smaller! The header and the line numbers are - * printed in the margin. - */ - -static option_table_T printer_opts[OPT_PRINT_NUM_OPTIONS] - = - { - { "top", TRUE, 0, NULL, 0, FALSE }, - { "bottom", TRUE, 0, NULL, 0, FALSE }, - { "left", TRUE, 0, NULL, 0, FALSE }, - { "right", TRUE, 0, NULL, 0, FALSE }, - { "header", TRUE, 0, NULL, 0, FALSE }, - { "syntax", FALSE, 0, NULL, 0, FALSE }, - { "number", FALSE, 0, NULL, 0, FALSE }, - { "wrap", FALSE, 0, NULL, 0, FALSE }, - { "duplex", FALSE, 0, NULL, 0, FALSE }, - { "portrait", FALSE, 0, NULL, 0, FALSE }, - { "paper", FALSE, 0, NULL, 0, FALSE }, - { "collate", FALSE, 0, NULL, 0, FALSE }, - { "jobsplit", FALSE, 0, NULL, 0, FALSE }, - { "formfeed", FALSE, 0, NULL, 0, FALSE }, - } -; - -static const uint32_t cterm_color_8[8] = { - 0x000000, 0xff0000, 0x00ff00, 0xffff00, - 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff -}; - -static const uint32_t cterm_color_16[16] = { - 0x000000, 0x0000c0, 0x008000, 0x004080, - 0xc00000, 0xc000c0, 0x808000, 0xc0c0c0, - 0x808080, 0x6060ff, 0x00ff00, 0x00ffff, - 0xff8080, 0xff40ff, 0xffff00, 0xffffff -}; - -static int current_syn_id; - -#define PRCOLOR_BLACK 0 -#define PRCOLOR_WHITE 0xffffff - -static TriState curr_italic; -static TriState curr_bold; -static TriState curr_underline; -static uint32_t curr_bg; -static uint32_t curr_fg; -static int page_count; - -#define OPT_MBFONT_USECOURIER 0 -#define OPT_MBFONT_ASCII 1 -#define OPT_MBFONT_REGULAR 2 -#define OPT_MBFONT_BOLD 3 -#define OPT_MBFONT_OBLIQUE 4 -#define OPT_MBFONT_BOLDOBLIQUE 5 -#define OPT_MBFONT_NUM_OPTIONS 6 - -static option_table_T mbfont_opts[OPT_MBFONT_NUM_OPTIONS] = -{ - { "c", FALSE, 0, NULL, 0, FALSE }, - { "a", FALSE, 0, NULL, 0, FALSE }, - { "r", FALSE, 0, NULL, 0, FALSE }, - { "b", FALSE, 0, NULL, 0, FALSE }, - { "i", FALSE, 0, NULL, 0, FALSE }, - { "o", FALSE, 0, NULL, 0, FALSE }, -}; - -/* - * These values determine the print position on a page. - */ -typedef struct { - int lead_spaces; // remaining spaces for a TAB - int print_pos; // virtual column for computing TABs - colnr_T column; // byte column - linenr_T file_line; // line nr in the buffer - size_t bytes_printed; // bytes printed so far - int ff; // seen form feed character -} prt_pos_T; - -struct prt_mediasize_S { - char *name; - double width; // width and height in points for portrait - double height; -}; - -// PS font names, must be in Roman, Bold, Italic, Bold-Italic order -struct prt_ps_font_S { - int wx; - int uline_offset; - int uline_width; - int bbox_min_y; - int bbox_max_y; - char *(ps_fontname[4]); -}; - -/* Structures to map user named encoding and mapping to PS equivalents for - * building CID font name */ -struct prt_ps_encoding_S { - char *encoding; - char *cmap_encoding; - int needs_charset; -}; - -struct prt_ps_charset_S { - char *charset; - char *cmap_charset; - int has_charset; -}; - -// Collections of encodings and charsets for multi-byte printing -struct prt_ps_mbfont_S { - int num_encodings; - struct prt_ps_encoding_S *encodings; - int num_charsets; - struct prt_ps_charset_S *charsets; - char *ascii_enc; - char *defcs; -}; - -// Types of PS resource file currently used -typedef enum { - PRT_RESOURCE_TYPE_PROCSET = 0, - PRT_RESOURCE_TYPE_ENCODING = 1, - PRT_RESOURCE_TYPE_CMAP = 2, -} PrtResourceType; - -// String versions of PS resource types -static const char *const prt_resource_types[] = -{ - [PRT_RESOURCE_TYPE_PROCSET] = "procset", - [PRT_RESOURCE_TYPE_ENCODING] = "encoding", - [PRT_RESOURCE_TYPE_CMAP] = "cmap", -}; - -struct prt_ps_resource_S { - char_u name[64]; - char_u filename[MAXPATHL + 1]; - PrtResourceType type; - char_u title[256]; - char_u version[256]; -}; - -struct prt_dsc_comment_S { - char *string; - int len; - int type; -}; - -struct prt_dsc_line_S { - int type; - char_u *string; - int len; -}; - -/* Static buffer to read initial comments in a resource file, some can have a - * couple of KB of comments! */ -#define PRT_FILE_BUFFER_LEN (2048) -struct prt_resfile_buffer_S { - char_u buffer[PRT_FILE_BUFFER_LEN]; - int len; - int line_start; - int line_end; -}; - -#ifdef INCLUDE_GENERATED_DECLARATIONS -# include "hardcopy.c.generated.h" -#endif - -/* - * Parse 'printoptions' and set the flags in "printer_opts". - * Returns an error message or NULL; - */ -char *parse_printoptions(void) -{ - return parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS); -} - -/* - * Parse 'printoptions' and set the flags in "printer_opts". - * Returns an error message or NULL; - */ -char *parse_printmbfont(void) -{ - return parse_list_options(p_pmfn, mbfont_opts, OPT_MBFONT_NUM_OPTIONS); -} - -/* - * Parse a list of options in the form - * option:value,option:value,option:value - * - * "value" can start with a number which is parsed out, e.g. margin:12mm - * - * Returns an error message for an illegal option, NULL otherwise. - * Only used for the printer at the moment... - */ -static char *parse_list_options(char_u *option_str, option_table_T *table, size_t table_size) -{ - option_table_T *old_opts; - char *ret = NULL; - char_u *stringp; - char_u *colonp; - char *commap; - char *p; - size_t idx = 0; // init for GCC - int len; - - // Save the old values, so that they can be restored in case of an error. - old_opts = (option_table_T *)xmalloc(sizeof(option_table_T) * table_size); - - for (idx = 0; idx < table_size; idx++) { - old_opts[idx] = table[idx]; - table[idx].present = false; - } - - /* - * Repeat for all comma separated parts. - */ - stringp = option_str; - while (*stringp) { - colonp = (char_u *)vim_strchr((char *)stringp, ':'); - if (colonp == NULL) { - ret = N_("E550: Missing colon"); - break; - } - commap = vim_strchr((char *)stringp, ','); - if (commap == NULL) { - commap = (char *)option_str + STRLEN(option_str); - } - - len = (int)(colonp - stringp); - - for (idx = 0; idx < table_size; ++idx) { - if (STRNICMP(stringp, table[idx].name, len) == 0) { - break; - } - } - - if (idx == table_size) { - ret = N_("E551: Illegal component"); - break; - } - - p = (char *)colonp + 1; - table[idx].present = true; - - if (table[idx].hasnum) { - if (!ascii_isdigit(*p)) { - ret = N_("E552: digit expected"); - break; - } - - table[idx].number = getdigits_int(&p, false, 0); - } - - table[idx].string = (char_u *)p; - table[idx].strlen = (int)(commap - p); - - stringp = (char_u *)commap; - if (*stringp == ',') { - stringp++; - } - } - - if (ret != NULL) { - // Restore old options in case of error - for (idx = 0; idx < table_size; idx++) { - table[idx] = old_opts[idx]; - } - } - - xfree(old_opts); - return ret; -} - -/* - * If using a dark background, the colors will probably be too bright to show - * up well on white paper, so reduce their brightness. - */ -static uint32_t darken_rgb(uint32_t rgb) -{ - return ((rgb >> 17) << 16) - + (((rgb & 0xff00) >> 9) << 8) - + ((rgb & 0xff) >> 1); -} - -static uint32_t prt_get_term_color(int colorindex) -{ - // TODO(vim): Should check for xterm with 88 or 256 colors. - if (t_colors > 8) { - return cterm_color_16[colorindex % 16]; - } - return cterm_color_8[colorindex % 8]; -} - -static uint32_t prt_get_color(int hl_id, int modec) -{ - int colorindex; - uint32_t fg_color; - - const char *color = highlight_color(hl_id, "fg#", 'g'); - if (color != NULL) { - RgbValue rgb = name_to_color(color, &colorindex); - if (rgb != -1) { - return (uint32_t)rgb; - } - } - - color = highlight_color(hl_id, "fg", modec); - if (color == NULL) { - colorindex = 0; - } else { - colorindex = atoi(color); - } - - if (colorindex >= 0 && colorindex < t_colors) { - fg_color = prt_get_term_color(colorindex); - } else { - fg_color = PRCOLOR_BLACK; - } - - return fg_color; -} - -static void prt_get_attr(int hl_id, prt_text_attr_T *pattr, int modec) -{ - pattr->bold = (highlight_has_attr(hl_id, HL_BOLD, modec) != NULL); - pattr->italic = (highlight_has_attr(hl_id, HL_ITALIC, modec) != NULL); - pattr->underline = (highlight_has_attr(hl_id, HL_UNDERLINE, modec) != NULL); - pattr->undercurl = (highlight_has_attr(hl_id, HL_UNDERCURL, modec) != NULL); - pattr->underdouble = (highlight_has_attr(hl_id, HL_UNDERDOUBLE, modec) != NULL); - pattr->underdotted = (highlight_has_attr(hl_id, HL_UNDERDOTTED, modec) != NULL); - pattr->underdashed = (highlight_has_attr(hl_id, HL_UNDERDASHED, modec) != NULL); - - uint32_t fg_color = prt_get_color(hl_id, modec); - - if (fg_color == PRCOLOR_WHITE) { - fg_color = PRCOLOR_BLACK; - } else if (*p_bg == 'd') { - fg_color = darken_rgb(fg_color); - } - - pattr->fg_color = fg_color; - pattr->bg_color = PRCOLOR_WHITE; -} - -static void prt_set_fg(uint32_t fg) -{ - if (fg != curr_fg) { - curr_fg = fg; - mch_print_set_fg(fg); - } -} - -static void prt_set_bg(uint32_t bg) -{ - if (bg != curr_bg) { - curr_bg = bg; - mch_print_set_bg(bg); - } -} - -static void prt_set_font(const TriState bold, const TriState italic, const TriState underline) -{ - if (curr_bold != bold - || curr_italic != italic - || curr_underline != underline) { - curr_underline = underline; - curr_italic = italic; - curr_bold = bold; - mch_print_set_font(bold, italic, underline); - } -} - -// Print the line number in the left margin. -static void prt_line_number(prt_settings_T *const psettings, const int page_line, - const linenr_T lnum) -{ - prt_set_fg(psettings->number.fg_color); - prt_set_bg(psettings->number.bg_color); - prt_set_font(psettings->number.bold, psettings->number.italic, - psettings->number.underline); - mch_print_start_line(true, page_line); - - // Leave two spaces between the number and the text; depends on - // PRINT_NUMBER_WIDTH. - char tbuf[20]; - snprintf(tbuf, sizeof(tbuf), "%6ld", (long)lnum); - for (int i = 0; i < 6; i++) { - (void)mch_print_text_out((char_u *)(&tbuf[i]), 1); - } - - if (psettings->do_syntax) { - // Set colors for next character. - current_syn_id = -1; - } else { - // Set colors and font back to normal. - prt_set_fg(PRCOLOR_BLACK); - prt_set_bg(PRCOLOR_WHITE); - prt_set_font(kFalse, kFalse, kFalse); - } -} - -/* - * Get the currently effective header height. - */ -int prt_header_height(void) -{ - if (printer_opts[OPT_PRINT_HEADERHEIGHT].present) { - return printer_opts[OPT_PRINT_HEADERHEIGHT].number; - } - return 2; -} - -/* - * Return TRUE if using a line number for printing. - */ -int prt_use_number(void) -{ - return printer_opts[OPT_PRINT_NUMBER].present - && TOLOWER_ASC(printer_opts[OPT_PRINT_NUMBER].string[0]) == 'y'; -} - -/* - * Return the unit used in a margin item in 'printoptions'. - * Returns PRT_UNIT_NONE if not recognized. - */ -int prt_get_unit(int idx) -{ - int u = PRT_UNIT_NONE; - int i; - static char *(units[4]) = PRT_UNIT_NAMES; - - if (printer_opts[idx].present) { - for (i = 0; i < 4; ++i) { - if (STRNICMP(printer_opts[idx].string, units[i], 2) == 0) { - u = i; - break; - } - } - } - return u; -} - -// Print the page header. -static void prt_header(prt_settings_T *const psettings, const int pagenum, const linenr_T lnum) -{ - int width = psettings->chars_per_line; - - // Also use the space for the line number. - if (prt_use_number()) { - width += PRINT_NUMBER_WIDTH; - } - - assert(width >= 0); - const size_t tbuf_size = (size_t)width + IOSIZE; - char_u *tbuf = xmalloc(tbuf_size); - - if (*p_header != NUL) { - linenr_T tmp_lnum, tmp_topline, tmp_botline; - int use_sandbox = FALSE; - - /* - * Need to (temporarily) set current line number and first/last line - * number on the 'window'. Since we don't know how long the page is, - * set the first and current line number to the top line, and guess - * that the page length is 64. - */ - tmp_lnum = curwin->w_cursor.lnum; - tmp_topline = curwin->w_topline; - tmp_botline = curwin->w_botline; - curwin->w_cursor.lnum = lnum; - curwin->w_topline = lnum; - curwin->w_botline = lnum + 63; - printer_page_num = pagenum; - - use_sandbox = was_set_insecurely(curwin, "printheader", 0); - build_stl_str_hl(curwin, (char *)tbuf, (size_t)width + IOSIZE, - (char *)p_header, use_sandbox, - ' ', width, NULL, NULL); - - // Reset line numbers - curwin->w_cursor.lnum = tmp_lnum; - curwin->w_topline = tmp_topline; - curwin->w_botline = tmp_botline; - } else { - snprintf((char *)tbuf, tbuf_size, _("Page %d"), pagenum); - } - - prt_set_fg(PRCOLOR_BLACK); - prt_set_bg(PRCOLOR_WHITE); - prt_set_font(kTrue, kFalse, kFalse); - - // Use a negative line number to indicate printing in the top margin. - int page_line = 0 - prt_header_height(); - mch_print_start_line(true, page_line); - for (char_u *p = tbuf; *p != NUL;) { - const int l = utfc_ptr2len((char *)p); - assert(l >= 0); - if (mch_print_text_out(p, (size_t)l)) { - page_line++; - if (page_line >= 0) { // out of room in header - break; - } - mch_print_start_line(true, page_line); - } - p += l; - } - - xfree(tbuf); - - if (psettings->do_syntax) { - // Set colors for next character. - current_syn_id = -1; - } else { - // Set colors and font back to normal. - prt_set_fg(PRCOLOR_BLACK); - prt_set_bg(PRCOLOR_WHITE); - prt_set_font(kFalse, kFalse, kFalse); - } -} - -/* - * Display a print status message. - */ -static void prt_message(char_u *s) -{ - // TODO(bfredl): delete this - grid_fill(&default_grid, Rows - 1, Rows, 0, Columns, ' ', ' ', 0); - grid_puts(&default_grid, s, Rows - 1, 0, HL_ATTR(HLF_R)); - ui_flush(); -} - -void ex_hardcopy(exarg_T *eap) -{ - linenr_T lnum; - int collated_copies, uncollated_copies; - prt_settings_T settings; - size_t bytes_to_print = 0; - int page_line; - int jobsplit; - - CLEAR_FIELD(settings); - settings.has_color = true; - - if (*eap->arg == '>') { - char *errormsg = NULL; - - // Expand things like "%.ps". - if (expand_filename(eap, eap->cmdlinep, &errormsg) == FAIL) { - if (errormsg != NULL) { - emsg(errormsg); - } - return; - } - settings.outfile = (char_u *)skipwhite(eap->arg + 1); - } else if (*eap->arg != NUL) { - settings.arguments = (char_u *)eap->arg; - } - - /* - * Initialise for printing. Ask the user for settings, unless forceit is - * set. - * The mch_print_init() code should set up margins if applicable. (It may - * not be a real printer - for example the engine might generate HTML or - * PS.) - */ - if (mch_print_init(&settings, - curbuf->b_fname == NULL ? (char_u *)buf_spname(curbuf) : curbuf->b_sfname == - NULL ? (char_u *)curbuf->b_fname : (char_u *)curbuf->b_sfname, - eap->forceit) == FAIL) { - return; - } - - settings.modec = 'c'; - - if (!syntax_present(curwin)) { - settings.do_syntax = FALSE; - } else if (printer_opts[OPT_PRINT_SYNTAX].present - && TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) != 'a') { - settings.do_syntax = - (TOLOWER_ASC(printer_opts[OPT_PRINT_SYNTAX].string[0]) == 'y'); - } else { - settings.do_syntax = settings.has_color; - } - - // Set up printing attributes for line numbers - settings.number.fg_color = PRCOLOR_BLACK; - settings.number.bg_color = PRCOLOR_WHITE; - settings.number.bold = kFalse; - settings.number.italic = kTrue; - settings.number.underline = kFalse; - - // Syntax highlighting of line numbers. - if (prt_use_number() && settings.do_syntax) { - int id = syn_name2id("LineNr"); - if (id > 0) { - id = syn_get_final_id(id); - } - - prt_get_attr(id, &settings.number, settings.modec); - } - - /* - * Estimate the total lines to be printed - */ - for (lnum = eap->line1; lnum <= eap->line2; lnum++) { - bytes_to_print += STRLEN(skipwhite((char *)ml_get(lnum))); - } - if (bytes_to_print == 0) { - msg(_("No text to be printed")); - goto print_fail_no_begin; - } - - // Set colors and font to normal. - curr_bg = 0xffffffff; - curr_fg = 0xffffffff; - curr_italic = kNone; - curr_bold = kNone; - curr_underline = kNone; - - prt_set_fg(PRCOLOR_BLACK); - prt_set_bg(PRCOLOR_WHITE); - prt_set_font(kFalse, kFalse, kFalse); - current_syn_id = -1; - - jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present - && TOLOWER_ASC(printer_opts[OPT_PRINT_JOBSPLIT].string[0]) == 'y'); - - if (!mch_print_begin(&settings)) { - goto print_fail_no_begin; - } - - /* - * Loop over collated copies: 1 2 3, 1 2 3, ... - */ - page_count = 0; - for (collated_copies = 0; - collated_copies < settings.n_collated_copies; - collated_copies++) { - prt_pos_T prtpos; // current print position - prt_pos_T page_prtpos; // print position at page start - int side; - - CLEAR_FIELD(page_prtpos); - page_prtpos.file_line = eap->line1; - prtpos = page_prtpos; - - if (jobsplit && collated_copies > 0) { - // Splitting jobs: Stop a previous job and start a new one. - mch_print_end(&settings); - if (!mch_print_begin(&settings)) { - goto print_fail_no_begin; - } - } - - /* - * Loop over all pages in the print job: 1 2 3 ... - */ - for (page_count = 0; prtpos.file_line <= eap->line2; ++page_count) { - /* - * Loop over uncollated copies: 1 1 1, 2 2 2, 3 3 3, ... - * For duplex: 12 12 12 34 34 34, ... - */ - for (uncollated_copies = 0; - uncollated_copies < settings.n_uncollated_copies; - uncollated_copies++) { - // Set the print position to the start of this page. - prtpos = page_prtpos; - - /* - * Do front and rear side of a page. - */ - for (side = 0; side <= settings.duplex; ++side) { - /* - * Print one page. - */ - - // Check for interrupt character every page. - os_breakcheck(); - if (got_int || settings.user_abort) { - goto print_fail; - } - - assert(prtpos.bytes_printed <= SIZE_MAX / 100); - sprintf((char *)IObuff, _("Printing page %d (%zu%%)"), - page_count + 1 + side, - prtpos.bytes_printed * 100 / bytes_to_print); - if (!mch_print_begin_page(IObuff)) { - goto print_fail; - } - - if (settings.n_collated_copies > 1) { - sprintf((char *)IObuff + STRLEN(IObuff), - _(" Copy %d of %d"), - collated_copies + 1, - settings.n_collated_copies); - } - prt_message(IObuff); - - /* - * Output header if required - */ - if (prt_header_height() > 0) { - prt_header(&settings, page_count + 1 + side, - prtpos.file_line); - } - - for (page_line = 0; page_line < settings.lines_per_page; - ++page_line) { - prtpos.column = hardcopy_line(&settings, - page_line, &prtpos); - if (prtpos.column == 0) { - // finished a file line - prtpos.bytes_printed += - STRLEN(skipwhite((char *)ml_get(prtpos.file_line))); - if (++prtpos.file_line > eap->line2) { - break; // reached the end - } - } else if (prtpos.ff) { - // Line had a formfeed in it - start new page but - // stay on the current line - break; - } - } - - if (!mch_print_end_page()) { - goto print_fail; - } - if (prtpos.file_line > eap->line2) { - break; // reached the end - } - } - - /* - * Extra blank page for duplexing with odd number of pages and - * more copies to come. - */ - if (prtpos.file_line > eap->line2 && settings.duplex - && side == 0 - && uncollated_copies + 1 < settings.n_uncollated_copies) { - if (!mch_print_blank_page()) { - goto print_fail; - } - } - } - if (settings.duplex && prtpos.file_line <= eap->line2) { - page_count++; - } - - // Remember the position where the next page starts. - page_prtpos = prtpos; - } - - vim_snprintf((char *)IObuff, IOSIZE, _("Printed: %s"), - settings.jobname); - prt_message(IObuff); - } - -print_fail: - if (got_int || settings.user_abort) { - sprintf((char *)IObuff, "%s", _("Printing aborted")); - prt_message(IObuff); - } - mch_print_end(&settings); - -print_fail_no_begin: - mch_print_cleanup(); -} - -/* - * Print one page line. - * Return the next column to print, or zero if the line is finished. - */ -static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T *ppos) -{ - colnr_T col; - char_u *line; - int need_break = FALSE; - int outputlen; - int tab_spaces; - int print_pos; - prt_text_attr_T attr; - int id; - - if (ppos->column == 0 || ppos->ff) { - print_pos = 0; - tab_spaces = 0; - if (!ppos->ff && prt_use_number()) { - prt_line_number(psettings, page_line, ppos->file_line); - } - ppos->ff = FALSE; - } else { - // left over from wrap halfway through a tab - print_pos = ppos->print_pos; - tab_spaces = ppos->lead_spaces; - } - - mch_print_start_line(false, page_line); - line = ml_get(ppos->file_line); - - /* - * Loop over the columns until the end of the file line or right margin. - */ - for (col = ppos->column; line[col] != NUL && !need_break; col += outputlen) { - if ((outputlen = utfc_ptr2len((char *)line + col)) < 1) { - outputlen = 1; - } - // syntax highlighting stuff. - if (psettings->do_syntax) { - id = syn_get_id(curwin, ppos->file_line, col, 1, NULL, false); - if (id > 0) { - id = syn_get_final_id(id); - } else { - id = 0; - } - // Get the line again, a multi-line regexp may invalidate it. - line = ml_get(ppos->file_line); - - if (id != current_syn_id) { - current_syn_id = id; - prt_get_attr(id, &attr, psettings->modec); - prt_set_font(attr.bold, attr.italic, attr.underline); - prt_set_fg(attr.fg_color); - prt_set_bg(attr.bg_color); - } - } - - /* - * Appropriately expand any tabs to spaces. - */ - if (line[col] == TAB || tab_spaces != 0) { - if (tab_spaces == 0) { - tab_spaces = tabstop_padding(print_pos, - curbuf->b_p_ts, - curbuf->b_p_vts_array); - } - - while (tab_spaces > 0) { - need_break = mch_print_text_out((char_u *)" ", 1); - print_pos++; - tab_spaces--; - if (need_break) { - break; - } - } - // Keep the TAB if we didn't finish it. - if (need_break && tab_spaces > 0) { - break; - } - } else if (line[col] == FF - && printer_opts[OPT_PRINT_FORMFEED].present - && TOLOWER_ASC(printer_opts[OPT_PRINT_FORMFEED].string[0]) - == 'y') { - ppos->ff = TRUE; - need_break = 1; - } else { - need_break = mch_print_text_out(line + col, (size_t)outputlen); - print_pos += utf_ptr2cells((char *)line + col); - } - } - - ppos->lead_spaces = tab_spaces; - ppos->print_pos = print_pos; - - /* - * Start next line of file if we clip lines, or have reached end of the - * line, unless we are doing a formfeed. - */ - if (!ppos->ff - && (line[col] == NUL - || (printer_opts[OPT_PRINT_WRAP].present - && TOLOWER_ASC(printer_opts[OPT_PRINT_WRAP].string[0]) - == 'n'))) { - return 0; - } - return col; -} - -/* - * PS printer stuff. - * - * Sources of information to help maintain the PS printing code: - * - * 1. PostScript Language Reference, 3rd Edition, - * Addison-Wesley, 1999, ISBN 0-201-37922-8 - * 2. PostScript Language Program Design, - * Addison-Wesley, 1988, ISBN 0-201-14396-8 - * 3. PostScript Tutorial and Cookbook, - * Addison Wesley, 1985, ISBN 0-201-10179-3 - * 4. PostScript Language Document Structuring Conventions Specification, - * version 3.0, - * Adobe Technote 5001, 25th September 1992 - * 5. PostScript Printer Description File Format Specification, Version 4.3, - * Adobe technote 5003, 9th February 1996 - * 6. Adobe Font Metrics File Format Specification, Version 4.1, - * Adobe Technote 5007, 7th October 1998 - * 7. Adobe CMap and CIDFont Files Specification, Version 1.0, - * Adobe Technote 5014, 8th October 1996 - * 8. Adobe CJKV Character Collections and CMaps for CID-Keyed Fonts, - * Adoboe Technote 5094, 8th September, 2001 - * 9. CJKV Information Processing, 2nd Edition, - * O'Reilly, 2002, ISBN 1-56592-224-7 - * - * Some of these documents can be found in PDF form on Adobe's web site - - * http://www.adobe.com - */ - -#define PRT_PS_DEFAULT_DPI (72) // Default user space resolution -#define PRT_PS_DEFAULT_FONTSIZE (10) - -#define PRT_MEDIASIZE_LEN ARRAY_SIZE(prt_mediasize) - -static struct prt_mediasize_S prt_mediasize[] = -{ - { "A4", 595.0, 842.0 }, - { "letter", 612.0, 792.0 }, - { "10x14", 720.0, 1008.0 }, - { "A3", 842.0, 1191.0 }, - { "A5", 420.0, 595.0 }, - { "B4", 729.0, 1032.0 }, - { "B5", 516.0, 729.0 }, - { "executive", 522.0, 756.0 }, - { "folio", 595.0, 935.0 }, - { "ledger", 1224.0, 792.0 }, // Yes, it is wider than taller! - { "legal", 612.0, 1008.0 }, - { "quarto", 610.0, 780.0 }, - { "statement", 396.0, 612.0 }, - { "tabloid", 792.0, 1224.0 } -}; - -#define PRT_PS_FONT_ROMAN (0) -#define PRT_PS_FONT_BOLD (1) -#define PRT_PS_FONT_OBLIQUE (2) -#define PRT_PS_FONT_BOLDOBLIQUE (3) - -// Standard font metrics for Courier family -static struct prt_ps_font_S prt_ps_courier_font = -{ - 600, - -100, 50, - -250, 805, - { "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique" } -}; - -// Generic font metrics for multi-byte fonts -static struct prt_ps_font_S prt_ps_mb_font = -{ - 1000, - -100, 50, - -250, 805, - { NULL, NULL, NULL, NULL } -}; - -// Pointer to current font set being used -static struct prt_ps_font_S *prt_ps_font; - -#define CS_JIS_C_1978 (0x01) -#define CS_JIS_X_1983 (0x02) -#define CS_JIS_X_1990 (0x04) -#define CS_NEC (0x08) -#define CS_MSWINDOWS (0x10) -#define CS_CP932 (0x20) -#define CS_KANJITALK6 (0x40) -#define CS_KANJITALK7 (0x80) - -// Japanese encodings and charsets -static struct prt_ps_encoding_S j_encodings[] = -{ - { "iso-2022-jp", NULL, (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990| - CS_NEC) }, - { "euc-jp", "EUC", (CS_JIS_C_1978|CS_JIS_X_1983|CS_JIS_X_1990) }, - { "sjis", "RKSJ", (CS_JIS_C_1978|CS_JIS_X_1983|CS_MSWINDOWS| - CS_KANJITALK6|CS_KANJITALK7) }, - { "cp932", "RKSJ", CS_JIS_X_1983 }, - { "ucs-2", "UCS2", CS_JIS_X_1990 }, - { "utf-8", "UTF8", CS_JIS_X_1990 } -}; -static struct prt_ps_charset_S j_charsets[] = -{ - { "JIS_C_1978", "78", CS_JIS_C_1978 }, - { "JIS_X_1983", NULL, CS_JIS_X_1983 }, - { "JIS_X_1990", "Hojo", CS_JIS_X_1990 }, - { "NEC", "Ext", CS_NEC }, - { "MSWINDOWS", "90ms", CS_MSWINDOWS }, - { "CP932", "90ms", CS_JIS_X_1983 }, - { "KANJITALK6", "83pv", CS_KANJITALK6 }, - { "KANJITALK7", "90pv", CS_KANJITALK7 } -}; - -#define CS_GB_2312_80 (0x01) -#define CS_GBT_12345_90 (0x02) -#define CS_GBK2K (0x04) -#define CS_SC_MAC (0x08) -#define CS_GBT_90_MAC (0x10) -#define CS_GBK (0x20) -#define CS_SC_ISO10646 (0x40) - -// Simplified Chinese encodings and charsets -static struct prt_ps_encoding_S sc_encodings[] = -{ - { "iso-2022", NULL, (CS_GB_2312_80|CS_GBT_12345_90) }, - { "gb18030", NULL, CS_GBK2K }, - { "euc-cn", "EUC", (CS_GB_2312_80|CS_GBT_12345_90|CS_SC_MAC| - CS_GBT_90_MAC) }, - { "gbk", "EUC", CS_GBK }, - { "ucs-2", "UCS2", CS_SC_ISO10646 }, - { "utf-8", "UTF8", CS_SC_ISO10646 } -}; -static struct prt_ps_charset_S sc_charsets[] = -{ - { "GB_2312-80", "GB", CS_GB_2312_80 }, - { "GBT_12345-90", "GBT", CS_GBT_12345_90 }, - { "MAC", "GBpc", CS_SC_MAC }, - { "GBT-90_MAC", "GBTpc", CS_GBT_90_MAC }, - { "GBK", "GBK", CS_GBK }, - { "GB18030", "GBK2K", CS_GBK2K }, - { "ISO10646", "UniGB", CS_SC_ISO10646 } -}; - -#define CS_CNS_PLANE_1 (0x01) -#define CS_CNS_PLANE_2 (0x02) -#define CS_CNS_PLANE_1_2 (0x04) -#define CS_B5 (0x08) -#define CS_ETEN (0x10) -#define CS_HK_GCCS (0x20) -#define CS_HK_SCS (0x40) -#define CS_HK_SCS_ETEN (0x80) -#define CS_MTHKL (0x100) -#define CS_MTHKS (0x200) -#define CS_DLHKL (0x400) -#define CS_DLHKS (0x800) -#define CS_TC_ISO10646 (0x1000) - -// Traditional Chinese encodings and charsets -static struct prt_ps_encoding_S tc_encodings[] = -{ - { "iso-2022", NULL, (CS_CNS_PLANE_1|CS_CNS_PLANE_2) }, - { "euc-tw", "EUC", CS_CNS_PLANE_1_2 }, - { "big5", "B5", (CS_B5|CS_ETEN|CS_HK_GCCS|CS_HK_SCS| - CS_HK_SCS_ETEN|CS_MTHKL|CS_MTHKS|CS_DLHKL| - CS_DLHKS) }, - { "cp950", "B5", CS_B5 }, - { "ucs-2", "UCS2", CS_TC_ISO10646 }, - { "utf-8", "UTF8", CS_TC_ISO10646 }, - { "utf-16", "UTF16", CS_TC_ISO10646 }, - { "utf-32", "UTF32", CS_TC_ISO10646 } -}; -static struct prt_ps_charset_S tc_charsets[] = -{ - { "CNS_1992_1", "CNS1", CS_CNS_PLANE_1 }, - |