You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/pitfall/annotations.rkt

59 lines
1.8 KiB
Racket

#lang pitfall/racket
(provide annotation-mixin)
(define (annotation-mixin [% mixin-tester%])
(class %
(super-new)
(as-methods
annotate
link
_convertRect)))
(define/contract (annotate this x y w h options)
(number? number? number? number? hash? . ->m . object?)
(hash-set*! options
'Type "Annot"
'Rect (send this _convertRect x y w h)
'Border '(0 0 0))
(unless (equal? (· options Subtype) "Link")
(hash-ref! options 'C
(λ ()
(send this _normalizeColor (or (· options color) '(0 0 0))))))
(hash-remove! options 'color)
(when (string? (· options Dest)) (hash-update! options 'Dest String))
(for ([(k v) (in-hash options)])
(hash-set! options (string->symbol (string-titlecase (symbol->string k))) v))
(define annots-ref (send this ref options))
(send (· this page) annotations annots-ref)
(· annots-ref end)
this)
(define/contract (link this x y w h url [options (mhash)])
((number? number? number? number? string?) (hash?) . ->*m . object?)
(hash-set*! options
'Subtype "Link"
'A (send this ref (mhash 'S "URI"
'URI (String url))))
(send (· options A) end)
(send this annotate x y w h options))
(define/contract (_convertRect this x1 y1 w h)
(number? number? number? number? . ->m . (list/c number? number? number? number?))
;; flip y1 and y2
(let ([y2 y1]
[y1 (+ y1 h)]
[x2 (+ x1 w)])
(match-define (list m0 m1 m2 m3 m4 m5) (· this _ctm))
;; original code mutates x1 and y1 during transformation
;; I think this is wrong
(let* ([x1a (+ (* x1 m0) (* y1 m2) m4)]
[y1a (+ (* x1 m1) (* y1 m3) m5)]
[x2a (+ (* x2 m0) (* y2 m2) m4)]
[y2a (+ (* x2 m1) (* y2 m3) m5)])
(list x1a y1a x2a y2a))))