]>
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 | ||
b71e9aa4 | 78 | buffer->Defragment(); |
b72812a6 | 79 | |
b71e9aa4 | 80 | wxTextOutputStream str(stream); |
40989e46 | 81 | |
44cc96a8 JS |
82 | wxTextAttr currentParaStyle = buffer->GetAttributes(); |
83 | wxTextAttr currentCharStyle = buffer->GetAttributes(); | |
40989e46 | 84 | |
b774c698 JS |
85 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) |
86 | str << wxT("<html><head></head><body>\n"); | |
40989e46 | 87 | |
50f65288 | 88 | OutputFont(currentParaStyle, str); |
b72812a6 | 89 | |
2dec6761 | 90 | m_font = false; |
50f65288 | 91 | m_inTable = false; |
b72812a6 | 92 | |
50f65288 WS |
93 | m_indents.Clear(); |
94 | m_listTypes.Clear(); | |
40989e46 | 95 | |
b71e9aa4 JS |
96 | wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst(); |
97 | while (node) | |
98 | { | |
99 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); | |
100 | wxASSERT (para != NULL); | |
40989e46 | 101 | |
b71e9aa4 JS |
102 | if (para) |
103 | { | |
44cc96a8 | 104 | wxTextAttr paraStyle(para->GetCombinedAttributes()); |
b72812a6 | 105 | |
50f65288 | 106 | BeginParagraphFormatting(currentParaStyle, paraStyle, str); |
40989e46 | 107 | |
b71e9aa4 JS |
108 | wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst(); |
109 | while (node2) | |
110 | { | |
111 | wxRichTextObject* obj = node2->GetData(); | |
112 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); | |
113 | if (textObj && !textObj->IsEmpty()) | |
114 | { | |
44cc96a8 | 115 | wxTextAttr charStyle(para->GetCombinedAttributes(obj->GetAttributes())); |
50f65288 | 116 | BeginCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); |
b72812a6 | 117 | |
42688aea | 118 | wxString text = textObj->GetText(); |
40989e46 | 119 | |
42688aea JS |
120 | if (charStyle.HasTextEffects() && (charStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)) |
121 | text.MakeUpper(); | |
122 | ||
50f65288 WS |
123 | wxString toReplace = wxRichTextLineBreakChar; |
124 | text.Replace(toReplace, wxT("<br>")); | |
125 | ||
42688aea | 126 | str << text; |
40989e46 | 127 | |
50f65288 | 128 | EndCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); |
b71e9aa4 | 129 | } |
40989e46 | 130 | |
2dec6761 | 131 | wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage); |
020b6011 | 132 | if( image && (!image->IsEmpty() || image->GetImageBlock().GetData())) |
d2d0adc7 | 133 | WriteImage( image, stream ); |
40989e46 | 134 | |
b71e9aa4 JS |
135 | node2 = node2->GetNext(); |
136 | } | |
50f65288 WS |
137 | |
138 | EndParagraphFormatting(currentParaStyle, paraStyle, str); | |
139 | ||
d5363f57 | 140 | str << wxT("\n"); |
b71e9aa4 | 141 | } |
b71e9aa4 JS |
142 | node = node->GetNext(); |
143 | } | |
b72812a6 | 144 | |
50f65288 | 145 | CloseLists(-1, str); |
40989e46 | 146 | |
50f65288 | 147 | str << wxT("</font>"); |
b72812a6 | 148 | |
b774c698 JS |
149 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) |
150 | str << wxT("</body></html>"); | |
b72812a6 | 151 | |
b774c698 | 152 | str << wxT("\n"); |
40989e46 | 153 | |
50f65288 WS |
154 | m_buffer = NULL; |
155 | ||
b71e9aa4 JS |
156 | return true; |
157 | } | |
158 | ||
44cc96a8 | 159 | void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttr& currentStyle, const wxTextAttr& thisStyle, const wxTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& str) |
b71e9aa4 | 160 | { |
2dec6761 | 161 | wxString style; |
40989e46 | 162 | |
d2d0adc7 | 163 | // Is there any change in the font properties of the item? |
44cc96a8 | 164 | if (thisStyle.GetFontFaceName() != currentStyle.GetFontFaceName()) |
d2d0adc7 | 165 | { |
44cc96a8 | 166 | wxString faceName(thisStyle.GetFontFaceName()); |
d2d0adc7 JS |
167 | style += wxString::Format(wxT(" face=\"%s\""), faceName.c_str()); |
168 | } | |
44cc96a8 JS |
169 | if (thisStyle.GetFontSize() != currentStyle.GetFontSize()) |
170 | style += wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle.GetFontSize())); | |
d2d0adc7 JS |
171 | if (thisStyle.GetTextColour() != currentStyle.GetTextColour() ) |
172 | { | |
173 | wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX)); | |
174 | style += wxString::Format(wxT(" color=\"%s\""), color.c_str()); | |
175 | } | |
40989e46 | 176 | |
d2d0adc7 | 177 | if (style.size()) |
21e354f1 JS |
178 | { |
179 | str << wxString::Format(wxT("<font %s >"), style.c_str()); | |
180 | m_font = true; | |
181 | } | |
40989e46 | 182 | |
44cc96a8 | 183 | if (thisStyle.GetFontWeight() == wxBOLD) |
2dec6761 | 184 | str << wxT("<b>"); |
44cc96a8 | 185 | if (thisStyle.GetFontStyle() == wxITALIC) |
2dec6761 | 186 | str << wxT("<i>"); |
44cc96a8 | 187 | if (thisStyle.GetFontUnderlined()) |
2dec6761 | 188 | str << wxT("<u>"); |
b72812a6 | 189 | |
d2d0adc7 JS |
190 | if (thisStyle.HasURL()) |
191 | str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">"); | |
b71e9aa4 JS |
192 | } |
193 | ||
44cc96a8 | 194 | void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, const wxTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream) |
b71e9aa4 | 195 | { |
d2d0adc7 | 196 | if (thisStyle.HasURL()) |
50f65288 | 197 | stream << wxT("</a>"); |
d2d0adc7 | 198 | |
44cc96a8 | 199 | if (thisStyle.GetFontUnderlined()) |
50f65288 | 200 | stream << wxT("</u>"); |
44cc96a8 | 201 | if (thisStyle.GetFontStyle() == wxITALIC) |
50f65288 | 202 | stream << wxT("</i>"); |
44cc96a8 | 203 | if (thisStyle.GetFontWeight() == wxBOLD) |
50f65288 | 204 | stream << wxT("</b>"); |
40989e46 | 205 | |
d2d0adc7 | 206 | if (m_font) |
2dec6761 JS |
207 | { |
208 | m_font = false; | |
50f65288 | 209 | stream << wxT("</font>"); |
2dec6761 JS |
210 | } |
211 | } | |
b71e9aa4 | 212 | |
50f65288 | 213 | /// Begin paragraph formatting |
44cc96a8 | 214 | void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& str) |
2dec6761 | 215 | { |
42688aea JS |
216 | if (thisStyle.HasPageBreak()) |
217 | { | |
42688aea JS |
218 | str << wxT("<div style=\"page-break-after:always\"></div>\n"); |
219 | } | |
40989e46 | 220 | |
7947a11d | 221 | if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0) |
2dec6761 | 222 | { |
50f65288 | 223 | if (thisStyle.HasBulletStyle()) |
2dec6761 | 224 | { |
50f65288 WS |
225 | int indent = thisStyle.GetLeftIndent(); |
226 | ||
227 | // Close levels high than this | |
228 | CloseLists(indent, str); | |
b72812a6 | 229 | |
50f65288 WS |
230 | if (m_indents.GetCount() > 0 && indent == m_indents.Last()) |
231 | { | |
232 | // Same level, no need to start a new list | |
233 | } | |
234 | else if (m_indents.GetCount() == 0 || indent > m_indents.Last()) | |
235 | { | |
236 | m_indents.Add(indent); | |
b72812a6 | 237 | |
50f65288 WS |
238 | wxString tag; |
239 | int listType = TypeOfList(thisStyle, tag); | |
240 | m_listTypes.Add(listType); | |
b72812a6 | 241 | |
e5163682 JS |
242 | // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs. |
243 | // TODO: pass a flag that indicates we're using wxHTML. | |
244 | str << wxT("<p>\n"); | |
b72812a6 | 245 | |
50f65288 WS |
246 | str << tag; |
247 | } | |
b72812a6 | 248 | |
50f65288 | 249 | str << wxT("<li> "); |
2dec6761 | 250 | } |
2dec6761 JS |
251 | else |
252 | { | |
50f65288 | 253 | CloseLists(-1, str); |
b72812a6 | 254 | |
50f65288 | 255 | wxString align = GetAlignment(thisStyle); |
2b2c1044 JS |
256 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); |
257 | ||
258 | if (thisStyle.HasParagraphSpacingAfter() && thisStyle.GetParagraphSpacingAfter() == 0) | |
03647350 | 259 | str << wxT(" style=\"line-height: 0px\""); |
2b2c1044 JS |
260 | |
261 | str << wxT(">"); | |
40989e46 | 262 | |
50f65288 WS |
263 | // Use a table |
264 | int indentTenthsMM = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent(); | |
265 | // TODO: convert to pixels | |
266 | int indentPixels = indentTenthsMM/4; | |
267 | str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels); | |
268 | ||
269 | OutputFont(thisStyle, str); | |
40989e46 | 270 | |
50f65288 | 271 | if (thisStyle.GetLeftSubIndent() < 0) |
d2d0adc7 | 272 | { |
50f65288 | 273 | str << SymbolicIndent( - thisStyle.GetLeftSubIndent()); |
d2d0adc7 | 274 | } |
50f65288 | 275 | |
b72812a6 | 276 | m_inTable = true; |
2dec6761 JS |
277 | } |
278 | } | |
50f65288 WS |
279 | else |
280 | { | |
281 | CloseLists(-1, str); | |
282 | ||
283 | wxString align = GetAlignment(thisStyle); | |
2b2c1044 JS |
284 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); |
285 | ||
286 | if (thisStyle.HasParagraphSpacingAfter() && thisStyle.GetParagraphSpacingAfter() == 0) | |
287 | str << wxT(" style=\"line-height: 0px\""); | |
288 | ||
289 | str << wxT(">"); | |
b72812a6 | 290 | } |
2dec6761 | 291 | } |
50f65288 WS |
292 | |
293 | /// End paragraph formatting | |
44cc96a8 | 294 | void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& stream) |
2dec6761 | 295 | { |
50f65288 | 296 | if (m_inTable) |
2dec6761 | 297 | { |
50f65288 WS |
298 | if (thisStyle.HasFont()) |
299 | stream << wxT("</font>"); | |
b72812a6 | 300 | |
c433798e | 301 | stream << wxT("</td></tr></table></p>\n"); |
50f65288 | 302 | m_inTable = false; |
2dec6761 | 303 | } |
e5163682 | 304 | else if (!thisStyle.HasBulletStyle()) |
c433798e | 305 | stream << wxT("</p>\n"); |
2dec6761 JS |
306 | } |
307 | ||
50f65288 WS |
308 | /// Closes lists to level (-1 means close all) |
309 | void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str) | |
310 | { | |
311 | // Close levels high than this | |
312 | int i = m_indents.GetCount()-1; | |
313 | while (i >= 0) | |
314 | { | |
315 | int l = m_indents[i]; | |
316 | if (l > level) | |
317 | { | |
318 | if (m_listTypes[i] == 0) | |
319 | str << wxT("</ol>"); | |
320 | else | |
321 | str << wxT("</ul>"); | |
322 | m_indents.RemoveAt(i); | |
323 | m_listTypes.RemoveAt(i); | |
324 | } | |
325 | else | |
326 | break; | |
327 | i --; | |
328 | } | |
329 | } | |
330 | ||
331 | /// Output font tag | |
44cc96a8 | 332 | void wxRichTextHTMLHandler::OutputFont(const wxTextAttr& style, wxTextOutputStream& stream) |
2dec6761 | 333 | { |
50f65288 WS |
334 | if (style.HasFont()) |
335 | { | |
44cc96a8 | 336 | stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style.GetFontFaceName().c_str(), PtToSize(style.GetFontSize())); |
b72812a6 JS |
337 | if (style.HasTextColour()) |
338 | stream << wxString::Format(wxT(" color=\"%s\""), style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str()); | |
339 | stream << wxT(" >"); | |
50f65288 | 340 | } |
2dec6761 JS |
341 | } |
342 | ||
44cc96a8 | 343 | int wxRichTextHTMLHandler::TypeOfList( const wxTextAttr& thisStyle, wxString& tag ) |
2dec6761 | 344 | { |
d2d0adc7 JS |
345 | // We can use number attribute of li tag but not all the browsers support it. |
346 | // also wxHtmlWindow doesn't support type attribute. | |
40989e46 | 347 | |
50f65288 | 348 | bool m_is_ul = false; |
d2d0adc7 | 349 | if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD)) |
2dec6761 | 350 | tag = wxT("<ol type=\"1\">"); |
d2d0adc7 | 351 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER) |
2dec6761 | 352 | tag = wxT("<ol type=\"A\">"); |
d2d0adc7 | 353 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER) |
2dec6761 | 354 | tag = wxT("<ol type=\"a\">"); |
d2d0adc7 | 355 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER) |
2dec6761 | 356 | tag = wxT("<ol type=\"I\">"); |
d2d0adc7 | 357 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER) |
2dec6761 | 358 | tag = wxT("<ol type=\"i\">"); |
b71e9aa4 JS |
359 | else |
360 | { | |
2dec6761 JS |
361 | tag = wxT("<ul>"); |
362 | m_is_ul = true; | |
b71e9aa4 | 363 | } |
b72812a6 | 364 | |
50f65288 WS |
365 | if (m_is_ul) |
366 | return 1; | |
367 | else | |
368 | return 0; | |
b71e9aa4 JS |
369 | } |
370 | ||
44cc96a8 | 371 | wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttr& thisStyle ) |
2dec6761 JS |
372 | { |
373 | switch( thisStyle.GetAlignment() ) | |
374 | { | |
375 | case wxTEXT_ALIGNMENT_LEFT: | |
376 | return wxT("left"); | |
377 | case wxTEXT_ALIGNMENT_RIGHT: | |
378 | return wxT("right"); | |
379 | case wxTEXT_ALIGNMENT_CENTER: | |
380 | return wxT("center"); | |
381 | case wxTEXT_ALIGNMENT_JUSTIFIED: | |
382 | return wxT("justify"); | |
383 | default: | |
384 | return wxT("left"); | |
385 | } | |
386 | } | |
387 | ||
d2d0adc7 | 388 | void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream) |
2dec6761 JS |
389 | { |
390 | wxTextOutputStream str(stream); | |
40989e46 | 391 | |
2dec6761 | 392 | str << wxT("<img src=\""); |
40989e46 | 393 | |
d2d0adc7 JS |
394 | #if wxUSE_FILESYSTEM |
395 | if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) | |
396 | { | |
397 | if (!image->GetImage().Ok() && image->GetImageBlock().GetData()) | |
398 | image->LoadFromBlock(); | |
399 | if (image->GetImage().Ok() && !image->GetImageBlock().GetData()) | |
400 | image->MakeBlock(); | |
401 | ||
402 | if (image->GetImage().Ok()) | |
b72812a6 | 403 | { |
d2d0adc7 | 404 | wxString ext(image->GetImageBlock().GetExtension()); |
86501081 | 405 | wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, ext)); |
d2d0adc7 | 406 | wxMemoryFSHandler::AddFile(tempFilename, image->GetImage(), image->GetImageBlock().GetImageType()); |
b72812a6 | 407 | |
d2d0adc7 | 408 | m_imageLocations.Add(tempFilename); |
b72812a6 | 409 | |
d2d0adc7 JS |
410 | str << wxT("memory:") << tempFilename; |
411 | } | |
412 | else | |
413 | str << wxT("memory:?"); | |
40989e46 | 414 | |
d2d0adc7 JS |
415 | sm_fileCounter ++; |
416 | } | |
417 | else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
418 | { | |
419 | if (!image->GetImage().Ok() && image->GetImageBlock().GetData()) | |
420 | image->LoadFromBlock(); | |
421 | if (image->GetImage().Ok() && !image->GetImageBlock().GetData()) | |
422 | image->MakeBlock(); | |
423 | ||
424 | if (image->GetImage().Ok()) | |
b72812a6 | 425 | { |
d2d0adc7 JS |
426 | wxString tempDir(GetTempDir()); |
427 | if (tempDir.IsEmpty()) | |
428 | tempDir = wxFileName::GetTempDir(); | |
b72812a6 | 429 | |
d2d0adc7 | 430 | wxString ext(image->GetImageBlock().GetExtension()); |
86501081 | 431 | wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir, sm_fileCounter, ext)); |
d2d0adc7 | 432 | image->GetImageBlock().Write(tempFilename); |
b72812a6 | 433 | |
d2d0adc7 | 434 | m_imageLocations.Add(tempFilename); |
b72812a6 JS |
435 | |
436 | str << wxFileSystem::FileNameToURL(tempFilename); | |
d2d0adc7 JS |
437 | } |
438 | else | |
439 | str << wxT("file:?"); | |
40989e46 | 440 | |
d2d0adc7 JS |
441 | sm_fileCounter ++; |
442 | } | |
443 | else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied | |
444 | #endif | |
445 | { | |
446 | str << wxT("data:"); | |
447 | str << GetMimeType(image->GetImageBlock().GetImageType()); | |
448 | str << wxT(";base64,"); | |
449 | ||
450 | if (image->GetImage().Ok() && !image->GetImageBlock().GetData()) | |
451 | image->MakeBlock(); | |
452 | ||
453 | wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() ); | |
454 | str << data; | |
455 | ||
456 | delete[] data; | |
457 | } | |
40989e46 | 458 | |
2dec6761 JS |
459 | str << wxT("\" />"); |
460 | } | |
461 | ||
d2d0adc7 | 462 | long wxRichTextHTMLHandler::PtToSize(long size) |
2dec6761 | 463 | { |
50f65288 WS |
464 | int i; |
465 | int len = m_fontSizeMapping.GetCount(); | |
466 | for (i = 0; i < len; i++) | |
467 | if (size <= m_fontSizeMapping[i]) | |
468 | return i+1; | |
b72812a6 | 469 | return 7; |
2dec6761 JS |
470 | } |
471 | ||
472 | wxString wxRichTextHTMLHandler::SymbolicIndent(long indent) | |
473 | { | |
474 | wxString in; | |
475 | for(;indent > 0; indent -= 20) | |
476 | in.Append( wxT(" ") ); | |
477 | return in; | |
478 | } | |
479 | ||
21e354f1 | 480 | const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType) |
2dec6761 JS |
481 | { |
482 | switch(imageType) | |
483 | { | |
484 | case wxBITMAP_TYPE_BMP: | |
485 | return wxT("image/bmp"); | |
486 | case wxBITMAP_TYPE_TIF: | |
487 | return wxT("image/tiff"); | |
488 | case wxBITMAP_TYPE_GIF: | |
489 | return wxT("image/gif"); | |
490 | case wxBITMAP_TYPE_PNG: | |
491 | return wxT("image/png"); | |
492 | case wxBITMAP_TYPE_JPEG: | |
493 | return wxT("image/jpeg"); | |
494 | default: | |
495 | return wxT("image/unknown"); | |
496 | } | |
497 | } | |
498 | ||
d2d0adc7 | 499 | // exim-style base64 encoder |
2dec6761 JS |
500 | wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len ) |
501 | { | |
d2d0adc7 JS |
502 | // elements of enc64 array must be 8 bit values |
503 | // otherwise encoder will fail | |
504 | // hmmm.. Does wxT macro define a char as 16 bit value | |
505 | // when compiling with UNICODE option? | |
21e354f1 | 506 | static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); |
2dec6761 JS |
507 | wxChar* output = new wxChar[4*((in_len+2)/3)+1]; |
508 | wxChar* p = output; | |
40989e46 | 509 | |
2dec6761 JS |
510 | while( in_len-- > 0 ) |
511 | { | |
512 | register wxChar a, b; | |
40989e46 | 513 | |
2dec6761 | 514 | a = *input++; |
40989e46 | 515 | |
2dec6761 | 516 | *p++ = enc64[ (a >> 2) & 0x3f ]; |
40989e46 | 517 | |
07854e5e | 518 | if( in_len-- == 0 ) |
2dec6761 JS |
519 | { |
520 | *p++ = enc64[ (a << 4 ) & 0x30 ]; | |
521 | *p++ = '='; | |
522 | *p++ = '='; | |
523 | break; | |
524 | } | |
40989e46 | 525 | |
2dec6761 | 526 | b = *input++; |
40989e46 | 527 | |
2dec6761 | 528 | *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f]; |
40989e46 | 529 | |
07854e5e | 530 | if( in_len-- == 0 ) |
2dec6761 JS |
531 | { |
532 | *p++ = enc64[ (b << 2) & 0x3f ]; | |
533 | *p++ = '='; | |
534 | break; | |
535 | } | |
40989e46 | 536 | |
2dec6761 | 537 | a = *input++; |
40989e46 | 538 | |
2dec6761 | 539 | *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ]; |
40989e46 | 540 | |
2dec6761 JS |
541 | *p++ = enc64[ a & 0x3f ]; |
542 | } | |
543 | *p = 0; | |
40989e46 | 544 | |
2dec6761 JS |
545 | return output; |
546 | } | |
b71e9aa4 | 547 | #endif |
2dec6761 | 548 | // wxUSE_STREAMS |
b71e9aa4 | 549 | |
d2d0adc7 JS |
550 | /// Delete the in-memory or temporary files generated by the last operation |
551 | bool wxRichTextHTMLHandler::DeleteTemporaryImages() | |
552 | { | |
553 | return DeleteTemporaryImages(GetFlags(), m_imageLocations); | |
554 | } | |
555 | ||
556 | /// Delete the in-memory or temporary files generated by the last operation | |
557 | bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations) | |
558 | { | |
559 | size_t i; | |
560 | for (i = 0; i < imageLocations.GetCount(); i++) | |
561 | { | |
562 | wxString location = imageLocations[i]; | |
b72812a6 | 563 | |
d2d0adc7 JS |
564 | if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) |
565 | { | |
566 | #if wxUSE_FILESYSTEM | |
567 | wxMemoryFSHandler::RemoveFile(location); | |
568 | #endif | |
569 | } | |
570 | else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
571 | { | |
572 | if (wxFileExists(location)) | |
573 | wxRemoveFile(location); | |
574 | } | |
575 | } | |
b72812a6 | 576 | |
d2d0adc7 JS |
577 | return true; |
578 | } | |
579 | ||
580 | ||
b71e9aa4 | 581 | #endif |
2dec6761 | 582 | // wxUSE_RICHTEXT |
d2d0adc7 | 583 |