gui
[lbook_fbreader.git] / fbreader / src / formats / rtf / RtfReader.h
blobff065cd814a619d9165db4191e5c124dbec1b5e3
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #ifndef __RTFREADER_H__
21 #define __RTFREADER_H__
23 #include <string>
24 #include <map>
25 #include <stack>
27 #include <ZLEncodingConverter.h>
29 #include <ZLTextAlignmentType.h>
31 #include "../EncodedTextReader.h"
33 class ZLInputStream;
34 class RtfCommand;
36 class RtfReader : public EncodedTextReader {
38 private:
39 static void fillKeywordMap();
40 static void addAction(const std::string &tag, RtfCommand *command);
42 private:
43 static std::map<std::string, RtfCommand*> ourKeywordMap;
45 protected:
46 RtfReader(const std::string &encoding);
47 virtual ~RtfReader();
49 public:
50 virtual bool readDocument(const std::string &fileName);
52 protected:
53 enum DestinationType {
54 DESTINATION_NONE,
55 DESTINATION_SKIP,
56 DESTINATION_INFO,
57 DESTINATION_TITLE,
58 DESTINATION_AUTHOR,
59 DESTINATION_PICTURE,
60 DESTINATION_STYLESHEET,
61 DESTINATION_FOOTNOTE,
64 enum FontProperty {
65 FONT_BOLD,
66 FONT_ITALIC,
67 FONT_UNDERLINED
70 virtual void addCharData(const char *data, size_t len, bool convert) = 0;
71 virtual void insertImage(const std::string &mimeType, const std::string &fileName, size_t startOffset, size_t size) = 0;
72 virtual void setEncoding(int code) = 0;
73 virtual void switchDestination(DestinationType destination, bool on) = 0;
74 virtual void setAlignment() = 0;
75 virtual void setFontProperty(FontProperty property) = 0;
76 virtual void newParagraph() = 0;
78 void interrupt();
80 private:
81 bool parseDocument();
82 void processKeyword(const std::string &keyword, int *parameter = 0);
83 void processCharData(const char *data, size_t len, bool convert = true);
85 protected:
86 struct RtfReaderState {
87 bool Bold;
88 bool Italic;
89 bool Underlined;
90 ZLTextAlignmentType Alignment;
91 DestinationType Destination;
93 bool ReadDataAsHex;
96 RtfReaderState myState;
98 private:
99 bool mySpecialMode;
101 std::string myFileName;
102 shared_ptr<ZLInputStream> myStream;
103 char *myStreamBuffer;
105 std::stack<RtfReaderState> myStateStack;
107 int myBinaryDataSize;
108 std::string myNextImageMimeType;
110 int myIsInterrupted;
112 friend class RtfNewParagraphCommand;
113 friend class RtfFontPropertyCommand;
114 friend class RtfAlignmentCommand;
115 friend class RtfCharCommand;
116 friend class RtfDestinationCommand;
117 friend class RtfStyleCommand;
118 friend class RtfSpecialCommand;
119 friend class RtfPictureCommand;
120 friend class RtfFontResetCommand;
121 friend class RtfCodepageCommand;
124 class RtfCommand {
125 protected:
126 virtual ~RtfCommand();
128 public:
129 virtual void run(RtfReader &reader, int *parameter) const = 0;
132 class RtfNewParagraphCommand : public RtfCommand {
133 public:
134 void run(RtfReader &reader, int *parameter) const;
137 class RtfFontPropertyCommand : public RtfCommand {
139 public:
140 RtfFontPropertyCommand(RtfReader::FontProperty property);
141 void run(RtfReader &reader, int *parameter) const;
143 private:
144 RtfReader::FontProperty myProperty;
147 class RtfAlignmentCommand : public RtfCommand {
148 public:
149 RtfAlignmentCommand(ZLTextAlignmentType alignment);
150 void run(RtfReader &reader, int *parameter) const;
152 private:
153 ZLTextAlignmentType myAlignment;
156 class RtfCharCommand : public RtfCommand {
157 public:
158 RtfCharCommand(const std::string &chr);
159 void run(RtfReader &reader, int *parameter) const;
161 private:
162 std::string myChar;
165 class RtfDestinationCommand : public RtfCommand {
166 public:
167 RtfDestinationCommand(RtfReader::DestinationType dest);
168 void run(RtfReader &reader, int *parameter) const;
170 private:
171 RtfReader::DestinationType myDestination;
174 class RtfStyleCommand : public RtfCommand {
175 public:
176 void run(RtfReader &reader, int *parameter) const;
179 class RtfSpecialCommand : public RtfCommand {
180 void run(RtfReader &reader, int *parameter) const;
183 class RtfPictureCommand : public RtfCommand {
184 public:
185 RtfPictureCommand(const std::string &mimeType);
186 void run(RtfReader &reader, int *parameter) const;
188 private:
189 const std::string myMimeType;
192 class RtfFontResetCommand : public RtfCommand {
193 public:
194 void run(RtfReader &reader, int *parameter) const;
197 class RtfCodepageCommand : public RtfCommand {
198 public:
199 void run(RtfReader &reader, int *parameter) const;
202 #endif /* __RTFREADER_H__ */