KiCad PCB EDA Suite
Loading...
Searching...
No Matches
embedded_files.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <map>
23#include <set>
24
25#include <wx/string.h>
26#include <wx/filename.h>
27
28#include <mmh3_hash.h>
29#include <picosha2.h>
31#include <functional>
32#include <kicommon.h>
33
34class OUTPUTFORMATTER;
35
36namespace KIFONT
37{
38 class OUTLINE_FONT;
39}
40
42{
43public:
45 {
54
57 is_valid( false )
58 {}
59
60 bool Validate()
61 {
63 hash.add( decompressedData );
64
65 is_valid = ( hash.digest().ToString() == data_hash );
66 return is_valid;
67 }
68
69 // This is the old way of validating the file. It is deprecated and retained only
70 // to validate files that were previously embedded.
72 {
73 std::string new_sha;
74 picosha2::hash256_hex_string( decompressedData, new_sha );
75
76 is_valid = ( new_sha == data_hash );
77 return is_valid;
78 }
79
80 wxString GetLink() const
81 {
82 return wxString::Format( "%s://%s", FILEEXT::KiCadUriPrefix, name );
83 }
84
85 wxString name;
89 std::vector<char> decompressedData;
90 std::string data_hash;
91 };
92
102
103 EMBEDDED_FILES() = default;
104
105 EMBEDDED_FILES( EMBEDDED_FILES&& other ) noexcept;
106 EMBEDDED_FILES( const EMBEDDED_FILES& other );
107 EMBEDDED_FILES( const EMBEDDED_FILES& other, bool aDeepCopy );
108
110 {
111 for( auto& file : m_files )
112 delete file.second;
113 }
114
115 using FILE_ADDED_CALLBACK = std::function<void( EMBEDDED_FILE* )>;
116
118 {
119 m_fileAddedCallback = callback;
120 }
121
126
133 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
134
138 void AddFile( EMBEDDED_FILE* aFile );
139
145 void RemoveFile( const wxString& name, bool aErase = true );
146
155 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
156
163 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
164 {
165 return aFile.GetLink();
166 }
167
168 bool HasFile( const wxString& name ) const
169 {
170 wxFileName fileName( name );
171
172 return m_files.find( fileName.GetFullName() ) != m_files.end();
173 }
174
175 bool IsEmpty() const
176 {
177 return m_files.empty();
178 }
179
184 virtual void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction )
185 {
186 }
187
195 const std::vector<wxString>* UpdateFontFiles();
196
201 const std::vector<wxString>* GetFontFiles() const;
202
206 void ClearEmbeddedFonts();
207
215 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
216
223 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
224
228 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
229 {
230 auto it = m_files.find( aName );
231
232 return it == m_files.end() ? nullptr : it->second;
233 }
234
235 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
236 {
237 return m_files;
238 }
239
240 wxFileName GetTemporaryFileName( const wxString& aName ) const;
241
242 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
243
244 void ClearEmbeddedFiles( bool aDeleteFiles = true )
245 {
246 for( auto& file : m_files )
247 {
248 if( aDeleteFiles )
249 delete file.second;
250 }
251
252 m_files.clear();
253 }
254
255 virtual void EmbedFonts() {};
256
257 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
258 {
259 return std::set<KIFONT::OUTLINE_FONT*>();
260 };
261
262 void SetAreFontsEmbedded( bool aEmbedFonts )
263 {
264 m_embedFonts = aEmbedFonts;
265 }
266
268 {
269 return m_embedFonts;
270 }
271
272 static uint32_t Seed()
273 {
274 return 0xABBA2345;
275 }
276
277 EMBEDDED_FILES& operator=( EMBEDDED_FILES&& other ) noexcept;
278 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
279
280private:
281 std::map<wxString, EMBEDDED_FILE*> m_files;
282 std::vector<wxString> m_fontFiles;
284
285protected:
286 bool m_embedFonts = false;
289};
const char * name
std::vector< wxString > m_fontFiles
virtual ~EMBEDDED_FILES()
virtual void EmbedFonts()
bool IsEmpty() const
void SetFileAddedCallback(FILE_ADDED_CALLBACK callback)
@ PERMISSIONS_ERROR
Could not read/write file.
@ FILE_ALREADY_EXISTS
File already exists in the collection.
@ OUT_OF_MEMORY
Could not allocate memory.
@ FILE_NOT_FOUND
File not found on disk.
@ CHECKSUM_ERROR
Checksum in file does not match data.
EMBEDDED_FILE * GetEmbeddedFile(const wxString &aName) const
Returns the embedded file with the given name or nullptr if it does not exist.
FILE_ADDED_CALLBACK m_fileAddedCallback
wxString GetEmbeddedFileLink(const EMBEDDED_FILE &aFile) const
Return the link for an embedded file.
bool HasFile(const wxString &name) const
virtual void RunOnNestedEmbeddedFiles(const std::function< void(EMBEDDED_FILES *)> &aFunction)
Provide access to nested embedded files, such as symbols in schematics and footprints in boards.
void ClearEmbeddedFiles(bool aDeleteFiles=true)
virtual std::set< KIFONT::OUTLINE_FONT * > GetFonts() const
static uint32_t Seed()
EMBEDDED_FILES()=default
wxFileName GetTemporaryFileName(EMBEDDED_FILE *aFile) const
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
void SetAreFontsEmbedded(bool aEmbedFonts)
std::function< void(EMBEDDED_FILE *)> FILE_ADDED_CALLBACK
FILE_ADDED_CALLBACK GetFileAddedCallback() const
std::map< wxString, EMBEDDED_FILE * > m_files
bool m_embedFonts
If set, fonts will be embedded in the element on save.
bool GetAreFontsEmbedded() const
Class OUTLINE_FONT implements outline font drawing.
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition mmh3_hash.h:60
FORCE_INLINE void add(const std::string &input)
Definition mmh3_hash.h:95
FORCE_INLINE HASH_128 digest()
Definition mmh3_hash.h:114
An interface used to output 8 bit text in a convenient way.
Definition richio.h:295
static const std::string KiCadUriPrefix
#define KICOMMON_API
Definition kicommon.h:28
std::vector< char > decompressedData
Definition of file extensions used in Kicad.