summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/mcf2graph/mcf2graph.mf
blob: 668964031b39072b6d6d90f7191d85ef604b90e1 (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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mcf2graph ver 4.03     Copyright (c) 2013-2016   Akira Yamaji
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software
% and associated documentation files (the "Software"), to deal in the Software without restriction,
% including without limitation the rights to use, copy, modify, merge, publish, distribute,
% sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies
% or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,
% INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE
% AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  mcf2graph is METAFONT/METAPOST macro package convert
%  Molecular Coding Format(MCF) to font(pk)/eps/sgv/png/mdl molfile(V2000)
%----------------------------------------------------------------------------------------------
% This package is located at : http://www.ctan.org/pkg/mcf2graph
% Suggestion or request mail to : mcf2graph@gmail.com 
%----------------------------------------------------------------------------------------------
% Set output no image file        : mpost -s ahangle=0  FILENAME
% Set outputformat to "png"       : mpost -s ahangle=1  FILENAME
% Set outputformat to "svg"       : mpost -s ahangle=2  FILENAME
% Set outputformat to MDL Molfile : mpost -s ahangle=3  FILENAME
% Set first font only (for test)  : mpost -s ahlength=1 FILENAME
% Set output checklist            : mpost -s ahlength=2 FILENAME
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tracingstats:=1;
message " This is mcf2graph ver 4.0  2016.11";
%-------------------------------------------------------------------------------------------------
newinternal nA,nB,nC,nD,nE,nF,def_num,def_int,tbl_cnt;
numeric save_para[],comD[][],parD[][],cntD[],tbl_str_wd[],tbl_atom[],tbl_subst[][],tbl_atom_wt[],
        tbl_char_wd[],tbl_char_ht[],tbl_char_dp[];
string  tbl_atom_str[],strD[],var[],tag[],mp_log_name,aux_delimiter;
picture atom_font[],chem_structure;
%-------------------------------------------------------------------------------------------------
char_num:=str_cnt:=proc_end:=0;
sw_numberA:=sw_numberB:=sw_mol_frame:=sw_auxout:=sw_solid:=sw_expand:=sw_font_frame:=0;
sw_atom_frame:=sw_checklist:=sw_MOLout:=sw_subst_off:=sw_bond_single:=sw_auxfix:=sw_clip:=0;
numberA_start:=numberB_start:=1; numberA_end:=numberB_end:=4095; aux_max:=max_inf_num:=20;
aux_delimiter:=";";  var1:="jobname";  tag1:="F";  var2:="char_num"; tag2:="C";
for i=3 upto aux_max: var[i]:=tag[i]:=""; endfor
%=================================================================================================
if (known green)and(known ahlength):
  f_MP:=1;
  color color_list[];
  mp_log_name:=jobname&"-info.aux";
  if     ahlength=1: proc_end:=1;  fi                          % first molecular only
  if     ahlength=2: sw_checklist:=1; def shipit = enddef; fi  % checklist on
  if     ahangle=0: def shipit = enddef;                       % No image file
  elseif ahangle=1: outputformat:="png"; hppp:=vppp:=0.1;      % PNG format
  elseif ahangle=2: outputformat:="svg";                       % SVG format
  elseif ahangle=3: sw_MOLout:=1; def shipit = enddef;         % MDL Molefile on
  fi
  outputtemplate:="%j-%3c."&outputformat;
  def beginchar(expr a,b,c,d)= beginfig(a) w:=b*pt; h:=c*pt; enddef;
  def endchar = endfig enddef;
  def printf expr s= write s to mp_log_name enddef;
  def # = enddef;
  def Cp(expr s) = if known s: if s<>0: withcolor color_list[s] fi fi enddef;
  def define_pixels(text s) = enddef;
else: f_MP:=0;
  def Cp(expr s) = enddef; def color = transform enddef;
  mode_setup; def printf expr s= message s enddef;
fi
ahangle:=45; ahlength:=4;
clearit;  chem_structure:=currentpicture;
%--------------------------------------------------------------------------------------------------
let DIV= /; let MUL= *; let LT= <; let GT= >; let AND= &; let :: = : ; let == = =;
%--------------------------------------------------------------------------------------------------
?3:=?20:=Ph:=Ph1:=Ph2:=hz:=0; vt:=1;
margin_left_right:=margin_top_bottom:=0.4mm;             ratio_bond_width:=0.1;
ratio_chain_ring:= 0.66;      ratio_atom_bond:=0.36;     ratio_thickness_bond:=0.015;  
ratio_char_bond:=1.5;         ratio_bondgap_bond:=0.15;  ratio_zebragap_bond:=0.12;
ratio_zebra_black:=0.4;       ratio_wedge_bond:=0.12;    ratio_atomgap_atom:=0.050;
offset_thickness#:=0.2pt#;    offset_bond_gap#:=0.3pt#;  offset_zebra_gap#:=0.1pt#;
offset_atom#:=0.8pt#;         offset_wedge#:=0.4pt#;     thickness_font_frame#:=0.2pt#;
max_bond_length#:=10mm;    bond_len#:=8mm#;    font_wd#:=30mm#;    font_ht#:=20mm#;
%--------------------------------------------------------------------------------------------------
def_emb_start:=500;      % 500  => 2499   for embedded definition    (max 2000)
def_usr_start:=2500;     % 2500 => 2999   for user     definition    (max 500)
def_int_start:=3000;     % 3000 => 4000   for internal definition    (max 1000)
%--------------------------------------------------------------------------------------------------
def def_com(expr n)(text tx)= nA:=n; forsuffixes list=tx:: list:=nA; nA:=nA+1; endfor enddef;
def_com(-4000)(_term,_jp_atom,_jp_atom_abs,_jp_bond,_cyc,_cyc_sB,_cyc_eB,_set_line,_chg_line,
  _dl,_N,_O,_S,_H,_tmp_line,_chg_len,_get_len,_ring_len,_tmp_len,_rot_ang,_set_ang,_adj_ang,
  _chg_env,_tmp_env,_set_colorA,_set_colorB,_postA,_postB,_postC,_postD,_postE,_postF,_set_psA,
  _stack_reset,_set_clr,_set_adr,_mk_bond,_set_atom,_arg_ang,_chg_atom,_tmp_rot,_fuse,_push,
  _pop,_size_a);
def_com(1)(si,dl,dr,dm,tm,wf,wb,bd,bz,zf,zb,dt,no,ar,vf,vb,wv,nl,si_,wf_,wb_,bd_);
%--------------------------------------------------------------------------------------------------
def parameter_list=
  sw_checklist,sw_numberA,sw_numberB,sw_mol_frame,sw_auxout,sw_solid,sw_expand,sw_MOLout,
  sw_atom_frame,sw_font_frame,sw_subst_off,sw_bond_single,sw_clip,
  margin_left_right,margin_top_bottom,ratio_atom_bond,ratio_thickness_bond,ratio_char_bond,
  ratio_chain_ring,max_bond_width,ratio_bondgap_bond,ratio_zebra_black,ratio_zebragap_bond,
  ratio_wedge_bond,ratio_atomgap_atom,ratio_bond_width,font_wd#,font_ht#,
  bond_len#,offset_atom#,offset_wedge#,max_bond_length#,offset_zebra_gap#,offset_bond_gap#,
  thickness_font_frame#,offset_thickness#,numberA_start,numberA_end,numberB_start,numberB_end
enddef;
%--------------------------------------------------------------------------------------------------
vardef pic_c(expr i,s)= substring(i,i+1) of s enddef;
%--------------------------------------------------------------------------------------------------
nA:=0; for list=parameter_list: save_para[incr nA]:=list; endfor
def store_par(text t)=
  nA:=0; for list=t: if save_para[incr nA]<>list: save_para[nA]:=list; fi endfor enddef;
def restore_par(text t)=  nA:=0;
  forsuffixes list=t: if list<>save_para[incr nA]: list:=save_para[nA]; fi endfor enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def beginfont(text s)=
  store_par(parameter_list);
  begingroup
  save cntA,cntB,cntM,minX,minY,f_beginchar,numS,
       posB,posA,posM,lineB,sB,eB,angB,angA,lenB,ang_br,info;
  numeric lineB[],sB[],eB[],angB[],angA[],lenB[],ang_br[],numS[];
  pair posB[],posA[],posM[];
  string info[];
  %------------------------------------------------------------------------------------------------
  for i:=1 upto max_inf_num: info[i]:=":"; endfor
  f_beginchar:=inf_num:=cntM:=0;
  for list=s: info[incr inf_num]:=list; endfor
  char_num:=char_num+1;
enddef;
%==================================================================================================
def endfont=
  if (sw_font_frame=1)or(sw_font_frame=3): EXT(draw_frame((0,0),w,h,thickness_font_frame);) fi
  if (sw_font_frame=2)or(sw_font_frame=3): EXT(draw_frame(p0,wd,ht,thickness_font_frame);) fi
  if sw_font_frame=4: EXT(draw_corner(w,h,thickness_font_frame);) fi
  if (sw_auxout=1)or(sw_checklist=1)or(sw_MOLout=1): proc_calc_out; fi
  currentpicture:=chem_structure;
  chem_structure:=nullpicture;
  endchar;
  clearit;
  endgroup;
  restore_par(parameter_list);
  if proc_end=1: scantokens("bye"); fi
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def set_def_MC=
  save /,*,//,/*,*/,**,~,~~,^,^^,`,'`,{,},<,>,>>,:>,&,&#,:,=,\,\\,*\,\*,@,$,#,|,||,
       d,w,z,inside_def_MC;
  inside_def_MC:=1; d:=dl; w:=wf; z:=zf;
  vardef $(text a)tertiary b == $$(a)(b) enddef; tertiarydef a=b == $$(a)(b) enddef; 
  vardef @(text a)tertiary b == @@(a)(b) enddef; tertiarydef a:b == @@(a)(b) enddef;
  vardef #primary a == if numeric a:: a-4095 elseif pair a:: a+(0,-4095) fi enddef;
  def &primary n ==(_cyc,n) enddef; def &# == & # enddef;
  def ^^primary n == (_tmp_rot,n) enddef; def ~~primary n == (_tmp_line,n) enddef;
  def '`primary n == (_tmp_len,n) enddef; def :>primary n == (_tmp_env,n) enddef;
  def >>primary n == (_chg_env,n) enddef; def ``primary n == (_chg_len,n) enddef;
  def <primary n  == (_rot_ang,n) enddef;
  tertiarydef a^b == ^^b,a enddef;  tertiarydef a`b == '`b,a enddef;
  tertiarydef a~b == ~~b,a enddef;  tertiarydef a>b == :>b,a enddef;
  def * == (0,1) enddef;  def \ == *,0 enddef;  def \\ == *,0~dm enddef;
  def *\ == *,0~wf enddef;  def \* == *,0~zf enddef;
  def {  == (_push,1) enddef; def }  == (_pop,1)  enddef;
  def {{ == (_push,0) enddef; def }} == (_pop,0)  enddef;
  def | == {,} enddef; def || == (_stack_reset,0) enddef;
  def /secondary n ==  (_postA,n) enddef; def //secondary n  == (_postB,n) enddef;
  def */secondary n == (_postC,n) enddef; def /*secondary n  == (_postD,n) enddef;
  def **secondary n == (_postE,n) enddef; def */*secondary n == (_postF,n) enddef;
enddef;
%=================================================================================================
vardef '(text s)= ''(incr def_num)(s); def_num enddef;
%-------------------------------------------------------------------------------------------------
def ''(expr k)(text s)=
  begingroup
  if unknown inside_def_MC:: set_def_MC fi
  cntD[k]:=0;
  for list==s::
    if known list::
      if pair list:: comD[k][incr cntD[k]]:=xpart(list); parD[k][cntD[k]]:=ypart(list);
      elseif numeric list::
        if list>=def_emb_start::
          for i==1 upto cntD[list]::
             comD[k][incr cntD[k]]:=comD[list][i]; parD[k][cntD[k]]:=parD[list][i];
            endfor 
        else:: comD[k][incr cntD[k]]:=_mk_bond; parD[k][cntD[k]]:=list; fi
      elseif string list:: strD[incr str_cnt]:=list;
        comD[k][incr cntD[k]]:=_set_atom; parD[k][cntD[k]]:=str_cnt;
        if pic_c(0,list)=="{":: tbl_str_wd[str_cnt]:=1.6; else:: tbl_str_wd[str_cnt]:=1; fi
      fi
    fi
  endfor
  endgroup
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def !?(expr a)(expr b) =
  PA(_jp_bond,a) PA(_get_len,a) PA(_push,0)
  PA(_chg_len,_ring_len) for i==1 upto b-2:: PA(_mk_bond,360 DIV b) endfor  PA(_pop,0)
  if a>=1:: PA(_cyc_eB,0) elseif a>=-999:: PA(_cyc_eB,a-b+2) else:: PA(_cyc_eB,a) fi
enddef;
%-------------------------------------------------------------------------------------------------
def !!?(expr a)(expr b)(expr c) =
  PA(_jp_bond,xpart(a)) PA(_push,0)
  if     b==6:: PA(_chg_len,0) for i==1 upto c-1:: PA(_mk_bond,60) endfor
  elseif b==5:: if     c==2:: PA(_chg_len,1.25) PA(_mk_bond,80)
                elseif c==3:: PA(_chg_len,1.1)  PA(_mk_bond,78) PA(_mk_bond,72) fi
  elseif b==4:: PA(_chg_len,1.225) PA(_mk_bond,105) fi
  PA(_pop,0) if ypart(a)<=0:: PA(_cyc_eB,ypart(a)-c+1) else:: PA(_cyc_eB,ypart(a)) fi
enddef;
%-------------------------------------------------------------------------------------------------
def !??(expr a)(expr b)(expr c) =
  if      b==5::
    PA(_jp_bond,a) PA(_push,0) PA(_chg_len,c DIV 10) PA(_mk_bond,72-((c-9) MUL 1.5))
    PA(_mk_bond,72+(c-9)) PA(_mk_bond,72+(c-9)) PA(_pop,0)
    if a>=1:: PA(_cyc_eB,0) else:: PA(_cyc_eB,list) fi
  elseif b==6::
    PA(_jp_bond,a) PA(_push,0) PA(_chg_len,c DIV 10) PA(_mk_bond,60-(c-8))
    PA(_mk_bond,60+((c-8) DIV 2)) PA(_mk_bond,60+((c-8) DIV 2)) PA(_mk_bond,60+((c-8) DIV 2))
    PA(_pop,0) if a>=1:: PA(_cyc_eB,0) else:: PA(_cyc_eB,list) fi
  elseif b==7::
    PA(_jp_bond,a) PA(_push,0) PA(_chg_len,c DIV 10) PA(_mk_bond,(360 DIV 7)-(c-8))
    PA(_mk_bond,360 DIV 7+((c-8) DIV 2.5)) PA(_mk_bond,360 DIV 7+((c-8) DIV 2.5))
    PA(_mk_bond,360 DIV 7+((c-8) DIV 2.5)) PA(_mk_bond,360 DIV 7+((c-8) DIV 2.5))
    PA(_pop,0) if a>=1:: PA(_cyc_eB,0) else:: PA(_cyc_eB,list) fi
  elseif b==8::
    PA(_jp_bond,a) PA(_push,0) PA(_chg_len,c DIV 10) PA(_mk_bond,45-(c-8))
    PA(_mk_bond,45+((c-8) DIV 3)) PA(_mk_bond,45+((c-8) DIV 3)) PA(_mk_bond,45+((c-8) DIV 3))
    PA(_mk_bond,45+((c-8) DIV 3)) PA(_mk_bond,45+((c-8) DIV 3))
    PA(_pop,0) if a>=1:: PA(_cyc_eB,0) else:: PA(_cyc_eB,list) fi
  fi
enddef;
%==================================================================================================
vardef $$(text t)(expr e) =
  nA:=0; def_int:=def_int+1;
  if known e:: if numeric e::
    if (e>=si)and(e<=bd_):: if e==dl:: for list==t:: PA(_dl,list) endfor
                            else:: for list==t:: PA(_set_adr,list) PA(_set_line,e) endfor fi
    elseif (e>=?3)and(e<=?20):: for list==t:: !?(list)(e-?3+3) endfor
    elseif e==Ph1:: !?(t)(6) PA(_dl,-2) PA(_dl,-4)
    elseif e==Ph2:: !?(t)(6) PA(_dl,-1) PA(_dl,-3) PA(_dl,-5)
    elseif comD[e][1]==_fuse::
      if comD[e][2]<=6:: for list==t:: !!?(list)(parD[e][1])(comD[e][2]) endfor
      elseif (comD[e][2]>=11)and(comD[e][2]<=15)::
        for list==t:: !??(list)(parD[e][1])(comD[e][2]) endfor
      fi
    fi
  elseif color e:: color_list[incr cntC]:=e;
    for list==t:: PA(_set_adr,list) PA(_set_colorB,cntC) endfor
  fi fi
  cntD[def_int]:=nA;
  def_int    %------- Retern value -------
enddef;
%-------------------------------------------------------------------------------------------------
vardef @@(text t)(expr s)=
  nA:=0; def_int:=def_int+1;
  if known s:: if numeric s::
    if (s>=H)and(s<=U)::
       for list==t::
          if     s==N:: PA(_N,list) elseif s==O:: PA(_O,list) elseif s==S:: PA(_S,list)
          else::        PA(_set_adr,list) PA(_chg_atom,s) fi  endfor
    elseif s==NH::  for list==t:: if numeric  list:: PAs((_N,list),~~no,'`_size_a,(_H,list))
                                 elseif pair list:: PAs(list) fi  endfor
    elseif s==SOO:: for list==t:: PAs((_S,list),(_set_adr,list),
                                  ^^35,~~dm,(_set_psA,O),^^-35,~~dm,(_set_psA,O))  endfor
    elseif s==SO::  for list==t::
                      if  numeric list:: PAs((_S,list),(_set_adr,list),~~dm,(_set_psA,O))
                      elseif pair list:: PAs(list) fi  endfor
    fi
  elseif pair s::  nE:=xpart(s); nF:=ypart(s); PA(_set_clr,1)
    for list==t::
      if numeric list::
        if     nE==_postA::            elseif nE==_postB:: PAs(~~dm)
        elseif nE==_postC:: PAs(~~wf)  elseif nE==_postD:: PAs(~~zf)
        elseif nE==_postE:: PAs(~~nl)  elseif nE==_postF:: PAs(~~no) fi
        PA(_set_adr,list)  PA(_set_psA,nF)
      elseif pair list:: PAs(list) fi
    endfor
    PA(_set_clr,0)
    elseif color s:: color_list[incr cntC]:=s;
      for list==t:: PA(_set_adr,list) PA(_set_colorA,cntC) endfor fi fi
  cntD[def_int]:=nA;
  def_int  %------- Retern value -------
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def EXT(text s)=
  begingroup
  save wd,ht,n,p; pair p[]; wd:=w-2margin_left_right; ht:=h-2margin_top_bottom;
  p0:=(margin_left_right,margin_top_bottom);  n:=cntM; for i=1 upto n: p[i]:=posM[i]; endfor
  s addto chem_structure also currentpicture; clearit;
  endgroup;
enddef;
%--------------------------------------------------------------------------------------------------
def ext(text s)=
  begingroup
  save wd,ht,n,a,l,p; numeric a[]; pair p[]; wd:=mol_wd; ht:=mol_ht; l:=bond_len;
  p0:=(minX,minY); n:=cntA; for i=1 upto n: p[i]:=posA[i]; a[i]:=ang_br[i]; endfor
  s addto chem_structure also currentpicture shifted (sftX,sftY); clearit;
  endgroup;
enddef;
%=================================================================================================
def MCf(text s)= MCd(1,1)(.5,.5)(s) enddef;  % MC fill draw
%-------------------------------------------------------------------------------------------------
def MCd(expr RW,RH)(expr FW,FH)(text TX)=
  begingroup
  save com,par,adrA,adrB,f_bra,envT,envB,lenT,lineT,strAT,stkA,stkB,stkL,stkM,cntP,
       bondL,temp_len,temp_cntB,f_end,f_term,rotT,adrS,f_lineT,f_rotT,f_lenT,f_envT,angT,tempB,
       glu,sftX,sftY,xpos,ypos,str_len,max_x,max_y,par_s,par_e,score_u,hat,f_char,lenB,
       ps_adr,ps_com,op_com,op_par,op_cnt,crR,colorA,colorB;
  %-----------------------------------------------------------------------------------------------
  save PA,PAs,PP,PG;
  def PA(expr a,b)= comD[def_int][incr nA]:=a; parD[def_int][nA]:=b; enddef;
  def PAs(text t)= for list==t:: PA(xpart(list),ypart(list)) endfor enddef;
  def PP(expr a,b)= op_com[cntP][incr op_cnt[cntP]]:=a; op_par[cntP][op_cnt[cntP]]:=b; enddef;
  def PG(expr a,b)= comD[0][incr cntD[0]]:=a; parD[0][cntD[0]]:=b; enddef;
  %-----------------------------------------------------------------------------------------------
  numeric stkB[],stkA[],stkL[],stkM[],f_char[],ps_adr[],ps_com[],colorA[],colorB[],
          op_com[][],op_par[][],op_cnt[];
  %-----------------------------------------------------------------------------------------------
  if (sw_numberA>=1)or(sw_numberB>=1): ratio_atom_bond:=0.25; fi
  if (sw_expand=1)or(sw_MOLout=1): expand_set; fi
  def_num:=def_usr_start;
  def_int:=def_int_start;
  par_s=ASCII("{"); par_e=ASCII("}"); score_u=ASCII("_"); hat=ASCII("^");
  cntA:=cntB:=cntP:=cntC:=0; strD[0]:=""; str_cnt:=2000; crR:=-ratio_chain_ring;
  %===============================================================================================
  ''(0)(TX,(_term,0)); proc_bond_atom(1); if (cntP>0)and(sw_subst_off=0): ps_get(1); fi
  define_pixels(font_wd,font_ht,thickness_font_frame);
  char_use_check;
  if     sw_solid=1: bond_len#:=font_wd#*ratio_bond_width;
                     proc_size_setup; proc_skeleton; proc_scaling;
  elseif sw_solid=2: proc_size_setup; proc_skeleton; proc_scaling;
  else:
    bond_len#:=3mm#; proc_size_setup;
    for i=1 upto 6: proc_skeleton; proc_scaling;
      if (mol_ht/mol_wd)>(font_ht/font_wd): factor:=(font_ht*RH-2margin_top_bottom)/mol_ht;
      else:                                 factor:=(font_wd*RW-2margin_left_right)/mol_wd;  fi
      exitif factor=1;
      bond_len#:=bond_len#*factor; proc_size_setup;
    endfor
    if bond_len#>max_bond_length#:
       bond_len#:=max_bond_length#; proc_size_setup; proc_skeleton; proc_scaling;  fi
  fi
  proc_char_setup;
  %-----------------------------------------------------------------------------------------------
  if f_beginchar=0:
     if sw_clip=1: font_wd#:=font_wd#*((mol_wd+2margin_left_right)/font_wd);
                   font_ht#:=font_ht#*((mol_ht+2margin_top_bottom)/font_ht);  fi
     beginchar(char_num,font_wd#,font_ht#,0); f_beginchar:=1;
  fi
  %-----------------------------------------------------------------------------------------------
  for i=1 upto cntB: if lineB[i]<si_ : draw_bond(i,sB[i],eB[i],angB[i],lineB[i]); fi endfor
  for i=1 upto cntB: if lineB[i]>=si_: draw_bond(i,sB[i],eB[i],angB[i],lineB[i]); fi endfor
  %-----------------------------------------------------------------------------------------------
  for i=1 upto cntA:
    if (sw_numberA>=1)and(i>=numberA_start)and(i<=numberA_end):
       if     sw_numberA=2: nA:=i-numberA_start+1;
       elseif sw_numberA=3: if numberA_end<cntA: nA:=numberA_end-i+1; else: nA:=cntA-i+1; fi
       else:                nA:=i; fi
       if     i<10:  draw_atom(i,posA[i],0,decimal(nA),1);
       else:         draw_atom(i,posA[i]+(-.2atom_wd,0),0,decimal(nA),1);  fi
    elseif sw_numberB>=1:
    else:  draw_atom(i,posA[i],ang_br[i],strD[numS[i]],tbl_str_wd[numS[i]]);  fi
  endfor
  %-----------------------------------------------------------------------------------------------
  sftX:=margin_left_right-minX+(w-mol_wd-2margin_left_right)*FW;
  sftY:=margin_top_bottom-minY+(h-mol_ht-2margin_top_bottom)*FH;
  posM[incr cntM]:=(minX+sftX,minY+sftY);
  if sw_mol_frame=1: ext(draw_frame(p0,wd,ht,0.2pt);) fi
  addto chem_structure also currentpicture shifted (sftX,sftY);
  clearit;
  endgroup;
enddef;
%-------------------------------------------------------------------------------------------------
def ps_put(expr n,p)=
  ps_adr[incr cntP]:=n; ps_com[cntP]:=p; op_cnt[cntP]:=0;
  if lineT<>0:  PP(_tmp_line,lineT) fi   if rotT<>0:   PP(_rot_ang,rotT) fi
  if lenT<>crR: PP(_chg_len,lenT) fi     if envT<>hz:  PP(_chg_env,envT) fi
  if lineT=nl:  PP(_tmp_len,0) elseif lineT=no: PP(_adj_ang,0) fi PP(_mk_bond,0)
  if f_lineT=0: lineT:=si; fi  if f_lenT=0: lenT:=crR; fi 
  if f_rotT=0:  rotT:=0;   fi  if f_envT=0: envT:=hz;  fi
enddef;
%-------------------------------------------------------------------------------------------------
def ps_get(expr n)=
  nA:=cntP; nB:=cntD[0];
  for i=n upto cntP:
    PG(_jp_atom_abs,ps_adr[i]) PG(_push,1)
    for j=1 upto op_cnt[i]: PG(op_com[i][j],op_par[i][j]) endfor
    for j=1 upto cntD[ps_com[i]]: PG(comD[ps_com[i]][j],parD[ps_com[i]][j]) endfor 
    PG(_pop,1) PG(_term,0)
  endfor
  proc_bond_atom(nB+1); if cntP>nA: ps_get(nA+1); fi
enddef;
%-------------------------------------------------------------------------------------------------
def draw_frame(expr PS,LX,LY,PN)=
  draw ((PN/2,PN/2)--(LX-PN/2,PN/2)--(LX-PN/2,LY-PN/2)--(PN/2,LY-PN/2)--cycle) shifted PS wpss PN;
enddef;
%-------------------------------------------------------------------------------------------------
def draw_corner(expr LX,LY,PN)=
  drawdot (PN/2,PN/2) wpss PN;       drawdot (LX-PN/2,PN/2) wpss PN;
  drawdot (LX-PN/2,LY-PN/2) wpss PN; drawdot (PN/2,LY-PN/2) wpss PN;
enddef;
%-------------------------------------------------------------------------------------------------
def draw_vector(expr POS,ROT,LEN)=
  fill (-2mm,-1mm)--(0,0)--(-2mm,1mm)--(-2mm,.4mm)--(-LEN,.4mm)--
       (-LEN,-.4mm)--(-2mm,-.4mm)--cycle rotated ROT shifted POS;
enddef;
%-------------------------------------------------------------------------------------------------
def proc_size_setup=
  if (bond_len#*ratio_atom_bond+offset_atom#)<.8bond_len#:
        atom_wd#:=bond_len#*ratio_atom_bond+offset_atom#;
  else: atom_wd#:=.8bond_len#; ratio_chain_ring:= .5; fi
  wedge_wd#:=    bond_len#*ratio_wedge_bond+offset_wedge#;
  zebra_gap#:=   bond_len#*ratio_zebragap_bond+offset_zebra_gap#;
  bondgap#:=     bond_len#*ratio_bondgap_bond+offset_bond_gap#;
  bond_pen_wd#:= bond_len#*ratio_thickness_bond+offset_thickness#;
  char_pen_wd#:= bond_pen_wd#*ratio_char_bond;
  define_pixels(bond_len,zebra_gap,wedge_wd,atom_wd,bondgap,bond_pen_wd,char_pen_wd);
enddef;
%-------------------------------------------------------------------------------------------------
def proc_scaling=
  minX:=minY:=4095.999; max_x:=max_y:=-4095.999;
  for i=1 upto cntA:
    xpos:=xpart(posA[i]); ypos:=ypart(posA[i]);
    if numS[i]<>0: str_len:=nC:=nD:=0;
      for j=0 upto length(strD[numS[i]])-1:
        nA:=ASCII(pic_c(j,strD[numS[i]]));
        if (nA=score_u)or(nA=hat)or(nA=par_s)or(nA=par_e):
          if (nA=hat)and(nC<.5atom_wd): nC:=.5atom_wd; fi;
          if (nA=score_u)and(nD<.5atom_wd): nD:=.5atom_wd; fi;
        else: str_len:=str_len+atom_wd*tbl_char_wd[nA]; fi  
      endfor
      str_ang:=(ang_br[i] mod 360); nF:=str_len;
      if     (str_ang<=91)or(str_ang>=269): nF:=0; else: str_len:=0; fi
      if     (xpos-nF+.5atom_wd)<minX: minX:=xpos-nF+.5atom_wd;
      elseif (xpos+str_len-.5atom_wd)>max_x: max_x:=xpos+str_len-.5atom_wd; fi
      if     (ypos-nD-.5atom_wd)<minY: minY:=ypos-nD-.5atom_wd;
      elseif (ypos+nC+.5atom_wd)>max_y: max_y:=ypos+nC+.5atom_wd; fi
      else: if xpos<minX: minX:=xpos; elseif xpos>max_x: max_x:=xpos; fi
            if ypos<minY: minY:=ypos; elseif ypos>max_y: max_y:=ypos; fi fi
  endfor
  mol_wd:=max_x-minX; mol_ht:=max_y-minY;
enddef;
%-------------------------------------------------------------------------------------------------
def char_use_check=
  for i=1 upto 128: f_char[i]:=0; endfor
  if (sw_numberA>0)or(sw_numberB>0): for j=ASCII("0") upto ASCII("9"): f_char[j]:=1; endfor
  else: for i=1 upto cntA: if numS[i]<>0: for j=0 upto length(strD[numS[i]])-1:
    f_char[ASCII(pic_c(j,strD[numS[i]]))]:=1; endfor fi endfor fi
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def proc_bond_atom(expr n)=
  f_bra:=f_end:=f_term:=rotT:=adrS:=f_lineT:=f_rotT:=f_lenT:=f_envT:=envT:=envB:=strAT:=0;
  stkL[0]:=bondL:=crR; lenT:=crR; sB[0]:=0; eB[0]:=1; lineT:=si;
  for i=0 upto 5: stkA[i]:=stkB[i]:=0; endfor
  %-----------------------------------------------------------------------------------------------
  for i=n upto cntD[0]:
  com:=comD[0][i]; par:=parD[0][i];
  if     com=_push: 
    if par=1: stkL[adrS]:=bondL; stkA[incr adrS]:=adrA:=cntA; stkB[adrS]:=adrB:=cntB;
    elseif par=0: temp_len:=bondL; fi
  elseif com=_pop: if par=1: adrA:=stkA[decr adrS]; adrB:=stkB[adrS]; bondL:=stkL[adrS];
                   elseif par=0: bondL:=temp_len; fi
  elseif com=_mk_bond:    if (par=0)and(rotT<>0): rotT:=0; fi  add_atom;
  elseif com=_set_adr:    adrT:=par;
  elseif com=_set_atom:   strAT:=par;
  elseif com=_set_psA:    ps_put(getA(adrT),par);
  elseif (com>=_postA)and(com<=_postF):
    if     com=_postA: elseif com=_postB: lineT:=dm; elseif com=_postC: lineT:=wf;
    elseif com=_postD: lineT:=zf; elseif com=_postE: lineT:=nl;
    elseif com=_postF: lineT:=no; lenT:=_size_a; fi  ps_put(cntA+1,par);
  elseif com=_jp_bond:     tempB:=getB(par); sB[cntB+1]:=sB[tempB]; f_bra:=1;
  elseif com=0or_jp_atom:  termA;  sB[cntB+1]:=getA(par); f_bra:=1;
  elseif com=_jp_atom_abs: sB[cntB+1]:=par; f_bra:=1; temp_cntB:=cntB;
  elseif com=_chg_atom:   numS[getA(adrT)]:=parD[par][1];
  elseif com=_N:          numS[getA(par)]:=parD[N][1];
  elseif com=_O:          numS[getA(par)]:=parD[O][1];
  elseif com=_S:          numS[getA(par)]:=parD[S][1];
  elseif com=_H:          ps_put(getA(par),H);
  elseif com=_chg_len: if par=_ring_len: bondL:=ringL;
                       elseif par=0: if lenT<>crR: bondL:=lenT; else: if bondL<0: bondL:=1; fi fi
                       else: bondL:=par; fi
  elseif com=_get_len: if par=0: if bondL=crR: bondL:=lenT; fi else: ringL:=lenB[getB(par)]; fi
  elseif com=_tmp_len: lenT:=par;
  elseif com=_set_line: lineB[getB(adrT)]:=par;
  elseif com=_dl:       lineB[getB(par)]:=dl;
  elseif (com=_chg_line)or(com=_tmp_line): lineT:=par;
  elseif com=_tmp_rot:    rotT:=par;
  elseif com=_cyc:        f_end:=getA(par); add_atom;
  elseif com=_cyc_eB:  f_end:=eB[getB(par)]; add_atom;
  elseif com=_cyc_sB:  f_end:=sB[getB(par)]; add_atom;
  elseif com=_chg_env: envB:=par;
  elseif com=_tmp_env: envT:=par;
  elseif com=_set_clr:
     if par=1: if lineT<>si: f_lineT:=1; fi if rotT<>0:  f_rotT:=1; fi
               if lenT<>crR: f_lenT:=1;  fi if envT<>hz: f_envT:=1; fi fi
     if par=0: f_lineT:=f_rotT:=f_lenT:=f_envT:=rotT:=envT:=0; lineT:=si; lenT:=crR; fi
  elseif com=_stack_reset: stkA[adrS+1]:=0;
  elseif com=_term:       termA;
  elseif com=_set_colorA: colorA[getA(adrT)]:=par;
  elseif com=_set_colorB: colorB[getB(adrT)]:=par;
  fi
  endfor
enddef;
%-------------------------------------------------------------------------------------------------
def add_atom=
  lineB[incr cntB]:=lineT; lineT:=si;
  if lenT=crR: lenB[cntB]:=bondL; else: lenB[cntB]:=lenT; lenT:=crR; fi
  if f_bra=0:  cntA:=cntA+1; sB[cntB]:=cntA; numS[cntA]:=strAT; strAT:=0;
              if f_MP=1: colorA[cntA]:=colorB[cntA]:=0; fi  else: f_bra:=0; fi
  if f_end=0: eB[cntB]:=cntA+1; f_term:=0; else: eB[cntB]:=f_end; f_end:=0; f_term:=1; fi
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def proc_skeleton=
  adrA:=adrB:=cntA:=cntB:=angT:=adrS:=f_end:=f_bra:=rotT:=f_term:=0;
  envT:=envB:=f_lineT:=f_rotT:=f_lenT:=f_envT:=stkM[0]:=0; lineT:=si;
  angA[0]:=angB[0]:=ang_br[0]:=0; posA[0]:=posB[0]:=posB[1]:=(0,0);
  for i=0 upto 5: stkA[i]:=stkB[i]:=0; endfor
  %----------------------------------------------------------------------------------------------
  for i=1 upto cntD[0]:
  com:=comD[0][i]; par:=parD[0][i];
  if com=_push: if par=1: stkM[adrS]:=envB; adrS:=adrS+1; stkA[adrS]:=adrA:=cntA;
                          stkB[adrS]:=adrB:=cntB;
                elseif par=0: temp_len:=bondL; fi
  elseif com=_pop:     if par=1: adrA:=stkA[decr adrS]; adrB:=stkB[adrS]; envB:=stkM[adrS];
                       elseif par=0: temp_len:=bondL; fi
  elseif com=_jp_bond:     tempB:=getB(par); posB[cntB+1]:=posA[sB[tempB]];
                           angT:=angB[tempB]-180; f_bra:=1; rotT:=0;
  elseif com=0or_jp_atom:  termB; adrT:=getA(par); posB[cntB+1]:=posA[adrT];
                           angT:=ang_br[adrT]; f_bra:=1; rotT:=0;
  elseif com=_jp_atom_abs: adrT:=par; posB[cntB+1]:=posA[adrT];
                           angT:=ang_br[adrT]; f_bra:=1; rotT:=0; temp_cntB:=cntB;
  elseif com=_adj_ang:     if (angT<40)or(angT>320): angT:=0; elseif angT<140: angT:=90;
                           elseif angT<220: angT:=180; else: angT:=270; fi
  elseif com=_mk_bond:     if (par=0)and(rotT<>0):par:=rotT; rotT:=0; fi add_bond(par);
  elseif com=_rot_ang:     angT:=(angT+par) mod 360;
  elseif com=_tmp_rot:     rotT:=par;
  elseif (com>=_postA)and(com<=_postF): rotT:=0;
  elseif com=_chg_env: envB:=par;
  elseif com=_tmp_env: envT:=par;
  elseif com=_set_clr: f_clr:=par; if par=0: lineT:=si; lenT:=crR; rotT:=envT:=0; fi
  elseif com=_cyc:     f_end:=1; proc_cyc(getA(par));
  elseif com=_cyc_sB:  f_end:=1; proc_cyc(sB[getB(par)]);
  elseif com=_cyc_eB:  f_end:=1; proc_cyc(eB[getB(par)]);
  elseif com=_term:    termB;
  elseif com=_stack_reset:   stkA[adrS+1]:=0;
  fi
  endfor
enddef;
%-------------------------------------------------------------------------------------------------
def add_bond(expr m)=
  cntB:=cntB+1; if m=_arg_ang: nA:=proc_env(angT,envB,60,-60); else: nA:=m; fi
  if f_bra=0: adrT:=cntA:=cntA+1; posA[cntA]:=posB[cntB]; angA[cntA]:=angT;
               if nA>=0: ang_br[cntA]:=angT+nA/2-90; else: ang_br[cntA]:=angT+nA/2+90; fi
  else: f_bra:=0; fi
  angB[cntB]:=angT:=(angT+nA) mod 360;
  if f_end=0: if lenB[cntB]=_size_a: posB[cntB+1]:=sfB(posB[cntB],atom_wd,angT);
             else: nA:=lenB[cntB];glu:=0; if nA<0: nA:=-nA; glu_c(adrT);glu_c(cntA+1); fi
             posB[cntB+1]:=sfB(posB[cntB],nA*bond_len+glu,angT); fi
             posA[cntA+1]:=posB[cntB+1]; f_term:=0;
  else: f_end:=0; f_term:=1; fi
enddef;
%==================================================================================================
vardef getA(expr n)=
 if n>=0: stkA[adrS+1]+n elseif n>=-999: cntA+n+1 else: stkA[adrS]+n+4095 fi enddef;
vardef getB(expr n)=
 if n>=1: stkB[adrS+1]+n elseif n=0: tempB elseif n>=-999: cntB+n+1 else: stkB[adrS]+n+4095 fi
enddef;
def termA= if f_term=0: if f_bra=0: numS[incr cntA]:=strAT; strAT:=0;
           if f_MP=1: colorA[cntA]:=0; fi else: f_bra:=0; fi f_term:=1; fi enddef;
def termB=
  if f_term=0: if f_bra=0: ang_br[incr cntA]:=angT; else: f_bra:=0; fi f_term:=1; fi enddef;
def proc_cyc(expr n)= add_bond(angle(posA[n]-posB[cntB+1])-angT);  enddef;
def glu_c(expr n)=  if numS[n]<>0: nB:=angT mod 180; nC:=.5tbl_str_wd[numS[n]]*atom_wd;
                    if (nB<45)or(nB>135):glu:=glu+((sind(nB)*nC)++nC);
                    else                :glu:=glu+((cosd(nB)*.5atom_wd)++.5atom_wd) fi fi
enddef;
vardef proc_env(expr n,m,a,b)=  r:=(n mod 360);
  if m>=def_emb_start: parD[m][cntB-temp_cntB-1] else:
    if     m=hz:if r=0:a elseif r<=90:b elseif r<=180:a elseif r<270:b  else:a fi
    elseif m=vt:if r=0:b elseif r<90:a  elseif r<=180:b elseif r<=271:a else:b fi
    elseif (m>=-180)and(m<=180): m fi fi
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def draw_atom(expr n,p,r,s,WD)=
  begingroup
  save pA,pT,dx;
  pair pA,pT;
  %----------------------------------------------------------------------------------------------
  if ((r mod 360)<=90+1)or((r mod 360)>=270-1): dx:=1; else: dx:=-1; fi
  pT:=p-(.5atom_wd*dx*WD,.5atom_wd);
  nE:=nC:=0;
  for i=0 upto length(s)-1:
    nB:=ASCII(pic_c(i,s));
    if dx<0: if nB=par_s:k:=i+1; nC:=1; forever:exitif pic_c(i+(incr nC),s)="}";endfor fi
             if nC>0: nB:=ASCII(pic_c(k+(decr nC),s)); fi
    fi
    if     nB=score_u: if nE=0: nE:=-.5; else: nE:=0; fi;
    elseif nB=hat:     if nE=0: nE:= .5; else: nE:=0; fi;
    elseif (nB<>par_s)and(nB<>par_e):
      nA:=atom_wd*tbl_char_wd[nB];
      pA:=pT+(.5*((dx-1)*nA),nE*atom_wd);
      if sw_atom_frame=1: draw_frame(pA,nA,atom_wd,0.1pt); fi
      if (sw_numberA>=1)or(sw_numberB>=1):
        erase fill unitsquare xscaled nA yscaled atom_wd shifted pA wpcs 0.01pt; fi
      addto currentpicture also atom_font[nB]
         shifted (pA-(0,atom_wd*tbl_char_dp[nB])) Cp(colorA[n]);
      pT:=pT+(nA*dx,0);
    fi
  endfor
  endgroup
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def draw_bond(expr n,BP,EP,ANG,LN)=
  begingroup
  save ww,ap,am,hpt,Ls,Le,pA,pB,pC,pT;
  pair Ls,Le;
  path pA,pB,pC,pT;
  %----------------------------------------------------------------------------------------------
  nC:=length(posA[EP]-posA[BP]);
  if (nC>0)and(LN<>0):
    ww:=wedge_wd; hpt:=.5bond_pen_wd; ap:=ANG+90; am:=ANG-90;
    if sw_numberB>=1: clipping(0,0,posA[BP],posA[EP],nC);
    else: clipping(numS[BP],numS[EP],posA[BP],posA[EP],nC); fi
    pickup pencircle scaled bond_pen_wd;
    if known colorB[n]: nB:=colorB[n]; fi
    %---------------------------------------------------------------------------------------------
    if     (LN=si)or(sw_bond_single=1): draw pA Cp(nB);
    elseif LN=dl:draw pA Cp(nB); draw sfB(pC,bondgap,ap) Cp(nB);
    elseif LN=dr:draw pA Cp(nB); draw sfB(pC,bondgap,am) Cp(nB);
    elseif LN=dm:draw sfB(pB,bondgap/1.75,ap) Cp(nB); draw sfB(pB,bondgap/1.75,am) Cp(nB);
    elseif LN=tm:draw pA Cp(nB);draw sfB(pA,bondgap,ap) Cp(nB); draw sfB(pA,bondgap,am) Cp(nB);
    elseif LN=wf:fill Ls--sfB(Le,ww,am)--sfB(Le,ww,ap)--cycle;
    elseif LN=wb:fill sfB(Ls,ww,am)--Le--sfB(Ls,ww,ap)--cycle;
    elseif LN=bd:draw pA withpen penrazor rotated ap scaled bondgap Cp(nB);
    elseif LN=bz:bz_put(sfB(Ls,ww,ap),sfB(Le,ww,ap),sfB(Ls,ww,am),sfB(Le,ww,am),nB);
    elseif LN=zf:wz_put(Ls,sfB(Le,ww,ap),sfB(Le,ww,am),nB);
    elseif LN=zb:wz_put(Le,sfB(Ls,ww,am),sfB(Ls,ww,ap),nB);
    elseif LN=dt:for i=0 step .75zebra_gap/length(Le-Ls) until 1: drawdot i[Ls,Le]; endfor
    elseif LN=wv:draw_wv;
    elseif LN=vf:draw pA Cp(nB);draw sfB(Le,bondgap,ANG-150)--Le--sfB(Le,bondgap,ANG+150) Cp(nB);
    elseif LN=vb:draw pA Cp(nB);draw sfB(Ls,bondgap,ANG-30)--Ls--sfB(Ls,bondgap,ANG+30) Cp(nB);
    elseif LN=si_:erase draw 0.15[Ls,Le]--0.85[Ls,Le] wpcs 0.8bondgap; draw pA Cp(nB);
    elseif LN=wf_:erase fill sfB(0.15[Ls,Le],.5ww,ap)--sfB(0.15[Ls,Le],.5ww,am)--
                             sfB(0.85[Ls,Le],1.3ww,am)--sfB(0.85[Ls,Le],1.3ww,ap)--cycle;
                  fill Ls--sfB(Le,ww,am)--sfB(Le,ww,ap)--cycle;
    elseif LN=wb_:erase fill sfB(0.15[Ls,Le],1.3ww,ap)--sfB(0.15[Ls,Le],1.3ww,am)--
                             sfB(0.85[Ls,Le],.5ww,am)--sfB(0.85[Ls,Le],.5ww,ap)--cycle;
                  fill sfB(Ls,ww,am)--Le--sfB(Ls,ww,ap)--cycle;
    elseif LN=bd_:erase draw 0.15[Ls,Le]--0.85[Ls,Le] wpcs 1.6bondgap;
                  draw pA withpen penrazor rotated ap scaled bondgap Cp(nB);
    fi
    if (sw_numberB>=1)and(n>=numberB_start)and(n<=numberB_end):
        if     sw_numberB=2: nB:=n-numberB_start+1;
        elseif sw_numberB=3: if numberB_end<cntB: nB:=numberB_end-n+1; else: nB:=cntB-n+1; fi
        else:                nB:=n; fi
        draw_atom(n,.5[posA[BP],posA[EP]],0,decimal(nB),1); fi
  fi
  endgroup
enddef;
%-------------------------------------------------------------------------------------------------
def draw_wv=  nB:=3bond_pen_wd; nA:=nB/length(Le-Ls);
  draw Le for i=1 upto 1/nA: if odd(i): ..controls(((i-.5)*nA)[sfB(Le,nB,ap),sfB(Ls,nB,ap)])
  else: ..controls(((i-.5)*nA)[sfB(Le,nB,am),sfB(Ls,nB,am)]) fi ..(i*nA)[Le,Ls] endfor ..Ls;
enddef;
%------------------------------------------------------------------------------------------------
def wpcs expr n= withpen pencircle scaled n enddef;
def wpss expr n= withpen pensquare scaled n enddef;
def sqr(expr WD,HT)= ((0,0)--(WD,0)--(WD,HT)--(0,HT)--cycle) enddef;
def sfB(expr p,l,r)= p shifted ((l,0) rotated r) enddef;
def wz_put(expr PA,PB,PD,CL)=  nC:=length(Le-Ls);
  for i=0 upto nC/zebra_gap: 
    nA:=zebra_gap*i/nC; nD:=(zebra_gap*i+ratio_zebra_black*zebra_gap)/nC;
    if nD>((nC-zebra_gap)/nC): nD:=1; fi
    fill nA[PB,PA]--nA[PD,PA]--nD[PD,PA]--nD[PB,PA]--cycle Cp(CL); endfor  enddef;
def bz_put(expr PA,PB,PC,PD,CL)=  nC:=length(Le-Ls);
  for i=0 upto nC/zebra_gap: 
    nA:=zebra_gap*i/nC; nD:=(zebra_gap*i+ratio_zebra_black*zebra_gap)/nC;
    fill nA[PB,PA]--nA[PD,PC]--nD[PD,PC]--nD[PB,PA]--cycle Cp(CL); endfor  enddef;
def cut_off(expr p,w,h,s)=
  p intersectionpoint sqr(w+.1h,1.1h) shifted (s-(.5(w+.1h),.5(1.1h))); enddef;
def clipping(expr SA,SB,PA,PB,LEN)=
  pT:=PA--PB; nA:=0.6atom_wd/LEN;
  if (SA=0)and(SB=0): Ls:=PA; Le:=PB; pA:=pB:=pT; pC:=0.1[PA,PB]--0.1[PB,PA];
  elseif SA=0: Ls:=PA; Le:=cut_off(pT,atom_wd*tbl_str_wd[SB],atom_wd,PB); 
     pA:=Ls--Le; pB:=Ls--nA[PB,PA]; pC:=0.1[PA,PB]--Le;
  elseif SB=0: Ls:= cut_off(pT,atom_wd*tbl_str_wd[SA],atom_wd,PA); Le:=PB;
     pA:=Ls--Le; pB:=nA[PA,PB]--Le; pC:=Ls--(0.1)[PB,PA];
  else: Ls:=cut_off(pT,atom_wd*tbl_str_wd[SA],atom_wd,PA);
        Le:=cut_off(pT,atom_wd*tbl_str_wd[SB],atom_wd,PB);
        pB:=nA[PA,PB]--nA[PB,PA]; pC:=pA:=Ls--Le; fi
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def_num:=def_emb_start;
%-------------------------------------------------------------------------------------------------
lr:='(60 for i==1 upto 10:: ,-60,60 endfor); rl:='(-60,lr);
def ring expr n= <((-180 DIV n)-90),(_chg_len,0),
  for i==2 upto n:: ,(360 DIV n) endfor,(_cyc_sB,1-n) enddef;
for i=3 upto 20: ?[i]:='({{,ring i,}}); endfor
Ph:=Ph1:='(?6,(_dl,-2),(_dl,-4),(_dl,-6)); Ph2:='(?6,(_dl,-1),(_dl,-3),(_dl,-5));
!:='((_mk_bond,_arg_ang)); !0:='(<180,180);
for i=1  upto 20:  ![i]:='({{,(_get_len,0),! for j==2 upto i::,! endfor,}}); endfor
Me:='(); Et:=!; Pr:='(!2); Bu:='(!3);
for i=4,5,6:   for j=2 upto i-2: ?[i][j]:='((_fuse,i),(j,0)); endfor endfor
for i=5,6,7,8: for j=11 upto 15: ?[i][j]:='((_fuse,i),(j,0)); endfor endfor
%=================================================================================================
H :='("H");C:='("C");N:='("N");O:='("O");F:='("F");S:='("S");P:='("P");I:='("I");
Si:='("{Si}");Al:='("{Al}");Mg:='("{Mg}");Zn:='("{Zn}");As:='("{As}");Cu:='("{Cu}");
Ag:='("{Ag}");Au:='("{Au}");Sn:='("{Sn}");Cl:='("{Cl}");Br:='("{Br}");Li:='("{Li}");
Na:='("{Na}");Ca:='("{Ca}");Hg:='("{Hg}");Ni:='("{Ni}");Ti:='("{Ti}");U:='("U");
%-------------------------------------------------------------------------------------------------
plus:='("+"); minus:='("-"); HH:='("H{_2_}"); NH:='(N,*/*H); NnoH:='(); SO:='(S,//O);
SOO :='(S,//O^-35,//O^35); CHO:='("CHO"); OH:='("OH"); COOH:='("COOH"); CH2:='("C{H_2_}");
CH3:='("C{H_3_}"); CN:='("CN"); NH2:='("N{H_2_}"); NO2:='("N{O_2_}"); SH:='("SH");
SO3:='("S{O_3_}"); NMe:='(N,/Me); tBu:='(60,-2\,-3*,60); iPr:='(60,-2\); OMe:='(O,!);
SMe:='(S,!); OEt:='(O,!,Et); SEt:='(S,!,Et); OiPr:='(O,!,iPr); SiPr:='(S,!,iPr);
OPr:='(O,!,Pr); SPr:='(S,!,Pr); COO:='(//O,!,O); COOMe:='(COO,!); COMe:='(//O,!);
OCOMe:='(O,!,//O,!); COOEt:='(COO,!,Et); COOiPr:='(COO,!,iPr); COOPr:='(COO,!,Pr);
COOtBu:='(COO,!,tBu); NMeMe:='(N,/Me,!); NHCOMe:='(NH,!,//O,!); NHiPr:='(NH,!,iPr);
NHtBu:='(NH,!,tBu);NHH:='(N,/H,!,H); NHMe:='(NH,!); NHEt:='(NH,!,Et);
CF2:='(/F,60,F); CCl2:='(/Cl,60,Cl); CBr2:='(/Br,60,Br); CF3:='(/F,/F^60,60,F);
CCl3:='(/Cl,/Cl^60,60,Cl);CBr3:='(/Br,/Br^60,60,Br);O_H:='(O,!,H);CO_H:='(//O,!,H);
%==================================================================================================
CO_OH:='(//O,!,O,!,H); C_N:='(!~tm,N); N_HH:='(N,/H,60,H); N_OO:='(N,//O,60~dm,O);
S_H:='(S,!,H); N_H:='(N,/H); C_HHH:='(/H,/H^60,60,H);
%--------------------------------------------------------------------------------------------------
def expand_set=
  save CH,NH,NO,OH,CHO,COOH,CN,SH;
  CH3:=C_HHH; NH:=N_H; NH2:=N_HH; NO2:=N_OO; OH:=O_H; CHO:=CO_H; COOH:=CO_OH; CN:=C_N; SH:=S_H;
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def char_size_set(expr a)(expr b)(expr c)(expr s)=
  for j=0 upto length(s)-1: nA:=ASCII(pic_c(j,s));
    tbl_char_wd[nA]:=a; tbl_char_ht[nA]:=b; tbl_char_dp[nA]:=c;
  endfor
enddef;
%---------------------------------------------------------------
char_size_set( 1)( 1)(  0)("CGHMNOQ");
char_size_set(.9)( 1)(  0)("ABDEFIJKLPRSTUVWXYZ-+/><m");
char_size_set(.7)( 1)(  0)(" ()0123456789nhj=*bdiltf");
char_size_set(.7)( 1)(.15)("pqg");
char_size_set(.7)(.8)(  0)("acekosuvxzwry");
char_size_set(.4)( 1)(  0)(".,:;'`");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def proc_char_setup=
begingroup
save Z,aW,aH,fW,hW,hW,hH,fP,hP,vL,hL,PO,POh,sH,FD;
pair Z[];
%---------------------------------------------------------------
def FD(expr c)(text s)=
  if f_char[ASCII(c)]=1:
    aW#:=atom_wd#*tbl_char_wd[ASCII(c)]*(1-ratio_atomgap_atom*2);
    aH#:=atom_wd#*tbl_char_ht[ASCII(c)]*(1-ratio_atomgap_atom*2);
    define_pixels(aW,aH);
    fW:=aW-hP; hW:=.5aW; fH:=aH-hP; hH:=.5aH;
    Z01:=( 0,hP); Z02:=(hP, 0); Z03:=(hP,hP); Z04:=(aW,hP); Z05:=(fW, 0); Z08:=( 0,hH);
    Z09:=(hP,hH); Z10:=(fW,hH); Z12:=( 0,fH); Z13:=(hP,fH); Z14:=(hW,fH); Z16:=(aW,fH);
    Z18:=(hP,aH); Z19:=(hW,hP); Z20:=(hW,hH); Z06:=(hW,aH); Z07:=(hW, 0); Z11:=(hW,aW);
    Z15:=(fW,aH); Z17:=(aW,hH);
    s;
    atom_font[ASCII(c)]:=
    currentpicture shifted (ratio_atomgap_atom*atom_wd,ratio_atomgap_atom*atom_wd);
    clearit;
  fi
enddef;
%-----------------------------------------------------------------------------------------
def vL= withpen penrazor scaled fP enddef;
def hL= withpen penrazor rotated 90 scaled fP enddef;
def PO=
  Z10..(.8aW,fH)..tension 1.5..(.2aW,fH)..Z09..(.2aW,hP)..tension 1.5..(.8aW,hP)..cycle enddef;
def POh= (hP,.25aH)..Z19..(fW,.25aH)..Z20..cycle enddef;
def sH expr n= shifted (0,n) enddef;
def SP(expr m,n)expr p=subpath(m*length(p),n*length(p)) of p enddef;
%==================================================================================================
pickup pencircle scaled char_pen_wd;
fP:=char_pen_wd; hP:=.5char_pen_wd;
%------------------------------------------------------------------------------------------
FD(" ")();
FD(".")(drawdot Z19 wpcs fP);
FD(",")(drawdot Z19 wpcs fP; draw (hW+.5fP,hP)..(hW+.3fP,-fP)..(hW-.5fP,hP-2fP));
FD("'")(drawdot Z14 wpcs fP; draw (hW+.5fP,fH)..(hW+.3fP,fH-fP)..(hW-.5fP,fH-2fP));
FD("`")(drawdot (hW,fH-2fP) wpcs fP;draw (hW-.5fP,fH-2fP)..(hW-.3fP,fH-fP)..(hW+.5fP,fH));
FD(":")(drawdot (hW,.2aH) wpcs fP; drawdot (hW,.8aH) wpcs fP); 
FD(";")(drawdot (hW,.2aH) wpcs fP; drawdot (hW,.8aH) wpcs fP;draw (hW+hP,.2aH)..(hW-hP,.2aH-2fP));
FD("(")(draw Z19...Z09...Z14);
FD(")")(draw Z19...Z10...Z14);
FD(">")(cutdraw Z01--Z17--Z12);
FD("<")(cutdraw Z16--Z08--Z04);
FD("0")(draw Z09...Z14...Z10...Z19...cycle);
FD("1")(draw Z07--Z06--(hW-hP,fH)--(hW-hP,aH-2.5hP) vL);
FD("2")(cutdraw SP(0,.71)(hP,1.3hP)--(fW,.55aH)..Z14..(hP,.5aH)..(hW,.27aH); draw Z04--Z01 hL);
FD("3")(cutdraw SP(0,.77)POh; cutdraw SP(.23,.95)POh sH .5aH-hP; draw (.3aW,hH)--Z20 hL);
FD("4")(draw (0,.3aH)--(aW,.3aH) hL; draw (.7aW,0)--(.7aW,aH)--(1.2hP,.3aH+hP) vL);
FD("5")(draw Z12--Z16 hL; draw Z13--(hP,.49aH) vL;
        cutdraw (.5hP,.15aH)..(.65aW,1.3hP)..(fW,.3aH)..(hW,.6aH)..(.6hP,.53aH));
FD("6")(draw Z19..(fW,.5fW)..(hW,fW)..(hP,.5fW)..cycle; cutdraw (hP,.4aH)--Z06);
FD("7")(draw (0,.fH)--Z16 hL; draw (aW-1.2hP,aH-fP)--(.4aW,0) vL scaled 1.2);
FD("8")(draw POh; draw (hP,.75aH)...Z14...(fW,.75aH)...Z20...cycle;);
FD("9")(draw Z19..(fW,.5fW)..(hW,fW)..(hP,.5fW)..cycle sH .32aH; cutdraw (fW,.6aH)--Z07);
FD("A")(draw Z02--Z06--Z05 vL scaled 1.14; draw .33[Z02,Z06]--.33[Z05,Z06]);
FD("B")(draw Z13--Z14{right}..(.9fH,.75aH)..{left}(.5aW,hH)--Z09--(.5aW,hH){right}..
            (.9fH,.25aH)..{left}Z19--Z03; draw Z02--Z18 vL);
FD("C")(cutdraw SP(.05,.95)PO); 
FD("D")(draw Z13--Z14..Z10..Z19--Z03; draw Z02--Z18 vL);
FD("E")(draw Z04--Z01 hL;draw Z12--Z16 hL;draw Z08--Z17 hL;draw Z02--Z18 vL);
FD("F")(draw Z12--Z16 hL; draw (0,.45aH)--(fW,.45aH) hL; draw Z02--Z13 vL);
FD("G")(cutdraw SP(.06,.97)PO; draw bot Z20-- bot Z17 hL);
FD("H")(draw Z02--Z18 vL; draw Z09--Z10; draw Z05--Z15 vL);
FD("I")(draw Z19--Z14 vL; draw (hW-fP,hP)--(hW+fP,hP) hL; draw (hW-fP,fH)--(hW+fP,fH) hL);
FD("J")(cutdraw Z09..(hP,.4aH){down}..{right}Z19{right}..{up}(fW,.4aH)..Z15);
FD("K")(cutdraw Z02--Z18; cutdraw .5[.45[Z02,Z18],Z16]--Z05; cutdraw .45[Z02,Z18]--Z16);
FD("L")(draw Z04--Z01 hL; draw Z02--Z18 vL);
FD("M")(draw Z02--Z18 vL; draw Z18--Z19--Z15 vL scaled 1.14; draw Z15--Z05 vL);
FD("N")(draw Z02--Z18 vL; draw Z05--Z15 vL;draw (1.4hP,aH)--(aW-1.4hP,0) vL scaled 1.4);
FD("O")(draw PO);
FD("P")(draw Z02--Z18 vL; draw Z13--(.3aW,fH){right}..(fW,.7aH)..{left}(.3aW,.44aH)..(hP,.44aH))
FD("Q")(draw PO; draw (.6aW,.4aH)--Z05);
FD("R")(draw Z02--Z18 vL; draw Z13--(.3aW,fH){right}..(fW,.7aH)..{left}(.3aW,.44aH)..(hP,.44aH);
        cutdraw Z05{up}..{left}(hW,.44aH));
FD("S")(cutdraw SP(.05,.45)PO; cutdraw SP(.55,.95)PO; draw (fW,.3aH){up}..{up}(hP,.7aH));
FD("T")(draw Z12--Z16 hL;draw .5[Z12,Z16]--Z07 vL);
FD("U")(cutdraw Z18..Z09{down}..{right}Z19{right}..{up}Z10..Z15);
FD("V")(draw Z18--Z07--Z15 vL scaled 1.2);
FD("W")(draw Z18--(.25aW,0)--Z06--(.75aW,0)--Z15 vL scaled 1.08);
FD("X")(draw Z18..Z05 vL scaled 1.4; draw Z02..Z15 vL scaled 1.4);
FD("Y")(draw Z18--Z20--Z15 vL scaled 1.2; draw Z20--Z07 vL);
FD("Z")(draw Z12--Z16 hL;draw (1.4hP,fP)--(aW-1.4hP,aH-fP) vL scaled 1.4;draw Z01--Z04 hL);
FD("-")(draw Z09--Z10 hL);
FD("=")(draw (hP,.3aH)--(fW,.3aH) hL;draw (hP,.6aH)--(fW,.6aH) hL);
FD("/")(draw (fW,fH)..Z01);
FD("+")(draw Z19--Z14 vL; draw Z09--Z10 hL);
FD("a")(draw Z19..Z10..Z14..Z09..cycle; draw Z05--Z15 vL);
FD("b")(draw Z19..Z10..Z11..Z09..cycle; draw Z02--(hP,1.2aH) vL);
FD("c")(cutdraw SP(.06,.94)Z10..Z14..Z09..Z19..cycle);
FD("d")(draw Z19..Z10..Z11..Z09..cycle; draw Z05--(fW,1.2aH) vL);
FD("e")(cutdraw SP(0,.92)Z10..Z14..Z09..Z19..cycle; draw Z10--Z09);
FD("f")(cutdraw (.5aW,0)--(.5aW,.75aH){up}..(.75aW,fH)..{down}(fW,.8aH); draw Z08--Z17 hL);
FD("g")(draw Z19..Z10..Z11..Z09..cycle sH .3aH; cutdraw Z15--(fW,hP)..(hW,0)..(.1aW,fP));
FD("h")(draw Z02..(hP,.3aH){up}..(hW,.7fH)..{down}(fW,.3aH)..Z05; draw (hP,.3aH)--Z18 vL);
FD("i")(draw Z07--(hW,.63aH) vL; draw Z14 wpcs 1.4fP);
FD("j")(draw Z19--(hW,.63aH) vL; draw Z19..(.25aW,-hP)..Z01; draw Z14 wpcs 1.4fP);
FD("k")(draw Z02--Z18 vL; cutdraw .5[.45[Z02,Z18],Z16]--Z05; cutdraw .45[Z02,Z18]--Z16);
FD("l")(draw Z07--Z06 vL; draw Z14--Z13 hL; draw Z19--(fW,hP) hL);
FD("m")(cutdraw Z02..(hP,.3aH){up}..(.28aW,.7fH)..{down}(hW,.3aH)..Z07;
        cutdraw (hW,.3aH){up}..(.7aW,.7aH-hP)..{down}(fW,.3aH)..Z05; draw (hP,.3aH)--(hP,.7aH) vL);
FD("n")(cutdraw Z02..(hP,.3aH){up}..(hW,.7fH)..{down}(fW,.3aH)..Z05; draw (hP,.3aH)--(hP,.7aH) vL);
FD("o")(draw Z19..Z10..Z14..Z09..cycle);
FD("p")(draw Z19..Z10..Z11..Z09..cycle sH .3aH; draw (hP,0)--Z18 vL);
FD("q")(draw Z19..Z10..Z11..Z09..cycle sH .3aH; draw (fW,0)--Z15 vL);
FD("r")(cutdraw SP(.35,.72)Z19..Z10..Z14..Z09..cycle sH -fP; draw Z02--Z18 vL);
FD("s")(cutdraw SP(.05,.45)PO; cutdraw SP(.55,.95)PO; draw (fW,.3aH){up}..{up}(hP,.7aH));
FD("t")(draw Z07--Z06 vL; draw (0,.66aH)--(aW,.66aH) hL);
FD("u")(cutdraw Z18..(hP,.55aH){down}..Z19..(fW,.55aH){up}..Z15; draw Z15--Z05 vL);
FD("v")(draw Z18--Z07--Z15 vL);
FD("w")(draw Z18--(.25aW,0)--Z06--(.75aW,0)--Z15 vL);
FD("x")(draw Z18--Z05 vL scaled 1.4; draw Z15--Z02 vL scaled 1.4);
FD("y")(draw Z18--Z20 vL scaled 1.4; draw Z15--Z02 vL scaled 1.4);
FD("z")(draw Z12--Z16 hL;draw (1.4hP,fP)--(aW-1.4hP,aH-fP) vL scaled 1.4; draw Z01--Z04 hL);
endgroup
enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
def blank_str:= "            " enddef;
vardef fixed_r(expr n)(expr s)= (substring(0,n-length(s)) of blank_str)&s enddef;
vardef fixed_l(expr n)(expr s)= s&(substring(0,n-length(s)) of blank_str) enddef;
vardef fdr(expr n)(expr s)=fixed_r(n)(decimal(s)) enddef;
def warning(expr s)= message "% "&decimal(char_num)&fdr(3)(incr warning_cnt)&")"&s&" !!!"; enddef;
def B_check(expr s)(expr t)(expr n)=
  if strD[numS[s]]=t: if bond_cnt>n:
    warning(fdr(4)(s)&" ["&strD[numS[s]]&"] has "&fdr(3)(bond_cnt)&" bonds"); fi fi
enddef;
vardef stripP(expr a)=if length(a)=4: substring (1,3) of a else: a fi enddef;
%==================================================================================================
def STa(expr a,b)=
  tbl_atom_str[incr tbl_cnt]:=a; tbl_atom[tbl_cnt]:=0; tbl_atom_wt[tbl_cnt]:=b;
enddef;
%----------------------------------------------------------------------------------
def STb(expr a)(text s)=
  tbl_atom_str[incr tbl_cnt]:=a; tbl_atom[tbl_cnt]:=0;
  for list=s: tbl_atom[tbl_cnt]:=tbl_atom[tbl_cnt]+1;
    for j=1 upto tbl_cnt:
      if list=tbl_atom_str[j]: tbl_subst[tbl_cnt][tbl_atom[tbl_cnt]]:=j; fi
    endfor
  endfor
enddef;
%==================================================================================================
tbl_cnt:=0;
STa("C",12.0107); STa("H",1.00794);  STa("{Al}",26.9815); STa("{As}",74.9236); STa("B",10.811);
STa("{Br}",79.904); STa("{Ca}",40.078); STa("{Cl}",35.453); STa("{Cu}",63.546); STa("D",2.012);
STa("F",18.9984); STa("{Fe}",55.845); STa("{Hg}",200.59); STa("I",126.90447); STa("K",39.0983);
STa("{Li}",6.941); STa("{Mg}",24.305); STa("N",14.0067); STa("{Na}",22.98977); STa("{Ni}",58.693);
STa("O",15.9994); STa("P",30.973761); STa("S",32.065); STa("{Si}",28.0855); STa("{Sn}",118.71);
STa("{Ti}",47.867); STa("{Zn}",65.409);
%--------------------------------------------------------------------------------------------------
STb("{H_2_}")("H","H"); STb("OH")("O","H"); STb("O{Na}")("O","{Na}"); STb("CN")("C","N");
STb("SH")("S","H"); STb("CHO")("C","H","O");  STb("N{O_2_}")("N","O","O");
STb("N{H_2_}")("N","H","H");
STb("S{O_3_}")("S","O","O","O"); STb("COOH")("C","O","O","H");
STb("C{H_3_}")("C","H","H","H"); STb("C{F_3_}")("C","F","F","F");
%==================================================================================================
def proc_get_info=
  for j=1 upto inf_num:
    nA:=0; for i=0 upto length(info[j]): if pic_c(i,info[j])=":": nA:=i; fi exitif nA>0; endfor
    if nA>0:
      s_tag:=substring (0,nA) of info[j];
      s_var:=substring (nA+1,length(info[j])) of info[j];
      if known scantokens("inf_"&s_tag): scantokens("inf_"&s_tag):=s_var; fi
    fi
  endfor
enddef;
%==================================================================================================
def proc_calc_out=
  begingroup
  save warning_cnt,cal_MW,cal_MWp,knownA,cal_FM,bondC,tmp_wtp,bond_cnt,hide_H,
       inf_NO,inf_EN,inf_JN,inf_FM,inf_CAS,inf_USE,inf_EXA,inf_EXB,inf_MW;
  numeric sumA[],bondC[];
  string cal_FM,cal_MW_str,sumA,s_tag,s_var,
         inf_NO,inf_EN,inf_JN,inf_FM,inf_CAS,inf_USE,inf_EXA,inf_EXB,inf_MW;
  inf_NO:=inf_EN:=inf_JN:=inf_FM:=inf_CAS:=inf_USE:=inf_EXA:=inf_EXB:=inf_MW:="";
  cal_MW_str:=cal_FM:=""; hide_H:=warning_cnt:=cal_MW:=cal_MWp:=0;
  %------------------------------------------------------------------------------------------
  proc_get_info;
  for i=1 upto tbl_cnt: sumA[i]:=0; endfor
  for i=1 upto cntA: knownA:=bond_cnt:=0;
    for j=1 upto cntB: if     (lineB[j]=dl)or(lineB[j]=dr)or(lineB[j]=dm): bondC[j]:=2;
                       elseif (lineB[j]=tm): bondC[j]:=3; elseif lineB[j]=nl: bondC[j]:=0;
                       elseif lineB[j]=0: bondC[j]:=0; else: bondC[j]:=1; fi
                       if (sB[j]=i)or(eB[j]=i): bond_cnt:=bond_cnt+bondC[j]; fi endfor
    Bcnt[i]:=bond_cnt;
    if strD[numS[i]]="": strD[numS[i]]:="C"; fi
    if strD[numS[i]]="C": if bond_cnt<4: hide_H:=hide_H+(4-bond_cnt); fi fi
    B_check(i)("C")(4); B_check(i)("H")(1);    B_check(i)("O")(2);
    B_check(i)("F")(1); B_check(i)("{Cl}")(1); B_check(i)("Br}")(1);
    for j=1 upto tbl_cnt:
      if strD[numS[i]]=tbl_atom_str[j]:
        if tbl_atom[j]=0: sumA[j]:=sumA[j]+1;
        else: for k=1 upto tbl_atom[j]: sumA[tbl_subst[j][k]]:=sumA[tbl_subst[j][k]]+1; endfor fi
        knownA:=1; fi  endfor
    if knownA=0: warning(" Unknown Str("&strD[numS[i]]&") is used "&decimal(i)); fi
  endfor
  sumA[2]:=sumA[2]+hide_H;
  for i=1 upto tbl_cnt:
    if sumA[i]>=1:
      tmp_wtp:=(tbl_atom_wt[i]/100)*sumA[i];
      if (tmp_wtp<=40)and(cal_MWp<=40): cal_MW:=cal_MW+(tbl_atom_wt[i]*sumA[i]);
      else: cal_MWp:=cal_MWp+tmp_wtp; fi
      cal_FM:=cal_FM&stripP(tbl_atom_str[i]) if sumA[i]>=2: &decimal(sumA[i]) fi;
    fi
  endfor
  if sw_auxout=1:    proc_auxfile_out;   fi
  if sw_checklist=1: proc_checklist_out; fi
  if sw_MOLout=1:    proc_MOLfile_out;    fi
  endgroup
enddef;
%==================================================================================================
def auxtag_out=
  for i=1 upto aux_max: exitif tag[i]=""; if i=1: printf tag[1] else: &aux_delimiter&tag[i] fi
  endfor
enddef;
%==================================================================================================
def proc_auxfile_out=
  for i=1 upto aux_max:  exitif tag[i]="";
    if i=1: printf "" else: &aux_delimiter fi if sw_auxfix=0: &tag[i]&":" fi &
    if     string  scantokens(var[i]): scantokens(var[i])
    elseif numeric scantokens(var[i]): decimal(scantokens(var[i])) fi
  endfor
  if sw_auxfix=0: for i=1 upto inf_num: &aux_delimiter&info[i] endfor; fi
enddef;
%==================================================================================================
def proc_checklist_out=  nA:=0;
  message "% Molecular name = "& inf_EN;
  message "%  warnings = "&fdr(3)(warning_cnt)&" / Expanded command = "&decimal(cntD[0]);
  message "%  width * height = " & fdr(8)(mol_wd)&" * "&fdr(8)(mol_ht);
  message "%  Shift width * height  = "& fdr(8)(minX)&" * "&fdr(8)(minY);
  message "%  Bond length = "&fdr(3)(bond_len)&"   Atom size   = "&fdr(3)(atom_wd);
  message "%  Atom count="&fdr(3)(cntA)&" Bond count="&fdr(3)(cntB)&
          " Ring count="&fdr(3)(cntB-cntA+1)&" Hide H ="&fdr(3)(hide_H);
  message "%-----------------------------------< atom >< bond >-------";
  for i=1 upto cntA: message "%  "&fdr(3)(i)&"  ("&fdr(10)(xpart(posA[i])/bond_len)&" ) ("&
     fdr(10)(ypart(posA[i])/bond_len)&" )  "&fixed_l(8)(strD[numS[i]])&"  "&fdr(3)(Bcnt[i]);
  endfor
  message "%Bond list---------------------------------------------------------";
  for i=1 upto cntB: if lineB[i]<>0: message "% "&fdr(3)(incr nA)&" "&fdr(3)(sB[i])&
                     " -> "&fdr(3)(eB[i])&" ("&fdr(3)(bondC[i]-si+1)&")"; fi
  endfor
  message "%Fomula / Molecular weight ---------------------------------------";
  if cal_MWp<=40: cal_MW_str:=fdr(10)(cal_MW); else: cal_MW_str:=fdr(10)(cal_MWp)&" * 100"; fi
  for i=1 upto tbl_cnt:
    if sumA[i]>=1:
       message "% "&fixed_l(4)(stripP(tbl_atom_str[i]))&"("&fdr(9)(tbl_atom_wt[i])&
               ") * "&fdr(6)(sumA[i])&" = "
       if tmp_wtp<=40: &fdr(12)(tbl_atom_wt[i]*sumA[i]); else: &fdr(12)(tmp_wtp)&" * 100"; fi fi
  endfor
  message "% Weight  Calc: " &cal_MW_str &" / Input: "
    if inf_MW<>"": &inf_MW &" / weight gap= " &decimal(cal_MW-scantokens(inf_MW)) fi;
  message "% Fomula  Calc: "&cal_FM&" / Input: "
    if inf_FM<>"": &inf_FM&" / "&  if inf_FM=cal_FM: "MACTCH" else: "NOT MACTCH" fi fi;
  message "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
enddef;
%==================================================================================================
def proc_MOLfile_out=
  mp_log_name:=jobname&"-"&fit_zero(char_num)&decimal(char_num)&"-"&inf_EN&".mol";
  printf ""; printf "  -MCFtoMOL- "&fixed_l(20)(info[1]); printf "";
  printf fdr(3)(cntA)&fdr(3)(cntB)&"  0  0  0  0  0  0  0  0999 V2000";
  for i=1 upto cntA: printf fdr(10)(xpart(posA[i])/bond_len)& fdr(10)(ypart(posA[i])/bond_len)&
           fdr(10)(0)&" "&fixed_l(2)(stripP(strD[numS[i]]))&"  0  0  0  0"; endfor
  for i=1 upto cntB: if lineB[i]<>0:
    if     (lineB[i]=dl)or(lineB[i]=dr)or(lineB[i]=dm): nA:=2;
    elseif lineB[i]=tm: nA:=3;
    elseif (lineB[i]=wf)or(lineB[i]=zb)or(lineB[i]=bd): nB:=1;
    elseif (lineB[i]=zf)or(lineB[i]=wb)or(lineB[i]=dt): nB:=6;
    elseif lineB[i]=wv: nB:=4;  else: nA:=1; nB:=0; fi
    printf fdr(3)(sB[i])&fdr(3)(eB[i])&fdr(3)(nA)&fdr(3)(nB)&"     0  0"; fi endfor
  printf "M  END";
enddef;
%-------------------------------------------------------------------------------------------------
vardef fit_zero(expr n)=if n<=9: "00" elseif n<=99: "0" fi enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%