1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/winpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_HTML && wxUSE_STREAMS
21 #include "wx/settings.h"
24 #include "wx/html/htmldefs.h"
25 #include "wx/html/winpars.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/html/styleparams.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_whitespaceMode
= Whitespace_Normal
;
56 m_lastWordCell
= NULL
;
61 for (i
= 0; i
< 2; i
++)
62 for (j
= 0; j
< 2; j
++)
63 for (k
= 0; k
< 2; k
++)
64 for (l
= 0; l
< 2; l
++)
65 for (m
= 0; m
< 7; m
++)
67 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
68 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
70 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
74 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
77 // fill in wxHtmlParser's tables:
78 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
81 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
82 mod
->FillHandlersTable(this);
83 node
= node
->GetNext();
87 wxHtmlWinParser::~wxHtmlWinParser()
91 for (i
= 0; i
< 2; i
++)
92 for (j
= 0; j
< 2; j
++)
93 for (k
= 0; k
< 2; k
++)
94 for (l
= 0; l
< 2; l
++)
95 for (m
= 0; m
< 7; m
++)
97 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
98 delete m_FontsTable
[i
][j
][k
][l
][m
];
103 delete[] m_tmpStrBuf
;
106 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
108 m_Modules
.Append(module);
111 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
113 m_Modules
.DeleteObject(module);
116 // build all HTML font sizes (1..7) from the given base size
117 static void wxBuildFontSizes(int *sizes
, int size
)
119 // using a fixed factor (1.2, from CSS2) is a bad idea as explained at
120 // http://www.w3.org/TR/CSS21/fonts.html#font-size-props but this is by far
121 // simplest thing to do so still do it like this for now
122 sizes
[0] = int(size
* 0.75); // exception to 1.2 rule, otherwise too small
123 sizes
[1] = int(size
* 0.83);
125 sizes
[3] = int(size
* 1.2);
126 sizes
[4] = int(size
* 1.44);
127 sizes
[5] = int(size
* 1.73);
128 sizes
[6] = int(size
* 2);
131 static int wxGetDefaultHTMLFontSize()
133 // base the default font size on the size of the default system font but
134 // also ensure that we have a font of reasonable size, otherwise small HTML
135 // fonts are unreadable
136 int size
= wxNORMAL_FONT
->GetPointSize();
142 void wxHtmlWinParser::SetFonts(const wxString
& normal_face
,
143 const wxString
& fixed_face
,
146 static int default_sizes
[7] = { 0 };
149 if ( !default_sizes
[0] )
150 wxBuildFontSizes(default_sizes
, wxGetDefaultHTMLFontSize());
152 sizes
= default_sizes
;
157 for (i
= 0; i
< 7; i
++)
158 m_FontsSizes
[i
] = sizes
[i
];
160 m_FontFaceFixed
= fixed_face
;
161 m_FontFaceNormal
= normal_face
;
164 SetInputEncoding(m_InputEnc
);
167 for (i
= 0; i
< 2; i
++)
168 for (j
= 0; j
< 2; j
++)
169 for (k
= 0; k
< 2; k
++)
170 for (l
= 0; l
< 2; l
++)
171 for (m
= 0; m
< 7; m
++) {
172 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
174 delete m_FontsTable
[i
][j
][k
][l
][m
];
175 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
180 void wxHtmlWinParser::SetStandardFonts(int size
,
181 const wxString
& normal_face
,
182 const wxString
& fixed_face
)
185 size
= wxGetDefaultHTMLFontSize();
188 wxBuildFontSizes(f_sizes
, size
);
190 wxString normal
= normal_face
;
191 if ( normal
.empty() )
192 normal
= wxNORMAL_FONT
->GetFaceName();
194 SetFonts(normal
, fixed_face
, f_sizes
);
197 void wxHtmlWinParser::InitParser(const wxString
& source
)
199 wxHtmlParser::InitParser(source
);
200 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
202 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
203 m_FontSize
= 3; //default one
204 CreateCurrentFont(); // we're selecting default font into
206 // we're not using GetCharWidth/Height() because of
207 // differences under X and win
209 m_DC
->GetTextExtent( wxT("H"), &w
, &h
);
214 m_Link
= wxHtmlLinkInfo( wxEmptyString
);
215 m_LinkColor
.Set(0, 0, 0xFF);
216 m_ActualColor
.Set(0, 0, 0);
217 const wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
218 m_ActualBackgroundColor
= m_windowInterface
219 ? m_windowInterface
->GetHTMLBackgroundColour()
221 m_ActualBackgroundMode
= wxTRANSPARENT
;
222 m_Align
= wxHTML_ALIGN_LEFT
;
223 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
;
224 m_ScriptBaseline
= 0;
225 m_tmpLastWasSpace
= false;
226 m_lastWordCell
= NULL
;
228 // open the toplevel container that contains everything else and that
229 // is never closed (this makes parser's life easier):
232 // then open the first container into which page's content will go:
236 wxString charset
= ExtractCharsetInformation(source
);
237 if (!charset
.empty())
239 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
240 if (enc
!= wxFONTENCODING_SYSTEM
)
241 SetInputEncoding(enc
);
245 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
247 m_Container
->InsertCell
251 m_ActualBackgroundColor
,
252 m_ActualBackgroundMode
== wxTRANSPARENT
? wxHTML_CLR_TRANSPARENT_BACKGROUND
: wxHTML_CLR_BACKGROUND
256 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
259 void wxHtmlWinParser::DoneParser()
263 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
265 wxHtmlParser::DoneParser();
268 #if WXWIN_COMPATIBILITY_2_6
269 wxHtmlWindow
*wxHtmlWinParser::GetWindow()
271 if (!m_windowInterface
)
273 return wxDynamicCast(m_windowInterface
->GetHTMLWindow(), wxHtmlWindow
);
277 wxObject
* wxHtmlWinParser::GetProduct()
279 wxHtmlContainerCell
*top
;
285 while (top
->GetParent()) top
= top
->GetParent();
286 top
->RemoveExtraSpacing(true, true);
291 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
292 const wxString
& url
) const
294 if ( !m_windowInterface
)
295 return wxHtmlParser::OpenURL(type
, url
);
298 wxHtmlOpeningStatus status
;
301 wxString
myfullurl(myurl
);
303 // consider url as absolute path first
304 wxURI
current(myurl
);
305 myfullurl
= current
.BuildUnescapedURI();
307 // if not absolute then ...
308 if( current
.IsReference() )
310 wxString basepath
= GetFS()->GetPath();
311 wxURI
base(basepath
);
313 // ... try to apply base path if valid ...
314 if( !base
.IsReference() )
316 wxURI
path(myfullurl
);
317 path
.Resolve( base
);
318 myfullurl
= path
.BuildUnescapedURI();
322 // ... or force such addition if not included already
323 if( !current
.GetPath().Contains(base
.GetPath()) )
326 wxURI
connected( basepath
);
327 myfullurl
= connected
.BuildUnescapedURI();
333 status
= m_windowInterface
->OnHTMLOpeningURL(type
, myfullurl
, &redirect
);
334 if ( status
!= wxHTML_REDIRECT
)
340 if ( status
== wxHTML_BLOCK
)
343 int flags
= wxFS_READ
;
344 if (type
== wxHTML_URL_IMAGE
)
345 flags
|= wxFS_SEEKABLE
;
347 return GetFS()->OpenFile(myurl
, flags
);
350 #define NBSP_UNICODE_VALUE (wxChar(160))
352 #define CUR_NBSP_VALUE m_nbsp
354 #define CUR_NBSP_VALUE NBSP_UNICODE_VALUE
357 void wxHtmlWinParser::AddText(const wxString
& txt
)
361 m_nbsp
= GetEntitiesParser()->GetCharForCode(NBSP_UNICODE_VALUE
);
364 if ( m_whitespaceMode
== Whitespace_Normal
)
368 size_t lng
= txt
.length();
369 if (lng
+1 > m_tmpStrBufSize
)
371 delete[] m_tmpStrBuf
;
372 m_tmpStrBuf
= new wxChar
[lng
+1];
373 m_tmpStrBufSize
= lng
+1;
375 wxChar
*temp
= m_tmpStrBuf
;
377 wxString::const_iterator i
= txt
.begin();
378 const wxString::const_iterator end
= txt
.end();
380 if (m_tmpLastWasSpace
)
383 (*i
== wxT('\n') || *i
== wxT('\r') || *i
== wxT(' ') ||
393 const wxChar d
= temp
[templen
++] = *i
;
394 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
398 (*i
== wxT('\n') || *i
== wxT('\r') ||
399 *i
== wxT(' ') || *i
== wxT('\t')) )
412 temp
[templen
-1] = wxT(' ');
413 FlushWordBuf(temp
, templen
);
414 m_tmpLastWasSpace
= true;
418 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
420 FlushWordBuf(temp
, templen
);
421 m_tmpLastWasSpace
= false;
424 else // m_whitespaceMode == Whitespace_Pre
426 if ( txt
.find(CUR_NBSP_VALUE
) != wxString::npos
)
428 // we need to substitute spaces for here just like we
429 // did in the Whitespace_Normal branch above
431 txt2
.Replace(CUR_NBSP_VALUE
, ' ');
439 // don't eat any whitespace in <pre> block
440 m_tmpLastWasSpace
= false;
444 void wxHtmlWinParser::FlushWordBuf(wxChar
*buf
, int& len
)
448 for ( int i
= 0; i
< len
; i
++ )
450 if ( buf
[i
] == CUR_NBSP_VALUE
)
456 m_EncConv
->Convert(buf
);
459 AddWord(wxString(buf
, len
));
464 void wxHtmlWinParser::AddWord(wxHtmlWordCell
*word
)
466 ApplyStateToCell(word
);
468 m_Container
->InsertCell(word
);
469 word
->SetPreviousWord(m_lastWordCell
);
470 m_lastWordCell
= word
;
473 void wxHtmlWinParser::AddPreBlock(const wxString
& text
)
475 if ( text
.find('\t') != wxString::npos
)
478 text2
.reserve(text
.length());
480 const wxString::const_iterator end
= text
.end();
481 wxString::const_iterator copyFrom
= text
.begin();
483 int posColumn
= m_posColumn
;
484 for ( wxString::const_iterator i
= copyFrom
; i
!= end
; ++i
, ++pos
)
489 text2
.append(copyFrom
, i
);
491 const unsigned SPACES_PER_TAB
= 8;
492 const size_t expandTo
= SPACES_PER_TAB
- posColumn
% SPACES_PER_TAB
;
493 text2
.append(expandTo
, ' ');
495 posColumn
+= expandTo
;
503 if ( copyFrom
!= text
.end() )
504 text2
.append(copyFrom
, text
.end());
506 AddWord(new wxHtmlWordWithTabsCell(text2
, text
, m_posColumn
, *(GetDC())));
508 m_posColumn
= posColumn
;
512 // no special formatting needed
514 m_posColumn
+= text
.length();
519 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
521 m_Container
= new wxHtmlContainerCell(m_Container
);
522 m_Container
->SetAlignHor(m_Align
);
524 m_tmpLastWasSpace
= true;
525 /* to avoid space being first character in paragraph */
531 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
533 m_tmpLastWasSpace
= true;
534 /* to avoid space being first character in paragraph */
535 return m_Container
= c
;
540 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
542 m_Container
= m_Container
->GetParent();
547 void wxHtmlWinParser::SetFontSize(int s
)
557 void wxHtmlWinParser::SetDC(wxDC
*dc
, double pixel_scale
, double font_scale
)
560 m_PixelScale
= pixel_scale
;
561 m_FontScale
= font_scale
;
564 void wxHtmlWinParser::SetFontPointSize(int pt
)
566 if (pt
<= m_FontsSizes
[0])
568 else if (pt
>= m_FontsSizes
[6])
572 // Find the font closest to the given value with a simple linear search
573 // (binary search is not worth it here for so small number of elements)
574 for ( int n
= 0; n
< 6; n
++ )
576 if ( (pt
> m_FontsSizes
[n
]) && (pt
<= m_FontsSizes
[n
+ 1]) )
578 if ( (pt
- m_FontsSizes
[n
]) >= (m_FontsSizes
[n
+ 1] - pt
) )
580 // The actual size is closer to the next entry than to this
585 // Notice that m_FontSize starts from 1, hence +1 here.
594 wxFont
* wxHtmlWinParser::CreateCurrentFont()
596 int fb
= GetFontBold(),
597 fi
= GetFontItalic(),
598 fu
= GetFontUnderlined(),
600 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
602 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
603 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
604 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
606 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
609 if (*fontptr
!= NULL
&& (*faceptr
!= face
611 || *encptr
!= m_OutputEnc
618 if (*fontptr
== NULL
)
621 *fontptr
= new wxFont(
622 (int) (m_FontsSizes
[fs
] * m_FontScale
),
623 ff
? wxMODERN
: wxSWISS
,
624 fi
? wxITALIC
: wxNORMAL
,
625 fb
? wxBOLD
: wxNORMAL
,
626 fu
? true : false, face
631 *encptr
= m_OutputEnc
;
634 m_DC
->SetFont(**fontptr
);
640 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
643 m_UseLink
= (link
.GetHref() != wxEmptyString
);
646 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
649 m_FontFaceFixed
= face
;
651 m_FontFaceNormal
= face
;
654 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
655 SetInputEncoding(m_InputEnc
);
659 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell
*cell
)
663 cell
->SetLink(GetLink());
665 // apply current script mode settings:
666 cell
->SetScriptMode(GetScriptMode(), GetScriptBaseline());
671 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
673 // the character used for non-breakable space may change:
676 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
679 if (enc
== wxFONTENCODING_DEFAULT
)
682 wxFontEncoding altfix
, altnorm
;
683 bool availfix
, availnorm
;
685 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
686 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
688 if (availnorm
&& availfix
)
694 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
695 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
699 m_OutputEnc
= altnorm
;
703 // at least normal face?
706 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
708 m_OutputEnc
= altnorm
;
713 // okay, let's convert to ISO_8859-1, available always
714 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
716 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
721 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
723 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
727 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
730 if (m_InputEnc
== m_OutputEnc
)
733 m_EncConv
= new wxEncodingConverter();
734 if (!m_EncConv
->Init(m_InputEnc
,
735 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
736 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
737 wxCONVERT_SUBSTITUTE
))
738 { // total failure :-(
739 wxLogError(_("Failed to display HTML document in %s encoding"),
740 wxFontMapper::GetEncodingName(enc
).c_str());
741 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
750 //-----------------------------------------------------------------------------
751 // wxHtmlWinTagHandler
752 //-----------------------------------------------------------------------------
754 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
756 void wxHtmlWinTagHandler::ApplyStyle(const wxHtmlStyleParams
&styleParams
)
760 str
= styleParams
.GetParam(wxS("color"));
764 if ( wxHtmlTag::ParseAsColour(str
, &clr
) )
766 m_WParser
->SetActualColor(clr
);
767 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
));
771 str
= styleParams
.GetParam(wxS("background-color"));
775 if ( wxHtmlTag::ParseAsColour(str
, &clr
) )
777 m_WParser
->SetActualBackgroundColor(clr
);
778 m_WParser
->SetActualBackgroundMode(wxSOLID
);
779 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
, wxHTML_CLR_BACKGROUND
));
783 str
= styleParams
.GetParam(wxS("font-size"));
787 int foundIndex
= str
.Find(wxS("pt"));
788 if (foundIndex
!= wxNOT_FOUND
)
790 str
.Truncate(foundIndex
);
793 if (str
.ToLong(&sizeValue
) == true)
796 m_WParser
->SetFontPointSize(sizeValue
);
797 m_WParser
->GetContainer()->InsertCell(
798 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
801 // else: check for other ways of specifying size (TODO)
804 str
= styleParams
.GetParam(wxS("font-weight"));
807 // Only bold and normal supported just now
808 if ( str
== wxS("bold") )
810 m_WParser
->SetFontBold(true);
811 m_WParser
->GetContainer()->InsertCell(
812 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
814 else if ( str
== wxS("normal") )
816 m_WParser
->SetFontBold(false);
817 m_WParser
->GetContainer()->InsertCell(
818 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
822 str
= styleParams
.GetParam(wxS("font-style"));
825 // "oblique" and "italic" are more or less the same.
826 // "inherit" (using the parent font) is not supported.
827 if ( str
== wxS("oblique") || str
== wxS("italic") )
829 m_WParser
->SetFontItalic(true);
830 m_WParser
->GetContainer()->InsertCell(
831 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
833 else if ( str
== wxS("normal") )
835 m_WParser
->SetFontItalic(false);
836 m_WParser
->GetContainer()->InsertCell(
837 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
841 str
= styleParams
.GetParam(wxS("text-decoration"));
844 // Only underline is supported.
845 if ( str
== wxS("underline") )
847 m_WParser
->SetFontUnderlined(true);
848 m_WParser
->GetContainer()->InsertCell(
849 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
853 str
= styleParams
.GetParam(wxS("font-family"));
856 m_WParser
->SetFontFace(str
);
857 m_WParser
->GetContainer()->InsertCell(
858 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
862 //-----------------------------------------------------------------------------
864 //-----------------------------------------------------------------------------
866 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
867 // This module is an ancestor for tag handlers modules defined
868 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
870 // Do not add any winpars.cpp shutdown or initialization code to it,
871 // create a new module instead!
873 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
875 bool wxHtmlTagsModule::OnInit()
877 wxHtmlWinParser::AddModule(this);
881 void wxHtmlTagsModule::OnExit()
883 wxHtmlWinParser::RemoveModule(this);