PhoenixInkscape  1.2.1
Batch convert SVG to PNG with Inkscape
Loading...
Searching...
No Matches
main.cpp File Reference
#include "convertToString.h"
#include "PPath.h"
#include "OptionParser.h"
#include "pxml_utils.h"
#include "pinkscape_slide.h"
+ Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  PLayer
 Get the begin and end slide for current layer. More...
 

Typedefs

typedef std::vector< long unsigned int > PSlide
 Slide composed of layer.
 
typedef std::vector< PLayerPVecLayer
 Vector of layer.
 
typedef std::vector< PSlidePVecSlide
 Define the layer in slide.
 

Functions

OptionParser createOptionParser ()
 Create the OptionParser of this program.
 
void createSlideWithLayer (PSlide &slide, const PVecLayer &vecLayerSlide, long unsigned int index)
 Create the slide to be generated.
 
void createVecSlide (PVecSlide &vecSlide, PVecLayer &vecLayerSlide)
 Create the vector of slides.
 
void createVectorSlide (PVecLayer &vecLayerSlide, const PVecXml &vecLayer)
 Create the vector of slide layer.
 
PString getInkscapeLabel (const PXml &layerXml)
 Get the inkscape label of current PXml.
 
PString getSlideNumber (long unsigned int i)
 Convert the slide index into string.
 
int main (int argc, char **argv)
 
int processFiles (const std::vector< PPath > &listInputFile)
 Process all the input files.
 
bool processFileSvg (const PPath &inputFile, bool isInkscapeExist)
 Process all the input files.
 
bool saveSlides (POutoutMode &outputMode, const PPath &baseOutputName, const PVecSlide &vecSlides, const PVecXml &vecLayerXml, const PXml &lighRoot, bool isInkscapeExist)
 Save the slides.
 

Typedef Documentation

◆ PSlide

typedef std::vector<long unsigned int> PSlide

Slide composed of layer.

Definition at line 44 of file main.cpp.

◆ PVecLayer

typedef std::vector<PLayer> PVecLayer

Vector of layer.

Definition at line 41 of file main.cpp.

◆ PVecSlide

typedef std::vector<PSlide > PVecSlide

Define the layer in slide.

Definition at line 47 of file main.cpp.

Function Documentation

◆ createOptionParser()

OptionParser createOptionParser ( )

Create the OptionParser of this program.

Returns
OptionParser of this program

Definition at line 17 of file main.cpp.

17 {
18 OptionParser parser(true, __PROGRAM_VERSION__);
19 parser.setExampleLongOption("phoenix_inkscapesplitter --input=file.svg --output=output/dir/");
20 parser.setExampleShortOption("phoenix_inkscapesplitter -i file1.svg file1.svg fileN.svg -o output/dir/");
21
22 parser.addOption("input", "i", OptionType::FILENAME, true, "list of input files to be treated");
23
24 PString defaultOutputDir(".");
25 parser.addOption("output", "o", defaultOutputDir, "Output directory");
26
27 return parser;
28}

Referenced by main().

+ Here is the caller graph for this function:

◆ createSlideWithLayer()

void createSlideWithLayer ( PSlide & slide,
const PVecLayer & vecLayerSlide,
long unsigned int index )

Create the slide to be generated.

Parameters
[out]slide: slide to be generated
vecLayerSlide: vector of the desired slides for the layers
index: index of the current slide to be generated

Definition at line 111 of file main.cpp.

111 {
112 long unsigned int i(0lu);
113 for(PVecLayer::const_iterator it(vecLayerSlide.begin()); it != vecLayerSlide.end(); ++it){
114 if(index >= it->begin && index <= it->end){
115 slide.push_back(i);
116 }
117 ++i;
118 }
119}

Referenced by createVecSlide().

+ Here is the caller graph for this function:

◆ createVecSlide()

void createVecSlide ( PVecSlide & vecSlide,
PVecLayer & vecLayerSlide )

Create the vector of slides.

Parameters
[out]vecSlide: vector of slides to be created
vecLayerSlide: vector of the desired slides for the layers

Definition at line 125 of file main.cpp.

125 {
126 if(vecLayerSlide.size() == 0lu){return;}
127 PVecLayer::iterator it(vecLayerSlide.begin());
128 long unsigned int minSlide(it->begin), maxSlide(it->end);
129 ++it;
130 while(it != vecLayerSlide.end()){
131 if(it->begin < minSlide){minSlide = it->begin;}
132 if(it->end > maxSlide){maxSlide = it->end;}
133 ++it;
134 }
135 for(PVecLayer::iterator itCmd(vecLayerSlide.begin()); itCmd != vecLayerSlide.end(); ++itCmd){
136 if(itCmd->command == "UntilEnd"){
137 itCmd->end = maxSlide;
138 }
139 }
140 for(long unsigned int i(minSlide); i < maxSlide + 1lu; ++i){
141 PSlide slide;
142 createSlideWithLayer(slide, vecLayerSlide, i);
143 vecSlide.push_back(slide);
144 }
145}
void createSlideWithLayer(PSlide &slide, const PVecLayer &vecLayerSlide, long unsigned int index)
Create the slide to be generated.
Definition main.cpp:111
std::vector< long unsigned int > PSlide
Slide composed of layer.
Definition main.cpp:44

References createSlideWithLayer().

Referenced by processFileSvg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createVectorSlide()

void createVectorSlide ( PVecLayer & vecLayerSlide,
const PVecXml & vecLayer )

Create the vector of slide layer.

Parameters
[out]vecLayerSlide: output vector of layer slide
vecLayer: vector of layer from svg file

Definition at line 66 of file main.cpp.

66 {
67 long unsigned int i(0lu);
68 for(PVecXml::const_iterator itLayer(vecLayer.begin()); itLayer != vecLayer.end(); ++itLayer){
69 PString inkscapeLabel(getInkscapeLabel(*itLayer));
70 std::cout << "\tlayer '" << inkscapeLabel << "'";
71 PVecString listNb(inkscapeLabel.split('-'));
72 long unsigned int begin(0lu), end(0lu);
73 bool isBeginDefined(false), isEndDefine(false);
74 PString command("");
75 for(PVecString::iterator it(listNb.begin()); it != listNb.end(); ++it){
76 if(it->isNumber()){
77 if(isBeginDefined){
78 end = atol(it->c_str());
79 isEndDefine = true;
80 }else{
81 begin = atol(it->c_str());
82 isBeginDefined = true;
83 }
84 }else{
85 if(*it == "N" && isBeginDefined){
86 command = "UntilEnd";
87 std::cout << "\t=> until end";
88 }
89
90 }
91 }
92 std::cout << std::endl;
93 if(!isBeginDefined && !isEndDefine){
94 begin = i;
95 end = i;
96 }
97 PLayer layer;
98 layer.command = command;
99 layer.begin = begin;
100 if(isEndDefine){layer.end = end;}
101 else{layer.end = begin;}
102 vecLayerSlide.push_back(layer);
103 }
104}
PString getInkscapeLabel(const PXml &layerXml)
Get the inkscape label of current PXml.
Definition main.cpp:53
Get the begin and end slide for current layer.
Definition main.cpp:31
long unsigned int end
Last slide where the layer has to be printed.
Definition main.cpp:35
long unsigned int begin
first slide where the layer has to be printed
Definition main.cpp:33
PString command
Extra command.
Definition main.cpp:37

References PLayer::begin, PLayer::command, PLayer::end, and getInkscapeLabel().

Referenced by processFileSvg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInkscapeLabel()

PString getInkscapeLabel ( const PXml & layerXml)

Get the inkscape label of current PXml.

Parameters
layerXml: inkscape layer
Returns
inkscape label of current PXml

Definition at line 53 of file main.cpp.

53 {
54 PString attr("");
55 PXmlAttr attrXml;
56 if(pxml_getAttrIfExist(attrXml, layerXml, "inkscape:label")){
57 attr = attrXml.getValue();
58 }
59 return attr;
60}

Referenced by createVectorSlide().

+ Here is the caller graph for this function:

◆ getSlideNumber()

PString getSlideNumber ( long unsigned int i)

Convert the slide index into string.

Parameters
i: slide index
Returns
output string

Definition at line 151 of file main.cpp.

151 {
152 if(i < 10lu){
153 return "0" + valueToString(i);
154 }else{
155 return valueToString(i);
156 }
157}

Referenced by saveSlides().

+ Here is the caller graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 293 of file main.cpp.

293 {
294 OptionParser parser = createOptionParser();
295 parser.parseArgument(argc, argv);
296
297 const OptionMode & defaultMode = parser.getDefaultMode();
298 std::vector<PPath> listInputFile;
299 defaultMode.getValue(listInputFile, "input");
300
301 return processFiles(listInputFile);
302}
int processFiles(const std::vector< PPath > &listInputFile)
Process all the input files.
Definition main.cpp:280
OptionParser createOptionParser()
Create the OptionParser of this program.
Definition main.cpp:17

References createOptionParser(), and processFiles().

+ Here is the call graph for this function:

◆ processFiles()

int processFiles ( const std::vector< PPath > & listInputFile)

Process all the input files.

Parameters
listInputFile: list of the input files
Returns
0 on success, -1 otherwise

Definition at line 280 of file main.cpp.

280 {
281 if(listInputFile.size() == 0lu){
282 std::cerr << "processFiles : no input file" << std::endl;
283 return -1;
284 }
285 bool isInkscapeExist = system("inkscape --version") == 0;
286 bool b(true);
287 for(std::vector<PPath>::const_iterator it(listInputFile.begin()); it != listInputFile.end() && b; ++it){
288 b &= processFileSvg(*it, isInkscapeExist);
289 }
290 return 1 - b;
291}
bool processFileSvg(const PPath &inputFile, bool isInkscapeExist)
Process all the input files.
Definition main.cpp:221

References processFileSvg().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ processFileSvg()

bool processFileSvg ( const PPath & inputFile,
bool isInkscapeExist )

Process all the input files.

Parameters
inputFile: list of the input files
isInkscapeExist: true if the inkscape program has been found
Returns
true on success, false otherwise

Definition at line 221 of file main.cpp.

221 {
222 if(inputFile == ""){return false;}
223 PXml root;
224 if(!pxml_parserFile(root, inputFile)){
225 std::cerr << "processFileSvg : can't load file '"<<inputFile<<"'" << std::endl;
226 return false;
227 }
228// cout << "processFileSvg : root nbChild : " << root.getVecChild().size() << endl;
229// PVecXml & vecChild = root.getVecChild();
230// for(PVecXml::iterator it(vecChild.begin()); it != vecChild.end(); ++it){
231// cout << "processFileSvg : '" << it->getName() << "'" << endl;
232// }
233 PXml svgBalise;
234 if(!pxml_getChildIfExist(svgBalise, root, "svg")){
235 std::cerr << "processFileSvg : can't find 'svg' balise in root" << std::endl;
236 return false;
237 }
238
239// PVecXml & vecChildSvg = svgBalise.getVecChild();
240// for(PVecXml::iterator it(vecChildSvg.begin()); it != vecChildSvg.end(); ++it){
241// cout << "processFileSvg : svg '" << it->getName() << "'" << endl;
242// }
243
244 PVecXml vecLayer;
245 if(!pxml_getVecChildIfExist(vecLayer, svgBalise, "g")){
246 std::cerr << "processFileSvg : can't find 'g' balise in svg" << std::endl;
247 return false;
248 }
249 long unsigned int nbCalque(vecLayer.size());
250 std::cout << "processFileSvg : get " << nbCalque << " layer";
251 if(nbCalque > 1lu){std::cout << "s";}
252 std::cout << std::endl;
253
254 PVecLayer vecLayerSlide;
255 createVectorSlide(vecLayerSlide, vecLayer);
256 if(vecLayerSlide.size() == 0lu){
257 std::cerr << "processFileSvg : no layer defined with -begin-end synthax" << std::endl;
258 return false;
259 }
260 PVecSlide vecSlides;
261 createVecSlide(vecSlides, vecLayerSlide);
262
263 PXml lighRoot(root);
264 PXml * svgPtr = pxml_getChildPtr(lighRoot, "svg");
265 PXml svgNoLayer(pxml_eraseVecChild(*svgPtr, "g"));
266 *svgPtr = svgNoLayer;
267
268 POutoutMode outputMode;
269 PString baseOutputSlideName(inputFile.getFileName().eraseExtension());
270 pinkscape_loadSlideMap(outputMode.mapSlide, baseOutputSlideName);
271 bool b = saveSlides(outputMode, baseOutputSlideName, vecSlides, vecLayer, lighRoot, isInkscapeExist);
272 pinkscape_saveSlideMap(outputMode.mapSlide, baseOutputSlideName);
273 return b;
274}
std::vector< PSlide > PVecSlide
Define the layer in slide.
Definition main.cpp:47
std::vector< PLayer > PVecLayer
Vector of layer.
Definition main.cpp:41
void createVecSlide(PVecSlide &vecSlide, PVecLayer &vecLayerSlide)
Create the vector of slides.
Definition main.cpp:125
bool saveSlides(POutoutMode &outputMode, const PPath &baseOutputName, const PVecSlide &vecSlides, const PVecXml &vecLayerXml, const PXml &lighRoot, bool isInkscapeExist)
Save the slides.
Definition main.cpp:167
void createVectorSlide(PVecLayer &vecLayerSlide, const PVecXml &vecLayer)
Create the vector of slide layer.
Definition main.cpp:66
void pinkscape_loadSlideMap(PMapSlide &mapFormula, const PPath &baseOutputName)
Load the map file of all the formulae if it exists.
void pinkscape_saveSlideMap(const PMapSlide &mapFormula, const PPath &baseOutputName)
Save the map file of the formulae to avoid extra latex call.
Output mode of the html backend.
PMapSlide mapSlide
Map of the formula which are already saved as png files.

References createVecSlide(), createVectorSlide(), POutoutMode::mapSlide, pinkscape_loadSlideMap(), pinkscape_saveSlideMap(), and saveSlides().

Referenced by processFiles().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveSlides()

bool saveSlides ( POutoutMode & outputMode,
const PPath & baseOutputName,
const PVecSlide & vecSlides,
const PVecXml & vecLayerXml,
const PXml & lighRoot,
bool isInkscapeExist )

Save the slides.

Parameters
baseOutputName: base of the slides output names
vecSlides: vector of slides to be created
vecLayerXml: vector of xml layer to be used
lighRoot: root xml without the layers in svg
isInkscapeExist: true if inkscape has been found
Returns
true on success, false otherwise

Definition at line 167 of file main.cpp.

169{
170 long unsigned int i(0lu);
171 for(PVecSlide::const_iterator itSlide(vecSlides.begin()); itSlide != vecSlides.end(); ++itSlide){
172 PXml tmpRoot(lighRoot);
173 tmpRoot.setName("");
174 PXml * svgPtr = pxml_getChildPtr(tmpRoot, "svg");
175 const PSlide & slide = *itSlide;
176 for(PSlide::const_iterator it(slide.begin()); it != slide.end(); ++it){
177// We need to suppress attribute : style="display:none"
178 PXml tmpLayer(vecLayerXml[*it]);
179 pxml_setAttr(tmpLayer, "style", "display:inline");
180 svgPtr->getVecChild().push_back(tmpLayer); //We add the corresponding layer
181 }
182 //Save svg file
183 PPath outputSlide(PPath("./") + baseOutputName + PPath("_") + getSlideNumber(i) + PPath(".svg"));
184 //Get the svg content
185 PString slideContent(pxml_baliseStr(tmpRoot, true));
186 //Check if the svg is already saved
187 if(pinkscape_isSlideKnown(outputMode, outputSlide, slideContent)){
188 continue;
189 }
190 //If not, save the svg into png image
191 if(!outputSlide.saveFileContent(slideContent)){
192 std::cerr << "saveSlides : can't save svg file '"<<outputSlide<<"'" << std::endl;
193 return false;
194 }
195 //Call inkscape to make png file
196 PPath outputSlidePng(baseOutputName + PPath("_") + getSlideNumber(i) + PPath(".png"));
197// PString command("inkscape --without-gui -e "+outputSlidePng+" "+outputSlide);
198 PString command(PString("convert ") + outputSlide + PString(" ") + outputSlidePng);
199// PString command("inkscape --batch-process -o "+outputSlidePng+" "+outputSlide); //for Inkscape 1.1.2 (0a00cf5339, 2022-02-04)
200
201 if(system(command.c_str()) != 0){
202 std::cerr << "saveSlides : can't create png file with command '"<<command<<"'" << std::endl;
203 return !isInkscapeExist;
204 }
205 //Removing the temporary svg files
206 command = PString("rm ") + outputSlide;
207 if(system(command.c_str()) != 0){
208 std::cerr << "saveSlides : can't remove temporary svg file with command '"<<command<<"'" << std::endl;
209 return false;
210 }
211 ++i;
212 }
213 return true;
214}
PString getSlideNumber(long unsigned int i)
Convert the slide index into string.
Definition main.cpp:151
bool pinkscape_isSlideKnown(POutoutMode &outputMode, const PPath &outputSlide, const PString &slideContent)
Check if the slide we are going to save was already saved (with the same content) or not.

References getSlideNumber(), and pinkscape_isSlideKnown().

Referenced by processFileSvg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: