Fix incorrect handling of unicode chars (non ASCII7) in filenames in .gbrjob files.

This commit is contained in:
jean-pierre charras 2019-07-14 11:19:03 +02:00
parent f11d950448
commit 269f88ee28
3 changed files with 44 additions and 7 deletions

View File

@ -92,6 +92,11 @@ private:
REPORTER* m_reporter; REPORTER* m_reporter;
wxFileName m_filename; wxFileName m_filename;
wxArrayString m_GerberFiles; // List of gerber files in job wxArrayString m_GerberFiles; // List of gerber files in job
// Convert a JSON string, that uses escaped sequence of 4 hexdecimal digits
// to encode unicode chars when not ASCII7 codes
// json11 converts this sequence to UTF8 string
wxString formatStringFromJSON( const std::string& name );
}; };
@ -136,10 +141,8 @@ bool GERBER_JOBFILE_READER::ReadGerberJobFile()
for( auto& entry : json_parser["FilesAttributes"].array_items() ) for( auto& entry : json_parser["FilesAttributes"].array_items() )
{ {
//wxLogMessage( entry.dump().c_str() );
std::string name = entry["Path"].string_value(); std::string name = entry["Path"].string_value();
//wxLogMessage( name.c_str() ); m_GerberFiles.Add( formatStringFromJSON( name ) );
m_GerberFiles.Add( FormatStringFromGerber( name ) );
} }
} }
else else
@ -155,6 +158,17 @@ bool GERBER_JOBFILE_READER::ReadGerberJobFile()
} }
wxString GERBER_JOBFILE_READER::formatStringFromJSON( const std::string& name )
{
// Convert a JSON string, that uses a escaped sequence of 4 hexdecimal digits
// to encode unicode chars
// Our json11 library returns in this case a UTF8 sequence. Just convert it to
// a wxString.
wxString wstr = FROM_UTF8( name.c_str() );
return wstr;
}
bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName ) bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
{ {
wxFileName filename = aFullFileName; wxFileName filename = aFullFileName;

View File

@ -55,6 +55,26 @@ GERBER_JOBFILE_WRITER::GERBER_JOBFILE_WRITER( BOARD* aPcb, REPORTER* aReporter )
m_indent = 0; m_indent = 0;
} }
std::string GERBER_JOBFILE_WRITER::formatStringFromUTF32( const wxString& aText )
{
std::string fmt_text; // the text after UTF32 to UTF8 conversion
for( unsigned long letter: aText )
{
if( letter >= ' ' && letter <= 0x7F )
fmt_text += char( letter );
else
{
char buff[10];
sprintf( buff, "\\u%4.4lX", letter );
fmt_text += buff;
}
}
return fmt_text;
}
enum ONSIDE GERBER_JOBFILE_WRITER::hasSilkLayers() enum ONSIDE GERBER_JOBFILE_WRITER::hasSilkLayers()
{ {
int flag = SIDE_NONE; int flag = SIDE_NONE;
@ -396,10 +416,8 @@ void GERBER_JOBFILE_WRITER::addJSONFilesAttributes()
if( !skip_file ) if( !skip_file )
{ {
// name can contain non ASCII7 chars. // Ensure the name is JSON compatible.
// Only ASCII7 chars are accepted in gerber files. others must be converted to std::string strname = formatStringFromUTF32( name );
// a gerber hexa sequence.
std::string strname = formatStringToGerber( name );
openBlock(); openBlock();
addJSONObject( wxString::Format( "\"Path\": \"%s\",\n", strname.c_str() ) ); addJSONObject( wxString::Format( "\"Path\": \"%s\",\n", strname.c_str() ) );

View File

@ -202,6 +202,11 @@ private:
addIndent(); m_JSONbuffer << aParam; addIndent(); m_JSONbuffer << aParam;
} }
/** A helper function to convert a wxString ( therefore a Unicode text ) to
* a JSON compatible string (a escaped unicode sequence of 4 hexa).
*/
std::string formatStringFromUTF32( const wxString& aText );
private: private:
BOARD* m_pcb; // The board BOARD* m_pcb; // The board
REPORTER* m_reporter; // a reporter for messages (can be null) REPORTER* m_reporter; // a reporter for messages (can be null)