Compilo.
[wxWidgets.git] / src / html / winpars.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/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 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/dc.h"
21 #include "wx/log.h"
22 #include "wx/settings.h"
23 #endif
24
25 #include "wx/html/htmldefs.h"
26 #include "wx/html/winpars.h"
27 #include "wx/html/htmlwin.h"
28 #include "wx/fontmap.h"
29 #include "wx/uri.h"
30
31
32 //-----------------------------------------------------------------------------
33 // wxHtmlWinParser
34 //-----------------------------------------------------------------------------
35
36 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser, wxHtmlParser)
37
38 wxList wxHtmlWinParser::m_Modules;
39
40 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindowInterface *wndIface)
41 {
42 m_tmpStrBuf = NULL;
43 m_tmpStrBufSize = 0;
44 m_windowInterface = wndIface;
45 m_Container = NULL;
46 m_DC = NULL;
47 m_CharHeight = m_CharWidth = 0;
48 m_UseLink = false;
49 #if !wxUSE_UNICODE
50 m_EncConv = NULL;
51 m_InputEnc = wxFONTENCODING_ISO8859_1;
52 m_OutputEnc = wxFONTENCODING_DEFAULT;
53 #endif
54 m_lastWordCell = NULL;
55
56 {
57 int i, j, k, l, m;
58 for (i = 0; i < 2; i++)
59 for (j = 0; j < 2; j++)
60 for (k = 0; k < 2; k++)
61 for (l = 0; l < 2; l++)
62 for (m = 0; m < 7; m++)
63 {
64 m_FontsTable[i][j][k][l][m] = NULL;
65 m_FontsFacesTable[i][j][k][l][m] = wxEmptyString;
66 #if !wxUSE_UNICODE
67 m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT;
68 #endif
69 }
70
71 SetFonts(wxEmptyString, wxEmptyString, NULL);
72 }
73
74 // fill in wxHtmlParser's tables:
75 wxList::compatibility_iterator node = m_Modules.GetFirst();
76 while (node)
77 {
78 wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData();
79 mod->FillHandlersTable(this);
80 node = node->GetNext();
81 }
82 }
83
84 wxHtmlWinParser::~wxHtmlWinParser()
85 {
86 int i, j, k, l, m;
87
88 for (i = 0; i < 2; i++)
89 for (j = 0; j < 2; j++)
90 for (k = 0; k < 2; k++)
91 for (l = 0; l < 2; l++)
92 for (m = 0; m < 7; m++)
93 {
94 if (m_FontsTable[i][j][k][l][m] != NULL)
95 delete m_FontsTable[i][j][k][l][m];
96 }
97 #if !wxUSE_UNICODE
98 delete m_EncConv;
99 #endif
100 delete[] m_tmpStrBuf;
101 }
102
103 void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module)
104 {
105 m_Modules.Append(module);
106 }
107
108 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module)
109 {
110 m_Modules.DeleteObject(module);
111 }
112
113 // build all HTML font sizes (1..7) from the given base size
114 static void wxBuildFontSizes(int *sizes, int size)
115 {
116 // using a fixed factor (1.2, from CSS2) is a bad idea as explained at
117 // http://www.w3.org/TR/CSS21/fonts.html#font-size-props but this is by far
118 // simplest thing to do so still do it like this for now
119 sizes[0] = int(size * 0.75); // exception to 1.2 rule, otherwise too small
120 sizes[1] = int(size * 0.83);
121 sizes[2] = size;
122 sizes[3] = int(size * 1.2);
123 sizes[4] = int(size * 1.44);
124 sizes[5] = int(size * 1.73);
125 sizes[6] = int(size * 2);
126 }
127
128 static int wxGetDefaultHTMLFontSize()
129 {
130 // base the default font size on the size of the default system font but
131 // also ensure that we have a font of reasonable size, otherwise small HTML
132 // fonts are unreadable
133 int size = wxNORMAL_FONT->GetPointSize();
134 if ( size < 10 )
135 size = 10;
136 return size;
137 }
138
139 void wxHtmlWinParser::SetFonts(const wxString& normal_face,
140 const wxString& fixed_face,
141 const int *sizes)
142 {
143 static int default_sizes[7] = { 0 };
144 if ( !sizes )
145 {
146 if ( !default_sizes[0] )
147 wxBuildFontSizes(default_sizes, wxGetDefaultHTMLFontSize());
148
149 sizes = default_sizes;
150 }
151
152 int i, j, k, l, m;
153
154 for (i = 0; i < 7; i++)
155 m_FontsSizes[i] = sizes[i];
156
157 m_FontFaceFixed = fixed_face;
158 m_FontFaceNormal = normal_face;
159
160 #if !wxUSE_UNICODE
161 SetInputEncoding(m_InputEnc);
162 #endif
163
164 for (i = 0; i < 2; i++)
165 for (j = 0; j < 2; j++)
166 for (k = 0; k < 2; k++)
167 for (l = 0; l < 2; l++)
168 for (m = 0; m < 7; m++) {
169 if (m_FontsTable[i][j][k][l][m] != NULL)
170 {
171 delete m_FontsTable[i][j][k][l][m];
172 m_FontsTable[i][j][k][l][m] = NULL;
173 }
174 }
175 }
176
177 void wxHtmlWinParser::SetStandardFonts(int size,
178 const wxString& normal_face,
179 const wxString& fixed_face)
180 {
181 if (size == -1)
182 size = wxGetDefaultHTMLFontSize();
183
184 int f_sizes[7];
185 wxBuildFontSizes(f_sizes, size);
186
187 wxString normal = normal_face;
188 if ( normal.empty() )
189 normal = wxNORMAL_FONT->GetFaceName();
190
191 SetFonts(normal, fixed_face, f_sizes);
192 }
193
194 void wxHtmlWinParser::InitParser(const wxString& source)
195 {
196 wxHtmlParser::InitParser(source);
197 wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!"));
198
199 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
200 m_FontSize = 3; //default one
201 CreateCurrentFont(); // we're selecting default font into
202
203 // we're not using GetCharWidth/Height() because of
204 // differences under X and win
205 wxCoord w,h;
206 m_DC->GetTextExtent( wxT("H"), &w, &h);
207 m_CharWidth = w;
208 m_CharHeight = h;
209
210 m_UseLink = false;
211 m_Link = wxHtmlLinkInfo( wxEmptyString );
212 m_LinkColor.Set(0, 0, 0xFF);
213 m_ActualColor.Set(0, 0, 0);
214 m_Align = wxHTML_ALIGN_LEFT;
215 m_ScriptMode = wxHTML_SCRIPT_NORMAL;
216 m_ScriptBaseline = 0;
217 m_tmpLastWasSpace = false;
218 m_lastWordCell = NULL;
219
220 // open the toplevel container that contains everything else and that
221 // is never closed (this makes parser's life easier):
222 OpenContainer();
223
224 // then open the first container into which page's content will go:
225 OpenContainer();
226
227 #if !wxUSE_UNICODE
228 wxString charset = ExtractCharsetInformation(source);
229 if (!charset.empty())
230 {
231 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset);
232 if (enc != wxFONTENCODING_SYSTEM)
233 SetInputEncoding(enc);
234 }
235 #endif
236
237 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
238 wxColour windowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ;
239
240 m_Container->InsertCell
241 (
242 new wxHtmlColourCell
243 (
244 m_windowInterface
245 ? m_windowInterface->GetHTMLBackgroundColour()
246 : windowColour,
247 wxHTML_CLR_BACKGROUND
248 )
249 );
250
251 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
252 }
253
254 void wxHtmlWinParser::DoneParser()
255 {
256 m_Container = NULL;
257 #if !wxUSE_UNICODE
258 SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call
259 #endif
260 wxHtmlParser::DoneParser();
261 }
262
263 #if WXWIN_COMPATIBILITY_2_6
264 wxHtmlWindow *wxHtmlWinParser::GetWindow()
265 {
266 if (!m_windowInterface)
267 return NULL;
268 return wxDynamicCast(m_windowInterface->GetHTMLWindow(), wxHtmlWindow);
269 }
270 #endif
271
272 wxObject* wxHtmlWinParser::GetProduct()
273 {
274 wxHtmlContainerCell *top;
275
276 CloseContainer();
277 OpenContainer();
278
279 top = m_Container;
280 while (top->GetParent()) top = top->GetParent();
281 top->RemoveExtraSpacing(true, true);
282
283 return top;
284 }
285
286 wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
287 const wxString& url) const
288 {
289 if ( !m_windowInterface )
290 return wxHtmlParser::OpenURL(type, url);
291
292 wxString myurl(url);
293 wxHtmlOpeningStatus status;
294 for (;;)
295 {
296 wxString myfullurl(myurl);
297
298 // consider url as absolute path first
299 wxURI current(myurl);
300 myfullurl = current.BuildUnescapedURI();
301
302 // if not absolute then ...
303 if( current.IsReference() )
304 {
305 wxString basepath = GetFS()->GetPath();
306 wxURI base(basepath);
307
308 // ... try to apply base path if valid ...
309 if( !base.IsReference() )
310 {
311 wxURI path(myfullurl);
312 path.Resolve( base );
313 myfullurl = path.BuildUnescapedURI();
314 }
315 else
316 {
317 // ... or force such addition if not included already
318 if( !current.GetPath().Contains(base.GetPath()) )
319 {
320 basepath += myurl;
321 wxURI connected( basepath );
322 myfullurl = connected.BuildUnescapedURI();
323 }
324 }
325 }
326
327 wxString redirect;
328 status = m_windowInterface->OnHTMLOpeningURL(type, myfullurl, &redirect);
329 if ( status != wxHTML_REDIRECT )
330 break;
331
332 myurl = redirect;
333 }
334
335 if ( status == wxHTML_BLOCK )
336 return NULL;
337
338 int flags = wxFS_READ;
339 if (type == wxHTML_URL_IMAGE)
340 flags |= wxFS_SEEKABLE;
341
342 return GetFS()->OpenFile(myurl, flags);
343 }
344
345 void wxHtmlWinParser::AddText(const wxChar* txt)
346 {
347 size_t i = 0,
348 x,
349 lng = wxStrlen(txt);
350 register wxChar d;
351 int templen = 0;
352 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
353
354 if (lng+1 > m_tmpStrBufSize)
355 {
356 delete[] m_tmpStrBuf;
357 m_tmpStrBuf = new wxChar[lng+1];
358 m_tmpStrBufSize = lng+1;
359 }
360 wxChar *temp = m_tmpStrBuf;
361
362 if (m_tmpLastWasSpace)
363 {
364 while ((i < lng) &&
365 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
366 (txt[i] == wxT('\t')))) i++;
367 }
368
369 while (i < lng)
370 {
371 x = 0;
372 d = temp[templen++] = txt[i];
373 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
374 {
375 i++, x++;
376 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
377 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
378 }
379 else i++;
380
381 if (x)
382 {
383 temp[templen-1] = wxT(' ');
384 DoAddText(temp, templen, nbsp);
385 m_tmpLastWasSpace = true;
386 }
387 }
388
389 if (templen && (templen > 1 || temp[0] != wxT(' ')))
390 {
391 DoAddText(temp, templen, nbsp);
392 m_tmpLastWasSpace = false;
393 }
394 }
395
396 void wxHtmlWinParser::DoAddText(wxChar *temp, int& templen, wxChar nbsp)
397 {
398 temp[templen] = 0;
399 templen = 0;
400 #if !wxUSE_UNICODE
401 if (m_EncConv)
402 m_EncConv->Convert(temp);
403 #endif
404 size_t len = wxStrlen(temp);
405 for (size_t j = 0; j < len; j++)
406 {
407 if (temp[j] == nbsp)
408 temp[j] = wxT(' ');
409 }
410
411 wxHtmlCell *c = new wxHtmlWordCell(temp, *(GetDC()));
412
413 ApplyStateToCell(c);
414
415 m_Container->InsertCell(c);
416 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
417 m_lastWordCell = (wxHtmlWordCell*)c;
418 }
419
420
421
422 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
423 {
424 m_Container = new wxHtmlContainerCell(m_Container);
425 m_Container->SetAlignHor(m_Align);
426 m_tmpLastWasSpace = true;
427 /* to avoid space being first character in paragraph */
428 return m_Container;
429 }
430
431
432
433 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
434 {
435 m_tmpLastWasSpace = true;
436 /* to avoid space being first character in paragraph */
437 return m_Container = c;
438 }
439
440
441
442 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
443 {
444 m_Container = m_Container->GetParent();
445 return m_Container;
446 }
447
448
449 void wxHtmlWinParser::SetFontSize(int s)
450 {
451 if (s < 1) s = 1;
452 else if (s > 7) s = 7;
453 m_FontSize = s;
454 }
455
456
457
458 wxFont* wxHtmlWinParser::CreateCurrentFont()
459 {
460 int fb = GetFontBold(),
461 fi = GetFontItalic(),
462 fu = GetFontUnderlined(),
463 ff = GetFontFixed(),
464 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
465
466 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
467 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
468 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
469 #if !wxUSE_UNICODE
470 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
471 #endif
472
473 if (*fontptr != NULL && (*faceptr != face
474 #if !wxUSE_UNICODE
475 || *encptr != m_OutputEnc
476 #endif
477 ))
478 {
479 delete *fontptr;
480 *fontptr = NULL;
481 }
482
483 if (*fontptr == NULL)
484 {
485 *faceptr = face;
486 *fontptr = new wxFont(
487 (int) (m_FontsSizes[fs] * m_PixelScale),
488 ff ? wxMODERN : wxSWISS,
489 fi ? wxITALIC : wxNORMAL,
490 fb ? wxBOLD : wxNORMAL,
491 fu ? true : false, face
492 #if wxUSE_UNICODE
493 );
494 #else
495 , m_OutputEnc);
496 *encptr = m_OutputEnc;
497 #endif
498 }
499 m_DC->SetFont(**fontptr);
500 return (*fontptr);
501 }
502
503
504
505 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
506 {
507 m_Link = link;
508 m_UseLink = (link.GetHref() != wxEmptyString);
509 }
510
511 void wxHtmlWinParser::SetFontFace(const wxString& face)
512 {
513 if (GetFontFixed()) m_FontFaceFixed = face;
514 else m_FontFaceNormal = face;
515
516 #if !wxUSE_UNICODE
517 if (m_InputEnc != wxFONTENCODING_DEFAULT)
518 SetInputEncoding(m_InputEnc);
519 #endif
520 }
521
522 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell *cell)
523 {
524 // set the link:
525 if (m_UseLink)
526 cell->SetLink(GetLink());
527
528 // apply current script mode settings:
529 cell->SetScriptMode(GetScriptMode(), GetScriptBaseline());
530 }
531
532
533 #if !wxUSE_UNICODE
534 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
535 {
536 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
537 if (m_EncConv)
538 {
539 delete m_EncConv;
540 m_EncConv = NULL;
541 }
542
543 if (enc == wxFONTENCODING_DEFAULT) return;
544
545 wxFontEncoding altfix, altnorm;
546 bool availfix, availnorm;
547
548 // exact match?
549 availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal);
550 availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed);
551 if (availnorm && availfix)
552 m_OutputEnc = enc;
553
554 // alternatives?
555 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) &&
556 wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) &&
557 altnorm == altfix)
558 m_OutputEnc = altnorm;
559
560 // at least normal face?
561 else if (availnorm)
562 m_OutputEnc = enc;
563 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false))
564 m_OutputEnc = altnorm;
565
566 else
567 {
568 #ifndef __WXMAC__
569 // okay, let's convert to ISO_8859-1, available always
570 m_OutputEnc = wxFONTENCODING_DEFAULT;
571 #else
572 m_OutputEnc = wxLocale::GetSystemEncoding() ;
573 #endif
574 }
575
576 m_InputEnc = enc;
577 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
578 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
579 else
580 GetEntitiesParser()->SetEncoding(m_OutputEnc);
581
582 if (m_InputEnc == m_OutputEnc) return;
583
584 m_EncConv = new wxEncodingConverter();
585 if (!m_EncConv->Init(m_InputEnc,
586 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
587 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
588 wxCONVERT_SUBSTITUTE))
589 { // total failure :-(
590 wxLogError(_("Failed to display HTML document in %s encoding"),
591 wxFontMapper::GetEncodingName(enc).c_str());
592 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
593 delete m_EncConv;
594 m_EncConv = NULL;
595 }
596 }
597 #endif
598
599
600
601
602 //-----------------------------------------------------------------------------
603 // wxHtmlWinTagHandler
604 //-----------------------------------------------------------------------------
605
606 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
607
608 //-----------------------------------------------------------------------------
609 // wxHtmlTagsModule
610 //-----------------------------------------------------------------------------
611
612 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
613 // This module is an ancestor for tag handlers modules defined
614 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
615 //
616 // Do not add any winpars.cpp shutdown or initialization code to it,
617 // create a new module instead!
618
619 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
620
621 bool wxHtmlTagsModule::OnInit()
622 {
623 wxHtmlWinParser::AddModule(this);
624 return true;
625 }
626
627 void wxHtmlTagsModule::OnExit()
628 {
629 wxHtmlWinParser::RemoveModule(this);
630 }
631
632 #endif