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