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