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