autorounding := 1 ;
mode_setup;
define_pixels(em,asc,desc,ex);
define_blacker_pixels(penwidth,penthickness) ;
define_blacker_pixels(tehta_penwidth) ;
define_corrected_pixels(o);
define_whole_pixels(straight_sidebar,curve_sidebar) ;
 
define_pixels(fudge, tinco_width, parma_width, romen_width) ;
define_pixels(luva_width) ;
pickup pencircle xscaled penwidth yscaled penthickness rotated penangle;
main_nib := savepen ;
pickup pencircle xscaled tehta_penwidth yscaled penthickness rotated penangle;
tehta_nib := savepen ;
 
def draw_luva(suffix i,j,k,l)
 (expr luva_type,bow_stem_y,bow_arch_x,
       bow_side_y,bow_end_x) =
 y.i = bow_stem_y * ex ;
 x.j - x.i = bow_arch_x * (x.k - x.i) ;
 y.k = (if (luva_type = "calma") or (luva_type = "quesse"): 1 - fi bow_side_y) * ex ;
 x.l - x.i = bow_end_x * (x.k - x.i) ;
 numeric x.luva_serif,y.luva_serif ;
 z.luva_serif = z.l + 0.03em *
   if luva_type = "tinco": dir 135 else: dir 315 fi ;
 
 path luva_path ;
 luva_path = z.i{dir( if (luva_type = "tinco") or (luva_type = "parma"):
                  penangle else: penangle+180 fi)}
      .. z.j{if (luva_type = "tinco") or (luva_type = "parma"):
                   right else: left fi}
     .. z.k ..
      if (luva_type = "parma") or (luva_type = "quesse"): {curl 0.3}z.l
      else: z.l .. {curl 2}z.luva_serif fi ;
 draw luva_path ;
 labels(i,j,k,l)
 enddef ;
 
def draw_tinco_luva(suffix i,j,k,l) =
 % this draws a complete tinco luva, using the four given suffices
 % for the control points. ALL the information about the control
 % points is determined within the macro; they are provided only for
 % information and later use.
 lft x.i = straight_sidebar ; % intersection with stem
 top y.j = round(ex + o + tinco_fudge) ; % top
 rt x.k = round (x.i + luva_width) ; % right
 bot y.l = 0; % round (- o) ; % bottom, with fudge factor
 draw_luva(i,j,k,l)("tinco",tp_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )
 
 enddef ;
 
def draw_ando_luva(suffix i,j,k,l,ii,jj,kk,ll) =
 % this draws a complete ando luva
 % first bow
 lft x.i = straight_sidebar ; % intersection with stem
 top y.j = round(ex + o + tinco_fudge) ; % top
 rt x.k = round (x.i + luva_width) ; % right
 bot y.l =  round (- o) ; % bottom, with fudge factor
 draw_luva(i,j,k,l)("tinco",tp_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )    ;
 % now the second luva
 z.second = ( (x.i+pt,y.i) .. (infinity,y.i) ) intersectionpoint luva_path ;
 x.ii = good.x (xpart z.second) ;
 y.jj = y.j ;
 rt x.kk = round (x.ii + luva_width) ; % right
 y.ll = y.l ;
 draw_luva(ii,jj,kk,ll)("tinco",tp_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )
 enddef ;
 
def draw_parma_luva(suffix i,j,k,l) =
 % this draws a complete parma luva
 lft x.i = straight_sidebar ; % intersection with stem
 top y.j = round(ex + o) ; % top
 rt x.k = round (x.i + luva_width) ; % right
 bot y.l = 0 ;% bottom
 draw_luva(i,j,k,l)("parma",tp_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x )
 enddef ;
 
def draw_umbar_luva(suffix i,j,k,l,ii,jj,kk,ll) =
 % this draws a complete umbar luva
 % first bow
 lft x.i = straight_sidebar ; % intersection with stem
 top y.j = round(ex + o) ; % top
 rt x.k = round (x.i + luva_width) ; % right
 bot y.l = 0 ;% bottom
 draw_luva(i,j,k,l)("parma",tp_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x ) ;
 %second bow
 z.second = ( (x.i+pt,y.i) .. (infinity,y.i) ) intersectionpoint luva_path ;
 x.ii = good.x (xpart z.second) ;
 y.jj = y.j ;
 rt x.kk = round (x.ii + luva_width) ; % right
 y.ll = y.l ;
 draw_luva(ii,jj,kk,ll)("parma",tp_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x )
 enddef ;
 
def draw_calma_luva(suffix i,j,k,l) =
 % this draws a complete calma luva
 rt x.i = w - straight_sidebar ; % intersection with stem
 y.j = good.y 0 ; % bottom
 lft x.k = round (x.i - luva_width) ; % left
 top y.l = round(ex - fudge) ; % top
 draw_luva(i,j,k,l)("calma",cq_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )
 enddef ;
 
def draw_anga_luva(suffix i,j,k,l,ii,jj,kk,ll) =
 % this draws a complete anga luva
 % first bow
 rt x.i = w - straight_sidebar ; % intersection with stem
 y.j = good.y 0 ; % bottom
 lft x.k = round (x.i - luva_width) ; % left
 top y.l = round(ex - fudge) ; % top
 draw_luva(i,j,k,l)("calma",cq_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )  ;
 % now the second luva
 z.second = ( (x.i-pt,y.i) .. (-infinity,y.i) ) intersectionpoint luva_path ;
 x.ii = good.x (xpart z.second) ;
 y.jj = y.j ;
 lft x.kk = round (x.ii - luva_width) ; % left
 y.ll = y.l ;
 draw_luva(ii,jj,kk,ll)("calma",cq_bow_stem_y,
                            tc_bow_arch_x,
                            tc_bow_side_y,
                            tc_bow_end_x )
 enddef ;
 
def draw_quesse_luva(suffix i,j,k,l) =
 % this draws a complete quesse luva
 rt x.i = w - straight_sidebar ; % intersection with stem
 y.j = good.y 0 ; % bottom
 lft x.k = round (x.i - luva_width) ; % left
 top y.l = round(ex) ; % top
 draw_luva(i,j,k,l)("quesse",cq_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x )
 enddef ;
 
def draw_ungwe_luva(suffix i,j,k,l,ii,jj,kk,ll) =
 % this draws a complete ungwe luva
 % first bow
 rt x.i = w - straight_sidebar ; % intersection with stem
 y.j = good.y 0 ; % bottom
 lft x.k = round (x.i - luva_width) ; % left
 top y.l = round(ex) ; % top
 draw_luva(i,j,k,l)("quesse",cq_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x ) ;
 % second bow
 z.second = ( (x.i-pt,y.i) .. (-infinity,y.i) ) intersectionpoint luva_path ;
 x.ii = good.x (xpart z.second) ;
 y.jj = y.j ;
 lft x.kk = round (x.ii - luva_width) ; % left
 y.ll = y.l ;
 draw_luva(ii,jj,kk,ll)("quesse",cq_bow_stem_y,
                            pq_bow_arch_x,
                            pq_bow_side_y,
                            pq_bow_end_x )
 enddef ;
 
 
 
def draw_telco(suffix i,j)(expr serifs)  = % serifs = t,b,tb for Top & Bottom
 z.i.$ = z.i + 0.05em * dir(penangle+180) ;
 z.i.$$ = z.i + 0.1em * down ;
 z.j.$ = z.j + 0.05em * dir(penangle) ;
 z.j.$$ = z.j + 0.1em * up ;
 draw z.i .. z.j ;
 if (serifs = "t") or (serifs = "tb"):
  filldraw z.i -- z.i.${dir(penangle-90)}
   .. {down}z.i.$$ & z.i.$$ .. z.i & cycle ; fi
 if (serifs = "b") or (serifs = "tb"):
  filldraw z.j -- z.j.${dir(penangle+90)}
   .. {up}z.j.$$ & z.j.$$ .. z.j & cycle ; fi
 labels(i,j)
 enddef ;
 
def draw_bar(suffix i,j)(expr serifs) = % serifs = r,l,rl for Right/Left
 z.right_serif1 = z.j + 0.04em * left ;
 z.right_serif2 = z.j + 0.04em * dir penangle ;
 z.left_serif1 = z.i + 0.04em * right ;
 z.left_serif2 = z.i + 0.04em * dir(penangle+180) ;
 draw if (serifs = "l") or (serifs = "rl"):
         z.left_serif2{dir penangle} .. z.left_serif1 else: z.i fi
      ---
      if (serifs = "r") or (serifs = "rl"):
         z.right_serif1 .. {dir penangle}z.right_serif2 else: z.j fi ;
labels(i,j)
 enddef ;
 
 
def draw_dot(suffix i) =
 % calculate width of pen
 numeric penwdth ;
 1.1penwdth = (pen_rt - pen_lft) ++ (pen_top - pen_bot) ;
 z.i.$ = z.i + 1/2penwdth*dir(penangle+90) ;
 z.i.$$ = z.i + 1/2penwidth*dir(penangle-90) ;
 draw z.i.$$ -- z.i.$ ; drawdot z.i.$$ ; drawdot z.i.$ ;
enddef ;
