]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/html/winpars.cpp |
5526e819 VS |
3 | // Purpose: wxHtmlParser class (generic parser) |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
65571936 | 6 | // Licence: wxWindows licence |
5526e819 VS |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
3096bd2f | 9 | #include "wx/wxprec.h" |
5526e819 | 10 | |
2b5f62a0 | 11 | #ifdef __BORLANDC__ |
93763ad5 | 12 | #pragma hdrstop |
5526e819 VS |
13 | #endif |
14 | ||
93763ad5 WS |
15 | #if wxUSE_HTML && wxUSE_STREAMS |
16 | ||
b4f4d3dd | 17 | #ifndef WX_PRECOMP |
04dbb646 VZ |
18 | #include "wx/intl.h" |
19 | #include "wx/dc.h" | |
e4db172a | 20 | #include "wx/log.h" |
9eddec69 | 21 | #include "wx/settings.h" |
5526e819 VS |
22 | #endif |
23 | ||
69941f05 VS |
24 | #include "wx/html/htmldefs.h" |
25 | #include "wx/html/winpars.h" | |
26 | #include "wx/html/htmlwin.h" | |
0d7acfb9 | 27 | #include "wx/html/styleparams.h" |
b250d384 | 28 | #include "wx/fontmap.h" |
3ec372c8 | 29 | #include "wx/uri.h" |
5526e819 VS |
30 | |
31 | ||
32 | //----------------------------------------------------------------------------- | |
33 | // wxHtmlWinParser | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
4f44ea36 | 36 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser, wxHtmlParser) |
5526e819 VS |
37 | |
38 | wxList wxHtmlWinParser::m_Modules; | |
39 | ||
bc55e31b | 40 | wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindowInterface *wndIface) |
5526e819 | 41 | { |
211dfedd VS |
42 | m_tmpStrBuf = NULL; |
43 | m_tmpStrBufSize = 0; | |
bc55e31b | 44 | m_windowInterface = wndIface; |
5526e819 VS |
45 | m_Container = NULL; |
46 | m_DC = NULL; | |
47 | m_CharHeight = m_CharWidth = 0; | |
d1da8872 | 48 | m_UseLink = false; |
2b5f62a0 | 49 | #if !wxUSE_UNICODE |
c7820964 | 50 | m_nbsp = 0; |
b250d384 | 51 | m_EncConv = NULL; |
2b5f62a0 VZ |
52 | m_InputEnc = wxFONTENCODING_ISO8859_1; |
53 | m_OutputEnc = wxFONTENCODING_DEFAULT; | |
54 | #endif | |
6a603a10 | 55 | m_whitespaceMode = Whitespace_Normal; |
b6d93b26 | 56 | m_lastWordCell = NULL; |
6a603a10 | 57 | m_posColumn = 0; |
5526e819 VS |
58 | |
59 | { | |
60 | int i, j, k, l, m; | |
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++) | |
3c8c8da2 | 65 | for (m = 0; m < 7; m++) |
e3c7fd79 | 66 | { |
5526e819 | 67 | m_FontsTable[i][j][k][l][m] = NULL; |
f1ad10f3 | 68 | m_FontsFacesTable[i][j][k][l][m] = wxEmptyString; |
2b5f62a0 | 69 | #if !wxUSE_UNICODE |
b250d384 | 70 | m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT; |
2b5f62a0 | 71 | #endif |
f1ad10f3 | 72 | } |
4eecf115 VS |
73 | |
74 | SetFonts(wxEmptyString, wxEmptyString, NULL); | |
5526e819 VS |
75 | } |
76 | ||
77 | // fill in wxHtmlParser's tables: | |
222ed1d6 | 78 | wxList::compatibility_iterator node = m_Modules.GetFirst(); |
3c8c8da2 | 79 | while (node) |
4f9297b0 VS |
80 | { |
81 | wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData(); | |
82 | mod->FillHandlersTable(this); | |
83 | node = node->GetNext(); | |
5526e819 VS |
84 | } |
85 | } | |
86 | ||
b250d384 VS |
87 | wxHtmlWinParser::~wxHtmlWinParser() |
88 | { | |
89 | int i, j, k, l, m; | |
90 | ||
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++) | |
3c8c8da2 | 95 | for (m = 0; m < 7; m++) |
e3c7fd79 | 96 | { |
3c8c8da2 | 97 | if (m_FontsTable[i][j][k][l][m] != NULL) |
b250d384 VS |
98 | delete m_FontsTable[i][j][k][l][m]; |
99 | } | |
2b5f62a0 | 100 | #if !wxUSE_UNICODE |
211dfedd | 101 | delete m_EncConv; |
2b5f62a0 | 102 | #endif |
211dfedd | 103 | delete[] m_tmpStrBuf; |
b250d384 VS |
104 | } |
105 | ||
5526e819 VS |
106 | void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module) |
107 | { | |
108 | m_Modules.Append(module); | |
109 | } | |
110 | ||
f6bcfd97 BP |
111 | void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module) |
112 | { | |
113 | m_Modules.DeleteObject(module); | |
114 | } | |
115 | ||
ba804ab2 VZ |
116 | // build all HTML font sizes (1..7) from the given base size |
117 | static void wxBuildFontSizes(int *sizes, int size) | |
118 | { | |
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 | |
1d203ed8 | 122 | sizes[0] = int(size * 0.75); // exception to 1.2 rule, otherwise too small |
ba804ab2 VZ |
123 | sizes[1] = int(size * 0.83); |
124 | sizes[2] = size; | |
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); | |
129 | } | |
130 | ||
1d203ed8 VZ |
131 | static int wxGetDefaultHTMLFontSize() |
132 | { | |
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(); | |
137 | if ( size < 10 ) | |
138 | size = 10; | |
139 | return size; | |
140 | } | |
141 | ||
ba804ab2 VZ |
142 | void wxHtmlWinParser::SetFonts(const wxString& normal_face, |
143 | const wxString& fixed_face, | |
4eecf115 | 144 | const int *sizes) |
5526e819 | 145 | { |
ba804ab2 VZ |
146 | static int default_sizes[7] = { 0 }; |
147 | if ( !sizes ) | |
148 | { | |
149 | if ( !default_sizes[0] ) | |
1d203ed8 | 150 | wxBuildFontSizes(default_sizes, wxGetDefaultHTMLFontSize()); |
ba804ab2 VZ |
151 | |
152 | sizes = default_sizes; | |
153 | } | |
4eecf115 | 154 | |
c9f56e70 VS |
155 | int i, j, k, l, m; |
156 | ||
ba804ab2 VZ |
157 | for (i = 0; i < 7; i++) |
158 | m_FontsSizes[i] = sizes[i]; | |
159 | ||
5526e819 VS |
160 | m_FontFaceFixed = fixed_face; |
161 | m_FontFaceNormal = normal_face; | |
3c8c8da2 | 162 | |
2b5f62a0 | 163 | #if !wxUSE_UNICODE |
b250d384 | 164 | SetInputEncoding(m_InputEnc); |
2b5f62a0 | 165 | #endif |
c9f56e70 VS |
166 | |
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++) { | |
3c8c8da2 | 172 | if (m_FontsTable[i][j][k][l][m] != NULL) |
e3c7fd79 | 173 | { |
c9f56e70 VS |
174 | delete m_FontsTable[i][j][k][l][m]; |
175 | m_FontsTable[i][j][k][l][m] = NULL; | |
176 | } | |
177 | } | |
5526e819 VS |
178 | } |
179 | ||
10e5c7ea VS |
180 | void wxHtmlWinParser::SetStandardFonts(int size, |
181 | const wxString& normal_face, | |
182 | const wxString& fixed_face) | |
7acd3625 | 183 | { |
ba804ab2 | 184 | if (size == -1) |
1d203ed8 | 185 | size = wxGetDefaultHTMLFontSize(); |
d1da8872 | 186 | |
7acd3625 | 187 | int f_sizes[7]; |
ba804ab2 VZ |
188 | wxBuildFontSizes(f_sizes, size); |
189 | ||
190 | wxString normal = normal_face; | |
191 | if ( normal.empty() ) | |
192 | normal = wxNORMAL_FONT->GetFaceName(); | |
d1da8872 | 193 | |
10e5c7ea | 194 | SetFonts(normal, fixed_face, f_sizes); |
7acd3625 RD |
195 | } |
196 | ||
5526e819 VS |
197 | void wxHtmlWinParser::InitParser(const wxString& source) |
198 | { | |
199 | wxHtmlParser::InitParser(source); | |
2b5f62a0 | 200 | wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!")); |
5526e819 VS |
201 | |
202 | m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE; | |
f2c2fa4d | 203 | m_FontSize = 3; //default one |
5526e819 | 204 | CreateCurrentFont(); // we're selecting default font into |
03647350 | 205 | |
5620726f RR |
206 | // we're not using GetCharWidth/Height() because of |
207 | // differences under X and win | |
208 | wxCoord w,h; | |
209 | m_DC->GetTextExtent( wxT("H"), &w, &h); | |
210 | m_CharWidth = w; | |
211 | m_CharHeight = h; | |
5526e819 | 212 | |
d1da8872 | 213 | m_UseLink = false; |
9548f380 | 214 | m_Link = wxHtmlLinkInfo( wxEmptyString ); |
5526e819 VS |
215 | m_LinkColor.Set(0, 0, 0xFF); |
216 | m_ActualColor.Set(0, 0, 0); | |
f5413b87 VZ |
217 | const wxColour windowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ; |
218 | m_ActualBackgroundColor = m_windowInterface | |
219 | ? m_windowInterface->GetHTMLBackgroundColour() | |
220 | : windowColour; | |
3ee9771b | 221 | m_ActualBackgroundMode = wxTRANSPARENT; |
efba2b89 | 222 | m_Align = wxHTML_ALIGN_LEFT; |
3c115835 VS |
223 | m_ScriptMode = wxHTML_SCRIPT_NORMAL; |
224 | m_ScriptBaseline = 0; | |
d1da8872 | 225 | m_tmpLastWasSpace = false; |
b6d93b26 | 226 | m_lastWordCell = NULL; |
5526e819 | 227 | |
69328c1a VS |
228 | // open the toplevel container that contains everything else and that |
229 | // is never closed (this makes parser's life easier): | |
5526e819 | 230 | OpenContainer(); |
69328c1a VS |
231 | |
232 | // then open the first container into which page's content will go: | |
5526e819 | 233 | OpenContainer(); |
2b5f62a0 | 234 | |
fa2f5d3b | 235 | #if !wxUSE_UNICODE |
2b5f62a0 VZ |
236 | wxString charset = ExtractCharsetInformation(source); |
237 | if (!charset.empty()) | |
238 | { | |
239 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset); | |
240 | if (enc != wxFONTENCODING_SYSTEM) | |
241 | SetInputEncoding(enc); | |
242 | } | |
243 | #endif | |
244 | ||
4f9297b0 | 245 | m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor)); |
bc55e31b VS |
246 | |
247 | m_Container->InsertCell | |
248 | ( | |
249 | new wxHtmlColourCell | |
250 | ( | |
f5413b87 | 251 | m_ActualBackgroundColor, |
3ee9771b | 252 | m_ActualBackgroundMode == wxTRANSPARENT ? wxHTML_CLR_TRANSPARENT_BACKGROUND : wxHTML_CLR_BACKGROUND |
bc55e31b VS |
253 | ) |
254 | ); | |
255 | ||
4f9297b0 | 256 | m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont())); |
5526e819 VS |
257 | } |
258 | ||
5526e819 VS |
259 | void wxHtmlWinParser::DoneParser() |
260 | { | |
261 | m_Container = NULL; | |
2b5f62a0 VZ |
262 | #if !wxUSE_UNICODE |
263 | SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call | |
264 | #endif | |
5526e819 VS |
265 | wxHtmlParser::DoneParser(); |
266 | } | |
267 | ||
bc55e31b VS |
268 | #if WXWIN_COMPATIBILITY_2_6 |
269 | wxHtmlWindow *wxHtmlWinParser::GetWindow() | |
270 | { | |
271 | if (!m_windowInterface) | |
272 | return NULL; | |
273 | return wxDynamicCast(m_windowInterface->GetHTMLWindow(), wxHtmlWindow); | |
274 | } | |
275 | #endif | |
276 | ||
5526e819 VS |
277 | wxObject* wxHtmlWinParser::GetProduct() |
278 | { | |
279 | wxHtmlContainerCell *top; | |
280 | ||
281 | CloseContainer(); | |
282 | OpenContainer(); | |
67cfebc2 | 283 | |
5526e819 | 284 | top = m_Container; |
4f9297b0 | 285 | while (top->GetParent()) top = top->GetParent(); |
ace0fab4 VS |
286 | top->RemoveExtraSpacing(true, true); |
287 | ||
5526e819 VS |
288 | return top; |
289 | } | |
290 | ||
0423bdc7 | 291 | wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type, |
6cc4e6b8 | 292 | const wxString& url) const |
04db5c3f | 293 | { |
bc55e31b VS |
294 | if ( !m_windowInterface ) |
295 | return wxHtmlParser::OpenURL(type, url); | |
296 | ||
297 | wxString myurl(url); | |
298 | wxHtmlOpeningStatus status; | |
299 | for (;;) | |
6cc4e6b8 | 300 | { |
bc55e31b | 301 | wxString myfullurl(myurl); |
4bfa3189 | 302 | |
bc55e31b VS |
303 | // consider url as absolute path first |
304 | wxURI current(myurl); | |
305 | myfullurl = current.BuildUnescapedURI(); | |
4bfa3189 | 306 | |
bc55e31b VS |
307 | // if not absolute then ... |
308 | if( current.IsReference() ) | |
309 | { | |
310 | wxString basepath = GetFS()->GetPath(); | |
311 | wxURI base(basepath); | |
4bfa3189 | 312 | |
bc55e31b VS |
313 | // ... try to apply base path if valid ... |
314 | if( !base.IsReference() ) | |
315 | { | |
316 | wxURI path(myfullurl); | |
317 | path.Resolve( base ); | |
318 | myfullurl = path.BuildUnescapedURI(); | |
319 | } | |
320 | else | |
321 | { | |
322 | // ... or force such addition if not included already | |
323 | if( !current.GetPath().Contains(base.GetPath()) ) | |
4bfa3189 | 324 | { |
bc55e31b VS |
325 | basepath += myurl; |
326 | wxURI connected( basepath ); | |
327 | myfullurl = connected.BuildUnescapedURI(); | |
4bfa3189 WS |
328 | } |
329 | } | |
6cc4e6b8 | 330 | } |
0423bdc7 | 331 | |
bc55e31b VS |
332 | wxString redirect; |
333 | status = m_windowInterface->OnHTMLOpeningURL(type, myfullurl, &redirect); | |
334 | if ( status != wxHTML_REDIRECT ) | |
335 | break; | |
2c892c0b | 336 | |
bc55e31b | 337 | myurl = redirect; |
6cc4e6b8 | 338 | } |
2c892c0b | 339 | |
bc55e31b VS |
340 | if ( status == wxHTML_BLOCK ) |
341 | return NULL; | |
342 | ||
5c4035d6 MW |
343 | int flags = wxFS_READ; |
344 | if (type == wxHTML_URL_IMAGE) | |
345 | flags |= wxFS_SEEKABLE; | |
346 | ||
347 | return GetFS()->OpenFile(myurl, flags); | |
04db5c3f | 348 | } |
5526e819 | 349 | |
50967574 VS |
350 | #define NBSP_UNICODE_VALUE (wxChar(160)) |
351 | #if !wxUSE_UNICODE | |
352 | #define CUR_NBSP_VALUE m_nbsp | |
353 | #else | |
354 | #define CUR_NBSP_VALUE NBSP_UNICODE_VALUE | |
355 | #endif | |
356 | ||
5bce3e6f | 357 | void wxHtmlWinParser::AddText(const wxString& txt) |
5526e819 | 358 | { |
6a603a10 VS |
359 | #if !wxUSE_UNICODE |
360 | if ( m_nbsp == 0 ) | |
361 | m_nbsp = GetEntitiesParser()->GetCharForCode(NBSP_UNICODE_VALUE); | |
6a603a10 | 362 | #endif |
211dfedd | 363 | |
6a603a10 | 364 | if ( m_whitespaceMode == Whitespace_Normal ) |
211dfedd | 365 | { |
6a603a10 | 366 | int templen = 0; |
5bce3e6f | 367 | |
6a603a10 VS |
368 | size_t lng = txt.length(); |
369 | if (lng+1 > m_tmpStrBufSize) | |
5bce3e6f | 370 | { |
6a603a10 VS |
371 | delete[] m_tmpStrBuf; |
372 | m_tmpStrBuf = new wxChar[lng+1]; | |
373 | m_tmpStrBufSize = lng+1; | |
5bce3e6f | 374 | } |
6a603a10 | 375 | wxChar *temp = m_tmpStrBuf; |
5526e819 | 376 | |
6a603a10 VS |
377 | wxString::const_iterator i = txt.begin(); |
378 | const wxString::const_iterator end = txt.end(); | |
379 | ||
380 | if (m_tmpLastWasSpace) | |
e3c7fd79 | 381 | { |
5bce3e6f | 382 | while ( (i < end) && |
6a603a10 VS |
383 | (*i == wxT('\n') || *i == wxT('\r') || *i == wxT(' ') || |
384 | *i == wxT('\t')) ) | |
5bce3e6f VS |
385 | { |
386 | ++i; | |
5bce3e6f | 387 | } |
5526e819 | 388 | } |
5526e819 | 389 | |
6a603a10 | 390 | while (i < end) |
e3c7fd79 | 391 | { |
6a603a10 | 392 | size_t x = 0; |
50967574 | 393 | const wxChar d = temp[templen++] = *i; |
6a603a10 VS |
394 | if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t'))) |
395 | { | |
396 | ++i, ++x; | |
397 | while ( (i < end) && | |
398 | (*i == wxT('\n') || *i == wxT('\r') || | |
399 | *i == wxT(' ') || *i == wxT('\t')) ) | |
400 | { | |
401 | ++i; | |
402 | ++x; | |
403 | } | |
404 | } | |
405 | else | |
406 | { | |
407 | ++i; | |
408 | } | |
409 | ||
6a603a10 VS |
410 | if (x) |
411 | { | |
412 | temp[templen-1] = wxT(' '); | |
413 | FlushWordBuf(temp, templen); | |
414 | m_tmpLastWasSpace = true; | |
415 | } | |
5526e819 | 416 | } |
af035b26 | 417 | |
6a603a10 VS |
418 | if (templen && (templen > 1 || temp[0] != wxT(' '))) |
419 | { | |
420 | FlushWordBuf(temp, templen); | |
421 | m_tmpLastWasSpace = false; | |
422 | } | |
423 | } | |
424 | else // m_whitespaceMode == Whitespace_Pre | |
4f9297b0 | 425 | { |
6a603a10 VS |
426 | if ( txt.find(CUR_NBSP_VALUE) != wxString::npos ) |
427 | { | |
428 | // we need to substitute spaces for here just like we | |
429 | // did in the Whitespace_Normal branch above | |
430 | wxString txt2(txt); | |
431 | txt2.Replace(CUR_NBSP_VALUE, ' '); | |
432 | AddPreBlock(txt2); | |
433 | } | |
434 | else | |
435 | { | |
436 | AddPreBlock(txt); | |
437 | } | |
438 | ||
439 | // don't eat any whitespace in <pre> block | |
97eac136 VS |
440 | m_tmpLastWasSpace = false; |
441 | } | |
442 | } | |
443 | ||
6a603a10 | 444 | void wxHtmlWinParser::FlushWordBuf(wxChar *buf, int& len) |
97eac136 | 445 | { |
6a603a10 | 446 | buf[len] = 0; |
c7820964 | 447 | |
50967574 VS |
448 | for ( int i = 0; i < len; i++ ) |
449 | { | |
450 | if ( buf[i] == CUR_NBSP_VALUE ) | |
451 | buf[i] = ' '; | |
452 | } | |
453 | ||
2b5f62a0 | 454 | #if !wxUSE_UNICODE |
97eac136 | 455 | if (m_EncConv) |
6a603a10 | 456 | m_EncConv->Convert(buf); |
2b5f62a0 | 457 | #endif |
97eac136 | 458 | |
6a603a10 | 459 | AddWord(wxString(buf, len)); |
97eac136 | 460 | |
6a603a10 VS |
461 | len = 0; |
462 | } | |
463 | ||
464 | void wxHtmlWinParser::AddWord(wxHtmlWordCell *word) | |
465 | { | |
466 | ApplyStateToCell(word); | |
3c115835 | 467 | |
6a603a10 VS |
468 | m_Container->InsertCell(word); |
469 | word->SetPreviousWord(m_lastWordCell); | |
470 | m_lastWordCell = word; | |
5526e819 VS |
471 | } |
472 | ||
6a603a10 VS |
473 | void wxHtmlWinParser::AddPreBlock(const wxString& text) |
474 | { | |
475 | if ( text.find('\t') != wxString::npos ) | |
476 | { | |
477 | wxString text2; | |
478 | text2.reserve(text.length()); | |
479 | ||
480 | const wxString::const_iterator end = text.end(); | |
481 | wxString::const_iterator copyFrom = text.begin(); | |
6a603a10 VS |
482 | size_t pos = 0; |
483 | int posColumn = m_posColumn; | |
484 | for ( wxString::const_iterator i = copyFrom; i != end; ++i, ++pos ) | |
485 | { | |
486 | if ( *i == '\t' ) | |
487 | { | |
488 | if ( copyFrom != i ) | |
489 | text2.append(copyFrom, i); | |
490 | ||
491 | const unsigned SPACES_PER_TAB = 8; | |
492 | const size_t expandTo = SPACES_PER_TAB - posColumn % SPACES_PER_TAB; | |
493 | text2.append(expandTo, ' '); | |
494 | ||
495 | posColumn += expandTo; | |
496 | copyFrom = i + 1; | |
6a603a10 VS |
497 | } |
498 | else | |
499 | { | |
500 | ++posColumn; | |
501 | } | |
502 | } | |
503 | if ( copyFrom != text.end() ) | |
504 | text2.append(copyFrom, text.end()); | |
505 | ||
506 | AddWord(new wxHtmlWordWithTabsCell(text2, text, m_posColumn, *(GetDC()))); | |
507 | ||
508 | m_posColumn = posColumn; | |
509 | } | |
510 | else | |
511 | { | |
512 | // no special formatting needed | |
513 | AddWord(text); | |
514 | m_posColumn += text.length(); | |
515 | } | |
516 | } | |
5526e819 VS |
517 | |
518 | ||
519 | wxHtmlContainerCell* wxHtmlWinParser::OpenContainer() | |
520 | { | |
521 | m_Container = new wxHtmlContainerCell(m_Container); | |
4f9297b0 | 522 | m_Container->SetAlignHor(m_Align); |
6a603a10 | 523 | m_posColumn = 0; |
d1da8872 | 524 | m_tmpLastWasSpace = true; |
5526e819 VS |
525 | /* to avoid space being first character in paragraph */ |
526 | return m_Container; | |
527 | } | |
528 | ||
529 | ||
530 | ||
531 | wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c) | |
532 | { | |
d1da8872 | 533 | m_tmpLastWasSpace = true; |
5526e819 VS |
534 | /* to avoid space being first character in paragraph */ |
535 | return m_Container = c; | |
536 | } | |
537 | ||
538 | ||
539 | ||
540 | wxHtmlContainerCell* wxHtmlWinParser::CloseContainer() | |
541 | { | |
4f9297b0 | 542 | m_Container = m_Container->GetParent(); |
5526e819 VS |
543 | return m_Container; |
544 | } | |
545 | ||
546 | ||
f2c2fa4d VS |
547 | void wxHtmlWinParser::SetFontSize(int s) |
548 | { | |
8aef90e8 VS |
549 | if (s < 1) |
550 | s = 1; | |
551 | else if (s > 7) | |
552 | s = 7; | |
f2c2fa4d VS |
553 | m_FontSize = s; |
554 | } | |
555 | ||
556 | ||
c44a49b8 VS |
557 | void wxHtmlWinParser::SetDC(wxDC *dc, double pixel_scale, double font_scale) |
558 | { | |
559 | m_DC = dc; | |
560 | m_PixelScale = pixel_scale; | |
561 | m_FontScale = font_scale; | |
562 | } | |
563 | ||
f68e16c5 VZ |
564 | void wxHtmlWinParser::SetFontPointSize(int pt) |
565 | { | |
566 | if (pt <= m_FontsSizes[0]) | |
567 | m_FontSize = 1; | |
568 | else if (pt >= m_FontsSizes[6]) | |
569 | m_FontSize = 7; | |
570 | else | |
571 | { | |
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++ ) | |
575 | { | |
576 | if ( (pt > m_FontsSizes[n]) && (pt <= m_FontsSizes[n + 1]) ) | |
577 | { | |
0199dae3 VZ |
578 | if ( (pt - m_FontsSizes[n]) >= (m_FontsSizes[n + 1] - pt) ) |
579 | { | |
580 | // The actual size is closer to the next entry than to this | |
581 | // one, so use it. | |
582 | n++; | |
583 | } | |
584 | ||
585 | // Notice that m_FontSize starts from 1, hence +1 here. | |
586 | m_FontSize = n + 1; | |
f68e16c5 VZ |
587 | |
588 | break; | |
589 | } | |
590 | } | |
591 | } | |
592 | } | |
f2c2fa4d | 593 | |
5526e819 VS |
594 | wxFont* wxHtmlWinParser::CreateCurrentFont() |
595 | { | |
596 | int fb = GetFontBold(), | |
597 | fi = GetFontItalic(), | |
598 | fu = GetFontUnderlined(), | |
599 | ff = GetFontFixed(), | |
f2c2fa4d | 600 | fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ; |
5526e819 | 601 | |
f1ad10f3 VS |
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]); | |
2b5f62a0 | 605 | #if !wxUSE_UNICODE |
b250d384 | 606 | wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]); |
2b5f62a0 | 607 | #endif |
f1ad10f3 | 608 | |
2b5f62a0 VZ |
609 | if (*fontptr != NULL && (*faceptr != face |
610 | #if !wxUSE_UNICODE | |
611 | || *encptr != m_OutputEnc | |
612 | #endif | |
613 | )) | |
4f9297b0 | 614 | { |
5276b0a5 | 615 | wxDELETE(*fontptr); |
f1ad10f3 VS |
616 | } |
617 | ||
3c8c8da2 | 618 | if (*fontptr == NULL) |
4f9297b0 | 619 | { |
f1ad10f3 VS |
620 | *faceptr = face; |
621 | *fontptr = new wxFont( | |
c44a49b8 | 622 | (int) (m_FontsSizes[fs] * m_FontScale), |
f1ad10f3 VS |
623 | ff ? wxMODERN : wxSWISS, |
624 | fi ? wxITALIC : wxNORMAL, | |
625 | fb ? wxBOLD : wxNORMAL, | |
d1da8872 | 626 | fu ? true : false, face |
2b5f62a0 VZ |
627 | #if wxUSE_UNICODE |
628 | ); | |
629 | #else | |
630 | , m_OutputEnc); | |
631 | *encptr = m_OutputEnc; | |
632 | #endif | |
5526e819 | 633 | } |
4f9297b0 | 634 | m_DC->SetFont(**fontptr); |
f1ad10f3 | 635 | return (*fontptr); |
5526e819 VS |
636 | } |
637 | ||
638 | ||
639 | ||
f2c2fa4d VS |
640 | void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link) |
641 | { | |
3c8c8da2 | 642 | m_Link = link; |
f2c2fa4d VS |
643 | m_UseLink = (link.GetHref() != wxEmptyString); |
644 | } | |
645 | ||
3c8c8da2 | 646 | void wxHtmlWinParser::SetFontFace(const wxString& face) |
b250d384 | 647 | { |
8aef90e8 VS |
648 | if (GetFontFixed()) |
649 | m_FontFaceFixed = face; | |
650 | else | |
651 | m_FontFaceNormal = face; | |
b250d384 | 652 | |
2b5f62a0 | 653 | #if !wxUSE_UNICODE |
b250d384 VS |
654 | if (m_InputEnc != wxFONTENCODING_DEFAULT) |
655 | SetInputEncoding(m_InputEnc); | |
2b5f62a0 | 656 | #endif |
b250d384 VS |
657 | } |
658 | ||
3c115835 VS |
659 | void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell *cell) |
660 | { | |
661 | // set the link: | |
662 | if (m_UseLink) | |
663 | cell->SetLink(GetLink()); | |
664 | ||
665 | // apply current script mode settings: | |
666 | cell->SetScriptMode(GetScriptMode(), GetScriptBaseline()); | |
667 | } | |
b250d384 VS |
668 | |
669 | ||
2b5f62a0 | 670 | #if !wxUSE_UNICODE |
b250d384 VS |
671 | void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) |
672 | { | |
c7820964 VS |
673 | // the character used for non-breakable space may change: |
674 | m_nbsp = 0; | |
675 | ||
b250d384 | 676 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; |
5276b0a5 | 677 | wxDELETE(m_EncConv); |
b250d384 | 678 | |
8aef90e8 VS |
679 | if (enc == wxFONTENCODING_DEFAULT) |
680 | return; | |
b250d384 VS |
681 | |
682 | wxFontEncoding altfix, altnorm; | |
683 | bool availfix, availnorm; | |
3c8c8da2 | 684 | |
142b3bc2 VS |
685 | availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal); |
686 | availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed); | |
8aef90e8 | 687 | |
3c8c8da2 | 688 | if (availnorm && availfix) |
8aef90e8 VS |
689 | { |
690 | // exact match? | |
b250d384 | 691 | m_OutputEnc = enc; |
8aef90e8 | 692 | } |
3c8c8da2 | 693 | |
d1da8872 WS |
694 | else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) && |
695 | wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) && | |
b250d384 | 696 | altnorm == altfix) |
8aef90e8 VS |
697 | { |
698 | // alternatives? | |
b250d384 | 699 | m_OutputEnc = altnorm; |
8aef90e8 | 700 | } |
b250d384 | 701 | else if (availnorm) |
8aef90e8 VS |
702 | { |
703 | // at least normal face? | |
b250d384 | 704 | m_OutputEnc = enc; |
8aef90e8 | 705 | } |
d1da8872 | 706 | else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false)) |
8aef90e8 | 707 | { |
b250d384 | 708 | m_OutputEnc = altnorm; |
8aef90e8 | 709 | } |
b250d384 | 710 | else |
c83e1237 SC |
711 | { |
712 | #ifndef __WXMAC__ | |
6a17b868 | 713 | // okay, let's convert to ISO_8859-1, available always |
b250d384 | 714 | m_OutputEnc = wxFONTENCODING_DEFAULT; |
90548138 | 715 | #else |
c83e1237 | 716 | m_OutputEnc = wxLocale::GetSystemEncoding() ; |
90548138 | 717 | #endif |
c83e1237 | 718 | } |
3c8c8da2 | 719 | |
b250d384 | 720 | m_InputEnc = enc; |
daa616fc | 721 | if (m_OutputEnc == wxFONTENCODING_DEFAULT) |
8aef90e8 | 722 | { |
daa616fc | 723 | GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM); |
8aef90e8 | 724 | } |
daa616fc | 725 | else |
8aef90e8 | 726 | { |
daa616fc | 727 | GetEntitiesParser()->SetEncoding(m_OutputEnc); |
8aef90e8 | 728 | } |
3c8c8da2 | 729 | |
8aef90e8 VS |
730 | if (m_InputEnc == m_OutputEnc) |
731 | return; | |
b250d384 VS |
732 | |
733 | m_EncConv = new wxEncodingConverter(); | |
3c8c8da2 | 734 | if (!m_EncConv->Init(m_InputEnc, |
b250d384 VS |
735 | (m_OutputEnc == wxFONTENCODING_DEFAULT) ? |
736 | wxFONTENCODING_ISO8859_1 : m_OutputEnc, | |
3c8c8da2 | 737 | wxCONVERT_SUBSTITUTE)) |
6a17b868 | 738 | { // total failure :-( |
3c8c8da2 VZ |
739 | wxLogError(_("Failed to display HTML document in %s encoding"), |
740 | wxFontMapper::GetEncodingName(enc).c_str()); | |
b250d384 | 741 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; |
5276b0a5 | 742 | wxDELETE(m_EncConv); |
b250d384 VS |
743 | } |
744 | } | |
2b5f62a0 | 745 | #endif |
b250d384 VS |
746 | |
747 | ||
f2c2fa4d | 748 | |
5526e819 VS |
749 | |
750 | //----------------------------------------------------------------------------- | |
751 | // wxHtmlWinTagHandler | |
752 | //----------------------------------------------------------------------------- | |
753 | ||
754 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler) | |
755 | ||
0d7acfb9 VZ |
756 | void wxHtmlWinTagHandler::ApplyStyle(const wxHtmlStyleParams &styleParams) |
757 | { | |
758 | wxString str; | |
759 | ||
760 | str = styleParams.GetParam(wxS("color")); | |
761 | if ( !str.empty() ) | |
762 | { | |
763 | wxColour clr; | |
764 | if ( wxHtmlTag::ParseAsColour(str, &clr) ) | |
765 | { | |
766 | m_WParser->SetActualColor(clr); | |
767 | m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr)); | |
768 | } | |
769 | } | |
770 | ||
771 | str = styleParams.GetParam(wxS("background-color")); | |
772 | if ( !str.empty() ) | |
773 | { | |
774 | wxColour clr; | |
775 | if ( wxHtmlTag::ParseAsColour(str, &clr) ) | |
776 | { | |
777 | m_WParser->SetActualBackgroundColor(clr); | |
778 | m_WParser->SetActualBackgroundMode(wxSOLID); | |
779 | m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND)); | |
780 | } | |
781 | } | |
782 | ||
783 | str = styleParams.GetParam(wxS("font-size")); | |
784 | if ( !str.empty() ) | |
785 | { | |
786 | // Point size | |
787 | int foundIndex = str.Find(wxS("pt")); | |
788 | if (foundIndex != wxNOT_FOUND) | |
789 | { | |
790 | str.Truncate(foundIndex); | |
791 | ||
792 | long sizeValue; | |
793 | if (str.ToLong(&sizeValue) == true) | |
794 | { | |
795 | // Set point size | |
796 | m_WParser->SetFontPointSize(sizeValue); | |
797 | m_WParser->GetContainer()->InsertCell( | |
798 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
799 | } | |
800 | } | |
801 | // else: check for other ways of specifying size (TODO) | |
802 | } | |
803 | ||
804 | str = styleParams.GetParam(wxS("font-weight")); | |
805 | if ( !str.empty() ) | |
806 | { | |
807 | // Only bold and normal supported just now | |
808 | if ( str == wxS("bold") ) | |
809 | { | |
810 | m_WParser->SetFontBold(true); | |
811 | m_WParser->GetContainer()->InsertCell( | |
812 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
813 | } | |
814 | else if ( str == wxS("normal") ) | |
815 | { | |
816 | m_WParser->SetFontBold(false); | |
817 | m_WParser->GetContainer()->InsertCell( | |
818 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
819 | } | |
820 | } | |
821 | ||
822 | str = styleParams.GetParam(wxS("font-style")); | |
823 | if ( !str.empty() ) | |
824 | { | |
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") ) | |
828 | { | |
829 | m_WParser->SetFontItalic(true); | |
830 | m_WParser->GetContainer()->InsertCell( | |
831 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
832 | } | |
833 | else if ( str == wxS("normal") ) | |
834 | { | |
835 | m_WParser->SetFontItalic(false); | |
836 | m_WParser->GetContainer()->InsertCell( | |
837 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
838 | } | |
839 | } | |
840 | ||
841 | str = styleParams.GetParam(wxS("text-decoration")); | |
842 | if ( !str.empty() ) | |
843 | { | |
844 | // Only underline is supported. | |
845 | if ( str == wxS("underline") ) | |
846 | { | |
847 | m_WParser->SetFontUnderlined(true); | |
848 | m_WParser->GetContainer()->InsertCell( | |
849 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
850 | } | |
851 | } | |
852 | ||
853 | str = styleParams.GetParam(wxS("font-family")); | |
854 | if ( !str.empty() ) | |
855 | { | |
856 | m_WParser->SetFontFace(str); | |
857 | m_WParser->GetContainer()->InsertCell( | |
858 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
859 | } | |
860 | } | |
861 | ||
5526e819 VS |
862 | //----------------------------------------------------------------------------- |
863 | // wxHtmlTagsModule | |
864 | //----------------------------------------------------------------------------- | |
865 | ||
d6a6d666 VS |
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. | |
869 | // | |
870 | // Do not add any winpars.cpp shutdown or initialization code to it, | |
871 | // create a new module instead! | |
5526e819 VS |
872 | |
873 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule) | |
874 | ||
5526e819 VS |
875 | bool wxHtmlTagsModule::OnInit() |
876 | { | |
877 | wxHtmlWinParser::AddModule(this); | |
d1da8872 | 878 | return true; |
5526e819 VS |
879 | } |
880 | ||
5526e819 VS |
881 | void wxHtmlTagsModule::OnExit() |
882 | { | |
f6bcfd97 | 883 | wxHtmlWinParser::RemoveModule(this); |
5526e819 | 884 | } |
d6a6d666 | 885 | |
223d09f6 | 886 | #endif |