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 WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
142
149 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
150 {
151 return aFile.GetLink();
152 }
153
154 bool HasFile( const wxString& name ) const
155 {
156 wxFileName fileName( name );
157
158 return m_files.find( fileName.GetFullName() ) != m_files.end();
159 }
160
161 bool IsEmpty() const
162 {
163 return m_files.empty();
164 }
165
170 virtual void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction )
171 {
172 }
173
181 const std::vector<wxString>* UpdateFontFiles();
182
187 const std::vector<wxString>* GetFontFiles() const;
188
192 void ClearEmbeddedFonts();
193
201 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
202
209 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
210
218 static RETURN_CODE ComputeFileHash( const wxFileName& aFileName, std::string& aHash );
219
223 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
224 {
225 auto it = m_files.find( aName );
226
227 return it == m_files.end() ? nullptr : it->second.get();
228 }
229
239 const std::map<wxString, std::shared_ptr<EMBEDDED_FILE>>& EmbeddedFileMap() const
240 {
241 return m_files;
242 }
243
244 wxFileName GetTemporaryFileName( const wxString& aName ) const;
245
246 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
247
248 void ClearEmbeddedFiles( bool aDeleteFiles = true )
249 {
250 // aDeleteFiles is retained for API compatibility; with shared_ptr ownership the map
251 // entries are released either way. Other holders of the same shared_ptr keep the
252 // underlying EMBEDDED_FILE alive.
253 (void) aDeleteFiles;
254 m_files.clear();
255 }
256
257 virtual void EmbedFonts() {};
258
259 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
260 {
261 return std::set<KIFONT::OUTLINE_FONT*>();
262 };
263
264 void SetAreFontsEmbedded( bool aEmbedFonts )
265 {
266 m_embedFonts = aEmbedFonts;
267 }
268
270 {
271 return m_embedFonts;
272 }
273
274 static uint32_t Seed()
275 {
276 return 0xABBA2345;
277 }
278
279 EMBEDDED_FILES& operator=( EMBEDDED_FILES&& other ) noexcept;
280 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
281
282private:
283 std::map<wxString, std::shared_ptr<EMBEDDED_FILE>> m_files;
284 std::vector<wxString> m_fontFiles;
286
287protected:
288 bool m_embedFonts = false;
291};
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.