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 <cstdint>
23#include <map>
24#include <set>
25
26#include <wx/string.h>
27#include <wx/filename.h>
28
30#include <functional>
31#include <kicommon.h>
32
33class OUTPUTFORMATTER;
34
35namespace KIFONT
36{
37 class OUTLINE_FONT;
38}
39
41{
42public:
44 {
53
56 is_valid( false )
57 {}
58
59 bool Validate();
60
61 // This is the old way of validating the file. It is deprecated and retained only
62 // to validate files that were previously embedded.
63 bool Validate_SHA256();
64
65 wxString GetLink() const;
66
67 wxString name;
71 std::vector<char> decompressedData;
72 std::string data_hash;
73 };
74
84
85 EMBEDDED_FILES() = default;
86
87 EMBEDDED_FILES( EMBEDDED_FILES&& other ) noexcept;
88 EMBEDDED_FILES( const EMBEDDED_FILES& other );
89 EMBEDDED_FILES( const EMBEDDED_FILES& other, bool aDeepCopy );
90
92 {
93 for( auto& file : m_files )
94 delete file.second;
95 }
96
97 using FILE_ADDED_CALLBACK = std::function<void( EMBEDDED_FILE* )>;
98
100 {
101 m_fileAddedCallback = callback;
102 }
103
108
115 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
116
120 void AddFile( EMBEDDED_FILE* aFile );
121
127 void RemoveFile( const wxString& name, bool aErase = true );
128
137 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
138
145 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
146 {
147 return aFile.GetLink();
148 }
149
150 bool HasFile( const wxString& name ) const
151 {
152 wxFileName fileName( name );
153
154 return m_files.find( fileName.GetFullName() ) != m_files.end();
155 }
156
157 bool IsEmpty() const
158 {
159 return m_files.empty();
160 }
161
166 virtual void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction )
167 {
168 }
169
177 const std::vector<wxString>* UpdateFontFiles();
178
183 const std::vector<wxString>* GetFontFiles() const;
184
188 void ClearEmbeddedFonts();
189
197 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
198
205 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
206
214 static RETURN_CODE ComputeFileHash( const wxFileName& aFileName, std::string& aHash );
215
219 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
220 {
221 auto it = m_files.find( aName );
222
223 return it == m_files.end() ? nullptr : it->second;
224 }
225
226 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
227 {
228 return m_files;
229 }
230
231 wxFileName GetTemporaryFileName( const wxString& aName ) const;
232
233 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
234
235 void ClearEmbeddedFiles( bool aDeleteFiles = true )
236 {
237 for( auto& file : m_files )
238 {
239 if( aDeleteFiles )
240 delete file.second;
241 }
242
243 m_files.clear();
244 }
245
246 virtual void EmbedFonts() {};
247
248 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
249 {
250 return std::set<KIFONT::OUTLINE_FONT*>();
251 };
252
253 void SetAreFontsEmbedded( bool aEmbedFonts )
254 {
255 m_embedFonts = aEmbedFonts;
256 }
257
259 {
260 return m_embedFonts;
261 }
262
263 static uint32_t Seed()
264 {
265 return 0xABBA2345;
266 }
267
268 EMBEDDED_FILES& operator=( EMBEDDED_FILES&& other ) noexcept;
269 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
270
271private:
272 std::map<wxString, EMBEDDED_FILE*> m_files;
273 std::vector<wxString> m_fontFiles;
275
276protected:
277 bool m_embedFonts = false;
280};
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.
An interface used to output 8 bit text in a convenient way.
Definition richio.h:295
#define KICOMMON_API
Definition kicommon.h:28
std::vector< char > decompressedData
Definition of file extensions used in Kicad.