%% 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 lydtype.sty, lydtype-doc.tex
%    and the derived file lydtype-doc.pdf. The Noto Sans Lydian font is distributed under the terms of the SIL Open Font License 1.1 as included in the package.
% 
%
% Version history:
% 1.0 - Original Release Version

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lydtype}[2025/11/03 Lydian Typing Package]

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

%Formatting commands
\newcommand{\badeq}{\char"2E17}
\newcommand{\hl}{\char"2E22}
\newcommand{\hr}{\char"2E23}
\newcommand{\tsup}[1]{\textsuperscript{#1}}
\newcommand{\tsub}[1]{\textsubscript{#1}}


\ExplSyntaxOn

% Font selection commands, meant to be basically internal
\newcommand{\swapa}{\setmainfont[ExternalLocation=Fonts/]{NotoSansLydian-Regular.ttf}\textdir TRT\pardir TRT}
\newcommand{\swaplat}{\setmainfont{Charis SIL}\textdir TLT\pardir TLT}

% Define token-to-Unicode mapping
\prop_new:N \c_hitt_dict
\prop_set_from_keyval:Nn \c_hitt_dict {
    a=10920,
    b=10921,
    p=10921,
    g=10922,
    d=10923,
    ð=10923,
    e=10924,
    v=10925,
    w=10925,
    i=10926,
    y=10927,
    k=10928,
    l=10929,
    m=1092A,
    n=1092B,
    o=1092C,
    r=1092D,
    s=1092E,
    ś=1092E,
    t=1092F,
    u=10930,
    f=10931,
    q=10932,
    kw=10932,
    sh=10933,
    š=10933,
    š=10933,
    ç=10933,
    ch=10934,
    tsh=10934,
    tch=10934,
    tç=10934,
    ã=10935,
    ẽ=10936,
    λ=10937,
    gl=10937,
    lh=10937,
    ll=10937,
    ng=10938,
    c=10939,
    ts=10939
}

\NewDocumentCommand\Lyd{O{}m}
{
    % switch cases for font selection with optional argument
    \str_case:nn {#1}
    {
        {} {\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 \lydian_dict_lookup % map over all characters
}

\cs_new:Npn \lydian_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


