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"
30 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler
, wxRichTextFileHandler
)
32 /// Can we handle this filename (if using files)? By default, checks the extension.
33 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
35 wxString path
, file
, ext
;
36 wxSplitPath(filename
, & path
, & file
, & ext
);
38 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
43 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
49 * We need to output only _changes_ in character formatting.
52 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
56 wxTextOutputStream
str(stream
);
58 wxTextAttrEx currentParaStyle
= buffer
->GetAttributes();
59 wxTextAttrEx currentCharStyle
= buffer
->GetAttributes();
61 str
<< wxT("<html><head></head><body>\n");
64 wxRichText may be support paper formats like a1/a2/a3/a4
65 when this widget grown enough, i should turn back and support its new features
68 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td>");
70 wxString left_indent = SymbolicIndent(currentParaStyle.GetLeftIndent());
71 wxString right_indent = SymbolicIndent(currentParaStyle.GetRightIndent());
73 str << wxString::Format(wxT("%s</td><td></td><td>%s</td></tr><tr>"),
74 left_indent.c_str(), //Document-Wide Left Indent
75 right_indent.c_str()); //Document-Wide Right Indent
77 str << wxT("<td></td><td width=\"100%\">");
80 str
<< wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
82 str
<< wxString::Format(wxT("<font face=\"%s\" size=\"%ld\" color=\"%s\" >"),
83 currentParaStyle
.GetFont().GetFaceName().c_str(), Pt_To_Size( currentParaStyle
.GetFont().GetPointSize() ),
84 currentParaStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
86 //wxString align = GetAlignment( currentParaStyle.GetAlignment() );
87 //str << wxString::Format(wxT("<p align=\"%s\">"), align );
93 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
96 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
97 wxASSERT (para
!= NULL
);
101 OutputParagraphFormatting(currentParaStyle
, para
->GetAttributes(), stream
);
103 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
106 wxRichTextObject
* obj
= node2
->GetData();
107 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
108 if (textObj
&& !textObj
->IsEmpty())
110 BeginCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
);
112 str
<< textObj
->GetText();
114 EndCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
);
117 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
118 if( image
&& !image
->IsEmpty())
119 Image_to_Base64( image
, stream
);
121 node2
= node2
->GetNext();
124 //OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
126 node
= node
->GetNext();
129 str
<< wxT("</font></td></tr></table></body></html>\n");
134 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
136 wxTextOutputStream
str(stream
);
138 //Is the item bulleted one?
139 if( thisStyle
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
141 //Is there any opened list?
146 //Is the item among the previous ones
147 //Is the item one of the previous list tag's child items
148 if( (thisStyle
.GetLeftIndent() == (m_indent
+ 100)) || (thisStyle
.GetLeftIndent() < 100) )
149 str
<< wxT("<li>");//Yes it is
154 //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(thisStyle
, str
);
161 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
163 TypeOfList(thisStyle
, tag
);
164 str
<< tag
<< wxT("<li>");
169 //No there isn't a list
171 //navigate to new list's horizontal position(indent)
172 NavigateToListPosition(thisStyle
, str
);
174 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
176 TypeOfList(thisStyle
, tag
);
177 str
<< tag
<< wxT("<li>");
179 //Now we have a list, mark it.
185 //The item is not bulleted and there is a list what should be closed now.
188 str
<< (m_is_ul
? wxT("</ul>") : wxT("</ol>"));
189 //And mark as there is no an opened list
193 // does the item have an indentation ?
194 if( thisStyle
.GetLeftIndent() )
196 if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
200 if( (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) == m_indent
)
202 if( thisStyle
.GetLeftSubIndent() < 0 )
204 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
209 if( thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent() > m_indent
)
211 Indent(thisStyle
, str
);
212 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
213 m_indents
.Add( m_indent
);
217 int i
= m_indents
.size() - 1;
220 if( m_indent
< (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) )
222 Indent(thisStyle
, str
);
223 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
224 m_indents
.Add( m_indent
);
228 else if( m_indent
== (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) )
230 if( thisStyle
.GetLeftSubIndent() < 0 )
232 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
238 str
<< wxT("</td></tr></table>");
240 m_indents
.RemoveAt(i
);
242 if( i
< 1 ){m_indent
=0; break;}
243 m_indent
= m_indents
[i
-1];
251 Indent(thisStyle
, str
);
252 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
253 m_indents
.Add( m_indent
);
259 //The item is not indented and there is a table(s) what should be closed now.
262 for(unsigned int i
= 0; i
< m_indents
.size(); i
++ )
263 str
<< wxT("</td></tr></table>");
272 //Is there any change on the font properties of the item
273 if( thisStyle
.GetFont().GetFaceName() != currentStyle
.GetFont().GetFaceName() )
274 style
+= wxString::Format(wxT(" face=\"%s\""), thisStyle
.GetFont().GetFaceName().c_str());
275 if( thisStyle
.GetFont().GetPointSize() != currentStyle
.GetFont().GetPointSize() )
276 style
+= wxString::Format(wxT(" size=\"%ld\""), Pt_To_Size(thisStyle
.GetFont().GetPointSize()) );
277 if( thisStyle
.GetTextColour() != currentStyle
.GetTextColour() )
278 style
+= wxString::Format(wxT(" color=\"%s\""), thisStyle
.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX
).c_str());
282 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
286 if( thisStyle
.GetFont().GetWeight() == wxBOLD
)
288 if( thisStyle
.GetFont().GetStyle() == wxITALIC
)
290 if( thisStyle
.GetFont().GetUnderlined() )
294 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
296 wxTextOutputStream
str(stream
);
298 if( thisStyle
.GetFont().GetUnderlined() )
300 if( thisStyle
.GetFont().GetStyle() == wxITALIC
)
302 if( thisStyle
.GetFont().GetWeight() == wxBOLD
)
308 str
<< wxT("</font>");
312 /// Output paragraph formatting
313 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
315 //If there is no opened list currently, insert a <p> after every paragraph
318 wxTextOutputStream
str(stream
);
319 wxString align
= GetAlignment( thisStyle
);
320 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
324 void wxRichTextHTMLHandler::NavigateToListPosition(const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
326 //indenting an item using an ul/ol tag is equal to inserting 5 x on its left side.
327 //so we should start from 100 point left
329 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
330 //of the item, horizontally?
331 if( m_indent
+ 100 < thisStyle
.GetLeftIndent() )
334 LIndent(thisStyle
, str
);
335 m_indent
= thisStyle
.GetLeftIndent() - 100;
336 m_indents
.Add( m_indent
);
341 int i
= m_indents
.size() - 1;
344 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
346 if( m_indent
+ 100 < thisStyle
.GetLeftIndent() )
349 LIndent(thisStyle
, str
);
350 m_indent
= thisStyle
.GetLeftIndent() - 100;
351 m_indents
.Add( m_indent
);
354 else if( m_indent
+ 100 == thisStyle
.GetLeftIndent() )
358 //No it is not, the second td's left wall of the current indentaion table is at the
359 //right side of the current item horizontally, so close it.
360 str
<< wxT("</td></tr></table>");
362 m_indents
.RemoveAt(i
);
364 if( i
< 1 ){m_indent
=0; break;}
365 m_indent
= m_indents
[i
-1];
369 void wxRichTextHTMLHandler::Indent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
371 //As a five year experienced web developer i assure you there is no way to indent an item
372 //in html way, but we can use tables.
376 //Item -> "Hello world"
377 //Its Left Indentation -> 100
378 //Its Left Sub-Indentation ->40
379 //A typical indentation-table for the item will be construct as the following
383 //LSI = Left Sub Indent
384 //LI = Left Indent - LSI
386 //-------------------------------------------
387 //| nbsp;|nbsp;nbsp;Hello World |
390 //| --LI-- | --LSI-- |
391 //-------------------------------------------
393 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
395 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) - m_indent
);
396 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
397 str
<< wxT("<td width=\"100%\">");
399 if( thisStyle
.GetLeftSubIndent() < 0 )
401 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
405 void wxRichTextHTMLHandler::LIndent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
408 //r.BeginNumberedBullet(1, 200, 60);
410 //r.WriteText(wxT("first item"));
411 //r.EndNumberedBullet();
412 //r.BeginNumberedBullet(2, 200, 60);
414 //r.WriteText(wxT("second item."));
415 //r.EndNumberedBullet();
417 //A typical indentation-table for the item will be construct as the following
419 //1 x nbsp = 20 point
420 //ULI -> 100pt (UL/OL tag indents its sub element by 100 point)
421 //<--------- 100 pt ---------->|
422 //------------------------------------------------------
423 //| nbsp; nbsp;|<ul> |
424 //| |<-ULI-><li>first item |
425 //| |<-ULI-><li>second item |
427 //------------------------------------------------------
431 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
433 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() - m_indent
) - 100);
434 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
435 str
<< wxT("<td width=\"100%\">");
438 void wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx
& thisStyle
, wxString
& tag
)
440 //We can use number attribute of li tag but not all the browsers support it.
441 //also wxHtmlWindow doesn't support type attribute.
444 if( thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
445 tag
= wxT("<ol type=\"1\">");
446 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
447 tag
= wxT("<ol type=\"A\">");
448 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
449 tag
= wxT("<ol type=\"a\">");
450 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
451 tag
= wxT("<ol type=\"I\">");
452 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
453 tag
= wxT("<ol type=\"i\">");
461 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx
& thisStyle
)
463 switch( thisStyle
.GetAlignment() )
465 case wxTEXT_ALIGNMENT_LEFT
:
467 case wxTEXT_ALIGNMENT_RIGHT
:
469 case wxTEXT_ALIGNMENT_CENTER
:
470 return wxT("center");
471 case wxTEXT_ALIGNMENT_JUSTIFIED
:
472 return wxT("justify");
478 void wxRichTextHTMLHandler::Image_to_Base64(wxRichTextImage
* image
, wxOutputStream
& stream
)
480 wxTextOutputStream
str(stream
);
482 str
<< wxT("<img src=\"");
484 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
485 str
<< wxT(";base64,");
487 if (image
->GetImage().Ok() && !image
->GetImageBlock().GetData())
490 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
498 long wxRichTextHTMLHandler::Pt_To_Size(long size
)
500 //return most approximate size
501 if(size
< 9 ) return 1;
502 else if( size
< 11 ) return 2;
503 else if( size
< 14 ) return 3;
504 else if( size
< 18 ) return 4;
505 else if( size
< 23 ) return 5;
506 else if( size
< 30 ) return 6;
510 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
513 for(;indent
> 0; indent
-= 20)
514 in
.Append( wxT(" ") );
518 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
522 case wxBITMAP_TYPE_BMP
:
523 return wxT("image/bmp");
524 case wxBITMAP_TYPE_TIF
:
525 return wxT("image/tiff");
526 case wxBITMAP_TYPE_GIF
:
527 return wxT("image/gif");
528 case wxBITMAP_TYPE_PNG
:
529 return wxT("image/png");
530 case wxBITMAP_TYPE_JPEG
:
531 return wxT("image/jpeg");
533 return wxT("image/unknown");
537 //exim-style base64 encoder
538 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
540 //elements of enc64 array must be 8 bit values
541 //otherwise encoder will fail
542 //hmmm.. Does wxT macro define a char as 16 bit value
543 //when compiling with UNICODE option?
544 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
545 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
548 while( in_len
-- > 0 )
550 register wxChar a
, b
;
554 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
558 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
566 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
570 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
577 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
579 *p
++ = enc64
[ a
& 0x3f ];