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 final : 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 final : public Element
117  {
118  friend class ElementFactory;
119  HyperlinkElement( Element* pParent, const OUString& rURI )
120  : Element( pParent ), URI( rURI ) {}
121  public:
122  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
123 
124  OUString URI;
125  };
126 
127  struct GraphicalElement : public Element
128  {
129  protected:
130  GraphicalElement(Element* pParent, sal_Int32 nGCId)
131  : Element(pParent)
132  , GCId(nGCId)
134  , IsForText(false)
135  , FontSize(0.0)
136  , TextStyleId(0)
137  {
138  }
139 
140  public:
141  sal_Int32 GCId;
143  bool IsForText;
144  double FontSize;
145  sal_Int32 TextStyleId;
146  };
147 
149  {
150  protected:
151  DrawElement( Element* pParent, sal_Int32 nGCId )
152  : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
153 
154  public:
156  sal_Int32 ZOrder;
157  };
158 
159  struct FrameElement final : public DrawElement
160  {
161  friend class ElementFactory;
162  FrameElement( Element* pParent, sal_Int32 nGCId )
163  : DrawElement( pParent, nGCId ) {}
164 
165  public:
166  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
167  };
168 
169  struct TextElement final : public GraphicalElement
170  {
171  friend class ElementFactory;
172  TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
173  : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
174 
175  public:
176  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
177 
178  OUStringBuffer Text;
179  sal_Int32 FontId;
180  };
181 
182  struct ParagraphElement final : public Element
183  {
184  friend class ElementFactory;
185  explicit ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
186 
187  public:
188  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
189 
190  // returns true only if only a single line is contained
191  bool isSingleLined( PDFIProcessor const & rProc ) const;
192  // returns the highest line height of the contained textelements
193  // line height is font height if the text element is itself multilined
194  double getLineHeight( PDFIProcessor& rProc ) const;
195  // returns the first text element child; does not recurse through subparagraphs
197 
200  bool bRtl;
201  };
202 
203  struct PolyPolyElement final : public DrawElement
204  {
205  friend class ElementFactory;
206  PolyPolyElement( Element* pParent, sal_Int32 nGCId,
207  const basegfx::B2DPolyPolygon& rPolyPoly,
208  sal_Int8 nAction );
209  public:
210  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
211 
212  void updateGeometry();
213 
214 #if OSL_DEBUG_LEVEL > 0
215  virtual void emitStructure( int nLevel ) override;
216 #endif
217 
220  };
221 
222  struct ImageElement final : public DrawElement
223  {
224  friend class ElementFactory;
225  ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
226  : DrawElement( pParent, nGCId ), Image( nImage ) {}
227 
228  public:
229  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
230 
232  };
233 
234  struct PageElement final : public Element
235  {
236  friend class ElementFactory;
237  PageElement( Element* pParent, sal_Int32 nPageNr )
238  : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
239  TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 )
240  {}
241  private:
242  // helper method for resolveHyperlinks
243  bool resolveHyperlink( const std::list<std::unique_ptr<Element>>::iterator& link_it, std::list<std::unique_ptr<Element>>& rElements );
244  public:
245  virtual ~PageElement() override;
246 
247  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
248 
249  void resolveHyperlinks();
250  void resolveFontStyles( PDFIProcessor const & rProc );
251  void resolveUnderlines( PDFIProcessor const & rProc );
252 
253  sal_Int32 PageNumber;
254  ListElement Hyperlinks; // contains not yet realized links on this page
255  double TopMargin;
256  double BottomMargin;
257  double LeftMargin;
258  double RightMargin;
259  std::unique_ptr<Element> HeaderElement;
260  std::unique_ptr<Element> FooterElement;
261  };
262 
263  struct DocumentElement final : public Element
264  {
265  friend class ElementFactory;
266  public:
267  DocumentElement() : Element( nullptr ) {}
268  virtual ~DocumentElement() override;
269 
270  virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
271  };
272 
273  // this class is the differentiator of document types: it will create
274  // Element objects with an optimize() method suitable for the document type
276  {
277  public:
278  ElementFactory() = delete;
279 
280  static HyperlinkElement* createHyperlinkElement( Element* pParent, const OUString& rURI )
281  { return new HyperlinkElement( pParent, rURI ); }
282 
283  static TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
284  { return new TextElement( pParent, nGCId, nFontId ); }
286  { return new ParagraphElement( pParent ); }
287 
288  static FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
289  { return new FrameElement( pParent, nGCId ); }
290  static PolyPolyElement*
292  sal_Int32 nGCId,
293  const basegfx::B2DPolyPolygon& rPolyPoly,
294  sal_Int8 nAction)
295  { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
296  static ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
297  { return new ImageElement( pParent, nGCId, nImage ); }
298 
300  sal_Int32 nPageNr )
301  { return new PageElement( pParent, nPageNr ); }
302  static std::shared_ptr<DocumentElement> createDocumentElement()
303  { return std::make_shared<DocumentElement>(); }
304  };
305 }
306 
307 #endif
308 
309 /* 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)
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:45
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)
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
static std::shared_ptr< DocumentElement > createDocumentElement()
bool m_bDetectedRangeSegmentation false
TextElement(Element *pParent, sal_Int32 nGCId, sal_Int32 nFontId)