Text Justification Test Case and Code Explanation

Rather than create a separate text case and waste my time typing filler text, I've decided to use an explanation of the code as filler text. Counting Spaces and declaring variables is, I think, self-explanatory, so I'm going to focus on the Line Layout code. The English might be more confusing than the code, but at least the filler text is more realistic. :)

Full text justification takes the space between the end of the line and the left margin and incorporates it into the text. This can be done by widening the width of all spaces in the line or by widening all the character glyphs on the line. Generally the second method is a fall-back for the first in case there are no spaces to widen. (I haven't tried to implement this yet; I want first to know what happens with what I've already done.) The variables 'tchar', 'mNChar' (in PerFrameData) and 'mJCSpacing' are therefore not currently needed.

'dwidth' represents the difference between the previous line width and the new one after justification. 'extra' is the extra space per space (or character), and 'rem' takes the remainder of the 'remainingWidth'.

ApplyWSJustification applies the spacing changes to all frames within the specified span and returns the difference between the span's old and new width. For each frame, the difference in previous frames' widths (represented by 'deltaX') is added to the frame's x-coordinate.
If the frame is a text frame and if the text frame has less spaces than 'rem',* then extra plus another twit is added to each space and the value of 'rem' compensated for the twits thus used. Otherwise, 'mJWSpacing' is set just to 'extra'. The number of spaces multiplied by the 'mJWSpacing' gives the difference in that frame's width, which is added to both 'deltaX' and the frame's 'mBounds.width'.
If the frame is not a text frame but is a span frame, its child frames are justified and its width adjusted. In all cases, the frame's new coordinates are set and the next frame in the span goes through the loop. At the end, the new value of 'rem' is sent upward, and 'deltaX' returned.

Back in HorizontalAlignFrames, 'dwidth' is set to the difference in the line's width, returned by ApplyWSJustification. This is subtracted from the 'remainingWidth' since all of 'rem' may not have been used up. (I decided that it wasn't worth the trouble to use up all of 'rem' for the spaces; in most cases, there won't be enough spaces to make a noticable difference anyway.)

I haven't looked at tabbed text yet. Otherwise, I think preformatted will work just as well as regular paragraph text.

index

* The 'if' statements are separated so that any text frames without spaces won't go through all the 'else' checks.