# baffle_63.pl August 7, 2009 Alan Folmsbee, Kona, Hawaii
# .RTF Spec requires decimal Unicode in Wordpad Rich 
# Text Format, after backslash u
# Unicode spec has Greek tables in hex so convert to decimal for .rtf
use strict;
use warnings;

my $paragraph = "C:/import/writings/greek3/english_50.txt";
my  $code_out = "C:/import/writings/greek3/greek_63.rtf";
open(ENGLISH, "$paragraph") or die "Can't open $paragraph: $!";
open(GREEK, ">$code_out") or die "Can't open $code_out: $!";

# 26 small letters assigned for Unicode Greek version of English sentence.
# The letters c and j are left as in English so the 24 Greek letters have 2 added.
my @greek_small_letters = (945, 946, 99, 948, 949, 966, 947, 951, 953, 106, 
954, 955, 956, 957, 959, 960, 952, 961, 963, 964, 965, 968, 969, 967, 958, 950);
# The RTF header is sent to the file.
print GREEK "\{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\{\\fonttbl\{\\f0\\froman\\fcharset0 Times New Roman\;\}\{\\f1\\fswiss\\fcharset0 Arial\;\}\}
\{\\\*\\generator alan Msftedit 5\.41\.15\.1515\;\}\\viewkind4\\uc1\\pard\\sb100\\sa100\\f0\\fs24";
my $k;
my $offset;
for ($k=0; $k<26; $k++)
    {
     print GREEK "\\"."u".$greek_small_letters[$k].",";
    }
# Next, show capital letters numbered 32 less than small letters
for ($k=0; $k<26; $k++)
    {
     $offset = $greek_small_letters[$k] - 32;
     print GREEK "\\"."u".$offset.",";
    }
 
print GREEK "\\"."line";

my $line;
my $size;
my @english_sentence;
my $letter;
my $greek_letter;
my $flag_match = 0;

while ($line = <ENGLISH>)
   {
   print "\n$line";
   chomp $line;
   @english_sentence = split (//,$line); 
   $size = @english_sentence;
   for ($letter=0; $letter<$size; $letter++)
       {
       $flag_match = 0;
       for ($k=0; $k<26; $k++)
          {
          # small letters
          if ($flag_match !=1)
             {
             if (ord($english_sentence[$letter]) == ($k+97))
                {
                $greek_letter = $greek_small_letters[$k];
                $flag_match = 1;
                }
             else 
                {
                $greek_letter = ord($english_sentence[$letter]);
                } 
             }
          # capital letters
          if ($flag_match !=1)
             {
             if (ord($english_sentence[$letter]) == ($k+65))
                {
                $greek_letter = $greek_small_letters[$k] - 32;
                $flag_match = 1;
                }
             else 
                {
                $greek_letter = ord($english_sentence[$letter]);
                } 
             }

          }
# handle newline code, the invisible code used as /line or /par in .rtf
       if ($letter == ($size-1))
          {
          print GREEK "\\"."u".$greek_letter."\\"."line";
          }
       else
          {
          print GREEK "\\"."u".$greek_letter.",";;
          }
       }
   }
print "\nThe size of the last sentence was $size characters";
print GREEK "\}";
close (ENGLISH);
close (GREEK);
exit();
