summaryrefslogtreecommitdiff
path: root/src/lib/SW602GraphicListener.h
blob: b038654457799dfea0a8add83533babc3b0c7519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libsw602 project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_SW602_GRAPHIC_LISTENER_H
#define INCLUDED_SW602_GRAPHIC_LISTENER_H

#include <vector>

#include "SW602GraphicStyle.h"
#include "SW602Listener.h"
#include "SW602Types.h"

namespace libsw602
{

class SW602GraphicShape;

namespace SW602GraphicListenerInternal
{
struct GraphicState;
struct State;
}

/** This class contains the code needed to create Graphic document.

    \note All units are specified in librevenge::RVNG_POINT
 */
class SW602GraphicListener : public SW602Listener
{
public:
  /** constructor */
  SW602GraphicListener(SW602ParserStatePtr parserState, std::vector<SW602PageSpan> const &pageList, librevenge::RVNGDrawingInterface *documentInterface);
  /** simplified constructor (can be used for a embedded graphic with one page).

   \note the box coordinates must be given in point.*/
  SW602GraphicListener(SW602ParserStatePtr parserState, SW602Box2f const &box, librevenge::RVNGDrawingInterface *documentInterface);
  /** destructor */
  virtual ~SW602GraphicListener();

  /** returns the listener type */
  Type getType() const
  {
    return Graphic;
  }

  /** sets the documents language */
  void setDocumentLanguage(std::string locale);
  /** starts a new document */
  void startDocument();
  /** ends the actual document */
  void endDocument(bool delayed=true);

  // ------ general information --------
  /** returns true if a text zone is opened */
  bool canWriteText() const;
  /** returns true if a document is opened */
  bool isDocumentStarted() const;

  /** function called to add a subdocument and modify the origin*/
  void handleSubDocument(SW602Vec2f const &orig, SW602SubDocumentPtr subDocument, libsw602::SubDocumentType subDocumentType);
  /** function called to add a subdocument */
  void handleSubDocument(SW602SubDocumentPtr subDocument, libsw602::SubDocumentType subDocumentType)
  {
    handleSubDocument(SW602Vec2f(0,0), subDocument, subDocumentType);
  }
  /** returns try if a subdocument is open  */
  bool isSubDocumentOpened(libsw602::SubDocumentType &subdocType) const;
  /** store the position and the style (which will be needed further to insert a textbox or a table with openTable) */
  bool openFrame(SW602Position const &pos, SW602GraphicStyle const &style=SW602GraphicStyle::emptyStyle());
  /** close a frame */
  void closeFrame();
  /** open a group */
  bool openGroup(SW602Position const &pos);
  /** close a group */
  void closeGroup();
  /** open a layer */
  bool openLayer(librevenge::RVNGString const &name);
  /** close a layer */
  void closeLayer();

  // ------ page --------
  /** opens a master page */
  bool openMasterPage(SW602PageSpan &masterPage);
  /** close a master page */
  void closeMasterPage()
  {
    _closePageSpan(true);
  }
  /** returns true if a page is opened */
  bool isPageSpanOpened() const;
  /** returns the current page span

  \note this forces the opening of a new page if no page is opened.*/
  SW602PageSpan const &getPageSpan();

  // ------ header/footer --------
  /** insert a header */
  bool insertHeader(SW602SubDocumentPtr subDocument, librevenge::RVNGPropertyList const &extras);
  /** insert a footer */
  bool insertFooter(SW602SubDocumentPtr subDocument, librevenge::RVNGPropertyList const &extras);
  /** returns true if the header/footer is open */
  bool isHeaderFooterOpened() const;

  // ------ text data -----------
  //! adds a basic character, ..
  void insertChar(uint8_t character);
  /** insert a character using the font converter to find the utf8
      character */
  void insertCharacter(unsigned char c);
  /** adds an unicode character.
   *  By convention if \a character=0xfffd(undef), no character is added */
  void insertUnicode(uint32_t character);
  //! adds a unicode string
  void insertUnicodeString(librevenge::RVNGString const &str);

  //! adds a tab
  void insertTab();
  //! adds an end of line ( by default an hard one)
  void insertEOL(bool softBreak=false);

  // ------ text format -----------
  //! sets the font
  void setFont(SW602Font const &font);
  //! returns the actual font
  SW602Font const &getFont() const;

  // ------ paragraph format -----------
  //! returns true if a paragraph or a list is opened
  bool isParagraphOpened() const;
  //! sets the paragraph
  void setParagraph(SW602Paragraph const &paragraph);
  //! returns the actual paragraph
  SW602Paragraph const &getParagraph() const;

  // ------- fields ----------------
  //! adds a field type
  void insertField(SW602Field const &field);

  // ------- link ----------------
  //! open a link
  void openLink(SW602Link const &link);
  //! close a link
  void closeLink();

  // ------- subdocument -----------------
  /** adds a picture with potential various representationin given position */
  void insertPicture(SW602Position const &pos, SW602EmbeddedObject const &picture,
                     SW602GraphicStyle const &style=SW602GraphicStyle::emptyStyle());
  /** adds a shape picture in given position */
  void insertShape(SW602Position const &pos, SW602GraphicShape const &shape, SW602GraphicStyle const &style);
  /** adds a textbox in given position */
  void insertTextBox(SW602Position const &pos, SW602SubDocumentPtr subDocument, SW602GraphicStyle const &style);
  /** adds a group: ie. next insertion will be done relative to this bdbox[0] position */
  void insertGroup(SW602Box2f const &bdbox, SW602SubDocumentPtr subDocument);
  /** insert a note

   \note as RVNGDrawingInterface does not accept note, note can only be inserted in a text zone (and are inserted between --) */
  void insertNote(SW602Note const &note, SW602SubDocumentPtr &subDocument);
  /** adds comment

   \note as RVNGDrawingInterface does not accept comment, comment can only be inserted in a text zone (and are inserted between --) */
  void insertComment(SW602SubDocumentPtr &subDocument);

  // ------- table -----------------

  /** adds a table in given position */
  void insertTable(SW602Position const &pos, SW602Table &table, SW602GraphicStyle const &style=SW602GraphicStyle::emptyStyle());
  /** open a table (using the last parameters of openFrame for the position ) */
  void openTable(SW602Table const &table);
  /** open a table in a given position */
  void openTable(SW602Position const &pos, SW602Table const &table, SW602GraphicStyle const &style);
  /** closes this table */
  void closeTable();
  /** open a row with given height ( if h < 0.0, set min-row-height = -h )*/
  void openTableRow(float h, librevenge::RVNGUnit unit, bool headerRow=false);
  /** closes this row */
  void closeTableRow();
  /** open a cell */
  void openTableCell(SW602Cell const &cell);
  /** close a cell */
  void closeTableCell();
  /** add empty cell */
  void addEmptyTableCell(SW602Vec2i const &pos, SW602Vec2i span=SW602Vec2i(1,1));

  // ------- section ---------------

  /** returns true if we can add open a section, add page break, ... */
  bool canOpenSectionAddBreak() const
  {
    return false;
  }
  //! returns true if a section is opened
  bool isSectionOpened() const
  {
    return false;
  }
  //! returns the actual section
  SW602Section const &getSection() const;
  //! open a section if possible
  bool openSection(SW602Section const &section);
  //! close a section
  bool closeSection()
  {
    return false;
  }
  //! inserts a break type: ColumBreak, PageBreak, ..
  void insertBreak(BreakType breakType);

protected:
  //! does open a new page (low level)
  void _openPageSpan(bool sendHeaderFooters=true);
  //! does close a page (low level)
  void _closePageSpan(bool masterPage=false);

  void _startSubDocument();
  void _endSubDocument();

  /** adds in propList the frame parameters.

   \note if there is some gradient, first draw a rectangle to print the gradient and them update propList */
  void _handleFrameParameters(librevenge::RVNGPropertyList &propList, SW602Position const &pos, SW602GraphicStyle const &style);

  void _openParagraph();
  void _closeParagraph();
  void _resetParagraphState(const bool isListElement=false);

  /** open a list level */
  void _openListElement();
  /** close a list level */
  void _closeListElement();
  /** update the list so that it corresponds to the actual level */
  void _changeList();
  /** low level: find a list id which corresponds to actual list and a change of level.

  \note called when the list id is not set
  */
  int _getListId() const;

  void _openSpan();
  void _closeSpan();

  void _flushText();

  /** creates a new parsing state (copy of the actual state)
   *
   * \return the old one */
  boost::shared_ptr<SW602GraphicListenerInternal::State> _pushParsingState();
  //! resets the previous parsing state
  void _popParsingState();

protected:
  //! the actual global state
  boost::shared_ptr<SW602GraphicListenerInternal::GraphicState> m_ds;
  //! the actual local parse state
  boost::shared_ptr<SW602GraphicListenerInternal::State> m_ps;
  //! stack of local state
  std::vector<boost::shared_ptr<SW602GraphicListenerInternal::State> > m_psStack;
  //! the parser state
  SW602ParserStatePtr m_parserState;
  //! the document interface
  librevenge::RVNGDrawingInterface *m_documentInterface;

private:
  SW602GraphicListener(const SW602GraphicListener &);
  SW602GraphicListener &operator=(const SW602GraphicListener &);
};

}

#endif

/* vim:set shiftwidth=2 softtabstop=2 expandtab: */