File: image.rkt

package info (click to toggle)
racket-mode 20201227git0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,040 kB
  • sloc: lisp: 9,808; makefile: 55
file content (46 lines) | stat: -rw-r--r-- 1,715 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#lang racket/base

;;; Portions Copyright (C) 2012 Jose Antonio Ortega Ruiz.

(require file/convertible
         racket/file
         racket/format
         racket/match)

(provide emacs-can-use-svg!
         convert-image)

;; Emacs front end tells us whether SVG is an image file type Emacs
;; can render.
(define use-svg? #t)
(define (emacs-can-use-svg! command-line-flag-str)
  (set! use-svg? (equal? command-line-flag-str "--use-svg")))

(define (convert-image v)
  (and (convertible? v)
       ;; Rationale for the order here:
       ;;
       ;; - Try bounded before unbounded flavors. Because we want
       ;;   accurate image width, if available, for pretty-printing.
       ;;
       ;; - Within each flavor: Try svg (if this Emacs can use it)
       ;;   before png. Because space.
       (let ([fmts/exts (if use-svg?
                            '((svg-bytes+bounds "svg")
                              (png-bytes+bounds "png")
                              (svg-bytes        "svg")
                              (png-bytes        "png"))
                            '((png-bytes+bounds "png")
                              (png-bytes        "png")))])
         (for/or ([fmt/ext (in-list fmts/exts)])
           (apply convert-and-save v fmt/ext)))))

(define (convert-and-save v fmt ext)
  (define (default-width _) 4096)
  (match (convert v fmt)
    [(or (list* (? bytes? bstr) width _)                  ;bytes+bounds
         (and (? bytes? bstr) (app default-width width))) ;bytes
     (define filename (make-temporary-file (~a "racket-image-~a." ext)))
     (with-output-to-file filename #:exists 'truncate (λ () (display bstr)))
     (cons (format "#<Image: ~a>" filename) width)]
    [_ #f]))