lib/LaTeXML/Post/SVG.pm untested

filter: Statementstmt 0.0% 0 / 411 Branchbran 0.0% 0 / 160 Conditioncond 0.0% 0 / 105 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 805 CC 81 SCAR 42.2
CC
81
Cov
0%
convertText
272.0
boxContentPos
110.0
ovalPath
90.0
CRAP 67.8
1 # /=====================================================================\ #
2 # | LaTeXML:SVG | #
3 # | | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Post::SVG;
13 use strict;
14 use warnings;
15 use LaTeXML::Common::XML;
16 use LaTeXML::Common::Model;
17 use LaTeXML::Util::Transform;
18 use LaTeXML::Util::Geometry;
19 use LaTeXML::Util::Image;
20 use LaTeXML::Post;
21 use base qw(LaTeXML::Post::Processor);
22  
23 my $NSURI = "http://dlmf.nist.gov/LaTeXML"; # CONSTANT
24 my $svgURI = 'http://www.w3.org/2000/svg'; # CONSTANT
25 my $NR = '[\-\+\d\.e]+'; # CONSTANT
26  
27 ####################################
28 ## main function
29 ####################################
30 # We need to find potential nodes to convert,
31 # but don't want those already containing proper SVG.
32 # However, we may not have svg as a registered namespace, so...
33 sub toProcess {
34 my ($self, $doc) = @_;
35 return $doc->findnodes("//ltx:picture[child::*[not(local-name()='svg' and namespace-uri()='$svgURI')]]"); }
36  
37 sub process {
38 my ($self, $doc, @svg) = @_;
39 local $::IDCOUNTER = 0;
40 if (!$$self{model}) {
41 $$self{model} = LaTeXML::Common::Model->new();
42 $$self{model}->setRelaxNGSchema("LaTeXML");
43 $$self{model}->loadSchema(); }
44 $doc->addNamespace($svgURI, 'svg');
45 map { $self->ProcessSVG($_) } @svg;
46 $doc->adjust_latexml_doctype('SVG'); # Add SVG if LaTeXML dtd.
47 return $doc; }
48  
49 sub getQName {
50 my ($node) = @_;
51 return $LaTeXML::Post::DOCUMENT->getQName($node); }
52  
53 sub copy_valid_attributes {
54 my ($self, $to, $from) = @_;
55 my $qname = getQName($to);
56 foreach my $attr ($from->attributes) {
57 my $key = $attr->getName;
58 if ($$self{model}->canHaveAttribute($qname, $key)) {
59 $from->removeAttribute($key) if $key eq 'xml:id';
60 $to->setAttribute($key, $from->getAttribute($key)); } }
61 return; }
62  
63 sub copy_position {
64 my ($self, $to, $from) = @_;
65 my $x = $from->getAttribute('x');
66 my $y = $from->getAttribute('y');
67 $to->setAttribute(cx => $x) if defined $x;
68 $to->setAttribute(cy => $y) if defined $y;
69 return; }
70  
71 sub to_px {
72 my ($pt) = @_;
73 return ($pt =~ s/pt$// ? $pt * $LaTeXML::Util::Image::DPI / 72.27 : $pt); }
74  
75 ####################################
76 ## fixes an svg node
77 ####################################
78  
79 sub ProcessSVG {
80 my ($self, $node) = @_;
81 # holds information about current font
82 local @::FONTSTACK = ({ fill => 'black' });
83 # if during processing some definitions are required, they are stored here
84 # at end of processing, required definitions are inserted into the tree
85 local %::DEFS = ();
86 my $newSVG = $node->parentNode->addNewChild($svgURI, 'svg');
87 $newSVG->setAttribute(version => '1.1');
88 my $newNode = convertNode($self, $newSVG, $node);
89 if (%::DEFS) {
90 my $defnode = $newSVG->addNewChild($svgURI, 'defs');
91 foreach my $key (sort keys %::DEFS) {
92 $defnode->appendChild($::DEFS{$key});
93 }
94 }
95 $self->copy_valid_attributes($newSVG, $node);
96 makeViewBox($newSVG);
97 simplifyGroups($newSVG);
98 $node->replaceNode($newSVG);
99 return; }
100  
101 sub makeViewBox {
102 my ($node) = @_;
103 my ($w, $h) = get_attr($node, qw(width height));
104 #### $node->setAttribute(viewBox => "0 0 $w $h");
105 # Does origin-x,origin-y need to be reflected in the initial transform?
106 $node->setAttribute(width => to_px($node->getAttribute('width')));
107 $node->setAttribute(height => to_px($node->getAttribute('height')));
108 $node->setAttribute(overflow => 'visible') if (($node->getAttribute('clip') || '') ne 'true');
109 $node->removeAttribute('clip');
110 return; }
111  
112 sub simplifyGroups {
113 my ($node) = @_;
114 map { simplifyGroups($_) } element_nodes($node);
115 if ((getQName($node) || '') eq 'ltx:g') {
116 my ($parent, @sons) = ($node->parentNode, $node->childNodes);
117 if (scalar(@sons) == 0) {
118 $parent->removeChild($node);
119 } elsif (scalar(@sons) == 1 && isElementNode($sons[0])
120 && ($sons[0]->namespaceURI eq $svgURI)) {
121 my $son = $sons[0]; my @attr = valid_attributes($node);
122 if ($#attr == -1) {
123 $parent->insertBefore($son, $node);
124 $parent->removeChild($node);
125 } elsif ($#attr == 0 && $attr[0]->getName eq 'transform') {
126 mergeTransform($son, $attr[0]->getValue);
127 $parent->insertBefore($son, $node);
128 $parent->removeChild($node);
129 }
130 }
131 }
132 return; }
133  
134 #======================================================================
135 # Converting specific tags.
136 #======================================================================
137 my %converters = ( # CONSTANT
138 'ltx:picture' => \&convertPicture, 'ltx:path' => \&convertPath,
139 'ltx:g' => \&convertG, 'ltx:text' => \&convertText,
140 'ltx:polygon' => \&convertPolygon, 'ltx:line' => \&convertLine,
141 'ltx:rect' => \&convertRect, 'ltx:bezier' => \&convertBezier,
142 'ltx:circle' => \&convertCircle,
143 'ltx:ellipse' => \&convertEllipse, 'ltx:wedge' => \&convertWedge,
144 'ltx:arc' => \&convertArc, 'ltx:dots' => \&convertDots);
145  
146 sub convertNode {
147 my ($self, $parent, $node) = @_;
148 my $tag = getQName($node);
149 if (!$tag) {
150 $parent->appendChild($node); }
151 elsif (my $converter = $converters{$tag}) {
152 &$converter($self, $parent, $node); }
153 else {
154 # Node is random LaTeXML element, and so will need an svg:foreignObject wrapper.
155 # Moreover, svg will want to know the size of the foreign thing.
156 # Hopefully, this will have been recorded on a containing ltx:g, using innerwidth/innerheight.
157 my $oldparent = $node->parentNode;
158 my $width = $node->getAttribute('width') # Use node's own width, if any
159 || $node->getAttribute('imagewidth') # or if an image
160 || $oldparent->getAttribute('innerwidth') # else hopefully from containing ltx:g
161 || $oldparent->getAttribute('width');
162 my $height = $node->getAttribute('height')
163 || $node->getAttribute('imageheight')
164 || $oldparent->getAttribute('innerheight')
165 || $oldparent->getAttribute('height');
166 my $depth = $node->getAttribute('depth')
167 || $oldparent->getAttribute('innerdepth')
168 || $oldparent->getAttribute('depth');
169 $width = "1pt" unless defined $width; # Required but must be non-zero
170 $height = "1pt" unless defined $height;
171 $depth = "0pt" unless defined $depth;
172 my $g = $parent->addNewChild($svgURI, 'g');
173 # my $y = to_px($height);
174 my $y = to_px($height) + to_px($depth);
175 $g->setAttribute(transform => "translate(0,$y) scale(1, -1)");
176 my $new = $g->addNewChild($svgURI, 'foreignObject');
177 $new->setAttribute(width => to_px($width)) if defined $width;
178 $new->setAttribute(height => to_px($height)) if defined $height;
179 $new->setAttribute(overflow => 'visible');
180 $new->appendChild($node); }
181 return; }
182  
183 sub convertPath {
184 my ($self, $parent, $node) = @_;
185 my $newNode = $parent->addNewChild($svgURI, 'path');
186 $self->copy_valid_attributes($newNode, $node);
187 map { convertNode($self, $newNode, $_) } element_nodes($node);
188 return $newNode; }
189  
190 # I think we need to shift the origin to the bottom before we mirror the y scale!
191 sub convertPicture {
192 my ($self, $parent, $node) = @_;
193 my $h = to_px($node->getAttribute('height') || '0');
194 my $gNode = $parent->addNewChild($svgURI, 'g');
195 $gNode->setAttribute(transform => "translate(0,$h) scale(1,-1)");
196 map { convertNode($self, $gNode, $_) } element_nodes($node);
197 return $gNode; }
198  
199 sub convertG {
200 my ($self, $parent, $node) = @_;
201 my ($xoff, $yoff) = boxContentPos($node);
202 my $newNode = $parent->addNewChild($svgURI, 'g');
203 if ((($node->getAttribute('framed') || '') eq 'true')
204 && (($node->getAttribute('fillframe') || '') eq 'true')) {
205 my $bgName = getFillFrame($node->getAttribute('fill') || 'white');
206 $newNode->setAttribute(filter => "url(#$bgName)"); }
207 $self->copy_valid_attributes($newNode, $node);
208 map { convertNode($self, $newNode, $_) } element_nodes($node);
209 return $newNode; }
210  
211 sub convertText {
212 my ($self, $parent, $node) = @_;
213  
214 my $oldparent = $node->parentNode;
215 my $p = ((getQName($oldparent) || '') eq 'ltx:g' ? $oldparent->getAttribute('pos') || '' : 'bl');
216 my $newNode = $parent->addNewChild($svgURI, 'text');
217 my $x = $node->getAttribute('x') || 0;
218 my $y = $node->getAttribute('y') || 0;
219 $newNode->setAttribute(x => $x);
220 $newNode->setAttribute(y => $y);
221  
222 ## if (my $text = text_in_node($node)) { $newNode->appendText($text); }
223 mergeTransform($node, 'scale(1, -1)');
224 $self->copy_valid_attributes($newNode, $node);
225 # Translate the font info.
226 push(@::FONTSTACK, {});
227 if (my $fontsize = $node->getAttribute('fontsize')) {
228 $::FONTSTACK[0]{'font-size'} = $fontsize; }
229 if (my $font = $node->getAttribute('font')) {
230 my $type = 'fill';
231 if ($font =~ /italic/) {
232 $type = 'font-style'; }
233 elsif ($font =~ /slanted/) {
234 $type = 'font-style'; $font = 'oblique'; }
235 elsif ($font =~ /bold/) {
236 $type = 'font-weight'; }
237 elsif ($font =~ /medium/) {
238 $type = 'font-weight'; $font = 'normal'; }
239 elsif ($font =~ /smallcaps/) {
240 $type = 'font-variant'; $font = 'small-caps'; }
241 elsif ($font =~ /upright/) {
242 $type = 'font-variant'; $font = 'normal'; }
243 elsif ($font =~ /tiny|footnote|small|normal|large|Large|LARGE|huge|Huge/) {
244 $type = 'font-size'; $font = ''; }
245 elsif ($font =~ /serif|sansserif|typewriter|caligraphic|fraktur|script/) {
246 $type = 'font-family'; $font = ''; }
247 $::FONTSTACK[0]{$type} = $font if $font;
248 }
249 my %Font = %{ $::FONTSTACK[0] };
250 foreach my $attr (sort keys %Font) {
251 next if $newNode->hasAttribute($attr);
252 $newNode->setAttribute($attr => $Font{$attr}) if $Font{$attr}; }
253  
254 my @children = $node->childNodes;
255 foreach my $child (@children) {
256 if (isTextNode($child)) {
257 $newNode->appendText($child->data); }
258 else {
259 convertNode($self, $newNode, $child); } }
260 pop(@::FONTSTACK);
261 return $newNode; }
262  
263 sub convertPolygon {
264 my ($self, $parent, $node) = @_;
265 my $newNode = $parent->addNewChild($svgURI, 'path');
266 $newNode->setAttribute(d => arcPoints($node) . ' z');
267 $self->copy_valid_attributes($newNode, $node);
268 map { convertNode($self, $newNode, $_) } element_nodes($node);
269 return $newNode; }
270  
271 sub convertLine {
272 my ($self, $parent, $node) = @_;
273 my $newNode = $parent->addNewChild($svgURI, 'path');
274 $newNode->setAttribute(d => arcPoints($node));
275 $self->copy_valid_attributes($newNode, $node);
276 setArrows($newNode, $node, $node->getAttribute('stroke'));
277 map { convertNode($self, $newNode, $_) } element_nodes($node);
278 return $newNode; }
279  
280 sub convertRect {
281 my ($self, $parent, $node) = @_;
282 my $newNode;
283 if (my $part = $node->getAttribute('part')) {
284 $newNode = $parent->addNewChild($svgURI, 'path');
285 $newNode->setAttribute(d => ovalPath($part, get_attr($node, qw(x y width height rx))));
286 $self->copy_valid_attributes($newNode, $node); }
287 else {
288 $newNode = $parent->addNewChild($svgURI, 'rect');
289 $self->copy_valid_attributes($newNode, $node); }
290 map { convertNode($self, $newNode, $_) } element_nodes($node);
291 return $newNode; }
292  
293 sub convertBezier {
294 my ($self, $parent, $node) = @_;
295 my @p = explodeCoord($node->getAttribute('points') || '');
296 my $n = ($#p + 1) / 2; my $x0 = shift(@p); my $y0 = shift(@p);
297 my %cmd = (4 => 'C', 3 => 'Q');
298 my $newNode = $parent->addNewChild($svgURI, 'path');
299 $newNode->setAttribute(d => "M $x0,$y0 " . ($cmd{$n} || 'T') . ' ' . coordList(@p));
300 $self->copy_valid_attributes($newNode, $node);
301 setArrows($newNode, $node, $newNode->getAttribute('stroke'));
302 $newNode->setAttribute('stroke-dasharray' => '2') if $node->hasAttribute('displayedpoints');
303 map { convertNode($self, $newNode, $_) } element_nodes($node);
304 return $newNode; }
305  
306 # NOTE: I messed this one up!
307 sub convertVbox {
308 my ($self, $parent, $node) = @_;
309 my $text = '';
310 foreach my $child (element_nodes($node)) {
311 my $cn = getQName($child);
312 if ((($cn || '') =~ /^ltx:(text|hbox)$/) && (my $t = text_in_node($node))) { $text .= $t . "\n"; } }
313 my $dummynode = new_node($NSURI, 'text');
314 $dummynode->appendText($text);
315 my $newNode = $parent->addNewChild($svgURI, 'text');
316 $self->copy_valid_attributes($newNode, $node);
317 convertText($newNode, $dummynode);
318 return $newNode; }
319  
320 sub convertCircle {
321 my ($self, $parent, $node) = @_;
322 my $newNode = $parent->addNewChild($svgURI, 'circle');
323 $self->copy_valid_attributes($newNode, $node);
324 $self->copy_position($newNode, $node);
325 map { convertNode($self, $newNode, $_) } element_nodes($node);
326 return $newNode; }
327  
328 #?
329 sub convertDots {
330 my ($self, $parent, $node) = @_;
331 my $newNode = $parent->addNewChild($svgURI, 'g');
332 my @p = explodeCoord($node->getAttribute('points') || '');
333 while (@p) {
334 my ($x, $y) = (shift(@p), shift(@p));
335 my $dot = $newNode->addNewChild($svgURI, 'circle');
336 ### copy_attributes($dot, $node);
337 $self->copy_valid_attributes($newNode, $node);
338 if (my $size = $node->getAttribute('dotsize')) {
339 $dot->setAttribute(r => $size); }
340 $dot->setAttribute(cx => $x);
341 $dot->setAttribute(cy => $y); }
342 return $newNode; }
343  
344 sub convertEllipse {
345 my ($self, $parent, $node) = @_;
346 my $newNode = $parent->addNewChild($svgURI, 'ellipse');
347 $self->copy_valid_attributes($newNode, $node);
348 $self->copy_position($newNode, $node);
349 map { convertNode($self, $newNode, $_) } element_nodes($node);
350 return $newNode; }
351  
352 sub convertWedge {
353 my ($self, $parent, $node) = @_;
354 my ($x, $y, $r, $a1, $a2) = get_attr($node, qw(x y r angle1 angle2));
355 my $bb = $a2 - $a1; $bb += 360 if $bb < 0;
356 $bb = $bb > 180 ? 1 : 0; ($a1, $a2) = radians($a1, $a2);
357 my ($x1, $y1, $x2, $y2) = trunc(2, $x + $r * cos($a1), $y + $r * sin($a1), $x + $r * cos($a2), $y + $r * sin($a2));
358 my $newNode = $parent->addNewChild($svgURI, 'path');
359 $newNode->setAttribute(d => "M $x $y L $x1 $y1 A $r $r 0 $bb 1 $x2 $y2 z");
360 $self->copy_valid_attributes($newNode, $node);
361 map { convertNode($self, $newNode, $_) } element_nodes($node);
362 return $newNode; }
363  
364 sub convertArc {
365 my ($self, $parent, $node) = @_;
366 my ($x, $y, $r, $a1, $a2, $sp, $stroke, $fill) =
367 get_attr($node, qw(x y r angle1 angle2 showpoints stroke fill));
368 my $bb = $a2 - $a1;
369 $bb += 360 if $bb < 0; $bb = $bb > 180 ? 1 : 0; ($a1, $a2) = radians($a1, $a2);
370 my ($x1, $y1, $x2, $y2) = trunc(2, $x + $r * cos($a1), $y + $r * sin($a1), $x + $r * cos($a2), $y + $r * sin($a2));
371 my $linestroke = ($stroke || '') eq 'none' ? $fill : $stroke;
372  
373 my $newNode = $parent->addNewChild($svgURI, 'g');
374 if (my $transform = $node->getAttribute('transform')) {
375 $newNode->setAttribute(transform => $transform); }
376 if (($sp || '') eq 'true') {
377 my $newLine = $newNode->addNewChild($svgURI, 'path');
378 $newLine->setAttribute(d => "M $x1 $y1 $x $y $x2 $y2");
379 $newLine->setAttribute(fill => 'none');
380 $newLine->setAttribute('stroke-dasharray' => '2');
381 $newLine->setAttribute(stroke => $linestroke);
382 $self->copy_valid_attributes($newLine, $node); }
383 my $newArc = $newNode->addNewChild($svgURI, 'path');
384 $newArc->setAttribute(d => "M $x1 $y1 A $r $r 0 $bb 1 $x2 $y2");
385 $self->copy_valid_attributes($newArc, $node);
386 setArrows($newArc, $node, $linestroke);
387 # map { $newNode->appendChild(convertNode($self,$_)) } element_nodes($node);
388 return $newNode; }
389  
390 #################################################################
391  
392 sub getFillFrame {
393 my ($fill) = @_;
394 my $bgName = 'bg' . $fill; $bgName =~ s/\#//g;
395 $bgName .= ($::IDCOUNTER++);
396 $::DEFS{$bgName} =
397 new_node($svgURI, 'filter', [new_node($svgURI, 'feFlood', undef, 'flood-color' => $fill, 'flood-opacity' => 1, result => 'bg'),
398 new_node($svgURI, 'feMerge', [new_node($svgURI, 'feMergeNode', undef, in => 'bg'),
399 new_node($svgURI, 'feMergeNode', undef, in => 'SourceGraphic')])],
400 id => $bgName, primitiveUnits => 'objectBoundingBox', x => '-0.1', y => '-0.1', width => '1.2', height => '1.2') unless $::DEFS{$bgName};
401 return $bgName;
402 }
403  
404 sub getArrow {
405 my ($fill, $type) = @_; my $ar = 'AR' . ($fill || ''); $ar =~ s/\#//g;
406 if ($type eq '>') {
407 $ar .= '_R'; }
408 elsif ($type eq '<') {
409 $ar .= '_L'; }
410 $ar .= ($::IDCOUNTER++);
411 $::DEFS{$ar} = new_node($svgURI, 'marker', new_node($svgURI, 'path', undef, fill => $fill, stroke => 'none',
412 d => ($type eq '>') ? 'M 0 0 L 20 10 L 0 20 L 8 9 z' : 'M 0 10 L 20 0 L 12 10 L 20 20 z'),
413 id => $ar, viewBox => '0 0 20 20', markerUnits => 'strokeWidth', markerWidth => 20, markerHeight => 12, orient => 'auto', refX => 20, refY => 10)
414 unless $::DEFS{$ar};
415 return $ar; }
416  
417 sub setArrows {
418 my ($node, $from, $fill) = @_;
419 return unless $from->hasAttribute('terminators');
420 my $t = $from->getAttribute('terminators'); ###remove_attr($node, qw(terminators arrowlength));
421 return unless $t =~ /([^\-]*)-(.*)/;
422 my ($start, $end) = ($1, $2);
423 if ($start =~ s/(>|<)//) {
424 $node->setAttribute('marker-start' => 'url(#' . getArrow($fill, $1) . ')'); }
425 if ($end =~ s/(>|<)//) {
426 $node->setAttribute('marker-end' => 'url(#' . getArrow($fill, $1) . ')'); }
427 return; }
428  
429 sub mergeTransform {
430 my ($node, $new_t) = @_;
431 my $old_t = $node->getAttribute('transform');
432 my $t = ($old_t && $new_t) ? Transform("$new_t $old_t")->toString :
433 ($old_t ? $old_t : ($new_t ? $new_t : undef));
434 $node->setAttribute(transform => $t) if $t;
435 return; }
436  
437 sub ovalPath {
438 my ($opt, $x, $y, $w, $h, $r) = @_;
439  
440 my $trStart = "M " . ($x + $w / 2) . " $y ";
441 my $trContent = "L " . ($x + $w - $r) . " $y A $r $r 0 0 1 " . ($x + $w) . " " . ($y - $r) . " L " . ($x + $w) . " " . ($y - $h / 2) . " ";
442 my $tlStart = "M $x " . ($y - $h / 2) . " ";
443 my $tlContent = "L $x " . ($y - $r) . " A $r $r 0 0 1 " . ($x + $r) . " $y L " . ($x + $w / 2) . " $y ";
444  
445 my $brStart = "M " . ($x + $w) . " " . ($y - $h / 2) . " ";
446 my $brContent = "L " . ($x + $w) . " " . ($y - $h + $r) . " A $r $r 0 0 1 " . ($x + $w - $r) . " " . ($y - $h) .
447 " L " . ($x + $w / 2) . " " . ($y - $h) . " ";
448 my $blStart = "M " . ($x + $w / 2) . " " . ($y - $h) . " ";
449 my $blContent = "L " . ($x + $r) . " " . ($y - $h) . " A $r $r 0 0 1 $x " . ($y - $h + $r) . " L $x " . ($y - $h / 2) . " ";
450 my $path = '';
451 if ($opt eq 't') {
452 $path = $tlStart . $tlContent . $trContent; }
453 elsif ($opt eq 'b') {
454 $path = $brStart . $brContent . $blContent; }
455 elsif ($opt eq 'l') {
456 $path = $blStart . $blContent . $tlContent; }
457 elsif ($opt eq 'r') {
458 $path = $trStart . $trContent . $brContent; }
459 elsif ($opt eq 'tr' || $opt eq 'rt') {
460 $path = $trStart . $trContent; }
461 elsif ($opt eq 'tl' || $opt eq 'lt') {
462 $path = $tlStart . $tlContent; }
463 elsif ($opt eq 'br' || $opt eq 'rb') {
464 $path = $brStart . $brContent; }
465 elsif ($opt eq 'bl' || $opt eq 'lb') {
466 $path = $blStart . $blContent; }
467 chop($path);
468 return $path; }
469  
470 sub boxContentPos {
471 my ($node) = @_;
472 my ($nw, $nh, $npos) = get_attr($node, qw(width height pos));
473 return (0, 0) unless defined $nw && defined $nh;
474 $nw =~ s/pt$//;
475 $nh =~ s/pt$//;
476 if (!$npos) {
477 return ($nw / 2, $nh / 2); }
478 elsif ($npos eq 't') {
479 return ($nw / 2, $nh); }
480 elsif ($npos eq 'b') {
481 return ($nw / 2, 0); }
482 elsif ($npos eq 'l') {
483 return (0, $nh / 2); }
484 elsif ($npos eq 'r') {
485 return ($nw, $nh / 2); }
486 elsif ($npos eq 'tr' || $npos eq 'rt') {
487 return ($nw, $nh); }
488 elsif ($npos eq 'tl' || $npos eq 'lt') {
489 return (0, $nh); }
490 elsif ($npos eq 'br' || $npos eq 'rb') {
491 return ($nw, 0); }
492 else {
493 return (0, 0); } }
494  
495 sub arcPoints {
496 my ($node) = @_;
497 my ($pts, $r) = get_attr($node, qw(points arc));
498 return 'M ' . $pts if !$r && $pts;
499 local *getP = sub {
500 my ($x1, $y1, $x2, $y2) = @_;
501 # TODO: Do we need a warning if $dst is zero?
502 # Default to 0.01 since we'll use it as a denominator
503 my $dst = sqrt(($x1 - $x2)**2 + ($y1 - $y2)**2) || 0.01;
504 my $s = ($x2 - $x1) * ($y2 - $y1) >= 0 ? 1 : -1;
505 trunc(2, $s, $x1 + ($x2 - $x1) * $r / $dst, $y1 + ($y2 - $y1) * $r / $dst); };
506 my @p = explodeCoord($pts); my $n = ($#p + 1) / 2;
507 my $d = "M $p[0] $p[1] ";
508 for (my $i = 1 ; $i < $n - 1 ; $i++) {
509 my ($x2, $y2) = ($p[2 * $i - 2], $p[2 * $i - 1]);
510 my ($x1, $y1) = ($p[2 * $i], $p[2 * $i + 1]);
511 my ($sa, $xa, $ya) = getP($x1, $y1, $x2, $y2);
512 ($x2, $y2) = ($p[2 * $i + 2], $p[2 * $i + 3]);
513 my ($sb, $xb, $yb) = getP($x1, $y1, $x2, $y2);
514 my $sf = ($sa >= $sb) ? 0 : 1;
515 $d .= "L $xa $ya A $r $r 0 0 $sf $xb $yb ";
516 }
517 $d .= "L $p[2*$n-2] $p[2*$n-1]";
518 return $d; }
519  
520 1;