summaryrefslogtreecommitdiffstats
path: root/src/kile/symbols/gesymb.cpp
blob: 4254145c0e2c18c6b222565d9502061da28e4ee5 (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
/***************************************************************************
    begin                : Mon April 17 2006
    edit:		 : Sat March 29 2008
    copyright            : (C) 2006-2008 by Thomas Braun
    email                : braun@physik.fu-berlin.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "gesymb.h"

using std::cout;

int main( int argc, char ** argv )
{
int latexret=-1;
int dvipngret=-1;
TQString texcommand, texfile,type,dvipngcommand,line;
TQString pkgs,pkgsarg,savepkgs="",savepkgsarg="";
bool env=false;


if (argc < 2)
	usage();


texfile=argv[2];
type=argv[1];

TQFile f( texfile );
if ( !f.open( IO_ReadOnly ) )
{
	cout << "File " << texfile.latin1() << " is not readable\n";
	return 1;
}

texcommand="latex " + texfile;
dvipngcommand="dvipng  --picky -bg Transparent -x 518 -O -1.2in,-1.2in -T bbox -D 300 -o img%03d" + type + ".png " + texfile.left(texfile.length()-4);

cout << texcommand.latin1() << "\n";
cout << dvipngcommand.latin1() << "\n";

latexret = system(texcommand.latin1());
dvipngret= system(dvipngcommand.latin1());

if (latexret)
{
	cout << "Error compiling the latex file\n";
	return 1;
}
if(dvipngret)
{ 
	cout << "Error producing the pngs\n";
	return 1;
}

TQTextStream t(&f);

TQRegExp optarg("\\\\(?:math)?command\\[(.*)\\]\\{");
TQRegExp arg("\\\\(?:math)?command\\{(.*)\\}");
TQRegExp beginenvpkgs("^\\\\begin\\{neededpkgs\\}(?:\\[(.*)\\])?\\{(.*)\\}");
TQRegExp endenvpkgs("^\\\\end\\{neededpkgs\\}");
TQRegExp cmdpkgs("\\\\pkgs(?:\\[(.*)\\])?\\{(.*)\\}");
TQRegExp	comment("^\\s*%");

int number=1;
beginenvpkgs.setMinimal(true);
cmdpkgs.setMinimal(true);
// optarg.setMinimal(true); in some cases necessary

while( (line = t.readLine()) != 0L)
{
	if( line.tqfind(comment) != -1 )
		continue;
	if(env)
	{
		pkgs=savepkgs;
		pkgsarg=savepkgsarg;
	}
	else
	{
		pkgs="";
		pkgsarg="";
	}

	if ( line.tqfind(beginenvpkgs) != -1)
	{
		env=true;
		pkgs=beginenvpkgs.cap(2);
		pkgsarg=beginenvpkgs.cap(1);
		savepkgs=pkgs;
		savepkgsarg=pkgsarg;
	}
	else if( line.tqfind(cmdpkgs) != -1)
	{
		pkgs=cmdpkgs.cap(2);
		pkgsarg=cmdpkgs.cap(1);
	}
	else if( line.tqfind(endenvpkgs) != -1)
	{
		env=false;
		savepkgs="";
		savepkgsarg="";
		pkgs="";
		pkgsarg="";
	}

	cout << "line is " << line.latin1();
	cout << "; pkgs=" << pkgs.latin1() << " ,pkgsarg=" << pkgsarg.latin1() << " ,savepkgs=" << savepkgs.latin1() << " ,savepkgsarg=" << savepkgsarg.latin1() << "\n";

	if ( line.tqfind(optarg) != -1)
		writeComment(optarg.cap(1),pkgs,pkgsarg,type,number++);
	else if(line.tqfind(arg) != -1)
		writeComment(arg.cap(1),pkgs,pkgsarg,type,number++);
}

f.close();
return 0;
}

void usage()
{
	cout << "Usage:\n";
	cout << "gesymb <type> <latex-file>\n";
	exit(1);
}

void writeComment(TQString cmd, TQString pkgs, TQString pkgsarg, TQString type, int number)
{

TQImage image;
TQString fname;
fname.sprintf("img%03d%s.png",number,type.latin1());

cout << "fname is " << fname.latin1() << "\n";

if(image.load(fname))
{
	image.setText("Command",0,cmd);
	image.setText("Packages",0, ( pkgsarg.isEmpty() ? "" : '[' + pkgsarg + ']' ) + ( pkgs.isEmpty() ? "" : '{' + pkgs + '}') );
	
	if(!image.save(fname,"PNG"))
	{
		cout << "Image " << fname.latin1() << " could not be saved\n";
		exit(1);
	}
	readComment(fname);
}
else
	cout << "===writeComment=== ERROR " << fname.latin1() << " could not be loaded\n";

}

void readComment(TQString fname)
{
TQImage image;

if(image.load(fname))
{
	cout << "image " << fname.latin1()  << " has Command comment_" <<  TQString(image.text("Command")).latin1() << "_\n";
	cout << "image " << fname.latin1()  << " has Packages comment_" <<  TQString(image.text("Packages")).latin1() << "_\n";
	
}
else
	cout << "===readComment=== ERROR " << fname.latin1() << " could not be loaded\n";
}