]>
Commit | Line | Data |
---|---|---|
b71e9aa4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
40989e46 | 2 | // Name: src/richtext/richtexthtml.cpp |
b71e9aa4 JS |
3 | // Purpose: HTML I/O for wxRichTextCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 2005-09-30 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
40989e46 | 16 | #pragma hdrstop |
b71e9aa4 JS |
17 | #endif |
18 | ||
19 | #if wxUSE_RICHTEXT | |
20 | ||
21 | #include "wx/richtext/richtexthtml.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
b71e9aa4 JS |
24 | #endif |
25 | ||
26 | #include "wx/filename.h" | |
27 | #include "wx/wfstream.h" | |
28 | #include "wx/txtstrm.h" | |
29 | ||
30 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler) | |
31 | ||
32 | /// Can we handle this filename (if using files)? By default, checks the extension. | |
33 | bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const | |
34 | { | |
35 | wxString path, file, ext; | |
36 | wxSplitPath(filename, & path, & file, & ext); | |
37 | ||
38 | return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm")); | |
39 | } | |
40 | ||
41 | ||
42 | #if wxUSE_STREAMS | |
43 | bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream)) | |
44 | { | |
45 | return false; | |
46 | } | |
47 | ||
48 | /* | |
49 | * We need to output only _changes_ in character formatting. | |
50 | */ | |
51 | ||
52 | bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) | |
53 | { | |
54 | buffer->Defragment(); | |
40989e46 | 55 | |
b71e9aa4 | 56 | wxTextOutputStream str(stream); |
40989e46 | 57 | |
b71e9aa4 JS |
58 | wxTextAttrEx currentParaStyle = buffer->GetAttributes(); |
59 | wxTextAttrEx currentCharStyle = buffer->GetAttributes(); | |
40989e46 | 60 | |
b71e9aa4 | 61 | str << wxT("<html><head></head><body>\n"); |
40989e46 | 62 | |
2dec6761 JS |
63 | /* |
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 | |
66 | but not yet | |
40989e46 | 67 | |
2dec6761 | 68 | str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td>"); |
40989e46 | 69 | |
2dec6761 JS |
70 | wxString left_indent = SymbolicIndent(currentParaStyle.GetLeftIndent()); |
71 | wxString right_indent = SymbolicIndent(currentParaStyle.GetRightIndent()); | |
40989e46 | 72 | |
2dec6761 JS |
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 | |
40989e46 | 76 | |
2dec6761 JS |
77 | str << wxT("<td></td><td width=\"100%\">"); |
78 | */ | |
40989e46 | 79 | |
2dec6761 | 80 | str << wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"100%\">"); |
40989e46 WS |
81 | |
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()); | |
85 | ||
2dec6761 JS |
86 | //wxString align = GetAlignment( currentParaStyle.GetAlignment() ); |
87 | //str << wxString::Format(wxT("<p align=\"%s\">"), align ); | |
40989e46 | 88 | |
2dec6761 JS |
89 | m_font = false; |
90 | m_indent = 0; | |
91 | m_list = false; | |
40989e46 | 92 | |
b71e9aa4 JS |
93 | wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst(); |
94 | while (node) | |
95 | { | |
96 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); | |
97 | wxASSERT (para != NULL); | |
40989e46 | 98 | |
b71e9aa4 JS |
99 | if (para) |
100 | { | |
2dec6761 | 101 | OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream); |
40989e46 | 102 | |
b71e9aa4 JS |
103 | wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst(); |
104 | while (node2) | |
105 | { | |
106 | wxRichTextObject* obj = node2->GetData(); | |
107 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); | |
108 | if (textObj && !textObj->IsEmpty()) | |
109 | { | |
2dec6761 | 110 | BeginCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream); |
40989e46 | 111 | |
b71e9aa4 | 112 | str << textObj->GetText(); |
40989e46 | 113 | |
2dec6761 | 114 | EndCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream); |
b71e9aa4 | 115 | } |
40989e46 | 116 | |
2dec6761 JS |
117 | wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage); |
118 | if( image && !image->IsEmpty()) | |
119 | Image_to_Base64( image, stream ); | |
40989e46 | 120 | |
b71e9aa4 JS |
121 | node2 = node2->GetNext(); |
122 | } | |
d5363f57 | 123 | str << wxT("\n"); |
2dec6761 | 124 | //OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false); |
b71e9aa4 | 125 | } |
b71e9aa4 JS |
126 | node = node->GetNext(); |
127 | } | |
40989e46 | 128 | |
2dec6761 | 129 | str << wxT("</font></td></tr></table></body></html>\n"); |
40989e46 | 130 | |
b71e9aa4 JS |
131 | return true; |
132 | } | |
133 | ||
2dec6761 | 134 | void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxTextAttrEx& currentStyle, const wxTextAttrEx& thisStyle, wxOutputStream& stream) |
b71e9aa4 JS |
135 | { |
136 | wxTextOutputStream str(stream); | |
40989e46 | 137 | |
2dec6761 JS |
138 | //Is the item bulleted one? |
139 | if( thisStyle.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE ) | |
b71e9aa4 | 140 | { |
2dec6761 JS |
141 | //Is there any opened list? |
142 | if( m_list ) | |
143 | { | |
144 | //Yes there is | |
40989e46 | 145 | |
2dec6761 JS |
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 | |
150 | else | |
151 | { | |
152 | //No it isn't | |
40989e46 | 153 | |
2dec6761 JS |
154 | //So we should close the list tag |
155 | str << (m_is_ul ? wxT("</ul>") : wxT("</ol>")); | |
40989e46 | 156 | |
2dec6761 JS |
157 | //And renavigate to new list's horizontal position |
158 | NavigateToListPosition(thisStyle, str); | |
159 | //Ok it's done | |
40989e46 | 160 | |
2dec6761 JS |
161 | //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc. |
162 | wxString tag; | |
163 | TypeOfList(thisStyle, tag); | |
21e354f1 | 164 | str << tag << wxT("<li>"); |
2dec6761 JS |
165 | } |
166 | } | |
167 | else | |
168 | { | |
169 | //No there isn't a list | |
40989e46 | 170 | |
2dec6761 | 171 | //navigate to new list's horizontal position(indent) |
40989e46 WS |
172 | NavigateToListPosition(thisStyle, str); |
173 | ||
2dec6761 JS |
174 | //Get the appropriate tag, an ol for numerical values, an ul for dot, square etc. |
175 | wxString tag; | |
176 | TypeOfList(thisStyle, tag); | |
21e354f1 | 177 | str << tag << wxT("<li>"); |
40989e46 | 178 | |
2dec6761 JS |
179 | //Now we have a list, mark it. |
180 | m_list = true; | |
40989e46 | 181 | } |
b71e9aa4 | 182 | } |
2dec6761 JS |
183 | else if( m_list ) |
184 | { | |
40989e46 | 185 | //The item is not bulleted and there is a list what should be closed now. |
2dec6761 | 186 | //So close the list |
b71e9aa4 | 187 | |
2dec6761 JS |
188 | str << (m_is_ul ? wxT("</ul>") : wxT("</ol>")); |
189 | //And mark as there is no an opened list | |
190 | m_list = false; | |
191 | } | |
40989e46 | 192 | |
2dec6761 JS |
193 | // does the item have an indentation ? |
194 | if( thisStyle.GetLeftIndent() ) | |
b71e9aa4 | 195 | { |
2dec6761 JS |
196 | if( thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ) |
197 | { | |
198 | if( m_indent ) | |
199 | { | |
200 | if( (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) == m_indent ) | |
201 | { | |
202 | if( thisStyle.GetLeftSubIndent() < 0 ) | |
203 | { | |
21e354f1 | 204 | str << SymbolicIndent(~thisStyle.GetLeftSubIndent()); |
2dec6761 JS |
205 | } |
206 | } | |
207 | else | |
208 | { | |
209 | if( thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent() > m_indent ) | |
210 | { | |
211 | Indent(thisStyle, str); | |
212 | m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent(); | |
213 | m_indents.Add( m_indent ); | |
214 | } | |
215 | else | |
216 | { | |
217 | int i = m_indents.size() - 1; | |
218 | for(; i > -1; i--) | |
219 | { | |
220 | if( m_indent < (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) ) | |
221 | { | |
222 | Indent(thisStyle, str); | |
223 | m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent(); | |
224 | m_indents.Add( m_indent ); | |
40989e46 | 225 | |
2dec6761 JS |
226 | break; |
227 | } | |
228 | else if( m_indent == (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) ) | |
229 | { | |
230 | if( thisStyle.GetLeftSubIndent() < 0 ) | |
231 | { | |
21e354f1 | 232 | str << SymbolicIndent(~thisStyle.GetLeftSubIndent()); |
2dec6761 JS |
233 | } |
234 | break; | |
235 | } | |
236 | else | |
237 | { | |
238 | str << wxT("</td></tr></table>"); | |
40989e46 | 239 | |
2dec6761 | 240 | m_indents.RemoveAt(i); |
40989e46 | 241 | |
2dec6761 JS |
242 | if( i < 1 ){m_indent=0; break;} |
243 | m_indent = m_indents[i-1]; | |
244 | } | |
245 | } | |
246 | } | |
247 | } | |
248 | } | |
249 | else | |
250 | { | |
251 | Indent(thisStyle, str); | |
252 | m_indent = thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent(); | |
253 | m_indents.Add( m_indent ); | |
254 | } | |
255 | } | |
b71e9aa4 | 256 | } |
2dec6761 | 257 | else if( m_indent ) |
b71e9aa4 | 258 | { |
2dec6761 | 259 | //The item is not indented and there is a table(s) what should be closed now. |
40989e46 | 260 | |
2dec6761 JS |
261 | //So close them |
262 | for(unsigned int i = 0; i < m_indents.size(); i++ ) | |
263 | str << wxT("</td></tr></table>"); | |
40989e46 | 264 | |
2dec6761 JS |
265 | m_indent = 0; |
266 | m_indents.Clear(); | |
b71e9aa4 | 267 | } |
40989e46 WS |
268 | |
269 | ||
2dec6761 | 270 | wxString style; |
40989e46 | 271 | |
2dec6761 JS |
272 | //Is there any change on the font properties of the item |
273 | if( thisStyle.GetFont().GetFaceName() != currentStyle.GetFont().GetFaceName() ) | |
21e354f1 | 274 | style += wxString::Format(wxT(" face=\"%s\""), thisStyle.GetFont().GetFaceName().c_str()); |
2dec6761 | 275 | if( thisStyle.GetFont().GetPointSize() != currentStyle.GetFont().GetPointSize() ) |
d0c3476b | 276 | style += wxString::Format(wxT(" size=\"%ld\""), Pt_To_Size(thisStyle.GetFont().GetPointSize()) ); |
2dec6761 | 277 | if( thisStyle.GetTextColour() != currentStyle.GetTextColour() ) |
40989e46 WS |
278 | style += wxString::Format(wxT(" color=\"%s\""), thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str()); |
279 | ||
21e354f1 JS |
280 | if( style.size() ) |
281 | { | |
282 | str << wxString::Format(wxT("<font %s >"), style.c_str()); | |
283 | m_font = true; | |
284 | } | |
40989e46 | 285 | |
2dec6761 JS |
286 | if( thisStyle.GetFont().GetWeight() == wxBOLD ) |
287 | str << wxT("<b>"); | |
288 | if( thisStyle.GetFont().GetStyle() == wxITALIC ) | |
289 | str << wxT("<i>"); | |
290 | if( thisStyle.GetFont().GetUnderlined() ) | |
291 | str << wxT("<u>"); | |
b71e9aa4 JS |
292 | } |
293 | ||
2dec6761 | 294 | void wxRichTextHTMLHandler::EndCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream) |
b71e9aa4 | 295 | { |
b71e9aa4 | 296 | wxTextOutputStream str(stream); |
40989e46 | 297 | |
2dec6761 JS |
298 | if( thisStyle.GetFont().GetUnderlined() ) |
299 | str << wxT("</u>"); | |
300 | if( thisStyle.GetFont().GetStyle() == wxITALIC ) | |
301 | str << wxT("</i>"); | |
302 | if( thisStyle.GetFont().GetWeight() == wxBOLD ) | |
303 | str << wxT("</b>"); | |
40989e46 | 304 | |
2dec6761 JS |
305 | if( m_font ) |
306 | { | |
307 | m_font = false; | |
308 | str << wxT("</font>"); | |
309 | } | |
310 | } | |
b71e9aa4 | 311 | |
2dec6761 JS |
312 | /// Output paragraph formatting |
313 | void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream) | |
314 | { | |
315 | //If there is no opened list currently, insert a <p> after every paragraph | |
316 | if(!m_list) | |
b71e9aa4 | 317 | { |
2dec6761 JS |
318 | wxTextOutputStream str(stream); |
319 | wxString align = GetAlignment( thisStyle ); | |
21e354f1 | 320 | str << wxString::Format(wxT("<p align=\"%s\">"), align.c_str()); |
b71e9aa4 | 321 | } |
2dec6761 | 322 | } |
b71e9aa4 | 323 | |
2dec6761 JS |
324 | void wxRichTextHTMLHandler::NavigateToListPosition(const wxTextAttrEx& thisStyle, wxTextOutputStream& str) |
325 | { | |
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 | |
40989e46 WS |
328 | |
329 | //Is the second td's left wall of the current indentaion table at the 100+ point-left-side | |
2dec6761 JS |
330 | //of the item, horizontally? |
331 | if( m_indent + 100 < thisStyle.GetLeftIndent() ) | |
b71e9aa4 | 332 | { |
2dec6761 JS |
333 | //yes it is |
334 | LIndent(thisStyle, str); | |
335 | m_indent = thisStyle.GetLeftIndent() - 100; | |
336 | m_indents.Add( m_indent ); | |
337 | return; | |
b71e9aa4 | 338 | } |
2dec6761 | 339 | //No it isn't |
40989e46 | 340 | |
2dec6761 JS |
341 | int i = m_indents.size() - 1; |
342 | for(; i > -1; i--) | |
343 | { | |
40989e46 | 344 | //Is the second td's left wall of the current indentaion table at the 100+ point-left-side |
2dec6761 JS |
345 | //of the item ? |
346 | if( m_indent + 100 < thisStyle.GetLeftIndent() ) | |
347 | { | |
348 | //Yes it is | |
349 | LIndent(thisStyle, str); | |
350 | m_indent = thisStyle.GetLeftIndent() - 100; | |
351 | m_indents.Add( m_indent ); | |
352 | break; | |
353 | } | |
354 | else if( m_indent + 100 == thisStyle.GetLeftIndent() ) | |
355 | break;//exact match | |
356 | else | |
357 | { | |
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>"); | |
40989e46 | 361 | |
2dec6761 | 362 | m_indents.RemoveAt(i); |
40989e46 | 363 | |
2dec6761 JS |
364 | if( i < 1 ){m_indent=0; break;} |
365 | m_indent = m_indents[i-1]; | |
366 | } | |
367 | } | |
368 | } | |
369 | void wxRichTextHTMLHandler::Indent( const wxTextAttrEx& thisStyle, wxTextOutputStream& str ) | |
370 | { | |
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. | |
40989e46 WS |
373 | |
374 | ||
375 | ||
2dec6761 JS |
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 | |
40989e46 | 380 | |
2dec6761 JS |
381 | //3 x nbsp = 60 |
382 | //2 x nbsp = 40 | |
383 | //LSI = Left Sub Indent | |
384 | //LI = Left Indent - LSI | |
385 | // | |
386 | //------------------------------------------- | |
387 | //| nbsp;|nbsp;nbsp;Hello World | | |
40989e46 | 388 | //| | | | | |
2dec6761 JS |
389 | //| V | V | |
390 | //| --LI-- | --LSI-- | | |
391 | //------------------------------------------- | |
40989e46 | 392 | |
2dec6761 | 393 | str << wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"); |
40989e46 | 394 | |
2dec6761 | 395 | wxString symbolic_indent = SymbolicIndent( (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent()) - m_indent ); |
21e354f1 | 396 | str << wxString::Format( wxT("<td>%s</td>"), symbolic_indent.c_str() ); |
2dec6761 | 397 | str << wxT("<td width=\"100%\">"); |
40989e46 | 398 | |
2dec6761 JS |
399 | if( thisStyle.GetLeftSubIndent() < 0 ) |
400 | { | |
21e354f1 | 401 | str << SymbolicIndent(~thisStyle.GetLeftSubIndent()); |
2dec6761 JS |
402 | } |
403 | } | |
404 | ||
405 | void wxRichTextHTMLHandler::LIndent( const wxTextAttrEx& thisStyle, wxTextOutputStream& str ) | |
406 | { | |
407 | //Code: | |
408 | //r.BeginNumberedBullet(1, 200, 60); | |
409 | //r.Newline(); | |
410 | //r.WriteText(wxT("first item")); | |
411 | //r.EndNumberedBullet(); | |
412 | //r.BeginNumberedBullet(2, 200, 60); | |
413 | //r.Newline(); | |
414 | //r.WriteText(wxT("second item.")); | |
415 | //r.EndNumberedBullet(); | |
416 | // | |
417 | //A typical indentation-table for the item will be construct as the following | |
40989e46 | 418 | |
2dec6761 JS |
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> | | |
40989e46 | 424 | //| |<-ULI-><li>first item | |
2dec6761 JS |
425 | //| |<-ULI-><li>second item | |
426 | //| |</ul> | | |
427 | //------------------------------------------------------ | |
40989e46 WS |
428 | // |<-100->| |
429 | ||
430 | ||
2dec6761 | 431 | str << wxT("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"); |
40989e46 | 432 | |
2dec6761 | 433 | wxString symbolic_indent = SymbolicIndent( (thisStyle.GetLeftIndent() - m_indent) - 100); |
21e354f1 | 434 | str << wxString::Format( wxT("<td>%s</td>"), symbolic_indent.c_str() ); |
2dec6761 JS |
435 | str << wxT("<td width=\"100%\">"); |
436 | } | |
437 | ||
438 | void wxRichTextHTMLHandler::TypeOfList( const wxTextAttrEx& thisStyle, wxString& tag ) | |
439 | { | |
40989e46 | 440 | //We can use number attribute of li tag but not all the browsers support it. |
2dec6761 | 441 | //also wxHtmlWindow doesn't support type attribute. |
40989e46 | 442 | |
2dec6761 JS |
443 | m_is_ul = false; |
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\">"); | |
b71e9aa4 JS |
454 | else |
455 | { | |
2dec6761 JS |
456 | tag = wxT("<ul>"); |
457 | m_is_ul = true; | |
b71e9aa4 JS |
458 | } |
459 | } | |
460 | ||
2dec6761 JS |
461 | wxString wxRichTextHTMLHandler::GetAlignment( const wxTextAttrEx& thisStyle ) |
462 | { | |
463 | switch( thisStyle.GetAlignment() ) | |
464 | { | |
465 | case wxTEXT_ALIGNMENT_LEFT: | |
466 | return wxT("left"); | |
467 | case wxTEXT_ALIGNMENT_RIGHT: | |
468 | return wxT("right"); | |
469 | case wxTEXT_ALIGNMENT_CENTER: | |
470 | return wxT("center"); | |
471 | case wxTEXT_ALIGNMENT_JUSTIFIED: | |
472 | return wxT("justify"); | |
473 | default: | |
474 | return wxT("left"); | |
475 | } | |
476 | } | |
477 | ||
478 | void wxRichTextHTMLHandler::Image_to_Base64(wxRichTextImage* image, wxOutputStream& stream) | |
479 | { | |
480 | wxTextOutputStream str(stream); | |
40989e46 | 481 | |
2dec6761 JS |
482 | str << wxT("<img src=\""); |
483 | str << wxT("data:"); | |
484 | str << GetMimeType(image->GetImageBlock().GetImageType()); | |
485 | str << wxT(";base64,"); | |
40989e46 | 486 | |
d5363f57 JS |
487 | if (image->GetImage().Ok() && !image->GetImageBlock().GetData()) |
488 | image->MakeBlock(); | |
40989e46 | 489 | |
2dec6761 JS |
490 | wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() ); |
491 | str << data; | |
40989e46 | 492 | |
2dec6761 | 493 | delete[] data; |
40989e46 | 494 | |
2dec6761 JS |
495 | str << wxT("\" />"); |
496 | } | |
497 | ||
498 | long wxRichTextHTMLHandler::Pt_To_Size(long size) | |
499 | { | |
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; | |
507 | else return 7; | |
508 | } | |
509 | ||
510 | wxString wxRichTextHTMLHandler::SymbolicIndent(long indent) | |
511 | { | |
512 | wxString in; | |
513 | for(;indent > 0; indent -= 20) | |
514 | in.Append( wxT(" ") ); | |
515 | return in; | |
516 | } | |
517 | ||
21e354f1 | 518 | const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType) |
2dec6761 JS |
519 | { |
520 | switch(imageType) | |
521 | { | |
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"); | |
532 | default: | |
533 | return wxT("image/unknown"); | |
534 | } | |
535 | } | |
536 | ||
537 | //exim-style base64 encoder | |
538 | wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len ) | |
539 | { | |
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 | |
40989e46 | 543 | //when compiling with UNICODE option? |
21e354f1 | 544 | static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); |
2dec6761 JS |
545 | wxChar* output = new wxChar[4*((in_len+2)/3)+1]; |
546 | wxChar* p = output; | |
40989e46 | 547 | |
2dec6761 JS |
548 | while( in_len-- > 0 ) |
549 | { | |
550 | register wxChar a, b; | |
40989e46 | 551 | |
2dec6761 | 552 | a = *input++; |
40989e46 | 553 | |
2dec6761 | 554 | *p++ = enc64[ (a >> 2) & 0x3f ]; |
40989e46 | 555 | |
2dec6761 JS |
556 | if( in_len-- <= 0 ) |
557 | { | |
558 | *p++ = enc64[ (a << 4 ) & 0x30 ]; | |
559 | *p++ = '='; | |
560 | *p++ = '='; | |
561 | break; | |
562 | } | |
40989e46 | 563 | |
2dec6761 | 564 | b = *input++; |
40989e46 | 565 | |
2dec6761 | 566 | *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f]; |
40989e46 | 567 | |
2dec6761 JS |
568 | if( in_len-- <= 0 ) |
569 | { | |
570 | *p++ = enc64[ (b << 2) & 0x3f ]; | |
571 | *p++ = '='; | |
572 | break; | |
573 | } | |
40989e46 | 574 | |
2dec6761 | 575 | a = *input++; |
40989e46 | 576 | |
2dec6761 | 577 | *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ]; |
40989e46 | 578 | |
2dec6761 JS |
579 | *p++ = enc64[ a & 0x3f ]; |
580 | } | |
581 | *p = 0; | |
40989e46 | 582 | |
2dec6761 JS |
583 | return output; |
584 | } | |
b71e9aa4 | 585 | #endif |
2dec6761 | 586 | // wxUSE_STREAMS |
b71e9aa4 JS |
587 | |
588 | #endif | |
2dec6761 | 589 | // wxUSE_RICHTEXT |