1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/winpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/settings.h"
25 #include "wx/html/htmldefs.h"
26 #include "wx/html/winpars.h"
27 #include "wx/html/htmlwin.h"
28 #include "wx/fontmap.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser
, wxHtmlParser
)
38 wxList
wxHtmlWinParser::m_Modules
;
40 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindowInterface
*wndIface
)
44 m_windowInterface
= wndIface
;
47 m_CharHeight
= m_CharWidth
= 0;
51 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
52 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
54 m_lastWordCell
= NULL
;
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
++)
64 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
65 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
67 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
71 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
74 // fill in wxHtmlParser's tables:
75 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
78 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
79 mod
->FillHandlersTable(this);
80 node
= node
->GetNext();
84 wxHtmlWinParser::~wxHtmlWinParser()
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
++)
94 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
95 delete m_FontsTable
[i
][j
][k
][l
][m
];
100 delete[] m_tmpStrBuf
;
103 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
105 m_Modules
.Append(module);
108 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
110 m_Modules
.DeleteObject(module);
113 // build all HTML font sizes (1..7) from the given base size
114 static void wxBuildFontSizes(int *sizes
, int size
)
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.69);
120 sizes
[1] = int(size
* 0.83);
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);
128 void wxHtmlWinParser::SetFonts(const wxString
& normal_face
,
129 const wxString
& fixed_face
,
132 static int default_sizes
[7] = { 0 };
135 if ( !default_sizes
[0] )
136 wxBuildFontSizes(default_sizes
, wxNORMAL_FONT
->GetPointSize());
138 sizes
= default_sizes
;
143 for (i
= 0; i
< 7; i
++)
144 m_FontsSizes
[i
] = sizes
[i
];
146 m_FontFaceFixed
= fixed_face
;
147 m_FontFaceNormal
= normal_face
;
150 SetInputEncoding(m_InputEnc
);
153 for (i
= 0; i
< 2; i
++)
154 for (j
= 0; j
< 2; j
++)
155 for (k
= 0; k
< 2; k
++)
156 for (l
= 0; l
< 2; l
++)
157 for (m
= 0; m
< 7; m
++) {
158 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
160 delete m_FontsTable
[i
][j
][k
][l
][m
];
161 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
166 void wxHtmlWinParser::SetStandardFonts(int size
,
167 const wxString
& normal_face
,
168 const wxString
& fixed_face
)
171 size
= wxNORMAL_FONT
->GetPointSize();
174 wxBuildFontSizes(f_sizes
, size
);
176 wxString normal
= normal_face
;
177 if ( normal
.empty() )
178 normal
= wxNORMAL_FONT
->GetFaceName();
180 SetFonts(normal
, fixed_face
, f_sizes
);
183 void wxHtmlWinParser::InitParser(const wxString
& source
)
185 wxHtmlParser::InitParser(source
);
186 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
188 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
189 m_FontSize
= 3; //default one
190 CreateCurrentFont(); // we're selecting default font into
191 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
192 /* NOTE : we're not using GetCharWidth/Height() because
193 of differences under X and win
197 m_Link
= wxHtmlLinkInfo( wxEmptyString
);
198 m_LinkColor
.Set(0, 0, 0xFF);
199 m_ActualColor
.Set(0, 0, 0);
200 m_Align
= wxHTML_ALIGN_LEFT
;
201 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
;
202 m_ScriptBaseline
= 0;
203 m_tmpLastWasSpace
= false;
204 m_lastWordCell
= NULL
;
206 // open the toplevel container that contains everything else and that
207 // is never closed (this makes parser's life easier):
210 // then open the first container into which page's content will go:
214 wxString charset
= ExtractCharsetInformation(source
);
215 if (!charset
.empty())
217 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
218 if (enc
!= wxFONTENCODING_SYSTEM
)
219 SetInputEncoding(enc
);
223 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
224 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
226 m_Container
->InsertCell
231 ? m_windowInterface
->GetHTMLBackgroundColour()
233 wxHTML_CLR_BACKGROUND
237 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
240 void wxHtmlWinParser::DoneParser()
244 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
246 wxHtmlParser::DoneParser();
249 #if WXWIN_COMPATIBILITY_2_6
250 wxHtmlWindow
*wxHtmlWinParser::GetWindow()
252 if (!m_windowInterface
)
254 return wxDynamicCast(m_windowInterface
->GetHTMLWindow(), wxHtmlWindow
);
258 wxObject
* wxHtmlWinParser::GetProduct()
260 wxHtmlContainerCell
*top
;
266 while (top
->GetParent()) top
= top
->GetParent();
267 top
->RemoveExtraSpacing(true, true);
272 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
273 const wxString
& url
) const
275 if ( !m_windowInterface
)
276 return wxHtmlParser::OpenURL(type
, url
);
279 wxHtmlOpeningStatus status
;
282 wxString
myfullurl(myurl
);
284 // consider url as absolute path first
285 wxURI
current(myurl
);
286 myfullurl
= current
.BuildUnescapedURI();
288 // if not absolute then ...
289 if( current
.IsReference() )
291 wxString basepath
= GetFS()->GetPath();
292 wxURI
base(basepath
);
294 // ... try to apply base path if valid ...
295 if( !base
.IsReference() )
297 wxURI
path(myfullurl
);
298 path
.Resolve( base
);
299 myfullurl
= path
.BuildUnescapedURI();
303 // ... or force such addition if not included already
304 if( !current
.GetPath().Contains(base
.GetPath()) )
307 wxURI
connected( basepath
);
308 myfullurl
= connected
.BuildUnescapedURI();
314 status
= m_windowInterface
->OnHTMLOpeningURL(type
, myfullurl
, &redirect
);
315 if ( status
!= wxHTML_REDIRECT
)
321 if ( status
== wxHTML_BLOCK
)
324 return GetFS()->OpenFile(myurl
);
327 void wxHtmlWinParser::AddText(const wxChar
* txt
)
334 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
336 if (lng
+1 > m_tmpStrBufSize
)
338 delete[] m_tmpStrBuf
;
339 m_tmpStrBuf
= new wxChar
[lng
+1];
340 m_tmpStrBufSize
= lng
+1;
342 wxChar
*temp
= m_tmpStrBuf
;
344 if (m_tmpLastWasSpace
)
347 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
348 (txt
[i
] == wxT('\t')))) i
++;
354 d
= temp
[templen
++] = txt
[i
];
355 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
358 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
359 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
365 temp
[templen
-1] = wxT(' ');
366 DoAddText(temp
, templen
, nbsp
);
367 m_tmpLastWasSpace
= true;
371 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
373 DoAddText(temp
, templen
, nbsp
);
374 m_tmpLastWasSpace
= false;
378 void wxHtmlWinParser::DoAddText(wxChar
*temp
, int& templen
, wxChar nbsp
)
384 m_EncConv
->Convert(temp
);
386 size_t len
= wxStrlen(temp
);
387 for (size_t j
= 0; j
< len
; j
++)
393 wxHtmlCell
*c
= new wxHtmlWordCell(temp
, *(GetDC()));
397 m_Container
->InsertCell(c
);
398 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
399 m_lastWordCell
= (wxHtmlWordCell
*)c
;
404 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
406 m_Container
= new wxHtmlContainerCell(m_Container
);
407 m_Container
->SetAlignHor(m_Align
);
408 m_tmpLastWasSpace
= true;
409 /* to avoid space being first character in paragraph */
415 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
417 m_tmpLastWasSpace
= true;
418 /* to avoid space being first character in paragraph */
419 return m_Container
= c
;
424 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
426 m_Container
= m_Container
->GetParent();
431 void wxHtmlWinParser::SetFontSize(int s
)
434 else if (s
> 7) s
= 7;
440 wxFont
* wxHtmlWinParser::CreateCurrentFont()
442 int fb
= GetFontBold(),
443 fi
= GetFontItalic(),
444 fu
= GetFontUnderlined(),
446 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
448 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
449 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
450 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
452 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
455 if (*fontptr
!= NULL
&& (*faceptr
!= face
457 || *encptr
!= m_OutputEnc
465 if (*fontptr
== NULL
)
468 *fontptr
= new wxFont(
469 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
470 ff
? wxMODERN
: wxSWISS
,
471 fi
? wxITALIC
: wxNORMAL
,
472 fb
? wxBOLD
: wxNORMAL
,
473 fu
? true : false, face
478 *encptr
= m_OutputEnc
;
481 m_DC
->SetFont(**fontptr
);
487 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
490 m_UseLink
= (link
.GetHref() != wxEmptyString
);
493 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
495 if (GetFontFixed()) m_FontFaceFixed
= face
;
496 else m_FontFaceNormal
= face
;
499 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
500 SetInputEncoding(m_InputEnc
);
504 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell
*cell
)
508 cell
->SetLink(GetLink());
510 // apply current script mode settings:
511 cell
->SetScriptMode(GetScriptMode(), GetScriptBaseline());
516 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
518 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
525 if (enc
== wxFONTENCODING_DEFAULT
) return;
527 wxFontEncoding altfix
, altnorm
;
528 bool availfix
, availnorm
;
531 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
532 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
533 if (availnorm
&& availfix
)
537 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
538 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
540 m_OutputEnc
= altnorm
;
542 // at least normal face?
545 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
546 m_OutputEnc
= altnorm
;
551 // okay, let's convert to ISO_8859-1, available always
552 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
554 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
559 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
560 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
562 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
564 if (m_InputEnc
== m_OutputEnc
) return;
566 m_EncConv
= new wxEncodingConverter();
567 if (!m_EncConv
->Init(m_InputEnc
,
568 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
569 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
570 wxCONVERT_SUBSTITUTE
))
571 { // total failure :-(
572 wxLogError(_("Failed to display HTML document in %s encoding"),
573 wxFontMapper::GetEncodingName(enc
).c_str());
574 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
584 //-----------------------------------------------------------------------------
585 // wxHtmlWinTagHandler
586 //-----------------------------------------------------------------------------
588 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
590 //-----------------------------------------------------------------------------
592 //-----------------------------------------------------------------------------
594 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
595 // This module is an ancestor for tag handlers modules defined
596 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
598 // Do not add any winpars.cpp shutdown or initialization code to it,
599 // create a new module instead!
601 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
603 bool wxHtmlTagsModule::OnInit()
605 wxHtmlWinParser::AddModule(this);
609 void wxHtmlTagsModule::OnExit()
611 wxHtmlWinParser::RemoveModule(this);