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;
52 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
53 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
55 m_lastWordCell
= NULL
;
59 for (i
= 0; i
< 2; i
++)
60 for (j
= 0; j
< 2; j
++)
61 for (k
= 0; k
< 2; k
++)
62 for (l
= 0; l
< 2; l
++)
63 for (m
= 0; m
< 7; m
++)
65 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
66 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
68 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
72 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
75 // fill in wxHtmlParser's tables:
76 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
79 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
80 mod
->FillHandlersTable(this);
81 node
= node
->GetNext();
85 wxHtmlWinParser::~wxHtmlWinParser()
89 for (i
= 0; i
< 2; i
++)
90 for (j
= 0; j
< 2; j
++)
91 for (k
= 0; k
< 2; k
++)
92 for (l
= 0; l
< 2; l
++)
93 for (m
= 0; m
< 7; m
++)
95 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
96 delete m_FontsTable
[i
][j
][k
][l
][m
];
101 delete[] m_tmpStrBuf
;
104 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
106 m_Modules
.Append(module);
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
111 m_Modules
.DeleteObject(module);
114 // build all HTML font sizes (1..7) from the given base size
115 static void wxBuildFontSizes(int *sizes
, int size
)
117 // using a fixed factor (1.2, from CSS2) is a bad idea as explained at
118 // http://www.w3.org/TR/CSS21/fonts.html#font-size-props but this is by far
119 // simplest thing to do so still do it like this for now
120 sizes
[0] = int(size
* 0.75); // exception to 1.2 rule, otherwise too small
121 sizes
[1] = int(size
* 0.83);
123 sizes
[3] = int(size
* 1.2);
124 sizes
[4] = int(size
* 1.44);
125 sizes
[5] = int(size
* 1.73);
126 sizes
[6] = int(size
* 2);
129 static int wxGetDefaultHTMLFontSize()
131 // base the default font size on the size of the default system font but
132 // also ensure that we have a font of reasonable size, otherwise small HTML
133 // fonts are unreadable
134 int size
= wxNORMAL_FONT
->GetPointSize();
140 void wxHtmlWinParser::SetFonts(const wxString
& normal_face
,
141 const wxString
& fixed_face
,
144 static int default_sizes
[7] = { 0 };
147 if ( !default_sizes
[0] )
148 wxBuildFontSizes(default_sizes
, wxGetDefaultHTMLFontSize());
150 sizes
= default_sizes
;
155 for (i
= 0; i
< 7; i
++)
156 m_FontsSizes
[i
] = sizes
[i
];
158 m_FontFaceFixed
= fixed_face
;
159 m_FontFaceNormal
= normal_face
;
162 SetInputEncoding(m_InputEnc
);
165 for (i
= 0; i
< 2; i
++)
166 for (j
= 0; j
< 2; j
++)
167 for (k
= 0; k
< 2; k
++)
168 for (l
= 0; l
< 2; l
++)
169 for (m
= 0; m
< 7; m
++) {
170 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
172 delete m_FontsTable
[i
][j
][k
][l
][m
];
173 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
178 void wxHtmlWinParser::SetStandardFonts(int size
,
179 const wxString
& normal_face
,
180 const wxString
& fixed_face
)
183 size
= wxGetDefaultHTMLFontSize();
186 wxBuildFontSizes(f_sizes
, size
);
188 wxString normal
= normal_face
;
189 if ( normal
.empty() )
190 normal
= wxNORMAL_FONT
->GetFaceName();
192 SetFonts(normal
, fixed_face
, f_sizes
);
195 void wxHtmlWinParser::InitParser(const wxString
& source
)
197 wxHtmlParser::InitParser(source
);
198 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
200 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
201 m_FontSize
= 3; //default one
202 CreateCurrentFont(); // we're selecting default font into
204 // we're not using GetCharWidth/Height() because of
205 // differences under X and win
207 m_DC
->GetTextExtent( wxT("H"), &w
, &h
);
212 m_Link
= wxHtmlLinkInfo( wxEmptyString
);
213 m_LinkColor
.Set(0, 0, 0xFF);
214 m_ActualColor
.Set(0, 0, 0);
215 m_Align
= wxHTML_ALIGN_LEFT
;
216 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
;
217 m_ScriptBaseline
= 0;
218 m_tmpLastWasSpace
= false;
219 m_lastWordCell
= NULL
;
221 // open the toplevel container that contains everything else and that
222 // is never closed (this makes parser's life easier):
225 // then open the first container into which page's content will go:
229 wxString charset
= ExtractCharsetInformation(source
);
230 if (!charset
.empty())
232 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
233 if (enc
!= wxFONTENCODING_SYSTEM
)
234 SetInputEncoding(enc
);
238 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
239 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
241 m_Container
->InsertCell
246 ? m_windowInterface
->GetHTMLBackgroundColour()
248 wxHTML_CLR_BACKGROUND
252 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
255 void wxHtmlWinParser::DoneParser()
259 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
261 wxHtmlParser::DoneParser();
264 #if WXWIN_COMPATIBILITY_2_6
265 wxHtmlWindow
*wxHtmlWinParser::GetWindow()
267 if (!m_windowInterface
)
269 return wxDynamicCast(m_windowInterface
->GetHTMLWindow(), wxHtmlWindow
);
273 wxObject
* wxHtmlWinParser::GetProduct()
275 wxHtmlContainerCell
*top
;
281 while (top
->GetParent()) top
= top
->GetParent();
282 top
->RemoveExtraSpacing(true, true);
287 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
288 const wxString
& url
) const
290 if ( !m_windowInterface
)
291 return wxHtmlParser::OpenURL(type
, url
);
294 wxHtmlOpeningStatus status
;
297 wxString
myfullurl(myurl
);
299 // consider url as absolute path first
300 wxURI
current(myurl
);
301 myfullurl
= current
.BuildUnescapedURI();
303 // if not absolute then ...
304 if( current
.IsReference() )
306 wxString basepath
= GetFS()->GetPath();
307 wxURI
base(basepath
);
309 // ... try to apply base path if valid ...
310 if( !base
.IsReference() )
312 wxURI
path(myfullurl
);
313 path
.Resolve( base
);
314 myfullurl
= path
.BuildUnescapedURI();
318 // ... or force such addition if not included already
319 if( !current
.GetPath().Contains(base
.GetPath()) )
322 wxURI
connected( basepath
);
323 myfullurl
= connected
.BuildUnescapedURI();
329 status
= m_windowInterface
->OnHTMLOpeningURL(type
, myfullurl
, &redirect
);
330 if ( status
!= wxHTML_REDIRECT
)
336 if ( status
== wxHTML_BLOCK
)
339 int flags
= wxFS_READ
;
340 if (type
== wxHTML_URL_IMAGE
)
341 flags
|= wxFS_SEEKABLE
;
343 return GetFS()->OpenFile(myurl
, flags
);
346 void wxHtmlWinParser::AddText(const wxString
& txt
)
351 size_t lng
= txt
.length();
352 if (lng
+1 > m_tmpStrBufSize
)
354 delete[] m_tmpStrBuf
;
355 m_tmpStrBuf
= new wxChar
[lng
+1];
356 m_tmpStrBufSize
= lng
+1;
358 wxChar
*temp
= m_tmpStrBuf
;
360 wxString::const_iterator i
= txt
.begin();
361 wxString::const_iterator end
= txt
.end();
363 if (m_tmpLastWasSpace
)
366 (*i
== wxT('\n') || *i
== wxT('\r') || *i
== wxT(' ') ||
376 d
= temp
[templen
++] = *i
;
377 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
381 (*i
== wxT('\n') || *i
== wxT('\r') ||
382 *i
== wxT(' ') || *i
== wxT('\t')) )
393 temp
[templen
-1] = wxT(' ');
394 DoAddText(temp
, templen
);
395 m_tmpLastWasSpace
= true;
399 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
401 DoAddText(temp
, templen
);
402 m_tmpLastWasSpace
= false;
406 void wxHtmlWinParser::DoAddText(wxChar
*temp
, int& templen
)
408 #define NBSP_UNICODE_VALUE 160
411 m_nbsp
= GetEntitiesParser()->GetCharForCode(NBSP_UNICODE_VALUE
);
412 #define CUR_NBSP_VALUE m_nbsp
414 #define CUR_NBSP_VALUE NBSP_UNICODE_VALUE
421 m_EncConv
->Convert(temp
);
423 size_t len
= wxStrlen(temp
);
424 for (size_t j
= 0; j
< len
; j
++)
426 if (temp
[j
] == CUR_NBSP_VALUE
)
430 wxHtmlCell
*c
= new wxHtmlWordCell(temp
, *(GetDC()));
434 m_Container
->InsertCell(c
);
435 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
436 m_lastWordCell
= (wxHtmlWordCell
*)c
;
441 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
443 m_Container
= new wxHtmlContainerCell(m_Container
);
444 m_Container
->SetAlignHor(m_Align
);
445 m_tmpLastWasSpace
= true;
446 /* to avoid space being first character in paragraph */
452 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
454 m_tmpLastWasSpace
= true;
455 /* to avoid space being first character in paragraph */
456 return m_Container
= c
;
461 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
463 m_Container
= m_Container
->GetParent();
468 void wxHtmlWinParser::SetFontSize(int s
)
479 wxFont
* wxHtmlWinParser::CreateCurrentFont()
481 int fb
= GetFontBold(),
482 fi
= GetFontItalic(),
483 fu
= GetFontUnderlined(),
485 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
487 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
488 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
489 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
491 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
494 if (*fontptr
!= NULL
&& (*faceptr
!= face
496 || *encptr
!= m_OutputEnc
504 if (*fontptr
== NULL
)
507 *fontptr
= new wxFont(
508 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
509 ff
? wxMODERN
: wxSWISS
,
510 fi
? wxITALIC
: wxNORMAL
,
511 fb
? wxBOLD
: wxNORMAL
,
512 fu
? true : false, face
517 *encptr
= m_OutputEnc
;
520 m_DC
->SetFont(**fontptr
);
526 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
529 m_UseLink
= (link
.GetHref() != wxEmptyString
);
532 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
535 m_FontFaceFixed
= face
;
537 m_FontFaceNormal
= face
;
540 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
541 SetInputEncoding(m_InputEnc
);
545 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell
*cell
)
549 cell
->SetLink(GetLink());
551 // apply current script mode settings:
552 cell
->SetScriptMode(GetScriptMode(), GetScriptBaseline());
557 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
559 // the character used for non-breakable space may change:
562 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
569 if (enc
== wxFONTENCODING_DEFAULT
)
572 wxFontEncoding altfix
, altnorm
;
573 bool availfix
, availnorm
;
575 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
576 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
578 if (availnorm
&& availfix
)
584 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
585 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
589 m_OutputEnc
= altnorm
;
593 // at least normal face?
596 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
598 m_OutputEnc
= altnorm
;
603 // okay, let's convert to ISO_8859-1, available always
604 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
606 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
611 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
613 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
617 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
620 if (m_InputEnc
== m_OutputEnc
)
623 m_EncConv
= new wxEncodingConverter();
624 if (!m_EncConv
->Init(m_InputEnc
,
625 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
626 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
627 wxCONVERT_SUBSTITUTE
))
628 { // total failure :-(
629 wxLogError(_("Failed to display HTML document in %s encoding"),
630 wxFontMapper::GetEncodingName(enc
).c_str());
631 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
641 //-----------------------------------------------------------------------------
642 // wxHtmlWinTagHandler
643 //-----------------------------------------------------------------------------
645 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
647 //-----------------------------------------------------------------------------
649 //-----------------------------------------------------------------------------
651 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
652 // This module is an ancestor for tag handlers modules defined
653 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
655 // Do not add any winpars.cpp shutdown or initialization code to it,
656 // create a new module instead!
658 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
660 bool wxHtmlTagsModule::OnInit()
662 wxHtmlWinParser::AddModule(this);
666 void wxHtmlTagsModule::OnExit()
668 wxHtmlWinParser::RemoveModule(this);