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 (C) 2024 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
24#include <wx/string.h>
25#include <wx/filename.h>
26
27#include <mmh3_hash.h>
28#include <picosha2.h>
30
31class OUTPUTFORMATTER;
32
34{
35public:
37 {
38 enum class FILE_TYPE
39 {
40 FONT,
41 MODEL,
44 OTHER
45 };
46
49 is_valid( false )
50 {}
51
52 bool Validate()
53 {
55 hash.add( decompressedData );
56
57 is_valid = ( hash.digest().ToString() == data_hash );
58 return is_valid;
59 }
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.
64 {
65 std::string new_sha;
66 picosha2::hash256_hex_string( decompressedData, new_sha );
67
68 is_valid = ( new_sha == data_hash );
69 return is_valid;
70 }
71
72 wxString GetLink() const
73 {
74 return wxString::Format( "%s://%s", FILEEXT::KiCadUriPrefix, name );
75 }
76
77 wxString name;
81 std::vector<char> decompressedData;
82 std::string data_hash;
83 };
84
85 enum class RETURN_CODE : int
86 {
87 OK, // Success
88 FILE_NOT_FOUND, // File not found on disk
89 PERMISSIONS_ERROR, // Could not read/write file
90 FILE_ALREADY_EXISTS, // File already exists in the collection
91 OUT_OF_MEMORY, // Could not allocate memory
92 CHECKSUM_ERROR, // Checksum in file does not match data
93 };
94
95 EMBEDDED_FILES() = default;
96
98 {
99 for( auto& file : m_files )
100 delete file.second;
101 }
102
108 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
109
113 void AddFile( EMBEDDED_FILE* aFile );
114
119 void RemoveFile( const wxString& name, bool aErase = true );
120
128 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, int aNestLevel, bool aWriteData ) const;
129
135 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
136 {
137 return aFile.GetLink();
138 }
139
140 bool HasFile( const wxString& name ) const
141 {
142 wxFileName fileName( name );
143
144 return m_files.find( fileName.GetFullName() ) != m_files.end();
145 }
146
147 bool IsEmpty() const
148 {
149 return m_files.empty();
150 }
151
159 const std::vector<wxString>* UpdateFontFiles();
160
165 const std::vector<wxString>* GetFontFiles() const;
166
170 void ClearEmbeddedFonts();
171
178 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
179
186 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
187
191 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
192 {
193 auto it = m_files.find( aName );
194
195 return it == m_files.end() ? nullptr : it->second;
196 }
197
198 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
199 {
200 return m_files;
201 }
202
203 wxFileName GetTemporaryFileName( const wxString& aName ) const;
204
205 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
206
207 void ClearEmbeddedFiles( bool aDeleteFiles = true )
208 {
209 for( auto& file : m_files )
210 {
211 if( aDeleteFiles )
212 delete file.second;
213 }
214
215 m_files.clear();
216 }
217
218 virtual void EmbedFonts() {};
219
220 void SetAreFontsEmbedded( bool aEmbedFonts )
221 {
222 m_embedFonts = aEmbedFonts;
223 }
224
226 {
227 return m_embedFonts;
228 }
229
230 static uint32_t Seed()
231 {
232 return 0xABBA2345;
233 }
234
235private:
236 std::map<wxString, EMBEDDED_FILE*> m_files;
237 std::vector<wxString> m_fontFiles;
238
239protected:
240 bool m_embedFonts = false; // If set, fonts will be embedded in the element on save
241 // Otherwise, font files embedded in the element will be
242 // removed on save
243};
const char * name
Definition: DXF_plotter.cpp:57
std::vector< wxString > m_fontFiles
virtual void EmbedFonts()
bool IsEmpty() const
void RemoveFile(const wxString &name, bool aErase=true)
Removes a file from the collection and frees the memory.
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
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
Returns the link for an embedded file.
bool HasFile(const wxString &name) const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, int aNestLevel, bool aWriteData) const
Output formatter for the embedded files.
void ClearEmbeddedFiles(bool aDeleteFiles=true)
void ClearEmbeddedFonts()
Removes all embedded fonts from the collection.
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Loads 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
wxFileName GetTemporaryFileName(EMBEDDED_FILE *aFile) const
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
void SetAreFontsEmbedded(bool aEmbedFonts)
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Takes data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncoded...
std::map< wxString, EMBEDDED_FILE * > m_files
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.