Tuesday, May 27, 2008

Justifying text in SQR Reports

Blocktxt.sqr is a program that illustrates the use of a technique of printing a text string so that it wraps, with justified text. In other words, it will be flush left AND flush right, with spaces placed through the text. This style of print is often used in newspaper columns.

!
! This program shows how to block a paragraph (also known as flush right).
! It is not intended to handle all situations and is not supported by MITI.
!
begin-report
do main
end-report

begin-setup
#define paragraph_width 60 ! define the width of the paragraph
end-setup

begin-procedure main

let $str = 'This is a paragraph of text that will be printed as a block. In other words, it will be printed in newspaper terminology as "flush right". The basic technique is divide the white space at the end of each portion into the gaps.'
let $str = $str $sta
let $str = $str $sta
let $str = $str $sta
let $str = $str $sta
let $str = $str $sta
let $str = $str $sta

while 1
!
! loop and process portions of the string
!
do get_next_str($str,$substr)
if $str = ''
!
! null string indicates that the last portion was processed
! only print this part, don't add blanks to it
!
print $substr (+1,10)
break
else
!
! add blanks to the current sub-string
!
do add_blanks_to_gaps($substr)
print $substr (+1,10)
end-if
end-while
end-procedure

begin-procedure get_next_str(:$str,:$substr)
!
! this procedure returns a portion of a string
! and sets the original string to the remainder
!
let #l = length($str)
if #l <= {paragraph_width}
!
! last portion of the string, so return that and set original string to null
!
move $str to $substr
move '' to $str
else
!
! determine how much of the string to return as the substring
!
let #i = {paragraph_width} + 1
let $substr = substr($str,1,#i)
while #i > 0
let #j = instr($substr,' ',#i)
subtract 1 from #i
if #j > 0
break
end-if
end-while
!
! form the $substr and return the remainder of the string in $str
!
let $substr = substr($str,1,#i)
let $str = substr($str,#i+2,#l-#i-1)
end-if
end-procedure

begin-procedure add_blanks_to_gaps(:$substr)
!
! this procedure adds spaces to the "gap" in the substring
! so that the length of the substring is equal to the paragraph width
!
! first determine how long the substring is (#l)
! and how many spaces to add to the substring
!
move ' ' to $gap
let #l = length($substr)
let #s = {paragraph_width} - #l
move 1 to #i
while #s > 0
!
! loop and look for the next "gap"
!
let #j = instr($substr,$gap,#i)
if #j = 0
!
! no more "gaps" in the string, increase $gap and go back to the start
!
let $gap = $gap ' '
move 1 to #i
break
else
!
! found a gap, form a string that will have an extra space at the "gap"
!
let #l = length($substr)
let $substr = substr($substr,1,#j)
substr($substr,#j,length($substr)-#j+1)
let #i = #j + 2
subtract 1 from #s
end-if
end-while
end-procedure

! Here is the sample output

! This is a paragraph of text that will be printed as a block.
! In other words, it will be printed in newspaper terminology
! as "flush right". The basic technique is divide the white
! space at the end of each portion into the gaps.

source: http://www.sqrug.org/ftp/blocktxt/