;	(13-apr-92)
pro text_size,text,ncolumns,nrows,xsize=xsize,ysize=ysize,size=size
;+
;  Name:
;    text_size
;  Purpose:
;    Try to figure out the size of a text drawn in a window
;
;  Calling sequence:
;    text_size,text,ncolumns,nrows
;    text_size,text,ncolumns,nrows,xsize=size,ysize=ysize
;
;  Inputs:
;    text	= A text string, scalar or array.
;  Outputs:
;    ncolumns	= Number of columns required (array if text is an array)
;    nrows	= Numbe of rows required     (array if text is an array)
;
;  Optional Input Keywords:
;    xsize	= Size of the pixmap window (default is 512)
;    ysize	= Size of the pixmap window (default is 512)
;    size	= Size parameter in the call to xyouts (may be a vector to
	;		match text)
;
;  Method:
;    Open a window and then read back the results
;
;  Side effects:
;    A window will be created temporarily and then deleted.
;  Modification History:
;    13-apr-92, Written, JRL
;-

set_plot, 'x'		; Make sure we working with the X-device

if n_elements(xsize) eq 0 then xsize = 512
if n_elements(ysize) eq 0 then ysize = 512
if n_elements(size)  eq 0 then size  = 1.0

window_save = !d.window			; Save the current window index
window,xs=xsize,ys=ysize,/free		; Open a new window
wd = !d.window

xpos = xsize * .10
ypos = ysize * .20			; Placement in the temporary window

ntit = n_elements( text )
if n_elements(size) ge ntit then wsize = size else wsize = replicate(size,ntit)
for i=0,ntit-1 do begin

  erase & wshow
  xyouts,xpos,ypos,text(i),size=wsize(i),/dev	; Write the text
  dump = tvrd(0,0,xsize,ysize)		; read it back

  xxx = sumcol( dump )			; Collapse to a row vector
  yyy = sumrow( dump )			; Collapse to a column vector

  ix = where(xxx gt 0)
  iy = where(yyy gt 0)

  xmin = min(ix) & xmax = max(ix) 
  ymin = min(iy) & ymax = max(iy) 

  if i eq 0 then begin
    ncolumns = xmax-xmin+1
    nrows    = ymax-ymin+1
  endif else begin
    ncolumns = [ncolumns, xmax-xmin+1]
    nrows    = [nrows,    ymax-ymin+1]
  endelse

endfor
!c = 0

wdelete,wd					; Delete the window
if window_save gt 0 then wset,window_save	; Restore the window index

return
end


