% ======================================================================
%		   KD Classical Greek Family of Fonts
%		   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%
%	A set of MF source fonts for use with TeX version 3.0 or higher
%	accompanied with macros and hyphenation tables to facilitate
%	the typesetting of greek texts
%
%	This piece of work is partially based on original work of
%	Sylvio Levi (design of an excellent set of fonts)  
%	and Yianni Haralambous (ideas about macros other fonts).
%
%	This file is part of the greektex package and abides to 
%	copyright laws of the GNU general public software licence
%
%	You are allowed to use or modify this file as long as the 
%	original authors are clearly mentioned. It is ILLEGAL to sell
%	any part of this work or work derived from it. You may not
%	charge for this work except to cover for reasonable media 
%	expensess.
%
%	K J Dryllerakis (C) 1991-1992 
%
% ======================================================================
%
% Computer Classic Greek Font Accents
% KDACCENT.MF: defines all Greek diacritic marks.
%
%	K J Dryllerakis May 1991. Code based on Haralambous Package.
%
if one_accent: gen_acc:=0; else: gen_acc:=1; fi
def acc_corr=0 enddef;
def separator=
	if one_accent :
		max(1,vround(1/12x_height))
	else:
		max(1,vround(1/20x_height))
	fi
enddef;

def grave(expr center) =
	if serifs: x22-x21=1/4w; x21-.5stem=hround(center-2/3(x22-x21)-.5stem);
	else: lft x21l=hround (1/4w-.5stem); rt x22r=hround(center+.25u+.5vair); fi
	generate_grave; penlabels(21,22);
enddef;

def acute(expr center) =
	if one_accent:
		generate_universal(center);
	else:
		if serifs: x21-x22=1/4w; x21+.5stem=hround(center+2/3(x21-x22)+.5stem);
		else: rt x21r=hround (3/4w+.5stem); lft x22l=hround(center-.25u-.5vair); fi
		generate_grave; penlabels(21,22);
	fi
enddef;

vardef search(expr xyzzy) =
	pair p;ypart(p)=y22l;p-(x22,xyzzy)=whatever*((z21-(x22,xyzzy))rotated 90);
	length(p-(x22,xyzzy))>.5(hair-currentbreadth) 
enddef;

vardef generate_grave=
	if serifs: pickup crisp.nib;
	  y21+.5stem=h+eps; bot y22l=max(.8[h,x_height],x_height+o+separator);
	  y22=solve search(y21,y22l); x22l=xpart(p); z22r=2z22-z22l;
	  pos21(stem,angle(z22r-z22l));
	  filldraw circ_stroke z21e--z22e;  %  diagonal
	else: pickup fine.nib; pos21(stem,0); pos22(vair,0);
	  top y21=h; bot y22=vround .8[h,x_height];
	  filldraw stroke z21e--z22e; %  diagonal
	fi
enddef;

def corr_circumflex(expr center,corr)=
	if one_accent:
		generate_universal(center);
	else:
		w:=w+corr; 
		generate_circumflex(center,false);
		w:=w-corr;
	fi
enddef;

def circumflex(expr center)=
	if one_accent:
		generate_universal(center);
	else:
		if italics: w:= w + acc_corr;	fi
		generate_circumflex(center,false);      % variable `squeeze'
		if italics: w:=wi-acc_corr; fi
	fi
enddef;

def generate_circumflex(expr center,squeeze)=
	numeric mid_width; mid_width=if squeeze:.1 else: .5 fi [vair,stem];
	if serifs:
	pickup fine.nib; pos21(hround(mid_width-eps),180); pos22(vair,90);
	 pos23(mid_width,90); pos24(vair,90); pos25(hround(mid_width-eps),180);
	 lft x21r=w-rt x25l=hround (center-w/3); x22-x21=x23-x22=x24-x23=x25-x24;
	 bot y21=bot y24l=vround(.75[x_height,h]-vair);
	 top y22r=top y25=h; y23=.5[y22,y24];
	 filldraw stroke z21e{up}...z22e{right}..z23e..{right}z24e...{up}z25e;
	else:
	 pos21(vair,-180); pos23(vair,0); y21=y23;
	 lft x21r=hround(center-w/3); rt x23r=hround(center+w/3);
	 bot y23=vround max(x_height+o+separator,
	   if squeeze: 2/3 else: 1/3 fi [x_height,h]-.5mid_width);
	 x22=center; pos22(mid_width,90); top y22r=h;
	 filldraw stroke z21e{up}...z22e...{down}z23e;
	fi
enddef;

vardef diaeresis(expr center)=
	generate_diaeresis(center,dot_size,dot_size+1/8w,h,x_height+separator);
enddef;

vardef diaeresis_acute(expr center)=
	  if serifs: x21-x22=1/4w; x21+.5stem=hround(center+5/6(x21-x22)+.5stem);
	  else: rt x21r=hround (3/4w+.5stem); lft x22l=hround(center-.5vair); fi
	  generate_grave; penlabels(21,22);
	  generate_diaeresis(center,.9dot_size,-1,.5[h,x_height],x_height+separator);
enddef;

vardef diaeresis_grave(expr center)=
	  if serifs: x22-x21=1/4w; x21-.5stem=hround(center-5/6(x22-x21)-.5stem);
	  else: lft x21l=hround (1/4w-.5stem); rt x22r=hround(center+.5vair); fi
	  generate_grave; penlabels(21,22);
	  generate_diaeresis(center,.9dot_size,-1,.5[h,x_height],x_height+separator);
enddef;
 
vardef diaeresis_circumflex(expr center)=
	if italics: w:= w + acc_corr;	fi
	generate_circumflex(center,true);
	if italics: w:=w-acc_corr; fi
        numeric sp_bottom, sp_top;
	  sp_top=vround(.15[bot y21l,x_height]); sp_top+sp_bottom=bot y21l+x_height;
	  generate_diaeresis(center,5/8dot_size,5/8dot_size+1/4w,sp_top,sp_bottom);
enddef;
 
vardef generate_diaeresis(expr center,target_dot_diam,sep,eff_h,base)=
	pickup tiny.nib; numeric dot_diam;
	dot_diam:=max(tiny.breadth,hround(target_dot_diam));
	pos31(dot_diam,0); pos32(dot_diam,90); x31=x32; top y32r=vround(eff_h);
	if bot y32l<base: y32l:=min(y32r-eps,base+.5currentbreadth); fi
	y31=.5[y32l,y32r];
	pos33(dot_diam,0); penpos34(y32r-y32l,90); y33=y34=y31; x33=x34;
	if sep>0 : center=.5[x31,x33]; x33-x31=sep;
	else:
		  y35l=y35r=.5[y31,y32]; z35l=whatever[z21l,z22l]; z35r=whatever[z21r,z22r];
		  rt x31r=hround((min(x35l,x35r)-.5currentbreadth)-.05w);
		  lft x33l=hround((max(x35l,x35r)+.5currentbreadth)+.05w);
	fi
	dot(31,32); dot(33,34);
	penlabels(31,32,33,34,35);
enddef;
 
def rough(suffix $,@)(expr dot_size,depth,shear) =
	 pickup fine.nib; pos$(dot_size,90);
	 if square_dots: pos$'(dot_size,180); z$'=z$; dot($',$);  %  squarish dot
	  comma_join_:=max(fine.breadth,floor .7dot_size);
	  comma_bot_:=max(fine.breadth,floor .5dot_size);
	  pos@0(comma_join_,180); pos@1(comma_join_,180);
	  pos@2(comma_bot_,180); y@0=y$; y@1=y$l; y@2=y@1+dot_size-depth;
	  x@0r=x@1r=x$'r; lft x@2r=good.x(x$-eps);
	  filldraw stroke z@0e--z@1e..z@2e;  %  tail
	 else: pos@1(vair,90); pos@2(vair,180); pos@3(vair,-90);
	  z@1r=z$r; lft x@2r=hround(x$-.5depth+.5shear);
	  x@3+.5vair=x$+.5dot_size+shear;
	  y@2=.5[y@1,y@3]; bot y@3r=vround(top y@1r-depth);
	  y_:=ypart((z@1{left}...z@2{down}...z@3)
	   intersectiontimes (z$l{left}..{right}z$r)); if y_<0: y_:=1; fi
	  filldraw z$r{right}..subpath (0,y_) of (z$l{left}..{right}z$r)--cycle; %  dot
	  filldraw stroke z@1e{left}...z@2e{down}...{right}z@3e; fi  %  tail
	 penlabels(@1,@2,@3); 
enddef;

def smooth(suffix $,@)(expr dot_size,depth,shear) =
	 pickup fine.nib; pos$(dot_size,90);
	 if square_dots: pos$'(dot_size,0); z$'=z$; dot($',$);  %  squarish dot
	  comma_join_:=max(fine.breadth,floor .7dot_size);
	  comma_bot_:=max(fine.breadth,floor .5dot_size);
	  pos@0(comma_join_,0); pos@1(comma_join_,0);
	  pos@2(comma_bot_,0); y@0=y$; y@1=y$l; y@2=y@1+dot_size-depth;
	  x@0r=x@1r=x$'r; rt x@2r=good.x(x$-eps);
	  filldraw stroke z@0e--z@1e..z@2e;  %  tail
	 else: pos@1(vair,90); pos@2(vair,0); pos@3(vair,-90);
	  z@1r=z$r; rt x@2r=hround(x$+.5depth+.5shear);
	  x@3-.5vair=hround(x$-.5dot_size+shear);
	  y@2=.5[y@1,y@3]; bot y@3r=vround(top y@1r-depth);
	  y_:=ypart((z@1{right}...z@2{down}...z@3)
	   intersectiontimes (z$l{right}..{left}z$r)); if y_<0: y_:=1; fi
	  filldraw z$r{left}..subpath (0,y_) of (z$l{right}..{left}z$r)--cycle; %  dot
	  filldraw stroke z@1e{right}...z@2e{down}...{left}z@3e; fi  %  tail
	 penlabels(@1,@2,@3); 
enddef;

vardef spirit(expr center) text type=
	if one_accent:
		;
	else:
		numeric sp_bottom, sp_height;
		sp_bottom=h-sp_height=max(.8[h,x_height],x_height+o+separator);
		numeric dot_diam#; dot_diam# = 3/4dot_size#;
		define_whole_blacker_pixels(dot_diam);
		dot_diam:=max(fine.breadth,min(dot_diam,floor(2/3sp_height)));
		x31 if1type0:-else:+fi.5dot_diam=hround(center if1type0:-else:+fi.5dot_diam);
		y31+.5dot_diam=h;
		if1type0:smooth else:rough fi (31,a,dot_diam,sp_height,0);
		penlabels(31);
	fi
enddef;

vardef spirit_acute(expr center) text type =
	if one_accent:
		generate_universal(center);
	else:
		numeric sp_bottom, sp_height;
		sp_bottom=h-sp_height=max(.8[h,x_height],x_height+o+separator);
		numeric dot_diam#; dot_diam# = 3/4dot_size#;
		define_whole_blacker_pixels(dot_diam);
		dot_diam:=max(fine.breadth,min(dot_diam,floor(2/3sp_height)));
		if serifs: x21-x22=1/6w;
		 x21+.5stem=hround(center+.25u+.5vair+(x21-x22)+.5stem);
		else: rt x21r=hround (4/5w+.5stem); lft x22l=hround(center+.25u); fi
		generate_grave; penlabels(21,22);
		if 1type0: % spirit and accent separated by .5u (rough) or .25u (smooth)
		 x31=hround(lft x22 if serifs:r-.5sp_height else:l-.5dot_diam fi -.25u);
		else: x31+.5dot_diam=hround(lft x22 if serifs:r else: l fi -.5u); fi
		y31+.5dot_diam=h;
		if1type0:smooth else:rough fi (31,a,dot_diam,sp_height,0);
	fi
enddef;

vardef spirit_grave(expr center) text type =
	numeric sp_bottom, sp_height;
	sp_bottom=h-sp_height=max(.8[h,x_height],x_height+o+separator);
	numeric dot_diam#; dot_diam# = 3/4dot_size#;
	define_whole_blacker_pixels(dot_diam);
	dot_diam:=max(fine.breadth,min(dot_diam,floor(2/3sp_height)));
	if serifs: x22-x21=1/6w; x21-.5stem=hround(center+.25u);
	else: rt x22r=hround (4/5w+.5stem); lft x21l=hround(center+.25u); fi
	generate_grave; penlabels(21,22);
	if 1type0: % spirit and accent separated by .5u (rough) or .25u (smooth)
	 x31=hround(lft x21l-.5 if serifs:sp_height else:dot_diam fi -.25u);
	else: x31+.5dot_diam=hround(lft x21l-.5u); fi
	y31+.5dot_diam=h;
	if1type0:smooth else:rough fi (31,a,dot_diam,sp_height,0);
enddef;
 
vardef spirit_circumflex(expr center) text type=
	if one_accent:
		generate_universal(center);
	else:
		if italics: w:= w + acc_corr;	fi
		generate_circumflex(center,true);
		if italics: w:=w-acc_corr; fi
		numeric sp_bottom, sp_height, sp_top, box_top;
		box_top=bot if serifs: y21l else: y22l fi;
		sp_top=sp_bottom+sp_height=vround(.15[box_top,x_height]);
		sp_top+sp_bottom=box_top+x_height;
		numeric dot_diam#; dot_diam# = 5/8dot_size#;
		define_whole_blacker_pixels(dot_diam);
		pickup fine.nib;
		dot_diam:=max(fine.breadth,min(dot_diam,floor(2/3sp_height-eps)));
		x31 if1type0:-else:+fi.5dot_diam=hround(center if1type0:-else:+fi.5dot_diam);
		y31+.5dot_diam=vround(sp_top);
		if1type0:smooth else:rough fi (31,a,dot_diam,sp_height,0);
		penlabels(31);
	fi
enddef;
 
def iota_sub(expr pos_stem) =
	if known pic.iota:
	  addto currentpicture also pic.iota shifted (hround(pos_stem),0);
	else:
	  picture temp_picture; temp_picture=currentpicture; clearit;
	  pickup fine.nib;
	  x51=0; pos51(stem,180); top y51=vround(-o-2separator);
	  x52=x51; pos52(stem,180); bot y52=-iota_dp;
	  filldraw circ_stroke z52e--z51e;
	%
	%  edw arqizei to diko mou
	%
	% z54=z52;x53=x54+0.5(y51-y54); y53=y54;
	% pos54(stem,90); pos53(stem,90);
	% filldraw circ_stroke z54e---z53e;
	% lft x55=0.5(rt x52+x52); bot y55=0.5(y52-bot y52)+y52;
	% x56=x55;top y56=y51; pos55(stem,180); pos56(stem,180);
	% unfilldraw circ_stroke z55e--z56e;
	%
	  if not working_hard : picture pic.iota; pic.iota=currentpicture; fi
	  currentpicture:=currentpicture shifted (hround(pos_stem),0);
	  addto currentpicture also temp_picture;
	fi;
enddef;

vardef generate_universal(expr center)=
	x91=center+.15u;x94=center-.15u;
	y91=y94=x_height+1.5separator;
	x92=x91+.3univ_acc_breadth*(body_height-x_height);
	x93=x91-.3univ_acc_breadth*(body_height-x_height);
	y93=y92=y91+univ_acc_breadth*(body_height-x_height);
	fill z91--z92--z93--z94--cycle;
enddef;

vardef universal(expr center)=
	generate_universal(center);
enddef;

vardef diaeresis_universal(expr center)=
	x91=center;
	y91=x_height+separator+dot_size;
	x92=x91+.5univ_acc_breadth*(body_height-x_height);
	x93=x91-.5univ_acc_breadth*(body_height-x_height);
	y93=y92=y91+.866univ_acc_breadth*(body_height-x_height);
	fill z91--z92--z93--cycle;
	generate_diaeresis(center,dot_size,
	1.3univ_acc_breadth*(body_height-x_height),.6[h,x_height],
	x_height+separator);
enddef;

%
% Generate the Accents
%
cmchar "Acute accent";
beginchar("'",9u#,acc_ht#,0);
	adjust_fit(0,0);
	if one_accent:
		universal(.5w);
	else:
		acute(.5w);
	fi
endchar;
%
cmchar "Grave accent";
beginchar("`",9u#,acc_ht#,0);
	adjust_fit(0,0);
	if one_accent:
		universal(.5w);
	else:
		grave(.5w);
	fi
endchar;
%
cmchar "Circumflex accent";
beginchar("~",9u#,circ_ht#,0);
	adjust_fit(0,0);
	if one_accent:
		universal(.5w);
	else:
		circumflex(.5w);
	fi
endchar;
%

cmchar "Rough breathing";
if one_accent:
	beginchar("<",.1u#,acc_ht#,0);
	adjust_fit(0,0);
else:
	beginchar("<",9u#,acc_ht#,0);
	adjust_fit(0,0);spirit(.5w)<;
fi
endchar;

cmchar "Rough breathing with grave";
beginchar(oct"001",9u#,acc_ht#,0);
	adjust_fit(0,0);
	spirit_grave(.5w)<;
endchar;
%
cmchar "Rough breathing with acute";
beginchar(oct"126",9u#,acc_ht#,0);
	adjust_fit(0,0);
	if one_accent: 
		universal(.5w);
	else:
		spirit_acute(.5w)<;
	fi
endchar;

cmchar "Rough breathing with circumflex";
beginchar(oct"100",9u#,acc_ht#,0);
adjust_fit(0,0);
	if one_accent: 
		universal(.5w);
	else:
		spirit_circumflex(.5w)<;
	fi
endchar;

cmchar "Smooth breathing";
if one_accent:
	beginchar(">",.1u#,acc_ht#,0);
	adjust_fit(0,0);
else:
	beginchar(">",9u#,acc_ht#,0);
	adjust_fit(0,0);spirit(.5w)>;
fi
endchar;

cmchar "Smooth breathing with acute";
beginchar(oct"136",9u#,acc_ht#,0);
adjust_fit(0,0);
	if one_accent: 
		universal(.5w);
	else:
		spirit_acute(.5w)>;
	fi
endchar;

cmchar "Smooth breathing with grave";
beginchar(oct"002",9u#,Circ_ht#,0);
adjust_fit(0,0);
	spirit_grave(.5w)>;
endchar;
 
cmchar "Smooth breathing and circumflex";
beginchar(oct"134",9u#,Circ_ht#,0);
adjust_fit(0,0);
	if one_accent: 
		universal(.5w);
	else:
		spirit_circumflex(.5w)>;
	fi
endchar;

cmchar "Diaeresis";
beginchar(oct"042",9u#,circ_ht#,0);
	adjust_fit(0,0);diaeresis(.5w);
endchar;
 
cmchar "Diaeresis with acute";
beginchar(oct"043",9u#,acc_ht#,0);
adjust_fit(0,0);
	if one_accent: 
		diaeresis_universal(.5w);
	else:
		diaeresis_acute(.5w);
	fi
endchar;

cmchar "Diaeresis with grave";
beginchar(oct"044",9u#,acc_ht#,0);
adjust_fit(0,0);
	diaeresis_grave(.5w);
endchar;

cmchar "Diaeresis with circumflex";
beginchar(oct"040",9u#,Circ_ht#,0);
adjust_fit(0,0);
	diaeresis_circumflex(.5w);
endchar;

cmchar "Iota subscript";
beginchar("|",9u#,0,.6desc_depth#);
adjust_fit(0,0);
	if one_accent: 
		;
	else:
		iota_sub(.5w);
	fi
endchar;


cmchar "Breve accent";
beginchar(oct"036",9u#,min(asc_height#,2x_height#),0);
italcorr h#*slant+.5vair#-1.5u#;
adjust_fit(0,0);
pickup crisp.nib; pos1(vair,-180); pos3(vair,0);
top y1=top y3=h; lft x1r=w-rt x3r=hround(2u-.5vair);
numeric mid_thickness; mid_thickness=vround 1/3[vair,stem];
pos2(mid_thickness,-90); x2=.5w;
bot y2r=vround max(x_height+o+tiny,1/3[x_height,h]+o-.5mid_thickness);
filldraw stroke z1e{down}...z2e{right}...{up}z3e;  % stroke
endchar;

cmchar "Macron (bar) accent";
numeric macron_breadth#; macron_breadth#=.2[vair#,stem#];
beginchar(oct"037",9u#,.4[x_height#,asc_height#]+macron_breadth#,0);
italcorr h#*slant-.75u#;
adjust_fit(0,0);
numeric macron_breadth; macron_breadth:=Vround .2[vair,stem];
pickup if serifs: crisp.nib else: fine.nib fi;
pos1(macron_breadth,90); pos2(macron_breadth,90);
top y1r=top y2r=h+o; lft x1=w-rt x2=hround 1.25u;
filldraw stroke z1e--z2e;  % bar
endchar;

