]> git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
implemented writing in original encoding
[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 && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/intl.h"
26 #include "wx/dc.h"
27 #endif
28
29 #include "wx/html/htmldefs.h"
30 #include "wx/html/winpars.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/fontmap.h"
33 #include "wx/log.h"
34
35
36 //-----------------------------------------------------------------------------
37 // wxHtmlWinParser
38 //-----------------------------------------------------------------------------
39
40
41 wxList wxHtmlWinParser::m_Modules;
42
43 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow *wnd) : wxHtmlParser()
44 {
45 m_tmpStrBuf = NULL;
46 m_tmpStrBufSize = 0;
47 m_Window = wnd;
48 m_Container = NULL;
49 m_DC = NULL;
50 m_CharHeight = m_CharWidth = 0;
51 m_UseLink = FALSE;
52 m_EncConv = NULL;
53 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
54
55 {
56 int i, j, k, l, m;
57 for (i = 0; i < 2; i++)
58 for (j = 0; j < 2; j++)
59 for (k = 0; k < 2; k++)
60 for (l = 0; l < 2; l++)
61 for (m = 0; m < 7; m++)
62 {
63 m_FontsTable[i][j][k][l][m] = NULL;
64 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
65 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
66 }
67 #ifdef __WXMSW__
68 static int default_sizes[7] = {7, 8, 10, 12, 16, 22, 30};
69 #elif defined(__WXMAC__)
70 static int default_sizes[7] = {9, 12, 14, 18, 24, 30, 36};
71 #else
72 static int default_sizes[7] = {10, 12, 14, 16, 19, 24, 32};
73 #endif
74 SetFonts("", "", default_sizes);
75 }
76
77 // fill in wxHtmlParser's tables:
78 wxNode *node = m_Modules.GetFirst();
79 while (node)
80 {
81 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData();
82 mod->FillHandlersTable(this);
83 node = node->GetNext();
84 }
85 }
86
87 wxHtmlWinParser::~wxHtmlWinParser()
88 {
89 int i, j, k, l, m;
90
91 for (i = 0; i < 2; i++)
92 for (j = 0; j < 2; j++)
93 for (k = 0; k < 2; k++)
94 for (l = 0; l < 2; l++)
95 for (m = 0; m < 7; m++)
96 {
97 if (m_FontsTable[i][j][k][l][m] != NULL)
98 delete m_FontsTable[i][j][k][l][m];
99 }
100 delete m_EncConv;
101 delete[] m_tmpStrBuf;
102 }
103
104 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
105 {
106 m_Modules.Append(module);
107 }
108
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module)
110 {
111 m_Modules.DeleteObject(module);
112 }
113
114 void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
115 {
116 int i, j, k, l, m;
117
118 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
119 m_FontFaceFixed = fixed_face;
120 m_FontFaceNormal = normal_face;
121
122 SetInputEncoding(m_InputEnc);
123
124 for (i = 0; i < 2; i++)
125 for (j = 0; j < 2; j++)
126 for (k = 0; k < 2; k++)
127 for (l = 0; l < 2; l++)
128 for (m = 0; m < 7; m++) {
129 if (m_FontsTable[i][j][k][l][m] != NULL)
130 {
131 delete m_FontsTable[i][j][k][l][m];
132 m_FontsTable[i][j][k][l][m] = NULL;
133 }
134 }
135 }
136
137 void wxHtmlWinParser::InitParser(const wxString& source)
138 {
139 wxHtmlParser::InitParser(source);
140 wxASSERT_MSG(m_DC != NULL, _T("no DC assigned to wxHtmlWinParser!!"));
141
142 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
143 m_FontSize = 3; //default one
144 CreateCurrentFont(); // we're selecting default font into
145 m_DC->GetTextExtent("H", &m_CharWidth, &m_CharHeight);
146 /* NOTE : we're not using GetCharWidth/Height() because
147 of differences under X and win
148 */
149
150 m_UseLink = FALSE;
151 m_Link = wxHtmlLinkInfo("", "");
152 m_LinkColor.Set(0, 0, 0xFF);
153 m_ActualColor.Set(0, 0, 0);
154 m_Align = wxHTML_ALIGN_LEFT;
155 m_tmpLastWasSpace = FALSE;
156
157 OpenContainer();
158
159 OpenContainer();
160 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
161 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
162 }
163
164 void wxHtmlWinParser::DoneParser()
165 {
166 m_Container = NULL;
167 SetInputEncoding(wxFONTENCODING_DEFAULT); // for next call
168 wxHtmlParser::DoneParser();
169 }
170
171 wxObject* wxHtmlWinParser::GetProduct()
172 {
173 wxHtmlContainerCell *top;
174
175 CloseContainer();
176 OpenContainer();
177
178 top = m_Container;
179 while (top->GetParent()) top = top->GetParent();
180 return top;
181 }
182
183 wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
184 const wxString& url) const
185 {
186 // FIXME - normalize the URL to full path before passing to
187 // OnOpeningURL!!
188 if ( m_Window )
189 {
190 wxString redirect;
191 wxString myurl(url);
192 wxHtmlOpeningStatus status;
193 for (;;)
194 {
195 if ( m_Window->OnOpeningURL(type, myurl, &redirect) == wxHTML_REDIRECT )
196 myurl = redirect;
197 else
198 break;
199 }
200
201 if ( status == wxHTML_BLOCK )
202 return NULL;
203 else
204 return GetFS()->OpenFile(myurl);
205 }
206 else
207 return wxHtmlParser::OpenURL(type, url);
208 }
209
210 void wxHtmlWinParser::AddText(const wxChar* txt)
211 {
212 wxHtmlCell *c;
213 size_t i = 0,
214 x,
215 lng = wxStrlen(txt);
216 register wxChar d;
217 int templen = 0;
218 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
219
220 if (lng+1 > m_tmpStrBufSize)
221 {
222 delete[] m_tmpStrBuf;
223 m_tmpStrBuf = new wxChar[lng+1];
224 m_tmpStrBufSize = lng+1;
225 }
226 wxChar *temp = m_tmpStrBuf;
227
228 if (m_tmpLastWasSpace)
229 {
230 while ((i < lng) &&
231 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
232 (txt[i] == wxT('\t')))) i++;
233 }
234
235 while (i < lng)
236 {
237 x = 0;
238 d = temp[templen++] = txt[i];
239 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
240 {
241 i++, x++;
242 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
243 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
244 }
245 else i++;
246
247 if (x)
248 {
249 temp[templen-1] = wxT(' ');
250 temp[templen] = 0;
251 if (templen == 1) continue;
252 templen = 0;
253 if (m_EncConv)
254 m_EncConv->Convert(temp);
255 size_t len = wxStrlen(temp);
256 for (size_t j = 0; j < len; j++)
257 if (temp[j] == nbsp)
258 temp[j] = wxT(' ');
259 c = new wxHtmlWordCell(temp, *(GetDC()));
260 if (m_UseLink)
261 c->SetLink(m_Link);
262 m_Container->InsertCell(c);
263 m_tmpLastWasSpace = TRUE;
264 }
265 }
266
267 if (templen && (templen > 1 || temp[0] != wxT(' ')))
268 {
269 temp[templen] = 0;
270 if (m_EncConv)
271 m_EncConv->Convert(temp);
272 size_t len = wxStrlen(temp);
273 for (size_t j = 0; j < len; j++)
274 if (temp[j] == nbsp)
275 temp[j] = wxT(' ');
276 c = new wxHtmlWordCell(temp, *(GetDC()));
277 if (m_UseLink)
278 c->SetLink(m_Link);
279 m_Container->InsertCell(c);
280 m_tmpLastWasSpace = FALSE;
281 }
282 }
283
284
285
286 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
287 {
288 m_Container = new wxHtmlContainerCell(m_Container);
289 m_Container->SetAlignHor(m_Align);
290 m_tmpLastWasSpace = TRUE;
291 /* to avoid space being first character in paragraph */
292 return m_Container;
293 }
294
295
296
297 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
298 {
299 m_tmpLastWasSpace = TRUE;
300 /* to avoid space being first character in paragraph */
301 return m_Container = c;
302 }
303
304
305
306 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
307 {
308 m_Container = m_Container->GetParent();
309 return m_Container;
310 }
311
312
313 void wxHtmlWinParser::SetFontSize(int s)
314 {
315 if (s < 1) s = 1;
316 else if (s > 7) s = 7;
317 m_FontSize = s;
318 }
319
320
321
322 wxFont* wxHtmlWinParser::CreateCurrentFont()
323 {
324 int fb = GetFontBold(),
325 fi = GetFontItalic(),
326 fu = GetFontUnderlined(),
327 ff = GetFontFixed(),
328 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
329
330 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
331 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
332 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
333 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
334
335 if (*fontptr != NULL && (*faceptr != face || *encptr != m_OutputEnc))
336 {
337 delete *fontptr;
338 *fontptr = NULL;
339 }
340
341 if (*fontptr == NULL)
342 {
343 *faceptr = face;
344 *encptr = m_OutputEnc;
345 *fontptr = new wxFont(
346 (int) (m_FontsSizes[fs] * m_PixelScale),
347 ff ? wxMODERN : wxSWISS,
348 fi ? wxITALIC : wxNORMAL,
349 fb ? wxBOLD : wxNORMAL,
350 fu ? TRUE : FALSE, face,
351 m_OutputEnc);
352 }
353 m_DC->SetFont(**fontptr);
354 return (*fontptr);
355 }
356
357
358
359 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
360 {
361 m_Link = link;
362 m_UseLink = (link.GetHref() != wxEmptyString);
363 }
364
365
366 void wxHtmlWinParser::SetFontFace(const wxString& face)
367 {
368 if (GetFontFixed()) m_FontFaceFixed = face;
369 else m_FontFaceNormal = face;
370
371 if (m_InputEnc != wxFONTENCODING_DEFAULT)
372 SetInputEncoding(m_InputEnc);
373 }
374
375
376
377 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
378 {
379 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
380 if (m_EncConv)
381 {
382 delete m_EncConv;
383 m_EncConv = NULL;
384 }
385
386 if (enc == wxFONTENCODING_DEFAULT) return;
387
388 wxFontEncoding altfix, altnorm;
389 bool availfix, availnorm;
390
391 // exact match?
392 availnorm = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceNormal);
393 availfix = wxTheFontMapper->IsEncodingAvailable(enc, m_FontFaceFixed);
394 if (availnorm && availfix)
395 m_OutputEnc = enc;
396
397 // alternatives?
398 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) &&
399 wxTheFontMapper->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) &&
400 altnorm == altfix)
401 m_OutputEnc = altnorm;
402
403 // at least normal face?
404 else if (availnorm)
405 m_OutputEnc = enc;
406 else if (wxTheFontMapper->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE))
407 m_OutputEnc = altnorm;
408
409 // okay, let convert to ISO_8859-1, available always
410 else
411 m_OutputEnc = wxFONTENCODING_DEFAULT;
412
413 m_InputEnc = enc;
414 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
415 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
416 else
417 GetEntitiesParser()->SetEncoding(m_OutputEnc);
418
419 if (m_InputEnc == m_OutputEnc) return;
420
421 m_EncConv = new wxEncodingConverter();
422 if (!m_EncConv->Init(m_InputEnc,
423 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
424 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
425 wxCONVERT_SUBSTITUTE))
426 { // total failture :-(
427 wxLogError(_("Failed to display HTML document in %s encoding"),
428 wxFontMapper::GetEncodingName(enc).c_str());
429 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
430 delete m_EncConv;
431 m_EncConv = NULL;
432 }
433 }
434
435
436
437
438
439 //-----------------------------------------------------------------------------
440 // wxHtmlWinTagHandler
441 //-----------------------------------------------------------------------------
442
443 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
444
445
446
447 //-----------------------------------------------------------------------------
448 // wxHtmlTagsModule
449 //-----------------------------------------------------------------------------
450
451
452 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
453
454
455 bool wxHtmlTagsModule::OnInit()
456 {
457 wxHtmlWinParser::AddModule(this);
458 return TRUE;
459 }
460
461
462
463 void wxHtmlTagsModule::OnExit()
464 {
465 wxHtmlWinParser::RemoveModule(this);
466 }
467 #endif
468