LibreOffice Module sdext (master)  1
genericelements.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
21 #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
22 
23 #include "pdfihelper.hxx"
24 #include "treevisiting.hxx"
25 
26 #include <com/sun/star/task/XStatusIndicator.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <rtl/ustring.hxx>
30 #include <rtl/ustrbuf.hxx>
31 
32 #include <list>
33 
34 namespace pdfi
35 {
36  class XmlEmitter;
37  class StyleContainer;
38  class ImageContainer;
39  class PDFIProcessor;
40  class ElementFactory;
41 
42 
43  struct EmitContext
44  {
46  XmlEmitter& _rEmitter,
47  StyleContainer& _rStyles,
48  ImageContainer& _rImages,
49  PDFIProcessor& _rProcessor,
50  const css::uno::Reference<
51  css::task::XStatusIndicator>& _xStatusIndicator,
52  css::uno::Reference< css::uno::XComponentContext > const & xContext)
53  :
54  rEmitter(_rEmitter),
55  rStyles(_rStyles),
56  rImages(_rImages),
57  rProcessor(_rProcessor),
58  xStatusIndicator(_xStatusIndicator),
59  m_xContext(xContext)
60  {}
61 
66  css::uno::Reference<
67  css::task::XStatusIndicator> xStatusIndicator;
68  css::uno::Reference<
69  css::uno::XComponentContext > m_xContext;
70  };
71 
72  struct Element
73  {
74  protected:
75  explicit Element( Element* pParent )
76  : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
77  {
78  if( pParent )
79  pParent->Children.emplace_back( this );
80  }
81 
82  public:
83  virtual ~Element();
84 
89  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) = 0;
93  void updateGeometryWith( const Element* pMergeFrom );
94 
95 #if OSL_DEBUG_LEVEL > 0
96  // xxx refact TODO: move code to visitor
97  virtual void emitStructure( int nLevel );
98 #endif
99 
102  static void setParent( std::list<std::unique_ptr<Element>>::iterator const & el, Element* pNewParent );
103 
104  double x, y, w, h;
105  sal_Int32 StyleId;
107  std::list<std::unique_ptr<Element>> Children;
108  };
109 
110  struct ListElement : public Element
111  {
112  ListElement() : Element( nullptr ) {}
113  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
114  };
115 
116  struct HyperlinkElement : public Element
117  {
118  friend class ElementFactory;
119  protected:
120  HyperlinkElement( Element* pParent, const OUString& rURI )
121  : Element( pParent ), URI( rURI ) {}
122  public:
123  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
124 
125  OUString const URI;
126  };
127 
128  struct GraphicalElement : public Element
129  {
130  protected:
131  GraphicalElement(Element* pParent, sal_Int32 nGCId)
132  : Element(pParent)
133  , GCId(nGCId)
134  , MirrorVertical(false)
135  , IsForText(false)
136  , FontSize(0.0)
137  , TextStyleId(0)
138  {
139  }
140 
141  public:
142  sal_Int32 GCId;
144  bool IsForText;
145  double FontSize;
146  sal_Int32 TextStyleId;
147  };
148 
150  {
151  protected:
152  DrawElement( Element* pParent, sal_Int32 nGCId )
153  : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
154 
155  public:
157  sal_Int32 ZOrder;
158  };
159 
160  struct FrameElement : public DrawElement
161  {
162  friend class ElementFactory;
163  protected:
164  FrameElement( Element* pParent, sal_Int32 nGCId )
165  : DrawElement( pParent, nGCId ) {}
166 
167  public:
168  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
169  };
170 
172  {
173  friend class ElementFactory;
174  protected:
175  TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
176  : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
177 
178  public:
179  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
180 
181  OUStringBuffer Text;
182  sal_Int32 FontId;
183  };
184 
185  struct ParagraphElement : public Element
186  {
187  friend class ElementFactory;
188  protected:
189  explicit ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
190 
191  public:
192  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
193 
194  // returns true only if only a single line is contained
195  bool isSingleLined( PDFIProcessor const & rProc ) const;
196  // returns the highest line height of the contained textelements
197  // line height is font height if the text element is itself multilined
198  double getLineHeight( PDFIProcessor& rProc ) const;
199  // returns the first text element child; does not recurse through subparagraphs
201 
204  bool bRtl;
205  };
206 
207  struct PolyPolyElement : public DrawElement
208  {
209  friend class ElementFactory;
210  protected:
211  PolyPolyElement( Element* pParent, sal_Int32 nGCId,
212  const basegfx::B2DPolyPolygon& rPolyPoly,
213  sal_Int8 nAction );
214  public:
215  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
216 
217  void updateGeometry();
218 
219 #if OSL_DEBUG_LEVEL > 0
220  virtual void emitStructure( int nLevel ) override;
221 #endif
222 
225  };
226 
227  struct ImageElement : public DrawElement
228  {
229  friend class ElementFactory;
230  protected:
231  ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
232  : DrawElement( pParent, nGCId ), Image( nImage ) {}
233 
234  public:
235  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
236 
237  ImageId const Image;
238  };
239 
240  struct PageElement : public Element
241  {
242  friend class ElementFactory;
243  protected:
244  PageElement( Element* pParent, sal_Int32 nPageNr )
245  : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
246  TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 )
247  {}
248  private:
249  // helper method for resolveHyperlinks
250  bool resolveHyperlink( const std::list<std::unique_ptr<Element>>::iterator& link_it, std::list<std::unique_ptr<Element>>& rElements );
251  public:
252  virtual ~PageElement() override;
253 
254  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
255 
256  void resolveHyperlinks();
257  void resolveFontStyles( PDFIProcessor const & rProc );
258  void resolveUnderlines( PDFIProcessor const & rProc );
259 
260  sal_Int32 const PageNumber;
261  ListElement Hyperlinks; // contains not yet realized links on this page
262  double TopMargin;
263  double BottomMargin;
264  double LeftMargin;
265  double RightMargin;
266  std::unique_ptr<Element> HeaderElement;
267  std::unique_ptr<Element> FooterElement;
268  };
269 
270  struct DocumentElement : public Element
271  {
272  friend class ElementFactory;
273  protected:
274  DocumentElement() : Element( nullptr ) {}
275  public:
276  virtual ~DocumentElement() override;
277 
278  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
279  };
280 
281  // this class is the differentiator of document types: it will create
282  // Element objects with an optimize() method suitable for the document type
284  {
285  public:
286  ElementFactory() = delete;
287 
288  static HyperlinkElement* createHyperlinkElement( Element* pParent, const OUString& rURI )
289  { return new HyperlinkElement( pParent, rURI ); }
290 
291  static TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
292  { return new TextElement( pParent, nGCId, nFontId ); }
294  { return new ParagraphElement( pParent ); }
295 
296  static FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
297  { return new FrameElement( pParent, nGCId ); }
298  static PolyPolyElement*
300  sal_Int32 nGCId,
301  const basegfx::B2DPolyPolygon& rPolyPoly,
302  sal_Int8 nAction)
303  { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
304  static ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
305  { return new ImageElement( pParent, nGCId, nImage ); }
306 
308  sal_Int32 nPageNr )
309  { return new PageElement( pParent, nPageNr ); }
311  { return new DocumentElement(); }
312  };
313 }
314 
315 #endif
316 
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
basegfx::B2DPolyPolygon PolyPoly
virtual void emitStructure(int nLevel)
HyperlinkElement(Element *pParent, const OUString &rURI)
signed char sal_Int8
bool isSingleLined(PDFIProcessor const &rProc) const
ParagraphElement(Element *pParent)
void applyToChildren(ElementTreeVisitor &)
Apply visitor to all children.
static ParagraphElement * createParagraphElement(Element *pParent)
GraphicalElement(Element *pParent, sal_Int32 nGCId)
static DocumentElement * createDocumentElement()
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
Main entry from the parser.
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt)=0
To be implemented by every tree node that needs to be visitable.
PolyPolyElement(Element *pParent, sal_Int32 nGCId, const basegfx::B2DPolyPolygon &rPolyPoly, sal_Int8 nAction)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
FontSize
virtual void emitStructure(int nLevel) override
PageElement(Element *pParent, sal_Int32 nPageNr)
virtual ~Element()
static TextElement * createTextElement(Element *pParent, sal_Int32 nGCId, sal_Int32 nFontId)
PDFIProcessor & rProcessor
std::unique_ptr< Element > HeaderElement
TextElement * getFirstTextChild() const
virtual ~DocumentElement() override
DrawElement(Element *pParent, sal_Int32 nGCId)
std::unique_ptr< Element > FooterElement
sal_Int32 ImageId
Definition: pdfihelper.hxx:46
ImageContainer & rImages
ImageElement(Element *pParent, sal_Int32 nGCId, ImageId nImage)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
To be visited by all tree element types.
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
static PageElement * createPageElement(Element *pParent, sal_Int32 nPageNr)
css::uno::Reference< css::task::XStatusIndicator > xStatusIndicator
std::list< std::unique_ptr< Element > > Children
Element(Element *pParent)
virtual ~PageElement() override
static FrameElement * createFrameElement(Element *pParent, sal_Int32 nGCId)
void resolveFontStyles(PDFIProcessor const &rProc)
bool resolveHyperlink(const std::list< std::unique_ptr< Element >>::iterator &link_it, std::list< std::unique_ptr< Element >> &rElements)
Output interface to ODF.
Definition: xmlemitter.hxx:32
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
StyleContainer & rStyles
void updateGeometryWith(const Element *pMergeFrom)
Union element geometry with given element.
static PolyPolyElement * createPolyPolyElement(Element *pParent, sal_Int32 nGCId, const basegfx::B2DPolyPolygon &rPolyPoly, sal_Int8 nAction)
void resolveUnderlines(PDFIProcessor const &rProc)
sal_Int32 const PageNumber
static void setParent(std::list< std::unique_ptr< Element >>::iterator const &el, Element *pNewParent)
el must be a valid dereferenceable iterator of el->Parent->Children pNewParent must not be NULL ...
XmlEmitter & rEmitter
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
FrameElement(Element *pParent, sal_Int32 nGCId)
static ImageElement * createImageElement(Element *pParent, sal_Int32 nGCId, ImageId nImage)
double getLineHeight(PDFIProcessor &rProc) const
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
EmitContext(XmlEmitter &_rEmitter, StyleContainer &_rStyles, ImageContainer &_rImages, PDFIProcessor &_rProcessor, const css::uno::Reference< css::task::XStatusIndicator > &_xStatusIndicator, css::uno::Reference< css::uno::XComponentContext > const &xContext)
static HyperlinkElement * createHyperlinkElement(Element *pParent, const OUString &rURI)
OUStringBuffer Text
TextElement(Element *pParent, sal_Int32 nGCId, sal_Int32 nFontId)