%
% Copyright (c) 2024 Zeping Lee
% Released under the LaTeX Project Public License v1.3c License.
% Repository: https://gitee.com/xkwxdyy/exam-zh
%

\NeedsTeXFormat{LaTeX2e}

\RequirePackage{expl3}
\RequirePackage{xparse}

\ProvidesExplPackage {exam-zh-question} {2024-02-15} {v0.2.1}
  {exam-zh question module}


\RequirePackage { amsthm }
\@ifpackageloaded { tcolorbox }
  { \tcbuselibrary { breakable } }
  { \RequirePackage [ most ] { tcolorbox } }
% \RequirePackage { zref-savepos }
\RequirePackage { xeCJKfntef }
\RequirePackage { enumitem }

% https://github.com/CTeX-org/forum/issues/264#issuecomment-1200087776
\disable@package@load { etex }
  {
    \cs_set_eq:NN \globcount \newcount
    \cs_set_eq:NN \globdimen \newdimen
  }
\RequirePackage { linegoal }


\ExplSyntaxOff

\usetikzlibrary{shapes.misc}
\ExplSyntaxOn




\NewDocumentCommand \questionsetup { m }
  { \keys_set:nn { exam-zh / question } { #1 } }
\NewDocumentCommand \fillinsetup { m }
  { \keys_set:nn { exam-zh / fillin } { #1 } }

% ulem ������������������ \emph��������� \normalem ������
\normalem

\setlist{nosep}
\setlist
  {
    labelsep    = 4pt,
  }

% question ������������������

% ���������
\int_new:N \g__examzh_question_index_int
% ������������
\tl_new:N \l__examzh_question_answer_color_tl
% ������������
\int_new:N \l__examzh_question_points_int
% ������������������������
\bool_new:N \l__examzh_question_show_points_bool
\bool_new:N \l__examzh_question_show_points_auto_bool
% ������������������������������������������������������������
\bool_new:N \l__examzh_question_points_separate_par_bool
% ������������������
\bool_new:N \l__examzh_question_show_paren_bool
% ������������������
\bool_new:N \l__examzh_question_show_answer_bool
\bool_new:N \l__examzh_question_show_fillin_answer_bool
\bool_new:N \l__examzh_question_show_paren_answer_bool
% ���������������
\skip_new:N \l__examzh_question_top_sep_skip
\skip_new:N \l__examzh_question_bottom_sep_skip
% label ���������
\tl_new:N \l__examzh_question_label_align_tl
\tl_new:N \l__examzh_problem_label_align_tl


\keys_define:nn { exam-zh }
  { question .meta:nn = { exam-zh / question } {#1} }
\keys_define:nn { exam-zh }
  { problem .meta:nn = { exam-zh / problem } {#1} }


\keys_define:nn { exam-zh / question }
  {
    % ������������ question ������������������
    index               .int_gset:N = \g__examzh_question_index_int ,
    % ������
    points              .int_set:N = \l__examzh_question_points_int ,
    % ������������������
    show-points         .choice: ,
    show-points / auto  .code:n =
      { \bool_set_true:N \l__examzh_question_show_points_auto_bool } ,
    show-points / true  .code:n =
      {
        \bool_set_true:N  \l__examzh_question_show_points_bool
        \bool_set_false:N \l__examzh_question_show_points_auto_bool
      } ,
    show-points / false .code:n =
      {
        \bool_set_false:N \l__examzh_question_show_points_bool
        \bool_set_false:N \l__examzh_question_show_points_auto_bool
      } ,
    % ������������������������
    points-separate-par .bool_set:N = \l__examzh_question_points_separate_par_bool ,
    % ������������������
    % show-answer         .bool_set:N = \l__examzh_question_show_answer_bool ,
    show-answer         .choice: ,
    show-answer / true .code:n = 
      {
        \bool_set_true:N \l__examzh_question_show_fillin_answer_bool
        \bool_set_true:N \l__examzh_question_show_paren_answer_bool
      },
    show-answer / false .code:n = 
      {
        \bool_set_false:N \l__examzh_question_show_fillin_answer_bool
        \bool_set_false:N \l__examzh_question_show_paren_answer_bool
      },
    % ������������
    top-sep             .skip_set:N = \l__examzh_question_top_sep_skip ,
    % ������������
    bottom-sep          .skip_set:N = \l__examzh_question_bottom_sep_skip ,
    label .tl_set:N = \l__examzh_question_label_tl,
    combine-fillin .bool_set:N = \l__examzh_question_combine_fillin_bool,
    combine-fillin-args .tl_set:N = \l__examzh_question_combine_fillin_args_tl,
    label-align .choices:nn =
      { left, center, right }
      { \tl_set_eq:NN \l__examzh_question_label_align_tl \l_keys_choice_tl },
    hang .bool_set:N = \l__examzh_question_hang_bool,
    points-prelabel .tl_set:N = \l__examzh_question_points_prelabel_tl,
    points-postlabel .tl_set:N = \l__examzh_question_points_postlabel_tl,
  }


\keys_set:nn { exam-zh / question }
  {
    index               = 1,
    points              = 0 ,
    show-points         = auto ,
    points-separate-par = false ,
    show-answer         = false ,
    top-sep             = .25em plus .25em minus .1em ,
    bottom-sep          = .25em plus .25em minus .1em ,
    label               = \arabic*.,
    combine-fillin      = false,
    label-align         = right,
    hang                = true,
    points-prelabel     = {���},
    points-postlabel    = {������}
  }

\keys_define:nn { exam-zh / problem }
  {
    % ������������ question ������������������
    index               .int_gset:N = \g__examzh_question_index_int ,
    % ������
    points              .int_set:N = \l__examzh_question_points_int ,
    % ������������������
    show-points         .choice: ,
    show-points / auto  .code:n =
      { \bool_set_true:N \l__examzh_question_show_points_auto_bool } ,
    show-points / true  .code:n =
      {
        \bool_set_true:N  \l__examzh_question_show_points_bool
        \bool_set_false:N \l__examzh_question_show_points_auto_bool
      } ,
    show-points / false .code:n =
      {
        \bool_set_false:N \l__examzh_question_show_points_bool
        \bool_set_false:N \l__examzh_question_show_points_auto_bool
      } ,
    % ������������������������
    points-separate-par .bool_set:N = \l__examzh_problem_points_separate_par_bool,
    % ������������������
    % show-answer         .bool_set:N = \l__examzh_question_show_answer_bool ,
    show-answer         .choice: ,
    show-answer / true .code:n = 
      {
        \bool_set_true:N \l__examzh_question_show_fillin_answer_bool
        \bool_set_true:N \l__examzh_question_show_paren_answer_bool
      },
    show-answer / false .code:n = 
      {
        \bool_set_false:N \l__examzh_question_show_fillin_answer_bool
        \bool_set_false:N \l__examzh_question_show_paren_answer_bool
      },
    % ������������
    top-sep             .skip_set:N = \l__examzh_problem_top_sep_skip ,
    % ������������
    bottom-sep          .skip_set:N = \l__examzh_problem_bottom_sep_skip ,
    label .tl_set:N = \l__examzh_question_label_tl,
    label-align .choices:nn =
      { left, center, right }
      { \tl_set_eq:NN \l__examzh_problem_label_align_tl \l_keys_choice_tl },
    points-prelabel .tl_set:N = \l__examzh_problem_points_prelabel_tl,
    points-postlabel .tl_set:N = \l__examzh_problem_points_postlabel_tl,
  }

\keys_set:nn { exam-zh / problem }
  {
    index               = 1,
    points              = 0 ,
    show-points         = auto ,
    points-separate-par = false ,
    show-answer         = false ,
    top-sep             = .25em plus .25em minus .1em ,
    bottom-sep          = .25em plus .25em minus .1em ,
    label               = \arabic*.,
    label-align         = right,
    points-prelabel     = {���},
    points-postlabel    = {������}
  }

% ������������������������������������
\bool_new:N \l__examzh_question_problem_style_bool


% ������������������������������
\NewDocumentEnvironment { question } { O { } +b }
  {
    % \bool_set_false:N \l__examzh_question_problem_style_bool
    \group_begin:
      \__examzh_question_begin:nn {#1}{#2}
      \__examzh_question_end:nn {#1}{#2} 
    \group_end:
  }
  {}
% \NewDocumentEnvironment { question } { O { } }
%   {
%     % \bool_set_false:N \l__examzh_question_problem_style_bool
%     \group_begin:
%       \group_begin:
%         \tcbverbatimwrite { \jobname-tempfile.exam }
%   }
%   {
%         \endtcbverbatimwrite
%       \group_end:
%       \__examzh_question_begin:nn {#1} { \input{\jobname-tempfile.exam} }
%       \__examzh_question_end:nn {#1} { \input{\jobname-tempfile.exam} } 
%     \group_end:
%   }

% ���������
\NewDocumentEnvironment { problem } { O { } +b }
  {
    % \bool_set_true:N \l__examzh_question_problem_style_bool
    \group_begin:
      \__examzh_problem_begin:nn {#1}{#2}
      \__examzh_problem_end:nn {#1}{#2} 
    \group_end:
  }
  { }

\prg_generate_conditional_variant:Nnn \int_compare:nNn { xNn } { T }

% question ��� problem ��������� list ������������
\cs_new:Npn \__examzh_enumerate_set_question:
  {
    \setlist[enumerate, 1]
      {
        labelindent = \parindent,
        labelsep    = 2pt,
        leftmargin  = *,
        % label       = { \arabic * .}
        label       = {��� \arabic * ���},
      }

    \setlist[enumerate, 2]
      {
        % labelindent = *,
        leftmargin  = 2em, 
        widest      = 0,
        itemindent  = 0em,
        labelsep    = 0pt,
        % labelwidth  = 2em,
        listparindent = \parindent,
        label       = {��� \alph * ���},
      }
    \setlist[enumerate, 3]
      {
        % labelindent = *,
        leftmargin  = 2em, 
        widest      = 0,
        itemindent  = 0em,
        labelsep    = 0pt,
        % labelwidth  = 2em,
        listparindent = \parindent,
        label       = {��� \roman * ���},
      }
  }
\cs_new:Npn \__examzh_enumerate_set_problem:
  {
    \setlist[enumerate, 1]
      {
        labelindent = 2em,
        labelsep    = 4pt,
        leftmargin  = *,
        % label       = { \arabic * .}
        label       = {��� \arabic * ���},
      }

    \setlist[enumerate, 2]
      {
        % labelindent = *,
        leftmargin  = 2em, 
        widest      = 0,
        itemindent  = 0em,
        labelsep    = 0pt,
        % labelwidth  = 2em,
        listparindent = \parindent,
        label       = {��� \alph * ���},
      }
    \setlist[enumerate, 3]
      {
        % labelindent = *,
        leftmargin  = 2em, 
        widest      = 0,
        itemindent  = 0em,
        labelsep    = 0pt,
        % labelwidth  = 2em,
        listparindent = \parindent,
        label       = {��� \roman * ���},
      }
  }

\cs_new:Npn \__examzh_question_begin:nn #1#2
  {
    \par
    % \bool_if:NTF \l__examzh_question_combine_fillin_bool
    %   { \keys_set:nn { exam-zh / question } { label-align = left } }
    %   { \keys_set:nn { exam-zh / question } { label-align = right } }
    % ������������
    \keys_set:nn { exam-zh / question } { #1 }
    % ���������������������������
    \int_gincr:N \g__examzh_question_index_int
    % ������������������
    % \addvspace { \l__examzh_question_top_sep_skip }
    \vspace { \l__examzh_question_top_sep_skip }
    % ������ enumerate ������������
    \__examzh_enumerate_set_question:
    % ���������������������������
    \int_set:Nn \clubpenalty { 10000 }
    \int_set:Nn \widowpenalty { 10000 }
    % ���������������������������������
    \int_set:Nn \interlinepenalty { 301 }
    % ������������������ source2e ��� enumerate ���������������
    % \@enumdepth ������������ enumerate ���������������������
    % ��������������������� question ��������� enumerate ��������� level 2 ���������
    % ��������� question ������ enumerate ������������������������������

    % \int_incr:N \@enumdepth

    % ������ show-points = auto ������������������������������������������������������������������
    % ������������������������������������������������������������������������������������������������������������
    % ������������������������
    \bool_if:NT \l__examzh_question_show_points_auto_bool
      {
        \bool_set_false:N \l__examzh_question_show_points_bool
      }
    % ������������������������
    \list 
      {
        % \int_use:N \g__examzh_question_index_int . 
        \bool_if:NTF \l__examzh_question_combine_fillin_bool
          {
            % \framebox[6em]
            % \fbox
              % {
            \__examzh_question_make_label:n
              {
                  \tl_if_blank:VTF \l__examzh_question_combine_fillin_args_tl
                  { \fillin }
                  {
                    \use:x
                      {
                        \exp_not:N \fillin \l__examzh_question_combine_fillin_args_tl
                      }
                  }
                  \__examzh_question_the_label:
              }
              % }
          }
          {
            \__examzh_question_make_label:n
              {
                \__examzh_question_the_label:
              }
          }
        % \__examzh_question_make_label:n
        %   {
        %     \bool_if:NT \l__examzh_question_combine_fillin_bool
        %       {
        %         % \makebox
        %           % {
        %             \tl_if_blank:VTF \l__examzh_question_combine_fillin_args_tl
        %               { \fillin }
        %               {
        %                 \use:x
        %                   {
        %                     \exp_not:N \fillin \l__examzh_question_combine_fillin_args_tl
        %                   }
        %               }
        %           % }
        %       }
        %     \int_compare:xNnT { \g__examzh_question_index_int } < { 11 }
        %       { \phantom {1} }
        %     \__examzh_question_the_label:
        %   }
      }
      {
        % ��� group ��������������� combine-fillin ��� type ������������������������ fillin ���type
        \group_begin:
          \dim_gset:Nn \topsep       { 0pt }
          \dim_gset:Nn \partopsep    { 0pt }
          \dim_gset:Nn \itemsep      { 0em }
          \dim_gset:Nn \parsep       { 0pt }
          % \group_begin:
            % ������ \fillin ������������������������������������������������ question ������������������ type ��������������������� \l__examzh_fillin_type_str ������
            % ������������ \l__examzh_question_combine_fillin_args_tl ��������� type ���������������
            \__examzh_question_begin_fillin_type_set:
            \__examzh_question_begin_labelsep_labelwidth_set:
          % \group_end:
          % \bool_if:NTF \l__examzh_question_problem_style_bool
          %   {
          %     % ������������������ + ������ 2em ���������
          %     \bool_if:NTF \l__examzh_question_combine_fillin_bool
          %       {
          %         % ������ combine ������������ question ���������������
          %         \bool_if:NTF \l__examzh_question_hang_bool
          %           { \dim_gset:Nn \itemindent { 0em } }
          %           { \dim_gset:Nn \itemindent { 2em } }
          %         \bool_if:NTF \l__examzh_question_combine_fillin_bool
          %           {
          %             \bool_if:NTF \l__examzh_question_hang_bool
          %               { \dim_gset:Nn \leftmargin { 6em } }
          %               { \dim_gset:Nn \leftmargin { 4em } }
          %           }
          %           {
          %             \bool_if:NTF \l__examzh_question_hang_bool
          %               { \dim_gset:Nn \leftmargin { 2em } }
          %               { \dim_gset:Nn \leftmargin { 0em } }
          %           }
          %       }
          %       {
          %         \dim_gset:Nn \leftmargin { 0pt }
          %         \dim_gset:Nn \itemindent { 2em }
          %       }
          %   }
            % {
              % ���������������������������������
              \bool_if:NTF \l__examzh_question_hang_bool
                { \dim_gset:Nn \itemindent { 0em } }
                { \dim_gset:Nn \itemindent { 2em } }
              \bool_if:NTF \l__examzh_question_combine_fillin_bool
                {
                  \bool_if:NTF \l__examzh_question_hang_bool
                    { \dim_gset:Nn \leftmargin { 6em } }
                    { \dim_gset:Nn \leftmargin { 4em } }
                }
                {
                  \bool_if:NTF \l__examzh_question_hang_bool
                    { \dim_gset:Nn \leftmargin { 2em } }
                    { \dim_gset:Nn \leftmargin { 0em } }
                }
            % }
          \dim_gset_eq:NN \listparindent \itemindent
        \group_end:
      }
    \item \relax
    % ������������������
    \bool_if:NT \l__examzh_question_show_points_bool
      {
        % ������������������������ show-points ��� bool ��� true ���������������
        \int_compare:nNnT { \l__examzh_question_points_int } > { 0 }
          { \l__examzh_question_points_prelabel_tl \int_use:N \l__examzh_question_points_int ~ \l__examzh_question_points_postlabel_tl }
        % ���������������������������������������
        \bool_if:NT \l__examzh_question_points_separate_par_bool
          % \par ������������������ \nopagebreak ������������������������������������������������������������
          { \par \nopagebreak }   
      }
  }
\int_new:N \l__examzh_question_begin_fillin_args_bracket_num_int
\cs_generate_variant:Nn \regex_count:nnN { nVN }
\prg_generate_conditional_variant:Nnn \regex_extract_once:nnN { nxN } { F } 
\cs_new:Npn \__examzh_question_begin_fillin_type_set:
  {
    \regex_count:nVN { \[ } % \]
      \l__examzh_question_combine_fillin_args_tl
      \l__examzh_question_begin_fillin_args_bracket_num_int
    \use_none:n { \] }  % ������ \[ ���������������
    % \int_use:N \l__examzh_question_begin_fillin_args_bracket_num_int
    \int_compare:nNnT { \l__examzh_question_begin_fillin_args_bracket_num_int } = {2}
      {
        \regex_extract_once:nxNF { \[ (.*?) \] } { \l__examzh_question_combine_fillin_args_tl } \l_tmpa_seq { \fail }
        \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
        % \seq_use:Nn \l_tmpa_seq {,}
        \keys_set:nx { exam-zh / fillin } { \seq_use:Nn \l_tmpa_seq {,} }
      }
  }
\cs_new:Npn \__examzh_problem_begin_labelsep_labelwidth_set:
  {
    \str_case:Vn \l__examzh_problem_label_align_tl 
      {
        { left } 
          { 
            \dim_gset:Nn \labelsep { .7em }
            \dim_gset:Nn \labelwidth { 1.8em } 
          }
        { center }
          {
            \dim_gset:Nn \labelsep { .7em } 
            \dim_gset:Nn \labelwidth { 1.3em } 
          }
        { right  } 
          {
            \dim_gset:Nn \labelsep { .7em } 
            \dim_gset:Nn \labelwidth { 1.3em } 
          }
      }
  }
\cs_new:Npn \__examzh_question_begin_labelsep_labelwidth_set:
  {
    \bool_if:NTF \l__examzh_question_combine_fillin_bool
      { 
        \str_case:Vn \l__examzh_question_label_align_tl
          {
            { left } 
              { 
                \str_case:VnF \l__examzh_fillin_type_str
                  {
                    { paren } 
                      { 
                        % combin-left-paren
                        \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
                          {
                            % ������������
                            \dim_gset:Nn \labelsep { 2.6em } 
                            \dim_gset:Nn \labelwidth { 3.8em } 
                          }
                          {
                            % ���������������
                            \dim_gset:Nn \labelsep { 4.5em } 
                            \dim_gset:Nn \labelwidth { 3.8em } 
                          }
                      }
                    { line }
                      { 
                        % combin-left-line
                        \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
                          {
                            % ������������
                            \dim_gset:Nn \labelsep { 2.2em } 
                            \dim_gset:Nn \labelwidth { 3.8em } 
                          }
                          {
                            % ���������������
                            \dim_gset:Nn \labelsep { 3.8em } 
                            \dim_gset:Nn \labelwidth { 3.8em } 
                          } 
                      }
                  }
                  { 
                    % combin-left-paren/line ������
                    \dim_gset:Nn \labelsep { 2.8em }
                    \dim_gset:Nn \labelwidth { 1.3em }
                  }
              }
            { center }
              { 
                \str_case:VnF \l__examzh_fillin_type_str
                  {
                    { paren } 
                      { 
                        % combin-center-paren
                        \dim_gset:Nn \labelsep { 2em } 
                        \dim_gset:Nn \labelwidth { 5em } 
                      }
                    { line }
                      { 
                        % combin-center-line
                        \dim_gset:Nn \labelsep { 2.8em } 
                        \dim_gset:Nn \labelwidth { 4em } 
                      }
                  }
                  { 
                    % combin-center-paren/line ������
                    \dim_gset:Nn \labelsep { 2em }
                    \dim_gset:Nn \labelwidth { 1.3em }
                  }
              }
            { right  }
              {
                \str_case:VnF \l__examzh_fillin_type_str
                  {
                    { paren } 
                      { 
                        % combin-left-paren
                        \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
                          {
                            % ������������
                            \dim_gset:Nn \labelsep { 3.8em } 
                            \dim_gset:Nn \labelwidth { 2em } 
                          }
                          {
                            % ���������������
                            \dim_gset:Nn \labelsep { 5.8em } 
                            \dim_gset:Nn \labelwidth { 2em } 
                          }
                      }
                    { line }
                      { 
                        % combin-left-line
                        \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
                          {
                            % ������������
                            \dim_gset:Nn \labelsep { 0.7em } 
                            \dim_gset:Nn \labelwidth { 1.3em } 
                          }
                          {
                            % ���������������
                            \dim_gset:Nn \labelsep { 5.2em } 
                            \dim_gset:Nn \labelwidth { 2em } 
                          }
                      }
                  }
                  { 
                    % combin-left-paren/line ������
                    \dim_gset:Nn \labelsep { 2.8em } 
                    \dim_gset:Nn \labelwidth { 1.3em }
                  }
              }
          }
      }
      {
        \str_case:Vn \l__examzh_question_label_align_tl
          {
            { left } 
              { 
                \dim_gset:Nn \labelsep { .7em }
                \dim_gset:Nn \labelwidth { 1.8em } 
              }
            { center }
              {
                \dim_gset:Nn \labelsep { .7em } 
                \dim_gset:Nn \labelwidth { 1.3em } 
              }
            { right  } 
              {
                \dim_gset:Nn \labelsep { .7em } 
                \dim_gset:Nn \labelwidth { 1.3em } 
              }
          }
        
      }
  }

\cs_new:Npn \__examzh_question_end:nn #1#2
  {
    #2
    % ������������������
    \endlist
    % ������������������
    % \addvspace { \l__examzh_question_bottom_sep_skip }
    \vspace { \l__examzh_question_bottom_sep_skip }
  }

\cs_new:Npn \__examzh_problem_begin:nn #1#2
  {
    \par
    % \bool_if:NTF \l__examzh_question_combine_fillin_bool
    %   { \keys_set:nn { exam-zh / question } { label-align = left } }
    %   { \keys_set:nn { exam-zh / question } { label-align = right } }
    % ������������
    \keys_set:nn { exam-zh / problem } { #1 }
    % ���������������������������
    \int_gincr:N \g__examzh_question_index_int
    % ������������������
    % \addvspace { \l__examzh_question_top_sep_skip }
    \vspace { \l__examzh_problem_top_sep_skip }
    % ������ enumerate ������������
    \__examzh_enumerate_set_problem:
    % ���������������������������
    \int_set:Nn \clubpenalty { 10000 }
    \int_set:Nn \widowpenalty { 10000 }
    % ���������������������������������
    \int_set:Nn \interlinepenalty { 301 }
    % ������������������ source2e ��� enumerate ���������������
    % \@enumdepth ������������ enumerate ���������������������
    % ��������������������� problem ��������� enumerate ��������� level 2 ���������
    % ��������� question ������ enumerate ������������������������������
    % \int_incr:N \@enumdepth
    % ������ show-points = auto ������������������������������������������������������������������
    % ������������������������������������������������������������������������������������������������������������
    % ������������������������
    \bool_if:NT \l__examzh_question_show_points_auto_bool
      {
        \bool_set_true:N  \l__examzh_question_show_points_bool
      }
    % ������������������������
    \list 
      {
        % \int_use:N \g__examzh_question_index_int . 
        \__examzh_problem_make_label:n
          {
            \__examzh_question_the_label:
          }
      }
      {
        % ��� group ��������������� combine-fillin ��� type ������������������������ fillin ���type
        \group_begin:
          \dim_gset:Nn \topsep       { 0pt }
          \dim_gset:Nn \partopsep    { 0pt }
          \dim_gset:Nn \itemsep      { 0pt }
          \dim_gset:Nn \parsep       { 0pt }
          \__examzh_problem_begin_labelsep_labelwidth_set:
          \dim_gset:Nn \leftmargin { 0pt }
          \dim_gset:Nn \itemindent { 2em }
          \dim_gset_eq:NN \listparindent \itemindent
        \group_end:
      }
    \item \relax
    % ������������������
    \bool_if:NT \l__examzh_question_show_points_bool
      {
        % ������������������������ show-points ��� bool ��� true ���������������
        \int_compare:nNnT { \l__examzh_question_points_int } > { 0 }
          { \l__examzh_problem_points_prelabel_tl \int_use:N \l__examzh_question_points_int ~ \l__examzh_problem_points_postlabel_tl }
        % ���������������������������������������
        \bool_if:NT \l__examzh_problem_points_separate_par_bool
          % \par ������������������ \nopagebreak ������������������������������������������������������������
          { \par \nopagebreak }   
      }
  }

\cs_new:Npn \__examzh_problem_end:nn #1#2
  {
    #2
    % ������������������
    \endlist
    % ������������������
    % \addvspace { \l__examzh_question_bottom_sep_skip }
    \vspace { \l__examzh_problem_bottom_sep_skip }
  }

% ������ question / problem ��� label
\tl_new:N \l__examzh_question_counters_commands_set_tl

\cs_new:Npn \__examzh_question_the_label:
  {
    \group_begin:
      % ������������������������������������
      \l__examzh_question_counters_commands_set_tl
      % ������������������ label
      \l__examzh_question_label_tl
    \group_end:
  }
\cs_new:Npn \__examzh_question_make_label:n #1
  {
    \str_case:Vn \l__examzh_question_label_align_tl
      {
        { left   } { \rlap { #1 } \hss }
        { center } { \hss \clap { #1 } \hss }
        { right  } { \hss \llap { #1 } }
      }
  }
\cs_new:Npn \__examzh_problem_make_label:n #1
  {
    \str_case:Vn \l__examzh_problem_label_align_tl
      {
        { left   } { \rlap { #1 } \hss }
        { center } { \hss \clap { #1 } \hss }
        { right  } { \hss \llap { #1 } }
      }
  }
\NewDocumentCommand \AddQuestionCounter { m m }
  {
    % ���������������������
    \tl_put_right:Nn \l__examzh_question_counters_commands_set_tl
      { \__examzh_process_counter:NNn #1 #2 { question } }
    % ������������������������
    \cs_set_eq:cN { __examzh_question_save_ \cs_to_str:N #1 : } #2
    \cs_set_eq:cN { __examzh_question_save_ \cs_to_str:N #2 : } #2
  }

\AddQuestionCounter \arabic \@arabic
\AddQuestionCounter \alph   \@alph
\AddQuestionCounter \Alph   \@Alph
\AddQuestionCounter \roman  \@roman
\AddQuestionCounter \Roman  \@Roman

\cs_new:Npn \__examzh_process_counter:NNn #1#2#3
  % #1: \Alph
  % #2: \@Alph
  % #3: question / fillin
  {
    \cs_set:Npn #1 { \use:c { __examzh_ #3 _process_counter_aux:Nn } #2 }
    \cs_set:Npn #2 { \use:c { __examzh_ #3 _process_counter_aux:Nn } #2 }
  }

\cs_new:Npn \__examzh_question_process_counter_aux:Nn #1#2
  {
    \tl_if_eq:nnTF {#2} { * }
      {
        % \Alph*
        \use:c { __examzh_question_save_ \cs_to_str:N #1 : }
          { \int_eval:n { \g__examzh_question_index_int - 1 } }
      }
      {
        % \Alph{...}
        \use:c { __examzh_question_save_ \cs_to_str:N #1 : }
          {#2}
      }
  }


% ������ fillin/no-answer-type = counter ��� label
\tl_new:N \l__examzh_fillin_counters_commands_set_tl


\cs_new:Npn \__examzh_fillin_the_label:
  {
    \group_begin:
      % ������������������������������������
      \l__examzh_fillin_counters_commands_set_tl
      % ������������������ label
      \l__examzh_fillin_label_tl
    \group_end:
  }

\NewDocumentCommand \AddFillinCounter { m m }
  {
    % ���������������������
    \tl_put_right:Nn \l__examzh_fillin_counters_commands_set_tl
      { \__examzh_process_counter:NNn #1 #2 { fillin } }
    % ������������������������
    \cs_set_eq:cN { __examzh_fillin_save_ \cs_to_str:N #1 : } #2
    \cs_set_eq:cN { __examzh_fillin_save_ \cs_to_str:N #2 : } #2
  }

\AddFillinCounter \arabic \@arabic
\AddFillinCounter \alph   \@alph
\AddFillinCounter \Alph   \@Alph
\AddFillinCounter \roman  \@roman
\AddFillinCounter \Roman  \@Roman

\cs_new:Npn \__examzh_fillin_process_counter_aux:Nn #1#2
  {
    \tl_if_eq:nnTF {#2} { * }
      {
        % \Alph*
        \use:c { __examzh_fillin_save_ \cs_to_str:N #1 : }
          { \int_eval:n { \g__examzh_fillin_no_answer_counter_int - 1 } }
      }
      {
        % \Alph{...}
        \use:c { __examzh_fillin_save_ \cs_to_str:N #1 : }
          {#2}
      }
  }

% ������������������������������ unicode ������������
% \circlednumber ������������������������ LaTeX2e ��� <counter>������������������������ <intexpr>���
\NewDocumentCommand \circlednumber { s m }
  {
    \int_if_exist:cTF { c@ #2 }
      { \int_set_eq:Nc \l_tmpa_int { c@#2 } }
      { \int_set:Nn \l_tmpa_int { #2 } }
    \IfBooleanTF {#1}
      {
        \exp_args:Nx \__examzh_tikz_circled_number:n { \int_use:N \l_tmpa_int }
      }
      {
        \exp_args:Nx \__examzh_question_circled_number:n { \int_use:N \l_tmpa_int }
      }
  }

\cs_new:Npn \__examzh_circled_number:nn #1#2
  {
    \int_set:Nn \l_tmpa_int {#1}
    \int_compare:nNnTF { \l_tmpa_int } = { 0 }
      { \int_set:Nn \l_tmpa_int { "24EA } }
      {
        \int_compare:nNnTF { \l_tmpa_int } < { 21 }
          { \int_add:Nn \l_tmpa_int { "245F } }
          {
            \int_compare:nNnTF { \l_tmpa_int } < { 36 }
              { \int_add:Nn \l_tmpa_int { "3250 } }
              {
                \int_compare:nNnTF { \l_tmpa_int } < { 51 }
                  { \int_add:Nn \l_tmpa_int { "32B0 } }
                  {
                    \msg_error:nnn { exam-zh / #2 }
                      { invalid-circled-number } { \int_use:N \l_tmpa_int }
                  }
              }
          }
      }
    \group_begin:
      \CJKfamily+ { }
      \symbol { \l_tmpa_int }
    \group_end:
  }

\msg_new:nnn { exam-zh / question } { invalid-circled-number }
{ Invalid~ circled~ number~ #1. }

\msg_new:nnn { exam-zh / fillin } { invalid-circled-number }
{ Invalid~ circled~ number~ #1. }

\cs_new:Npn \__examzh_question_circled_number:n #1
  { \__examzh_circled_number:nn {#1} { question } }
\cs_new:Npn \__examzh_fillin_circled_number:n #1
  { \__examzh_circled_number:nn {#1} { fillin } }

\AddQuestionCounter \circlednumber \__examzh_question_circled_number:n
\AddFillinCounter \circlednumber  \__examzh_fillin_circled_number:n

% tikz ������������������
\fp_new:N \l__examzh_tikz_circled_number_xscale_fp   % ������������������
\fp_new:N \l__examzh_tikz_circled_number_yscale_fp   % ������������������
\dim_new:N \l__examzh_tikz_circled_number_total_hegiht_dim   % ������������������
\dim_new:N \l__examzh_tikz_circled_number_radius_dim     % ������

\cs_new:Npn \__examzh_tikz_circled_number_aux:n #1
  {
    % ������������������������������������
    \fp_set:Nn \l__examzh_tikz_circled_number_xscale_fp
      {
        \int_compare:nNnTF {#1} < { 10 } 
          { 0.9 }
          {
            \int_compare:nNnTF {#1} < { 100 }
              { 0.7 }
              { 0.5 }
          } 
      }
    \fp_set:Nn \l__examzh_tikz_circled_number_yscale_fp
      {
        \int_compare:nNnTF {#1} < { 10 } 
          { 0.9 }
          {
            \int_compare:nNnTF {#1} < { 100 }
              { 0.8 }
              { 0.6 }
          } 
      }
    % ���������������������
    \hbox_set:Nn \l_tmpa_box {#1}
    \dim_set:Nn \l__examzh_tikz_circled_number_total_hegiht_dim
      { \box_ht:N \l_tmpa_box + \box_dp:N \l_tmpa_box  }
    % ������������������
    \dim_set:Nn \l__examzh_tikz_circled_number_radius_dim 
      { \dim_eval:n { \l__examzh_tikz_circled_number_total_hegiht_dim / 2 + 0.34 ex } }
    % ������
    \tikz [ baseline ]
      {
        \node
          [ inner~sep = 0pt, outer~sep = 0pt ]
          at (0, \dim_use:N \l__examzh_tikz_circled_number_total_hegiht_dim / 2 ) 
          {
            \hbox_set:Nn \l_tmpa_box
              {
                \int_compare:nNnTF {#1} > {9}
                  { \textbf {#1} }
                  {#1}
              }
            \makebox[0.35em][c]
              { 
                % \scalebox { \fp_use:N \l__examzh_tikz_circled_number_xscale_fp } 
                  % [ \fp_use:N \l__examzh_tikz_circled_number_yscale_fp ] 
                \box_scale:Nnn \l_tmpa_box
                  { \fp_use:N \l__examzh_tikz_circled_number_xscale_fp }
                  { \fp_use:N \l__examzh_tikz_circled_number_yscale_fp } 
                \box_use_drop:N \l_tmpa_box
              }
          };
        \draw (0, \l__examzh_tikz_circled_number_total_hegiht_dim / 2 )
          circle ( \l__examzh_tikz_circled_number_radius_dim );
      }
  }
\cs_new:Npn \__examzh_tikz_circled_number:n #1
  {
    \__examzh_tikz_circled_number_aux:n { \int_eval:n {#1} }
  }
\AddQuestionCounter \tikzcirclednumber \__examzh_tikz_circled_number:n
\AddFillinCounter \tikzcirclednumber \__examzh_tikz_circled_number:n

% ���������������
% ���������������������������
\bool_new:N \l__examzh_paren_type_hfill_bool
\keys_define:nn { exam-zh / paren }
  {
    show-answer .bool_set:N = \l__examzh_question_show_paren_answer_bool,
    text-color        .tl_set:N = \l__examzh_paren_text_color_tl ,
    % ������������������������������
    show-paren          .bool_set:N = \l__examzh_question_show_paren_bool ,
    type .choice:,
    type / hfill .code:n =
      {
        \bool_set_true:N \l__examzh_paren_type_hfill_bool
      },
    type / none .code:n =
      {
        \bool_set_false:N \l__examzh_paren_type_hfill_bool
      },
  }
\keys_set:nn { exam-zh / paren }
  {
    show-answer = false,
    text-color  = black,
    show-paren  = true,
    type        = hfill
  }
\keys_define:nn { exam-zh }
  { paren .meta:nn = { exam-zh / paren } {#1} }

% TODO������ paren ������������������������
\NewDocumentCommand \paren { s O { } }
  {
    % ������������ show answer ��������� show paren 
    \bool_if:NT \l__examzh_question_show_paren_answer_bool
      { \bool_set_true:N \l__examzh_question_show_paren_bool }
    \bool_if:NT \l__examzh_question_show_paren_bool
      {
        % ������������������������������������
        % \null -> \hbox{}
        % ������������������������ ������������, [Mar 19, 2022 at 22:47:07]:
          % ��������������������������������������������������������������� 3em ��������������������������������������������� 2em ��������������������������������������������������� \hill ������������������������
          % ������������������������ \hfill
          % ������ \nobreak ��� \allowbreak ���������������������������������������������������������������������������������������
          % ������������ source2e ��� \@dottedtocline���������������������
        % ������������ hfill ���������
        \bool_if:NT \l__examzh_paren_type_hfill_bool
          {
            \nobreak \hfill \allowbreak
            \null \nobreak \hfill \nobreak
          }
        \hbox:n
          {
            ���
            % \hbox_to_wd:nn { 2em }
              % {
                \bool_if:NTF \l__examzh_question_show_paren_answer_bool
                  % { \hfill \__examzh_paren_print_answer:n {#1} \hfill }
                  {
                    \IfBlankTF{#2}
                      {
                        % ��������������������� \paren ���������������������
                        \kern 2em
                      }
                      {
                        \kern0.7em \__examzh_paren_print_answer:n {#2} \kern0.7em
                      }
                  }
                  {
                    % \paren* ���������������������������������������\paren ������������������������������������
                    \IfBooleanTF{#1}{ \kern 3em }{ \kern 2em }
                  }
              % }
            ��� \kern -.4em
          }
      }
  }
% ��������������������������� ������ show ��������������������������������������������������������� print
\cs_new:Npn \__examzh_fillin_print_answer:n #1
  {
    % \group_begin:
      \tl_if_eq:NnF \l__examzh_fillin_text_color_tl { black }
        { \exp_args:NV \color \l__examzh_fillin_text_color_tl }
      #1
    % \group_end:
  }
\cs_new:Npn \__examzh_paren_print_answer:n #1
  {
    \group_begin:
      \tl_if_eq:NnF \l__examzh_paren_text_color_tl { black }
        { \exp_args:NV \color \l__examzh_paren_text_color_tl }
      #1
    \group_end:
  }


% fillin ������������������������
\str_new:N \l__examzh_fillin_type_str

% fillin type = paren ���������������
\bool_new:N \l__examzh_fillin_paren_banjiao_bool

% fillin ��� width ������������������������������������������������������������
\bool_new:N \l__examzh_fillin_width_fill_bool

% ���������������������������������
\str_new:N \l__examzh_fillin_no_answer_type_str

% no-answer-type = counter ������������
\int_new:N \g__examzh_fillin_no_answer_counter_int

% ������fillin ������������������������������������������������
\bool_new:N \l__examzh_fillin_width_type_hidden_bool

\keys_define:nn { exam-zh / fillin }
  {
    type .code:n = 
      {
        \str_set:Nn \l__examzh_fillin_type_str {#1}
      },
    show-answer .bool_set:N = \l__examzh_question_show_fillin_answer_bool,
    width .dim_set:N = \l__examzh_fillin_F_width_dim,
    width-type .choice:,
    width-type / fill .code:n =
      { \bool_set_true:N \l__examzh_fillin_width_fill_bool },
    width-type / normal .code:n =
      { \bool_set_false:N \l__examzh_fillin_width_fill_bool },
    box-color .tl_set:N = \l__examzh_fillin_box_color_tl,
    text-color .tl_set:N = \l__examzh_fillin_text_color_tl,
    no-answer-type .choices:nn =
      { blacktriangle, counter, none, hidden }
      { \str_set:Nx \l__examzh_fillin_no_answer_type_str { \l_keys_choice_tl } },
    no-answer-counter-index .int_gset:N = \g__examzh_fillin_no_answer_counter_int,
    no-answer-counter-label .tl_set:N = \l__examzh_fillin_label_tl,
    paren-type .choice:,
    paren-type / banjiao .code:n = 
      { \bool_set_true:N \l__examzh_fillin_paren_banjiao_bool },
    paren-type / quanjiao .code:n = 
      { \bool_set_false:N \l__examzh_fillin_paren_banjiao_bool },
    depth .dim_set:N = \l__examzh_fillin_line_depth_dim
  }
\keys_set:nn { exam-zh / fillin }
  {
    type                     = line,
    show-answer              = false,
    width                    = 3em,
    box-color                = black,
    text-color               = black,
    no-answer-type           = blacktriangle,
    no-answer-counter-index  = 1,
    no-answer-counter-label  = \arabic*,
    paren-type               = banjiao,
    width-type               = normal,
    depth                    = 0.4em
  }

\keys_define:nn { exam-zh }
  { fillin .meta:nn = { exam-zh / fillin } {#1} }


\dim_new:N \l__examzh_question_answer_depth_dim

% ������������
% \fillin \fillin[] \fillin[][] ��� show-answer = false ������������
% no-answer-type = blacktriangle ������������������������
% no-answer-type = counter ���������������������������������������������
% no-answer-type = none ������������
\NewDocumentCommand \fillin { s O{} o }
  {
    \group_begin:
      \IfNoValueTF {#3}
        {
          \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
            {
              % ������������
              \IfBooleanTF {#1}
                {
                  % \fillin*[]
                  \__examzh_fillin_breakline:n {#2}
                }
                {
                  % \fillin[]
                  \__examzh_fillin:n {#2}
                }
            }
            {
              % ���������������
              \IfBooleanTF {#1}
                { \__examzh_fillin_no_answer_breakable_typeset: }
                { \__examzh_fillin_no_answer_unbreakable_typeset: }
            }
        }
        {
          \keys_set:nn { exam-zh / fillin } {#2}
          \bool_if:NTF \l__examzh_question_show_fillin_answer_bool
            {
              % ������������
              \IfBooleanTF {#1}
                {
                  % \fillin*[][]
                  \__examzh_fillin_breakline:n {#3}
                }
                {
                  % \fillin[][]
                  \__examzh_fillin:n {#3}
                }
            }
            {
              % ���������������
              \IfBooleanTF {#1}
                { \__examzh_fillin_no_answer_breakable_typeset: }
                { \__examzh_fillin_no_answer_unbreakable_typeset: }
            }
        }
    \group_end:
    \space \ignorespaces
  }
\msg_new:nnn { exam-zh / fillin } { no-such-noanswertype }
  {
    There~is~no~such~noanswertype~named~#1!\\
    Please~read~the~manual~carefully!
  }
\cs_new:Npn \__examzh_fillin_no_answer_unbreakable_typeset:
  {
    \str_case:VnF \l__examzh_fillin_no_answer_type_str
      {
        { blacktriangle } { \__examzh_fillin_no_answer_typeset_blacktriangle: }
        { counter } { \__examzh_fillin_no_answer_typeset_counter: }
        { none } { \__examzh_fillin_output_unbreakable_F: }
      }
      {
        \msg_error:nnx { exam-zh / fillin } { no-such-noanswertype }
          { \l__examzh_fillin_no_answer_type_str }
      }
  }
\cs_new:Npn \__examzh_fillin_no_answer_breakable_typeset:
  {
    \str_case:VnF \l__examzh_fillin_no_answer_type_str
      {
        { blacktriangle } { \__examzh_fillin_no_answer_typeset_blacktriangle: }
        { counter } { \__examzh_fillin_no_answer_typeset_counter: }
        { none } { \__examzh_fillin_output_breakable_F: }
      }
      {
        \msg_error:nnx { exam-zh / fillin } { no-such-noanswertype }
          { \l__examzh_fillin_no_answer_type_str }
      }
  }
\cs_new:Npn \__examzh_fillin_no_answer_typeset_blacktriangle:
  {
    \__examzh_fillin_without_judge:n { \__examzh_fillin_blacktriangle: }
  }
\cs_new:Npn \__examzh_fillin_no_answer_typeset_counter:
  {
    \int_gincr:N \g__examzh_fillin_no_answer_counter_int
    \__examzh_fillin_without_judge:n
      { \__examzh_fillin_the_label: }
      % { \int_eval:n { \g__examzh_fillin_no_answer_counter_int - 1 } }
  }
\cs_new:Npn \__examzh_fillin_without_judge:n #1
  {
    % \ULdepth ��� \uline ���������������������
    \dim_set:Nn \ULdepth { 0.3em }
    % lazy ������������������������������������������������������������ bool ��� 
    % ������������������������������������������������������ LaTeX3 ��� b��������������������� lazy evaluation ������������
    \hbox_set:Nn \l_tmpa_box { \__examzh_fillin_print_answer:n {#1} }
    \dim_set:Nn \l__examzh_question_answer_depth_dim
      { \box_dp:N \l_tmpa_box }
    \__examzh_fillin_output_T:
  }
\cs_new:Npn \__examzh_fillin:n #1
  {
    % \ULdepth ��� \uline ���������������������
    \dim_set:Nn \ULdepth { 0.3em }
    % lazy ������������������������������������������������������������ bool ��� 
    % ������������������������������������������������������ LaTeX3 ��� b��������������������� lazy evaluation ������������
    \bool_lazy_and:nnTF
      { \bool_if_p:N \l__examzh_question_show_fillin_answer_bool }
      { \bool_not_p:n { \tl_if_empty_p:n {#1} } }
      {
        \hbox_set:Nn \l_tmpa_box { \__examzh_fillin_print_answer:n {#1} }
        \dim_set:Nn \l__examzh_question_answer_depth_dim
          { \box_dp:N \l_tmpa_box }
        \__examzh_fillin_output_T:
      }
      { 
        \__examzh_fillin_output_unbreakable_F:
      }
  }
\cs_new:Npn \__examzh_fillin_breakline:n #1
  {
    \bool_lazy_and:nnTF
      { \bool_if_p:N \l__examzh_question_show_fillin_answer_bool }
      { \bool_not_p:n { \tl_if_empty_p:n {#1} } }
      {
        \tl_set:Nn \l_tmpa_tl { \__examzh_fillin_print_answer:n {#1} }
        \__examzh_fillin_output_breakline_T:
      }
      { 
        \__examzh_fillin_output_breakable_F:
      }
  }
\msg_new:nnn { exam-zh } { no-fillin-type }
  {
    There~is~no~type~of~\fillin~named~#1!\\
    Please~read~the~manual~for~more~details.
  }
\cs_new:Npn \__examzh_fillin_output_T:
  {
    \str_case:VnF \l__examzh_fillin_type_str
      {
        { line } { \__examzh_fillin_uline_T: }
        { paren } { \__examzh_fillin_paren_T: }
        { circle } { \__examzh_fillin_circle_T: }
        { blank } { \__examzh_fillin_blank_T: }
        { rectangle } { \__examzh_fillin_rectangle_T: }
      }
      {
        \msg_error:nnx { exam-zh } { no-fillin-type }
          { \l__examzh_fillin_type_str }
      }
  }
\cs_new:Npn \__examzh_fillin_output_breakline_T:
  {
    \str_case:VnF \l__examzh_fillin_type_str
      {
        { line } { \__examzh_fillin_uline_breakline_T: }
        { paren } { \__examzh_fillin_paren_breakline_T: }
        { blank } { \__examzh_fillin_blank_breakline_T: }
      }
      {
        \msg_error:nnx { exam-zh } { no-breakable-fillin-type }
          { \l__examzh_fillin_type_str }
      }
  }
\msg_new:nnn { exam-zh } { no-breakable-fillin-type }
  {
    The~type~:~#1~ cannot~be~used~in~breakable~fillin~cmd.
  }
\cs_new:Npn \__examzh_fillin_output_breakable_F:
  {
    \str_case:VnF \l__examzh_fillin_type_str
      {
        { line } { \__examzh_fillin_uline_breakable_F: }
        { paren } { \__examzh_fillin_paren_breakable_F: }
        { circle } { \__examzh_fillin_circle_F: }
        { blank } { \__examzh_fillin_blank_breakable_F: }
        { rectangle } { \__examzh_fillin_rectangle_F: }
      }
      {
        \msg_error:nnx { exam-zh } { no-fillin-type }
          { \l__examzh_fillin_type_str }
      }
  }
\cs_new:Npn \__examzh_fillin_output_unbreakable_F:
  {
    \str_case:VnF \l__examzh_fillin_type_str
      {
        { line } { \__examzh_fillin_uline_unbreakable_F: }
        { paren } { \__examzh_fillin_paren_unbreakable_F: }
        { circle } { \__examzh_fillin_circle_F: }
        { blank } { \__examzh_fillin_blank_unbreakable_F: }
        { rectangle } { \__examzh_fillin_rectangle_F: }
      }
      {
        \msg_error:nnx { exam-zh } { no-fillin-type }
          { \l__examzh_fillin_type_str }
      }
  }
\cs_new:Npn \__examzh_fillin_uline_T:
  {
    % \uline
    \CJKunderline*
      [ depth = \l__examzh_fillin_line_depth_dim ]
      {
        \hspace* { 0.5em plus .5em minus .5em }
        \dim_compare:nNnTF { \l__examzh_question_answer_depth_dim } > { 0.2em }
          {
            \dim_sub:Nn \l__examzh_question_answer_depth_dim { 0.2em }
            \raisebox { \l__examzh_question_answer_depth_dim }
              { \box_use_drop:N \l_tmpa_box }
          }
          { \box_use_drop:N \l_tmpa_box }
        \hspace* { 0.5em plus .5em minus .5em }
      }
  }
\cs_new:Npn \__examzh_fillin_uline_breakable_F:
  {
    % \uline { \hspace* { \l__examzh_fillin_F_width_dim } } 
    \__examzh_fillin_breakable_hspace:NN \CJKunderline \allowbreak
  }
\cs_new:Npn \__examzh_fillin_uline_unbreakable_F:
  {
    \unskip
    \hspace* { 0.5em plus .5em minus .5em }
    \uline { \hspace* { \l__examzh_fillin_F_width_dim } }
    \ignorespaces
  }
% \cs_new:Nn \__examzh_fillin_uline:
  % {
    % \bgroup
    %   \color{ \l__examzh_fillin_text_color_tl } 
    %   \markoverwith{\textcolor{black}{\rule[-0.7ex]{2pt}{0.4pt}}}
    %   \ULon
% xeCJKfntef.sty
% xeCJK: ���������������������������������������������
% https://github.com/CTeX-org/ctex-kit/commit/ad44c6674bb377653544349f23b7c629bc9e4677
\RenewDocumentCommand \CJKunderline { s t- s o }
  {
    \xeCJK_ulem_group_begin:
      \xeCJK_fntef_boot:nnNNNn { underline } { uline } #1#2#3 {#4}
      \xeCJK_fntef_initial:nnn
        { \l__xeCJK_uline_depth_tl }
        { \l__xeCJK_uline_sep_tl }
        {
          \l__xeCJK_uline_format_tl
          \tex_vrule:D
            height \dim_eval:n { \l__xeCJK_uline_thickness_tl }
            depth \c_zero_dim
            width .2em
        }
      % ��� CJKunderline ������������������
      \color { \l__examzh_fillin_text_color_tl }
      \xeCJK_ulem_on:n
  }
  % }
\cs_new:Npn \__examzh_fillin_uline_breakline_T:
  {
    \CJKunderline*
      [ depth = \l__examzh_fillin_line_depth_dim ]
    % \uline
    % \__examzh_fillin_uline:
      {
        \hspace* { 0.5em plus .5em minus .5em }
        % \color{ \l__examzh_fillin_text_color_tl } 
        \l_tmpa_tl
        % ���������������������������������������������������������������������������
        \hspace* { 0.5em plus .5em minus .5em }
      }
  }
\cs_new:Npn \__examzh_fillin_paren_T:
  {
    \unskip
    \bool_if:NTF \l__examzh_fillin_paren_banjiao_bool
      {
        (
          \hspace* { 0.5em plus .5em minus .5em }
          \group_begin:
            \box_use_drop:N \l_tmpa_box
          \group_end:
          \hspace* { 0.5em plus .5em minus .5em }
        )
      }
      {
        ���
          \hspace* { 0.5em plus .5em minus .5em }
          \group_begin:
            \box_use_drop:N \l_tmpa_box
          \group_end:
          \hspace* { 0.5em plus .5em minus .5em }
        ���
      }
    \ignorespaces
  }
\cs_new:Npn \__examzh_fillin_paren_breakline_T:
  {
    \unskip
    \bool_if:NTF \l__examzh_fillin_paren_banjiao_bool
      {
        (
          \hspace* { 0.5em plus .5em minus .5em }
          \group_begin:
            \l_tmpa_tl
          \group_end:
          \hspace* { 0.5em plus .5em minus .5em }
        )
      }
      {
        ���
          \hspace* { 0.5em plus .5em minus .5em }
          \group_begin:
            \l_tmpa_tl
          \group_end:
          \hspace* { 0.5em plus .5em minus .5em }
        ���
      }
    \ignorespaces
  }
\box_new:N \c__examzh_banjiao_right_brace_box
\box_new:N \c__examzh_quanjiao_right_brace_box
\hbox_set:Nn \c__examzh_banjiao_right_brace_box { ) }
\hbox_set:Nn \c__examzh_quanjiao_right_brace_box { ��� }
\dim_const:Nn \c__examzh_banjiao_right_brace_width_dim  % (
  { \box_wd:N \c__examzh_banjiao_right_brace_box }
\dim_const:Nn \c__examzh_quanjiao_right_brace_width_dim  % (
  { \box_wd:N \c__examzh_quanjiao_right_brace_box }
\cs_new:Npn \__examzh_fillin_paren_breakable_F:
  {
    \unskip
    \hspace* { 0.5em plus .5em minus .5em }
    \bool_if:NTF \l__examzh_fillin_paren_banjiao_bool
      {
        ( \__examzh_fillin_breakable_hspace:NN \use:n \nobreak \kern-\c__examzh_banjiao_right_brace_width_dim ) \allowbreak
      }
      {
        ��� \__examzh_fillin_breakable_hspace:NN \use:n \nobreak \kern-\c__examzh_quanjiao_right_brace_width_dim ���\allowbreak
      }
    \ignorespaces
  }
\cs_new:Npn \__examzh_fillin_paren_unbreakable_F:
  {
    \unskip
    \hspace* { 0.5em plus .5em minus .5em }
    \bool_if:NTF \l__examzh_fillin_paren_banjiao_bool
      {
        ( \hspace* { \l__examzh_fillin_F_width_dim } )
      }
      {
        ��� \hspace* { \l__examzh_fillin_F_width_dim } ���
      }
    \ignorespaces
  }
\cs_new:Npn \__examzh_fillin_blank_T:
  {
    \unskip
    \hspace* { 0.5em plus .5em minus .5em }
    \group_begin:
      \box_use_drop:N \l_tmpa_box
    \group_end: 
    \ignorespaces
  }
\cs_new:Npn \__examzh_fillin_blank_breakline_T:
  {
    \unskip
    \hspace* { 0.5em plus .5em minus .5em }
    \group_begin:
      \l_tmpa_tl 
    \group_end:
    \ignorespaces
  }
\cs_new:Npn \__examzh_fillin_blank_breakable_F:
  {
    % \hspace* { \l__examzh_fillin_F_width_dim }
    \__examzh_fillin_breakable_hspace:NN \use:n \allowbreak
  }
\cs_new:Npn \__examzh_fillin_blank_unbreakable_F:
  {
    \hspace* { \l__examzh_fillin_F_width_dim }
    % \__examzh_fillin_breakable_hspace:NN \use:n \allowbreak
  }
\tikzset
  {
    fillin-circle/.style = 
      {
        rounded~rectangle~west~arc = convex,
        draw, rounded~rectangle,
        color = \l__examzh_fillin_box_color_tl, text = \l__examzh_fillin_text_color_tl
      }
  }
\cs_new:Npn \__examzh_fillin_circle_T:
  {
    \hspace* { .5em minus .5em }
    \tikz[baseline=-3pt]
      {
        \node [fillin-circle] at (0,0) 
          { \box_use_drop:N \l_tmpa_box };
      }
    \hspace* { .5em minus .5em }
  }
\cs_new:Npn \__examzh_fillin_circle_F:
  {
    \hspace* { 0.5em plus .5em minus .5em }
    \tikz[baseline=-3pt]
      {
        \node [fillin-circle] at (0,0) 
          { \phantom{t} };
      }
    \hspace* { 0.5em plus .5em minus .5em }
  }
\cs_new:Npn \__examzh_fillin_rectangle_T:
  {
    \hspace* { .5em minus .5em }
    \begin{tikzpicture}[baseline = -3pt]
      \node[draw, color = \l__examzh_fillin_box_color_tl, text = \l__examzh_fillin_text_color_tl] 
        { \box_use_drop:N \l_tmpa_box };
    \end{tikzpicture}
    \hspace* { .5em minus .5em }
  }
\cs_new:Npn \__examzh_fillin_rectangle_F:
  {
    \hspace* { 0.5em plus .5em minus .5em }
    \begin{tikzpicture}[baseline = -3pt]
      \node[draw, color = \l__examzh_fillin_box_color_tl, text = \l__examzh_fillin_text_color_tl] 
        { \phantom{a} };
    \end{tikzpicture}
    \hspace* { 0.5em plus .5em minus .5em }
  }

% ���������������������������������������������������
\cs_generate_variant:Nn \dim_sub:Nn { NV }
\cs_generate_variant:Nn \dim_add:Nn { NV }
\cs_generate_variant:Nn \dim_set:Nn { NV, Nx }
% ������������������������ list ���������
\bool_new:N \l__if_list_bool
\int_new:N \l__list_depth_int
\cs_generate_variant:Nn \dim_set:Nn { Nx }
\AddToHook { cmd / list / after }
  {
    \bool_set_true:N \l__if_list_bool
    \int_incr:N \l__list_depth_int
    \dim_if_exist:cF { l__list_leftmargin_ \int_to_roman:n { \l__list_depth_int } _dim }
      {
        \dim_new:c { l__list_leftmargin_ \int_to_roman:n { \l__list_depth_int } _dim }
      }
    % ��������������������������� \leftmargin ���
    \dim_set_eq:cN { l__list_leftmargin_ \int_to_roman:n { \l__list_depth_int } _dim } \leftmargin
  }
\AddToHook { cmd / endlist  / before } 
  { \int_zero:N \l__list_depth_int }

\cs_new:Npn \__examzh_fillin_breakable_hspace:NN #1#2
  % #1: CJKunderline / use:n
  % #2: \allowbreak
  {
    \dim_set_eq:NN \l_tmpb_dim \linegoal
    % ������ \l__examzh_fillin_F_width_dim ��� linegoal
    \dim_compare:nNnTF { \l__examzh_fillin_F_width_dim } > { \l_tmpb_dim }
      {
        % ������ linegoal ������������ linegoal��������� \l__examzh_fillin_F_width_dim ������ linegoal ������
        % \dim_set:NV \l_tmpa_dim \linegoal
        \dim_set:NV \l_tmpa_dim \l_tmpb_dim
        % ������������ list ���������
        \bool_if:NTF \l__if_list_bool
          {
            % ������ 1 ��� ��������������� leftmargin ��������� list ������ linegoal ������
            \int_step_inline:nn { \l__list_depth_int }
              {
                \dim_add:Nn \l_tmpa_dim { \dim_use:c { l__list_leftmargin_ \int_to_roman:n { ##1 } _dim } }
              }
            #1 { \hspace { \l_tmpa_dim } }
          }
          {
            % ���������
            #1 { \hspace { \l_tmpb_dim } }
          }
        \dim_sub:NV \l__examzh_fillin_F_width_dim \l_tmpa_dim
        \dim_while_do:nNnn { \l__examzh_fillin_F_width_dim } > { \linewidth }
          {
            % ���������\l__examzh_fillin_F_width_dim ��� \linewidth ��������������������������������������������� linewidth
            \\ #1 { \hspace* { \linewidth } }
            \dim_sub:Nn \l__examzh_fillin_F_width_dim { \linewidth }
          }
        % \\ #1 { \hspace* { \linewidth } }
        \dim_compare:nNnT { \l__examzh_fillin_F_width_dim } < { \linewidth }
          { 
            % ������������������ fill
            \bool_if:NTF \l__examzh_fillin_width_fill_bool
              {
                \\ #1 { \hspace* { \linewidth } } 
              }
              {
                \\ #1 { \hspace* { \l__examzh_fillin_F_width_dim } } 
              }
          }
      }
      {
        #1 { \hspace* { \l__examzh_fillin_F_width_dim } }
      }
    #2
  }


\dim_new:N \l__examzh_blacktriangle_length_dim
\dim_set:Nn \l__examzh_blacktriangle_length_dim { .7em }
\cs_new:Npn \__examzh_fillin_blacktriangle:
  {
    \tikz[rounded~corners=0.5pt,baseline=0pt]
      {
        \fill[] (0,0) -- ++(60\c_colon_str \l__examzh_blacktriangle_length_dim) -- ++(-60\c_colon_str \l__examzh_blacktriangle_length_dim) -- cycle ;
      }
  }

\str_new:N \l__examzh_solution_blank_type_str
\bool_new:N \g__examzh_solution_show_bool
\bool_new:N \g__examzh_solution_show_move_bool
\keys_define:nn { exam-zh / solution }
  {
    % show-solution .bool_set:N = \g__examzh_solution_show_bool,
    show-solution .choice:,
    show-solution / hide .code:n =
      {
        \bool_gset_false:N \g__examzh_solution_show_bool
      },
    show-solution / show-stay .code:n =
      {
        \bool_gset_true:N \g__examzh_solution_show_bool
        \bool_gset_false:N \g__examzh_solution_show_move_bool
      },
    show-solution / show-move .code:n =
      {
        \bool_gset_true:N \g__examzh_solution_show_bool
        \bool_gset_true:N \g__examzh_solution_show_move_bool
      },
    % show-answer .bool_set:N = \g__examzh_solution_show_bool,
    show-qed      .bool_set:N = \l__examzh_solution_show_qed_bool,
    qedsymbol     .tl_set:N = \l__examzh_solution_qedsymbol_tl,
    label-content  .tl_set:N   = \l__examzh_solution_label_content_tl,
    label-punct    .tl_set:N   = \l__examzh_solution_label_punct_tl,
    score-showleader .bool_set:N = \l__examzh_score_show_leader_bool,
    score-pre-content .tl_set:N = \l__examzh_score_pre_content_tl,
    score-post-content .tl_set:N = \l__examzh_score_post_content_tl,
    score-format .tl_set:N = \l__examzh_score_format_tl,
    text-color .tl_set:N = \l__examzh_solution_text_color_tl,
    blank-type .choices:nn =
      { none, manual, hide }
      {
        \str_set:Nx \l__examzh_solution_blank_type_str { \l_keys_choice_tl }
      },
    blank-vsep .skip_set:N = \l__examzh_solution_blank_vsep_skip,
    top-sep    .skip_set:N = \l__examzh_solution_top_sep_skip,
    bottom-sep .skip_set:N = \l__examzh_solution_bottom_sep_skip,
    parbreak .bool_set:N = \l__examzh_solution_par_break_bool,
  }
\keys_set:nn { exam-zh / solution }
  {
    show-solution      = hide,
    show-qed           = true,
    qedsymbol          = $\square$,
    label-content      = {������},
    label-punct        = {},
    score-showleader   = true,
    score-pre-content  = {},
    score-post-content = ���,
    score-format       = \color{red},
    text-color         = black,
    blank-type         = none,
    blank-vsep         = 12ex plus 1ex minus 1ex,
    top-sep            = .25em plus .25em minus .1em,
    bottom-sep         = 0pt,
    parbreak           = false
  }
\keys_define:nn { exam-zh }
  { solution .meta:nn = { exam-zh / solution } {#1} }
% ���������������
\iow_new:N \g__write_soltion_to_auxfile
% ������������������
\cs_generate_variant:Nn \iow_open:Nn { Nx }
\int_new:N \g__examzh_solution_aux_index_int
\NewDocumentEnvironment { solution } { O{ } +b }
  {
    % \addvspace { \l__examzh_solution_top_sep_skip }
    \keys_set:nn { exam-zh / solution } {#1}
    % ������������������ \examsetup ������ qedsymbol ���������������������
    \cs_set_eq:NN \qedsymbol \l__examzh_solution_qedsymbol_tl
    \bool_if:NTF \g__examzh_solution_show_bool
      {
        \bool_if:NTF \g__examzh_solution_show_move_bool
          {
            % ������������������
            \iow_open:Nn \g__write_soltion_to_auxfile {examzh \int_use:N \g__examzh_question_index_int .solution}
            \iow_now:Nx \g__write_soltion_to_auxfile 
              {
                \par \noindent
                \int_eval:n { \g__examzh_question_index_int - 1 }.  ������������
                \exp_not:n {#2}
                \par \exp_not:n { \vspace*{1ex} }
              }
            \iow_close:N \g__write_soltion_to_auxfile

            % ���������������������
            % \group_begin:
            % \tl_set:Nn \tcb@verbatim@begin@hook 
            %   { \iow_now:Nx \tcb@out { \int_use:N \g__examzh_question_index_int } }
            % \tcbverbatimwrite {examzh \int_use:N \g__examzh_question_index_int .solution} #2
            % \endtcbverbatimwrite
            % \group_end:
          }
          {
            \vspace { \l__examzh_solution_top_sep_skip }
            \__examzh_solution_print_answer:n {#2}
            \par    
            \vspace { \l__examzh_solution_bottom_sep_skip }
          }
      }
      {
        \str_case:VnF \l__examzh_solution_blank_type_str
          {
            { none } { }
            { manual } { \addvspace { \l__examzh_solution_blank_vsep_skip } }
            { hide } { \__examzh_solution_simply_hide_solution:n {#2} }
          }
          {}
      }
    \par
    % \mode_leave_vertical:
    % \addvspace { \l__examzh_solution_bottom_sep_skip }
  }
  {}

\AtEndDocument
  {
    \bool_if:NT \g__examzh_solution_show_move_bool
      {
        \newpage
        \begin{center}
          \zihao{-2} \heiti \makebox[5em][s]{������������}
        \end{center}
        \vspace*{3ex}

        \kaishu
        \int_compare:nNnT { \g__examzh_question_index_int } > { 0 }
          {
            \int_step_inline:nn { \g__examzh_question_index_int }
              {
                \file_if_exist:nT { examzh #1 .solution }
                  { \file_input:n { examzh #1 .solution } }
              }
          }
      }
  }



\cs_new:Npn \__examzh_solution_simply_hide_solution:n #1
  {
    \begin{tcolorbox}
      [
        invisible,
        frame~hidden,
        breakable,
        opacityback  = 0,
        opacityframe = 0,
        size  = minimal,
        width = \linewidth
      ]
      #1
    \end{tcolorbox}
  }
\cs_new:Npn \__examzh_solution_print_answer:n #1
  {
    \par
    \pushQED { \qed }
    % \normalfont \topsep6 \p@ \@plus6 \p@ \relax
    % \trivlist
    % \item \relax
    \group_begin:
      % \hspace* { 2\ccwd }
      \bfseries  \l__examzh_solution_label_content_tl  
      \@addpunct { \l__examzh_solution_label_punct_tl }
    \group_end:
    \hspace{0.5em} 
    % \ignorespaces
    % ���������������������������
    \bool_if:NT \l__examzh_solution_par_break_bool { \par }
    % \group_begin:
    \begingroup
      \color { \l__examzh_solution_text_color_tl } #1
    \endgroup
    % \group_end:
    \bool_if:NT \l__examzh_solution_show_qed_bool
      { \popQED }
    % \endtrivlist 
    % \@endpefalse
  }

% https://github.com/CTeX-org/forum/issues/256#issuecomment-1172319787
\zref@require@unique

\NewDocumentCommand { \score } { O{} m }
  {
    \group_begin:
      \keys_set:nn { exam / question } {#1}
      \mode_if_math:TF
        { 
          \__examzh_score_math_version:n { #2 }
        }
        { 
          \__examzh_score_text_version:n { #2 }
        }
    \group_end:
  }
\cs_new:Npn \__examzh_score_math_version:n #1
  {
    \bool_if:NTF \l__examzh_score_show_leader_bool
      {
        \__examzh_math_cdotfill:n 
          { 
            \group_begin:
              \l__examzh_score_format_tl
              \l__examzh_score_pre_content_tl
              #1
              \l__examzh_score_post_content_tl 
            \group_end:
          }
      }
      {
        \__examzh_math_nodotfill:n
          {
            { 
              \group_begin:
                \l__examzh_score_format_tl
                \l__examzh_score_pre_content_tl
                #1
                \l__examzh_score_post_content_tl 
              \group_end:
            }
          }
      }
  }
\cs_new:Npn \__examzh_score_text_version:n #1
  {
    \bool_if:NTF \l__examzh_score_show_leader_bool
      {
        \group_begin:
          \__examzh_cdotfill:
          \l__examzh_score_format_tl
          \l__examzh_score_pre_content_tl #1 \l__examzh_score_post_content_tl 
        \group_end:
      }
      {
        \hfill
        \group_begin:
          \l__examzh_score_format_tl
          \l__examzh_score_pre_content_tl #1 \l__examzh_score_post_content_tl
        \group_end:
      }
    \par \noindent \ignorespaces
  }
% ������ latex.ltx, line 651 ��� \dotfill
\cs_new:Npn \__examzh_cdotfill:
  {
    \mode_leave_vertical:
    \cleaders
      \hbox_to_wd:nn { .44em } { \hss $\cdot$ \hss }
      \hfill \kern\z@
  }
\cs_new_protected:Npn \__examzh_math_nodotfill:n #1
  {
    \stepcounter { zref@unique }
    \hbox_overlap_right:n
      {
        \zsaveposx { \thezref@unique L }
        \zref@ifrefundefined { \thezref@unique R }
          { }
          {
            \skip_horizontal:n
              {
                  \zposx { \thezref@unique R } sp
                - \zposx { \thezref@unique L } sp
              }
          }
      }
    \tag * { \zsaveposx { \thezref@unique R } #1 }
  }
\cs_new_protected:Npn \__examzh_math_cdotfill:n #1
  {
    \stepcounter { zref@unique }
    \hbox_overlap_right:n
      {
        \zsaveposx { \thezref@unique L }
        \zref@ifrefundefined { \thezref@unique R }
          { }
          {
            \cleaders
              \hbox_to_wd:nn { .44em } { \hss $\cdot$ \hss }
              \skip_horizontal:n
                {
                    \zposx { \thezref@unique R } sp
                  - \zposx { \thezref@unique L } sp
                }
          }
      }
    \tag * { \zsaveposx { \thezref@unique R } #1 }
  }

\endinput