1 /************************************************************************/
2 /* */
3 /* Geometry Specification strings. */
4 /* */
5 /************************************************************************/
6
7 # include "appUtilConfig.h"
8
9 # include <stdio.h>
10 # include <string.h>
11
12 # include <appDebugon.h>
13
14 # include "appUnit.h"
15 # include "geoString.h"
16
17 /************************************************************************/
18 /* */
19 /* Represent a Length as a string. */
20 /* */
21 /************************************************************************/
22
appGeoLengthNumberToString(char * target,int twips,int unitInt)23 static void appGeoLengthNumberToString( char * target,
24 int twips,
25 int unitInt )
26 {
27 double units= appUnitFromTwips( twips, unitInt );
28
29 if ( (int)units == units || units > 100 || units < -100 )
30 {
31 if ( units < 0 )
32 { sprintf( target, "%d", (int)( units- 0.4995 ) ); }
33 else{ sprintf( target, "%d", (int)( units+ 0.4995 ) ); }
34 }
35 else{ sprintf( target, "%3.3g", units ); }
36
37 return;
38 }
39
geoLengthToString(char * target,int twips,int unitInt)40 void geoLengthToString( char * target,
41 int twips,
42 int unitInt )
43 {
44 const char * unitString= appUnitTypeString( unitInt );
45
46 char numberString[40];
47 char * s;
48 int l;
49
50 appGeoLengthNumberToString( numberString, twips, unitInt );
51
52 s= numberString;
53 while( *s == ' ' )
54 { s++; }
55 l= strlen( s );
56 while( l > 0 && s[l-1] == ' ' )
57 { l--; s[l]= '\0'; }
58
59 sprintf( target, "%s%s", s, unitString );
60
61 return;
62 }
63
64 /************************************************************************/
65 /* */
66 /* Represent a Rectangle as a string. */
67 /* */
68 /************************************************************************/
69
geoRectangleToString(char * target,int widthTwips,int heightTwips,int unitInt)70 void geoRectangleToString( char * target,
71 int widthTwips,
72 int heightTwips,
73 int unitInt )
74 {
75 const char * unitString= appUnitTypeString( unitInt );
76
77 char widthString[40];
78 char heightString[40];
79 char * ws;
80 char * hs;
81 int l;
82
83 appGeoLengthNumberToString( widthString, widthTwips, unitInt );
84 ws= widthString;
85 while( *ws == ' ' )
86 { ws++; }
87 l= strlen( ws );
88 while( l > 0 && ws[l-1] == ' ' )
89 { l--; ws[l]= '\0'; }
90
91 appGeoLengthNumberToString( heightString, heightTwips, unitInt );
92 hs= heightString;
93 while( *hs == ' ' )
94 { hs++; }
95 l= strlen( hs );
96 while( l > 0 && hs[l-1] == ' ' )
97 { l--; hs[l]= '\0'; }
98
99 sprintf( target, "%s x %s%s", ws, hs, unitString );
100
101 return;
102 }
103
104 /************************************************************************/
105 /* */
106 /* Extract a Rectangle from a string. */
107 /* */
108 /************************************************************************/
109
geoRectangleFromString(const char * s,int defaultUnitInt,int * pWidth,int * pHeight)110 int geoRectangleFromString( const char * s,
111 int defaultUnitInt,
112 int * pWidth,
113 int * pHeight )
114 {
115 double width;
116 double height;
117 char scratch[100];
118 int got;
119
120 int unitInt;
121
122 int w_ival;
123 int w_nom;
124 int w_div;
125
126 int h_ival;
127 int h_nom;
128 int h_div;
129
130 got= sscanf( s, "%lg x %lg%s", &width, &height, scratch );
131
132 if ( got == 2 )
133 {
134 *pWidth= (int)appUnitToTwips( width, defaultUnitInt );
135 *pHeight= (int)appUnitToTwips( height, defaultUnitInt );
136 return 0;
137 }
138
139 if ( got == 3 )
140 {
141 unitInt= appUnitTypeInt( scratch );
142
143 if ( unitInt >= 0 )
144 {
145 *pWidth= (int)appUnitToTwips( width, unitInt );
146 *pHeight= (int)appUnitToTwips( height, unitInt );
147 return 0;
148 }
149 }
150
151 got= sscanf( s, "%d %d/%d x %d %d/%d%s",
152 &w_ival, &w_nom, &w_div,
153 &h_ival, &h_nom, &h_div, scratch );
154 if ( got == 6 )
155 {
156 width= w_ival+ (double)w_nom/(double)w_div;
157 height= h_ival+ (double)h_nom/(double)h_div;
158
159 *pWidth= (int)appUnitToTwips( width, defaultUnitInt );
160 *pHeight= (int)appUnitToTwips( height, defaultUnitInt );
161 return 0;
162 }
163
164 if ( got == 7 )
165 {
166 width= w_ival+ (double)w_nom/(double)w_div;
167 height= h_ival+ (double)h_nom/(double)h_div;
168 unitInt= appUnitTypeInt( scratch );
169
170 if ( unitInt >= 0 )
171 {
172 *pWidth= (int)appUnitToTwips( width, unitInt );
173 *pHeight= (int)appUnitToTwips( height, unitInt );
174 return 0;
175 }
176 }
177
178 got= sscanf( s, "%lg x %d %d/%d%s",
179 &width, &h_ival, &h_nom, &h_div, scratch );
180 if ( got == 4 )
181 {
182 height= h_ival+ (double)h_nom/(double)h_div;
183
184 *pWidth= (int)appUnitToTwips( width, defaultUnitInt );
185 *pHeight= (int)appUnitToTwips( height, defaultUnitInt );
186 return 0;
187 }
188
189 if ( got == 5 )
190 {
191 height= h_ival+ (double)h_nom/(double)h_div;
192 unitInt= appUnitTypeInt( scratch );
193
194 if ( unitInt >= 0 )
195 {
196 *pWidth= (int)appUnitToTwips( width, unitInt );
197 *pHeight= (int)appUnitToTwips( height, unitInt );
198 return 0;
199 }
200 }
201
202 got= sscanf( s, "%d %d/%d x %lg%s",
203 &w_ival, &w_nom, &w_div, &height, scratch );
204 if ( got == 4 )
205 {
206 width= w_ival+ (double)w_nom/(double)w_div;
207
208 *pWidth= (int)appUnitToTwips( width, defaultUnitInt );
209 *pHeight= (int)appUnitToTwips( height, defaultUnitInt );
210 return 0;
211 }
212
213 if ( got == 5 )
214 {
215 width= w_ival+ (double)w_nom/(double)w_div;
216 unitInt= appUnitTypeInt( scratch );
217
218 if ( unitInt >= 0 )
219 {
220 *pWidth= (int)appUnitToTwips( width, unitInt );
221 *pHeight= (int)appUnitToTwips( height, unitInt );
222 return 0;
223 }
224 }
225
226 return -1;
227 }
228
229 /************************************************************************/
230 /* */
231 /* Extract a Length from a string. */
232 /* */
233 /************************************************************************/
234
geoLengthFromString(const char * s,int defaultUnitInt,int * pValue)235 int geoLengthFromString( const char * s,
236 int defaultUnitInt,
237 int * pValue )
238 {
239 double value;
240 char scratch[100];
241 int got;
242 int unitInt;
243
244 int ival;
245 int nomi;
246 int divi;
247
248 got= sscanf( s, "%lg%s", &value, scratch );
249
250 if ( got == 1 )
251 {
252 value= appUnitToTwips( value, defaultUnitInt );
253 if ( value < 0 )
254 { *pValue= value- 0.499; }
255 else{ *pValue= value+ 0.499; }
256 return 0;
257 }
258
259 if ( got == 2 )
260 {
261 unitInt= appUnitTypeInt( scratch );
262
263 if ( unitInt >= 0 )
264 {
265 value= appUnitToTwips( value, unitInt );
266 if ( value < 0 )
267 { *pValue= value- 0.499; }
268 else{ *pValue= value+ 0.499; }
269 return 0;
270 }
271 }
272
273 got= sscanf( s, "%d %d/%d%s", &ival, &nomi, &divi, scratch );
274
275 if ( got == 3 )
276 {
277 value= ival+ (double)nomi/(double)divi;
278 value= appUnitToTwips( value, defaultUnitInt );
279 if ( value < 0 )
280 { *pValue= value- 0.499; }
281 else{ *pValue= value+ 0.499; }
282 return 0;
283 }
284
285
286 if ( got == 4 )
287 {
288 unitInt= appUnitTypeInt( scratch );
289
290 if ( unitInt >= 0 )
291 {
292 value= ival+ (double)nomi/(double)divi;
293 value= appUnitToTwips( value, unitInt );
294 if ( value < 0 )
295 { *pValue= value- 0.499; }
296 else{ *pValue= value+ 0.499; }
297 return 0;
298 }
299 }
300
301 got= sscanf( s, "%d/%d%s", &nomi, &divi, scratch );
302
303 if ( got == 2 )
304 {
305 value= (double)nomi/(double)divi;
306 value= appUnitToTwips( value, defaultUnitInt );
307 if ( value < 0 )
308 { *pValue= value- 0.499; }
309 else{ *pValue= value+ 0.499; }
310 return 0;
311 }
312
313 if ( got == 3 )
314 {
315 unitInt= appUnitTypeInt( scratch );
316
317 if ( unitInt >= 0 )
318 {
319 value= (double)nomi/(double)divi;
320 value= appUnitToTwips( value, unitInt );
321 if ( value < 0 )
322 { *pValue= value- 0.499; }
323 else{ *pValue= value+ 0.499; }
324 return 0;
325 }
326 }
327
328 return -1;
329 }
330