pdfedit  0.4.5
About: PDFedit is a free and open source library for manipulating PDF documents.
  Fossies Dox: pdfedit-0.4.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cpagecontents.h
Go to the documentation of this file.
1 /*
2  * PDFedit - free program for PDF document manipulation.
3  * Copyright (C) 2006-2009 PDFedit team: Michal Hocko,
4  * Jozef Misutka,
5  * Martin Petricek
6  * Former team members: Miroslav Jahoda
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in doc/LICENSE.GPL); if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  *
22  * Project is hosted on http://sourceforge.net/projects/pdfedit
23  */
24 // vim:tabstop=4:shiftwidth=4:noexpandtab:textwidth=80
25 
26 #ifndef _CPAGECONTENTS_H_
27 #define _CPAGECONTENTS_H_
28 
29 // static includes
30 #include "kernel/static.h"
31 #include "kernel/cpagemodule.h"
32 
33 #include "kernel/iproperty.h"
34 #include "kernel/ccontentstream.h"
35 #include "kernel/cstream.h"
36 #include "kernel/textoutput.h"
38 #include "kernel/stateupdater.h"
39 #include "kernel/cobjectsimple.h"
40 
41 
42 //==========================================================
43 namespace pdfobjects {
44 //==========================================================
45 
46 // Forward declaration
47 class CPage;
48 class CDict;
49 
50 
51 //==========================================================
52 // CPageContents
53 //==========================================================
54 
60 {
61 
62  //==========================================================
63  // Contents observer
64  //==========================================================
65 private:
66 
70  class ContentsWatchDog: public IPropertyObserver
71  {
72  private:
73  CPageContents* _cnt;
74  public:
75  ContentsWatchDog (CPageContents* cnt) : _cnt(cnt) { assert(_cnt); }
76  virtual ~ContentsWatchDog() throw() {}
77  // IPropertyObserver Interface
78  virtual void notify (boost::shared_ptr<IProperty>, boost::shared_ptr<const IProperty::ObserverContext>) const throw();
79  virtual priority_t getPriority() const throw()
80  { return 0; }
81 
82  }; // class ContentsWatchDog
83 
84  //==========================================================
85 
90  struct Tm
91  {
92  Tm ()
93  {
94  std::fill (_tm, _tm+6, 0); _tm[0] = _tm[3] = 1;
95  }
96  void operator= (const PdfOperator::Operands& ops)
97  {
98  if (ops.size() != 6)
99  return;
100  for (size_t i = 0; i < 6; ++i)
101  _tm[i] = utils::getDoubleFromIProperty(ops[i]);
102  }
103  void set_position (const libs::Point& p)
104  { _tm[4] = p.x; _tm[5] = p.y; }
105  operator PdfOperator::Operands ()
106  {
107  PdfOperator::Operands _opers;
108  for (const double* it = &_tm[0]; it != &_tm[6]; ++it)
109  _opers.push_back (boost::shared_ptr<IProperty>(new CReal (*it)));
110  return _opers;
111  }
112  double _tm [6];
113  };
114 
115 
116  // Typedefs
117 private:
118  typedef std::vector<boost::shared_ptr<CContentStream> > CCs;
119 
120  // Variables
121 private:
122  CCs _ccs; // content streams
123  CPage* _page; // pages
124  boost::shared_ptr<CDict> _dict; // pages
125  boost::shared_ptr<ContentsWatchDog> _wd;
126  Tm _likely_tm;
127 
128 
129  // Ctor & Dtor
130 public:
131  CPageContents (CPage* page);
132  ~CPageContents ();
133 
134  //
135  // ICPageModule interface
136  //
137 public:
139  virtual void reset ();
140 
141 
142  //
143  // Methods
144  //
145 public:
146 
150  void reparse ();
151 
165  template<typename Container>
166  void addToFront (const Container& cont);
167 
181  template<typename Container>
182  void addToBack (const Container& cont);
183 
191  void remove (size_t csnum);
192 
204  template<typename RectangleContainer>
205  size_t findText (std::string text,
206  RectangleContainer& recs,
207  const TextSearchParams& params = TextSearchParams()) const;
208 
212  void replaceText (const std::string& what, const std::string& with);
213 
217  void addText (const std::string& what,
218  const libs::Point& where,
219  const std::string& font_id);
220 
224  void addInlineImage (const CStream::Buffer& what,
225  const libs::Point& image_size,
226  const libs::Point& where);
227 
231  template<typename WordEngine,
232  typename LineEngine,
233  typename ColumnEngine>
235  {
237 
239 
240  // Create gfx resource and state
241  boost::shared_ptr<GfxResources> gfxres;
242  boost::shared_ptr<GfxState> gfxstate;
243  _xpdf_display_params (gfxres, gfxstate);
244  assert (gfxres && gfxstate);
245 
246  // Create page text class with parametrized parts
247  TextSource text_source;
248 
249  // Get text from all content streams
250  init();
251  for (CCs::iterator it = _ccs.begin(); it != _ccs.end(); ++it)
252  {
253  // Get operators and build text representation if not empty
255  (*it)->getPdfOperators (ops);
256  if (!ops.empty())
257  {
259  StateUpdater::updatePdfOperators<TextSource&> (itt, gfxres, *gfxstate, text_source);
260  }
261  }
262 
263  // Create lines, columns...
264  text_source.format ();
265  // Build the output
266  if (hasValidPdf(_dict))
267  text_source.output (out, _page_pos());
268  else
269  text_source.output (out, 0);
270  }
271 
272  //
273  // Getters & Setters
274  //
275 public:
276 
285  template<typename OpContainer, typename PositionComparator>
286  void getObjectsAtPosition (OpContainer& opContainer, PositionComparator cmp)
287  {
288  init();
289  // Get the objects with specific comparator
290  for (CCs::iterator it = _ccs.begin (); it != _ccs.end(); ++it)
291  (*it)->getOperatorsAtPosition (opContainer, cmp);
292  }
293 
297  boost::shared_ptr<CContentStream> getContentStream (CContentStream* cc);
298 
302  boost::shared_ptr<CContentStream> getContentStream (size_t pos);
303 
307  template<typename Container>
308  void getContentStreams (Container& container)
309  {
310  init();
311  container.clear();
312  std::copy (_ccs.begin(), _ccs.end(), std::back_inserter(container));
313  }
314 
327  void getText (std::string& text,
328  const std::string* encoding = NULL,
329  const libs::Rectangle* rc = NULL) const;
330 
334  void moveAbove (boost::shared_ptr<const CContentStream> ct);
335  void moveAbove (size_t pos);
336 
340  void moveBelow (boost::shared_ptr<const CContentStream> ct);
341  void moveBelow (size_t pos);
342 
343 
344 
345  //
346  // CCs methods
347  //
348 public:
349 
354  template<typename Cont>
355  static void setContents (boost::shared_ptr<CDict> dict, const Cont& cont);
356 
357  //
358  // CCs helper methods
359  //
360 private:
362  void toFront (CRef& ref);
364  void toBack (CRef& ref);
365 
367  void remove (boost::shared_ptr<const CContentStream> cs);
369  void remove (const IndiRef& rf);
370 
378  bool parse ();
379 
380  //
381  // Helper methods
382  //
383 private:
384 
388  inline void init ()
389  {
390  if (_ccs.empty())
391  parse ();
392  }
393 
397  inline void change (bool invalid = false);
398 
399  //
400  // Helper methods because of cpage not included in headers
401  //
402 private:
406  void _xpdf_display_params (boost::shared_ptr<GfxResources>& res,
407  boost::shared_ptr<GfxState>& state);
411  size_t _page_pos () const;
412 
413 
414  //
415  // Helper methods because of cpage not included in headers
416  //
417 private:
418 
426  void reg_observer (boost::shared_ptr<IProperty> ip = boost::shared_ptr<IProperty>()) const;
427 
433  void unreg_observer (boost::shared_ptr<IProperty> ip = boost::shared_ptr<IProperty>()) const;
434 
435 
439  struct ContentsObserverFreeSection
440  {
441  CPageContents* _cnt;
442  ContentsObserverFreeSection (CPageContents* cnt) : _cnt (cnt)
443  { _cnt->unreg_observer(); }
444  ~ContentsObserverFreeSection ()
445  { _cnt->reg_observer();}
446  };
447 
448 }; // class CPageContents
449 
450 //==========================================================
451 } // namespace pdfobjects
452 //==========================================================
453 
454 
455 #endif // _CPAGECONTENTS_H_
456