Commit 9ebabd19538512b6515696b164a17b90060a75c3

Authored by m-holger
Committed by Jay Berkenbilt
1 parent 0a3c5331

Add new methods QPDF::newStream

include/qpdf/QPDF.hh
@@ -382,6 +382,36 @@ class QPDF @@ -382,6 +382,36 @@ class QPDF
382 QPDF_DLL 382 QPDF_DLL
383 std::map<QPDFObjGen, QPDFXRefEntry> getXRefTable(); 383 std::map<QPDFObjGen, QPDFXRefEntry> getXRefTable();
384 384
  385 + // Public factory methods
  386 +
  387 + // Create a new stream. A subsequent call must be made to
  388 + // replaceStreamData() to provide data for the stream. The stream's
  389 + // dictionary may be retrieved by calling getDict(), and the resulting
  390 + // dictionary may be modified. Alternatively, you can create a new
  391 + // dictionary and call replaceDict to install it.
  392 + QPDF_DLL
  393 + QPDFObjectHandle newStream();
  394 +
  395 + // Create a new stream. Use the given buffer as the stream data. The
  396 + // stream dictionary's /Length key will automatically be set to the size of
  397 + // the data buffer. If additional keys are required, the stream's
  398 + // dictionary may be retrieved by calling getDict(), and the resulting
  399 + // dictionary may be modified. This method is just a convenient wrapper
  400 + // around the newStream() and replaceStreamData(). It is a convenience
  401 + // methods for streams that require no parameters beyond the stream length.
  402 + // Note that you don't have to deal with compression yourself if you use
  403 + // QPDFWriter. By default, QPDFWriter will automatically compress
  404 + // uncompressed stream data. Example programs are provided that
  405 + // illustrate this.
  406 + QPDF_DLL
  407 + QPDFObjectHandle newStream(std::shared_ptr<Buffer> data);
  408 +
  409 + // Create new stream with data from string. This method will
  410 + // create a copy of the data rather than using the user-provided
  411 + // buffer as in the std::shared_ptr<Buffer> version of newStream.
  412 + QPDF_DLL
  413 + QPDFObjectHandle newStream(std::string const& data);
  414 +
385 // Install this object handle as an indirect object and return an 415 // Install this object handle as an indirect object and return an
386 // indirect reference to it. 416 // indirect reference to it.
387 QPDF_DLL 417 QPDF_DLL
libqpdf/QPDF.cc
@@ -2002,6 +2002,31 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh) @@ -2002,6 +2002,31 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh)
2002 } 2002 }
2003 2003
2004 QPDFObjectHandle 2004 QPDFObjectHandle
  2005 +QPDF::newStream()
  2006 +{
  2007 + return makeIndirectObject(QPDF_Stream::create(
  2008 + this, nextObjGen(), QPDFObjectHandle::newDictionary(), 0, 0));
  2009 +}
  2010 +
  2011 +QPDFObjectHandle
  2012 +QPDF::newStream(std::shared_ptr<Buffer> data)
  2013 +{
  2014 + auto result = newStream();
  2015 + result.replaceStreamData(
  2016 + data, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
  2017 + return result;
  2018 +}
  2019 +
  2020 +QPDFObjectHandle
  2021 +QPDF::newStream(std::string const& data)
  2022 +{
  2023 + auto result = newStream();
  2024 + result.replaceStreamData(
  2025 + data, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
  2026 + return result;
  2027 +}
  2028 +
  2029 +QPDFObjectHandle
2005 QPDF::reserveObjectIfNotExists(QPDFObjGen const& og) 2030 QPDF::reserveObjectIfNotExists(QPDFObjGen const& og)
2006 { 2031 {
2007 if (!isCached(og) && m->xref_table.count(og) == 0) { 2032 if (!isCached(og) && m->xref_table.count(og) == 0) {
libqpdf/QPDFObjectHandle.cc
@@ -2165,30 +2165,29 @@ QPDFObjectHandle::newStream(QPDF* qpdf) @@ -2165,30 +2165,29 @@ QPDFObjectHandle::newStream(QPDF* qpdf)
2165 "attempt to create stream in null qpdf object"); 2165 "attempt to create stream in null qpdf object");
2166 } 2166 }
2167 QTC::TC("qpdf", "QPDFObjectHandle newStream"); 2167 QTC::TC("qpdf", "QPDFObjectHandle newStream");
2168 - QPDFObjectHandle stream_dict = newDictionary();  
2169 - QPDFObjectHandle result = qpdf->makeIndirectObject(QPDFObjectHandle(  
2170 - QPDF_Stream::create(qpdf, QPDFObjGen(), stream_dict, 0, 0)));  
2171 - auto stream = result.asStream();  
2172 - stream->setObjGen(result.getObjGen());  
2173 - return result; 2168 + return qpdf->newStream();
2174 } 2169 }
2175 2170
2176 QPDFObjectHandle 2171 QPDFObjectHandle
2177 QPDFObjectHandle::newStream(QPDF* qpdf, std::shared_ptr<Buffer> data) 2172 QPDFObjectHandle::newStream(QPDF* qpdf, std::shared_ptr<Buffer> data)
2178 { 2173 {
  2174 + if (qpdf == nullptr) {
  2175 + throw std::runtime_error(
  2176 + "attempt to create stream in null qpdf object");
  2177 + }
2179 QTC::TC("qpdf", "QPDFObjectHandle newStream with data"); 2178 QTC::TC("qpdf", "QPDFObjectHandle newStream with data");
2180 - QPDFObjectHandle result = newStream(qpdf);  
2181 - result.replaceStreamData(data, newNull(), newNull());  
2182 - return result; 2179 + return qpdf->newStream(data);
2183 } 2180 }
2184 2181
2185 QPDFObjectHandle 2182 QPDFObjectHandle
2186 QPDFObjectHandle::newStream(QPDF* qpdf, std::string const& data) 2183 QPDFObjectHandle::newStream(QPDF* qpdf, std::string const& data)
2187 { 2184 {
  2185 + if (qpdf == nullptr) {
  2186 + throw std::runtime_error(
  2187 + "attempt to create stream in null qpdf object");
  2188 + }
2188 QTC::TC("qpdf", "QPDFObjectHandle newStream with string"); 2189 QTC::TC("qpdf", "QPDFObjectHandle newStream with string");
2189 - QPDFObjectHandle result = newStream(qpdf);  
2190 - result.replaceStreamData(data, newNull(), newNull());  
2191 - return result; 2190 + return qpdf->newStream(data);
2192 } 2191 }
2193 2192
2194 QPDFObjectHandle 2193 QPDFObjectHandle