# baffle_cyrillic_66.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/cyrillic_66.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 = (1072, 1073, 1096, 1076, 1077, 1092, 1075, 
1095, 1080, 1064, 1082, 1083, 1084, 1085, 1086, 1087,
1102, 1088, 1089, 1090, 1091, 1097, 1099, 1093, 1079, 1078);
# 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, to become Cyrillic";
print GREEK "\}";
close (ENGLISH);
close (GREEK);
exit(); 
