[REBOL] Re: Justifying text
From: carl:cybercraft at: 15-Dec-2002 13:02
On 15-Dec-02, Gabriele Santilli wrote:
> Hi Carl,
> On Saturday, December 14, 2002, 1:40:49 PM, you wrote:
>>>> layout [x: text " abc "]
> layout [x: text as-is " abc "]
Thanks Gabriele! (And Romano!:)
And that simplified things a lot, as did adding background colors to
the text faces so I could see exactly what was happening.
So, here's the debugged version. (; (Well, at least on Amiga.)
Enjoy...
rebol []
justify: func [
{This accepts a string and an integer and returns a
block containing 5 values, the values being the left
part of the string and the font/space/x value to use
for justifying it, the right part of the string and
its font/space/x value, plus an integer representing
the padding needed between them.
}
text [string!] "Text to be justified."
width [integer!] "Width to justify in pixels."
/local text-width pad rem
][
face/text: text
if none? text-width: size-text face [text-width: 0x0]
if any [width <= text-width/x text = ""][
return reduce [text 0 "" 0 0]
]
pad: to-integer width - text-width/x / (-1 + length? text)
rem: remainder width - text-width/x (-1 + length? text)
reduce [
copy/part text rem
either rem = 0 [0][pad + 1]
skip text rem
pad
either rem = 0 [0][pad]
]
]
lines: [
"This is an"
"attempt at"
"justifying"
"text using"
"REBOL/View."
""
"It is pretty"
"limited but"
"might be of"
"some use to"
"someone now"
"that it's"
"debugged! (:"
]
build-layout: does [
lo: copy [
backdrop tan
style txt text para [origin: 0x0 margin: 0x0]
across
space 0x0
]
face/text: " "
space-size: size-text face
foreach line lines [
blk: justify line width
append lo reduce [
'txt 'as-is copy blk/1 'black 'gold 'font reduce [
to-set-word 'space to-pair reduce [blk/2 0]
]
]
if not zero? blk/5 [append lo reduce ['pad blk/5]]
append lo reduce [
'txt 'as-is copy blk/3 'black 'wheat 'font reduce [
to-set-word 'space to-pair reduce [blk/4 0]
]
'return
]
]
append lo [button 60 "Close" [unview]]
]
; Get longest line
;------------------
width: 0
foreach line lines [
if line-length: size-text make face [text: line][
width: max width line-length/x
]
]
; Tests
;-------
build-layout
view layout lo
width: width + 20
build-layout
view layout lo
width: width + 40
build-layout
view layout lo
--
Carl Read