aydar.js
2019-1-1 16:17:37

Thank you very much


soegaard2
2019-1-1 22:15:26

Can anyone explain why this program results in different numbers each time it is run?


soegaard2
2019-1-1 22:15:36

#lang racket (require pict) (send (let/ec return (pict->bitmap (dc (λ (dc dx dy) (return (send dc get-pen))) 1 1))) get-width)


alexharsanyi
2019-1-1 23:10:17

The dc pict is called twice, the fist time with an un-initialized draw context whose pen returns a different width each time. The second time, dc is called with a device context that has a default pen with a width of 1. Try running this program several times:


alexharsanyi
2019-1-1 23:10:27
#lang racket
(require pict)
(pict->bitmap (dc (λ (dc dx dy) (printf "*** ~a~%" (send (send dc get-pen) get-width))) 1 1))

soegaard2
2019-1-1 23:12:06

@alexharsanyi Thanks!


soegaard2
2019-1-1 23:14:05

Hmm. Wonder why it is called twice.


soegaard2
2019-1-1 23:14:52

That doesn’t seem right.


alexharsanyi
2019-1-1 23:20:29

The first time it is called when the dc pict object is constructed. This code calls it once, and produces random values #lang racket (require pict) (define x (dc (λ (dc dx dy) (printf "*** ~a~%" (send (send dc get-pen) get-width))) 1 1))


alexharsanyi
2019-1-1 23:21:20

Evaluating x multiple times will than call the lambda with a device context that always has a pen of width 1


lexi.lambda
2019-1-1 23:55:35

IIRC the contract on dc invokes the function with some random values as a sort of fuzz check?


soegaard2
2019-1-2 00:01:03

So that means that all picts takes twice as much to to evaluate (than is strictly needed)?


soegaard2
2019-1-2 00:03:04

The contract is: [dc (->i ([draw (-> (is-a?/c dc<%>) real? real? any)] [w real?] [h real?]) ([d (or/c #f real?)] [a (or/c #f real?)]) #:pre/name (draw) "draw proc does not restore the dc state after being called" (does-draw-restore-the-state-after-being-called? draw) [p pict?])]


soegaard2
2019-1-2 00:05:04

Looks ok, I think.


soegaard2
2019-1-2 00:06:25

But I think you are on to something (wrt contracts).


soegaard2
2019-1-2 00:06:32

This results in 1 every time:


soegaard2
2019-1-2 00:06:37

(send (let/ec return (pict->bitmap (unsafe-dc (λ (dc dx dy) (return (send dc get-pen))) 1 1))) get-width)


soegaard2
2019-1-2 00:06:46

(changed dc to unsafe-dc)


alexharsanyi
2019-1-2 00:07:11

does-draw-restore-the-state-after-being-called? calls draw to check that it restores the state?


soegaard2
2019-1-2 00:08:18

Oh.: (define (does-draw-restore-the-state-after-being-called? draw) (define bdc (new bitmap-dc% [bitmap (make-bitmap 1 1)])) (prandomize-state bdc) (define old-state (get-dc-state bdc)) (draw bdc 0 0) (equal? (get-dc-state bdc) old-state))


alexharsanyi
2019-1-2 00:09:06
#lang racket
(require pict racket/draw)
(define p (send the-pen-list find-or-create-pen "black" 1 'solid))
(define x (dc (λ (dc dx dy) (send dc set-pen p)) 1 1))

alexharsanyi
2019-1-2 00:09:24

The above fails the contract even though the pict is not rendered


soegaard2
2019-1-2 00:11:10

The problem here is that the call (draw bdc 0 0) could take a long time.


soegaard2
2019-1-2 00:11:34

How often is does-draw-restore-the-state-after-being-called? called? Every time the pict is rendered?


florence
2019-1-2 00:12:33

@soegaard2 IIRC it’s called only when the contract is attached


florence
2019-1-2 00:12:40

but i may be misremembering


alexharsanyi
2019-1-2 00:13:52

based on the code examples above, it is called only when the pict is created, not when it is rendered


florence
2019-1-2 00:15:02

^ yeah that sounds right


soegaard2
2019-1-2 00:15:33

So what happens if I have a pict that when drawn relies on creating subpicts?


soegaard2
2019-1-2 00:16:01

Let’s say p is (above p1 p2).


soegaard2
2019-1-2 00:16:13

Where p1 and p2 are pictures to be created.


soegaard2
2019-1-2 00:16:36

Will the draw operation of, say, p1 be called twice or four times?


florence
2019-1-2 00:18:52

if p1 and p2 are created by your dc drawer, it will be called twice.


florence
2019-1-2 00:19:26

the draw operation is splatted directly into the picts internal drawing structure, and isn’t wrapped by a contract that calls does-draw-restore-the-state-after-being-called?



florence
2019-1-2 00:22:00

if you find it’s reeeallllyy slow, or the extra side effects are getting in the way, you could use unsafe-dc


soegaard2
2019-1-2 00:23:56

That or single out the case width=0 and height=0.


soegaard2
2019-1-2 00:24:55

I have quite a few different uses of dc in metapict - which depends heavily on picts within picts.


soegaard2
2019-1-2 00:28:28

Thanks for the help.