summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/rtf2html.h
blob: 5685e4a1a532d8b38d321eeda6f11599bc76b619 (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
/*
    rtf2html.h  -  A simple RTF Parser

    Copyright (c) 2002 by Vladimir Shutoff   <vovan@shutoff.ru>             (original code)
    Copyright (c) 2004 by Thiago S. Barcelos <barcelos@ime.usp.br>          (Kopete port)
    Kopete    (c) 2002-2003 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef RTF2HTML_H
#define RTF2HTML_H

#include <tqstring.h>
#include <stdio.h>

#include <tqtextcodec.h>
#include <tqcolor.h>
#include <tqregexp.h>
#include <kdebug.h>

#include <vector>
#include <stack>
#include <string>
#include <stdarg.h>

using namespace std;

struct FontDef
{
    int		charset;
    string	taggedName;
    string	nonTaggedName;
};

class RTF2HTML;

enum TagEnum
{
    TAG_ALL = 0,
    TAG_FONT_SIZE,
    TAG_FONT_COLOR,
    TAG_FONT_FAMILY,
    TAG_BG_COLOR,
    TAG_BOLD,
    TAG_ITALIC,
    TAG_UNDERLINE
};

class ParStyle
{
public:
    ParStyle() { dir = DirLTR; }
    void clearFormatting();

public:
    enum {DirLTR, DirRTL} dir;
};

class Level
{
public:
    Level(RTF2HTML *_p);
    Level(const Level&);
    void setText(const char* str);
    void setFontTbl() { m_bFontTbl = true; }
    void setColors() { m_bColors = true; resetColors(); }
    void setRed(unsigned char val) { setColor(val, &m_nRed); }
    void setGreen(unsigned char val) { setColor(val, &m_nGreen); }
    void setBlue(unsigned char val) { setColor(val, &m_nBlue); }
    void setFont(unsigned nFont);
    void setEncoding(unsigned nFont);
    void setFontName();
    void setFontColor(unsigned short color);
    void setFontBgColor(unsigned short color);
    void setFontSizeHalfPoints(unsigned short sizeInHalfPoints);
    void setFontSize(unsigned short sizeInPoints);
    void setBold(bool);
    void setItalic(bool);
    void setUnderline(bool);
    void startParagraph();
    bool isParagraphOpen() const;
    void clearParagraphFormatting();
    void setParagraphDirLTR();
    void setParagraphDirRTL();
    void addLineBreak();
    void flush();
    void reset();
    void resetTag(TagEnum tag);
protected:
    string text;
    void Init();
    RTF2HTML *p;
    void resetColors() { m_nRed = m_nGreen = m_nBlue = 0; m_bColorInit = false; }
    void setColor(unsigned char val, unsigned char *p)
    { *p = val; m_bColorInit=true; }

    // Marks the position in m_tags where this level begun.
    unsigned m_nTagsStartPos;

    // True when parsing the fonts table
    bool m_bFontTbl;
    // True when parsing the colors table.
    bool m_bColors;
    // True when inside a 'fname' block.
    bool m_bFontName;
    // False until we get the tagged font name.
    bool m_bTaggedFontNameOk;

    unsigned char m_nRed;
    unsigned char m_nGreen;
    unsigned char m_nBlue;
    bool m_bColorInit;
    unsigned m_nFont; // 1-based
    unsigned m_nEncoding;
    unsigned m_nFontColor; // 1-based
    unsigned m_nFontSize;
    unsigned m_nFontBgColor; // 1-based
    bool m_bBold;
    bool m_bItalic;
    bool m_bUnderline;
};

class OutTag
{
public:
    OutTag(TagEnum _tag, unsigned _param) : tag(_tag), param(_param) {}
    TagEnum tag;
    unsigned param;
};

enum quoteMode
{
    quoteHTML,
    quoteXML,
    quoteNOBR
};

class RTF2HTML
{
    friend class Level;

public:
    RTF2HTML();
    TQString Parse(const char *rtf, const char *encoding);

    // Paragraph-specific functions:

    TQString quoteString(const TQString &_str, quoteMode mode = quoteHTML);
    // Appends a string with formatting into the paragraph buffer.
    void PrintUnquoted(const char *str, ...);
    // Quotes and appends a string to the paragraph buffer.
    void PrintQuoted(const TQString &str);
    // Writes down the tags from oTags into the paragraph buffer.
    void FlushOutTags();
    // Retrieves the top not-yet-written tag.
    OutTag* getTopOutTag(TagEnum tagType);
    // Writes down the paragraph buffer and resets the paragraph state.
    void FlushParagraph();

    // Document-wide functions:

    void PutTag(TagEnum n)
    {
       tags.push(n);
    }

protected:

// Paragraph members

    // True if the paragraph was opened explicitly.
    bool bExplicitParagraph;
    // The paragraph's HTML buffer.
    TQString sParagraph;
    // Defines the paragraph's formatting.
    ParStyle parStyle;
    // Tags which weren't yet printed out.
    vector<OutTag> oTags;

// Document members

    // The document HTML buffer.
    TQString s;
    // Fonts table.
    vector<FontDef> fonts;
    // Colors table.
    vector<TQColor> colors;
    // Stack of tags (across all levels, not just current level)
    stack<TagEnum> tags;

// RTF parser internals

    const char *rtf_ptr;
    const char *encoding;
    Level cur_level;
    stack<Level> levels;
};

#endif