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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <cstdint>
23#include <map>
24#include <memory>
25#include <set>
26
27#include <wx/string.h>
28#include <wx/filename.h>
29
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
62 // This is the old way of validating the file. It is deprecated and retained only
63 // to validate files that were previously embedded.
64 bool Validate_SHA256();
65
66 wxString GetLink() const;
67
68 wxString name;
72 std::vector<char> decompressedData;
73 std::string data_hash;
74 };
75
85
86 EMBEDDED_FILES() = default;
87
88 EMBEDDED_FILES( EMBEDDED_FILES&& other ) noexcept;
89 EMBEDDED_FILES( const EMBEDDED_FILES& other );
90 EMBEDDED_FILES( const EMBEDDED_FILES& other, bool aDeepCopy );
91
92 virtual ~EMBEDDED_FILES() = default;
93
94 using FILE_ADDED_CALLBACK = std::function<void( EMBEDDED_FILE* )>;
95
97 {
98 m_fileAddedCallback = callback;
99 }
100
105
112 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
113
117 void AddFile( EMBEDDED_FILE* aFile );
118
124 void AddFile( std::shared_ptr<EMBEDDED_FILE> aFile );
125
131 void RemoveFile( const wxString& name, bool aErase = true );
132
141 void AssignSharedFrom( const EMBEDDED_FILES& aSource,
142 const std::set<wxString>& aExcludedNames = {} );
143
152 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
153
160 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
161 {
162 return aFile.GetLink();
163 }
164
165 bool HasFile( const wxString& name ) const
166 {
167 wxFileName fileName( name );
168
169 return m_files.find( fileName.GetFullName() ) != m_files.end();
170 }
171
172 bool IsEmpty() const
173 {
174 return m_files.empty();
175 }
176
181 virtual void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction )
182 {
183 }
184
192 const std::vector<wxString>* UpdateFontFiles();
193
198 const std::vector<wxString>* GetFontFiles() const;
199
203 void ClearEmbeddedFonts();
204
212 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
213
220 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
221
229 static RETURN_CODE ComputeFileHash( const wxFileName& aFileName, std::string& aHash );
230
234 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
235 {
236 auto it = m_files.find( aName );
237
238 return it == m_files.end() ? nullptr : it->second.get();
239 }
240
250 const std::map<wxString, std::shared_ptr<EMBEDDED_FILE>>& EmbeddedFileMap() const
251 {
252 return m_files;
253 }
254
255 wxFileName GetTemporaryFileName( const wxString& aName ) const;
256
257 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
258
259 void ClearEmbeddedFiles( bool aDeleteFiles = true )
260 {
261 // aDeleteFiles is retained for API compatibility; with shared_ptr ownership the map
262 // entries are released either way. Other holders of the same shared_ptr keep the
263 // underlying EMBEDDED_FILE alive.
264 (void) aDeleteFiles;
265 m_files.clear();
266 }
267
268 virtual void EmbedFonts() {};
269
270 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
271 {
272 return std::set<KIFONT::OUTLINE_FONT*>();
273 };
274
275 void SetAreFontsEmbedded( bool aEmbedFonts )
276 {
277 m_embedFonts = aEmbedFonts;
278 }
279
281 {
282 return m_embedFonts;
283 }
284
285 static uint32_t Seed()
286 {
287 return 0xABBA2345;
288 }
289
290 EMBEDDED_FILES& operator=( EMBEDDED_FILES&& other ) noexcept;
291 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
292
293private:
294 std::map<wxString, std::shared_ptr<EMBEDDED_FILE>> m_files;
295 std::vector<wxString> m_fontFiles;
297
298protected:
299 bool m_embedFonts = false;
302};
const char * name
std::vector< wxString > m_fontFiles
virtual void EmbedFonts()
bool IsEmpty() const
virtual ~EMBEDDED_FILES()=default
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()
std::map< wxString, std::shared_ptr< EMBEDDED_FILE > > m_files
const std::map< wxString, std::shared_ptr< EMBEDDED_FILE > > & EmbeddedFileMap() const
Provide an iterable view of the file collection.
EMBEDDED_FILES()=default
wxFileName GetTemporaryFileName(EMBEDDED_FILE *aFile) const
void SetAreFontsEmbedded(bool aEmbedFonts)
std::function< void(EMBEDDED_FILE *)> FILE_ADDED_CALLBACK
FILE_ADDED_CALLBACK GetFileAddedCallback() const
bool m_embedFonts
If set, fonts will be embedded in the element on save.
bool GetAreFontsEmbedded() const
Class OUTLINE_FONT implements outline font drawing.
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
#define KICOMMON_API
Definition kicommon.h:27
std::vector< char > decompressedData
Definition of file extensions used in Kicad.