1 // $Id: pdfclock.cpp,v 1.24 2006/10/01 19:18:32 rjs Exp $
2 //
3 // A little PDFlib application to draw an analog clock.
4 //
5 
6 #include <iostream>
7 
8 #include <time.h>
9 
10 #if !defined(WIN32) && !defined(MAC)
11 #include <unistd.h>
12 #endif
13 
14 #include "pdflib.hpp"
15 
16 using namespace std;
17 
18 #define RADIUS		200.0f
19 #define MARGIN		20.0f
20 
21 int
main()22 main()
23 {
24     try {
25 	PDFlib		*p;
26 	double		alpha;
27 	time_t		timer;
28 	struct tm	ltime;
29 
30 	p = new PDFlib();
31 
32 	//  This means we must check return values of load_font() etc.
33 	p->set_parameter("errorpolicy", "return");
34 
35 	// This line is required to avoid problems on Japanese systems
36 	p->set_parameter("hypertextencoding", "host");
37 
38 	// Open new PDF file
39 	if (p->begin_document("pdfclock.pdf", "") == -1) {
40 	    cerr << "Error: " << p->get_errmsg() << endl; return 2;
41 	}
42 
43 	p->set_info("Creator", "pdfclock.cpp");
44 	p->set_info("Author", "Thomas Merz");
45 	p->set_info("Title", "PDF clock (C++)");
46 
47 	p->begin_page_ext((unsigned int) (2 * (RADIUS + MARGIN)),
48 			  (unsigned int) (2 * (RADIUS + MARGIN)), "");
49 
50 	p->translate(RADIUS + MARGIN, RADIUS + MARGIN);
51 	p->setcolor("fillstroke", "rgb", 0, 0, 1, 0);
52 	p->save();
53 
54 	// minute strokes
55 	p->setlinewidth(2);
56 	for (alpha = 0; alpha < 360; alpha += 6) {
57 	    p->rotate(6);
58 	    p->moveto(RADIUS, 0);
59 	    p->lineto( (RADIUS-MARGIN/3), 0);
60 	    p->stroke();
61 	}
62 
63 	p->restore();
64 	p->save();
65 
66 	// 5 minute strokes
67 	p->setlinewidth(3);
68 	for (alpha = 0; alpha < 360; alpha += 30) {
69 	    p->rotate(30);
70 	    p->moveto(RADIUS, 0);
71 	    p->lineto(RADIUS-MARGIN, 0);
72 	    p->stroke();
73 	}
74 
75 	time(&timer);
76 	ltime = *localtime(&timer);
77 
78 	// draw hour hand
79 	p->save();
80 	p->rotate(
81 		(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0));
82 	p->moveto(-RADIUS/10, -RADIUS/20);
83 	p->lineto(RADIUS/2, 0);
84 	p->lineto(-RADIUS/10, RADIUS/20);
85 	p->closepath();
86 	p->fill();
87 	p->restore();
88 
89 	// draw minute hand
90 	p->save();
91 	p->rotate( (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0));
92 	p->moveto(-RADIUS/10, -RADIUS/20);
93 	p->lineto(RADIUS * 0.8, 0);
94 	p->lineto(-RADIUS/10, RADIUS/20);
95 	p->closepath();
96 	p->fill();
97 	p->restore();
98 
99 	// draw second hand
100 	p->setcolor("fillstroke", "rgb", 1, 0, 0, 0);
101 	p->setlinewidth(2);
102 	p->save();
103 	p->rotate( -((ltime.tm_sec - 15) * 6));
104 	p->moveto(-RADIUS/5, 0);
105 	p->lineto(RADIUS, 0);
106 	p->stroke();
107 	p->restore();
108 
109 	// draw little circle at center
110 	p->circle(0, 0, RADIUS/30);
111 	p->fill();
112 
113 	p->restore();
114 
115 	p->end_page_ext("");
116 
117 	p->end_document("");
118     }
119 
120     catch (PDFlib::Exception &ex) {
121 	cerr << "PDFlib exception occurred in pdfclock sample: " << endl;
122 	cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
123 	    << ": " << ex.get_errmsg() << endl;
124 	return 2;
125     }
126 
127     return 0;
128 }
129