%%
%% This is file `xCJK2uni.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% xCJK2uni.dtx  (with options: `package')
%% 
%%     Copyright (C) 2013-2014, 2016, 2018-2019 by Qing Lee <sobenlee@gmail.com>
%% --------------------------------------------------------------------------
%% 
%%     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. This version of this license is in
%%        http://www.latex-project.org/lppl/lppl-1-3c.txt
%%     and the latest version of this license is in
%%        http://www.latex-project.org/lppl.txt
%%     and version 1.3 or later is part of all distributions of
%%     LaTeX version 2005/12/01 or later.
%% 
%%     This work has the LPPL maintenance status `maintained'.
%% 
%%     The Current Maintainer of this work is Qing Lee.
%% 
%% --------------------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\GetIdInfo$Id: xCJK2uni.dtx 21b29fd 2019-04-07 23:27:31 +0800 Qing Lee <sobenlee@gmail.com> $
  {Convert CJK character to Unicode.}
\ProvidesExplPackage{\ExplFileName}
  {\ExplFileDate}{0.8}{\ExplFileDescription}
\msg_new:nnn { xCJK2uni } { l3-too-old }
  {
    Support~package~'expl3'~too~old. \\\\
    Please~update~an~up~to~date~version~of~the~bundles\\\\
    'l3kernel'~and~'l3packages'\\\\
    using~your~TeX~package~manager~or~from~CTAN.
  }
\@ifpackagelater { expl3 } { 2019/03/05 } { }
  { \msg_error:nn { xCJK2uni } { l3-too-old } }
\sys_if_engine_pdftex:F
  {
    \msg_new:nnn  { xCJK2uni } { pdflatex }
      { The~xCJK2uni~package~is~only~supported~in~pdfTeX. }
    \msg_critical:nn { xCJK2uni } { pdflatex }
  }
\tl_new:N \l__CJKtu_sfd_tl
\prop_new:N \c__CJKtu_sfd_prop
\seq_new:N \c__CJKtu_encoding_seq
\int_new:N \l__CJKtu_i_min_int
\int_new:N \l__CJKtu_i_max_int
\int_new:N \l__CJKtu_ii_min_int
\int_new:N \l__CJKtu_ii_max_int
\int_new:N \l__CJKtu_gap_begin_int
\int_new:N \l__CJKtu_gap_end_int
\int_new:N \l__CJKtu_plane_int
\int_set:Nn \l__CJKtu_i_max_int { "FE }
\int_set:Nn \l__CJKtu_ii_max_int { "FE }
\cs_new_protected_nopar:Npn \__CJKtu_set_enc:nnnnnn #1#2#3#4#5#6
  {
    \seq_gput_right:Nn \c__CJKtu_encoding_seq {#1}
    \prop_gput:Nnn \c__CJKtu_sfd_prop {#1} {#2}
    \int_const:cn { c__CJKtu_ #1 _i_min_int } {#3}
    \int_const:cn { c__CJKtu_ #1 _ii_min_int } {#4}
    \tl_if_empty:nTF {#5}
      {
        \int_const:cn { c__CJKtu_ #1 _gap_begin_int }
          { \l__CJKtu_ii_max_int + 1 }
        \int_const:cn { c__CJKtu_ #1 _gap_end_int }
          { \use:c { c__CJKtu_ #1 _gap_begin_int } }
        \int_const:cn { c__CJKtu_ #1 _plane_int }
          { \l__CJKtu_ii_max_int - (#4) + 1 }
      }
      {
        \int_const:cn { c__CJKtu_ #1 _gap_begin_int } { #5 + 1 }
        \int_const:cn { c__CJKtu_ #1 _gap_end_int } {#6}
        \int_const:cn { c__CJKtu_ #1 _plane_int }
          { (#5) - (#4) + \l__CJKtu_ii_max_int - (#6) + 2 }
      }
    \cs_if_exist_use:NTF \CJKaddEncHook
      { {#1} { \__CJKtu_enc_map:nn {#1} {#2} } }
      {
        \AtBeginDocument
          {
            \cs_if_exist_use:NT \CJKaddEncHook
              { {#1} { \__CJKtu_enc_map:nn {#1} {#2} } }
          }
      }
  }
\cs_new_protected_nopar:Npn \__CJKtu_enc_map:nn #1#2
  {
    \int_set_eq:Nc \l__CJKtu_i_min_int     { c__CJKtu_ #1 _i_min_int }
    \int_set_eq:Nc \l__CJKtu_ii_min_int    { c__CJKtu_ #1 _ii_min_int }
    \int_set_eq:Nc \l__CJKtu_gap_begin_int { c__CJKtu_ #1 _gap_begin_int }
    \int_set_eq:Nc \l__CJKtu_gap_end_int   { c__CJKtu_ #1 _gap_end_int }
    \int_set_eq:Nc \l__CJKtu_plane_int     { c__CJKtu_ #1 _plane_int }
    \cs_if_exist:cF { CJKtu_ #1 _sfd_map:nn }
      { \group_begin: \file_input:n { xCJK2uni-#2.def } \group_end: }
    \cs_set_eq:Nc \CJKtu_sfd_map:nn { CJKtu_ #1 _sfd_map:nn }
  }
\cs_new_protected_nopar:Npn \__CJKtu_use_enc_map:n #1
  {
    \prop_get:NxNTF \c__CJKtu_sfd_prop {#1} \l__CJKtu_sfd_tl
      { \__CJKtu_enc_map:nn {#1} { \l__CJKtu_sfd_tl } }
      { \msg_error:nnx { xCJK2uni } { invalid-encoding } {#1} }
  }
\msg_new:nnn { xCJK2uni } { invalid-encoding }
  {
    The~enconding~`#1'~is~invalid.\\
    Only~\seq_use:Nnnn \c__CJKtu_encoding_seq { ~and~ } { ,~ } { ,~and~ } ~
    are~supported.
  }
\prg_generate_conditional_variant:Nnn \prop_get:NnN { Nx } { TF }
\__CJKtu_set_enc:nnnnnn { GB   } { UGB }      { "A1 } { "A1 } { } { }
\__CJKtu_set_enc:nnnnnn { JIS  } { UJIS }     { "A1 } { "A1 } { } { }
\__CJKtu_set_enc:nnnnnn { KS   } { UKS }      { "A1 } { "A1 } { } { }
\__CJKtu_set_enc:nnnnnn { Bg5  } { UBig5 }    { "A1 } { "40 } { "7E } { "A1 }
\__CJKtu_set_enc:nnnnnn { Bg5+ } { UBg5plus } { "81 } { "40 } { "7E } { "80 }
\__CJKtu_set_enc:nnnnnn { GBK  } { UGBK }     { "81 } { "40 } { "7E } { "80 }
\cs_new_nopar:Npn \__CJKtu_sfd_to_unicode:nn #1#2
  {
    \exp:w \exp_last_unbraced:Nf
      \__CJKtu_get_unicode:w \CJKtu_sfd_map:nn {#1} {#2} \exp_end:
  }
\cs_new_nopar:Npn \__CJKtu_get_unicode:w #1 \exp_end:
  {
    \tl_if_empty:nTF {#1}
      { \exp_end: }
      { \__CJKtu_get_unicode_aux:w #1 \q_stop }
  }
\cs_new_nopar:Npn \__CJKtu_get_unicode_aux:w #1 \or: #2 \q_stop
  { \or: #2 \exp_end: #1 }
\group_begin:
\char_set_catcode_active:n { "7F }
\cs_new_nopar:Npn \__CJKtu_char_to_unicode:n #1
  {
    \exp:w
    \int_compare:nNnTF { \tl_count:n {#1} } = 2
      { \CJKtu_byte:NN #1 }
      {
        \tl_if_head_eq_meaning:nNTF {#1} ^^7f
          { \__CJKtu_byte:wNwnw #1 }
          { \__CJKtu_encoding_error: }
      }
  }
\cs_new_nopar:Npn \__CJKtu_byte:wNwnw ^^7f #1 ^^7f #2 ^^7f
  { \CJKtu_byte_map:nn { `#1 } {#2} }
\cs_new_nopar:Npn \__CJKtu_byte:Nwnw #1 ^^7f #2 ^^7f
  { \CJKtu_byte_map:nn { `#1 } {#2} }
\cs_new_protected_nopar:Npn \CJKtu_bookmarks_hook:
  {
    \cs_set_eq:NN ^^7f \__CJKtu_byte:Nwnw
    \cs_set_eq:NN \CJK@XX  \CJKtu_byte:NN
    \cs_set_eq:NN \CJKchar \__CJKtu_byte:wnn
    \cs_set_eq:NN \CCTchar \CJKtu_byte:nn
    \cs_set_eq:NN \@CCTSetChar \__CJKtu_CCT_byte:NNN
    \cs_set_eq:NN \__CJKtu_get_unicode:n \__CJKtu_UTF_xvi_be:n
  }
\group_end:
\cs_new_nopar:Npn \CJKtu_byte:NN #1#2
  { \CJKtu_byte:nn { `#1 } { `#2 } }
\cs_new_nopar:Npn \__CJKtu_CCT_byte:NNN #1#2#3
  { \CJKtu_byte:nn { `#1 } { `#3 } }
\cs_new_nopar:Npn \__CJKtu_byte:wnn #1# { \CJKtu_byte:nn }
\cs_new_nopar:Npn \CJKtu_byte:nn #1#2
  {
    \int_compare:nTF { \l__CJKtu_i_min_int <= #1 <= \l__CJKtu_i_max_int }
      {
        \int_compare:nTF { \l__CJKtu_ii_min_int <= #2 < \l__CJKtu_gap_begin_int }
          { \CJKtu_byte_map:nn {#1} {#2} }
          {
            \int_compare:nTF
              { \l__CJKtu_gap_end_int <= #2 <= \l__CJKtu_ii_max_int }
              { \CJKtu_byte_map:nn {#1} {#2} }
              { \__CJKtu_encoding_error: }
          }
      }
      { \__CJKtu_encoding_error: }
  }
\cs_new_nopar:Npn \__CJKtu_encoding_error:
  {
    \msg_expandable_error:nn { xCJK2uni } { encoding-error }
    \exp_end:
  }
\msg_new:nnn { xCJK2uni } { encoding-error }
  { Wrong~encoding~scheme. }
\cs_new_nopar:Npn \CJKtu_byte_map:nn #1#2
  {
    \exp_args:Nf \__CJKtu_index_map:n
      {
        \int_eval:n
          {
              ( #1 - \l__CJKtu_i_min_int ) * \l__CJKtu_plane_int
            + (#2) - \l__CJKtu_ii_min_int
            \int_compare:nNnF {#2} < \l__CJKtu_gap_begin_int
              { + \l__CJKtu_gap_begin_int - \l__CJKtu_gap_end_int }
          }
      }
  }
\cs_new_nopar:Npn \__CJKtu_index_map:n #1
  {
    \__CJKtu_get_unicode:n
      {
        \CJKtu_sfd_map:nn
          { \int_div_truncate:nn {#1} { 256 } + 1 }
          { \int_mod:nn {#1} { 256 } }
      }
  }
\cs_new_nopar:Npn \__CJKtu_get_unicode:n #1
  { \exp_last_unbraced:Nf \__CJKtu_get_unicode:w #1 \exp_end: }
\cs_new_nopar:Npn \__CJKtu_UTF_xvi_be:n #1
  { \exp_args:Nf \__CJKtu_UTF_xvi_aux:n { \int_eval:n { "#1 } } }
\cs_new_nopar:Npn \__CJKtu_UTF_xvi_aux:n #1
  {
    \HyPsd@DecimalToOctalFirst
      { \int_div_truncate:nn {#1} { 256 } }
    \HyPsd@DecimalToOctalSecond
      { \int_mod:nn {#1} { 256 } }
  }
\cs_if_exist_use:NTF \hypersetup { { unicode , CJKbookmarks = false } }
  { \PassOptionsToPackage { unicode , CJKbookmarks = false } { hyperref } }
\msg_new:nnn { xCJK2uni } { UTF-8-encoding }
  { It~is~not~necessary~to~load~xCJK2uni~in~UTF-8~encoding. }
\tl_if_exist:NF \pdfstringdefPreHook { \tl_new:N \pdfstringdefPreHook }
\@ifpackageloaded { CJKutf8 }
  { \msg_warning:nn { xCJK2uni } { UTF-8-encoding } }
  {
    \cs_new_eq:NN \CJKtu@bookmarks@hook \CJKtu_bookmarks_hook:
    \tl_gput_right:Nn \pdfstringdefPreHook { \CJKtu@bookmarks@hook }
    \AtBeginDocument
      {
        \@ifpackageloaded { CJKutf8 }
          {
            \cs_gset_eq:NN \CJKtu@bookmarks@hook \scan_stop:
            \msg_warning:nn { xCJK2uni } { UTF-8-encoding }
          }
          { \cs_if_exist_use:NT \hypersetup { { CJKbookmarks = false } } }
      }
  }
\cs_new_eq:NN \useCJKencmap \__CJKtu_use_enc_map:n
\cs_new_eq:NN \CJKchartouni \__CJKtu_char_to_unicode:n
\cs_new_eq:NN \CJKsfdtouni  \__CJKtu_sfd_to_unicode:nn
\cs_new_protected_nopar:Npn \__CJKtu_initial:N #1
  {
    \tl_if_exist:NTF #1
      {
        \prop_get:NxNTF \c__CJKtu_sfd_prop {#1} \l__CJKtu_sfd_tl
          { \__CJKtu_enc_map:nn {#1} { \l__CJKtu_sfd_tl } }
          { \__CJKtu_use_enc_map:n { GBK } }
      }
      { \__CJKtu_use_enc_map:n { GBK } }
  }
\exp_args:Nc \__CJKtu_initial:N { CJK @ @ @ enc }
%% 
%%     This package consists of the file  xCJK2uni.dtx,
%%                  and the derived files xCJK2uni.pdf,
%%                                        xCJK2uni.sty,
%%                                        xCJK2uni.ins,
%%                                        xCJK2uni-make.tex,
%%                                        xCJK2uni-sfd.def,
%%                                        xCJK2uni-UBg5plus.def,
%%                                        xCJK2uni-UBig5.def,
%%                                        xCJK2uni-UGB.def,
%%                                        xCJK2uni-UGBK.def,
%%                                        xCJK2uni-UJIS.def,
%%                                        xCJK2uni-UKS.def,
%%                                        c****.cmap, and
%%                                        README.md.
%%
%% End of file `xCJK2uni.sty'.
