added encoding conversion to wxHTML
[wxWidgets.git] / src / html / winpars.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: winpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmldefs.h"
29 #include "wx/html/winpars.h"
30 #include "wx/html/htmlwin.h"
31 #include "wx/fontmap.h"
32
33
34 //-----------------------------------------------------------------------------
35 // wxHtmlWinParser
36 //-----------------------------------------------------------------------------
37
38
39 wxList wxHtmlWinParser::m_Modules;
40
41 wxHtmlWinParser::wxHtmlWinParser(wxWindow *wnd) : wxHtmlParser()
42 {
43 m_Window = wnd;
44 m_Container = NULL;
45 m_DC = NULL;
46 m_CharHeight = m_CharWidth = 0;
47 m_UseLink = FALSE;
48 m_EncConv = NULL;
49 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
50
51 {
52 int i, j, k, l, m;
53 for (i = 0; i < 2; i++)
54 for (j = 0; j < 2; j++)
55 for (k = 0; k < 2; k++)
56 for (l = 0; l < 2; l++)
57 for (m = 0; m < 7; m++) {
58 m_FontsTable[i][j][k][l][m] = NULL;
59 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
60 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
61 }
62 #ifdef __WXMSW__
63 static int default_sizes[7] = {7, 8, 10, 12, 16, 22, 30};
64 #else
65 static int default_sizes[7] = {10, 12, 14, 16, 19, 24, 32};
66 #endif
67 SetFonts("", "", default_sizes);
68 }
69
70 // fill in wxHtmlParser's tables:
71 wxNode *node = m_Modules.GetFirst();
72 while (node) {
73 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node -> GetData();
74 mod -> FillHandlersTable(this);
75 node = node -> GetNext();
76 }
77 }
78
79
80 wxHtmlWinParser::~wxHtmlWinParser()
81 {
82 int i, j, k, l, m;
83
84 for (i = 0; i < 2; i++)
85 for (j = 0; j < 2; j++)
86 for (k = 0; k < 2; k++)
87 for (l = 0; l < 2; l++)
88 for (m = 0; m < 7; m++) {
89 if (m_FontsTable[i][j][k][l][m] != NULL)
90 delete m_FontsTable[i][j][k][l][m];
91 }
92 if (m_EncConv) delete m_EncConv;
93 }
94
95
96 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
97 {
98 m_Modules.Append(module);
99 }
100
101
102
103 void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
104 {
105 int i, j, k, l, m;
106
107 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
108 m_FontFaceFixed = fixed_face;
109 m_FontFaceNormal = normal_face;
110
111 SetInputEncoding(m_InputEnc);
112
113 for (i = 0; i < 2; i++)
114 for (j = 0; j < 2; j++)
115 for (k = 0; k < 2; k++)
116 for (l = 0; l < 2; l++)
117 for (m = 0; m < 7; m++) {
118 if (m_FontsTable[i][j][k][l][m] != NULL) {
119 delete m_FontsTable[i][j][k][l][m];
120 m_FontsTable[i][j][k][l][m] = NULL;
121 }
122 }
123 }
124
125
126
127 void wxHtmlWinParser::InitParser(const wxString& source)
128 {
129 wxHtmlParser::InitParser(source);
130 wxASSERT_MSG(m_DC != NULL, _("no DC assigned to wxHtmlWinParser!!"));
131
132 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
133 m_FontSize = 3; //default one
134 CreateCurrentFont(); // we're selecting default font into
135 m_DC -> GetTextExtent("H", &m_CharWidth, &m_CharHeight);
136 /* NOTE : we're not using GetCharWidth/Height() because
137 of differences under X and win
138 */
139
140 m_UseLink = FALSE;
141 m_Link = wxHtmlLinkInfo("", "");
142 m_LinkColor.Set(0, 0, 0xFF);
143 m_ActualColor.Set(0, 0, 0);
144 m_Align = wxHTML_ALIGN_LEFT;
145 m_tmpLastWasSpace = FALSE;
146
147 OpenContainer();
148
149 OpenContainer();
150 m_Container -> InsertCell(new wxHtmlColourCell(m_ActualColor));
151 m_Container -> InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
152 }
153
154
155
156 void wxHtmlWinParser::DoneParser()
157 {
158 m_Container = NULL;
159 SetInputEncoding(wxFONTENCODING_DEFAULT); // for next call
160 wxHtmlParser::DoneParser();
161 }
162
163
164
165 wxObject* wxHtmlWinParser::GetProduct()
166 {
167 wxHtmlContainerCell *top;
168
169 CloseContainer();
170 OpenContainer();
171
172 top = m_Container;
173 while (top -> GetParent()) top = top -> GetParent();
174 return top;
175 }
176
177
178
179 void wxHtmlWinParser::AddText(const char* txt)
180 {
181 wxHtmlCell *c;
182 int i = 0, x, lng = strlen(txt);
183 char temp[wxHTML_BUFLEN];
184 register char d;
185 int templen = 0;
186
187 if (m_tmpLastWasSpace) {
188 while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++;
189 }
190
191 while (i < lng) {
192 x = 0;
193 d = temp[templen++] = txt[i];
194 if ((d == '\n') || (d == '\r') || (d == ' ') || (d == '\t')) {
195 i++, x++;
196 while ((i < lng) && ((txt[i] == '\n') || (txt[i] == '\r') || (txt[i] == ' ') || (txt[i] == '\t'))) i++, x++;
197 }
198 else i++;
199
200 if (x) {
201 temp[templen-1] = ' ';
202 temp[templen] = 0;
203 templen = 0;
204 if (m_EncConv) m_EncConv -> Convert(temp);
205 c = new wxHtmlWordCell(temp, *(GetDC()));
206 if (m_UseLink) c -> SetLink(m_Link);
207 m_Container -> InsertCell(c);
208 m_tmpLastWasSpace = TRUE;
209 }
210 }
211 if (templen) {
212 temp[templen] = 0;
213 if (m_EncConv) m_EncConv -> Convert(temp);
214 c = new wxHtmlWordCell(temp, *(GetDC()));
215 if (m_UseLink) c -> SetLink(m_Link);
216 m_Container -> InsertCell(c);
217 m_tmpLastWasSpace = FALSE;
218 }
219 }
220
221
222
223 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
224 {
225 m_Container = new wxHtmlContainerCell(m_Container);
226 m_Container -> SetAlignHor(m_Align);
227 m_tmpLastWasSpace = TRUE;
228 /* to avoid space being first character in paragraph */
229 return m_Container;
230 }
231
232
233
234 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
235 {
236 m_tmpLastWasSpace = TRUE;
237 /* to avoid space being first character in paragraph */
238 return m_Container = c;
239 }
240
241
242
243 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
244 {
245 m_Container = m_Container -> GetParent();
246 return m_Container;
247 }
248
249
250 void wxHtmlWinParser::SetFontSize(int s)
251 {
252 if (s < 1) s = 1;
253 else if (s > 7) s = 7;
254 m_FontSize = s;
255 }
256
257
258
259 wxFont* wxHtmlWinParser::CreateCurrentFont()
260 {
261 int fb = GetFontBold(),
262 fi = GetFontItalic(),
263 fu = GetFontUnderlined(),
264 ff = GetFontFixed(),
265 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
266
267 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
268 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
269 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
270 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
271
272 if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc)) {
273 delete *fontptr;
274 *fontptr = NULL;
275 }
276
277 if (*fontptr == NULL) {
278 *faceptr = face;
279 *encptr = m_OutputEnc;
280 *fontptr = new wxFont(
281 m_FontsSizes[fs] * m_PixelScale,
282 ff ? wxMODERN : wxSWISS,
283 fi ? wxITALIC : wxNORMAL,
284 fb ? wxBOLD : wxNORMAL,
285 fu ? TRUE : FALSE, face,
286 m_OutputEnc);
287 }
288 m_DC -> SetFont(**fontptr);
289 return (*fontptr);
290 }
291
292
293
294 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
295 {
296 m_Link = link;
297 m_UseLink = (link.GetHref() != wxEmptyString);
298 }
299
300
301 void wxHtmlWinParser::SetFontFace(const wxString& face)
302 {
303 if (GetFontFixed()) m_FontFaceFixed = face;
304 else m_FontFaceNormal = face;
305
306 if (m_InputEnc != wxFONTENCODING_DEFAULT)
307 SetInputEncoding(m_InputEnc);
308 }
309
310
311
312 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
313 {
314 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
315 if (m_EncConv) {delete m_EncConv; m_EncConv = NULL;}
316
317 if (enc == wxFONTENCODING_DEFAULT) return;
318
319 wxFontEncoding altfix, altnorm;
320 bool availfix, availnorm;
321
322 // exact match?
323 availnorm = wxTheFontMapper -> IsEncodingAvailable(enc, m_FontFaceNormal);
324 availfix = wxTheFontMapper -> IsEncodingAvailable(enc, m_FontFaceFixed);
325 if (availnorm && availfix)
326 m_OutputEnc = enc;
327
328 // alternatives?
329 else if (wxTheFontMapper -> GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) &&
330 wxTheFontMapper -> GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) &&
331 altnorm == altfix)
332 m_OutputEnc = altnorm;
333
334 // at least normal face?
335 else if (availnorm)
336 m_OutputEnc = enc;
337 else if (wxTheFontMapper -> GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE))
338 m_OutputEnc = altnorm;
339
340 // okay, let convert to ISO_8859-1, available always
341 else
342 m_OutputEnc = wxFONTENCODING_DEFAULT;
343
344 m_InputEnc = enc;
345
346 if (m_InputEnc == m_OutputEnc) return;
347
348 m_EncConv = new wxEncodingConverter();
349 if (!m_EncConv -> Init(m_InputEnc,
350 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
351 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
352 wxCONVERT_SUBSTITUTE))
353 { // total failture :-(
354 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
355 delete m_EncConv;
356 m_EncConv = NULL;
357 }
358 }
359
360
361
362
363
364 //-----------------------------------------------------------------------------
365 // wxHtmlWinTagHandler
366 //-----------------------------------------------------------------------------
367
368 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
369
370
371
372 //-----------------------------------------------------------------------------
373 // wxHtmlTagsModule
374 //-----------------------------------------------------------------------------
375
376
377 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
378
379
380 bool wxHtmlTagsModule::OnInit()
381 {
382 wxHtmlWinParser::AddModule(this);
383 return TRUE;
384 }
385
386
387
388 void wxHtmlTagsModule::OnExit()
389 {
390 }
391 #endif
392