source: trunk/SRC/ToBeReviewed/STRING/str_size.pro @ 142

Last change on this file since 142 was 142, checked in by navarro, 18 years ago

english and nicer header (2a)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1;+
2;
3; @file_comments
4; The purpose of this function is to return the proper
5; character size to make a specified string a specifed
6; width in a window. The width is specified in normalized
7; coordinates. The function is extremely useful for sizing
8; strings and labels in resizeable graphics windows.
9;
10; @categories
11; Graphics Programs, Widgets.
12;
13; @param STRING {in}{required}
14; This is the string that you want to make a specifed
15; target size or width.
16;
17; @param TARGETWIDTH {in}{optional}
18; This is the target width of the string in normalized
19; coordinates in the current graphics window. The character
20; size of the string (returned as thisCharSize) will be
21; calculated to get the string width as close as possible to
22; the target width. The default is 0.25.
23;
24; @keyword INITSIZE 
25; This is the initial size of the string. Default is 1.0.
26;
27; @keyword STEP
28; This is the amount the string size will change in each step
29; of the interative process of calculating the string size.
30; The default value is 0.05.
31;
32; @returns
33; thisCharSize. This is the size the specified string should be set
34; to if you want to produce output of the specified target
35; width. The value is in standard character size units where
36; 1.0 is the standard character size.
37;
38; @examples
39; To make the string "Happy Holidays" take up 30% of the width of
40; the current graphics window, type this:
41;
42;   XYOUTS, 0.5, 0.5, ALIGN=0.5, "Happy Holidays", $
43;   CHARSIZE=STR_SIZE("Happy Holidays", 0.3)
44;
45; @history
46; Written by: David Fanning, 17 DEC 96.
47; Added a scaling factor to take into account the aspect ratio
48; of the window in determing the character size. 28 Oct 97. DWF
49;
50; @version
51; $Id$
52;
53;-
54
55FUNCTION STR_SIZE, string, targetWidth, INITSIZE=initsize, STEP=step
56;
57  compile_opt idl2, strictarrsubs
58;
59
60ON_ERROR, 1
61
62   ; Check positional parameters.
63
64np = N_PARAMS()
65CASE np OF
66   0: MESSAGE, 'One string parameter is required.'
67   1: targetWidth = 0.25
68   ELSE:
69ENDCASE
70
71   ; Check keywords. Assign default values.
72
73IF N_ELEMENTS(step) EQ 0 THEN step = 0.05
74IF N_ELEMENTS(initsize) EQ 0 THEN initsize = 1.0
75
76   ; Calculate a trial width.
77
78size = initsize
79XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
80      CHARSIZE=-size, /NORMAL
81
82   ; Size is perfect.
83
84IF thisWidth EQ targetWidth THEN RETURN, size * Float(!D.Y_Size)/!D.X_Size
85
86   ; Initial size is too big.
87
88IF thisWidth GT targetWidth THEN BEGIN
89   REPEAT BEGIN
90     XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
91        CHARSIZE=-size, /NORMAL
92      size = size - step
93   ENDREP UNTIL thisWidth LE targetWidth
94   RETURN, size * Float(!D.Y_Size)/!D.X_Size
95ENDIF
96
97   ; Initial size is too small.
98
99IF thisWidth LT targetWidth THEN BEGIN
100   REPEAT BEGIN
101     XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
102        CHARSIZE=-size, /NORMAL
103      size = size + step
104   ENDREP UNTIL thisWidth GT targetWidth
105   size = size - step ; Need a value slightly smaller than target.
106   RETURN, size * Float(!D.Y_Size)/!D.X_Size
107ENDIF
108
109END
Note: See TracBrowser for help on using the repository browser.