|
20 | 20 | #include "contour.h" |
21 | 21 | #include "strlib.h" |
22 | 22 | #include "property.h" |
| 23 | +#include "md5.h" |
23 | 24 |
|
24 | 25 | typedef struct |
25 | 26 | { |
@@ -134,6 +135,9 @@ FILE *stream; |
134 | 135 | #define GR_HEADER "<?xml version='1.0' encoding='ISO-8859-1'?>\n<gr>\n" |
135 | 136 | #define GR_TRAILER "</gr>\n" |
136 | 137 |
|
| 138 | +#define nominalWindowHeight 500 |
| 139 | +#define qualityFactor 4 |
| 140 | + |
137 | 141 | typedef enum |
138 | 142 | { |
139 | 143 | OPTION_LINES, OPTION_MESH, OPTION_FILLED_MESH, OPTION_Z_SHADED_MESH, |
@@ -4948,6 +4952,107 @@ void gr_endgraphics(void) |
4948 | 4952 | } |
4949 | 4953 | } |
4950 | 4954 |
|
| 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 | + |
4951 | 5056 | void gr_beginselection(int index, int type) |
4952 | 5057 | { |
4953 | 5058 | check_autoinit; |
|
0 commit comments