use strict;
use warnings;
package TeX;
our ( %TeX_L2, %L2_TeX, %TeX_w, %w_TeX );

use charnames ':full';

BEGIN
{
    %TeX_w = ( 
               "AA" => "\N{LATIN CAPITAL LETTER A WITH RING ABOVE}",
               "\'a" => "\N{LATIN SMALL LETTER A WITH ACUTE}",
               "\'e" => "\N{LATIN SMALL LETTER E WITH ACUTE}",
               "\'\\i" => "\N{LATIN SMALL LETTER I WITH ACUTE}",
               "\'o" => "\N{LATIN SMALL LETTER O WITH ACUTE}",
               "\'u" => "\N{LATIN SMALL LETTER U WITH ACUTE}",
               "\'y" => "\N{LATIN SMALL LETTER Y WITH ACUTE}",
               "\'A" => "\N{LATIN CAPITAL LETTER A WITH ACUTE}",
               "\'E" => "\N{LATIN CAPITAL LETTER E WITH ACUTE}",
               "\'\\I" => "\N{LATIN CAPITAL LETTER I WITH ACUTE}",
               "\'O" => "\N{LATIN CAPITAL LETTER O WITH ACUTE}",
               "\'U" => "\N{LATIN CAPITAL LETTER U WITH ACUTE}",
               "\'Y" => "\N{LATIN CAPITAL LETTER Y WITH ACUTE}",
               "v c" => "\N{LATIN SMALL LETTER C WITH CARON}",
               "v d" => "\N{LATIN SMALL LETTER D WITH CARON}",
               "v e" => "\N{LATIN SMALL LETTER E WITH CARON}",
               "v n" => "\N{LATIN SMALL LETTER N WITH CARON}",
               "v r" => "\N{LATIN SMALL LETTER R WITH CARON}",
               "v s" => "\N{LATIN SMALL LETTER S WITH CARON}",
               "v t" => "\N{LATIN SMALL LETTER T WITH CARON}",
               "v z" => "\N{LATIN SMALL LETTER Z WITH CARON}",
               "v C" => "\N{LATIN CAPITAL LETTER C WITH CARON}",
               "v D" => "\N{LATIN CAPITAL LETTER D WITH CARON}",
               "v E" => "\N{LATIN CAPITAL LETTER E WITH CARON}",
               "v N" => "\N{LATIN CAPITAL LETTER N WITH CARON}",
               "v R" => "\N{LATIN CAPITAL LETTER R WITH CARON}",
               "v S" => "\N{LATIN CAPITAL LETTER S WITH CARON}",
               "v T" => "\N{LATIN CAPITAL LETTER T WITH CARON}",
               "v Z" => "\N{LATIN CAPITAL LETTER Z WITH CARON}",
               "accent23u" => "\N{LATIN SMALL LETTER U WITH RING ABOVE}",
               "accent23U" => "\N{LATIN CAPITAL LETTER U WITH RING ABOVE}",
               "\"o" => "\N{LATIN SMALL LETTER O WITH DIAERESIS}",
               "\`o" => "\N{LATIN SMALL LETTER O WITH GRAVE}",
               );

    %w_TeX = ();
    
    foreach my $a ( keys( %TeX_w ) )
    {
        $w_TeX{ $TeX_w{$a} } = "{\\$a}";
    } 

    %TeX_L2 = ( "\'" =>
                { 'a' => 'á', 'e' => 'é', 'i' => 'í', 'o' => 'ó',
                  'u' => 'ú', 'y' => 'ý',
                  'A' => 'Á', 'E' => 'É', 'I' => 'Í', 'O' => 'Ó', 
                  'U' => 'Ú', 'Y' => 'Ý' },
                "v " =>
                { 'c' => 'è', 'd' => 'ï', 'e' => 'ì', 'n' => 'ò',
                  'r' => 'ø', 's' => '¹', 't' => '»', 'z' => '¾',
                  'C' => 'È', 'D' => 'Ï', 'E' => 'Ì', 'N' => 'Ò',
                  'R' => 'Ø', 'S' => '©', 'T' => '«', 'Z' => '®' },
                );
    %L2_TeX = ();
    
    foreach my $a ( keys( %TeX_L2 ) )
    {
        foreach my $l ( keys( %{$TeX_L2{$a}} ) )
        {
            $L2_TeX{ $TeX_L2{$a}{$l} } = "{\\$a$l}";
        }
    }
}
sub wchar2TeX
{
    my $acc = '';

    foreach my $t ( split //, $_[0] ) {
        $acc .= ( $w_TeX{$t} || $t );
    }
    return $acc;
}

sub utf82TeX {
    local $_ = shift;

    $_ or return $_;
    utf8::decode( $_ );
    $_ = wchar2TeX( $_ );
    return $_;
}


sub TeX2wchar
{
    local $_ = $_[0];

    $_ or return $_;
    s/\{\\(AA)\}/$TeX_w{$1}/ge;
    s/\{\s*\\(v |\'|accent23|\"|\`)\s*([a-zA-Z]|\\[iI])\s*\}/
        ($TeX_w{$1.$2}||"{\\$1$2}")/ge;
    
    return $_;
}

sub TeX2utf8 {
    local $_ = shift;

    $_ or return $_;
    utf8::decode( $_ );
    $_ = TeX2wchar( $_ );
    utf8::encode( $_ );
    return $_;
}



sub tex2latin
{
    my $tx = $_[0];
    my $out = '';
    s/\\i\s+/i/g;
    while( $tx =~ /\{\s*\\(v |\'|accent23)\s*([a-zA-Z])\s*\}/ )
    {
        $tx = $'; #';
        $out .=  $` . ( $TeX_L2{ $1 }{ $2 } || $& );
    }

    return $out . $tx;
}
