! ! reflow_text.sqr ! ! This program re-flows text into 4 styles - flush left, flush right, ! centered, and justified. The resulting text's width is determined by ! the first parameter, default 60, maximum 150. If the text to be re-flowed ! is not specified as the second parameter, an example line is used. ! Paragraphs are determined by blank line separations and are maintained. ! ! __ _ _ _ ! / _|| | | | | | ! _ __ ___ | |_ | | ___ __ __ | |_ ___ __ __| |_ ___ __ _ _ __ ! | '__| / _ \| _|| | / _ \\ \ /\ / / | __| / _ \\ \/ /| __| / __| / _` || '__| ! | | | __/| | | || (_) |\ V V / | |_ | __/ > < | |_ _ \__ \| (_| || | ! |_| \___||_| |_| \___/ \_/\_/ \__| \___|/_/\_\ \__|(_)|___/ \__, ||_| ! _____ | | ! |_____| |_| ! !************************************************************************ ! ! Execution: sqrt reflow_text ! the response is: ! Enter a numeric width for the text: ! Enter the name of a dataset for reflowing: ! ! Alt Execution: sqrt reflow_text 45 abcde.lis ! !************************************************************************ ! Maintenance Log ! ! Date Programmer Comments ! ======= ========== ======== ! May1999 Peter Reeve-Newson Original coding. ! !************************************************************************ ! PVCS Maintenance Log ! $Log$ ! !************************************************************************ Begin-Setup declare-layout report-layout top-margin=0.0 left-margin=0.0 max-columns=160 max-lines=100 end-declare declare-report Justified layout=report-layout end-declare declare-report AlignLeft layout=report-layout end-declare declare-report Centered layout=report-layout end-declare declare-report AlignRight layout=report-layout end-declare End-Setup Begin-Report input #para_width 'Enter a numeric width for the text' if #para_width < 15 move 60 to #para_width end-if input $datasetname 'Enter the name of a dataset for reflowing' if $datasetname = '' move 'N' to $more_text show 'No dataset name entered. Using some example text' let $str = 'This is a paragraph of blurbtext that will be printed as a block. In other words, it will be printed in newspaperterminology as "flush right". The basic technique is divide the white space at the end of each portion into the gaps.' else move 'Y' to $more_text do obtain_dataset end-if do main End-Report Begin-Procedure main ! loop and process whole dataset/string ! do main_more while 'Y' = $more_text do paragraph_end do obtain_dataset_text do main_more end-while End-Procedure Begin-Procedure main_more ! loop and process portions (a paragraph) of the string ! while 1 do get_next_str if $str = '' ! null string indicates that the last portion was processed ! only print this part, don't add blanks to it ! use-report AlignLeft print $substr (+1,10) use-report AlignRight let $Rsubstr = lpad ($substr, (#para_width + 1), ' ') print $Rsubstr (+1,10) use-report Centered let #centre = round ( ((#para_width + length($substr)) / 2), 0) let $Csubstr = lpad ($substr, #centre, ' ') print $Csubstr (+1,10) use-report Justified print $substr (+1,10) break else ! add blanks to the current sub-string ! use-report AlignLeft print $substr (+1,10) use-report AlignRight let $Rsubstr = lpad ($substr, #para_width, ' ') print $Rsubstr (+1,10) use-report Centered let #centre = round ( ((#para_width + length($substr)) / 2), 0) let $Csubstr = lpad ($substr, #centre, ' ') print $Csubstr (+1,10) do add_blanks_to_gaps use-report Justified print $substr (+1,10) end-if end-while End-Procedure Begin-Procedure obtain_dataset ! Open the dataset and obtain the first paragraph ! open $datasetname as 1 for-reading record=255 status=#open-status if #open-status != 0 ! goto open-exit stop end-if do obtain_dataset_text End-Procedure Begin-Procedure obtain_dataset_text ! Read a paragraph of lines of the dataset and string them together with a single space. ! move '' to $str read 1 into $aline:255 while not #end-file ! if end of paragraph, done getting a paragraph string if length ($aline) = 0 move 'Y' to $more_text break end-if let $str = $str || $aline ! if any mutliple spaces, remove them let #j = instr($str, ' ', 1) while #j > 0 let #len = length($str) let $str = substr($str, 1, #j ) || substr($str, (#j + 2), (#len - #j - 1)) let #j = instr($str, ' ', 1) ! any more mutliple spaces? end-while let #j = instr($str, ' ', 1) if #j = 1 let $str = substr($str, 2, (length($str) - 1)) end-if ! if doesn't end with a space, add one before appending more input let #len = length($str) if substr($str, (#len - 1), 1) != ' ' let $str = $str || ' ' end-if read 1 into $aline:255 end-while if #end-file move 'N' to $more_text end-if End-Procedure Begin-Procedure paragraph_end ! throw in a blank line between paragraphs ! use-report AlignLeft print ' ' (+1,10) use-report AlignRight print ' ' (+1,10) use-report Centered print ' ' (+1,10) use-report Justified print ' '(+1,10) End-Procedure Begin-Procedure get_next_str ! Come in with $str. Returns a portion of a string ($substr) ! and sets the original string to the remainder ($str) ! let #len = length($str) if #len <= #para_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 by going backwards ! from the para_width to the first space ! let #i = #para_width + 1 let $substr = substr($str, 1, #i) while #i > 0 let #j = instr($substr,' ', #i) subtract 1 from #i if #i = 1 ! there are no spaces in the chunk so just take the max width ! let $substr = substr($str, 1, #para_width) let $str = substr($str, (#para_width + 1), (#len - #para_width)) break end-if if #j > 0 ! form the $substr and return the remainder of the string in $str ! let $substr = substr($str, 1, #i) let $str = substr($str, (#i + 2), (#len - #i - 1)) break end-if end-while end-if End-Procedure Begin-Procedure add_blanks_to_gaps ! 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 and how many spaces to add to it ! move ' ' to $gap let #len = length($substr) let #s = #para_width - #len move 1 to #i ! must add some spaces in this segment while #s > 0 ! if there are no gaps to play with so get out ! let #j = instr($substr, $gap, 1) if #j = 0 break end-if ! 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 #len = length($substr) let $substr = substr($substr, 1, #j ) || substr($substr, #j, (#len - #j + 1)) let #i = #j + 2 subtract 1 from #s end-if end-while End-Procedure ! Here is the sample output for 60 width, justified: ! ! This is a paragraph of blurbtext that will be printed as a ! block. In other words, it will be printed in ! newspaperterminology as "flush right". The basic technique ! is divide the white space at the end of each portion into ! the gaps. ! __ ____ __ __ __ ! ___ ____ ____/ / _____ ___ / __// /____ _ __ / /_ ___ _ __ / /_ ! / _ \ / __ \ / __ / / ___// _ \ / /_ / // __ \| | /| / / / __// _ \ | |/_// __/ ! / __// / / // /_/ / / / / __// __// // /_/ /| |/ |/ / / /_ / __/_> < / /_ ! \___//_/ /_/ \__,_/ /_/ \___//_/ /_/ \____/ |__/|__/______\__/ \___//_/|_| \__/ ! /_____/ !