%% File LXBBASE copied and modified from AMSYB by American Mathematical Society
%% in order to cope with the necessities of the slides fonts
%%
%% Claudio Beccari 2007
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Changes made for version 2.1:
% Fixed Problems with smode in draw_outlines and showpoints
%   Stefan Lindner, 21-March-1991
% Fixed Problems with serifs at small point sizes
%   Stefan Lindner and Lutz Birkhahn, 14-April-91

vardef curve@#(expr a,one,b,two)=
  save x,y;
  z0=if(str@#="br")or(str@#="tl"):(xpart b,ypart a)
     else:(xpart a,ypart b) fi;
  z1=one[a,z0]; z2=two[b,z0];
  a ..controls z1 and z2.. b
 enddef;

vardef reversecurve@#(expr a,one,b,two)=reverse curve@#(a,one,b,two) enddef;


vardef beak(expr injoin,intip,outtip,indent,outjoin,tense)=
  save x,y,t,toward,onright,ref; pair toward; boolean onright; path ref;
  onright=(xpart intip>xpart injoin);    
  toward=if onright: right else: left fi;
  z1=intip if onright:-else:+ fi (inbeak,0);
  z2=outtip if onright:-else:+ fi (outbeak,0);
  ref=outjoin{toward}...(z1 if onright:+else:- fi (2*abs(outjoin-injoin),0));
  y4=.5[ypart outjoin,ypart indent];
  z3=ref intersectionpoint ((0,y4)--(w,y4));
  (t3,t4)=ref intersectiontimes ((0,y4)--(w,y4)); 
 
  injoin{toward}..tension tense..z1--intip--outtip--z2...indent{-toward}...
    {-(direction t3 of ref)}z3--subpath(t3,0) of ref
 enddef; 




boolean its_a_leftserif;                

vardef full_serif_points@#
(expr A,B,Bl,Br,leftlength,rightlength,midbracket_pull)=
%%%% min_limit(join_radius)(.5serif_thickness);   % -sl 14.04.91
  (z1-B)=whatever*(A@#-B@#);            
  (z1l-Bl)=whatever*(A@#-B@#);          
  (z1r-Br)=whatever*(A@#-B@#);          

  y2=y2l=y2r=ypart A;                   
  y1=y1l=y1r=y3=y4=ypart A if ypart A>ypart B:-else:+fi serif_thickness;
                                        
  x5=x2=.5[x1l,x1r];                    
  y5=entasis[y2l,y1l];                  

  if its_a_leftserif:x3=x2l=x1l-round(leftlength+serif_constant_amt); 
   else:x4=x2r=x1r+round(rightlength+serif_constant_amt); fi
enddef;                      

vardef leftserif@#(expr A,B,Bl,Br,alength,midbracket_pull)=
  its_a_leftserif:=true;  
  save x,y,p; path p[]; 
  min_limit(join_radius)(.5serif_thickness);   % -sl 14.04.91
  full_serif_points@#(A,B,Bl,Br,alength,0,midbracket_pull);                
  p0:=Bl{z1l-Bl}...{z3-z1l}z3;                     
  if midbracket_pull<>0: 
    z3'l=z1l; pos3'(alength+5pt,.5[angle(B-A),if ypart A>ypart B:-fi 180]);
    z8'=p0 intersectionpoint (z3'l--z3'r);         
    z8=(midbracket_pull-eps)[z8',z1l];fi           
  if ypart A>ypart B:reverse fi   
  ((if midbracket_pull=0:p0 else:Bl{z1l-Bl}...z8...{z3-z1l}z3 fi 
   if softpath:)softjoin(z3--z2l)softjoin(else:--fi z2l..z5{right}))
enddef;
 
vardef rightserif@#(expr A,B,Bl,Br,alength,midbracket_pull)=
  its_a_leftserif:=false;
  save x,y,p; path p[]; 
  min_limit(join_radius)(.5serif_thickness);   % -sl 14.04.91
  full_serif_points@#(A,B,Bl,Br,0,alength,midbracket_pull);
  p4:=z4{z1r-z4}...{Br-z1r}Br;                     
  if midbracket_pull<>0:
    z4'l=z1r; pos4'(alength+5pt,.5[angle(B-A),0]);
    z9'=p4 intersectionpoint (z4'l--z4'r);         
    z9=(midbracket_pull-eps)[z9',z1r];fi           
  if ypart A>ypart B:reverse fi
   ((z5{right}..z2r if softpath:)softjoin(z2r--z4)softjoin(else:--fi
    if midbracket_pull=0:p4 else:z4{z1r-z4}...z9...{Br-z1r}Br fi))
enddef;
                            
vardef fullserif@#(expr A,B,Bl,Br,leftlength,rightlength,midbracket_pull)= 
  save x,y,p; path p[];
  p1=rightserif(A,B,Bl,Br,rightlength,midbracket_pull); 
  p2=leftserif(A,B,Bl,Br,leftlength,midbracket_pull);
  if ypart A>ypart B:(p1--p2)else:(p2--p1)fi
enddef;


vardef straightarm@#(expr join,tip,heel,thickness,pull)=
  save x,y,toward,p; pair toward; path p;
  toward=if (str@#="br")or(str@#="tr"):right else:left fi; 
  z1=tip if (str@#="br")or(str@#="tr"):- else:+ fi (thickness,0);
  z2=point .5 of (join{toward}...{tip-heel}z1);
  z1-z3=whatever*(tip-heel); y3=ypart join;
  p=z1{z3-z1}...{join-z3}join;
  z4=pull[z3,point .5 of p];
  if (str@#="br")or(str@#="tl"):reverse fi
  (join{toward}...z4...{tip-heel}z1--tip--heel)
 enddef; 
 

vardef constant_angle(expr top_pt,bot_pt,stem)suffix $=
  save theta;
  theta=if str$="lr":-else:+fi (angle(length(top_pt-bot_pt) +-+ stem,stem));
  angle(top_pt-bot_pt)+theta-90
enddef;


vardef pos@#(expr b,d) =
 (x@#r-x@#l,y@#r-y@#l)=(b,0)rotated d;x@#=.5(x@#l+x@#r);y@#=.5(y@#l+y@#r)enddef;

vardef mpos(text t)(expr b,d)=forsuffixes $=t:pos$(b,d); endfor enddef;
 

def draw_outlines=  pickup pencircle scaled 1.5linethickness;
  for n=1 upto 3:if known p[n]:draw p[n];fi 
                 if known p[n]':draw p[n]';fi
                 if known p[n]'':draw p[n]'';fi endfor
  if numeric mode:      % -sl    21.03.1991
  if mode=proof: drawgrid; fi
  fi                    % -sl    21.03.1991
  enddef;

def drawgrid=
 pickup pencircle scaled 1;
 for y:=h,xheight,0,-descender: draw (0,y)--(w,y); endfor
 for y:=unit step unit until (cap+.5unit): draw (0,y)--(w,y); endfor
 for y:=unit step -unit until (-d-.5unit): draw (0,y)--(w,y); endfor
 for x:=0 step unit until (w+.5unit): draw (x,-d)--(x,h); endfor
enddef;
 

vardef onaline(suffix a,b)(text t)=forsuffixes $=t:z$=whatever[z.a,z.b]; endfor
 enddef;
 

def showpoints(text t)= 
 if numeric mode:      % -sl    21.03.1991
 if mode=proof:pickup pencircle scaled 3;
 forsuffixes $:=t:forsuffixes s:=l,,r:if known z$.s:draw z$s;fi endfor endfor
 pickup pencircle scaled 1; penlabels(t); fi
 fi                    % -sl    21.03.1991
enddef;


vardef inlimit(text amt)(expr lowerlimit,upperlimit)=save this;
 this:=max(amt,lowerlimit); this:=min(this,upperlimit);this
enddef;


let semi_ = ;; let colon_ = :; let endchar_ = endchar;
def iff expr b = if b:let next_=use_it else:let next_=lose_it fi; next_ enddef;
def use_it = let : = restore_colon; enddef;
def restore_colon = let : = colon_; enddef;
def lose_it = let endchar=fi; let ;=restore_endchar semi_ if false enddef;
def restore_endchar=let ;=semi_; let endchar=endchar_; enddef;
def always_iff expr b = use_it enddef;
boolean wanted[];

 % for x:="I":
 %   wanted[byte x]:=true; endfor
          % this allows specifying only those characters which are to be shown
          % the chars can be specified inside of quotes("c") or as a number(23)
def OK expr x=known wanted[byte x] enddef;
 %  let iff=always_iff;               % allows testing of all chars in the file
           

def min_limit(text this)(expr limit)=if this>limit:save this;this=limit;fi
enddef;


% def define_adj_whole_pixels(text t)=
% forsuffixes $:=t: $:=hround(max(eps,$.#-linethickness#)*hppp); endfor enddef;

def define_min_adj_whole_pixels(text t)=
 forsuffixes $:=t: $:=max(lowres_min,hround(max(eps,$.#-linethickness#)*hppp));
 endfor enddef;

def bool(text t)=boolean t; t enddef;

def vpix(text t)(text tt)= t:=tt; t:=vround(tt.#*hppp); enddef;   % whole v pix
def wpix(text tt)(text t)= 
 forsuffixes $:=t: $:=hround(tt.#*hppp); endfor enddef;    % whole pixels
def adjpix(text tt)(text t)=                                % adjusted pixels
 forsuffixes $:=t: $:=hround(max(eps,tt.#*hppp-linethickness)); endfor enddef;

def minadjpix(text ttt)(text tt)(text t)=      % adjusted pixels with a minimum
 forsuffixes $:=t: $:=max(ttt,hround(tt.#*hppp-linethickness));
 endfor enddef;


def highres_lowres(text t)(expr a,b)=t:=if lowres:b else:a fi enddef;

