view release on metacpan or search on metacpan
2.37 2008.03.02
- Added NS definitions for svg and xlink to enable xlink:href and svg: namespace usage in Firefox.
2.36 2007.09.16
- Fixed SVG::DOM POD
2.35 2007.09.14
- Fixed SVG::DOM POD
2.34 2005.05.17
- SVG::Element - Removed elsep entry for CDATA, cdata, cdata_noxmlesc fields to get rid of
artificial blank spaces that confuse some browsers
- SVG::DOM - added getRootNode method
- Added user-contributed DOM2 methods to SVG::DOM
2.33 2005.05.14
- Fixed the xlink reference behaviour which was wrong and
broke Mozilla native SVG. Fix submitted by Ian Hickson
2.32 2004.10.10
- Bufgix to repair broken test which causes failure of make test
examples/attributeManip.pl view on Meta::CPAN
use strict;
use warnings;
use SVG;
# Create an SVG object
# (c) 2003 Ronan Oger
my $svg = SVG->new(width=>200,height=>200);
$svg->title()->cdata('I am a title');
# Use explicit element constructor to generate a group element:
my $y = $svg->group(
id => 'group_y',
style => { stroke=>'red', fill=>'green' }
);
# Add some circles to the group
$y->circle(cx=>100, cy=>100, r=>50, id=>'circle_in_group_y_1');
examples/circle.pl view on Meta::CPAN
use warnings;
use SVG;
# create an SVG object with a size of 200x200 pixels
my $svg = SVG->new(
width => 200,
height => 200,
);
$svg->title()->cdata('I am a title');
# use explicit element constructor to generate a group element
my $y = $svg->group(
id => 'group_y',
style => {
stroke => 'red',
fill =>'green',
},
);
examples/image_sample.pl view on Meta::CPAN
#
#
use SVG;
use CGI ':new :header';
my $p = CGI->new;
$| = 1;
my $svg = SVG->new(width=>800,height=>400);
$svg->desc( id=>'root-desc')->cdata('hello this is a description');
$svg->title( id=>'root-title')->cdata('Dynamic SVG - Image usage within SVG using Perl');
#use another SVG component as an image inside our image
$svg->image(id=>'image_1',
-href=>'SVG_02_sample.pl',
x=>150,
y=>150,
width=>100,
height=>100);
examples/image_sample.pl view on Meta::CPAN
$svg->anchor('-href'=>"http://www.hackmare.com/",target=>'new_window')
->group(id=>'png_group')
->image(id=>'image_2',
-href=>'http://www.hackmare.com/icons/logo/hackmaresplash600_1.png',
x=>10,
y=>10,
width=>600,
height=>94,);
$svg->text(x=>20,y=>280)->cdata('EXPLANATION');
$svg->text(x=>20,y=>310)->cdata('One image is imported as a full SVG image');
$svg->text(x=>20,y=>325)->cdata('The second (hackmare) image is imported is an .png image');
$svg->text(x=>20,y=>340)->cdata('Notice that the hackmare image contains a url anchor');
$svg->text(x=>20,y=>355,style=>{fill=>'red'})->cdata('Actually, the link anchor contains a group which contains the image');
$svg->text(x=>200,y=>385,style=>{fill=>'red','fill-opacity'=>0.2})->cdata("This image was generated with perl using Ronan Oger's SVG module");
print $p->header('image/svg-xml');
print $svg->xmlify;
print "\n";
examples/inline_sample.pl view on Meta::CPAN
BEGIN {
push @INC , '../';
push @INC , '../SVG';
}
use SVG;
use strict;
my $svg = SVG->new(width=>800,height=>400,);
$svg->desc( id=>'root-desc')->cdata('hello this is a description');
$svg->title( id=>'root-title')->cdata('Dynamic SVG - Image usage within SVG using Perl');
#use another SVG component as an image inside our image
$svg->image(id=>'image_1',
-href=>'SVG_02_sample.pl',
x=>150,
y=>150,
width=>100,
height=>100);
examples/inline_sample.pl view on Meta::CPAN
$svg->anchor('-href'=>"http://www.hackmare.com/",target=>'new_window')
->group(id=>'png_group')
->image(id=>'image_2',
-href=>'http://www.hackmare.com/icons/logo/hackmaresplash600_1.png',
x=>10,
y=>10,
width=>600,
height=>94,);
$svg->text(x=>20,y=>280)->cdata('EXPLANATION');
$svg->text(x=>20,y=>310)->cdata('One image is imported as a full SVG image');
$svg->text(x=>20,y=>325)->cdata('The second (hackmare) image is imported is an .png image');
$svg->text(x=>20,y=>340)->cdata('Notice that the hackmare image contains a url anchor');
$svg->text(x=>20,y=>355,style=>{fill=>'red'})->cdata('Actually, the link anchor contains a group which contains the image');
$svg->text(x=>200,y=>385,style=>{fill=>'red','fill-opacity'=>0.2})->cdata("This image was generated with perl using Ronan Oger's SVG module");
print $svg->xmlify(namespace=>'abc',);
examples/inlinesvg.pl view on Meta::CPAN
transform=>'rotate('.(-2.5*5*rand()).')',
style=>&text_style);
my $w=$a2->text(id=>'t5',
x=>$width/2*rand(),
y=>$height-20+5*rand(),
transform=>'rotate('.(-2.5*5*rand()).')',
style=>&text_style);
$t->cdata('Text generated using the high-level "text" tag');
$t->cdata('Courtesy of RO IT Systems GmbH');
$v->cdata('Text generated using the low-level "tag" tag');
$w->cdata('But what about inline SVG? Yes, we do that too');
$w->cdata('All this with SVG.pm? Wow.');
print $p->header('image/svg-xml');
print $svg->render(-inline=>1);
exit;
#################
# Subroutine to round up the value of a number or of a text representation of number
#
examples/sun_text_sample.pl view on Meta::CPAN
}
use SVG;
use strict;
use CGI ':header';
my $p = CGI->new;
print $p->header(-type=>'image/svg+xml');
my $svg = SVG->new(width=>500,height=>500);
$svg->desc()->cdata('This example shows some more features of SVG Text');
$svg->title()->cdata('Sample 3: text');
$svg->comment(
'hello I am a dog. Actually, I am an SVG demo of the perl SVG.pm module',
'While the original static example was done by SUN, this is a 100% dynamic',
'sample. Case in point. Last time I looked, the SUN sample did not work on any',
'of my browser implementations.',
'=========================================================================',
'SVG Sample Pool : Text',
'This sample shows some powerful features of SVG Text elements ',
'among which are the "text" element; "tspan" element; "textPath" element;',
examples/sun_text_sample.pl view on Meta::CPAN
$defg_m->line(y1=>-15,x1=>0,y2=>15,x2=>0,style=>{'stroke'=>'currentColor'});
$svg->comment(
'=====================================================================',
'Simple text element, for the graphics title. ',
'This illustrates a very simple text element where text is centered ',
'about its anchor. ',
'=====================================================================',
'Draw simple text');
$svg->text(x=>200, y=>80, style=>
{'text-anchor'=>'middle', 'font-size'=>60, 'font-weight'=>800, 'font-family'=>'Verdana', 'font-style'=>'italic'})->cdata('hello, Sun.');
$svg->comment('Display marker for the anchor point');
$svg->use(-href=>"#marker", style=>"color:black", transform=>'translate(200, 80)');
$svg->comment(
'=====================================================================',
"The first part of the picture: 'SVG' following the upper curved line",
'defs / xlink:href in textPath is the way to achieve text on a path.',
'This illustrates: text, textPath and tspan',
'=====================================================================',
examples/sun_text_sample.pl view on Meta::CPAN
$textLayout1->comment('Draw the path on which text is laid out');
$textLayout1->use(-href=>"#Path1", style=>{stroke=>'yellow','stroke-width'=>40, 'fill'=>'none'});
$textLayout1->use(-href=>"#Path1", style=>{stroke=>'black','stroke-width'=>1, fill=>'none'});
$textLayout1->comment('Layout text on path');
$textLayout1->text(style=>{'font-family'=>'Verdana',
'font-size'=>80, 'font-weight'=>800,
fill=>'blue', 'text-anchor'=>'middle'});
my $textLayoutpath1 = $textLayout1->text(-type=>'path', -href=>"#Path1", startOffset=>"0");
$textLayoutpath1->text(-type=>'span', style=>"fill:black")->cdata('S');
$textLayoutpath1->text(-type=>'span', style=>{stroke=>'black',fill=>'white'})->cdata('V');
$textLayoutpath1->text(-type=>'span', style=>"fill:red")->cdata('G');
$svg->comment('textLayout1',
'=======================================================================',
"The second part of the picture: 'is' following the right vertical line ",
'This illustrates glyph layout capabilities, here top to bottom layout.',
'======================================================================='
);
$svg->defs()->path(id=>"Path2", d=>"M 100 0 l 0 150");
my $tl2 = $svg->group(id=>"textLayout2" ,transform=>"translate(200, 250)");
$tl2->use(-href=>"#Path2", style=>{stroke=>'red', 'stroke-width'=>40});
$tl2->comment("Here we change the writing-mode of the text element to 'tb' (for 'top to bottom')");
$tl2->text( x=>"100", y=>"75", style=>{'font-family'=>'Verdana', 'font-weight'=>800, 'font-size'=>50,fill=>'white', 'writing-mode'=>'tb', 'text-anchor'=>'middle'})->cdata('is');
$tl2->use( -href=>"#marker", style=>"color:black;", transform=>"translate(100, 75)" );
$svg->comment('======================================================================',
"Third part of the picture: 'very' following the bottom horizontal line",
'This illustrates one way of displaying text upside down. ',
'======================================================================',
'Define the path where the text is laid out');
examples/sun_text_sample.pl view on Meta::CPAN
$svg->defs()->path(id=>"Path3", d=>"M 100 150 l -200 0");
$svg->comment('Draw the path on which text is laid out');
my $tl3 = $svg->group( id=>"textLayout3", transform=>"translate(200, 250)");
$tl3->use(-href=>"#Path3", style=>"stroke:yellow; stroke-width:40");
$tl3->use( -href=>"#Path3", style=>{stroke=>'black','stroke-width'=>1});
$tl3->text(style=>{ 'font-family'=>'Verdana', 'font-size'=>40,
'font-weight'=>900, fill=>'black',
stroke=>'none', 'text-anchor'=>'middle'} );
$tl3->text(type=>'path', -href=>"#Path3", 'xml:space'=>"default")->cdata('very');
$svg->comment('textLayout3');
$svg->comment('========================================================================',
"The fourth part of the picture: 'cool' following the left vertical line",
'This further illustrates tspan, this time directly in a text element.',
examples/sun_text_sample.pl view on Meta::CPAN
my $tl4 = $svg->group(id=>"textLayout4", transform=>"translate(200, 250)");
$tl4->use( -href=>"#Path4", style=>"stroke:red; stroke-width:40");
my $tl4_t = $tl4->text( x=>"0", y=>"0",
style=>{'font-family'=>'Verdana', 'font-size'=>50,
'font-style'=>'italic',fill=>'white',
stroke=>'black', 'writing-mode'=>'lr',
'text-anchor'=>"middle"},
transform=>"translate(-100, 75) rotate(-90)");
$tl4_t->tspan(dy=>"0")->cdata('cool!');
$tl4_t->tspan(dy=>"-25",
style=>{'font-size'=>10, stroke=>'none', fill=>'black'})
->cdata('SVG');
$tl4_t->tspan(dy=>"0",style=>{'font-size'=>10, stroke=>'none', fill=>'black'})->cdata('SVG');
$tl4->use(transform=>"translate(-100, 75)", -href=>"#marker", style=>"color:black;");
$svg->comment(
'=============================================================================',
"Below are steps to produce the 'SVG' in the box, mainly using the text-anchor",
'to align the three glyphs ',
'This illustrates the various text anchors. ',
'=============================================================================');
my $tl5 = $svg->group( id=>"textLayout5", transform=>"translate(180, 290)", style=>"font-weight:800");
$tl5->use( -href=>"#marker", style=>{fill=>'black', stroke=>'black',transform=>'translate(30, 50)'});
$tl5->comment('Anchored to the end');
$tl5->text( x=>"30", y=>"50", style=>{'font-family'=>'Verdana','font-size'=>100,
stroke=>'black', fill=>'none', 'text-anchor'=>'end'})->cdata('S');
$tl5->comment('Anchored to the start');
$tl5->text( x=>"30", y=>"50",
style=>{'font-family'=>'Verdana', 'font-size'=>40,
'font-weight'=>700, stroke=>'black',fill=>'none',
'text-anchor'=>'start'})
->cdata('G');
$tl5->comment("When the orientation is 'top_bottom' using 'start' as the anchor makes",
'the glyph aligns to the upper line',);
$tl5->use(-href=>"#marker", style=>"color:red", transform=>"translate(48, -30)");
$tl5->comment('Anchored to the start, with top to bottom text layout');
$tl5->text( x=>"48", y=>"-30",
style=>{'font-family'=>'Verdana',
'font-size'=>50,stroke=>'red',
fill=>'none','writing-mode'=>'tb',
'text-anchor'=>'start'})
->cdata('V');
$svg->anchor(-href=>"http://roitsystems.com/")->text(x=>200, y=>160, style=>
{'text-anchor'=>'middle', 'font-size'=>30, 'font-weight'=>800, 'font-family'=>'Verdana', 'font-style'=>'italic', opacity=>0.3})->cdata('Use SVG.pm');
print $svg->xmlify;
__END__
<?xml version="1.0" encoding="iso-8859-1"?>
examples/svg.psgi view on Meta::CPAN
# run this example using plackup -r examples/svg.psgi
use SVG;
my $app = sub {
my $svg = SVG->new(
width => 200,
height => 200,
);
$svg->title()->cdata('I am a title');
# add a circle
$svg->circle(
cx => 100,
cy => 100,
r => 50,
id => 'circle_in_group_y',
style => {
fill => '#FF0000',
}
examples/svg_dom_sample.pl view on Meta::CPAN
}
}
my $s = SVG->new(width=>100,height=>50);
my $g1 = $s->group(id=>'group_1');
$g1->circle(width=>1,height=>1,id=>'test_id');
$g1->rect(id=>'id_2');
$g1->rect(id=>'id_3');
$g1->rect(id=>'id_4',x=>15,y=>150);
$g1->anchor(-xref=>'http://www.roitsystems.com/tutorial/',id=>'anchor_1')
->text(id=>'text_1',x=>15,y=>150,stroke=>'red')->cdata('Hello, World');
my $g2 = $s->group(id=>'group_2');
$g2->ellipse(id=>'id_5');
$g2->ellipse(id=>'id_6');
$g2->ellipse(id=>'id_7');
$s->ellipse(id=>'id_8');
$s->ellipse(id=>'id_9');
print "SVG::DOM Demonstration\n";
examples/svgtest2.pl view on Meta::CPAN
transform=>'rotate('.(-2.5*5*rand()).')',
style=>&text_style);
my $w=$a2->text(id=>'t5',
x=>$width/2*rand(),
y=>$height-20+5*rand(),
transform=>'rotate('.(-2.5*5*rand()).')',
style=>&text_style);
$t->cdata('Text generated using the high-level "text" tag');
$t->cdata('Courtesy of RO IT Systems GmbH');
$v->cdata('Text generated using the low-level "tag" tag');
$w->cdata('But what about inline SVG? Yes, we do that too');
$w->cdata('All this with SVG.pm? Wow.');
print $p->header('image/svg-xml');
print $svg->render
;
exit;
#################
# Subroutine to round up the value of a number or of a text representation of number
examples/yaph5.cgi view on Meta::CPAN
'fill'=>'rgb(0,0,0)',
'opacity'=>'0.5'};
$svg->text(x=>"318", y=>"333",
transform=>'matrix(1.58041 -0.293543 0.333969 1.3891 -396.141 -55.3847)', style=>{'font-family'=>'Arial Rounded MT Bold',
'font-size'=>100,'stroke-width'=>1,'stroke-opacity'=>1,
stroke=>'rgb(0,0,0)', 'fill-opacity'=>1,fill=>'rgb(0,0,0)',
opacity=>1,visibility=>'inherit'},
filter=>'url(#pointillist)')
->cdata('A');
$svg->polygon(points=>'33.6776,266.425 34.408,266.795 33.6684,267.165 33.8903,266.795',
'stroke-miterlimit'=>4, 'stroke-linejoin'=>'miter',
fill=>'rgb(0,0,0)');
$svg->polygon(points=>'75.8931,140.313 -18,268.028 77.0816,395.744 48.5571,268.028',
'stroke-linejoin'=>'miter',
fill=>'url(#red-yellow-red)',
filter=>'url(#Bumpy)');
examples/yaph5.cgi view on Meta::CPAN
'stroke-opacity'=>0.9,stroke=>'url(#custom-paint)',
'fill-opacity'=>0.8, fill=>'rgb(0,0,0)',opacity=>0.8};
$svg->polygon(points=>$points,style=>$style);
$svg->anchor(-href => "http://burks.brighton.ac.uk/burks/foldoc/49/60.htm",id=>'a_1');
my $ytg = $svg->group(id=>'yaph_text_group',style=>$font_style);
$ytg->text(x=>"441",y=>"302",style=>$font_style,
transform => 'matrix(0.767738 -0.769086 0.91578 0.644758 80.4565 534.986)'
)->cdata('Yet Another');
$ytg->text(x=>441,y=>302,style=>$font_style,
transform => 'matrix(0.774447 0.760459 0 0.924674 357.792 -428.792)',
)->cdata('PERL Hack');
my $j_style = {'stroke-miterlimit'=>4,
'stroke-width' =>10,
'stroke-opacity' =>1,
'stroke' =>'url(#red-dark-green)',
'fill-opacity' =>0.85,
'opacity' =>0.70,
'font-family'=>'Arial monospaced for SAP',
'font-size'=>250,
'stroke'=>'url(#custom-paint_1)',
'fill'=>'rgb(71,254,130)',};
my $j_trans = 'matrix(1.58041 0.293543 0.333969 1.3891 -396.141 -55.3847)';
$svg->text(id=>'big_J',x=>217,y=>270,style=>$j_style,transform=>$j_trans)->cdata('J');
$path = "M128 130 L202 130";
$style={'stroke-miterlimit'=>4,
'stroke-linejoin'=>'miter',
'stroke-linecap'=>'round',
'stroke-width'=>11,
'stroke-opacity'=>1,
'stroke'=>'url(#transparent-sky_1)',
'fill-opacity'=>1,
'fill'=>'rgb(0,0,0)',
examples/yaph5.cgi view on Meta::CPAN
$style = {'font-family'=>'Arial monospaced for SAP',
'font-size'=>32,
'stroke-width'=>1,
'stroke-opacity'=>1,
'stroke'=>'url(#custom-paint_1)',
'fill-opacity'=>1,
'fill'=>'rgb(74,214,130)',
'opacity'=>1};
$svg->text(x=>273,y=>437,style=>$style)->cdata($string);
print $svg->xmlify;
x1 => 0,
y1 => 10,
x2 => 10,
y2 => 0,
);
To draw multiple connected lines, use L<"polyline">.
=head2 text
$text = $svg->text(%attributes)->cdata();
$text_path = $svg->text(-type=>'path');
$text_span = $text_path->text(-type=>'span')->cdata('A');
$text_span = $text_path->text(-type=>'span')->cdata('B');
$text_span = $text_path->text(-type=>'span')->cdata('C');
Define the container for a text string to be drawn in the image.
B<Input:>
-type = path type (path | polyline | polygon)
-type = text element type (path | span | normal [default])
my $text1 = $svg->text(
id => 'l1',
x => 10,
y => 10
)->cdata('hello, world');
my $text2 = $svg->text(
id => 'l1',
x => 10,
y => 10,
-cdata => 'hello, world',
);
my $text = $svg->text(
id => 'tp',
x => 10,
y => 10,
-type => path,
)
->text(id=>'ts' -type=>'span')
->cdata('hello, world');
SEE ALSO:
L<"desc">, L<"cdata">.
=head2 title
$tag = $svg->title(%attributes)
Generate the title of the image.
my $tag = $svg->title(id=>'document-title')->cdata('This is the title');
=head2 desc
$tag = $svg->desc(%attributes)
Generate the description of the image.
my $tag = $svg->desc(id=>'document-desc')->cdata('This is a description');
=head2 comment
$tag = $svg->comment(@comments)
Generate the description of the image.
my $tag = $svg->comment('comment 1','comment 2','comment 3');
=head2 pi (Processing Instruction)
$tag = $svg->script(%attributes)
Generate a script container for dynamic (client-side) scripting using
ECMAscript, Javascript or other compatible scripting language.
my $tag = $svg->script(-type=>"text/ecmascript");
#or my $tag = $svg->script();
#note that type ecmascript is not Mozilla compliant
# populate the script tag with cdata
# be careful to manage the javascript line ends.
# Use qq{text} or q{text} as appropriate.
# make sure to use the CAPITAL CDATA to poulate the script.
$tag->CDATA(qq{
function d() {
//simple display function
for(cnt = 0; cnt < d.length; cnt++)
document.write(d[cnt]);//end for loop
document.write("<BR>");//write a line break
}
Style definitions apply to an object and all its children for all properties for
which the value of the property is not redefined by the child.
$tag = $SVG->style(%attributes)
Generate a style container for inline or xlink:href based styling instructions
my $tag = $SVG->style(type=>"text/css");
# Populate the style tag with cdata.
# Be careful to manage the line ends.
# Use qq{text}, where text is the script
$tag1->CDATA(qq{
rect fill:red;stroke:green;
circle fill:red;stroke:orange;
ellipse fill:none;stroke:yellow;
text fill:black;stroke:none;
});
$SVG->attrib $name, \%value
Remove an attribute:
$svg->attrib($name,undef);
B<Aliases:> attr attribute
Sets/replaces attributes for a tag.
=head2 cdata
$svg->cdata($text)
Sets cdata to $text. SVG.pm allows you to set cdata for any tag. If the tag is
meant to be an empty tag, SVG.pm will not complain, but the rendering agent will
fail. In the SVG DTD, cdata is generally only meant for adding text or script
content.
$svg->text(
style => {
'font' => 'Arial',
'font-size' => 20
})->cdata('SVG.pm is a perl module on CPAN!');
my $text = $svg->text( style => { 'font' => 'Arial', 'font-size' => 20 } );
$text->cdata('SVG.pm is a perl module on CPAN!');
B<Result:>
<text style="font: Arial; font-size: 20">SVG.pm is a perl module on CPAN!</text>
SEE ALSO:
L<"CDATA">, L<"desc">, L<"title">, L<"text">, L<"script">.
=head2 cdata_noxmlesc
$script = $svg->script();
$script->cdata_noxmlesc($text);
Generates cdata content for text and similar tags which do not get xml-escaped.
In othe words, does not parse the content and inserts the exact string into the cdata location.
=head2 CDATA
$script = $svg->script();
$script->CDATA($text);
Generates a <![CDATA[ ... ]]> tag with the contents of $text rendered exactly as supplied. SVG.pm allows you to set cdata for any tag. If the tag is
meant to be an empty tag, SVG.pm will not complain, but the rendering agent will
fail. In the SVG DTD, cdata is generally only meant for adding text or script
content.
my $text = qq{
var SVGDoc;
var groups = new Array();
var last_group;
/*****
*
* init
*
*****/
function init(e) {
SVGDoc = e.getTarget().getOwnerDocument();
append_group(1, 4, 6); // group 0
append_group(5, 4, 3); // group 1
append_group(2, 3); // group 2
}
]]E<gt>
SEE ALSO: L<"cdata">, L<"script">.
=head2 xmlescp and xmlescape
$string = $svg->xmlescp($string)
$string = $svg->xmlesc($string)
$string = $svg->xmlescape($string)
SVG module does not xml-escape characters that are incompatible with the XML specification. B<xmlescp> and B<xmlescape> provides this functionality. It is a helper function which generates an XML-escaped string for reserved characters such as ampersa...
The behaviour of xmlesc is to apply the following transformation to the input string $s:
lib/SVG/DOM.pm view on Meta::CPAN
my ( $self, $att, $val ) = @_;
$self->attrib( $att, $val );
}
#-----------------
# sub getCDATA / getCdata / getData
sub getCDATA {
my $self = shift;
if ( exists $self->{-cdata} ) {
return $self->{-cdata};
}
return;
}
*getCdata = \&getCDATA;
*getData = \&getCDATA;
# ----------------
# 2005-12-30 - Martin Owens, apply greater DOM specification (write)
# http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html
lib/SVG/DOM.pm view on Meta::CPAN
Return the string value attribute value for an attribute of name $name.
=head2 $ref = $obj->setAttributes({name1=>$value1,name2=>undef,name3=>$value3})
Set a set of attributes. If $value is undef, deletes the attribute.
=head2 $value = $obj->setAttribute($name,$value);
Set attribute $name to $value. If $value is undef, deletes the attribute.
=head2 $cdata = $obj->getCDATA()
Return the canonical data (i.e. textual content) of the current node.
Alias: getCdata(), getData()
=head2 $boolean = $obj->isAncestor($element)
Returns 1 if the current node is an ancestor of the specified element, otherwise 0.
=head2 $boolean = $obj->isDescendant($element)
lib/SVG/Element.pm view on Meta::CPAN
#rest of the xml
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$xml .= $k->xmlify($ns);
}
}
return $xml;
}
my $is_cdataish
= defined $self->{-cdata}
|| defined $self->{-CDATA}
|| defined $self->{-cdata_noxmlesc};
if ( defined $self->{-childs} || $is_cdataish ) {
$xml .= $self->{-docref}->{-elsep}
unless ( $self->{-inline} && $self->{-name} );
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$xml .= xmltagopen_ln( $self->{-name}, $ns, %attrs );
$self->{-docref}->{-level}++;
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$xml .= $k->xmlify($ns);
}
}
if ( defined $self->{-cdata} ) {
$xml .= $self->xmlescp( $self->{-cdata} );
}
if ( defined $self->{-CDATA} ) {
$xml .= '<![CDATA[' . $self->{-CDATA} . ']]>';
}
if ( defined $self->{-cdata_noxmlesc} ) {
$xml .= $self->{-cdata_noxmlesc};
}
#return without writing the tag out if it the document tag
$self->{-docref}->{-level}--;
unless ($is_cdataish) {
$xml .= $self->{-docref}->{-elsep};
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
}
$xml .= xmltagclose_ln( $self->{-name}, $ns );
}
else {
$xml .= $self->{-docref}->{-elsep};
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$xml .= xmltag_ln( $self->{-name}, $ns, %attrs );
}
lib/SVG/Element.pm view on Meta::CPAN
return $code;
}
if ( defined $self->{-childs} ) {
$code .= $self->{-docref}->{-elsep};
$code .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$code
.= $self->{-name} . '('
. ( join ', ', ( map {"$_=>'$attrs{$_}'"} sort keys %attrs ) )
. ')';
if ( $self->{-cdata} ) {
$code .= "->cdata($self->{-cdata})";
}
elsif ( $self->{-CDATA} ) {
$code .= "->CDATA($self->{-CDATA})";
}
elsif ( $self->{-cdata_noxmlesc} ) {
$code .= "->cdata_noxmlesc($self->{-cdata_noxmlesc})";
}
$self->{-docref}->{-level}++;
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$code .= $k->perlify();
}
}
$self->{-docref}->{-level}--;
}
lib/SVG/Element.pm view on Meta::CPAN
# 3 defined arguments - set
$self->{$name} = $val;
}
return $self;
}
*attr = \&attrib;
*attribute = \&attrib;
sub cdata {
my ( $self, @txt ) = @_;
$self->{-cdata} = join( ' ', @txt );
return ($self);
}
sub CDATA {
my ( $self, @txt ) = @_;
$self->{-CDATA} = join( '\n', @txt );
return ($self);
}
sub cdata_noxmlesc {
my ( $self, @txt ) = @_;
$self->{-cdata_noxmlesc} = join( '\n', @txt );
return ($self);
}
sub filter {
my ( $self, %attrs ) = @_;
return $self->tag( 'filter', %attrs );
}
sub fe {
my ( $self, %attrs ) = @_;
t/04-inline.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 12;
use SVG qw(-inline 1);
# test: -inline
my $svg1 = SVG->new();
isa_ok $svg1, 'SVG';
isa_ok $svg1->text->cdata("An inline document"), 'SVG::Element';
my $xml1a = $svg1->render();
unlike $xml1a, qr/DOCTYPE/, "1 render inline document";
unlike $xml1a, qr/^<\?xml .*?\?>\s*/sm;
like $xml1a,
qr{<svg height="100%" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">};
like $xml1a, qr{<text>An inline document</text>};
my $xml1b = $svg1->render( -inline => 0 );
like $xml1b, qr/DOCTYPE/, "2 render not inline";
t/06-doctype.t view on Meta::CPAN
use warnings;
use Test::More tests => 8;
use SVG ();
# test: -sysid -pubid -docroot
my $svg = SVG->new();
$svg->text->cdata("Document type declaration test");
my $xml = $svg->dtddecl();
ok( $xml, "dtd reclaration" );
like( $xml, qr/DOCTYPE svg /, "doctype found" );
like( $xml, qr{ PUBLIC "-//W3C//DTD SVG 1.0//EN" }, "PUBLIC found" );
like( $xml, qr{ "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">},
"SVG 1.0 TR" );
$svg = SVG->new( -docroot => "mysvg" );
t/07-extension.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 1;
use SVG;
my $svg = SVG->new( -extension => q{<!ENTITY % myentity "myvalue">} );
$svg->group->text->cdata("Extensions");
my $xml = $svg->render;
like(
$xml,
qr/[\n<!ENTITY % myentity "myvalue">\n]>/,
"ENTITY myentity myvalue"
);
t/08-looknfeel.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 2;
use SVG ( -indent => '*', -elsep => '|', -nocredits => 1 );
# test: -indent -elsep -nocredits
my $svg = SVG->new();
$svg->group->text->cdata("Look and Feel");
my $xml = $svg->render();
like( $xml, qr/\n|\|/, "correct element separation" );
like( $xml, qr/\*\*/, "correct indent string" );
t/09-script.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 8;
use SVG;
my $svg = SVG->new;
my $tag = $svg->script( type => "text/ecmascript" );
# populate the script tag with cdata be careful to manage the
# javascript line ends. qâtextâ or qq§text§ where text is the
# script works well for this.
$tag->CDATA(
qq|
function d(){
//simple display function
for(cnt = 0; cnt < d.length; cnt++)
document.write(d[cnt]);//end for loop
document.write("<hr>");//write a line break
t/15-parentage.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 18;
use SVG;
# test: getFirstChild, getLastChild, getParent, getChildren
my $svg = SVG->new;
my $parent = $svg->group();
my $child1 = $parent->text->cdata("I am the first child");
my $child2 = $parent->text->cdata("I am the second child");
my $child3 = $parent->text->cdata("I am the third child");
is( $parent->getFirstChild(), $child1, "getFirstChild" );
is( $child1->getParent(), $parent, "getParent 1" );
is( $parent->getLastChild(), $child3, "getLastChild" );
is( $child2->getParent(), $parent, "getParent 2" );
ok( $parent->hasChildren(), "hasChildren" );
my @children = $parent->getChildren();
is( scalar(@children), 3, "correct number of children" );
is( $children[0], $child1, "getChildren 1" );
t/16-siblings.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 4;
use SVG;
# test: getFirstChild, getLastChild, getParent, getChildren
my $svg = SVG->new;
my $parent = $svg->group();
my $child1 = $parent->text->cdata("I am the first child");
my $child2 = $parent->text->cdata("I am the second child");
ok( $child1->hasSiblings(), "hasSiblings" );
is( $child1->getNextSibling(), $child2, "getNextSibling" );
is( $child2->getPreviousSibling(), $child1, "getPreviousSibling" );
$child2->insertSiblingAfter($child1);
is( $child2->getNextSibling(), $child1, "insertSiblingAfter" );
t/17-tagtypes.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 4;
use SVG;
# test: getElementTypes, getElementsByType, getElementType, getElementsByType, getElementTypes
my $svg = SVG->new;
my $parent = $svg->group();
my $child1 = $parent->text->cdata("I am the first child");
my $child2 = $parent->text->cdata("I am the second child");
is( $child1->getElementType(), "text", "getElementType" );
is( scalar( @{ $svg->getElementsByType("g") } ),
1, "getElementsByType test 1" );
is( scalar( @{ $svg->getElementsByType("text") } ),
2, "getElementsByType test 2" );
is( scalar( @{ $svg->getElementTypes() } ), 3, "getElementTypes" );
t/18-filter.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 2;
use SVG;
# test: fe
my $svg = SVG->new;
my $parent = $svg->group();
my $child1 = $parent->text->cdata("I am the first child");
my $child2 = $parent->text->cdata("I am the second child");
my $fe = $svg->fe(
-type => 'diffuselighting', # required - element name omitting 'fe'
id => 'filter_1',
style => {
'font' => [qw(Arial Helvetica sans)],
'font-size' => 10,
'fill' => 'red',
},
transform => 'rotate(-45)'
);
t/23-xmlescape.t view on Meta::CPAN
use Test::More tests => 14;
use SVG ( -printerror => 0, -raiseerror => 0 );
# test: special characters
my $bad_chars = '><!&';
my $esc_chars = '><!&';
my $svg = SVG->new;
ok( my $out1 = $svg->text()->cdata_noxmlesc( $svg->xmlescp($bad_chars) ),
"Testing toxic characters to xmlescp" );
like( $out1->xmlify(), qr/$esc_chars/, 'Toxic chars are escaped' );
ok(
my $out2 = $svg->text()->cdata($bad_chars),
"Testing toxic characters to cdata"
);
like( $out2->xmlify(), qr/$esc_chars/, 'Toxic chars are escaped' );
$bad_chars = "Line one\nLine two";
$esc_chars = "Line one\nLine two";
ok( my $out3 = $svg->text()->cdata($bad_chars),
'Testing new line characters' );
like( $out3->xmlify(), qr/$esc_chars/, 'New lines are allowed' );
$bad_chars = "Col1\tcol2";
$esc_chars = "Col1\tcol2";
ok( my $out4 = $svg->text()->cdata($bad_chars), 'Testing tab characters' );
like( $out4->xmlify(), qr/$esc_chars/, 'Tabs are allowed' );
$bad_chars = '`backticks`';
$esc_chars = '`backticks`';
ok( my $out5 = $svg->text()->cdata($bad_chars), 'Testing backticks' );
like( $out5->xmlify(), qr/$esc_chars/, 'Backticks are ok' );
$bad_chars
= "Remove these: \x01, \x02, \x03, \x04, \x05, \x06, \x07, \x08, \x0b, \x1f";
$esc_chars = 'Remove these: , , , , , , , , , ';
ok( my $out6 = $svg->text()->cdata($bad_chars),
'Testing restricted characters' );
like( $out6->xmlify(), qr/$esc_chars/, 'Restricted characters removed' );
$bad_chars = '[@hkb:536:8bp]: hkb-536';
$esc_chars = '\[@hkb:536:8bp\]: hkb-536';
ok( my $out7 = $svg->text()->cdata($bad_chars), 'More weird input' );
like( $out7->xmlify(), qr/$esc_chars/ );