summaryrefslogtreecommitdiff
path: root/src/lib/SW602TextListener.h
blob: 57ad36cc3e8b1abb008317cee2f683d60820a29e (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
/* -*- 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/.
 */

/** \file SW602TextListener.h
 * Defines SW602TextListener: the libsw602 word processor listener
 *
 * \note this class is the only class which does the interface with
 * the librevenge::RVNGTextInterface
 */
#ifndef INCLUDED_SW602_TEXT_LISTENER_H
#define INCLUDED_SW602_TEXT_LISTENER_H

#include <vector>

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

namespace libsw602
{

class SW602Cell;
class SW602GraphicStyle;
class SW602GraphicShape;
class SW602Table;

namespace SW602TextListenerInternal
{
struct DocumentState;
struct State;
}

/** This class contents the main functions needed to create a Word processing Document */
class SW602TextListener : public SW602Listener
{
public:
  /** constructor */
  SW602TextListener(SW602ParserStatePtr parserState, std::vector<SW602PageSpan> const &pageList, librevenge::RVNGTextInterface *documentInterface);
  /** destructor */
  virtual ~SW602TextListener();

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

  /** sets the documents language */
  void setDocumentLanguage(std::string locale);

  /** starts the document */
  void startDocument();
  /** ends the document */
  void endDocument(bool sendDelayedSubDoc=true);
  /** returns true if a document is opened */
  bool isDocumentStarted() const;

  /** function called to add a subdocument */
  void handleSubDocument(SW602SubDocumentPtr subDocument, libsw602::SubDocumentType subDocumentType);
  /** returns try if a subdocument is open  */
  bool isSubDocumentOpened(libsw602::SubDocumentType &subdocType) const;
  /** tries to open a frame */
  bool openFrame(SW602Position const &pos, SW602GraphicStyle const &style=SW602GraphicStyle::emptyStyle());
  /** tries to close the last open frame */
  void closeFrame();
  /** open a group */
  bool openGroup(SW602Position const &pos);
  /** close a group */
  void closeGroup();

  /** returns true if we can add text data */
  bool canWriteText() const
  {
    return SW602TextListener::isDocumentStarted();
  }

  // ------ page --------
  /** 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 -----------------
  /** insert a note */
  void insertNote(SW602Note const &note, SW602SubDocumentPtr &subDocument);

  /** adds comment */
  void insertComment(SW602SubDocumentPtr &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 &frameStyle=SW602GraphicStyle::emptyStyle());

  // ------- table -----------------
  /** open a table*/
  void openTable(SW602Table const &table);
  /** 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;
  //! returns true if a section is opened
  bool isSectionOpened() const;
  //! returns the actual section
  SW602Section const &getSection() const;
  //! open a section if possible
  bool openSection(SW602Section const &section);
  //! close a section
  bool closeSection();
  //! inserts a break type: ColumBreak, PageBreak, ..
  void insertBreak(BreakType breakType);

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

  void _startSubDocument();
  void _endSubDocument();

  void _handleFrameParameters(librevenge::RVNGPropertyList &propList, SW602Position const &pos);

  void _openParagraph();
  void _closeParagraph();
  void _appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool isListElement=false);
  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;

  /** low level: the function which opens a new span property */
  void _openSpan();
  /** low level: the function which closes the last opened span property */
  void _closeSpan();

  /** low level: flush the deferred text */
  void _flushText();
  /** low level: flush the deferred tabs */
  void _flushDeferredTabs();

  void _insertBreakIfNecessary(librevenge::RVNGPropertyList &propList);

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

protected:
  //! the main parse state
  boost::shared_ptr<SW602TextListenerInternal::DocumentState> m_ds;
  //! the actual local parse state
  boost::shared_ptr<SW602TextListenerInternal::State> m_ps;
  //! stack of local state
  std::vector<boost::shared_ptr<SW602TextListenerInternal::State> > m_psStack;
  //! the parser state
  SW602ParserStatePtr m_parserState;
  //! the document interface
  librevenge::RVNGTextInterface *m_documentInterface;

private:
  //! copy constructor (unimplemented)
  SW602TextListener(const SW602TextListener &);
  //! operator= (unimplemented)
  SW602TextListener &operator=(const SW602TextListener &);
};

}

#endif

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