pdf_object.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:7k
- /*
- * ===========================================================================
- * PRODUCTION $Log: pdf_object.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 21:03:45 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: pdf_object.cpp,v 1000.1 2004/06/01 21:03:45 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:
- * CPdfObject - Stream for output of Adobe PDF objects
- */
- #include <ncbi_pch.hpp>
- #include "pdf_object.hpp"
- #include "pdf_object_writer.hpp"
- #include <util/compress/stream.hpp>
- #include <util/compress/zlib.hpp>
- #include <util/ascii85.hpp>
- BEGIN_NCBI_SCOPE
- CPdfObject::CPdfObject(unsigned int obj_num,
- unsigned int generation,
- bool allowCompression
- )
- : m_ObjNum(obj_num),
- m_Generation(generation),
- m_Separator("n"),
- m_AllowCompression(allowCompression)
- {
- }
- CPdfObject::~CPdfObject()
- {
- m_Buffer.freeze(false);
- }
- enum EPdfObjectFormat
- {
- eUncompressed,
- eCompressed
- };
- unsigned int CPdfObject::GetObjNum(void) const
- {
- return m_ObjNum;
- }
- unsigned int CPdfObject::GetGeneration(void) const
- {
- return m_Generation;
- }
- void CPdfObject::PrintTo(CNcbiOstream& stream) const
- {
- // start the page content object
- stream << m_ObjNum << ' ' << m_Generation << " obj" << endl;
- const_cast<CPdfObject*>(this)->x_PrintTo(stream);
- stream << "endobj" << endl;
- }
- void CPdfObject::x_PrintTo(CNcbiOstream& stream)
- {
- string::size_type os_len = m_Buffer.pcount();
- const bool compress = m_AllowCompression && os_len > 4096;
- AutoPtr< char, ArrayDeleter<char> > strmbuf;
- if (os_len > 0) {
- // add the stream contents
- if (compress) {
- const size_t cbuflen = size_t(os_len * 1.03);
- AutoPtr< char, ArrayDeleter<char> > cbuf(new char[cbuflen]);
- const string str = m_Buffer.str();
- // compress the data
- CZipCompression c;
- unsigned int len;
- c.CompressBuffer(str.data(), os_len, cbuf.get(), cbuflen, &len);
- // encode the data using ASCII-85
- // ASCII-85 encoding produces five ASCII characters for
- // every four bytes of binary data
- const size_t buflen = size_t(len * 1.3);
- strmbuf.reset(new char[buflen]);
- os_len = CAscii85::s_Encode(cbuf.get(), len, strmbuf.get(), buflen);
- // add stream-related attributes to dictionary
- m_Dictionary["Length"] = new CPdfNumber(int(os_len));
- m_Dictionary["Filter"] = new CPdfObj("[/ASCII85Decode/FlateDecode]");
- }
- else {
- // add stream-related attributes to dictionary
- // don't count the last newline in the stream as part of the length
- m_Dictionary["Length"] = new CPdfNumber(int(os_len - 1));
- }
- }
- // write dictionary
- stream << m_Dictionary << endl;
- // write the stream
- if (os_len > 0) {
- stream << "stream" << endl;
- // add the stream contents
- if (compress && strmbuf.get()) {
- const unsigned int line_len = 72;
- const char* ptr = strmbuf.get();
- size_t l = os_len;
- for ( ; l > line_len; l -= line_len, ptr += line_len) {
- stream.write(ptr, line_len) << pdfeol;
- }
- stream.write(ptr, l) << pdfeol;
- }
- else {
- // write uncompressed stream
- stream << string(m_Buffer.str(), os_len);
- }
- stream << "endstream" << endl;
- }
- }
- CPdfDictionary& CPdfObject::GetDictionary(void)
- {
- return m_Dictionary;
- }
- CPdfObject::TPdfObjRef& CPdfObject::operator[](const string& key)
- {
- return m_Dictionary[key];
- }
- CPdfObject& CPdfObject::operator<<(CPdfObject& (*pf)(CPdfObject&))
- {
- return (*pf)(*this);
- }
- CNcbiOstream& CPdfObject::GetWriteBuffer(void)
- {
- return m_Buffer;
- }
- string CPdfObject::GetSeparator(void) const
- {
- return m_Separator;
- }
- CPdfTrailer::CPdfTrailer() : CPdfObject(0)
- {
- }
- CPdfTrailer::~CPdfTrailer()
- {
- }
- void CPdfTrailer::PrintTo(CNcbiOstream& stream) const
- {
- stream << "trailer" << endl;
- const_cast<CPdfTrailer*>(this)->x_PrintTo(stream);
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: pdf_object.cpp,v $
- * Revision 1000.1 2004/06/01 21:03:45 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
- *
- * Revision 1.10 2004/05/21 22:27:50 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.9 2003/07/16 20:04:17 meric
- * Added ASCII-85 encoding of compressed stream data
- *
- * Revision 1.8 2003/07/16 15:42:30 meric
- * Add flag to specify if object may be compressed
- *
- * Revision 1.7 2003/07/15 14:49:51 meric
- * Compress streams longer than 4KB
- *
- * Revision 1.6 2003/06/25 18:02:51 meric
- * Source rearrangement: move "private" headers into the src/ tree
- *
- * Revision 1.5 2003/06/24 15:54:43 meric
- * Fixed: Cast Length value to int before adding to object dictionary
- *
- * Revision 1.4 2003/06/18 17:25:39 meric
- * Final phase of print reorg: remove dependence on gui/opengl and OpenGL
- *
- * Revision 1.3 2003/06/16 19:19:36 meric
- * Fixed: CPdfTrailer c'tor passes 0 instead of -1 to avoid unsigned warning
- *
- * 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
- *
- *
- * ===========================================================================
- */