Graphics import: support loading from memory buffer.

This commit is contained in:
Alex Shvartzkop 2023-08-18 20:38:23 +03:00
parent 26a52ecfe9
commit 4e43d4e0db
5 changed files with 76 additions and 8 deletions

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -137,6 +137,26 @@ bool DXF_IMPORT_PLUGIN::Load( const wxString& aFileName )
} }
bool DXF_IMPORT_PLUGIN::LoadFromMemory( const wxMemoryBuffer& aMemBuffer )
{
try
{
return ImportDxfFile( aMemBuffer );
}
catch( const std::bad_alloc& )
{
m_layers.clear();
m_blocks.clear();
m_styles.clear();
m_internalImporter.ClearShapes();
reportMsg( _( "Memory was exhausted trying to load the DXF, it may be too large." ) );
return false;
}
}
bool DXF_IMPORT_PLUGIN::Import() bool DXF_IMPORT_PLUGIN::Import()
{ {
wxCHECK( m_importer, false ); wxCHECK( m_importer, false );
@ -203,6 +223,20 @@ bool DXF_IMPORT_PLUGIN::ImportDxfFile( const wxString& aFile )
} }
bool DXF_IMPORT_PLUGIN::ImportDxfFile( const wxMemoryBuffer& aMemBuffer )
{
DL_Dxf dxf_reader;
std::string str( reinterpret_cast<char*>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
// Note the dxf reader takes care of switching to "C" locale before reading the file
// and will close the file after reading
bool success = dxf_reader.in( str, this );
return success;
}
void DXF_IMPORT_PLUGIN::reportMsg( const wxString& aMessage ) void DXF_IMPORT_PLUGIN::reportMsg( const wxString& aMessage )
{ {
// Add message to keep trace of not handled dxf entities // Add message to keep trace of not handled dxf entities

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -231,6 +231,7 @@ public:
} }
bool Load( const wxString& aFileName ) override; bool Load( const wxString& aFileName ) override;
bool LoadFromMemory( const wxMemoryBuffer& aMemBuffer ) override;
bool Import() override; bool Import() override;
double GetImageWidth() const override; double GetImageWidth() const override;
@ -307,6 +308,13 @@ public:
*/ */
bool ImportDxfFile( const wxString& aFile ); bool ImportDxfFile( const wxString& aFile );
/**
* Implementation of the method used for communicate with this filter.
*
* @param aMemBuffer is the memory bufferr.
*/
bool ImportDxfFile( const wxMemoryBuffer& aMemBuffer );
/** /**
* @return the list of messages in one string. Each message ends by '\n' * @return the list of messages in one string. Each message ends by '\n'
*/ */

View File

@ -2,7 +2,7 @@
* This program source code file is part of KICAD, a free EDA CAD application. * This program source code file is part of KICAD, a free EDA CAD application.
* *
* Copyright (C) 2016 CERN * Copyright (C) 2016 CERN
* Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
* *
* @author Maciej Suminski <maciej.suminski@cern.ch> * @author Maciej Suminski <maciej.suminski@cern.ch>
* *
@ -91,6 +91,13 @@ public:
*/ */
virtual bool Load( const wxString& aFileName ) = 0; virtual bool Load( const wxString& aFileName ) = 0;
/**
* Set memory buffer with content for import.
*
* It is necessary to have the GRAPHICS_IMPORTER object set before.
*/
virtual bool LoadFromMemory( const wxMemoryBuffer& aMemBuffer ) = 0;
/** /**
* Return image height from original imported file. * Return image height from original imported file.
* *

View File

@ -2,7 +2,7 @@
* This program source code file is part of KICAD, a free EDA CAD application. * This program source code file is part of KICAD, a free EDA CAD application.
* *
* Copyright (C) 2016 CERN * Copyright (C) 2016 CERN
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
* @author Janito V. Ferreira Filho * @author Janito V. Ferreira Filho
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -25,6 +25,7 @@
#include "svg_import_plugin.h" #include "svg_import_plugin.h"
#include <nanosvg.h>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
@ -33,6 +34,8 @@
#include <eda_item.h> #include <eda_item.h>
#include "graphics_importer.h" #include "graphics_importer.h"
static const int SVG_DPI = 96;
static VECTOR2D calculateBezierBoundingBoxExtremity( const float* aCurvePoints, static VECTOR2D calculateBezierBoundingBoxExtremity( const float* aCurvePoints,
std::function< const float&( const float&, const float& ) > comparator ); std::function< const float&( const float&, const float& ) > comparator );
static float calculateBezierSegmentationThreshold( const float* aCurvePoints ); static float calculateBezierSegmentationThreshold( const float* aCurvePoints );
@ -64,13 +67,30 @@ bool SVG_IMPORT_PLUGIN::Load( const wxString& aFileName )
return false; return false;
// nsvgParseFromFile will close the file after reading // nsvgParseFromFile will close the file after reading
m_parsedImage = nsvgParseFromFile( fp, "mm", 96 ); m_parsedImage = nsvgParseFromFile( fp, "mm", SVG_DPI );
wxCHECK( m_parsedImage, false ); wxCHECK( m_parsedImage, false );
return true; return true;
} }
bool SVG_IMPORT_PLUGIN::LoadFromMemory( const wxMemoryBuffer& aMemBuffer )
{
wxCHECK( m_importer, false );
std::string str( reinterpret_cast<char*>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
wxCHECK( str.data()[aMemBuffer.GetDataLen()] == '\0', false );
// nsvgParse will modify the string data
m_parsedImage = nsvgParse( str.data(), "mm", SVG_DPI );
wxCHECK( m_parsedImage, false );
return true;
}
bool SVG_IMPORT_PLUGIN::Import() bool SVG_IMPORT_PLUGIN::Import()
{ {
auto alpha = auto alpha =

View File

@ -3,7 +3,7 @@
* *
* Copyright (C) 2016 CERN * Copyright (C) 2016 CERN
* @author Janito V. Ferreira Filho <janito.vff@gmail.com> * @author Janito V. Ferreira Filho <janito.vff@gmail.com>
* Copyright (C) 2018-2019 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -26,8 +26,6 @@
#ifndef SVG_IMPORT_PLUGIN_H #ifndef SVG_IMPORT_PLUGIN_H
#define SVG_IMPORT_PLUGIN_H #define SVG_IMPORT_PLUGIN_H
#include "nanosvg.h"
#include "graphics_import_plugin.h" #include "graphics_import_plugin.h"
#include "graphics_importer_buffer.h" #include "graphics_importer_buffer.h"
#include <math/vector2d.h> #include <math/vector2d.h>
@ -66,6 +64,7 @@ public:
bool Import() override; bool Import() override;
bool Load( const wxString& aFileName ) override; bool Load( const wxString& aFileName ) override;
bool LoadFromMemory( const wxMemoryBuffer& aMemBuffer ) override;
virtual double GetImageHeight() const override; virtual double GetImageHeight() const override;
virtual double GetImageWidth() const override; virtual double GetImageWidth() const override;