function handles=mytext(x,y,str,fname,ptsize,iibold,tcol)
%
% Call handles=mytext(x,y,str,fname,ptsize,iibold,tcol);
% Places text str of color tcol at (x,y) justified right and bottom (ptsize>0)
% or left and top (ptsize<0) inside a patch of opposite color as tcol.  Optionally
% returns handles to text and patch.
%
% New: set tcol='kx' or 'mx' to omit the background patch of opposity color.
% New: set ptsize=100 + ptsize to make vertical alignment 'Middle' instead of 'Bottom'.
%
if isempty(str); return; end
if nargin<4, fname='Times'; end
if nargin<5, ptsize=14; end
if nargin<6, iibold=1; end
if nargin<7, tcol='k'; end
%
ax=axis; delx=diff(ax(1:2)); dely=abs(diff(ax(3:4)));
pos=get(gca,'Position'); asize=pos(3:4);
offset=.025;
dx=offset*delx/asize(1);
dy=offset*dely/asize(2);
%
if iibold, wt='Bold'; else; wt='Normal'; end
val='Bottom'; if abs(ptsize)>100, ptsize=mod(ptsize,100); val='Middle'; dy=0; end
if ptsize>0,
	h=text(x-dx,y+dy,str,'Color',tcol(1),'FontName',fname,'FontSize',ptsize, ...
		'FontWeight',wt, ...
		'HorizontalAlignment','Right','VerticalAlignment',val);
else
	h=text(x+dx,y+dy,str,'Color',tcol(1),'FontName',fname,'FontSize',-ptsize, ...
		'FontWeight',wt, ...
		'HorizontalAlignment','Left','VerticalAlignment',val);
end
if length(tcol)==1,
	ext=get(h,'Extent'); col=get(h,'Color');
	sg=2*strcmp(get(gca,'Ydir'),'normal') - 1;
	xbox=[ext(1),ext(1)+ext(3),ext(1)+ext(3),ext(1)];
	ybox=[ext(2)+sg*ext(4),ext(2)+sg*ext(4),ext(2),ext(2)];
	[hp]=patch(xbox,ybox,1-col,'EdgeColor',1-col);
	%
	delete(h)
	if ptsize>0,
		h=text(x-dx,y+dy,str,'Color',tcol(1),'FontName',fname,'FontSize',ptsize, ...
			'FontWeight',wt, ...
			'HorizontalAlignment','Right','VerticalAlignment',val);
	else
		h=text(x+dx,y+dy,str,'Color',tcol(1),'FontName',fname,'FontSize',-ptsize, ...
			'FontWeight',wt, ...
			'HorizontalAlignment','Left','VerticalAlignment',val);
	end
	if nargout>0, handles=[h,hp]; end
else
	if nargout>0, handles=[h]; end
end
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
