Doxygen
Loading...
Searching...
No Matches
formula.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2022 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#include <map>
17#include <vector>
18#include <string>
19#include <utility>
20
21#include "formula.h"
22#include "message.h"
23#include "config.h"
24#include "util.h"
25#include "portable.h"
26#include "image.h"
27#include "fileinfo.h"
28#include "dir.h"
29#include "regex.h"
30#include "linkedmap.h"
31#include "threadpool.h"
32#include "latexgen.h"
33#include "debug.h"
34
35// TODO: remove these dependencies
36#include "doxygen.h" // for Doxygen::indexList
37#include "indexlist.h" // for Doxygen::indexList
38
39static int determineInkscapeVersion(const Dir &thisDir);
40
48
50{
51}
52
54{
55 static FormulaManager fm;
56 return fm;
57}
58
60{
61 std::ifstream f = Portable::openInputStream(dir+"/formula.repository");
62 if (f.is_open())
63 {
64 uint32_t formulaCount=0;
65 msg("Reading formula repository...\n");
66 std::string readLine;
67 std::string line;
68 std::string prefix("\\_form#");
69 int nextLineNr=1;
70 bool hasNextLine = !getline(f,readLine).fail();
71 while (hasNextLine)
72 {
73 line = readLine;
74 int lineNr = nextLineNr;
75
76 // look ahead a bit because a formula can be spread over several lines
77 while ((hasNextLine = !getline(f,readLine).fail()))
78 {
79 nextLineNr+=1;
80 if (!readLine.compare(0, prefix.size(), prefix)) break;
81 line += "\n" + readLine;
82 }
83
84 // new format: \_form#<digits>=<digits>x<digits>:formula
85 static const reg::Ex re_new(R"(\\_form#(\d+)=(\d+)x(\d+):)");
86 // old format: \_form#<digits>:formula
87 static const reg::Ex re_old(R"(\\_form#(\d+):)");
88
89 reg::Match match;
90 int id = -1;
91 int width = -1;
92 int height = -1;
93 std::string text;
94 if (reg::search(line,match,re_new)) // try new format first
95 {
96 id = std::stoi(match[1].str());
97 width = std::stoi(match[2].str());
98 height = std::stoi(match[3].str());
99 text = line.substr(match.position()+match.length());
100 //printf("new format found id=%d width=%d height=%d text=%s\n",id,width,height,qPrinf(text));
101 }
102 else if (reg::search(line,match,re_old)) // check for old format
103 {
104 //id = std::stoi(match[1].str());
105 //text = line.substr(match.position()+match.length());
106 //printf("old format found id=%d text=%s\n",id,qPrint(text));
107 msg("old formula.repository format detected; forcing upgrade.\n");
108 p->repositoriesValid = false;
109 break;
110 }
111 else // unexpected content
112 {
113 warn_uncond("{}/formula.repository contains invalid content at line {}: found: '{}'\n",dir,lineNr,line);
114 p->repositoriesValid = false;
115 break;
116 }
117
118 auto it = p->formulaIdMap.find(id);
119 Formula *formula=nullptr;
120 if (it!=p->formulaIdMap.end()) // formula already found in a repository for another output format
121 {
122 formula = it->second;
123 if (formula->text().str()!=text) // inconsistency between repositories detected
124 {
125 msg("differences detected between formula.repository files; forcing upgrade.\n");
126 p->repositoriesValid = false;
127 break;
128 }
129 formulaCount++;
130 }
131 else // create new formula from cache
132 {
133 //printf("formula not found adding it under id=%d\n",id);
134 formula = p->formulas.add(text,id,width,height);
135 p->formulaIdMap.emplace(id,formula);
136 }
137
138 if (formula) // if an entry in the repository exists also check if there is a generated image
139 {
140 QCString formImgName;
141 formImgName.sprintf("form_%d",formula->id());
142 FileInfo fiPng((dir+"/"+formImgName+".png").str());
143 FileInfo fiSvg((dir+"/"+formImgName+".svg").str());
144 // mark formula as cached, so we do not need to regenerate the images
145 bool isCached = fiPng.exists() || fiSvg.exists();
146 formula->setCached(isCached);
147 //printf("formula %d: cached=%d\n",formula->id(),isCached);
148
149 FileInfo fiPngDark((dir+"/"+formImgName+"_dark.png").str());
150 FileInfo fiSvgDark((dir+"/"+formImgName+"_dark.svg").str());
151 bool isCachedDark = fiPngDark.exists() || fiSvgDark.exists();
152 formula->setCachedDark(isCachedDark);
153 //printf("formula %d: cachedDark=%d\n",formula->id(),isCachedDark);
154 }
155 }
156
157 // For the first repository all formulas should be new (e.g. formulaCount==0).
158 // For the other repositories the same number of formulas should be found
159 // (and number of formulas should be the same for all repositories, content is already check above)
160 if (formulaCount>0 && formulaCount!=p->formulas.size()) // inconsistency between repositories
161 {
162 msg("differences detected between formula.repository files; forcing upgrade.\n");
163 p->repositoriesValid = false;
164 }
165 }
166 else // no repository found for an output format
167 {
168 p->repositoriesValid = false;
169 }
170}
171
173{
174 //printf("checkRepositories valid=%d\n",p->repositoriesValid);
175 if (!p->repositoriesValid)
176 {
177 clear(); // clear cached formulas, so the corresponding images and repository files
178 // are regenerated
179 p->repositoriesValid = true;
180 }
181}
182
183void FormulaManager::createLatexFile(const QCString &fileName,Format format,Mode mode,IntVector &formulasToGenerate)
184{
185 // generate a latex file containing one formula per page.
186 QCString texName=fileName+".tex";
187 std::ofstream f = Portable::openOutputStream(texName);
188 if (f.is_open())
189 {
190 TextStream t(&f);
191 t << "\\documentclass{article}\n";
192 t << "\\usepackage{iftex}\n";
193 t << "\\usepackage{ifthen}\n";
194 t << "\\usepackage{epsfig}\n"; // for those who want to include images
195 t << "\\usepackage[utf8]{inputenc}\n"; // looks like some older distributions with newunicode package 1.1 need this option.
196 t << "\\usepackage{xcolor}\n";
197
198 if (mode==Mode::Dark) // invert page and text colors
199 {
200 t << "\\color{white}\n";
201 t << "\\pagecolor{black}\n";
202 }
203
206
207 QCString macroFile = Config_getString(FORMULA_MACROFILE);
208 if (!macroFile.isEmpty())
209 {
210 FileInfo fi(macroFile.str());
211 QCString stripMacroFile = fi.fileName();
212 t << "\\input{" << stripMacroFile << "}\n";
213 }
214
215 t << "\\pagestyle{empty}\n";
216 t << "\\begin{document}\n";
217 for (const auto &formula : p->formulas)
218 {
219 int id = formula->id();
220 // only formulas for which no image is cached are generated
221 //printf("check formula %d: cached=%d cachedDark=%d\n",formula->id(),formula->isCached(),formula->isCachedDark());
222 if ((mode==Mode::Light && !formula->isCached()) ||
223 (mode==Mode::Dark && !formula->isCachedDark())
224 )
225 {
226 // we force a pagebreak after each formula
227 t << formula->text() << "\n\\pagebreak\n\n";
228 formulasToGenerate.push_back(id);
229 }
230 QCString resultName;
231 resultName.sprintf("form_%d%s.%s",id, mode==Mode::Light?"":"_dark", format==Format::Vector?"svg":"png");
232 Doxygen::indexList->addImageFile(resultName);
233 }
234 t << "\\end{document}\n";
235 t.flush();
236 f.close();
237 }
238}
239
240static bool createDVIFile(const QCString &fileName)
241{
242 QCString latexCmd = "latex";
243 const size_t argsLen = 4096;
244 char args[argsLen];
245 int rerunCount=1;
246 while (rerunCount<8)
247 {
248 //printf("Running latex...\n");
249 qsnprintf(args,argsLen,"-interaction=batchmode %s >%s",qPrint(fileName),Portable::devNull());
250 if ((Portable::system(latexCmd,args)!=0) || (Portable::system(latexCmd,args)!=0))
251 {
252 err("Problems running latex. Check your installation or look "
253 "for typos in {0}.tex and check {0}.log!\n",fileName);
254 return false;
255 }
256 // check the log file if we need to run latex again to resolve references
257 QCString logFile = fileToString(fileName+".log");
258 if (logFile.isEmpty() ||
259 (logFile.find("Rerun to get cross-references right")==-1 && logFile.find("Rerun LaTeX")==-1))
260 {
261 break;
262 }
263 rerunCount++;
264 }
265 return true;
266}
267
268static bool createPostscriptFile(const QCString &fileName,const QCString &formBase,int pageIndex)
269{
270 const size_t argsLen = 4096;
271 char args[argsLen];
272 // run dvips to convert the page with number pageIndex to an
273 // postscript file.
274 qsnprintf(args,argsLen,"-q -D 600 -n 1 -p %d -o %s_tmp.ps %s.dvi",pageIndex,qPrint(formBase),qPrint(fileName));
275 if (Portable::system("dvips",args)!=0)
276 {
277 err("Problems running dvips. Check your installation!\n");
278 return false;
279 }
280 return true;
281}
282
283static bool createEPSbboxFile(const QCString &formBase)
284{
285 const size_t argsLen = 4096;
286 char args[argsLen];
287 // extract the bounding box for the postscript file
288 qsnprintf(args,argsLen,"-q -dBATCH -dNOPAUSE -P- -dNOSAFER -sDEVICE=bbox %s_tmp.ps 2>%s_tmp.epsi",
289 qPrint(formBase),qPrint(formBase));
291 {
292 err("Problems running {}. Check your installation!\n",Portable::ghostScriptCommand());
293 return false;
294 }
295 return true;
296}
297
298static bool extractBoundingBox(const QCString &formBase,
299 int *x1,int *y1,int *x2,int *y2,
300 double *x1hi,double *y1hi,double *x2hi,double *y2hi)
301{
302 FileInfo fi((formBase+"_tmp.epsi").str());
303 if (fi.exists())
304 {
305 QCString eps = fileToString(formBase+"_tmp.epsi");
306 int i = eps.find("%%BoundingBox:");
307 if (i!=-1)
308 {
309 sscanf(eps.data()+i,"%%%%BoundingBox:%d %d %d %d",x1,y1,x2,y2);
310 }
311 else
312 {
313 err("Couldn't extract bounding box from {}_tmp.epsi\n",formBase);
314 return false;
315 }
316 i = eps.find("%%HiResBoundingBox:");
317 if (i!=-1)
318 {
319 sscanf(eps.data()+i,"%%%%HiResBoundingBox:%lf %lf %lf %lf",x1hi,y1hi,x2hi,y2hi);
320 }
321 else
322 {
323 err("Couldn't extract high resolution bounding box from {}_tmp.epsi\n",formBase);
324 return false;
325 }
326 }
327 //printf("Bounding box [%d %d %d %d]\n",x1,y1,x2,y2);
328 return true;
329}
330
331static std::mutex g_formulaUpdateMutex;
332
333static double updateFormulaSize(Formula *formula,int x1,int y1,int x2,int y2)
334{
335 double scaleFactor = 1.25;
336 int zoomFactor = Config_getInt(FORMULA_FONTSIZE);
337 if (zoomFactor<8 || zoomFactor>50) zoomFactor=10;
338 scaleFactor *= zoomFactor/10.0;
339
340 if (formula)
341 {
342 std::lock_guard<std::mutex> lock(g_formulaUpdateMutex);
343 formula->setWidth(static_cast<int>((x2-x1)*scaleFactor+0.5));
344 formula->setHeight(static_cast<int>((y2-y1)*scaleFactor+0.5));
345 }
346 return scaleFactor;
347}
348
349static bool createCroppedPDF(const QCString &formBase,int x1,int y1,int x2,int y2)
350{
351 const size_t argsLen = 4096;
352 char args[argsLen];
353 // crop the image to its bounding box
354 qsnprintf(args,argsLen,"-q -dBATCH -dNOPAUSE -P- -dNOSAFER -sDEVICE=pdfwrite"
355 " -o %s_tmp.pdf -c \"[/CropBox [%d %d %d %d] /PAGES pdfmark\" -f %s_tmp.ps",
356 qPrint(formBase),x1,y1,x2,y2,qPrint(formBase));
358 {
359 err("Problems running {}. Check your installation!\n",Portable::ghostScriptCommand());
360 return false;
361 }
362 return true;
363}
364
365static bool createCroppedEPS(const QCString &formBase)
366{
367 const size_t argsLen = 4096;
368 char args[argsLen];
369 // crop the image to its bounding box
370 qsnprintf(args,argsLen,"-q -dBATCH -dNOPAUSE -P- -dNOSAFER -sDEVICE=eps2write"
371 " -o %s_tmp.eps -f %s_tmp.ps",qPrint(formBase),qPrint(formBase));
373 {
374 err("Problems running {}. Check your installation!\n",Portable::ghostScriptCommand());
375 return false;
376 }
377 return true;
378}
379
380static bool createSVGFromPDF(const QCString &formBase,const QCString &outFile)
381{
382 const size_t argsLen = 4096;
383 char args[argsLen];
384 qsnprintf(args,argsLen,"%s_tmp.pdf %s",qPrint(formBase),qPrint(outFile));
385 if (Portable::system("pdf2svg",args)!=0)
386 {
387 err("Problems running pdf2svg. Check your installation!\n");
388 return false;
389 }
390 return true;
391}
392
393static bool createSVGFromPDFviaInkscape(const Dir &thisDir,const QCString &formBase,const QCString &outFile)
394{
395 const size_t argsLen = 4096;
396 char args[argsLen];
397 int inkscapeVersion = determineInkscapeVersion(thisDir);
398 if (inkscapeVersion == -1)
399 {
400 err("Problems determining the version of inkscape. Check your installation!\n");
401 return false;
402 }
403 else if (inkscapeVersion == 0)
404 {
405 qsnprintf(args,argsLen,"-l %s -z %s_tmp.pdf 2>%s",qPrint(outFile),qPrint(formBase),Portable::devNull());
406 }
407 else // inkscapeVersion >= 1
408 {
409 qsnprintf(args,argsLen,"--export-type=svg --export-filename=%s %s_tmp.pdf 2>%s",qPrint(outFile),qPrint(formBase),Portable::devNull());
410 }
411 if (Portable::system("inkscape",args)!=0)
412 {
413 err("Problems running inkscape. Check your installation!\n");
414 return false;
415 }
416 return true;
417}
418
419
420static bool updateEPSBoundingBox(const QCString &formBase,
421 int x1,int y1,int x2,int y2,
422 double x1hi,double y1hi,double x2hi,double y2hi)
423{
424 // read back %s_tmp.eps and replace
425 // bounding box values with x1,y1,x2,y2 and remove the HiResBoundingBox
426 std::ifstream epsIn = Portable::openInputStream(formBase+"_tmp.eps");
427 std::ofstream epsOut = Portable::openOutputStream(formBase+"_tmp_corr.eps");
428 if (epsIn.is_open() && epsOut.is_open())
429 {
430 std::string line;
431 while (getline(epsIn,line))
432 {
433 if (line.rfind("%%BoundingBox",0)==0)
434 {
435 epsOut << "%%BoundingBox: " << std::max(0,x1-1) << " " << std::max(0,y1-1) << " " << (x2+1) << " " << (y2+1) << "\n";
436 }
437 else if (line.rfind("%%HiResBoundingBox",0)==0)
438 {
439 epsOut << "%%HiResBoundingBox: " << std::max(0.0,x1hi-1.0) << " " << std::max(0.0,y1hi-1.0) << " " << (x2hi+1.0) << " " << (y2hi+1.0) << "\n";
440 }
441 else
442 {
443 epsOut << line << "\n";
444 }
445 }
446 epsIn.close();
447 epsOut.close();
448 }
449 else
450 {
451 err("Problems correcting the eps files from {}_tmp.eps to {}_tmp_corr.eps\n",
452 formBase,formBase);
453 return false;
454 }
455 return true;
456}
457
458static bool createPNG(const QCString &formBase,const QCString &outFile,double scaleFactor)
459{
460 const size_t argsLen = 4096;
461 char args[argsLen];
462 qsnprintf(args,argsLen,"-q -dNOSAFER -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -dTextAlphaBits=4 "
463 "-r%d -sOutputFile=%s %s_tmp_corr.eps",static_cast<int>(scaleFactor*72),qPrint(outFile),qPrint(formBase));
465 {
466 err("Problems running {}. Check your installation!\n",Portable::ghostScriptCommand());
467 return false;
468 }
469 return true;
470}
471
472static StringVector generateFormula(const Dir &thisDir,const QCString &formulaFileName,Formula *formula,int pageNum,int pageIndex,
474{
475 StringVector tempFiles;
476 QCString outputFile;
477 outputFile.sprintf("form_%d%s.%s",pageNum, mode==FormulaManager::Mode::Light?"":"_dark", format==FormulaManager::Format::Vector?"svg":"png");
478 msg("Generating image {} for formula\n",outputFile);
479
480 QCString formBase;
481 formBase.sprintf("_form%d%s",pageNum,mode==FormulaManager::Mode::Light?"":"_dark");
482
483 if (!createPostscriptFile(formulaFileName,formBase,pageIndex)) return tempFiles;
484
485 int x1=0,y1=0,x2=0,y2=0;
486 double x1hi=0.0,y1hi=0.0,x2hi=0.0,y2hi=0.0;
488 {
489 if (!createEPSbboxFile(formBase)) return tempFiles;
490 // extract the bounding box info from the generated .epsi file
491 if (!extractBoundingBox(formBase,&x1,&y1,&x2,&y2,&x1hi,&y1hi,&x2hi,&y2hi)) return tempFiles;
492 }
493 else // for dark images the bounding box is wrong (includes the black) so
494 // use the bounding box of the light image instead.
495 {
496 QCString formBaseLight;
497 formBaseLight.sprintf("_form%d",pageNum);
498 if (!extractBoundingBox(formBaseLight,&x1,&y1,&x2,&y2,&x1hi,&y1hi,&x2hi,&y2hi)) return tempFiles;
499 }
500
501 // convert the corrected EPS to a bitmap
502 double scaleFactor = updateFormulaSize(formula,x1,y1,x2,y2);
503
505 {
506 if (!createCroppedPDF(formBase,x1,y1,x2,y2)) return tempFiles;
507
508 // if we have pdf2svg available use it to create a SVG image
509 if (Portable::checkForExecutable("pdf2svg"))
510 {
511 createSVGFromPDF(formBase,outputFile);
512 }
513 else if (Portable::checkForExecutable("inkscape")) // alternative is to use inkscape
514 {
515 createSVGFromPDFviaInkscape(thisDir,formBase,outputFile);
516 }
517 else
518 {
519 err("Neither 'pdf2svg' nor 'inkscape' present for conversion of formula to 'svg'\n");
520 return tempFiles;
521 }
522
523 tempFiles.push_back(formBase.str()+"_tmp.pdf");
524 }
525 else // format==FormulaManager::Format::Bitmap
526 {
527 if (!createCroppedEPS(formBase)) return tempFiles;
528
529 if (!updateEPSBoundingBox(formBase,x1,y1,x2,y2,x1hi,y1hi,x2hi,y2hi)) return tempFiles;
530
531 if (hd==FormulaManager::HighDPI::On) // for high DPI display it looks much better if the
532 // image resolution is higher than the display resolution
533 {
534 scaleFactor*=2;
535 }
536
537 if (!createPNG(formBase,outputFile,scaleFactor)) return tempFiles;
538
539 tempFiles.push_back(formBase.str()+"_tmp.eps");
540 tempFiles.push_back(formBase.str()+"_tmp_corr.eps");
541 }
542
543 // remove intermediate image files
544 tempFiles.push_back(formBase.str()+"_tmp.ps");
546 {
547 tempFiles.push_back(formBase.str()+"_tmp.epsi");
548 }
549 return tempFiles;
550}
551
553{
554 IntVector formulasToGenerate;
555 QCString formulaFileName = mode==Mode::Light ? "_formulas" : "_formulas_dark";
556 createLatexFile(formulaFileName,format,mode,formulasToGenerate);
557
558 if (!formulasToGenerate.empty()) // there are new formulas
559 {
560 if (!createDVIFile(formulaFileName)) return;
561
562 auto getFormula = [this](int pageNum) -> Formula *
563 {
564 auto it = p->formulaIdMap.find(pageNum);
565 if (it!=p->formulaIdMap.end())
566 {
567 return it->second;
568 }
569 return nullptr;
570 };
571
572 int pageIndex=1;
573 std::size_t numThreads = static_cast<std::size_t>(Config_getInt(NUM_PROC_THREADS));
574 if (numThreads>1) // multi-threaded version
575 {
576 ThreadPool threadPool(numThreads);
577 std::vector< std::future< StringVector > > results;
578 for (int pageNum : formulasToGenerate)
579 {
580 // create images for each formula.
581 auto formula = getFormula(pageNum);
582 auto processFormula = [=]() -> StringVector
583 {
584 return generateFormula(thisDir,formulaFileName,formula,pageNum,pageIndex,format,hd,mode);
585 };
586 results.emplace_back(threadPool.queue(processFormula));
587 pageIndex++;
588 }
589 for (auto &f : results)
590 {
591 auto tf = f.get();
592 p->tempFiles.insert(p->tempFiles.end(),tf.begin(),tf.end()); // append tf to p->tempFiles
593 }
594 }
595 else // single threaded version
596 {
597 for (int pageNum : formulasToGenerate)
598 {
599 // create images for each formula.
600 auto formula = getFormula(pageNum);
601 StringVector tf = generateFormula(thisDir,formulaFileName,formula,pageNum,pageIndex,format,hd,mode);
602 p->tempFiles.insert(p->tempFiles.end(),tf.begin(),tf.end()); // append tf to p->tempFiles
603
604 pageIndex++;
605 }
606 }
607 // remove intermediate files produced by latex
608 p->tempFiles.push_back(formulaFileName.str()+".dvi");
609 p->tempFiles.push_back(formulaFileName.str()+".log");
610 p->tempFiles.push_back(formulaFileName.str()+".aux");
611 }
612 // remove the latex file itself
613 p->tempFiles.push_back(formulaFileName.str()+".tex");
614
615 // write/update the formula repository so we know what text the
616 // generated images represent (we use this next time to avoid regeneration
617 // of the images, and to avoid forcing the user to delete all images in order
618 // to let a browser refresh the images).
619 std::ofstream f = Portable::openOutputStream("formula.repository");
620 if (f.is_open())
621 {
622 TextStream t(&f);
623 for (const auto &formula : p->formulas)
624 {
625 t << "\\_form#" << formula->id();
626 if (formula->width()!=-1 && formula->height()!=-1)
627 {
628 t << "=" << formula->width() << "x" << formula->height();
629 }
630 t << ":" << formula->text() << "\n";
631 }
632 }
633}
634
636{
637 Dir d(path.str());
638 // store the original directory
639 if (!d.exists())
640 {
641 term("Output directory '{}' does not exist!\n",path);
642 }
643 std::string oldDir = Dir::currentDirPath();
644
645 QCString macroFile = Config_getString(FORMULA_MACROFILE);
646 QCString stripMacroFile;
647 if (!macroFile.isEmpty())
648 {
649 FileInfo fi(macroFile.str());
650 macroFile=fi.absFilePath();
651 stripMacroFile = fi.fileName();
652 }
653
654 // go to the html output directory (i.e. path)
656 Dir thisDir;
657
658 if (!macroFile.isEmpty())
659 {
660 copyFile(macroFile,stripMacroFile);
661 }
662
663 createFormulasTexFile(thisDir,format,hd,Mode::Light);
664 if (Config_getEnum(HTML_COLORSTYLE)!=HTML_COLORSTYLE_t::LIGHT) // all modes other than light need a dark version
665 {
666 // note that the dark version reuses the bounding box of the light version so it needs to be
667 // created after the light version.
668 createFormulasTexFile(thisDir,format,hd,Mode::Dark);
669 }
670
671 // clean up temporary files
673 {
674 for (const auto &file : p->tempFiles)
675 {
676 thisDir.remove(file);
677 }
678 }
679
680 // reset the directory to the original location.
681 Dir::setCurrent(oldDir);
682}
683
685{
686 p->formulas.clear();
687 p->formulaIdMap.clear();
688}
689
690int FormulaManager::addFormula(const QCString &formulaText,int width,int height)
691{
692 Formula *formula = p->formulas.find(formulaText);
693 if (formula) // same formula already stored
694 {
695 return formula->id();
696 }
697 // add new formula
698 int id = static_cast<int>(p->formulas.size());
699 formula = p->formulas.add(formulaText,id,width,height);
700 p->formulaIdMap.emplace(id,formula);
701 return id;
702}
703
704const Formula *FormulaManager::findFormula(int formulaId) const
705{
706 auto it = p->formulaIdMap.find(formulaId);
707 return it != p->formulaIdMap.end() ? it->second : nullptr;
708}
709
710#if 0
712{
713 auto it = p->formulaIdMap.find(formulaId);
714 return it != p->formulaIdMap.end() ? it->second : nullptr;
715}
716#endif
717
718
720{
721 return !p->formulas.empty();
722}
723
724static std::mutex g_inkscapeDetectionMutex;
725
726// helper function to detect and return the major version of inkscape.
727// return -1 if the version cannot be determined.
728static int determineInkscapeVersion(const Dir &thisDir)
729{
730 std::lock_guard<std::mutex> lock(g_inkscapeDetectionMutex);
731 // The command line interface (CLI) of Inkscape 1.0 has changed in comparison to
732 // previous versions. In order to invokine Inkscape, the used version is detected
733 // and based on the version the right syntax of the CLI is chosen.
734 static int inkscapeVersion = -2;
735 if (inkscapeVersion == -2) // initial one time version check
736 {
737 QCString inkscapeVersionFile = "inkscape_version" ;
738 inkscapeVersion = -1;
739 QCString args = "-z --version >"+inkscapeVersionFile+" 2>"+Portable::devNull();
740 if (Portable::system("inkscape",args)!=0)
741 {
742 // looks like the old syntax gave problems, lets try the new syntax
743 args = " --version >"+inkscapeVersionFile+" 2>"+Portable::devNull();
744 if (Portable::system("inkscape",args)!=0)
745 {
746 return -1;
747 }
748 }
749 // read version file and determine major version
750 std::ifstream inkscapeVersionIn = Portable::openInputStream(inkscapeVersionFile);
751 if (inkscapeVersionIn.is_open())
752 {
753 std::string line;
754 while (getline(inkscapeVersionIn,line))
755 {
756 size_t dotPos = line.find('.');
757 if (line.rfind("Inkscape ",0)==0 && dotPos>0)
758 {
759 // get major version
760 inkscapeVersion = std::stoi(line.substr(9,dotPos-9));
761 break;
762 }
763 }
764 inkscapeVersionIn.close();
765 }
766 else // failed to open version file
767 {
768 return -1;
769 }
771 {
772 thisDir.remove(inkscapeVersionFile.str());
773 }
774 }
775 return inkscapeVersion;
776}
constexpr auto prefix
Definition anchor.cpp:44
@ Formula
Definition debug.h:33
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
Class representing a directory in the file system.
Definition dir.h:75
static std::string currentDirPath()
Definition dir.cpp:342
std::string absPath() const
Definition dir.cpp:364
bool remove(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:314
static bool setCurrent(const std::string &path)
Definition dir.cpp:350
bool exists() const
Definition dir.cpp:257
static IndexList * indexList
Definition doxygen.h:133
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
std::string fileName() const
Definition fileinfo.cpp:118
std::string absFilePath() const
Definition fileinfo.cpp:101
Class representing a LaTeX formula as found in the documentation.
Definition formula.h:29
void setCachedDark(bool cached)
Definition formula.h:46
QCString text() const
Definition formula.h:37
void setHeight(int height)
Definition formula.h:41
void setCached(bool cached)
Definition formula.h:45
void setWidth(int width)
Definition formula.h:40
int id() const
Definition formula.h:36
void createLatexFile(const QCString &fileName, Format format, Mode mode, IntVector &formulasToGenerate)
Definition formula.cpp:183
void initFromRepository(const QCString &dir)
Definition formula.cpp:59
bool hasFormulas() const
Definition formula.cpp:719
void checkRepositories()
Definition formula.cpp:172
const Formula * findFormula(int formulaId) const
Definition formula.cpp:704
void createFormulasTexFile(Dir &d, Format format, HighDPI hd, Mode mode)
Definition formula.cpp:552
static FormulaManager & instance()
Definition formula.cpp:53
std::unique_ptr< Private > p
Definition formula.h:90
void generateImages(const QCString &outputDir, Format format, HighDPI hd=HighDPI::Off)
Definition formula.cpp:635
int addFormula(const QCString &formulaText, int width=-1, int height=-1)
Definition formula.cpp:690
Container class representing a vector of objects with keys.
Definition linkedmap.h:36
This is an alternative implementation of QCString.
Definition qcstring.h:101
int find(char c, int index=0, bool cs=TRUE) const
Definition qcstring.cpp:43
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:163
const std::string & str() const
Definition qcstring.h:552
QCString & sprintf(const char *format,...)
Definition qcstring.cpp:29
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:172
Text streaming class that buffers data.
Definition textstream.h:36
void flush()
Flushes the buffer.
Definition textstream.h:196
Class managing a pool of worker threads.
Definition threadpool.h:48
auto queue(F &&f, Args &&... args) -> std::future< decltype(f(args...))>
Queue the callable function f for the threads to execute.
Definition threadpool.h:77
Class representing a regular expression.
Definition regex.h:39
Object representing the matching results.
Definition regex.h:151
#define Config_getInt(name)
Definition config.h:34
#define Config_getString(name)
Definition config.h:32
#define Config_getEnum(name)
Definition config.h:35
std::vector< int > IntVector
Definition containers.h:38
std::vector< std::string > StringVector
Definition containers.h:33
static bool createSVGFromPDFviaInkscape(const Dir &thisDir, const QCString &formBase, const QCString &outFile)
Definition formula.cpp:393
static bool createPNG(const QCString &formBase, const QCString &outFile, double scaleFactor)
Definition formula.cpp:458
static int determineInkscapeVersion(const Dir &thisDir)
Definition formula.cpp:728
static std::mutex g_formulaUpdateMutex
Definition formula.cpp:331
static bool createCroppedEPS(const QCString &formBase)
Definition formula.cpp:365
static std::mutex g_inkscapeDetectionMutex
Definition formula.cpp:724
static bool createPostscriptFile(const QCString &fileName, const QCString &formBase, int pageIndex)
Definition formula.cpp:268
static double updateFormulaSize(Formula *formula, int x1, int y1, int x2, int y2)
Definition formula.cpp:333
static StringVector generateFormula(const Dir &thisDir, const QCString &formulaFileName, Formula *formula, int pageNum, int pageIndex, FormulaManager::Format format, FormulaManager::HighDPI hd, FormulaManager::Mode mode)
Definition formula.cpp:472
static bool createDVIFile(const QCString &fileName)
Definition formula.cpp:240
static bool createCroppedPDF(const QCString &formBase, int x1, int y1, int x2, int y2)
Definition formula.cpp:349
static bool createEPSbboxFile(const QCString &formBase)
Definition formula.cpp:283
static bool extractBoundingBox(const QCString &formBase, int *x1, int *y1, int *x2, int *y2, double *x1hi, double *y1hi, double *x2hi, double *y2hi)
Definition formula.cpp:298
static bool createSVGFromPDF(const QCString &formBase, const QCString &outFile)
Definition formula.cpp:380
static bool updateEPSBoundingBox(const QCString &formBase, int x1, int y1, int x2, int y2, double x1hi, double y1hi, double x2hi, double y2hi)
Definition formula.cpp:420
void writeExtraLatexPackages(TextStream &t)
void writeLatexSpecialFormulaChars(TextStream &t)
#define warn_uncond(fmt,...)
Definition message.h:122
#define msg(fmt,...)
Definition message.h:94
#define err(fmt,...)
Definition message.h:127
#define term(fmt,...)
Definition message.h:137
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:659
std::ofstream openOutputStream(const QCString &name, bool append=false)
Definition portable.cpp:648
bool checkForExecutable(const QCString &fileName)
Definition portable.cpp:423
const char * ghostScriptCommand()
Definition portable.cpp:437
int system(const QCString &command, const QCString &args, bool commandHasConsole=true)
Definition portable.cpp:105
const char * devNull()
Definition portable.cpp:614
bool search(std::string_view str, Match &match, const Ex &re, size_t pos)
Search in a given string str starting at position pos for a match against regular expression re.
Definition regex.cpp:844
Portable versions of functions that are platform dependent.
#define qsnprintf
Definition qcstring.h:49
const char * qPrint(const char *s)
Definition qcstring.h:687
LinkedMap< Formula > formulas
Definition formula.cpp:43
std::map< int, Formula * > formulaIdMap
Definition formula.cpp:44
StringVector tempFiles
Definition formula.cpp:46
QCString fileToString(const QCString &name, bool filter, bool isSourceCode)
Definition util.cpp:1473
bool copyFile(const QCString &src, const QCString &dest)
Copies the contents of file with name src to the newly created file with name dest.
Definition util.cpp:5859
A bunch of utility functions.