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"
22 #include "wx/richtext/richtextstyles.h"
27 #include "wx/filename.h"
28 #include "wx/wfstream.h"
29 #include "wx/txtstrm.h"
32 #include "wx/filesys.h"
33 #include "wx/fs_mem.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler
, wxRichTextFileHandler
)
38 int wxRichTextHTMLHandler::sm_fileCounter
= 1;
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)
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);
52 /// Can we handle this filename (if using files)? By default, checks the extension.
53 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
55 wxString path
, file
, ext
;
56 wxSplitPath(filename
, & path
, & file
, & ext
);
58 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
63 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
69 * We need to output only _changes_ in character formatting.
72 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
76 ClearTemporaryImageLocations();
80 wxTextOutputStream
str(stream
);
82 wxTextAttr currentParaStyle
= buffer
->GetAttributes();
83 wxTextAttr currentCharStyle
= buffer
->GetAttributes();
85 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
86 str
<< wxT("<html><head></head><body>\n");
88 str
<< wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
90 OutputFont(currentParaStyle
, str
);
98 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
101 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
102 wxASSERT (para
!= NULL
);
106 wxTextAttr
paraStyle(para
->GetCombinedAttributes());
108 BeginParagraphFormatting(currentParaStyle
, paraStyle
, str
);
110 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
113 wxRichTextObject
* obj
= node2
->GetData();
114 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
115 if (textObj
&& !textObj
->IsEmpty())
117 wxTextAttr
charStyle(para
->GetCombinedAttributes(obj
->GetAttributes()));
118 BeginCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
120 wxString text
= textObj
->GetText();
122 if (charStyle
.HasTextEffects() && (charStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
125 wxString toReplace
= wxRichTextLineBreakChar
;
126 text
.Replace(toReplace
, wxT("<br>"));
130 EndCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
133 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
134 if( image
&& !image
->IsEmpty())
135 WriteImage( image
, stream
);
137 node2
= node2
->GetNext();
140 EndParagraphFormatting(currentParaStyle
, paraStyle
, str
);
144 node
= node
->GetNext();
149 str
<< wxT("</font>");
151 str
<< wxT("</td></tr></table><p>");
153 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
154 str
<< wxT("</body></html>");
163 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttr
& currentStyle
, const wxTextAttr
& thisStyle
, const wxTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& str
)
167 // Is there any change in the font properties of the item?
168 if (thisStyle
.GetFontFaceName() != currentStyle
.GetFontFaceName())
170 wxString
faceName(thisStyle
.GetFontFaceName());
171 style
+= wxString::Format(wxT(" face=\"%s\""), faceName
.c_str());
173 if (thisStyle
.GetFontSize() != currentStyle
.GetFontSize())
174 style
+= wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle
.GetFontSize()));
175 if (thisStyle
.GetTextColour() != currentStyle
.GetTextColour() )
177 wxString
color(thisStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
));
178 style
+= wxString::Format(wxT(" color=\"%s\""), color
.c_str());
183 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
187 if (thisStyle
.GetFontWeight() == wxBOLD
)
189 if (thisStyle
.GetFontStyle() == wxITALIC
)
191 if (thisStyle
.GetFontUnderlined())
194 if (thisStyle
.HasURL())
195 str
<< wxT("<a href=\"") << thisStyle
.GetURL() << wxT("\">");
198 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, const wxTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& stream
)
200 if (thisStyle
.HasURL())
201 stream
<< wxT("</a>");
203 if (thisStyle
.GetFontUnderlined())
204 stream
<< wxT("</u>");
205 if (thisStyle
.GetFontStyle() == wxITALIC
)
206 stream
<< wxT("</i>");
207 if (thisStyle
.GetFontWeight() == wxBOLD
)
208 stream
<< wxT("</b>");
213 stream
<< wxT("</font>");
217 /// Begin paragraph formatting
218 void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, wxTextOutputStream
& str
)
220 if (thisStyle
.HasPageBreak())
222 str
<< wxT("</tr></td></table>");
223 str
<< wxT("<div style=\"page-break-after:always\"></div>\n");
224 str
<< wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
227 if (thisStyle
.HasLeftIndent() && thisStyle
.GetLeftIndent() != 0)
229 if (thisStyle
.HasBulletStyle())
231 int indent
= thisStyle
.GetLeftIndent();
233 // Close levels high than this
234 CloseLists(indent
, str
);
236 if (m_indents
.GetCount() > 0 && indent
== m_indents
.Last())
238 // Same level, no need to start a new list
240 else if (m_indents
.GetCount() == 0 || indent
> m_indents
.Last())
242 m_indents
.Add(indent
);
245 int listType
= TypeOfList(thisStyle
, tag
);
246 m_listTypes
.Add(listType
);
248 wxString align
= GetAlignment(thisStyle
);
249 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
260 wxString align
= GetAlignment(thisStyle
);
261 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
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
);
269 OutputFont(thisStyle
, str
);
271 if (thisStyle
.GetLeftSubIndent() < 0)
273 str
<< SymbolicIndent( - thisStyle
.GetLeftSubIndent());
283 wxString align
= GetAlignment(thisStyle
);
284 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
288 /// End paragraph formatting
289 void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, wxTextOutputStream
& stream
)
293 if (thisStyle
.HasFont())
294 stream
<< wxT("</font>");
296 stream
<< wxT("</td></tr></table>\n");
301 /// Closes lists to level (-1 means close all)
302 void wxRichTextHTMLHandler::CloseLists(int level
, wxTextOutputStream
& str
)
304 // Close levels high than this
305 int i
= m_indents
.GetCount()-1;
308 int l
= m_indents
[i
];
311 if (m_listTypes
[i
] == 0)
315 m_indents
.RemoveAt(i
);
316 m_listTypes
.RemoveAt(i
);
325 void wxRichTextHTMLHandler::OutputFont(const wxTextAttr
& style
, wxTextOutputStream
& stream
)
329 stream
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style
.GetFontFaceName().c_str(), PtToSize(style
.GetFontSize()));
330 if (style
.HasTextColour())
331 stream
<< wxString::Format(wxT(" color=\"%s\""), style
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
336 int wxRichTextHTMLHandler::TypeOfList( const wxTextAttr
& thisStyle
, wxString
& tag
)
338 // We can use number attribute of li tag but not all the browsers support it.
339 // also wxHtmlWindow doesn't support type attribute.
341 bool m_is_ul
= false;
342 if (thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
343 tag
= wxT("<ol type=\"1\">");
344 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
345 tag
= wxT("<ol type=\"A\">");
346 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
347 tag
= wxT("<ol type=\"a\">");
348 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
349 tag
= wxT("<ol type=\"I\">");
350 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
351 tag
= wxT("<ol type=\"i\">");
364 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttr
& thisStyle
)
366 switch( thisStyle
.GetAlignment() )
368 case wxTEXT_ALIGNMENT_LEFT
:
370 case wxTEXT_ALIGNMENT_RIGHT
:
372 case wxTEXT_ALIGNMENT_CENTER
:
373 return wxT("center");
374 case wxTEXT_ALIGNMENT_JUSTIFIED
:
375 return wxT("justify");
381 void wxRichTextHTMLHandler::WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
)
383 wxTextOutputStream
str(stream
);
385 str
<< wxT("<img src=\"");
388 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
390 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
391 image
->LoadFromBlock();
392 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
395 if (image
->GetImage().Ok())
397 wxString
ext(image
->GetImageBlock().GetExtension());
398 wxString
tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter
, ext
));
399 wxMemoryFSHandler::AddFile(tempFilename
, image
->GetImage(), image
->GetImageBlock().GetImageType());
401 m_imageLocations
.Add(tempFilename
);
403 str
<< wxT("memory:") << tempFilename
;
406 str
<< wxT("memory:?");
410 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
412 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
413 image
->LoadFromBlock();
414 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
417 if (image
->GetImage().Ok())
419 wxString
tempDir(GetTempDir());
420 if (tempDir
.IsEmpty())
421 tempDir
= wxFileName::GetTempDir();
423 wxString
ext(image
->GetImageBlock().GetExtension());
424 wxString
tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir
, sm_fileCounter
, ext
));
425 image
->GetImageBlock().Write(tempFilename
);
427 m_imageLocations
.Add(tempFilename
);
429 str
<< wxFileSystem::FileNameToURL(tempFilename
);
432 str
<< wxT("file:?");
436 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
440 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
441 str
<< wxT(";base64,");
443 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
446 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
455 long wxRichTextHTMLHandler::PtToSize(long size
)
458 int len
= m_fontSizeMapping
.GetCount();
459 for (i
= 0; i
< len
; i
++)
460 if (size
<= m_fontSizeMapping
[i
])
465 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
468 for(;indent
> 0; indent
-= 20)
469 in
.Append( wxT(" ") );
473 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
477 case wxBITMAP_TYPE_BMP
:
478 return wxT("image/bmp");
479 case wxBITMAP_TYPE_TIF
:
480 return wxT("image/tiff");
481 case wxBITMAP_TYPE_GIF
:
482 return wxT("image/gif");
483 case wxBITMAP_TYPE_PNG
:
484 return wxT("image/png");
485 case wxBITMAP_TYPE_JPEG
:
486 return wxT("image/jpeg");
488 return wxT("image/unknown");
492 // exim-style base64 encoder
493 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
495 // elements of enc64 array must be 8 bit values
496 // otherwise encoder will fail
497 // hmmm.. Does wxT macro define a char as 16 bit value
498 // when compiling with UNICODE option?
499 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
500 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
503 while( in_len
-- > 0 )
505 register wxChar a
, b
;
509 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
513 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
521 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
525 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
532 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
534 *p
++ = enc64
[ a
& 0x3f ];
543 /// Delete the in-memory or temporary files generated by the last operation
544 bool wxRichTextHTMLHandler::DeleteTemporaryImages()
546 return DeleteTemporaryImages(GetFlags(), m_imageLocations
);
549 /// Delete the in-memory or temporary files generated by the last operation
550 bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
)
553 for (i
= 0; i
< imageLocations
.GetCount(); i
++)
555 wxString location
= imageLocations
[i
];
557 if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
560 wxMemoryFSHandler::RemoveFile(location
);
563 else if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
565 if (wxFileExists(location
))
566 wxRemoveFile(location
);