]>
Commit | Line | Data |
---|---|---|
b71e9aa4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
40989e46 | 2 | // Name: src/richtext/richtexthtml.cpp |
b71e9aa4 JS |
3 | // Purpose: HTML I/O for wxRichTextCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 2005-09-30 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
40989e46 | 16 | #pragma hdrstop |
b71e9aa4 JS |
17 | #endif |
18 | ||
19 | #if wxUSE_RICHTEXT | |
20 | ||
21 | #include "wx/richtext/richtexthtml.h" | |
50f65288 | 22 | #include "wx/richtext/richtextstyles.h" |
b71e9aa4 JS |
23 | |
24 | #ifndef WX_PRECOMP | |
b71e9aa4 JS |
25 | #endif |
26 | ||
27 | #include "wx/filename.h" | |
28 | #include "wx/wfstream.h" | |
29 | #include "wx/txtstrm.h" | |
30 | ||
d2d0adc7 JS |
31 | #if wxUSE_FILESYSTEM |
32 | #include "wx/filesys.h" | |
33 | #include "wx/fs_mem.h" | |
34 | #endif | |
35 | ||
b71e9aa4 JS |
36 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler) |
37 | ||
d2d0adc7 JS |
38 | int wxRichTextHTMLHandler::sm_fileCounter = 1; |
39 | ||
50f65288 WS |
40 | wxRichTextHTMLHandler::wxRichTextHTMLHandler(const wxString& name, const wxString& ext, int type) |
41 | : wxRichTextFileHandler(name, ext, type), m_buffer(NULL), m_font(false), m_inTable(false) | |
42 | { | |
43 | m_fontSizeMapping.Add(8); | |
44 | m_fontSizeMapping.Add(10); | |
45 | m_fontSizeMapping.Add(13); | |
46 | m_fontSizeMapping.Add(17); | |
47 | m_fontSizeMapping.Add(22); | |
48 | m_fontSizeMapping.Add(30); | |
49 | m_fontSizeMapping.Add(100); | |
50 | } | |
51 | ||
b71e9aa4 JS |
52 | /// Can we handle this filename (if using files)? By default, checks the extension. |
53 | bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const | |
54 | { | |
55 | wxString path, file, ext; | |
bd365871 | 56 | wxFileName::SplitPath(filename, & path, & file, & ext); |
b71e9aa4 JS |
57 | |
58 | return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm")); | |
59 | } | |
60 | ||
61 | ||
62 | #if wxUSE_STREAMS | |
63 | bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream)) | |
64 | { | |
65 | return false; | |
66 | } | |
67 | ||
68 | /* | |
69 | * We need to output only _changes_ in character formatting. | |
70 | */ | |
71 | ||
72 | bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) | |
73 | { | |
50f65288 WS |
74 | m_buffer = buffer; |
75 | ||
d2d0adc7 JS |
76 | ClearTemporaryImageLocations(); |
77 | ||
f7667b84 JS |
78 | wxRichTextDrawingContext context(buffer); |
79 | buffer->Defragment(context); | |
b72812a6 | 80 | |
2c48f032 JS |
81 | #if wxUSE_UNICODE |
82 | wxCSConv* customEncoding = NULL; | |
83 | wxMBConv* conv = NULL; | |
84 | if (!GetEncoding().IsEmpty()) | |
b71e9aa4 | 85 | { |
2c48f032 JS |
86 | customEncoding = new wxCSConv(GetEncoding()); |
87 | if (!customEncoding->IsOk()) | |
b71e9aa4 | 88 | { |
5276b0a5 | 89 | wxDELETE(customEncoding); |
2c48f032 JS |
90 | } |
91 | } | |
92 | if (customEncoding) | |
93 | conv = customEncoding; | |
94 | else | |
95 | conv = & wxConvUTF8; | |
96 | #endif | |
40989e46 | 97 | |
2c48f032 JS |
98 | { |
99 | #if wxUSE_UNICODE | |
100 | wxTextOutputStream str(stream, wxEOL_NATIVE, *conv); | |
101 | #else | |
102 | wxTextOutputStream str(stream, wxEOL_NATIVE); | |
103 | #endif | |
4fdc2c5f | 104 | |
24777478 JS |
105 | wxRichTextAttr currentParaStyle = buffer->GetAttributes(); |
106 | wxRichTextAttr currentCharStyle = buffer->GetAttributes(); | |
4fdc2c5f | 107 | |
2c48f032 JS |
108 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) |
109 | str << wxT("<html><head></head><body>\n"); | |
4fdc2c5f | 110 | |
2c48f032 | 111 | OutputFont(currentParaStyle, str); |
4fdc2c5f | 112 | |
2c48f032 JS |
113 | m_font = false; |
114 | m_inTable = false; | |
4fdc2c5f | 115 | |
2c48f032 JS |
116 | m_indents.Clear(); |
117 | m_listTypes.Clear(); | |
4fdc2c5f | 118 | |
2c48f032 JS |
119 | wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst(); |
120 | while (node) | |
121 | { | |
122 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); | |
123 | wxASSERT (para != NULL); | |
4fdc2c5f | 124 | |
2c48f032 | 125 | if (para) |
b71e9aa4 | 126 | { |
24777478 | 127 | wxRichTextAttr paraStyle(para->GetCombinedAttributes()); |
4fdc2c5f | 128 | |
2c48f032 | 129 | BeginParagraphFormatting(currentParaStyle, paraStyle, str); |
4fdc2c5f | 130 | |
2c48f032 JS |
131 | wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst(); |
132 | while (node2) | |
b71e9aa4 | 133 | { |
2c48f032 JS |
134 | wxRichTextObject* obj = node2->GetData(); |
135 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); | |
136 | if (textObj && !textObj->IsEmpty()) | |
137 | { | |
24777478 | 138 | wxRichTextAttr charStyle(para->GetCombinedAttributes(obj->GetAttributes())); |
2c48f032 | 139 | BeginCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); |
4fdc2c5f | 140 | |
2c48f032 | 141 | wxString text = textObj->GetText(); |
4fdc2c5f | 142 | |
2c48f032 JS |
143 | if (charStyle.HasTextEffects() && (charStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)) |
144 | text.MakeUpper(); | |
4fdc2c5f | 145 | |
2c48f032 JS |
146 | wxString toReplace = wxRichTextLineBreakChar; |
147 | text.Replace(toReplace, wxT("<br>")); | |
4fdc2c5f | 148 | |
2c48f032 | 149 | str << text; |
4fdc2c5f | 150 | |
2c48f032 JS |
151 | EndCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); |
152 | } | |
4fdc2c5f | 153 | |
2c48f032 JS |
154 | wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage); |
155 | if( image && (!image->IsEmpty() || image->GetImageBlock().GetData())) | |
156 | WriteImage( image, stream ); | |
4fdc2c5f | 157 | |
2c48f032 | 158 | node2 = node2->GetNext(); |
b71e9aa4 | 159 | } |
4fdc2c5f | 160 | |
2c48f032 | 161 | EndParagraphFormatting(currentParaStyle, paraStyle, str); |
4fdc2c5f | 162 | |
2c48f032 | 163 | str << wxT("\n"); |
b71e9aa4 | 164 | } |
2c48f032 | 165 | node = node->GetNext(); |
b71e9aa4 | 166 | } |
4fdc2c5f | 167 | |
2c48f032 | 168 | CloseLists(-1, str); |
4fdc2c5f | 169 | |
2c48f032 | 170 | str << wxT("</font>"); |
4fdc2c5f | 171 | |
2c48f032 JS |
172 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) |
173 | str << wxT("</body></html>"); | |
4fdc2c5f | 174 | |
2c48f032 | 175 | str << wxT("\n"); |
b71e9aa4 | 176 | } |
b72812a6 | 177 | |
2c48f032 JS |
178 | #if wxUSE_UNICODE |
179 | if (customEncoding) | |
180 | delete customEncoding; | |
181 | #endif | |
40989e46 | 182 | |
50f65288 WS |
183 | m_buffer = NULL; |
184 | ||
b71e9aa4 JS |
185 | return true; |
186 | } | |
187 | ||
24777478 | 188 | void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, const wxRichTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& str) |
b71e9aa4 | 189 | { |
2dec6761 | 190 | wxString style; |
40989e46 | 191 | |
d2d0adc7 | 192 | // Is there any change in the font properties of the item? |
44cc96a8 | 193 | if (thisStyle.GetFontFaceName() != currentStyle.GetFontFaceName()) |
d2d0adc7 | 194 | { |
44cc96a8 | 195 | wxString faceName(thisStyle.GetFontFaceName()); |
d2d0adc7 JS |
196 | style += wxString::Format(wxT(" face=\"%s\""), faceName.c_str()); |
197 | } | |
44cc96a8 JS |
198 | if (thisStyle.GetFontSize() != currentStyle.GetFontSize()) |
199 | style += wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle.GetFontSize())); | |
5701f833 JS |
200 | |
201 | bool bTextColourChanged = (thisStyle.GetTextColour() != currentStyle.GetTextColour()); | |
202 | bool bBackgroundColourChanged = (thisStyle.GetBackgroundColour() != currentStyle.GetBackgroundColour()); | |
203 | if (bTextColourChanged || bBackgroundColourChanged) | |
d2d0adc7 | 204 | { |
5701f833 JS |
205 | style += wxT(" style=\""); |
206 | ||
207 | if (bTextColourChanged) | |
208 | { | |
209 | wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX)); | |
210 | style += wxString::Format(wxT("color: %s"), color.c_str()); | |
211 | } | |
212 | if (bTextColourChanged && bBackgroundColourChanged) | |
213 | style += wxT(";"); | |
214 | if (bBackgroundColourChanged) | |
215 | { | |
216 | wxString color(thisStyle.GetBackgroundColour().GetAsString(wxC2S_HTML_SYNTAX)); | |
217 | style += wxString::Format(wxT("background-color: %s"), color.c_str()); | |
218 | } | |
219 | ||
220 | style += wxT("\""); | |
d2d0adc7 | 221 | } |
40989e46 | 222 | |
d2d0adc7 | 223 | if (style.size()) |
21e354f1 JS |
224 | { |
225 | str << wxString::Format(wxT("<font %s >"), style.c_str()); | |
226 | m_font = true; | |
227 | } | |
40989e46 | 228 | |
43f4e852 | 229 | if (thisStyle.GetFontWeight() == wxFONTWEIGHT_BOLD) |
2dec6761 | 230 | str << wxT("<b>"); |
43f4e852 | 231 | if (thisStyle.GetFontStyle() == wxFONTSTYLE_ITALIC) |
2dec6761 | 232 | str << wxT("<i>"); |
44cc96a8 | 233 | if (thisStyle.GetFontUnderlined()) |
2dec6761 | 234 | str << wxT("<u>"); |
b72812a6 | 235 | |
d2d0adc7 JS |
236 | if (thisStyle.HasURL()) |
237 | str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">"); | |
75936ec6 JS |
238 | |
239 | if (thisStyle.HasTextEffects()) | |
240 | { | |
241 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH) | |
242 | str << wxT("<del>"); | |
243 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) | |
244 | str << wxT("<sup>"); | |
245 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) | |
246 | str << wxT("<sub>"); | |
247 | } | |
b71e9aa4 JS |
248 | } |
249 | ||
24777478 | 250 | void wxRichTextHTMLHandler::EndCharacterFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, const wxRichTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream) |
b71e9aa4 | 251 | { |
d2d0adc7 | 252 | if (thisStyle.HasURL()) |
50f65288 | 253 | stream << wxT("</a>"); |
d2d0adc7 | 254 | |
44cc96a8 | 255 | if (thisStyle.GetFontUnderlined()) |
50f65288 | 256 | stream << wxT("</u>"); |
43f4e852 | 257 | if (thisStyle.GetFontStyle() == wxFONTSTYLE_ITALIC) |
50f65288 | 258 | stream << wxT("</i>"); |
43f4e852 | 259 | if (thisStyle.GetFontWeight() == wxFONTWEIGHT_BOLD) |
50f65288 | 260 | stream << wxT("</b>"); |
40989e46 | 261 | |
75936ec6 JS |
262 | if (thisStyle.HasTextEffects()) |
263 | { | |
264 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH) | |
265 | stream << wxT("</del>"); | |
266 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) | |
267 | stream << wxT("</sup>"); | |
268 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) | |
269 | stream << wxT("</sub>"); | |
270 | } | |
271 | ||
d2d0adc7 | 272 | if (m_font) |
2dec6761 JS |
273 | { |
274 | m_font = false; | |
50f65288 | 275 | stream << wxT("</font>"); |
2dec6761 JS |
276 | } |
277 | } | |
b71e9aa4 | 278 | |
50f65288 | 279 | /// Begin paragraph formatting |
24777478 | 280 | void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, wxTextOutputStream& str) |
2dec6761 | 281 | { |
42688aea JS |
282 | if (thisStyle.HasPageBreak()) |
283 | { | |
42688aea JS |
284 | str << wxT("<div style=\"page-break-after:always\"></div>\n"); |
285 | } | |
40989e46 | 286 | |
7947a11d | 287 | if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0) |
2dec6761 | 288 | { |
50f65288 | 289 | if (thisStyle.HasBulletStyle()) |
2dec6761 | 290 | { |
50f65288 WS |
291 | int indent = thisStyle.GetLeftIndent(); |
292 | ||
293 | // Close levels high than this | |
294 | CloseLists(indent, str); | |
b72812a6 | 295 | |
50f65288 WS |
296 | if (m_indents.GetCount() > 0 && indent == m_indents.Last()) |
297 | { | |
298 | // Same level, no need to start a new list | |
299 | } | |
300 | else if (m_indents.GetCount() == 0 || indent > m_indents.Last()) | |
301 | { | |
302 | m_indents.Add(indent); | |
b72812a6 | 303 | |
50f65288 WS |
304 | wxString tag; |
305 | int listType = TypeOfList(thisStyle, tag); | |
306 | m_listTypes.Add(listType); | |
b72812a6 | 307 | |
e5163682 JS |
308 | // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs. |
309 | // TODO: pass a flag that indicates we're using wxHTML. | |
310 | str << wxT("<p>\n"); | |
b72812a6 | 311 | |
50f65288 WS |
312 | str << tag; |
313 | } | |
b72812a6 | 314 | |
50f65288 | 315 | str << wxT("<li> "); |
2dec6761 | 316 | } |
2dec6761 JS |
317 | else |
318 | { | |
50f65288 | 319 | CloseLists(-1, str); |
b72812a6 | 320 | |
50f65288 | 321 | wxString align = GetAlignment(thisStyle); |
2b2c1044 JS |
322 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); |
323 | ||
4fdc2c5f JS |
324 | wxString styleStr; |
325 | ||
326 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore()) | |
327 | { | |
328 | float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0; | |
329 | ||
330 | styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM); | |
331 | } | |
332 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter()) | |
333 | { | |
334 | float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0; | |
335 | ||
336 | styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM); | |
337 | } | |
338 | ||
339 | float indentLeftMM = (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent())/10.0; | |
340 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (indentLeftMM > 0.0)) | |
341 | { | |
342 | styleStr += wxString::Format(wxT("margin-left: %.2fmm; "), indentLeftMM); | |
343 | } | |
344 | float indentRightMM = thisStyle.GetRightIndent()/10.0; | |
345 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasRightIndent() && (indentRightMM > 0.0)) | |
346 | { | |
347 | styleStr += wxString::Format(wxT("margin-right: %.2fmm; "), indentRightMM); | |
348 | } | |
349 | // First line indentation | |
350 | float firstLineIndentMM = - thisStyle.GetLeftSubIndent() / 10.0; | |
351 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (firstLineIndentMM > 0.0)) | |
352 | { | |
353 | styleStr += wxString::Format(wxT("text-indent: %.2fmm; "), firstLineIndentMM); | |
354 | } | |
355 | ||
356 | if (!styleStr.IsEmpty()) | |
357 | str << wxT(" style=\"") << styleStr << wxT("\""); | |
2b2c1044 JS |
358 | |
359 | str << wxT(">"); | |
40989e46 | 360 | |
50f65288 | 361 | // TODO: convert to pixels |
945c909d | 362 | int indentPixels = static_cast<int>(indentLeftMM*10/4); |
4fdc2c5f JS |
363 | |
364 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) | |
365 | { | |
366 | // Use a table to do indenting if we don't have CSS | |
367 | str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels); | |
368 | m_inTable = true; | |
369 | } | |
50f65288 | 370 | |
4fdc2c5f | 371 | if (((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) && (thisStyle.GetLeftSubIndent() < 0)) |
d2d0adc7 | 372 | { |
50f65288 | 373 | str << SymbolicIndent( - thisStyle.GetLeftSubIndent()); |
d2d0adc7 | 374 | } |
2dec6761 JS |
375 | } |
376 | } | |
50f65288 WS |
377 | else |
378 | { | |
379 | CloseLists(-1, str); | |
380 | ||
381 | wxString align = GetAlignment(thisStyle); | |
2b2c1044 JS |
382 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); |
383 | ||
4fdc2c5f JS |
384 | wxString styleStr; |
385 | ||
386 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore()) | |
387 | { | |
388 | float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0; | |
389 | ||
390 | styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM); | |
391 | } | |
392 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter()) | |
393 | { | |
394 | float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0; | |
395 | ||
396 | styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM); | |
397 | } | |
398 | ||
399 | if (!styleStr.IsEmpty()) | |
400 | str << wxT(" style=\"") << styleStr << wxT("\""); | |
2b2c1044 JS |
401 | |
402 | str << wxT(">"); | |
b72812a6 | 403 | } |
7e7ced16 | 404 | OutputFont(thisStyle, str); |
2dec6761 | 405 | } |
50f65288 WS |
406 | |
407 | /// End paragraph formatting | |
24777478 | 408 | void wxRichTextHTMLHandler::EndParagraphFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, wxTextOutputStream& stream) |
2dec6761 | 409 | { |
7e7ced16 JS |
410 | if (thisStyle.HasFont()) |
411 | stream << wxT("</font>"); | |
412 | ||
50f65288 | 413 | if (m_inTable) |
2dec6761 | 414 | { |
c433798e | 415 | stream << wxT("</td></tr></table></p>\n"); |
50f65288 | 416 | m_inTable = false; |
2dec6761 | 417 | } |
e5163682 | 418 | else if (!thisStyle.HasBulletStyle()) |
c433798e | 419 | stream << wxT("</p>\n"); |
2dec6761 JS |
420 | } |
421 | ||
50f65288 WS |
422 | /// Closes lists to level (-1 means close all) |
423 | void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str) | |
424 | { | |
425 | // Close levels high than this | |
426 | int i = m_indents.GetCount()-1; | |
427 | while (i >= 0) | |
428 | { | |
429 | int l = m_indents[i]; | |
430 | if (l > level) | |
431 | { | |
432 | if (m_listTypes[i] == 0) | |
433 | str << wxT("</ol>"); | |
434 | else | |
435 | str << wxT("</ul>"); | |
436 | m_indents.RemoveAt(i); | |
437 | m_listTypes.RemoveAt(i); | |
438 | } | |
439 | else | |
440 | break; | |
441 | i --; | |
442 | } | |
443 | } | |
444 | ||
445 | /// Output font tag | |
24777478 | 446 | void wxRichTextHTMLHandler::OutputFont(const wxRichTextAttr& style, wxTextOutputStream& stream) |
2dec6761 | 447 | { |
50f65288 WS |
448 | if (style.HasFont()) |
449 | { | |
44cc96a8 | 450 | stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style.GetFontFaceName().c_str(), PtToSize(style.GetFontSize())); |
b72812a6 JS |
451 | if (style.HasTextColour()) |
452 | stream << wxString::Format(wxT(" color=\"%s\""), style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str()); | |
453 | stream << wxT(" >"); | |
50f65288 | 454 | } |
2dec6761 JS |
455 | } |
456 | ||
24777478 | 457 | int wxRichTextHTMLHandler::TypeOfList( const wxRichTextAttr& thisStyle, wxString& tag ) |
2dec6761 | 458 | { |
d2d0adc7 JS |
459 | // We can use number attribute of li tag but not all the browsers support it. |
460 | // also wxHtmlWindow doesn't support type attribute. | |
40989e46 | 461 | |
50f65288 | 462 | bool m_is_ul = false; |
d2d0adc7 | 463 | if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD)) |
2dec6761 | 464 | tag = wxT("<ol type=\"1\">"); |
d2d0adc7 | 465 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER) |
2dec6761 | 466 | tag = wxT("<ol type=\"A\">"); |
d2d0adc7 | 467 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER) |
2dec6761 | 468 | tag = wxT("<ol type=\"a\">"); |
d2d0adc7 | 469 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER) |
2dec6761 | 470 | tag = wxT("<ol type=\"I\">"); |
d2d0adc7 | 471 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER) |
2dec6761 | 472 | tag = wxT("<ol type=\"i\">"); |
b71e9aa4 JS |
473 | else |
474 | { | |
2dec6761 JS |
475 | tag = wxT("<ul>"); |
476 | m_is_ul = true; | |
b71e9aa4 | 477 | } |
b72812a6 | 478 | |
50f65288 WS |
479 | if (m_is_ul) |
480 | return 1; | |
481 | else | |
482 | return 0; | |
b71e9aa4 JS |
483 | } |
484 | ||
24777478 | 485 | wxString wxRichTextHTMLHandler::GetAlignment( const wxRichTextAttr& thisStyle ) |
2dec6761 JS |
486 | { |
487 | switch( thisStyle.GetAlignment() ) | |
488 | { | |
489 | case wxTEXT_ALIGNMENT_LEFT: | |
490 | return wxT("left"); | |
491 | case wxTEXT_ALIGNMENT_RIGHT: | |
492 | return wxT("right"); | |
493 | case wxTEXT_ALIGNMENT_CENTER: | |
494 | return wxT("center"); | |
495 | case wxTEXT_ALIGNMENT_JUSTIFIED: | |
496 | return wxT("justify"); | |
497 | default: | |
498 | return wxT("left"); | |
499 | } | |
500 | } | |
501 | ||
d2d0adc7 | 502 | void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream) |
2dec6761 JS |
503 | { |
504 | wxTextOutputStream str(stream); | |
40989e46 | 505 | |
2dec6761 | 506 | str << wxT("<img src=\""); |
40989e46 | 507 | |
d2d0adc7 JS |
508 | #if wxUSE_FILESYSTEM |
509 | if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) | |
510 | { | |
cdaed652 | 511 | #if 0 |
a1b806b9 | 512 | if (!image->GetImage().IsOk() && image->GetImageBlock().GetData()) |
d2d0adc7 | 513 | image->LoadFromBlock(); |
a1b806b9 | 514 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) |
d2d0adc7 | 515 | image->MakeBlock(); |
cdaed652 | 516 | #endif |
d2d0adc7 | 517 | |
cdaed652 | 518 | if (image->GetImageBlock().IsOk()) |
b72812a6 | 519 | { |
cdaed652 VZ |
520 | wxImage img; |
521 | image->GetImageBlock().Load(img); | |
522 | if (img.IsOk()) | |
523 | { | |
524 | wxString ext(image->GetImageBlock().GetExtension()); | |
43f4e852 | 525 | wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, ext.c_str())); |
cdaed652 | 526 | wxMemoryFSHandler::AddFile(tempFilename, img, image->GetImageBlock().GetImageType()); |
b72812a6 | 527 | |
cdaed652 | 528 | m_imageLocations.Add(tempFilename); |
b72812a6 | 529 | |
cdaed652 VZ |
530 | str << wxT("memory:") << tempFilename; |
531 | } | |
d2d0adc7 JS |
532 | } |
533 | else | |
534 | str << wxT("memory:?"); | |
40989e46 | 535 | |
d2d0adc7 JS |
536 | sm_fileCounter ++; |
537 | } | |
538 | else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
539 | { | |
cdaed652 | 540 | #if 0 |
a1b806b9 | 541 | if (!image->GetImage().IsOk() && image->GetImageBlock().GetData()) |
d2d0adc7 | 542 | image->LoadFromBlock(); |
a1b806b9 | 543 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) |
d2d0adc7 | 544 | image->MakeBlock(); |
cdaed652 | 545 | #endif |
d2d0adc7 | 546 | |
a1b806b9 | 547 | if (image->GetImageBlock().IsOk()) |
b72812a6 | 548 | { |
d2d0adc7 JS |
549 | wxString tempDir(GetTempDir()); |
550 | if (tempDir.IsEmpty()) | |
551 | tempDir = wxFileName::GetTempDir(); | |
b72812a6 | 552 | |
d2d0adc7 | 553 | wxString ext(image->GetImageBlock().GetExtension()); |
43f4e852 | 554 | wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir.c_str(), sm_fileCounter, ext.c_str())); |
d2d0adc7 | 555 | image->GetImageBlock().Write(tempFilename); |
b72812a6 | 556 | |
d2d0adc7 | 557 | m_imageLocations.Add(tempFilename); |
b72812a6 JS |
558 | |
559 | str << wxFileSystem::FileNameToURL(tempFilename); | |
d2d0adc7 JS |
560 | } |
561 | else | |
562 | str << wxT("file:?"); | |
40989e46 | 563 | |
d2d0adc7 JS |
564 | sm_fileCounter ++; |
565 | } | |
566 | else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied | |
567 | #endif | |
568 | { | |
569 | str << wxT("data:"); | |
570 | str << GetMimeType(image->GetImageBlock().GetImageType()); | |
571 | str << wxT(";base64,"); | |
cdaed652 | 572 | #if 0 |
a1b806b9 | 573 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) |
d2d0adc7 | 574 | image->MakeBlock(); |
cdaed652 | 575 | #endif |
a1b806b9 | 576 | if (image->GetImageBlock().IsOk()) |
cdaed652 VZ |
577 | { |
578 | wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() ); | |
579 | str << data; | |
d2d0adc7 | 580 | |
cdaed652 VZ |
581 | delete[] data; |
582 | } | |
d2d0adc7 | 583 | } |
40989e46 | 584 | |
2dec6761 JS |
585 | str << wxT("\" />"); |
586 | } | |
587 | ||
d2d0adc7 | 588 | long wxRichTextHTMLHandler::PtToSize(long size) |
2dec6761 | 589 | { |
50f65288 WS |
590 | int i; |
591 | int len = m_fontSizeMapping.GetCount(); | |
592 | for (i = 0; i < len; i++) | |
593 | if (size <= m_fontSizeMapping[i]) | |
594 | return i+1; | |
b72812a6 | 595 | return 7; |
2dec6761 JS |
596 | } |
597 | ||
598 | wxString wxRichTextHTMLHandler::SymbolicIndent(long indent) | |
599 | { | |
600 | wxString in; | |
601 | for(;indent > 0; indent -= 20) | |
602 | in.Append( wxT(" ") ); | |
603 | return in; | |
604 | } | |
605 | ||
21e354f1 | 606 | const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType) |
2dec6761 JS |
607 | { |
608 | switch(imageType) | |
609 | { | |
610 | case wxBITMAP_TYPE_BMP: | |
611 | return wxT("image/bmp"); | |
4ca8531f | 612 | case wxBITMAP_TYPE_TIFF: |
2dec6761 JS |
613 | return wxT("image/tiff"); |
614 | case wxBITMAP_TYPE_GIF: | |
615 | return wxT("image/gif"); | |
616 | case wxBITMAP_TYPE_PNG: | |
617 | return wxT("image/png"); | |
618 | case wxBITMAP_TYPE_JPEG: | |
619 | return wxT("image/jpeg"); | |
620 | default: | |
621 | return wxT("image/unknown"); | |
622 | } | |
623 | } | |
624 | ||
d2d0adc7 | 625 | // exim-style base64 encoder |
2dec6761 JS |
626 | wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len ) |
627 | { | |
d2d0adc7 JS |
628 | // elements of enc64 array must be 8 bit values |
629 | // otherwise encoder will fail | |
630 | // hmmm.. Does wxT macro define a char as 16 bit value | |
631 | // when compiling with UNICODE option? | |
21e354f1 | 632 | static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); |
2dec6761 JS |
633 | wxChar* output = new wxChar[4*((in_len+2)/3)+1]; |
634 | wxChar* p = output; | |
40989e46 | 635 | |
2dec6761 JS |
636 | while( in_len-- > 0 ) |
637 | { | |
638 | register wxChar a, b; | |
40989e46 | 639 | |
2dec6761 | 640 | a = *input++; |
40989e46 | 641 | |
2dec6761 | 642 | *p++ = enc64[ (a >> 2) & 0x3f ]; |
40989e46 | 643 | |
07854e5e | 644 | if( in_len-- == 0 ) |
2dec6761 JS |
645 | { |
646 | *p++ = enc64[ (a << 4 ) & 0x30 ]; | |
647 | *p++ = '='; | |
648 | *p++ = '='; | |
649 | break; | |
650 | } | |
40989e46 | 651 | |
2dec6761 | 652 | b = *input++; |
40989e46 | 653 | |
2dec6761 | 654 | *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f]; |
40989e46 | 655 | |
07854e5e | 656 | if( in_len-- == 0 ) |
2dec6761 JS |
657 | { |
658 | *p++ = enc64[ (b << 2) & 0x3f ]; | |
659 | *p++ = '='; | |
660 | break; | |
661 | } | |
40989e46 | 662 | |
2dec6761 | 663 | a = *input++; |
40989e46 | 664 | |
2dec6761 | 665 | *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ]; |
40989e46 | 666 | |
2dec6761 JS |
667 | *p++ = enc64[ a & 0x3f ]; |
668 | } | |
669 | *p = 0; | |
40989e46 | 670 | |
2dec6761 JS |
671 | return output; |
672 | } | |
b71e9aa4 | 673 | #endif |
2dec6761 | 674 | // wxUSE_STREAMS |
b71e9aa4 | 675 | |
d2d0adc7 JS |
676 | /// Delete the in-memory or temporary files generated by the last operation |
677 | bool wxRichTextHTMLHandler::DeleteTemporaryImages() | |
678 | { | |
679 | return DeleteTemporaryImages(GetFlags(), m_imageLocations); | |
680 | } | |
681 | ||
682 | /// Delete the in-memory or temporary files generated by the last operation | |
683 | bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations) | |
684 | { | |
685 | size_t i; | |
686 | for (i = 0; i < imageLocations.GetCount(); i++) | |
687 | { | |
688 | wxString location = imageLocations[i]; | |
b72812a6 | 689 | |
d2d0adc7 JS |
690 | if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) |
691 | { | |
692 | #if wxUSE_FILESYSTEM | |
693 | wxMemoryFSHandler::RemoveFile(location); | |
694 | #endif | |
695 | } | |
696 | else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
697 | { | |
698 | if (wxFileExists(location)) | |
699 | wxRemoveFile(location); | |
700 | } | |
701 | } | |
b72812a6 | 702 | |
d2d0adc7 JS |
703 | return true; |
704 | } | |
705 | ||
706 | ||
b71e9aa4 | 707 | #endif |
2dec6761 | 708 | // wxUSE_RICHTEXT |
d2d0adc7 | 709 |