unstable/gui/pict: added scale-to

This commit is contained in:
Ryan Culpepper 2011-07-30 18:13:19 -05:00
parent efa8051a57
commit 2a0153cadb
2 changed files with 51 additions and 0 deletions

View File

@ -356,6 +356,33 @@
[pin-arrow-label-line pin-arrow-label-line-contract]
[pin-arrows-label-line pin-arrow-label-line-contract])
;; the following are by ryanc
(define (scale-to p w h #:mode [mode 'preserve])
(let* ([w0 (pict-width p)]
[h0 (pict-height p)]
[wfactor0 (if (zero? w0) 1 (/ w w0))]
[hfactor0 (if (zero? h0) 1 (/ h h0))])
(let-values ([(wfactor hfactor)
(case mode
((preserve inset)
(let ([factor (min wfactor0 hfactor0)])
(values factor factor)))
((distort)
(values wfactor0 hfactor0)))])
(let ([scaled-pict (scale p wfactor hfactor)])
(case mode
((inset)
(cc-superimpose (blank w h) scaled-pict))
(else
scaled-pict))))))
(provide/contract
[scale-to
(->* (pict? real? real?)
(#:mode (or/c 'preserve 'inset 'distort))
pict?)])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Tagged picts

View File

@ -93,6 +93,30 @@ Extends @racket[pict]'s bounding box to a minimum @racket[width] and/or
]
}
@defproc[(scale-to [pict pict?]
[width real?]
[height real?]
[#:mode mode (or/c 'preserve 'inset 'distort) 'preserve])
pict?]{
Scales @racket[pict] so that its width and height are at most
@racket[width] and @racket[height], respectively. If @racket[mode] is
@racket['preserve], the width and height are scaled by the same factor
so @racket[pict]'s aspect ratio is preserved; the result's bounding
box may be smaller than @racket[width] by @racket[height]. If
@racket[mode] is @racket['inset], the aspect ratio is preserved as
with @racket['preserve], but the resulting pict is centered in a
bounding box of exactly @racket[width] by @racket[height]. If
@racket[mode] is @racket['distort], the width and height are scaled
separately.
@examples[#:eval the-eval
(frame (scale-to (circle 100) 40 20))
(frame (scale-to (circle 100) 40 20 #:mode 'inset))
(frame (scale-to (circle 100) 40 20 #:mode 'distort))
]
}
@subsection{Conditional Manipulations}
These pict transformers all take boolean arguments that determine whether to