# baffle_cuneiform_69.pl August 8, 2009 Alan Folmsbee, Kona, Hawaii
# .RTF Spec requires decimal Unicode in Wordpad Rich
# Text Format, after backslash u
# Unicode spec has Russian Cyrillic 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/cuneiform_69.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.
# English (Latin) has 26, Cyrillic has 32 or more. 
# Hex 0x0430 = "A" 4*256+3*16=1052 for small letters
# 1072 - 32 = decimal 1040 for CAPITALs
my @greek_small_letters = (73728, 73792, 74322, 73811, 73790, 73813, 
73885, 73888, 73849, 73882, 73808, 73799, 73800, 73809, 73990, 73997, 73842, 74151, 74269, 73889, 74507, 73979, 74364, 73773, 73793, 73969);

# 65536 = 2^^16
# 73889 - 65536 = 8353
# 74507 - 65536 = 8971
# 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].", ";
}
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];
$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, to become Cyrillic";
print GREEK "\}";
close (ENGLISH);
close (GREEK);
exit(); 
