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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
/*
* $XConsortium: draw.c,v 1.8 94/04/17 20:43:35 gildea Exp $
* $XFree86: xc/programs/xditview/draw.c,v 1.5 2001/08/27 23:35:12 dawes Exp $
*
Copyright (c) 1991 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
*
*/
/*
* draw.c
*
* accept dvi function calls and translate to X
*/
/*
Support for ditroff drawing commands added: lines, circles, ellipses,
arcs and splines. Splines are approximated as short lines by iterating
a simple approximation algorithm. This seems good enough for previewing.
David Evans <dre@cs.nott.ac.uk>, 14th March, 1990
*/
#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include "DviP.h"
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950
#endif
/* the following are for use in the spline approximation algorithm */
typedef struct Point {
double x;
double y;
struct Point *next;
} Point;
#define ITERATIONS 10 /* iterations to approximate spline */
#define midx(p,q) ((p->x + q->x) / 2) /* mid x point on pq */
#define midy(p,q) ((p->y + q->y) / 2) /* mid y point on pq */
#define length(p,q) sqrt(((q->x - p->x)*(q->x - p->x)) \
+ ((q->y - p->y)*(q->y - p->y))) /* length of pq */
Point *spline = (Point *)NULL; /* head of spline linked list */
static void ApproxSpline(int n);
static void DeletePoint(Point *p);
static void DrawSplineSegments(DviWidget dw);
static int GetSpline(char *s);
static void InsertPoint(Point *p, Point *q);
static void LineApprox(Point *p1, Point *p2, Point *p3);
static Point * MakePoint(double x, double y);
void
HorizontalMove(dw, delta)
DviWidget dw;
int delta;
{
dw->dvi.state->x += delta;
}
void
HorizontalGoto(dw, NewPosition)
DviWidget dw;
int NewPosition;
{
dw->dvi.state->x = NewPosition;
}
void
VerticalMove(dw, delta)
DviWidget dw;
int delta;
{
dw->dvi.state->y += delta;
}
void
VerticalGoto(dw, NewPosition)
DviWidget dw;
int NewPosition;
{
dw->dvi.state->y = NewPosition;
}
#ifdef USE_XFT
static void
DrawText (DviWidget dw)
{
int i;
XftFont *font;
font = dw->dvi.cache.font;
for (i = 0; i <= dw->dvi.cache.index; i++)
{
if (dw->dvi.cache.cache[i].font)
font = dw->dvi.cache.cache[i].font;
XftDrawString8 (dw->dvi.draw, &dw->dvi.black,
font,
dw->dvi.cache.cache[i].x,
dw->dvi.cache.start_y,
(unsigned char *) dw->dvi.cache.cache[i].chars,
dw->dvi.cache.cache[i].nchars);
}
}
#endif
void
FlushCharCache (dw)
DviWidget dw;
{
int xx, yx;
xx = ToX(dw, dw->dvi.state->x);
yx = ToX(dw, dw->dvi.state->y);
if (dw->dvi.cache.char_index != 0)
{
#ifdef USE_XFT
DrawText (dw);
#else
XDrawText (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
dw->dvi.cache.start_x, dw->dvi.cache.start_y,
dw->dvi.cache.cache, dw->dvi.cache.index + 1);
#endif
}
dw->dvi.cache.index = 0;
dw->dvi.cache.max = DVI_TEXT_CACHE_SIZE;
if (dw->dvi.noPolyText)
dw->dvi.cache.max = 1;
dw->dvi.cache.char_index = 0;
dw->dvi.cache.cache[0].nchars = 0;
dw->dvi.cache.start_x = dw->dvi.cache.x = xx;
dw->dvi.cache.start_y = dw->dvi.cache.y = yx;
}
#if 0
void
ClearPage (DviWidget dw)
{
if (dw->dvi.display_enable)
XClearWindow (XtDisplay (dw), XtWindow (dw));
}
#endif
void
SetGCForDraw (dw)
DviWidget dw;
{
int lw;
if (dw->dvi.state->line_style != dw->dvi.line_style ||
dw->dvi.state->line_width != dw->dvi.line_width)
{
lw = ToX(dw, dw->dvi.state->line_width);
if (lw <= 1)
lw = 0;
XSetLineAttributes (XtDisplay (dw), dw->dvi.normal_GC,
lw, LineSolid, CapButt, JoinMiter);
dw->dvi.line_style = dw->dvi.state->line_style;
dw->dvi.line_width = dw->dvi.state->line_width;
}
}
void
DrawLine (dw, x, y)
DviWidget dw;
int x, y;
{
if (dw->dvi.display_enable)
XDrawLine (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
ToX(dw, dw->dvi.state->x), ToX(dw, dw->dvi.state->y),
ToX(dw, dw->dvi.state->x + x), ToX(dw,dw->dvi.state->y + y));
dw->dvi.state->x += x;
dw->dvi.state->y += y;
}
void
DrawCircle (dw, diameter)
DviWidget dw;
int diameter;
{
if (dw->dvi.display_enable)
XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
ToX(dw, dw->dvi.state->x),
ToX(dw, dw->dvi.state->y - (diameter / 2)),
ToX(dw, diameter), ToX(dw, diameter), 0, 360 * 64);
dw->dvi.state->x += diameter;
}
void
DrawEllipse (dw, a, b)
DviWidget dw;
int a, b;
{
if (dw->dvi.display_enable)
XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
ToX(dw, dw->dvi.state->x), ToX(dw, dw->dvi.state->y - (b / 2)),
ToX(dw,a), ToX(dw,b), 0, 360 * 64);
dw->dvi.state->x += a;
}
/* Convert angle in degrees to 64ths of a degree */
static int
ConvertAngle(int theta)
{
return(theta * 64);
}
void
DrawArc (dw, x0, y0, x1, y1)
DviWidget dw;
int x0, y0, x1, y1;
{
int xc, yc, x2, y2, r;
int angle1, angle2;
/* centre */
xc = dw->dvi.state->x + x0;
yc = dw->dvi.state->y + y0;
/* to */
x2 = xc + x1;
y2 = yc + y1;
dw->dvi.state->x = x2;
dw->dvi.state->y = y2;
if (dw->dvi.display_enable) {
/* radius */
r = (int)sqrt((float) x1 * x1 + (float) y1 * y1);
/* start and finish angles */
if (x0 == 0) {
if (y0 >= 0)
angle1 = 90;
else
angle1 = 270;
}
else {
angle1 = (int) (atan((double)(y0) / (double)(x0)) * 180 / M_PI);
if (x0 > 0)
angle1 = 180 - angle1;
else
angle1 = -angle1;
}
if (x1 == 0) {
if (y1 <= 0)
angle2 = 90;
else
angle2 = 270;
}
else {
angle2 = (int) (atan((double)(y1) / (double)(x1)) * 180 / M_PI);
if (x1 < 0)
angle2 = 180 - angle2;
else
angle2 = -angle2;
}
if (angle1 < 0)
angle1 += 360;
if (angle2 < 0)
angle2 += 360;
if (angle2 < angle1)
angle1 -= 360;
angle2 = angle2 - angle1;
angle1 = ConvertAngle(angle1);
angle2 = ConvertAngle(angle2);
XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
ToX(dw, xc - r), ToX(dw, yc - r),
ToX(dw, 2 * r), ToX(dw, 2 * r),
angle1, angle2);
}
}
/* copy next non-blank string from p to temp, update p */
static char *
getstr(char *p, char *temp)
{
while (*p == ' ' || *p == '\t' || *p == '\n')
p++;
if (*p == '\0') {
temp[0] = 0;
return((char *)NULL);
}
while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
*temp++ = *p++;
*temp = '\0';
return(p);
}
/* Draw a spline by approximating with short lines. */
/*ARGSUSED*/
void
DrawSpline (dw, s, len)
DviWidget dw;
char *s;
int len;
{
int n;
/* get coordinate pairs into spline linked list */
if ((n = GetSpline(s)) <= 0)
return;
ApproxSpline(n);
DrawSplineSegments(dw);
}
/* Parse string s to create a linked list of Point's with spline */
/* as its head. Return the number of coordinate pairs found. */
static int
GetSpline(s)
char *s;
{
double x, y, x1, y1;
int n = 0;
Point *pt;
char *p = s, d[10];
if (!*p)
return(n);
pt = spline = MakePoint(0.0, 0.0);
n = 1;
x = y = 0.0;
p = s;
while (p && *p) {
if ((p = getstr(p, d)) == (char *)NULL)
break;
x1 = x + atof(d);
if ((p = getstr(p, d)) == (char *)NULL)
break;
y1 = y + atof(d);
pt->next = MakePoint(x1, y1);
pt = pt->next;
x = pt->x;
y = pt->y;
n++;
}
/* number of pairs of points */
return(n);
}
/* Approximate a spline by lines generated by iterations of the */
/* approximation algorithm from the original n points in the spline. */
static void
ApproxSpline(n)
int n;
{
int mid, j;
Point *p1, *p2, *p3, *p;
if (n < 3)
return;
/* number of mid-points to calculate */
mid = n - 3;
/* remember original points are stored as an array of n points */
/* so I can index it directly to calculate mid-points only. */
if (mid > 0) {
p = spline->next;
j = 1;
while (j < n-2) {
p1 = p;
p = p->next;
p2 = p;
InsertPoint(p1, MakePoint(midx(p1, p2), midy(p1, p2)));
j++;
}
}
/* Now approximate curve by line segments. */
/* There *should* be the correct number of points now! */
p = spline;
while (p != (Point *)NULL) {
p1 = p;
if ((p = p->next) == (Point *)NULL)
break;
p2 = p;
if ((p = p->next) == (Point *)NULL)
break;
p3 = p; /* This point becomes first point of next curve */
LineApprox(p1, p2, p3);
}
}
/* p1, p2, and p3 are initially 3 *consecutive* points on the curve. */
/* For each adjacent pair of points find the mid-point, insert this */
/* in the linked list, delete the first of the two used (unless it */
/* is the first for this curve). Repeat this ITERATIONS times. */
/*ARGSUSED*/
static void
LineApprox(p1, p2, p3)
Point *p1, *p2, *p3;
{
Point *p4, *p;
int reps = ITERATIONS;
while (reps) {
for (p = p1; p != (Point *)NULL && p != p3; ) {
InsertPoint(p, p4 = MakePoint( midx(p,p->next), midy(p,p->next) ));
if (p != p1)
DeletePoint(p);
p = p4->next; /* skip inserted point! */
}
reps--;
}
}
/* Traverse the linked list, calling DrawLine to approximate the */
/* spline curve. Rounding errors are taken into account so that */
/* the "curve" is continuous, and ends up where expected. */
static void
DrawSplineSegments(dw)
DviWidget dw;
{
Point *p, *q;
double x1, y1;
int dx, dy;
double xpos, ypos;
p = spline;
dx = dy = 0;
/* save the start position */
xpos = dw->dvi.state->x;
ypos = dw->dvi.state->y;
x1 = y1 = 0.0;
while (p != (Point *)NULL) {
dx = p->x - x1 + 0.5;
dy = p->y - y1 + 0.5;
DrawLine (dw, dx, dy);
x1 = p->x;
y1 = p->y;
dw->dvi.state->x = xpos + x1;
dw->dvi.state->y = ypos + y1;
q = p;
p = p->next;
XtFree((char *)q);
}
spline = (Point *)NULL;
}
/* Malloc memory for a Point, and initialise the elements to x, y, NULL */
/* Return a pointer to the new Point. */
static Point *
MakePoint(x, y)
double x, y;
{
Point *p;
p = (Point *) XtMalloc (sizeof (Point));
p->x = x;
p->y = y;
p->next = (Point *)NULL;
return(p);
}
/* Insert point q in linked list after point p. */
static void
InsertPoint(p, q)
Point *p, *q;
{
/* point q to the next point */
q->next = p->next;
/* point p to new inserted one */
p->next = q;
}
/* Delete point p from the linked list. */
static void
DeletePoint(p)
Point *p;
{
Point *tmp;
tmp = p->next;
p->x = p->next->x;
p->y = p->next->y;
p->next = p->next->next;
XtFree((char *)tmp);
}
|