%%     Copyright (C) 2020-2022 by Nan Geng <nangeng@nwafu.edu.cn>
%% --------------------------------------------------------------------------
%%
%%     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 Nan Geng.
%%
%% --------------------------------------------------------------------------
%%
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\RequirePackage{expl3}
\ProvidesExplPackage{circledtext}{2024-09-09}{v1.1.1}
  {Typeset circled text with l3draw}

\RequirePackage { xtemplate, l3keys2e, l3draw, xparse }

% ������TeXLive���������������
\cs_if_free:NT \box_ht_plus_dp:N
  {
    \cs_new_protected:Npn \box_ht_plus_dp:N #1
      { \tex_dimexpr:D \box_ht:N #1 + \box_dp:N #1 \scan_stop: }
  }

% ������������������������
% #1 *������������������������
% #2 ������������
% #3 ������
\NewDocumentCommand{\circledtext}{ s O{} m }
  {
    \group_begin:
      \IfBooleanTF{ #1 }
        {
          \bool_set_true:N  \l__circledtext_negative_bool
        }{
          \bool_set_false:N \l__circledtext_negative_bool
        }
      \__circledtext_handle:nn { #2 } { #3 }
    \group_end:
  }

% ������������������
\bool_new:N   \l__circledtext_negative_bool

% ���������������
\box_new:N    \l__circledtext_basebox_box

% ������������������
\tl_new:N     \l__circledtext_char_box_type_tl
% ������������������������
\clist_new:N  \g__circledtext_char_box_list_clist

% ������������
\tl_new:N     \l__circledtext_resize_method_tl
% ������������������
\clist_new:N  \g__circledtext_resize_method_clist

% ������������������������������
\dim_new:N    \l__circledtext_char_box_size_dim
% ���������������������������������������
\dim_new:N    \l__circledtext_char_box_radius_dim
% ���������������
\dim_new:N    \l__circledtext_char_width_dim
% ���������������
\dim_new:N    \l__circledtext_char_height_dim
% ������(���)������������
\dim_new:N    \l__circledtext_box_width_dim
% ������(���)������������
\dim_new:N    \l__circledtext_box_height_dim
% ������(���)������������
\dim_new:N    \l__circledtext_box_linewidth_dim
% ������(���)������������������������������������
\dim_new:N    \l__circledtext_cross_linewidth_dim
% ������(���)������(���������)
\dim_new:N    \l__circledtext_char_dp_dim

% ������(���)������������
\coffin_new:N \l__circledtext_str_box_coffin
% ���������������������
\coffin_new:N \l__circledtext_box_coffin
% ������������������
\coffin_new:N \l__circledtext_char_coffin
% ������������������
\coffin_new:N \l__circledtext_tmpa_coffin
% ������������������
\coffin_new:N \l__circledtext_tmpb_coffin

% ���������������
\dim_new:N    \charboxwd
% ���������������
\dim_new:N    \charboxht

% ���������������(���)
\tl_new:N     \l__circledtext_chars_tl
% ������(���)������������
\tl_new:N     \l__circledtext_character_format_tl
% ���������������������������������������������������������
\int_new:N    \l__circledtext_cross_color_ratio_int
\int_set:Nn   \l__circledtext_cross_color_ratio_int { 30 }
% ������(���)���������������(������������������)
\int_new:N    \l__circledtext_charstroke_type_int

% ������������������������
\fp_new:N     \l__circledtext_char_shrink_fp

% ���������������������
\cs_new_nopar:Nn \__circledtext_aux_color_boxfill:
  { }

% ���������l3draw���������
% ���������������������������������zitie������(\url{https://www.ctan.org/pkg/zitie})���

% ������������������
% #1 ������������
% #2 ���������������
\cs_set_nopar:Npn \__circledtext_color_select:nn #1#2
  {
    \color_set:nn {#1} {#2}
  }
\cs_generate_variant:Nn \__circledtext_color_select:nn {nx}

% ������������������
% #1 ������������
% #2 ������������
% #3 ���������������
\cs_set_nopar:Npn \__circledtext_color_select:nnn #1#2#3
  {
    \color_set:nnn {#1} {#2} {#3}
  }
\cs_generate_variant:Nn \__circledtext_color_select:nnn {nnx}

% ���������������������������
\cs_new:Npn \__circledtext_calc_basechar_w_h:
  {
    \dim_set:Nn \l__circledtext_char_width_dim
      {
        \box_wd:N \l__circledtext_basebox_box
      }
    \dim_set:Nn \l__circledtext_char_height_dim
      {
        \box_ht_plus_dp:N \l__circledtext_basebox_box
      }
  }

% ���������������������������������
\cs_new_nopar:Npn \__circledtext_coffin_ht_plus_dp:N #1
  {
    \coffin_ht:N #1 + \coffin_dp:N #1
  }

% ���������������������������(���������������������������������������)
\cs_new:Npn \__circledtext_calc_char_box_size:
  {
    % ���������������������������������������
    \hbox_set:Nn \l_tmpa_box
      {
        \tl_use:N \l__circledtext_character_format_tl
        \tl_use:N \c__circledtext_basechar_tl
      }

    % ������������
    \dim_set:Nn \l_tmpa_dim
      {
        \box_wd:N \l_tmpa_box
      }
    % ������������
    \dim_set:Nn \l_tmpb_dim
      {
        \box_ht_plus_dp:N \l_tmpa_box
      }

    % ������������
    \dim_set:Nn \l__circledtext_char_dp_dim
      {
        \box_dp:N \l_tmpa_box
      }

    % ���������������
    \dim_compare:nNnTF \l_tmpa_dim > \l_tmpb_dim
      {
        \dim_set_eq:NN \l__circledtext_char_box_size_dim \l_tmpa_dim
      }
      {
        \dim_set_eq:NN \l__circledtext_char_box_size_dim \l_tmpb_dim
      }

    % ���������������������������������������(������)
    \dim_set_eq:NN \charboxwd \l__circledtext_char_box_size_dim
    \dim_set_eq:NN \charboxht \l__circledtext_char_box_size_dim

    % ���������������
    \dim_set:Nn \l__circledtext_char_box_radius_dim
      {
        \fp_to_dim:n
          {
            \fp_eval:n { \l__circledtext_char_box_size_dim * sqrt(2)/ 2 }
          }
      }
  }

% ������������������������������������������������
% ���������6������������������������
% #1 ���������x������
% #2 ���������y������
% #3 ���������x������
% #4 ���������y������
% #5 x������������������(������������������)
% #6 y������������������(������������������)
\cs_new_nopar:Npn \__circledtext_char_box_type:n #1
  {
    __circledtext_char_box_construct_type_ #1 :nnnnnn
  }
% ������������������������������������������������������
% ���������������6������������������������
% #1 ���������x������
% #2 ���������y������
% #3 ���������x������
% #4 ���������y������
% #5 x������������������(������������������)
% #6 y������������������(������������������)
\cs_new_nopar:Npn \__circledtext_char_box_type_c:n #1
  {
    \use:c
      {
        __circledtext_char_box_construct_type_ #1 :nnnnnn
      }
  }

% ���������������������������������������������
% #1 ������������
\cs_new:Npn \__circledtext_new_char_box_construct:nn #1
  {
    % ���������������������clist
    \clist_put_right:Nn \g__circledtext_char_box_list_clist {#1}
    % ������\cs_new:cn __circledtext_char_box_construct_type_none:nnnnnn
    \cs_new:cn { \__circledtext_char_box_type:n {#1} }
  }
\cs_generate_variant:Nn \__circledtext_new_char_box_construct:nn { V }
\cs_generate_variant:Nn \__circledtext_new_char_box_construct:nn { x }

% ������������������������������

% ���������
\__circledtext_new_char_box_construct:nn { none } { }

% ���������������
\__circledtext_new_char_box_construct:nn { __filledsquare }
  {
    \cs_if_eq:NNF \__circledtext_aux_color_boxfill: \c_empty_tl
      {
        \__circledtext_aux_color_boxfill:
        \draw_scope_begin:
          \bool_if:NTF \l__circledtext_negative_bool
            {
              \color_fill:n { circledtextcharcolor }
            }{
              \color_fill:n { circledtextboxfill   }
            }
          \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
          \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
          \draw_path_use_clear:n { fill }
        \draw_scope_end:
      }
  }

% ���������������������
\__circledtext_new_char_box_construct:nn { __negfilledsquare }
  {
    \cs_if_eq:NNF \__circledtext_aux_color_boxfill: \c_empty_tl
      {
        \__circledtext_aux_color_boxfill:
        \draw_scope_begin:
          \bool_if:NTF \l__circledtext_negative_bool
            {
              \color_fill:n { circledtextboxfill   }
            }{
              \color_fill:n { circledtextcharcolor }
            }
          \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
          \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
          \draw_path_use_clear:n { fill }
        \draw_scope_end:
      }
  }

% ������������������������
\__circledtext_new_char_box_construct:nn { __innerfilledcircle }
  {
    \cs_if_eq:NNF \__circledtext_aux_color_boxfill: \c_empty_tl
      {
        \__circledtext_aux_color_boxfill:
        \draw_scope_begin:
          \bool_if:NTF \l__circledtext_negative_bool
            {
              \color_fill:n { circledtextcharcolor }
            }{
              \color_fill:n { circledtextboxfill   }
            }
          \draw_path_circle:nn { #3/2, #4/2 } { #3*#5/2 }
          \draw_path_use_clear:n { fill }
        \draw_scope_end:
      }
  }

% ������������������������
\__circledtext_new_char_box_construct:nn { __outerfilledcircle }
  {
    \cs_if_eq:NNF \__circledtext_aux_color_boxfill: \c_empty_tl
      {
        \__circledtext_aux_color_boxfill:
        \draw_scope_begin:
          \bool_if:NTF \l__circledtext_negative_bool
            {
              \color_fill:n { circledtextcharcolor }
            }{
              \color_fill:n { circledtextboxfill   }
            }
          \draw_path_circle:nn { #3/2, #4/2 } { \l__circledtext_char_box_radius_dim*#5 }
          \draw_path_use_clear:n { fill }
        \draw_scope_end:
      }
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __lowerfilledsquare }
  {
    \draw_scope_begin:
      \color_fill:n { lowerbgboxfill   }
      \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
      \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __upperfilledsquare }
  {
    \draw_scope_begin:
      \color_fill:n { upperbgboxfill   }
      \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
      \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __shadowfilledsquare }
  {
    \draw_scope_begin:
      \color_fill:n { shadowboxfill   }
      \draw_transform_shift:n { (#3-#3*#5)/2 + \charboxwd * 0.1,
                                (#4-#4*#6)/2 - \charboxht * 0.1 }
      \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __innerlowerfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { lowerbgboxfill   }
      \draw_path_circle:nn { #3/2, #4/2 } { #3*#5/2 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __innerupperfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { upperbgboxfill   }
      \draw_path_circle:nn { #3/2, #4/2 } { #3*#5/2 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __innershadowfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { shadowboxfill   }
      \draw_transform_shift:n { \charboxwd*0.1, -\charboxht*0.1 }
      \draw_path_circle:nn { #3/2, #4/2 } { #3*#5/2 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __outerlowerfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { lowerbgboxfill   }
      \draw_path_circle:nn { #3/2, #4/2 }
        { \l__circledtext_char_box_radius_dim*#5 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __outerupperfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { upperbgboxfill   }
      \draw_path_circle:nn { #3/2, #4/2 }
        { \l__circledtext_char_box_radius_dim*#5 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ������������������������������
\__circledtext_new_char_box_construct:nn { __outershadowfilledcircle }
  {
    \draw_scope_begin:
      \color_fill:n { shadowboxfill   }
      \draw_transform_shift:n { \charboxwd*0.1, -\charboxht*0.1 }
      \draw_path_circle:nn { #3/2, #4/2 }
        { \l__circledtext_char_box_radius_dim*#5 }
      \draw_path_use_clear:n { fill }
    \draw_scope_end:
  }

% ���������������
\__circledtext_new_char_box_construct:nn { __squarebox }
  {
    \draw_scope_begin:
      \color_stroke:n { circledtextcharboxcolor   }
      \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
      \draw_path_rectangle:nn { #1, #2 } { #3*#5, #4*#6 }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ������������������������
\__circledtext_new_char_box_construct:nn { __innercirclebox }
  {
    \__circledtext_aux_color_boxfill:
    \draw_scope_begin:
      \color_stroke:n { circledtextcharboxcolor   }
      \draw_path_circle:nn { #3/2, #4/2 } { #3*#5/2 }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ������������������������
\__circledtext_new_char_box_construct:nn { __outercirclebox }
  {
    \draw_scope_begin:
      \color_stroke:n { circledtextcharboxcolor }
      \draw_path_circle:nn { #3/2, #4/2 }
        { \l__circledtext_char_box_radius_dim*#5 }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ���������������������(���������������������)
\__circledtext_new_char_box_construct:nn { __dcross }
  {
    \draw_scope_begin:
      \tl_if_empty:NF \l__circledtext_dash_pattern_tl
        {
          \exp_args:No \draw_dash_pattern:nn { \l__circledtext_dash_pattern_tl } { 0pt }
        }
      \draw_linewidth:n{ \l__circledtext_cross_linewidth_dim }
      \color_stroke:n { circledtextcrosscolor }
      \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
      \draw_path_moveto:n { #1   ,    #2 }
      \draw_path_lineto:n { #3*#5, #4*#6 }
      \draw_path_moveto:n { #1   , #4*#6 }
      \draw_path_lineto:n { #3   , #2*#5 }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ���������������������(���������������������������)
\__circledtext_new_char_box_construct:nn { __scross }
  {
    \draw_scope_begin:
      \tl_if_empty:NF \l__circledtext_dash_pattern_tl
        {
          \exp_args:No \draw_dash_pattern:nn { \l__circledtext_dash_pattern_tl } { 0pt }
        }
      \draw_linewidth:n{ \l__circledtext_cross_linewidth_dim }
      \color_stroke:n { circledtextcrosscolor }
      \draw_transform_shift:n { (#3-#3*#5)/2, (#4-#4*#6)/2 }
      \draw_path_moveto:n { #3*#5/2,      #2 }
      \draw_path_lineto:n { #3*#5/2,   #4*#6 }
      \draw_path_moveto:n { #1     , #4*#6/2 }
      \draw_path_lineto:n { #3*#5  , #4*#6/2 }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ���������������������(���������������)
\__circledtext_new_char_box_construct:nn { __innerdcross }
  {
    \draw_scope_begin:
      \tl_if_empty:NF \l__circledtext_dash_pattern_tl
        {
          \exp_args:No \draw_dash_pattern:nn { \l__circledtext_dash_pattern_tl } { 0pt }
        }
      \draw_linewidth:n{ \l__circledtext_cross_linewidth_dim }
      \color_stroke:n { circledtextcrosscolor }
      \draw_path_moveto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #2 } % line's first point
            { #3, #4 } % line's second point
            { #3/2, #4/2 } { #3*#5/2 }
            {1}  % index of intersect
        }
      \draw_path_lineto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #2 } % line's first point
            { #3, #4 } % line's second point
            { #3/2, #4/2 } { #3*#5/2 }
            {2}  % index of intersect
        }
      \draw_path_moveto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #4 } % line's first point
            { #3, #2 } % line's second point
            { #3/2, #4/2 } { #3*#5/2 }
            {1}  % index of intersect
        }
      \draw_path_lineto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #4 } % line's first point
            { #3, #2 } % line's second point
            { #3/2, #4/2 } { #3*#5/2 }
            {2}  % index of intersect
        }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ���������������������(���������������)
\__circledtext_new_char_box_construct:nn { __outercross }
  {
    \draw_scope_begin:
      \tl_if_empty:NF \l__circledtext_dash_pattern_tl
        {
          \exp_args:No \draw_dash_pattern:nn { \l__circledtext_dash_pattern_tl } { 0pt }
        }
      \draw_linewidth:n{ \l__circledtext_cross_linewidth_dim }
      \color_stroke:n { circledtextcrosscolor }
      \draw_path_moveto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #3/2, #2 } % line's first point
            { #3/2, #4 } % line's second point
            { #3/2, #4/2 } { \l__circledtext_char_box_radius_dim*#5 }
            {1}  % index of intersect
        }
      \draw_path_lineto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #3/2, #2 } % line's first point
            { #3/2, #4 } % line's second point
            { #3/2, #4/2 } { \l__circledtext_char_box_radius_dim*#5 }
            {2}  % index of intersect
        }
      \draw_path_moveto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #4/2 } % line's first point
            { #3, #4/2 } % line's second point
            { #3/2, #4/2 } { \l__circledtext_char_box_radius_dim*#5 }
            {1}  % index of intersect
        }
      \draw_path_lineto:n
        {
          \draw_point_intersect_line_circle:nnnnn
            { #1, #4/2 } % line's first point
            { #3, #4/2 } % line's second point
            { #3/2, #4/2 } { \l__circledtext_char_box_radius_dim*#5 }
            {2}  % index of intersect
        }
      \draw_path_use_clear:n { stroke }
    \draw_scope_end:
  }

% ������������������������������������
\__circledtext_new_char_box_construct:nn { o }
  {
    \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
  }

% ������������������������������������������������������������
\clist_map_variable:nNn {o+,+o} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ������������������������������������������������������������
\clist_map_variable:nNn {ox,xo} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innerdcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������������������������������������������������
\clist_map_variable:nNn {ox+,o+x,xo+,x+o,+ox,+xo} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn  \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innerdcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������������������������
\clist_map_variable:nNn {x+,+x} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __innerdcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ������������������������������������
\__circledtext_new_char_box_construct:nn { O }
  {
    \__circledtext_char_box_type_c:n { __filledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
  }

% ������������������������������������������������������������
\clist_map_variable:nNn {O+,+O} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __filledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ������������������������������������������������������������
\clist_map_variable:nNn {OX,XO} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __filledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __dcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������������������������������������������������
\clist_map_variable:nNn {OX+,O+X,XO+,X+O,+OX,+XO} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __filledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __dcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������������������������
\clist_map_variable:nNn {X+,+X} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __dcross } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������82%���������������
\__circledtext_new_char_box_construct:nn { oo }
  {
    \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {0.82} {0.82}
  }

% ������������������������������������
\clist_map_variable:nNn {Oo,oO} \l_tmpa_tl
  {
    \__circledtext_new_char_box_construct:Vn \l_tmpa_tl
      {
        \__circledtext_char_box_type_c:n { __negfilledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __innerfilledcircle } {#1} {#2} {#3} {#4} {#5} {#6}
        \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
      }
  }

% ���������������������82%���������������
\__circledtext_new_char_box_construct:nn { OO }
  {
    \__circledtext_char_box_type_c:n { __filledsquare } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {0.82} {0.82}
  }

% 3���������������(������������������������������)
\__circledtext_new_char_box_construct:nn { ooo }
  {
    % ���������������������������
    \__circledtext_char_box_type_c:n { __outershadowfilledcircle }
                                       {#1} {#2} {#3} {#4} {1.0} {1.0}
    % ���������������������������������
    \__circledtext_char_box_type_c:n { __outerlowerfilledcircle }
                                     {#1} {#2} {#3} {#4} {#5} {#6}
    % ������������������������������
    \__circledtext_char_box_type_c:n { __outerupperfilledcircle }
                                     {#1} {#2} {#3} {#4} {0.88} {0.88}
    % ���������������������������������
    \__circledtext_char_box_type_c:n { __outerlowerfilledcircle }
                                     {#1} {#2} {#3} {#4} {0.72} {0.72}
    % ���������������������
    \__circledtext_char_box_type_c:n { __outercirclebox }
                                     {#1} {#2} {#3} {#4} {0.88} {0.88}
    \__circledtext_char_box_type_c:n { __outercirclebox }
                                     {#1} {#2} {#3} {#4} {0.72} {0.72}

  }

% 3���������������������
\__circledtext_new_char_box_construct:nn { OOO }
  {
    % ���������������������������
    \__circledtext_char_box_type_c:n { __shadowfilledsquare }
                                       {#1} {#2} {#3} {#4} {1.0} {1.0}
    % ���������������������������������
    \__circledtext_char_box_type_c:n { __lowerfilledsquare}
                                     {#1} {#2} {#3} {#4} {#5} {#6}
    % ���������������������������������
    \__circledtext_char_box_type_c:n { __upperfilledsquare}
                                     {#1} {#2} {#3} {#4} {0.88} {0.88}
    % ���������������������������������
    \__circledtext_char_box_type_c:n { __lowerfilledsquare}
                                     {#1} {#2} {#3} {#4} {0.72} {0.72}
    % ���������������������
    \__circledtext_char_box_type_c:n { __squarebox }
                                     {#1} {#2} {#3} {#4} {0.88} {0.88}
    \__circledtext_char_box_type_c:n { __squarebox }
                                     {#1} {#2} {#3} {#4} {0.72} {0.72}

  }

\msg_new:nnn { circledtext } { box-exists } { The~ box~ type~ `#1~ not~ exists. }

% ������������������������������
\cs_new_nopar:Npn \__circledtext_resize:n #1
  {
    __circledtext_processor_resize_ #1 :w
  }

% ������������������������������������������
\cs_new_nopar:Npn \__circledtext_resize_c:n #1
  {
    \use:c
      {
        __circledtext_processor_resize_ #1 :w
      }
  }

% ������������������������
% #1 dim������������1
% #2 dim������������2
% #3 ������������1
% #4 ������������2
% #5 ������������3
% ������ #1 > 0           ������#3������
% ������ #1 <= 0 ���#2 > 0 ������#4������
% ������ #1 <= 0 ���#2 <= 0������#5������
\cs_new:Npn \__circledtext_dim_gezero_dispatch:NNnnn #1#2 #3#4#5
  {
    \dim_compare:nNnTF #1 > \c_zero_dim
      { #3 }
      {
        \dim_compare:nNnTF #2 > \c_zero_dim
          { #4 } { #5 }
      }
  }

% ������������������������
% #1 dim������������1
% #2 dim������������2
% #3 ������������1
% #4 ������������2
% #5 ������������3
% #6 ������������4
% ������ #1 > 0  ��� #2 > 0 ������#3������
% ������ #1 > 0  ��� #2 <= 0������#4������
% ������ #1 <= 0 ��� #2 > 0 ������#5������
% ������ #1 <= 0 ��� #2 <= 0������#6������
\cs_new:Npn \__circledtext_dim_gezero_dispatch:NNnnnn #1#2 #3#4#5#6
  {
    \dim_compare:nNnTF #1 > \c_zero_dim
      {
        \dim_compare:nNnTF #2 > \c_zero_dim
          { #3 } { #4 }
      }
      {
        \dim_compare:nNnTF #2 > \c_zero_dim
          { #5 } { #6 }
      }
  }

% ������������������������(���������������������������������������������)
\cs_new:Npn \__circledtext_force_size_dispatch:nnn % height, width, none
  {
    \__circledtext_dim_gezero_dispatch:NNnnn \l__circledtext_height_dim
      \l__circledtext_width_dim
  }

% ������������������������(������������������������������������������������������)
\cs_new:Npn \__circledtext_force_size_dispatch:nnnn % both, height, width, none
  {
    \__circledtext_dim_gezero_dispatch:NNnnnn \l__circledtext_box_height_dim
      \l__circledtext_box_width_dim
  }

% ������������������
\cs_new:Npn \__circledtext_new_resize_method:nn #1
  {
    \clist_put_right:Nn \g__circledtext_resize_method_clist {#1}
    \cs_new:cpn { \__circledtext_resize:n {#1} }
  }

% ���������
\__circledtext_new_resize_method:nn { none } { }

% ���������������������
\__circledtext_new_resize_method:nn { real }
  {
    \__circledtext_force_size_dispatch:nnnn
      {% ������������������
        \coffin_resize:Nnn \l__circledtext_box_coffin
                           \l__circledtext_box_width_dim
                           \l__circledtext_box_height_dim
      }
      {% ���������������������������
        \coffin_scale:Nnn \l__circledtext_box_coffin
          {
            \dim_ratio:nn { \l__circledtext_box_height_dim }
                          { \__circledtext_coffin_ht_plus_dp:N \l__circledtext_box_coffin }
          }
          {
            \dim_ratio:nn { \l__circledtext_box_height_dim }
                          { \__circledtext_coffin_ht_plus_dp:N \l__circledtext_box_coffin }
          }
      }
      {% ���������������������������
        \coffin_scale:Nnn \l__circledtext_box_coffin
          {
            \dim_ratio:nn { \l__circledtext_box_width_dim }
                          { \coffin_wd:N \l__circledtext_box_coffin }
          }
          {
            \dim_ratio:nn { \l__circledtext_box_width_dim }
                          { \coffin_wd:N \l__circledtext_box_coffin }
          }
      }
      {% ���������������������������
        \coffin_scale:Nnn \l__circledtext_box_coffin
                          { \l__circledtext_x_scale_tl }
                          { \l__circledtext_y_scale_tl }
      }
  }

% ������������������������
\__circledtext_new_resize_method:nn { base }
  {
    \__circledtext_force_size_dispatch:nnnn
      {% ���������������������������
        \coffin_resize:Nnn \l__circledtext_box_coffin
                           \l__circledtext_box_width_dim
                           \l__circledtext_box_height_dim
      }
      {% ���������������������������������������
        \coffin_resize:Nnn \l__circledtext_box_coffin
           {
             \l__circledtext_char_width_dim * \dim_ratio:nn { \l__circledtext_box_height_dim }
               { \__circledtext_coffin_ht_plus_dp:N \l__circledtext_box_coffin }
           }
           {
             \l__circledtext_box_height_dim
           }
      }
      {% ���������������������������������������
        \coffin_resize:Nnn \l__circledtext_box_coffin
           {
             \l__circledtext_box_width_dim
           }
           {
             \l__circledtext_char_height_dim * \dim_ratio:nn { \l__circledtext_box_width_dim }
               { \coffin_wd:N \l__circledtext_box_coffin }
           }
      }
      {% ���������������������������������
        \coffin_resize:Nnn \l__circledtext_box_coffin
           {
             \l__circledtext_x_scale_tl \l__circledtext_char_width_dim
           }
           {
             \l__circledtext_y_scale_tl \l__circledtext_char_height_dim
           }
      }
  }
\msg_new:nnn { circledtext } { resize-type } { using~ `#1'~ resize. }

% ������������������
\cs_new:Npn \__circledtext_chars_stroke:nn #1#2
  {
    \special { pdf:code ~ q ~ #1 } #2 \special { pdf:code ~ Q }
  }
% ������������������
\cs_new_protected:Npn \__circledtext_chars_stroke_construct:n #1
  {
    \int_case:nn {\l__circledtext_charstroke_type_int}
      {
        {1}{ #1 }
        {2}{
          \__circledtext_chars_stroke:nn { 1 ~ Tr ~ 0.10 ~ w ~ [] ~ 0 ~ d ~ 1 ~ J } {#1}
        }
        {3}{
          \__circledtext_chars_stroke:nn { 1 ~ Tr ~ 0.10 ~ w ~ [1~1] ~ 0 ~ d ~ 1 ~ J } {#1}
        }
        {4}{
          \__circledtext_chars_stroke:nn { 3 ~ Tr } {#1}
        }

      }
  }
\cs_generate_variant:Nn  \__circledtext_chars_stroke_construct:n { V }
\cs_generate_variant:Nn  \__circledtext_chars_stroke_construct:n { x }

% key_value������������
\keys_define:nn { circledtext }
  {
    % ���������
    basechar  .code:n = { \tl_gset:Nx \c__circledtext_basechar_tl {#1}
                          \__circledtext_calc_basechar_w_h:
                        },
    % ������������
    charf .code:n = { \tl_gset:Nn \l__circledtext_character_format_tl {#1}
                      \__circledtext_calc_char_box_size:
                    },
    % charf .initial:n = \normalsize ,
    % ������������
    boxtype .code:n = { \exp_args:NNx \clist_if_in:NnTF
                            \g__circledtext_char_box_list_clist {#1}
                            { \tl_set:Nx \l__circledtext_char_box_type_tl {#1} }
                            { \msg_error:nnx { circledtext } { box-exists } {#1} }
                        % \__circledtext_calc_char_box_size:
                      },
    % ������������
    resize    .code:n = { \exp_args:NNx \clist_if_in:NnTF
                            \g__circledtext_resize_method_clist {#1}
                            { \tl_set:Nx \l__circledtext_resize_method_tl {#1} }
                            { \msg_error:nnx { circledtext } { resize-method } {#1} }
                        },
    % ������������
    xscale .tl_set:N = \l__circledtext_x_scale_tl ,
    xscale .initial:n = 1 ,
    yscale .tl_set:N = \l__circledtext_y_scale_tl ,
    yscale .initial:n = 1 ,
    scale  .meta:n = { xscale = #1 , yscale = #1 } ,
    width  .dim_set:N = \l__circledtext_box_width_dim ,
    height .dim_set:N = \l__circledtext_box_height_dim ,

    % ������������������
    boxlinewidth .dim_set:N = \l__circledtext_box_linewidth_dim ,
    boxlinewidth .initial:n = 0.4pt ,
    % ���������������
    crosslinewidth .dim_set:N = \l__circledtext_cross_linewidth_dim ,
    crosslinewidth .initial:n = 0.3pt ,

    % ���������������
    boxcolor  .code:n = { \tl_set:Nx \l_tmpa_tl {
                          #1 ! \int_use:N \l__circledtext_cross_color_ratio_int }
                          \__circledtext_color_select:nn { circledtextcharboxcolor } {#1}
                          \__circledtext_color_select:nx { circledtextcrosscolor }
                                                         { \l_tmpa_tl }
                        } ,
    boxcolor  .initial:n = black ,
    boxcolor* .code:n = { \tl_set:Nx \l_tmpa_tl {
                          #1 ! \int_use:N \l__circledtext_cross_color_ratio_int }
                          \__circledtext_color_select:nnn { circledtextcharboxcolor } #1
                          \__circledtext_color_select:nnx { circledtextcrosscolor }
                                                          \l_tmpa_tl
                        } ,
    % ���������������������������������������(���������boxcolor���)
    crosscolorratio  .code:n = { \int_set:Nn \l__circledtext_cross_color_ratio_int { #1 }
                                 \__circledtext_color_select:nn { circledtextcrosscolor }
                                   { circledtextcharboxcolor ! #1 }
                               },
    crosscolorratio  .initial:n = 30,

    % ������������
    charcolor  .code:n = { \__circledtext_color_select:nn { circledtextcharcolor } {#1}
                           \tl_if_eq:nnT { #1 } { black }
                             {
                               \__circledtext_color_select:nn { circledtextboxfill }
                                                          { white }
                               \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                  { \color_fill:n { white } }
                             }
                         } ,
    charcolor  .initial:n = black ,
    charcolor* .code:n = { \__circledtext_color_select:nnn { circledtextcharcolor } #1 } ,
    color  .meta:n = { boxcolor = #1, crosscolor = #1,  charcolor = #1 } ,
    color* .meta:n = { boxcolor* = #1, crosscolor = #1,  charcolor* = #1 } ,
    % ������������������������������
    boxfill  .code:n = { \exp_args:Nx \tl_if_empty:nTF {#1}
                           {
                             \__circledtext_color_select:nn { circledtextboxfill }
                                                        { white }
                             \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                { \color_fill:n { white } }
                           }{ \__circledtext_color_select:nn { circledtextboxfill } {#1}
                              \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                { \color_fill:n {#1} }
                           }
                        } ,
    boxfill  .initial:n = {} ,
    boxfill* .code:n = { \__circledtext_color_select:nnn { circledtextboxfill } #1
                           \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                             { \color_fill:nn #1 }
                         } ,

    % ������������������������������������������
    lowerbgcolor  .code:n = { \exp_args:Nx \tl_if_empty:nTF {#1}
                                {
                                  \__circledtext_color_select:nn { lowerbgboxfill }
                                    { yellow!70!red }
                                  \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                    { \color_fill:n { yellow!70!red } }
                                }{ \__circledtext_color_select:nn { lowerbgboxfill } { #1 }
                                  \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                    { \color_fill:n { #1 } }
                                }
                            } ,
    lowerbgcolor  .initial:n = {} ,

    % ������������������������������������������
    upperbgcolor  .code:n = { \exp_args:Nx \tl_if_empty:nTF {#1}
                                {
                                  \__circledtext_color_select:nn { upperbgboxfill }
                                    { yellow!80!black }
                                  \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                    { \color_fill:n { yellow!80!black } }
                                }{ \__circledtext_color_select:nn { upperbgboxfill } { #1 }
                                  \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                    { \color_fill:n { #1 } }
                                }
                            } ,
    upperbgcolor  .initial:n = {} ,

    % ������������������������
    shadowcolor  .code:n = { \exp_args:Nx \tl_if_empty:nTF {#1}
                               {
                                 \__circledtext_color_select:nn { shadowboxfill }
                                   { black!35!white }
                                 \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                   { \color_fill:n { black!35!white } }
                               }{ \__circledtext_color_select:nn { shadowboxfill } { #1 }
                                 \cs_set_nopar:Npn \__circledtext_aux_color_boxfill:
                                   { \color_fill:n { #1 } }
                               }
                            } ,
    shadowcolor  .initial:n = {} ,

    % ������������
    charstroke .choice:,
    charstroke .value_required:n = true,
    charstroke .choices:nn =
      { none, solid, dashed, invisible }
      { \int_set_eq:NN \l__circledtext_charstroke_type_int \l_keys_choice_int },
    charstroke .initial:n = none,
    % ������������
    dashpattern .tl_set:N = \l__circledtext_dash_pattern_tl ,
    dashpattern .initial:n = { } ,

    % ������������������������
    charshrink  .fp_set:N  = \l__circledtext_char_shrink_fp ,
    charshrink  .initial:n = 0.75,

    unknown .code:n = { \msg_error:nn { circledtext } { unknown-option } }
  }
\msg_new:nnn { circledtext } { unknown-option }
  { package~ option~ "\l_keys_key_tl"~ is~ unknown. }

% ���������������������������������
\sys_if_engine_xetex:TF
  {
    \keys_set:nn { circledtext }
      {
        basechar = ���,
      }
  }{
    \sys_if_engine_luatex:TF
      {
        \keys_set:nn { circledtext }
          {
            basechar = ���,
          }
      }{
        \keys_set:nn { circledtext }
          {
            basechar = x,
          }
      }
  }

\keys_set:nn { circledtext }
  {
    charf    = \normalsize,
    boxtype  = o,
    resize   = none,
  }

% ������������������������
\NewDocumentCommand \circledtextset { m }
  {
    \keys_set:nn { circledtext } {#1}
  }

% ������������������������������������������������������������
% #1 ���������������������(���)
\cs_new:Npn \__circledtext_single_char_construct:N #1
  {
    \tl_if_empty:NTF #1
      {
        \hcoffin_set:Nn \l__circledtext_char_coffin
          {
            \__circledtext_single_handle:N \c__circledtext_basechar_tl
          }
      }{
        \hcoffin_set:Nn \l__circledtext_char_coffin
          {
            \__circledtext_single_handle:N #1
          }
      }
  }

% ������������������������������
% #1 ������������
% #2 ���������������������(���)
\cs_new:Npn \__circledtext_handle:nn #1#2
  {
    \group_begin:
      % ������������
      \keys_set:nn { circledtext } { #1 }

      % ���������������
      \tl_gset:Nx \l__circledtext_chars_tl {#2}

      % ������������
      \__circledtext_single_char_construct:N \l__circledtext_chars_tl

      % ���������������������������������
      \coffin_set_eq:NN \l__circledtext_box_coffin \l__circledtext_char_coffin

      % ���������������������������(������������������������)
      \dim_set:Nn \l_tmpa_dim
        { \__circledtext_coffin_ht_plus_dp:N \l__circledtext_box_coffin }
      % ���������������������������������������
      \coffin_scale:Nnn \l__circledtext_box_coffin
        {
          \dim_ratio:nn { \charboxht } { \l_tmpa_dim }
        }
        {
          \dim_ratio:nn { \charboxht } { \l_tmpa_dim }
        }

      % ���������������������������������
      \__circledtext_resize_c:n { \l__circledtext_resize_method_tl }

      % ������������=������������+������������
      \dim_add:Nn \l__circledtext_char_dp_dim { \l__circledtext_box_linewidth_dim }

      % ������������(������������������)
      \coffin_typeset:Nnnnn \l__circledtext_box_coffin
        { l } { b } { 0pt } { -\l__circledtext_char_dp_dim }
    \group_end:
  }

% ������������������������
% #1 ���������������������(���)������
\cs_new:Npn \__circledtext_single_handle:N #1
  {
    \group_begin:
      \tl_set:Nf \l__circledtext_curr_char_tl {#1}
      \__circledtext_single_construct:N \l__circledtext_curr_char_tl
    \group_end:
  }

% ������������������
% #1 ���������������������(���)������
\cs_new:Npn \__circledtext_single_construct:N #1
  {
    % ���������������������������������������������������������
    \hcoffin_set:Nn \l__circledtext_box_coffin
      {
        \bool_if:NTF \l__circledtext_negative_bool
          {
            \color_select:n { circledtextboxfill   }
          }{
            \color_select:n { circledtextcharcolor }
          }
        \tl_use:N \l__circledtext_character_format_tl
        \__circledtext_chars_stroke_construct:n { #1 }
      }

    % ������������������������������
    \__circledtext_single_box_construct:
  }

% ���������������������������������������������������
\cs_new:Npn \__circledtext_single_box_construct:
  {
    % ������������������������������������
    \dim_set:Nn \l_tmpa_dim
      { \coffin_wd:N \l__circledtext_box_coffin }
    \dim_set:Nn \l_tmpb_dim
      { \__circledtext_coffin_ht_plus_dp:N \l__circledtext_box_coffin }
    \dim_compare:nNnF \l_tmpa_dim > \l_tmpb_dim
      {
        \dim_set_eq:NN \l_tmpa_dim \l_tmpb_dim
      }

    % ������������������������
    \coffin_scale:Nnn \l__circledtext_box_coffin
      {
        \dim_ratio:nn { \charboxwd } { \l_tmpa_dim }
      }
      {
        \dim_ratio:nn { \charboxht } { \l_tmpa_dim }
      }

    % ������������������������������������������������������
    \coffin_scale:Nnn \l__circledtext_box_coffin
      { \l__circledtext_char_shrink_fp }
      { \l__circledtext_char_shrink_fp }

    % ���������������������������
    \draw_begin:
      \draw_linewidth:n { \l__circledtext_box_linewidth_dim }

      \draw_path_scope_begin:
        \__circledtext_char_box_type_c:n { \l__circledtext_char_box_type_tl }
          { 0 } { 0 } { \charboxwd } { \charboxht } { 1.0 } { 1.0 }
        \draw_transform_shift:n {\charboxwd / 2.0, \charboxht / 2.0 }
        \draw_coffin_use:Nnn \l__circledtext_box_coffin { hc } { vc }
      \draw_path_scope_end:
    \draw_end:
  }

\endinput