Skip to content

Commit 0ec121f

Browse files
committed
Added gr_mathtex function (LaTeX equation support)
1 parent 717cfc7 commit 0ec121f

File tree

10 files changed

+459
-3
lines changed

10 files changed

+459
-3
lines changed

lib/gr/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
INCDIR = $(GRDIR)/include
55

66
GROBJS = gr.o text.o contour.o spline.o strlib.o property.o image.o \
7-
grforbnd.o
7+
md5.o grforbnd.o
88
DEFINES =
99
INCLUDES = -I../gks -I../jpeg -I../png
1010
CC = cc
@@ -70,7 +70,7 @@ clean:
7070

7171
depend:
7272
makedepend -Y -- gr.c text.c contour.c spline.c strlib.c property.c \
73-
image.c grforbnd.c 2> /dev/null
73+
image.c md5.c grforbnd.c 2> /dev/null
7474

7575
# DO NOT DELETE THIS LINE -- make depend depends on it.
7676

@@ -80,4 +80,5 @@ spline.o: spline.h
8080
strlib.o: strlib.h
8181
property.o: property.h
8282
image.o: gr.h
83+
md5.o: md5.h
8384
grforbnd.o: gr.h

lib/gr/gr.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "contour.h"
2121
#include "strlib.h"
2222
#include "property.h"
23+
#include "md5.h"
2324

2425
typedef struct
2526
{
@@ -134,6 +135,9 @@ FILE *stream;
134135
#define GR_HEADER "<?xml version='1.0' encoding='ISO-8859-1'?>\n<gr>\n"
135136
#define GR_TRAILER "</gr>\n"
136137

138+
#define nominalWindowHeight 500
139+
#define qualityFactor 4
140+
137141
typedef enum
138142
{
139143
OPTION_LINES, OPTION_MESH, OPTION_FILLED_MESH, OPTION_Z_SHADED_MESH,
@@ -4948,6 +4952,107 @@ void gr_endgraphics(void)
49484952
}
49494953
}
49504954

4955+
static
4956+
void latex2image(char *string, int pointSize, float *rgb,
4957+
int *width, int *height, int **data)
4958+
{
4959+
int color;
4960+
char s[FILENAME_MAX], path[FILENAME_MAX], cache[33];
4961+
char *tmp, *null, cmd[1024];
4962+
char tex[FILENAME_MAX], dvi[FILENAME_MAX], png[FILENAME_MAX];
4963+
FILE *stream;
4964+
4965+
color = ((int)(rgb[0] / 255) ) +
4966+
((int)(rgb[1] / 255) << 8) +
4967+
((int)(rgb[2] / 255) << 16);
4968+
sprintf(s, "%d%x%s", pointSize, color, string);
4969+
md5(s, cache);
4970+
#ifdef WIN32
4971+
sprintf(path, "C:\\TEMP\\gr-cache-%s.png", cache);
4972+
#else
4973+
sprintf(path, "/tmp/gr-cache-%s.png", cache);
4974+
#endif
4975+
4976+
if (access(path, R_OK) != 0)
4977+
{
4978+
tmp = tempnam(".", NULL);
4979+
sprintf(tex, "%s.tex", tmp);
4980+
sprintf(dvi, "%s.dvi", tmp);
4981+
sprintf(png, "%s.png", tmp);
4982+
#ifdef _WN32
4983+
null = "NUL";
4984+
#else
4985+
null = "/dev/null";
4986+
#endif
4987+
4988+
stream = fopen(tex, "w");
4989+
fprintf(stream, "\
4990+
\\documentclass{article}\n\
4991+
\\pagestyle{empty}\n\
4992+
\\usepackage[dvips]{color}\n\
4993+
\\color[rgb]{%.3f,%.3f,%.3f}\n\
4994+
\\begin{document}\n\
4995+
\\[\n", rgb[0], rgb[1], rgb[2]);
4996+
fwrite(string, strlen(string), 1, stream);
4997+
fprintf(stream, "\n\
4998+
\\]\n\
4999+
\\end{document}");
5000+
fclose(stream);
5001+
5002+
sprintf(cmd, "latex -interaction=batchmode -halt-on-error %s -o %s >%s",
5003+
tex, dvi, null);
5004+
system(cmd);
5005+
5006+
if (access(dvi, R_OK) == 0)
5007+
{
5008+
sprintf(cmd, "dvipng -q -T tight -x %d %s -o %s >%s",
5009+
pointSize * 100, dvi, png, null);
5010+
system(cmd);
5011+
5012+
rename(png, path);
5013+
5014+
sprintf(cmd, "rm -f %s.*", tmp);
5015+
system(cmd);
5016+
}
5017+
}
5018+
5019+
if (access(path, R_OK) == 0)
5020+
gr_readimage(path, width, height, data);
5021+
}
5022+
5023+
void gr_mathtex(float x, float y, char *string)
5024+
{
5025+
int errind, pointSize, color;
5026+
float chh, rgb[3];
5027+
int width, height, *data = NULL;
5028+
float w, h, xmin, xmax, ymin, ymax;
5029+
5030+
check_autoinit;
5031+
5032+
gks_inq_text_height(&errind, &chh);
5033+
pointSize = chh * qualityFactor * nominalWindowHeight;
5034+
5035+
gks_inq_text_color_index(&errind, &color);
5036+
gks_inq_rgb(color, &rgb[0], &rgb[1], &rgb[2]);
5037+
5038+
latex2image(string, pointSize, rgb, &width, &height, &data);
5039+
5040+
if (data != NULL)
5041+
{
5042+
w = width / (float) (qualityFactor * nominalWindowHeight);
5043+
h = height / (float) (qualityFactor * nominalWindowHeight);
5044+
xmin = x - 0.5 * w;
5045+
xmax = x + 0.5 * w;
5046+
ymin = y - 0.5 * h;
5047+
ymax = y + 0.5 * h;
5048+
5049+
gr_selntran(0);
5050+
gr_drawimage(xmin, xmax, ymin, ymax, width, height, data);
5051+
5052+
free(data);
5053+
}
5054+
}
5055+
49515056
void gr_beginselection(int index, int type)
49525057
{
49535058
check_autoinit;

lib/gr/gr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ DLLEXPORT void gr_settransparency(float);
116116
DLLEXPORT void gr_setcoordxform(float [3][2]);
117117
DLLEXPORT void gr_begingraphics(char *);
118118
DLLEXPORT void gr_endgraphics(void);
119+
DLLEXPORT void gr_mathtex(float, float, char *);
119120
DLLEXPORT void gr_beginselection(int, int);
120121
DLLEXPORT void gr_endselection(void);
121122
DLLEXPORT void gr_moveselection(float, float);

lib/gr/grforbnd.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,17 @@ void FORTRAN(gr_endgraphics)(void)
540540
{
541541
gr_endgraphics();
542542
}
543+
544+
void FORTRAN(gr_mathtex)(
545+
float *x, float *y, char *string, unsigned char string_len)
546+
{
547+
char *_string;
548+
549+
_string = (char *) calloc(1, sizeof(char) * string_len);
550+
strncpy(_string, string, string_len);
551+
552+
gr_mathtex(*x, *y, _string);
553+
554+
free(_string);
555+
}
556+

lib/gr/libgr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ EXPORTS
3737
gr_inqtextext
3838
gr_moveselection
3939
gr_ndctowc
40+
gr_mathtex
4041
gr_opengks
4142
gr_openws
4243
gr_polyline

lib/gr/makefile.vc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $(ZLIBS):
4646
libgr.lib: libgr.dll
4747

4848
libgr.dll: gr.obj text.obj contour.obj spline.obj strlib.obj property.obj \
49-
image.obj
49+
image.obj md5.obj
5050
$(DLLLINK) /out:$@ $** -def:libgr.def \
5151
$(JPEGLIBS) $(PNGLIBS) $(ZLIBS) $(GKSLIBS) $(DLLLFLAGS) $(DLLLIBS)
5252

0 commit comments

Comments
 (0)