pdf.cpp

来自「ncbi源码」· C++ 代码 · 共 405 行

CPP
405
字号
/* * =========================================================================== * PRODUCTION $Log: pdf.cpp,v $ * PRODUCTION Revision 1000.2  2004/06/01 21:03:41  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.17 * PRODUCTION * =========================================================================== *//*  $Id: pdf.cpp,v 1000.2 2004/06/01 21:03:41 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software / database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software / database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors:  Peter Meric * * File Description: *   CPdf - Adobe PDF output * */#include <ncbi_pch.hpp>#include <gui/print/pdf.hpp>#include <gui/print/vector_object.hpp>#include "pdf_object.hpp"#include "page_handler.hpp"#include "pdf_object_writer.hpp"BEGIN_NCBI_SCOPECPdf::CPdf()    : m_ObjectWriter(new CPdfObjectWriter()),      m_PageDictionary(new CPdfDictionary()){    m_PageHandler.reset(new CPageHandler(*m_PageBuffers,                                         *m_ObjectWriter,                                         m_ObjIdGenerator                                        )                       );}CPdf::~CPdf(){}void CPdf::SetOptions(const CPrintOptions& options){    CPostscript::SetOptions(options);    m_PageHandler->SetOptions(m_Options);}void CPdf::SetOutputStream(CNcbiOstream* ostream){    CPostscript::SetOutputStream(ostream);    m_ObjectWriter->SetOutputStream(ostream);}void CPdf::BeginDocument(){    *m_Strm << "%PDF-1.3" << pdfeol;    // output binary characters in a comment to alert programs this is a binary file    *m_Strm << '%';    const unsigned int N = 10;    const char binchar[N] =    {        char(0x80), char(0x81), char(0x82), char(0x83), char(0x84),        char(0x85), char(0x86), char(0x87), char(0x88), char(0x89)    };    m_Strm->write(binchar, N);    *m_Strm << pdfeol;    // create objects    TPdfObjectRef info(new CPdfObject(m_ObjIdGenerator.NextId()));    m_Catalog.Reset(new CPdfObject(m_ObjIdGenerator.NextId()));    TPdfObjectRef outlines(new CPdfObject(m_ObjIdGenerator.NextId()));    // populate info    CPdfObject& _info = *info;    _info["Author"] = new CPdfString("National Center for Biotechnology Information");    _info["Creator"] = new CPdfString("NCBI Genome Workbench");    _info["Producer"] = new CPdfString("NCBI PDF Generator(meric@ncbi.nlm.nih.gov)");    _info["Title"] = new CPdfString(m_Options.GetTitle());    _info["CreationDate"] = new CPdfString("D:20030603123400 - 05'00'");    m_ObjectWriter->WriteObject(info);    // populate catalog    CPdfObject& catalog = *m_Catalog;    catalog["Type"] = new CPdfName("Catalog");    catalog["Outlines"] = new CPdfIndirectObj(outlines);    catalog["Pages"] = new CPdfIndirectObj(m_PageHandler->GetObject());    catalog["PageLayout"] = new CPdfName(m_Options.GetNumPages() > 1 ? "OneColumn" : "SinglePage");    catalog["PageMode"] = new CPdfName("UseNone");    catalog["ViewerPreferences"] = new CPdfObj("<</DisplayDocTitle true>>");    if (m_Options.GetPrintOutput()) {        // Add print action to Catalog dictionary        catalog["OpenAction"] = new CPdfObj("<</S/Named /N/Print>>");    }    catalog["PageLabels"] = new CPdfObj("<</Nums[0<</S/D/P(Panel )>>]>>");    m_ObjectWriter->WriteObject(m_Catalog);    // populate outlines    CPdfObject& ol = *outlines;    ol["Type"] = new CPdfName("Outlines");    ol["Count"] = new CPdfNumber(int(0));    m_ObjectWriter->WriteObject(outlines);    // populate trailer    m_Trailer.Reset(new CPdfTrailer());    CPdfTrailer& trailer = *m_Trailer;    trailer["Info"] = new CPdfIndirectObj(info);    trailer["Root"] = new CPdfIndirectObj(m_Catalog);    TPdfObjectRef f1(new CPdfObject(m_ObjIdGenerator.NextId()));    CPdfObject& _f1 = *f1;    _f1["Type"] = new CPdfName("Font");    _f1["Subtype"] = new CPdfName("Type1");    _f1["Name"] = new CPdfName("F1");    _f1["BaseFont"] = new CPdfName("Helvetica");    TPdfObjectRef f2(new CPdfObject(m_ObjIdGenerator.NextId()));    CPdfObject& _f2 = *f2;    _f2["Type"] = new CPdfName("Font");    _f2["Subtype"] = new CPdfName("Type1");    _f2["Name"] = new CPdfName("F2");    _f2["BaseFont"] = new CPdfName("Courier");    m_Fonts.Reset(new CPdfObject(m_ObjIdGenerator.NextId()));    CPdfObject& fonts = *m_Fonts;    fonts["F1"] = new CPdfIndirectObj(f1);    fonts["F2"] = new CPdfIndirectObj(f2);    m_PrintInEndDoc.push_back(f1);    m_PrintInEndDoc.push_back(f2);    m_PrintInEndDoc.push_back(m_Fonts);    CRef<CPdfDictionary> resources(new CPdfDictionary());    CPdfDictionary& res = *resources;    res["Font"] = new CPdfIndirectObj(m_Fonts);    (*m_PageDictionary)["Resources"] = resources;}void CPdf::BeginPage(){    //enum CPdfObject::EPdfObjectFormat type = CPdfObject::eUncompressed;    m_Content.Reset(new CPdfObject(m_ObjIdGenerator.NextId()));}void CPdf::EndPage(){    if ( !m_Content ) {        return;    }    m_PageHandler->SetContent(m_Content);    //m_PageCount += m_PageHandler->WritePages(m_PageCount + 1);    m_PageCount += m_PageHandler->WritePages();}void CPdf::PrintObject(const CObject* obj, CPrintState& state){    //CRgbaColor& curr_nonstipple = state.m_NonStipple;    const CPVecText* txt = dynamic_cast < const CPVecText*>(obj);    if (txt) {        x_PrintText(txt, state);        return;    }    const CPVecPoint* point = dynamic_cast < const CPVecPoint*>(obj);    if (point) {        /*           const CRgbaColor& color = point->GetColor();           if ( !(color == curr_nonstipple) ) {           const float* c = color.GetColorArray();           color.PrintTo(*m_Strm, false);         *m_Strm << " rg" << endl;         curr_nonstipple = color;         }         *m_Strm << point << " P" << pdfeol;         */        //cerr << "point: " << *point << pdfeol;        return;    }    const CPVecLine* line = dynamic_cast < const CPVecLine*>(obj);    if (line) {        x_PrintLine(line, state);        return;    }    const CPVecPolygon* poly = dynamic_cast < const CPVecPolygon*>(obj);    if ( !poly ) {        return;    }    x_PrintPolygon(poly, state);}void CPdf::x_PrintText(const CPVecText* txt, CPrintState& state){    CRgbaColor& curr_nonstipple = state.m_NonStipple;    CNcbiOstream& out = m_Content->GetWriteBuffer();    // set the text color    const CRgbaColor& color = txt->GetColor();    if ( !(color == curr_nonstipple) ) {        color.PrintTo(out, false);        out << " rg" << pdfeol;        curr_nonstipple = color;    }    // output the text, font and font size    out << "BT" << pdfeol;    out << "/F1 8 Tf" << pdfeol;    const float* p = txt->GetPosition();    out << p[0] << ' ' << p[1] << " Td" << pdfeol;    out << '(' << txt->GetText() << ") Tj" << pdfeol;    out << "ET" << pdfeol;}void CPdf::x_PrintLine(const CPVecLine* line, CPrintState& state){    CRgbaColor& curr_stipple = state.m_Stipple;    const pair<CPVecPoint, CPVecPoint> p(line->GetPoints());    CNcbiOstream& out = m_Content->GetWriteBuffer();    const CRgbaColor& color = p.first.GetColor();    if ( !(color == curr_stipple) ) {        color.PrintTo(out, false);        out << " RG" << pdfeol;        curr_stipple = color;    }    // only print X, Y coordinates    p.first.PrintTo(out, CPVecPoint::eCoordXY);    out << " m ";    p.second.PrintTo(out, CPVecPoint::eCoordXY);    out << " l s" << pdfeol;}void CPdf::x_PrintPolygon(const CPVecPolygon* poly, CPrintState& state){    CRgbaColor& curr_nonstipple = state.m_NonStipple;    if (poly->IsFlatColored()) {        CNcbiOstream& out = m_Content->GetWriteBuffer();        const CRgbaColor& color = (*poly->begin())->GetColor();        if ( !(color == curr_nonstipple) ) {            color.PrintTo(out, false);            out << " rg" << pdfeol;            curr_nonstipple = color;        }        bool first = true;        ITERATE(CPVecPolygon, it, *poly) {            const CPVecPoint* p = *it;            if ( !first ) {                out << ' ';            }            p->PrintTo(out, CPVecPoint::eCoordXY); // only print X, Y coordinates            if (first) {                out << " m";                first = false;            } else {                out << " l";            }        }        out << " h f" << pdfeol;    }}void CPdf::EndDocument(){    m_PageHandler->WritePageTree(m_PageDictionary);    ITERATE(vector<TPdfObjectRef>, it, m_PrintInEndDoc) {        m_ObjectWriter->WriteObject(*it);    }    // the number of objects is defined as one more than the highest    // object number    const unsigned int num_objects = m_ObjIdGenerator.NextId();    // write the cross reference    const CT_POS_TYPE xref_start = m_ObjectWriter->WriteXRef(num_objects);    // write the trailer    (*m_Trailer)["Size"] = new CPdfNumber(num_objects);    *m_Strm << *m_Trailer;    // output the location of the last xref section    *m_Strm << "startxref" << endl << (xref_start - CT_POS_TYPE(0)) << endl;    *m_Strm << "%%EOF" << pdfeol;}END_NCBI_SCOPE/* * =========================================================================== * $Log: pdf.cpp,v $ * Revision 1000.2  2004/06/01 21:03:41  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.17 * * Revision 1.17  2004/05/21 22:27:50  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.16  2004/02/20 20:03:27  ucko * Fix to compile with stricter implementations of CT_POS_TYPE * * Revision 1.15  2003/08/15 17:02:16  meric * Updates include paths for print-related files from gui/utils to gui/print * * Revision 1.14  2003/07/16 21:36:25  meric * Cast contants to char to avoid warnings regarding truncation of constants * * Revision 1.13  2003/07/16 20:03:13  meric * Modify writing of binary char comment in at top of PDF file * * Revision 1.12  2003/06/25 18:02:51  meric * Source rearrangement: move "private" headers into the src/ tree * * Revision 1.11  2003/06/24 22:34:27  meric * modified char array to int array, casting to char upon use * * Revision 1.10  2003/06/24 21:46:52  meric * Moved includes from pdf.hpp where practical * * Revision 1.9  2003/06/24 18:34:11  meric * Write binary chars in comment at top of PDF file * Prefix page numbers with "Panel " * * Revision 1.8  2003/06/18 17:25:38  meric * Final phase of print reorg: remove dependence on gui/opengl and OpenGL * * Revision 1.7  2003/06/18 16:40:33  meric * First phase of print reorg: remove dependence on gui/opengl and OpenGL * except for class COpenGLPrintBuffer * * Revision 1.6  2003/06/17 20:07:39  meric * Remove unused variables * * Revision 1.5  2003/06/17 19:33:01  meric * Insert Print action in PDF document when print option set * * Revision 1.4  2003/06/16 21:46:07  meric * Changed catalog options * * Revision 1.3  2003/06/16 21:15:25  meric * Fixed %%PDF comment * * Revision 1.2  2003/06/16 12:44:52  dicuccio * Clean-up after initial commit * * Revision 1.1  2003 / 06 / 13 18:13:56  meric * Initial version * * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?