1#!/bin/sh
2# $Id: pdfclock.tcl 14574 2005-10-29 16:27:43Z bonefish $
3#
4# PDFlib client: pdfclock example in Tcl
5#
6
7# Hide the exec to Tcl but not to the shell by appending a backslash\
8exec tclsh "$0" ${1+"$@"}
9
10# The lappend line is unnecessary if PDFlib has been installed
11# in the Tcl package directory
12set auto_path [linsert $auto_path 0 .libs .]
13
14package require pdflib 5.0
15
16set RADIUS 200.0
17set MARGIN 20.0
18
19set p [PDF_new]
20
21if {[PDF_open_file $p "pdfclock.pdf"] == -1} {
22    puts stderr "Error: [PDF_get_errmsg $p]"
23}
24
25
26PDF_set_info $p "Creator" "pdfclock.tcl"
27PDF_set_info $p "Author" "Thomas Merz"
28PDF_set_info $p "Title" "PDF clock (Tcl)"
29
30PDF_begin_page $p [expr 2 * ($RADIUS + $MARGIN)] [expr  2 * ($RADIUS + $MARGIN)]
31
32PDF_translate $p [expr $RADIUS + $MARGIN] [expr $RADIUS + $MARGIN]
33PDF_setcolor $p "fillstroke" "rgb" 0.0 0.0 1.0 0.0
34PDF_save $p
35
36# minute strokes
37PDF_setlinewidth $p 2.0
38for {set alpha  0} {$alpha < 360} {set alpha [expr $alpha + 6]} {
39    PDF_rotate $p 6.0
40    PDF_moveto $p $RADIUS 0.0
41    PDF_lineto $p [expr $RADIUS-$MARGIN/3] 0.0
42    PDF_stroke $p
43}
44
45PDF_restore $p
46PDF_save $p
47
48# 5 minute strokes
49PDF_setlinewidth $p 3.0
50for {set alpha  0} {$alpha < 360} {set alpha [expr $alpha + 30]} {
51    PDF_rotate $p 30.0
52    PDF_moveto $p $RADIUS 0.0
53    PDF_lineto $p [expr $RADIUS-$MARGIN] 0.0
54    PDF_stroke $p
55}
56
57set tm_hour [ clock format [clock seconds] -format "%I" ]
58set tm_min  [ clock format [clock seconds] -format "%M" ]
59set tm_sec  [ clock format [clock seconds] -format "%S" ]
60
61# This is a Tcl-itis: when the clock returns "08" seconds, tm_sec
62# won't be recognized as an integer. Therefore we "cast" it to integer.
63# Note that this doesn't happen with, for example, the value "07"
64# because this is a valid octal number...
65
66scan $tm_hour %d tm_hour
67scan $tm_min  %d tm_min
68scan $tm_sec  %d tm_sec
69
70# draw hour hand
71PDF_save $p
72PDF_rotate $p [expr -(($tm_min/60.0) + $tm_hour - 3.0) * 30.0]
73PDF_moveto $p [expr -$RADIUS/10] [expr -$RADIUS/20]
74PDF_lineto $p [expr $RADIUS/2] 0.0
75PDF_lineto $p [expr -$RADIUS/10] [expr $RADIUS/20]
76PDF_closepath $p
77PDF_fill $p
78PDF_restore $p
79
80# draw minute hand
81PDF_save $p
82PDF_rotate $p [expr -(($tm_sec/60.0) + $tm_min - 15.0) * 6.0]
83PDF_moveto $p [expr -$RADIUS/10] [expr -$RADIUS/20]
84PDF_lineto $p [expr $RADIUS * 0.8] 0.0
85PDF_lineto $p [expr -$RADIUS/10] [expr $RADIUS/20]
86PDF_closepath $p
87PDF_fill $p
88PDF_restore $p
89
90# draw second hand
91PDF_setcolor $p "fillstroke" "rgb" 1.0 0.0 0.0 0.0
92PDF_setlinewidth $p 2
93PDF_save $p
94PDF_rotate $p [expr -(($tm_sec - 15.0) * 6.0)]
95PDF_moveto $p [expr -$RADIUS/5] 0.0
96PDF_lineto $p $RADIUS 0.0
97PDF_stroke $p
98PDF_restore $p
99
100# draw little circle at center
101PDF_circle $p 0 0 [expr $RADIUS/30]
102PDF_fill $p
103
104PDF_restore $p
105
106PDF_end_page $p
107PDF_close $p
108
109PDF_delete $p
110