Embed pictures with pict? #140

Closed
opened 7 years ago by jonsterling · 3 comments
jonsterling commented 7 years ago (Migrated from github.com)

I have seen that it is possible in Scribble to embed pictures generated with pict just by writing code like this in a document:

(require pict)
@(colorize (filled-ellipse 40 40) "beige").

I tried to do the same in a pollen page as follows:

(colorize (filled-ellipse 40 40) "beige").

But I received a contract violation:

decode-elements: contract violation
  expected: txexpr-elements?
  ...

Is there a proper way to do what I wanted?

I have seen that it is possible in Scribble to embed pictures generated with `pict` just by writing code like this in a document: ```racket (require pict) @(colorize (filled-ellipse 40 40) "beige"). ``` I tried to do the same in a pollen page as follows: ```racket ◊(colorize (filled-ellipse 40 40) "beige"). ``` But I received a contract violation: ``` decode-elements: contract violation expected: txexpr-elements? ... ``` Is there a proper way to do what I wanted?
mbutterick commented 7 years ago (Migrated from github.com)

Scribble is converting the pict object to a png, saving it, and generating a link to the new file. So you could make a macro that does the same housekeeping. (Edit: I take it back, you can do it with a function not a macro):

test.html.pm

#lang pollen
(require pict)
(pict-as-png (colorize (filled-ellipse 40 40) "red"))

pollen.rkt

#lang racket/base
(require racket/class pict)
(provide pict-as-png)

(define (pict-as-png pict)
  (let ([png-name (format "~a.png" (gensym))])
    (send (pict->bitmap pict) save-file png-name 'png)
    `(img ((src ,png-name)))))
Scribble is converting the `pict` object to a `png`, saving it, and generating a link to the new file. So you could make a macro that does the same housekeeping. (Edit: I take it back, you can do it with a function not a macro): `test.html.pm` ```racket #lang pollen ◊(require pict) ◊(pict-as-png (colorize (filled-ellipse 40 40) "red")) ``` `pollen.rkt` ```racket #lang racket/base (require racket/class pict) (provide pict-as-png) (define (pict-as-png pict) (let ([png-name (format "~a.png" (gensym))]) (send (pict->bitmap pict) save-file png-name 'png) `(img ((src ,png-name))))) ```
jonsterling commented 7 years ago (Migrated from github.com)

@mbutterick Thank you very much for the help! Another thing I have tried out (which worked) is encoding the image as base-64 and inlining it into the html. Your solution is probably better for caching purposes.

@mbutterick Thank you very much for the help! Another thing I have tried out (which worked) is encoding the image as base-64 and inlining it into the html. Your solution is probably better for caching purposes.
mbutterick commented 7 years ago (Migrated from github.com)

Sure. The main idea is that whatever comes back from your Pollen function has to be something that counts as a txexpr-element?

Though it seems to be immovable folk wisdom at this point, I would not necessarily assume that the base64 approach is slower.

Sure. The main idea is that whatever comes back from your Pollen function has to be something that counts as a `txexpr-element?` Though it seems to be immovable folk wisdom at this point, I would [not necessarily assume](http://practicaltypography.com/the-scorpion-express.html#the-file-size-myth) that the base64 approach is slower.
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen#140
Loading…
There is no content yet.