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 wxTextAttrEx currentParaStyle
= buffer
->GetAttributes();
83 wxTextAttrEx 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 wxTextAttrEx
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 wxTextAttrEx
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 wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& thisStyle
, const wxTextAttrEx
& WXUNUSED(paraStyle
), wxTextOutputStream
& str
)
167 // Is there any change in the font properties of the item?
168 if (thisStyle
.GetFont().GetFaceName() != currentStyle
.GetFont().GetFaceName())
170 wxString
faceName(thisStyle
.GetFont().GetFaceName());
171 style
+= wxString::Format(wxT(" face=\"%s\""), faceName
.c_str());
173 if (thisStyle
.GetFont().GetPointSize() != currentStyle
.GetFont().GetPointSize())
174 style
+= wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle
.GetFont().GetPointSize()));
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
.GetFont().GetWeight() == wxBOLD
)
189 if (thisStyle
.GetFont().GetStyle() == wxITALIC
)
191 if (thisStyle
.GetFont().GetUnderlined())
194 if (thisStyle
.HasURL())
195 str
<< wxT("<a href=\"") << thisStyle
.GetURL() << wxT("\">");
198 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, const wxTextAttrEx
& WXUNUSED(paraStyle
), wxTextOutputStream
& stream
)
200 if (thisStyle
.HasURL())
201 stream
<< wxT("</a>");
203 if (thisStyle
.GetFont().GetUnderlined())
204 stream
<< wxT("</u>");
205 if (thisStyle
.GetFont().GetStyle() == wxITALIC
)
206 stream
<< wxT("</i>");
207 if (thisStyle
.GetFont().GetWeight() == wxBOLD
)
208 stream
<< wxT("</b>");
213 stream
<< wxT("</font>");
217 /// Begin paragraph formatting
218 void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& 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 wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& 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 wxTextAttrEx
& style
, wxTextOutputStream
& stream
)
329 stream
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\" color=\"%s\" >"),
330 style
.GetFont().GetFaceName().c_str(), PtToSize(style
.GetFont().GetPointSize()),
331 style
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
335 int wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx
& thisStyle
, wxString
& tag
)
337 // We can use number attribute of li tag but not all the browsers support it.
338 // also wxHtmlWindow doesn't support type attribute.
340 bool m_is_ul
= false;
341 if (thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
342 tag
= wxT("<ol type=\"1\">");
343 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
344 tag
= wxT("<ol type=\"A\">");
345 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
346 tag
= wxT("<ol type=\"a\">");
347 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
348 tag
= wxT("<ol type=\"I\">");
349 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
350 tag
= wxT("<ol type=\"i\">");
363 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx
& thisStyle
)
365 switch( thisStyle
.GetAlignment() )
367 case wxTEXT_ALIGNMENT_LEFT
:
369 case wxTEXT_ALIGNMENT_RIGHT
:
371 case wxTEXT_ALIGNMENT_CENTER
:
372 return wxT("center");
373 case wxTEXT_ALIGNMENT_JUSTIFIED
:
374 return wxT("justify");
380 void wxRichTextHTMLHandler::WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
)
382 wxTextOutputStream
str(stream
);
384 str
<< wxT("<img src=\"");
387 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
389 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
390 image
->LoadFromBlock();
391 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
394 if (image
->GetImage().Ok())
396 wxString
ext(image
->GetImageBlock().GetExtension());
397 wxString
tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter
, ext
));
398 wxMemoryFSHandler::AddFile(tempFilename
, image
->GetImage(), image
->GetImageBlock().GetImageType());
400 m_imageLocations
.Add(tempFilename
);
402 str
<< wxT("memory:") << tempFilename
;
405 str
<< wxT("memory:?");
409 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
411 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
412 image
->LoadFromBlock();
413 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
416 if (image
->GetImage().Ok())
418 wxString
tempDir(GetTempDir());
419 if (tempDir
.IsEmpty())
420 tempDir
= wxFileName::GetTempDir();
422 wxString
ext(image
->GetImageBlock().GetExtension());
423 wxString
tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir
, sm_fileCounter
, ext
));
424 image
->GetImageBlock().Write(tempFilename
);
426 m_imageLocations
.Add(tempFilename
);
428 str
<< wxFileSystem::FileNameToURL(tempFilename
);
431 str
<< wxT("file:?");
435 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
439 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
440 str
<< wxT(";base64,");
442 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
445 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
454 long wxRichTextHTMLHandler::PtToSize(long size
)
457 int len
= m_fontSizeMapping
.GetCount();
458 for (i
= 0; i
< len
; i
++)
459 if (size
<= m_fontSizeMapping
[i
])
464 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
467 for(;indent
> 0; indent
-= 20)
468 in
.Append( wxT(" ") );
472 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
476 case wxBITMAP_TYPE_BMP
:
477 return wxT("image/bmp");
478 case wxBITMAP_TYPE_TIF
:
479 return wxT("image/tiff");
480 case wxBITMAP_TYPE_GIF
:
481 return wxT("image/gif");
482 case wxBITMAP_TYPE_PNG
:
483 return wxT("image/png");
484 case wxBITMAP_TYPE_JPEG
:
485 return wxT("image/jpeg");
487 return wxT("image/unknown");
491 // exim-style base64 encoder
492 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
494 // elements of enc64 array must be 8 bit values
495 // otherwise encoder will fail
496 // hmmm.. Does wxT macro define a char as 16 bit value
497 // when compiling with UNICODE option?
498 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
499 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
502 while( in_len
-- > 0 )
504 register wxChar a
, b
;
508 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
512 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
520 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
524 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
531 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
533 *p
++ = enc64
[ a
& 0x3f ];
542 /// Delete the in-memory or temporary files generated by the last operation
543 bool wxRichTextHTMLHandler::DeleteTemporaryImages()
545 return DeleteTemporaryImages(GetFlags(), m_imageLocations
);
548 /// Delete the in-memory or temporary files generated by the last operation
549 bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
)
552 for (i
= 0; i
< imageLocations
.GetCount(); i
++)
554 wxString location
= imageLocations
[i
];
556 if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
559 wxMemoryFSHandler::RemoveFile(location
);
562 else if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
564 if (wxFileExists(location
))
565 wxRemoveFile(location
);