% \iffalse meta-comment
%
% File: unisc.dtx
% -------------------------------------------------------
% Package:     unisc
% Version:     v0.2 (29 April, 2022)
% Author:      निरंजन
% Description: Unicode small caps with Xe/LuaLaTeX.
% Repository:  https://puszcza.gnu.org.ua/projects/unisc
% License:     GPLv3+, GFDLv1.3+
% -------------------------------------------------------
%
% The LaTeX Package unisc v0.2
% Copyright © 2022 निरंजन
%
% This program is free software: you can redistribute it
% and/or modify it under the terms of the GNU General Public
% License as published by the Free Software Foundation,
% either version 3 of the License, or (at your option) any
% later version.
%
% This program is distributed in the hope that it will be
% useful, but WITHOUT ANY WARRANTY; without even the implied
% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
% PURPOSE. See the GNU General Public License for more
% details.
%
% You should have received a copy of the GNU General Public
% License along with this program. If not, see
% <https://www.gnu.org/licenses/>.
%
% The current maintainer of this work is निरंजन.
%
% This work consists of the files unisc.dtx
%                                 unisc.ins
% and the derived file            unisc.sty.
% \fi
% \iffalse
%<*internal>
\iffalse
%</internal>
%<*readme>
-------------------------------------------------------
Package:     unisc
Version:     v0.2 (29 April, 2022)
Author:      निरंजन
Description: Unicode small caps with Xe/LuaLaTeX.
Repository:  https://puszcza.gnu.org.ua/projects/unisc
License:     GPLv3+, GFDLv1.3+
-------------------------------------------------------
%</readme>
%<*internal>
\fi
%</internal>
%<*driver|package>
\def\uniscname{unisc}
\def\uniscversion{0.2}
\def\uniscdate{2022-04-29}
\def\uniscdescription{%
  Unicode small caps with Xe/LuaLaTeX.%
}
%</driver|package>
%<*driver>
\documentclass{l3doc}
\usepackage{xcolor}
\usepackage{fontawesome5}
\usepackage{hologo}
\DeclareRobustCommand\XeLaTeX{\hologo{XeLaTeX}}
\usepackage{unisc}
\usepackage{hyperref,hyperxmp}
\hypersetup{%
  unicode,%
  colorlinks,%
  urlcolor=blue!60!black,%
  linkcolor=red!60!black,%
  citecolor=green!60!black,%
  pdftitle={The unisc package},%
  pdfauthor={निरंजन},%
  pdfsubject={\uniscdescription},%
  pdfcreator={निरंजन},%
  pdfkeywords={Unicode, small caps, LaTeX},%
  pdfcopyright={%
    The LaTeX package unisc\textLF
    Copyright © 2022 निरंजन\textLF
    Permission is granted to copy, distribute and/or modify
    this document under the terms of the GNU Free
    Documentation License, Version 1.3 or any later version
    published by the Free Software Foundation; with no
    Invariant Sections, no Front-Cover Texts, and no
    Back-Cover Texts. A copy of the license is included in
    the section entitled “GNU Free Documentation License”.%
  },%
  pdflicenseurl={https://www.gnu.org/licenses/fdl-1.3.txt}%
}
\ExplSyntaxOn
\pdf_version_gset:n{2.0}
\ExplSyntaxOff
\usepackage{mdframed}
\usepackage{fontspec}
\newfontfamily\charissil{CharisSIL}
\newfontfamily\devanagari[%
  Script=Devanagari,%
  Scale=0.8%
]%
{Shobhika}
\NewCommandCopy\oldamp\&
\def\&{\textit{\oldamp}}

\begin{document}
    \DocInput{\uniscname.dtx}
\end{document}
%</driver>
% \fi
% \title{The \pkg{\uniscname} package}
% \author{^^A
%   {\devanagari निरंजन}\thanks{^^A
%     \href{mailto:hi.niranjan@pm.me}^^A
%     {\faIcon{envelope} \texttt{hi.niranjan@pm.me}}^^A
%   }^^A
% }
%     \date{^^A
%   Version \uniscversion\ --- \uniscdate\\[1ex]^^A
%   {^^A
%     \small
%     \faIcon{globe}^^A
%     \quad
%     \url{https://puszcza.gnu.org.ua/projects/unisc}\\[0.5ex]^^A
%     \faIcon{bug}^^A
%     \quad
%     \url{https://puszcza.gnu.org.ua/bugs/?group=unisc}^^A
%   }^^A
% }
%
% \maketitle
% 
% \begin{documentation}
%   \begin{abstract}
%     \LaTeX\ produces small caps with
%     \cs{textsc}\marg{text} or \cs{scshape}. Both of these
%     commands don't produce small caps in Unicode. If the
%     output text is copied and pasted somewhere it shows
%     the same characters used in the input. This package
%     aims to internally convert all the characters provided
%     to the commands mentioned above. It assumes that the
%     file using this package is compiled with Lua/\XeLaTeX\
%     \& a good Unicode font which has the small caps
%     characters, e.g.,
%     \href{https://ctan.org/pkg/charissil}{Charis
%     SIL}. Please note that as there are no Unicode slots
%     allotted to upper case small caps, both the lower case
%     and upper case input will be converted to only one
%     relevant Unicode character in the output, e.g.,
%     \verb|\textsc{Hh}| will produce the same character
%     twice unlike \verb|\oldtextsc{Hh}|.
%   \end{abstract}
%   
%   \tableofcontents
%   
%   \vfill
%   
%   {^^A
%     \setlength{\parindent}{0pt}
%     The \LaTeX\ package \pkg{unisc}
%     
%     Copyright © 2022 {\devanagari निरंजन}
%   
%     Permission is granted to copy, distribute and/or
%     modify this document under the terms of the GNU Free
%     Documentation License, Version 1.3 or any later
%     version published by the Free Software Foundation;
%     with no Invariant Sections, no Front-Cover Texts, and
%     no Back-Cover Texts. A copy of the license is included
%     in the section entitled ``GNU Free Documentation
%     License''.^^A
%   }^^A
%     
%     \newpage
%     
%     \section{Documentation}
%     
%     This package provides the following two macros.
%
%     \subsection{Macros}
%     
%     This package renews two basic \LaTeX\ commands, i.e.,
%     \cs{scshape} \& \cs{textsc}. To keep the original
%     definitions available for the users, I have copied the
%     old commands \& provided two of the following macros
%     which behave exactly like their old variants.
%     
%     \begin{function}{\oldscshape}
%       \begin{syntax}
%         \{\cs{oldscshape} \meta{text}\}
%       \end{syntax}
%     \end{function}
%
%     \begin{function}{\oldtextsc}
%       \begin{syntax}
%         \cs{oldtextsc}\marg{text}
%       \end{syntax}
%     \end{function}
%
%     \subsection{The \enquote*{x} factor}
%
%     This package doesn't support the small capital shape
%     of the character \enquote*{x} as there is no Unicode
%     number allotted for a \enquote*{small capital x} as of
%     now. The package simply prints the small x (for both
%     \verb|\textsc{x}| \& \verb|\textsc{X}|) as it is of
%     the same height of the other small capital letters
%     along with a warning message that warns the users
%     about this deviation.
%     
%     \subsection{Example}
%     
%     \begin{mdframed}
% \begin{verbatim}
% \documentclass{article}
% \usepackage{unisc}
% \usepackage{fontspec}
% \setmainfont{CharisSIL}% https://ctan.org/pkg/charissil

% \begin{document}
% \noindent\textbf{Fake small caps:}
% {\oldscshape hello world} \oldtextsc{hello world}.

% \noindent\textbf{Unicode small caps:}
% {\scshape hello world} \textsc{hello world}.
% \end{document}
% \end{verbatim}
%     \end{mdframed}
%     
%     This produces the following:
%     
%     \begin{mdframed}
%       \charissil
%       \noindent\textbf{Fake small caps:}
%       {\oldscshape hello world} \oldtextsc{hello world}.
%       
%       \noindent\textbf{Unicode small caps:}
%       {\scshape hello world} \textsc{hello world}.
%     \end{mdframed}
%   \end{documentation}
%   
%   \newpage
%   
%   \begin{implementation}
%     \section{Implementation}
%     Provide package with its basic information.
%    \begin{macrocode}
%<@@=unisc>
%<*package>
\ProvidesPackage{unisc}[2022/04/29 v0.2
Unicode small caps with Lua/XeLaTeX.]
%    \end{macrocode}
% Load the necessary packages.
%    \begin{macrocode}
\RequirePackage{pgfparser}
\RequirePackage{xpatch}
%    \end{macrocode}
% Make copies of old commands.
%    \begin{macrocode}
\NewCommandCopy\oldtextsc\textsc
\NewCommandCopy\oldscshape\scshape
%    \end{macrocode}
% As pointed out by Florent Rougon in
% \href{https://topanswers.xyz/transcript?room=2050&id=138859#c138859}%
% {this} comment; the definition of \cs{oldtextsc} uses
% \cs{scshape} twice which needs to be replaced with
% \cs{oldscshape}. For that the following patching is done
% \& the old definition is retained.
%    \begin{macrocode}
\xpatchcmd{\oldtextsc}{\scshape}{\oldscshape}{}{\ERROR}
\xpatchcmd{\oldtextsc}{\scshape}{\oldscshape}{}{\ERROR}
%    \end{macrocode}
% The following code is kindly provided by Jonathan P.\
% Spratte on the following thread:
% \url{https://topanswers.xyz/tex?q=2005}. I have modified
% it a bit as per my needs and my indentation style.
%    \begin{macrocode}
\protected\long\def\unisc@output#1{%
  \xdef\unisc@outputcontainer{%
    \unexpanded\expandafter{%
      \unisc@outputcontainer\unexpanded{#1}%
    }%
  }%
}
\protected\long\def\unisc@outputexpanded#1{%
  \xdef\unisc@outputcontainer{%
    \unexpanded\expandafter{\unisc@outputcontainer#1%
    }%
  }%
}
\providecommand\@gobbletwo[2]{}
\pgfparserdefunknown{scparser}{initial}{%
  \let\unisc@initiated\@empty
  \pgfparserswitch{initiated}%
  \pgfparserreinsert
}
\pgfparserset{scparser/silent=true}
\pgfparserdefunknown{scparser}{all}{%
  \expandafter\unisc@output\expandafter{\pgfparserletter}%
}
\newcommand*\unisc@bgroup{\expandafter{\iffalse}\fi}
\newcommand*\unisc@egroup{\iffalse{\fi}}
\pgfparserdef{scparser}{all}\begingroup
  {\begingroup\unisc@outputexpanded\begingroup}
\pgfparserdef{scparser}{all}\endgroup
  {\unisc@checkend\endgroup}
\pgfparserdef{scparser}{all}{\meaning\bgroup}{%
  \unisc@bgroup\unisc@outputexpanded\unisc@bgroup
}
\pgfparserdef{scparser}{all}{\meaning\egroup}
  {\unisc@checkend\unisc@egroup}
\newcommand\unisc@savedefinition[1]
  {\unexpanded{\def#1}{\unexpanded\expandafter{#1}}}
\protected\def\unisc@checkend#1{%
  \expanded{%
    \unexpanded{#1}%
    \unexpanded{\ifdefined\unisc@initiated}%
    \unisc@savedefinition\pgfparserstate
    \unexpanded{\unisc@outputexpanded#1}%
    \unexpanded{\else}%
    \unexpanded{\pgfparserswitch{final}}%
    \unisc@savedefinition\pgfparser@current
    \unisc@savedefinition\pgfparser@usersname
    \unexpanded{\fi}%
  }%
}
%    \end{macrocode}
% Following is the code for the replacements of the
% regular characters with the Unicode ones.
%    \begin{macrocode}
%% LATIN LETTER SMALL CAPITAL A
\pgfparserdef{scparser}{initiated}a%
  {\unisc@outputexpanded{\symbol{"1D00}}}
\pgfparserdef{scparser}{initiated}A%
  {\unisc@outputexpanded{\symbol{"1D00}}}
%% LATIN LETTER SMALL CAPITAL B
\pgfparserdef{scparser}{initiated}b%
  {\unisc@outputexpanded{\symbol{"0299}}}
\pgfparserdef{scparser}{initiated}B%
  {\unisc@outputexpanded{\symbol{"0299}}}
%% LATIN LETTER SMALL CAPITAL C
\pgfparserdef{scparser}{initiated}c%
  {\unisc@outputexpanded{\symbol{"1D04}}}
\pgfparserdef{scparser}{initiated}C%
  {\unisc@outputexpanded{\symbol{"1D04}}}
%% LATIN LETTER SMALL CAPITAL D
\pgfparserdef{scparser}{initiated}d%
  {\unisc@outputexpanded{\symbol{"1D05}}}
\pgfparserdef{scparser}{initiated}D%
  {\unisc@outputexpanded{\symbol{"1D05}}}
%% LATIN LETTER SMALL CAPITAL E
\pgfparserdef{scparser}{initiated}e%
  {\unisc@outputexpanded{\symbol{"1D07}}}
\pgfparserdef{scparser}{initiated}E%
  {\unisc@outputexpanded{\symbol{"1D07}}}
%% LATIN LETTER SMALL CAPITAL F
\pgfparserdef{scparser}{initiated}f%
  {\unisc@outputexpanded{\symbol{"A730}}}
\pgfparserdef{scparser}{initiated}F%
  {\unisc@outputexpanded{\symbol{"A730}}}
%% LATIN LETTER SMALL CAPITAL G
\pgfparserdef{scparser}{initiated}g%
  {\unisc@outputexpanded{\symbol{"0262}}}
\pgfparserdef{scparser}{initiated}G%
  {\unisc@outputexpanded{\symbol{"0262}}}
%% LATIN LETTER SMALL CAPITAL H
\pgfparserdef{scparser}{initiated}h%
  {\unisc@outputexpanded{\symbol{"029C}}}
\pgfparserdef{scparser}{initiated}H%
  {\unisc@outputexpanded{\symbol{"029C}}}
%% LATIN LETTER SMALL CAPITAL I
\pgfparserdef{scparser}{initiated}i%
  {\unisc@outputexpanded{\symbol{"026A}}}
\pgfparserdef{scparser}{initiated}I%
  {\unisc@outputexpanded{\symbol{"026A}}}
%% LATIN LETTER SMALL CAPITAL J
\pgfparserdef{scparser}{initiated}j%
  {\unisc@outputexpanded{\symbol{"1D0A}}}
\pgfparserdef{scparser}{initiated}J%
  {\unisc@outputexpanded{\symbol{"1D0A}}}
%% LATIN LETTER SMALL CAPITAL K
\pgfparserdef{scparser}{initiated}k%
  {\unisc@outputexpanded{\symbol{"1D0B}}}
\pgfparserdef{scparser}{initiated}K%
  {\unisc@outputexpanded{\symbol{"1D0B}}}
%% LATIN LETTER SMALL CAPITAL L
\pgfparserdef{scparser}{initiated}l%
  {\unisc@outputexpanded{\symbol{"029F}}}
\pgfparserdef{scparser}{initiated}L%
  {\unisc@outputexpanded{\symbol{"029F}}}
%% LATIN LETTER SMALL CAPITAL M
\pgfparserdef{scparser}{initiated}m%
  {\unisc@outputexpanded{\symbol{"1D0D}}}
\pgfparserdef{scparser}{initiated}M%
  {\unisc@outputexpanded{\symbol{"1D0D}}}
%% LATIN LETTER SMALL CAPITAL N
\pgfparserdef{scparser}{initiated}n%
  {\unisc@outputexpanded{\symbol{"0274}}}
\pgfparserdef{scparser}{initiated}N%
  {\unisc@outputexpanded{\symbol{"0274}}}
%% LATIN LETTER SMALL CAPITAL O
\pgfparserdef{scparser}{initiated}o%
  {\unisc@outputexpanded{\symbol{"1D0F}}}
\pgfparserdef{scparser}{initiated}O%
  {\unisc@outputexpanded{\symbol{"1D0F}}}
%% LATIN LETTER SMALL CAPITAL P
\pgfparserdef{scparser}{initiated}p%
  {\unisc@outputexpanded{\symbol{"1D18}}}
\pgfparserdef{scparser}{initiated}P%
  {\unisc@outputexpanded{\symbol{"1D18}}}
%% LATIN LETTER SMALL CAPITAL Q
\pgfparserdef{scparser}{initiated}q%
  {\unisc@outputexpanded{\symbol{"A7AF}}}
\pgfparserdef{scparser}{initiated}Q%
  {\unisc@outputexpanded{\symbol{"A7AF}}}
%% LATIN LETTER SMALL CAPITAL R
\pgfparserdef{scparser}{initiated}r%
  {\unisc@outputexpanded{\symbol{"0280}}}
\pgfparserdef{scparser}{initiated}R%
  {\unisc@outputexpanded{\symbol{"0280}}}
%% LATIN LETTER SMALL CAPITAL S
\pgfparserdef{scparser}{initiated}s%
  {\unisc@outputexpanded{\symbol{"A731}}}
\pgfparserdef{scparser}{initiated}S%
  {\unisc@outputexpanded{\symbol{"A731}}}
%% LATIN LETTER SMALL CAPITAL T
\pgfparserdef{scparser}{initiated}t%
  {\unisc@outputexpanded{\symbol{"1D1B}}}
\pgfparserdef{scparser}{initiated}T%
  {\unisc@outputexpanded{\symbol{"1D1B}}}
%% LATIN LETTER SMALL CAPITAL U
\pgfparserdef{scparser}{initiated}u%
  {\unisc@outputexpanded{\symbol{"1D1C}}}
\pgfparserdef{scparser}{initiated}U%
  {\unisc@outputexpanded{\symbol{"1D1C}}}
%% LATIN LETTER SMALL CAPITAL V
\pgfparserdef{scparser}{initiated}v%
  {\unisc@outputexpanded{\symbol{"1D20}}}
\pgfparserdef{scparser}{initiated}V%
  {\unisc@outputexpanded{\symbol{"1D20}}}
%% LATIN LETTER SMALL CAPITAL W
\pgfparserdef{scparser}{initiated}w%
  {\unisc@outputexpanded{\symbol{"1D21}}}
\pgfparserdef{scparser}{initiated}W%
  {\unisc@outputexpanded{\symbol{"1D21}}}
%% LATIN LETTER X
\pgfparserdef{scparser}{initiated}x{%
  \unisc@outputexpanded{\symbol{"0078}}%
  \PackageWarning{unisc}{%
    You have used the unsupported character `x' with the\MessageBreak
    \string\textsc\space or \string\scshape\space command%
  }%
}
\pgfparserdef{scparser}{initiated}X{%
  \unisc@outputexpanded{\symbol{"0078}}%
  \PackageWarning{unisc}{%
    You have used the unsupported character `X' in the\MessageBreak
    argument of \string\textsc
  }%
}
%% No Unicode number for small capital x as of now.
%% \textsc{X} == \textsc{x} for size-consistency.
%% LATIN LETTER SMALL CAPITAL Y
\pgfparserdef{scparser}{initiated}y%
  {\unisc@outputexpanded{\symbol{"028F}}}
\pgfparserdef{scparser}{initiated}Y%
  {\unisc@outputexpanded{\symbol{"028F}}}
%% LATIN LETTER SMALL CAPITAL Z
\pgfparserdef{scparser}{initiated}z%
  {\unisc@outputexpanded{\symbol{"1D22}}}
\pgfparserdef{scparser}{initiated}Z%
  {\unisc@outputexpanded{\symbol{"1D22}}}
%% BLANK SPACE
\pgfparserdef{scparser}{initiated}{blank space}%
  {\unisc@outputexpanded{ }}
%% END OF INPUT CHARACTERS
\pgfparserdeffinal{scparser}%
  {\expanded{\unisc@outputcontainer}}
%    \end{macrocode}
% Renew the definitions of \cs{textsc} \& \cs{scshape}
% commands.
%    \begin{macrocode}
\AddToHook{begindocument/end}{%
  \RenewDocumentCommand\scshape{  }{%
    \ifdefined\unisc@initiated
      \PackageError{unisc}{Nested use not allowed}{%
        \string\unisc@initiated\space already defined,
        this shouldn't happen. Aborting.%
      }%
      \expandafter\@gobbletwo
    \else
      \global\let\unisc@outputcontainer\@empty
    \fi
    \pgfparserparse{scparser}%
  }
  \RenewDocumentCommand\textsc{ m }{%
    \begingroup\scshape #1\endgroup
  }%
}
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \end{implementation}
% \newpage
% \addcontentsline{toc}{section}{^^A
%   GNU Free Documentation License^^A
% }
% \include{gfdl-tex.tex}
% \Finale