> On Jul 22, 2017, at 1:34 PM, kay <klu...@gmail.com> wrote:
> 
> When using 2htdp/image to create a text image, is this possible to specify a 
> "bounding box" for the image, so that the text could be wrapped to fit that 
> box?
> 
> E.g., below line:
> 
>  (text "This wide, sharp telescopic view reveals galaxies scattered beyond 
> the stars of the Milky Way at the northern boundary of the high-flying 
> constellation Pegasus. Prominent at the upper right is NGC 7331. A mere 50 
> million light-years away, the large spiral is one of the brighter galaxies 
> not included in Charles Messier's famous 18th century catalog. The disturbed 
> looking group of galaxies at the lower left is well-known as Stephan's 
> Quintet. About 300 million light-years distant, the quintet dramatically 
> illustrates a multiple galaxy collision, its powerful, ongoing interactions 
> posed for a brief cosmic snapshot. On the sky, the quintet and NGC 7331 are 
> separated by about half a degree." 10 "black")
> 
> 
> creates a super long line text image without wrapping, when I want to overlay 
> it on another image, it's mostly useless. I want to specify a "size" (aka 
> bounding box) for it so it wraps properly.
> 
> Is that possible?


(require racket/string) ;; string-split 

(require 2htdp/image)

;; String Number StringColor Number Number -> Image
;; break up a string into a text-image paragraph of reasonable width
;; GENERATIVE RECURSION split up into words, turn them into text,
;; try to squeeze as many words into a line as possible before its image-width
;; becomes larger than width 

;; note how I am willing to add an extra space to the end of the text 
(check-expect (para "one word at a time" 10 "black" 200)
              (text "one word at a time " 10 "black"))

;; generative recursion: examples such as this one will cause an infinite loop
#;
(check-expect (para "one word at a time" 10 "black" 10)
              (text "one word at a time " 10 "black"))

(define (para txt size color width)
  (local ((define txt-as-words (string-split txt))
          (define txt-as-image (map (lambda (x) (text x size color)) 
txt-as-words))
          (define space        (text " " size color))
          ;; [Listof Image] Image -> Image
          ;; ACCUMULATOR line represnts the line that is currently conctructed 
from the words
          ;; between txt-as-image0 and txt-as-image that are not already put 
into a line 
          (define (concat txt-as-image line)
            (cond
              [(empty? txt-as-image) line]
              [else (local ((define candidate (beside line (first txt-as-image) 
space)))
                      (if (<= (image-width candidate) width)
                          (concat (rest txt-as-image) candidate)
                          (above/align 'left line (concat txt-as-image 
empty-image))))])))
    (concat txt-as-image empty-image)))

(define x 
  "This wide, sharp telescopic view reveals galaxies scattered beyond the stars 
of the Milky Way
at the northern boundary of the high-flying constellation Pegasus. Prominent at 
the upper right is
NGC 7331. A mere 50 million light-years away, the large spiral is one of the 
brighter galaxies not
included in Charles Messier's famous 18th century catalog. The disturbed 
looking group of galaxies
at the lower left is well-known as Stephan's Quintet. About 300 million 
light-years distant, the
quintet dramatically illustrates a multiple galaxy collision, its powerful, 
ongoing interactions
posed for a brief cosmic snapshot. On the sky, the quintet and NGC 7331 are 
separated by about half
a degree.")


(para x 10 "black" 200)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to