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

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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