/*
ASCII2MathML-SVGGradientsFilters.js v3.41, May 13th 2009
=====================================================================
Copyright Dr.ir. S.A. Miedema, www.Fullxml.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License 
(at http://www.gnu.org/licences/lgpl.html) for more details.
*/

//Start of Gradients
function createGradient(offset1,color1,offset2,color2,id,direction,offset3,color3) {
  createLinearGradient(offset1,color1,offset2,color2,id,direction,offset3,color3);
}

function createLinearGradient(offset1,color1,offset2,color2,id,direction,offset3,color3) {
  var node, qnode, stopnode, angle, dx, dy;
  qnode = doc.getElementById("defs");

  if (id!=null) node = doc.getElementById(id);
  if (node==null) {
    node = myCreateElementSVG("linearGradient");
    node.setAttribute("id", id);
    qnode.appendChild(node);
  }
  if (direction==Horizontal || direction==Vertical) {
    node.setAttribute("x1","0%");
    if (direction==Horizontal) {node.setAttribute("x2","100%");} else {node.setAttribute("x2","0%");}
    node.setAttribute("y1","0%");
    if (direction==Horizontal) {node.setAttribute("y2","0%");} else {node.setAttribute("y2","100%");}
  } else {
    if (direction!=null) {angle=direction;} else {angle=0;}
    angle = -angle*pi/180;
    dx = 100*cos(angle);
    dy = 100*sin(angle);
    if (dx>=0) {node.setAttribute("x1","0%");node.setAttribute("x2",dx.toFixed(2)+"%");} else {node.setAttribute("x2","0%");node.setAttribute("x1",-dx.toFixed(2)+"%");}
    if (dy>=0) {node.setAttribute("y1","0%");node.setAttribute("y2",dy.toFixed(2)+"%");} else {node.setAttribute("y2","0%");node.setAttribute("y1",-dy.toFixed(2)+"%");}
  }
  
  stopnode = myCreateElementSVG("stop");
  stopnode.setAttribute("offset",offset1);
  stopnode.setAttribute("stop-color",color1);
  node.appendChild(stopnode);
  
  stopnode = myCreateElementSVG("stop");
  stopnode.setAttribute("offset",offset2);
  stopnode.setAttribute("stop-color",color2);
  node.appendChild(stopnode);
  
  if (offset3!=null && color3!=null) {
    stopnode = myCreateElementSVG("stop");
    stopnode.setAttribute("offset",offset3);
    stopnode.setAttribute("stop-color",color3);
    node.appendChild(stopnode);
  }
}

function createRadialGradient(offset1,color1,offset2,color2,id,radius,offset3,color3, excx, excy) {
  var node, qnode, stopnode;
  qnode = doc.getElementById("defs");

  if (id!=null) node = doc.getElementById(id);
  if (node==null) {
    node = myCreateElementSVG("radialGradient");
    node.setAttribute("id", id);
    qnode.appendChild(node);
  }
  node.setAttribute("cx","50%");
  node.setAttribute("cy","50%");
  node.setAttribute("r",radius);
  if (excx!=null) {node.setAttribute("fx",excx);} else {node.setAttribute("fx","50%");}
  if (excy!=null) {node.setAttribute("fy",excy);} else {node.setAttribute("fy","50%");}
  
  stopnode = myCreateElementSVG("stop");
  stopnode.setAttribute("offset",offset1);
  stopnode.setAttribute("stop-color",color1);
  node.appendChild(stopnode);
  
  stopnode = myCreateElementSVG("stop");
  stopnode.setAttribute("offset",offset2);
  stopnode.setAttribute("stop-color",color2);
  node.appendChild(stopnode);
  
  if (offset3!=null && color3!=null) {
    stopnode = myCreateElementSVG("stop");
    stopnode.setAttribute("offset",offset3);
    stopnode.setAttribute("stop-color",color3);
    node.appendChild(stopnode);
  }
}

function setGradient(id, setFillOpacity) {
  fill = "url(#"+id+")";
  fillopacity = setFillOpacity;
}
//End of Gradients

//Start of Filters
function createSVGFilter(filterid, factorX, factorY, filterUnits) {
  var filternode, qnode, xfactor, yfactor;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  if (factorX!=null) {xfactor = factorX;} else {xfactor = 50;};
  if (factorY!=null) {yfactor = factorY;} else {yfactor = 50;}
  filternode.setAttribute("x","-"+(xfactor/2)+"%");
  filternode.setAttribute("y","-"+(yfactor/2)+"%");
  filternode.setAttribute("width",(xfactor*1+100)+"%");
  filternode.setAttribute("height",(yfactor*1+100)+"%");
  if (filterUnits!=null) filternode.setAttribute("filterUnits",filterUnits);
}

function addSVGFilterOffset(filterid, input, output, offsetX, offsetY) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feOffset");
  effectnode.setAttribute("in",input);
  effectnode.setAttribute("result",output);
  effectnode.setAttribute("dx",offsetX);
  effectnode.setAttribute("dy",offsetY);
  filternode.appendChild(effectnode);
}

function addSVGFilterGaussianBlur(filterid, input, output, stddefX, stddefY) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feGaussianBlur");
  effectnode.setAttribute("in",input);
  effectnode.setAttribute("result",output);
  if (stddefY!=null) {effectnode.setAttribute("stdDeviation",stddefX+" "+stddefY);} else {effectnode.setAttribute("stdDeviation",stddefX);}
  filternode.appendChild(effectnode);
}

function addSVGFilterColorMatrix(filterid, input, output, matrixType, RBrightness, GBrightness,BBrightness, ABrightness) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feColorMatrix");
  effectnode.setAttribute("in",input);
  effectnode.setAttribute("result",output);
  effectnode.setAttribute("type",matrixType);
  effectnode.setAttribute("values",RBrightness+" 0 0 0 0  0 "+GBrightness+" 0 0 0  0 0 "+BBrightness+" 0 0  0 0 0 "+ABrightness+" 0");
  filternode.appendChild(effectnode);
}

function addSVGFilterBlend(filterid, input1, input2, output, mode) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feBlend");
  effectnode.setAttribute("in",input1);
  effectnode.setAttribute("in2",input2);
  effectnode.setAttribute("result",output);
  effectnode.setAttribute("mode",mode);
  filternode.appendChild(effectnode);
}

function addSVGFilterComposite(filterid, input1, input2, output, operator, k1, k2, k3, k4) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feComposite");
  effectnode.setAttribute("in",input1);
  effectnode.setAttribute("in2",input2);
  effectnode.setAttribute("result",output);
  effectnode.setAttribute("operator",operator);
  if (k1!=null) effectnode.setAttribute("k1",k1);
  if (k2!=null) effectnode.setAttribute("k2",k2);
  if (k3!=null) effectnode.setAttribute("k3",k3);
  if (k4!=null) effectnode.setAttribute("k4",k4);
  filternode.appendChild(effectnode);
}

function addSVGMerge(filterid, input1, input2) {
  var filternode, qnode, effectnode, subnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feMerge");
  filternode.appendChild(effectnode);

  subnode = myCreateElementSVG("feMergeNode");
  subnode.setAttribute("in",input1);
  effectnode.appendChild(subnode);
  subnode = myCreateElementSVG("feMergeNode");
  subnode.setAttribute("in",input2);
  effectnode.appendChild(subnode);
}

function addSVGSpecularLighting(filterid, input, output, surfaceScale, specularConstant, specularExponent, color, x, y, z, azimuth, elevation) {
  var filternode, qnode, effectnode, subnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feSpecularLighting");
  effectnode.setAttribute("in",input);
  effectnode.setAttribute("result",output);
  effectnode.setAttribute("surfaceScale",surfaceScale);
  effectnode.setAttribute("specularConstant",specularConstant);
  effectnode.setAttribute("specularExponent",specularExponent);
  effectnode.setAttribute("lighting-color",color);
  filternode.appendChild(effectnode);

  if (x!=null && y!=null && z!=null) {
    subnode = myCreateElementSVG("fePointLight");
    subnode.setAttribute("x",x);
    subnode.setAttribute("y",y);
    subnode.setAttribute("z",z);
    effectnode.appendChild(subnode);
  }

  if (azimuth!=null && elevation!=null) {
    subnode = myCreateElementSVG("feDistantLight");
    subnode.setAttribute("azimuth",azimuth);
    subnode.setAttribute("elevation",elevation);
    effectnode.appendChild(subnode);
  }
}

function addSVGFilterTemplate(filterid, input, output) {
  var filternode, qnode, effectnode;
  qnode = doc.getElementById("defs");

  if (filterid!=null) filternode = doc.getElementById(filterid);
  if (filternode==null) {
    filternode = myCreateElementSVG("filter");
    filternode.setAttribute("id", filterid);
    qnode.appendChild(filternode);
  }
  effectnode = myCreateElementSVG("feComposite");
  effectnode.setAttribute("in",input1);
  effectnode.setAttribute("in2",input2);
  effectnode.setAttribute("result",output);
  filternode.appendChild(effectnode);
}

function addFilter(objectid,filterid) {
  var objectnode = doc.getElementById(objectid);
  objectnode.setAttribute("filter","url(#"+filterid+")")
}
//End of Filters

//Start of Specific Filters
function createDropShadow(id, offsetX, offsetY, stddef, factorX, factorY, useAlpha, alphabrightness) {
  var brightness;
  createSVGFilter(id, factorX, factorY);
  if (useAlpha!=null) {
    if (useAlpha) {addSVGFilterOffset(id, "SourceAlpha", "offOut", offsetX, offsetY);} else {addSVGFilterOffset(id, "SourceGraphic", "offOut", offsetX, offsetY);}
  } else {
    addSVGFilterOffset(id, "SourceAlpha", "offOut", offsetX, offsetY);
  }
  if (alphabrightness!=null) {brightness=alphabrightness;} else {brightness=1;}
  addSVGFilterColorMatrix(id, "offOut", "MatrixOut", "matrix", 1, 1, 1, brightness);
  addSVGFilterGaussianBlur(id, "MatrixOut", "blurOut", stddef);
  addSVGFilterBlend(id, "SourceGraphic", "blurOut", null, "normal");
}

function createDDD(id, factorX, factorY,inverse) {
  createSVGFilter(id, factorX, factorY,"userSpaceOnUse");
  addSVGFilterGaussianBlur(id, "SourceAlpha", "blur", 4);
  addSVGFilterOffset(id, "blur", "offsetBlur", 4, 4);
  if (inverse) {
    addSVGSpecularLighting(id, "blur", "specOut", 5, 0.75, 20, "#BBBBBB", 5000, 10000, 20000);
  } else {
    addSVGSpecularLighting(id, "blur", "specOut", 5, 0.75, 20, "#BBBBBB", -5000, -10000, 20000);
  }
  addSVGFilterComposite(id, "specOut", "SourceAlpha", "specOut", "in");
  addSVGFilterComposite(id, "SourceGraphic", "specOut", "litPaint", "arithmetic", 0, 1, 1, 0);
  addSVGMerge(id, "offsetBlur", "litPaint")
}
//End of Specific Filters

//Start of Transformations
function rotate(id, p, angle) {
  var node, rotatestr;
  rotatestr = "";
  if (id!=null) {
    node = doc.getElementById(id);
  	var xangle = p[0]*xunitlength+origin[0];
  	var yangle = height-p[1]*yunitlength-origin[1];
  	rotatestr = " translate("+xangle+","+yangle+") rotate("+angle+") translate(-"+xangle+",-"+yangle+")";
	node.setAttribute("transform",rotatestr);
  }
  return rotatestr;
}

function translation(id, p) {
  var node, translatestr;
  translatestr = "";
  if (id!=null) {
    node = doc.getElementById(id);
  	var xangle = p[0]*xunitlength;
  	var yangle = -p[1]*yunitlength;
  	translatestr = " translate("+xangle+","+yangle+")";
	node.setAttribute("transform",translatestr);
  }
  return translatestr;
}

function skewx(id, p, angle) {
  var node, skewxstr;
  skewxstr = "";
  if (id!=null) {
    node = doc.getElementById(id);
  	var xangle = p[0]*xunitlength+origin[0];
  	var yangle = height-p[1]*yunitlength-origin[1];
  	skewxstr = " translate("+xangle+","+yangle+") skewX("+angle+") translate(-"+xangle+",-"+yangle+")";
	node.setAttribute("transform",skewxstr);
  }
  return skewxstr;
}

function skewy(id, p, angle) {
  var node, skewystr;
  skewystr = "";
  if (id!=null) {
    node = doc.getElementById(id);
  	var xangle = p[0]*xunitlength+origin[0];
  	var yangle = height-p[1]*yunitlength-origin[1];
  	skewystr = " translate("+xangle+","+yangle+") skewY("+angle+") translate(-"+xangle+",-"+yangle+")";
	node.setAttribute("transform",skewystr);
  }
  return skewystr;
}

function scale(id, p, scalex, scaley) {
  var node, scalestr;
  scalestr = "";
  if (id!=null) {
    node = doc.getElementById(id);
  	var xangle = p[0]*xunitlength+origin[0];
  	var yangle = height-p[1]*yunitlength-origin[1];
  	scalestr = " translate("+xangle+","+yangle+") scale("+scalex+","+scaley+") translate(-"+xangle+",-"+yangle+")";
	node.setAttribute("transform",scalestr);
  }
  return scalestr;
}

function transform(id, transformstr) {
  var node;
  if (id!=null) {
    node = doc.getElementById(id);
	node.setAttribute("transform",transformstr);
  }
}
//End of Transformations