% fgebase.mf : Supplemental defintions for fge
% J.J. Green 2005-2006

fgebase := 1;

% vertical versions of knuth's bulb, not sure why need quite
% so many of these

def vbulb(suffix $, $$, $$$) =
  
  z$$$r = z$$r;
  path_.l := z$l{0, y$$r-y$r}...{x$$r-x$r, 0}z$$l;
  filldraw path_.l--z$$r{y$r-y$$r, 0}...{0, x$r-x$$r}z$r--cycle; % link
  path_.r := z$$$l{x$r-x$$r, 0}..z$$$r{x$$r-x$r, 0}; % near-circle
  filldraw path_.r--z$$l{x$$r-x$r, 0}..cycle; % bulb

enddef;

def dlbulb(suffix $, $$, $$$) =
  
  z$$$r = z$$r;
  path_.l := z$l{0, y$$r-y$r}...{x$$r-x$r, 0}z$$l;
  filldraw path_.l--z$$r{y$$r-y$r, 0}...{0, x$$r-x$r}z$r--cycle; % link
  path_.r := z$$$l{x$$r-x$r, 0}..z$$$r{x$r-x$$r, 0}; % near-circle
  filldraw path_.r--z$$l{x$r-x$$r, 0}..cycle; % bulb

enddef;

def drbulb(suffix $, $$, $$$) =
  
  z$$$r = z$$r;
  path_.l := z$l{0, y$$r-y$r}...{-x$r+x$$r, 0}z$$l;
  filldraw path_.l--z$$r{y$r-y$$r, 0}...{0, x$r-x$$r}z$r--cycle; % link
  path_.r := z$$$l{x$$r-x$r, 0}..z$$$r{x$r-x$$r, 0}; % near-circle
  filldraw path_.r--z$$l{x$r-x$$r, 0}..cycle; % bulb

enddef;

% vertical arm

vardef varm(suffix $, $$, @)(expr darkness, jut) =  % arm from |z$| to |z$$|
  
  y@0=good.y(y$$r-jut);
  x@0=x$r;
  if serifs: x@1=x$l; z@1=z$$l+whatever*(z$$r-z@0);
    z@2=.5[z$l, z@1];
    path p_; p_= z$$l{z@1-z$$l}...darkness[z@1, .5[z@2, z$$l] ]...z@2
    ---z$l--z$r--z@0--z$$r--cycle;
    if (x$$>x$) <> (xpart precontrol 1 of p_ > xpart postcontrol 1 of p_):
      p_:=z$$l{z@1-z$$l}...darkness[z@1, .5[z@2, z$$l] ]
      ---z$l--z$r--z@0--z$$r--cycle;
    fi
    filldraw p_; % arm and beak
  else:
    filldraw z$l--z$r--z@0--z$$r--cycle;
  fi  % sans-serif arm
  penlabels(@0, @1, @2);

enddef;

% horizontal serifs for rotated characters: here interchange x & y, 
% so top is right, left is bot and so on

vardef hserif(suffix $, $$, @)  % serif at |z$| for stroke from |z$$|
  (expr darkness, jut) suffix modifier =
  pickup crisp.nib; numeric bracket_height; pair downward;
  bracket_height=if dark.modifier: 1.5 fi\\ bracket;
  if x$<x$$:
    x@2=min(x$+bracket_height, x$$);
    rt x@1-slab = lft x@0+eps = tiny.lft x$;
    downward = z$-z$$;
    if x@1>x@2: x@2:=x@1; fi
  else:
    x@2 = max(x$-bracket_height, x$$);
    lft x@1+slab = rt x@0-eps = tiny.rt x$;
    downward = z$$-z$;
    if x@1<x@2: x@2:=x@1; fi
  fi
  x@3 = x@2;
  z@3 = whatever[z$, z$$];
  if jut<0:
    z@2-penoffset downward of currentpen =
    z$r-penoffset downward of pen_[tiny.nib]+whatever*downward;
    bot y@0 = bot y@1 = tiny.bot y$r + jut;
    if y@3<y@2+eps: y@3:=y@2+eps; fi
  else:
    z@2+penoffset downward of currentpen =
    z$l+penoffset downward of pen_[tiny.nib]+whatever*downward;
    top y@0 = top y@1 = tiny.top y$l + jut;
    if y@3>y@2-eps: y@3:=y@2-eps; fi
  fi
  pair corner;
  xpart corner=x@1;
  corner = z@2+whatever*downward;
  filldraw
  z@2{z$-z$$}...darkness[corner, 0.5[z@1, z@2]]{z@1-z@2}
  ...{0, jut}z@1--z@0--(x@0, y$)--z@3--cycle; % the serif
  labels (@1, @2);
enddef;

