1 /* ruler.c */
2 /* COPYRIGHT (C) 2000 THE VICTORIA UNIVERSITY OF MANCHESTER and John Levon
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 /* For ruler stuff */
18 /*
19  * $Log: ruler.c,v $
20  * Revision 1.2  2000/12/06 20:56:03  moz
21  * GPL stuff.
22  *
23  * Revision 1.1.1.1  2000/08/21 01:05:31  moz
24  *
25  *
26  * Revision 1.1.1.1  2000/07/19 22:45:30  moz
27  * CVS Import
28  *
29  * Revision 1.6  2000/03/08 22:12:30  moz
30  * Compile fixes.
31  *
32  * Revision 1.5  2000/01/30 17:21:41  moz
33  * no_desert unused.
34  *
35  * Revision 1.4  1999/11/15 02:05:30  moz
36  * Name change.
37  *
38  * Revision 1.3  1999/05/19 17:08:20  moz
39  * 1.0 Checkin.
40  *
41  * Revision 1.2  1999/04/03 21:11:56  moz
42  * Removed debug message.
43  *
44  * Revision 1.1  1999/03/30 00:05:55  moz
45  * Initial revision
46  *
47  */
48 
49 #include "include/figurine.h"
50 #include "include/extern.h"
51 
52 int
nudge_ruler_x(WindowStruct * window,View * view,int direction)53 nudge_ruler_x(WindowStruct *window, View *view, int direction)
54 {
55 	int factor;
56 	double off;
57 	double orig;
58 
59 	orig = view->x_inches_offset;
60 
61 	if (view->in_mm)
62 		{
63 		if (view->zoom_factor >= 10.0)
64 			factor=1;
65 		else if (view->zoom_factor >=3.0)
66 			factor=2;
67 		else if (view->zoom_factor >= 0.9)
68 			factor=10;
69 		else
70 			factor=20;
71 		}
72 	else
73 		{
74 		if (view->zoom_factor >= 10.0)
75 			factor=1;
76 		else if (view->zoom_factor >= 4.0)
77 			factor=2;
78 		else if (view->zoom_factor >= 0.5)
79 			factor=8;
80 		else
81 			factor=16;
82 		};
83 
84 	if (view->in_mm)
85 		off = direction * MM_TO_INCHES(1.0*factor);
86 	else
87 		off = direction * factor*1.0/32.0;
88 
89 		if ((view->x_inches_offset+off)<0)
90 		view->x_inches_offset = 0;
91 	else if (direction == LEFT)
92 		view->x_inches_offset = max(view->x_inches_offset+off, -25.0);
93 	else
94 		view->x_inches_offset = min(view->x_inches_offset+off, 15.0 + view->doc->width_in_inches);
95 
96 	if (I2D(orig,view)!=I2D(view->x_inches_offset,view))
97 		{
98 		if (view->in_mm)
99 			draw_ruler_x_mm(window,view->zoom_factor, INCHES_TO_MM(view->x_inches_offset));
100 		else
101 			draw_ruler_x(window,view->zoom_factor, view->x_inches_offset);
102 
103 		return 1;
104 		}
105 	 else
106 	 	return -1;
107 
108 }
109 
110 int
nudge_ruler_y(WindowStruct * window,View * view,int direction)111 nudge_ruler_y(WindowStruct *window, View *view, int direction)
112 {
113 	int factor;
114 	double off;
115 	double orig;
116 
117 	orig = view->y_inches_offset;
118 
119 	if (view->in_mm)
120 		{
121 		if (view->zoom_factor >= 10.0)
122 			factor=1;
123 		else if (view->zoom_factor >=3.0)
124 			factor=2;
125 		else if (view->zoom_factor >= 0.9)
126 			factor=10;
127 		else
128 			factor=20;
129 		}
130 	else
131 		{
132 		if (view->zoom_factor >= 10.0)
133 			factor=1;
134 		else if (view->zoom_factor >= 4.0)
135 			factor=2;
136 		else if (view->zoom_factor >= 0.5)
137 			factor=8;
138 		else
139 			factor=16;
140 		};
141 
142 	if (view->in_mm)
143 		off = direction * MM_TO_INCHES(1.0*factor);
144 	else
145 		off = direction * factor*1.0/32.0;
146 
147 	if ((view->y_inches_offset+off)<0)
148 		view->y_inches_offset = 0;
149 	else if (direction == UP)
150 		view->y_inches_offset = max(view->y_inches_offset+off, -25.0);
151 	else
152 		view->y_inches_offset = min(view->y_inches_offset+off, 15.0 + view->doc->height_in_inches);
153 
154 	if (I2D(orig,view)!=I2D(view->y_inches_offset,view))
155 		{
156 		if (view->in_mm)
157 			draw_ruler_y_mm(window,view->zoom_factor, INCHES_TO_MM(view->y_inches_offset));
158 		else
159 			draw_ruler_y(window,view->zoom_factor, view->y_inches_offset);
160 		return 1;
161 		}
162 	else
163 		return -1;
164 
165 }
166 
167 /* the draw routines attempt to leave out lines and numbers that would
168 	make the ruler look cluttered */
169 
170 void
draw_ruler_x(WindowStruct * window,double zoom_factor,double inches_offset)171 draw_ruler_x(WindowStruct *window, double zoom_factor, double inches_offset)
172 {
173 	int count;
174 	int x=0;
175 	int max_height;
176 	int height;
177 	int initial;
178 	int inch;  /* inch in screen pixels at current zoom  */
179 	double inc_offset=0;
180 	char str[3];
181 
182 	XClearWindow(display,window->win);
183 
184 	set_clip(rulergc, 0, 0, (int)window->w, (int)window->h);
185 	XDrawLine(display,window->win,rulergc,0,RULER_PIXEL_SIZE,((int)window->w)-1,RULER_PIXEL_SIZE);
186 
187 	inch = INCHES_TO_PIXELS(1.0*zoom_factor);
188 
189 	if (inch<6)
190 		max_height = RULER_PIXEL_SIZE - 14;
191 	else if (inch<16)
192 		max_height = RULER_PIXEL_SIZE - 8;
193 	else if (inch<32)
194 		max_height = RULER_PIXEL_SIZE - 6;
195 	else if (inch<63)
196 		max_height = RULER_PIXEL_SIZE - 4;
197 	else if (inch<90)
198 		max_height = RULER_PIXEL_SIZE - 3;
199 	else
200 		max_height = RULER_PIXEL_SIZE - 2;
201 
202 	count = (int)(inches_offset*32.0); /* offset is in 1/32 inch units */
203 
204 	x = initial =  ((((double)count)/32.0) - inches_offset)*zoom_factor*screen_ppi;
205 
206 	do
207 		{
208 		if (count % 32 == 0)
209 			{
210 			/* draw every text line  */
211 			sprintf(str,"%d",count/32);
212 			if (inch > XTextWidth(rulerfont,str,(int)strlen(str))+4)
213 				XDrawString(display,window->win,rulergc,
214 					x-XTextWidth(rulerfont,str,(int)strlen(str))-1,
215 					3+rulerfont->max_bounds.ascent, str, (int)strlen(str));
216 			height = RULER_PIXEL_SIZE - 14;
217 			}
218 		else if (count % 64 == 0)
219 			{
220 			/* draw every other text line if space */
221 			sprintf(str,"%d",count/32);
222 			if ((2*inch) > XTextWidth(rulerfont,str,(int)strlen(str))+4)
223 				XDrawString(display,window->win,rulergc,
224 					x-XTextWidth(rulerfont,str,(int)strlen(str))-1,
225 					3+rulerfont->max_bounds.ascent, str, (int)strlen(str));
226 			height = RULER_PIXEL_SIZE - 14;
227 			}
228 		else if (count % 16 == 0)
229 			height = RULER_PIXEL_SIZE - 8;
230 		else if (count % 8 == 0)
231 			height = RULER_PIXEL_SIZE - 6;
232 		else if (count % 4 == 0)
233 			height = RULER_PIXEL_SIZE - 4;
234 		else if (count % 2 == 0)
235 			height = RULER_PIXEL_SIZE - 3;
236 		else
237 			height = RULER_PIXEL_SIZE - 2;
238 
239 		if (height <= max_height)
240 			XDrawLine(display,window->win,rulergc, x, height, x, RULER_PIXEL_SIZE);
241 
242 		count++;
243 		inc_offset += 1.0/32.0;
244 		x = initial + INCHES_TO_PIXELS(inc_offset*zoom_factor);
245 
246 		} while (x<((int)window->w));
247 }
248 
249 void
draw_ruler_x_mm(WindowStruct * window,double zoom_factor,double mm_offset)250 draw_ruler_x_mm(WindowStruct *window, double zoom_factor, double mm_offset)
251 {
252 	int count;
253 	int x=0;
254 	int max_height;
255 	int height;
256 	int initial;
257 	int cm;  /* cm in screen pixels at current zoom  */
258 	double inc_offset=0;
259 	char str[3];
260 
261 	XClearWindow(display,window->win);
262 
263 	set_clip(rulergc, 0, 0, (int)window->w, (int)window->h);
264 	XDrawLine(display,window->win,rulergc,0,RULER_PIXEL_SIZE,(int)window->w-1,RULER_PIXEL_SIZE);
265 
266 	cm = INCHES_TO_PIXELS(MM_TO_INCHES(10)*zoom_factor);
267 
268 	if (cm<25)
269 		max_height = RULER_PIXEL_SIZE - 14;
270 	else if (cm<32)
271 		max_height = RULER_PIXEL_SIZE - 8;
272 	else
273 		max_height = RULER_PIXEL_SIZE - 3;
274 
275 	count = (int)(mm_offset*1.0); /* offset is in 1 mm units */
276 
277 	x = initial =  ((((double)count)) - mm_offset)*zoom_factor*(screen_ppi/25.4);
278 
279 	do
280 		{
281 		if (count % 10 == 0)
282 			{
283 			/* draw every text line  */
284 			sprintf(str,"%d",count/10);
285 			if (cm > XTextWidth(rulerfont,str,(int)strlen(str))+4)
286 				XDrawString(display,window->win,rulergc,
287 					x-XTextWidth(rulerfont,str,(int)strlen(str))-1,
288 					3+rulerfont->max_bounds.ascent, str, (int)strlen(str));
289 			height = RULER_PIXEL_SIZE - 14;
290 			}
291 		else if (count % 20 == 0)
292 			{
293 			/* draw every other text line if space */
294 			sprintf(str,"%d",count/10);
295 			if ((2*cm) > XTextWidth(rulerfont,str,(int)strlen(str))+4)
296 				XDrawString(display,window->win,rulergc,
297 					x-XTextWidth(rulerfont,str,(int)strlen(str))-1,
298 					3+rulerfont->max_bounds.ascent, str, (int)strlen(str));
299 			height = RULER_PIXEL_SIZE - 14;
300 			}
301 		else if (count % 5 == 0)
302 			height = RULER_PIXEL_SIZE - 8;
303 		else
304 			height = RULER_PIXEL_SIZE - 3;
305 
306 		if (height <= max_height)
307 			XDrawLine(display,window->win,rulergc, x, height, x, RULER_PIXEL_SIZE);
308 
309 		count++;
310 		inc_offset += 1.0;
311 		x = initial + INCHES_TO_PIXELS(MM_TO_INCHES(inc_offset)*zoom_factor);
312 
313 		} while (x<(int)window->w);
314 
315 }
316 
317 void
draw_ruler_y(WindowStruct * window,double zoom_factor,double inches_offset)318 draw_ruler_y(WindowStruct *window, double zoom_factor, double inches_offset)
319 {
320 	int count;
321 	int y=0;
322 	int max_height;
323 	int height;
324 	int initial;
325 	int inch;  /* inch in screen pixels at current zoom  */
326 	double inc_offset=0;
327 	char str[3];
328 
329 	XClearWindow(display,window->win);
330 	set_clip(rulergc, 0, 0, (int)window->w, (int)window->h);
331 
332 	XDrawLine(display,window->win,rulergc,RULER_PIXEL_SIZE,0,RULER_PIXEL_SIZE,(int)window->h-1);
333 
334 	inch = INCHES_TO_PIXELS(1.0*zoom_factor);
335 
336 	if (inch<6)
337 		max_height = RULER_PIXEL_SIZE - 14;
338 	else if (inch<16)
339 		max_height = RULER_PIXEL_SIZE - 8;
340 	else if (inch<32)
341 		max_height = RULER_PIXEL_SIZE - 6;
342 	else if (inch<63)
343 		max_height = RULER_PIXEL_SIZE - 4;
344 	else if (inch<90)
345 		max_height = RULER_PIXEL_SIZE - 3;
346 	else
347 		max_height = RULER_PIXEL_SIZE - 2;
348 
349 	count = (int)(inches_offset*32.0); /* offset is in 1/32 inch units */
350 	y = initial =  ((((double)count)/32.0) - inches_offset)*zoom_factor*screen_ppi;
351 
352 	do
353 		{
354 		if (count % 32 == 0)
355 			{
356 			/* draw every text line  */
357 			sprintf(str,"%d",count/32);
358 			if (inch > rulerfont->max_bounds.ascent + rulerfont->max_bounds.descent + 3)
359 				XDrawString(display,window->win,rulergc, 4,
360 						y-rulerfont->max_bounds.descent, str, (int)strlen(str));
361 			height = RULER_PIXEL_SIZE - 14;
362 			}
363 		else if (count % 64 == 0)
364 			{
365 			/* draw every other text line if space */
366 			sprintf(str,"%d",count/32);
367 			if ((2*inch) > rulerfont->max_bounds.ascent + rulerfont->max_bounds.descent + 3)
368 				XDrawString(display,window->win,rulergc, 4,
369 						y-rulerfont->max_bounds.descent, str, (int)strlen(str));
370 			height = RULER_PIXEL_SIZE - 14;
371 			}
372 		else if (count % 16 == 0)
373 			height = RULER_PIXEL_SIZE - 8;
374 		else if (count % 8 == 0)
375 			height = RULER_PIXEL_SIZE - 6;
376 		else if (count % 4 == 0)
377 			height = RULER_PIXEL_SIZE - 4;
378 		else if (count % 2 == 0)
379 			height = RULER_PIXEL_SIZE - 3;
380 		else
381 			height = RULER_PIXEL_SIZE - 2;
382 
383 		if (height <= max_height)
384 			XDrawLine(display,window->win,rulergc, height, y, RULER_PIXEL_SIZE, y);
385 
386 		count++;
387 		inc_offset += 1.0/32.0;
388 		y = initial + INCHES_TO_PIXELS(inc_offset*zoom_factor);
389 
390 		} while (y<(int)window->h);
391 
392 }
393 
394 void
draw_ruler_y_mm(WindowStruct * window,double zoom_factor,double mm_offset)395 draw_ruler_y_mm(WindowStruct *window, double zoom_factor, double mm_offset)
396 {
397 	int count;
398 	int y=0;
399 	int max_height;
400 	int height;
401 	int initial;
402 	int cm;  /* cm in screen pixels at current zoom  */
403 	double inc_offset=0;
404 	char str[3];
405 
406 	XClearWindow(display,window->win);
407 
408 	set_clip(rulergc, 0, 0, (int)window->w, (int)window->h);
409 	XDrawLine(display,window->win,rulergc,RULER_PIXEL_SIZE,0,RULER_PIXEL_SIZE,(int)window->h-1);
410 
411 	cm = INCHES_TO_PIXELS(MM_TO_INCHES(10)*zoom_factor);
412 
413 	if (cm<25)
414 		max_height = RULER_PIXEL_SIZE - 14;
415 	else if (cm<32)
416 		max_height = RULER_PIXEL_SIZE - 8;
417 	else
418 		max_height = RULER_PIXEL_SIZE - 3;
419 
420 	count = (int)(mm_offset*1.0); /* offset is in 1 mm units */
421 	y = initial =  ((((double)count)) - mm_offset)*zoom_factor*(screen_ppi/25.4);
422 
423 	do
424 		{
425 		if (count % 10 == 0)
426 			{
427 			/* draw every text line  */
428 			sprintf(str,"%d",count/10);
429 			if (cm > rulerfont->max_bounds.ascent + rulerfont->max_bounds.descent + 3)
430 				XDrawString(display,window->win,rulergc, 4,
431 						y-rulerfont->max_bounds.descent, str, (int)strlen(str));
432 			height = RULER_PIXEL_SIZE - 14;
433 			}
434 		else if (count % 20 == 0)
435 			{
436 			/* draw every other text line if space */
437 			sprintf(str,"%d",count/10);
438 			if ((2*cm) > rulerfont->max_bounds.ascent + rulerfont->max_bounds.descent + 3)
439 				XDrawString(display,window->win,rulergc, 4,
440 						y-rulerfont->max_bounds.descent, str, (int)strlen(str));
441 			height = RULER_PIXEL_SIZE - 14;
442 			}
443 		else if (count % 5 == 0)
444 			height = RULER_PIXEL_SIZE - 8;
445 		else
446 			height = RULER_PIXEL_SIZE - 3;
447 
448 		if (height <= max_height)
449 			XDrawLine(display,window->win,rulergc, height, y, RULER_PIXEL_SIZE, y);
450 
451 		count++;
452 		inc_offset += 1.0;
453 		y = initial + INCHES_TO_PIXELS(MM_TO_INCHES(inc_offset)*zoom_factor);
454 
455 		} while (y<(int)window->h);
456 
457 }
458