1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtexthtml.cpp
3 // Purpose: HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtexthtml.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 /// Can we handle this filename (if using files)? By default, checks the extension.
40 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
42 wxString path
, file
, ext
;
43 wxSplitPath(filename
, & path
, & file
, & ext
);
45 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
50 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
56 * We need to output only _changes_ in character formatting.
59 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
61 ClearTemporaryImageLocations();
65 wxTextOutputStream
str(stream
);
67 wxTextAttrEx currentParaStyle
= buffer
->GetAttributes();
68 wxTextAttrEx currentCharStyle
= buffer
->GetAttributes();
70 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
71 str
<< wxT("<html><head></head><body>\n");
73 str
<< wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">\n");
75 str
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\" color=\"%s\" >"),
76 currentParaStyle
.GetFont().GetFaceName().c_str(), PtToSize(currentParaStyle
.GetFont().GetPointSize()),
77 currentParaStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
83 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
86 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
87 wxASSERT (para
!= NULL
);
91 wxTextAttrEx
paraStyle(para
->GetCombinedAttributes());
93 OutputParagraphFormatting(currentParaStyle
, paraStyle
, stream
);
95 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
98 wxRichTextObject
* obj
= node2
->GetData();
99 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
100 if (textObj
&& !textObj
->IsEmpty())
102 wxTextAttrEx
charStyle(para
->GetCombinedAttributes(obj
->GetAttributes()));
103 BeginCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, stream
);
105 wxString text
= textObj
->GetText();
107 if (charStyle
.HasTextEffects() && (charStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
112 EndCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, stream
);
115 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
116 if( image
&& !image
->IsEmpty())
117 WriteImage( image
, stream
);
119 node2
= node2
->GetNext();
123 node
= node
->GetNext();
126 str
<< wxT("</font></td></tr></table>");
128 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
129 str
<< wxT("</body></html>");
136 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& thisStyle
, const wxTextAttrEx
& paraStyle
, wxOutputStream
& stream
)
138 wxTextOutputStream
str(stream
);
140 // Is the item a bulleted one?
141 if ( paraStyle
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
143 // Is there any opened list?
148 // Is the item among the previous ones?
149 // Is the item one of the previous list tag's child items?
150 if ((paraStyle
.GetLeftIndent() == (m_indent
+ 100)) || (paraStyle
.GetLeftIndent() < 100))
151 str
<< wxT("<li>"); //Yes it is
154 // No it isn't, so we should close the list tag
155 str
<< (m_is_ul
? wxT("</ul>") : wxT("</ol>"));
157 // And renavigate to new list's horizontal position
158 NavigateToListPosition(paraStyle
, str
);
160 // Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
162 TypeOfList(paraStyle
, tag
);
163 str
<< tag
<< wxT("<li>");
168 // No there isn't a list.
169 // navigate to new list's horizontal position(indent)
170 NavigateToListPosition(paraStyle
, str
);
172 // Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
174 TypeOfList(paraStyle
, tag
);
175 str
<< tag
<< wxT("<li>");
177 // Now we have a list, mark it.
183 // The item is not bulleted and there is a list that should be closed now.
186 str
<< (m_is_ul
? wxT("</ul>") : wxT("</ol>"));
188 // And mark as there is no an opened list
192 // does the item have an indentation ?
193 if( paraStyle
.GetLeftIndent() )
195 if (paraStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
199 if ((paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent()) == m_indent
)
201 if (paraStyle
.GetLeftSubIndent() < 0)
203 str
<< SymbolicIndent(~paraStyle
.GetLeftSubIndent());
208 if (paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent() > m_indent
)
210 Indent(paraStyle
, str
);
211 m_indent
= paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent();
212 m_indents
.Add( m_indent
);
216 int i
= m_indents
.size() - 1;
219 if (m_indent
< (paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent()))
221 Indent(paraStyle
, str
);
222 m_indent
= paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent();
223 m_indents
.Add( m_indent
);
227 else if (m_indent
== (paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent()))
229 if (paraStyle
.GetLeftSubIndent() < 0)
231 str
<< SymbolicIndent(~paraStyle
.GetLeftSubIndent());
237 str
<< wxT("</td></tr></table>");
239 m_indents
.RemoveAt(i
);
245 m_indent
= m_indents
[i
-1];
253 Indent(paraStyle
, str
);
254 m_indent
= paraStyle
.GetLeftIndent() + paraStyle
.GetLeftSubIndent();
255 m_indents
.Add( m_indent
);
261 // The item is not indented and there is a table(s) that should be closed now.
263 for (unsigned int i
= 0; i
< m_indents
.size(); i
++)
264 str
<< wxT("</td></tr></table>");
273 // Is there any change in the font properties of the item?
274 if (thisStyle
.GetFont().GetFaceName() != currentStyle
.GetFont().GetFaceName())
276 wxString
faceName(thisStyle
.GetFont().GetFaceName());
277 style
+= wxString::Format(wxT(" face=\"%s\""), faceName
.c_str());
279 if (thisStyle
.GetFont().GetPointSize() != currentStyle
.GetFont().GetPointSize())
280 style
+= wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle
.GetFont().GetPointSize()));
281 if (thisStyle
.GetTextColour() != currentStyle
.GetTextColour() )
283 wxString
color(thisStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
));
284 style
+= wxString::Format(wxT(" color=\"%s\""), color
.c_str());
289 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
293 if (thisStyle
.GetFont().GetWeight() == wxBOLD
)
295 if (thisStyle
.GetFont().GetStyle() == wxITALIC
)
297 if (thisStyle
.GetFont().GetUnderlined())
300 if (thisStyle
.HasURL())
301 str
<< wxT("<a href=\"") << thisStyle
.GetURL() << wxT("\">");
304 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, const wxTextAttrEx
& WXUNUSED(paraStyle
), wxOutputStream
& stream
)
306 wxTextOutputStream
str(stream
);
308 if (thisStyle
.HasURL())
311 if (thisStyle
.GetFont().GetUnderlined())
313 if (thisStyle
.GetFont().GetStyle() == wxITALIC
)
315 if (thisStyle
.GetFont().GetWeight() == wxBOLD
)
321 str
<< wxT("</font>");
325 /// Output paragraph formatting
326 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
328 // If there is no opened list currently, insert a <p> after every paragraph
331 wxTextOutputStream
str(stream
);
332 wxString align
= GetAlignment(thisStyle
);
333 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
336 if (thisStyle
.HasPageBreak())
338 wxTextOutputStream
str(stream
);
339 str
<< wxT("<div style=\"page-break-after:always\"></div>\n");
343 void wxRichTextHTMLHandler::NavigateToListPosition(const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
345 // indenting an item using an ul/ol tag is equal to inserting 5 x on its left side.
346 // so we should start from 100 point left
348 // Is the second td's left wall of the current indentaion table at the 100+ point-left-side
349 // of the item, horizontally?
350 if (m_indent
+ 100 < thisStyle
.GetLeftIndent())
353 LIndent(thisStyle
, str
);
354 m_indent
= thisStyle
.GetLeftIndent() - 100;
355 m_indents
.Add( m_indent
);
360 int i
= m_indents
.size() - 1;
363 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
365 if (m_indent
+ 100 < thisStyle
.GetLeftIndent())
368 LIndent(thisStyle
, str
);
369 m_indent
= thisStyle
.GetLeftIndent() - 100;
370 m_indents
.Add( m_indent
);
373 else if (m_indent
+ 100 == thisStyle
.GetLeftIndent())
377 // No it is not, the second td's left wall of the current indentaion table is at the
378 //right side of the current item horizontally, so close it.
379 str
<< wxT("</td></tr></table>");
381 m_indents
.RemoveAt(i
);
387 m_indent
= m_indents
[i
-1];
391 void wxRichTextHTMLHandler::Indent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
393 //There is no way to indent an item in HTML, but we can use tables.
395 // Item -> "Hello world"
396 // Its Left Indentation -> 100
397 // Its Left Sub-Indentation ->40
398 // A typical indentation-table for the item will be construct as the following
402 // LSI = Left Sub Indent
403 // LI = Left Indent - LSI
405 // -------------------------------------------
406 // | nbsp;|nbsp;nbsp;Hello World |
409 // | --LI-- | --LSI-- |
410 // -------------------------------------------
412 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
414 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) - m_indent
);
415 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
416 str
<< wxT("<td width=\"100%\">");
418 if (thisStyle
.GetLeftSubIndent() < 0)
420 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
424 void wxRichTextHTMLHandler::LIndent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
427 // r.BeginNumberedBullet(1, 200, 60);
429 // r.WriteText(wxT("first item"));
430 // r.EndNumberedBullet();
431 // r.BeginNumberedBullet(2, 200, 60);
433 // r.WriteText(wxT("second item."));
434 // r.EndNumberedBullet();
436 // A typical indentation-table for the item will be construct as the following
438 // 1 x nbsp = 20 point
439 // ULI -> 100pt (UL/OL tag indents its sub element by 100 point)
440 // <--------- 100 pt ---------->|
441 // ------------------------------------------------------
442 // | nbsp; nbsp;|<ul> |
443 // | |<-ULI-><li>first item |
444 // | |<-ULI-><li>second item |
446 // ------------------------------------------------------
450 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
452 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() - m_indent
) - 100);
453 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
454 str
<< wxT("<td width=\"100%\">");
457 void wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx
& thisStyle
, wxString
& tag
)
459 // We can use number attribute of li tag but not all the browsers support it.
460 // also wxHtmlWindow doesn't support type attribute.
463 if (thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
464 tag
= wxT("<ol type=\"1\">");
465 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
466 tag
= wxT("<ol type=\"A\">");
467 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
468 tag
= wxT("<ol type=\"a\">");
469 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
470 tag
= wxT("<ol type=\"I\">");
471 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
472 tag
= wxT("<ol type=\"i\">");
480 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx
& thisStyle
)
482 switch( thisStyle
.GetAlignment() )
484 case wxTEXT_ALIGNMENT_LEFT
:
486 case wxTEXT_ALIGNMENT_RIGHT
:
488 case wxTEXT_ALIGNMENT_CENTER
:
489 return wxT("center");
490 case wxTEXT_ALIGNMENT_JUSTIFIED
:
491 return wxT("justify");
497 void wxRichTextHTMLHandler::WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
)
499 wxTextOutputStream
str(stream
);
501 str
<< wxT("<img src=\"");
504 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
506 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
507 image
->LoadFromBlock();
508 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
511 if (image
->GetImage().Ok())
513 wxString
ext(image
->GetImageBlock().GetExtension());
514 wxString
tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter
, (const wxChar
*) ext
));
515 wxMemoryFSHandler::AddFile(tempFilename
, image
->GetImage(), image
->GetImageBlock().GetImageType());
517 m_imageLocations
.Add(tempFilename
);
519 str
<< wxT("memory:") << tempFilename
;
522 str
<< wxT("memory:?");
526 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
528 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
529 image
->LoadFromBlock();
530 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
533 if (image
->GetImage().Ok())
535 wxString
tempDir(GetTempDir());
536 if (tempDir
.IsEmpty())
537 tempDir
= wxFileName::GetTempDir();
539 wxString
ext(image
->GetImageBlock().GetExtension());
540 wxString
tempFilename(wxString::Format(wxT("%s/image%d.%s"), (const wxChar
*) tempDir
, sm_fileCounter
, (const wxChar
*) ext
));
541 image
->GetImageBlock().Write(tempFilename
);
543 m_imageLocations
.Add(tempFilename
);
545 str
<< wxFileSystem::FileNameToURL(tempFilename
);
548 str
<< wxT("file:?");
552 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
556 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
557 str
<< wxT(";base64,");
559 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
562 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
571 long wxRichTextHTMLHandler::PtToSize(long size
)
573 // return approximate size
574 if (size
< 9 ) return 1;
575 else if( size
< 11 ) return 2;
576 else if( size
< 14 ) return 3;
577 else if( size
< 18 ) return 4;
578 else if( size
< 23 ) return 5;
579 else if( size
< 30 ) return 6;
583 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
586 for(;indent
> 0; indent
-= 20)
587 in
.Append( wxT(" ") );
591 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
595 case wxBITMAP_TYPE_BMP
:
596 return wxT("image/bmp");
597 case wxBITMAP_TYPE_TIF
:
598 return wxT("image/tiff");
599 case wxBITMAP_TYPE_GIF
:
600 return wxT("image/gif");
601 case wxBITMAP_TYPE_PNG
:
602 return wxT("image/png");
603 case wxBITMAP_TYPE_JPEG
:
604 return wxT("image/jpeg");
606 return wxT("image/unknown");
610 // exim-style base64 encoder
611 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
613 // elements of enc64 array must be 8 bit values
614 // otherwise encoder will fail
615 // hmmm.. Does wxT macro define a char as 16 bit value
616 // when compiling with UNICODE option?
617 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
618 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
621 while( in_len
-- > 0 )
623 register wxChar a
, b
;
627 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
631 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
639 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
643 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
650 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
652 *p
++ = enc64
[ a
& 0x3f ];
661 /// Delete the in-memory or temporary files generated by the last operation
662 bool wxRichTextHTMLHandler::DeleteTemporaryImages()
664 return DeleteTemporaryImages(GetFlags(), m_imageLocations
);
667 /// Delete the in-memory or temporary files generated by the last operation
668 bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
)
671 for (i
= 0; i
< imageLocations
.GetCount(); i
++)
673 wxString location
= imageLocations
[i
];
675 if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
678 wxMemoryFSHandler::RemoveFile(location
);
681 else if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
683 if (wxFileExists(location
))
684 wxRemoveFile(location
);