%% Copyright 2025 Jonathan Walls
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   https://www.latex-project.org/lppl/lppl-1-3c.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Jonathan Walls.
%
% This work consists of the files hittype.sty, hittype-doc.tex
%    and the derived file hittype-doc.pdf.
% 
%
% Version history:
% 2.2 - Fixed missing glyphs in UllikummiB.ttf
% 2.1 - Added some specific sign combinations
% 2.0 - Major revision, created new structure that allows using HitType and LuwianType packages simultaneously
% 1.1 - changed command definition structure to fix spacing issues and updated documentation
% 1.0 - Original Release Version

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{hittype}[2025/08/13 Hittite Cuneiform Typing Package]

%Required packages and declarations
\usepackage{fontspec}
\usepackage{expl3}

%Formatting commands
\newcommand{\sumer}[3]{\normalfont{\textsuperscript{#1}}\normalfont{#2}\textit{#3}}
\newcommand{\badeq}{\char"2E17}
\newcommand{\funh}{\char"1E2B}
\newcommand{\funH}{\char"1E2A}
\newcommand{\hl}{\char"2E22}
\newcommand{\hr}{\char"2E23}
\newcommand{\tsup}[1]{\textsuperscript{#1}}
\newcommand{\tsub}[1]{\textsubscript{#1}}
\newcommand{\bracks}[1]{\normalfont[\textit{#1}\normalfont]}


\ExplSyntaxOn

% Font selection commands, meant to be basically internal
\newcommand{\swapa}{\setmainfont[ExternalLocation=Fonts/]{UllikummiA.ttf}\Large}
\newcommand{\swapb}{\setmainfont[ExternalLocation=Fonts/]{UllikummiB.ttf}\Large}
\newcommand{\swapc}{\setmainfont[ExternalLocation=Fonts/]{UllikummiC.ttf}\Large}
\newcommand{\swaplat}{\setmainfont{Charis SIL}\normalsize}

% Define token-to-Unicode mapping
\prop_new:N \c_hitt_dict
\prop_set_from_keyval:Nn \c_hitt_dict {
    hal=1212C,
    pec1=1227E,
    pec2=1227E,
    bal=12044,
    cir=122D3,
    gir=1212B,
    gir2=12108,
    gir3=1210A,
    gir4=1230B\char"1201C,
    tar=122FB,
    an=1202D,
    apin=12033,
    mah=12224,
    nu=12261,
    kul=121B0,
    numun=121B0,
    arad=12034,
    mun=12075,
    nar=1221C,
    qa=122E1,
    mug=1222E,
    uz=122BB,
    uz6=1235A,
    hu=12137,
    u=1230B,
    u2=12311,
    u3=12147,
    u5=12137\char"122DB,
    u8=121C7,
    ce=122BA,
    ce12=1223A,
    sixsa=100000,
    sixsa2=100000,
    rad=122E5,
    gee=12101,
    ri=12291,
    tur=12309,
    tur3=12263\char"121EC,
    kun=121B2,
    nun=12263,
    ti=122FE,
    mac=12047,
    mac2=12227,
    nam=12246,
    en=12097,
    inanna=12239,
    ru=12292,
    ib=12141,
    zul=12084,
    kin=121A5,
    cucana=1245A,
    kab=1218F,
    hub=1213D,
    gidim=12107,
    tuk=12307,
    sila=120E2,
    sila4=120E2,
    bugin=121B9,
    bugin2=121C5,
    ga=120B5,
    ga2=120B7,
    ama=120BC,
    ur=12328,
    ur2=1232B,
    ur3=120E1,
    esag=120E5,
    esag2=120E5,
    galga=120CC,
    gan=120F6,
    gan2=120F7,
    eren=1209E,
    cec=122C0,
    cec2=122C1,
    cunigin=0\swapc\char"122D7\swapa\char"121B8,
    cunigin2=0\swapc\char"122D7\swapa\char"121B8,
    sig=122DD,
    sig2=122E0,
    sig4=122DE,
    sig5=12146\char"1209F,
    sig7=1214A,
    sig2munus=122E0\swapc\char"122A9\swapa,
    ig=12145,
    ku=121AA,
    ku3=121AC,
    ku7=121AF,
    gicimmar=122B7,
    dara=12070,
    dara3=12070,
    ni=1224C,
    na=1223E,
    na2=1223F,
    na4=1238E,
    nim=1224F,
    gag=12195,
    nee=1224D,
    ir=12155,
    lu=121FB,
    lu2=121FD,
    lu3=12216,
    acgab=1203F,
    me=12228,
    me3=1201E,
    gun=1206F,
    gun3=1206F,
    gunni=121A0\char"1224C,
    iti=12317,
    cinig=122D2,
    si=122DB,
    ma=12220,
    ma2=12223,
    cudun=12088,
    cudun3=12088,
    diri=122DB\char"12000,
    tab=122F0,
    cum=122F3,
    az=1228D,
    ug=1228C,
    nib=1228B,
    uku=12087,
    um=1231D,
    nab=1202E,
    mul=1202F,
    de=12324,
    de2=12324,
    azu=12259,
    ad=1201C,
    kec=1219F,
    kec2=1219F,
    ezen=12378,
    ezen4=12378,
    zi=12363,
    zi2=12362,
    urudu=1234F,
    muru=12318,
    muru2=12318,
    unu=12014,
    mic=12229,
    bad=12041,
    bad3=120A6,
    lugal=12217,
    dim=12074,
    dim3=12290\swapc\char"120F5\swapa,
    pan=1227C,
    ninda=12252,
    ninda2=12252,
    kum=12123,
    ag=1201D,
    ag2=12258,
    gaz=12124,
    cam=1225A\char"12000\char"1202D,
    cam2=1225A\char"12000\char"1202D,
    cam3=12253,
    egir=12095,
    lil=121F8,
    kac=1207D,
    kac4=1207D,
    dub=1207E,
    dub2=12080,
    gectin=120FE,
    uc=12351,
    ukkin=1215E,
    nundum=1217B,
    su=122E2,
    su6=1217E,
    kaxur2=12387,
    kaxpa=12384,
    kaxgic=12380,
    kir=1216F,
    kir14=1216F,
    kaxu=12187,
    kaxlum=12383,
    zu=1236A,
    zu9=12189,
    bun=1216E,
    bun2=1216E,
    tukur=12181,
    tukur2=12181,
    mu=1222C,
    mu7=12172,
    eme=12174,
    nag=12158,
    gu=12116,
    gu2=12118,
    gu7=12165,
    kaxac2=1237F,
    ic=12156,
    kuc=12156,
    kuc7=12156,
    ub=12312,
    bi=12049,
    cem=122C6,
    lunga=122C6,
    amar=1202B,
    siskur=1202C,
    gud=1211E,
    ta=122EB,
    il=1214B,
    il2=1214D,
    dug=12081,
    bappir=122CB,
    gab=120EE,
    duh=12083,
    gim=12076,
    umbin=12322,
    ka=12157,
    ka2=1218D,
    am=12120,
    ne=12248,
    lal=121F2,
    lal3=122ED,
    tah=1222D,
    gibil=1224B,
    gad=120F0,
    cab=1227A\char"12141,
    mackim=1227A\char"1207D,
    sipa=1227A\char"121FB,
    gic=12111,
    hab=121B8,
    tul=121E5,
    tul2=121E5,
    lagabxsum=121E1,
    al=12020,
    gur=12125,
    lagar=121EC,
    utul=1205F,
    utul5=1205F,
    ubur=1205B,
    ekisim5xamac=12057,
    mar=12225,
    sag=12295,
    gurun=12127,
    kid=121A4,
    kal=12197,
    un=12326,
    luh=1221B,
    e=1208A,
    e2=1208D,
    sa=12293,
    dur=12119,
    uzu=1235C,
    nir=1226A,
    ba=12040,
    mud=12137\char"1212D,
    mud4=121AA\char"1230B\char"12195,
    du=1207A,
    du6=121EF,
    tug=12306,
    tug2=12306,
    da=12055,
    engur=121C9,
    i=1213F,
    ia=12140,
    hachur=12222,
    gug=12216,
    gug2=12216,
    nimgir=12216,
    gin=12086,
    gin2=12086,
    gigir=121C0,
    balag=12046,
    alam=12029,
    tag=122FA,
    tag4=122FA,
    kisal=121A6,
    kislah=121A0\char"12313,
    cen=122BF,
    sanga=122C3,
    lahta=12064,
    ra=1228F,
    gi=12100,
    gi4=12104,
    bar=12047,
    bar2=12048,
    lah=1207B,
    lah4=1207B,
    zag=12360,
    gar=120FC,
    gar3=120FC,
    ac=12038,
    ac2=1203E,
    gal=120F2,
    dag=12056,
    bur=12053,
    uri=12335,
    gam=120F5,
    gloss=12471,
    te=122FC,
    kar=122FC\char"12000,
    karac=121A0\char"12197\char"12041,
    cu=122D7,
    gecpu2=122D7\char"1204D,
    cu2=122D9,
    gukkalkun=121FB\swapc\char"1213E\char"121B2\swapa,
    idigna=0\swapc\char"12226\char"12118\char"120FC\swapa,
    ukuc=1213E,
    ukuc2=1213E,
    pab=1227D,
    pabhal=0\swapb\char"1227D\swapa\char"1212C,
    bulug=0\swapc\char"1227D,
    bulug3=0\swapc\char"1227D,
    gilim=12103,
    kaskal=1219C,
    kib=12112,
    dul=1230B\char"12306,
    ictar=1230B\char"1206F,
    udun=1230B\char"1222C,
    mi=1222A,
    dugud=12082,
    gig=1222A\char"1226D,
    cagan=1230B\char"120F6,
    gul=12122,
    ugu=1230B\char"12157,
    kic=121A7,
    buru=12098,
    buru14=12098,
    ul=1230C,
    amarxku6=12372,
    ab=1200A,
    ab2=12016,
    kisim=121A8,
    kisim5=121A8,
    tum=12308,
    tum3=12250,
    kisim5xu2mac=100007,
    ab2xa=12371,
    lic=121FA,
    11=1230B\char"12079,
    igi=12146,
    ci=12146,
    ar=12148,
    hul=12146\char"12328,
    agrig=12146\char"1207E,
    pa=1227A,
    pa3=12146\char"12292,
    ca=122AD,
    ca2=122FB,
    ca3=122AE,
    pad=1227B,
    20=1230B\char"1230B,
    sal=122A9,
    dam=1206E,
    nin=122A9\char"12306,
    zum=1236E,
    ance=12032,
    alim=1210B,
    geme=122A9\char"121B3,
    geme2=122A9\char"121B3,
    lam=121F4,
    el=12096,
    nagar=12244,
    cah=122DA,
    lum=1221D,
    di=12072,
    ki=121A0,
    uru=12337,
    uru5=122FD,
    ud=12313,
    wa=1227F,
    waap=12281,
    wii=12285,
    wipi=12282,
    wuu=12287,
    wuu2=12288,
    wee=12284,
    wupu=12283,
    wiip=12286,
    waa=12280,
    erin=1209F,
    erin2=1209F,
    nunuz=1226D,
    kur=121B3,
    tin=12077,
    30=1230D,
    ah=12134,
    har=1212F,
    bir=12135,
    hi=1212D,
    gacan=120FD,
    im=1214E,
    bu=1204D,
    sud=122E4,
    sur7=121A0\char"12195,
    muc=12232,
    li=121F7,
    tir=12301,
    nidaba=122BA\char"12240,
    tu=12305,
    50=12410,
    huc=1212D\char"1210A,
    suhur=122E6,
    sum=122E7,
    lul=10000A,
    naga=12240,
    car=122AC,
    in=12154,
    kam=12130,
    dic=12079,
    1=12079,
    70=12079\char"1230B,
    mec=12228\char"1230D,
    2=1222B,
    la=121B7,
    la5=121F3,
    cur=122E9,
    cur4=121F3\char"122AC,
    a=12000,
    id=12009,
    id2=12000\char"121C9,
    za=1235D,
    ha=12129,
    3=12408,
    4=12409,
    5=1240A,
    6=1240B,
    7=1240C,
    8=1240D,
    9=1240E
}

\NewDocumentCommand\Hitt{O{}m}
{
    % switch cases for font selection with optional argument
    \str_case:nn {#1}
    {
        {A} {\swapa}
        {B} {\swapb}
        {C} {\swapc}
        {} {\swapa}
    }

    % splitting on spaces to allow for legibility of cuneiform inputs
    \seq_set_split:Nnn {\l_tmpa_seq} {~} {#2}
    \seq_show:N \l_tmpa_seq


    \seq_map_function:NN \l_tmpa_seq \period_split

    % swap back to Latin characters once all cuneiform is finished
    \swaplat
}

% secondary split on periods for word components - keep all functions separate because nesting does bad things with expansion
\cs_new:Npn \period_split #1
{
    \seq_set_split:Nnn {\l_tmpb_seq} {.} {#1}
    \seq_show:N \l_tmpb_seq
    \seq_map_function:NN \l_tmpb_seq \hitt_dict_lookup % map over all characters
}

\cs_new:Npn \hitt_dict_lookup #1
{
    \prop_if_in:NnTF \c_hitt_dict {#1}
    {   
        \prop_get:NnN \c_hitt_dict {#1} \l_tmpa_tl
        % Use \char from the retrieved Unicode value
        \char"\l_tmpa_tl\allowbreak
    }
    {
        % If no match, output the character itself
        \swaplat
        {#1}
        \swapa
    }
}

\ExplSyntaxOff


