# -*- mode: Perl -*-
# /=====================================================================\ #
# |  fontenc                                                            | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Thanks to the arXMLiv group for initial implementation              | #
# |    http://arxmliv.kwarc.info/                                       | #
# | Released to the Public Domain                                       | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Font Encoding
# We ALSO need to read in or set the char=>unicode mapping.

# Some encodings require us to define Cyrillic macros
foreach my $option (qw(T2A T2B T2C X2 LCY OT2)) {
  DeclareOption($option, sub {
      setupCyrillic();
      loadFontEncoding();
      return; }); }

# Anything else doesn't really matter....
DeclareOption(undef, sub {
    loadFontEncoding();
    return; });

# WELL... Actually, some "encodings" map the normal 7bit (or 8)
# apparently ASCII input characters to a completely different font.
# EG. OT2 maps to cyrillic.

our %cyrillic_map =
  (cyra => "\x{0430}", cyrb => "\x{0431}", cyrv => "\x{0432}", cyrg => "\x{0433}",
  cyrd => "\x{0434}", cyre     => "\x{0435}", cyrzh => "\x{0436}", cyrz => "\x{0437}",
  cyri => "\x{0438}", cyrishrt => "\x{0439}", cyrk  => "\x{043A}", cyrl => "\x{043B}",
  cyrm => "\x{043C}", cyrn     => "\x{043D}", cyro  => "\x{043E}", cyrp => "\x{043F}",

  cyrr     => "\x{0440}", cyrs    => "\x{0441}", cyrt     => "\x{0442}", cyru   => "\x{0443}",
  cyrf     => "\x{0444}", cyrh    => "\x{0445}", cyrc     => "\x{0446}", cyrch  => "\x{0447}",
  cyrsh    => "\x{0448}", cyrshch => "\x{0449}", cyrhrdsn => "\x{044A}", cyrery => "\x{044B}",
  cyrsftsn => "\x{044C}", cyrerev => "\x{044D}", cyryu    => "\x{044E}", cyrya  => "\x{044F}",
  cyryo    => "\x{0451}",

  CYRA => "\x{0410}", CYRB     => "\x{0411}", CYRV  => "\x{0412}", CYRG => "\x{0413}",
  CYRD => "\x{0414}", CYRE     => "\x{0415}", CYRZH => "\x{0416}", CYRZ => "\x{0417}",
  CYRI => "\x{0418}", CYRISHRT => "\x{0419}", CYRK  => "\x{041A}", CYRL => "\x{041B}",
  CYRM => "\x{041C}", CYRN     => "\x{041D}", CYRO  => "\x{041E}", CYRP => "\x{041F}",

  CYRR     => "\x{0420}", CYRS    => "\x{0421}", CYRT     => "\x{0422}", CYRU   => "\x{0423}",
  CYRF     => "\x{0424}", CYRH    => "\x{0425}", CYRC     => "\x{0426}", CYRCH  => "\x{0427}",
  CYRSH    => "\x{0428}", CYRSHCH => "\x{0429}", CYRHRDSN => "\x{042A}", CYRERY => "\x{042B}",
  CYRSFTSN => "\x{042C}", CYREREV => "\x{042D}", CYRYU    => "\x{042E}", CYRYA  => "\x{042F}",
  CYRYO    => "\x{0401}");

sub setupCyrillic {
  foreach my $cmd (keys %cyrillic_map) {
    DefMacroI('\\' . $cmd, undef, $cyrillic_map{$cmd}); }
  AddToMacro(T_CS('\@uclclist'),
    Tokenize(<<'EoTeX'));
      \cyra\CYRA\cyrabhch\CYRABHCH\cyrabhchdsc\CYRABHCHDSC\cyrabhdze
  \CYRABHDZE\cyrabhha\CYRABHHA\cyrae\CYRAE\cyrb\CYRB\cyrbyus
  \CYRBYUS\cyrc\CYRC\cyrch\CYRCH\cyrchldsc\CYRCHLDSC\cyrchrdsc
  \CYRCHRDSC\cyrchvcrs\CYRCHVCRS\cyrd\CYRD\cyrdelta\CYRDELTA
  \cyrdje\CYRDJE\cyrdze\CYRDZE\cyrdzhe\CYRDZHE\cyre\CYRE\cyreps
  \CYREPS\cyrerev\CYREREV\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rz\CYRZ\cyrzdsc\CYRZDSC\cyrzh\CYRZH
  \cyrzhdsc\CYRZHDSC
EoTeX
  return; }

ProcessOptions();
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sub loadFontEncoding {
  my $encoding = ToString(Digest(T_CS('\CurrentOption')));
  DefMacroI('\encodingdefault', undef, Tokens(Explode($encoding)), scope => 'global');
  my $encfile = lc($encoding) . "enc";
  InputDefinitions($encfile, type => 'def');
  if (LoadFontMap($encoding)) {
    MergeFont(encoding => $encoding); }
  return; }
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1;
