/****************************************************************************** * * $Id$ * * Copyright (C) 1997-1999 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. * * All output generated with Doxygen is not covered by this license. * */ #include #include #include #include #include #include #include "formula.h" #include "image.h" #include "util.h" #include "message.h" Formula::Formula(const char *text) { static int count=0; number = count++; form=text; } Formula::~Formula() { } int Formula::getId() { return number; } void FormulaList::generateBitmaps(const char *path) { int x1,y1,x2,y2; QDir d(path); // store the original directory if (!d.exists()) { err("Error: Output dir %s does not exist!\n",path); exit(1); } QString oldDir = QDir::currentDirPath().copy(); // goto the html output directory (i.e. path) QDir::setCurrent(d.absPath()); QDir thisDir; // generate a latex file containing one formula per page. QString texName="_formulas.tex"; QList pagesToGenerate; pagesToGenerate.setAutoDelete(TRUE); FormulaListIterator fli(*this); Formula *formula; QFile f(texName); if (f.open(IO_WriteOnly)) { QTextStream t(&f); t << "\\documentclass{article}" << endl; t << "\\usepackage{epsf}" << endl; // for those who want to include images t << "\\pagestyle{empty}" << endl; t << "\\begin{document}" << endl; int page=0; for (fli.toFirst();(formula=fli.current());++fli) { QString resultName; resultName.sprintf("form-%d.gif",formula->getId()); // only formulas for which no image exists are generated QFileInfo fi(resultName); if (!fi.exists()) { // we force a pagebreak after each formula t << formula->getFormulaText() << endl << "\\pagebreak\n\n"; pagesToGenerate.append(new int(page)); } page++; } t << "\\end{document}" << endl; f.close(); } if (pagesToGenerate.count()>0) // there are new formulas { //printf("Running latex...\n"); //system("latex _formulas.tex /dev/null"); system("latex _formulas.tex"); //printf("Running dvips...\n"); QListIterator pli(pagesToGenerate); int *pagePtr; int pageIndex=1; for (;(pagePtr=pli.current());++pli,++pageIndex) { int pageNum=*pagePtr; msg("Generating image form-%d.gif for formula\n",pageNum); char dviCmd[256]; QString formBase; formBase.sprintf("_form%d",pageNum); // run dvips to convert the page with number pageIndex to an // encapsulated postscript. sprintf(dviCmd,"dvips -q -D 600 -E -n 1 -p %d -o %s.eps _formulas.dvi", pageIndex,formBase.data()); system(dviCmd); // now we read the generated postscript file to extract the bounding box QFileInfo fi(formBase+".eps"); if (fi.exists()) { QString eps = fileToString(formBase+".eps"); int i=eps.find("%%BoundingBox:"); if (i!=-1) { sscanf(eps.data()+i,"%%%%BoundingBox:%d %d %d %d",&x1,&y1,&x2,&y2); } else { err("Error: Couldn't extract bounding box!\n"); } } // next we generate a postscript file which contains the eps // and displays it in the right colors and the right bounding box f.setName(formBase+".ps"); if (f.open(IO_WriteOnly)) { QTextStream t(&f); t << "1 1 1 setrgbcolor" << endl; // anti-alias to white background t << "newpath" << endl; t << "-1 -1 moveto" << endl; t << x2-x1+2 << " -1 lineto" << endl; t << x2-x1+2 << " " << y2-y1+2 << " lineto" << endl; t << "-1 " << y2-y1+2 << " lineto" <0 && imageY>0) { //printf("Converting image...\n"); char *data = new char[imageX*imageY*3]; // rgb 8:8:8 format uint i,x,y,ix,iy; f.readBlock(data,imageX*imageY*3); Image srcImage(imageX,imageY), filteredImage(imageX,imageY), dstImage(imageX/4,imageY/4); uchar *ps=srcImage.getData(); // convert image to black (1) and white (0) index. for (i=0;igetId() << ":" << formula->getFormulaText() << endl; } f.close(); } // reset the directory to the original location. QDir::setCurrent(oldDir); } #ifdef FORMULA_TEST int main() { FormulaList fl; fl.append(new Formula("$x^2$")); fl.append(new Formula("$y^2$")); fl.append(new Formula("$\\sqrt{x_0^2+x_1^2+x_2^2}$")); fl.generateBitmaps("dest"); return 0; } #endif