public static function getInstance($x, $y, $id, $text, $style = null) { $t = new SVGText('<text></text>'); $t->setX($x); $t->setY($y); $t->setId($id); $t->setAttribute('style', $style); $t->addChild('tspan', $text); return $t; }
*/ require_once "../svglib/svglib.php"; $svg = SVGDocument::getInstance(); $svg->setTitle("Javascript example"); #add some javascript functions $svg->addScript("\n function changeColor(evt, element)\n {\n destination = document.getElementById('destination');\n\n if ( evt.ctrlKey )\n {\n destination.style.stroke = element.style.fill;\n }\n else\n {\n destination.style.fill = element.style.fill;\n }\n\n evt.preventDefault();\n return false;\n }\n"); #mount a simple color array $colors[] = 'red'; $colors[] = 'green'; $colors[] = 'blue'; $colors[] = 'yellow'; $colors[] = 'orange'; $colors[] = 'gray'; $colors[] = 'black'; $colors[] = 'white'; $text = SVGText::getInstance(10, 25, null, 'Left click for fill - control click for stroke'); $svg->addShape($text); foreach ($colors as $line => $color) { $rect = SVGRect::getInstance($line * 60 + 10, 40, null, 50, 50); $style = new SVGStyle(); $style->setFill($color); $style->setStroke("darkGray", 1); $rect->setStyle($style); $rect->addOnclick("return changeColor(evt,this);"); $rect->addAttribute("onmouseover", "this.style.stroke = 'lightGray';"); $rect->addAttribute("onmouseout", "this.style.stroke = 'gray';"); $svg->addShape($rect); } $rect = SVGRect::getInstance(140, 100, 'destination', 200, 200); $style = new SVGStyle(); $style->setFill('none');
$svg->addShape($circle); $ellipse = SVGEllipse::getInstance(200, 200, 100, 40); $ellipse->rotate(-30, 200, 200); $style2 = new SVGStyle(); $style2->setFill('none'); $style2->setStroke('blue', 3); $ellipse->setStyle($style2); $svg->addShape($ellipse); $style = new SVGStyle(); #create a style object #set fill and stroke $style->setFill('#f2f2f2'); $style->setStroke('#e1a100'); $style->setStrokeWidth(2); #create a text $text = SVGText::getInstance(22, 50, 'myText', 'This is a text', $style); $svg->addShape($text); #$svg->addShape( SVGPath::getInstance( array('m 58,480','639,1'), 'myPath', 'fill:none;stroke:#000000;stroke-width:1px;') );#create a path $svg->addShape(SVGLine::getInstance(50, 50, 500, 500, null, $style)); #many types of output try { $svg->asXML(getcwd() . '/output/output.svg'); #svg #example how to using SVG Inkscape #define('INKSCAPE_PATH', 'H:\ferramentas\Inkscape\inkscape' ); #$svg->export( getcwd() . '/output/inkscape.png', null, null, true, SVGDocument::EXPORT_TYPE_INKSCAPE ); #$svg->export( getcwd() . '/output/output.png' ); #png $svg->export(getcwd() . '/output/output.jpg'); #jpg $svg->export(getcwd() . '/output/output.gif'); #gif
public function onCreate() { if (!$this->getMaxY()) { $this->findMaxY(); } if (!$this->getMaxX()) { $this->findMaxX(); } $this->setWidth($this->getMaxX() + 100 . 'px'); $this->setHeight($this->getMaxY() + 100 . 'px'); $this->setDefaultViewBox(); $clipPath = SVGClipPath::getInstance('clipPath'); $clipRect = SVGRect::getInstance(0, 0, null, $this->getWidth(), $this->getHeight()); $clipPath->addShape($clipRect); $this->addDefs($clipPath); $backGroup = SVGGroup::getInstance('backGroup'); $line1 = SVGLine::getInstance($this->startX, $this->startY, $this->startX, $this->maxY + $this->startY, null, new SVGStyle(array('stroke' => 'black', 'stroke-width' => 1))); $line2 = SVGLine::getInstance($this->startX, $this->maxY + $this->startY, $this->maxX + $this->startX, $this->maxY + $this->startY, null, new SVGStyle(array('stroke' => 'black', 'stroke-width' => 1))); $backGroup->addShape($line1); $backGroup->addShape($line2); #vertical counter for ($i = 0; $i <= $this->maxY; $i += 25) { $text = SVGText::getInstance($this->startX - 30, $this->startY + $i, null, $this->maxY - $i); $text->setStyle("font-family:Arial"); $backGroup->addShape($text); } #horizontal counter for ($i = 0; $i <= $this->maxX; $i += 50) { $text = SVGText::getInstance($this->startX + $i, $this->startY + $this->maxY + 20, null, $i); $text->setStyle("font-family:Arial"); $backGroup->addShape($text); } $data = $this->getData(); $mainGroup = SVGGroup::getInstance('mainGroup'); $mainGroup->setStyle(new SVGStyle(array('clip-path' => 'url(#clipPath)'))); if (is_array($data)) { foreach ($data as $obj) { $itemData = $obj->data; $style = $obj->style; if (!$style) { $style = new SVGStyle(array('stroke' => 'blue', 'fill' => 'blue', 'stroke-width' => 1)); } if (is_array($itemData)) { foreach ($itemData as $line => $info) { $previous = $this->normalizeLineData(@$itemData[$line - 1]); $info = $this->normalizeLineData($info); $line = SVGLine::getInstance($previous[0], $previous[1], $info[0], $info[1], null, $style); //$this->addShape( $line ); $mainGroup->addShape($line); $circle = SVGCircle::getInstance($info[0], $info[1], 3, null, $style); $circle->setTitle($info[0] . ',' . $info[1]); $circle->addAttribute("onmouseover", "this.style.stroke = 'lightGray';"); $circle->addAttribute("onmouseout", "this.style.stroke = 'gray';"); //$this->addShape( $circle ); $mainGroup->addShape($circle); } } } } $this->addShape($backGroup); $this->addShape($mainGroup); $this->addScript("\n var width = \$('svg').attr('width').replace('px','');\n \$('svg #clippath rect').attr('width',0);\n var anim = setInterval('slideRight()', 1);\n\n function slideRight()\n {\n var currentWidth = parseInt( \$('svg #clippath rect').attr('width') );\n currentWidth += 1;\n\n \$('svg #clippath rect').attr('width',currentWidth );\n\n if ( currentWidth >= width )\n {\n clearInterval(anim);\n }\n }\n"); /* $this->addScript( " $('svg #mainGroup').hide(); setTimeout('showGraph()', 500); function showGraph() { $('svg #mainGroup').hide(); $('svg #mainGroup').show('slow'); }" ); */ }
private function parseText() { $o = Scale::getInstance(); /* * The style options deserve some comments. The monospace and font-size * choices are not accidental. This gives the best sort of estimation * for font size to scale that I could come up with empirically. * * N.B. This might change with different scales. I kind of feel like this * is a bug waiting to be filed, but whatever. */ $fSize = 0.95 * $o->yScale; $this->svgObjects->pushGroup('text'); $this->svgObjects->setOption('fill', 'black'); $this->svgObjects->setOption('style', "font-family:LMMono10,monospace;font-size:{$fSize}px"); /* * Text gets the same scanning treatment as boxes. We do left-to-right * scanning, which should probably be configurable in case someone wants * to use this with e.g. Arabic or some other right-to-left language. * Either way, this isn't UTF-8 safe (thanks, PHP!!!), so that'll require * thought regardless. */ $boxes = $this->svgObjects->getGroup('boxes'); $bound = count($boxes); foreach ($this->grid as $row => $line) { $cols = count($line); for ($i = 0; $i < $cols; $i++) { if ($this->getChar($row, $i) != ' ') { /* More magic numbers that probably need research. */ $t = new SVGText($i - 0.6, $row + 0.3); /* Time to figure out which (if any) box we live inside */ $tP = $t->getPoint(); $maxPoint = new Point(-1, -1); $boxQueue = array(); for ($j = 0; $j < $bound; $j++) { if ($boxes[$j]->hasPoint($tP->gridX, $tP->gridY)) { $boxPoints = $boxes[$j]->getPoints(); $boxTL = $boxPoints[0]; /* * This text is in this box, but it may still be in a more * specific nested box. Find the box with the highest top * left X,Y coordinate. Keep a queue of boxes in case the top * most box doesn't have a fill. */ if ($boxTL->y > $maxPoint->y && $boxTL->x > $maxPoint->x) { $maxPoint->x = $boxTL->x; $maxPoint->y = $boxTL->y; $boxQueue[] = $boxes[$j]; } } } if (count($boxQueue) > 0) { /* * Work backwards through the boxes to find the box with the most * specific fill. */ for ($j = count($boxQueue) - 1; $j >= 0; $j--) { $fill = $boxQueue[$j]->getOption('fill'); if ($fill == 'none' || $fill == null) { continue; } if (substr($fill, 0, 1) != '#') { if (!isset($GLOBALS['A2S_colors'][strtolower($fill)])) { continue; } else { $fill = $GLOBALS['A2S_colors'][strtolower($fill)]; } } else { if (strlen($fill) != 4 && strlen($fill) != 7) { continue; } } if ($fill) { /* Attempt to parse the fill color */ if (strlen($fill) == 4) { $cR = hexdec(str_repeat($fill[1], 2)); $cG = hexdec(str_repeat($fill[2], 2)); $cB = hexdec(str_repeat($fill[3], 2)); } elseif (strlen($fill) == 7) { $cR = hexdec(substr($fill, 1, 2)); $cG = hexdec(substr($fill, 3, 2)); $cB = hexdec(substr($fill, 5, 2)); } /* * This magic is gleaned from the working group paper on * accessibility at http://www.w3.org/TR/AERT. The recommended * contrast is a brightness difference of at least 125 and a * color difference of at least 500. Since our default color * is black, that makes the color difference easier. */ $bFill = ($cR * 299 + $cG * 587 + $cB * 114) / 1000; $bDiff = $cR + $cG + $cB; $bText = 0; if ($bFill - $bText < 125 || $bDiff < 500) { /* If black is too dark, white will work */ $t->setOption('fill', '#fff'); } else { $t->setOption('fill', '#000'); } break; } } if ($j < 0) { $t->setOption('fill', '#000'); } } else { /* This text isn't inside a box; make it black */ $t->setOption('fill', '#000'); } /* We found a stringy character, eat it and the rest. */ $str = $this->getChar($row, $i++); while ($i < count($line) && $this->getChar($row, $i) != ' ') { $str .= $this->getChar($row, $i++); /* Eat up to 1 space */ if ($this->getChar($row, $i) == ' ') { $str .= ' '; $i++; } } if ($str == '') { continue; } $t->setString($str); /* * If we were in a box, group with the box. Otherwise it gets its * own group. */ if (count($boxQueue) > 0) { $t->setOption('stroke', 'none'); $t->setOption('style', "font-family:LMMono10,monospace;font-size:{$fSize}px"); $boxQueue[count($boxQueue) - 1]->addText($t); } else { $this->svgObjects->addObject($t); } } } } }