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#ifndef EMBEDDED_FILES_H
21#define EMBEDDED_FILES_H
22
23#include <map>
24
25#include <wx/string.h>
26#include <wx/filename.h>
27
28#include <embedded_files_lexer.h>
30#include <richio.h>
31#include <picosha2.h>
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 {
54 std::string new_sha;
55 picosha2::hash256_hex_string( decompressedData, new_sha );
56
57 is_valid = ( new_sha == data_sha );
58 return is_valid;
59 }
60
61 wxString GetLink() const
62 {
63 return wxString::Format( "%s://%s", FILEEXT::KiCadUriPrefix, name );
64 }
65
66 wxString name;
70 std::vector<char> decompressedData;
71 std::string data_sha;
72 };
73
74 enum class RETURN_CODE : int
75 {
76 OK, // Success
77 FILE_NOT_FOUND, // File not found on disk
78 PERMISSIONS_ERROR, // Could not read/write file
79 FILE_ALREADY_EXISTS, // File already exists in the collection
80 OUT_OF_MEMORY, // Could not allocate memory
81 CHECKSUM_ERROR, // Checksum in file does not match data
82 };
83
84 EMBEDDED_FILES() = default;
85
87 {
88 for( auto& file : m_files )
89 delete file.second;
90 }
91
97 EMBEDDED_FILE* AddFile( const wxFileName& aName, bool aOverwrite );
98
102 void AddFile( EMBEDDED_FILE* aFile );
103
108 void RemoveFile( const wxString& name, bool aErase = true );
109
117 void WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, int aNestLevel, bool aWriteData ) const;
118
124 wxString GetEmbeddedFileLink( const EMBEDDED_FILE& aFile ) const
125 {
126 return aFile.GetLink();
127 }
128
129 bool HasFile( const wxString& name ) const
130 {
131 wxFileName fileName( name );
132
133 return m_files.find( fileName.GetFullName() ) != m_files.end();
134 }
135
136 bool IsEmpty() const
137 {
138 return m_files.empty();
139 }
140
148 const std::vector<wxString>* UpdateFontFiles();
149
154 const std::vector<wxString>* GetFontFiles() const;
155
159 void ClearEmbeddedFonts();
160
167 static RETURN_CODE CompressAndEncode( EMBEDDED_FILE& aFile );
168
175 static RETURN_CODE DecompressAndDecode( EMBEDDED_FILE& aFile );
176
180 EMBEDDED_FILE* GetEmbeddedFile( const wxString& aName ) const
181 {
182 auto it = m_files.find( aName );
183
184 return it == m_files.end() ? nullptr : it->second;
185 }
186
187 const std::map<wxString, EMBEDDED_FILE*>& EmbeddedFileMap() const
188 {
189 return m_files;
190 }
191
192 wxFileName GetTemporaryFileName( const wxString& aName ) const;
193
194 wxFileName GetTemporaryFileName( EMBEDDED_FILE* aFile ) const;
195
196 void ClearEmbeddedFiles( bool aDeleteFiles = true )
197 {
198 for( auto& file : m_files )
199 {
200 if( aDeleteFiles )
201 delete file.second;
202 }
203
204 m_files.clear();
205 }
206
207 virtual void EmbedFonts() {};
208
209 void SetAreFontsEmbedded( bool aEmbedFonts )
210 {
211 m_embedFonts = aEmbedFonts;
212 }
213
215 {
216 return m_embedFonts;
217 }
218
219private:
220 std::map<wxString, EMBEDDED_FILE*> m_files;
221 std::vector<wxString> m_fontFiles;
222
223protected:
224 bool m_embedFonts = false; // If set, fonts will be embedded in the element on save
225 // Otherwise, font files embedded in the element will be
226 // removed on save
227};
228
229
230
231class EMBEDDED_FILES_PARSER : public EMBEDDED_FILES_LEXER
232{
233public:
235 EMBEDDED_FILES_LEXER( aReader )
236 {
237 }
238
239 void ParseEmbedded( EMBEDDED_FILES* aFiles );
240
241};
242#endif // EMBEDDED_FILES_H
const char * name
Definition: DXF_plotter.cpp:57
void ParseEmbedded(EMBEDDED_FILES *aFiles)
EMBEDDED_FILES_PARSER(LINE_READER *aReader)
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.
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
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
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
Definition of file extensions used in Kicad.