From 673593f91179ee4189f856d82e2e5db1c7ee1a98 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 16 Dec 2021 21:47:26 +0100 Subject: [PATCH 1/2] Document that wxString passed to AddFile() must use Latin-1 Otherwise To8BitData() would return an empty buffer for it. --- interface/wx/fs_mem.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/interface/wx/fs_mem.h b/interface/wx/fs_mem.h index 546ced5b2b..6b3af9b911 100644 --- a/interface/wx/fs_mem.h +++ b/interface/wx/fs_mem.h @@ -83,6 +83,10 @@ public: Stored data (bitmap, text or raw data) will be copied into private memory stream and available under name @c "memory:" + @e filename. + When using the overload taking @c wxString data, the string must + contain only Latin-1 characters (which includes strings created using + wxString::From8BitData()). + @note you must use a @a type value (aka image format) that wxWidgets can save (e.g. JPG, PNG, see wxImage documentation)! From f84c3a79681df4c0a0ebdeb624f4487683225180 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 16 Dec 2021 21:54:43 +0100 Subject: [PATCH 2/2] Fall back to using UTF-8 in wxMemoryFSHandler::AddFile() This seems to be better than just losing the data completely if converting it to Latin-1 fails. --- interface/wx/fs_mem.h | 8 +++++--- src/common/fs_mem.cpp | 7 ++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/interface/wx/fs_mem.h b/interface/wx/fs_mem.h index 6b3af9b911..37f13021ab 100644 --- a/interface/wx/fs_mem.h +++ b/interface/wx/fs_mem.h @@ -83,9 +83,11 @@ public: Stored data (bitmap, text or raw data) will be copied into private memory stream and available under name @c "memory:" + @e filename. - When using the overload taking @c wxString data, the string must - contain only Latin-1 characters (which includes strings created using - wxString::From8BitData()). + When using the overload taking @c wxString data, if the string contains + only Latin-1 characters (which includes strings created using + wxString::From8BitData()), its data is used as is. Otherwise, the UTF-8 + representation of the string is stored as the data associated with the + given @a filename. @note you must use a @a type value (aka image format) that wxWidgets can save (e.g. JPG, PNG, see wxImage documentation)! diff --git a/src/common/fs_mem.cpp b/src/common/fs_mem.cpp index 6153a76a96..b177a824e8 100644 --- a/src/common/fs_mem.cpp +++ b/src/common/fs_mem.cpp @@ -180,7 +180,12 @@ void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename, const wxString& textdata, const wxString& mimetype) { - const wxCharBuffer buf(textdata.To8BitData()); + // We try to use the provided data "as is" if possible, but if not, we fall + // back to UTF-8 because it's better to do this than just lose the data + // completely. + wxCharBuffer buf(textdata.To8BitData()); + if ( !buf.length() ) + buf = textdata.utf8_str(); AddFileWithMimeType(filename, buf.data(), buf.length(), mimetype); }