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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// Copyright (C) 1999-2016
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"
#include "gridbase.h"
#include "attribute.h"
#include "widget.h"
#include "util.h"
EXTERN void TkDrawAngledChars(Display *display,
Drawable drawable, GC gc, Tk_Font tkfont,
const char *source, int numBytes, double x,
double y, double angle);
extern "C" {
#include "ast.h"
#include "grf.h"
}
GridBase::GridBase(Widget* pp) : parent_(pp)
{
option_ = NULL;
renderMode_ = X11;
line_ = new Attribute(parent_);
text_ = new Attribute(parent_);
gridGC_ = NULL;
pixmap_ = 0;
mode_ = Widget::RGB;
}
GridBase::GridBase(Widget* pp, const char* op) : parent_(pp)
{
option_ = dupstr(op);
renderMode_ = X11;
line_ = new Attribute(parent_);
text_ = new Attribute(parent_);
gridGC_ = NULL;
pixmap_ = 0;
mode_ = Widget::RGB;
}
GridBase::~GridBase()
{
if (option_)
delete [] option_;
if (line_)
delete line_;
if (text_)
delete text_;
}
int GridBase::gAttr(int which, double value, double* old, int prim)
{
Attribute* attr;
switch (prim) {
case GRF__TEXT:
attr = text_;
switch (which) {
case GRF__STYLE:
break;
case GRF__WIDTH:
break;
case GRF__SIZE:
if (old)
*old = attr->size();
if (value != AST__BAD)
attr->setSize(value);
break;
case GRF__FONT:
if (old)
*old = attr->font();
if (value != AST__BAD)
attr->setFont(value);
break;
case GRF__COLOUR:
if (old)
*old = attr->colour();
if (value != AST__BAD)
attr->setColour(value);
break;
}
break;
case GRF__LINE:
attr = line_;
switch (which) {
case GRF__STYLE:
if (old)
*old = attr->style();
if (value != AST__BAD)
attr->setStyle(value);
break;
case GRF__WIDTH:
if (old)
*old = attr->width();
if (value != AST__BAD)
attr->setWidth(value);
break;
case GRF__SIZE:
break;
case GRF__FONT:
break;
case GRF__COLOUR:
if (old)
*old = attr->colour();
if (value != AST__BAD)
attr->setColour(value);
break;
}
break;
}
return 1;
}
int GridBase::gCap(int cap, int value)
{
switch (cap) {
case GRF__SCALES:
return 0;
case GRF__MJUST:
return 1;
case GRF__ESC:
return 0;
}
return 0;
}
// X11 Render functions
int GridBase::x11Line(int n, float* x, float* y)
{
if (n<2 || !x || !y)
return 1;
XSetForeground(parent_->getDisplay(), gridGC_, line_->color());
int w = (int)line_->width();
if (w<1)
w = 1;
switch (line_->style()) {
case Attribute::SOLID:
XSetLineAttributes(parent_->getDisplay(), gridGC_, w,
LineSolid, CapButt, JoinMiter);
break;
case Attribute::DASH:
XSetLineAttributes(parent_->getDisplay(), gridGC_, w,
LineOnOffDash, CapButt, JoinMiter);
char dlist[] = {8,3};
XSetDashes(parent_->getDisplay(), gridGC_, 0, dlist, 2);
break;
}
for (int i=0; i<n-1; i++) {
Vector s = Vector(x[i],y[i]).round();
Vector e = Vector(x[i+1],y[i+1]).round();
XDrawLine(parent_->getDisplay(), pixmap_, gridGC_,
(int)s[0],(int)s[1],(int)e[0],(int)e[1]);
}
return 1;
}
int GridBase::x11Text(const char* txt, float x, float y,
const char* just, Vector up)
{
XSetFont(parent_->getDisplay(), gridGC_, Tk_FontId(text_->tkfont()));
XSetForeground(parent_->getDisplay(), gridGC_, text_->color());
Vector vv = Vector(x,y);
double angle = calcTextAngle(just, up);
Vector cc = vv * calcTextPos(vv, angle, txt, just, up, text_->tkfont());
TkDrawAngledChars(parent_->getDisplay(), pixmap_, gridGC_, text_->tkfont(),
txt, strlen(txt),
cc[0], cc[1], radToDeg(angle));
return 1;
}
// PS Render functions
int GridBase::psLine(int n, float* x, float* y)
{
if (n<2 || !x || !y)
return 1;
psColor(line_);
{
ostringstream str;
str << line_->width() << " setlinewidth" << endl << ends;
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
}
{
ostringstream str;
switch (line_->style()) {
case Attribute::SOLID:
str << "[] 0 setdash" << endl << ends;
break;
case Attribute::DASH:
str << "[8 3] 0 setdash" << endl << ends;
break;
}
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
}
for (int i=0; i<n; i++) {
Vector v = Vector(x[i],y[i]) * matrix_;
ostringstream str;
if (i == 0) {
str << "newpath " << endl;
str << v.TkCanvasPs(parent_->getCanvas()) << " moveto" << endl << ends;
}
else
str << v.TkCanvasPs(parent_->getCanvas()) << " lineto" << endl << ends;
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
}
ostringstream str;
str << "stroke" << endl << ends;
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
return 1;
}
int GridBase::psText(const char* txt, float x, float y,
const char* just, Vector up)
{
Tcl_DString psdstr;
Tcl_DStringInit(&psdstr);
Vector vv = Vector(x,y) * matrix_;
double angle = calcTextAngle(just, up);
Vector cc = vv * calcTextPos(vv, angle, txt, just, up, text_->psfont());
ostringstream str;
const char* ff = Tk_NameOfFont(text_->psfont());
str << '/' << psFontName(ff)
<< " findfont " << int(psFontSize(ff)*parent_->getDisplayRatio())
<< " scalefont setfont" << endl;
psColor(text_);
str << "gsave "
<< cc.TkCanvasPs(parent_->getCanvas()) << " moveto" << endl
<< radToDeg(angle) << " rotate "
<< '(' << psQuote(txt) << ')' << " show"
<< " grestore" << endl << ends;
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
return 1;
}
void GridBase::psColor(Attribute* attr)
{
ostringstream str;
switch ((Widget::PSColorSpace)mode_) {
case Widget::BW:
case Widget::GRAY:
psColorGray(parent_->getXColor(attr->colorName()), str);
str << " setgray";
break;
case Widget::RGB:
psColorRGB(parent_->getXColor(attr->colorName()), str);
str << " setrgbcolor";
break;
case Widget::CMYK:
psColorCMYK(parent_->getXColor(attr->colorName()), str);
str << " setcmykcolor";
break;
}
str << endl << ends;
Tcl_AppendResult(parent_->getInterp(), str.str().c_str(), NULL);
}
#ifdef MAC_OSX_TK
int GridBase::macosxLine(int n, float* x, float* y)
{
if (n<2 || !x || !y)
return 1;
macosxColor(parent_->getXColor(line_->colorName()));
macosxWidth(line_->width());
switch (line_->style()) {
case Attribute::SOLID:
macosxDash(NULL,0);
break;
case Attribute::DASH:
float dlist[] = {8,3};
macosxDash(dlist,2);
break;
}
Vector* v = new Vector[n];
for (int i=0; i<n; i++)
v[i] = Vector(x[i],y[i]) * matrix_;
macosxDrawLines(v, n);
delete [] v;
return 1;
}
int GridBase::macosxText(const char* txt, float x, float y,
const char* just, Vector up)
{
// change this later
Tcl_DString psdstr;
Tcl_DStringInit(&psdstr);
int psSize = Tk_PostscriptFontName(text_->tkfont(), &psdstr);
macosxFont(Tcl_DStringValue(&psdstr),psSize);
Tcl_DStringFree(&psdstr);
Vector vv = Vector(x,y) * matrix_;
double angle = calcTextAngle(just, up);
Vector cc = vv * calcTextPos(vv, angle, txt, just, up, text_->tkfont());
macosxColor(parent_->getXColor(text_->colorName()));
macosxDrawText(cc, angle, txt);
return 1;
}
#endif
#ifdef __WIN32
int GridBase::win32Line(int n, float* x, float* y)
{
if (n<2 || !x || !y)
return 1;
win32Color(parent_->getXColor(line_->colorName()));
win32Width(line_->width());
switch (line_->style()) {
case Attribute::SOLID:
win32Dash(NULL,0);
break;
case Attribute::DASH:
float dlist[] = {8,3};
win32Dash(dlist,2);
break;
}
Vector v[n];
for (int i=0; i<n; i++)
v[i] = Vector(x[i],y[i]) * matrix_;
win32DrawLines(v, n);
return 1;
}
int GridBase::win32Text(const char* txt, float x, float y,
const char* just, Vector up)
{
win32Font(text_->tkfont());
win32Color(parent_->getXColor(text_->colorName()));
Vector vv = Vector(x,y) * matrix_;
double angle = calcTextAngle(just, up);;
Vector cc = vv * calcTextPos(vv, angle, txt, just, up, text_->tkfont());
win32DrawText(cc, angle, txt);
return 1;
}
#endif
// Support
double GridBase::calcTextAngle(const char* just, Vector up)
{
double a = up.angle();
// our angle is 90 off from ast's, and the other direction
double rr = -(a - M_PI_2);
// special case for text rotated exactly 90
if (up[0]==-1 && up[1]==0)
rr += M_PI;
// normalize
if (rr>0)
while (rr>M_TWOPI)
rr -= M_TWOPI;
else
while (rr<0)
rr += M_TWOPI;
return rr;
}
Matrix GridBase::calcTextPos(const Vector& vv, double angle, const char* txt,
const char* just, Vector up, Tk_Font font)
{
Tk_FontMetrics metrics;
Tk_GetFontMetrics(font, &metrics);
int width = Tk_TextWidth(font, txt, strlen(txt));
Matrix m1,m2;
switch (just[0]) {
case 'T':
break;
case 'C':
m1 = Translate(0,metrics.linespace/2);
break;
case 'B':
m1 = Translate(0,metrics.ascent);
break;
case 'M':
m1 = Translate(0,metrics.linespace);
break;
}
switch (just[1]) {
case 'L':
break;
case 'C':
m2 = Translate(-width/2.,0);
break;
case 'R':
m2 = Translate(-width,0);
break;
}
Matrix rr = Translate(-vv) *
Rotate(-angle) *
m1 * m2 *
Rotate(angle) *
Translate(vv);
// special case for text rotated exactly 90
Matrix mm;
if (up[0] == -1 && up[1] == 0) {
Vector cc = vv*rr;
mm = Translate(-cc) *
Rotate(-angle) *
Translate(-width/2.,metrics.linespace/2.) *
FlipY() *
Translate(width/2.,-metrics.linespace/2.) *
Rotate(angle) *
Translate(cc);
}
return rr*mm;
}
|