19 #include <qfileinfo.h>
20 #include <qtextstream.h>
54 if (!d.exists()) {
err(
"Output dir %s does not exist!\n",path); exit(1); }
55 QCString oldDir = QDir::currentDirPath().utf8();
57 QDir::setCurrent(d.absPath());
60 QCString texName=
"_formulas.tex";
61 QList<int> pagesToGenerate;
62 pagesToGenerate.setAutoDelete(TRUE);
66 bool formulaError=FALSE;
67 if (f.open(IO_WriteOnly))
71 t <<
"\\documentclass{article}" <<
endl;
72 t <<
"\\usepackage{epsfig}" <<
endl;
74 t <<
"\\pagestyle{empty}" <<
endl;
75 t <<
"\\begin{document}" <<
endl;
77 for (fli.toFirst();(formula=fli.current());++fli)
80 resultName.sprintf(
"form_%d.png",formula->
getId());
82 QFileInfo fi(resultName);
87 pagesToGenerate.append(
new int(page));
92 t <<
"\\end{document}" <<
endl;
95 if (pagesToGenerate.count()>0)
100 if (latexCmd.isEmpty()) latexCmd=
"latex";
104 err(
"Problems running latex. Check your installation or look "
105 "for typos in _formulas.tex and check _formulas.log!\n");
111 QListIterator<int> pli(pagesToGenerate);
114 for (;(pagePtr=pli.current());++pli,++pageIndex)
116 int pageNum=*pagePtr;
117 msg(
"Generating image form_%d.png for formula\n",pageNum);
120 formBase.sprintf(
"_form%d",pageNum);
123 sprintf(dviArgs,
"-q -D 600 -E -n 1 -p %d -o %s.eps _formulas.dvi",
124 pageIndex,formBase.data());
128 err(
"Problems running dvips. Check your installation!\n");
130 QDir::setCurrent(oldDir);
135 QFileInfo fi(formBase+
".eps");
139 int i=eps.find(
"%%BoundingBox:");
142 sscanf(eps.data()+i,
"%%%%BoundingBox:%d %d %d %d",&x1,&y1,&x2,&y2);
146 err(
"Couldn't extract bounding box!\n");
151 f.setName(formBase+
".ps");
152 if (f.open(IO_WriteOnly))
155 t <<
"1 1 1 setrgbcolor" <<
endl;
156 t <<
"newpath" <<
endl;
157 t <<
"-1 -1 moveto" <<
endl;
158 t << (x2-x1+2) <<
" -1 lineto" << endl;
159 t << (x2-x1+2) <<
" " << (y2-y1+2) <<
" lineto" <<
endl;
160 t <<
"-1 " << (y2-y1+2) <<
" lineto" <<endl;
161 t <<
"closepath" <<
endl;
163 t << -x1 <<
" " << -y1 <<
" translate" <<
endl;
164 t <<
"0 0 0 setrgbcolor" <<
endl;
165 t <<
"(" << formBase <<
".eps) run" <<
endl;
170 double scaleFactor = 16.0/3.0;
172 if (zoomFactor<8 || zoomFactor>50) zoomFactor=10;
173 scaleFactor *= zoomFactor/10.0;
174 int gx = (((int)((x2-x1)*scaleFactor))+3)&~1;
175 int gy = (((int)((y2-y1)*scaleFactor))+3)&~1;
181 sprintf(gsArgs,
"-q -g%dx%d -r%dx%dx -sDEVICE=ppmraw "
182 "-sOutputFile=%s.pnm -dNOPAUSE -dBATCH -- %s.ps",
183 gx,gy,(
int)(scaleFactor*72),(
int)(scaleFactor*72),
184 formBase.data(),formBase.data()
191 QDir::setCurrent(oldDir);
195 f.setName(formBase+
".pnm");
196 uint imageX=0,imageY=0;
198 if (f.open(IO_ReadOnly))
203 s=t.readLine().utf8();
204 if (s.length()<2 || s.left(2)!=
"P6")
205 err(
"ghostscript produced an illegal image format!");
210 while (!t.eof() && (s=t.readLine().utf8()) && !s.isEmpty() && s.at(0)==
'#') { }
211 sscanf(s,
"%d %d",&imageX,&imageY);
213 if (imageX>0 && imageY>0)
216 char *data =
new char[imageX*imageY*3];
218 f.readBlock(data,imageX*imageY*3);
219 Image srcImage(imageX,imageY),
220 filteredImage(imageX,imageY),
221 dstImage(imageX/4,imageY/4);
222 uchar *ps=srcImage.getData();
224 for (i=0;i<imageX*imageY;i++) *ps++= (data[i*3]==0 ? 1 : 0);
226 static int filterMask[]={1,2,1,2,8,2,1,2,1};
227 for (y=0;y<srcImage.getHeight();y++)
229 for (x=0;x<srcImage.getWidth();x++)
236 s+=srcImage.getPixel(x+ix-1,y+iy-1)*filterMask[iy*3+ix];
239 filteredImage.setPixel(x,y,s);
251 int c=filteredImage.getPixel(xp+0,yp+0)+
252 filteredImage.getPixel(xp+1,yp+0)+
253 filteredImage.getPixel(xp+2,yp+0)+
254 filteredImage.getPixel(xp+3,yp+0)+
255 filteredImage.getPixel(xp+0,yp+1)+
256 filteredImage.getPixel(xp+1,yp+1)+
257 filteredImage.getPixel(xp+2,yp+1)+
258 filteredImage.getPixel(xp+3,yp+1)+
259 filteredImage.getPixel(xp+0,yp+2)+
260 filteredImage.getPixel(xp+1,yp+2)+
261 filteredImage.getPixel(xp+2,yp+2)+
262 filteredImage.getPixel(xp+3,yp+2)+
263 filteredImage.getPixel(xp+0,yp+3)+
264 filteredImage.getPixel(xp+1,yp+3)+
265 filteredImage.getPixel(xp+2,yp+3)+
266 filteredImage.getPixel(xp+3,yp+3);
269 dstImage.
setPixel(x,y,QMIN(15,(c*15)/(16*10)));
274 resultName.sprintf(
"form_%d.png",pageNum);
277 dstImage.
save(resultName,1);
283 thisDir.remove(formBase+
".eps");
284 thisDir.remove(formBase+
".pnm");
285 thisDir.remove(formBase+
".ps");
288 thisDir.remove(
"_formulas.dvi");
289 if (!formulaError) thisDir.remove(
"_formulas.log");
290 thisDir.remove(
"_formulas.aux");
293 if (!formulaError) thisDir.remove(
"_formulas.tex");
298 f.setName(
"formula.repository");
299 if (f.open(IO_WriteOnly))
302 for (fli.toFirst();(formula=fli.current());++fli)
309 QDir::setCurrent(oldDir);
317 fl.append(
new Formula(
"$x^2$"));
318 fl.append(
new Formula(
"$y^2$"));
319 fl.append(
new Formula(
"$\\sqrt{x_0^2+x_1^2+x_2^2}$"));