]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/richtext/richtexthtml.cpp | |
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__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_RICHTEXT | |
20 | ||
21 | #include "wx/richtext/richtexthtml.h" | |
22 | #include "wx/richtext/richtextstyles.h" | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #endif | |
26 | ||
27 | #include "wx/filename.h" | |
28 | #include "wx/wfstream.h" | |
29 | #include "wx/txtstrm.h" | |
30 | ||
31 | #if wxUSE_FILESYSTEM | |
32 | #include "wx/filesys.h" | |
33 | #include "wx/fs_mem.h" | |
34 | #endif | |
35 | ||
36 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler) | |
37 | ||
38 | int wxRichTextHTMLHandler::sm_fileCounter = 1; | |
39 | ||
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) | |
42 | { | |
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); | |
50 | } | |
51 | ||
52 | /// Can we handle this filename (if using files)? By default, checks the extension. | |
53 | bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const | |
54 | { | |
55 | wxString path, file, ext; | |
56 | wxFileName::SplitPath(filename, & path, & file, & ext); | |
57 | ||
58 | return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm")); | |
59 | } | |
60 | ||
61 | ||
62 | #if wxUSE_STREAMS | |
63 | bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream)) | |
64 | { | |
65 | return false; | |
66 | } | |
67 | ||
68 | /* | |
69 | * We need to output only _changes_ in character formatting. | |
70 | */ | |
71 | ||
72 | bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) | |
73 | { | |
74 | m_buffer = buffer; | |
75 | ||
76 | ClearTemporaryImageLocations(); | |
77 | ||
78 | wxRichTextDrawingContext context(buffer); | |
79 | buffer->Defragment(context); | |
80 | ||
81 | #if wxUSE_UNICODE | |
82 | wxCSConv* customEncoding = NULL; | |
83 | wxMBConv* conv = NULL; | |
84 | if (!GetEncoding().IsEmpty()) | |
85 | { | |
86 | customEncoding = new wxCSConv(GetEncoding()); | |
87 | if (!customEncoding->IsOk()) | |
88 | { | |
89 | wxDELETE(customEncoding); | |
90 | } | |
91 | } | |
92 | if (customEncoding) | |
93 | conv = customEncoding; | |
94 | else | |
95 | conv = & wxConvUTF8; | |
96 | #endif | |
97 | ||
98 | { | |
99 | #if wxUSE_UNICODE | |
100 | wxTextOutputStream str(stream, wxEOL_NATIVE, *conv); | |
101 | #else | |
102 | wxTextOutputStream str(stream, wxEOL_NATIVE); | |
103 | #endif | |
104 | ||
105 | wxRichTextAttr currentParaStyle = buffer->GetAttributes(); | |
106 | wxRichTextAttr currentCharStyle = buffer->GetAttributes(); | |
107 | ||
108 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) | |
109 | str << wxT("<html><head></head><body>\n"); | |
110 | ||
111 | OutputFont(currentParaStyle, str); | |
112 | ||
113 | m_font = false; | |
114 | m_inTable = false; | |
115 | ||
116 | m_indents.Clear(); | |
117 | m_listTypes.Clear(); | |
118 | ||
119 | wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst(); | |
120 | while (node) | |
121 | { | |
122 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); | |
123 | wxASSERT (para != NULL); | |
124 | ||
125 | if (para) | |
126 | { | |
127 | wxRichTextAttr paraStyle(para->GetCombinedAttributes()); | |
128 | ||
129 | BeginParagraphFormatting(currentParaStyle, paraStyle, str); | |
130 | ||
131 | wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst(); | |
132 | while (node2) | |
133 | { | |
134 | wxRichTextObject* obj = node2->GetData(); | |
135 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); | |
136 | if (textObj && !textObj->IsEmpty()) | |
137 | { | |
138 | wxRichTextAttr charStyle(para->GetCombinedAttributes(obj->GetAttributes())); | |
139 | BeginCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); | |
140 | ||
141 | wxString text = textObj->GetText(); | |
142 | ||
143 | if (charStyle.HasTextEffects() && (charStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)) | |
144 | text.MakeUpper(); | |
145 | ||
146 | wxString toReplace = wxRichTextLineBreakChar; | |
147 | text.Replace(toReplace, wxT("<br>")); | |
148 | ||
149 | str << text; | |
150 | ||
151 | EndCharacterFormatting(currentCharStyle, charStyle, paraStyle, str); | |
152 | } | |
153 | ||
154 | wxRichTextImage* image = wxDynamicCast(obj, wxRichTextImage); | |
155 | if( image && (!image->IsEmpty() || image->GetImageBlock().GetData())) | |
156 | WriteImage( image, stream ); | |
157 | ||
158 | node2 = node2->GetNext(); | |
159 | } | |
160 | ||
161 | EndParagraphFormatting(currentParaStyle, paraStyle, str); | |
162 | ||
163 | str << wxT("\n"); | |
164 | } | |
165 | node = node->GetNext(); | |
166 | } | |
167 | ||
168 | CloseLists(-1, str); | |
169 | ||
170 | str << wxT("</font>"); | |
171 | ||
172 | if ((GetFlags() & wxRICHTEXT_HANDLER_NO_HEADER_FOOTER) == 0) | |
173 | str << wxT("</body></html>"); | |
174 | ||
175 | str << wxT("\n"); | |
176 | } | |
177 | ||
178 | #if wxUSE_UNICODE | |
179 | if (customEncoding) | |
180 | delete customEncoding; | |
181 | #endif | |
182 | ||
183 | m_buffer = NULL; | |
184 | ||
185 | return true; | |
186 | } | |
187 | ||
188 | void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, const wxRichTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& str) | |
189 | { | |
190 | wxString style; | |
191 | ||
192 | // Is there any change in the font properties of the item? | |
193 | if (thisStyle.GetFontFaceName() != currentStyle.GetFontFaceName()) | |
194 | { | |
195 | wxString faceName(thisStyle.GetFontFaceName()); | |
196 | style += wxString::Format(wxT(" face=\"%s\""), faceName.c_str()); | |
197 | } | |
198 | if (thisStyle.GetFontSize() != currentStyle.GetFontSize()) | |
199 | style += wxString::Format(wxT(" size=\"%ld\""), PtToSize(thisStyle.GetFontSize())); | |
200 | ||
201 | bool bTextColourChanged = (thisStyle.GetTextColour() != currentStyle.GetTextColour()); | |
202 | bool bBackgroundColourChanged = (thisStyle.GetBackgroundColour() != currentStyle.GetBackgroundColour()); | |
203 | if (bTextColourChanged || bBackgroundColourChanged) | |
204 | { | |
205 | style += wxT(" style=\""); | |
206 | ||
207 | if (bTextColourChanged) | |
208 | { | |
209 | wxString color(thisStyle.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX)); | |
210 | style += wxString::Format(wxT("color: %s"), color.c_str()); | |
211 | } | |
212 | if (bTextColourChanged && bBackgroundColourChanged) | |
213 | style += wxT(";"); | |
214 | if (bBackgroundColourChanged) | |
215 | { | |
216 | wxString color(thisStyle.GetBackgroundColour().GetAsString(wxC2S_HTML_SYNTAX)); | |
217 | style += wxString::Format(wxT("background-color: %s"), color.c_str()); | |
218 | } | |
219 | ||
220 | style += wxT("\""); | |
221 | } | |
222 | ||
223 | if (style.size()) | |
224 | { | |
225 | str << wxString::Format(wxT("<font %s >"), style.c_str()); | |
226 | m_font = true; | |
227 | } | |
228 | ||
229 | if (thisStyle.GetFontWeight() == wxFONTWEIGHT_BOLD) | |
230 | str << wxT("<b>"); | |
231 | if (thisStyle.GetFontStyle() == wxFONTSTYLE_ITALIC) | |
232 | str << wxT("<i>"); | |
233 | if (thisStyle.GetFontUnderlined()) | |
234 | str << wxT("<u>"); | |
235 | ||
236 | if (thisStyle.HasURL()) | |
237 | str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">"); | |
238 | ||
239 | if (thisStyle.HasTextEffects()) | |
240 | { | |
241 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH) | |
242 | str << wxT("<del>"); | |
243 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) | |
244 | str << wxT("<sup>"); | |
245 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) | |
246 | str << wxT("<sub>"); | |
247 | } | |
248 | } | |
249 | ||
250 | void wxRichTextHTMLHandler::EndCharacterFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, const wxRichTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream) | |
251 | { | |
252 | if (thisStyle.HasURL()) | |
253 | stream << wxT("</a>"); | |
254 | ||
255 | if (thisStyle.GetFontUnderlined()) | |
256 | stream << wxT("</u>"); | |
257 | if (thisStyle.GetFontStyle() == wxFONTSTYLE_ITALIC) | |
258 | stream << wxT("</i>"); | |
259 | if (thisStyle.GetFontWeight() == wxFONTWEIGHT_BOLD) | |
260 | stream << wxT("</b>"); | |
261 | ||
262 | if (thisStyle.HasTextEffects()) | |
263 | { | |
264 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH) | |
265 | stream << wxT("</del>"); | |
266 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) | |
267 | stream << wxT("</sup>"); | |
268 | if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) | |
269 | stream << wxT("</sub>"); | |
270 | } | |
271 | ||
272 | if (m_font) | |
273 | { | |
274 | m_font = false; | |
275 | stream << wxT("</font>"); | |
276 | } | |
277 | } | |
278 | ||
279 | /// Begin paragraph formatting | |
280 | void wxRichTextHTMLHandler::BeginParagraphFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, wxTextOutputStream& str) | |
281 | { | |
282 | if (thisStyle.HasPageBreak()) | |
283 | { | |
284 | str << wxT("<div style=\"page-break-after:always\"></div>\n"); | |
285 | } | |
286 | ||
287 | if (thisStyle.HasLeftIndent() && thisStyle.GetLeftIndent() != 0) | |
288 | { | |
289 | if (thisStyle.HasBulletStyle()) | |
290 | { | |
291 | int indent = thisStyle.GetLeftIndent(); | |
292 | ||
293 | // Close levels high than this | |
294 | CloseLists(indent, str); | |
295 | ||
296 | if (m_indents.GetCount() > 0 && indent == m_indents.Last()) | |
297 | { | |
298 | // Same level, no need to start a new list | |
299 | } | |
300 | else if (m_indents.GetCount() == 0 || indent > m_indents.Last()) | |
301 | { | |
302 | m_indents.Add(indent); | |
303 | ||
304 | wxString tag; | |
305 | int listType = TypeOfList(thisStyle, tag); | |
306 | m_listTypes.Add(listType); | |
307 | ||
308 | // wxHTML needs an extra <p> before a list when using <p> ... </p> in previous paragraphs. | |
309 | // TODO: pass a flag that indicates we're using wxHTML. | |
310 | str << wxT("<p>\n"); | |
311 | ||
312 | str << tag; | |
313 | } | |
314 | ||
315 | str << wxT("<li> "); | |
316 | } | |
317 | else | |
318 | { | |
319 | CloseLists(-1, str); | |
320 | ||
321 | wxString align = GetAlignment(thisStyle); | |
322 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); | |
323 | ||
324 | wxString styleStr; | |
325 | ||
326 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore()) | |
327 | { | |
328 | float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0; | |
329 | ||
330 | styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM); | |
331 | } | |
332 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter()) | |
333 | { | |
334 | float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0; | |
335 | ||
336 | styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM); | |
337 | } | |
338 | ||
339 | float indentLeftMM = (thisStyle.GetLeftIndent() + thisStyle.GetLeftSubIndent())/10.0; | |
340 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (indentLeftMM > 0.0)) | |
341 | { | |
342 | styleStr += wxString::Format(wxT("margin-left: %.2fmm; "), indentLeftMM); | |
343 | } | |
344 | float indentRightMM = thisStyle.GetRightIndent()/10.0; | |
345 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasRightIndent() && (indentRightMM > 0.0)) | |
346 | { | |
347 | styleStr += wxString::Format(wxT("margin-right: %.2fmm; "), indentRightMM); | |
348 | } | |
349 | // First line indentation | |
350 | float firstLineIndentMM = - thisStyle.GetLeftSubIndent() / 10.0; | |
351 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && (firstLineIndentMM > 0.0)) | |
352 | { | |
353 | styleStr += wxString::Format(wxT("text-indent: %.2fmm; "), firstLineIndentMM); | |
354 | } | |
355 | ||
356 | if (!styleStr.IsEmpty()) | |
357 | str << wxT(" style=\"") << styleStr << wxT("\""); | |
358 | ||
359 | str << wxT(">"); | |
360 | ||
361 | // TODO: convert to pixels | |
362 | int indentPixels = static_cast<int>(indentLeftMM*10/4); | |
363 | ||
364 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) | |
365 | { | |
366 | // Use a table to do indenting if we don't have CSS | |
367 | str << wxString::Format(wxT("<table border=0 cellpadding=0 cellspacing=0><tr><td width=\"%d\"></td><td>"), indentPixels); | |
368 | m_inTable = true; | |
369 | } | |
370 | ||
371 | if (((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) == 0) && (thisStyle.GetLeftSubIndent() < 0)) | |
372 | { | |
373 | str << SymbolicIndent( - thisStyle.GetLeftSubIndent()); | |
374 | } | |
375 | } | |
376 | } | |
377 | else | |
378 | { | |
379 | CloseLists(-1, str); | |
380 | ||
381 | wxString align = GetAlignment(thisStyle); | |
382 | str << wxString::Format(wxT("<p align=\"%s\""), align.c_str()); | |
383 | ||
384 | wxString styleStr; | |
385 | ||
386 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingBefore()) | |
387 | { | |
388 | float spacingBeforeMM = thisStyle.GetParagraphSpacingBefore() / 10.0; | |
389 | ||
390 | styleStr += wxString::Format(wxT("margin-top: %.2fmm; "), spacingBeforeMM); | |
391 | } | |
392 | if ((GetFlags() & wxRICHTEXT_HANDLER_USE_CSS) && thisStyle.HasParagraphSpacingAfter()) | |
393 | { | |
394 | float spacingAfterMM = thisStyle.GetParagraphSpacingAfter() / 10.0; | |
395 | ||
396 | styleStr += wxString::Format(wxT("margin-bottom: %.2fmm; "), spacingAfterMM); | |
397 | } | |
398 | ||
399 | if (!styleStr.IsEmpty()) | |
400 | str << wxT(" style=\"") << styleStr << wxT("\""); | |
401 | ||
402 | str << wxT(">"); | |
403 | } | |
404 | OutputFont(thisStyle, str); | |
405 | } | |
406 | ||
407 | /// End paragraph formatting | |
408 | void wxRichTextHTMLHandler::EndParagraphFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, wxTextOutputStream& stream) | |
409 | { | |
410 | if (thisStyle.HasFont()) | |
411 | stream << wxT("</font>"); | |
412 | ||
413 | if (m_inTable) | |
414 | { | |
415 | stream << wxT("</td></tr></table></p>\n"); | |
416 | m_inTable = false; | |
417 | } | |
418 | else if (!thisStyle.HasBulletStyle()) | |
419 | stream << wxT("</p>\n"); | |
420 | } | |
421 | ||
422 | /// Closes lists to level (-1 means close all) | |
423 | void wxRichTextHTMLHandler::CloseLists(int level, wxTextOutputStream& str) | |
424 | { | |
425 | // Close levels high than this | |
426 | int i = m_indents.GetCount()-1; | |
427 | while (i >= 0) | |
428 | { | |
429 | int l = m_indents[i]; | |
430 | if (l > level) | |
431 | { | |
432 | if (m_listTypes[i] == 0) | |
433 | str << wxT("</ol>"); | |
434 | else | |
435 | str << wxT("</ul>"); | |
436 | m_indents.RemoveAt(i); | |
437 | m_listTypes.RemoveAt(i); | |
438 | } | |
439 | else | |
440 | break; | |
441 | i --; | |
442 | } | |
443 | } | |
444 | ||
445 | /// Output font tag | |
446 | void wxRichTextHTMLHandler::OutputFont(const wxRichTextAttr& style, wxTextOutputStream& stream) | |
447 | { | |
448 | if (style.HasFont()) | |
449 | { | |
450 | stream << wxString::Format(wxT("<font face=\"%s\" size=\"%ld\""), style.GetFontFaceName().c_str(), PtToSize(style.GetFontSize())); | |
451 | if (style.HasTextColour()) | |
452 | stream << wxString::Format(wxT(" color=\"%s\""), style.GetTextColour().GetAsString(wxC2S_HTML_SYNTAX).c_str()); | |
453 | stream << wxT(" >"); | |
454 | } | |
455 | } | |
456 | ||
457 | int wxRichTextHTMLHandler::TypeOfList( const wxRichTextAttr& thisStyle, wxString& tag ) | |
458 | { | |
459 | // We can use number attribute of li tag but not all the browsers support it. | |
460 | // also wxHtmlWindow doesn't support type attribute. | |
461 | ||
462 | bool m_is_ul = false; | |
463 | if (thisStyle.GetBulletStyle() == (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD)) | |
464 | tag = wxT("<ol type=\"1\">"); | |
465 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER) | |
466 | tag = wxT("<ol type=\"A\">"); | |
467 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER) | |
468 | tag = wxT("<ol type=\"a\">"); | |
469 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER) | |
470 | tag = wxT("<ol type=\"I\">"); | |
471 | else if (thisStyle.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER) | |
472 | tag = wxT("<ol type=\"i\">"); | |
473 | else | |
474 | { | |
475 | tag = wxT("<ul>"); | |
476 | m_is_ul = true; | |
477 | } | |
478 | ||
479 | if (m_is_ul) | |
480 | return 1; | |
481 | else | |
482 | return 0; | |
483 | } | |
484 | ||
485 | wxString wxRichTextHTMLHandler::GetAlignment( const wxRichTextAttr& thisStyle ) | |
486 | { | |
487 | switch( thisStyle.GetAlignment() ) | |
488 | { | |
489 | case wxTEXT_ALIGNMENT_LEFT: | |
490 | return wxT("left"); | |
491 | case wxTEXT_ALIGNMENT_RIGHT: | |
492 | return wxT("right"); | |
493 | case wxTEXT_ALIGNMENT_CENTER: | |
494 | return wxT("center"); | |
495 | case wxTEXT_ALIGNMENT_JUSTIFIED: | |
496 | return wxT("justify"); | |
497 | default: | |
498 | return wxT("left"); | |
499 | } | |
500 | } | |
501 | ||
502 | void wxRichTextHTMLHandler::WriteImage(wxRichTextImage* image, wxOutputStream& stream) | |
503 | { | |
504 | wxTextOutputStream str(stream); | |
505 | ||
506 | str << wxT("<img src=\""); | |
507 | ||
508 | #if wxUSE_FILESYSTEM | |
509 | if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) | |
510 | { | |
511 | #if 0 | |
512 | if (!image->GetImage().IsOk() && image->GetImageBlock().GetData()) | |
513 | image->LoadFromBlock(); | |
514 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) | |
515 | image->MakeBlock(); | |
516 | #endif | |
517 | ||
518 | if (image->GetImageBlock().IsOk()) | |
519 | { | |
520 | wxImage img; | |
521 | image->GetImageBlock().Load(img); | |
522 | if (img.IsOk()) | |
523 | { | |
524 | wxString ext(image->GetImageBlock().GetExtension()); | |
525 | wxString tempFilename(wxString::Format(wxT("image%d.%s"), sm_fileCounter, ext.c_str())); | |
526 | wxMemoryFSHandler::AddFile(tempFilename, img, image->GetImageBlock().GetImageType()); | |
527 | ||
528 | m_imageLocations.Add(tempFilename); | |
529 | ||
530 | str << wxT("memory:") << tempFilename; | |
531 | } | |
532 | } | |
533 | else | |
534 | str << wxT("memory:?"); | |
535 | ||
536 | sm_fileCounter ++; | |
537 | } | |
538 | else if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
539 | { | |
540 | #if 0 | |
541 | if (!image->GetImage().IsOk() && image->GetImageBlock().GetData()) | |
542 | image->LoadFromBlock(); | |
543 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) | |
544 | image->MakeBlock(); | |
545 | #endif | |
546 | ||
547 | if (image->GetImageBlock().IsOk()) | |
548 | { | |
549 | wxString tempDir(GetTempDir()); | |
550 | if (tempDir.IsEmpty()) | |
551 | tempDir = wxFileName::GetTempDir(); | |
552 | ||
553 | wxString ext(image->GetImageBlock().GetExtension()); | |
554 | wxString tempFilename(wxString::Format(wxT("%s/image%d.%s"), tempDir.c_str(), sm_fileCounter, ext.c_str())); | |
555 | image->GetImageBlock().Write(tempFilename); | |
556 | ||
557 | m_imageLocations.Add(tempFilename); | |
558 | ||
559 | str << wxFileSystem::FileNameToURL(tempFilename); | |
560 | } | |
561 | else | |
562 | str << wxT("file:?"); | |
563 | ||
564 | sm_fileCounter ++; | |
565 | } | |
566 | else // if (GetFlags() & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64) // this is implied | |
567 | #endif | |
568 | { | |
569 | str << wxT("data:"); | |
570 | str << GetMimeType(image->GetImageBlock().GetImageType()); | |
571 | str << wxT(";base64,"); | |
572 | #if 0 | |
573 | if (image->GetImage().IsOk() && !image->GetImageBlock().GetData()) | |
574 | image->MakeBlock(); | |
575 | #endif | |
576 | if (image->GetImageBlock().IsOk()) | |
577 | { | |
578 | wxChar* data = b64enc( image->GetImageBlock().GetData(), image->GetImageBlock().GetDataSize() ); | |
579 | str << data; | |
580 | ||
581 | delete[] data; | |
582 | } | |
583 | } | |
584 | ||
585 | str << wxT("\" />"); | |
586 | } | |
587 | ||
588 | long wxRichTextHTMLHandler::PtToSize(long size) | |
589 | { | |
590 | int i; | |
591 | int len = m_fontSizeMapping.GetCount(); | |
592 | for (i = 0; i < len; i++) | |
593 | if (size <= m_fontSizeMapping[i]) | |
594 | return i+1; | |
595 | return 7; | |
596 | } | |
597 | ||
598 | wxString wxRichTextHTMLHandler::SymbolicIndent(long indent) | |
599 | { | |
600 | wxString in; | |
601 | for(;indent > 0; indent -= 20) | |
602 | in.Append( wxT(" ") ); | |
603 | return in; | |
604 | } | |
605 | ||
606 | const wxChar* wxRichTextHTMLHandler::GetMimeType(int imageType) | |
607 | { | |
608 | switch(imageType) | |
609 | { | |
610 | case wxBITMAP_TYPE_BMP: | |
611 | return wxT("image/bmp"); | |
612 | case wxBITMAP_TYPE_TIFF: | |
613 | return wxT("image/tiff"); | |
614 | case wxBITMAP_TYPE_GIF: | |
615 | return wxT("image/gif"); | |
616 | case wxBITMAP_TYPE_PNG: | |
617 | return wxT("image/png"); | |
618 | case wxBITMAP_TYPE_JPEG: | |
619 | return wxT("image/jpeg"); | |
620 | default: | |
621 | return wxT("image/unknown"); | |
622 | } | |
623 | } | |
624 | ||
625 | // exim-style base64 encoder | |
626 | wxChar* wxRichTextHTMLHandler::b64enc( unsigned char* input, size_t in_len ) | |
627 | { | |
628 | // elements of enc64 array must be 8 bit values | |
629 | // otherwise encoder will fail | |
630 | // hmmm.. Does wxT macro define a char as 16 bit value | |
631 | // when compiling with UNICODE option? | |
632 | static const wxChar enc64[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); | |
633 | wxChar* output = new wxChar[4*((in_len+2)/3)+1]; | |
634 | wxChar* p = output; | |
635 | ||
636 | while( in_len-- > 0 ) | |
637 | { | |
638 | register wxChar a, b; | |
639 | ||
640 | a = *input++; | |
641 | ||
642 | *p++ = enc64[ (a >> 2) & 0x3f ]; | |
643 | ||
644 | if( in_len-- == 0 ) | |
645 | { | |
646 | *p++ = enc64[ (a << 4 ) & 0x30 ]; | |
647 | *p++ = '='; | |
648 | *p++ = '='; | |
649 | break; | |
650 | } | |
651 | ||
652 | b = *input++; | |
653 | ||
654 | *p++ = enc64[(( a << 4 ) | ((b >> 4) &0xf )) & 0x3f]; | |
655 | ||
656 | if( in_len-- == 0 ) | |
657 | { | |
658 | *p++ = enc64[ (b << 2) & 0x3f ]; | |
659 | *p++ = '='; | |
660 | break; | |
661 | } | |
662 | ||
663 | a = *input++; | |
664 | ||
665 | *p++ = enc64[ ((( b << 2 ) & 0x3f ) | ((a >> 6)& 0x3)) & 0x3f ]; | |
666 | ||
667 | *p++ = enc64[ a & 0x3f ]; | |
668 | } | |
669 | *p = 0; | |
670 | ||
671 | return output; | |
672 | } | |
673 | #endif | |
674 | // wxUSE_STREAMS | |
675 | ||
676 | /// Delete the in-memory or temporary files generated by the last operation | |
677 | bool wxRichTextHTMLHandler::DeleteTemporaryImages() | |
678 | { | |
679 | return DeleteTemporaryImages(GetFlags(), m_imageLocations); | |
680 | } | |
681 | ||
682 | /// Delete the in-memory or temporary files generated by the last operation | |
683 | bool wxRichTextHTMLHandler::DeleteTemporaryImages(int flags, const wxArrayString& imageLocations) | |
684 | { | |
685 | size_t i; | |
686 | for (i = 0; i < imageLocations.GetCount(); i++) | |
687 | { | |
688 | wxString location = imageLocations[i]; | |
689 | ||
690 | if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) | |
691 | { | |
692 | #if wxUSE_FILESYSTEM | |
693 | wxMemoryFSHandler::RemoveFile(location); | |
694 | #endif | |
695 | } | |
696 | else if (flags & wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES) | |
697 | { | |
698 | if (wxFileExists(location)) | |
699 | wxRemoveFile(location); | |
700 | } | |
701 | } | |
702 | ||
703 | return true; | |
704 | } | |
705 | ||
706 | ||
707 | #endif | |
708 | // wxUSE_RICHTEXT | |
709 |