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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* x11.c: X11 window interface for Metafont, using Xt. Original by
rusty@garnet.berkeley.edu. */
#define EXTERN extern
#include "../mfd.h"
#ifdef X11WIN /* almost whole file */
/* For wchar_t et al., that the X files might want. */
#include <kpathsea/systypes.h>
#undef input /* the XWMHints structure has a field named `input' */
#undef output
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#undef wchar_t
#define PLANE 0
static unsigned int mf_defwidth = 0;
static unsigned int mf_defheight = 0;
static Display *mf_display;
static Window mf_window;
static Pixmap mf_pixmap;
static XtAppContext mf_app;
static GC mf_dgc; /* draw gc */
static GC mf_egc; /* erase gc */
static GC mf_cgc; /* copy plane gc */
typedef struct
{
unsigned int mf_width, mf_height;
Pixel mf_fg, mf_bg;
} mf_resources_struct;
static mf_resources_struct mf_x11_resources;
/* Don't paint anything until we're mapped. */
static Boolean mf_mapped;
#ifdef MF_XT_DEBUG
static int mf_max_x, mf_max_y;
static int mf_min_x, mf_min_y;
static void mf_checkextent ();
#endif
static XtResource mf_resources[]
= { { "width", "Width", XtRInt, sizeof (int),
XtOffset (mf_resources_struct *, mf_width), XtRInt,
(XtPointer) & mf_defwidth },
{ "height", "Height", XtRInt, sizeof (int),
XtOffset (mf_resources_struct *, mf_height), XtRInt,
(XtPointer) &mf_defheight },
{ "foreground", "Foreground", XtRPixel, sizeof (Pixel),
XtOffset (mf_resources_struct *, mf_fg),
XtRString, (XtPointer) "Black" },
{ "background", "Background", XtRPixel, sizeof (Pixel),
XtOffset (mf_resources_struct *, mf_bg), XtRString,
(XtPointer) "White" },
};
/* Maybe someday we'll read options, until then, this is just here for
the resources. */
static XrmOptionDescRec mf_optiondesclist[]
= { { "-width", "width", XrmoptionSepArg, (XPointer) NULL },
{ "-height", "height", XrmoptionSepArg, (XPointer) NULL },
{ "-fg", "foreground", XrmoptionSepArg, (XPointer) NULL },
{ "-bg", "background", XrmoptionSepArg, (XPointer) NULL },
};
static void mf_events ();
static void mf_mapstatus ();
static void mf_newpixmap P2H(unsigned int, unsigned int);
static void mf_redraw ();
static void mf_repaint ();
/* Return 1 if display opened successfully, else 0. */
int
mf_x11_initscreen ()
{
XSetWindowAttributes xwa;
Widget mf_toplevel;
Widget mf_canvas;
XGCValues gcv;
Arg args[1];
int mf_argc;
char *mf_argv[2];
mf_argv[0] = "mf";
mf_argv[1] = NULL;
mf_argc = 1;
mf_toplevel = XtInitialize ("mf", "Metafont",
mf_optiondesclist,
XtNumber (mf_optiondesclist),
&mf_argc, mf_argv);
XtGetApplicationResources (mf_toplevel, (XtPointer) & mf_x11_resources,
mf_resources, XtNumber (mf_resources),
NULL, 0);
if (mf_argc != 1)
{
fprintf (stderr, "Usage: %s\n", mf_argv[0]);
return 0;
}
/* If nothing specified in their resources (e.g., .Xdefaults)
then use the values of Metafont's "screen". */
if (mf_x11_resources.mf_width == 0)
mf_x11_resources.mf_width = screenwidth;
if (mf_x11_resources.mf_height == 0)
mf_x11_resources.mf_height = screendepth;
mf_canvas = XtCreateManagedWidget ("canvas", widgetClass, mf_toplevel,
NULL, 0);
XtSetArg (args[0], XtNwidth, mf_x11_resources.mf_width);
XtSetValues (mf_canvas, args, 1);
XtSetArg (args[0], XtNheight, mf_x11_resources.mf_height);
XtSetValues (mf_canvas, args, 1);
/* for mf_x11_updatescreen() */
mf_app = XtWidgetToApplicationContext (mf_canvas);
XtAddEventHandler (mf_canvas, (Cardinal) ExposureMask, True,
mf_repaint, NULL);
XtAddEventHandler (mf_canvas, (Cardinal) StructureNotifyMask, True,
mf_mapstatus, NULL);
XtRealizeWidget (mf_toplevel);
mf_display = XtDisplay (mf_canvas);
mf_window = XtWindow (mf_canvas);
/* Since Metafont isn't your typical x window program that
sits in XTMainLoop, if the server supports backing store
and save unders this will help keep the output looking
nice. */
xwa.backing_store = Always;
xwa.save_under = True;
XChangeWindowAttributes (mf_display, mf_window,
CWBackingStore | CWSaveUnder, &xwa);
gcv.background = mf_x11_resources.mf_bg;
gcv.foreground = mf_x11_resources.mf_fg;
gcv.function = GXcopy;
/* copy plane gc */
mf_cgc = XCreateGC (mf_display, mf_window,
GCForeground | GCBackground | GCFunction, &gcv);
mf_newpixmap (screenwidth > mf_x11_resources.mf_width
? screenwidth : mf_x11_resources.mf_width,
screendepth > mf_x11_resources.mf_height
? screendepth : mf_x11_resources.mf_height);
return 1;
}
void
mf_x11_updatescreen ()
{
mf_events ();
mf_redraw ();
#ifdef MF_XT_DEBUG
printf ("max_x=%d, min_x=%d, max_y=%d, min_y=%d\n",
mf_max_x, mf_min_x,
mf_max_y, mf_min_y);
#endif
}
void
mf_x11_blankrectangle P4C(screencol, left,
screencol, right,
screenrow, top,
screenrow, bottom)
{
XFillRectangle (mf_display, mf_pixmap, mf_egc, (int) left, (int) top,
(int) (right - left + 1), (int) (bottom - top + 1));
mf_events ();
}
void
mf_x11_paintrow P4C(screenrow, row,
pixelcolor, init_color,
transspec, tvect,
register screencol, vector_size)
{
GC gc;
int col;
gc = (init_color == 0) ? mf_egc : mf_dgc;
do
{
col = *tvect++;
#ifdef MF_XT_DEBUG
mf_checkextent (col, *tvect, row);
#endif /* MF_XT_DEBUG */
XDrawLine (mf_display, mf_pixmap, gc, col, (int) row,
(int) *tvect, (int) row);
gc = (gc == mf_egc) ? mf_dgc : mf_egc;
}
while (--vector_size > 0);
mf_events ();
}
#ifdef MF_XT_DEBUG
static void
mf_checkextent P3C(int, x1, int, x2, int, y)
{
if (x1 < mf_min_x)
mf_min_x = x1;
if (x1 > mf_max_x)
mf_max_x = x1;
if (x2 < mf_min_x)
mf_min_x = x2;
if (x2 > mf_max_x)
mf_max_x = x2;
if (y > mf_max_y)
mf_max_y = y;
if (y < mf_min_y)
mf_min_y = y;
}
#endif /* MF_XT_DEBUG */
static void
mf_events ()
{
XEvent event;
if (XtAppPending (mf_app) != 0)
{
while (XtAppPending (mf_app) != 0)
{
XtAppNextEvent (mf_app, &event);
XtDispatchEvent (&event);
}
}
}
static void
mf_newpixmap P2C(unsigned int, width, unsigned int, height)
{
XGCValues gcv;
Pixmap newpixmap;
/* width == mf_width and height == mf_height
the first time mf_newpixmap() is called.
*/
if (width < mf_x11_resources.mf_width && height < mf_x11_resources.mf_height)
return;
newpixmap = XCreatePixmap (mf_display, mf_window, width, height, 1);
gcv.background = 0;
gcv.foreground = 1;
if (mf_dgc != 0)
XFreeGC (mf_display, mf_dgc);
/* draw gc */
gcv.line_width = 1;
mf_dgc = XCreateGC (mf_display, newpixmap,
GCForeground | GCBackground | GCLineWidth, &gcv);
if (mf_egc != 0)
XFreeGC (mf_display, mf_egc);
/* erase gc */
gcv.foreground = 0;
mf_egc = XCreateGC (mf_display, newpixmap,
GCForeground | GCBackground | GCLineWidth, &gcv);
XFillRectangle (mf_display, newpixmap, mf_egc, 0, 0, width, height);
if (mf_pixmap != 0)
{
XCopyArea (mf_display, mf_pixmap, newpixmap, mf_dgc, 0, 0,
mf_x11_resources.mf_width,
mf_x11_resources.mf_height, 0, 0);
XFreePixmap (mf_display, mf_pixmap);
}
mf_pixmap = newpixmap;
mf_x11_resources.mf_width = width;
mf_x11_resources.mf_height = height;
}
static void
mf_repaint P3C(Widget, w, XtPointer, data, XEvent*, ev)
{
if (!mf_mapped || !ev || ev->type != Expose)
return;
/* We are a ``simple application''. */
if (ev->xexpose.count == 0)
{
XEvent event;
/* skip all excess redraws */
while (XCheckTypedEvent (mf_display, Expose, &event) != False)
continue;
mf_redraw ();
}
}
static void
mf_mapstatus P3C(Widget, w, XtPointer, data, XEvent*, ev)
{
switch (ev->type)
{
case MapNotify:
mf_mapped = True;
break;
case UnmapNotify:
mf_mapped = False;
break;
case ConfigureNotify:
mf_newpixmap (ev->xconfigure.width, ev->xconfigure.height);
mf_redraw ();
break;
}
}
static void
mf_redraw ()
{
XCopyPlane (mf_display, mf_pixmap, mf_window, mf_cgc, 0, 0,
mf_x11_resources.mf_width, mf_x11_resources.mf_height,
0, 0, (unsigned long) 1);
XFlush (mf_display);
}
#else
int x11_dummy;
#endif /* X11WIN */
|