1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtexthtml.cpp
3 // Purpose: HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/richtext/richtexthtml.h"
21 #include "wx/richtext/richtextstyles.h"
26 #include "wx/filename.h"
27 #include "wx/wfstream.h"
28 #include "wx/txtstrm.h"
31 #include "wx/filesys.h"
32 #include "wx/fs_mem.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler
, wxRichTextFileHandler
)
37 int wxRichTextHTMLHandler::sm_fileCounter
= 1;
39 wxRichTextHTMLHandler::wxRichTextHTMLHandler(const wxString
& name
, const wxString
& ext
, int type
)
40 : wxRichTextFileHandler(name
, ext
, type
), m_buffer(NULL
), m_font(false), m_inTable(false)
42 m_fontSizeMapping
.Add(8);
43 m_fontSizeMapping
.Add(10);
44 m_fontSizeMapping
.Add(13);
45 m_fontSizeMapping
.Add(17);
46 m_fontSizeMapping
.Add(22);
47 m_fontSizeMapping
.Add(30);
48 m_fontSizeMapping
.Add(100);
51 /// Can we handle this filename (if using files)? By default, checks the extension.
52 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
54 wxString path
, file
, ext
;
55 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
57 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
62 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
68 * We need to output only _changes_ in character formatting.
71 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
75 ClearTemporaryImageLocations();
77 wxRichTextDrawingContext
context(buffer
);
78 buffer
->Defragment(context
);
81 wxCSConv
* customEncoding
= NULL
;
82 wxMBConv
* conv
= NULL
;
83 if (!GetEncoding().IsEmpty())
85 customEncoding
= new wxCSConv(GetEncoding());
86 if (!customEncoding
->IsOk())
88 wxDELETE(customEncoding
);
92 conv
= customEncoding
;
99 wxTextOutputStream
str(stream
, wxEOL_NATIVE
, *conv
);
101 wxTextOutputStream
str(stream
, wxEOL_NATIVE
);
104 wxRichTextAttr currentParaStyle
= buffer
->GetAttributes();
105 wxRichTextAttr currentCharStyle
= buffer
->GetAttributes();
107 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
108 str
<< wxT("<html><head></head><body>\n");
110 OutputFont(currentParaStyle
, str
);
118 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
121 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
122 wxASSERT (para
!= NULL
);
126 wxRichTextAttr
paraStyle(para
->GetCombinedAttributes());
128 BeginParagraphFormatting(currentParaStyle
, paraStyle
, str
);
130 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
133 wxRichTextObject
* obj
= node2
->GetData();
134 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
135 if (textObj
&& !textObj
->IsEmpty())
137 wxRichTextAttr
charStyle(para
->GetCombinedAttributes(obj
->GetAttributes()));
138 BeginCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
140 wxString text
= textObj
->GetText();
142 if (charStyle
.HasTextEffects() && (charStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
145 wxString toReplace
= wxRichTextLineBreakChar
;
146 text
.Replace(toReplace
, wxT("<br>"));
150 EndCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
153 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
154 if( image
&& (!image
->IsEmpty() || image
->GetImageBlock().GetData()))
155 WriteImage( image
, stream
);
157 node2
= node2
->GetNext();
160 EndParagraphFormatting(currentParaStyle
, paraStyle
, str
);
164 node
= node
->GetNext();
169 str
<< wxT("</font>");
171 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
172 str
<< wxT("</body></html>");
179 delete customEncoding
;
187 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& thisStyle
, const wxRichTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& str
)
191 // Is there any change in the font properties of the item?
192 if (thisStyle
.GetFontFaceName() != currentStyle
.GetFontFaceName())
194 wxString
faceName(thisStyle
.GetFontFaceName());
195 style
+= wxString::Format(wxT(" face=\"%s\""), faceName
.c_str());
197 if (thisStyle
.GetFontSize() != currentStyle
.GetFontSize())
198 style
+= wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle
.GetFontSize()));
200 bool bTextColourChanged
= (thisStyle
.GetTextColour() != currentStyle
.GetTextColour());
201 bool bBackgroundColourChanged
= (thisStyle
.GetBackgroundColour() != currentStyle
.GetBackgroundColour());
202 if (bTextColourChanged
|| bBackgroundColourChanged
)
204 style
+= wxT(" style=\"");
206 if (bTextColourChanged
)
208 wxString
color(thisStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
));
209 style
+= wxString::Format(wxT("color: %s"), color
.c_str());
211 if (bTextColourChanged
&& bBackgroundColourChanged
)
213 if (bBackgroundColourChanged
)
215 wxString
color(thisStyle
.GetBackgroundColour().GetAsString(wxC2S_HTML_SYNTAX
));
216 style
+= wxString::Format(wxT("background-color: %s"), color
.c_str());
224 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
228 if (thisStyle
.GetFontWeight() == wxFONTWEIGHT_BOLD
)
230 if (thisStyle
.GetFontStyle() == wxFONTSTYLE_ITALIC
)
232 if (thisStyle
.GetFontUnderlined())
235 if (thisStyle
.HasURL())
236 str
<< wxT("<a href=\"") << thisStyle
.GetURL() << wxT("\">");
238 if (thisStyle
.HasTextEffects())
240 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
)
242 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
244 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
)
249 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxRichTextAttr
& WXUNUSED(currentStyle
), const wxRichTextAttr
& thisStyle
, const wxRichTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& stream
)
251 if (thisStyle
.HasURL())
252 stream
<< wxT("</a>");
254 if (thisStyle
.GetFontUnderlined())
255 stream
<< wxT("</u>");
256 if (thisStyle
.GetFontStyle() == wxFONTSTYLE_ITALIC
)
257 stream
<< wxT("</i>");
258 if (thisStyle
.GetFontWeight() == wxFONTWEIGHT_BOLD
)
259 stream
<< wxT("</b>");
261 if (thisStyle
.HasTextEffects())
263 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
)
264 stream
<< wxT("</del>");
265 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
266 stream
<< wxT("</sup>");
267 if (thisStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
)
268 stream
<< wxT("</sub>");
274 stream
<< wxT("</font>");
278 /// Begin paragraph formatting
279 void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxRichTextAttr
& WXUNUSED(currentStyle
), const wxRichTextAttr
& thisStyle
, wxTextOutputStream
& str
)
281 if (thisStyle
.HasPageBreak())
283 str
<< wxT("<div style=\"page-break-after:always\"></div>\n");
286 if (thisStyle
.HasLeftIndent() && thisStyle
.GetLeftIndent() != 0)
288 if (thisStyle
.HasBulletStyle())
290 int indent
= thisStyle
.GetLeftIndent();
292 // Close levels high than this
293 CloseLists(indent
, str
);
295 if (m_indents
.GetCount() > 0 && indent
== m_indents
.Last())
297 // Same level, no need to start a new list
299 else if (m_indents
.GetCount() == 0 || indent
> m_indents
.Last())
301 m_indents
.Add(indent
);
304 int listType
= TypeOfList(thisStyle
, tag
);
305 m_listTypes
.Add(listType
);
307 // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs.
308 // TODO: pass a flag that indicates we're using wxHTML.
320 wxString align
= GetAlignment(thisStyle
);
321 str
<< wxString::Format(wxT("<p align=\"%s\""), align
.c_str());
325 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && thisStyle
.HasParagraphSpacingBefore())
327 float spacingBeforeMM
= thisStyle
.GetParagraphSpacingBefore() / 10.0;
329 styleStr
+= wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM
);
331 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && thisStyle
.HasParagraphSpacingAfter())
333 float spacingAfterMM
= thisStyle
.GetParagraphSpacingAfter() / 10.0;
335 styleStr
+= wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM
);
338 float indentLeftMM
= (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent())/10.0;
339 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && (indentLeftMM
> 0.0))
341 styleStr
+= wxString::Format(wxT("margin-left: %.2fmm; "), indentLeftMM
);
343 float indentRightMM
= thisStyle
.GetRightIndent()/10.0;
344 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && thisStyle
.HasRightIndent() && (indentRightMM
> 0.0))
346 styleStr
+= wxString::Format(wxT("margin-right: %.2fmm; "), indentRightMM
);
348 // First line indentation
349 float firstLineIndentMM
= - thisStyle
.GetLeftSubIndent() / 10.0;
350 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && (firstLineIndentMM
> 0.0))
352 styleStr
+= wxString::Format(wxT("text-indent: %.2fmm; "), firstLineIndentMM
);
355 if (!styleStr
.IsEmpty())
356 str
<< wxT(" style=\"") << styleStr
<< wxT("\"");
360 // TODO: convert to pixels
361 int indentPixels
= static_cast<int>(indentLeftMM
*10/4);
363 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) == 0)
365 // Use a table to do indenting if we don't have CSS
366 str
<< wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels
);
370 if (((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) == 0) && (thisStyle
.GetLeftSubIndent() < 0))
372 str
<< SymbolicIndent( - thisStyle
.GetLeftSubIndent());
380 wxString align
= GetAlignment(thisStyle
);
381 str
<< wxString::Format(wxT("<p align=\"%s\""), align
.c_str());
385 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && thisStyle
.HasParagraphSpacingBefore())
387 float spacingBeforeMM
= thisStyle
.GetParagraphSpacingBefore() / 10.0;
389 styleStr
+= wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM
);
391 if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS
) && thisStyle
.HasParagraphSpacingAfter())
393 float spacingAfterMM
= thisStyle
.GetParagraphSpacingAfter() / 10.0;
395 styleStr
+= wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM
);
398 if (!styleStr
.IsEmpty())
399 str
<< wxT(" style=\"") << styleStr
<< wxT("\"");
403 OutputFont(thisStyle
, str
);
406 /// End paragraph formatting
407 void wxRichTextHTMLHandler::EndParagraphFormatting(const wxRichTextAttr
& WXUNUSED(currentStyle
), const wxRichTextAttr
& thisStyle
, wxTextOutputStream
& stream
)
409 if (thisStyle
.HasFont())
410 stream
<< wxT("</font>");
414 stream
<< wxT("</td></tr></table></p>\n");
417 else if (!thisStyle
.HasBulletStyle())
418 stream
<< wxT("</p>\n");
421 /// Closes lists to level (-1 means close all)
422 void wxRichTextHTMLHandler::CloseLists(int level
, wxTextOutputStream
& str
)
424 // Close levels high than this
425 int i
= m_indents
.GetCount()-1;
428 int l
= m_indents
[i
];
431 if (m_listTypes
[i
] == 0)
435 m_indents
.RemoveAt(i
);
436 m_listTypes
.RemoveAt(i
);
445 void wxRichTextHTMLHandler::OutputFont(const wxRichTextAttr
& style
, wxTextOutputStream
& stream
)
449 stream
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style
.GetFontFaceName().c_str(), PtToSize(style
.GetFontSize()));
450 if (style
.HasTextColour())
451 stream
<< wxString::Format(wxT(" color=\"%s\""), style
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
456 int wxRichTextHTMLHandler::TypeOfList( const wxRichTextAttr
& thisStyle
, wxString
& tag
)
458 // We can use number attribute of li tag but not all the browsers support it.
459 // also wxHtmlWindow doesn't support type attribute.
461 bool m_is_ul
= false;
462 if (thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
463 tag
= wxT("<ol type=\"1\">");
464 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
465 tag
= wxT("<ol type=\"A\">");
466 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
467 tag
= wxT("<ol type=\"a\">");
468 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
469 tag
= wxT("<ol type=\"I\">");
470 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
471 tag
= wxT("<ol type=\"i\">");
484 wxString
wxRichTextHTMLHandler::GetAlignment( const wxRichTextAttr
& thisStyle
)
486 switch( thisStyle
.GetAlignment() )
488 case wxTEXT_ALIGNMENT_LEFT
:
490 case wxTEXT_ALIGNMENT_RIGHT
:
492 case wxTEXT_ALIGNMENT_CENTER
:
493 return wxT("center");
494 case wxTEXT_ALIGNMENT_JUSTIFIED
:
495 return wxT("justify");
501 void wxRichTextHTMLHandler::WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
)
503 wxTextOutputStream
str(stream
);
505 str
<< wxT("<img src=\"");
508 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
511 if (!image
->GetImage().IsOk() && image
->GetImageBlock().GetData())
512 image
->LoadFromBlock();
513 if (image
->GetImage().IsOk() && !image
->GetImageBlock().GetData())
517 if (image
->GetImageBlock().IsOk())
520 image
->GetImageBlock().Load(img
);
523 wxString
ext(image
->GetImageBlock().GetExtension());
524 wxString
tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter
, ext
.c_str()));
525 wxMemoryFSHandler::AddFile(tempFilename
, img
, image
->GetImageBlock().GetImageType());
527 m_imageLocations
.Add(tempFilename
);
529 str
<< wxT("memory:") << tempFilename
;
533 str
<< wxT("memory:?");
537 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
540 if (!image
->GetImage().IsOk() && image
->GetImageBlock().GetData())
541 image
->LoadFromBlock();
542 if (image
->GetImage().IsOk() && !image
->GetImageBlock().GetData())
546 if (image
->GetImageBlock().IsOk())
548 wxString
tempDir(GetTempDir());
549 if (tempDir
.IsEmpty())
550 tempDir
= wxFileName::GetTempDir();
552 wxString
ext(image
->GetImageBlock().GetExtension());
553 wxString
tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir
.c_str(), sm_fileCounter
, ext
.c_str()));
554 image
->GetImageBlock().Write(tempFilename
);
556 m_imageLocations
.Add(tempFilename
);
558 str
<< wxFileSystem::FileNameToURL(tempFilename
);
561 str
<< wxT("file:?");
565 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
569 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
570 str
<< wxT(";base64,");
572 if (image
->GetImage().IsOk() && !image
->GetImageBlock().GetData())
575 if (image
->GetImageBlock().IsOk())
577 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
587 long wxRichTextHTMLHandler::PtToSize(long size
)
590 int len
= m_fontSizeMapping
.GetCount();
591 for (i
= 0; i
< len
; i
++)
592 if (size
<= m_fontSizeMapping
[i
])
597 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
600 for(;indent
> 0; indent
-= 20)
601 in
.Append( wxT(" ") );
605 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
609 case wxBITMAP_TYPE_BMP
:
610 return wxT("image/bmp");
611 case wxBITMAP_TYPE_TIFF
:
612 return wxT("image/tiff");
613 case wxBITMAP_TYPE_GIF
:
614 return wxT("image/gif");
615 case wxBITMAP_TYPE_PNG
:
616 return wxT("image/png");
617 case wxBITMAP_TYPE_JPEG
:
618 return wxT("image/jpeg");
620 return wxT("image/unknown");
624 // exim-style base64 encoder
625 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
627 // elements of enc64 array must be 8 bit values
628 // otherwise encoder will fail
629 // hmmm.. Does wxT macro define a char as 16 bit value
630 // when compiling with UNICODE option?
631 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
632 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
635 while( in_len
-- > 0 )
637 register wxChar a
, b
;
641 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
645 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
653 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
657 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
664 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
666 *p
++ = enc64
[ a
& 0x3f ];
675 /// Delete the in-memory or temporary files generated by the last operation
676 bool wxRichTextHTMLHandler::DeleteTemporaryImages()
678 return DeleteTemporaryImages(GetFlags(), m_imageLocations
);
681 /// Delete the in-memory or temporary files generated by the last operation
682 bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
)
685 for (i
= 0; i
< imageLocations
.GetCount(); i
++)
687 wxString location
= imageLocations
[i
];
689 if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
692 wxMemoryFSHandler::RemoveFile(location
);
695 else if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
697 if (wxFileExists(location
))
698 wxRemoveFile(location
);