PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/rectangle.sc

https://bitbucket.org/bunny351/ezd
Scala | 216 lines | 202 code | 14 blank | 0 comment | 3 complexity | 8f5ec82cdbf36e176de4bbc28f84c4ca MD5 | raw file
  1. ;;; ezd - easy drawing for X11 displays.
  2. ;;;
  3. ;;; The procedures in this module generate the GRAPHIC objects representing
  4. ;;; rectangles and polygons.
  5. ;* Copyright 1990-1993 Digital Equipment Corporation
  6. ;* All Rights Reserved
  7. ;*
  8. ;* Permission to use, copy, and modify this software and its documentation is
  9. ;* hereby granted only under the following terms and conditions. Both the
  10. ;* above copyright notice and this permission notice must appear in all copies
  11. ;* of the software, derivative works or modified versions, and any portions
  12. ;* thereof, and both notices must appear in supporting documentation.
  13. ;*
  14. ;* Users of this software agree to the terms and conditions set forth herein,
  15. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  16. ;* right and license under any changes, enhancements or extensions made to the
  17. ;* core functions of the software, including but not limited to those affording
  18. ;* compatibility with other hardware or software environments, but excluding
  19. ;* applications which incorporate this software. Users further agree to use
  20. ;* their best efforts to return to Digital any such changes, enhancements or
  21. ;* extensions that they make and inform Digital of noteworthy uses of this
  22. ;* software. Correspondence should be provided to Digital at:
  23. ;*
  24. ;* Director of Licensing
  25. ;* Western Research Laboratory
  26. ;* Digital Equipment Corporation
  27. ;* 250 University Avenue
  28. ;* Palo Alto, California 94301
  29. ;*
  30. ;* This software may be distributed (but not offered for sale or transferred
  31. ;* for compensation) to third parties, provided such third parties agree to
  32. ;* abide by the terms and conditions of this notice.
  33. ;*
  34. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  35. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  36. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  37. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  38. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  39. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  40. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  41. ;* SOFTWARE.
  42. (module rectangle)
  43. (include "struct.sch")
  44. (include "commands.sch")
  45. (include "ginfo.sch")
  46. (include "display.sch")
  47. (include "view.sch")
  48. (include "psdraw.sch")
  49. (include "drawing.sch")
  50. (include "graphic.sch")
  51. (include "xternal.sch")
  52. ;;; A rectangle is generated by the following procedure.
  53. (define (RECTANGLE x y width height line-width color dash)
  54. (make-graphic
  55. #f
  56. (lambda ()
  57. (let ((ux1 (user->x x))
  58. (ux2 (user->x (+ x width)))
  59. (uy1 (user->y y))
  60. (uy2 (user->y (+ y height)))
  61. (lwdiv2 (quotient (user->lw line-width) 2)))
  62. (list (- (min ux1 ux2) lwdiv2)
  63. (- (min uy1 uy2) lwdiv2)
  64. (+ (max ux1 ux2) lwdiv2 1)
  65. (+ (max uy1 uy2) lwdiv2 1))))
  66. (if (eq? color 'clear)
  67. draw-clear
  68. (lambda ()
  69. (xdrawrectangle *dpy* *xwindow*
  70. (cv-gc (user->lw line-width) color #f dash #f #f)
  71. (min (user->x x) (user->x (+ x width)))
  72. (min (user->y y) (user->y (+ y height)))
  73. (user->width width) (user->height height))))
  74. (if (eq? color 'clear)
  75. draw-clear
  76. (lambda ()
  77. (pscolor color)
  78. (pscommand "newpath" x y "moveto" width 0 "rlineto"
  79. 0 height "rlineto" (- width) 0 "rlineto"
  80. "closepath")
  81. (psstroke (user->lw line-width) dash)))
  82. (lambda (minx miny maxx maxy)
  83. (let* ((lwdiv2 (quotient (user->lw line-width) 2))
  84. (in-minx (+ x (width->user lwdiv2)))
  85. (in-miny (+ y (height->user lwdiv2)))
  86. (in-maxx (+ x width (- (width->user lwdiv2))))
  87. (in-maxy (+ y height (- (height->user lwdiv2)))))
  88. (not (and (<= in-minx minx maxx in-maxx)
  89. (<= in-miny miny maxy in-maxy)))))))
  90. (define-ezd-command
  91. `(rectangle ,number? ,number? ,non-negative? ,non-negative?
  92. (optional ,non-negative?) (optional ,color?) (optional ,dash?))
  93. "(rectangle x y width height [<line-width>] [<color>] [dash])"
  94. rectangle)
  95. ;;; A filled rectangle is generated by the following procedure.
  96. (define (FILL-RECTANGLE x y width height color stipple)
  97. (make-graphic
  98. #f
  99. (lambda ()
  100. (let ((ux1 (user->x x))
  101. (ux2 (user->x (+ x width)))
  102. (uy1 (user->y y))
  103. (uy2 (user->y (+ y height))))
  104. (list (min ux1 ux2)
  105. (min uy1 uy2)
  106. (+ (max ux1 ux2) 1)
  107. (+ (max uy1 uy2) 1))))
  108. (if (eq? color 'clear)
  109. draw-clear
  110. (lambda ()
  111. (xfillrectangle *dpy* *xwindow*
  112. (cv-gc #f color stipple #f #f #f)
  113. (min (user->x x) (user->x (+ x width)))
  114. (min (user->y y) (user->y (+ y height)))
  115. (user->width width) (user->height height))))
  116. (if (eq? color 'clear)
  117. draw-clear
  118. (lambda ()
  119. (pscolor color)
  120. (pscommand "newpath" x y "moveto" width 0 "rlineto"
  121. 0 height "rlineto" (- width) 0 "rlineto" "closepath"
  122. "fill")))
  123. (lambda (minx miny max maxy) #t)))
  124. (define-ezd-command
  125. `(fill-rectangle ,number? ,number? ,non-negative? ,non-negative?
  126. (optional ,color?) (optional ,stipple?))
  127. "(fill-rectangle x y width height [<color>] [<stipple>])"
  128. fill-rectangle)
  129. ;;; A filled polygon is generated by the following procedure.
  130. (define (FILL-POLYGON points color stipple)
  131. (let ((buffer (make-string (* 4 (length points))))
  132. (point-count (length points)))
  133. (define (LOAD-BUFFER)
  134. (let loop ((i 0) (points points))
  135. (if points
  136. (let ((x (caar points))
  137. (y (cadar points)))
  138. (c-shortint-set! buffer i (user->x x))
  139. (c-shortint-set! buffer (+ i 2) (user->y y))
  140. (loop (+ i 4) (cdr points))))))
  141. (let loop ((pl points) (minx #f) (miny #f) (maxx #f) (maxy #f))
  142. (if pl
  143. (let ((x (caar pl))
  144. (y (cadar pl)))
  145. (loop (cdr pl) (bbmin x minx) (bbmin y miny)
  146. (bbmax x maxx) (bbmax y maxy)))
  147. (make-graphic
  148. #f
  149. (lambda ()
  150. (let ((ux1 (user->x minx))
  151. (ux2 (user->x maxx))
  152. (uy1 (user->y miny))
  153. (uy2 (user->y maxy)))
  154. (list (min ux1 ux2)
  155. (min uy1 uy2)
  156. (+ (max ux1 ux2) 1)
  157. (+ (max uy1 uy2) 1))))
  158. (if (eq? color 'clear)
  159. draw-clear
  160. (lambda ()
  161. (load-buffer)
  162. (xfillpolygon *dpy* *xwindow*
  163. (cv-gc #f color stipple #f #f #f)
  164. (cons 'xpointap buffer) point-count
  165. complex coordmodeorigin)))
  166. (if (eq? color 'clear)
  167. draw-clear
  168. (lambda ()
  169. (pscolor color)
  170. (pscommand "newpath")
  171. (let loop ((pl points) (cmd "moveto"))
  172. (when pl
  173. (pscommand (caar pl)
  174. (cadar pl) cmd)
  175. (loop (cdr pl) "lineto")))
  176. (pscommand "closepath" "fill")))
  177. (lambda (minx miny maxx maxy)
  178. (let ((region (begin (load-buffer)
  179. (xpolygonregion
  180. (cons 'xpointap buffer)
  181. point-count
  182. evenoddrule))))
  183. (define (IN? x y)
  184. (not (zero? (xpointinregion
  185. region (user->x x)
  186. (user->y y)))))
  187. (let ((in (or (in? minx miny)
  188. (in? minx maxy)
  189. (in? maxx miny)
  190. (in? maxx maxy)
  191. (in? (+ minx
  192. (/ (- maxx minx) 2))
  193. (+ miny
  194. (/ (- maxy miny)
  195. 2))))))
  196. (xdestroyregion region)
  197. in))))))))
  198. (define-ezd-command
  199. `(fill-polygon (repeat ,number? ,number?) (optional ,color?)
  200. (optional ,stipple?))
  201. "(fill-polygon x1 y1 ... xn yn [<color>] [<stipple>])"
  202. fill-polygon)