1
0
aclindsay.com/svgparser/parser/index.php

345 lines
8.8 KiB
PHP
Raw Permalink Normal View History

2011-02-24 14:25:26 -05:00
<?php
/*
SVG to dojox.gfx Parser
Copyright (C) 2009 Aaron Lindsay <aclindsay@aclindsay.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
require_once("parser/Parser.php");
$requestData = '';
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
$httpContent = fopen('php://input', 'r');
while ($data = fread($httpContent, 1024)) {
$requestData .= $data;
}
fclose($httpContent);
}
if ($requestData == '')
exit;
$parser = new Parser();
$svg = $parser->parse($requestData);
//first, create javascript objects for all the gradients
$linearGradients = $svg->getCompleteLinearGradients();
$radialGradients = $svg->getCompleteRadialGradients();
$paths = $svg->getPaths();
$rects = $svg->getRectangles();
$circles = $svg->getCircles();
$ellipses = $svg->getEllipses();
$lines = $svg->getLines();
$polylines = $svg->getPolylines();
$texts = $svg->getTexts();
echo "/*
SVG to dojox.gfx Parser
Copyright (C) 2009 Aaron Lindsay <aclindsay@aclindsay.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/\n\n";
echo "//set 'g' equal to the dojo group you want to add this drawing as...\n//ex. var g = surface.createGroup()\n\n";
//echo all the linear gradients
foreach ($linearGradients as $gradient) {
//make sure the gradient has its transform(s) applied
if (!$gradient->transformsApplied())
$gradient->applyTransforms();
echo "var gradient".$gradient->getId()." = {\n";
echo "\ttype: \"linear\",\n";
echo "\tx1: ".$gradient->getX1().", y1: ".$gradient->getY1().",\n";
echo "\tx2: ".$gradient->getX2().", y2: ".$gradient->getY2().",\n";
echo "\tcolors: [\n";
$stops = $gradient->getStops();
for ($i = 0; $i < count($stops)-1; $i++) {
echo "\t\t{offset: ".$stops[$i]->getOffset().", color: \"".$stops[$i]->getColor()."\"},\n";
}
echo "\t\t{offset: ".$stops[count($stops)-1]->getOffset().", color: \"".$stops[count($stops)-1]->getColor()."\"}\n";
echo "\t]\n";
echo "};\n";
}
//now, do the same for all the radial gradients
foreach ($radialGradients as $gradient) {
//make sure the gradient has its transform(s) applied
if (!$gradient->transformsApplied())
$gradient->applyTransforms();
echo "var gradient".$gradient->getId()." = {\n";
echo "\ttype: \"radial\",\n";
echo "\tcx: ".$gradient->getCx().", cy: ".$gradient->getCy().",\n";
echo "\tr: ".$gradient->getR().",\n";
echo "\tfx: ".$gradient->getFx().", fy: ".$gradient->getFy().",\n";
echo "\tcolors: [\n";
$stops = $gradient->getStops();
for ($i = 0; $i < count($stops)-1; $i++) {
echo "\t\t{offset: ".$stops[$i]->getOffset().", color: \"".$stops[$i]->getColor()."\"},\n";
}
echo "\t\t{offset: ".$stops[count($stops)-1]->getOffset().", color: \"".$stops[count($stops)-1]->getColor()."\"}\n";
echo "\t]\n";
echo "};\n";
}
//now, get all the paths
foreach ($paths as $path) {
echo "g.createPath({type: \"path\", path: \"".$path->getPath()."\"})";
addFill($path);
addStroke($path);
addTransform($path);
echo ";\n";
}
//and all rectangles
foreach ($rects as $rect) {
echo "g.createRect({type: \"rect\", x: ".$rect->getX().", y: ".$rect->getY().", height: ".$rect->getHeight().", width: ".$rect->getWidth().", r: ".$rect->getR()."})";
addFill($rect);
addStroke($rect);
addTransform($rect);
echo ";\n";
}
//and all circles
foreach ($circles as $circle) {
echo "g.createCircle({type: \"circle\", cx:".$circle->getCx().", cy:".$circle->getCy().", r: ".$circle->getR()."})";
addFill($circle);
addStroke($circle);
addTransform($circle);
echo ";\n";
}
//and all ellipses
foreach ($ellipses as $ellipse) {
echo "g.createEllipse({type: \"ellipse\", cx:".$ellipse->getCx().", cy:".$ellipse->getCy().", rx: ".$ellipse->getRx().", ry: ".$ellipse->getRy()."})";
addFill($ellipse);
addStroke($ellipse);
addTransform($ellipse);
echo ";\n";
}
//and all lines
foreach ($lines as $line) {
echo "g.createLine({type: \"line\", x1:".$line->getX1().", y1:".$line->getY1().", x2:".$line->getX2().", y2:".$line->getY2()."})";
addStroke($line);
addTransform($line);
echo ";\n";
}
//and all polylines
foreach ($polylines as $polyline) {
echo "//TODO - implement polylines\n";
}
//and all texts
foreach ($texts as $text) {
echo "g.createText({type: \"text\", x: ".$text->getX().", y: ".$text->getY().", text: \"".$text->getText()."\", align: \"".$text->getTextAnchor()."\", decoration: \"".$text->getTextDecoration()."\"})";
addFill($text);
addStroke($text);
addTransform($text);
echo ";\n";
}
function addFont($shape) {
$putComma = false;
//get the font family, if available
$family = $shape->getStyle("font-family");
if ($family != null) {
if ($putComma)
echo ",";
else {
echo ".setFont({";
$putComma = true;
}
echo "family: \"".$family."\"";
}
//get the font variant, if available
$variant = $shape->getStyle("font-variant");
if ($variant != null) {
if ($putComma)
echo ",";
else {
echo ".setFont({";
$putComma = true;
}
echo "variant: \"".$variant."\"";
}
//get the font size, if available
$size = $shape->getStyle("font-size");
if ($size != null) {
if ($putComma)
echo ",";
else {
echo ".setFont({";
$putComma = true;
}
echo "size: \"".$size."\"";
}
//get the font style, if available
$style = $shape->getStyle("font-style");
if ($style != null) {
if ($putComma)
echo ",";
else {
echo ".setFont({";
$putComma = true;
}
echo "style: \"".$style."\"";
}
//get the font weight, if available
$weight = $shape->getStyle("font-weight");
if ($weight != null) {
if ($putComma)
echo ",";
else {
echo ".setFont({";
$putComma = true;
}
echo "style: \"".$weight."\"";
}
if ($putComma)
echo "})";
}
function addFill($shape) {
$fill = $shape->getStyle("fill");
if ($fill == null)
return;
if (ereg("url\(.*\)", $fill)) {
$parts = split("[\(\)]", $fill);
$url = trim($parts[1], "#");
echo ".setFill(gradient".$url.")";
} else if (ereg("#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})", $fill)) {
echo ".setFill(\"".$fill."\")";
}
}
function addStroke($shape) {
$putComma = false;
//get the stroke width, if available
$width = $shape->getStyle("stroke-width");
if ($width != null) {
if ($putComma)
echo ",";
else {
echo ".setStroke({";
$putComma = true;
}
$width = trim($width, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
echo "width: $width";
}
//get the stroke color, if available
$color = $shape->getStyle("stroke");
if ($color != null) {
if ($putComma)
echo ",";
else {
echo ".setStroke({";
$putComma = true;
}
echo "color: \"".$color."\"";
}
//get the stroke cap, if available
$cap = $shape->getStyle("stroke-linecap");
if ($cap != null) {
if ($putComma)
echo ",";
else {
echo ".setStroke({";
$putComma = true;
}
echo "cap: \"$cap\"";
}
//get the line join, if available
$join = $shape->getStyle("stroke-linejoin");
if ($join != null) {
if ($putComma)
echo ",";
else {
echo ".setStroke({";
$putComma = true;
}
if ($join == "miter")
echo "join: 4";
else
echo "join: \"$join\"";
}
//if any kind of dash is made in SVG, convert it to 'style: "Dash"' in dojox.gfx
$style = $shape->getStyle("stroke-dasharray");
if ($style != null && $style != 'none') {
if ($putComma)
echo ",";
else {
echo ".setStroke({";
$putComma = true;
}
echo "style: \"Dash\"";
}
if ($putComma)
echo "})";
}
function addTransform($shape) {
//echo "\n==== transform:";
//var_dump($shape->getTransform());
$transform = $shape->getTransform();
if (!$transform->isIdentity()) {
echo ".setTransform({xx:".$transform->getXx().", xy:".$transform->getXy().", dx:".$transform->getDx().", yy:".$transform->getYy().", yx:".$transform->getYx().", dy:".$transform->getDy()."})";
}
}
?>