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
33class OUTPUTFORMATTER;
34
35namespace KIFONT
36{
37 class OUTLINE_FONT;
38}
39
41{
42public:
44 {
45 enum class FILE_TYPE
46 {
47 FONT,
48 MODEL,
51 OTHER
52 };
53
56 is_valid( false )
57 {}
58
59 bool Validate()
60 {
62 hash.add( decompressedData );
63
64 is_valid = ( hash.digest().ToString() == data_hash );
65 return is_valid;
66 }
67
68 // This is the old way of validating the file. It is deprecated and retained only
69 // to validate files that were previously embedded.
71 {
72 std::string new_sha;
73 picosha2::hash256_hex_string( decompressedData, new_sha );
74
75 is_valid = ( new_sha == data_hash );
76 return is_valid;
77 }
78
79 wxString GetLink() const
80 {
81 return wxString::Format( "%s://%s", FILEEXT::KiCadUriPrefix, name );
82 }
83
84 wxString name;
88 std::vector<char> decompressedData;
89 std::string data_hash;
90 };
91
92 enum class RETURN_CODE : int
93 {
94 OK,
100 };
101
102 EMBEDDED_FILES() = default;
103
104 EMBEDDED_FILES( EMBEDDED_FILES&& other ) noexcept;
105 EMBEDDED_FILES( const EMBEDDED_FILES& other );
106
108 {
109 for( auto& file : m_files )
110 delete file.second;
111 }
112
113 using FileAddedCallback = std::function<void(EMBEDDED_FILE*)>;
114
116 {
117 m_fileAddedCallback = callback;
118 }
119
121 {
122 return m_fileAddedCallback;
123 }
124
131 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
132
136 void AddFile( EMBEDDED_FILE* aFile );
137
143 void RemoveFile( const wxString& name, bool aErase = true );
144
153 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const;
154
161 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
162 {
163 return aFile.GetLink();
164 }
165
166 bool HasFile( const wxString& name ) const
167 {
168 wxFileName fileName( name );
169
170 return m_files.find( fileName.GetFullName() ) != m_files.end();
171 }
172
173 bool IsEmpty() const
174 {
175 return m_files.empty();
176 }
177
185 const std::vector<wxString>* UpdateFontFiles();
186
191 const std::vector<wxString>* GetFontFiles() const;
192
196 void ClearEmbeddedFonts();
197
205 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
206
213 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
214
218 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
219 {
220 auto it = m_files.find( aName );
221
222 return it == m_files.end() ? nullptr : it->second;
223 }
224
225 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
226 {
227 return m_files;
228 }
229
230 wxFileName GetTemporaryFileName( const wxString& aName ) const;
231
232 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
233
234 void ClearEmbeddedFiles( bool aDeleteFiles = true )
235 {
236 for( auto& file : m_files )
237 {
238 if( aDeleteFiles )
239 delete file.second;
240 }
241
242 m_files.clear();
243 }
244
245 virtual void EmbedFonts() {};
246
247 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
248 {
249 return std::set<KIFONT::OUTLINE_FONT*>();
250 };
251
252 void SetAreFontsEmbedded( bool aEmbedFonts )
253 {
254 m_embedFonts = aEmbedFonts;
255 }
256
258 {
259 return m_embedFonts;
260 }
261
262 static uint32_t Seed()
263 {
264 return 0xABBA2345;
265 }
266
267 EMBEDDED_FILES& operator=(EMBEDDED_FILES&& other) noexcept;
268 EMBEDDED_FILES& operator=( const EMBEDDED_FILES& other );
269
270private:
271 std::map<wxString, EMBEDDED_FILE*> m_files;
272 std::vector<wxString> m_fontFiles;
274
275protected:
276 bool m_embedFonts = false;
279};
const char * name
Definition: DXF_plotter.cpp:59
std::vector< wxString > m_fontFiles
virtual void EmbedFonts()
bool IsEmpty() const
void RemoveFile(const wxString &name, bool aErase=true)
Remove a file from the collection and frees the memory.
FileAddedCallback m_fileAddedCallback
std::function< void(EMBEDDED_FILE *)> FileAddedCallback
@ 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.
wxFileName GetTemporaryFileName(const wxString &aName) const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, bool aWriteData) const
Output formatter for the embedded files.
const std::vector< wxString > * UpdateFontFiles()
Helper function to get a list of fonts for fontconfig to add to the library.
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
wxString GetEmbeddedFileLink(const EMBEDDED_FILE &aFile) const
Return the link for an embedded file.
bool HasFile(const wxString &name) const
void ClearEmbeddedFiles(bool aDeleteFiles=true)
void ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
EMBEDDED_FILES & operator=(EMBEDDED_FILES &&other) noexcept
virtual std::set< KIFONT::OUTLINE_FONT * > GetFonts() const
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
static uint32_t Seed()
const std::vector< wxString > * GetFontFiles() const
If we just need the cached version of the font files, we can use this function which is const and wil...
EMBEDDED_FILES()=default
FileAddedCallback GetFileAddedCallback() const
wxFileName GetTemporaryFileName(EMBEDDED_FILE *aFile) const
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
void SetAreFontsEmbedded(bool aEmbedFonts)
void SetFileAddedCallback(FileAddedCallback callback)
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Take data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncodedD...
std::map< wxString, EMBEDDED_FILE * > m_files
bool m_embedFonts
If set, fonts will be embedded in the element on save.
bool GetAreFontsEmbedded() const
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:322
static const std::string KiCadUriPrefix
std::vector< char > decompressedData
std::string ToString() const
Definition: hash_128.h:47
Definition of file extensions used in Kicad.