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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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