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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
|
/*-------------------------------------------------------------------------*/
/* cairo.c --- mainly cairo versions of the UDraw... stuff in functions.c */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* originally written by Tim Edwards, 8/13/93 */
/* All cairo graphics library modifications by Erik van der Wal, May 2014 */
/*-------------------------------------------------------------------------*/
#ifdef HAVE_CAIRO
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
#ifndef XC_WIN32
#include <X11/Intrinsic.h>
#endif
#ifdef TCL_WRAPPER
#include <tk.h>
#endif /* TCL_WRAPPER */
#include "xcircuit.h"
#include "colordefs.h"
#include "prototypes.h"
extern XCWindowData *areawin;
extern int *appcolors;
extern Globaldata xobjs;
extern short fontcount;
extern fontinfo *fonts;
extern Display *dpy;
static void xc_cairo_strokepath(short style, float width);
/*----------------------------------------------------------------------------*/
void xc_cairo_set_matrix(const Matrix *xcm)
{
cairo_matrix_t m = {
.xx = xcm->a, .xy = xcm->b, .x0 = xcm->c,
.yx = xcm->d, .yy = xcm->e, .y0 = xcm->f
};
cairo_set_matrix(areawin->cr, &m);
};
/*----------------------------------------------------------------------*/
/* Set the color, based on the given color index */
/*----------------------------------------------------------------------*/
void xc_cairo_set_color(unsigned long coloridx)
{
unsigned short r, g, b;
double red, green, blue;
xc_get_color_rgb(coloridx, &r, &g, &b);
cairo_set_source_rgb(areawin->cr, r / 65535., g / 65535., b / 65535.);
}
/*----------------------------------------------------------------------------*/
void UDrawLine(XPoint *pt1, XPoint *pt2)
{
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
cairo_set_line_width(areawin->cr, xobjs.pagelist[areawin->page]->wirewidth);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_BEVEL);
cairo_move_to(areawin->cr, pt1->x, pt1->y);
cairo_line_to(areawin->cr, pt2->x, pt2->y);
cairo_stroke(areawin->cr);
cairo_restore(areawin->cr);
}
/*----------------------------------------------------------------------*/
/* Add circle at given point to indicate that the point is a parameter. */
/* The circle is divided into quarters. For parameterized y-coordinate */
/* the top and bottom quarters are drawn. For parameterized x- */
/* coordinate, the left and right quarters are drawn. A full circle */
/* indicates either both x- and y-coordinates are parameterized, or */
/* else any other kind of parameterization (presently, not used). */
/* */
/* (note that the two angles in XDrawArc() are 1) the start angle, */
/* measured in absolute 64th degrees from 0 (3 o'clock), and 2) the */
/* path length, in relative 64th degrees (positive = counterclockwise, */
/* negative = clockwise)). */
/*----------------------------------------------------------------------*/
void UDrawCircle(XPoint *upt, u_char which)
{
XPoint wpt;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
user_to_window(*upt, &wpt);
cairo_set_line_width(areawin->cr, .75);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_BUTT);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_MITER);
/* TODO: angles might be mirrored or turning the wrong way */
switch(which) {
case P_POSITION_X:
cairo_arc(areawin->cr, wpt.x, wpt.y, 4., M_PI * -.25, M_PI * .25);
cairo_arc(areawin->cr, wpt.x, wpt.y, 4., M_PI * .75, M_PI * 1.25);
break;
case P_POSITION_Y:
cairo_arc(areawin->cr, wpt.x, wpt.y, 4., M_PI * .25, M_PI * .75);
cairo_arc(areawin->cr, wpt.x, wpt.y, 4., M_PI * 1.25, M_PI * 1.75);
break;
default:
cairo_arc(areawin->cr, wpt.x, wpt.y, 4., 0., M_PI * 2.);
break;
}
cairo_restore(areawin->cr);
}
/*----------------------------------------------------------------------*/
/* Add "X" at string origin */
/*----------------------------------------------------------------------*/
void UDrawXAt(XPoint *wpt)
{
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_width(areawin->cr, .75);
cairo_move_to(areawin->cr, wpt->x - 3., wpt->y - 3.);
cairo_line_to(areawin->cr, wpt->x + 3., wpt->y + 3.);
cairo_move_to(areawin->cr, wpt->x + 3., wpt->y - 3.);
cairo_line_to(areawin->cr, wpt->x - 3., wpt->y + 3.);
cairo_stroke(areawin->cr);
cairo_restore(areawin->cr);
}
void UDrawXLine(XPoint opt, XPoint cpt)
{
XPoint upt, vpt;
double dashes[] = {4., 4.};
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
xc_cairo_set_color(AUXCOLOR);
cairo_set_dash(areawin->cr, dashes, sizeof(dashes) / sizeof(double), 0.);
cairo_set_line_width(areawin->cr, .75);
user_to_window(cpt, &upt);
user_to_window(opt, &vpt);
cairo_move_to(areawin->cr, vpt.x, vpt.y);
cairo_line_to(areawin->cr, upt.x, upt.y);
cairo_stroke(areawin->cr);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_move_to(areawin->cr, upt.x - 3., upt.y - 3.);
cairo_line_to(areawin->cr, upt.x + 3., upt.y + 3.);
cairo_move_to(areawin->cr, upt.x + 3., upt.y - 3.);
cairo_line_to(areawin->cr, upt.x - 3., upt.y + 3.);
cairo_stroke(areawin->cr);
cairo_restore(areawin->cr);
}
/*-------------------------------------------------------------------------*/
void UDrawBox(XPoint origin, XPoint corner)
{
XPoint worig, wcorn;
double r, g, b, a;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
user_to_window(origin, &worig);
user_to_window(corner, &wcorn);
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
cairo_set_line_width(areawin->cr, 1.0);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_MITER);
cairo_move_to(areawin->cr, worig.x + .5, worig.y + .5);
cairo_line_to(areawin->cr, worig.x + .5, wcorn.y + .5);
cairo_line_to(areawin->cr, wcorn.x + .5, wcorn.y + .5);
cairo_line_to(areawin->cr, wcorn.x + .5, worig.y + .5);
cairo_close_path(areawin->cr);
xc_cairo_set_color(AUXCOLOR);
cairo_pattern_get_rgba(cairo_get_source(areawin->cr), &r, &g, &b, &a);
cairo_set_source_rgba(areawin->cr, r, g, b, .1 * a);
cairo_fill_preserve(areawin->cr);
cairo_set_source_rgba(areawin->cr, r, g, b, a);
cairo_stroke(areawin->cr);
cairo_restore(areawin->cr);
}
/*----------------------------------------------------------------------*/
/* Draw a box indicating the dimensions of the edit element that most */
/* closely reach the position "corner". */
/*----------------------------------------------------------------------*/
float UDrawRescaleBox(XPoint *corner)
{
XPoint newpoints[5];
float newscale;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
/* No return here, since the return value might be needed? */
}
if (areawin->selects == 0)
return 0.;
newscale = UGetRescaleBox(corner, newpoints);
if (areawin->redraw_ongoing) {
int i;
cairo_save(areawin->cr);
xc_cairo_set_color(AUXCOLOR);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_BEVEL);
cairo_move_to(areawin->cr, newpoints[0].x, newpoints[0].y);
for (i = 1; i < 4; i++)
cairo_line_to(areawin->cr, newpoints[i].x, newpoints[i].y);
xc_cairo_strokepath(0, 1);
cairo_restore(areawin->cr);
}
return newscale;
}
/*-------------------------------------------------------------------------*/
void UDrawBBox()
{
XPoint origin;
XPoint worig, wcorn, corner;
objinstptr bbinst = areawin->topinstance;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
if ((!areawin->bboxon) || (checkforbbox(topobject) != NULL)) return;
origin = bbinst->bbox.lowerleft;
corner.x = origin.x + bbinst->bbox.width;
corner.y = origin.y + bbinst->bbox.height;
/* Include any schematic labels in the bounding box. */
extendschembbox(bbinst, &origin, &corner);
user_to_window(origin, &worig);
user_to_window(corner, &wcorn);
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
xc_cairo_set_color(BBOXCOLOR);
cairo_set_line_width(areawin->cr, 1.0);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_MITER);
cairo_move_to(areawin->cr, worig.x + .5, worig.y + .5);
cairo_line_to(areawin->cr, worig.x + .5, wcorn.y + .5);
cairo_line_to(areawin->cr, wcorn.x + .5, wcorn.y + .5);
cairo_line_to(areawin->cr, wcorn.x + .5, worig.y + .5);
cairo_close_path(areawin->cr);
cairo_stroke(areawin->cr);
cairo_restore(areawin->cr);
}
/*----------------------------------------------------------------------*/
/* Main recursive object instance drawing routine. */
/* context is the instance information passed down from above */
/* theinstance is the object instance to be drawn */
/* level is the level of recursion */
/* passcolor is the inherited color value passed to object */
/* passwidth is the inherited linewidth value passed to the object */
/* stack contains graphics context information */
/*----------------------------------------------------------------------*/
void UDrawObject(objinstptr theinstance, short level, int passcolor,
float passwidth, pushlistptr *stack)
{
genericptr *areagen;
float tmpwidth;
int defaultcolor = passcolor;
int curcolor = passcolor;
int thispart;
short savesel;
XPoint bboxin[2], bboxout[2];
u_char xm, ym;
objectptr theobject = theinstance->thisobject;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
/* Save the number of selections and set it to zero while we do the */
/* object drawing. */
savesel = areawin->selects;
areawin->selects = 0;
/* All parts are given in the coordinate system of the object, unless */
/* this is the top-level object, in which they will be interpreted as */
/* relative to the screen. */
UPushCTM();
if (level != 0)
UPreMultCTM(DCTM, theinstance->position, theinstance->scale,
theinstance->rotation);
if (theinstance->style & LINE_INVARIANT)
passwidth /= fabs(theinstance->scale);
/* do a quick test for intersection with the display window */
bboxin[0].x = theobject->bbox.lowerleft.x;
bboxin[0].y = theobject->bbox.lowerleft.y;
bboxin[1].x = theobject->bbox.lowerleft.x + theobject->bbox.width;
bboxin[1].y = theobject->bbox.lowerleft.y + theobject->bbox.height;
if (level == 0)
extendschembbox(theinstance, &(bboxin[0]), &(bboxin[1]));
UTransformbyCTM(DCTM, bboxin, bboxout, 2);
xm = (bboxout[0].x < bboxout[1].x) ? 0 : 1;
ym = (bboxout[0].y < bboxout[1].y) ? 0 : 1;
if (bboxout[xm].x < areawin->width && bboxout[ym].y < areawin->height &&
bboxout[1 - xm].x > 0 && bboxout[1 - ym].y > 0) {
/* make parameter substitutions */
psubstitute(theinstance);
/* draw all of the elements */
tmpwidth = UTopTransScale(passwidth);
cairo_set_line_width(areawin->cr, tmpwidth);
cairo_set_dash(areawin->cr, NULL, 0, 0.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(areawin->cr, CAIRO_LINE_JOIN_BEVEL);
/* guard against plist being regenerated during a redraw by the */
/* expression parameter mechanism (should that be prohibited?) */
for (thispart = 0; thispart < theobject->parts; thispart++) {
areagen = theobject->plist + thispart;
if ((*areagen)->type & DRAW_HIDE) continue;
if (defaultcolor != DOFORALL) {
if ((*areagen)->color != curcolor) {
if ((*areagen)->color == DEFAULTCOLOR)
curcolor = defaultcolor;
else
curcolor = (*areagen)->color;
XcTopSetForeground(curcolor);
}
}
switch(ELEMENTTYPE(*areagen)) {
case(POLYGON):
if (level == 0 || !((TOPOLY(areagen))->style & BBOX))
UDrawPolygon(TOPOLY(areagen), passwidth);
break;
case(SPLINE):
UDrawSpline(TOSPLINE(areagen), passwidth);
break;
case(ARC):
UDrawArc(TOARC(areagen), passwidth);
break;
case(PATH):
UDrawPath(TOPATH(areagen), passwidth);
break;
case(GRAPHIC):
UDrawGraphic(TOGRAPHIC(areagen));
break;
case(OBJINST):
UDrawObject(TOOBJINST(areagen), level + 1, curcolor, passwidth, stack);
break;
case(LABEL):
if (level == 0 || TOLABEL(areagen)->pin == False)
UDrawString(TOLABEL(areagen), curcolor, theinstance);
else if ((TOLABEL(areagen)->justify & PINVISIBLE) && areawin->pinpointon)
UDrawString(TOLABEL(areagen), curcolor, theinstance);
else if (TOLABEL(areagen)->justify & PINVISIBLE)
UDrawStringNoX(TOLABEL(areagen), curcolor, theinstance);
else if (level == 1 && TOLABEL(areagen)->pin &&
TOLABEL(areagen)->pin != INFO && areawin->pinpointon)
UDrawXDown(TOLABEL(areagen));
break;
}
}
/* restore the color passed to the object, if different from current color */
if ((defaultcolor != DOFORALL) && (passcolor != curcolor))
XTopSetForeground(passcolor);
}
/* restore the selection list (if any) */
areawin->selects = savesel;
UPopCTM();
}
/*-------------------------------------------------------------------------*/
static void xc_cairo_strokepath(short style, float width)
{
float tmpwidth;
if (!(style & CLIPMASK) || (areawin->showclipmasks == TRUE)) {
if (style & FILLED || (!(style & FILLED) && style & OPAQUE)) {
if ((style & FILLSOLID) == FILLSOLID)
cairo_fill_preserve(areawin->cr);
else {
double red, green, blue, alpha;
cairo_pattern_get_rgba(cairo_get_source(areawin->cr),
&red, &green, &blue, &alpha);
if (!(style & FILLED))
cairo_set_source_rgba(areawin->cr, 1., 1., 1., alpha);
else {
double m = (1 + ((style & FILLSOLID) >> 5)) / 8.;
if (style & OPAQUE) {
double n = (1. - m);
cairo_set_source_rgba(areawin->cr, m * red + n,
m * green + n, m * blue + n, alpha);
}
else
cairo_set_source_rgba(areawin->cr, red, green, blue,
m * alpha);
}
cairo_fill_preserve(areawin->cr);
cairo_set_source_rgba(areawin->cr, red, green, blue, alpha);
}
}
if (!(style & NOBORDER)) {
cairo_set_line_width(areawin->cr, width);
cairo_set_line_join(areawin->cr, (style & SQUARECAP) ?
CAIRO_LINE_JOIN_MITER : CAIRO_LINE_JOIN_BEVEL);
if (style & (DASHED | DOTTED)) {
double dashes[2] = {4.0 * width, 4.0 * width};
if (style & DOTTED)
dashes[0] = width;
cairo_set_dash(areawin->cr, dashes, 2, 0.0);
cairo_set_line_width(areawin->cr, width);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_BUTT);
}
else {
cairo_set_dash(areawin->cr, NULL, 0, 0.0);
cairo_set_line_cap(areawin->cr, (style & SQUARECAP) ?
CAIRO_LINE_CAP_SQUARE : CAIRO_LINE_CAP_ROUND);
}
/* draw the spline and close off if so specified */
if (!(style & UNCLOSED))
cairo_close_path(areawin->cr);
cairo_stroke_preserve(areawin->cr);
}
}
if (style & CLIPMASK)
cairo_clip_preserve(areawin->cr);
cairo_new_path(areawin->cr); // clear preserved paths
}
/*-------------------------------------------------------------------------*/
void UDrawPolygon(polyptr thepoly, float passwidth)
{
int i;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
if (thepoly->number) {
cairo_move_to(areawin->cr, thepoly->points[0].x, thepoly->points[0].y);
for (i = 1; i < thepoly->number; i++)
cairo_line_to(areawin->cr, thepoly->points[i].x, thepoly->points[i].y);
xc_cairo_strokepath(thepoly->style, thepoly->width * passwidth);
}
}
/*-------------------------------------------------------------------------*/
void UDrawArc(arcptr thearc, float passwidth)
{
XPoint tmppoints[RSTEPS + 2];
float scaledwidth;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
scaledwidth = thearc->width * passwidth;
if (abs(thearc->radius) == thearc->yaxis)
cairo_arc(areawin->cr, thearc->position.x, thearc->position.y,
abs(thearc->radius), thearc->angle1 * M_PI / 180.0,
thearc->angle2 * M_PI / 180.0);
else {
// perform elliptical arc, as described in cairo manual
cairo_save(areawin->cr);
cairo_translate(areawin->cr, thearc->position.x, thearc->position.y);
cairo_scale(areawin->cr, abs(thearc->radius), thearc->yaxis);
cairo_arc(areawin->cr, 0.0, 0.0, 1.0, thearc->angle1 * M_PI / 180.0,
thearc->angle2 * M_PI / 180.0);
cairo_restore(areawin->cr);
}
xc_cairo_strokepath(thearc->style, thearc->width * passwidth);
if (thearc->cycle != NULL) {
UDrawXLine(thearc->position, areawin->save);
}
}
/*-------------------------------------------------------------------------*/
void UDrawPath(pathptr thepath, float passwidth)
{
genericptr *genpath;
polyptr thepoly;
splineptr thespline;
Boolean draweditlines = FALSE;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
/* First pass---check for any splines that are being edited. If */
/* any one is, then draw all control points for all splines in the */
/* path. */
for (genpath = thepath->plist; genpath < thepath->plist + thepath->parts;
genpath++) {
if (ELEMENTTYPE(*genpath) == SPLINE) {
thespline = TOSPLINE(genpath);
if (thespline->cycle != NULL) {
draweditlines = TRUE;
break;
}
}
}
/* Draw first point */
if (thepath->parts) {
genpath = thepath->plist;
switch(ELEMENTTYPE(*genpath)) {
case POLYGON:
thepoly = TOPOLY(genpath);
cairo_move_to(areawin->cr, thepoly->points[0].x,
thepoly->points[0].y);
break;
case SPLINE:
thespline = TOSPLINE(genpath);
cairo_move_to(areawin->cr, thespline->ctrl[0].x,
thespline->ctrl[0].y);
break;
}
}
/* Draw all other points */
for (genpath = thepath->plist; genpath < thepath->plist + thepath->parts;
genpath++) {
int i;
switch(ELEMENTTYPE(*genpath)) {
case POLYGON:
thepoly = TOPOLY(genpath);
for (i = 1; i < thepoly->number; i++)
cairo_line_to(areawin->cr, thepoly->points[i].x,
thepoly->points[i].y);
break;
case SPLINE:
thespline = TOSPLINE(genpath);
cairo_curve_to(areawin->cr, thespline->ctrl[1].x,
thespline->ctrl[1].y, thespline->ctrl[2].x,
thespline->ctrl[2].y, thespline->ctrl[3].x,
thespline->ctrl[3].y);
break;
}
}
xc_cairo_strokepath(thepath->style, thepath->width * passwidth);
/* Finally draw the edit lines */
if (draweditlines) {
for (genpath = thepath->plist; genpath < thepath->plist + thepath->parts;
genpath++) {
if (ELEMENTTYPE(*genpath) == SPLINE) {
thespline = TOSPLINE(genpath);
UDrawXLine(thespline->ctrl[0], thespline->ctrl[1]);
UDrawXLine(thespline->ctrl[3], thespline->ctrl[2]);
}
}
}
}
/*-------------------------------------------------------------------------*/
void UDrawSpline(splineptr thespline, float passwidth)
{
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_move_to(areawin->cr, thespline->ctrl[0].x, thespline->ctrl[0].y);
cairo_curve_to(areawin->cr, thespline->ctrl[1].x, thespline->ctrl[1].y,
thespline->ctrl[2].x, thespline->ctrl[2].y,
thespline->ctrl[3].x, thespline->ctrl[3].y);
xc_cairo_strokepath(thespline->style, thespline->width * passwidth);
if (thespline->cycle != NULL) {
UDrawXLine(thespline->ctrl[0], thespline->ctrl[1]);
UDrawXLine(thespline->ctrl[3], thespline->ctrl[2]);
}
}
/*-------------------------------------------------------------------------*/
void UDrawGraphic(graphicptr gp)
{
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
cairo_translate(areawin->cr,
gp->position.x,
gp->position.y);
cairo_rotate(areawin->cr, gp->rotation * M_PI / -180.);
cairo_scale(areawin->cr, gp->scale, -gp->scale);
cairo_set_source_surface(areawin->cr, gp->source,
- cairo_image_surface_get_width(gp->source) / 2.,
- cairo_image_surface_get_height(gp->source) / 2.);
cairo_paint(areawin->cr);
cairo_restore(areawin->cr);
}
/*----------------------------*/
/* Draw the grids, axis, etc. */
/*----------------------------*/
void draw_grids(void)
{
double spc, spc2, spc3;
cairo_matrix_t m = {
.xx = 1., .yx = 0., .xy = 0., .yy = -1.,
.x0 = -areawin->pcorner.x * areawin->vscale,
.y0 = areawin->height + areawin->pcorner.y * areawin->vscale
};
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
return;
}
cairo_save(areawin->cr);
/* draw lines for grid */
spc = xobjs.pagelist[areawin->page]->gridspace * areawin->vscale;
if (areawin->gridon && spc > 8) {
double x, y;
int ix, iy;
/* find bottom-right point on the grid */
double xbegin = areawin->width;
double ybegin = areawin->height;
cairo_set_matrix(areawin->cr, &m);
cairo_scale(areawin->cr, spc, spc);
cairo_device_to_user(areawin->cr, &xbegin, &ybegin);
xbegin = floor(xbegin);
ybegin = ceil(ybegin);
ix = xbegin;
iy = ybegin;
cairo_user_to_device(areawin->cr, &xbegin, &ybegin);
cairo_identity_matrix(areawin->cr);
/* draw the grid */
xc_cairo_set_color(GRIDCOLOR);
cairo_set_line_width(areawin->cr, 1.);
for (x = xbegin; x >= 0.; x -= spc, ix--) {
if (!ix && areawin->axeson) continue; /* do not draw main axis */
cairo_move_to(areawin->cr, floor(x) + .5, .5);
cairo_line_to(areawin->cr, floor(x) + .5, areawin->height + .5);
}
for (y = ybegin; y >= 0.; y -= spc, iy++) {
if (!iy && areawin->axeson) continue; /* do not draw main axis */
cairo_move_to(areawin->cr, .5, floor(y) + .5);
cairo_line_to(areawin->cr, areawin->width + .5, floor(y) + .5);
}
cairo_stroke(areawin->cr);
}
if (areawin->axeson) {
/* find main axis */
double x = 0, y = 0;
cairo_set_matrix(areawin->cr, &m);
cairo_user_to_device(areawin->cr, &x, &y);
cairo_identity_matrix(areawin->cr);
/* draw the grid */
xc_cairo_set_color(AXESCOLOR);
cairo_set_line_width(areawin->cr, 1.);
cairo_move_to(areawin->cr, floor(x) + .5, .5);
cairo_line_to(areawin->cr, floor(x) + .5, areawin->height + .5);
cairo_move_to(areawin->cr, .5, floor(y) + .5);
cairo_line_to(areawin->cr, areawin->width + .5, floor(y) + .5);
cairo_stroke(areawin->cr);
}
/* bounding box goes beneath everything except grid/axis lines */
UDrawBBox();
/* draw a little red dot at each snap-to point */
spc2 = xobjs.pagelist[areawin->page]->snapspace * areawin->vscale;
if (areawin->snapto && spc2 > 8) {
double x, y;
/* find bottom-right point on the grid */
double xbegin = areawin->width;
double ybegin = areawin->height;
cairo_set_matrix(areawin->cr, &m);
cairo_scale(areawin->cr, spc2, spc2);
cairo_device_to_user(areawin->cr, &xbegin, &ybegin);
xbegin = floor(xbegin);
ybegin = ceil(ybegin);
cairo_user_to_device(areawin->cr, &xbegin, &ybegin);
cairo_identity_matrix(areawin->cr);
/* draw the grid */
xc_cairo_set_color(SNAPCOLOR);
cairo_set_line_width(areawin->cr, 1.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_ROUND);
for (x = xbegin; x >= 0.; x -= spc2) {
for (y = ybegin; y >= 0.; y -= spc2) {
cairo_move_to(areawin->cr, floor(x) + .5, floor(y) + .5);
cairo_close_path(areawin->cr);
}
}
cairo_stroke(areawin->cr);
}
/* Draw major snap points */
spc3 = spc * 20.;
if (spc > 4.) {
double x, y;
/* find bottom-right point on the grid */
double xbegin = areawin->width;
double ybegin = areawin->height;
cairo_set_matrix(areawin->cr, &m);
cairo_scale(areawin->cr, spc3, spc3);
cairo_device_to_user(areawin->cr, &xbegin, &ybegin);
xbegin = floor(xbegin);
ybegin = ceil(ybegin);
cairo_user_to_device(areawin->cr, &xbegin, &ybegin);
cairo_identity_matrix(areawin->cr);
/* draw the grid */
xc_cairo_set_color(GRIDCOLOR);
cairo_set_line_width(areawin->cr, 3.);
cairo_set_line_cap(areawin->cr, CAIRO_LINE_CAP_ROUND);
for (x = xbegin; x >= 0.; x -= spc3) {
for (y = ybegin; y >= 0.; y -= spc3) {
cairo_move_to(areawin->cr, floor(x) + .5, floor(y) + .5);
cairo_close_path(areawin->cr);
}
}
cairo_stroke(areawin->cr);
}
cairo_restore(areawin->cr);
}
/*---------------------------------------------------------------------*/
/* draw a single character at 0, 0 using current transformation matrix */
/*---------------------------------------------------------------------*/
float UDrawChar(u_char code, short styles, short ffont, int groupheight,
int passcolor, float passwidth)
{
objectptr drawchar;
XPoint alphapts[2];
float localwidth;
objinst charinst;
if (!areawin->redraw_ongoing) {
areawin->redraw_needed = True;
/* No return here, since the return value might be needed? */
}
if ((ffont >= fontcount) || (fonts[ffont].encoding == NULL))
return 0;
alphapts[0].x = 0;
alphapts[0].y = 0;
charinst.type = OBJINST;
charinst.color = DEFAULTCOLOR;
charinst.rotation = 0;
charinst.scale = fonts[ffont].scale;
charinst.position = alphapts[0];
charinst.params = NULL;
/* get proper font and character */
drawchar = fonts[ffont].encoding[(u_char)code];
charinst.thisobject = drawchar;
if (fonts[ffont].cairo_family) {
cairo_save(areawin->cr);
cairo_text_extents_t extents;
cairo_select_font_face(areawin->cr, fonts[ffont].cairo_family,
fonts[ffont].slant, fonts[ffont].weight);
}
if (!fonts[ffont].cairo_family && (fonts[ffont].flags & 0x22) == 0x22) {
/* font is not a cairo font and it is derived and italic */
USlantCTM(DCTM, 0.25); /* premultiply by slanting function */
}
/* Draw glyph */
if (!(styles & 64) && areawin->redraw_ongoing) {
if (fonts[ffont].cairo_family) {
cairo_set_font_size (areawin->cr, 40.); /* TODO: Why 40? */
cairo_scale(areawin->cr, 1., -1.);
cairo_move_to (areawin->cr, 0., 0.);
cairo_show_text(areawin->cr, fonts[ffont].utf8encoding[code]);
cairo_new_path(areawin->cr);
}
else
UDrawObject(&charinst, SINGLE, passcolor, passwidth, NULL);
}
/* Determine character width */
if (fonts[ffont].cairo_family) {
/* Determine localwidth on a fixed font size of 100, to prevent hinting */
/* from destroying scale independance */
cairo_text_extents_t extents;
cairo_identity_matrix(areawin->cr);
cairo_set_font_size(areawin->cr, 100.);
cairo_text_extents(areawin->cr, fonts[ffont].utf8encoding[code],
&extents);
localwidth = extents.x_advance * fonts[ffont].scale / 100. * 40.; /* TODO: Why 40? */
cairo_restore(areawin->cr);
}
else
localwidth = (drawchar->bbox.lowerleft.x + drawchar->bbox.width)
* fonts[ffont].scale;
/* under- and overlines */
if (!(styles & 64)) {
if (styles & 8)
alphapts[0].y = alphapts[1].y = -6;
else if (styles & 16)
alphapts[0].y = alphapts[1].y = groupheight + 4;
if (styles & 24 && areawin->redraw_ongoing) {
alphapts[0].x = 0; alphapts[1].x = localwidth;
cairo_set_line_width(areawin->cr, passwidth);
cairo_move_to(areawin->cr, alphapts[0].x, alphapts[0].y);
cairo_line_to(areawin->cr, alphapts[1].x, alphapts[1].y);
cairo_stroke(areawin->cr);
}
}
return localwidth;
}
float xc_cairo_get_char_extents(const fontinfo *font, unsigned char c,
float *top, float *bottom)
{
/* Determine localwidth on a fixed font size of 100, to */
/* prevent hinting from destroying scale independance */
cairo_text_extents_t extents;
cairo_save(areawin->cr);
cairo_identity_matrix(areawin->cr);
cairo_select_font_face(areawin->cr, font->cairo_family, font->slant,
fonts->weight);
cairo_set_font_size(areawin->cr, 100.);
cairo_text_extents(areawin->cr, font->utf8encoding[c], &extents);
cairo_restore(areawin->cr);
if (top)
*top = -extents.y_bearing * 40. / 100.;
if (bottom)
*bottom = (extents.height - extents.y_bearing) * 40. / 100.;
return extents.x_advance * 40. / 100.; /* TODO: Why 40? */
}
/*----------------------------------------------------------------------*/
/* A light wrapper around cairo_surface_t, to a generalized xcImage */
/*----------------------------------------------------------------------*/
/* caching for cairo_surface_t */
static xcImage *xcImagePixel_oldimg = NULL;
static uint32_t *xcImagePixel_data;
static int xcImagePixel_width;
static int xcImagePixel_height;
static inline void xcImageCheckCache(xcImage *img)
{
if (img != xcImagePixel_oldimg) {
xcImagePixel_oldimg = img;
xcImagePixel_data = (uint32_t*) cairo_image_surface_get_data(img);
xcImagePixel_width = cairo_image_surface_get_width(img);
xcImagePixel_height = cairo_image_surface_get_height(img);
}
}
xcImage *xcImageCreate(int width, int height)
{
return cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
}
void xcImageDestroy(xcImage *img)
{
cairo_surface_destroy(img);
}
int xcImageGetWidth(xcImage *img)
{
xcImageCheckCache(img);
return xcImagePixel_width;
}
int xcImageGetHeight(xcImage *img)
{
xcImageCheckCache(img);
return xcImagePixel_height;
}
void xcImagePutPixel(xcImage *img, int x, int y, u_char r, u_char g, u_char b)
{
xcImageCheckCache(img);
xcImagePixel_data[y * xcImagePixel_width + x] = (r << 16) | (g << 8) | b;
}
void xcImageGetPixel(xcImage *img, int x, int y, u_char *r, u_char *g,
u_char *b)
{
xcImageCheckCache(img);
uint32_t argb = xcImagePixel_data[y * xcImagePixel_width + x];
*r = argb >> 16;
*g = argb >> 8;
*b = argb;
}
#endif /* HAVE_CAIRO */
|