Doxygen
Loading...
Searching...
No Matches
dotgraph.cpp
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright (C) 1997-2019 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 <mutex>
17#include <regex>
18
19#include "config.h"
20#include "doxygen.h"
21#include "indexlist.h"
22#include "md5.h"
23#include "message.h"
24#include "util.h"
25
26#include "dot.h"
27#include "dotrunner.h"
28#include "dotgraph.h"
29#include "dotnode.h"
30#include "dotfilepatcher.h"
31#include "fileinfo.h"
32#include "portable.h"
33
34#define MAP_CMD "cmapx"
35
36//QCString DotGraph::DOT_FONTNAME; // will be initialized in initDot
37//int DotGraph::DOT_FONTSIZE; // will be initialized in initDot
38
39/*! Checks if a file "baseName".md5 exists. If so the contents
40* are compared with \a md5. If equal FALSE is returned.
41* The .md5 is created or updated after successful creation of the output file.
42*/
43static bool sameMd5Signature(const QCString &baseName,
44 const QCString &md5)
45{
46 bool same = false;
47 char md5stored[33];
48 md5stored[0]=0;
49 std::ifstream f = Portable::openInputStream(baseName+".md5",true);
50 if (f.is_open())
51 {
52 // read checksum
53 f.read(md5stored,32);
54 md5stored[32]='\0';
55 // compare checksum
56 if (!f.fail() && md5==md5stored)
57 {
58 same = true;
59 }
60 //printf("sameSignature(%s,%s==%s)=%d\n",qPrint(baseName),md5stored,qPrint(md5),same);
61 }
62 else
63 {
64 //printf("sameSignature(%s) not found\n",qPrint(baseName));
65 }
66 return same;
67}
68
69static bool deliverablesPresent(const QCString &file1,const QCString &file2)
70{
71 bool file1Ok = true;
72 bool file2Ok = true;
73 if (!file1.isEmpty())
74 {
75 FileInfo fi(file1.str());
76 file1Ok = (fi.exists() && fi.size()>0);
77 }
78 if (!file2.isEmpty())
79 {
80 FileInfo fi(file2.str());
81 file2Ok = (fi.exists() && fi.size()>0);
82 }
83 return file1Ok && file2Ok;
84}
85
86static bool insertMapFile(TextStream &out,const QCString &mapFile,
87 const QCString &relPath,const QCString &mapLabel)
88{
89 FileInfo fi(mapFile.str());
90 if (fi.exists() && fi.size()>0) // reuse existing map file
91 {
92 TextStream t;
93 DotFilePatcher::convertMapFile(t,mapFile,relPath,false);
94 if (!t.empty())
95 {
96 out << "<map name=\"" << mapLabel << "\" id=\"" << mapLabel << "\">\n";
97 out << t.str();
98 out << "</map>\n";
99 }
100 return true;
101 }
102 return false; // no map file yet, need to generate it
103}
104
105//--------------------------------------------------------------------
106
108{
110 ("." + getDotImageExtension()) : (Config_getBool(USE_PDFLATEX) ? ".pdf" : ".eps"));
111}
112
114
116 TextStream& t, // output stream for the code file (html, ...)
117 GraphOutputFormat gf, // bitmap(png/svg) or ps(eps/pdf)
118 EmbeddedOutputFormat ef, // html, latex, ...
119 const QCString &path, // output folder
120 const QCString &fileName, // name of the code file (for code patcher)
121 const QCString &relPath, // output folder relative to code file
122 bool generateImageMap, // in case of bitmap, shall there be code generated?
123 int graphId) // number of this graph in the current code, used in svg code
124{
125 m_graphFormat = gf;
126 m_textFormat = ef;
127 m_dir = Dir(path.str());
128 m_fileName = fileName;
129 m_relPath = relPath;
130 m_generateImageMap = generateImageMap;
131 m_graphId = graphId;
132
133 m_absPath = m_dir.absPath() + "/";
135
137
139
141 {
142 std::lock_guard<std::mutex> lock(g_dotIndexListMutex);
144 }
145
146 generateCode(t);
147
148 return m_baseName;
149}
150
152{
153 if (!m_dir.exists())
154 {
155 term("Output dir %s does not exist!\n", m_dir.path().c_str());
156 }
157
158 char sigStr[33];
159 uint8_t md5_sig[16];
160 // calculate md5
161 MD5Buffer(m_theGraph.data(), static_cast<unsigned int>(m_theGraph.length()), md5_sig);
162 // convert result to a string
163 MD5SigToString(md5_sig, sigStr);
164
165 // already queued files are processed again in case the output format has changed
166
167 if (sameMd5Signature(absBaseName(), sigStr) &&
170 )
171 )
172 {
173 // all needed files are there
174 return FALSE;
175 }
176
177 // need to rebuild the image
178
179 // write .dot file because image was new or has changed
180 std::ofstream f = Portable::openOutputStream(absDotName());
181 if (!f.is_open())
182 {
183 err("Could not open file %s for writing\n",qPrint(absDotName()));
184 return TRUE;
185 }
186 f << m_theGraph;
187 f.close();
188
190 {
191 // run dot to create a bitmap image
192 DotRunner * dotRun = DotManager::instance()->createRunner(absDotName(), sigStr);
193 dotRun->addJob(Config_getEnumAsString(DOT_IMAGE_FORMAT), absImgName(), absDotName(), 1);
195 }
197 {
198 // run dot to create a .eps image
199 DotRunner *dotRun = DotManager::instance()->createRunner(absDotName(), sigStr);
200 if (Config_getBool(USE_PDFLATEX))
201 {
202 dotRun->addJob("pdf",absImgName(),absDotName(),1);
203 }
204 else
205 {
206 dotRun->addJob("ps",absImgName(),absDotName(),1);
207 }
208 }
209 return TRUE;
210}
211
213{
214 QCString imgExt = getDotImageExtension();
216 {
217 t << "<para>\n";
218 t << " <informalfigure>\n";
219 t << " <mediaobject>\n";
220 t << " <imageobject>\n";
221 t << " <imagedata";
222 t << " width=\"50%\" align=\"center\" valign=\"middle\" scalefit=\"0\" fileref=\"" << m_relPath << m_baseName << "." << imgExt << "\">";
223 t << "</imagedata>\n";
224 t << " </imageobject>\n";
225 t << " </mediaobject>\n";
226 t << " </informalfigure>\n";
227 t << "</para>\n";
228 }
229 else if (m_graphFormat==GraphOutputFormat::BITMAP && m_generateImageMap) // produce HTML to include the image
230 {
231 if (imgExt=="svg") // add link to SVG file without map file
232 {
233 if (!m_noDivTag) t << "<div class=\"center\">";
234 if (m_regenerate || !DotFilePatcher::writeSVGFigureLink(t,m_relPath,m_baseName,absImgName())) // need to patch the links in the generated SVG file
235 {
236 if (m_regenerate)
237 {
239 createFilePatcher(absImgName())->
240 addSVGConversion(m_relPath,FALSE,QCString(),m_zoomable,m_graphId);
241 }
242 int mapId = DotManager::instance()->
243 createFilePatcher(m_fileName)->
244 addSVGObject(m_baseName,absImgName(),m_relPath);
245 t << "<!-- " << "SVG " << mapId << " -->";
246 }
247 if (!m_noDivTag) t << "</div>\n";
248 }
249 else // add link to bitmap file with image map
250 {
251 if (!m_noDivTag) t << "<div class=\"center\">";
252 t << "<img src=\"" << relImgName() << "\" border=\"0\" usemap=\"#" << correctId(getMapLabel()) << "\" alt=\"" << getImgAltText() << "\"/>";
253 if (!m_noDivTag) t << "</div>";
254 t << "\n";
256 {
257 int mapId = DotManager::instance()->
258 createFilePatcher(m_fileName)->
259 addMap(absMapName(), m_relPath, m_urlOnly, QCString(), getMapLabel());
260 t << "<!-- MAP " << mapId << " -->\n";
261 }
262 }
263 }
264 else if (m_graphFormat==GraphOutputFormat::EPS) // produce tex to include the .eps image
265 {
267 {
268 int figId = DotManager::instance()->
269 createFilePatcher(m_fileName)->
270 addFigure(m_baseName,absBaseName(),FALSE /*TRUE*/);
271 t << "\n% FIG " << figId << "\n";
272 }
273 }
274}
275
277{
278 t << "digraph ";
279 if (title.isEmpty())
280 {
281 t << "\"Dot Graph\"";
282 }
283 else
284 {
285 t << "\"" << convertToXML(title) << "\"";
286 }
287 t << "\n{\n";
288 if (Config_getBool(INTERACTIVE_SVG)) // insert a comment to force regeneration when this
289 // option is toggled
290 {
291 t << " // INTERACTIVE_SVG=YES\n";
292 }
293 t << " // LATEX_PDF_SIZE\n"; // write placeholder for LaTeX PDF bounding box size replacement
294 t << " bgcolor=\"transparent\";\n";
295 QCString c = Config_getString(DOT_COMMON_ATTR);
296 if (!c.isEmpty()) c += ",";
297 t << " edge [" << c << Config_getString(DOT_EDGE_ATTR) << "];\n";
298 t << " node [" << c << Config_getString(DOT_NODE_ATTR) << "];\n";
299}
300
302{
303 t << "}\n";
304}
305
307 GraphType gt,
308 GraphOutputFormat format,
309 const QCString &rank, // either "LR", "RL", or ""
310 bool renderParents,
311 bool backArrows,
312 const QCString &title,
313 QCString &graphStr)
314{
315 //printf("computeMd5Signature\n");
316 TextStream md5stream;
317 writeGraphHeader(md5stream,title);
318 if (!rank.isEmpty())
319 {
320 md5stream << " rankdir=\"" << rank << "\";\n";
321 }
322 root->clearWriteFlag();
323 root->write(md5stream, gt, format, gt!=GraphType::CallGraph && gt!=GraphType::Dependency, TRUE, backArrows);
324 if (renderParents)
325 {
326 for (const auto &pn : root->parents())
327 {
328 if (pn->isVisible())
329 {
330 const auto &children = pn->children();
331 auto child_it = std::find(children.begin(),children.end(),root);
332 size_t index = child_it - children.begin();
333 root->writeArrow(md5stream, // stream
334 gt, // graph type
335 format, // output format
336 pn, // child node
337 &pn->edgeInfo()[index], // edge info
338 FALSE, // topDown?
339 backArrows // point back?
340 );
341 }
342 pn->write(md5stream, // stream
343 gt, // graph type
344 format, // output format
345 TRUE, // topDown?
346 FALSE, // toChildren?
347 backArrows // backward pointing arrows?
348 );
349 }
350 }
351 writeGraphFooter(md5stream);
352
353 graphStr=md5stream.str();
354}
355
static bool writeVecGfxFigure(TextStream &out, const QCString &baseName, const QCString &figureName)
static bool convertMapFile(TextStream &t, const QCString &mapName, const QCString &relPath, bool urlOnly=FALSE, const QCString &context=QCString())
static bool writeSVGFigureLink(TextStream &out, const QCString &relPath, const QCString &baseName, const QCString &absImgName)
Check if a reference to a SVG figure can be written and do so if possible.
QCString absBaseName() const
Definition dotgraph.h:78
bool m_noDivTag
Definition dotgraph.h:98
virtual QCString absMapName() const
Definition dotgraph.h:72
Dir m_dir
Definition dotgraph.h:87
int m_graphId
Definition dotgraph.h:91
static void computeGraph(DotNode *root, GraphType gt, GraphOutputFormat format, const QCString &rank, bool renderParents, bool backArrows, const QCString &title, QCString &graphStr)
Definition dotgraph.cpp:306
EmbeddedOutputFormat m_textFormat
Definition dotgraph.h:86
QCString imgName() const
Definition dotgraph.cpp:107
QCString m_fileName
Definition dotgraph.h:88
QCString absImgName() const
Definition dotgraph.h:81
static void writeGraphFooter(TextStream &t)
Definition dotgraph.cpp:301
bool m_urlOnly
Definition dotgraph.h:100
virtual QCString getBaseName() const =0
GraphOutputFormat m_graphFormat
Definition dotgraph.h:85
void generateCode(TextStream &t)
Definition dotgraph.cpp:212
bool prepareDotFile()
Definition dotgraph.cpp:151
static void writeGraphHeader(TextStream &t, const QCString &title=QCString())
Definition dotgraph.cpp:276
virtual QCString getImgAltText() const
Definition dotgraph.h:74
QCString relImgName() const
Definition dotgraph.h:82
QCString m_absPath
Definition dotgraph.h:93
virtual QCString getMapLabel() const =0
bool m_doNotAddImageToIndex
Definition dotgraph.h:97
bool m_regenerate
Definition dotgraph.h:96
bool m_zoomable
Definition dotgraph.h:99
QCString absDotName() const
Definition dotgraph.h:79
QCString m_theGraph
Definition dotgraph.h:95
QCString m_baseName
Definition dotgraph.h:94
QCString writeGraph(TextStream &t, GraphOutputFormat gf, EmbeddedOutputFormat ef, const QCString &path, const QCString &fileName, const QCString &relPath, bool writeImageMap=TRUE, int graphId=-1)
Definition dotgraph.cpp:115
virtual void computeTheGraph()=0
friend class DotNode
Definition dotgraph.h:36
bool m_generateImageMap
Definition dotgraph.h:90
QCString m_relPath
Definition dotgraph.h:89
static DotManager * instance()
Definition dot.cpp:78
DotRunner * createRunner(const QCString &absDotName, const QCString &md5Hash)
Definition dot.cpp:92
void write(TextStream &t, GraphType gt, GraphOutputFormat f, bool topDown, bool toChildren, bool backArrows)
Definition dotnode.cpp:630
void clearWriteFlag()
Definition dotnode.cpp:863
const DotNodeRefVector & parents() const
Definition dotnode.h:123
void writeArrow(TextStream &t, GraphType gt, GraphOutputFormat f, const DotNode *cn, const EdgeInfo *ei, bool topDown, bool pointBack=TRUE) const
Definition dotnode.cpp:579
void addJob(const QCString &format, const QCString &output, const QCString &srcFile, int srcLine)
Adds an additional job to the run.
static IndexList * indexList
Definition doxygen.h:134
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
size_t size() const
Definition fileinfo.cpp:23
void addImageFile(const QCString &name)
Definition indexlist.h:126
This is an alternative implementation of QCString.
Definition qcstring.h:101
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
const std::string & str() const
Definition qcstring.h:526
Text streaming class that buffers data.
Definition textstream.h:36
bool empty() const
Returns true iff the buffer is empty.
Definition textstream.h:253
std::string str() const
Return the contents of the buffer as a std::string object.
Definition textstream.h:229
#define Config_getEnumAsString(name)
Definition config.h:36
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
#define MAP_CMD
Definition dot.cpp:34
static bool insertMapFile(TextStream &out, const QCString &mapFile, const QCString &relPath, const QCString &mapLabel)
Definition dotgraph.cpp:86
static bool sameMd5Signature(const QCString &baseName, const QCString &md5)
Definition dotgraph.cpp:43
static bool deliverablesPresent(const QCString &file1, const QCString &file2)
Definition dotgraph.cpp:69
std::mutex g_dotIndexListMutex
Definition dotgraph.cpp:113
GraphType
Definition dotgraph.h:31
@ Dependency
Definition dotgraph.h:31
@ CallGraph
Definition dotgraph.h:31
EmbeddedOutputFormat
Definition dotgraph.h:30
GraphOutputFormat
Definition dotgraph.h:29
#define err(fmt,...)
Definition message.h:84
#define term(fmt,...)
Definition message.h:94
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
std::ofstream openOutputStream(const QCString &name, bool append=false)
Definition portable.cpp:665
Portable versions of functions that are platform dependent.
const char * qPrint(const char *s)
Definition qcstring.h:661
#define TRUE
Definition qcstring.h:37
#define FALSE
Definition qcstring.h:34
QCString convertToXML(const QCString &s, bool keepEntities)
Definition util.cpp:4266
QCString correctId(const QCString &s)
Definition util.cpp:4259
QCString getDotImageExtension()
Definition util.cpp:6616
A bunch of utility functions.