%
% lgt2upu.mf
%
%% Cyrillic font container with T2 encoding beta-support
%
% This file is future part of lxfonts package
% Version 3.2 // Patchlevel=0
% (c) O.Lapko
%
% This package belongs to the public domain under conditions similar to
% those of D. E. Knuth specified for the Computer Modern family of fonts.
% In particular, only the author is entitled to modify this file
% and to save it under the same name.
%
% Content:
%
% Uppercase Cyrillic letters 128--255 for T2 encoding
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% List of letternames and their codes in T2
%
%                X2  T2A  T2B  T2C
%
% G_up          "80  "80   -    -    Ghe upturn
% G_hcrs        "81  "81   -    -    Ghe horisontally crossed
% G_dsc         "82   -   "82   -    Ghe with descender
% G_tl          "83   -   "83   -    Ghe tail
% SHHA          "84  "84  "84  "84   Shha (same as lower h)
% ZH_dsc        "85  "85  "85   -    Zhe with descender
% Z_dsc         "86  "86   -    -    Ze with descender (cedilla)
% ABH_DZE       "87   -   "87  "87   Abkhasian dze
% UKR_I         "88  "88   -    -    Ukrainian i - latin i with umlaut (goes first for LH font)
% K_dsc         "89  "89  "89  "89   Ka with descender
% K_beak        "8a  "8a   -    -    Ka with reversed beak
% K_tl          "8b   -   "8b   -    Ka with tail
% L_tl          "8c   -   "8c   -    El with tail
% N_dsc         "8d  "8d  "8d  "8d   En with descender
% NG            "8e  "8e  "8e   -    Ligature en-ghe
% N_tl          "8f   -   "8f  "8f   En with tail
%
% OTLD          "90  "90  "90  "90   O tilded & Old Cyrillic Fita (goes first for wncoding)
% S_dsc         "91  "91   -    -    Es with descender (cedilla)
% U_shrt        "92  "92  "92   -    Short U (goes first for LH font)
% Y             "93  "93  "93   -    Straingt u (as upper Y)
% Y_hcrs        "94  "94   -    -    Straingt u horisontally crossed
% H_dsc         "95  "95  "95  "95   Kha with descender
% H_tl          "96   -   "96   -    Kha with tail
% CH_ldsc       "97   -   "97   -    Che with left descender
% CH_rdsc       "98  "98  "98  "98   Che with right descender
% UKR_E         "99  "99   -    -    Ukrainian e (goes first for wncoding and LH font)
% SCHWA         "9a  "9a  "9a  "9a   Schwa
% EPS           "9b   -   "9b   -    Epsilon
%

LHver_check(3,2);  % like |version_check| in ec

% Ukrainian i moved up for LH font
lhchar "Uppercase Cyrillic letter UKR_I (latin I with umlaut)";
cyrchar(UKR_I,max(6u#,4u#+cap_stem#),cap_height#+acc_height#,0);
if is_small_cap: getcharcode(ukr_i); fi
italcorr cap_height#*slant-.25u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#); h:=cap_height;
% upper_ukri; - borrowed from ec
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround(.5w-.5cap_stem); top y1=cap_height; bot y2=0;
filldraw stroke z1e--z2e; % stem
if serifs or monospace:
 dish_serif(1,2,a,1/3,1.05cap_jut,b,1/3,1.05cap_jut);  % upper serif
 dish_serif(2,1,c,1/3,1.05cap_jut,d,1/3,1.05cap_jut);  fi % lower serif
if slitex:
 pickup crisp.nib;           % arms based on proportions of uppercase E
 pos3(slab,90); pos4(slab if not serifs:+2stem_corr fi,-90);
 x3=x4=x1; top y3r=h; bot y4r=0;
 pos5(hair,0); pos6(hair,0); % top and bottom right arms
 rt x5r=rt x6r=hround(w-u);
 pos7(hair,180); pos8(hair,180); % top and bottom left arms
 lft x7r=lft x8r=hround(u);
 y5=y7=good.y(y3l-beak)-eps; % beak in case we ever want a beaked serif!
 y6=y8=good.y(y4l+beak)+eps; % watch that this doesn't get too heavy
 arm(3,5,a,darkness,jut); arm(3,7,b,darkness,jut); % top left and right arms
 arm(4,6,c,darkness,jut); arm(4,8,d,darkness,jut); % bottom left and right
fi
%%%%%
% the accent
uppercase_umlaut(0,0, 9,10,11,12);
penlabels(1,2,3,4,5,6,7,8, 9,10,11,12); endchar;

% O tilded/Fita moved up for wncoding
lhchar "Uppercase Cyrillic letter OTLD O tilded";
cyrchar(OTLD,14u#-width_adj#,cap_height#,0);
if is_small_cap: getcharcode(otld); fi
italcorr .7cap_height#*slant-.5u#;
adjust_fit(0,0);
% upper_o;tld
penpos1(vair',90); penpos3(vround(vair+1.5vair_corr),-90);
penpos2(cap_curve,180); penpos4(cap_curve,0);
if monospace:
 x2r=hround 1.5u;
 interim superness:=sqrt superness; % make |"O"|, not |"0"|
else:
 x2r=hround u; fi
x4r=w-x2r; x1=x3=.5w; y1r=h+o; y3r=-o;
y2=y4=.5h-vair_corr; y2l:=y4l:=.52h;
penstroke pulled_super_arc.e(1,2)(.5superpull)
 & pulled_super_arc.e(2,3)(.5superpull)
 & pulled_super_arc.e(3,4)(.5superpull)
 & pulled_super_arc.e(4,1)(.5superpull) & cycle;  % bowl
%%%%%
% uotilde;
z5=z2l; z6=z4l; penpos5(cap_bar,90); penpos6(cap_bar,90);
if (serifs) and (bar#<=.5curve#):
 numeric bar[];
 bar2=2/3[cap_bar,fudged.cap_stem]; penpos7(bar2,45); z7=.5[z5,z6];
 fill stroke z5e{2,1}..{2,-1}z7e..{2,1}z6e;
else:
 fill stroke z5e--z6e; fi
%%%%%
penlabels(1,2,3,4,5,6); endchar;

% Short U moved up for LH fonts
lhchar "Uppercase Cyrillic letter U_shrt - SHORT U";
cyrchar(U_shrt,13u#,cap_height#+acc_height#,0);
if is_small_cap: getcharcode(u_shrt); fi
italcorr x_height#*slant+.25u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#); full_h:=h; h:=cap_height;
% upper_u;
numeric left_stem,right_stem,bot_stem,bot_vair,outer_jut;
left_stem=cap_stem-stem_corr;
right_stem=min(cap_hair if hefty:-2stem_corr fi,left_stem);
bot_stem=min(cap_hair if hefty:-2stem_corr fi,left_stem);
bot_vair=Vround(if serifs:.5[vair',bot_stem] else:vair' fi);
outer_jut=.7cap_jut; x1l=w-x4r=l+letter_fit+outer_jut+.25u; % W jut
y1=y4r=h; x2l=x3l;
if monospace: y2=y3=.3h; else: y2=y3=max(stem,1/6h); fi
numeric alpha; x9=.5w-1/2(.5[bot_stem,right_stem]); y9=-o;
alpha=diag_ratio(2,bot_stem,y1-y3,x4r-x1l-2apex_corr);
numeric u_ratio; u_ratio=(y3-y9)/(y1-y9);
x2l-x1l=x4r-x3r+
   if monospace: 2apex_corr
   else: if serifs: 2 fi (u_ratio*.5[x1l,x4r]) fi;
penpos3(alpha*bot_stem,0); penpos4(alpha*right_stem,0);
numeric alpha[]; alpha3=(y1++(x2l-x1l))/y1;
penpos1(alpha3*left_stem,0); penpos2(alpha3*left_stem,0);
z0l=whatever[z1r,z2r]=z4l+whatever*(z3r-z4r); penpos0(alpha*bot_stem,0);
z0'=whatever[rt z3r,rt z4r]; y0'=bot y6r=if serifs:-o else: 0 fi;
penpos6(bot_vair,-90); x6r=.6[x3r,x1];
numeric t;
t=if monospace: 1.25right_stem else:(x3r-x0')++(y3r-y0')fi;
penpos5(bot_vair,.5[angle(z4r-z6r),angle(z4r-z3r)]-90);
path p; numeric tau; p=z0r{z3r-z4r}....{left}z6r;
z5r=p intersectionpoint ((0,y2l-.5bot_vair)--(w,y2l-.5bot_vair));
tau=xpart(p intersectiontimes (z2l--z2r));
if y0>notch_cut+y2:
 y0l:=notch_cut+y2; % +y2 added for sanserif face
 fill z0l+.5right{up}...{z4r-z3r}diag_end(0,4l,1,1,4r,3r)
  --subpath(0,tau) of p--z2l--diag_end(2l,1l,1,1,1r,2r){z2-z1}
  ...{down}z0l+.5left--cycle; % left and right diagonals
else:
 fill z0l--diag_end(0,4l,1,1,4r,3r)--subpath(0,tau) of p
  --z2l--diag_end(2l,1l,1,1,1r,0)--cycle; fi % left and right diagonals
fill p--z6l{right}...
  if x5l>x2l: z2l else: z5l fi
  {-direction tau of p}
 ..{z4r-z3r}z0l--cycle;
pickup fine.nib;
if serifs:
 numeric bulb_diam; bulb_diam=flare+(cap_stem-stem);
 pos6'(bot_vair,-90); z6'=z6; pos7(hair,-180); pos8(bulb_diam,-180);
 y8=1/6h; z7r=z8r; lft x8r=x1;
 cyrbulb(6',7,8);  % arc and bulb
 numeric inner_jut; pickup tiny.nib;
 prime_points_inside(1,2); prime_points_inside(4,3);
 if rt x1'r+jut+.5u+1<=lft x4'l-jut: inner_jut=cap_jut;
  else: rt x1'r+inner_jut+.5u+1=lft x4'l-inner_jut; fi
 dish_serif(1',2,a,1/3,outer_jut,b,1/2,inner_jut);  % left serif
 dish_serif(4',3,c,.6,inner_jut,d,1/2,outer_jut)(dark);  % right serif
else:
 top z6'l=z6l; bot z6'r=z6r;
 pos7(vair',-90); lft x7l=max(3.25u,rt x6r-t); bot y7r=bot y6'r;
 filldraw stroke z6'e--z7e; fi % arc
%%%%%
% the accent
h:=full_h;
cbreve(.5[rt x1r,lft x4l],0, 10,11,12,13,14,15,16)(cyrcaph); %0,0
penlabels(0,0',1,2,3,4,5,6,6',7,8,9, 10,11,12,13,14,15,16); endchar;

% Ukrainian e moved up for wncoding and LH font
iff serifs:%
lhchar "Uppercase Cyrillic letter UKR_E - Ukrainian E (C with mid-line)";
cyrchar(UKR_E,13u#,cap_height#,0);
if is_small_cap: getcharcode(ukr_e); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(0,0);
% upper_ukre;
pickup fine.nib; pos1(cap_hair,0); pos2(cap_band,90);
pos3(cap_curve,180); pos4(cap_band,270); pos5(hair,360);
rt x1r=rt x5r=hround(w-u); lft x3r=hround u; x2=x4=.55[x3,x1];
top y2r=h+o; bot y4r=-o; y3=.5h-vair_corr; y3l:=.52h;
bot y1=min(vround.675h,bot y2l-eps);
y5=max(good.y .95(h-y1),y4l+eps);
(x2l',y2l)=whatever[z2r,z1l]; x2l:=min(x2l',x2l+.5u);
(x4l',y4l)=whatever[z4r,z5l]; x4l:=min(x4l',x4l+.5u);
filldraw stroke z1e{x2-x1,10(y2-y1)}...pulled_super_arc.e(2,3)(.5superpull)
 & pulled_super_arc.e(3,4)(.5superpull)...{up}z5e; % arc
pos6(.3[fine.breadth,cap_hair],0); x6r=x1r; top y6=h+o;
x1r-x1'=2cap_curve-fine; y1'=y1;
path upper_arc; upper_arc=z1{x2-x1,10(y2-y1)}..z2{left};
numeric t; t=xpart(upper_arc intersectiontimes (z6l--z1'));
filldraw z1r--z6r--z6l--subpath(t,0) of upper_arc--cycle; % barb
% upukre_bar;
numeric bar[]; y7=y8=y3;
x7=x3l; x8=(min(lft x1l-.5hair,0.85[x3l,x1l]));
if (serifs=true) and (bar#<=.5curve#):
 penpos7(cap_bar,90); penpos8(cap_bar,90);
 bar2=7/8[cap_bar,fudged.cap_stem-3stem_corr];
 penpos9(bar2,45); z9=.575[z7l,z8l];
 fill circ_stroke z8e{-2,-1}..{-2,1}z9e..{-2,-1}z7e; % curve /\/
else:
 pickup fine.nib; pos7(cap_bar,90); pos8(cap_bar,90);
 filldraw stroke z7e--z8e; fi % bar --
%%%%%
penlabels(1,1',2,3,4,5,6); endchar;

iff not serifs:%
lhchar "Uppercase Cyrillic letter UKR_E - Ukrainian E (C with mid-line)";
cyrchar(UKR_E,11.5u#,cap_height#,0);
if is_small_cap: getcharcode(ukr_e); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(0,0);
% uppers_ukre;
pickup fine.nib; pos1(1.2flare,80); pos2(slab,90);
pos3(cap_curve,180); pos4(slab,270); pos5(flare,275);
rt x1r=hround(w-1.1u); x2=x4=.5w+1.25u;
lft x3r=hround max(u,2u-.5cap_curve); rt x5r=hround(w-.9u);
top y1r=vround.95h+o; top y2r=h+o; y3=.5h;
bot y4r=-o; bot y5r=vround .08h-o; y5l:=good.y y5l; x5l:=good.x x5l;
filldraw stroke rterm.e(2,1,right,.9,4) & super_arc.e(2,3)
 & super_arc.e(3,4) & term.e(4,5,right,.8,4);
% uukres_bar;
numeric bar[]; y6=y7=y3; x6=x3l; x7=(min(lft x1l-.5hair,0.85[x3l,x1l]));
pickup fine.nib;
bar1=if is_small_cap:max(fine.breadth+eps,.6[thin_join,vair]) else: cap_bar fi;
pos6(bar1,90); pos7(bar1,90);
filldraw stroke z6e--z7e; % bar --
%%%%%
penlabels(1,1',2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter G_up - GHE upturn";
cyrchar(G_up,if monospace: 9 else: 11.25 fi u#-width_adj#,
	cap_height#+cap_sbeak#,0);
if is_small_cap: getcharcode(g_up); fi
italcorr cap_height#*slant-beak_jut#-.25u#;
adjust_fit(cap_serif_fit#,0);
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); top y1=cap_height; bot y2=0;
filldraw stroke z1e--z2e; % stem
pickup crisp.nib; pos3(slab,90); pos4(hair,0);
top y3r=cap_height; x3=x1; rt x4r=hround(w-.75u);
y4=good.y(y3r+cap_sbeak)+eps;
pickup if serifs: crisp.nib; else: tiny.nib; fi
if serifs:
% armup(3,4,e,beak_darkness,beak_jut);  % upper arm and beak
 _one:=7; _two:=8; _three:=9;
 x[_one]=good.x(x4r-beak_jut); y[_one]=y3l; y[_two]=y3r;
 z[_two]=z4l-whatever*(z[_one]-z4r); z[_three]=.5[z3r,z[_two]];
 filldraw z4l{z[_two]-z4l}...beak_darkness[z[_two]
  ,.5[z[_three],z4l] ]...z[_three]
  ---z3r--z3l--z[_one]--z4r--cycle; % arm and beak
%%
 nodish_serif(1,2,a,1/3,cap_jut,b,1/3,.5cap_jut); % upper bracketing
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,1.25cap_jut); % lower serif
else:
 rt x5=crisp.rt x4r; y5=y3; pos5(slab,90);
 filldraw stroke z3e--z5e;
 pickup fine.nib; rt x6r=crisp.rt x4r; x6'=x6;
 y6'=y5l; y6=y5r+cap_sbeak;
 numeric light_stem;
 light_stem=max(fine.breadth,fudged.hair if hefty:-4stem_corr fi);
 pos6(light_stem,0); pos6'(light_stem,0);
 filldraw stroke z6e--z6'e; fi
penlabels(0,1,2,3,4,5,6); endchar;

if (more_letters=false) or (wncoding=true): endinput \\; fi

lhchar "Uppercase Cyrillic letter G_hcrs - GHE hcrossed";
cyrchar(G_hcrs,if monospace: 9u# else: 11u# fi-width_adj#, %G_width
 cap_height#,0);
if is_small_cap: getcharcode(g_hcrs); fi
italcorr cap_height#*slant-beak_jut#-.25u#;
adjust_fit(cap_serif_fit#,0);
% upper_g
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround max(2u ,3u-.5cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
pickup crisp.nib; pos3(slab,90); pos4(hair,0);
top y3r=h; x3=x1; rt x4r=hround(w-.75u); y4=good.y(y3l-beak)-eps;
arm(3,4,e,beak_darkness,beak_jut);  % upper arm and beak
if serifs:
 nodish_serif(1,2,a,1/3,cap_jut,b,1/3,.5cap_jut);  % upper serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,1.25cap_jut); fi % lower serif
%%%%%
% hcross
pickup crisp.nib; numeric outer_jut;
outer_jut=if serifs: max(limit_dist,1.1cap_jut) else: 1.8u fi;
pos5(bar,90); pos6(bar,90);
top y6r=top y5r=vround (.52h+.5bar)+eps;
x6=tiny.rt x1r+1.15outer_jut; x5=hround1/3u-cap_serif_fit;
filldraw stroke z5e--z6e;
penlabels(0,1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter G_dsc - GHE descender";
cyrchar(G_dsc,if monospace: 9u# else: 11u# fi-width_adj#, %G_width
 cap_height#,if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth
if is_small_cap: getcharcode(g_dsc); fi
italcorr cap_height#*slant-beak_jut#-.25u#;
adjust_fit(cap_serif_fit#,0);
% upper_g
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround max(2u ,3u-.5cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
pickup crisp.nib; pos3(slab,90); pos4(hair,0);
top y3r=h; x3=x1; rt x4r=hround(w-.75u); y4=good.y(y3l-beak)-eps;
arm(3,4,e,beak_darkness,beak_jut);  % upper arm and beak
%%%
if serifs:
 nodish_serif(1,2,a,1/3,cap_jut,b,1/3,.5cap_jut); % upper serif
 if is_ogonek:
    dish_serif(2,1,c,1/3,cap_jut,d,1/3,1.25cap_jut); fi %!!! % lower serif
fi
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |diag_end| refers to |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x2 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,5,6,7); % ogonek
else:
   numeric right_jut;
   right_jut=if serifs: 1.414cap_jut else: .5cap_stem+.5u fi;
   if not serifs:
      numeric arm_thickness; arm_thickness=vround(slab+vair_corr);
      save slab; slab:=arm_thickness; fi % bottom.slab
   r_serif(2,1,x,1/3,cap_jut,y,1/3,beak_darkness,right_jut,beak_jut); % lower rt descender
fi
penlabels(0,1,2,3,4, 5,6,7); endchar;

lhchar "Uppercase Cyrillic letter G_tl - GHE tail";
cyrchar(G_tl,12.5u#+width_adj#, %V_width+width_adj
 cap_height#,desc_depth#);
if is_small_cap: getcharcode(g_tl); fi
italcorr .75cap_height#*slant-.5u#; %B
adjust_fit(cap_serif_fit#,0); full_w:=w; w:=11u;%G_width
% upper_g
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround max(2u ,3u-.5cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
pickup crisp.nib; pos3(slab,90); pos4(hair,0);
top y3r=h; x3=x1; rt x4r=hround(w-.75u); y4=good.y(y3l-beak)-eps;
arm(3,4,e,beak_darkness,beak_jut);  % upper arm and beak
if serifs:
 nodish_serif(1,2,a,1/3,cap_jut,b,1/3,.5cap_jut);  % upper serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,1.25cap_jut); fi % lower serif
%%%%%
pickup tiny.nib;
w:=full_w;
% Tail_part(2);
_zero:=2;
_one:=5; _two:=6; _three:=7;
_four:=8; _five:=9; _six:=10; _seven:=11; _eight:=12;
%
pickup tiny.nib;
numeric bulb_diam;
bulb_diam=max(currentbreadth+eps,min(.82d-1-eps,flare+(cap_stem-stem)));
penpos[_one](vair,90); penpos[_two](vair,90);
penpos[_three](cap_curve if hefty:-3stem_corr fi,0);
y[_three]=.5[y[_one],-d]; x[_two]=max(.5w-.25u,w-7u);
x[_one]=x[_zero]; x[_three]r=hround(w-u);
x[_two]l:=x[_two]-.25cap_curve; y[_one]=y[_two]=.52h;
{{interim superness:=more_super;
 fill stroke super_arc.e([_three],[_two])..z[_one]e}};  % lobe
if serifs:
 z[_four]=z[_three];
 pos[_four](cap_curve if hefty:-3stem_corr fi,0);
 pos[_five](vair',-90); bot y[_five]r=-d-oo; y[_six]-.5flare=-.85d;
 pos[_six](cap_hair,-180); pos[_seven](bulb_diam,-180);
 rt x[_seven]l=max(lft x[_zero]l-jut+bulb_diam,
  floor x[_three]-3.5u if monospace: / expansion_factor fi);
 z[_six]r=z[_seven]r;
 x[_five]=max(rt x[_seven]l,.6[x[_four],x[_seven]r]);
 (x,y[_five]r)=whatever[z[_five]l,z[_four]l];
 x[_five]r:=max(x,.5[x[_six]r,x[_five]]);
 filldraw stroke super_arc.e([_four],[_five]);
 bulb([_five],[_six],[_seven]); % bulb
else:
 pickup fine.nib; z[_four]=z[_three];
 pos[_four](max(fine.breadth,cap_curve if hefty:-3stem_corr fi),0);
 pos[_eight](vair,-90); pos[_five](flare,-105);
 rt x[_five]r=x[_three]-6u; bot y[_five]r=-d;
 y[_five]l:=good.y y[_five]l; x[_five]l:=good.y x[_five]l;
 bot y[_eight]r=-d-o; x[_eight]=.35[lft x[_five]r,x[_three]r];
 filldraw stroke super_arc.e([_four],[_eight])
  & term.e([_eight],[_five],left,.75,4); fi
%%%
penlabels(0,1,2,3,4,5,6,7,8,9,10,11,12); endchar;

lhchar "Uppercase Cyrillic letter SHHA (as lower h)";
cyrchar(SHHA,13.5u#,cap_height#,0); %CH width
if is_small_cap: getcharcode(shha); fi
italcorr cap_height#*slant-.25u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#);
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
pos3(cap_stem,0); x3=w-x1;
save thin_join; thin_join=cap_hair;
full_x:=x_height; x_height:=1.15x_height;
h_stroke(2,g,3,4); x_height:=full_x; % arch and right stem
if serifs:
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,cap_jut);  % upper rt serif
 numeric inner_jut; pickup tiny.nib;
 if rt x2r+jut+.5u+1<=lft x4l-jut: inner_jut=jut;
  else: rt x2r+jut+.5u+1=lft x4l-inner_jut; fi
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,inner_jut); % lower rt serif
 dish_serif(4,3,e,1/3,inner_jut,f,1/3,cap_jut); fi % lower lft serif
penlabels(1,2,3,4); endchar;

iff not specific:%
lhchar "Uppercase Cyrillic letter ZH_dsc - ZHE descender (`roman')";
cyrchar(ZH_dsc,13.5u#,cap_height#, % N_width
 if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth
if is_small_cap: getcharcode(zh_dsc); fi
italcorr cap_height#*slant-.5u#; %K
adjust_fit(w#-2max(2u#+.5fudged.cap_stem#,3u#)+cap_serif_fit#,cap_serif_fit#); %ZHr_adj
% upperr_zh;
if odd((r-l)-fudged.cap_stem): change_width; fi % symmetric & all three equal
pickup tiny.nib; pos1(fudged.cap_stem,0); pos2(fudged.cap_stem,0);
lft x1l=lft x2l=hround (.5[l,r]-.5fudged.cap_stem)-eps; % middle stem point
top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
if serifs:
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,cap_jut); % upper stem serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,cap_jut); fi % lower stem serif
% K_rbowl;zh%mono:+.75u
numeric right_jut,stem[],alpha[]; pickup tiny.nib;
right_jut=if serifs:.6cap_jut else:.4tiny fi;
stem1=max(tiny.breadth,fudged.hair if hefty:-3stem_corr fi);
stem2=max(tiny.breadth,fudged.cap_stem-3stem_corr);
rt x3r=hround(r-letter_fit-u-right_jut if monospace:+.75u fi); %!!!
rt x6r=hround(r-letter_fit-.75u-right_jut if monospace:+.75u fi); %!!!
y4=1/3h; top y3=h; bot y6=0; x4=x1;
penpos3(alpha1*(stem1-tiny),0); penpos4(whatever,-90);
alpha1=diag_ratio(1,.5(stem1-tiny),y3-y4,x3r-x4);
penpos6(alpha2*(stem2-tiny),0);
alpha2=diag_ratio(1,.5(stem2-tiny),y1-y6,x6r-x1);
forsuffixes $=l,r: y3'$=h; y6'$=0; z4.$=z3'$+whatever*(z3-z4);
 z5.$=z6'$+whatever*(z1-z6)=whatever[z3,z4]; endfor
z5=.5[z5l,z5r];
z3'r=z3r+penoffset z3-z4 of currentpen+whatever*(z3-z4);
% we have also |z3'l=z3l+penoffset z4-z3 of currentpen+whatever*(z3-z4)|;\]
z6'r=z6r+penoffset z1-z6 of currentpen+whatever*(z1-z6);
z6'l=z6l+penoffset z6-z1 of currentpen+whatever*(z1-z6);
fill z4r--diag_end(4r,3'r,1,.5,3'l,4l)--z4l--cycle;  % upper diagonal
fill z5l--diag_end(5l,6'l,.5,1,6'r,5r)--z5r--cycle;  % lower diagonal
numeric inner_jut,up_inner_jut;
%%%
if serifs:
 if rt x2r+cap_jut+.5u+1<=lft x6l-cap_jut: inner_jut=cap_jut;
  else: rt x2r+cap_jut+.5u+1=lft x6l-inner_jut; fi
 if rt x1r+cap_jut+.5u+1<=lft x3l-1.2cap_jut: up_inner_jut=1.2cap_jut;
  else: rt x1r+cap_jut+.5u+1=lft x3l-up_inner_jut; fi
 dish_serif(3,4,e,2/3,up_inner_jut,f,1/2,right_jut)(dark); % upper diagonal serif
 if is_ogonek:
    dish_serif(6,5,g,1/2,inner_jut,h,1/3,right_jut)(dark); fi %!!! % lower diagonal serif
fi
%%%
% mirror
forsuffixes $ = 3r,3,3l,3'r,3'l,4r,4,4l,5l,5,5r,6l,6,6r,6'l,6'r:
 x.m$=l+(r-x$); y.m$=y$; endfor;
fill z.m4r--diag_end(.m4r,.m3'r,1,.5,.m3'l,.m4l)--z.m4l--cycle;  % upper diagonal
fill z.m5l--diag_end(.m5l,.m6'l,.5,1,.m6'r,.m5r)--z.m5r--cycle;  % lower diagonal
%%
if serifs:
 penpos7(alpha1*(stem1-tiny),0); penpos8(whatever,-90);
 penpos10(alpha2*(stem2-tiny),0);
 z7=z.m3; z8=z.m4; z9l=z.m5r; z9=z.m5; z9r=z.m5l; z10=z.m6;
 dish_serif(7,8,i,1/2,right_jut,j,2/3,up_inner_jut)(dark); % upper diagonal serif
 dish_serif(10,9,k,1/3,right_jut,l,1/2,inner_jut)(dark); fi % lower diagonal serif
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |diag_end| refers to |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x6 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,11,12,13); % ogonek
else:
   if not serifs:
      right_jut:=max(right_jut,hround (r-rt x6r-.65u)); fi
   r_serif(6,1,x,1/3,jut,y,1/3,beak_darkness,right_jut,beak_jut); % lower rt descender
fi
penlabels(1,2,3,4,5,6,7,8,9,10, 11,12,13); endchar;

lhchar "Uppercase Cyrillic letter Z_dsc - ZE descender (cedilla)";
cyrchar(Z_dsc,11u#,cap_height#,desc_depth#);
if is_small_cap: getcharcode(z_dsc); fi
italcorr .75cap_height#*slant-.5u#; %B
adjust_fit(if monospace: .35u#,.25u# else: 0,0 fi);
% upper_z;
pickup fine.nib; numeric right_curve,middle_weight;
middle_weight=max(fine.breadth,.6vair+.5);
pos2(cap_band,90); pos8(cap_band,-90);
if serifs:
   right_curve=cap_curve-stem_corr;
   pos1(cap_hair,180); pos9(cap_hair,-180); y1=.4[y5l,y2l];
else:
   right_curve=cap_curve-3stem_corr;
   pos1(1.1flare,120); pos9(flare,-120);
   top y1r=vround 7/8h+o; bot y9r=vround 1/7h-o; fi
pos4(middle_weight,-90); pos5(middle_weight,-90);
pos6(middle_weight,90); pos5'(middle_weight,90);
pos3(max(fine.breadth,right_curve-stem_corr),0);
pos7(max(fine.breadth,right_curve),0);
top y2r=h+o; bot y8r=-o;
x2=x6=x8=.5[1.5u,x7]; rt x7r=hround(w-.75u); rt x3r=hround(w-1.25u);
lft x5=lft x5'=min(hround 3.5u,lft x6)+eps;
y3=.5[y4l,y2l]; y7=.5[y6l,y8l]; x4=1/3[x5,x3l]; y4=y5; y5'=y6;
lft x1r=hround 1.1u; lft x9r=hround.9u;
top y5l-bot y5'l=vair; .5[y5l,y5'l]=.52h;
if serifs: y9=max(.52[top y8l,bot y5'l],y7+1);
   else: y9l:=good.y y9l; x9l:=good.x x9l; fi
filldraw stroke {{if not serifs:interim superness:=hein_super; fi
 super_arc.e(2,3)}} & super_arc.e(3,4)..z5e; % upper bowl
filldraw stroke z5'e..super_arc.e(6,7) & super_arc.e(7,8); % lower bowl
if serifs:
   filldraw stroke z1e{x2-x1,15(y2-y1)}....{right}z2e; % upper arc
   filldraw stroke
       {{interim superness:=more_super; super_arc.e(8,9)}}; % lower arc
else:
   filldraw stroke rterm.e(2,1,left,.9,4); % upper arc
   filldraw stroke  term.e(8,9,left,.8,4); % lower arc
fi
if serifs:
 path upper_arc; upper_arc=z1{x2-x1,15(y2-y1)}....{right}z2;
 pos10(.3[fine.breadth,cap_hair],0); x10l=x1r; top y10=h+o;
 x1'-x1l=2cap_curve; y1'=y1;
 numeric t; t=xpart(upper_arc intersectiontimes (z10r--z1'));
 filldraw z1r--z10l--z10r--subpath(t,0) of upper_arc--cycle; fi % upper serif
%%%%%
% uppercase cedilla/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   ogonek_pos=(x8r,0);
   easy_ogonek(the_pen,11,12,13); % ogonek
else:
   uppercase_cedilla(x8,0, 11,12,13,14,15);
fi
penlabels(0,1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15); endchar;

lhchar "Uppercase Cyrillic letter ABH_DZE - Abkhasian DZE";
cyrchar(ABH_DZE,10u#,cap_height#,0);
if is_small_cap: getcharcode(abh_dze); fi
italcorr cap_height#*slant-.5u#; %K
adjust_fit(0,.5serif_fit#);
pickup if serifs: tiny.nib else: fine.nib fi;
numeric arm_thickness,z_stem[];
z_stem1=max(currentbreadth,hround(if hefty:1/3[vair,hair] else: fudged.hair fi));
arm_thickness=if hefty: 2/3 else:.75 fi[slab,cap_stem];
if z_stem1<currentbreadth+eps: z_stem1:=currentbreadth+eps; fi
rt x1r=rt x2r=hround(w-.8u)+eps;
lft x3l=lft x4l=good.x(hround.3[.5u,w-.8u])-eps;
top y1=h; bot y2=min(bot y1,good.y(h-arm_thickness));
bot y4=good.y.(.52h-.5vair)-eps;
y3=max(y4,good.y(bot y4+vair-.5currentbreadth)-eps);
numeric alpha; alpha=diag_ratio(1,.5(z_stem1-currentbreadth),y2-y4,x2r-x3l);
z_stem2=alpha*(z_stem1-currentbreadth); %
if z_stem2<eps: z_stem2:=eps; fi
penpos1(z_stem2,0); penpos2(z_stem2,0);
penpos3(z_stem2,0); penpos4(z_stem2,0);
pair delta; delta=penoffset z3-z2 of currentpen;
fill top z1l--z2l+delta---z3l+delta..lft z3l---lft z4l..bot z4l
 ---bot z4r--z3r-delta---z2r-delta..rt z2r---rt z1r..top z1r
 ---cycle;  % diagonal
pos7(cap_curve,0); pos8(vair',-90); pos12(rt x4r-lft x4l,180); pos13(vair,90);
x12r=x4l; y12=y4; rt x7r=hround(w-.75u); y7=.5[bot y13l,top y8l];
bot y8r=-o; x8=hround.5[.5u,w-.8u]; x13=max(x8,rt x12l+eps);
y13=1/15[y4,h]; y13l:=min(y13l,.5[y4,y13l]);
filldraw stroke z12e{z2l-z3l}...{right}z13e; %link
filldraw stroke pulled_arc.e(13,7) & pulled_arc.e(7,8);  % bowl
if serifs:
 numeric bulb_diam[]; bulb_diam2=flare+(cap_stem-stem);
 pos10(bulb_diam2,-180); pos9(cap_hair,-180);
 lft x10r=hround .75u;
 y10+.5bulb_diam2=max(.52[top y8l,bot y13l],y7+1);
 {{less_tense; bulb(8,9,10)}};  % lower bulb
else:
 pos9(max(currentbreadth+eps,cap_hair-.2tiny),-175); lft x9r=hround .75u; y9=.23h;
 filldraw stroke z8e{left}....{dir 95}z9e; fi  % lower terminal
pickup crisp.nib;
pos5(max(h-bot y2l-.5crisp-vair_corr,crisp.breadth),90); pos6(hair,180);
top y5r=h; x5=x1; lft x6r=hround .75u;
y6=max(good.y(if serifs: h-slab-beak else: y5l-vair_corr fi)-eps,y3+1);
arm(5,6,a,beak_darkness,-beak_jut);  % upper arm and beak
penlabels(1,2,3,4,5,6,7,8,9,10,12,13); endchar;

% Ukrainian i moved up for LH font

iff not specific:%
lhchar "Uppercase Cyrillic letter K_dsc - KA descender (`roman')";
cyrchar(K_dsc,13.5u#,cap_height#, % N_width
 if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth
if is_small_cap: getcharcode(k_dsc); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#); %ur_serif_adj
% upperr_k;
pickup tiny.nib; pos1(fudged.cap_stem,0); pos2(fudged.cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5fudged.cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
if serifs:
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,cap_jut); % upper stem serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,cap_jut); fi % lower stem serif
% K_rbowl;
numeric right_jut,stem[],alpha[]; pickup tiny.nib;
right_jut=if serifs:.6cap_jut else:.4tiny fi;
stem1=max(tiny.breadth,fudged.hair if hefty:-3stem_corr fi);
stem2=max(tiny.breadth,fudged.cap_stem-3stem_corr);
rt x3r=hround(r-letter_fit-u-right_jut);
rt x6r=hround(r-letter_fit-.75u-right_jut);
y4=1/3h; top y3=h; bot y6=0; x4=x1;
penpos3(alpha1*(stem1-tiny),0); penpos4(whatever,-90);
alpha1=diag_ratio(1,.5(stem1-tiny),y3-y4,x3r-x4);
penpos6(alpha2*(stem2-tiny),0);
alpha2=diag_ratio(1,.5(stem2-tiny),y1-y6,x6r-x1);
forsuffixes $=l,r: y3'$=h; y6'$=0; z4.$=z3'$+whatever*(z3-z4);
 z5.$=z6'$+whatever*(z1-z6)=whatever[z3,z4]; endfor
z5=.5[z5l,z5r];
z3'r=z3r+penoffset z3-z4 of currentpen+whatever*(z3-z4);
% we have also |z3'l=z3l+penoffset z4-z3 of currentpen+whatever*(z3-z4)|;\]
z6'r=z6r+penoffset z1-z6 of currentpen+whatever*(z1-z6);
z6'l=z6l+penoffset z6-z1 of currentpen+whatever*(z1-z6);
fill z4r--diag_end(4r,3'r,1,.5,3'l,4l)--z4l--cycle;  % upper diagonal
fill z5l--diag_end(5l,6'l,.5,1,6'r,5r)--z5r--cycle;  % lower diagonal
numeric inner_jut,up_inner_jut;
%%%
if serifs:
 if rt x2r+cap_jut+.5u+1<=lft x6l-cap_jut: inner_jut=cap_jut;
  else: rt x2r+cap_jut+.5u+1=lft x6l-inner_jut; fi
 if rt x1r+cap_jut+.5u+1<=lft x3l-1.2cap_jut: up_inner_jut=1.2cap_jut;
  else: rt x1r+cap_jut+.5u+1=lft x3l-up_inner_jut; fi
 dish_serif(3,4,e,2/3,up_inner_jut,f,1/2,right_jut)(dark);  % upper diagonal serif
 if is_ogonek:
    dish_serif(6,5,g,1/2,inner_jut,h,1/3,right_jut)(dark); fi % lower diagonal serif
fi
%%%
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |diag_end| refers to |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x6 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   if not serifs:
      right_jut:=max(right_jut,hround (r-rt x6r-.65u)); fi
   r_serif(6,1,x,1/3,cap_jut,y,1/3,beak_darkness,right_jut,beak_jut); % lower left serif
fi
penlabels(1,2,3,4,5,6, 7,8,9); endchar;

iff not specific:%
lhchar "Uppercase Cyrillic letter K_beak - KA beak (`roman')";
cyrchar(K_beak,13.5u#,cap_height#,0); % N_width
if is_small_cap: getcharcode(k_beak); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(min(4.5u#-.5cap_stem#,3.5u#)-.5width_adj#, % Beak_adj
 cap_serif_fit#);
% upperr_k;beak
pickup tiny.nib; pos1(fudged.cap_stem,0); pos2(fudged.cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5fudged.cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
if serifs:
 nodish_serif(1,2,a,1/3,cap_jut,b,1/3,cap_jut); %!!! upper stem serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,cap_jut); fi % lower stem serif
% K_rbowl;
numeric right_jut,stem[],alpha[]; pickup tiny.nib;
right_jut=if serifs:.6cap_jut else:.4tiny fi;
stem1=max(tiny.breadth,fudged.hair if hefty:-3stem_corr fi);
stem2=max(tiny.breadth,fudged.cap_stem-3stem_corr);
rt x3r=hround(r-letter_fit-u-right_jut);
rt x6r=hround(r-letter_fit-.75u-right_jut);
y4=1/3h; top y3=h; bot y6=0; x4=x1;
penpos3(alpha1*(stem1-tiny),0); penpos4(whatever,-90);
alpha1=diag_ratio(1,.5(stem1-tiny),y3-y4,x3r-x4);
penpos6(alpha2*(stem2-tiny),0);
alpha2=diag_ratio(1,.5(stem2-tiny),y1-y6,x6r-x1);
forsuffixes $=l,r: y3'$=h; y6'$=0; z4.$=z3'$+whatever*(z3-z4);
 z5.$=z6'$+whatever*(z1-z6)=whatever[z3,z4]; endfor
z5=.5[z5l,z5r];
z3'r=z3r+penoffset z3-z4 of currentpen+whatever*(z3-z4);
% we have also |z3'l=z3l+penoffset z4-z3 of currentpen+whatever*(z3-z4)|;\]
z6'r=z6r+penoffset z1-z6 of currentpen+whatever*(z1-z6);
z6'l=z6l+penoffset z6-z1 of currentpen+whatever*(z1-z6);
fill z4r--diag_end(4r,3'r,1,.5,3'l,4l)--z4l--cycle;  % upper diagonal
fill z5l--diag_end(5l,6'l,.5,1,6'r,5r)--z5r--cycle;  % lower diagonal
numeric inner_jut,up_inner_jut;
%%
if serifs:
 if rt x2r+cap_jut+.5u+1<=lft x6l-cap_jut: inner_jut=cap_jut;
  else: rt x2r+cap_jut+.5u+1=lft x6l-inner_jut; fi
 if rt x1r+cap_jut+.5u+1<=lft x3l-1.2cap_jut: up_inner_jut=1.2cap_jut;
  else: rt x1r+cap_jut+.5u+1=lft x3l-up_inner_jut; fi
 dish_serif(3,4,e,2/3,up_inner_jut,f,1/2,right_jut)(dark); % upper diagonal serif
 dish_serif(6,5,g,1/2,inner_jut,h,1/3,right_jut)(dark); fi % lower diagonal serif
%%%
%%%%%
% upper left beak
pickup crisp.nib;
top y7r=h; x7=x1; pos7(slab,90); pos8(hair,180);
lft x8r=l+letter_fit+hround.65u; y8=good.y(y7l-beak)-eps;
arm(7,8,q,beak_darkness,-.7beak_jut);
penlabels(1,2,3,4,5,6, 7,8); endchar;

iff not specific:%
lhchar "Uppercase Cyrillic letter K_tl - KA tail (`roman')";
cyrchar(K_tl,13.5u#,cap_height#,desc_depth#); % N_width
if is_small_cap: getcharcode(k_tl); fi
italcorr cap_height#*slant-.5u#; %K
adjust_fit(cap_serif_fit#,cap_serif_fit#);
pickup tiny.nib; pos1(fudged.cap_stem,0); pos2(fudged.cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5fudged.cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; % stem
% Tail_part(2);
_zero:=2;
_one:=7; _two:=8; _three:=9;
_four:=10; _five:=11; _six:=12; _seven:=13; _eight:=14;
%
pickup tiny.nib;
numeric bulb_diam;
bulb_diam=max(currentbreadth+eps,min(.82d-1-eps,flare+(cap_stem-stem)));
penpos[_one](vair,90); penpos[_two](vair,90);
penpos[_three](cap_curve if hefty:-3stem_corr fi,0);
y[_three]=.5[y[_one],-d]; x[_two]=max(.5w-.25u,w-7u);
x[_one]=x[_zero]; x[_three]r=hround(w-u);
x[_two]l:=x[_two]-.25cap_curve; y[_one]=y[_two]=.52h;
{{interim superness:=more_super;
 fill stroke super_arc.e([_three],[_two])..z[_one]e}};  % lobe
if serifs:
 z[_four]=z[_three];
 pos[_four](cap_curve if hefty:-3stem_corr fi,0);
 pos[_five](vair',-90); bot y[_five]r=-d-oo; y[_six]-.5flare=-.85d;
 pos[_six](cap_hair,-180); pos[_seven](bulb_diam,-180);
 rt x[_seven]l=max(lft x[_zero]l-jut+bulb_diam,
  floor x[_three]-3.5u if monospace: / expansion_factor fi);
 z[_six]r=z[_seven]r;
 x[_five]=max(rt x[_seven]l,.6[x[_four],x[_seven]r]);
 (x,y[_five]r)=whatever[z[_five]l,z[_four]l];
 x[_five]r:=max(x,.5[x[_six]r,x[_five]]);
 filldraw stroke super_arc.e([_four],[_five]);
 bulb([_five],[_six],[_seven]); % bulb
else:
 pickup fine.nib; z[_four]=z[_three];
 pos[_four](max(fine.breadth,cap_curve if hefty:-3stem_corr fi),0);
 pos[_eight](vair,-90); pos[_five](flare,-105);
 rt x[_five]r=x[_three]-6u; bot y[_five]r=-d;
 y[_five]l:=good.y y[_five]l; x[_five]l:=good.y x[_five]l;
 bot y[_eight]r=-d-o; x[_eight]=.35[lft x[_five]r,x[_three]r];
 filldraw stroke super_arc.e([_four],[_eight])
  & term.e([_eight],[_five],left,.75,4); fi
%%%
% udiagonal;
pickup tiny.nib;
numeric right_jut,stem[],alpha[];
right_jut=if serifs:.6cap_jut else:.4tiny fi;
stem1=max(tiny.breadth,fudged.hair if hefty:-3stem_corr fi);
top y3=h; rt x3r=hround(r-letter_fit-u-right_jut); x4=x1; y4=1/3h;
alpha1=diag_ratio(1,.5(stem1-tiny),y3-y4,x3r-x4);
penpos3(alpha1*(stem1-tiny),0); penpos4(whatever,-90);
forsuffixes $=l,r: y3'$=h; y4'$=.52h;
z4'$=z3'$+whatever*(z3-z4); z4$=z3'$+whatever*(z3-z4); endfor
z3'r=z3r+penoffset z3-z4 of currentpen+whatever*(z3-z4);
z4'=.5[z4'l,z4'r]; pickup fine.nib;
fill z4'r--diag_end(4r,3'r,1,.5,3'l,4l)--z4'l--cycle;  % upper diagonal
%%
if serifs:
 numeric inner_jut,up_inner_jut;
 z1'=z1; pos1'(fudged.cap_stem,0); z2'=z2; pos2'(fudged.cap_stem,0);
 if rt x1'r+cap_jut+.5u+1<=lft x3l-1.2cap_jut: up_inner_jut=1.2cap_jut;
  else: rt x1'r+cap_jut+.5u+1=lft x3l-up_inner_jut; fi
 if rt x2r+cap_jut+.5u+1<=x[_three]r: inner_jut=cap_jut;
  else: rt x2r+inner_jut+.5u+1=x[_three]r; fi
 dish_serif(3,4',e,2/3,up_inner_jut,f,1/2,right_jut)(dark); % upper diagonal serif
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,cap_jut); % upper stem serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,inner_jut); fi % lower stem serif
penlabels(1,2,3,4,5,6, 7,8,9,10,11,12,13,14); endchar;

lhchar "Uppercase Cyrillic letter L_tl - EL tail";
cyrchar(L_tl,13.5u#,cap_height#,desc_depth#); % DL_width:N_width
if is_small_cap: getcharcode(l_tl); fi
italcorr cap_height#*slant-cap_serif_fit#+cap_jut#-2.5u#+min(.5cap_stem#,u#);
adjust_fit(cap_serif_fit# if not serifs:+.5(cap_stem#-u#) fi, %DL_adj
 cap_serif_fit#); %
% upper_l;
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
rt x1r=rt x2r=w-hround max(2u,3u-.5cap_stem); top y1=h; bot y2=0;
filldraw stroke z1e--z2e; %stem
numeric bulb_diam; bulb_diam=flare+(cap_stem-stem);
numeric light_hair,bot_vair,left_jut;
light_hair=max(tiny.breadth,fudged.hair if hefty:-4stem_corr fi);
left_jut=
   if serifs: max(limit_dist,1.1cap_jut)
   else:      hround (w+cap_serif_fit-rt x2r-.65u) fi;
bot_vair=Vround(.5[vair,light_hair]);
x3=1/3[w-rt x1r-(1/6left_jut),rt x1r] if not serifs:-.5(cap_stem-u) fi+eps;
if serifs:
 pos3(light_hair,0); pos4(light_hair,-90);
 pos6(bulb_diam,-180); pos5(cap_hair,-180);
 top y3=h; y6=1/6h-max(0,flare-stem-2vair_corr-.5); z6r=z5r;
 lft x6r=l+letter_fit+hround(.5u-.5);
 x4=max(lft x5r+.5bulb_diam,.4[lft x5r,rt x3r]); bot y4r=-o;
 filldraw stroke z4e{right}..
  controls (min(15/16[x4,x3],max(x4,lft x6r+2flare-eps))+(x3e-x3),y4e)
  and (x3e,good.y(min(y6-.5,1/3h-eps)+1/3(y4e-y4)))..z3e; % left tail
 {{less_tense; bulb(4,5,6)}};
else:
 pos3'(slab,90); pos1'(slab,90);
 lft x3'=x3-.5light_hair; rt x1'=x1r; top y3'r=top y1'r=h;
 filldraw stroke z3'e--z1'e;
 pickup fine.nib; pos3(max(fine.breadth,light_hair),0); pos4(bot_vair,-90); pos5(bot_vair,-90);
 bot y5r=0; x4=2/3[x3l,x5r]; lft x5=l+letter_fit+hround(.35u-.5);
 bot y4r=bot y5r; y3=y3'; x4l:=.25[x4,x5];
 path p;
 p=stroke z3{down}
   ..controls (x3,y5+.35h-eps) and (min(7/8[x4,x3],x5+2.5light_hair+.5u),y4)
   ..{left}z4--z5;
 z6=point .35 of p;
 pos6(max(fine.breadth,7/8[bot_vair,light_hair]),angle(direction .35 of p)+90);
 filldraw stroke z3e{down}...z6e{direction .35 of p}...{left}z4e--z5e; fi % arc
%%%
if serifs:
 x0=x3; y0=0;
 numeric inner_jut; inner_jut=.5(x1l-x3r)+.5crisp+eps;
 %dish_serif(2,1,a,1/3,cap_jut,b,1/3,cap_jut);       %lower right serif
 nodish_serif(1,2,c,1/3,inner_jut,d,1/3,cap_jut);   %upper right serif
 nodish_serif(3,0,e,1/3,1.05cap_jut,f,1/3,inner_jut); fi %upper left serif
%%%%%
% L_tail(2);
_zero:=2;
_one:=7; _two:=8; _three:=9; _four:=10; _five:=11;
%
pickup fine.nib;
pos[_five](tiny.rt x[_zero]r-tiny.lft x[_zero]l,0);
pos[_one](tiny.rt x[_zero]r-tiny.lft x[_zero]l,0);
z[_five]=z[_zero]; x[_one]r=x[_five]r;
bot y[_one]=max(-1/3d,-d+if serifs: vair' else: slab fi+eps);
if serifs:
 numeric bulb_diam;
 bulb_diam=max(currentbreadth+eps,min(.82d-1-eps,flare+(cap_stem-stem)));
 pos[_two](vair',-90); pos[_three](hair,-180);
 pos[_four](bulb_diam,-180);
 x[_two]=.5[x[_one],x[_three]r];
 bot y[_two]r=-d-oo; y[_three]-.5bulb_diam=-.85d;
 z[_three]r=z[_four]r;
 rt x[_four]l=floor x[_zero]-2.5u if monospace: / expansion_factor fi;
 filldraw stroke z[_five]e--pulled_arc.e([_one],[_two]);
 bulb([_two],[_three],[_four]); % bulb
else:
 pos[_two](slab,-90); pos[_three](flare,-105);
 rt x[_three]r=x[_zero]-4u; bot y[_three]r=-d;
 bot y[_two]r=-d-o; x[_two]=.4[x[_three]r,x[_zero]r];
 filldraw stroke z[_five]e--super_arc.e([_one],[_two])
  & term.e([_two],[_three],left,.8,4); fi
%%%
penlabels(0,1,2,3,4,5,6, 7,8,9,10,11); endchar;

lhchar "Uppercase Cyrillic letter N_dsc - EN descender";
cyrchar(N_dsc,13.5u#,cap_height#,
 if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth
if is_small_cap: getcharcode(n_dsc); fi
italcorr cap_height#*slant-cap_serif_fit#+cap_jut#-2.5u#+min(.5cap_stem#,u#);
adjust_fit(cap_serif_fit#,
 cap_serif_fit# if not serifs:+.5(cap_stem#-u#) fi); %DL_adj
% upper_n;
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
pos3(cap_stem,0); pos4(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); x3=x4=w-x1;
top y1=top y3=h; bot y2=bot y4=0;
filldraw stroke z1e--z2e; % left stem
filldraw stroke z3e--z4e; % right stem
penpos5(cap_bar,90); penpos6(cap_bar,90);
x5=x1; x6=x3; y5=y6=.52h;
fill stroke z5e--z6e;  % bar
%%%
if serifs:
 numeric inner_jut;
 if rt x1r+cap_jut+.5u+1<=lft x3l-cap_jut: inner_jut=cap_jut;
  else: rt x1r+inner_jut+.5u+1=lft x3l-inner_jut; fi
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,inner_jut); % upper left serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,inner_jut); % lower left serif
 dish_serif(3,4,e,1/3,inner_jut,f,1/3,cap_jut); % upper right serif
 if is_ogonek:
    dish_serif(4,3,g,1/3,inner_jut,h,1/3,cap_jut); fi  % lower right serif
fi
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |stem| is drawn by |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x4 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   numeric right_jut;
   right_jut=if serifs: max(limit_dist,1.1cap_jut) else: hround (r-rt x4r-.65u) fi;
   if not serifs:
      numeric arm_thickness; arm_thickness=vround(slab+vair_corr);
      save slab; slab:=arm_thickness; fi %bot.slab
   r_serif(4,3,x,1/3,cap_jut,y,1/3,beak_darkness,right_jut,beak_jut); % lower rt descender
fi
penlabels(1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter NG - ligature ENGHE";
cyrchar(NG,13.5u#,cap_height#,0);
if is_small_cap: getcharcode(ng); fi;
italcorr cap_height#*slant-beak_jut#; %Gamma+.25u
adjust_fit(cap_serif_fit#,min(4.5u#-.5stem#,3.5u#)-.5width_adj#); % Beak_adj
% upper_n;
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
pos3(cap_stem,0); pos4(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); x3=x4=w-x1;
top y1=top y3=h; bot y2=bot y4=0;
filldraw stroke z1e--z2e; % left stem
filldraw stroke z3e--z4e; % right stem
penpos5(cap_bar,90); penpos6(cap_bar,90);
x5=x1; x6=x3; y5=y6=.52h;
fill stroke z5e--z6e;  % bar
%%%
if serifs: numeric inner_jut;
 if rt x1r+cap_jut+.5u+1<=lft x3l-cap_jut: inner_jut=cap_jut;
  else: rt x1r+inner_jut+.5u+1=lft x3l-inner_jut; fi
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,inner_jut);  % upper left serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,inner_jut); % lower left serif
 nodish_serif(3,4,e,1/3,inner_jut,f,1/3,.5cap_jut);  %!!!% upper rt serif
 dish_serif(4,3,g,1/3,inner_jut,h,1/3,cap_jut); fi  % lower rt serif
%%%%%
% upper right beak
pickup crisp.nib; pos7(slab,90); pos8(hair,0);
top y7r=h; x7=x3; rt x8r=hround(r-.75u); y8=good.y(y7l-beak)-eps;
arm(7,8,i,beak_darkness,.7beak_jut);  % upper arm and beak
penlabels(1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter N_tl -  EN tail";
cyrchar(N_tl,13.5u#,cap_height#,desc_depth#);
if is_small_cap: getcharcode(n_tl); fi
italcorr cap_height#*slant-cap_serif_fit#+cap_jut#-2.5u#+min(.5cap_stem#,u#);
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_n;
pickup tiny.nib; pos1(cap_stem,0); pos2(cap_stem,0);
pos3(cap_stem,0); pos4(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); x3=x4=w-x1;
top y1=top y3=h; bot y2=bot y4=0;
filldraw stroke z1e--z2e; % left stem
filldraw stroke z3e--z4e; % right stem
penpos5(cap_bar,90); penpos6(cap_bar,90);
x5=x1; x6=x3; y5=y6=.52h;
fill stroke z5e--z6e;  % bar
%%%
if serifs:
 numeric inner_jut;
 if rt x1r+cap_jut+.5u+1<=lft x3l-cap_jut: inner_jut=cap_jut;
  else: rt x1r+inner_jut+.5u+1=lft x3l-inner_jut; fi
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,inner_jut);  % upper left serif
 dish_serif(2,1,c,1/3,cap_jut,d,1/3,inner_jut); % lower left serif
 dish_serif(3,4,e,1/3,inner_jut,f,1/3,cap_jut); fi % upper rt serif
 %dish_serif(4,3,g,1/3,inner_jut,h,1/3,cap_jut); fi  % lower rt serif
%%%%%
% L_tail(4);
_zero:=4;
_one:=7; _two:=8; _three:=9; _four:=10; _five:=11;
%
pickup fine.nib;
pos[_five](tiny.rt x[_zero]r-tiny.lft x[_zero]l,0);
pos[_one](tiny.rt x[_zero]r-tiny.lft x[_zero]l,0);
z[_five]=z[_zero]; x[_one]r=x[_five]r;
bot y[_one]=max(-1/3d,-d+if serifs: vair' else: slab fi+eps);
if serifs:
 numeric bulb_diam;
 bulb_diam=max(currentbreadth+eps,min(.82d-1-eps,flare+(cap_stem-stem)));
 pos[_two](vair',-90); pos[_three](hair,-180);
 pos[_four](bulb_diam,-180);
 x[_two]=.5[x[_one],x[_three]r];
 bot y[_two]r=-d-oo; y[_three]-.5bulb_diam=-.85d;
 z[_three]r=z[_four]r;
 rt x[_four]l=floor x[_zero]-2.5u if monospace: / expansion_factor fi;
 filldraw stroke z[_five]e--pulled_arc.e([_one],[_two]);
 bulb([_two],[_three],[_four]); % bulb
else:
 pos[_two](slab,-90); pos[_three](flare,-105);
 rt x[_three]r=x[_zero]-4u; bot y[_three]r=-d;
 bot y[_two]r=-d-o; x[_two]=.4[x[_three]r,x[_zero]r];
 filldraw stroke z[_five]e--super_arc.e([_one],[_two])
  & term.e([_two],[_three],left,.8,4); fi
%%%
penlabels(1,2,3,4,5,6, 7,8,9,10,11); endchar;

% O tilded moved up for wncoding
iff serifs:
lhchar "Uppercase Cyrillic letter S_dsc - ES descender (cedilla)";
cyrchar(S_dsc,13u#,cap_height#,desc_depth#);
if is_small_cap: getcharcode(s_dsc); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(0,0);
% upper_s;
pickup fine.nib; pos1(cap_hair,0); pos2(cap_band,90);
pos3(cap_curve,180); pos4(cap_band,270); pos5(hair,360);
rt x1r=rt x5r=hround(w-u); lft x3r=hround u; x2=x4=.55[x3,x1];
top y2r=h+o; bot y4r=-o; y3=.5[y2,y4];
bot y1=min(vround max(.6h,x_height-.5vair),bot y2l-eps);
y5=max(good.y .95(h-y1),y4l+eps);
(x2l',y2l)=whatever[z2r,z1l]; x2l:=min(x2l',x2l+.5u);
(x4l',y4l)=whatever[z4r,z5l]; x4l:=min(x4l',x4l+.5u);
filldraw stroke z1e{x2-x1,10(y2-y1)}
 ...pulled_arc.e(2,3) & pulled_arc.e(3,4)...{up}z5e; % arc
pos6(.3[fine.breadth,cap_hair],0); x6r=x1r; top y6=h+o;
x1r-x1'=2cap_curve-fine; y1'=y1;
path upper_arc; upper_arc=z1{x2-x1,10(y2-y1)}..z2{left};
numeric t; t=xpart(upper_arc intersectiontimes (z6l--z1'));
filldraw z1r--z6r--z6l--subpath(t,0) of upper_arc--cycle; % barb
%%%%%
% uppercase cedilla/ogonek
if is_ogonek:
   path p.r;
   p.r=pulled_arc.r(3,4)...{up}z5r;
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   numeric tt; % the time...
   tt=if hefty: 2.6 else: 2.5 fi;
   join_angle=angle((direction tt of p.r) rotated 180);
   ogonek_pos=(point tt of p.r)+.5(the_pen-if serifs: tiny else: fine fi)*
      unitvector((direction tt of p.r) rotated 90)-(ogonek_breadth-the_pen,0);
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   uppercase_cedilla(x4,0, 7,8,9,10,11);
fi
penlabels(1,1',2,3,4,5,6, 7,8,9,10,11); endchar;

iff not serifs:%
lhchar "Uppercase Cyrillic letter S_dsc - ES descender (cedilla)";
cyrchar(S_dsc,11.5u#,cap_height#,desc_depth#);
if is_small_cap: getcharcode(s_dsc); fi
italcorr cap_height#*slant-.5u#;
adjust_fit(0,0);
% uppers_s;
pickup fine.nib; pos1(1.2flare,80); pos2(slab,90);
pos3(cap_curve,180); pos4(slab,270); pos5(flare,275);
rt x1r=hround(w-1.1u); x2=x4=.5w+1.25u;
lft x3r=hround max(u,2u-.5cap_curve); rt x5r=hround(w-.9u);
top y1r=vround .95h+o; top y2r=h+o; y3=.5h;
bot y4r=-o; bot y5r=vround .08h-o; y5l:=good.y y5l; x5l:=good.x x5l;
filldraw stroke rterm.e(2,1,right,.9,4) & super_arc.e(2,3)
 & super_arc.e(3,4) & term.e(4,5,right,.8,4); % arc and terminals
%%%%%
% uppercase cedilla/ogonek
if is_ogonek:
   path p.r;
   p.r=term.r(4,5,right,.8,4);
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   numeric tt; % the time...
   tt=if is_bold: .3 else: .2 fi;
   join_angle=angle((direction tt of p.r) rotated 180);
   ogonek_move=-hi.o;
   ogonek_pos=(point tt of p.r)+.5(the_pen-if serifs: tiny else: fine fi)*
      unitvector((direction tt of p.r) rotated 90)-(ogonek_breadth-the_pen,0);
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   uppercase_cedilla(x4,0, 6,7,8,9,10);
fi
penlabels(1,1',2,3,4,5, 6,7,8,9,10); endchar;

% Short U moved up for LH fonts

lhchar "Uppercase Cyrillic letter Y - straight U (looks like Y)";
cyrchar(Y,13u#,cap_height#,0);
if is_small_cap: getcharcode(y); fi
italcorr cap_height#*slant+.45u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_y;
numeric left_stem,right_stem,outer_jut,dy,alpha;
left_stem=cap_stem-stem_corr;
right_stem=min(cap_hair if hefty:-2stem_corr fi,left_stem);
outer_jut=.75cap_jut; x1l=w-x4r=l+letter_fit+outer_jut+.05u;
x2l=x5l=hround(.5w-.5left_stem); x3r=x5r; y1=y4=h; y2=y3=y5=.4h;
dy=y1-y2; alpha=((x2l-x1l)++dy)/dy;
penpos1(alpha*left_stem,0); penpos2(alpha*left_stem,0);
penpos3(alpha*right_stem,0); penpos4(alpha*right_stem,0);
penpos5(left_stem,0); penpos6(left_stem,0); x5=x6; y6=0;
z0=whatever[z1r,z2r]=whatever[z3l,z4l];
if y0>y2+cap_notch_cut:
 y0:=y2+cap_notch_cut;
 fill z0+.5right{up}...{z4-z3}diag_end(3l,4l,1,1,4r,3r)--z5r
  --diag_end(5r,6r,1,1,6l,5l)--z5l--diag_end(2l,1l,1,1,1r,2r){z2-z1}
  ...{down}z0+.5left--cycle; % diagonals and stem
else:
 fill z0--diag_end(0,4l,1,1,4r,3r)--z5r
  --diag_end(5r,6r,1,1,6l,5l)--z5l
  --diag_end(2l,1l,1,1,1r,0)--cycle; fi  % diagonals and stem
if serifs:
 numeric inner_jut; pickup tiny.nib;
 prime_points_inside(1,2); prime_points_inside(4,3); prime_points_inside(6,5);
 if rt x1'r+cap_jut+.5u+1<=lft x4'l-cap_jut: inner_jut=cap_jut;
  else: rt x1'r+inner_jut+.5u+1=lft x4'l-inner_jut; fi
 dish_serif(1',2,a,1/3,outer_jut,b,1/2,inner_jut);  % upper left serif
 dish_serif(4',3,c,.6,inner_jut,d,1/2,outer_jut)(dark);  % upper right serif
 dish_serif(6',5,e,1/3,cap_jut,f,1/3,cap_jut); fi  % lower serif
penlabels(0,1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter Y_hcrs - straight U hcrossed";
cyrchar(Y_hcrs,13u#,cap_height#,0);
if is_small_cap: getcharcode(y_hcrs); fi
italcorr cap_height#*slant+.45u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_y;
numeric left_stem,right_stem,outer_jut,dy,alpha;
left_stem=cap_stem-stem_corr;
right_stem=min(cap_hair if hefty:-2stem_corr fi,left_stem);
outer_jut=.75cap_jut; x1l=w-x4r=l+letter_fit+outer_jut+.05u;
x2l=x5l=hround(.5w-.5left_stem); x3r=x5r; y1=y4=h; y2=y3=y5=.4h;
dy=y1-y2; alpha=((x2l-x1l)++dy)/dy;
penpos1(alpha*left_stem,0); penpos2(alpha*left_stem,0);
penpos3(alpha*right_stem,0); penpos4(alpha*right_stem,0);
penpos5(left_stem,0); penpos6(left_stem,0); x5=x6; y6=0;
z0=whatever[z1r,z2r]=whatever[z3l,z4l];
if y0>y2+cap_notch_cut:
 y0:=y2+cap_notch_cut;
 fill z0+.5right{up}...{z4-z3}diag_end(3l,4l,1,1,4r,3r)--z5r
  --diag_end(5r,6r,1,1,6l,5l)--z5l--diag_end(2l,1l,1,1,1r,2r){z2-z1}
  ...{down}z0+.5left--cycle; % diagonals and stem
else:
 fill z0--diag_end(0,4l,1,1,4r,3r)--z5r
  --diag_end(5r,6r,1,1,6l,5l)--z5l
  --diag_end(2l,1l,1,1,1r,0)--cycle; fi  % diagonals and stem
if serifs:
 numeric inner_jut; pickup tiny.nib;
 prime_points_inside(1,2); prime_points_inside(4,3); prime_points_inside(6,5);
 if rt x1'r+cap_jut+.5u+1<=lft x4'l-cap_jut: inner_jut=cap_jut;
  else: rt x1'r+inner_jut+.5u+1=lft x4'l-inner_jut; fi
 dish_serif(1',2,a,1/3,outer_jut,b,1/2,inner_jut);  % upper left serif
 dish_serif(4',3,c,.6,inner_jut,d,1/2,outer_jut)(dark);  % upper right serif
 dish_serif(6',5,e,1/3,cap_jut,f,1/3,cap_jut); fi  % lower serif
% hcross
pickup crisp.nib; pos7(bar,90); pos8(bar,90);
x7=.25w; x8=.75w; top y7r=top y8r=vround (y3-eps);
filldraw stroke z7e--z8e;
penlabels(0,1,2,3,4,5,6, 7,8); endchar;

lhchar "Uppercase Cyrillic letter H_dsc - KHA descender";
cyrchar(H_dsc,13u#,cap_height#,
 if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth
if is_small_cap: getcharcode(h_dsc); fi
italcorr cap_height#*slant-.25u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_h;
numeric stem[],outer_jut,xjut,alpha[];
stem1=cap_stem-2stem_corr; stem2=min(cap_hair,stem1);
outer_jut=.75cap_jut; xjut= if serifs: (stem1-stem2)/4 else: 0 fi;
x1l=l+letter_fit+.5u+outer_jut; x2r=r-letter_fit-u-outer_jut-xjut;
x3l=l+letter_fit+.25u+outer_jut+xjut; x4r=r-letter_fit-.25u-outer_jut;
y1=y2=h; y3=y4=0;
alpha1=diag_ratio(1,stem1,h,x4r-x1l);
alpha2=diag_ratio(1,stem2,h,x2r-x3l);
penpos1(alpha1*stem1,0); penpos2(alpha2*stem2,0);
penpos3(alpha2*stem2,0); penpos4(alpha1*stem1,0);
if hefty:
 z0=whatever[z1,z4]=whatever[z2,z3];
 x12=x34=x0; y13=y24=y0;
 z12=whatever[z2l,z3l]; z13=whatever[z2l,z3l];
 z24=whatever[z2r,z3r]; z34=whatever[z2r,z3r];
 forsuffixes $=13,24,34: z$'=.1[z$,z0]; endfor
 fill diag_end(12,1r,.5,1,1l,13')--z13'--diag_end(13',3l,1,.5,3r,34')--z34'
  --diag_end(34',4l,.5,1,4r,24')--z24'
  --diag_end(24',2r,1,.5,2l,12)--z12--cycle; % diagonals
else:
 fill diag_end(4r,1r,.5,1,1l,4l)
  --diag_end(1l,4l,.5,1,4r,1r)--cycle; % left diagonal
 fill diag_end(2l,3l,.5,1,3r,2r)
  --diag_end(3r,2r,.5,1,2l,3l)--cycle; fi % right diagonal
%%%
numeric inner_jut[]; pickup tiny.nib; % moved for cyr_serif
prime_points_inside(3,2); prime_points_inside(4,1);
if rt x3'r+cap_jut+.5u+1<=lft x4'l-cap_jut-xjut: inner_jut2=cap_jut;
 else: rt x3'r+inner_jut2+.5u+1=lft x4'l-inner_jut2-xjut; fi
if serifs:
 prime_points_inside(1,4); prime_points_inside(2,3);
 if rt x1'r+cap_jut+.5u+1<=lft x2'l-cap_jut-xjut: inner_jut1=cap_jut;
  else: rt x1'r+inner_jut1+.5u+1=lft x2'l-inner_jut1-xjut; fi
 dish_serif(1',4,a,1/3,outer_jut,b,2/3,inner_jut1);  % upper left serif
 if is_ogonek:
    dish_serif(4',1,c,2/3,inner_jut2,d,1/3,outer_jut);  % lower right serif
 fi
 dish_serif(2',3,e,2/3,inner_jut1+xjut,f,1/2,outer_jut+xjut)(dark);  % upper right serif
 dish_serif(3',2,g,1/2,outer_jut+xjut,h,2/3,inner_jut2+xjut)(dark); fi % lower left serif
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |diag_end| refers to |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x4 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   numeric right_jut;
   right_jut=if serifs: 1.1 outer_jut else: .4tiny fi;
   if not serifs:
      numeric arm_thickness; arm_thickness=vround(slab+vair_corr);
      save slab; slab:=arm_thickness; fi %bot.slab
   r_serif(4',1,x,1/3,inner_jut2,y,1/3,beak_darkness,right_jut,beak_jut); % lower rt descender
fi
penlabels(0,1,2,3,4,12,13,24,34); endchar;

lhchar "Uppercase Cyrillic letter H_tl (looks like X)";
cyrchar(H_tl,13u#,cap_height#,desc_depth#);
if is_small_cap: getcharcode(h_tl); fi
italcorr cap_height#*slant-.25u#;
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_h_tail;
numeric stem[],outer_jut,xjut,alpha[];
stem1=cap_stem-2stem_corr; stem2=min(cap_hair,stem1);
outer_jut=.75cap_jut; xjut= if serifs: (stem1-stem2)/4 else: 0 fi;
x1l=l+letter_fit+.5u+outer_jut; x2r=r-letter_fit-u-outer_jut-xjut;
x3l=l+letter_fit+.25u+outer_jut+xjut; x4r=r-letter_fit-.25u-outer_jut;
y1=y2=h; y3=y4=0;
alpha1=diag_ratio(1,stem1,h,x4r-x1l);
alpha2=diag_ratio(1,stem2,h,x2r-x3l);
penpos1(alpha1*stem1,0); penpos2(alpha2*stem2,0);
penpos3(alpha2*stem2,0); penpos4(alpha1*stem1,0);
z54=z4r-.5tiny*unitvector(direction 1 of (z1r--z4r));
if hefty:
 z0=whatever[z1,z4]=whatever[z2,z3];
 x12=x34=x0; y13=y24=y0;
 z12=whatever[z2l,z3l]; z13=whatever[z2l,z3l];
 z24=whatever[z2r,z3r]; z34=whatever[z2r,z3r];
 forsuffixes $=13,24,34: z$'=.1[z$,z0]; endfor
 fill diag_end(12,1r,.5,1,1l,13')--z13'--diag_end(13',3l,1,.5,3r,34')--
   z34'--z4l-- %!!!
   (.35[x54,x4r],y4r){up}...{z1r-z4r}z54 %!!!
   --z24' %!!!
  --diag_end(24',2r,1,.5,2l,12)--z12--cycle; % diagonals
else:
 fill diag_end(4r,1r,.5,1,1l,4l)--z4l-- %!!!
   (.35[x54,x4r],y4r){up}...{z1r-z4r}z54 %!!!
   --cycle; %!!! left diagonal
 fill diag_end(2l,3l,.5,1,3r,2r)
  --diag_end(3r,2r,.5,1,2l,3l)--cycle; fi % right diagonal
%%%
if serifs:
 numeric inner_jut[]; pickup tiny.nib;
 prime_points_inside(1,4); prime_points_inside(2,3);
 prime_points_inside(3,2); prime_points_inside(4,1);
 if rt x1'r+cap_jut+.5u+1<=lft x2'l-cap_jut-xjut: inner_jut1=cap_jut;
  else: rt x1'r+inner_jut1+.5u+1=lft x2'l-inner_jut1-xjut; fi
 if rt x3'r+cap_jut+.5u+1<=lft x4'l-cap_jut-xjut: inner_jut2=cap_jut;
  else: rt x3'r+inner_jut2+.5u+1=lft x4'l-inner_jut2-xjut; fi
 dish_serif(1',4,a,1/3,outer_jut,b,2/3,inner_jut1);  % upper left serif
 %dish_serif(4',1,c,2/3,inner_jut2,d,1/3,outer_jut);  % lower right serif
 dish_serif(2',3,e,2/3,inner_jut1+xjut,f,1/2,outer_jut+xjut)(dark);  % upper right serif
 dish_serif(3',2,g,1/2,outer_jut+xjut,h,2/3,inner_jut2+xjut)(dark); fi % lower left serif
%%%%%
% L_tail(4);X_tail
_zero:=4;
_one:=7; _two:=8; _three:=9; _four:=10; _five:=11;
%
% X_
pickup fine.nib;
stem3=hround (max(fine.breadth,.5[stem1,x[_zero]r-x[_zero]l]));
penpos[_five](stem3,0);
x[_five]r=x4r;
(x[_five]l,y)=whatever[z4l,z4l+dir angle(z4l-if serifs:z1l else:z34'fi)];
y[_five]=min(y4-eps,max(y,-d+if serifs: vair'else: slab fi));
x[_one]'r=x[_five]r;
penpos[_one]'(stem3,0); pos[_one](stem3,0); z[_one]=z[_one]';
bot y[_one]=min(y[_five],max(-1/3d,-d+if serifs: vair'else: slab fi));
fill z4l--z[_five]l--z[_one]'l--z[_one]'r--z[_five]r{up}
     ...{z1r-z4r}z54--cycle; %link
% _X
if serifs:
 numeric bulb_diam;
 bulb_diam=max(currentbreadth+eps,min(.82d-1-eps,flare+(cap_stem-stem)));
 pos[_two](vair',-90); pos[_three](hair,-180);
 pos[_four](bulb_diam,-180);
 x[_two]=.5[x[_one],x[_three]r];
 bot y[_two]r=-d-oo; y[_three]-.5bulb_diam=-.85d;
 z[_three]r=z[_four]r;
 rt x[_four]l=floor x[_one]-2.5u if monospace: / expansion_factor fi;%!!!
 filldraw stroke pulled_arc.e([_one],[_two]);%!!!
 bulb([_two],[_three],[_four]); % bulb
else:
 pos[_two](slab,-90); pos[_three](flare,-105);
 rt x[_three]r=x[_one]-4u; bot y[_three]r=-d;%!!!
 bot y[_two]r=-d-o; x[_two]=.4[x[_three]r,x[_one]r];%!!!
 filldraw stroke super_arc.e([_one],[_two])%!!!
  & term.e([_two],[_three],left,.8,4); fi
penlabels(34',[_one],[_two],[_five]);
%%%
penlabels(0,1,2,3,4,12,13,24,34,54, 5,6,7,8,[_five]); endchar;

lhchar "Uppercase Cyrillic letter CH_ldsc - CHE left descender";
cyrchar(CH_ldsc,13.5u#,cap_height#,cap_sbeak#);
if is_small_cap: getcharcode(ch_ldsc); fi
italcorr cap_height#*slant-cap_serif_fit#+cap_jut#-2.5u#+min(.5cap_stem#,u#);
adjust_fit(cap_serif_fit#,cap_serif_fit#);
% upper_ch;
pickup tiny.nib;
pos1(cap_stem,0); pos2(cap_stem,0); pos3(cap_stem,0); pos4(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); x3=x4=w-x1;
top y1=top y3=h; bot y2=.3[y6,h]; bot y4=0;
x5l=1/3[x1,x3]; y5=y6; if serifs: y6'=y6-cap_hair+1.05cap_stem; fi
x6=x6'=x3; y6=good.y (h-.52h if serifs: -.35(.5[cap_hair,.7cap_stem]) fi);
filldraw stroke z1e--z2e; % left stem
filldraw stroke z3e--z4e; % right stem
clearpen; penpos2'(cap_stem,0); z2'=z2;
numeric middle_weight; middle_weight=.5[vair,cap_band];
penpos5(middle_weight,90); penpos6(middle_weight,90); penpos6'(middle_weight,90);
if serifs:
 if y6'-y6>=middle_weight+1+eps:
  fill stroke  pulled_arc.e(2',5) & z5e{right}...{.75(z5-z6)+z6'}z6'e;
 else:
  fill stroke  pulled_arc.e(2',5) & z5e--z6e; fi % arc
 numeric inner_jut; pickup tiny.nib;
 if rt x1r+cap_jut+.5u+1<=lft x3l-cap_jut: inner_jut=cap_jut;
  else: rt x1r+inner_jut+.5u+1=lft x3l-inner_jut; fi
%%%
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,inner_jut);  % upper left serif
 dish_serif(3,4,e,1/3,inner_jut,f,1/3,cap_jut);  % upper right serif
 %dish_serif(4,3,g,1/3,inner_jut,h,1/3,cap_jut);  % lower right serif
else:
 filldraw stroke  pulled_arc.e(2',5) & z5e--z6e; fi % arc
%%%%%
% upper left descender
numeric right_jut;
right_jut=
   if serifs: max(limit_dist,1.1cap_jut)
   else:      hround (w+cap_serif_fit+.5(stem-u)-rt x4r-.65u) fi;
l_serif(4,3,x,1/3,beak_darkness,right_jut,beak_jut,y,1/3,cap_jut); % lower left descender
penlabels(1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter CH_rdsc - CHE right descender";
cyrchar(CH_rdsc,13.5u#,cap_height#,
 if is_ogonek: desc_depth# else: cap_sbeak# fi); %udesc/og_depth`
if is_small_cap: getcharcode(ch_rdsc); fi
italcorr cap_height#*slant-cap_serif_fit#+cap_jut#-2.5u#+min(.5cap_stem#,u#);
adjust_fit(cap_serif_fit#,
 cap_serif_fit# if not serifs:+.5(cap_stem#-u#) fi); %DL_adj
% upper_ch;
pickup tiny.nib;
pos1(cap_stem,0); pos2(cap_stem,0); pos3(cap_stem,0); pos4(cap_stem,0);
lft x1l=lft x2l=hround max(2u,3u-.5cap_stem); x3=x4=w-x1;
top y1=top y3=h; bot y2=.3[y6,h]; bot y4=0;
x5l=1/3[x1,x3]; y5=y6; if serifs: y6'=y6-cap_hair+1.05cap_stem; fi
x6=x6'=x3; y6=good.y (h-.52h if serifs: -.35(.5[cap_hair,.7cap_stem]) fi);
filldraw stroke z1e--z2e; % left stem
filldraw stroke z3e--z4e; % right stem
clearpen; penpos2'(cap_stem,0); z2'=z2;
numeric middle_weight; middle_weight=.5[vair,cap_band];
penpos5(middle_weight,90); penpos6(middle_weight,90); penpos6'(middle_weight,90);
if serifs:
 if y6'-y6>=middle_weight+1+eps:
  fill stroke  pulled_arc.e(2',5) & z5e{right}...{.75(z5-z6)+z6'}z6'e;
 else:
  fill stroke  pulled_arc.e(2',5) & z5e--z6e; fi % arc
 numeric inner_jut; pickup tiny.nib;
 if rt x1r+cap_jut+.5u+1<=lft x3l-cap_jut: inner_jut=cap_jut;
  else: rt x1r+inner_jut+.5u+1=lft x3l-inner_jut; fi
%%%
 dish_serif(1,2,a,1/3,cap_jut,b,1/3,inner_jut);  % upper left serif
 dish_serif(3,4,e,1/3,inner_jut,f,1/3,cap_jut);  % upper right serif
 if is_ogonek:
    dish_serif(4,3,g,1/3,inner_jut,h,1/3,cap_jut);  % lower right serif
 fi
else:
 filldraw stroke  pulled_arc.e(2',5) & z5e--z6e; fi % arc
%%%%%
% uppercase descender/ogonek
if is_ogonek:
   def the_pen=if known ogonek_pen: ogonek_pen
       elseif serifs: tiny elseif is_bold: fisp else: fine fi
   enddef;
   % |stem| is drawn by |tiny| pen, hence |tiny| in the following equation:
   ogonek_pos=(x4 if not serifs: l if is_bold: +.75tiny fi\\ fi,.5the_pen)
       if serifs and not is_egyptian: +(.5xvair,0) fi;
   easy_ogonek(the_pen,7,8,9); % ogonek
else:
   numeric right_jut;
   right_jut=
      if serifs: max(limit_dist,1.1cap_jut)
      else:      hround (w+cap_serif_fit+.5(stem-u)-rt x4r-.65u) fi;
   if not serifs:
      numeric arm_thickness; arm_thickness=vround(slab+vair_corr);
      save slab; slab:=arm_thickness; fi % botrom.slab
   r_serif(4,3,x,1/3,cap_jut,y,1/3,beak_darkness,right_jut,beak_jut); % lower rt descender
fi
penlabels(1,2,3,4,5,6, 7,8,9); endchar;

% Ukrainian e moved up for wncoding and LH font

lhchar "Uppercase Cyrillic letter SCHWA";
cyrchar(SCHWA,11.25u#+max(.75u#,.5cap_curve#),cap_height#,0);
if is_small_cap: getcharcode(schwa); fi
italcorr .7cap_height#*slant-.5u#; %O
adjust_fit(0 if monospace: +\\-.25u#,-.25u#+ else:,fi\\ 0);
% upper_schwa;`large e' shape (ecfonts)
numeric left_curve,right_curve;
left_curve=right_curve+3stem_corr=cap_curve if not serifs: -1.5stem_corr fi;
if right_curve<tiny.breadth: right_curve:=tiny.breadth; fi
if left_curve<tiny.breadth: left_curve:=tiny.breadth; fi
pickup tiny.nib;
pos1(right_curve,0); pos2(vair',90); pos3(left_curve,180);
y1=.48h; bot y2l=-o; y0l=top y1;
if serifs: x2-.25u=x4=.5w-.5u; else: x2=x4=.5w-.25u; fi
lft x1l=hround max(.5u,u-.5right_curve);
rt x3l=w-hround max(.75u,1.5u-.5left_curve); %lower_e+.25u
{{interim superness:=more_super;
 filldraw stroke super_arc.e(1,2)}}; % right bowl
y3=.5[y2,y4]; top y4l=h+o;
if serifs:
 pos4(cap_band,270);
 filldraw stroke pulled_arc.e(2,3) & pulled_arc.e(3,4); % left bowl, arc,
 pickup fine.nib; pos4'(cap_band,270); z4'=z4;
 lft x5l=max(hround u,lft x1l+.3u+eps);
 pos5(cap_hair,360); bot y5=min(vround .675h,bot y4r-eps);
 filldraw stroke z4'e{left}...{x5-x4,10(y5-y4)}z5e; %  and terminal
 pos6(.3[fine.breadth,cap_hair],360); x6l=x5l; top y6=h+o;
 x5'-x5l=2cap_curve-fine; y5'=y5;
 path upper_arc; upper_arc=z5{x4-x5,10(y4-y5)}..{right}z4;
 numeric t; t=xpart(upper_arc intersectiontimes (z6r--z5'));
 filldraw z5l--z6l--z6r--subpath(t,0) of upper_arc--cycle; % upper serif
else:
 pos4(slab,270);
 {{interim superness:=min(1.03superness,more_super);
  filldraw stroke super_arc.e(2,3)}};
 filldraw stroke super_arc.e(3,4);  % left bowl and arc
 pickup fine.nib; pos4'(slab,270); z4=z4';
 pos5(1.2flare,280); lft x5l=lft x1l+u; top y5l=vround .95h+o;
 filldraw stroke rterm.e(4',5,left,.75,20); fi  % terminal
path p; p=super_arc.l(2,3) & super_arc.l(3,4);
y1'r=y0r=y0l-7/8[thin_join,vair']; y1'l=y0l; x1'l=x1'r=x1;
forsuffixes $=l,r: x0$=xpart(((w,y0$)--(x1,y0$)) intersectionpoint p); endfor
fill stroke z0e--z1'e;  % crossbar
%%%%%
penlabels(0,1,2,3,4,5,6); endchar;

lhchar "Uppercase Cyrillic letter EPS - EPSILON";
cyrchar(EPS,if serifs: 11u# else: 10u# fi,cap_height#,0);
if is_small_cap: getcharcode(eps); fi
italcorr .75cap_height#*slant-.5u#; %B
adjust_fit(if monospace:.35u#,.25u# else: 0,0 fi);
% upper_eps;
pickup fine.nib; pos2(slab,90); top y2r=h+o;
numeric right_curve,middle_weight;
right_curve=max(currentbreadth+eps,cap_curve-3stem_corr);
middle_weight=max(fine.breadth+eps,.6vair+.5);
pos4(middle_weight,-90); pos5(middle_weight,-90);
pos6(middle_weight,90); pos5'(middle_weight,90);
pos7(right_curve,180);
x2-.5u=x6=x8=.5[w-1.5u,x7]; lft x7r=hround.75u;
pos3(max(currentbreadth+eps,right_curve-stem_corr),180); lft x3r=hround 1.25u;
pos8(slab,270); bot y8r=-o; y7=.5[y6l,y8l];
y3=.5[y4l,y2l]; x4=1/3[x5,x3l]; y4=y5; y5'=y6;
top y5l-bot y5'l=vair; .5[y5l,y5'l]=.52h;
rt x5=rt x5'=max(hround (w-3.5u),rt x6)+eps;
if serifs:
 pos1(cap_hair,0); pos9(hair,360);
 lft x1r=w-hround u; lft x9r=w-hround.9u;
 y1=good.y(max(.52[y5l,y2l],top y5l+.5flare+.5)-eps);
 y9=max(.52[top y8l,bot y5'l],y7+1);
 pos0(flare,0); z0r=z1r; bulb(2,1,0); % bulb
 filldraw stroke  z8e{right}...{up}z9e; % arc
else:
 pickup fine.nib; pos1(1.2flare,80); pos9(flare,275);
 rt x1r=hround(w-1.1u); rt x9r=hround(w-.9u);
 top y1r=vround .95h+o; bot y9r=vround .08h-o; y9l:=good.y y9l; x9l:=good.x x9l;
 filldraw stroke rterm.e(2,1,right,.9,4); % upper arc
 filldraw stroke term.e(8,9,right,.8,4); fi % arc and terminals
if not serifs: interim superness:=more_super; fi
filldraw stroke
 z5e..super_arc.e(4,3) &
 {{if not serifs:interim superness:=hein_super; fi
 super_arc.e(3,2)}}; % upper bowl
filldraw stroke super_arc.e(8,7) & super_arc.e(7,6)..z5'e; % lower bowl
%%%%%
penlabels(0,1,2,3,4,5,6,7,8,9); endchar;

endinput;
% end of file
