Skip to content

Instantly share code, notes, and snippets.

@takikawa
Created January 27, 2020 23:28
Show Gist options
  • Save takikawa/bd1cb0563bdf7a76cb88105b2e354b40 to your computer and use it in GitHub Desktop.
Save takikawa/bd1cb0563bdf7a76cb88105b2e354b40 to your computer and use it in GitHub Desktop.
Size text picts based on slide size
#lang racket
(require racket/draw
pict
slideshow)
;; Create a text pict that fits in target pixels of the slide
(define (auto-text str [style null] [target (* 0.8 client-w)] [angle 0])
(define initial-min 0)
(define initial-max 1000)
(define (close-enough? width target)
(< (abs (- width target)) 10))
(define (bin-search cur-min cur-max)
(define next-size
(exact-round (+ cur-min (/ (- cur-max cur-min) 2))))
(define width (pict-width (text str style next-size angle)))
(cond [(close-enough? width target)
(text str style next-size angle)]
[(> width target)
(bin-search cur-min next-size)]
[(< width target)
(bin-search next-size cur-max)]))
(bin-search initial-min initial-max))
(cc-superimpose (rectangle client-w client-h
#:border-color "black"
#:border-width 1)
(auto-text "Hello world!"
(cons (make-color 50 50 50) "Helvetica")
))
(cc-superimpose (rectangle client-w client-h
#:border-color "black"
#:border-width 1)
(auto-text "Hello world!"
(cons (make-color 50 50 50) "Helvetica")
(* 0.5 client-w)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment