%%
%% This is file `GS1.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% GS1.dtx  (with options: `package')
%% 
%% EXPERIMENTAL CODE
%% 
%% This Work depends on the correct version of expl3 release.  If
%% you'll update expl3 you may need a update of this this Work too.
%% 
%% Do not distribute this file without also distributing the
%% source files specified above.
%% 
%% Do not distribute modified version of this file.
%% 
%% File: GS1.dtx Copyright (C) 2012-2017 Markus Kohm
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "GS1 bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -------------------------------------------------------------------------
\RequirePackage{expl3}[2017/05/29]
\GetIdInfo$Id: GS1.dtx 23 2021-06-17 07:00:36Z mjk $
  {GS1 code handler and barcode generator}
\ProvidesExplPackage
  {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
\RequirePackage{rule-D}
\cs_new_nopar:Npn \__GS_new_seq_c:cn #1#2
  {
    \seq_new:c {c__GS_ #1 _seq}
    \seq_set_split:Nnn \l_tmpa_seq {} {#2}
    \seq_gset_eq:cN {c__GS_ #1 _seq} \l_tmpa_seq
  }
\__GS_new_seq_c:cn {AB0} {AAAAAA}
\__GS_new_seq_c:cn {AB1} {AABABB}
\__GS_new_seq_c:cn {AB2} {AABBAB}
\__GS_new_seq_c:cn {AB3} {AABBBA}
\__GS_new_seq_c:cn {AB4} {ABAABB}
\__GS_new_seq_c:cn {AB5} {ABBAAB}
\__GS_new_seq_c:cn {AB6} {ABBBAA}
\__GS_new_seq_c:cn {AB7} {ABABAB}
\__GS_new_seq_c:cn {AB8} {ABABBA}
\__GS_new_seq_c:cn {AB9} {ABBABA}
\__GS_new_seq_c:cn {A0} {3211} % start white (C0 same but start with black)
\__GS_new_seq_c:cn {A1} {2221}
\__GS_new_seq_c:cn {A2} {2122}
\__GS_new_seq_c:cn {A3} {1411}
\__GS_new_seq_c:cn {A4} {1132}
\__GS_new_seq_c:cn {A5} {1231}
\__GS_new_seq_c:cn {A6} {1114}
\__GS_new_seq_c:cn {A7} {1312}
\__GS_new_seq_c:cn {A8} {1213}
\__GS_new_seq_c:cn {A9} {3112}
\__GS_new_seq_c:cn {B0} {1123} % start white
\__GS_new_seq_c:cn {B1} {1222}
\__GS_new_seq_c:cn {B2} {2212}
\__GS_new_seq_c:cn {B3} {1141}
\__GS_new_seq_c:cn {B4} {2311}
\__GS_new_seq_c:cn {B5} {1321}
\__GS_new_seq_c:cn {B6} {4111}
\__GS_new_seq_c:cn {B7} {2131}
\__GS_new_seq_c:cn {B8} {3121}
\__GS_new_seq_c:cn {B9} {2113}
\__GS_new_seq_c:cn {margin}         {111} % start black
\__GS_new_seq_c:cn {separator}    {11111} % start white
\__GS_new_seq_c:cn {special}     {111111} % start white
\__GS_new_seq_c:cn {extra_margin}   {112} % start black
\__GS_new_seq_c:cn {extra_separator} {11} % start white
\dim_const:Nn \c__GS_module_min_width_dim {0.264mm}
\dim_const:Nn \c__GS_module_norm_width_dim {0.33mm}
\cs_new_nopar:Npn \__GS_set_key_code:nn #1#2
  {
    \tl_if_exist:NF \l_GS_code_type_tl { \tl_new:N \l_GS_code_type_tl }
    \tl_set:Nn \l_GS_code_type_tl { #1 }
    \int_if_exist:NF \l_GS_code_size_int { \int_new:N \l_GS_code_size_int }
    \int_set:Nn \l_GS_code_size_int { #2 }
  }
\keys_define:nn { GS1 }
  {
    ocrb          .bool_set:N = \l_GS_use_ocrb_bool,
    ocrb          .initial:n  = true,
    module_width  .dim_set:N  = \l_GS_module_wd_dim,
    module_width  .initial:V  = \c__GS_module_norm_width_dim,
    module_height .dim_set:N  = \l_GS_module_ht_dim,
    module_height .initial:V  = \c_zero_dim,
    code          .choice:,
    code / EAN-8  .code:n = { \__GS_set_key_code:nn { EAN } {  8 } },
    code / EAN8   .code:n = { \__GS_set_key_code:nn { EAN } {  8 } },
    code / EAN-13 .code:n = { \__GS_set_key_code:nn { EAN } { 13 } },
    code / EAN13  .code:n = { \__GS_set_key_code:nn { EAN } { 13 } },
    code          .initial:n = EAN-13,
    scale         .fp_set:N   = \l_GS_scale_fp,
    scale         .initial:n  = 1.0,
    scale_to_font .bool_set:N = \l_GS_scale_to_font_bool,
    scale_to_font .initial:n  = false,
    add_control   .bool_set:N = \l_GS_add_control_bool,
    add_control   .initial:n  = false,
  }
\seq_new:N \l__GS_code_seq
\msg_new:nnnn { GS1 } { EAN-code-size }
  {
    #1~isn't~a~valid~EAN~code~\msg_line_context:.
  }
  {
    The~given~code~is~neither~a~EAN-8~with~or~without~control~digit,\\
    nor~a~EAN-13~with~or~without~control~digit.\\\\
    The~GS1~module~currently~only~supports~EAN-8~and~EAN~13.
  }
\msg_new:nnn { GS1 } { module/minwidth }
  {
    Resulting~module~width~is~less~than~allowed~minimum~\msg_line_context:.\\\\
    GS1~specification~declares~a~minimum~module~width~of~#2.\\
    Currently~the~module~with~would~be~#1.\\
    To~avoid~problems,~I'll~increase~module~width~to~#2.
  }
\cs_new_nopar:Npn \GS_set_code_digit_seq:Nn #1#2
  {
    \seq_set_eq:NN #1 \c_empty_seq
    \tl_map_inline:nn
      { #2 }
      {
        \tl_if_in:nnT
          {0123456789}
          {##1}
          {
            \seq_put_right:Nn #1 { ##1 }
          }
      }
  }
\cs_new_nopar:Npn \GS_cut_EAN_control_digit:N #1
  {
    \int_case:nnF
      { \seq_count:N #1 }
      {
        {  7 } {  }
        {  8 } { \seq_pop_right:NN #1 \l_tempa_tl }
        { 12 } {  }
        { 13 } { \seq_pop_right:NN #1 \l_tempa_tl }
      }
      {
        \msg_error:nnn { GS1 } { EAN-code-size } { #1 }
      }
  }
\cs_new_nopar:Npn \int_set_to_EAN_control_digit:NN #1#2
  {
    \int_zero:N #1
    \seq_set_eq:NN \l_tmpa_seq #2
    \bool_until_do:nn
      { \seq_if_empty_p:N \l_tmpa_seq }
      {
        \seq_pop_left:NN \l_tmpa_seq \l_tmpb_tl
        \int_if_even:nTF
          { \seq_count:N \l_tmpa_seq }
          {
            \int_add:Nn #1 { 3 * \l_tmpb_tl }
          }
          {
            \int_add:Nn #1 { \l_tmpb_tl }
          }
      }
    \int_set:Nn #1 { \int_mod:nn { 10 - \int_mod:nn { #1 } { 10 } } { 10 } }
  }
\cs_new_nopar:Npn \GS_set_EAN_control_digit:N #1
  {
    \GS_cut_EAN_control_digit:N #1
    \int_set_to_EAN_control_digit:NN \l_tmpa_int #1
    \seq_put_right:NV #1 \l_tmpa_int
  }
\cs_new_nopar:Npn \__GS_modules:Nn #1#2
  {
    \seq_map_inline:Nn #1
      {
        \bool_if:NTF \l__GS_black_bool
          {
            \bool_set_false:N \l__GS_black_bool
            \hbox_set:Nn \l_tmpa_box { 0 }
            \rule:nnn
              {
                \dim_eval:n
                  {
                    \box_ht:N \l_tmpa_box + \l_GS_module_wd_dim / 2
                    - \dim_eval:n { #2 } + \l_GS_module_ht_dim
                  }
              }
              { \dim_eval:n { \l_GS_module_wd_dim * ##1 } }
              { \dim_eval:n { #2 } }
          }
          {
            \bool_set_true:N \l__GS_black_bool
            \hbox_to_wd:nn { \l_GS_module_wd_dim * ##1 } { }
          }
      }
  }

\cs_new_nopar:Npn \__GS_modules:NnN #1#2#3
  {
    \bool_if_exist:NF \l__GS_black_bool { \bool_new:N \l__GS_black_bool }
    \bool_set_eq:NN \l__GS_black_bool #3
    \__GS_modules:Nn #1 { #2 }
  }

\cs_new_nopar:Npn \__GS_modules_start_black:Nn #1#2
  {
    \__GS_modules:NnN #1 { #2 } \c_true_bool
  }

\cs_new_nopar:Npn \__GS_modules_start_white:Nn #1#2
  {
    \__GS_modules:NnN #1 { #2 } \c_false_bool
  }

\cs_generate_variant:Nn \__GS_modules:Nn { c }
\cs_new_nopar:Npn \GS_use_as_EAN_barcode:N #1
  {
    \seq_set_eq:NN \l__GS_code_seq #1

    \int_compare:nNnTF { \l_GS_code_size_int } { = } { 8 }
      {
        \hbox_to_wd:nn { \l_GS_module_wd_dim * 7 } { }
        \seq_set_eq:Nc \l_GS_system_seq { c__GS_AB0_seq }
      }
      {
        \hbox_to_wd:nn { \l_GS_module_wd_dim * 11 } { }
        \seq_pop_left:NN \l__GS_code_seq \l_tmpa_tl
        \seq_set_eq:Nc \l_GS_system_seq { c__GS_AB \l_tmpa_tl _seq }
        \hbox_overlap_left:n { \l_tmpa_tl }
      }

    \__GS_modules_start_black:Nn \c__GS_margin_seq
      { \l_GS_module_ht_dim + \l_GS_module_wd_dim * 5 }

    \int_step_inline:nnnn { 1 } { 1 }
      { \int_div_truncate:nn { \l_GS_code_size_int } { 2 } }
      {
        \seq_pop_left:NN \l__GS_code_seq \l_tmpa_tl
        \hbox_overlap_right:n { \l_tmpa_tl }
        \seq_pop_left:NN \l_GS_system_seq \l_tmpb_tl
        \__GS_modules:cn { c__GS_ \l_tmpb_tl \l_tmpa_tl _seq }
          { \l_GS_module_ht_dim }
      }

    \__GS_modules_start_white:Nn \c__GS_separator_seq
      { \l_GS_module_ht_dim + \l_GS_module_wd_dim * 5 }

    \int_step_inline:nnnn { 1 } { 1 }
      { \int_div_truncate:nn { \l_GS_code_size_int } { 2 } }
      {
        \seq_pop_left:NN \l__GS_code_seq \l_tmpa_tl
        \hbox_overlap_right:n { \l_tmpa_tl }
        \__GS_modules:cn { c__GS_A \l_tmpa_tl _seq }
          { \l_GS_module_ht_dim }
      }

    \__GS_modules_start_black:Nn \c__GS_margin_seq
      { \l_GS_module_ht_dim + \l_GS_module_wd_dim * 5 }

    \hbox_to_wd:nn { \l_GS_module_wd_dim * 7 } { }
  }
\RequirePackage{xparse}
\NewDocumentCommand \EANControlDigit
  { m }
  {
    \group_begin:
      \GS_set_code_digit_seq:Nn \l__GS_code_seq { #1 }
      \GS_cut_EAN_control_digit:N \l__GS_code_seq
      \int_set_to_EAN_control_digit:NN \l_tmpa_int \l__GS_code_seq
      \int_to_arabic:n { \l_tmpa_int }
    \group_end:
  }
\NewDocumentCommand \EANBarcode
  { o m }
  {
    \group_begin:
      \IfNoValueF{#1}{ \keys_set:nn { GS1 } { #1 } }
      \dim_compare:nNnT { \l_GS_module_ht_dim } { = } { \c_zero_dim }
        {
          \int_compare:nNnTF { \l_GS_code_size_int } { = } { 8 }
            { \dim_set:Nn \l_GS_module_ht_dim { 21.31 mm } }
            { \dim_set:Nn \l_GS_module_ht_dim { 25.01 mm } }
        }
      \bool_if:nT \l_GS_use_ocrb_bool
        {
          \usefont{OT1}{ocrb}{m}{n}\fontsize{9}{9}\selectfont
        }

      \GS_set_code_digit_seq:Nn \l__GS_code_seq { #2 }

      \bool_if:NT \l_GS_add_control_bool
        {
          \GS_set_EAN_control_digit:N \l__GS_code_seq
        }

      \int_compare:nNnT
        { \seq_count:N \l__GS_code_seq }
        { > }
        { \l_GS_code_size_int }
        {
          \msg_error:nnn { GS1 } { EAN-code-size } { #2 }
        }
      \int_while_do:nNnn
        { \seq_count:N \l__GS_code_seq }
        { < }
        { \l_GS_code_size_int }
        {
          \seq_put_left:Nn \l__GS_code_seq { 0 }
        }

      \bool_if:NT \l_GS_scale_to_font_bool {
        \hbox_set:Nn \l_tmpa_box { 0 }
        \dim_set:Nn \l_GS_module_wd_dim { \box_wd:N \l_tmpa_box / 7 }
      }

     \dim_set:Nn \l_tmpa_dim
       { \fp_to_decimal:N \l_GS_scale_fp \l_GS_module_wd_dim }

     \dim_compare:nNnT
        { \l_tmpa_dim }
        { < }
        { \c__GS_module_min_width_dim }
        {
          \msg_warning:nnxx { GS1 } { module/minwidth }
            { \dim_use:N \l_GS_module_wd_dim }
            { \dim_use:N \c__GS_module_min_width_dim }
          \dim_set:Nn \l_GS_module_wd_dim
          {
            \c__GS_module_min_width_dim *
            100 / \fp_to_int:n { 100 * \l_GS_scale_fp }
          }
        }

      \hbox_set:Nn \l_tmpa_box { \GS_use_as_EAN_barcode:N \l__GS_code_seq }
      \box_scale:Nnn \l_tmpa_box
        { \fp_to_int:n { 100 * \l_GS_scale_fp } / 100 }
        { \fp_to_int:n { 100 * \l_GS_scale_fp } / 100 }
      \box_use:N \l_tmpa_box
    \group_end:
  }
\NewDocumentCommand \GSSetup
  { m }
  { \keys_set:nn { GS1 } { #1 } }
%% 
%%
%% End of file `GS1.sty'.
