### ----------------------------------------------------
### -- Text::Utils
### -- Licenses: Artistic-2.0
### -- Authors: Tom Browder <tbrowder@acm.org>
### -- File: lib/Text/Utils.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 2
 $tl points wide, too wide for max line length of $mll points";
            }
            $begin-first-line = False;
        }
        if $begin-following-line {
            # check mll with first word
            if $tl > $mll {
                die "FATAL: First line, first word '$tmp-line' is $tl points wide, too wide for max line length of $mll points";
            }
            $begin-following-line = False;
        }

        if line-length-ok(:line($tmp-line))  {
            # enough space to add this
            $line ~= $next;
            note "DEBUG: good line: '$line'" if $debug;
            @words.shift; # remove the used word
            $first-word = False;
            note "DEBUG: good line with {@words.elems} words" if $debug;
            next;
        }

        # else we're done with this line
        @para.push: $line if $line;
        $line = EMPTY;
        last if not @words.elems;

        $first-word = True;
        $begin-following-line = True;
        $line ~= $lindent-spaces if $lindent-spaces;
        $line ~= $para-pre-text if $para-pre-text;
        $line ~= $line-pre-text if $line-pre-text;
        if not $checked-following {
            # do an initial length check on $line length
            line-length-ok :$line, :initial-following;
            $checked-following = True;
        }
    }

    # may have a line left
    @para.push: $line if $line;
    $line = EMPTY;

    # should not have any  words left
    if @words.elems {
        die "FATAL: Unexpected non-empty \@words: '{join(SPACE, @words)}'";
    }

    my sub line-length-ok(:$line, :$initial-first, :$initial-following) {
        my $mll = $width;
        my $nl  = $afm.stringwidth: $line, $font-size, :$kern;
        if $initial-first and $nl > $mll {
            die "FATAL: first line pre too long: $nl points is too long for max length $mll";
        }
        elsif $initial-following and $nl > $mll {
            die "FATAL: following lines pre too long: $nl points is too long for max length $mll";
        }
        return $nl <= $mll;
    }

    # TODO turn the para into a para object with line objects as children
    @para;
} # sub typeset-text


