-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch2svg-plugin.tcl
1732 lines (1536 loc) · 44.1 KB
/
patch2svg-plugin.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# META convert a patch to a picture (SVG)
# META DESCRIPTION exports your patch as an SVG-image
# META AUTHOR IOhannes m zmölnig <[email protected]>
# META VERSION 0.1
package require pdwindow 0.1
if [catch {
package require msgcat
::msgcat::mcload po
}] { puts "patch2svg: i18n failed" }
#package require uriencode
#package require tinyfileutils
namespace eval ::patch2svg:: {
variable label
proc export {mytoplevel filename} {
can2svg::canvas2file [tkcanvas_name $mytoplevel] $filename
}
set counter 0
# ::patch2svg::exportall
proc exportall {{template "%s%x.svg"}} {
## exports all open windows to SVG
## template vars:
## - '%s' window name
## - '%x' window id
## - '%c' counter (self-incrementing)
if {[string length $template] == 0 } {set template "%s%x.svg" }
::pdwindow::debug "patch2svg: template $template\n"
foreach w [get_patchwindows] {
incr ::patch2svg::counter
set wname [lookup_windowname $w]
set wname_ [string map [list "/" "_" " " "_"] $wname]
set name [string map [list %s "${wname_}" %x "${w}" %c "${::patch2svg::counter}"] $template]
pdtk_post "exporting to SVG: $name\n"
::pdwindow::debug " patch2svg: w $w\n"
::pdwindow::debug " patch2svg: wname $wname\n"
::pdwindow::debug " patch2svg: fname $name\n"
::pdwindow::debug " patch2svg: \n"
export $w $name
}
}
proc is_patchwindow {w} {
#expr {[winfo toplevel $w] eq $w && ![catch {$w cget -menu}]}
expr {[winfo class $w] eq "PatchWindow"}
}
proc get_patchwindows {{w .}} {
set list {}
if {[is_patchwindow $w]} {
lappend list $w
}
foreach w [winfo children $w] {
lappend list {*}[get_patchwindows $w]
}
return $list
}
# can2svg.tcl ---
#
# This file provides translation from canvas commands to XML/SVG format.
#
# Copyright (c) 2002-2007 Mats Bengtsson
#
# This file is distributed under BSD style license.
#
# $Id: can2svg.tcl,v 1.26 2008-01-27 08:19:36 matben Exp $
#
# ########################### USAGE ############################################
#
# NAME
# can2svg - translate canvas command to SVG.
#
# SYNOPSIS
# can2svg canvasCmd ?options?
# canvasCmd is everything except the widget path name.
#
# can2svg::canvas2file widgetPath fileName ?options?
# options: -height
# -width
#
# can2svg::can2svg canvasCmd ?options?
# options: -httpbasedir path
# -imagehandler tclProc
# -ovalasellipse 0|1
# -reusedefs 0|1
# -uritype file|http
# -usestyleattribute 0|1
# -usetags 0|all|first|last
# -windowitemhandler tclProc
#
# can2svg::config ?options?
# options: -allownewlines 0
# -filtertags ""
# -httpaddr localhost
# -ovalasellipse 0
# -reusedefs 1
# -uritype file
# -usetags all
# -usestyleattribute 1
# -windowitemhandler tclProc
#
# ########################### CHANGES ##########################################
#
# 0.1 first release
# 0.2 URI encoded image file path
# 0.3 uses xmllists more, added svgasxmllist
#
# ########################### TODO #############################################
#
# handle units (m->mm etc.)
# better support for stipple patterns
# how to handle tk editing? DOM?
#
# ...
# We need URN encoding for the file path in images. From my whiteboard code.
namespace eval can2svg {
# namespace export can2svg canvas2file
variable confopts
array set confopts {
-allownewlines 0
-filtertags ""
-httpaddr localhost
-ovalasellipse 0
-reusedefs 1
-uritype file
-usetags all
-usestyleattribute 1
-windowitemhandler ""
}
set confopts(-httpbasedir) [info script]
variable formatArrowMarker
variable formatArrowMarkerLast
# The key into this array is 'arrowMarkerDef_$col_$a_$b_$c', where
# col is color, and a, b, c are the arrow's shape.
variable defsArrowMarkerArr
# Similarly for stipple patterns.
variable defsStipplePatternArr
# This shouldn't be hardcoded!
variable defaultFont {Helvetica 12}
variable pi 3.14159265359
variable anglesToRadians [expr {$pi/180.0}]
variable grayStipples {gray75 gray50 gray25 gray12}
# Make 4x4 squares. Perhaps could be improved.
variable stippleDataArr
set stippleDataArr(gray75) \
{M 0 0 h3 M 0 1 h1 M 2 1 h2
M 0 2 h3 M 0 3 h1 M 2 3 h1}
set stippleDataArr(gray50) \
{M 0 0 h1 M 2 0 h1 M 1 1 h1 M 3 1 h1
M 0 2 h1 M 2 2 h1 M 1 3 h1 M 3 3 h1}
set stippleDataArr(gray25) \
{M 3 0 h1 M 1 1 h1 M 3 2 h1 M 1 3 h1}
set stippleDataArr(gray12) \
{M 1 1 h1 M 3 3 h1}
}
proc can2svg::config {args} {
variable confopts
set options [lsort [array names confopts -*]]
set usage [join $options ", "]
if {[llength $args] == 0} {
set result {}
foreach name $options {
lappend result $name $confopts($name)
}
return $result
}
regsub -all -- - $options {} options
set pat ^-([join $options |])$
if {[llength $args] == 1} {
set flag [lindex $args 0]
if {[regexp -- $pat $flag]} {
return $confopts($flag)
} else {
return -code error "Unknown option $flag, must be: $usage"
}
} else {
foreach {flag value} $args {
if {[regexp -- $pat $flag]} {
set confopts($flag) $value
} else {
return -code error "Unknown option $flag, must be: $usage"
}
}
}
}
# can2svg::can2svg --
#
# Make xml out of a canvas command, widgetPath removed.
#
# Arguments:
# cmd canvas create commands without prepending widget path.
# args -httpbasedir path
# -imagehandler tclProc
# -ovalasellipse 0|1
# -reusedefs 0|1
# -uritype file|http
# -usestyleattribute 0|1
# -usetags 0|all|first|last
#
# Results:
# xml data
proc can2svg::can2svg {cmd args} {
set xml ""
foreach xmllist [eval {svgasxmllist $cmd} $args] {
append xml [MakeXML $xmllist]
}
return $xml
}
# can2svg::svgasxmllist --
#
# Make a list of xmllists out of a canvas command, widgetPath removed.
#
# Arguments:
# cmd canvas create command without prepending widget path.
# args -httpbasedir path
# -imagehandler tclProc
# -ovalasellipse 0|1
# -reusedefs 0|1
# -uritype file|http
# -usestyleattribute 0|1
# -usetags 0|all|first|last
#
# Results:
# a list of xmllist = {tag attrlist isempty cdata {child1 child2 ...}}
proc can2svg::svgasxmllist {cmd args} {
variable confopts
variable defsArrowMarkerArr
variable defsStipplePatternArr
variable defaultFont
variable grayStipples
set nonum_ {[^0-9]}
set wsp_ {[ ]+}
set xmlLL [list]
array set argsA [array get confopts]
array set argsA $args
set args [array get argsA]
if {![string equal [lindex $cmd 0] "create"]} {
return
}
set type [lindex $cmd 1]
set rest [lrange $cmd 2 end]
# Separate coords from options.
set indopt [lsearch -regexp $rest "-${nonum_}"]
if {$indopt < 0} {
set ind end
set opts [list]
} else {
set ind [expr {$indopt - 1}]
set opts [lrange $rest $indopt end]
}
# Flatten coordinate list!
set coo [lrange $rest 0 $ind]
if {[llength $coo] == 1} {
set coo [lindex $coo 0]
}
array set optA $opts
# Is the item in normal state? If not, return.
if {[info exists optA(-state)] && $optA(-state) != "normal"} {
return
}
# Figure out if we've got a spline.
set haveSpline 0
if {[info exists optA(-smooth)] && ($optA(-smooth) != "0") && \
[info exists optA(-splinesteps)] && ($optA(-splinesteps) > 2)} {
set haveSpline 1
}
if {[info exists optA(-fill)]} {
set fillValue $optA(-fill)
if {![regexp {#[0-9]+} $fillValue]} {
set fillValue [FormatColorName $fillValue]
}
} else {
set fillValue black
}
set id ""
if {[string length $argsA(-filtertags)] && [info exists optA(-tags)]} {
set tag [uplevel #0 $argsA(-filtertags) [list $optA(-tags)]]
set id $tag
} elseif {($argsA(-usetags) != "0") && [info exists optA(-tags)]} {
# Remove any 'current' tag.
set optA(-tags) \
[lsearch -all -not -inline $optA(-tags) current]
set id ""
switch -- $argsA(-usetags) {
all {
set id $optA(-tags)
}
first {
set id [lindex $optA(-tags) 0]
}
last {
set id [lindex $optA(-tags) end]
}
}
}
set id [string trim $id]
if { $id != {} } {
if { ! [string is alpha [string index $id 0] ] } {
set id "PD$id"
}
set id [string map {" " "__"} $id]
set idAttr [list id $id]
} else {
set idAttr ""
}
# If we need a marker (arrow head) need to make that first.
if {[info exists optA(-arrow)] && ![string equal $optA(-arrow) "none"]} {
if {[info exists optA(-arrowshape)]} {
# Make a key of the arrowshape list into the array.
regsub -all -- $wsp_ $optA(-arrowshape) _ shapeKey
set arrowKey ${fillValue}_${shapeKey}
set arrowShape $optA(-arrowshape)
} else {
set arrowKey ${fillValue}
set arrowShape {8 10 3}
}
if {!$argsA(-reusedefs) || \
![info exists defsArrowMarkerArr($arrowKey)]} {
set defsArrowMarkerArr($arrowKey) \
[eval {MakeArrowMarker} $arrowShape {$fillValue}]
set xmlLL \
[concat $xmlLL $defsArrowMarkerArr($arrowKey)]
}
}
# If we need a stipple bitmap, need to make that first. Limited!!!
# Only: gray12, gray25, gray50, gray75
foreach key {-stipple -outlinestipple} {
if {[info exists optA($key)] && \
([lsearch $grayStipples $optA($key)] >= 0)} {
set stipple $optA($key)
if {![info exists defsStipplePatternArr($stipple)]} {
set defsStipplePatternArr($stipple) \
[MakeGrayStippleDef $stipple]
}
lappend xmlLL $defsStipplePatternArr($stipple)
}
}
#puts "can2svg::svgasxmllist cmd=$cmd, args=$args"
switch -- $type {
arc {
# Had to do it the hard way! (?)
# "Wrong" coordinate system :-(
set attr [CoordsToAttr $type $coo $opts elem]
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
lappend xmlLL [MakeXMLList $elem -attrlist $attr]
}
bitmap - image {
if {[info exists optA(-image)]} {
set elem "image"
set attr [eval {MakeImageAttr $coo $opts} $args]
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set subEs [list]
if {[info exists argsA(-imagehandler)]} {
set subE [uplevel #0 $argsA(-imagehandler) [list $cmd] $args]
if {[llength $subE]} {
set subEs [list $subE]
}
}
lappend xmlLL [MakeXMLList $elem -attrlist $attr -subtags $subEs]
}
}
line {
set attr [CoordsToAttr $type $coo $opts elem]
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
lappend xmlLL [MakeXMLList $elem -attrlist $attr]
}
oval {
set attr [CoordsToAttr $type $coo $opts elem]
foreach {x y w h} [NormalizeRectCoords $coo] break
if {[expr {$w == $h}] && !$argsA(-ovalasellipse)} {
# set elem "circle";# circle needs an r: not an rx & ry
set elem "ellipse"
} else {
set elem "ellipse"
}
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
lappend xmlLL [MakeXMLList $elem -attrlist $attr]
}
polygon {
set attr [CoordsToAttr $type $coo $opts elem]
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
lappend xmlLL [MakeXMLList $elem -attrlist $attr]
}
rectangle {
set attr [CoordsToAttr $type $coo $opts elem]
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
lappend xmlLL [MakeXMLList $elem -attrlist $attr]
}
text {
set elem "text"
set chdata ""
set nlines 1
if {[info exists optA(-font)]} {
set theFont $optA(-font)
} else {
set theFont $defaultFont
}
set ascent [font metrics $theFont -ascent]
set lineSpace [font metrics $theFont -linespace]
if {[info exists optA(-text)]} {
set placeholder "\uFFFD"
set chdata [regsub -all "\[^${placeholder}\n\[:print:\]\]" $optA(-text) ${placeholder}]
if {[info exists optA(-width)]} {
# MICK O'DONNELL: if the text is wrapped in the wgt, we need
# to simulate linebreaks
#
# If the item has got -width != 0 then we must wrap it ourselves
# using newlines since the -text does not have extra newlines
# at these linebreaks.
set lines [split $chdata \n]
set newlines {}
foreach line $lines {
set lines2 [SplitWrappedLines $line $theFont $optA(-width)]
set newlines [concat $newlines $lines2]
}
set chdata [join $newlines \n]
if {!$argsA(-allownewlines) || \
([llength $newlines] > [llength $lines])} {
set nlines [expr {[regexp -all "\n" $chdata] + 1}]
}
} else {
if {!$argsA(-allownewlines)} {
set nlines [expr {[regexp -all "\n" $chdata] + 1}]
}
}
}
# Figure out the coords of the first baseline.
set anchor center
if {[info exists optA(-anchor)]} {
set anchor $optA(-anchor)
}
foreach {xbase ybase} \
[GetTextSVGCoords $coo $anchor $chdata $theFont $nlines] {}
set attr {}
if {[string length $idAttr] > 0} {
set attr [concat $attr $idAttr]
}
set attr [concat $attr [MakeAttrList \
$type $opts $argsA(-usestyleattribute)]]
set dy 0
if {$nlines > 1} {
# We cannot use the 'tspan' trick here,
# as 'textLength' in <tspan> is ignored by most renderers (as of 2023),
# even though SVG-1.1 defines it...
set subList {}
foreach line [split $chdata "\n"] {
set ybase [expr $ybase + $dy]
set subAttr [list "x" $xbase "y" $ybase]
lappend subAttr "textLength" [font measure $theFont $line]
lappend subList [MakeXMLList $elem \
-attrlist $subAttr -chdata $line]
set dy $lineSpace
}
lappend xmlLL [MakeXMLList "g" -attrlist $attr \
-subtags $subList]
} else {
lappend attr "textLength" [font measure $theFont $chdata]
set attr [concat [list "x" $xbase "y" $ybase] $attr]
lappend xmlLL [MakeXMLList $elem -attrlist $attr \
-chdata $chdata]
}
}
window {
# There is no svg for this; must be handled by application layer.
#puts "window: $cmd"
if {[string length $argsA(-windowitemhandler)]} {
set xmllist \
[uplevel #0 $argsA(-windowitemhandler) [list $cmd] $args]
if {[llength $xmllist]} {
lappend xmlLL $xmllist
}
}
}
}
return $xmlLL
}
# can2svg::CoordsToAttr --
#
# Makes a list of attributes corresponding to type and coords.
#
# Arguments:
#
#
# Results:
# a list of attributes.
proc can2svg::CoordsToAttr {type coo opts svgElementVar} {
upvar $svgElementVar elem
array set optA $opts
# Figure out if we've got a spline.
set haveSpline 0
if {[info exists optA(-smooth)] && ($optA(-smooth) != "0") && \
[info exists optA(-splinesteps)] && ($optA(-splinesteps) > 2)} {
set haveSpline 1
}
set attr {}
switch -- $type {
arc {
set elem "path"
set data [MakeArcPath $coo $opts]
set attr [list "d" $data]
}
bitmap - image {
array set __optA $opts
if {[info exists __optA(-image)]} {
set elem "image"
set attr [ImageCoordsToAttr $coo $opts]
}
}
line {
if {$haveSpline} {
set elem "path"
set data [ParseSplineToPath $type $coo]
set attr [list "d" $data]
} else {
set elem "polyline"
set attr [list "points" $coo]
}
}
oval {
# Assume SVG ellipse.
set elem "ellipse"
foreach {x y w h} [NormalizeRectCoords $coo] break
set attr [list \
"cx" [expr {$x + $w/2.0}] "cy" [expr {$y + $h/2.0}] \
"rx" [expr {$w/2.0}] "ry" [expr {$h/2.0}]]
}
polygon {
if {$haveSpline} {
set elem "path"
set data [ParseSplineToPath $type $coo]
set attr [list "d" $data]
} else {
set elem "polygon"
set attr [list "points" $coo]
}
}
rectangle {
set elem "rect"
foreach {x y w h} [NormalizeRectCoords $coo] break
set attr [list "x" $x "y" $y "width" $w "height" $h]
}
text {
set elem "text"
# ?
}
}
return $attr
}
# can2svg::MakeArcPath --
#
# Makes a path using A commands from an arc.
# Conversion from center to endpoint parameterization.
# From: http://www.w3.org/TR/2003/REC-SVG11-20030114
proc can2svg::MakeArcPath {coo opts} {
variable anglesToRadians
variable pi
# Canvas defaults.
array set optA {
-extent 90
-start 0
-style pieslice
}
array set optA $opts
# Extract center and radius from bounding box.
foreach {x1 y1 x2 y2} $coo break
set cx [expr {($x1 + $x2)/2.0}]
set cy [expr {($y1 + $y2)/2.0}]
set rx [expr {abs($x1 - $x2)/2.0}]
set ry [expr {abs($y1 - $y2)/2.0}]
set start [expr {$anglesToRadians * $optA(-start)}]
set extent [expr {$anglesToRadians * $optA(-extent)}]
# NOTE: direction of angles are opposite for Tk and SVG!
set theta1 [expr {-1*$start}]
set delta [expr {-1*$extent}]
set theta2 [expr {$theta1 + $delta}]
set phi 0.0
# F.6.4 Conversion from center to endpoint parameterization.
set x1 [expr {$cx + $rx * cos($theta1) * cos($phi) - \
$ry * sin($theta1) * sin($phi)}]
set y1 [expr {$cy + $rx * cos($theta1) * sin($phi) + \
$ry * sin($theta1) * cos($phi)}]
set x2 [expr {$cx + $rx * cos($theta2) * cos($phi) - \
$ry * sin($theta2) * sin($phi)}]
set y2 [expr {$cy + $rx * cos($theta2) * sin($phi) + \
$ry * sin($theta2) * cos($phi)}]
set fa [expr {abs($delta) > $pi ? 1 : 0}]
set fs [expr {$delta > 0.0 ? 1 : 0}]
set data [format "M %.1f %.1f A" $x1 $y1]
append data [format " %.1f %.1f %.1f %1d %1d %.1f %.1f" \
$rx $ry $phi $fa $fs $x2 $y2]
switch -- $optA(-style) {
arc {
# empty.
}
chord {
append data " Z"
}
pieslice {
append data [format " L %.1f %.1f Z" $cx $cy]
}
}
return $data
}
# can2svg::MakeArcPathNonA --
#
# Makes a path without any A commands from an arc.
proc can2svg::MakeArcPathNonA {coo opts} {
variable anglesToRadians
array set optA $opts
foreach {x1 y1 x2 y2} $coo break
set cx [expr {($x1 + $x2)/2.0}]
set cy [expr {($y1 + $y2)/2.0}]
set rx [expr {abs($x1 - $x2)/2.0}]
set ry [expr {abs($y1 - $y2)/2.0}]
set rmin [expr {$rx > $ry ? $ry : $rx}]
# This approximation gives a maximum half pixel error.
set deltaPhi [expr {2.0/sqrt($rmin)}]
set extent [expr {$anglesToRadians * $optA(-extent)}]
set start [expr {$anglesToRadians * $optA(-start)}]
set nsteps [expr {int(abs($extent)/$deltaPhi) + 2}]
set delta [expr {$extent/$nsteps}]
set data [format "M %.1f %.1f L" \
[expr {$cx + $rx*cos($start)}] [expr {$cy - $ry*sin($start)}]]
for {set i 0} {$i <= $nsteps} {incr i} {
set phi [expr {$start + $i * $delta}]
append data [format " %.1f %.1f" \
[expr {$cx + $rx*cos($phi)}] [expr {$cy - $ry*sin($phi)}]]
}
if {[info exists optA(-style)]} {
switch -- $optA(-style) {
chord {
append data " Z"
}
pieslice {
append data [format " %.1f %.1f Z" $cx $cy]
}
}
} else {
# Pieslice is the default.
append data [format " %.1f %.1f Z" $cx $cy]
}
return $data
}
# can2svg::MakeAttrList --
#
# Handles the use of style attributes or presenetation attributes.
proc can2svg::MakeAttrList {type opts usestyleattribute} {
if {$usestyleattribute} {
set attrList [list style [MakeStyleAttr $type $opts]]
} else {
set attrList [MakeStyleList $type $opts]
}
return $attrList
}
# can2svg::MakeStyleAttr --
#
# Produce the SVG style attribute from the canvas item options.
#
# Arguments:
# type tk canvas widget item type
# opts
#
# Results:
# The SVG style attribute as a a string.
proc can2svg::MakeStyleAttr {type opts} {
set style ""
foreach {key value} [MakeStyleList $type $opts] {
append style "${key}: ${value}; "
}
return [string trim $style]
}
proc can2svg::MakeStyleList {type opts args} {
array set argsA {
-setdefaults 1
}
array set argsA $args
# Defaults for everything except text.
if {$argsA(-setdefaults) && ![string equal $type "text"]} {
array set styleArr {fill none stroke black}
}
set fillCol black
foreach {key value} $opts {
switch -- $key {
-arrow {
set arrowValue $value
}
-arrowshape {
set arrowShape $value
}
-capstyle {
if {[string equal $value "projecting"]} {
set value "square"
}
if {![string equal $value "butt"]} {
set styleArr(stroke-linecap) $value
}
}
-dash {
set dashValue $value
}
-dashoffset {
if {$value != 0} {
set styleArr(stroke-dashoffset) $value
}
}
-extent {
# empty
}
-fill {
# Need to translate names to hex spec.
if {![regexp {#[0-9]+} $value]} {
set value [FormatColorName $value]
}
set fillCol $value
if {[string equal $type "line"]} {
set styleArr(stroke) [MapEmptyToNone $value]
} else {
set styleArr(fill) [MapEmptyToNone $value]
}
}
-font {
array set styleArr [MakeFontStyleList $value]
}
-joinstyle {
set styleArr(stroke-linejoin) $value
}
-outline {
set styleArr(stroke) [MapEmptyToNone $value]
}
-outlinestipple {
set outlineStippleValue $value
}
-start {
# empty
}
-stipple {
set stippleValue $value
}
-width {
set styleArr(stroke-width) $value
}
}
}
# If any arrow specify its marker def url key.
if {[info exists arrowValue]} {
if {[info exists arrowShape]} {
foreach {a b c} $arrowShape break
set arrowIdKey "arrowMarkerDef_${fillCol}_${a}_${b}_${c}"
set arrowIdKeyLast "arrowMarkerLastDef_${fillCol}_${a}_${b}_${c}"
} else {
set arrowIdKey "arrowMarkerDef_${fillCol}"
set arrowIdKeyLast $arrowIdKey
}
switch -- $arrowValue {
first {
set styleArr(marker-start) "url(#$arrowIdKey)"
}
last {
set styleArr(marker-end) "url(#$arrowIdKeyLast)"
}
both {
set styleArr(marker-start) "url(#$arrowIdKey)"
set styleArr(marker-end) "url(#$arrowIdKeyLast)"
}
}
}
if {[info exists stippleValue]} {
# Overwrite any existing.
set styleArr(fill) "url(#tile[string trimleft $stippleValue @])"
}
if {[info exists outlineStippleValue]} {
# Overwrite any existing.
set styleArr(stroke) "url(#tile[string trimleft $stippleValue @])"
}
# Transform dash value.
if {[info exists dashValue]} {
# Two different syntax here.
if {[regexp {[\.,\-_]} $dashValue]} {
# .=2 ,=4 -=6 space=4 times stroke width.
# A space enlarges the... space.
# Not foolproof!
regsub -all -- {[^ ]} $dashValue "& " dash
regsub -all -- " " $dash "12 " dash
regsub -all -- " " $dash "8 " dash
regsub -all -- " " $dash "4 " dash
regsub -all -- {\.} $dash "2 " dash
regsub -all -- {,} $dash "4 " dash
regsub -all -- {-} $dash "6 " dash
# Multiply with stroke width if > 1.
if {[info exists styleArr(stroke-width)] && \
($styleArr(stroke-width) > 1)} {
set width $styleArr(stroke-width)
set dashOrig $dash
set dash {}
foreach num $dashOrig {
lappend dash [expr {int($width * $num)}]
}
}
set styleArr(stroke-dasharray) [string trim $dash]
} else {
set dashValue [string trim $dashValue]
if {$dashValue ne ""} {
set styleArr(stroke-dasharray) $dashValue
}
}
}
if {[string equal $type "polygon"]} {
set styleArr(fill-rule) "evenodd"
}
return [array get styleArr]
}
proc can2svg::FormatColorName {value} {
if {[string length $value] == 0} {
return $value
}
switch -- $value {
black - white - red - green - blue {
set col $value
}
default {
# winfo rgb . white -> 65535 65535 65535
foreach rgb [winfo rgb . $value] {
lappend rgbx [expr {$rgb >> 8}]
}
set col [eval {format "#%02x%02x%02x"} $rgbx]
}
}
return $col
}
# can2svg::MakeFontStyleList --
#
# Takes a tk font description and returns a flat style array.
#
# Arguments:
# fontDesc a tk font description
#
# Results:
# flat style array
proc can2svg::MakeFontStyleList {fontDesc} {
# MICK Modify - break a named font into its component fields
set font [lindex $fontDesc 0]
if {[lsearch -exact [font names] $font] > -1} {
# This is a font name
set styleArr(font-family) [font config $font -family]
set fsize [font config $font -size]
if {$fsize > 0} {
# points
set funit pt
} else {
# pixels (actually user units)
set funit px
}
set styleArr(font-size) "[expr {abs($fsize)}]$funit"
if {[font config $font -slant] == "italic"} {
set styleArr(font-style) italic
}
if {[font config $font -weight] == "bold"} {
set styleArr(font-weight) bold
}
if {[font config $font -underline]} {
set styleArr(text-decoration) underline
}
if {[font config $font -overstrike]} {
set styleArr(text-decoration) overline
}
} else {
set styleArr(font-family) [lindex $fontDesc 0]
if {[llength $fontDesc] > 1} {
# Mick: added pt at end
set fsize [lindex $fontDesc 1]
if {$fsize > 0} {
# points
set funit pt
} else {
# pixels (actually user units)
set funit px
}
set styleArr(font-size) "[expr {abs($fsize)}]$funit"
}
if {[llength $fontDesc] > 2} {
set tkstyle [lindex $fontDesc 2]