|
| 1 | +#lang racket |
| 2 | +(require (for-syntax syntax/parse racket/syntax) |
| 3 | + (only-in pict/convert pict-convertible?) |
| 4 | + (prefix-in p: pict) |
| 5 | + pict) |
| 6 | + |
| 7 | +(define-syntax (define-rhombus stx) |
| 8 | + (syntax-parse stx |
| 9 | + [(_ mod x:id) |
| 10 | + (define len (string-length (symbol->string (syntax-e #'x)))) |
| 11 | + (define r: |
| 12 | + (format-id #'x #:source #'x "r:~a" (syntax-e #'x))) |
| 13 | + #`(begin |
| 14 | + ;; just check that the function actually exists |
| 15 | + (when (module-declared? 'mod #t) |
| 16 | + (void (dynamic-require 'mod 'x))) |
| 17 | + (define-syntax (#,(syntax-property |
| 18 | + r: |
| 19 | + 'sub-range-binders |
| 20 | + (vector (syntax-local-introduce r:) 2 len |
| 21 | + (syntax-local-introduce #'x) 0 len)) |
| 22 | + stx) |
| 23 | + (syntax-parse stx |
| 24 | + [x:id (syntax/loc stx (thunk))] |
| 25 | + [(_ args (... ...)) |
| 26 | + ;; assume we're always using the same #%app |
| 27 | + (syntax/loc stx ((thunk) args (... ...)))])) |
| 28 | + (define (thunk) |
| 29 | + (dynamic-require 'mod 'x)))] |
| 30 | + [(_ mod x:id ...) |
| 31 | + #'(begin (define-rhombus mod x) ...)])) |
| 32 | + |
| 33 | + |
| 34 | +(provide |
| 35 | + pict-convertible? |
| 36 | + (rename-out |
| 37 | + [p:pict-width pict-width] |
| 38 | + [p:pict-height pict-height] |
| 39 | + [p:pict? pict?] |
| 40 | + [p:draw-pict draw-pict] |
| 41 | + [p:dc dc] |
| 42 | + [p:text-style/c text-style/c] |
| 43 | + [p:blank blank] |
| 44 | + [p:pict-descent pict-descent] |
| 45 | + [p:text text] |
| 46 | + [p:dc-for-text-size dc-for-text-size] |
| 47 | + [p:filled-rectangle filled-rectangle])) |
| 48 | + |
| 49 | +(define-rhombus |
| 50 | + (lib "pict/main.rhm") |
| 51 | + rectangle ;; just temporary |
| 52 | + beside |
| 53 | + stack |
| 54 | + overlay |
| 55 | + line) |
| 56 | + |
| 57 | +(define-rhombus |
| 58 | + rhombus/dot |
| 59 | + dynamic-dot-ref) |
| 60 | + |
| 61 | +(define-syntax (choose stx) |
| 62 | + (syntax-parse stx |
| 63 | + [(_ racket-pict:expr rhombus-pict:expr) |
| 64 | + #'(choose/proc (λ () racket-pict) |
| 65 | + (λ () rhombus-pict))])) |
| 66 | +(define (rhombus-present?) |
| 67 | + (module-declared? '(lib "pict/main.rhm") #t)) |
| 68 | +(define (choose/proc racket-pict rhombus-pict) |
| 69 | + (cond |
| 70 | + [(rhombus-present?) |
| 71 | + (rhombus-pict)] |
| 72 | + [else |
| 73 | + (racket-pict)])) |
| 74 | + |
| 75 | +(provide horizontal-line) |
| 76 | +(define (horizontal-line w) |
| 77 | + (choose |
| 78 | + (p:frame (p:blank w 0)) |
| 79 | + (r:line #:dx w))) |
| 80 | + |
| 81 | +(define-syntax-rule |
| 82 | + (define-simple name p:name r:name args ...) |
| 83 | + (define (name args ...) |
| 84 | + (choose |
| 85 | + (p:name args ...) |
| 86 | + (r:name args ...)))) |
| 87 | + |
| 88 | +(define-syntax (define-simple-dot stx) |
| 89 | + (syntax-parse stx |
| 90 | + [(_ name p-name:id pict args ...) |
| 91 | + #`(begin |
| 92 | + (provide name) |
| 93 | + (define (name pict args ...) |
| 94 | + (choose |
| 95 | + (p-name pict args ...) |
| 96 | + ((r:dynamic-dot-ref pict 'name) args ...))))])) |
| 97 | + |
| 98 | +(define-simple-dot ghost p:ghost p) |
| 99 | +(define-simple-dot launder p:launder p) |
| 100 | +(define-simple-dot refocus p:refocus p sub) |
| 101 | +(define-simple-dot colorize p:colorize p color) |
| 102 | + |
| 103 | +(provide inset) |
| 104 | +(define inset |
| 105 | + (case-lambda |
| 106 | + [(p amt) (choose (p:inset amt) ((r:dynamic-dot-ref p 'pad) amt))] |
| 107 | + [(p horiz vert) |
| 108 | + (choose (p:inset horiz vert) |
| 109 | + ((r:dynamic-dot-ref p 'pad) #:horiz horiz #:vert vert))] |
| 110 | + [(p l t r b) |
| 111 | + (choose (p:inset l t r b) |
| 112 | + ((r:dynamic-dot-ref p 'pad) #:left l #:top t #:right r #:bottom b))])) |
| 113 | + |
| 114 | +(define-syntax (define-append stx) |
| 115 | + (syntax-parse stx |
| 116 | + [(_ name racket-name rhombus-name rhombus-kwd rhombus-kwd-value) |
| 117 | + #'(begin |
| 118 | + (provide name) |
| 119 | + (define (name arg1 . args) |
| 120 | + (printf ">> ~s ~s ~s\n" name rhombus-name (cons arg1 args)) |
| 121 | + (choose |
| 122 | + (apply racket-name arg1 args) |
| 123 | + (keyword-apply rhombus-name |
| 124 | + (if (keyword<? 'rhombus-kwd '#:sep) |
| 125 | + (list 'rhombus-kwd '#:sep) |
| 126 | + (list '#:sep 'rhombus-kwd)) |
| 127 | + (if (keyword<? 'rhombus-kwd '#:sep) |
| 128 | + (list rhombus-kwd-value (if (number? arg1) arg1 0)) |
| 129 | + (list (if (number? arg1) arg1 0) rhombus-kwd-value)) |
| 130 | + (if (number? arg1) |
| 131 | + args |
| 132 | + (let ([ans (cons arg1 args)]) |
| 133 | + (printf "?? ~s\n" ans) |
| 134 | + ans))))))])) |
| 135 | + |
| 136 | +(define-append ht-append |
| 137 | + p:ht-append |
| 138 | + r:beside #:vert 'top) |
| 139 | +(define-append htl-append |
| 140 | + p:htl-append |
| 141 | + r:beside #:vert 'topline) |
| 142 | +(define-append hc-append |
| 143 | + p:hc-append |
| 144 | + r:beside #:vert 'center) |
| 145 | +(define-append hbl-append |
| 146 | + p:hbl-append |
| 147 | + r:beside #:vert 'baseline) |
| 148 | +(define-append hb-append |
| 149 | + p:hb-append |
| 150 | + r:beside #:vert 'bottom) |
| 151 | +(define-append vc-append |
| 152 | + p:vc-append |
| 153 | + r:stack #:horiz 'center) |
| 154 | +(define-append vl-append |
| 155 | + p:vl-append |
| 156 | + r:stack #:horiz 'left) |
| 157 | +(define-append vr-append |
| 158 | + p:vl-append |
| 159 | + r:stack #:horiz 'right) |
| 160 | + |
| 161 | +(define-syntax (define-superimpose stx) |
| 162 | + (syntax-parse stx |
| 163 | + [(_ name racket-name horiz vert) |
| 164 | + #'(begin |
| 165 | + (provide name) |
| 166 | + (define (name . args) |
| 167 | + (printf "calling ~s with ~s\n" name args) |
| 168 | + (choose |
| 169 | + (apply racket-name args) |
| 170 | + (apply r:overlay #:horiz horiz #:vert vert args))))])) |
| 171 | + |
| 172 | +(define-superimpose lt-superimpose p:lt-superimpose 'left 'top) |
| 173 | +(define-superimpose ltl-superimpose p:ltl-superimpose 'left 'topline) |
| 174 | +(define-superimpose lc-superimpose p:lc-superimpose 'left 'center) |
| 175 | +(define-superimpose lbl-superimpose p:lbl-superimpose 'left 'baseline) |
| 176 | +(define-superimpose lb-superimpose p:lb-superimpose 'left 'bottom) |
| 177 | + |
| 178 | +(define-superimpose ct-superimpose p:ct-superimpose 'center 'top) |
| 179 | +(define-superimpose ctl-superimpose p:ctl-superimpose 'center 'topline) |
| 180 | +(define-superimpose cc-superimpose p:cc-superimpose 'center 'center) |
| 181 | +(define-superimpose cbl-superimpose p:cbl-superimpose 'center 'baseline) |
| 182 | +(define-superimpose cb-superimpose p:cb-superimpose 'center 'bottom) |
| 183 | + |
| 184 | +(define-superimpose rt-superimpose p:rt-superimpose 'right 'top) |
| 185 | +(define-superimpose rtl-superimpose p:rtl-superimpose 'right 'topline) |
| 186 | +(define-superimpose rc-superimpose p:rc-superimpose 'right 'center) |
| 187 | +(define-superimpose rbl-superimpose p:rbl-superimpose 'right 'baseline) |
| 188 | +(define-superimpose rb-superimpose p:rb-superimpose 'right 'bottom) |
| 189 | + |
| 190 | +(define-namespace-anchor ns-anchor) |
| 191 | +(define ns (namespace-anchor->namespace ns-anchor)) |
| 192 | +(define r:find |
| 193 | + (cond |
| 194 | + [(rhombus-present?) |
| 195 | + (parameterize ([current-namespace ns]) |
| 196 | + (namespace-require '(all-except rhombus #%top)) |
| 197 | + (namespace-require 'rhombus/parse) |
| 198 | + (namespace-require 'redex/private/rhombus-bridge) |
| 199 | + ;(eval `(rhombus-top (group def π (op =) 3.14))) |
| 200 | + ;(eval 'π) |
| 201 | + (eval 'find))] |
| 202 | + [else |
| 203 | + #f])) |
| 204 | +(define-syntax (define-finder stx) |
| 205 | + (syntax-parse stx |
| 206 | + [(_ name racket-name horiz vert) |
| 207 | + #`(begin |
| 208 | + (provide name) |
| 209 | + (define (name pict subpict) |
| 210 | + (unless (pict? subpict) |
| 211 | + (error 'name "the version in pict-interface.rkt supports only picts, not pict paths")) |
| 212 | + (choose |
| 213 | + (racket-name pict subpict) |
| 214 | + (r:find pict subpict horiz vert))) |
| 215 | + (hash-set! finder-table |
| 216 | + name |
| 217 | + (cons horiz vert)))])) |
| 218 | +(define finder-table (make-hash)) |
| 219 | + |
| 220 | +(define-finder lt-find p:lt-find 'left 'top) |
| 221 | +(define-finder ltl-find p:lt-find 'left 'topline) |
| 222 | +(define-finder lc-find p:lt-find 'left 'center) |
| 223 | +(define-finder lbl-find p:lt-find 'left 'baseline) |
| 224 | +(define-finder lb-find p:lt-find 'left 'bottom) |
| 225 | + |
| 226 | +(define-finder ct-find p:lt-find 'center 'top) |
| 227 | +(define-finder ctl-find p:lt-find 'center 'topline) |
| 228 | +(define-finder cc-find p:lt-find 'center 'center) |
| 229 | +(define-finder cbl-find p:lt-find 'center 'baseline) |
| 230 | +(define-finder cb-find p:lt-find 'center 'bottom) |
| 231 | + |
| 232 | +(define-finder rt-find p:lt-find 'right 'top) |
| 233 | +(define-finder rtl-find p:lt-find 'right 'topline) |
| 234 | +(define-finder rc-find p:lt-find 'right 'center) |
| 235 | +(define-finder rbl-find p:lt-find 'right 'baseline) |
| 236 | +(define-finder rb-find p:lt-find 'right 'bottom) |
| 237 | + |
| 238 | +(provide pin-over) |
| 239 | +(define (pin-over base dx dy pict) |
| 240 | + (choose |
| 241 | + (p:pin-over base dx dy pict) |
| 242 | + (cond |
| 243 | + [(real? dx) |
| 244 | + (r:overlay base #:dx dx #:dy dy pict)] |
| 245 | + [else |
| 246 | + (define hv (hash-ref finder-table dy)) |
| 247 | + (define-values (dx dy) (r:find base dx (car hv) (cdr hv))) |
| 248 | + (r:overlay base #:dx dx #:dy dy pict)]))) |
0 commit comments