[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[142] | 3 | ; @file_comments |
---|
| 4 | ; The purpose of this function is to return the proper |
---|
[163] | 5 | ; character size to make a specified string a specified |
---|
[142] | 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. |
---|
[2] | 9 | ; |
---|
[142] | 10 | ; @categories |
---|
[157] | 11 | ; Graphics, Widget |
---|
[2] | 12 | ; |
---|
[142] | 13 | ; @param STRING {in}{required} |
---|
[163] | 14 | ; This is the string that you want to make a specified |
---|
[142] | 15 | ; target size or width. |
---|
[2] | 16 | ; |
---|
[163] | 17 | ; @param TARGETWIDTH {in}{optional}{default=0.25} |
---|
[142] | 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 |
---|
[163] | 22 | ; the target width. |
---|
[2] | 23 | ; |
---|
[226] | 24 | ; @keyword INITSIZE {default=1.0} |
---|
[142] | 25 | ; This is the initial size of the string. Default is 1.0. |
---|
[2] | 26 | ; |
---|
[163] | 27 | ; @keyword STEP{default=0.05} |
---|
[142] | 28 | ; This is the amount the string size will change in each step |
---|
[163] | 29 | ; of the interactive process of calculating the string size. |
---|
[2] | 30 | ; |
---|
[226] | 31 | ; @returns |
---|
[142] | 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. |
---|
[2] | 36 | ; |
---|
[142] | 37 | ; @examples |
---|
| 38 | ; To make the string "Happy Holidays" take up 30% of the width of |
---|
| 39 | ; the current graphics window, type this: |
---|
[2] | 40 | ; |
---|
[142] | 41 | ; XYOUTS, 0.5, 0.5, ALIGN=0.5, "Happy Holidays", $ |
---|
| 42 | ; CHARSIZE=STR_SIZE("Happy Holidays", 0.3) |
---|
[2] | 43 | ; |
---|
[142] | 44 | ; @history |
---|
| 45 | ; Written by: David Fanning, 17 DEC 96. |
---|
| 46 | ; Added a scaling factor to take into account the aspect ratio |
---|
[226] | 47 | ; of the window in determining the character size. 28 Oct 97. DWF |
---|
[2] | 48 | ; |
---|
[142] | 49 | ; @version |
---|
| 50 | ; $Id$ |
---|
[2] | 51 | ; |
---|
| 52 | ;- |
---|
| 53 | |
---|
[163] | 54 | FUNCTION str_size, string, targetWidth, INITSIZE=initsize, STEP=step |
---|
[114] | 55 | ; |
---|
| 56 | compile_opt idl2, strictarrsubs |
---|
| 57 | ; |
---|
[2] | 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 |
---|