2 * Grace - GRaphing, Advanced Computation and Exploration of data
4 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
6 * Copyright (c) 1996-2003 Grace Development Team
8 * Maintained by Evgeny Stambulchik
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Driver for the Grace Metafile format
37 #define CANVAS_BACKEND_API
38 #include "grace/canvas.h"
43 int register_mf_drv(Canvas
*canvas
)
47 d
= device_new("Metafile", DEVICE_FILE
, TRUE
, NULL
, NULL
);
52 device_set_fext(d
, "gmf");
68 return register_device(canvas
, d
);
71 int mf_initgraphics(const Canvas
*canvas
, void *data
, const CanvasStats
*cstats
)
75 FILE *prstream
= canvas_get_prstream(canvas
);
77 fprintf(prstream
, "#GMF-%s\n", GMF_VERSION
);
79 fprintf(prstream
, "FontResources {\n");
80 for (i
= 0; i
< cstats
->nfonts
; i
++) {
81 int font
= cstats
->fonts
[i
].font
;
82 fprintf(prstream
, "\t( %d , \"%s\" , \"%s\" )\n",
83 font
, get_fontalias(canvas
, font
), get_fontname(canvas
, font
));
85 fprintf(prstream
, "}\n");
87 fprintf(prstream
, "ColorResources {\n");
88 for (i
= 0; i
< cstats
->ncolors
; i
++) {
89 int cindex
= cstats
->colors
[i
];
91 get_rgb(canvas
, cindex
, &rgb
);
92 fprintf(prstream
, "\t( %d , %d , %d , %d )\n",
93 cindex
, rgb
.red
, rgb
.green
, rgb
.blue
);
95 fprintf(prstream
, "}\n");
97 fprintf(prstream
, "PatternResources {\n");
98 for (i
= 0; i
< cstats
->npatterns
; i
++) {
99 int patno
= cstats
->patterns
[i
];
100 Pattern
*pat
= canvas_get_pattern(canvas
, patno
);
101 fprintf(prstream
, "\t( %d , ", patno
);
102 for (j
= 0; j
< pat
->width
*pat
->height
/8; j
++) {
103 fprintf(prstream
, "%02x", pat
->bits
[j
]);
105 fprintf(prstream
, " )\n");
107 fprintf(prstream
, "}\n");
109 fprintf(prstream
, "DashResources {\n");
110 for (i
= 0; i
< cstats
->nlinestyles
; i
++) {
111 int lines
= cstats
->linestyles
[i
];
112 LineStyle
*ls
= canvas_get_linestyle(canvas
, lines
);
113 fprintf(prstream
, "\t( %d , [ ", lines
);
114 for (j
= 0; j
< ls
->length
; j
++) {
115 fprintf(prstream
, "%d ", ls
->array
[j
]);
117 fprintf(prstream
, "] )\n");
119 fprintf(prstream
, "}\n");
121 pg
= get_page_geometry(canvas
);
122 fprintf(prstream
, "InitGraphics { %.4f %ld %ld }\n",
123 pg
->dpi
, pg
->width
, pg
->height
);
125 return RETURN_SUCCESS
;
128 void mf_setpen(const Canvas
*canvas
)
131 FILE *prstream
= canvas_get_prstream(canvas
);
133 getpen(canvas
, &pen
);
134 fprintf(prstream
, "SetPen { %d %d }\n", pen
.color
, pen
.pattern
);
137 void mf_setdrawbrush(const Canvas
*canvas
)
139 FILE *prstream
= canvas_get_prstream(canvas
);
140 fprintf(prstream
, "SetLineWidth { %.4f }\n", getlinewidth(canvas
));
141 fprintf(prstream
, "SetLineStyle { %d }\n", getlinestyle(canvas
));
144 void mf_drawpixel(const Canvas
*canvas
, void *data
, const VPoint
*vp
)
146 FILE *prstream
= canvas_get_prstream(canvas
);
149 fprintf(prstream
, "DrawPixel { ( %.4f , %.4f ) }\n", vp
->x
, vp
->y
);
152 void mf_drawpolyline(const Canvas
*canvas
, void *data
,
153 const VPoint
*vps
, int n
, int mode
)
156 FILE *prstream
= canvas_get_prstream(canvas
);
159 mf_setdrawbrush(canvas
);
161 fprintf(prstream
, "DrawPolyline {\n");
162 if (mode
== POLYLINE_CLOSED
) {
163 fprintf(prstream
, "\tClosed\n");
165 fprintf(prstream
, "\tOpen\n");
167 for (i
= 0; i
< n
; i
++) {
168 fprintf(prstream
, "\t( %.4f , %.4f )\n", vps
[i
].x
, vps
[i
].y
);
170 fprintf(prstream
, "}\n");
173 void mf_fillpolygon(const Canvas
*canvas
, void *data
,
174 const VPoint
*vps
, int nc
)
177 FILE *prstream
= canvas_get_prstream(canvas
);
181 fprintf(prstream
, "FillPolygon {\n");
182 for (i
= 0; i
< nc
; i
++) {
183 fprintf(prstream
, "\t( %.4f , %.4f )\n", vps
[i
].x
, vps
[i
].y
);
185 fprintf(prstream
, "}\n");
188 void mf_drawarc(const Canvas
*canvas
, void *data
,
189 const VPoint
*vp1
, const VPoint
*vp2
, double a1
, double a2
)
191 FILE *prstream
= canvas_get_prstream(canvas
);
193 mf_setdrawbrush(canvas
);
196 "DrawArc { ( %.4f , %.4f ) ( %.4f , %.4f ) %.4f %.4f }\n",
197 vp1
->x
, vp1
->y
, vp2
->x
, vp2
->y
, a1
, a2
);
200 void mf_fillarc(const Canvas
*canvas
, void *data
,
201 const VPoint
*vp1
, const VPoint
*vp2
, double a1
, double a2
, int mode
)
204 FILE *prstream
= canvas_get_prstream(canvas
);
209 if (mode
== ARCFILL_CHORD
) {
212 name
= "FillPieSlice";
214 fprintf(prstream
, "%s { ( %.4f , %.4f ) ( %.4f , %.4f ) %.4f %.4f }\n",
215 name
, vp1
->x
, vp1
->y
, vp2
->x
, vp2
->y
, a1
, a2
);
218 void mf_putpixmap(const Canvas
*canvas
, void *data
,
219 const VPoint
*vp
, const CPixmap
*pm
)
225 FILE *prstream
= canvas_get_prstream(canvas
);
228 strcpy(buf
, "Bitmap");
230 strcpy(buf
, "Pixmap");
232 fprintf(prstream
, "Put%s {\n", buf
);
234 if (pm
->type
== PIXMAP_TRANSPARENT
) {
235 strcpy(buf
, "Transparent");
237 strcpy(buf
, "Opaque");
240 fprintf(prstream
, "\t( %.4f , %.4f ) %dx%d %s\n",
241 vp
->x
, vp
->y
, pm
->width
, pm
->height
, buf
);
243 for (k
= 0; k
< pm
->height
; k
++) {
244 fprintf(prstream
, "\t");
245 for (j
= 0; j
< pm
->width
; j
++) {
246 fprintf(prstream
, "%02x", (pm
->bits
)[k
*pm
->width
+j
]);
248 fprintf(prstream
, "\n");
251 paddedW
= PADBITS(pm
->width
, pm
->pad
);
252 for (k
= 0; k
< pm
->height
; k
++) {
253 fprintf(prstream
, "\t");
254 for (j
= 0; j
< paddedW
/pm
->pad
; j
++) {
255 for (i
= 0; i
< pm
->pad
; i
++) {
256 bit
= bin_dump(&pm
->bits
[k
*paddedW
/pm
->pad
+ j
], i
, pm
->pad
);
258 fprintf(prstream
, "X");
260 fprintf(prstream
, ".");
264 fprintf(prstream
, "\n");
268 fprintf(prstream
, "}\n");
271 void mf_puttext(const Canvas
*canvas
, void *data
,
272 const VPoint
*vp
, const char *s
, int len
, int font
, const TextMatrix
*tm
,
273 int underline
, int overline
, int kerning
)
276 FILE *prstream
= canvas_get_prstream(canvas
);
280 fprintf(prstream
, "PutText {\n");
281 fprintf(prstream
, "\t( %.4f , %.4f )\n", vp
->x
, vp
->y
);
283 fprintf(prstream
, "\t %d %.4f %.4f %.4f %.4f %d %d %d %d \"",
285 tm
->cxx
, tm
->cxy
, tm
->cyx
, tm
->cyy
,
286 underline
, overline
, kerning
, len
);
287 for (i
= 0; i
< len
; i
++) {
288 fputc(s
[i
], prstream
);
290 fprintf(prstream
, "\"\n");
292 fprintf(prstream
, "}\n");
295 void mf_leavegraphics(const Canvas
*canvas
, void *data
,
296 const CanvasStats
*cstats
)
298 FILE *prstream
= canvas_get_prstream(canvas
);
299 view v
= cstats
->bbox
;
301 fprintf(prstream
, "LeaveGraphics { %.4f %.4f %.4f %.4f }\n",
302 v
.xv1
, v
.yv1
, v
.xv2
, v
.yv2
);