Re: [racket-users] Wrap long text in 2htdp/image

2017-07-22 Thread Matthias Felleisen

> On Jul 22, 2017, at 1:34 PM, kay  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.


Re: [racket-users] Wrap long text in 2htdp/image

2017-07-22 Thread Neil Van Dyke

Notes on text wrapping/layout for anyone interested...

There are a lot of approaches just for wrapping lines of text (even 
before you get into interacting with other aspects of page layout).


The simplest is to assume a fixed-pitch font and a known max character 
width per line, and just scan through the string, breaking the line when 
you hit the max characters per line.


I'd say the next simplest is to assume fixed-pitch like above, but scan 
through the string looking for characters that delimit "chunks" 
(whitespace like space characters, certain punctuation), and, for each 
chunk breaking the line when adding the chunk to it would exceed the max 
character.  Unless you're already on a new line, and the chunk is simply 
too big.


Sometimes fixed-pitch formatting would fill lines with extra spaces, to 
make the right hand side non-ragged.  But that always looked poor in 
fixed-pitch.


And we used to sometimes, in both fixed and variable pitch, do 
hyphenation (inserting hyphens in the middle of words, to break them 
across lines), but Web browsers put an end to that, except for some 
print still (like some 2-column article formats).


As soon as you get past fixed-pitch fonts, you need to be able to at 
least look up font metrics for individual characters, if not strings, to 
see how wide in some units (points, pixels, mm, etc.) that will be.


Then, once you're doing fixed-pitch, horizontal whitespace (between 
words, between sentences, between an abbreviated honorific and a name, 
etc.) is not necessarily fixed either, and you can think about it.  You 
can also subtly increase or decrease these small horizontal whitespaces 
to make a right edge of a paragraph less-ragged or to squeeze a chunk 
onto a line on which it almost fits with default spacing.


Then you get into kerning -- being able to squeeze characters together, 
based on their shapes.  And sometimes combining/overlaying characters.  
Like whitespace with fixed-pitch, this is another place that you can do 
subtle adjustments for a non-ragged right edge, or to squeeze a chunk 
onto the line.


You can pick what degree of sophistication you want to start with.

Then you get into integrating this simple horizontal line breaking with 
the vertical[1], and with more complex page layout approaches.


And laying out things like mathematical expressions.

A long time ago, Computer Science's most revered procrastinator, Donald 
Knuth, did a lot of study and innovation of typesetting and fonts.  
Reading up on "the TeX algorithm" [2], and then maybe "Metafont", is not 
a bad starting point, to get a sense of way of thinking about text 
layout and having larger layouts converge (including annoying things 
like page references in the text being laid out changing the page 
numbering).


Even things that start to look outdated, like pagination with 
orphan/widow control, can still be relevant (e.g., imagine a 
non-paginated vertical scrolling view, and you would like the scrolling 
to be subtly more readable than the scrolling-viewport-indifferent 
layout and the fixed-pixel scroll increments).  Or general ideas apply 
to different aspects of page layout.


[1] "Outer Limits" intro. https://www.youtube.com/watch?v=8CtjhWhw2I8=15s

[2] https://en.wikipedia.org/wiki/TeX

--
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.


Re: [racket-users] Wrap long text in 2htdp/image

2017-07-22 Thread WarGrey Gyoudmon Ju
Yes, but you must implement it yourself.

This simplest one works for fixed-width font (implemented with pict-lib,
you get the idea):

(define desc
  (lambda [txt size width #:head [head (blank 0 0)] #:style [fstyle null]]
(define {desc0 txt size width fstyle}
  (call-with-current-continuation
   {lambda [return]
 (when (or (< width size) (zero? (string-length txt))) (return
(list (blank 0 0) txt)))
 (define hit (min (string-length txt) (exact-floor (/ width size
 (define hpict (text (substring txt 0 hit) fstyle size))
 (define final (desc0 (substring txt hit) size (- width (pict-width
hpict)) fstyle))
 (list (hc-append hpict (first final)) (second final))}))
(define smart (desc0 txt size width fstyle))
(vl-append head (cond [(zero? (string-length (second smart))) (first
smart)]
  [else (desc (second smart) size width #:head
(first smart) #:style fstyle)]

Once I implemented a more complex one[1] that works for all fonts with the
exponential search algorithm, but it's not efficient.
Now this constructor is re-written with the Pango Layout API[2].

Maybe racket/draw should support text layout interface directly.

[1]bitmap-desc

[2]bitmap-paragraph



On Sun, Jul 23, 2017 at 1:34 AM, kay  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?
>
> --
> 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.
>

-- 
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.


Re: [racket-users] Wrap long text in 2htdp/image

2017-07-22 Thread Robby Findler
This is a function that you'd have to implement yourself. You could break
the string up on spaces and then put them beside or above each other, as
appropriate. (A real word processing program uses a more complex algorithm
that involves adjusting the spaces between words (and possibly elsewhere, I
am not sure).)

The function "para" does something like this for picts.

Robby

On Sat, Jul 22, 2017 at 12:34 PM kay  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?
>
> --
> 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.
>

-- 
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.


[racket-users] Wrap long text in 2htdp/image

2017-07-22 Thread kay
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?

-- 
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.