Fix for UBSan build
[platform/upstream/doxygen.git] / src / svgpan_js.h
1 "/**\n"
2 " * The code below is based on SVGPan Library 1.2 and was modified for doxygen\n"
3 " * to support both zooming and panning via the mouse and via embedded bottons.\n"
4 " *\n"
5 " * This code is licensed under the following BSD license:\n"
6 " *\n"
7 " * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.\n"
8 " * \n"
9 " * Redistribution and use in source and binary forms, with or without modification, are\n"
10 " * permitted provided that the following conditions are met:\n"
11 " * \n"
12 " *    1. Redistributions of source code must retain the above copyright notice, this list of\n"
13 " *       conditions and the following disclaimer.\n"
14 " * \n"
15 " *    2. Redistributions in binary form must reproduce the above copyright notice, this list\n"
16 " *       of conditions and the following disclaimer in the documentation and/or other materials\n"
17 " *       provided with the distribution.\n"
18 " * \n"
19 " * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED\n"
20 " * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n"
21 " * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR\n"
22 " * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n"
23 " * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n"
24 " * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n"
25 " * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n"
26 " * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n"
27 " * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
28 " * \n"
29 " * The views and conclusions contained in the software and documentation are those of the\n"
30 " * authors and should not be interpreted as representing official policies, either expressed\n"
31 " * or implied, of Andrea Leofreddi.\n"
32 " */\n"
33 "\n"
34 "var root = document.documentElement;\n"
35 "var state = 'none';\n"
36 "var stateOrigin;\n"
37 "var stateTf = root.createSVGMatrix();\n"
38 "var cursorGrab = ' url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII=\"), move';\n"
39 "var zoomSteps = 10;\n"
40 "var zoomInFactor;\n"
41 "var zoomOutFactor;\n"
42 "var windowWidth;\n"
43 "var windowHeight;\n"
44 "var svgDoc;\n"
45 "var minZoom;\n"
46 "var maxZoom;\n"
47 "if (!window) window=this;\n"
48 "\n"
49 "/**\n"
50 " * Show the graph in the middle of the view, scaled to fit \n"
51 " */\n"
52 "function show()\n"
53 "{\n"
54 "  if (window.innerHeight) // Firefox\n"
55 "  {\n"
56 "    windowWidth = window.innerWidth;\n"
57 "    windowHeight = window.innerHeight;\n"
58 "  }\n"
59 "  else if (document.documentElement.clientWidth) // Chrome/Safari\n"
60 "  {\n"
61 "    windowWidth = document.documentElement.clientWidth\n"
62 "    windowHeight = document.documentElement.clientHeight\n"
63 "  }\n"
64 "  if (!windowWidth || !windowHeight) // failsafe\n"
65 "  {\n"
66 "    windowWidth = 800;\n"
67 "    windowHeight = 600;\n"
68 "  }\n"
69 "  minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);\n"
70 "  maxZoom = minZoom+1.5;\n"
71 "  zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);\n"
72 "  zoomOutFactor = 1.0/zoomInFactor;\n"
73 "\n"
74 "  var g = svgDoc.getElementById('viewport');\n"
75 "  try\n"
76 "  {\n"
77 "    var bb = g.getBBox(); // this can throw an exception if css { display: none }\n"
78 "    var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);\n"
79 "    var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);\n"
80 "    var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';\n"
81 "    g.setAttribute('transform',a);\n"
82 "  }\n"
83 "  catch(e) {}\n"
84 "}\n"
85 "\n"
86 "/**\n"
87 " * Register handlers\n"
88 " */\n"
89 "function init(evt) \n"
90 "{\n"
91 "  svgDoc = evt.target.ownerDocument;\n"
92 "  if (top.window && top.window.registerShow) // register show function in html doc for dynamic sections\n"
93 "  {\n"
94 "    top.window.registerShow(sectionId,show);\n"
95 "  }\n"
96 "  show();\n"
97 "\n"
98 "  setAttributes(root, {\n"
99 "     \"onmousedown\" : \"handleMouseDown(evt)\",\n"
100 "     \"onmousemove\" : \"handleMouseMove(evt)\",\n"
101 "     \"onmouseup\"   : \"handleMouseUp(evt)\"\n"
102 "  });\n"
103 "\n"
104 "  if (window.addEventListener)\n"
105 "  {\n"
106 "    if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 || \n"
107 "        navigator.userAgent.toLowerCase().indexOf(\"opera\") >= 0 || \n"
108 "        navigator.appVersion.indexOf(\"MSIE\") != -1)\n"
109 "    {\n"
110 "      window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9\n"
111 "    }\n"
112 "    else\n"
113 "    {\n"
114 "      window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others\n"
115 "    }\n"
116 "  }\n"
117 "}\n"
118 "\n"
119 "window.onresize=function()\n"
120 "{\n"
121 "  if (svgDoc) { show(); }\n"
122 "}\n"
123 "\n"
124 "/**\n"
125 " * Instance an SVGPoint object with given event coordinates.\n"
126 " */\n"
127 "function getEventPoint(evt) \n"
128 "{\n"
129 "  var p = root.createSVGPoint();\n"
130 "  p.x = evt.clientX;\n"
131 "  p.y = evt.clientY;\n"
132 "  return p;\n"
133 "}\n"
134 "\n"
135 "/**\n"
136 " * Sets the current transform matrix of an element.\n"
137 " */\n"
138 "function setCTM(element, matrix) \n"
139 "{\n"
140 "  var s = \"matrix(\" + matrix.a + \",\" + matrix.b + \",\" + matrix.c + \",\" + matrix.d + \",\" + matrix.e + \",\" + matrix.f + \")\";\n"
141 "  element.setAttribute(\"transform\", s);\n"
142 "}\n"
143 "\n"
144 "/**\n"
145 " * Sets attributes of an element.\n"
146 " */\n"
147 "function setAttributes(element, attributes)\n"
148 "{\n"
149 "  for (i in attributes)\n"
150 "    element.setAttributeNS(null, i, attributes[i]);\n"
151 "}\n"
152 "\n"
153 "function doZoom(g,point,zoomFactor)\n"
154 "{\n"
155 "  var p = point.matrixTransform(g.getCTM().inverse());\n"
156 "  var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);\n"
157 "  var n = g.getCTM().multiply(k);\n"
158 "  var s = Math.max(n.a,n.d);\n"
159 "  if      (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);\n"
160 "  else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);\n"
161 "  setCTM(g, n);\n"
162 "  stateTf = stateTf.multiply(n.inverse());\n"
163 "}\n"
164 "\n"
165 "/**\n"
166 " * Handle mouse move event.\n"
167 " */\n"
168 "function handleMouseWheel(evt) \n"
169 "{\n"
170 "  if (!evt) evt = window.evt;\n"
171 "  if (!evt.shiftKey) return; // only zoom when shift is pressed\n"
172 "  if (evt.preventDefault) evt.preventDefault();\n"
173 "  evt.returnValue = false;\n"
174 "\n"
175 "  if (state!='pan')\n"
176 "  {\n"
177 "    var delta;\n"
178 "    if (evt.wheelDelta)\n"
179 "    {\n"
180 "      delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari\n"
181 "    }\n"
182 "    else\n"
183 "    {\n"
184 "      delta = evt.detail / -180; // Mozilla\n"
185 "    }\n"
186 "    var svgDoc = evt.target.ownerDocument;\n"
187 "    var g = svgDoc.getElementById(\"viewport\");\n"
188 "    var p = getEventPoint(evt);\n"
189 "    doZoom(g,p,1+delta);\n"
190 "  }\n"
191 "}\n"
192 "\n"
193 "/**\n"
194 " * Handle mouse move event.\n"
195 " */\n"
196 "function handleMouseMove(evt) \n"
197 "{\n"
198 "  if(evt.preventDefault)\n"
199 "    evt.preventDefault();\n"
200 "\n"
201 "  evt.returnValue = false;\n"
202 "\n"
203 "  var g = svgDoc.getElementById(\"viewport\");\n"
204 "\n"
205 "  if (state == 'pan') \n"
206 "  {\n"
207 "    // Pan mode\n"
208 "    var p = getEventPoint(evt).matrixTransform(stateTf);\n"
209 "    setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));\n"
210 "  } \n"
211 "}\n"
212 "\n"
213 "/**\n"
214 " * Handle click event.\n"
215 " */\n"
216 "function handleMouseDown(evt) \n"
217 "{\n"
218 "  if(evt.preventDefault)\n"
219 "    evt.preventDefault();\n"
220 "  evt.returnValue = false;\n"
221 "  var g = svgDoc.getElementById(\"viewport\");\n"
222 "  state = 'pan';\n"
223 "  stateTf = g.getCTM().inverse();\n"
224 "  stateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n"
225 "  g.style.cursor = cursorGrab;\n"
226 "}\n"
227 "\n"
228 "/**\n"
229 " * Handle mouse button release event.\n"
230 " */\n"
231 "function handleMouseUp(evt) \n"
232 "{\n"
233 "  if (evt.preventDefault) evt.preventDefault();\n"
234 "  evt.returnValue = false;\n"
235 "  var g = svgDoc.getElementById(\"viewport\");\n"
236 "  g.style.cursor = \"default\";\n"
237 "  // Quit pan mode\n"
238 "  state = '';\n"
239 "}\n"
240 "\n"
241 "/**\n"
242 " * Dumps a matrix to a string (useful for debug).\n"
243 " */\n"
244 "function dumpMatrix(matrix) \n"
245 "{\n"
246 "  var s = \"[ \" + matrix.a + \", \" + matrix.c + \", \" + matrix.e + \"\\n  \" + matrix.b + \", \" + matrix.d + \", \" + matrix.f + \"\\n  0, 0, 1 ]\";\n"
247 "  return s;\n"
248 "}\n"
249 "\n"
250 "/**\n"
251 " * Handler for pan buttons\n"
252 " */\n"
253 "function handlePan(x,y)\n"
254 "{\n"
255 "  var g = svgDoc.getElementById(\"viewport\");\n"
256 "  setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));\n"
257 "}\n"
258 "\n"
259 "/**\n"
260 " * Handle reset button\n"
261 " */\n"
262 "function handleReset()\n"
263 "{\n"
264 "  show();\n"
265 "}\n"
266 "\n"
267 "/**\n"
268 " * Handler for zoom buttons\n"
269 " */\n"
270 "function handleZoom(evt,direction)\n"
271 "{\n"
272 "  var g = svgDoc.getElementById(\"viewport\");\n"
273 "  var factor = direction=='in' ? zoomInFactor : zoomOutFactor;\n"
274 "  var m = g.getCTM();\n"
275 "  var p = root.createSVGPoint();\n"
276 "  p.x = windowWidth/2;\n"
277 "  p.y = windowHeight/2;\n"
278 "  doZoom(g,p,factor);\n"
279 "}\n"
280 "\n"
281 "function serializeXmlNode(xmlNode) \n"
282 "{\n"
283 "  if (typeof window.XMLSerializer != \"undefined\") {\n"
284 "    return (new window.XMLSerializer()).serializeToString(xmlNode);\n"
285 "  } else if (typeof xmlNode.xml != \"undefined\") {\n"
286 "    return xmlNode.xml;\n"
287 "  }\n"
288 "  return \"\";\n"
289 "}\n"
290 "\n"
291 "/** \n"
292 " * Handler for print function\n"
293 " */\n"
294 "function handlePrint(evt)\n"
295 "{\n"
296 "  evt.returnValue = false;\n"
297 "  var g = svgDoc.getElementById(\"graph\");\n"
298 "  var xs = serializeXmlNode(g);\n"
299 "  try {\n"
300 "    var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+\n"
301 "                        ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');\n"
302 "    var d = w.document;\n"
303 "    d.write('<html xmlns=\"http://www.w3.org/1999/xhtml\" '+\n"
304 "            'xmlns:svg=\"http://www.w3.org/2000/svg\" '+\n"
305 "            'xmlns:xlink=\"http://www.w3.org/1999/xlink\">');\n"
306 "    d.write('<head><title>Print SVG</title></head>');\n"
307 "    d.write('<body style=\"margin: 0px; padding: 0px;\" onload=\"window.print();\">');\n"
308 "    d.write('<div id=\"svg\" style=\"width:'+windowWidth+'px; height:'+windowHeight+'px;\">'+xs+'</div>');\n"
309 "    d.write('</body>');\n"
310 "    d.write('</html>');\n"
311 "    d.close();\n"
312 "  } catch(e) {\n"
313 "    alert('Failed to open popup window needed for printing!\\n'+e.message);\n"
314 "  }\n"
315 "}\n"
316 "\n"
317 "\n"
318 "\n"
319 "\n"