This file is indexed.

/usr/share/common-lisp/source/mcclim/graphics.lisp is in cl-mcclim 0.9.6.dfsg.cvs20100315-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-

;;;  (c) copyright 1998,1999,2000,2001 by Michael McDonald (mikemac@mikemac.com)
;;;  (c) copyright 2001 by Arnaud Rouanet (rouanet@emi.u-bordeaux.fr)

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the 
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
;;; Boston, MA  02111-1307  USA.

(in-package :clim-internals)

;;; Work in progress that reduces consing of rest arguments and keyword
;;; processing. 
(defmacro with-medium-and-options ((sheet
				    &key ink clipping-region transformation
				    line-unit line-thickness
				    line-style line-style-p
				    line-dashes dashes-p
				    line-joint-shape line-cap-shape
				    text-style text-style-p
				    text-family text-family-p
				    text-face text-face-p
				    text-size text-size-p)
				   (medium)
				   &body body)
  (with-gensyms (continuation sheet-medium)
    `(flet ((,continuation (,medium)
	      ,@body))
       (declare (dynamic-extent #',continuation))
       (with-sheet-medium (,sheet-medium ,sheet)
	 (do-graphics-with-options-internal-1
	     ,sheet-medium #'continuation
	     ,ink ,clipping-region ,transformation
	     ,line-unit ,line-thickness
	     ,line-style ,line-style-p
	     ,line-dashes ,dashes-p
	     ,line-joint-shape ,line-cap-shape
	     ,text-style ,text-style-p
	     ,text-family ,text-family-p
	     ,text-face ,text-face-p
	     ,text-size ,text-size-p))))
  )

(defmethod do-graphics-with-options ((sheet sheet) func &rest options)
  (with-sheet-medium (medium sheet)
    (apply #'do-graphics-with-options-internal medium sheet func options)))

(defmethod do-graphics-with-options ((medium medium) func &rest options)
  (apply #'do-graphics-with-options-internal medium medium func options))

(defmethod do-graphics-with-options ((sheet t) func &rest options)
  (declare (ignore options))
  (if sheet
      (funcall func sheet)))

(defmethod do-graphics-with-options ((pixmap pixmap) func &rest options)
  (with-pixmap-medium (medium pixmap)
    (apply #'do-graphics-with-options-internal medium medium func options)))

(defmethod do-graphics-with-options-internal ((medium medium) orig-medium func
				     &rest args
				     &key ink clipping-region transformation
					  line-unit line-thickness
                                          (line-style nil line-style-p)
					  (line-dashes nil dashes-p)
					  line-joint-shape line-cap-shape
					  (text-style nil text-style-p)
					  (text-family nil text-family-p)
					  (text-face nil text-face-p)
					  (text-size nil text-size-p)
				     &allow-other-keys)
  (declare (ignore args))
  (let ((old-ink (medium-ink medium))
	(old-clip (medium-clipping-region medium))
	(old-transform (medium-transformation medium))
	(old-line-style (medium-line-style medium))
	(old-text-style (medium-text-style medium))
        (changed-line-style line-style-p)
        (changed-text-style text-style-p))
    (unwind-protect
        (progn
          (when (eq ink old-ink) (setf ink nil))
          
	  (when ink
	      (setf (medium-ink medium) ink))
	  (when transformation
	      (setf (medium-transformation medium)
		(compose-transformations old-transform transformation)))

          (when (and clipping-region old-clip
                     (or (eq clipping-region +everywhere+)
                         (eq clipping-region old-clip)
                         (region-contains-region-p clipping-region old-clip))
                     #+NIL (region-equal clipping-region old-clip))
            (setf clipping-region nil))

          (when clipping-region
            (setf (medium-clipping-region medium)
                  (region-intersection (if transformation
                                           (transform-region transformation old-clip)
                                           old-clip)
                                       clipping-region)))
          (when (null line-style)
              (setf line-style old-line-style))
	  (when (or line-unit line-thickness dashes-p line-joint-shape line-cap-shape)
            (setf changed-line-style t)
            (setf line-style (make-line-style
                              :unit (or line-unit
                                        (line-style-unit line-style))
                              :thickness (or line-thickness
                                             (line-style-thickness line-style))
                              :dashes (or line-dashes
                                          (line-style-dashes line-style))
                              :joint-shape (or line-joint-shape
                                               (line-style-joint-shape line-style))
                              :cap-shape (or line-cap-shape
                                             (line-style-cap-shape line-style)))))
          (when changed-line-style
            (setf (medium-line-style medium) line-style))
	  (if text-style-p
	      (setf text-style (merge-text-styles text-style
						  (medium-merged-text-style medium)))
              (setf text-style (medium-merged-text-style medium)))
	  (when (or text-family-p text-face-p text-size-p)
            (setf changed-text-style t)
            (setf text-style (merge-text-styles (make-text-style text-family
                                                                 text-face
                                                                 text-size)
                                                text-style)))
          (when changed-text-style
            (setf (medium-text-style medium) text-style))
          
	  (when orig-medium
	    (funcall func orig-medium)))
        
      (when ink
	(setf (medium-ink medium) old-ink))
      ;; First set transformation, then clipping!
      (when transformation
	(setf (medium-transformation medium) old-transform))
      (when clipping-region
	(setf (medium-clipping-region medium) old-clip))
      (when changed-line-style
        (setf (medium-line-style medium) old-line-style))
      (when changed-text-style 
        (setf (medium-text-style medium) old-text-style)))))

(defmacro with-medium-options ((sheet args)
			       &body body)
  `(flet ((graphics-op (medium)
	    (declare (ignorable medium))
	    ,@body))
     #-clisp (declare (dynamic-extent #'graphics-op))
     (apply #'do-graphics-with-options ,sheet #'graphics-op ,args)))

(defmacro with-drawing-options ((medium &rest drawing-options) &body body)
  (setq medium (stream-designator-symbol medium '*standard-output*))
  (with-gensyms (gcontinuation cont-arg)
    `(flet ((,gcontinuation (,cont-arg)
	      (declare (ignore ,cont-arg))
	      ,@body))
       #-clisp (declare (dynamic-extent #',gcontinuation))
       (apply #'invoke-with-drawing-options
	      ,medium #',gcontinuation (list ,@drawing-options)))))


(defmethod invoke-with-drawing-options ((medium medium) continuation
                                        &rest drawing-options
                                        &key ink transformation clipping-region
                                        line-style text-style
					&allow-other-keys)
  (declare (ignore ink transformation clipping-region line-style text-style))
  (with-medium-options (medium drawing-options)
    (funcall continuation medium)))

(defmethod invoke-with-drawing-options ((sheet sheet) continuation &rest drawing-options)
  (with-sheet-medium (medium sheet)
    (with-medium-options (medium drawing-options)
      (funcall continuation medium))))

;;; Compatibility with real CLIM
(defmethod invoke-with-drawing-options ((sheet t) continuation
					&rest drawing-options)
  (declare (ignore drawing-options))
  (funcall continuation sheet))

(defmethod invoke-with-identity-transformation
    ((sheet sheet) continuation)
  (with-sheet-medium (medium sheet)
    (letf (((medium-transformation medium) +identity-transformation+))
      (funcall continuation sheet))))


(defmethod invoke-with-identity-transformation
    ((destination pixmap) continuation)
  (with-pixmap-medium (medium destination)
    (letf (((medium-transformation medium) +identity-transformation+))
      (funcall continuation destination))))

(defmethod invoke-with-identity-transformation
    ((medium medium) continuation)
  (letf (((medium-transformation medium) +identity-transformation+))
    (funcall continuation medium)))

(defmethod invoke-with-local-coordinates (medium cont x y)
  ;; For now we do as real CLIM does.
  ;; Default seems to be the cursor position.
  ;; Moore suggests we use (0,0) if medium is no stream.
  ;;
  ;; Further the specification is vague about possible scalings ...
  ;;
  (unless (and x y)
    (multiple-value-bind (cx cy) (if (extended-output-stream-p medium)
                                     (stream-cursor-position medium)
                                     (values 0 0))
      (setf x (or x cx)
            y (or y cy))))
  (multiple-value-bind (mxx mxy myy myx tx ty)
      (get-transformation (medium-transformation medium))
    (declare (ignore tx ty))
    (with-identity-transformation (medium)
      (with-drawing-options
          (medium :transformation (make-transformation
                                   mxx mxy myy myx
                                   x y))
        (funcall cont medium)))))

(defmethod invoke-with-first-quadrant-coordinates (medium cont x y)
  ;; First we do the same as invoke-with-local-coordinates but rotate and
  ;; deskew it so that it becomes first-quadrant. We do this
  ;; by simply measuring the length of the transfomed x and y "unit vectors". 
  ;; [That is (0,0)-(1,0) and (0,0)-(0,1)] and setting up a transformation
  ;; which features an upward pointing y-axis and a right pointing x-axis with
  ;; a length equal to above measured vectors.
  (unless (and x y)
    (multiple-value-bind (cx cy) (if (extended-output-stream-p medium)
                                     (stream-cursor-position medium)
                                     (values 0 0))
      (setf x (or x cx)
            y (or y cy))))
  (let* ((tr (medium-transformation medium))
         (xlen
          (multiple-value-bind (dx dy) (transform-distance tr 1 0)
            (sqrt (+ (expt dx 2) (expt dy 2)))))
         (ylen
          (multiple-value-bind (dx dy) (transform-distance tr 0 1)
            (sqrt (+ (expt dx 2) (expt dy 2))))))
    (with-identity-transformation (medium)
      (with-drawing-options
          (medium :transformation (make-transformation
                                   xlen 0 0 (- ylen)
                                   x y))
        (funcall cont medium)))))

(defun draw-point (sheet point
		   &rest args
		   &key ink clipping-region transformation
			line-style line-thickness line-unit)
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness line-unit))
  (with-medium-options (sheet args)
    (multiple-value-bind (x y) (point-position point)
      (medium-draw-point* medium x y))))

(defun draw-point* (sheet x y
		    &rest args
		    &key ink clipping-region transformation
			 line-style line-thickness line-unit)
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness line-unit))
  (with-medium-options (sheet args)
    (medium-draw-point* medium x y)))

(defun expand-point-seq (point-seq)
  (let ((coord-seq nil))
    (do-sequence (point point-seq)
      (multiple-value-bind (x y) (point-position point)
	(setq coord-seq (list* y x coord-seq))))
    (nreverse coord-seq)))

(defun draw-points (sheet point-seq
		    &rest args
		    &key ink clipping-region transformation
			 line-style line-thickness line-unit)
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness line-unit))
  (with-medium-options (sheet args)
    (medium-draw-points* medium (expand-point-seq point-seq))))

(defun draw-points* (sheet coord-seq
		     &rest args
		     &key ink clipping-region transformation
			  line-style line-thickness line-unit)
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness line-unit))
  (with-medium-options (sheet args)
    (medium-draw-points* medium coord-seq)))

(defun draw-line (sheet point1 point2
		  &rest args
		  &key ink clipping-region transformation line-style line-thickness
		       line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (multiple-value-bind (x1 y1) (point-position point1)
      (multiple-value-bind (x2 y2) (point-position point2)
	(medium-draw-line* medium x1 y1 x2 y2)))))

(defun draw-line* (sheet x1 y1 x2 y2
		   &rest args
		   &key ink clipping-region transformation line-style line-thickness
			line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-line* medium x1 y1 x2 y2)))

(defun draw-lines (sheet point-seq
		   &rest args
		   &key ink clipping-region transformation line-style line-thickness
			line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-lines* medium (expand-point-seq point-seq))))

(defun draw-lines* (sheet coord-seq
		    &rest args
		    &key ink clipping-region transformation line-style line-thickness
			 line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-lines* medium coord-seq)))

(defun draw-polygon (sheet point-seq
		     &rest args
		     &key (filled t) (closed t)
			  ink clipping-region transformation line-style line-thickness
			  line-unit line-dashes line-joint-shape line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-polygon* medium (expand-point-seq point-seq) closed filled)))

(defun draw-polygon* (sheet coord-seq
		    &rest args
		    &key (filled t) (closed t)
			 ink clipping-region transformation line-style line-thickness
			 line-unit line-dashes line-joint-shape line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-polygon* medium coord-seq closed filled)))

(defun draw-rectangle (sheet point1 point2
			&rest args
			&key (filled t)
			     ink clipping-region transformation line-style line-thickness
			     line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape))
  (with-medium-options (sheet args)
    (multiple-value-bind (x1 y1) (point-position point1)
      (multiple-value-bind (x2 y2) (point-position point2)
	(medium-draw-rectangle* medium x1 y1 x2 y2 filled)))))

(defun draw-rectangle* (sheet x1 y1 x2 y2
			&rest args
			&key (filled t)
			     ink clipping-region transformation line-style line-thickness
			     line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape))
  (with-medium-options (sheet args)
    (medium-draw-rectangle* medium x1 y1 x2 y2 filled)))

(defun draw-rectangles (sheet points
                        &rest args
                        &key (filled t)
                        ink clipping-region transformation line-style line-thickness
			line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape))
  (with-medium-options (sheet args)
    (loop for point in points
	  nconcing (multiple-value-bind (x y) (point-position point)
		     (list x y)) into position-seq
	  finally (medium-draw-rectangles* medium position-seq filled))))

(defun draw-rectangles* (sheet position-seq
                         &rest args
                         &key (filled t)
                         ink clipping-region transformation line-style line-thickness
                         line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-joint-shape))
  (with-medium-options (sheet args)
    (medium-draw-rectangles* medium position-seq filled)))

(defun draw-triangle (sheet point1 point2 point3
                      &rest args
                      &key (filled t)
                      ink clipping-region transformation line-style line-thickness
                      line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
                   line-unit line-dashes line-joint-shape))
  (apply #'draw-polygon sheet (list point1 point2 point3) :filled filled :closed t args))

(defun draw-triangle* (sheet x1 y1 x2 y2 x3 y3
                       &rest args
                       &key (filled t)
                       ink clipping-region transformation line-style line-thickness
                       line-unit line-dashes line-joint-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
                   line-unit line-dashes line-joint-shape))
  (apply #'draw-polygon* sheet (list x1 y1 x2 y2 x3 y3) :filled filled :closed t args))

(defun draw-ellipse (sheet
		     center-point
		     radius-1-dx radius-1-dy radius-2-dx radius-2-dy
		     &rest args
		     &key (filled t) (start-angle 0.0) (end-angle (* 2.0 pi))
			  ink clipping-region transformation
			  line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (multiple-value-bind (center-x center-y) (point-position center-point)
      (medium-draw-ellipse* medium
			    center-x center-y
			    radius-1-dx radius-1-dy radius-2-dx radius-2-dy
			    start-angle end-angle filled))))

(defun draw-ellipse* (sheet
		      center-x center-y
		      radius-1-dx radius-1-dy radius-2-dx radius-2-dy
		      &rest args
		      &key (filled t) (start-angle 0.0) (end-angle (* 2.0 pi))
			   ink clipping-region transformation
			   line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-ellipse* medium
			  center-x center-y
			  radius-1-dx radius-1-dy radius-2-dx radius-2-dy
			  start-angle end-angle filled)))

(defun draw-circle (sheet
		    center-point radius
		    &rest args
		    &key (filled t) (start-angle 0.0) (end-angle (* 2.0 pi))
			 ink clipping-region transformation
			 line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (multiple-value-bind (center-x center-y) (point-position center-point)
      (medium-draw-ellipse* medium
			    center-x center-y
			    radius 0 0 radius
			    start-angle end-angle filled))))

(defun draw-circle* (sheet
		     center-x center-y radius
		     &rest args
		     &key (filled t) (start-angle 0.0) (end-angle (* 2.0 pi))
			  ink clipping-region transformation
			  line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (medium-draw-ellipse* medium
			  center-x center-y
			  radius 0 0 radius
			  start-angle end-angle filled)))

(defun draw-text (sheet string point
		   &rest args
		   &key (start 0) (end nil)
			(align-x :left) (align-y :baseline)
			(toward-point nil toward-point-p)
		        transform-glyphs
			ink clipping-region transformation
			text-style text-family text-face text-size)
  (declare (ignore ink clipping-region transformation
		   text-style text-family text-face text-size))
  (with-medium-options (sheet args)
    (multiple-value-bind (x y) (point-position point)
      (multiple-value-bind (toward-x toward-y)
	  (if toward-point-p
	      (point-position toward-point)
	      (values (1+ x) y))
        (medium-draw-text* medium string x y
                           start end
                           align-x align-y
                           toward-x toward-y transform-glyphs)))))

(defun draw-text* (sheet string x y
		   &rest args
		   &key (start 0) (end nil)
			(align-x :left) (align-y :baseline)
			(toward-x (1+ x)) (toward-y y) transform-glyphs
			ink clipping-region transformation
			text-style text-family text-face text-size)
  (declare (ignore ink clipping-region transformation
		   text-style text-family text-face text-size))
  (with-medium-options (sheet args)
    (medium-draw-text* medium string x y
		       start end
		       align-x align-y
		       toward-x toward-y transform-glyphs)))

;; This function belong to the extensions package.
(defun draw-glyph (sheet string x y
		   &rest args
		   &key (align-x :left) (align-y :baseline)
			toward-x toward-y transform-glyphs
			ink clipping-region transformation
			text-style text-family text-face text-size)
"Draws a single character of filled text represented by the given
element.  element is a character or other object to be translated
into a font index.  The given x and y specify the left baseline
position for the character."
  (declare (ignore ink clipping-region transformation
		   text-style text-family text-face text-size))
  (with-medium-options (sheet args)
    (medium-draw-glyph medium string x y
		       align-x align-y
		       toward-x toward-y transform-glyphs)))

(defun draw-arrow (sheet point-1 point-2
		   &rest args
		   &key ink clipping-region transformation
			line-style line-thickness
			line-unit line-dashes line-cap-shape
                        (to-head t) from-head (head-length 10) (head-width 5))
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness
		   line-unit line-dashes line-cap-shape
                   to-head from-head head-length head-width))
  (multiple-value-bind (x1 y1) (point-position point-1)
    (multiple-value-bind (x2 y2) (point-position point-2)
      (apply #'draw-arrow* sheet x1 y1 x2 y2 args))))

(defun draw-arrow* (sheet x1 y1 x2 y2
		   &rest args
		   &key ink clipping-region transformation
			line-style line-thickness
			line-unit line-dashes line-cap-shape
                        (to-head t) from-head (head-length 10) (head-width 5))
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (with-medium-options (sheet args)
    (with-translation (sheet x2 y2)
      (with-rotation (sheet (atan* (- x1 x2)
                                   (- y1 y2)))
        (let* ((end      0.0)
               (start    (sqrt (+ (expt (- x2 x1) 2)
                                  (expt (- y2 y1) 2))))
               (p end)
               (q start)

               (line-style (medium-line-style sheet))
               ;; FIXME: I believe this thickness is in "line-style-units",
               ;; which are only coincidentally the same as pixel coorindates
               ;; on screen backends, using :normal units. There is no function
               ;; documented for converting the units to stream coordinates.
               (thickness  (multiple-value-bind (dx dy)
                               (transform-distance (invert-transformation (medium-transformation sheet))
                                               (line-style-thickness line-style)
                                               0)
                             (sqrt (+ (* dx dx) (* dy dy)))))
               (width/2    (/ head-width 2))

               (a (atan (/ width/2 head-length)))
               (offset     (if (and head-length (not (zerop head-length)))
                               (/ thickness (* 2 (sin a )))
                               0.0))
               (tip-to-peak (+ head-length offset (- (* thickness 0.5 (sin a)))))) ;; okay, a guess..
         (when to-head   (incf p offset))
         (when from-head (decf q offset))
         (if (and to-head
                  from-head
                  (< (abs (- start end)) (* 2 tip-to-peak)))
             
             (let ((width (* 0.5 (+ head-width thickness)
                             (/ (abs (- start end))
                                (* 2 tip-to-peak)) )))
               (draw-polygon* sheet
                              (list end 0
                                    (/ start 2) width
                                    start 0
                                     (/ start 2) (- width))
                              :filled t
                              :line-thickness 0))
             (progn
               (when to-head
                 (draw-polygon* sheet
                                (list (+ p head-length) (- width/2)
                                      p 0
                                      (+ p head-length) width/2)
                                :filled nil
                                :closed nil))
               (when from-head
                 (draw-polygon* sheet
                                (list (- q head-length) (- width/2)
                                      q 0
                                      (- q head-length) width/2)
                                :filled nil
                                :closed nil))
               
               (unless (< q p)
                 (draw-line* sheet q 0 p 0)))) )))))


(defun draw-oval (sheet center-pt x-radius y-radius
		  &rest args
		  &key (filled t) ink clipping-region transformation
                       line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore filled ink clipping-region transformation
		   line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (multiple-value-bind (x1 y1) (point-position center-pt)
    (apply #'draw-oval* sheet x1 y1 x-radius y-radius args)))

(defun draw-oval* (sheet center-x center-y x-radius y-radius
                   &rest args
                   &key (filled t) ink clipping-region transformation
                         line-style line-thickness line-unit line-dashes line-cap-shape)
  (declare (ignore ink clipping-region transformation
		   line-style line-thickness
		   line-unit line-dashes line-cap-shape))
  (check-type x-radius (real 0))
  (check-type y-radius (real 0))
  (if (or (coordinate= x-radius 0) (coordinate= y-radius 0))
      (draw-circle* sheet center-x center-y (max x-radius y-radius)
                    :filled filled)
      (with-medium-options (sheet args)
        (if (coordinate<= y-radius x-radius)
            (let ((x1 (- center-x x-radius)) (x2 (+ center-x x-radius))
                  (y1 (- center-y y-radius)) (y2 (+ center-y y-radius)))
              (if filled
                  ;; Kludge coordinates, sometimes due to rounding the lines don't connect.
                  (draw-rectangle* sheet (floor x1) y1 (ceiling x2) y2)
                  (draw-lines* sheet (list (floor x1) y1 (ceiling x2) y1
                                           (floor x1) y2 (ceiling x2) y2)))
              (draw-circle* sheet x1 center-y y-radius
                            :filled filled
                            :start-angle (* pi 0.5)
                            :end-angle (* pi 1.5))
              (draw-circle* sheet x2 center-y y-radius
                            :filled filled
                            :start-angle (* pi 1.5)
                            :end-angle (* pi 2.5)))
            (with-rotation (sheet (/ pi 2) (make-point center-x center-y))
              (draw-oval* sheet center-x center-y y-radius x-radius
                          :filled filled)) ))))


;;; Pixmap functions

(defmethod copy-to-pixmap ((medium medium) medium-x medium-y width height
                           &optional pixmap (pixmap-x 0) (pixmap-y 0))
  (unless pixmap
    (setq pixmap (allocate-pixmap medium (+ pixmap-x width) (+ pixmap-y height))))
  (medium-copy-area medium medium-x medium-y width height
                    pixmap pixmap-x pixmap-y)
  pixmap)

(defmethod copy-to-pixmap ((sheet sheet) sheet-x sheet-y width height
                           &optional pixmap (pixmap-x 0) (pixmap-y 0))
  (copy-to-pixmap (sheet-medium sheet) sheet-x sheet-y width height
                  pixmap pixmap-x pixmap-y))

(defmethod copy-from-pixmap (pixmap pixmap-x pixmap-y width height
                             (medium medium) medium-x medium-y)
  (medium-copy-area pixmap pixmap-x pixmap-y width height
                    medium medium-x medium-y)
  pixmap)

(defmethod copy-from-pixmap (pixmap pixmap-x pixmap-y width height
                             (sheet sheet) sheet-x sheet-y)
  (medium-copy-area pixmap pixmap-x pixmap-y width height
                    (sheet-medium sheet) sheet-x sheet-y))

(defmethod copy-area ((medium medium) from-x from-y width height to-x to-y)
  (medium-copy-area medium from-x from-y width height
                    medium to-x to-y))

(defmethod copy-area ((sheet sheet) from-x from-y width height to-x to-y)
  (copy-area (sheet-medium sheet) from-x from-y width height to-x to-y))

(defmethod copy-area ((stream stream) from-x from-y width height to-x to-y)
  (if (sheetp stream)
      (copy-area (sheet-medium stream) from-x from-y width height to-x to-y)
    (error "COPY-AREA on a stream is not implemented")))

;;; XXX The modification of the sheet argument to hold the pixmap medium seems
;;; completely incorrect here; the description of the macro in the spec says
;;; nothing about that. On the other hand, the spec talks about "medium-var"
;;; when that is clearly meant to be a stream (and an output-recording stream
;;; at that, if the example in the Franz user guide is to be believed). What a
;;; mess. I think we need a pixmap output recording stream in order to do this
;;; right. -- moore
(defmacro with-output-to-pixmap ((medium-var sheet &key width height) &body body)
  (if (and width height)
      `(let* ((pixmap (allocate-pixmap ,sheet ,width ,height))
              (,medium-var (make-medium (port ,sheet) pixmap))
              (old-medium (sheet-medium ,sheet)))
         (setf (slot-value pixmap 'medium) ,medium-var) ; hmm, [seems to work] -- BTS
         (setf (%sheet-medium ,sheet) ,medium-var) ;is sheet a sheet-with-medium-mixin? --GB
         (unwind-protect
              (progn ,@body)
           (setf (%sheet-medium ,sheet) old-medium)) ;is sheet a sheet-with-medium-mixin? --GB
         pixmap)
      (let ((record (gensym "OUTPUT-RECORD-")))
        ;; rudi (2005-09-05) What to do when only width or height are
        ;; given?  And what's the meaning of medium-var?
        `(let* ((,medium-var ,sheet)
                (,record (with-output-to-output-record (,medium-var)
                           ,@body)))
           (with-output-to-pixmap
               (,medium-var 
                ,sheet
                :width ,(or width `(bounding-rectangle-width ,record))
                :height ,(or height `(bounding-rectangle-height ,record)))
             (replay-output-record ,record ,sheet))))))

;;; XXX This seems to be incorrect.
;;; This presumes that your drawing will completely fill the bounding rectangle
;;; of the sheet and will effectively randomize anything that isn't drawn
;;; within it.
;;; FIXME
#+nil
(defmacro with-double-buffering ((sheet) &body body)
  (let ((width (gensym))
	(height (gensym))
	(pixmap (gensym))
	(sheet-mirror (gensym)))
    `(let* ((,width (round (bounding-rectangle-width (sheet-region ,sheet))))
	    (,height (round (bounding-rectangle-height (sheet-region ,sheet))))
            (,(gensym) (progn (format *debug-io* "w-d-b ~A ~A~%" ,width ,height) (finish-output *debug-io*) 0))
	    (,pixmap (allocate-pixmap ,sheet ,width ,height))
	    (,sheet-mirror (sheet-direct-mirror ,sheet)))
       (unwind-protect
	   (progn
	     (setf (sheet-direct-mirror ,sheet) (pixmap-mirror ,pixmap))
	     ,@body
	     (setf (sheet-direct-mirror ,sheet) ,sheet-mirror)
             ; v-- the sheet might have been degrafted while we weren't looking
             ; in which case, we shouldn't blit it back
	     (copy-from-pixmap ,pixmap 0 0 ,width ,height ,sheet 0 0))
	 (deallocate-pixmap ,pixmap)))))

;;; Another attempt

(defun invoke-with-double-buffering (sheet continuation x1 y1 x2 y2)
  (let* ((medium (sheet-medium sheet))
	 (sheet-transform (sheet-native-transformation sheet))
	 (medium-transform (medium-transformation (sheet-medium sheet)))
	 (world-transform (compose-transformations sheet-transform
						   medium-transform)))
    (multiple-value-bind (sheet-x1 sheet-y1)
	(transform-position world-transform x1 y1)
      (multiple-value-bind (sheet-x2 sheet-y2)
	  (transform-position world-transform x2 y2)
	;; Be conservative with the size of the pixmap, including all of
	;; the pixels at the edges.
	(let* ((pixmap-x1 (floor sheet-x1))
	       (pixmap-y1 (floor sheet-y1))
	       (pixmap-x2 (ceiling sheet-x2))
	       (pixmap-y2 (ceiling sheet-y2))
	       (pixmap-width (- pixmap-x2 pixmap-x1))
	       (pixmap-height (- pixmap-y2 pixmap-y1))
	       (current-sheet-region (sheet-region sheet))
	       (sheet-native (compose-transformation-with-translation
			      sheet-transform
			      (- pixmap-x1)
			      (- pixmap-y1)))
	       (pixmap (allocate-pixmap sheet pixmap-width pixmap-height))
	       )
	  (unless pixmap
	    (error "Couldn't allocate pixmap")) 
	  (multiple-value-bind (user-pixmap-x1 user-pixmap-y1)
	      (untransform-position world-transform pixmap-x1 pixmap-y1)
	    (multiple-value-bind (user-pixmap-x2 user-pixmap-y2)
		(untransform-position world-transform pixmap-x2 pixmap-y2)
	      (flet ((set-native (transform region sheet)
		       (%%set-sheet-native-transformation transform sheet)
		       (setf (slot-value sheet 'region) region)
		       (invalidate-cached-regions sheet)
		       (invalidate-cached-transformations sheet)))
		;; Assume that the scaling for the sheet-native
		;; transformation for the pixmap will be the same as that of
		;; the mirror .
		(unwind-protect
		     (letf (((sheet-parent sheet) nil)
			    ((sheet-direct-mirror sheet)
			     (pixmap-mirror pixmap)))
		       (unwind-protect
			    (let ((pixmap-region
				   (make-bounding-rectangle user-pixmap-x1
							    user-pixmap-y1
							    user-pixmap-x2
							    user-pixmap-y2)))
			      (set-native sheet-native pixmap-region  sheet)
			      ;(break)
			      (with-drawing-options
				  (medium :ink (medium-background medium))
				
				(medium-draw-rectangle* medium
							user-pixmap-x1
							user-pixmap-y1
							user-pixmap-x2
							user-pixmap-y2
							t))
			      (funcall continuation sheet
				       user-pixmap-x1 user-pixmap-y1
				       user-pixmap-x2 user-pixmap-y2))
			 (set-native sheet-transform
				     current-sheet-region
				     sheet)))
		  (copy-from-pixmap pixmap 0 0
				    pixmap-width pixmap-height sheet
				    user-pixmap-x1 user-pixmap-y1)
		  (deallocate-pixmap pixmap))))))))))

(defmacro with-double-buffering (((sheet &rest bounds-args)
				  (&rest pixmap-args))
				 &body body)
  (with-gensyms (continuation)
    (let ((cont-form
	   (case (length pixmap-args)
	     (1
	      (with-gensyms (pixmap-x1 pixmap-y1 pixmap-x2 pixmap-y2)
		`(,continuation (,sheet
				 ,pixmap-x1 ,pixmap-y1 ,pixmap-x2 ,pixmap-y2)
		   (let ((,(car pixmap-args)
			  (make-bounding-rectangle ,pixmap-x1 ,pixmap-y1
						   ,pixmap-x2 ,pixmap-y2)))
		     ,@body))))
	     (4
	      `(,continuation (,sheet ,@pixmap-args)
		 ,@body))
	     (otherwise (error "Invalid pixmap-args ~S" pixmap-args)))))
      (case (length bounds-args)
	(1
	 (with-gensyms (x1 y1 x2 y2)
	   `(flet (,cont-form)
	      (declare (dynamic-extent #',continuation))
	      (with-bounding-rectangle* (,x1 ,y1 ,x2 ,y2)
		  ,(car bounds-args)
		(invoke-with-double-buffering ,sheet
					      #',continuation
					      ,x1 ,y1 ,x2 ,y2)))))
	(4
	 `(flet (,cont-form)
	    (declare (dynamic-extent #',continuation))
	    (invoke-with-double-buffering ,sheet #',continuation
					  ,@bounds-args)))
	(otherwise (error "invalid bounds-args ~S" bounds-args))))))



;;; Generic graphic operation methods

(defmacro def-graphic-op (name (&rest args))
  (let ((method-name (symbol-concat '#:medium- name '*)))
    `(eval-when (:execute :load-toplevel :compile-toplevel)
       (defmethod ,method-name ((stream sheet) ,@args)
	 (with-sheet-medium (medium stream)
	   (,method-name medium ,@args))))))

(def-graphic-op draw-point (x y))
(def-graphic-op draw-points (coord-seq))
(def-graphic-op draw-line (x1 y1 x2 y2))
(def-graphic-op draw-lines (coord-seq))
(def-graphic-op draw-polygon (coord-seq closed filled))
(def-graphic-op draw-rectangle (left top right bottom filled))
(def-graphic-op draw-rectangles (position-seq filled))
(def-graphic-op draw-ellipse (center-x center-y
				  radius-1-dx radius-1-dy radius-2-dx radius-2-dy
				  start-angle end-angle filled))
(def-graphic-op draw-circle (center-x center-y radius start-angle end-angle filled))
(def-graphic-op draw-text (string x y
			       start end
			       align-x align-y
			       toward-x toward-y transform-glyphs))

;;; Some image junk...

(defmethod medium-free-image-design ((sheet sheet-with-medium-mixin) design)
  (medium-free-image-design (sheet-medium sheet) design))

(defmethod medium-draw-image-design* :before (current-medium design x y)
  (with-slots (medium medium-data) design
    (unless (eq medium current-medium)
      (when medium
	(medium-free-image-design medium design))
      (setf medium current-medium)
      (setf medium-data nil))))

(defmethod medium-draw-image-design*
    ((medium sheet-with-medium-mixin) design x y)
  (medium-draw-image-design* (sheet-medium medium) design x y))

;;;;
;;;; DRAW-DESIGN
;;;;

(defmethod draw-design (medium (design point) &rest options &key &allow-other-keys)
  (apply #'draw-point* medium (point-x design) (point-y design) options))

(defmethod draw-design (medium (design polyline) &rest options &key &allow-other-keys)
  (apply #'draw-polygon medium (polygon-points design)
         :closed (polyline-closed design)
         :filled nil
         options))

(defmethod draw-design (medium (design polygon) &rest options &key &allow-other-keys)
  (apply #'draw-polygon medium (polygon-points design)
         :closed (polyline-closed design)
         :filled t
         options))

(defmethod draw-design (medium (design line) &rest options &key &allow-other-keys)
  (multiple-value-bind (x1 y1) (line-start-point* design)
    (multiple-value-bind (x2 y2) (line-end-point* design)
      (apply #'draw-line* medium x1 y1 x2 y2 options))))

(defmethod draw-design (medium (design rectangle) &rest options &key &allow-other-keys)
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* design)
    (apply #'draw-rectangle* medium x1 y1 x2 y2 options)))

(defmethod draw-design (medium (design ellipse) &rest options &key &allow-other-keys)
  (multiple-value-bind (cx cy) (ellipse-center-point* design)
    (multiple-value-bind (r1x r1y r2x r2y) (ellipse-radii design)
      (multiple-value-call #'draw-ellipse* medium
                           cx cy r1x r1y r2x r2y
                           :start-angle (ellipse-start-angle design)
                           :end-angle (ellipse-end-angle design)
                           options))))

(defmethod draw-design (medium (design elliptical-arc) &rest options &key &allow-other-keys)
  (multiple-value-bind (cx cy) (ellipse-center-point* design)
    (multiple-value-bind (r1x r1y r2x r2y) (ellipse-radii design)
      (multiple-value-call #'draw-ellipse* medium
                           cx cy r1x r1y r2x r2y
                           :start-angle (ellipse-start-angle design)
                           :end-angle (ellipse-end-angle design)
                           :filled nil
                           options))))

(defmethod draw-design (medium (design standard-region-union) &rest options &key &allow-other-keys)
  (map-over-region-set-regions (lambda (region)
                                 (apply #'draw-design medium region options))
                               design))

(defmethod draw-design (medium (design standard-rectangle-set) &rest options &key &allow-other-keys)
  ;; ### we can do better (faster) than this.
  (map-over-region-set-regions (lambda (region)
                                 (apply #'draw-design medium region options))
                               design))

#+nyi
(defmethod draw-design (medium (design standard-region-intersection) &rest options &key &allow-other-keys)
  )

#+nyi
(defmethod draw-design (medium (design standard-region-difference) &rest options &key &allow-other-keys)
  )

(defmethod draw-design (medium (design (eql +nowhere+)) &rest options &key &allow-other-keys)
  (declare (ignore medium options)
           (ignorable design))
  nil)

(defmethod draw-design ((medium sheet) (design (eql +everywhere+)) &rest options &key &allow-other-keys)
  (apply #'draw-design medium (bounding-rectangle (sheet-region medium)) options))

(defmethod draw-design ((medium medium) (design (eql +everywhere+)) &rest options &key &allow-other-keys)
  (apply #'draw-design medium (bounding-rectangle (sheet-region (medium-sheet medium))) options))

;;;

(defmethod draw-design (medium (color color) &rest options &key &allow-other-keys)
  (apply #'draw-design medium +everywhere+ :ink color options))

(defmethod draw-design (medium (color opacity) &rest options &key &allow-other-keys)
  (apply #'draw-design medium +everywhere+ :ink color options))

(defmethod draw-design (medium (color standard-flipping-ink) &rest options &key &allow-other-keys)
  (apply #'draw-design medium +everywhere+ :ink color options))

(defmethod draw-design (medium (color indirect-ink) &rest options &key &allow-other-keys)
  (apply #'draw-design medium +everywhere+ :ink color options))

;;;;

(defmethod draw-design
    (medium (design rgb-image-design) &rest options
     &key (x 0) (y 0) &allow-other-keys)
  (with-medium-options (medium options)
    (medium-draw-image-design* medium design x y))
  (values))

;;;;

(defmethod draw-design (medium (pattern pattern) &key clipping-region transformation
                        &allow-other-keys)
  (draw-pattern* medium pattern 0 0
   :clipping-region clipping-region :transformation transformation))

(defun draw-pattern* (medium pattern x y &key clipping-region transformation)
  ;; Note: I believe the sample implementation in the spec is incorrect.
  ;; --GB
  (check-type pattern pattern)
  (cond ((or clipping-region transformation)
         (with-drawing-options (medium :clipping-region clipping-region
                                       :transformation transformation)
           ;; Now this is totally bogus.
           (medium-draw-pattern* medium pattern x y) ))
        (t
         ;; What happens if there is already a transformation
         ;; applied to the medium?
         (medium-draw-pattern* medium pattern x y))))

(defmethod medium-draw-pattern* (medium pattern x y)
  (let ((width  (pattern-width pattern))
        (height (pattern-height pattern)))
    ;; As I read the spec, the pattern itself is not transformed, so
    ;; we should draw the full (untransformed) pattern at the tranformed
    ;; x/y coordinates. This requires we revert to the identity transformation
    ;; before drawing the rectangle. -Hefner
    (with-transformed-position ((medium-transformation medium) x y)
      (with-identity-transformation (medium)
	#+NIL ;; debugging aid.
	(draw-rectangle* medium x y (+ x width) (+ y height)
			 :filled t
			 :ink +red+)
	(draw-rectangle* medium x y (+ x width) (+ y height)
			 :filled t
			 :ink (transform-region
			       (make-translation-transformation x y)
			       pattern))))))

(defun draw-rounded-rectangle* (sheet x1 y1 x2 y2
                                      &rest args &key
                                      (radius 7)
                                      (radius-x radius)
                                      (radius-y radius)
                                      (radius-left  radius-x)
                                      (radius-right radius-x)
                                      (radius-top    radius-y)
                                      (radius-bottom radius-y)
                                      filled &allow-other-keys)
  "Draw a rectangle with rounded corners"

  (apply #'invoke-with-drawing-options sheet
    (lambda (medium)
      (declare (ignore medium))
      (let ((medium sheet))
        (if (not (and (>= (- x2 x1) (* 2 radius-x))
                      (>= (- y2 y1) (* 2 radius-y))))
            (draw-rectangle* medium x1 y1 x2 y2)       
            (with-grown-rectangle* ((ix1 iy1 ix2 iy2) (x1 y1 x2 y2)
                                    :radius-left   (- radius-left)
                                    :radius-right  (- radius-right)
                                    :radius-top    (- radius-top)
                                    :radius-bottom (- radius-bottom))
              (let ((zl (zerop radius-left))
                    (zr (zerop radius-right))
                    (zt (zerop radius-top))
                    (zb (zerop radius-bottom)))
                (if filled
                    (progn              ; Filled
                      (unless (or zl zt)
                        (draw-ellipse* medium ix1 iy1 radius-left  0 0 radius-top    :filled t))
                      (unless (or zr zt)
                        (draw-ellipse* medium ix2 iy1 radius-right 0 0 radius-top    :filled t))
                      (unless (or zl zb)
                        (draw-ellipse* medium ix1 iy2 radius-left  0 0 radius-bottom :filled t))
                      (unless (or zr zb)
                        (draw-ellipse* medium ix2 iy2 radius-right 0 0 radius-bottom :filled t))
                      (draw-rectangle* medium x1 iy1 x2 iy2 :filled t)
                      (draw-rectangle* medium ix1 y1 ix2 iy1 :filled t)
                      (draw-rectangle* medium ix1 iy2 ix2 y2 :filled t))                    
                    (progn              ; Unfilled
                      (unless (or zl zt)
                        (draw-ellipse* medium ix1 iy1 (- radius-left) 0 0 (- radius-top)
                                       :start-angle (/ pi 2) :end-angle pi
                                       :filled nil))
                      (unless (or zr zt)
                        (draw-ellipse* medium ix2 iy1 (- radius-right) 0 0 (- radius-top)
                                       :start-angle 0 :end-angle (/ pi 2)
                                       :filled nil))
                      (unless (or zl zb)
                        (draw-ellipse* medium ix1 iy2 (- radius-left) 0 0 (- radius-bottom)
                                       :start-angle pi :end-angle (* 3/2 pi)
                                       :filled nil))
                      (unless (or zr zb)
                        (draw-ellipse* medium ix2 iy2 (- radius-right) 0 0 (- radius-bottom)
                                       :start-angle (* 3/2 pi)
                                       :filled nil))
                      (labels ((fx (y p x1a x2a x1b x2b) (draw-line* medium (if p x1a x1b) y (if p x2a x2b) y))
                               (fy (x p y1a y2a y1b y2b) (draw-line* medium x (if p y1a y1b) x (if p y2a y2b))))
                        (fx y1 zt x1 x2 ix1 ix2)
                        (fy x1 zl y1 y2 iy1 iy2)
                        (fx y2 zb x1 x2 ix1 ix2)
                        (fy x2 zr y1 y2 iy1 iy2)))))))))
   (with-keywords-removed (args '(:radius :radius-x :radius-y
                                  :radius-left :radius-right
                                  :radius-top  :radius-bottom))
     args)))

;;; Bitmap images
;;;
;;; Based on CLIM 2.2, with an extension permitting the definition of
;;; new image formats by the user.

(defvar *bitmap-file-readers* (make-hash-table :test 'equalp)
  "A hash table mapping keyword symbols naming bitmap image
formats to a function that can read an image of that format. The
functions will be called with one argument, the pathname of the
file to be read. The functions should return two values as per
`read-bitmap-file'.")

(defmacro define-bitmap-file-reader (bitmap-format (&rest args) &body body)
  "Define a method for reading bitmap images of format
BITMAP-FORMAT that will be used by `read-bitmap-file' and
MAKE-PATTERN-FROM-BITMAP-FILE. BODY should return two values as
per `read-bitmap-file'."
  `(setf (gethash ,bitmap-format *bitmap-file-readers*)
         #'(lambda (,@args)
             ,@body)))

(defun bitmap-format-supported-p (format)
  "Return true if FORMAT is supported by `read-bitmap-file'."
  (not (null (gethash format *bitmap-file-readers*))))

(define-condition unsupported-bitmap-format (error)
  ((%format :reader bitmap-format
            :initarg :bitmap-format
            :initform (error "The bitmap format must be supplied")
            :documentation "The bitmap format that cannot be loaded"))
  (:report (lambda (condition stream)
             (format
              stream "Cannot read bitmap of unknown format \"~A\""
              (bitmap-format condition))))
  (:documentation "This exception is signalled when
`read-bitmap-file' is called on an bitmap of a type that no reader
has been defined for."))

(defun unsupported-bitmap-format (format)
  "Signal an error of type `unsupported-bitmap-format' for the
bitmap format `format'."
  (error 'unsupported-bitmap-format :bitmap-format format))

(defun read-bitmap-file (pathname &key (format :bitmap) (port (find-port)))
  "Read a bitmap file named by `pathname'. `Port' specifies the
port that the bitmap is to be used on. `Format' is a keyword
symbol naming any defined bitmap file format defined by
`clim-extensions:define-bitmap-file-reader'. Two values are
returned: a two-dimensional array of pixel values and an array of
either colors or color names. If the second value is non-NIL, the
pixel values are assumed to be indexes into this
array. Otherwise, the pixel values are taken to be RGB values
encoded in 32 bit unsigned integers, with the three most
significant octets being the values R, G and B, in order."
  (declare (ignore port)) ; XXX?
  (funcall (or (gethash format *bitmap-file-readers*)
               (unsupported-bitmap-format format))
           pathname))

(defun make-pattern-from-bitmap-file (pathname &key designs
                                      (format :bitmap) (port (find-port)))
  "Read a bitmap file named by `pathname'. `Port' specifies the
port that the bitmap is to be used on. `Format' is a keyword
symbol naming any defined bitmap file format defined by
`clim-extensions:define-bitmap-file-reader'. Two values are
returned: a two-dimensional array of pixel values and an array of
either colors or color names. If the second value is non-NIL, the
pixel values are assumed to be indexes into this
array. Otherwise, the pixel values are taken to be RGB values
encoded in 32 bit unsigned integers, with the three most
significant octets being the values R, G and B, in order."
  (multiple-value-bind (res read-designs)
      (read-bitmap-file pathname :format format :port port)
    (if read-designs
        (make-pattern res (or designs read-designs))
        (make-instance 'rgb-pattern :image (make-instance 'rgb-image
                                            :width (array-dimension res 1)
                                            :height (array-dimension res 0)
                                            :data res)))))

(define-bitmap-file-reader :xpm (pathname)
  (xpm-parse-file pathname))

(define-bitmap-file-reader :pixmap (pathname)
  (read-bitmap-file pathname :format :xpm))

(define-bitmap-file-reader :pixmap-3 (pathname)
  (read-bitmap-file pathname :format :xpm))