From: Michal H. <ms...@gm...> - 2010-06-11 14:08:49
|
CContentStream instantiation involves some non-trivial steps (like graphical state and resources objects creation and setting the self reference). Let's wrap this into a static helper function (this has to be a class function because page::display - used internally - is not accessible normally) and replace all direct instantiation with it. Finally, addToFront resp. addToBack has a new variant with the content stream parameter. This is intended for later use but it is nice to have this change along with the instantiation changes. Index: pdfedit-patches/src/kernel/cpagecontents.cc =================================================================== --- pdfedit-patches.orig/src/kernel/cpagecontents.cc 2010-06-11 14:10:52.000000000 +0200 +++ pdfedit-patches/src/kernel/cpagecontents.cc 2010-06-11 14:24:33.000000000 +0200 @@ -45,6 +45,21 @@ using namespace boost; using namespace observer; using namespace utils; +boost::shared_ptr<CContentStream> CPageContents::createContentStream(const CPage &page, CContentStream::CStreams *streams) { + + boost::shared_ptr<GfxResources> res; + boost::shared_ptr<GfxState> state; + page.display()->createXpdfDisplayParams(res, state); + + boost::shared_ptr<CContentStream> cc(streams + ?new CContentStream(*streams, state, res) + :new CContentStream(state, res)); + + // Init cc and save smart pointer of the content stream so pdfoperators can get it + cc->setSmartPointer(cc); + return cc; +} + //========================================================== // Contents watchdog @@ -262,7 +277,7 @@ CPageContents::getContentStream (size_t // template<typename Container> void -CPageContents::addToFront (const Container& cont) +CPageContents::addToFront (boost::shared_ptr<CContentStream> &cc, const Container& cont) { // Create cstream from container of pdf operators shared_ptr<CStream> stream = createStreamFromObjects (cont, _dict->getPdf()); @@ -277,15 +292,10 @@ CPageContents::addToFront (const Contain // Parse new stream to content stream and add it to the streams CContentStream::CStreams streams; streams.push_back (stream); - boost::shared_ptr<GfxResources> res; - boost::shared_ptr<GfxState> state; - _xpdf_display_params (res, state); - CCs _tmp; - // Init cc and save smart pointer of the content stream so pdfoperators can get it - boost::shared_ptr<CContentStream> cc (new CContentStream(streams,state,res)); - cc->setSmartPointer (cc); + cc->setStreams(streams); // copy it to front + CCs _tmp; _tmp.push_back (cc); init(); std::copy (_ccs.begin(), _ccs.end(), std::back_inserter(_tmp)); @@ -294,15 +304,21 @@ CPageContents::addToFront (const Contain // Indicate change change (); } + +template<typename Container> +void +CPageContents::addToFront (const Container& cont) +{ + boost::shared_ptr<CContentStream> cc = createContentStream(*_page, NULL); + addToFront(cc, cont); +} + template void CPageContents::addToFront<vector<shared_ptr<PdfOperator> > > (const vector<shared_ptr<PdfOperator> >& cont); template void CPageContents::addToFront<deque<shared_ptr<PdfOperator> > > (const deque<shared_ptr<PdfOperator> >& cont); -// -// -// template<typename Container> void -CPageContents::addToBack (const Container& cont) +CPageContents::addToBack (boost::shared_ptr<CContentStream> &cc, const Container& cont) { // Create cstream from container of pdf operators if (!hasValidPdf(_dict)) @@ -320,18 +336,23 @@ CPageContents::addToBack (const Containe // Parse new stream to content stream and add it to the streams CContentStream::CStreams streams; streams.push_back (stream); - boost::shared_ptr<GfxResources> res; - boost::shared_ptr<GfxState> state; - _xpdf_display_params (res, state); - // Init and save smart pointer - boost::shared_ptr<CContentStream> cc (new CContentStream(streams,state,res)); - cc->setSmartPointer (cc); + cc->setStreams(streams); init(); _ccs.push_back (cc); // Indicate change change (); } +// +// +// +template<typename Container> +void +CPageContents::addToBack (const Container& cont) +{ + boost::shared_ptr<CContentStream> cc = createContentStream(*_page, NULL); + addToBack(cc, cont); +} template void CPageContents::addToBack<vector<shared_ptr<PdfOperator> > > (const vector<shared_ptr<PdfOperator> >& cont); template void CPageContents::addToBack<deque<shared_ptr<PdfOperator> > > (const deque<shared_ptr<PdfOperator> >& cont); @@ -854,9 +875,7 @@ CPageContents::parse () // True if Contents is not [ ] while (!streams.empty()) { - shared_ptr<CContentStream> cc (new CContentStream(streams,state,res)); - // Save smart pointer of the content stream so pdfoperators can return it - cc->setSmartPointer (cc); + shared_ptr<CContentStream> cc = createContentStream(*_page, &streams); _ccs.push_back (cc); } Index: pdfedit-patches/src/kernel/cpagecontents.h =================================================================== --- pdfedit-patches.orig/src/kernel/cpagecontents.h 2010-06-11 14:10:52.000000000 +0200 +++ pdfedit-patches/src/kernel/cpagecontents.h 2010-06-11 14:12:29.000000000 +0200 @@ -165,6 +165,16 @@ public: template<typename Container> void addToFront (const Container& cont); + /** Add new content stream to the front. + * This function should be used when we have already preinitialized content stream + * and we want to fill it with the supplied operators. + * + * @param cc Content stream to fill and add. + * @param cont Container of operators to add. + */ + template<typename Container> + void addToFront (boost::shared_ptr<CContentStream> &cc, const Container& cont); + /** * Add new content stream to the back. This function adds new entry in the "Contents" * property of a page. The container of provided operators must form a valid @@ -181,6 +191,16 @@ public: template<typename Container> void addToBack (const Container& cont); + /** Add new content stream to the back. + * This function should be used when we have already preinitialized content stream + * and we want to fill it with the supplied operators. + * + * @param cc Content stream to fill and add. + * @param cont Container of operators to add. + */ + template<typename Container> + void addToBack (boost::shared_ptr<CContentStream> &cc, const Container& cont); + /** * Remove content stream. * This function removes all objects from "Contents" entry which form specified contentstream. @@ -445,6 +465,14 @@ private: { _cnt->reg_observer();} }; +private: + /** Helper for content stream creation. + * @param page Page where the created content stream will be placed. + * @param streams Streams (an empty content stream will be created if NULL). + */ + static boost::shared_ptr<CContentStream> createContentStream(const CPage &page, + CContentStream::CStreams *streams); + }; // class CPageContents //========================================================== |