1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
27 #include "wx/filename.h"
28 #include "wx/wfstream.h"
29 #include "wx/txtstrm.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler
, wxRichTextFileHandler
)
33 /// Can we handle this filename (if using files)? By default, checks the extension.
34 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
36 wxString path
, file
, ext
;
37 wxSplitPath(filename
, & path
, & file
, & ext
);
39 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
44 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
50 * We need to output only _changes_ in character formatting.
53 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
57 wxTextOutputStream
str(stream
);
59 wxTextAttrEx currentParaStyle
= buffer
->GetAttributes();
60 wxTextAttrEx currentCharStyle
= buffer
->GetAttributes();
62 str
<< wxT("<html><head></head><body>\n");
65 wxRichText may be support paper formats like a1/a2/a3/a4
66 when this widget grown enough, i should turn back and support its new features
69 str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td>");
71 wxString left_indent = SymbolicIndent(currentParaStyle.GetLeftIndent());
72 wxString right_indent = SymbolicIndent(currentParaStyle.GetRightIndent());
74 str << wxString::Format(wxT("%s</td><td></td><td>%s</td></tr><tr>"),
75 left_indent.c_str(), //Document-Wide Left Indent
76 right_indent.c_str()); //Document-Wide Right Indent
78 str << wxT("<td></td><td width=\"100%\">");
81 str
<< wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">");
83 str
<< wxString::Format(wxT("<font face=\"%s\" size=\"%i\" color=\"#%02X%02X%02X\" >"),
84 currentParaStyle
.GetFont().GetFaceName().c_str(), Pt_To_Size( currentParaStyle
.GetFont().GetPointSize() ),
85 currentParaStyle
.GetTextColour().Red(), currentParaStyle
.GetTextColour().Green(),
86 currentParaStyle
.GetTextColour().Blue());
88 //wxString align = GetAlignment( currentParaStyle.GetAlignment() );
89 //str << wxString::Format(wxT("<p align=\"%s\">"), align );
95 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
98 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
99 wxASSERT (para
!= NULL
);
103 OutputParagraphFormatting(currentParaStyle
, para
->GetAttributes(), stream
);
105 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
108 wxRichTextObject
* obj
= node2
->GetData();
109 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
110 if (textObj
&& !textObj
->IsEmpty())
112 BeginCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
);
114 str
<< textObj
->GetText();
116 EndCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
);
119 wxRichTextImage
* image
= wxDynamicCast(obj
, wxRichTextImage
);
120 if( image
&& !image
->IsEmpty())
121 Image_to_Base64( image
, stream
);
123 node2
= node2
->GetNext();
125 //OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
127 node
= node
->GetNext();
130 str
<< wxT("</font></td></tr></table></body></html>\n");
135 void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
137 wxTextOutputStream
str(stream
);
139 //Is the item bulleted one?
140 if( thisStyle
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
142 //Is there any opened list?
147 //Is the item among the previous ones
148 //Is the item one of the previous list tag's child items
149 if( (thisStyle
.GetLeftIndent() == (m_indent
+ 100)) || (thisStyle
.GetLeftIndent() < 100) )
150 str
<< wxT("<li>");//Yes it is
155 //So we should close the list tag
156 str
<< (m_is_ul
? wxT("</ul>") : wxT("</ol>"));
158 //And renavigate to new list's horizontal position
159 NavigateToListPosition(thisStyle
, str
);
162 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
164 TypeOfList(thisStyle
, tag
);
165 str
<< tag
<< wxT("<li>");
170 //No there isn't a list
172 //navigate to new list's horizontal position(indent)
173 NavigateToListPosition(thisStyle
, str
);
175 //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc.
177 TypeOfList(thisStyle
, tag
);
178 str
<< tag
<< wxT("<li>");
180 //Now we have a list, mark it.
186 //The item is not bulleted and there is a list what should be closed now.
189 str
<< (m_is_ul
? wxT("</ul>") : wxT("</ol>"));
190 //And mark as there is no an opened list
194 // does the item have an indentation ?
195 if( thisStyle
.GetLeftIndent() )
197 if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
201 if( (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) == m_indent
)
203 if( thisStyle
.GetLeftSubIndent() < 0 )
205 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
210 if( thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent() > m_indent
)
212 Indent(thisStyle
, str
);
213 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
214 m_indents
.Add( m_indent
);
218 int i
= m_indents
.size() - 1;
221 if( m_indent
< (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) )
223 Indent(thisStyle
, str
);
224 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
225 m_indents
.Add( m_indent
);
229 else if( m_indent
== (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) )
231 if( thisStyle
.GetLeftSubIndent() < 0 )
233 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
239 str
<< wxT("</td></tr></table>");
241 m_indents
.RemoveAt(i
);
243 if( i
< 1 ){m_indent
=0; break;}
244 m_indent
= m_indents
[i
-1];
252 Indent(thisStyle
, str
);
253 m_indent
= thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent();
254 m_indents
.Add( m_indent
);
260 //The item is not indented and there is a table(s) what 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 on the font properties of the item
274 if( thisStyle
.GetFont().GetFaceName() != currentStyle
.GetFont().GetFaceName() )
275 style
+= wxString::Format(wxT(" face=\"%s\""), thisStyle
.GetFont().GetFaceName().c_str());
276 if( thisStyle
.GetFont().GetPointSize() != currentStyle
.GetFont().GetPointSize() )
277 style
+= wxString::Format(wxT(" size=\"%i\""), Pt_To_Size(thisStyle
.GetFont().GetPointSize()) );
278 if( thisStyle
.GetTextColour() != currentStyle
.GetTextColour() )
279 style
+= wxString::Format(wxT(" color=\"#%02X%02X%02X\""), thisStyle
.GetTextColour().Red(),
280 thisStyle
.GetTextColour().Green(), thisStyle
.GetTextColour().Blue());
284 str
<< wxString::Format(wxT("<font %s >"), style
.c_str());
288 if( thisStyle
.GetFont().GetWeight() == wxBOLD
)
290 if( thisStyle
.GetFont().GetStyle() == wxITALIC
)
292 if( thisStyle
.GetFont().GetUnderlined() )
296 void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
298 wxTextOutputStream
str(stream
);
300 if( thisStyle
.GetFont().GetUnderlined() )
302 if( thisStyle
.GetFont().GetStyle() == wxITALIC
)
304 if( thisStyle
.GetFont().GetWeight() == wxBOLD
)
310 str
<< wxT("</font>");
314 /// Output paragraph formatting
315 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
)
317 //If there is no opened list currently, insert a <p> after every paragraph
320 wxTextOutputStream
str(stream
);
321 wxString align
= GetAlignment( thisStyle
);
322 str
<< wxString::Format(wxT("<p align=\"%s\">"), align
.c_str());
326 void wxRichTextHTMLHandler::NavigateToListPosition(const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
328 //indenting an item using an ul/ol tag is equal to inserting 5 x on its left side.
329 //so we should start from 100 point left
331 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
332 //of the item, horizontally?
333 if( m_indent
+ 100 < thisStyle
.GetLeftIndent() )
336 LIndent(thisStyle
, str
);
337 m_indent
= thisStyle
.GetLeftIndent() - 100;
338 m_indents
.Add( m_indent
);
343 int i
= m_indents
.size() - 1;
346 //Is the second td's left wall of the current indentaion table at the 100+ point-left-side
348 if( m_indent
+ 100 < thisStyle
.GetLeftIndent() )
351 LIndent(thisStyle
, str
);
352 m_indent
= thisStyle
.GetLeftIndent() - 100;
353 m_indents
.Add( m_indent
);
356 else if( m_indent
+ 100 == thisStyle
.GetLeftIndent() )
360 //No it is not, the second td's left wall of the current indentaion table is at the
361 //right side of the current item horizontally, so close it.
362 str
<< wxT("</td></tr></table>");
364 m_indents
.RemoveAt(i
);
366 if( i
< 1 ){m_indent
=0; break;}
367 m_indent
= m_indents
[i
-1];
371 void wxRichTextHTMLHandler::Indent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
373 //As a five year experienced web developer i assure you there is no way to indent an item
374 //in html way, but we can use tables.
378 //Item -> "Hello world"
379 //Its Left Indentation -> 100
380 //Its Left Sub-Indentation ->40
381 //A typical indentation-table for the item will be construct as the following
385 //LSI = Left Sub Indent
386 //LI = Left Indent - LSI
388 //-------------------------------------------
389 //| nbsp;|nbsp;nbsp;Hello World |
392 //| --LI-- | --LSI-- |
393 //-------------------------------------------
395 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
397 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() + thisStyle
.GetLeftSubIndent()) - m_indent
);
398 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
399 str
<< wxT("<td width=\"100%\">");
401 if( thisStyle
.GetLeftSubIndent() < 0 )
403 str
<< SymbolicIndent(~thisStyle
.GetLeftSubIndent());
407 void wxRichTextHTMLHandler::LIndent( const wxTextAttrEx
& thisStyle
, wxTextOutputStream
& str
)
410 //r.BeginNumberedBullet(1, 200, 60);
412 //r.WriteText(wxT("first item"));
413 //r.EndNumberedBullet();
414 //r.BeginNumberedBullet(2, 200, 60);
416 //r.WriteText(wxT("second item."));
417 //r.EndNumberedBullet();
419 //A typical indentation-table for the item will be construct as the following
421 //1 x nbsp = 20 point
422 //ULI -> 100pt (UL/OL tag indents its sub element by 100 point)
423 //<--------- 100 pt ---------->|
424 //------------------------------------------------------
425 //| nbsp; nbsp;|<ul> |
426 //| |<-ULI-><li>first item |
427 //| |<-ULI-><li>second item |
429 //------------------------------------------------------
433 str
<< wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
435 wxString symbolic_indent
= SymbolicIndent( (thisStyle
.GetLeftIndent() - m_indent
) - 100);
436 str
<< wxString::Format( wxT("<td>%s</td>"), symbolic_indent
.c_str() );
437 str
<< wxT("<td width=\"100%\">");
440 void wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx
& thisStyle
, wxString
& tag
)
442 //We can use number attribute of li tag but not all the browsers support it.
443 //also wxHtmlWindow doesn't support type attribute.
446 if( thisStyle
.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
))
447 tag
= wxT("<ol type=\"1\">");
448 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
449 tag
= wxT("<ol type=\"A\">");
450 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
451 tag
= wxT("<ol type=\"a\">");
452 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
453 tag
= wxT("<ol type=\"I\">");
454 else if( thisStyle
.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
455 tag
= wxT("<ol type=\"i\">");
463 wxString
wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx
& thisStyle
)
465 switch( thisStyle
.GetAlignment() )
467 case wxTEXT_ALIGNMENT_LEFT
:
469 case wxTEXT_ALIGNMENT_RIGHT
:
471 case wxTEXT_ALIGNMENT_CENTER
:
472 return wxT("center");
473 case wxTEXT_ALIGNMENT_JUSTIFIED
:
474 return wxT("justify");
480 void wxRichTextHTMLHandler::Image_to_Base64(wxRichTextImage
* image
, wxOutputStream
& stream
)
482 wxTextOutputStream
str(stream
);
484 str
<< wxT("<img src=\"");
486 str
<< GetMimeType(image
->GetImageBlock().GetImageType());
487 str
<< wxT(";base64,");
489 wxChar
* data
= b64enc( image
->GetImageBlock().GetData(), image
->GetImageBlock().GetDataSize() );
497 long wxRichTextHTMLHandler::Pt_To_Size(long size
)
499 //return most approximate size
500 if(size
< 9 ) return 1;
501 else if( size
< 11 ) return 2;
502 else if( size
< 14 ) return 3;
503 else if( size
< 18 ) return 4;
504 else if( size
< 23 ) return 5;
505 else if( size
< 30 ) return 6;
509 wxString
wxRichTextHTMLHandler::SymbolicIndent(long indent
)
512 for(;indent
> 0; indent
-= 20)
513 in
.Append( wxT(" ") );
517 const wxChar
* wxRichTextHTMLHandler::GetMimeType(int imageType
)
521 case wxBITMAP_TYPE_BMP
:
522 return wxT("image/bmp");
523 case wxBITMAP_TYPE_TIF
:
524 return wxT("image/tiff");
525 case wxBITMAP_TYPE_GIF
:
526 return wxT("image/gif");
527 case wxBITMAP_TYPE_PNG
:
528 return wxT("image/png");
529 case wxBITMAP_TYPE_JPEG
:
530 return wxT("image/jpeg");
532 return wxT("image/unknown");
536 //exim-style base64 encoder
537 wxChar
* wxRichTextHTMLHandler::b64enc( unsigned char* input
, size_t in_len
)
539 //elements of enc64 array must be 8 bit values
540 //otherwise encoder will fail
541 //hmmm.. Does wxT macro define a char as 16 bit value
542 //when compiling with UNICODE option?
543 static const wxChar enc64
[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
544 wxChar
* output
= new wxChar
[4*((in_len
+2)/3)+1];
547 while( in_len
-- > 0 )
549 register wxChar a
, b
;
553 *p
++ = enc64
[ (a
>> 2) & 0x3f ];
557 *p
++ = enc64
[ (a
<< 4 ) & 0x30 ];
565 *p
++ = enc64
[(( a
<< 4 ) | ((b
>> 4) &0xf )) & 0x3f];
569 *p
++ = enc64
[ (b
<< 2) & 0x3f ];
576 *p
++ = enc64
[ ((( b
<< 2 ) & 0x3f ) | ((a
>> 6)& 0x3)) & 0x3f ];
578 *p
++ = enc64
[ a
& 0x3f ];