]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtexthtml.cpp
Quote file names with spaces in wxFileType::ExpandCommand().
[wxWidgets.git] / src / richtext / richtexthtml.cpp
CommitLineData
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
36IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
37
d2d0adc7
JS
38int wxRichTextHTMLHandler::sm_fileCounter = 1;
39
50f65288
WS
40wxRichTextHTMLHandler::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.
53bool 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
63bool 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
72bool 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
2c48f032
JS
80#if wxUSE_UNICODE
81 wxCSConv* customEncoding = NULL;
82 wxMBConv* conv = NULL;
83 if (!GetEncoding().IsEmpty())
b71e9aa4 84 {
2c48f032
JS
85 customEncoding = new wxCSConv(GetEncoding());
86 if (!customEncoding->IsOk())
b71e9aa4 87 {
2c48f032
JS
88 delete customEncoding;
89 customEncoding = NULL;
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
2c48f032
JS
105 wxTextAttr currentParaStyle = buffer->GetAttributes();
106 wxTextAttr 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 {
2c48f032 127 wxTextAttr 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 {
138 wxTextAttr charStyle(para->GetCombinedAttributes(obj->GetAttributes()));
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
44cc96a8 188void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttr& currentStyle, const wxTextAttr& thisStyle, const wxTextAttr& 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()));
d2d0adc7
JS
200 if (thisStyle.GetTextColour() != currentStyle.GetTextColour() )
201 {
202 wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX));
203 style += wxString::Format(wxT(" color=\"%s\""), color.c_str());
204 }
40989e46 205
d2d0adc7 206 if (style.size())
21e354f1
JS
207 {
208 str << wxString::Format(wxT("<font %s >"), style.c_str());
209 m_font = true;
210 }
40989e46 211
44cc96a8 212 if (thisStyle.GetFontWeight() == wxBOLD)
2dec6761 213 str << wxT("<b>");
44cc96a8 214 if (thisStyle.GetFontStyle() == wxITALIC)
2dec6761 215 str << wxT("<i>");
44cc96a8 216 if (thisStyle.GetFontUnderlined())
2dec6761 217 str << wxT("<u>");
b72812a6 218
d2d0adc7
JS
219 if (thisStyle.HasURL())
220 str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">");
b71e9aa4
JS
221}
222
44cc96a8 223void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, const wxTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream)
b71e9aa4 224{
d2d0adc7 225 if (thisStyle.HasURL())
50f65288 226 stream << wxT("</a>");
d2d0adc7 227
44cc96a8 228 if (thisStyle.GetFontUnderlined())
50f65288 229 stream << wxT("</u>");
44cc96a8 230 if (thisStyle.GetFontStyle() == wxITALIC)
50f65288 231 stream << wxT("</i>");
44cc96a8 232 if (thisStyle.GetFontWeight() == wxBOLD)
50f65288 233 stream << wxT("</b>");
40989e46 234
d2d0adc7 235 if (m_font)
2dec6761
JS
236 {
237 m_font = false;
50f65288 238 stream << wxT("</font>");
2dec6761
JS
239 }
240}
b71e9aa4 241
50f65288 242/// Begin paragraph formatting
44cc96a8 243void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& str)
2dec6761 244{
42688aea
JS
245 if (thisStyle.HasPageBreak())
246 {
42688aea
JS
247 str << wxT("<div style=\"page-break-after:always\"></div>\n");
248 }
40989e46 249
7947a11d 250 if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0)
2dec6761 251 {
50f65288 252 if (thisStyle.HasBulletStyle())
2dec6761 253 {
50f65288
WS
254 int indent = thisStyle.GetLeftIndent();
255
256 // Close levels high than this
257 CloseLists(indent, str);
b72812a6 258
50f65288
WS
259 if (m_indents.GetCount() > 0 && indent == m_indents.Last())
260 {
261 // Same level, no need to start a new list
262 }
263 else if (m_indents.GetCount() == 0 || indent > m_indents.Last())
264 {
265 m_indents.Add(indent);
b72812a6 266
50f65288
WS
267 wxString tag;
268 int listType = TypeOfList(thisStyle, tag);
269 m_listTypes.Add(listType);
b72812a6 270
e5163682
JS
271 // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs.
272 // TODO: pass a flag that indicates we're using wxHTML.
273 str << wxT("<p>\n");
b72812a6 274
50f65288
WS
275 str << tag;
276 }
b72812a6 277
50f65288 278 str << wxT("<li> ");
2dec6761 279 }
2dec6761
JS
280 else
281 {
50f65288 282 CloseLists(-1, str);
b72812a6 283
50f65288 284 wxString align = GetAlignment(thisStyle);
2b2c1044
JS
285 str << wxString::Format(wxT("<p align=\"%s\""), align.c_str());
286
4fdc2c5f
JS
287 wxString styleStr;
288
289 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore())
290 {
291 float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0;
292
293 styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM);
294 }
295 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter())
296 {
297 float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0;
298
299 styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM);
300 }
301
302 float indentLeftMM = (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent())/10.0;
303 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (indentLeftMM > 0.0))
304 {
305 styleStr += wxString::Format(wxT("margin-left: %.2fmm; "), indentLeftMM);
306 }
307 float indentRightMM = thisStyle.GetRightIndent()/10.0;
308 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasRightIndent() && (indentRightMM > 0.0))
309 {
310 styleStr += wxString::Format(wxT("margin-right: %.2fmm; "), indentRightMM);
311 }
312 // First line indentation
313 float firstLineIndentMM = - thisStyle.GetLeftSubIndent() / 10.0;
314 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (firstLineIndentMM > 0.0))
315 {
316 styleStr += wxString::Format(wxT("text-indent: %.2fmm; "), firstLineIndentMM);
317 }
318
319 if (!styleStr.IsEmpty())
320 str << wxT(" style=\"") << styleStr << wxT("\"");
2b2c1044
JS
321
322 str << wxT(">");
40989e46 323
50f65288 324 // TODO: convert to pixels
945c909d 325 int indentPixels = static_cast<int>(indentLeftMM*10/4);
4fdc2c5f
JS
326
327 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0)
328 {
329 // Use a table to do indenting if we don't have CSS
330 str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels);
331 m_inTable = true;
332 }
50f65288 333
4fdc2c5f 334 if (((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) && (thisStyle.GetLeftSubIndent() < 0))
d2d0adc7 335 {
50f65288 336 str << SymbolicIndent( - thisStyle.GetLeftSubIndent());
d2d0adc7 337 }
2dec6761
JS
338 }
339 }
50f65288
WS
340 else
341 {
342 CloseLists(-1, str);
343
344 wxString align = GetAlignment(thisStyle);
2b2c1044
JS
345 str << wxString::Format(wxT("<p align=\"%s\""), align.c_str());
346
4fdc2c5f
JS
347 wxString styleStr;
348
349 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore())
350 {
351 float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0;
352
353 styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM);
354 }
355 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter())
356 {
357 float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0;
358
359 styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM);
360 }
361
362 if (!styleStr.IsEmpty())
363 str << wxT(" style=\"") << styleStr << wxT("\"");
2b2c1044
JS
364
365 str << wxT(">");
b72812a6 366 }
7e7ced16 367 OutputFont(thisStyle, str);
2dec6761 368}
50f65288
WS
369
370/// End paragraph formatting
44cc96a8 371void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttr& WXUNUSED(currentStyle), const wxTextAttr& thisStyle, wxTextOutputStream& stream)
2dec6761 372{
7e7ced16
JS
373 if (thisStyle.HasFont())
374 stream << wxT("</font>");
375
50f65288 376 if (m_inTable)
2dec6761 377 {
c433798e 378 stream << wxT("</td></tr></table></p>\n");
50f65288 379 m_inTable = false;
2dec6761 380 }
e5163682 381 else if (!thisStyle.HasBulletStyle())
c433798e 382 stream << wxT("</p>\n");
2dec6761
JS
383}
384
50f65288
WS
385/// Closes lists to level (-1 means close all)
386void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str)
387{
388 // Close levels high than this
389 int i = m_indents.GetCount()-1;
390 while (i >= 0)
391 {
392 int l = m_indents[i];
393 if (l > level)
394 {
395 if (m_listTypes[i] == 0)
396 str << wxT("</ol>");
397 else
398 str << wxT("</ul>");
399 m_indents.RemoveAt(i);
400 m_listTypes.RemoveAt(i);
401 }
402 else
403 break;
404 i --;
405 }
406}
407
408/// Output font tag
44cc96a8 409void wxRichTextHTMLHandler::OutputFont(const wxTextAttr& style, wxTextOutputStream& stream)
2dec6761 410{
50f65288
WS
411 if (style.HasFont())
412 {
44cc96a8 413 stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style.GetFontFaceName().c_str(), PtToSize(style.GetFontSize()));
b72812a6
JS
414 if (style.HasTextColour())
415 stream << wxString::Format(wxT(" color=\"%s\""), style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str());
416 stream << wxT(" >");
50f65288 417 }
2dec6761
JS
418}
419
44cc96a8 420int wxRichTextHTMLHandler::TypeOfList( const wxTextAttr& thisStyle, wxString& tag )
2dec6761 421{
d2d0adc7
JS
422 // We can use number attribute of li tag but not all the browsers support it.
423 // also wxHtmlWindow doesn't support type attribute.
40989e46 424
50f65288 425 bool m_is_ul = false;
d2d0adc7 426 if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD))
2dec6761 427 tag = wxT("<ol type=\"1\">");
d2d0adc7 428 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
2dec6761 429 tag = wxT("<ol type=\"A\">");
d2d0adc7 430 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
2dec6761 431 tag = wxT("<ol type=\"a\">");
d2d0adc7 432 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
2dec6761 433 tag = wxT("<ol type=\"I\">");
d2d0adc7 434 else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
2dec6761 435 tag = wxT("<ol type=\"i\">");
b71e9aa4
JS
436 else
437 {
2dec6761
JS
438 tag = wxT("<ul>");
439 m_is_ul = true;
b71e9aa4 440 }
b72812a6 441
50f65288
WS
442 if (m_is_ul)
443 return 1;
444 else
445 return 0;
b71e9aa4
JS
446}
447
44cc96a8 448wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttr& thisStyle )
2dec6761
JS
449{
450 switch( thisStyle.GetAlignment() )
451 {
452 case wxTEXT_ALIGNMENT_LEFT:
453 return wxT("left");
454 case wxTEXT_ALIGNMENT_RIGHT:
455 return wxT("right");
456 case wxTEXT_ALIGNMENT_CENTER:
457 return wxT("center");
458 case wxTEXT_ALIGNMENT_JUSTIFIED:
459 return wxT("justify");
460 default:
461 return wxT("left");
462 }
463}
464
d2d0adc7 465void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream)
2dec6761
JS
466{
467 wxTextOutputStream str(stream);
40989e46 468
2dec6761 469 str << wxT("<img src=\"");
40989e46 470
d2d0adc7
JS
471#if wxUSE_FILESYSTEM
472 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
473 {
474 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
475 image->LoadFromBlock();
476 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
477 image->MakeBlock();
478
479 if (image->GetImage().Ok())
b72812a6 480 {
d2d0adc7 481 wxString ext(image->GetImageBlock().GetExtension());
86501081 482 wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, ext));
d2d0adc7 483 wxMemoryFSHandler::AddFile(tempFilename, image->GetImage(), image->GetImageBlock().GetImageType());
b72812a6 484
d2d0adc7 485 m_imageLocations.Add(tempFilename);
b72812a6 486
d2d0adc7
JS
487 str << wxT("memory:") << tempFilename;
488 }
489 else
490 str << wxT("memory:?");
40989e46 491
d2d0adc7
JS
492 sm_fileCounter ++;
493 }
494 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
495 {
496 if (!image->GetImage().Ok() && image->GetImageBlock().GetData())
497 image->LoadFromBlock();
498 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
499 image->MakeBlock();
500
501 if (image->GetImage().Ok())
b72812a6 502 {
d2d0adc7
JS
503 wxString tempDir(GetTempDir());
504 if (tempDir.IsEmpty())
505 tempDir = wxFileName::GetTempDir();
b72812a6 506
d2d0adc7 507 wxString ext(image->GetImageBlock().GetExtension());
86501081 508 wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir, sm_fileCounter, ext));
d2d0adc7 509 image->GetImageBlock().Write(tempFilename);
b72812a6 510
d2d0adc7 511 m_imageLocations.Add(tempFilename);
b72812a6
JS
512
513 str << wxFileSystem::FileNameToURL(tempFilename);
d2d0adc7
JS
514 }
515 else
516 str << wxT("file:?");
40989e46 517
d2d0adc7
JS
518 sm_fileCounter ++;
519 }
520 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
521#endif
522 {
523 str << wxT("data:");
524 str << GetMimeType(image->GetImageBlock().GetImageType());
525 str << wxT(";base64,");
526
527 if (image->GetImage().Ok() && !image->GetImageBlock().GetData())
528 image->MakeBlock();
529
530 wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() );
531 str << data;
532
533 delete[] data;
534 }
40989e46 535
2dec6761
JS
536 str << wxT("\" />");
537}
538
d2d0adc7 539long wxRichTextHTMLHandler::PtToSize(long size)
2dec6761 540{
50f65288
WS
541 int i;
542 int len = m_fontSizeMapping.GetCount();
543 for (i = 0; i < len; i++)
544 if (size <= m_fontSizeMapping[i])
545 return i+1;
b72812a6 546 return 7;
2dec6761
JS
547}
548
549wxString wxRichTextHTMLHandler::SymbolicIndent(long indent)
550{
551 wxString in;
552 for(;indent > 0; indent -= 20)
553 in.Append( wxT("&nbsp;") );
554 return in;
555}
556
21e354f1 557const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType)
2dec6761
JS
558{
559 switch(imageType)
560 {
561 case wxBITMAP_TYPE_BMP:
562 return wxT("image/bmp");
563 case wxBITMAP_TYPE_TIF:
564 return wxT("image/tiff");
565 case wxBITMAP_TYPE_GIF:
566 return wxT("image/gif");
567 case wxBITMAP_TYPE_PNG:
568 return wxT("image/png");
569 case wxBITMAP_TYPE_JPEG:
570 return wxT("image/jpeg");
571 default:
572 return wxT("image/unknown");
573 }
574}
575
d2d0adc7 576// exim-style base64 encoder
2dec6761
JS
577wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len )
578{
d2d0adc7
JS
579 // elements of enc64 array must be 8 bit values
580 // otherwise encoder will fail
581 // hmmm.. Does wxT macro define a char as 16 bit value
582 // when compiling with UNICODE option?
21e354f1 583 static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2dec6761
JS
584 wxChar* output = new wxChar[4*((in_len+2)/3)+1];
585 wxChar* p = output;
40989e46 586
2dec6761
JS
587 while( in_len-- > 0 )
588 {
589 register wxChar a, b;
40989e46 590
2dec6761 591 a = *input++;
40989e46 592
2dec6761 593 *p++ = enc64[ (a >> 2) & 0x3f ];
40989e46 594
07854e5e 595 if( in_len-- == 0 )
2dec6761
JS
596 {
597 *p++ = enc64[ (a << 4 ) & 0x30 ];
598 *p++ = '=';
599 *p++ = '=';
600 break;
601 }
40989e46 602
2dec6761 603 b = *input++;
40989e46 604
2dec6761 605 *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f];
40989e46 606
07854e5e 607 if( in_len-- == 0 )
2dec6761
JS
608 {
609 *p++ = enc64[ (b << 2) & 0x3f ];
610 *p++ = '=';
611 break;
612 }
40989e46 613
2dec6761 614 a = *input++;
40989e46 615
2dec6761 616 *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ];
40989e46 617
2dec6761
JS
618 *p++ = enc64[ a & 0x3f ];
619 }
620 *p = 0;
40989e46 621
2dec6761
JS
622 return output;
623}
b71e9aa4 624#endif
2dec6761 625// wxUSE_STREAMS
b71e9aa4 626
d2d0adc7
JS
627/// Delete the in-memory or temporary files generated by the last operation
628bool wxRichTextHTMLHandler::DeleteTemporaryImages()
629{
630 return DeleteTemporaryImages(GetFlags(), m_imageLocations);
631}
632
633/// Delete the in-memory or temporary files generated by the last operation
634bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations)
635{
636 size_t i;
637 for (i = 0; i < imageLocations.GetCount(); i++)
638 {
639 wxString location = imageLocations[i];
b72812a6 640
d2d0adc7
JS
641 if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
642 {
643#if wxUSE_FILESYSTEM
644 wxMemoryFSHandler::RemoveFile(location);
645#endif
646 }
647 else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES)
648 {
649 if (wxFileExists(location))
650 wxRemoveFile(location);
651 }
652 }
b72812a6 653
d2d0adc7
JS
654 return true;
655}
656
657
b71e9aa4 658#endif
2dec6761 659// wxUSE_RICHTEXT
d2d0adc7 660