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
109 {
110 for( auto& file : m_files )
111 delete file.second;
112 }
113
114 using FILE_ADDED_CALLBACK = std::function<void( EMBEDDED_FILE* )>;
115
117 {
118 m_fileAddedCallback = callback;
119 }
120
125
132 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
133
137 void AddFile( EMBEDDED_FILE* aFile );
138
144 void RemoveFile( const wxString& name, bool aErase = true );
145
154 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
155
162 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
163 {
164 return aFile.GetLink();
165 }
166
167 bool HasFile( const wxString& name ) const
168 {
169 wxFileName fileName( name );
170
171 return m_files.find( fileName.GetFullName() ) != m_files.end();
172 }
173
174 bool IsEmpty() const
175 {
176 return m_files.empty();
177 }
178
183 virtual void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction )
184 {
185 }
186
194 const std::vector<wxString>* UpdateFontFiles();
195
200 const std::vector<wxString>* GetFontFiles() const;
201
205 void ClearEmbeddedFonts();
206
214 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
215
222 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
223
227 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
228 {
229 auto it = m_files.find( aName );
230
231 return it == m_files.end() ? nullptr : it->second;
232 }
233
234 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
235 {
236 return m_files;
237 }
238
239 wxFileName GetTemporaryFileName( const wxString& aName ) const;
240
241 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
242
243 void ClearEmbeddedFiles( bool aDeleteFiles = true )
244 {
245 for( auto& file : m_files )
246 {
247 if( aDeleteFiles )
248 delete file.second;
249 }
250
251 m_files.clear();
252 }
253
254 virtual void EmbedFonts() {};
255
256 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
257 {
258 return std::set<KIFONT::OUTLINE_FONT*>();
259 };
260
261 void SetAreFontsEmbedded( bool aEmbedFonts )
262 {
263 m_embedFonts = aEmbedFonts;
264 }
265
267 {
268 return m_embedFonts;
269 }
270
271 static uint32_t Seed()
272 {
273 return 0xABBA2345;
274 }
275
276 EMBEDDED_FILES& operator=( EMBEDDED_FILES&& other ) noexcept;
277 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
278
279private:
280 std::map<wxString, EMBEDDED_FILE*> m_files;
281 std::vector<wxString> m_fontFiles;
283
284protected:
285 bool m_embedFonts = false;
288};
const char * name
std::vector< wxString > m_fontFiles
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.