#!/usr/bin/perl BEGIN {(*STDERR = *STDOUT) || die;} use diagnostics; use warnings; use strict; $| = 1; use Text::Autoformat qw(autoformat break_TeX); #-------------------------------------------- # Configuration # Since vertical spacing is fixed (meaning we do not have # the option, at least in this release, of using double # spaced or 1.5 spaced lines) all computation is done in # units of vertical spacing. So we need the width to # height ratio. my $width_to_height = 5/9; # End configuration #-------------------------------------------- my $iterations_limit = 10; my $pi = 3.1412; my %taf_options = ( left => 1, #right => $width, justify => 'full', break => break_TeX, #case => 'sentence ', #widow => 1, ); my @messages = (); push @messages, < $slab_widths[0]) { $adjust += $adjust_increment; @lines = (); $message = $original_message; @lines = extract_top_down($original_message, $adjust, 1, \@slab_widths); $message = $lines[-1]; chomp $message; if($adjust >= $iterations_limit) { $adjust = 1; $adjust_increment = -1; } if($adjust <= -$iterations_limit) { last; } $pedastal and last; } align_tuit(\@lines); print "\nThe tuit as is after $adjust adjusts:\n"; print @lines, "\n\n"; } sub message_as_line { my $message = shift; $message =~ s/^\s+//gm; $message =~ s/\s+$//gm; $message =~ s/-\n//g; $message =~ s/\n/ /g; $message =~ s/\s\s+/ /g; return $message; } sub quantify_circle { my $message = shift; my $count = () = $message =~ m/./g; my $area = $count * $width_to_height; my $radius = sqrt ($area / $pi); my @slab_widths = (); for (- int $radius .. -1) { push @slab_widths, int (2 * (sqrt (($radius * $radius) - ($_ * $_)))/$width_to_height + 0.5); } push @slab_widths, (int ($radius * 2 / $width_to_height + 0.5)), reverse @slab_widths; if(0) { my $check = int ($radius * 2 / $width_to_height + 0.5); for (@slab_widths) { $check += 2 * $_; } my $layers = int $radius; print "count:$count, area:$area, radius:$radius, layers=$layers, check=$check,\n"; print "slab widths:\n@slab_widths,\n"; } return $radius, @slab_widths; } sub extract_top_down { my ($message, $adjust, $widow, $slabs) = @_; # following overrides the global one my %taf_options = ( left => 1, #right => $width, justify => 'full', break => break_TeX, #case => 'sentence ', widow => 1, ); my @lines = (); for my $width (@{$slabs}) { my $foo = $width + $adjust; $foo > 0 or $foo = 2; $taf_options{right} = $foo; ($widow) and $taf_options{widow} = $foo - 1; my $formatted = autoformat $message, {%taf_options}; if(!($formatted =~ s/^([^\n]+\n)(.+\n)$/$2/s)) { $message = $formatted; last; } push @lines, $1; $message = message_as_line($formatted); } push @lines, "$message\n"; return @lines; } sub align_tuit { my $lines = shift; for (@{$lines}) { $_ = " "x(40 - 0.5 * length($_)) . $_; } } __END__