~DevicePS() { delete actStream; # ifdef USE_PSLIB PS_shutdown(); // PSlib changes locale - bug no. 3428043 # ifdef HAVE_LOCALE_H setlocale(LC_ALL, "C"); # endif # endif }
void YZPrinter::doPrint( ) { const double fontsize = 10.0; PSDoc *doc = PS_new(); if (!doc) return ; QByteArray p = m_path.toLatin1(); PS_open_file(doc, p.data()); PS_set_info(doc, "Creator", "Yzis"); PS_set_info(doc, "Author", ""); PS_set_info(doc, "Title", p.data()); // Set so it'll fit on both A4 and letter paper; // some of us live in the US, with archaic paper sizes. ;-) PS_set_info(doc, "BoundingBox", "0 0 596 792"); int font; font = PS_findfont(doc, "Fixed", "", 0); dbg() << "findfont returned " << font << endl; if ( !font ) return ; //no font => abort QPrinter lpr(QPrinter::PrinterResolution); QPainter p( &lpr ); QFont f( "fixed" ); f.setFixedPitch( true ); f.setStyleHint( QFont::TypeWriter ); p.setFont( f ); unsigned int height = lpr.height(); unsigned int width = lpr.width(); unsigned int linespace = p.fontMetrics().lineSpacing(); unsigned int maxwidth = p.fontMetrics().maxWidth(); p.end(); PS_set_value(doc, "leading", linespace); unsigned int clipw = width / maxwidth - 1; unsigned int cliph = height / linespace - 1; unsigned int oldLinesVis = mView->getLinesVisible( ); unsigned int oldColumnsVis = mView->getColumnsVisible( ); //should be current's view setting no ? XXX bool number = mView->getLocalBooleanOption( "number" ); unsigned int marginLeft = 0; double red, green, blue; if ( number ) { marginLeft = ( 2 + QString::number( mView->buffer()->lineCount() ).length() ); } YOptionValue* ov_wrap = mView->getLocalOption( "wrap" ); bool oldWrap = ov_wrap->boolean(); ov_wrap->setBoolean( true ); mView->setVisibleArea( clipw - marginLeft, cliph, false ); unsigned int totalHeight = mView->drawTotalHeight(); mView->setVisibleArea( clipw - marginLeft, totalHeight, false ); mView->initDraw( 0, 0, 0, 0 ); unsigned int lastLineNumber = 0; unsigned int pageNumber = 0; QRect titleRect( 0, 0, width, linespace + linespace / 2 ); unsigned int topY = titleRect.height() + linespace; unsigned int curY = topY; unsigned int curX; cliph = ( height - topY ) / linespace; int nbPages = totalHeight / cliph + ( totalHeight % cliph ? 1 : 0 ); PS_begin_page(doc, 596, 792); PS_setfont(doc, font, fontsize); PS_set_parameter(doc, "hyphenation", "false"); PS_set_parameter(doc, "linebreak", "true"); PS_set_parameter(doc, "parbreak", "false"); PS_set_value(doc, "parindent", 0.0); PS_set_value(doc, "numindentlines", 0.0); while ( mView->drawNextLine( ) ) { if ( curY == topY ) { if ( pageNumber ) { PS_end_page(doc); PS_begin_page(doc, 596, 792); PS_setfont(doc, font, fontsize); PS_set_value(doc, "leading", linespace); } ++pageNumber; convertColor(Qt::black, red, green, blue); PS_setcolor(doc, "fillstroke", "rgb", red, green, blue, 0.0); QByteArray n = ( ' ' + mView->buffer()->fileName() ).toLatin1(); PS_show_boxed(doc, n.data(), titleRect.x(), titleRect.y(), titleRect.width(), titleRect.height(), "left", ""); QByteArray nb = ( QString::number( pageNumber ) + '/' + QString::number( nbPages ) + ' ' ).toLatin1(); PS_show_boxed(doc, nb.data(), titleRect.x(), titleRect.y(), titleRect.width(), titleRect.height(), "right", ""); } if ( number ) { unsigned int lineNumber = mView->drawLineNumber(); if ( lineNumber != lastLineNumber ) { //p.setPen( Qt::gray ); convertColor(Qt::gray, red, green, blue); PS_setcolor(doc, "fillstroke", "rgb", red, green, blue, 0.0); PS_moveto(doc, 0, curY); QByteArray m = QString::number( lineNumber ).rightJustified( marginLeft - 1, ' ' ).toLatin1(); PS_show(doc, m.data()); lastLineNumber = lineNumber; } } curX = marginLeft * maxwidth; while ( mView->drawNextCol( ) ) { QColor c = mView->drawColor( ); if ( c.isValid() && c != Qt::white ) convertColor(mView->drawColor(), red, green, blue); else convertColor(Qt::black, red, green, blue); PS_setcolor(doc, "fillstroke", "rgb", red, green, blue, 0.0); char buf[2] = { 0, 0 }; buf[0] = mView->drawChar().toLatin1(); PS_show_xy(doc, buf, curX, curY); curX += mView->drawLength( ) * maxwidth; } curY += linespace * mView->drawHeight(); if ( curY >= cliph * linespace + topY ) { // draw Rect convertColor(Qt::black, red, green, blue); PS_setcolor(doc, "fillstroke", "rgb", red, green, blue, 0.0); PS_rect(doc, 0, 0, width, curY); if ( number ) { PS_moveto(doc, marginLeft*maxwidth - maxwidth / 2, titleRect.height()); PS_lineto(doc, marginLeft*maxwidth - maxwidth / 2, curY); } PS_moveto(doc, titleRect.x(), titleRect.height()); PS_lineto(doc, titleRect.width(), titleRect.height()); curY = topY; } } if ( curY != topY ) { // draw Rect convertColor(Qt::black, red, green, blue); PS_setcolor(doc, "fillstroke", "rgb", red, green, blue, 0.0); PS_rect(doc, 0, 0, width, curY); if ( number ) { PS_moveto(doc, marginLeft*maxwidth - maxwidth / 2, titleRect.height()); PS_lineto(doc, marginLeft*maxwidth - maxwidth / 2, curY); } PS_moveto(doc, titleRect.x(), titleRect.height()); PS_lineto(doc, titleRect.width(), titleRect.height()); } PS_end_page(doc); PS_deletefont(doc, font); PS_close(doc); PS_delete(doc); PS_shutdown(); ov_wrap->setBoolean( oldWrap ); mView->setVisibleArea( oldColumnsVis, oldLinesVis, false ); }
int main(int argc, char* argv[]) { struct cfklp* cfklp; char *infile_name, *outfile_name; char *font, *justification; float leading, font_size, parindent; float page_width, page_height, margin; int numindentlines; bool linebreak, parbreak; bool verbose; int ch; /* */ if (argc == 1) { cfklp_usage(stdout); return EXIT_SUCCESS; } linebreak = DEFAULT_LINEBREAK; parbreak = DEFAULT_PARBREAK; font_size = DEFAULT_FONT_SIZE; leading = DEFAULT_LEADING; font = DEFAULT_FONT; numindentlines = DEFAULT_NUMINDENTLINES; parindent = DEFAULT_PARINDENT; justification = DEFAULT_JUSTIFICATION; page_width = DEFAULT_PAGE_WIDTH; page_height = DEFAULT_PAGE_HEIGHT; margin = DEFAULT_MARGIN; verbose = DEFAULT_VERBOSITY; while ((ch = getopt(argc, argv, "H:LPW:f:j:l:m:n:p:s:v")) != -1) { switch (ch) { case 'H': page_height = in(atof(optarg)); break; case 'L': linebreak = !linebreak; break; case 'P': parbreak = !parbreak; break; case 'W': page_width = in(atof(optarg)); break; case 'f': font = optarg; break; case 'j': justification = optarg; break; case 'l': leading = atof(optarg); break; case 'm': margin = in(atof(optarg)); break; case 'n': numindentlines = atoi(optarg); break; case 'p': parindent = atof(optarg); break; case 's': font_size = atof(optarg); break; case 'v': verbose = !verbose; break; case '?': default: cfklp_usage(stderr); return EXIT_FAILURE; } } argc -= optind; argv += optind; /* */ if (argc != 2) { fprintf(stderr, "%s: need an infile and an outfile\n", getprogname()); return EXIT_FAILURE; } infile_name = argv[0]; outfile_name = argv[1]; PS_boot(); cfklp = cfklp_new(infile_name, outfile_name); cfklp_set_font(cfklp, font); cfklp_set_font_size(cfklp, font_size); cfklp_set_justification(cfklp, justification); cfklp_set_page_size(cfklp, page_width, page_height, margin); cfklp_set_verbosity(cfklp, verbose); PS_set_value(cfklp->doc, "leading", leading); PS_set_value(cfklp->doc, "numindentlines", numindentlines); PS_set_value(cfklp->doc, "parindent", parindent); PS_set_parameter(cfklp->doc, "linebreak", bool_to_str(linebreak)); PS_set_parameter(cfklp->doc, "parbreak", bool_to_str(parbreak)); cfklp_read_infile(cfklp); if (cfklp->verbose) PS_list_values(cfklp->doc); cfklp_write_outfile(cfklp); cfklp_free(cfklp); PS_shutdown(); return EXIT_SUCCESS; }