summaryrefslogtreecommitdiff
path: root/src/dotrunner.cpp
blob: fbfeacab89320a501c549a3e0e23d2b295e4adc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/******************************************************************************
*
* Copyright (C) 1997-2019 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/

#include "dotrunner.h"

#include "util.h"
#include "portable.h"
#include "dot.h"
#include "message.h"
#include "ftextstream.h"
#include "config.h"

// the graphicx LaTeX has a limitation of maximum size of 16384
// To be on the save side we take it a little bit smaller i.e. 150 inch * 72 dpi
// It is anyway hard to view these size of images
#define MAX_LATEX_GRAPH_INCH  150
#define MAX_LATEX_GRAPH_SIZE  (MAX_LATEX_GRAPH_INCH * 72)

//-----------------------------------------------------------------------------------------

// since dot silently reproduces the input file when it does not
// support the PNG format, we need to check the result.
static void checkPngResult(const char *imgName)
{
  FILE *f = Portable::fopen(imgName,"rb");
  if (f)
  {
    char data[4];
    if (fread(data,1,4,f)==4)
    {
      if (!(data[1]=='P' && data[2]=='N' && data[3]=='G'))
      {
        err("Image '%s' produced by dot is not a valid PNG!\n"
          "You should either select a different format "
          "(DOT_IMAGE_FORMAT in the config file) or install a more "
          "recent version of graphviz (1.7+)\n",imgName
        );
      }
    }
    else
    {
      err("Could not read image '%s' generated by dot!\n",imgName);
    }
    fclose(f);
  }
  else
  {
    err("Could not open image '%s' generated by dot!\n",imgName);
  }
}

static bool resetPDFSize(const int width,const int height, const char *base)
{
  QCString tmpName = QCString(base)+".tmp";
  QCString patchFile = QCString(base)+".dot";
  if (!QDir::current().rename(patchFile,tmpName))
  {
    err("Failed to rename file %s to %s!\n",patchFile.data(),tmpName.data());
    return FALSE;
  }
  QFile fi(tmpName);
  QFile fo(patchFile);
  if (!fi.open(IO_ReadOnly)) 
  {
    err("problem opening file %s for patching!\n",tmpName.data());
    QDir::current().rename(tmpName,patchFile);
    return FALSE;
  }
  if (!fo.open(IO_WriteOnly))
  {
    err("problem opening file %s for patching!\n",patchFile.data());
    QDir::current().rename(tmpName,patchFile);
    fi.close();
    return FALSE;
  }
  FTextStream t(&fo);
  const int maxLineLen=100*1024;
  while (!fi.atEnd()) // foreach line
  {
    QCString line(maxLineLen);
    int numBytes = fi.readLine(line.rawData(),maxLineLen);
    if (numBytes<=0)
    {
      break;
    }
    line.resize(numBytes+1);
    if (line.find("LATEX_PDF_SIZE") != -1)
    {
      double scale = (width > height ? width : height)/double(MAX_LATEX_GRAPH_INCH);
      t << "  size=\""<<width/scale << "," <<height/scale <<"\";\n";
    }
    else
      t << line;
  }
  fi.close();
  fo.close();
  // remove temporary file
  QDir::current().remove(tmpName);
  return TRUE;
}

bool DotRunner::readBoundingBox(const char *fileName,int *width,int *height,bool isEps)
{
  const char *bb = isEps ? "%%PageBoundingBox:" : "/MediaBox [";
  int bblen = strlen(bb);
  FILE *f = Portable::fopen(fileName,"rb");
  if (!f) 
  {
    //printf("readBoundingBox: could not open %s\n",fileName);
    return FALSE;
  }
  const int maxLineLen=1024;
  char buf[maxLineLen];
  while (fgets(buf,maxLineLen,f)!=NULL)
  {
     const char *p = strstr(buf,bb);
     if (p) // found PageBoundingBox or /MediaBox string
     {
       int x,y;
       fclose(f);
       if (sscanf(p+bblen,"%d %d %d %d",&x,&y,width,height)!=4)
       {
         //printf("readBoundingBox sscanf fail\n");
         return FALSE;
       }
       return TRUE;
     }
  }
  err("Failed to extract bounding box from generated diagram file %s\n",fileName);
  fclose(f);
  return FALSE;
}

//---------------------------------------------------------------------------------

DotRunner::DotRunner(const QCString& absDotName, const QCString& md5Hash)
  : m_file(absDotName), m_md5Hash(md5Hash), m_cleanUp(Config_getBool(DOT_CLEANUP)),
    m_dotExe(Config_getString(DOT_PATH)+"dot")
{
  m_jobs.setAutoDelete(TRUE);
}

void DotRunner::addJob(const char *format,const char *output)
{
  QListIterator<DotJob> li(m_jobs);
  DotJob *s;
  for (li.toFirst(); (s = li.current()); ++li)
  {
    if (qstrcmp(s->format.data(), format) != 0) continue;
    if (qstrcmp(s->output.data(), output) != 0) continue;
    // we have this job already
    return;
  }
  QCString args = QCString("-T")+format+" -o \""+output+"\"";
  m_jobs.append(new DotJob(format, output, args));
}

QCString getBaseNameOfOutput(QCString const& output)
{
  int index = output.findRev('.');
  if (index < 0) return output;
  return output.left(index);
}

bool DotRunner::run()
{
  int exitCode=0;

  QCString dotArgs;
  QListIterator<DotJob> li(m_jobs);
  DotJob *s;

  // create output
  if (Config_getBool(DOT_MULTI_TARGETS))
  {
    dotArgs=QCString("\"")+m_file.data()+"\"";
    for (li.toFirst();(s=li.current());++li)
    {
      dotArgs+=' ';
      dotArgs+=s->args.data();
    }
    if ((exitCode=Portable::system(m_dotExe.data(),dotArgs,FALSE))!=0) goto error;
  }
  else
  {
    for (li.toFirst();(s=li.current());++li)
    {
      dotArgs=QCString("\"")+m_file.data()+"\" "+s->args.data();
      if ((exitCode=Portable::system(m_dotExe.data(),dotArgs,FALSE))!=0) goto error;
    }
  }

  // check output
  // As there should be only one pdf file be generated, we don't need code for regenerating multiple pdf files in one call
  for (li.toFirst();(s=li.current());++li)
  {
    if (qstrncmp(s->format.data(), "pdf", 3) == 0)
    {
      int width=0,height=0;
      if (!readBoundingBox(s->output.data(),&width,&height,FALSE)) goto error;
      if ((width > MAX_LATEX_GRAPH_SIZE) || (height > MAX_LATEX_GRAPH_SIZE))
      {
        if (!resetPDFSize(width,height,getBaseNameOfOutput(s->output.data()))) goto error;
        dotArgs=QCString("\"")+m_file.data()+"\" "+s->args.data();
        if ((exitCode=Portable::system(m_dotExe.data(),dotArgs,FALSE))!=0) goto error;
      }
    }

    if (qstrncmp(s->format.data(), "png", 3) == 0)
    {
      checkPngResult(s->output.data());
    }
  }

  // remove .dot files
  if (m_cleanUp) 
  {
    //printf("removing dot file %s\n",m_file.data());
    Portable::unlink(m_file.data());
  }

  // create checksum file
  if (!m_md5Hash.isEmpty()) 
  {
    QCString md5Name = getBaseNameOfOutput(m_file.data()) + ".md5";
    FILE *f = Portable::fopen(md5Name,"w");
    if (f)
    {
      fwrite(m_md5Hash.data(),1,32,f); 
      fclose(f);
    }
  }
  return TRUE;
error:
  err("Problems running dot: exit code=%d, command='%s', arguments='%s'\n",
    exitCode,m_dotExe.data(),dotArgs.data());
  return FALSE;
}


//--------------------------------------------------------------------

void DotRunnerQueue::enqueue(DotRunner *runner)
{
  QMutexLocker locker(&m_mutex);
  m_queue.enqueue(runner);
  m_bufferNotEmpty.wakeAll();
}

DotRunner *DotRunnerQueue::dequeue()
{
  QMutexLocker locker(&m_mutex);
  while (m_queue.isEmpty())
  {
    // wait until something is added to the queue
    m_bufferNotEmpty.wait(&m_mutex);
  }
  DotRunner *result = m_queue.dequeue();
  return result;
}

uint DotRunnerQueue::count() const
{
  QMutexLocker locker(&m_mutex);
  return m_queue.count();
}

//--------------------------------------------------------------------

DotWorkerThread::DotWorkerThread(DotRunnerQueue *queue)
  : m_queue(queue)
{
}

void DotWorkerThread::run()
{
  DotRunner *runner;
  while ((runner=m_queue->dequeue()))
  {
    runner->run();
  }
}