-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVG.pm
2609 lines (2027 loc) · 79 KB
/
SVG.pm
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
package GD::SVG;
use strict;
use Carp 'croak','carp','confess';
use SVG;
#use warnings;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
require Exporter;
$VERSION = '0.34';
# $Id: SVG.pm,v 1.16 2009/05/10 14:07:17 todd Exp $
# Conditional support for side-by-side raster generation. Off for now.
# Methods that support this are commented out multiple times (ie ######)
use constant USEGD => 0;
if (USEGD) {
eval "use GD";
}
# A global debug flag which can be overriden by new()
use constant DEBUG => 0;
@ISA = qw(Exporter);
%EXPORT_TAGS = ('cmp' => [qw(GD_CMP_IMAGE
GD_CMP_NUM_COLORS
GD_CMP_COLOR
GD_CMP_SIZE_X
GD_CMP_SIZE_Y
GD_CMP_TRANSPARENT
GD_CMP_BACKGROUND
GD_CMP_INTERLACE
GD_CMP_TRUECOLOR
)
]
);
@EXPORT = qw(
gdStyled
gdBrushed
gdTransparent
gdTinyFont
gdSmallFont
gdMediumBoldFont
gdLargeFont
gdGiantFont
gdDashSize
gdMaxColors
gdStyledBrushed
gdTiled
gdChord
gdEdged
gdNoFill
gdArc
gdPie
);
# Not yet implemented
#@EXPORT_OK = qw (
# GD_CMP_IMAGE
# GD_CMP_NUM_COLORS
# GD_CMP_COLOR
# GD_CMP_SIZE_X
# GD_CMP_SIZE_Y
# GD_CMP_TRANSPARENT
# GD_CMP_BACKGROUND
# GD_CMP_INTERLACE
# GD_CMP_TRUECOLOR
# );
# GD does not allow dynamic creation of fonts. These default values
# are approximate sizes for the various fonts based on an extensive
# afternoon of cross-comparison ;)
use constant DEFAULT_FONT => 'Helvetica';
use constant TINY_HEIGHT => 8;
use constant TINY_WIDTH => 5;
use constant TINY_WEIGHT => 'normal';
use constant SMALL_HEIGHT => 11; # originally 12
use constant SMALL_WIDTH => 6;
use constant SMALL_WEIGHT => 'normal';
use constant MEDIUM_BOLD_HEIGHT => 13;
use constant MEDIUM_BOLD_WIDTH => 7;
use constant MEDIUM_BOLD_WEIGHT => 'bold';
use constant LARGE_HEIGHT => 16;
use constant LARGE_WIDTH => 8;
use constant LARGE_WEIGHT => 'normal';
use constant GIANT_HEIGHT => 15;
use constant GIANT_WIDTH => 8;
use constant GIANT_WEIGHT => 'bold';
# TEXT_KLUDGE controls the number of pixels to bump text on the
# Y-axis in order to more closely match GD output.
use constant TEXT_KLUDGE => '2';
#########################
# END CONSTANTS - No user serviceable options below this point
#########################
# Trap GD methods that are not yet implemented in SVG.pm
sub AUTOLOAD {
my $self = shift;
warn "GD method $AUTOLOAD is not implemented in GD::SVG" if ref $self && $self->{debug} > 0;
}
##################################################
# Exported methods that belong in Main namespace #
##################################################
# In GD, the gdStyled method allows one to draw with a styled line
# Here we will simply return the format of the line along with a flag
# so that appropriate subroutines can deal with it.
# Similarly, the gdTransparent method lets users introduce gaps in
# lines. I'll handle it similarly to gdStyled...
# This might just be as simple as setting the color to the background color.
# (This, of course, will not work for styled lines).
sub gdStyled { return 'gdStyled'; }
sub gdBrushed { return 'gdBrushed'; }
sub gdTransparent { return 'gdTransparent'; }
sub gdStyledBrush { _error('gdStyledBrush'); }
sub gdTiled { _error('gdTiled'); }
sub gdDashSize { _error('gdDashSize'); }
sub gdMaxColors { _error('gdMaxColors'); }
# Bitwise operations for filledArcs
sub gdArc { return 0; }
sub gdPie { return 0; }
sub gdChord { return 1; }
sub gdEdged { return 4; }
sub gdNoFill { return 2; }
sub gdAntiAliased { _error('gdAntiAliased'); }
sub setAntiAliased { shift->_error('setAntiAliased'); }
sub setAntiAliasedDontBlend { shift->_error('setAntiAliasedDontBlend'); }
################################
# Font Factories and Utilities
################################
sub gdTinyFont {
my $this = bless {},'GD::SVG::Font';
$this->{font} = DEFAULT_FONT;
$this->{height} = TINY_HEIGHT;
$this->{width} = TINY_WIDTH;
$this->{weight} = TINY_WEIGHT;
return $this;
}
sub gdSmallFont {
my $this = bless {},'GD::SVG::Font';
$this->{font} = DEFAULT_FONT;
$this->{height} = SMALL_HEIGHT;
$this->{width} = SMALL_WIDTH;
$this->{weight} = SMALL_WEIGHT;
return $this;
}
sub gdMediumBoldFont {
my $this = bless {},'GD::SVG::Font';
$this->{font} = DEFAULT_FONT;
$this->{height} = MEDIUM_BOLD_HEIGHT;
$this->{width} = MEDIUM_BOLD_WIDTH;
$this->{weight} = MEDIUM_BOLD_WEIGHT;
return $this;
}
sub gdLargeFont {
my $this = bless {},'GD::SVG::Font';
$this->{font} = DEFAULT_FONT;
$this->{height} = LARGE_HEIGHT;
$this->{width} = LARGE_WIDTH;
$this->{weight} = LARGE_WEIGHT;
return $this;
}
sub gdGiantFont {
my $this = bless {},'GD::SVG::Font';
$this->{font} = DEFAULT_FONT;
$this->{height} = GIANT_HEIGHT;
$this->{width} = GIANT_WIDTH;
$this->{weight} = GIANT_WEIGHT;
return $this;
}
# Don't break stuff!
# The can() method is not supported in GD::SVG
# sub can { return 0; }
package GD::SVG::Image;
use Carp 'croak','carp','confess';
# There must be a better way to trap these errors
sub _error {
my ($self,$method) = @_;
warn "GD method $method is not implemented in GD::SVG" if ($self->{debug} > 0);
}
#########################
# GD Constants
#########################
# Kludge - use precalculated values of cos(theta) and sin(theta)
# so that I do no have to examine quadrants
my @cosT = (qw/1024 1023 1023 1022 1021 1020 1018 1016 1014 1011 1008
1005 1001 997 993 989 984 979 973 968 962 955 949 942 935 928 920 912
904 895 886 877 868 858 848 838 828 817 806 795 784 772 760 748 736
724 711 698 685 671 658 644 630 616 601 587 572 557 542 527 512 496
480 464 448 432 416 400 383 366 350 333 316 299 282 265 247 230 212
195 177 160 142 124 107 89 71 53 35 17 0 -17 -35 -53 -71 -89 -107 -124
-142 -160 -177 -195 -212 -230 -247 -265 -282 -299 -316 -333 -350 -366
-383 -400 -416 -432 -448 -464 -480 -496 -512 -527 -542 -557 -572 -587
-601 -616 -630 -644 -658 -671 -685 -698 -711 -724 -736 -748 -760 -772
-784 -795 -806 -817 -828 -838 -848 -858 -868 -877 -886 -895 -904 -912
-920 -928 -935 -942 -949 -955 -962 -968 -973 -979 -984 -989 -993 -997
-1001 -1005 -1008 -1011 -1014 -1016 -1018 -1020 -1021 -1022 -1023
-1023 -1024 -1023 -1023 -1022 -1021 -1020 -1018 -1016 -1014 -1011
-1008 -1005 -1001 -997 -993 -989 -984 -979 -973 -968 -962 -955 -949
-942 -935 -928 -920 -912 -904 -895 -886 -877 -868 -858 -848 -838 -828
-817 -806 -795 -784 -772 -760 -748 -736 -724 -711 -698 -685 -671 -658
-644 -630 -616 -601 -587 -572 -557 -542 -527 -512 -496 -480 -464 -448
-432 -416 -400 -383 -366 -350 -333 -316 -299 -282 -265 -247 -230 -212
-195 -177 -160 -142 -124 -107 -89 -71 -53 -35 -17 0 17 35 53 71 89 107
124 142 160 177 195 212 230 247 265 282 299 316 333 350 366 383 400
416 432 448 464 480 496 512 527 542 557 572 587 601 616 630 644 658
671 685 698 711 724 736 748 760 772 784 795 806 817 828 838 848 858
868 877 886 895 904 912 920 928 935 942 949 955 962 968 973 979 984
989 993 997 1001 1005 1008 1011 1014 1016 1018 1020 1021 1022 1023
1023/);
my @sinT = (qw/0 17 35 53 71 89 107 124 142 160 177 195 212 230 247
265 282 299 316 333 350 366 383 400 416 432 448 464 480 496 512 527
542 557 572 587 601 616 630 644 658 671 685 698 711 724 736 748 760
772 784 795 806 817 828 838 848 858 868 877 886 895 904 912 920 928
935 942 949 955 962 968 973 979 984 989 993 997 1001 1005 1008 1011
1014 1016 1018 1020 1021 1022 1023 1023 1024 1023 1023 1022 1021 1020
1018 1016 1014 1011 1008 1005 1001 997 993 989 984 979 973 968 962 955
949 942 935 928 920 912 904 895 886 877 868 858 848 838 828 817 806
795 784 772 760 748 736 724 711 698 685 671 658 644 630 616 601 587
572 557 542 527 512 496 480 464 448 432 416 400 383 366 350 333 316
299 282 265 247 230 212 195 177 160 142 124 107 89 71 53 35 17 0 -17
-35 -53 -71 -89 -107 -124 -142 -160 -177 -195 -212 -230 -247 -265 -282
-299 -316 -333 -350 -366 -383 -400 -416 -432 -448 -464 -480 -496 -512
-527 -542 -557 -572 -587 -601 -616 -630 -644 -658 -671 -685 -698 -711
-724 -736 -748 -760 -772 -784 -795 -806 -817 -828 -838 -848 -858 -868
-877 -886 -895 -904 -912 -920 -928 -935 -942 -949 -955 -962 -968 -973
-979 -984 -989 -993 -997 -1001 -1005 -1008 -1011 -1014 -1016 -1018
-1020 -1021 -1022 -1023 -1023 -1024 -1023 -1023 -1022 -1021 -1020
-1018 -1016 -1014 -1011 -1008 -1005 -1001 -997 -993 -989 -984 -979
-973 -968 -962 -955 -949 -942 -935 -928 -920 -912 -904 -895 -886 -877
-868 -858 -848 -838 -828 -817 -806 -795 -784 -772 -760 -748 -736 -724
-711 -698 -685 -671 -658 -644 -630 -616 -601 -587 -572 -557 -542 -527
-512 -496 -480 -464 -448 -432 -416 -400 -383 -366 -350 -333 -316 -299
-282 -265 -247 -230 -212 -195 -177 -160 -142 -124 -107 -89 -71 -53 -35
-17 /);
#############################
# GD::SVG::Image methods
#############################
sub new {
my ($self,$width,$height,$debug) = @_;
my $this = bless {},$self;
my $img = SVG->new(width=>$width,height=>$height);
$this->{img} = [$img];
$this->{width} = $width;
$this->{height} = $height;
# Let's create an internal representation of the image in GD
# so that I can easily use some of GD's methods
###GD###$this->{gd} = GD::Image->new($width,$height);
# Let's just assume that we always want the foreground color to be
# black This, for the most part, works for Bio::Graphics. This
# certainly needs to be fixed...
$this->{foreground} = $this->colorAllocate(0,0,0);
$this->{debug} = ($debug) ? $debug : GD::SVG::DEBUG;
return $this;
}
sub img {
my $this = shift;
return $this->{img}[0];
}
sub currentGroup {
my $this = shift;
$this->{currentGroup} = shift if @_;
return $this->{currentGroup} || $this->{img}[-1];
return $this->{img}[-1];
}
sub closeAllGroups {
my $this = shift;
while (@{$this->{img}}>1) {
pop @{$this->{img}};
}
}
#############################
# Image Data Output Methods #
#############################
sub svg {
my $self = shift;
$self->closeAllGroups;
my $img = $self->img;
$img->xmlify(-pubid => "-//W3C//DTD SVG 1.0//EN",
-inline => 1);
}
###GD###sub png {
###GD### my ($self,$compression) = @_;
###GD### return $self->{gd}->png($compression);
###GD###}
###GD###sub jpeg {
###GD### my ($self,$quality) = @_;
###GD### return $self->{gd}->jpeg($quality);
###GD###}
#############################
# Color management routines #
#############################
# As with GD, colorAllocate returns integers...
# This could easily rely on GD itself to generate the indices
sub colorAllocate {
my ($self,$r,$g,$b,$alpha) = @_;
$r ||= 0;
$g ||= 0;
$b ||= 0;
$alpha ||= 0;
###GD###my $newindex = $self->{gd}->colorAllocate($r,$g,$b);
# Cannot use the numberof keys to generate index
# colorDeallocate removes keys.
# Instead use the colors_added array.
my $new_index = (defined $self->{colors_added}) ? scalar @{$self->{colors_added}} : 0;
$self->{colors}->{$new_index} = [$r,$g,$b,$alpha];
# Keep a list of colors in the order that they are added
# This is used as a kludge for setBrush
push (@{$self->{colors_added}},$new_index);
return $new_index;
}
sub colorAllocateAlpha {
my $self = shift;
###GD###$self->{gd}->colorAllocateAlpha($r,$g,$b,$alpha);
$self->colorAllocate(@_);
}
sub colorDeallocate {
my ($self,$index) = @_;
my $colors = %{$self->{colors}};
delete $colors->{$index};
###GD###$self->{gd}->colorDeallocate($index);
}
# workaround for bad GD
sub colorClosest {
my ($self,@c) = @_;
###GD###my $index = $self->{gd}->colorClosest(@c);
# Let's just return the color for now.
# Terrible kludge.
my $index = $self->colorAllocate(@c);
return $index;
# my ($self,$gd,@c) = @_;
# return $self->{closestcache}{"@c"} if exists $self->{closestcache}{"@c"};
# return $self->{closestcache}{"@c"} = $gd->colorClosest(@c) if $GD::VERSION < 2.04;
# my ($value,$index);
# for (keys %COLORS) {
# my ($r,$g,$b) = @{$COLORS{$_}};
# my $dist = ($r-$c[0])**2 + ($g-$c[1])**2 + ($b-$c[2])**2;
# ($value,$index) = ($dist,$_) if !defined($value) || $dist < $value;
# }
# return $self->{closestcache}{"@c"} = $self->{translations}{$index};
}
sub colorClosestHWB { shift->_error('colorClosestHWB'); }
sub colorExact {
my ($self,$r,$g,$b) = @_;
###GD###my $index = $self->{gd}->colorExact($r,$g,$b);
# Let's just allocate the color instead of looking it up
my $index = $self->colorAllocate($r,$g,$b);
if ($index) {
return $index;
} else {
return ('-1');
}
}
sub colorResolve {
my ($self,$r,$g,$b) = @_;
###GD###my $index = $self->{gd}->colorResolve($r,$g,$b);
my $index = $self->colorAllocate($r,$g,$b);
return $index;
}
sub colorsTotal {
my $self = shift;
###GD###return $self->{gd}->colorsTotal;
return scalar keys %{$self->{colors}};
}
sub getPixel {
my ($self,$x,$y) = @_;
# Internal GD - probably unnecessary in this context...
# Will contstruct appropriate return value later
###GD### $self->{gd}->getPixel($x,$y);
# I don't have any cogent way to fetch the value of an asigned pixel
# without calculating all positions and loading into memory.
# For these purposes, I could maybe just look it up... From a hash
# table or something - Keep track of all assigned pixels and their
# color. Ugh. Compute intensive.
return (1);
}
# Given the color index, return its rgb triplet
sub rgb {
my ($self,$index) = @_;
my ($r,$g,$b) = @{$self->{colors}->{$index}};
return ($r,$g,$b);
}
sub transparent { shift->_error('transparent'); }
#######################
# Special Colors
#######################
# Kludgy preliminary support for gdBrushed This is based on
# Bio::Graphics implementation of set_pen which in essence just
# controls line color and thickness... We will assume that the last
# color added is intended to be the foreground color.
sub setBrush {
my ($self,$pen) = @_;
###GD###$self->{gd}->setBrush($pen);
my ($width,$height) = $pen->getBounds();
my $last_color = $pen->{colors_added}->[-1];
my ($r,$g,$b) = $self->rgb($last_color);
$self->{gdBrushed}->{color} = $self->colorAllocate($r,$g,$b);
$self->{gdBrushed}->{thickness} = $width;
}
# There is no direct translation of gdStyled. In gd, this is used to
# set the style for the line using the settings of the current brush.
# Drawing with the new style is then used by passing the gdStyled as a
# color.
sub setStyle {
my ($self,@colors) = @_;
###GD###$self->{gd}->setStyle(@colors);
$self->{gdStyled}->{color} = [ @colors ];
return;
}
# Lines in GD are 1 pixel in diameter by default.
# setThickness allows line thickness to be changed.
# This should be retained until it's changed again
# Each method should check the thickness of the line...
sub setThickness {
my ($self,$thickness) = @_;
###GD### $self->{gd}->setThickness($thickness);
$self->{line_thickness} = $thickness;
# WRONG!
# $self->{prev_line_thickness} = (!defined $self->{prev_line_thickness}) ? $thickness : undef;
}
########################
# Grouping subroutines #
########################
sub startGroup {
my $this = shift;
my $id = shift;
my $style = shift;
my @args;
push @args,(id => $id) if defined $id;
push @args,(style => $style) if defined $style;
my $group = $this->currentGroup->group(@args);
push @{$this->{img}},$group;
return $group;
}
sub endGroup {
my $this = shift;
my $group = shift;
if ($group) {
my @imgs = grep {$_ ne $group} @{$this->{img}};
$this->{img} = \@imgs;
}
elsif (@{$this->{img}}>1) {
pop @{$this->{img}};
}
delete $this->{currentGroup};
}
sub newGroup {
my $this = shift;
my $group = $this->startGroup(@_);
eval "require GD::Group" unless GD::Group->can('new');
return GD::Group->new($this,$group);
}
#######################
# Drawing subroutines #
#######################
sub setPixel {
my ($self,$x1,$y1,$color_index) = @_;
###GD### $self->{gd}->setPixel($x1,$y1,$color_index);
my ($img,$id,$thickness,$dasharray) = $self->_prep($x1,$y1);
my $color = $self->_get_color($color_index);
my $result =
$img->circle(cx=>$x1,cy=>$y1,r=>'0.03',
id=>$id,
style=>{
'stroke'=>$color,
'fill' =>$color,
'fill-opacity'=>'1.0'
}
);
return $result;
}
sub line {
my ($self,$x1,$y1,$x2,$y2,$color_index) = @_;
# Are we trying to draw with a styled line (ie gdStyled, gdBrushed?)
# If so, we need to deconstruct the values for line thickness,
# foreground color, and dash spacing
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->line($x1,$y1,$x2,$y2,$fg);
} else {
###GD### $self->{gd}->line($x1,$y1,$x2,$y2,$color_index);
my ($img,$id) = $self->_prep($x1,$y1);
my $style = $self->_build_style($id,$color_index,$color_index);
# Suggested patch by Jettero to fix lines
# that don't go to the ends of their length.
# This could possibly be relocated to _build_style
# but I'm unsure of the ramifications on other features.
$style->{'stroke-linecap'} = 'square';
my $result = $img->line(x1=>$x1,y1=>$y1,
x2=>$x2,y2=>$y2,
id=>$id,
style => $style,
);
$self->_reset();
return $result;
}
}
sub dashedLine { shift->_error('dashedLine'); }
# The fill parameter is used internally as a simplification...
sub rectangle {
my ($self,$x1,$y1,$x2,$y2,$color_index,$fill) = @_;
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->rectangle($x1,$y1,$x2,$y2,$fg,$fill);
} else {
###GD###$self->{gd}->rectangle($x1,$y1,$x2,$y2,$color_index);
my ($img,$id) = $self->_prep($x1,$y1);
my $style = $self->_build_style($id,$color_index,$fill);
# flip coordinates if they are "backwards"
($x1,$x2) = ($x2,$x1) if $x1 > $x2;
($y1,$y2) = ($y2,$y1) if $y1 > $y2;
my @args = (
x =>$x1,
y =>$y1,
width =>$x2-$x1,
height =>$y2-$y1,
id =>$id,
style => $style,
);
my $result = $img->rectangle(@args);
$self->_reset();
return $result;
}
}
# This should just call the rectangle method passing it a flag.
# I will need to fix the glyph that bypasses this option...
sub filledRectangle {
my ($self,$x1,$y1,$x2,$y2,$color) = @_;
# Call the rectangle method passing the fill color
$self->rectangle($x1,$y1,$x2,$y2,$color,$color);
}
sub polygon {
my ($self,$poly,$color,$fill) = @_;
$self->_polygon($poly,$color,$fill,1);
}
sub polyline {
my ($self,$poly,$color,$fill) = @_;
$self->_polygon($poly,$color,$fill,0);
}
sub polydraw {
my $self = shift; # the GD::Image
my $p = shift; # the GD::Polyline or GD::Polygon
my $c = shift; # the color
return $self->polyline($p, $c) if $p->isa('GD::Polyline');
return $self->polygon($p, $c);
}
sub _polygon {
my ($self,$poly,$color_index,$fill,$close) = @_;
my $shape = $close ? 'polygon' : 'polyline';
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->$shape($poly,$fg);
} else {
###GD###$self->{gd}->polygon($poly,$color);
# Create seperate x and y arrays of vertices
my (@xpoints,@ypoints);
if ($poly->can('_fetch_vertices')) {
@xpoints = $poly->_fetch_vertices('x');
@ypoints = $poly->_fetch_vertices('y');
} else {
my @points = $poly->vertices;
@xpoints = map { $_->[0] } @points;
@ypoints = map { $_->[1] } @points;
}
my ($img,$id) = $self->_prep($xpoints[0],$ypoints[0]);
my $points = $img->get_path(
x=>\@xpoints, y=>\@ypoints,
-type=>$shape,
);
my $style = $self->_build_style($id,$color_index,$fill);
my $result =
$img->$shape(
%$points,
id=>$id,
style => $style,
);
$self->_reset();
return $result;
}
}
# Passing the stroke doesn't really work as expected...
sub filledPolygon {
my ($self,$poly,$color) = @_;
my $result = $self->polygon($poly,$color,$color);
return $result;
}
sub ellipse {
my ($self,$x1,$y1,$width,$height,$color_index,$fill) = @_;
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->ellipse($x1,$y1,$width,$height,$fg);
} else {
###GD### $self->{gd}->ellipse($x1,$y1,$width,$height,$color_index);
my ($img,$id) = $self->_prep($x1,$y1);
# GD uses width and height - SVG uses radii...
$width = $width / 2;
$height = $height / 2;
my $style = $self->_build_style($id,$color_index,$fill);
my $result =
$img->ellipse(
cx=>$x1, cy=>$y1,
rx=>$width, ry=>$height,
id=>$id,
style => $style,
);
$self->_reset();
return $result;
}
}
sub filledEllipse {
my ($self,$x1,$y1,$width,$height,$color) = @_;
my $result = $self->ellipse($x1,$y1,$width,$height,$color,$color);
return $result;
}
# GD uses the arc() and filledArc() methods in two capacities
# 1. to create closed ellipses, where start and end are 0 and 360
# 2. to create honest-to-god open arcs
# The arc method is no longer being used to draw filledArcs.
# All the fill-specific code within is no deprecated.
sub arc {
my ($self,$cx,$cy,$width,$height,$start,$end,$color_index,$fill) = @_;
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->arc($cx,$cy,$width,$height,$start,$end,$fg);
} else {
###GD### $self->{gd}->arc($x,$y,$width,$height,$start,$end,$color);
# Are we just trying to draw a closed arc (an ellipse)?
my $result;
if ($start == 0 && $end == 360 || $end == 360 && $start == 0) {
$result = $self->ellipse($cx,$cy,$width,$height,$color_index,$fill);
} else {
my ($img,$id) = $self->_prep($cy,$cx);
# Taking a stab at drawing elliptical arcs
my ($start,$end,$large,$sweep,$a,$b) = _calculate_arc_params($start,$end,$width,$height);
my ($startx,$starty) = _calculate_point_coords($cx,$cy,$width,$height,$start);
my ($endx,$endy) = _calculate_point_coords($cx,$cy,$width,$height,$end);
# M = move to (origin of the curve)
# my $rotation = abs $start - $end;
my $style = $self->_build_style($id,$color_index,$fill);
$result =
$img->path('d'=>"M$startx,$starty " .
"A$a,$b 0 $large,$sweep $endx,$endy",
style => $style,
);
}
$self->_reset();
return $result;
}
}
# Return the x and y positions of start and stop of arcs.
sub _calculate_point_coords {
my ($cx,$cy,$width,$height,$angle) = @_;
my $x = ( $cosT[$angle % 360] * $width) / (2 * 1024) + $cx;
my $y = ( $sinT[$angle % 360] * $height) / (2 * 1024) + $cy;
return ($x,$y);
}
sub _calculate_arc_params {
my ($start,$end,$width,$height) = @_;
# GD uses diameters, SVG uses radii
my $a = $width / 2;
my $b = $height / 2;
while ($start < 0 ) { $start += 360; }
while ($end < 0 ) { $end += 360; }
while ($end < $start ) { $end += 360; }
my $large = (abs $start - $end > 180) ? 1 : 0;
# my $sweep = ($start > $end) ? 0 : 1; # directionality of the arc, + CW, - CCW
my $sweep = 1; # Always CW with GD
return ($start,$end,$large,$sweep,$a,$b);
}
sub filledArc {
my ($self,$cx,$cy,$width,$height,$start,$end,$color_index,$fill_style) = @_;
if ($color_index eq 'gdStyled' || $color_index eq 'gdBrushed') {
my $fg = $self->_distill_gdSpecial($color_index);
$self->filledArc($cx,$cy,$width,$height,$start,$end,$fg);
} else {
###GD### $self->{gd}->arc($x,$y,$width,$height,$start,$end,$color_index);
my $result;
# distill the special colors, if provided...
my $fill_color;
# Set it to gdArc, the default value to avoid undef errors in comparisons
$fill_style ||= 0;
if ($fill_style == 2 || $fill_style == 4 || $fill_style == 6) {
$fill_color = 'none';
} else {
$fill_color = $self->_get_color($color_index);
}
# Are we just trying to draw a closed filled arc (an ellipse)?
if (($start == 0 && $end == 360) || ($start == 360 && $end == 0)) {
$result = $self->ellipse($cx,$cy,$width,$height,$color_index,$fill_color);
}
# are we trying to draw a pie?
elsif ($end - $start > 180 && ($fill_style == 0 || $fill_style == 4)) {
$self->filledArc($cx,$cy,$width,$height,$start,$start+180,$color_index,$fill_style);
# $self->filledArc($cx,$cy,$width,$height,$start+180,$end,$color_index,$fill_style);
$result = $self->filledArc($cx,$cy,$width,$height,$start+180,$end,$color_index,$fill_style);
}
else {
my ($img,$id) = $self->_prep($cy,$cx);
my ($start,$end,$large,$sweep,$a,$b) = _calculate_arc_params($start,$end,$width,$height);
my ($startx,$starty) = _calculate_point_coords($cx,$cy,$width,$height,$start);
my ($endx,$endy) = _calculate_point_coords($cx,$cy,$width,$height,$end);
# Evaluate the various fill styles
# gdEdged connects the center to the start and end
if ($fill_style == 4 || $fill_style == 6) {
$self->line($cx,$cy,$startx,$starty,$color_index);
$self->line($cx,$cy,$endx,$endy,$color_index);
}
# gdNoFill outlines portions of the arc
# noFill or gdArc|gdNoFill
if ($fill_style == 2 || $fill_style == 6) {
$result = $self->arc($cx,$cy,$width,$height,$start,$end,$color_index);
return $result;
}
# gdChord|gdNofFill
if ($fill_style == 3) {
$result = $self->line($startx,$starty,$endx,$endy,$color_index);
return $result;
}
# Create the actual filled portion of the arc
# This is the default behavior for gdArc and if no style is passed.
if ($fill_style == 0 || $fill_style == 4) {
# M = move to (origin of the curve)
# my $rotation = abs $start - $end;
my $style = $self->_build_style($id,$color_index,$fill_color);
$result =
$img->path('d'=>"M$startx,$starty " .
"A$a,$b 0 $large,$sweep $endx,$endy",
style => $style,
);
}
# If we are filling, draw a filled triangle to complete.
# This is also the same as using gdChord by itself
my $poly = GD::SVG::Polygon->new();
$poly->addPt($cx,$cy);
$poly->addPt($startx,$starty);
$poly->addPt($endx,$endy);
$self->filledPolygon($poly,$color_index);
}
$self->_reset();
return $result;
}
}
# Flood fill that stops at first pixel of a different color.
sub fill { shift->_error('fill'); }
sub fillToBorder { shift->_error('fillToBorder'); }
##################################################
# Image Copying Methods
##################################################
# Taking a stab at implementing the copy() methods
# Should be relatively easy to implement clone() from this
sub copy {
my $self = shift;
my ($source,$dstx,$dsty,$srcx,$srcy,$width,$height) = @_;
# special case -- if we have been asked to copy a
# GD::Image into us, then we embed an image with the
# data:url
if ($source->isa('GD::Image') || $source->isa('GD::Simple')) {
return $self->_copy_image(@_);
}
my $topx = $srcx;
my $topy = $srcy;
my $bottomx = $srcx + $width; # arithmetic right here?
my $bottomy = $srcy + $height;
# Fetch all elements of the source image
my @elements = $source->img->getElements;
foreach my $element (reverse @elements) {
my $att = $element->getAttributes();
# Points|rectangles|text, circles|ellipses, lines
my $x = $att->{x} || $att->{cx} || $att->{x1};
my $y = $att->{y} || $att->{cy} || $att->{y1};
# Use the first point for polygons
unless ($x && $y) {
my @points = split(/\s/,$att->{points});
if (@points) {
($x,$y) = split(',',$points[0]);
}
}
# Paths
unless ($x && $y) {
my @d = split(/\s/,$att->{d});
if (@d) {
($x,$y) = split(',',$d[0]);
$x =~ s/^M//; # Remove the style directive
}
}
# Are the starting coords within the bounds of the desired rectangle?
# We will simplistically assume that the entire glyph fits inside
# the rectangle which may not be true.
if (($x >= $topx && $y >= $topy) &&
($x <= $bottomx && $y <= $bottomy)) {
my $type = $element->getType;
# warn "$type $x $y $bottomx $bottomy $topx $topy";
# Transform the coordinates as necessary,
# calculating the offsets relative to the
# original bounding rectangle in the source image
# Text or rectangles
if ($type eq 'text' || $type eq 'rect') {
my ($newx,$newy) = _transform_coords($topx,$topy,$x,$y,$dstx,$dsty);
$element->setAttribute('x',$newx);
$element->setAttribute('y',$newy);
# Circles or ellipses
} elsif ($type eq 'circle' || $type eq 'ellipse') {
my ($newx,$newy) = _transform_coords($topx,$topy,$x,$y,$dstx,$dsty);
$element->setAttribute('cx',$newx);
$element->setAttribute('cy',$newy);
# Lines
} elsif ($type eq 'line') {
my ($newx1,$newy1) = _transform_coords($topx,$topy,$x,$y,$dstx,$dsty);
my ($newx2,$newy2) = _transform_coords($topx,$topy,$att->{x2},$element->{y2},$dstx,$dsty);
$element->setAttribute('x1',$newx1);
$element->setAttribute('y1',$newy1);
$element->setAttribute('x2',$newx2);
$element->setAttribute('y2',$newy2);
# Polygons
} elsif ($type eq 'polygon') {
my @points = split(/\s/,$att->{points});
my @transformed;
foreach (@points) {
($x,$y) = split(',',$_);
my ($newx,$newy) = _transform_coords($topx,$topy,$x,$y,$dstx,$dsty);
push (@transformed,"$newx,$newy");
}
my $transformed = join(" ",@transformed);
$element->setAttribute('points',$transformed);
# Paths
} elsif ($type eq 'path') {
}
# Create new elements for the destination image
# via the generic SVG::Element::tag method
my %attributes = $element->getAttributes;
$self->img->tag($type,%attributes);
}
}
}
# Used internally by the copy method
# Transform coordinates of a given point with reference
# to a bounding rectangle
sub _transform_coords {
my ($refx,$refy,$x,$y,$dstx,$dsty) = @_;
my $xoffset = $x - $refx;
my $yoffset = $y - $refy;
my $newx = $dstx + $xoffset;
my $newy = $dsty + $yoffset;
return ($newx,$newy);
}
sub _copy_image {
my $self = shift;
my ($source,$dstx,$dsty,$srcx,$srcy,$width,$height) = @_;
eval "use MIME::Base64; 1"
or croak "The MIME::Base64 module is required to copy a GD::Image into a GD::SVG: $@";
my $subimage = GD::Image->new($width,$height); # will be loaded
$subimage->copy($source->isa('GD::Simple') ? $source->gd : $source,
0,0,
$srcx,$srcy,
$width,$height);
my $data = encode_base64($subimage->png);
my ($img,$id) = $self->_prep($dstx,$dsty);
my $result =
$img->image('x' => $dstx,
'y' => $dsty,
width => $width,
height => $height,
id => $id,
'xlink:href' => "data:image/png;base64,$data");
$self->_reset;
return $result;
}
##################################################
# Image Transformation Methods
##################################################
sub copyRotate90 {
return shift->_copyRotate(90);
}
sub copyRotate180 {
return shift->_copyRotate(180);
}
sub copyRotate270 {
return shift->_copyRotate(270);
}
sub copyRotate {