GCC Code Coverage Report


Directory: ./
File: tmp_project/PhoenixTex2Html/src/PLatexObj/platexobj_formulae.cpp
Date: 2025-03-24 18:12:43
Exec Total Coverage
Lines: 112 128 87.5%
Branches: 170 259 65.6%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fstream>
10
11 #include "data_all.h"
12 #include "convertToString.h"
13 #include "platexobj_menu.h"
14
15 #include "platexobj_formulae.h"
16
17 ///Load the map file of all the formulae if it exists
18 /** @param[out] mapFormula : map of formulae to be loaded to avoid calling latex too much
19 */
20 127 void platexobj_loadFormulaeMap(PMapFormula & mapFormula){
21
1/1
✓ Branch 1 taken 127 times.
127 PString fileName(FORMULAE_RECOVER_FILE);
22
3/3
✓ Branch 1 taken 127 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 82 times.
127 if(!data_load(fileName, mapFormula)){return;}
23
2/2
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 45 times.
127 }
24
25 ///Save the map file of the formulae to avoid extra latex call
26 /** @param[out] mapFormula : map of formulae to be saved
27 */
28 118 void platexobj_saveFormulaeMap(const PMapFormula & mapFormula){
29
1/1
✓ Branch 1 taken 118 times.
118 PString fileName(FORMULAE_RECOVER_FILE);
30
3/3
✓ Branch 1 taken 118 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 117 times.
118 if(!data_save(fileName, mapFormula)){return;}
31
2/2
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 1 times.
118 }
32
33
34 ///Save the latex file to be used to create a picture of the given formula
35 /** @param fileName : name of hte file to be generated
36 * @param latexEnv : name of the latex environement to be used
37 * @param mathContent : content of the latex environement
38 * @param fontColor : color of the font to be used
39 * @return true on success, false otherwise
40 */
41 2 bool platexobj_htmlStrLatexFile(const PString & fileName, const PString & latexEnv, const PString & mathContent, const PString & fontColor){
42
1/1
✓ Branch 1 taken 2 times.
2 std::ofstream fs;
43
1/1
✓ Branch 2 taken 2 times.
2 fs.open(fileName.c_str());
44
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!fs.is_open()) return false;
45
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if(latexEnv == "$"){
46
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\documentclass[8pt]{article}\n";
47 }else{
48 fs << "\\documentclass[12pt]{article}\n";
49 }
50
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\usepackage[T1]{fontenc}\n\\usepackage[utf8]{inputenc}\n\\usepackage{lmodern}\n\n\n";
51
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\usepackage{calc}\n\n\n\\usepackage{amssymb}\n\\usepackage{color}\n\\usepackage{amsfonts}\n\\usepackage{bbm}\n\\pagestyle{empty}\n\n\n";
52
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\usepackage{amsmath}\n\\usepackage{esint}\n\n";
53 // saveListLatexFunction(fs, ptListLatexFunc);
54 // saveListLatexEnv(fs, ptListLatexEnv);
55
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\begin{document}\n\n";
56
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\def\\sgn{\\mathop{\\mathgroup\\symoperators sgn}\\nolimits}\n";
57
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 fs << fontColor << " ";
58
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if(latexEnv == "$"){
59
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 fs << "$" << mathContent << "$\n";
60 }else{
61 fs << "\\begin{"<<latexEnv<<"}" << mathContent << "\\end{"<<latexEnv<<"}\n";
62 }
63
1/1
✓ Branch 1 taken 2 times.
2 fs << "\\end{document}\n";
64
1/1
✓ Branch 1 taken 2 times.
2 fs.close();
65 2 return true;
66 2 }
67
68 ///Lauch latex to create png file
69 /** @param fileNamePng : name of the png file to be created
70 * @return true on success, false otherwise
71 */
72 2 bool lauchLatexToPng(const PString & fileNamePng){
73
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 PString command(CMAKE_INSTALL_PREFIX "/share/PhoenixTex2Html/SCRIPTS/createTexDisplayMathpng.sh "+fileNamePng);
74
75
2/3
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if(system(command.c_str()) != 0){
76 std::cerr << "lauchLatexToPng : can't call 'latex' to create file" << std::endl;
77 return false;
78 }
79 2 return true;
80 2 }
81
82 ///Create an image with a latex formula
83 /** @param objId : id of the object to be used
84 * @param latexEnv : latex environement to be used
85 * @param texContent : text to put in the environement
86 * @param fontColor : color of the font to be used
87 * @return name of the created png file
88 */
89 2 PString platexObj_createImageWithMathTex(size_t objId, const PString & latexEnv, const PString & texContent, const PString & fontColor){
90
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 PString objIdStr(valueToString(objId));
91
1/1
✓ Branch 1 taken 2 times.
2 PString fileName("_tmp_file.tex");
92
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!platexobj_htmlStrLatexFile(fileName, latexEnv, texContent, fontColor)){
93 std::cerr << "platexObj_createImageWithMathTex : can't save latex file '"<<fileName<<"' for object " << objId << std::endl;
94 return "";
95 }
96
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 PPath outputDir(LATEX_IMAGE_OUTPUT_DIR);
97
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!outputDir.createDirectory()){
98 std::cerr << "platexObj_createImageWithMathTex : can't create directory '" << outputDir << "'" << std::endl;
99 return "";
100 }
101
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
4 PString fileNamePng(outputDir+"/imgMath"+objIdStr+".png");
102
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!lauchLatexToPng(fileNamePng)){
103 std::cerr << "platexObj_createImageWithMathTex : can't save png file '"<<fileNamePng<<"' for object " << objId << std::endl;
104 return "";
105 }
106 2 remove(fileName.c_str());
107
1/1
✓ Branch 1 taken 2 times.
2 return fileNamePng;
108 2 }
109
110 ///Create an image with a latex formula
111 /** @param[out] outputMode : output mode of the html backend
112 * @param objId : id of the object to be used
113 * @param latexEnv : latex environement to be used
114 * @param texContent : text to put in the environement
115 * @return name of the created png file
116 */
117 2 PString platexObj_createFormulaTex(POutoutMode & outputMode, size_t objId, const PString & latexEnv, const PString & texContent){
118 2 PMapFormula & mapFormula = outputMode.mapFormula;
119 //Let's check if we already know the content
120
1/1
✓ Branch 1 taken 2 times.
2 PMapFormula::iterator it(mapFormula.find(texContent));
121
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if(it != mapFormula.end()){ //We already know this formula
122 //Now we have to check if the file exists
123 if(PPath(it->second).isFileExist()){
124 return it->second;
125 }
126 }
127
1/1
✓ Branch 1 taken 2 times.
2 PString fontColor("");
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if(outputMode.currentStyle == "dark"){
129 fontColor = "\\pagecolor{black}\\color{white}";
130 }
131 //We do not know this formula, or the file of this formula does not exist
132
1/1
✓ Branch 1 taken 2 times.
2 PString outputFileName(platexObj_createImageWithMathTex(objId, latexEnv, texContent, fontColor));
133
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 mapFormula[texContent] = outputFileName;
134
1/1
✓ Branch 1 taken 2 times.
2 return outputFileName;
135 2 }
136
137
138 ///Convert a PLatexObj into a string
139 /** @param[out] outputMode : output mode of the html backend
140 * @param obj : PLatexObj to be converted into a string
141 * @param latexEnv : name of the latex environement to be used
142 * @return output string which contains the PLatexObj
143 */
144 20 PString platexobj_htmlStrLatex(POutoutMode & outputMode, const PLatexObj & obj, const PString & latexEnv){
145 20 PString body("");
146
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if(outputMode.isMathJaxMode){
147
8/8
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
✓ Branch 10 taken 20 times.
✓ Branch 13 taken 20 times.
✓ Branch 16 taken 20 times.
✓ Branch 19 taken 20 times.
✓ Branch 22 taken 20 times.
20 body += "$$\\begin{"+latexEnv+"}"+obj.getText()+"\\end{"+latexEnv+"}$$\n";
148 }else{
149 PString fileNamePng(platexObj_createFormulaTex(outputMode, obj.getId(), latexEnv, obj.getText()));
150 body += "<div class=\"formula\" /><img src=\"" + fileNamePng + "\" alt=\"nothing\" /></div>";
151 }
152 20 return body;
153 }
154
155 ///Convert a PLatexObj into a string
156 /** @param[out] outputMode : output mode of the html backend
157 * @param obj : PLatexObj to be converted into a string
158 * @return output string which contains the PLatexObj
159 */
160 58 PString platexobj_htmlStrInlineMath(POutoutMode & outputMode, const PLatexObj & obj){
161 58 PString body("");
162
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2 times.
58 if(outputMode.isMathJaxMode){
163
4/4
✓ Branch 1 taken 56 times.
✓ Branch 4 taken 56 times.
✓ Branch 7 taken 56 times.
✓ Branch 10 taken 56 times.
56 body += "$"+obj.getText()+"$\n";
164 }else{
165
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
2 PString fileNamePng(platexObj_createFormulaTex(outputMode, obj.getId(), "$", obj.getText()));
166
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
2 body += "<span class=\"inlineformula\" /><img src=\"" + fileNamePng + "\" alt=\""+obj.getText()+"\" /></span>";
167 2 }
168 58 return body;
169 }
170
171 ///Update the theme of the generated website
172 /** @param[out] outputMode : POutoutMode to be updated
173 * @param themeBaseFile : file which contains the theme we want to use
174 */
175 127 void platexobj_update_theme(POutoutMode & outputMode, const PPath & themeBaseFile){
176
3/3
✓ Branch 2 taken 127 times.
✓ Branch 5 taken 127 times.
✓ Branch 8 taken 127 times.
127 dico_create_nested_call(outputMode.themeCall, themeBaseFile.loadFileContent(), "${", "}");
177 127 }
178
179 ///Set a variable in the map of variable for theme (will take account the IS_XXX_BEGIN accordingly if the value is empty or not)
180 /** @param[out] outputMode : POutoutMode to be updated
181 * @param varName : name of the variable to be set
182 * @param value : value of the variable to be set
183 */
184 2034 void platexobj_theme_set_var(POutoutMode & outputMode, const PString & varName, const PString & value){
185
4/4
✓ Branch 2 taken 2034 times.
✓ Branch 5 taken 2034 times.
✓ Branch 8 taken 2034 times.
✓ Branch 11 taken 2034 times.
2034 outputMode.mapVar["${" + varName + "}"] = value;
186
2/2
✓ Branch 1 taken 713 times.
✓ Branch 2 taken 1321 times.
2034 if(value == ""){
187
4/4
✓ Branch 2 taken 713 times.
✓ Branch 5 taken 713 times.
✓ Branch 8 taken 713 times.
✓ Branch 11 taken 713 times.
713 outputMode.mapVar["${IS_" + varName + "_BEGIN}"] = "<!-- ";
188
4/4
✓ Branch 2 taken 713 times.
✓ Branch 5 taken 713 times.
✓ Branch 8 taken 713 times.
✓ Branch 11 taken 713 times.
713 outputMode.mapVar["${IS_" + varName + "_END}"] = " -->";
189 }else{
190
4/4
✓ Branch 2 taken 1321 times.
✓ Branch 5 taken 1321 times.
✓ Branch 8 taken 1321 times.
✓ Branch 11 taken 1321 times.
1321 outputMode.mapVar["${IS_" + varName + "_BEGIN}"] = "";
191
4/4
✓ Branch 2 taken 1321 times.
✓ Branch 5 taken 1321 times.
✓ Branch 8 taken 1321 times.
✓ Branch 11 taken 1321 times.
1321 outputMode.mapVar["${IS_" + varName + "_END}"] = "";
192 }
193 2034 }
194
195 ///Initialise basic variables of POutoutMode
196 /** @param[out] outputMode : POutoutMode to init variables
197 */
198 118 void platexobj_theme_init_var(POutoutMode & outputMode){
199
3/3
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
✓ Branch 7 taken 118 times.
118 outputMode.mapVar["${STYLE}"] = outputMode.currentStyle;
200
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 107 times.
118 if(outputMode.isBookTheme){
201
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
11 platexobj_theme_set_var(outputMode, "AUTHOR_EMAIL", outputMode.bookMail);
202
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
11 platexobj_theme_set_var(outputMode, "GITLAB_URL", outputMode.bookGitlabUrl);
203
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
11 platexobj_theme_set_var(outputMode, "GITLAB_MASTER_URL", outputMode.bookMasterProjectUrl);
204
3/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
11 outputMode.mapVar["${BOOK_TABLE_OF_CONTENT}"] = "";
205 }
206
1/1
✓ Branch 1 taken 118 times.
118 PString bibliography("");
207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 116 times.
118 if(outputMode.isBibliography){
208
1/1
✓ Branch 1 taken 2 times.
2 bibliography = "bibliography.html";
209 }
210
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 platexobj_theme_set_var(outputMode, "BIBLIOGRAPHY", bibliography);
211
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 PString remoteMathjaxFile(""), localMathjax("");
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if(outputMode.isRemoteMathjax){
213 remoteMathjaxFile = "load-mathjax.js";
214 }else{
215
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 116 times.
118 if(outputMode.isMathJaxMode){
216
1/1
✓ Branch 1 taken 2 times.
2 localMathjax = "MathJax.js";
217 }
218 }
219
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 platexobj_theme_set_var(outputMode, "REMOTE_MATHJAX", remoteMathjaxFile);
220
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 platexobj_theme_set_var(outputMode, "LOCAL_MATHJAX", localMathjax);
221 118 }
222
223 ///Update var of POutoutMode for current section
224 /** @param[out] outputMode : POutoutMode to be modified
225 * @param pageLink : link of the current page
226 * @param pageTitle : title of the current page
227 * @param prevSecLink : link of the previous section
228 * @param prevSecTitle : title of the previous section
229 * @param nextSecLink : link of the next section
230 * @param nextSecTitle : title of the next section
231 * @param parentSecLink : link of the parent section
232 * @param parentSecTitle : title of the parent section
233 * @param currentSourceFile : current source file of the section
234 * @param currentSourceLine : current source line of the section
235 */
236 549 void platexobj_theme_update_var(POutoutMode & outputMode, const PString & pageLink, const PString & pageTitle,
237 const PString & prevSecLink, const PString & prevSecTitle,
238 const PString & nextSecLink, const PString & nextSecTitle,
239 const PString & parentSecLink, const PString & parentSecTitle,
240 const PPath & currentSourceFile, size_t currentSourceLine)
241 {
242
2/2
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
549 outputMode.mapVar["${PAGE_LINK}"] = pageLink;
243
2/2
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
549 outputMode.mapVar["${PAGE_TITLE}"] = pageTitle;
244
2/2
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
549 outputMode.mapVar["${PARENT_SECTION_TITLE}"] = parentSecTitle;
245
1/1
✓ Branch 2 taken 549 times.
549 platexobj_theme_set_var(outputMode, "PARENT_SECTION", parentSecLink);
246
2/2
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
549 outputMode.mapVar["${PREV_PAGE_TITLE}"] = prevSecTitle;
247
1/1
✓ Branch 2 taken 549 times.
549 platexobj_theme_set_var(outputMode, "PREV_PAGE", prevSecLink);
248
2/2
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
549 outputMode.mapVar["${NEXT_PAGE_TITLE}"] = nextSecTitle;
249
1/1
✓ Branch 2 taken 549 times.
549 platexobj_theme_set_var(outputMode, "NEXT_PAGE", nextSecLink);
250
251
7/7
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
✓ Branch 8 taken 549 times.
✓ Branch 11 taken 549 times.
✓ Branch 14 taken 549 times.
✓ Branch 17 taken 549 times.
✓ Branch 20 taken 549 times.
549 outputMode.mapVar["${CURRENT_SOURCE_FILE}"] = currentSourceFile.makeAbsolute().simplify().getUnderPath(PPath(outputMode.bookGitlabUrl).getFileName());
252
3/3
✓ Branch 2 taken 549 times.
✓ Branch 5 taken 549 times.
✓ Branch 8 taken 549 times.
549 outputMode.mapVar["${CURRENT_SOURCE_LINE}"] = valueToString(currentSourceLine);
253
254 // outputMode.mapVar["${MAIN_TITLE}"] = "";
255 // outputMode.mapVar["${SUB_MENU}"] = "";
256
257
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 300 times.
549 if(outputMode.isBookTheme){
258
3/3
✓ Branch 2 taken 249 times.
✓ Branch 5 taken 249 times.
✓ Branch 8 taken 249 times.
249 outputMode.mapVar["${BOOK_TABLE_OF_CONTENT}"] = platexobj_menuToHtml(outputMode.fullMenu, pageLink, outputMode.isBibliography);
259 }
260 549 }
261
262 ///Create the page from variables in POutoutMode
263 /** @param[out] outputMode : POutoutMode to be used
264 * @return html page content
265 */
266 549 PString platexobj_theme_createPage(POutoutMode & outputMode){
267 549 PString body("");
268
3/3
✓ Branch 1 taken 549 times.
✓ Branch 4 taken 549 times.
✓ Branch 7 taken 549 times.
549 dico_replace_nested_call(body, outputMode.themeCall, outputMode.mapVar, "${", "}");
269 549 return body;
270 }
271
272
273