]> git.saurik.com Git - wxWidgets.git/blob - src/html/winpars.cpp
Reenable inversion code again.
[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 WXPRECOMP
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 void wxHtmlWinParser::SetFonts(const wxString& normal_face, const wxString& fixed_face,
114 const int *sizes)
115 {
116 static int default_sizes[7] =
117 {
118 wxHTML_FONT_SIZE_1,
119 wxHTML_FONT_SIZE_2,
120 wxHTML_FONT_SIZE_3,
121 wxHTML_FONT_SIZE_4,
122 wxHTML_FONT_SIZE_5,
123 wxHTML_FONT_SIZE_6,
124 wxHTML_FONT_SIZE_7
125 };
126
127 if (sizes == NULL) sizes = default_sizes;
128
129 int i, j, k, l, m;
130
131 for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
132 m_FontFaceFixed = fixed_face;
133 m_FontFaceNormal = normal_face;
134
135 #if !wxUSE_UNICODE
136 SetInputEncoding(m_InputEnc);
137 #endif
138
139 for (i = 0; i < 2; i++)
140 for (j = 0; j < 2; j++)
141 for (k = 0; k < 2; k++)
142 for (l = 0; l < 2; l++)
143 for (m = 0; m < 7; m++) {
144 if (m_FontsTable[i][j][k][l][m] != NULL)
145 {
146 delete m_FontsTable[i][j][k][l][m];
147 m_FontsTable[i][j][k][l][m] = NULL;
148 }
149 }
150 }
151
152 void wxHtmlWinParser::SetStandardFonts(int size,
153 const wxString& normal_face,
154 const wxString& fixed_face)
155 {
156 wxFont defaultFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
157
158 int f_sizes[7];
159 if (size == -1)
160 size = defaultFont.GetPointSize();
161
162 f_sizes[0] = int(size * 0.6);
163 f_sizes[1] = int(size * 0.8);
164 f_sizes[2] = size;
165 f_sizes[3] = int(size * 1.2);
166 f_sizes[4] = int(size * 1.4);
167 f_sizes[5] = int(size * 1.6);
168 f_sizes[6] = int(size * 1.8);
169
170 wxString normal = normal_face.empty() ?
171 defaultFont.GetFaceName() : normal_face;
172
173 SetFonts(normal, fixed_face, f_sizes);
174 }
175
176 void wxHtmlWinParser::InitParser(const wxString& source)
177 {
178 wxHtmlParser::InitParser(source);
179 wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!"));
180
181 m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE;
182 m_FontSize = 3; //default one
183 CreateCurrentFont(); // we're selecting default font into
184 m_DC->GetTextExtent( wxT("H"), &m_CharWidth, &m_CharHeight);
185 /* NOTE : we're not using GetCharWidth/Height() because
186 of differences under X and win
187 */
188
189 m_UseLink = false;
190 m_Link = wxHtmlLinkInfo( wxEmptyString );
191 m_LinkColor.Set(0, 0, 0xFF);
192 m_ActualColor.Set(0, 0, 0);
193 m_Align = wxHTML_ALIGN_LEFT;
194 m_ScriptMode = wxHTML_SCRIPT_NORMAL;
195 m_ScriptBaseline = 0;
196 m_tmpLastWasSpace = false;
197 m_lastWordCell = NULL;
198
199 // open the toplevel container that contains everything else and that
200 // is never closed (this makes parser's life easier):
201 OpenContainer();
202
203 // then open the first container into which page's content will go:
204 OpenContainer();
205
206 #if !wxUSE_UNICODE
207 wxString charset = ExtractCharsetInformation(source);
208 if (!charset.empty())
209 {
210 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset);
211 if (enc != wxFONTENCODING_SYSTEM)
212 SetInputEncoding(enc);
213 }
214 #endif
215
216 m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor));
217 wxColour windowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ;
218
219 m_Container->InsertCell
220 (
221 new wxHtmlColourCell
222 (
223 m_windowInterface
224 ? m_windowInterface->GetHTMLBackgroundColour()
225 : windowColour,
226 wxHTML_CLR_BACKGROUND
227 )
228 );
229
230 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
231 }
232
233 void wxHtmlWinParser::DoneParser()
234 {
235 m_Container = NULL;
236 #if !wxUSE_UNICODE
237 SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call
238 #endif
239 wxHtmlParser::DoneParser();
240 }
241
242 #if WXWIN_COMPATIBILITY_2_6
243 wxHtmlWindow *wxHtmlWinParser::GetWindow()
244 {
245 if (!m_windowInterface)
246 return NULL;
247 return wxDynamicCast(m_windowInterface->GetHTMLWindow(), wxHtmlWindow);
248 }
249 #endif
250
251 wxObject* wxHtmlWinParser::GetProduct()
252 {
253 wxHtmlContainerCell *top;
254
255 CloseContainer();
256 OpenContainer();
257
258 top = m_Container;
259 while (top->GetParent()) top = top->GetParent();
260 top->RemoveExtraSpacing(true, true);
261
262 return top;
263 }
264
265 wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
266 const wxString& url) const
267 {
268 if ( !m_windowInterface )
269 return wxHtmlParser::OpenURL(type, url);
270
271 wxString myurl(url);
272 wxHtmlOpeningStatus status;
273 for (;;)
274 {
275 wxString myfullurl(myurl);
276
277 // consider url as absolute path first
278 wxURI current(myurl);
279 myfullurl = current.BuildUnescapedURI();
280
281 // if not absolute then ...
282 if( current.IsReference() )
283 {
284 wxString basepath = GetFS()->GetPath();
285 wxURI base(basepath);
286
287 // ... try to apply base path if valid ...
288 if( !base.IsReference() )
289 {
290 wxURI path(myfullurl);
291 path.Resolve( base );
292 myfullurl = path.BuildUnescapedURI();
293 }
294 else
295 {
296 // ... or force such addition if not included already
297 if( !current.GetPath().Contains(base.GetPath()) )
298 {
299 basepath += myurl;
300 wxURI connected( basepath );
301 myfullurl = connected.BuildUnescapedURI();
302 }
303 }
304 }
305
306 wxString redirect;
307 status = m_windowInterface->OnHTMLOpeningURL(type, myfullurl, &redirect);
308 if ( status != wxHTML_REDIRECT )
309 break;
310
311 myurl = redirect;
312 }
313
314 if ( status == wxHTML_BLOCK )
315 return NULL;
316
317 return GetFS()->OpenFile(myurl);
318 }
319
320 void wxHtmlWinParser::AddText(const wxChar* txt)
321 {
322 size_t i = 0,
323 x,
324 lng = wxStrlen(txt);
325 register wxChar d;
326 int templen = 0;
327 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
328
329 if (lng+1 > m_tmpStrBufSize)
330 {
331 delete[] m_tmpStrBuf;
332 m_tmpStrBuf = new wxChar[lng+1];
333 m_tmpStrBufSize = lng+1;
334 }
335 wxChar *temp = m_tmpStrBuf;
336
337 if (m_tmpLastWasSpace)
338 {
339 while ((i < lng) &&
340 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
341 (txt[i] == wxT('\t')))) i++;
342 }
343
344 while (i < lng)
345 {
346 x = 0;
347 d = temp[templen++] = txt[i];
348 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
349 {
350 i++, x++;
351 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
352 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
353 }
354 else i++;
355
356 if (x)
357 {
358 temp[templen-1] = wxT(' ');
359 DoAddText(temp, templen, nbsp);
360 m_tmpLastWasSpace = true;
361 }
362 }
363
364 if (templen && (templen > 1 || temp[0] != wxT(' ')))
365 {
366 DoAddText(temp, templen, nbsp);
367 m_tmpLastWasSpace = false;
368 }
369 }
370
371 void wxHtmlWinParser::DoAddText(wxChar *temp, int& templen, wxChar nbsp)
372 {
373 temp[templen] = 0;
374 templen = 0;
375 #if !wxUSE_UNICODE
376 if (m_EncConv)
377 m_EncConv->Convert(temp);
378 #endif
379 size_t len = wxStrlen(temp);
380 for (size_t j = 0; j < len; j++)
381 {
382 if (temp[j] == nbsp)
383 temp[j] = wxT(' ');
384 }
385
386 wxHtmlCell *c = new wxHtmlWordCell(temp, *(GetDC()));
387
388 ApplyStateToCell(c);
389
390 m_Container->InsertCell(c);
391 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
392 m_lastWordCell = (wxHtmlWordCell*)c;
393 }
394
395
396
397 wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
398 {
399 m_Container = new wxHtmlContainerCell(m_Container);
400 m_Container->SetAlignHor(m_Align);
401 m_tmpLastWasSpace = true;
402 /* to avoid space being first character in paragraph */
403 return m_Container;
404 }
405
406
407
408 wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c)
409 {
410 m_tmpLastWasSpace = true;
411 /* to avoid space being first character in paragraph */
412 return m_Container = c;
413 }
414
415
416
417 wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
418 {
419 m_Container = m_Container->GetParent();
420 return m_Container;
421 }
422
423
424 void wxHtmlWinParser::SetFontSize(int s)
425 {
426 if (s < 1) s = 1;
427 else if (s > 7) s = 7;
428 m_FontSize = s;
429 }
430
431
432
433 wxFont* wxHtmlWinParser::CreateCurrentFont()
434 {
435 int fb = GetFontBold(),
436 fi = GetFontItalic(),
437 fu = GetFontUnderlined(),
438 ff = GetFontFixed(),
439 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
440
441 wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal;
442 wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]);
443 wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]);
444 #if !wxUSE_UNICODE
445 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
446 #endif
447
448 if (*fontptr != NULL && (*faceptr != face
449 #if !wxUSE_UNICODE
450 || *encptr != m_OutputEnc
451 #endif
452 ))
453 {
454 delete *fontptr;
455 *fontptr = NULL;
456 }
457
458 if (*fontptr == NULL)
459 {
460 *faceptr = face;
461 *fontptr = new wxFont(
462 (int) (m_FontsSizes[fs] * m_PixelScale),
463 ff ? wxMODERN : wxSWISS,
464 fi ? wxITALIC : wxNORMAL,
465 fb ? wxBOLD : wxNORMAL,
466 fu ? true : false, face
467 #if wxUSE_UNICODE
468 );
469 #else
470 , m_OutputEnc);
471 *encptr = m_OutputEnc;
472 #endif
473 }
474 m_DC->SetFont(**fontptr);
475 return (*fontptr);
476 }
477
478
479
480 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
481 {
482 m_Link = link;
483 m_UseLink = (link.GetHref() != wxEmptyString);
484 }
485
486 void wxHtmlWinParser::SetFontFace(const wxString& face)
487 {
488 if (GetFontFixed()) m_FontFaceFixed = face;
489 else m_FontFaceNormal = face;
490
491 #if !wxUSE_UNICODE
492 if (m_InputEnc != wxFONTENCODING_DEFAULT)
493 SetInputEncoding(m_InputEnc);
494 #endif
495 }
496
497 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell *cell)
498 {
499 // set the link:
500 if (m_UseLink)
501 cell->SetLink(GetLink());
502
503 // apply current script mode settings:
504 cell->SetScriptMode(GetScriptMode(), GetScriptBaseline());
505 }
506
507
508 #if !wxUSE_UNICODE
509 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
510 {
511 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
512 if (m_EncConv)
513 {
514 delete m_EncConv;
515 m_EncConv = NULL;
516 }
517
518 if (enc == wxFONTENCODING_DEFAULT) return;
519
520 wxFontEncoding altfix, altnorm;
521 bool availfix, availnorm;
522
523 // exact match?
524 availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal);
525 availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed);
526 if (availnorm && availfix)
527 m_OutputEnc = enc;
528
529 // alternatives?
530 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) &&
531 wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) &&
532 altnorm == altfix)
533 m_OutputEnc = altnorm;
534
535 // at least normal face?
536 else if (availnorm)
537 m_OutputEnc = enc;
538 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false))
539 m_OutputEnc = altnorm;
540
541 else
542 {
543 #ifndef __WXMAC__
544 // okay, let's convert to ISO_8859-1, available always
545 m_OutputEnc = wxFONTENCODING_DEFAULT;
546 #else
547 m_OutputEnc = wxLocale::GetSystemEncoding() ;
548 #endif
549 }
550
551 m_InputEnc = enc;
552 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
553 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
554 else
555 GetEntitiesParser()->SetEncoding(m_OutputEnc);
556
557 if (m_InputEnc == m_OutputEnc) return;
558
559 m_EncConv = new wxEncodingConverter();
560 if (!m_EncConv->Init(m_InputEnc,
561 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
562 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
563 wxCONVERT_SUBSTITUTE))
564 { // total failure :-(
565 wxLogError(_("Failed to display HTML document in %s encoding"),
566 wxFontMapper::GetEncodingName(enc).c_str());
567 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
568 delete m_EncConv;
569 m_EncConv = NULL;
570 }
571 }
572 #endif
573
574
575
576
577 //-----------------------------------------------------------------------------
578 // wxHtmlWinTagHandler
579 //-----------------------------------------------------------------------------
580
581 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
582
583 //-----------------------------------------------------------------------------
584 // wxHtmlTagsModule
585 //-----------------------------------------------------------------------------
586
587 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
588 // This module is an ancestor for tag handlers modules defined
589 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
590 //
591 // Do not add any winpars.cpp shutdown or initialization code to it,
592 // create a new module instead!
593
594 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
595
596 bool wxHtmlTagsModule::OnInit()
597 {
598 wxHtmlWinParser::AddModule(this);
599 return true;
600 }
601
602 void wxHtmlTagsModule::OnExit()
603 {
604 wxHtmlWinParser::RemoveModule(this);
605 }
606
607 #endif