40 namespace pdfobjects {
44 using namespace boost;
45 using namespace observer;
46 using namespace utils;
58 CPageContents::ContentsWatchDog::notify (shared_ptr<IProperty> newValue,
59 shared_ptr<const IProperty::ObserverContext> context)
const throw()
76 switch(context->getType())
87 shared_ptr<const CDict::CDictComplexObserverContext> ctxtdict =
105 shared_ptr<IProperty> oldValue = ctxtdict->getOriginalValue();
107 _cnt->unreg_observer (oldValue);
112 _cnt->reg_observer(newValue);
119 shared_ptr<const CArray::CArrayComplexObserverContext> ctxtarray =
136 assert (!
"Invalid change context - contents observer!");
145 _cnt->_page->invalidate ();
160 template<
typename Container>
161 boost::shared_ptr<CStream>
162 createStreamFromObjects (
const Container& cont, boost::weak_ptr<CPdf> pdf)
165 shared_ptr<CStream> newstr (
new CStream());
178 typename Container::const_iterator it = cont.begin();
179 for (; it != cont.end(); ++it)
182 (*it)->getStringRepresentation (tmpop);
188 newstr->setBuffer (str);
191 boost::shared_ptr<CPdf> p = pdf.lock();
193 IndiRef newref = p->addIndirectProperty (newstr);
194 newstr = IProperty::getSmartCObjectPtr<CStream> (p->getIndirectProperty (newref));
204 template<
typename In,
typename Out>
205 void getAllCStreams (
const In& in, Out& out)
207 for (
typename In::const_iterator it = in.begin(); it != in.end(); ++it)
210 (*it)->getCStreams (tmp);
211 copy (tmp.begin(), tmp.end(), back_inserter (out));
237 shared_ptr<CContentStream>
241 for (CCs::iterator it = _ccs.begin(); it != _ccs.end(); ++it)
242 if ((*it).get() == cc)
245 assert (!
"Contentstream not found");
250 shared_ptr<CContentStream>
254 if (pos >= _ccs.size())
263 template<
typename Container>
268 shared_ptr<CStream> stream = createStreamFromObjects (cont, _dict->getPdf());
274 CRef rf (stream->getIndiRef());
279 streams.push_back (stream);
280 boost::shared_ptr<GfxResources> res;
281 boost::shared_ptr<GfxState> state;
282 _xpdf_display_params (res, state);
285 boost::shared_ptr<CContentStream> cc (
new CContentStream(streams,state,res));
286 cc->setSmartPointer (cc);
291 std::copy (_ccs.begin(), _ccs.end(), std::back_inserter(_tmp));
297 template void CPageContents::addToFront<vector<shared_ptr<PdfOperator> > > (
const vector<shared_ptr<PdfOperator> >& cont);
298 template void CPageContents::addToFront<deque<shared_ptr<PdfOperator> > > (
const deque<shared_ptr<PdfOperator> >& cont);
303 template<
typename Container>
310 shared_ptr<CStream> stream = createStreamFromObjects (cont, _dict->getPdf());
316 CRef rf (stream->getIndiRef());
322 streams.push_back (stream);
323 boost::shared_ptr<GfxResources> res;
324 boost::shared_ptr<GfxState> state;
325 _xpdf_display_params (res, state);
327 boost::shared_ptr<CContentStream> cc (
new CContentStream(streams,state,res));
328 cc->setSmartPointer (cc);
335 template void CPageContents::addToBack<vector<shared_ptr<PdfOperator> > > (
const vector<shared_ptr<PdfOperator> >& cont);
336 template void CPageContents::addToBack<deque<shared_ptr<PdfOperator> > > (
const deque<shared_ptr<PdfOperator> >& cont);
348 if (csnum >= _ccs.size())
352 remove (_ccs[csnum]);
354 _ccs.erase (_ccs.begin() + csnum, _ccs.begin() + csnum + 1);
370 if (!textDev->isOk())
374 _page->display()->displayPage (*textDev);
382 scoped_ptr<GString> gtxt (textDev->getText(rec.xleft, rec.yleft, rec.xright, rec.yright));
383 text = gtxt->getCString();
394 template<
typename RectangleContainer>
396 RectangleContainer& recs,
401 assert (textDev->isOk());
402 if (!textDev->isOk())
406 _page->display()->displayPage (*textDev);
408 GBool startAtTop, stopAtBottom, startAtLast, stopAtLast, caseSensitive, backward;
409 startAtTop = stopAtBottom = startAtLast = stopAtLast =
gTrue;
410 caseSensitive = backward =
gFalse;
412 double xMin = 0, yMin = 0, xMax = 0, yMax = 0;
415 int length =
static_cast<int>(text.length());
417 for (
int i = 0;
i < length; ++
i)
418 utext[
i] = static_cast<Unicode> (text[
i] & 0xff);
420 if (textDev->findText(utext, length, startAtTop, stopAtBottom,
421 startAtLast,stopAtLast, caseSensitive, backward,
422 &xMin, &yMin, &xMax, &yMax))
428 while (textDev->findText (utext, length,
429 startAtTop, stopAtBottom,
430 startAtLast, stopAtLast,
431 caseSensitive, backward,
432 &xMin, &yMin, &xMax, &yMax))
446 template size_t CPageContents::findText<std::vector<libs::Rectangle> >
448 std::vector<libs::Rectangle>& recs,
459 for (CCs::iterator it = _ccs.begin (); it != _ccs.end(); ++it)
460 (*it)->replaceText (what, with);
469 const std::string& font_id)
483 std::string fontName (font_id);
484 if (fontName.empty())
485 fontName =
"PDFEDIT_F1";
486 double fontSize = 15.0;
490 fontOperands.push_back(shared_ptr<IProperty>(
new CName (fontName)) );
491 fontOperands.push_back(shared_ptr<IProperty>(
new CReal (fontSize)));
495 _likely_tm.set_position(where);
500 textOperands.push_back(shared_ptr<IProperty>(
new CString (what)));
506 std::vector<shared_ptr<PdfOperator> > contents;
507 contents.push_back(q);
548 shared_ptr<CInlineImage> inline_image (
new CInlineImage (image_dict, what));
555 std::vector<shared_ptr<PdfOperator> > contents;
556 contents.push_back(q);
576 static inline void add (CArray& arr,
CRef& ref)
577 { arr.addProperty (0, ref); }
581 static inline void add (CArray& arr,
CRef& ref, IProperty& content)
583 arr.addProperty (content);
584 arr.addProperty (ref);
586 static inline void add (CArray& arr,
CRef& ref)
587 { arr.addProperty (arr.getPropertyCount(), ref); }
589 enum OPERWHERE {FRONT, BACK} ;
590 template<
int T>
struct OpTrait;
591 template<>
struct OpTrait<FRONT> {
typedef struct ToFront Oper; };
592 template<>
struct OpTrait<BACK> {
typedef struct ToBack Oper; };
596 template<OPERWHERE WHERE>
598 cc_add (shared_ptr<CDict> _dict,
CRef& ref)
604 arr.addProperty (ref);
620 }
else if (
isArray (realcontent))
623 shared_ptr<CArray> array = IProperty::getSmartCObjectPtr<CArray> (realcontent);
642 CPageContents::toFront (
CRef& ref)
645 ContentsObserverFreeSection reg_lock (
this);
646 cc_add<FRONT> (_dict, ref);
656 CPageContents::toBack (
CRef& ref)
659 ContentsObserverFreeSection reg_lock (
this);
660 cc_add<BACK> (_dict, ref);
674 template<
typename Cont>
685 typedef vector<shared_ptr<CStream> > Css;
687 getAllCStreams (cont, css);
690 for (Css::iterator it = css.begin(); it != css.end(); ++it)
696 CRef rf ((*it)->getIndiRef ());
697 cc_add<BACK> (dict, rf);
701 template void CPageContents::setContents<vector<shared_ptr<CContentStream> > >
702 (shared_ptr<CDict> dict,
const vector<shared_ptr<CContentStream> >& cont);
714 ContentsObserverFreeSection reg_lock (
this);
720 typedef vector<shared_ptr<CStream> > Css;
722 cs->getCStreams (css);
725 for (Css::iterator it = css.begin(); it != css.end(); ++it)
733 remove ((*it)->getIndiRef ());
756 }
else if (
isArray (realcontent))
759 shared_ptr<CArray> array = IProperty::getSmartCObjectPtr<CArray> (realcontent);
760 for (
size_t i = 0;
i < array->getPropertyCount(); ++
i)
765 array->delProperty (
i);
789 boost::shared_ptr<GfxResources> res;
790 boost::shared_ptr<GfxState> state;
791 _xpdf_display_params (res, state);
796 for (CCs::iterator it = _ccs.begin(); it != _ccs.end(); ++it)
797 (*it)->reparse (
true, state, res);
803 CPageContents::parse ()
814 boost::shared_ptr<GfxResources> res;
815 boost::shared_ptr<GfxState> state;
816 _xpdf_display_params (res, state);
834 shared_ptr<CStream> stream = IProperty::getSmartCObjectPtr<CStream> (contents);
835 streams.push_back (stream);
840 shared_ptr<CArray> array = IProperty::getSmartCObjectPtr<CArray> (contents);
841 for (
size_t i = 0;
i < array->getPropertyCount(); ++
i)
853 assert (_ccs.empty());
855 while (!streams.empty())
857 shared_ptr<CContentStream> cc (
new CContentStream(streams,state,res));
859 cc->setSmartPointer (cc);
866 for (CCs::const_iterator it = _ccs.begin();
871 (*it)->getPdfOperators (ops);
875 while (!opit.isEnd())
878 opit.getCurrent()->getOperatorName (tmp);
883 opit.getCurrent()->getParameters (operands);
884 _likely_tm = operands;
902 CPageContents::reg_observer (boost::shared_ptr<IProperty> ip)
const
924 CPageContents::unreg_observer (boost::shared_ptr<IProperty> ip)
const
944 CPageContents::change (
bool invalid)
946 _page->_objectChanged (invalid);
950 CPageContents::_xpdf_display_params (boost::shared_ptr<GfxResources>& res,
951 boost::shared_ptr<GfxState>& state)
953 _page->display()->createXpdfDisplayParams (res, state);
957 CPageContents::_page_pos ()
const
970 CCs::iterator itNext = find (_ccs.begin(), _ccs.end(), ct);
971 if (itNext == _ccs.end())
974 if (itNext == _ccs.end())
978 shared_ptr<CContentStream> tmp = *itNext;
979 _ccs.erase (itNext, itNext + 1);
981 _ccs.insert (find (_ccs.begin(), _ccs.end(), ct), tmp);
984 ContentsObserverFreeSection reg_lock (
this);
999 unsigned int pos = 0;
1001 for (pos = 0; pos < _ccs.size(); ++pos)
1002 if (_ccs[pos] == ct)
1006 if (pos == _ccs.size() || 0 == pos)
1010 shared_ptr<CContentStream> tmp = _ccs[pos];
1011 _ccs[pos] = _ccs[pos - 1];
1012 _ccs[pos - 1] = tmp;
1016 ContentsObserverFreeSection reg_lock (
this);
1031 moveAbove (_page->contents()->getContentStream (pos));
1040 moveBelow (_page->contents()->getContentStream (pos));
1053 assert (!_wd.use_count());