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 wxFileName::SplitPath(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 OutputFont(currentParaStyle
, str
);
96 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
99 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
100 wxASSERT (para
!= NULL
);
104 wxTextAttr
paraStyle(para
->GetCombinedAttributes());
106 BeginParagraphFormatting(currentParaStyle
, paraStyle
, str
);
108 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
111 wxRichTextObject
* obj
= node2
->GetData();
112 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
113 if (textObj
&& !textObj
->IsEmpty())
115 wxTextAttr
charStyle(para
->GetCombinedAttributes(obj
->GetAttributes()));
116 BeginCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
118 wxString text
= textObj
->GetText();
120 if (charStyle
.HasTextEffects() && (charStyle
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
123 wxString toReplace
= wxRichTextLineBreakChar
;
124 text
.Replace(toReplace
, wxT("<br>"));
128 EndCharacterFormatting(currentCharStyle
, charStyle
, paraStyle
, str
);
131 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
132 if( image
&& (!image
->IsEmpty() || image
->GetImageBlock().GetData()))
133 WriteImage( image
, stream
);
135 node2
= node2
->GetNext();
138 EndParagraphFormatting(currentParaStyle
, paraStyle
, str
);
142 node
= node
->GetNext();
147 str
<< wxT("</font>");
149 if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER
) == 0)
150 str
<< wxT("</body></html>");
159 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttr
& currentStyle
, const wxTextAttr
& thisStyle
, const wxTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& str
)
163 // Is there any change in the font properties of the item?
164 if (thisStyle
.GetFontFaceName() != currentStyle
.GetFontFaceName())
166 wxString
faceName(thisStyle
.GetFontFaceName());
167 style
+= wxString::Format(wxT(" face=\"%s\""), faceName
.c_str());
169 if (thisStyle
.GetFontSize() != currentStyle
.GetFontSize())
170 style
+= wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle
.GetFontSize()));
171 if (thisStyle
.GetTextColour() != currentStyle
.GetTextColour() )
173 wxString
color(thisStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
));
174 style
+= wxString::Format(wxT(" color=\"%s\""), color
.c_str());
179 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
183 if (thisStyle
.GetFontWeight() == wxBOLD
)
185 if (thisStyle
.GetFontStyle() == wxITALIC
)
187 if (thisStyle
.GetFontUnderlined())
190 if (thisStyle
.HasURL())
191 str
<< wxT("<a href=\"") << thisStyle
.GetURL() << wxT("\">");
194 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, const wxTextAttr
& WXUNUSED(paraStyle
), wxTextOutputStream
& stream
)
196 if (thisStyle
.HasURL())
197 stream
<< wxT("</a>");
199 if (thisStyle
.GetFontUnderlined())
200 stream
<< wxT("</u>");
201 if (thisStyle
.GetFontStyle() == wxITALIC
)
202 stream
<< wxT("</i>");
203 if (thisStyle
.GetFontWeight() == wxBOLD
)
204 stream
<< wxT("</b>");
209 stream
<< wxT("</font>");
213 /// Begin paragraph formatting
214 void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, wxTextOutputStream
& str
)
216 if (thisStyle
.HasPageBreak())
218 str
<< wxT("<div style=\"page-break-after:always\"></div>\n");
221 if (thisStyle
.HasLeftIndent() && thisStyle
.GetLeftIndent() != 0)
223 if (thisStyle
.HasBulletStyle())
225 int indent
= thisStyle
.GetLeftIndent();
227 // Close levels high than this
228 CloseLists(indent
, str
);
230 if (m_indents
.GetCount() > 0 && indent
== m_indents
.Last())
232 // Same level, no need to start a new list
234 else if (m_indents
.GetCount() == 0 || indent
> m_indents
.Last())
236 m_indents
.Add(indent
);
239 int listType
= TypeOfList(thisStyle
, tag
);
240 m_listTypes
.Add(listType
);
242 // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs.
243 // TODO: pass a flag that indicates we're using wxHTML.
255 wxString align
= GetAlignment(thisStyle
);
256 str
<< wxString::Format(wxT("<p align=\"%s\""), align
.c_str());
258 if (thisStyle
.HasParagraphSpacingAfter() && thisStyle
.GetParagraphSpacingAfter() == 0)
259 str
<< wxT(" style=\"line-height: 0px\"");
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());
286 if (thisStyle
.HasParagraphSpacingAfter() && thisStyle
.GetParagraphSpacingAfter() == 0)
287 str
<< wxT(" style=\"line-height: 0px\"");
293 /// End paragraph formatting
294 void wxRichTextHTMLHandler::EndParagraphFormatting(const wxTextAttr
& WXUNUSED(currentStyle
), const wxTextAttr
& thisStyle
, wxTextOutputStream
& stream
)
298 if (thisStyle
.HasFont())
299 stream
<< wxT("</font>");
301 stream
<< wxT("</td></tr></table></p>\n");
304 else if (!thisStyle
.HasBulletStyle())
305 stream
<< wxT("</p>\n");
308 /// Closes lists to level (-1 means close all)
309 void wxRichTextHTMLHandler::CloseLists(int level
, wxTextOutputStream
& str
)
311 // Close levels high than this
312 int i
= m_indents
.GetCount()-1;
315 int l
= m_indents
[i
];
318 if (m_listTypes
[i
] == 0)
322 m_indents
.RemoveAt(i
);
323 m_listTypes
.RemoveAt(i
);
332 void wxRichTextHTMLHandler::OutputFont(const wxTextAttr
& style
, wxTextOutputStream
& stream
)
336 stream
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style
.GetFontFaceName().c_str(), PtToSize(style
.GetFontSize()));
337 if (style
.HasTextColour())
338 stream
<< wxString::Format(wxT(" color=\"%s\""), style
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
343 int wxRichTextHTMLHandler::TypeOfList( const wxTextAttr
& thisStyle
, wxString
& tag
)
345 // We can use number attribute of li tag but not all the browsers support it.
346 // also wxHtmlWindow doesn't support type attribute.
348 bool m_is_ul
= false;
349 if (thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
350 tag
= wxT("<ol type=\"1\">");
351 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
352 tag
= wxT("<ol type=\"A\">");
353 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
354 tag
= wxT("<ol type=\"a\">");
355 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
356 tag
= wxT("<ol type=\"I\">");
357 else if (thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
358 tag
= wxT("<ol type=\"i\">");
371 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttr
& thisStyle
)
373 switch( thisStyle
.GetAlignment() )
375 case wxTEXT_ALIGNMENT_LEFT
:
377 case wxTEXT_ALIGNMENT_RIGHT
:
379 case wxTEXT_ALIGNMENT_CENTER
:
380 return wxT("center");
381 case wxTEXT_ALIGNMENT_JUSTIFIED
:
382 return wxT("justify");
388 void wxRichTextHTMLHandler::WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
)
390 wxTextOutputStream
str(stream
);
392 str
<< wxT("<img src=\"");
395 if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
397 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
398 image
->LoadFromBlock();
399 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
402 if (image
->GetImage().Ok())
404 wxString
ext(image
->GetImageBlock().GetExtension());
405 wxString
tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter
, ext
));
406 wxMemoryFSHandler::AddFile(tempFilename
, image
->GetImage(), image
->GetImageBlock().GetImageType());
408 m_imageLocations
.Add(tempFilename
);
410 str
<< wxT("memory:") << tempFilename
;
413 str
<< wxT("memory:?");
417 else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
419 if (!image
->GetImage().Ok() && image
->GetImageBlock().GetData())
420 image
->LoadFromBlock();
421 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
424 if (image
->GetImage().Ok())
426 wxString
tempDir(GetTempDir());
427 if (tempDir
.IsEmpty())
428 tempDir
= wxFileName::GetTempDir();
430 wxString
ext(image
->GetImageBlock().GetExtension());
431 wxString
tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir
, sm_fileCounter
, ext
));
432 image
->GetImageBlock().Write(tempFilename
);
434 m_imageLocations
.Add(tempFilename
);
436 str
<< wxFileSystem::FileNameToURL(tempFilename
);
439 str
<< wxT("file:?");
443 else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied
447 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
448 str
<< wxT(";base64,");
450 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
453 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
462 long wxRichTextHTMLHandler::PtToSize(long size
)
465 int len
= m_fontSizeMapping
.GetCount();
466 for (i
= 0; i
< len
; i
++)
467 if (size
<= m_fontSizeMapping
[i
])
472 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
475 for(;indent
> 0; indent
-= 20)
476 in
.Append( wxT(" ") );
480 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
484 case wxBITMAP_TYPE_BMP
:
485 return wxT("image/bmp");
486 case wxBITMAP_TYPE_TIF
:
487 return wxT("image/tiff");
488 case wxBITMAP_TYPE_GIF
:
489 return wxT("image/gif");
490 case wxBITMAP_TYPE_PNG
:
491 return wxT("image/png");
492 case wxBITMAP_TYPE_JPEG
:
493 return wxT("image/jpeg");
495 return wxT("image/unknown");
499 // exim-style base64 encoder
500 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
502 // elements of enc64 array must be 8 bit values
503 // otherwise encoder will fail
504 // hmmm.. Does wxT macro define a char as 16 bit value
505 // when compiling with UNICODE option?
506 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
507 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
510 while( in_len
-- > 0 )
512 register wxChar a
, b
;
516 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
520 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
528 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
532 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
539 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
541 *p
++ = enc64
[ a
& 0x3f ];
550 /// Delete the in-memory or temporary files generated by the last operation
551 bool wxRichTextHTMLHandler::DeleteTemporaryImages()
553 return DeleteTemporaryImages(GetFlags(), m_imageLocations
);
556 /// Delete the in-memory or temporary files generated by the last operation
557 bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
)
560 for (i
= 0; i
< imageLocations
.GetCount(); i
++)
562 wxString location
= imageLocations
[i
];
564 if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
)
567 wxMemoryFSHandler::RemoveFile(location
);
570 else if (flags
& wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES
)
572 if (wxFileExists(location
))
573 wxRemoveFile(location
);