summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/circledtext/circledtext.sty
blob: f70b7168141bbf8edd3b3e78e90505220681a2e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
%%     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}{2022-04-28}{v1.1.0}
  {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_gset_eq:NN \l__circledtext_char_box_size_dim \l_tmpa_dim
      }
      {
        \dim_gset_eq:NN \l__circledtext_char_box_size_dim \l_tmpb_dim
      }

    % 设置基字符盒子正方形宽和高(相等)
    \dim_gset_eq:NN \charboxwd \l__circledtext_char_box_size_dim
    \dim_gset_eq:NN \charboxht \l__circledtext_char_box_size_dim

    % 外接圆半径
    \dim_gset: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} }
  }

% 定义字符边框盒子类型

% 无边框
\__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 { __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}
  }

% 填充内切圆叠加正方形正十字线与内切圆边框
\__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 { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __innercirclebox } {#1} {#2} {#3} {#4} {#5} {#6}
  }

% 填充内切圆叠加内切圆斜十字线与内切圆边框
\__circledtext_new_char_box_construct:nn { ox }
  {
    \__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}
  }

% 填充内切圆叠加内切圆正斜十字线与内切圆边框
\__circledtext_new_char_box_construct:nn { ox+ }
  {
    \__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}
  }

% 内切圆斜十字线叠加正十字线
\__circledtext_new_char_box_construct:nn { x+ }
  {
    \__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}
  }

% 填充正方形叠加正方形正十字线与正方形边框
\__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 { __scross } {#1} {#2} {#3} {#4} {#5} {#6}
    \__circledtext_char_box_type_c:n { __squarebox } {#1} {#2} {#3} {#4} {#5} {#6}
  }

% 填充正方形叠加正方形斜十字线与正方形边框
\__circledtext_new_char_box_construct:nn { OX }
  {
    \__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}
  }

% 填充正方形叠加正方形斜正十字线与正方形边框
\__circledtext_new_char_box_construct:nn { OX+ }
  {
    \__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}
  }

% 正方形斜十字线叠加正十字线
\__circledtext_new_char_box_construct:nn { X+ }
  {
    \__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}
  }

% 反色正方形叠加内切填充圆
\__circledtext_new_char_box_construct:nn { Oo }
  {
    \__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}
  }
\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 }
                         } ,
    % 笔画参数
    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