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 | |
---|
55 | FUNCTION STR_SIZE, string, targetWidth, INITSIZE=initsize, STEP=step |
---|
56 | ; |
---|
57 | compile_opt idl2, strictarrsubs |
---|
58 | ; |
---|
59 | |
---|
60 | ON_ERROR, 1 |
---|
61 | |
---|
62 | ; Check positional parameters. |
---|
63 | |
---|
64 | np = N_PARAMS() |
---|
65 | CASE np OF |
---|
66 | 0: MESSAGE, 'One string parameter is required.' |
---|
67 | 1: targetWidth = 0.25 |
---|
68 | ELSE: |
---|
69 | ENDCASE |
---|
70 | |
---|
71 | ; Check keywords. Assign default values. |
---|
72 | |
---|
73 | IF N_ELEMENTS(step) EQ 0 THEN step = 0.05 |
---|
74 | IF N_ELEMENTS(initsize) EQ 0 THEN initsize = 1.0 |
---|
75 | |
---|
76 | ; Calculate a trial width. |
---|
77 | |
---|
78 | size = initsize |
---|
79 | XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $ |
---|
80 | CHARSIZE=-size, /NORMAL |
---|
81 | |
---|
82 | ; Size is perfect. |
---|
83 | |
---|
84 | IF thisWidth EQ targetWidth THEN RETURN, size * Float(!D.Y_Size)/!D.X_Size |
---|
85 | |
---|
86 | ; Initial size is too big. |
---|
87 | |
---|
88 | IF 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 |
---|
95 | ENDIF |
---|
96 | |
---|
97 | ; Initial size is too small. |
---|
98 | |
---|
99 | IF 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 |
---|
107 | ENDIF |
---|
108 | |
---|
109 | END |
---|