-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.tcl
187 lines (130 loc) · 5.02 KB
/
font.tcl
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
# font.tcl
# package for creating font graphics
#
# -----------------------------------------------------------------------------
package require Tcl 8.6
package provide font 0.1.0
namespace eval ::font {
proc load_font {font_varname} {
upvar $font_varname font_data
set filename "font_def1.tcl"
source $filename
return 1
}
# A procedure to convert input text to a list of (pen,x,y) drawing actions
proc geometry {text font_var insertion_pt size} {
upvar $font_var font_def
# puts $font_def
puts "Mapping text to font 'glyphs': '$text'"
# cycle through each individual character and map the character to the font
set result ""
for {set char_counter 0} {$char_counter <[string length $text]} {incr char_counter} {
set character [string index $text $char_counter]
puts "working on character: $character"
# Add it to the list
set result [lappend result [string map $font_def $character]]
}
puts " input text has [llength $result] character definitions"
puts " result = $result"
#puts "Converting glyphs to absolute coordinates"
set geometry_list ""
set counter 0
# iterate over each character
foreach i $result {
# iterate over each stroke that makes up the character
foreach {pen x y} $i {
append geometry_list " " $pen " " [expr [lindex $insertion_pt 0] + $size * $counter + $x / 8.0 * $size] " " [expr [lindex $insertion_pt 1] + $y / 8.0 * $size ]
}
# increase the x-offset for the next character
incr counter
}
# puts "geometry_list = $geometry_list"
puts " text_xy has [expr [llength $geometry_list] / 3] points"
return $geometry_list
}; # End of procedure definion: geometry
proc angle {x y offset Y L} {
set pi [expr acos(-1.0)]
set rad2deg [expr 180 / $pi]
if {$x >= $offset} {
# puts "x ($x) >= offset ($offset)"
set D [expr sqrt(($offset - $x) * ($offset - $x) + ($Y - $y) * ($Y - $y))]
# angle1 = PI + acos(distance / (2 * LENGTH)) - atan((x - OFFSET) / (YAXIS - y)); //radians
set angle1 [expr $pi + acos($D / (2 * $L)) - atan(($x - $offset) / ($Y - $y))] ; #radians, clockwise from vertical
# angle2 = PI - acos(distance / (2 * LENGTH)) - atan((x - OFFSET) / (YAXIS - y)); //radians
set angle2 [expr $pi - acos($D / (2 * $L)) - atan(($x - $offset) / ($Y - $y))]
} else {
# puts "x ($x) < offset ($offset)"
set D [expr sqrt(($x - $offset)*($x - $offset) + ($Y - $y) * ($Y - $y))]
# puts "D=$D"
# puts "D/2L = [expr $D / (2 * $L)]"
# angle1 = PI + acos(distance / (2 * LENGTH)) + atan((OFFSET - x) / (YAXIS - y)); //radians
set angle1 [expr $pi + acos($D / (2 * $L)) + atan(($offset - $x) / ($Y - $y))] ; # radians, clockwise from vertical
# puts "angle1 = $angle1"
# angle2 = PI - acos(distance / (2 * LENGTH)) + atan((OFFSET - x) / (YAXIS - y)); //radians
set angle2 [expr $pi - acos($D / (2 * $L)) + atan(($offset - $x) / ($Y - $y))]
# puts "angle2 = $angle2"
}
# Convert to degrees and return
return [list [expr $angle1 * $rad2deg ] [expr $angle2 * $rad2deg]]
}
# Convert font definition from xy to rotation basis
proc xy2rot {text_xy offset Y L} {
# The input text is a list of triples (pen_down_flag x y)
set text_angles ""
foreach {i j k} $text_xy {
set rot [angle $j $k $offset $Y $L]
# puts "rot=$rot"
append text_angles " $i $rot"
}
puts " text_rot has [expr [llength $text_angles]/3] points."
return $text_angles
}
# Convert font definition from xy to rotation basis
proc xy2rot2 {text_xy offset1 offset2 y_axis length} {
# The input text is a list of triples (pen_down_flag x y)
set text_angles ""
foreach {i j k} $text_xy {
set rot [angle2 $j $k $offset1 $offset2 $y_axis $length]
# puts "rot=$rot"
append text_angles " $i $rot"
}
puts " text_rot has [expr [llength $text_angles]/3] points."
return $text_angles
}
#
proc step {rot steps_per_rot step_var} {
upvar $step_var step_var
set steps [expr 360.0 / $steps_per_rot * $rot]
puts "completing $steps steps"s
set gpio [list 10 11 12 13]
set steps [ list {1 0 0 0} {0 1 0 0 } {0 0 1 0} {0 0 0 1}]
set counter 0
while {$counter > 0} {
if {$rot > 0} {
# CW
} else {
# CCW
}
incr counter -1
}
}
}; # END OF NAMESPACE DEFINITION
return
# Test data
set x 209.99 ; # Cartesion coordinate of the destination point
set y 0.0 ; # Cartesian coordinate of the destination point
set offset 210.0 ; # The horizontal distance from the origin point (0,0) to the spindle mount of the arm, mm
set Y 465.0 ; # The vertical distance from the origin point(0,0) to the spindle mount of the arm, mm
set L 300.0 ; # the length of the plotting arm(s). The plotting arm linkages are all of equal length.
# set result [::tclStepper::angle $x $y $offset $Y $L]
# puts "Result is:$result"
# Load the font data
::font::load_font font_data
# Create some test text
set insertion_pt [list 100.0 100.0]
set size 10.0
set text "10:00 @"
set text_xy [::font::geometry $text font_data $insertion_pt $size]
# puts "text_xy=$text_xy"
set text_rot [::font::xy2rot $text_xy $offset $Y $L]
# puts "text_rot=$text_rot"