source: trunk/STRING/str_size.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • 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
67ON_ERROR, 1
68
69   ; Check positional parameters.
70
71np = N_PARAMS()
72CASE np OF
73   0: MESSAGE, 'One string parameter is required.'
74   1: targetWidth = 0.25
75   ELSE:
76ENDCASE
77
78   ; Check keywords. Assign default values.
79
80IF N_ELEMENTS(step) EQ 0 THEN step = 0.05
81IF N_ELEMENTS(initsize) EQ 0 THEN initsize = 1.0
82
83   ; Calculate a trial width.
84
85size = initsize
86XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
87      CHARSIZE=-size, /NORMAL
88
89   ; Size is perfect.
90
91IF thisWidth EQ targetWidth THEN RETURN, size * Float(!D.Y_Size)/!D.X_Size
92
93   ; Initial size is too big.
94
95IF thisWidth GT targetWidth THEN BEGIN
96   REPEAT BEGIN
97     XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
98        CHARSIZE=-size, /NORMAL
99      size = size - step
100   ENDREP UNTIL thisWidth LE targetWidth
101   RETURN, size * Float(!D.Y_Size)/!D.X_Size
102ENDIF
103
104   ; Initial size is too small.
105
106IF thisWidth LT targetWidth THEN BEGIN
107   REPEAT BEGIN
108     XYOUTS, 0.5, 0.5, ALIGN=0.5, string, WIDTH=thisWidth, $
109        CHARSIZE=-size, /NORMAL
110      size = size + step
111   ENDREP UNTIL thisWidth GT targetWidth
112   size = size - step ; Need a value slightly smaller than target.
113   RETURN, size * Float(!D.Y_Size)/!D.X_Size
114ENDIF
115
116END
Note: See TracBrowser for help on using the repository browser.