-
Notifications
You must be signed in to change notification settings - Fork 1
/
str_size.pro
214 lines (197 loc) · 7.07 KB
/
str_size.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;+
; NAME:
; STR_SIZE
;
; PURPOSE:
;
; The purpose of this function is to return the proper
; character size to make a specified string a specifed
; width in a window. The width is specified in normalized
; coordinates. The function is extremely useful for sizing
; strings and labels in resizeable graphics windows.
;
; AUTHOR:
;
; FANNING SOFTWARE CONSULTING
; David Fanning, Ph.D.
; 1645 Sheely Drive
; Fort Collins, CO 80526 USA
; Phone: 970-221-0438
; E-mail: [email protected]
; Coyote's Guide to IDL Programming: http://www.dfanning.com/
;
; CATEGORY:
;
; Graphics Programs, Widgets.
;
; CALLING SEQUENCE:
;
; thisCharSize = STR_SIZE(thisSting, targetWidth)
;
; INPUTS:
;
; thisString: This is the string that you want to make a specifed
; target size or width.
;
; OPTIONAL INPUTS:
;
; targetWidth: This is the target width of the string in normalized
; coordinates in the current graphics window. The character
; size of the string (returned as thisCharSize) will be
; calculated to get the string width as close as possible to
; the target width. The default is 0.25.
;
; KEYWORD PARAMETERS:
;
; INITSIZE: This is the initial size of the string. Default is 1.0.
;
; STEP: This is the amount the string size will change in each step
; of the interative process of calculating the string size.
; The default value is 0.05.
;
; XPOS: X position of the output test string. This can be
; used on the Postscript device, where no pixmap windows are
; available and where therefore the test strings would appear on
; the printable area. Default is 0.5 on most devices. If !D.NAME
; is PS, the default is 2.0 to draw the test string out of the
; drawable window area.
;
; YPOS: Y position of the output test string. This can be
; used on the Postscript device, where no pixmap windows are
; available and where therefore the test strings would appear on
; the printable area. Default is 0.5 on most devices. If !D.NAME
; is PS, the default is 2.0 to draw the test string out of the
; drawable window area.
;
; OUTPUTS:
;
; thisCharSize: This is the size the specified string should be set
; to if you want to produce output of the specified target
; width. The value is in standard character size units where
; 1.0 is the standard character size.
;
; EXAMPLE:
;
; To make the string "Happy Holidays" take up 30% of the width of
; the current graphics window, type this:
;
; XYOUTS, 0.5, 0.5, ALIGN=0.5, "Happy Holidays", $
; CHARSIZE=STR_SIZE("Happy Holidays", 0.3)
;
; MODIFICATION HISTORY:
;
; Written by: David Fanning, 17 DEC 96.
; Added a scaling factor to take into account the aspect ratio
; of the window in determing the character size. 28 Oct 97. DWF
; Added check to be sure hardware fonts are not selected. 29 April 2000. DWF.
; Added a pixmap to get proper scaling in skinny windows. 16 May 2000. DWF.
; Forgot I can't do pixmaps in all devices. :-( Fixed. 7 Aug 2000. DWF.
; Added support of PostScript at behest of Benjamin Hornberger. 11 November 2004. DWF.
;-
;
;###########################################################################
;
; LICENSE
;
; This software is OSI Certified Open Source Software.
; OSI Certified is a certification mark of the Open Source Initiative.
;
; Copyright © 2000-2004 Fanning Software Consulting.
;
; This software is provided "as-is", without any express or
; implied warranty. In no event will the authors be held liable
; for any damages arising from the use of this software.
;
; Permission is granted to anyone to use this software for any
; purpose, including commercial applications, and to alter it and
; redistribute it freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must
; not claim you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation
; would be appreciated, but is not required.
;
; 2. Altered source versions must be plainly marked as such, and must
; not be misrepresented as being the original software.
;
; 3. This notice may not be removed or altered from any source distribution.
;
; For more information on Open Source Software, visit the Open Source
; web site: http://www.opensource.org.
;
;###########################################################################
FUNCTION STR_SIZE, theString, targetWidth, $
INITSIZE=initsize, $
STEP=step, $
XPOS=xpos, $
YPOS=ypos
ON_ERROR, 2
; No hardware fonts.
thisFont = !P.Font
IF (thisFont EQ 0) AND (!D.NAME NE 'PS') THEN !P.Font = -1
; Check positional parameters.
np = N_PARAMS()
CASE np OF
0: MESSAGE, 'One string parameter is required.'
1: targetWidth = 0.25
ELSE:
ENDCASE
; Check keywords. Assign default values.
IF N_ELEMENTS(initsize) EQ 0 THEN initsize = 1.0
IF N_ELEMENTS(step) EQ 0 THEN step = 0.05
IF N_ELEMENTS(orientation) EQ 0 THEN orientation = 0.0 ELSE orientation = Float(orientation)
IF N_ELEMENTS(xpos) EQ 0 THEN IF !D.NAME NE 'PS' THEN xpos = 0.5 ELSE xpos = 2.0
IF N_ELEMENTS(ypos) EQ 0 THEN IF !D.NAME NE 'PS' THEN ypos = 0.5 ELSE ypos = 2.0
; Create a pixmap window for drawing the string.
currentWindow = !D.Window
IF !D.X_Size GE !D.Y_Size AND ((!D.Flags AND 256) NE 0) THEN BEGIN
Window, /Pixmap, /Free, XSize=!D.X_Size, YSize=!D.Y_Size
pixID = !D.Window
ENDIF ELSE BEGIN
IF ((!D.Flags AND 256) NE 0) THEN BEGIN
Window, /Pixmap, /Free, XSize=!D.Y_Size, YSize=!D.X_Size
pixID = !D.Window
ENDIF
ENDELSE
; Calculate a trial width.
strTrialSize = initsize
XYOUTS, xpos, ypos, ALIGN=0.5, theString, WIDTH=thisWidth, $
CHARSIZE=-strTrialSize, /NORMAL
; Size is perfect.
IF thisWidth EQ targetWidth THEN BEGIN
!P.Font = thisFont
theSize = strTrialSize; * Float(!D.Y_Size)/!D.X_Size
IF currentWindow NE -1 THEN WSet, currentWindow
IF N_Elements(pixID) NE 0 THEN WDelete, pixID
RETURN, theSize
ENDIF
; Initial size is too big.
IF thisWidth GT targetWidth THEN BEGIN
REPEAT BEGIN
XYOUTS, xpos, ypos, ALIGN=0.5, theString, WIDTH=thisWidth, $
CHARSIZE=-strTrialSize, /NORMAL
strTrialSize = strTrialSize - step
ENDREP UNTIL thisWidth LE targetWidth
!P.Font = thisFont
theSize = strTrialSize
IF currentWindow NE -1 THEN WSet, currentWindow
IF N_Elements(pixID) NE 0 THEN WDelete, pixID
RETURN, theSize
ENDIF
; Initial size is too small.
IF thisWidth LT targetWidth THEN BEGIN
REPEAT BEGIN
XYOUTS, xpos, ypos, ALIGN=0.5, theString, WIDTH=thisWidth, $
CHARSIZE=-strTrialSize, /NORMAL
strTrialSize = strTrialSize + step
ENDREP UNTIL thisWidth GT targetWidth
strTrialSize = strTrialSize - step ; Need a value slightly smaller than target.
!P.Font = thisFont
theSize = strTrialSize
IF currentWindow NE -1 THEN WSet, currentWindow
IF N_Elements(pixID) NE 0 THEN WDelete, pixID
RETURN, theSize
ENDIF
; Cleanup.
!P.Font = thisFont
END