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
32class OUTPUTFORMATTER;
33
34namespace KIFONT
35{
36 class OUTLINE_FONT;
37}
38
40{
41public:
43 {
44 enum class FILE_TYPE
45 {
46 FONT,
47 MODEL,
50 OTHER
51 };
52
55 is_valid( false )
56 {}
57
58 bool Validate()
59 {
61 hash.add( decompressedData );
62
63 is_valid = ( hash.digest().ToString() == data_hash );
64 return is_valid;
65 }
66
67 // This is the old way of validating the file. It is deprecated and retained only
68 // to validate files that were previously embedded.
70 {
71 std::string new_sha;
72 picosha2::hash256_hex_string( decompressedData, new_sha );
73
74 is_valid = ( new_sha == data_hash );
75 return is_valid;
76 }
77
78 wxString GetLink() const
79 {
80 return wxString::Format( "%s://%s", FILEEXT::KiCadUriPrefix, name );
81 }
82
83 wxString name;
87 std::vector<char> decompressedData;
88 std::string data_hash;
89 };
90
91 enum class RETURN_CODE : int
92 {
93 OK,
99 };
100
101 EMBEDDED_FILES() = default;
102
104 {
105 for( auto& file : m_files )
106 delete file.second;
107 }
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
169 const std::vector<wxString>* UpdateFontFiles();
170
175 const std::vector<wxString>* GetFontFiles() const;
176
180 void ClearEmbeddedFonts();
181
189 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
190
197 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
198
202 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
203 {
204 auto it = m_files.find( aName );
205
206 return it == m_files.end() ? nullptr : it->second;
207 }
208
209 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
210 {
211 return m_files;
212 }
213
214 wxFileName GetTemporaryFileName( const wxString& aName ) const;
215
216 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
217
218 void ClearEmbeddedFiles( bool aDeleteFiles = true )
219 {
220 for( auto& file : m_files )
221 {
222 if( aDeleteFiles )
223 delete file.second;
224 }
225
226 m_files.clear();
227 }
228
229 virtual void EmbedFonts() {};
230
231 virtual std::set<KIFONT::OUTLINE_FONT*> GetFonts() const
232 {
233 return std::set<KIFONT::OUTLINE_FONT*>();
234 };
235
236 void SetAreFontsEmbedded( bool aEmbedFonts )
237 {
238 m_embedFonts = aEmbedFonts;
239 }
240
242 {
243 return m_embedFonts;
244 }
245
246 static uint32_t Seed()
247 {
248 return 0xABBA2345;
249 }
250
251private:
252 std::map<wxString, EMBEDDED_FILE*> m_files;
253 std::vector<wxString> m_fontFiles;
254
255protected:
256 bool m_embedFonts = false;
259};
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.
@ 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.
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
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)
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.