jax.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/jax/output/SVG/jax.js
  6. *
  7. * Implements the SVG OutputJax that displays mathematics using
  8. * SVG (or VML in IE) to position the characters from math fonts
  9. * in their proper locations.
  10. *
  11. * ---------------------------------------------------------------------
  12. *
  13. * Copyright (c) 2011-2017 The MathJax Consortium
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. (function (AJAX,HUB,HTML,SVG) {
  28. var MML;
  29. var isArray = MathJax.Object.isArray;
  30. var SVGNS = "http://www.w3.org/2000/svg";
  31. var XLINKNS = "http://www.w3.org/1999/xlink";
  32. var EVENT, TOUCH, HOVER; // filled in later
  33. //
  34. // Get the URL of the page (for use with xlink:href) when there
  35. // is a <base> element on the page.
  36. //
  37. var SVGURL = (document.getElementsByTagName("base").length === 0) ? "" :
  38. String(document.location).replace(/#.*$/,"");
  39. SVG.Augment({
  40. HFUZZ: 2, // adjustments for height and depth of final svg element
  41. DFUZZ: 2, // to get baselines right (fragile).
  42. config: {
  43. styles: {
  44. ".MathJax_SVG": {
  45. "display": "inline",
  46. "font-style": "normal",
  47. "font-weight": "normal",
  48. "line-height": "normal",
  49. "font-size": "100%",
  50. "font-size-adjust":"none",
  51. "text-indent": 0,
  52. "text-align": "left",
  53. "text-transform": "none",
  54. "letter-spacing": "normal",
  55. "word-spacing": "normal",
  56. "word-wrap": "normal",
  57. "white-space": "nowrap",
  58. "float": "none",
  59. "direction": "ltr",
  60. "max-width": "none", "max-height": "none",
  61. "min-width": 0, "min-height": 0,
  62. border: 0, padding: 0, margin: 0
  63. },
  64. ".MathJax_SVG_Display": {
  65. position: "relative",
  66. display: "block!important",
  67. "text-indent": 0,
  68. "max-width": "none", "max-height": "none",
  69. "min-width": 0, "min-height": 0,
  70. width: "100%"
  71. },
  72. ".MathJax_SVG *": {
  73. transition: "none",
  74. "-webkit-transition": "none",
  75. "-moz-transition": "none",
  76. "-ms-transition": "none",
  77. "-o-transition": "none"
  78. },
  79. ".mjx-svg-href": {
  80. fill: "blue", stroke: "blue"
  81. },
  82. ".MathJax_SVG_Processing": {
  83. visibility: "hidden", position:"absolute", top:0, left:0,
  84. width:0, height: 0, overflow:"hidden", display:"block!important"
  85. },
  86. ".MathJax_SVG_Processed": {display:"none!important"},
  87. ".MathJax_SVG_ExBox": {
  88. display:"block!important", overflow:"hidden",
  89. width:"1px", height:"60ex",
  90. "min-height": 0, "max-height":"none",
  91. padding:0, border: 0, margin: 0
  92. },
  93. ".MathJax_SVG_LineBox": {display: "table!important"},
  94. ".MathJax_SVG_LineBox span": {
  95. display: "table-cell!important",
  96. width: "10000em!important",
  97. "min-width":0, "max-width":"none",
  98. padding:0, border:0, margin:0
  99. },
  100. "#MathJax_SVG_Tooltip": {
  101. position: "absolute", left: 0, top: 0,
  102. width: "auto", height: "auto",
  103. display: "none"
  104. }
  105. }
  106. },
  107. hideProcessedMath: true, // use display:none until all math is processed
  108. fontNames: ["TeX","STIX","STIX-Web","Asana-Math",
  109. "Gyre-Termes","Gyre-Pagella","Latin-Modern","Neo-Euler"],
  110. Config: function () {
  111. this.SUPER(arguments).Config.apply(this,arguments);
  112. var settings = HUB.config.menuSettings, config = this.config, font = settings.font;
  113. if (settings.scale) {config.scale = settings.scale}
  114. if (font && font !== "Auto") {
  115. font = font.replace(/(Local|Web|Image)$/i,"");
  116. font = font.replace(/([a-z])([A-Z])/,"$1-$2");
  117. this.fontInUse = font;
  118. } else {
  119. this.fontInUse = config.font || "TeX";
  120. }
  121. if (this.fontNames.indexOf(this.fontInUse) < 0) {this.fontInUse = "TeX"}
  122. this.fontDir += "/" + this.fontInUse;
  123. if (!this.require) {this.require = []}
  124. this.require.push(this.fontDir+"/fontdata.js");
  125. this.require.push(MathJax.OutputJax.extensionDir+"/MathEvents.js");
  126. },
  127. Startup: function () {
  128. // Set up event handling
  129. EVENT = MathJax.Extension.MathEvents.Event;
  130. TOUCH = MathJax.Extension.MathEvents.Touch;
  131. HOVER = MathJax.Extension.MathEvents.Hover;
  132. this.ContextMenu = EVENT.ContextMenu;
  133. this.Mousedown = EVENT.AltContextMenu;
  134. this.Mouseover = HOVER.Mouseover;
  135. this.Mouseout = HOVER.Mouseout;
  136. this.Mousemove = HOVER.Mousemove;
  137. // Make hidden div for doing tests and storing global SVG <defs>
  138. this.hiddenDiv = HTML.Element("div",{
  139. style:{visibility:"hidden", overflow:"hidden", position:"absolute", top:0,
  140. height:"1px", width: "auto", padding:0, border:0, margin:0,
  141. textAlign:"left", textIndent:0, textTransform:"none",
  142. lineHeight:"normal", letterSpacing:"normal", wordSpacing:"normal"}
  143. });
  144. if (!document.body.firstChild) {document.body.appendChild(this.hiddenDiv)}
  145. else {document.body.insertBefore(this.hiddenDiv,document.body.firstChild)}
  146. this.hiddenDiv = HTML.addElement(this.hiddenDiv,"div",{id:"MathJax_SVG_Hidden"});
  147. // Determine pixels-per-inch and em-size
  148. var div = HTML.addElement(this.hiddenDiv,"div",{style:{width:"5in"}});
  149. this.pxPerInch = div.offsetWidth/5; this.hiddenDiv.removeChild(div);
  150. // Used for measuring text sizes
  151. this.textSVG = this.Element("svg");
  152. // Global defs for font glyphs
  153. BBOX.GLYPH.defs = this.addElement(this.addElement(this.hiddenDiv.parentNode,"svg"),
  154. "defs",{id:"MathJax_SVG_glyphs"});
  155. // Used in preTranslate to get scaling factors
  156. this.ExSpan = HTML.Element("span",
  157. {style:{position:"absolute","font-size-adjust":"none"}},
  158. [["span",{className:"MathJax_SVG_ExBox"}]]
  159. );
  160. // Used in preTranslate to get linebreak width
  161. this.linebreakSpan = HTML.Element("span",{className:"MathJax_SVG_LineBox"},[["span"]]);
  162. // Set up styles
  163. return AJAX.Styles(this.config.styles,["InitializeSVG",this]);
  164. },
  165. //
  166. // Handle initialization that requires styles to be set up
  167. //
  168. InitializeSVG: function () {
  169. //
  170. // Get the default sizes (need styles in place to do this)
  171. //
  172. document.body.appendChild(this.ExSpan);
  173. document.body.appendChild(this.linebreakSpan);
  174. this.defaultEx = this.ExSpan.firstChild.offsetHeight/60;
  175. this.defaultWidth = this.linebreakSpan.firstChild.offsetWidth;
  176. document.body.removeChild(this.linebreakSpan);
  177. document.body.removeChild(this.ExSpan);
  178. },
  179. preTranslate: function (state) {
  180. var scripts = state.jax[this.id], i, m = scripts.length, n,
  181. script, prev, span, div, test, jax, ex, em, maxwidth, relwidth = false, cwidth,
  182. linebreak = this.config.linebreaks.automatic, width = this.config.linebreaks.width;
  183. if (linebreak) {
  184. relwidth = (width.match(/^\s*(\d+(\.\d*)?%\s*)?container\s*$/) != null);
  185. if (relwidth) {width = width.replace(/\s*container\s*/,"")}
  186. else {maxwidth = this.defaultWidth}
  187. if (width === "") {width = "100%"}
  188. } else {maxwidth = 100000} // a big width, so no implicit line breaks
  189. //
  190. // Loop through the scripts
  191. //
  192. for (i = 0; i < m; i++) {
  193. script = scripts[i]; if (!script.parentNode) continue;
  194. //
  195. // Remove any existing output
  196. //
  197. prev = script.previousSibling;
  198. if (prev && String(prev.className).match(/^MathJax(_SVG)?(_Display)?( MathJax(_SVG)?_Process(ing|ed))?$/))
  199. {prev.parentNode.removeChild(prev)}
  200. if (script.MathJax.preview) script.MathJax.preview.style.display = "none";
  201. //
  202. // Add the span, and a div if in display mode,
  203. // then set the role and mark it as being processed
  204. //
  205. jax = script.MathJax.elementJax; if (!jax) continue;
  206. jax.SVG = {
  207. display: (jax.root.Get("display") === "block"),
  208. preview: (jax.SVG||{}).preview // in case typeset calls are interleaved
  209. };
  210. span = div = HTML.Element("span",{
  211. style: {"font-size": this.config.scale+"%", display:"inline-block"},
  212. className:"MathJax_SVG", id:jax.inputID+"-Frame", isMathJax:true, jaxID:this.id,
  213. oncontextmenu:EVENT.Menu, onmousedown: EVENT.Mousedown,
  214. onmouseover:EVENT.Mouseover, onmouseout:EVENT.Mouseout, onmousemove:EVENT.Mousemove,
  215. onclick:EVENT.Click, ondblclick:EVENT.DblClick,
  216. // Added for keyboard accessible menu.
  217. onkeydown: EVENT.Keydown, tabIndex: HUB.getTabOrder(jax)
  218. });
  219. if (HUB.Browser.noContextMenu) {
  220. span.ontouchstart = TOUCH.start;
  221. span.ontouchend = TOUCH.end;
  222. }
  223. if (jax.SVG.display) {
  224. div = HTML.Element("div",{className:"MathJax_SVG_Display"});
  225. div.appendChild(span);
  226. }
  227. div.className += " MathJax_SVG_Processing";
  228. script.parentNode.insertBefore(div,script);
  229. //
  230. // Add the test span for determining scales and linebreak widths
  231. //
  232. script.parentNode.insertBefore(this.ExSpan.cloneNode(true),script);
  233. div.parentNode.insertBefore(this.linebreakSpan.cloneNode(true),div);
  234. }
  235. //
  236. // Determine the scaling factors for each script
  237. // (this only requires one reflow rather than a reflow for each equation)
  238. //
  239. var hidden = [];
  240. for (i = 0; i < m; i++) {
  241. script = scripts[i]; if (!script.parentNode) continue;
  242. test = script.previousSibling; div = test.previousSibling;
  243. jax = script.MathJax.elementJax; if (!jax) continue;
  244. ex = test.firstChild.offsetHeight/60;
  245. cwidth = Math.max(0,(div.previousSibling.firstChild.offsetWidth-2) / this.config.scale * 100);
  246. if (ex === 0 || ex === "NaN") {
  247. // can't read width, so move to hidden div for processing
  248. hidden.push(div);
  249. jax.SVG.isHidden = true;
  250. ex = this.defaultEx; cwidth = this.defaultWidth;
  251. }
  252. if (relwidth) {maxwidth = cwidth}
  253. jax.SVG.ex = ex;
  254. jax.SVG.em = em = ex / SVG.TeX.x_height * 1000; // scale ex to x_height
  255. jax.SVG.cwidth = cwidth/em * 1000;
  256. jax.SVG.lineWidth = (linebreak ? this.length2em(width,1,maxwidth/em*1000) : SVG.BIGDIMEN);
  257. }
  258. for (i = 0, n = hidden.length; i < n; i++) {
  259. this.hiddenDiv.appendChild(hidden[i]);
  260. this.addElement(this.hiddenDiv,"br");
  261. }
  262. //
  263. // Remove the test spans used for determining scales and linebreak widths
  264. //
  265. for (i = 0; i < m; i++) {
  266. script = scripts[i]; if (!script.parentNode) continue;
  267. test = scripts[i].previousSibling; span = test.previousSibling;
  268. jax = scripts[i].MathJax.elementJax; if (!jax) continue;
  269. if (!jax.SVG.isHidden) {span = span.previousSibling}
  270. span.parentNode.removeChild(span);
  271. test.parentNode.removeChild(test);
  272. if (script.MathJax.preview) script.MathJax.preview.style.display = "";
  273. }
  274. //
  275. // Set state variables used for displaying equations in chunks
  276. //
  277. state.SVGeqn = state.SVGlast = 0; state.SVGi = -1;
  278. state.SVGchunk = this.config.EqnChunk;
  279. state.SVGdelay = false;
  280. },
  281. Translate: function (script,state) {
  282. if (!script.parentNode) return;
  283. //
  284. // If we are supposed to do a chunk delay, do it
  285. //
  286. if (state.SVGdelay) {
  287. state.SVGdelay = false;
  288. HUB.RestartAfter(MathJax.Callback.Delay(this.config.EqnChunkDelay));
  289. }
  290. //
  291. // Get the data about the math
  292. //
  293. var jax = script.MathJax.elementJax, math = jax.root, div, span,
  294. localCache = (SVG.config.useFontCache && !SVG.config.useGlobalCache);
  295. if (jax.SVG.isHidden) {
  296. span = document.getElementById(jax.inputID+"-Frame");
  297. div = (jax.SVG.display ? span.parentElement : span);
  298. } else {
  299. div = script.previousSibling;
  300. span = (jax.SVG.display ? (div||{}).firstChild||div : div);
  301. }
  302. if (!div) return;
  303. //
  304. // Set the font metrics
  305. //
  306. this.em = MML.mbase.prototype.em = jax.SVG.em; this.ex = jax.SVG.ex;
  307. this.linebreakWidth = jax.SVG.lineWidth; this.cwidth = jax.SVG.cwidth;
  308. //
  309. // Typeset the math
  310. //
  311. this.mathDiv = div;
  312. span.appendChild(this.textSVG);
  313. if (localCache) {SVG.resetGlyphs()}
  314. this.initSVG(math,span);
  315. math.setTeXclass();
  316. try {math.toSVG(span,div)} catch (err) {
  317. if (err.restart) {while (span.firstChild) {span.removeChild(span.firstChild)}}
  318. if (localCache) {BBOX.GLYPH.n--}
  319. throw err;
  320. }
  321. span.removeChild(this.textSVG);
  322. //
  323. // Put it in place, and remove the processing marker
  324. //
  325. if (jax.SVG.isHidden) {script.parentNode.insertBefore(div,script)}
  326. div.className = div.className.split(/ /)[0];
  327. //
  328. // Check if we are hiding the math until more is processed
  329. //
  330. if (this.hideProcessedMath) {
  331. //
  332. // Hide the math and don't let its preview be removed
  333. //
  334. div.className += " MathJax_SVG_Processed";
  335. if (script.MathJax.preview) {
  336. jax.SVG.preview = script.MathJax.preview;
  337. delete script.MathJax.preview;
  338. }
  339. //
  340. // Check if we should show this chunk of equations
  341. //
  342. state.SVGeqn += (state.i - state.SVGi); state.SVGi = state.i;
  343. if (state.SVGeqn >= state.SVGlast + state.SVGchunk) {
  344. this.postTranslate(state,true);
  345. state.SVGchunk = Math.floor(state.SVGchunk*this.config.EqnChunkFactor);
  346. state.SVGdelay = true; // delay if there are more scripts
  347. }
  348. }
  349. },
  350. postTranslate: function (state,partial) {
  351. var scripts = state.jax[this.id];
  352. if (!this.hideProcessedMath) return;
  353. //
  354. // Reveal this chunk of math
  355. //
  356. for (var i = state.SVGlast, m = state.SVGeqn; i < m; i++) {
  357. var script = scripts[i];
  358. if (script && script.MathJax.elementJax) {
  359. //
  360. // Remove the processed marker
  361. //
  362. script.previousSibling.className = script.previousSibling.className.split(/ /)[0];
  363. var data = script.MathJax.elementJax.SVG;
  364. //
  365. // Remove the preview, if any
  366. //
  367. if (data.preview) {
  368. data.preview.innerHTML = "";
  369. data.preview.style.display = "none";
  370. script.MathJax.preview = data.preview;
  371. delete data.preview;
  372. }
  373. }
  374. }
  375. //
  376. // Save our place so we know what is revealed
  377. //
  378. state.SVGlast = state.SVGeqn;
  379. },
  380. resetGlyphs: function (reset) {
  381. if (this.config.useFontCache) {
  382. var GLYPH = BBOX.GLYPH;
  383. if (this.config.useGlobalCache) {
  384. GLYPH.defs = document.getElementById("MathJax_SVG_glyphs");
  385. GLYPH.defs.innerHTML = "";
  386. } else {
  387. GLYPH.defs = this.Element("defs");
  388. GLYPH.n++;
  389. }
  390. GLYPH.glyphs = {};
  391. if (reset) {GLYPH.n = 0}
  392. }
  393. },
  394. //
  395. // Return the containing HTML element rather than the SVG element, since
  396. // most browsers can't position to an SVG element properly.
  397. //
  398. hashCheck: function (target) {
  399. if (target && target.nodeName.toLowerCase() === "g")
  400. {do {target = target.parentNode} while (target && target.firstChild.nodeName !== "svg")}
  401. return target;
  402. },
  403. getJaxFromMath: function (math) {
  404. if (math.parentNode.className.match(/MathJax_SVG_Display/)) {math = math.parentNode}
  405. do {math = math.nextSibling} while (math && math.nodeName.toLowerCase() !== "script");
  406. return HUB.getJaxFor(math);
  407. },
  408. getHoverSpan: function (jax,math) {
  409. math.style.position = "relative"; // make sure inline containers have position set
  410. return math.firstChild;
  411. },
  412. getHoverBBox: function (jax,span,math) {
  413. var bbox = EVENT.getBBox(span.parentNode);
  414. bbox.h += 2; bbox.d -= 2; // bbox seems to be a bit off, so compensate (FIXME)
  415. return bbox;
  416. },
  417. Zoom: function (jax,span,math,Mw,Mh) {
  418. //
  419. // Re-render at larger size
  420. //
  421. span.className = "MathJax_SVG";
  422. //
  423. // get em size (taken from this.preTranslate)
  424. //
  425. var emex = span.appendChild(this.ExSpan.cloneNode(true));
  426. var ex = emex.firstChild.offsetHeight/60;
  427. this.em = MML.mbase.prototype.em = ex / SVG.TeX.x_height * 1000; this.ex = ex;
  428. this.linebreakWidth = jax.SVG.lineWidth; this.cwidth = jax.SVG.cwidth;
  429. emex.parentNode.removeChild(emex);
  430. span.appendChild(this.textSVG);
  431. this.mathDIV = span; this.zoomScale = parseInt(HUB.config.menuSettings.zscale) / 100;
  432. var tw = jax.root.data[0].SVGdata.tw; if (tw && tw < this.cwidth) this.cwidth = tw;
  433. this.idPostfix = "-zoom"; jax.root.toSVG(span,span); this.idPostfix = "";
  434. this.zoomScale = 1;
  435. span.removeChild(this.textSVG);
  436. //
  437. // Don't allow overlaps on any edge
  438. //
  439. var svg = span.getElementsByTagName("svg")[0].style;
  440. svg.marginTop = svg.marginRight = svg.marginLeft = 0;
  441. if (svg.marginBottom.charAt(0) === "-")
  442. span.style.marginBottom = svg.marginBottom.substr(1);
  443. if (this.operaZoomRefresh)
  444. {setTimeout(function () {span.firstChild.style.border="1px solid transparent"},1)}
  445. //
  446. // WebKit bug (issue #749)
  447. //
  448. if (span.offsetWidth < span.firstChild.offsetWidth) {
  449. span.style.minWidth = span.firstChild.offsetWidth + "px";
  450. math.style.minWidth = math.firstChild.offsetWidth + "px";
  451. }
  452. //
  453. // Get height and width of zoomed math and original math
  454. //
  455. span.style.position = math.style.position = "absolute";
  456. var zW = span.offsetWidth, zH = span.offsetHeight,
  457. mH = math.offsetHeight, mW = math.offsetWidth;
  458. span.style.position = math.style.position = "";
  459. //
  460. return {Y:-EVENT.getBBox(span).h, mW:mW, mH:mH, zW:zW, zH:zH};
  461. },
  462. initSVG: function (math,span) {},
  463. Remove: function (jax) {
  464. var span = document.getElementById(jax.inputID+"-Frame");
  465. if (span) {
  466. if (jax.SVG.display) {span = span.parentNode}
  467. span.parentNode.removeChild(span);
  468. }
  469. delete jax.SVG;
  470. },
  471. Em: function (m) {
  472. if (Math.abs(m) < .0006) return "0";
  473. return m.toFixed(3).replace(/\.?0+$/,"") + "em";
  474. },
  475. Ex: function (m) {
  476. m = m / this.TeX.x_height;
  477. if (Math.abs(m) < .0006) return "0";
  478. return m.toFixed(3).replace(/\.?0+$/,"") + "ex";
  479. },
  480. Percent: function (m) {
  481. return (100*m).toFixed(1).replace(/\.?0+$/,"") + "%";
  482. },
  483. Fixed: function (m,n) {
  484. if (Math.abs(m) < .0006) return "0";
  485. return m.toFixed(n||3).replace(/\.?0+$/,"");
  486. },
  487. length2em: function (length,mu,size) {
  488. if (typeof(length) !== "string") {length = length.toString()}
  489. if (length === "") {return ""}
  490. if (length === MML.SIZE.NORMAL) {return 1000}
  491. if (length === MML.SIZE.BIG) {return 2000}
  492. if (length === MML.SIZE.SMALL) {return 710}
  493. if (length === "infinity") {return SVG.BIGDIMEN}
  494. if (length.match(/mathspace$/)) {return 1000*SVG.MATHSPACE[length]}
  495. var emFactor = (this.zoomScale || 1) / SVG.em;
  496. var match = length.match(/^\s*([-+]?(?:\.\d+|\d+(?:\.\d*)?))?(pt|em|ex|mu|px|pc|in|mm|cm|%)?/);
  497. var m = parseFloat(match[1]||"1") * 1000, unit = match[2];
  498. if (size == null) {size = 1000}; if (mu == null) {mu = 1}
  499. if (unit === "em") {return m}
  500. if (unit === "ex") {return m * SVG.TeX.x_height/1000}
  501. if (unit === "%") {return m / 100 * size / 1000}
  502. if (unit === "px") {return m * emFactor}
  503. if (unit === "pt") {return m / 10} // 10 pt to an em
  504. if (unit === "pc") {return m * 1.2} // 12 pt to a pc
  505. if (unit === "in") {return m * this.pxPerInch * emFactor}
  506. if (unit === "cm") {return m * this.pxPerInch * emFactor / 2.54} // 2.54 cm to an inch
  507. if (unit === "mm") {return m * this.pxPerInch * emFactor / 25.4} // 10 mm to a cm
  508. if (unit === "mu") {return m / 18 * mu}
  509. return m*size / 1000; // relative to given size (or 1em as default)
  510. },
  511. thickness2em: function (length,mu) {
  512. var thick = SVG.TeX.rule_thickness;
  513. if (length === MML.LINETHICKNESS.MEDIUM) {return thick}
  514. if (length === MML.LINETHICKNESS.THIN) {return .67*thick}
  515. if (length === MML.LINETHICKNESS.THICK) {return 1.67*thick}
  516. return this.length2em(length,mu,thick);
  517. },
  518. getPadding: function (styles) {
  519. var padding = {top:0, right:0, bottom:0, left:0}, has = false;
  520. for (var id in padding) {if (padding.hasOwnProperty(id)) {
  521. var pad = styles["padding"+id.charAt(0).toUpperCase()+id.substr(1)];
  522. if (pad) {padding[id] = this.length2em(pad); has = true;}
  523. }}
  524. return (has ? padding : false);
  525. },
  526. getBorders: function (styles) {
  527. var border = {top:0, right:0, bottom:0, left:0}, has = false;
  528. for (var id in border) {if (border.hasOwnProperty(id)) {
  529. var ID = "border"+id.charAt(0).toUpperCase()+id.substr(1);
  530. var style = styles[ID+"Style"];
  531. if (style && style !== "none") {
  532. has = true;
  533. border[id] = this.length2em(styles[ID+"Width"]);
  534. border[id+"Style"] = styles[ID+"Style"];
  535. border[id+"Color"] = styles[ID+"Color"];
  536. if (border[id+"Color"] === "initial") {border[id+"Color"] = ""}
  537. } else {delete border[id]}
  538. }}
  539. return (has ? border : false);
  540. },
  541. Element: function (type,def) {
  542. var obj = (typeof(type) === "string" ? document.createElementNS(SVGNS,type) : type);
  543. obj.isMathJax = true;
  544. if (def) {for (var id in def) {if (def.hasOwnProperty(id)) {obj.setAttribute(id,def[id].toString())}}}
  545. return obj;
  546. },
  547. addElement: function (parent,type,def) {return parent.appendChild(this.Element(type,def))},
  548. TextNode: HTML.TextNode,
  549. addText: HTML.addText,
  550. ucMatch: HTML.ucMatch,
  551. HandleVariant: function (variant,scale,text) {
  552. var svg = BBOX.G();
  553. var n, N, c, font, VARIANT, i, m, id, M, RANGES;
  554. if (!variant) {variant = this.FONTDATA.VARIANT[MML.VARIANT.NORMAL]}
  555. if (variant.forceFamily) {
  556. text = BBOX.TEXT(scale,text,variant.font);
  557. if (variant.h != null) {text.h = variant.h}; if (variant.d != null) {text.d = variant.d}
  558. svg.Add(text); text = "";
  559. }
  560. VARIANT = variant;
  561. for (i = 0, m = text.length; i < m; i++) {
  562. variant = VARIANT;
  563. n = text.charCodeAt(i); c = text.charAt(i);
  564. if (n >= 0xD800 && n < 0xDBFF) {
  565. i++; n = (((n-0xD800)<<10)+(text.charCodeAt(i)-0xDC00))+0x10000;
  566. if (this.FONTDATA.RemapPlane1) {
  567. var nv = this.FONTDATA.RemapPlane1(n,variant);
  568. n = nv.n; variant = nv.variant;
  569. }
  570. } else {
  571. RANGES = this.FONTDATA.RANGES;
  572. for (id = 0, M = RANGES.length; id < M; id++) {
  573. if (RANGES[id].name === "alpha" && variant.noLowerCase) continue;
  574. N = variant["offset"+RANGES[id].offset];
  575. if (N && n >= RANGES[id].low && n <= RANGES[id].high) {
  576. if (RANGES[id].remap && RANGES[id].remap[n]) {
  577. n = N + RANGES[id].remap[n];
  578. } else {
  579. n = n - RANGES[id].low + N;
  580. if (RANGES[id].add) {n += RANGES[id].add}
  581. }
  582. if (variant["variant"+RANGES[id].offset])
  583. {variant = this.FONTDATA.VARIANT[variant["variant"+RANGES[id].offset]]}
  584. break;
  585. }
  586. }
  587. }
  588. if (variant.remap && variant.remap[n]) {
  589. n = variant.remap[n];
  590. if (variant.remap.variant) {variant = this.FONTDATA.VARIANT[variant.remap.variant]}
  591. } else if (this.FONTDATA.REMAP[n] && !variant.noRemap) {
  592. n = this.FONTDATA.REMAP[n];
  593. }
  594. if (isArray(n)) {variant = this.FONTDATA.VARIANT[n[1]]; n = n[0]}
  595. if (typeof(n) === "string") {
  596. text = n+text.substr(i+1);
  597. m = text.length; i = -1;
  598. continue;
  599. }
  600. font = this.lookupChar(variant,n); c = font[n];
  601. if (c) {
  602. if ((c[5] && c[5].space) || (c[5] === "" && c[0]+c[1] === 0)) {svg.w += c[2]} else {
  603. c = [scale,font.id+"-"+n.toString(16).toUpperCase()].concat(c);
  604. svg.Add(BBOX.GLYPH.apply(BBOX,c),svg.w,0);
  605. }
  606. } else if (this.FONTDATA.DELIMITERS[n]) {
  607. c = this.createDelimiter(n,0,1,font);
  608. svg.Add(c,svg.w,(this.FONTDATA.DELIMITERS[n].dir === "V" ? c.d: 0));
  609. } else {
  610. if (n <= 0xFFFF) {c = String.fromCharCode(n)} else {
  611. N = n - 0x10000;
  612. c = String.fromCharCode((N>>10)+0xD800)
  613. + String.fromCharCode((N&0x3FF)+0xDC00);
  614. }
  615. var box = BBOX.TEXT(scale*100/SVG.config.scale,c,{
  616. "font-family":variant.defaultFamily||SVG.config.undefinedFamily,
  617. "font-style":(variant.italic?"italic":""),
  618. "font-weight":(variant.bold?"bold":"")
  619. })
  620. if (variant.h != null) {box.h = variant.h}; if (variant.d != null) {box.d = variant.d}
  621. c = BBOX.G(); c.Add(box); svg.Add(c,svg.w,0);
  622. HUB.signal.Post(["SVG Jax - unknown char",n,variant]);
  623. }
  624. }
  625. if (text.length == 1 && font.skew && font.skew[n]) {svg.skew = font.skew[n]*1000}
  626. if (svg.element.childNodes.length === 1 && !svg.element.firstChild.getAttribute("x")) {
  627. svg.element = svg.element.firstChild;
  628. svg.removeable = false; svg.scale = scale;
  629. }
  630. return svg;
  631. },
  632. lookupChar: function (variant,n) {
  633. var i, m;
  634. if (!variant.FONTS) {
  635. var FONTS = this.FONTDATA.FONTS;
  636. var fonts = (variant.fonts || this.FONTDATA.VARIANT.normal.fonts);
  637. if (!(fonts instanceof Array)) {fonts = [fonts]}
  638. if (variant.fonts != fonts) {variant.fonts = fonts}
  639. variant.FONTS = [];
  640. for (i = 0, m = fonts.length; i < m; i++) {
  641. if (FONTS[fonts[i]]) {variant.FONTS.push(FONTS[fonts[i]])}
  642. }
  643. }
  644. for (i = 0, m = variant.FONTS.length; i < m; i++) {
  645. var font = variant.FONTS[i];
  646. if (typeof(font) === "string") {delete variant.FONTS; this.loadFont(font)}
  647. if (font[n]) {return font} else {this.findBlock(font,n)}
  648. }
  649. return {id:"unknown"};
  650. },
  651. findBlock: function (font,c) {
  652. if (font.Ranges) {
  653. // FIXME: do binary search?
  654. for (var i = 0, m = font.Ranges.length; i < m; i++) {
  655. if (c < font.Ranges[i][0]) return;
  656. if (c <= font.Ranges[i][1]) {
  657. var file = font.Ranges[i][2];
  658. for (var j = font.Ranges.length-1; j >= 0; j--)
  659. {if (font.Ranges[j][2] == file) {font.Ranges.splice(j,1)}}
  660. this.loadFont(font.directory+"/"+file+".js");
  661. }
  662. }
  663. }
  664. },
  665. loadFont: function (file) {
  666. HUB.RestartAfter(AJAX.Require(this.fontDir+"/"+file));
  667. },
  668. createDelimiter: function (code,HW,scale,font) {
  669. if (!scale) {scale = 1};
  670. var svg = BBOX.G();
  671. if (!code) {
  672. svg.Clean(); delete svg.element;
  673. svg.w = svg.r = this.TeX.nulldelimiterspace * scale;
  674. return svg;
  675. }
  676. if (!(HW instanceof Array)) {HW = [HW,HW]}
  677. var hw = HW[1]; HW = HW[0];
  678. var delim = {alias: code};
  679. while (delim.alias) {
  680. code = delim.alias; delim = this.FONTDATA.DELIMITERS[code];
  681. if (!delim) {delim = {HW: [0,this.FONTDATA.VARIANT[MML.VARIANT.NORMAL]]}}
  682. }
  683. if (delim.load) {HUB.RestartAfter(AJAX.Require(this.fontDir+"/fontdata-"+delim.load+".js"))}
  684. for (var i = 0, m = delim.HW.length; i < m; i++) {
  685. if (delim.HW[i][0]*scale >= HW-10-SVG.config.blacker || (i == m-1 && !delim.stretch)) {
  686. if (delim.HW[i][2]) {scale *= delim.HW[i][2]}
  687. if (delim.HW[i][3]) {code = delim.HW[i][3]}
  688. return this.createChar(scale,[code,delim.HW[i][1]],font).With({stretched: true});
  689. }
  690. }
  691. if (delim.stretch) {this["extendDelimiter"+delim.dir](svg,hw,delim.stretch,scale,font)}
  692. return svg;
  693. },
  694. createChar: function (scale,data,font) {
  695. var text = "", variant = {fonts: [data[1]], noRemap:true};
  696. if (font && font === MML.VARIANT.BOLD) {variant.fonts = [data[1]+"-bold",data[1]]}
  697. if (typeof(data[1]) !== "string") {variant = data[1]}
  698. if (data[0] instanceof Array) {
  699. for (var i = 0, m = data[0].length; i < m; i++) {text += String.fromCharCode(data[0][i])}
  700. } else {text = String.fromCharCode(data[0])}
  701. if (data[4]) {scale = scale*data[4]}
  702. var svg = this.HandleVariant(variant,scale,text);
  703. if (data[2]) {svg.x = data[2]*1000}
  704. if (data[3]) {svg.y = data[3]*1000}
  705. if (data[5]) {svg.h += data[5]*1000}
  706. if (data[6]) {svg.d += data[6]*1000}
  707. return svg;
  708. },
  709. extendDelimiterV: function (svg,H,delim,scale,font) {
  710. var top = this.createChar(scale,(delim.top||delim.ext),font);
  711. var bot = this.createChar(scale,(delim.bot||delim.ext),font);
  712. var h = top.h + top.d + bot.h + bot.d;
  713. var y = -top.h; svg.Add(top,0,y); y -= top.d;
  714. if (delim.mid) {var mid = this.createChar(scale,delim.mid,font); h += mid.h + mid.d}
  715. if (delim.min && H < h*delim.min) {H = h*delim.min}
  716. if (H > h) {
  717. var ext = this.createChar(scale,delim.ext,font);
  718. var k = (delim.mid ? 2 : 1), eH = (H-h) / k, s = (eH+100) / (ext.h+ext.d);
  719. while (k-- > 0) {
  720. var g = SVG.Element("g",{transform:"translate("+ext.y+","+(y-s*ext.h+50+ext.y)+") scale(1,"+s+")"});
  721. g.appendChild(ext.element.cloneNode(false)); svg.element.appendChild(g); y -= eH;
  722. if (delim.mid && k) {svg.Add(mid,0,y-mid.h); y -= (mid.h+mid.d)}
  723. }
  724. } else if (delim.mid) {
  725. y += (h - H)/2; svg.Add(mid,0,y-mid.h); y += -(mid.h + mid.d) + (h - H)/2;
  726. } else {
  727. y += (h - H);
  728. }
  729. svg.Add(bot,0,y-bot.h); svg.Clean();
  730. svg.scale = scale;
  731. svg.isMultiChar = true;
  732. },
  733. extendDelimiterH: function (svg,W,delim,scale,font) {
  734. var left = this.createChar(scale,(delim.left||delim.rep),font);
  735. var right = this.createChar(scale,(delim.right||delim.rep),font);
  736. svg.Add(left,-left.l,0);
  737. var w = (left.r - left.l) + (right.r - right.l), x = left.r - left.l;
  738. if (delim.mid) {var mid = this.createChar(scale,delim.mid,font); w += mid.w}
  739. if (delim.min && W < w*delim.min) {W = w*delim.min}
  740. if (W > w) {
  741. var rep = this.createChar(scale,delim.rep,font), fuzz = delim.fuzz || 0;
  742. var k = (delim.mid ? 2 : 1), rW = (W-w) / k, s = (rW+fuzz) / (rep.r-rep.l);
  743. while (k-- > 0) {
  744. var g = SVG.Element("g",{transform:"translate("+(x-fuzz/2-s*rep.l+rep.x)+","+rep.y+") scale("+s+",1)"});
  745. g.appendChild(rep.element.cloneNode(false)); svg.element.appendChild(g); x += rW;
  746. if (delim.mid && k) {svg.Add(mid,x,0); x += mid.w}
  747. }
  748. } else if (delim.mid) {
  749. x -= (w - W)/2; svg.Add(mid,x,0); x += mid.w - (w - W)/2;
  750. } else {
  751. x -= (w - W);
  752. }
  753. svg.Add(right,x-right.l,0); svg.Clean();
  754. svg.scale = scale;
  755. svg.isMultiChar = true;
  756. },
  757. MATHSPACE: {
  758. veryverythinmathspace: 1/18,
  759. verythinmathspace: 2/18,
  760. thinmathspace: 3/18,
  761. mediummathspace: 4/18,
  762. thickmathspace: 5/18,
  763. verythickmathspace: 6/18,
  764. veryverythickmathspace: 7/18,
  765. negativeveryverythinmathspace: -1/18,
  766. negativeverythinmathspace: -2/18,
  767. negativethinmathspace: -3/18,
  768. negativemediummathspace: -4/18,
  769. negativethickmathspace: -5/18,
  770. negativeverythickmathspace: -6/18,
  771. negativeveryverythickmathspace: -7/18
  772. },
  773. //
  774. // Units are em/1000 so quad is 1em
  775. //
  776. TeX: {
  777. x_height: 430.554,
  778. quad: 1000,
  779. num1: 676.508,
  780. num2: 393.732,
  781. num3: 443.73,
  782. denom1: 685.951,
  783. denom2: 344.841,
  784. sup1: 412.892,
  785. sup2: 362.892,
  786. sup3: 288.888,
  787. sub1: 150,
  788. sub2: 247.217,
  789. sup_drop: 386.108,
  790. sub_drop: 50,
  791. delim1: 2390,
  792. delim2: 1000,
  793. axis_height: 250,
  794. rule_thickness: 60,
  795. big_op_spacing1: 111.111,
  796. big_op_spacing2: 166.666,
  797. big_op_spacing3: 200,
  798. big_op_spacing4: 600,
  799. big_op_spacing5: 100,
  800. scriptspace: 100,
  801. nulldelimiterspace: 120,
  802. delimiterfactor: 901,
  803. delimitershortfall: 300,
  804. min_rule_thickness: 1.25, // in pixels
  805. min_root_space: 1.5 // in pixels
  806. },
  807. BIGDIMEN: 10000000,
  808. NBSP: "\u00A0"
  809. });
  810. var BBOX = SVG.BBOX = MathJax.Object.Subclass({
  811. type: "g", removeable: true,
  812. Init: function (def) {
  813. this.h = this.d = -SVG.BIGDIMEN; this.H = this.D = 0;
  814. this.w = this.r = 0; this.l = SVG.BIGDIMEN;
  815. this.x = this.y = 0; this.scale = 1; this.n = 0;
  816. if (this.type) {this.element = SVG.Element(this.type,def)}
  817. },
  818. With: function (def) {return HUB.Insert(this,def)},
  819. Add: function (svg,dx,dy,forcew,infront) {
  820. if (dx) {svg.x += dx}; if (dy) {svg.y += dy};
  821. if (svg.element) {
  822. if (svg.removeable && svg.element.childNodes.length === 1 && svg.n === 1) {
  823. var child = svg.element.firstChild, nodeName = child.nodeName.toLowerCase();
  824. if (nodeName === "use" || nodeName === "rect") {
  825. svg.element = child; svg.scale = svg.childScale;
  826. var x = svg.childX, y = svg.childY;
  827. svg.x += x; svg.y += y;
  828. svg.h -= y; svg.d += y; svg.H -= y; svg.D +=y;
  829. svg.w -= x; svg.r -= x; svg.l += x;
  830. svg.removeable = false;
  831. child.setAttribute("x",Math.floor(svg.x/svg.scale));
  832. child.setAttribute("y",Math.floor(svg.y/svg.scale));
  833. }
  834. }
  835. if (Math.abs(svg.x) < 1 && Math.abs(svg.y) < 1) {
  836. svg.remove = svg.removeable;
  837. } else {
  838. nodeName = svg.element.nodeName.toLowerCase();
  839. if (nodeName === "g") {
  840. if (!svg.element.firstChild) {svg.remove = svg.removeable}
  841. else {svg.element.setAttribute("transform","translate("+Math.floor(svg.x)+","+Math.floor(svg.y)+")")}
  842. } else if (nodeName === "line" || nodeName === "polygon" ||
  843. nodeName === "path" || nodeName === "a") {
  844. var transform = svg.element.getAttribute("transform") || "";
  845. if (transform) transform = " "+transform;
  846. transform = "translate("+Math.floor(svg.x)+","+Math.floor(svg.y)+")"+transform;
  847. svg.element.setAttribute("transform",transform);
  848. } else {
  849. svg.element.setAttribute("x",Math.floor(svg.x/svg.scale));
  850. svg.element.setAttribute("y",Math.floor(svg.y/svg.scale));
  851. }
  852. }
  853. if (svg.remove) {
  854. this.n += svg.n;
  855. while (svg.element.firstChild) {
  856. if (infront && this.element.firstChild) {
  857. this.element.insertBefore(svg.element.firstChild,this.element.firstChild);
  858. } else {
  859. this.element.appendChild(svg.element.firstChild);
  860. }
  861. }
  862. } else {
  863. if (infront) {this.element.insertBefore(svg.element,this.element.firstChild)}
  864. else {this.element.appendChild(svg.element)}
  865. }
  866. delete svg.element;
  867. }
  868. if (svg.hasIndent) {this.hasIndent = svg.hasIndent}
  869. if (svg.tw != null) {this.tw = svg.tw}
  870. if (svg.d - svg.y > this.d) {this.d = svg.d - svg.y; if (this.d > this.D) {this.D = this.d}}
  871. if (svg.y + svg.h > this.h) {this.h = svg.y + svg.h; if (this.h > this.H) {this.H = this.h}}
  872. if (svg.D - svg.y > this.D) {this.D = svg.D - svg.y}
  873. if (svg.y + svg.H > this.H) {this.H = svg.y + svg.H}
  874. if (svg.x + svg.l < this.l) {this.l = svg.x + svg.l}
  875. if (svg.x + svg.r > this.r) {this.r = svg.x + svg.r}
  876. if (forcew || svg.x + svg.w + (svg.X||0) > this.w) {this.w = svg.x + svg.w + (svg.X||0)}
  877. this.childScale = svg.scale; this.childX = svg.x; this.childY = svg.y; this.n++;
  878. return svg;
  879. },
  880. Align: function (svg,align,dx,dy,shift) {
  881. dx = ({left: dx, center: (this.w - svg.w)/2, right: this.w - svg.w - dx})[align] || 0;
  882. var w = this.w; this.Add(svg,dx+(shift||0),dy); this.w = w;
  883. },
  884. Clean: function () {
  885. if (this.h === -SVG.BIGDIMEN) {this.h = this.d = this.l = 0}
  886. return this;
  887. }
  888. });
  889. BBOX.ROW = BBOX.Subclass({
  890. Init: function () {
  891. this.SUPER(arguments).Init.call(this);
  892. this.svg = []; this.sh = this.sd = 0;
  893. },
  894. Check: function (data) {
  895. var svg = data.toSVG(); this.svg.push(svg);
  896. if (data.SVGcanStretch("Vertical")) {svg.mml = data}
  897. if (svg.h > this.sh) {this.sh = svg.h}
  898. if (svg.d > this.sd) {this.sd = svg.d}
  899. },
  900. Stretch: function () {
  901. for (var i = 0, m = this.svg.length; i < m; i++)
  902. {
  903. var svg = this.svg[i], mml = svg.mml;
  904. if (mml) {
  905. if (mml.forceStretch || mml.SVGdata.h !== this.sh || mml.SVGdata.d !== this.sd) {
  906. svg = mml.SVGstretchV(this.sh,this.sd);
  907. }
  908. mml.SVGdata.HW = this.sh; mml.SVGdata.D = this.sd;
  909. }
  910. if (svg.ic) {this.ic = svg.ic} else {delete this.ic}
  911. this.Add(svg,this.w,0,true);
  912. }
  913. delete this.svg;
  914. }
  915. });
  916. BBOX.RECT = BBOX.Subclass({
  917. type: "rect", removeable: false,
  918. Init: function (h,d,w,def) {
  919. if (def == null) {def = {stroke:"none"}}
  920. def.width = Math.floor(w); def.height = Math.floor(h+d);
  921. this.SUPER(arguments).Init.call(this,def);
  922. this.w = this.r = w; this.h = this.H = h+d; this.d = this.D = this.l = 0; this.y = -d;
  923. }
  924. });
  925. BBOX.FRAME = BBOX.Subclass({
  926. type: "rect", removeable: false,
  927. Init: function (h,d,w,t,dash,color,def) {
  928. if (def == null) {def = {}}; def.fill = "none";
  929. def["stroke-width"] = SVG.Fixed(t,2);
  930. def.width = Math.floor(w-t); def.height = Math.floor(h+d-t);
  931. def.transform = "translate("+Math.floor(t/2)+","+Math.floor(-d+t/2)+")";
  932. if (dash === "dashed")
  933. {def["stroke-dasharray"] = [Math.floor(6*SVG.em),Math.floor(6*SVG.em)].join(" ")}
  934. this.SUPER(arguments).Init.call(this,def);
  935. this.w = this.r = w; this.h = this.H = h;
  936. this.d = this.D = d; this.l = 0;
  937. }
  938. });
  939. BBOX.HLINE = BBOX.Subclass({
  940. type: "line", removeable: false,
  941. Init: function (w,t,dash,color,def) {
  942. if (def == null) {def = {"stroke-linecap":"square"}}
  943. if (color && color !== "") {def.stroke = color}
  944. def["stroke-width"] = SVG.Fixed(t,2);
  945. def.x1 = def.y1 = def.y2 = Math.floor(t/2); def.x2 = Math.floor(w-t/2);
  946. if (dash === "dashed") {
  947. var n = Math.floor(Math.max(0,w-t)/(6*t)), m = Math.floor(Math.max(0,w-t)/(2*n+1));
  948. def["stroke-dasharray"] = m+" "+m;
  949. }
  950. if (dash === "dotted") {
  951. def["stroke-dasharray"] = [1,Math.max(150,Math.floor(2*t))].join(" ");
  952. def["stroke-linecap"] = "round";
  953. }
  954. this.SUPER(arguments).Init.call(this,def);
  955. this.w = this.r = w; this.l = 0; this.h = this.H = t; this.d = this.D = 0;
  956. }
  957. });
  958. BBOX.VLINE = BBOX.Subclass({
  959. type: "line", removeable: false,
  960. Init: function (h,t,dash,color,def) {
  961. if (def == null) {def = {"stroke-linecap":"square"}}
  962. if (color && color !== "") {def.stroke = color}
  963. def["stroke-width"] = SVG.Fixed(t,2);
  964. def.x1 = def.x2 = def.y1 = Math.floor(t/2); def.y2 = Math.floor(h-t/2);
  965. if (dash === "dashed") {
  966. var n = Math.floor(Math.max(0,h-t)/(6*t)), m = Math.floor(Math.max(0,h-t)/(2*n+1));
  967. def["stroke-dasharray"] = m+" "+m;
  968. }
  969. if (dash === "dotted") {
  970. def["stroke-dasharray"] = [1,Math.max(150,Math.floor(2*t))].join(" ");
  971. def["stroke-linecap"] = "round";
  972. }
  973. this.SUPER(arguments).Init.call(this,def);
  974. this.w = this.r = t; this.l = 0; this.h = this.H = h; this.d = this.D = 0;
  975. }
  976. });
  977. BBOX.TEXT = BBOX.Subclass({
  978. type: "text", removeable: false,
  979. Init: function (scale,text,def) {
  980. if (!def) {def = {}}; def.stroke = "none";
  981. if (def["font-style"] === "") delete def["font-style"];
  982. if (def["font-weight"] === "") delete def["font-weight"];
  983. this.SUPER(arguments).Init.call(this,def);
  984. SVG.addText(this.element,text);
  985. SVG.textSVG.appendChild(this.element);
  986. var bbox = this.element.getBBox();
  987. SVG.textSVG.removeChild(this.element);
  988. scale *= 1000/SVG.em;
  989. this.element.setAttribute("transform","scale("+SVG.Fixed(scale)+") matrix(1 0 0 -1 0 0)");
  990. this.w = this.r = bbox.width*scale; this.l = 0;
  991. this.h = this.H = -bbox.y*scale;
  992. this.d = this.D = (bbox.height + bbox.y)*scale;
  993. }
  994. });
  995. BBOX.G = BBOX;
  996. BBOX.NULL = BBOX.Subclass({
  997. Init: function () {
  998. this.SUPER(arguments).Init.apply(this,arguments);
  999. this.Clean();
  1000. }
  1001. });
  1002. BBOX.GLYPH = BBOX.Subclass({
  1003. type: "path", removeable: false,
  1004. Init: function (scale,id,h,d,w,l,r,p) {
  1005. var def, t = SVG.config.blacker, GLYPH = BBOX.GLYPH;
  1006. var cache = SVG.config.useFontCache;
  1007. var transform = (scale === 1 ? null : "scale("+SVG.Fixed(scale)+")");
  1008. if (cache && !SVG.config.useGlobalCache) {id = "E"+GLYPH.n+"-"+id}
  1009. if (!cache || !GLYPH.glyphs[id]) {
  1010. def = {"stroke-width":t};
  1011. if (cache) {def.id = id} else if (transform) {def.transform = transform}
  1012. def.d = (p ? "M"+p+"Z" : "");
  1013. this.SUPER(arguments).Init.call(this,def);
  1014. if (cache) {GLYPH.defs.appendChild(this.element); GLYPH.glyphs[id] = true;}
  1015. }
  1016. if (cache) {
  1017. def = {}; if (transform) {def.transform = transform}
  1018. this.element = SVG.Element("use",def);
  1019. this.element.setAttributeNS(XLINKNS,"href",SVGURL+"#"+id);
  1020. }
  1021. this.h = (h+t) * scale; this.d = (d+t) * scale; this.w = (w+t/2) *scale;
  1022. this.l = (l+t/2) * scale; this.r = (r+t/2) * scale;
  1023. this.H = Math.max(0,this.h); this.D = Math.max(0,this.d);
  1024. this.x = this.y = 0; this.scale = scale;
  1025. }
  1026. },{
  1027. glyphs: {}, // which glpyhs have been used
  1028. defs: null, // the SVG <defs> element where glyphs are stored
  1029. n: 0 // the ID for local <defs> for self-contained SVG elements
  1030. });
  1031. HUB.Register.StartupHook("mml Jax Ready",function () {
  1032. MML = MathJax.ElementJax.mml;
  1033. MML.mbase.Augment({
  1034. SVG: BBOX,
  1035. toSVG: function () {
  1036. this.SVGgetStyles();
  1037. var variant = this.SVGgetVariant();
  1038. var svg = this.SVG(); this.SVGgetScale(svg);
  1039. this.SVGhandleSpace(svg);
  1040. for (var i = 0, m = this.data.length; i < m; i++) {
  1041. if (this.data[i]) {
  1042. var child = svg.Add(this.data[i].toSVG(variant,svg.scale),svg.w,0,true);
  1043. if (child.skew) {svg.skew = child.skew}
  1044. }
  1045. }
  1046. svg.Clean(); var text = this.data.join("");
  1047. if (svg.skew && text.length !== 1) {delete svg.skew}
  1048. if (svg.r > svg.w && text.length === 1 && !variant.noIC)
  1049. {svg.ic = svg.r - svg.w; svg.w = svg.r}
  1050. this.SVGhandleColor(svg);
  1051. this.SVGsaveData(svg);
  1052. return svg;
  1053. },
  1054. SVGchildSVG: function (i) {
  1055. return (this.data[i] ? this.data[i].toSVG() : BBOX());
  1056. },
  1057. SVGdataStretched: function (i,HW,D) {
  1058. this.SVGdata = {HW:HW, D:D};
  1059. if (!this.data[i]) {return BBOX()}
  1060. if (D != null) {return this.data[i].SVGstretchV(HW,D)}
  1061. if (HW != null) {return this.data[i].SVGstretchH(HW)}
  1062. return this.data[i].toSVG();
  1063. },
  1064. SVGsaveData: function (svg) {
  1065. if (!this.SVGdata) {this.SVGdata = {}}
  1066. this.SVGdata.w = svg.w, this.SVGdata.x = svg.x;
  1067. this.SVGdata.h = svg.h, this.SVGdata.d = svg.d;
  1068. if (svg.y) {this.SVGdata.h += svg.y; this.SVGdata.d -= svg.y}
  1069. if (svg.X != null) {this.SVGdata.X = svg.X}
  1070. if (svg.tw != null) {this.SVGdata.tw = svg.tw}
  1071. if (svg.skew) {this.SVGdata.skew = svg.skew}
  1072. if (svg.ic) {this.SVGdata.ic = svg.ic}
  1073. if (this["class"]) {svg.removeable = false; SVG.Element(svg.element,{"class":this["class"]})}
  1074. // FIXME: if an element is split by linebreaking, the ID will be the same on both parts
  1075. // FIXME: if an element has an id, its zoomed copy will have the same ID
  1076. if (this.id) {svg.removeable = false; SVG.Element(svg.element,{"id":this.id})}
  1077. if (this.href) {this.SVGaddHref(svg)}
  1078. if (SVG.config.addMMLclasses) {
  1079. this.SVGaddClass(svg.element,"mjx-svg-"+this.type);
  1080. svg.removeable = false;
  1081. }
  1082. var style = this.style;
  1083. if (style && svg.element) {
  1084. svg.element.style.cssText = style;
  1085. if (svg.element.style.fontSize) {svg.element.style.fontSize = ""} // handled by scale
  1086. svg.element.style.border = svg.element.style.padding = "";
  1087. if (svg.removeable) {svg.removeable = (svg.element.style.cssText === "")}
  1088. }
  1089. this.SVGaddAttributes(svg);
  1090. },
  1091. SVGaddClass: function (node,name) {
  1092. var classes = node.getAttribute("class");
  1093. node.setAttribute("class",(classes ? classes+" " : "")+name);
  1094. },
  1095. SVGaddAttributes: function (svg) {
  1096. //
  1097. // Copy RDFa, aria, and other tags from the MathML to the HTML-CSS
  1098. // output spans Don't copy those in the MML.nocopyAttributes list,
  1099. // the ignoreMMLattributes configuration list, or anything tha
  1100. // already exists as a property of the span (e.g., no "onlick", etc.)
  1101. // If a name in the ignoreMMLattributes object is set to false, then
  1102. // the attribute WILL be copied.
  1103. //
  1104. if (this.attrNames) {
  1105. var copy = this.attrNames, skip = MML.nocopyAttributes, ignore = HUB.config.ignoreMMLattributes;
  1106. var defaults = (this.type === "mstyle" ? MML.math.prototype.defaults : this.defaults);
  1107. for (var i = 0, m = copy.length; i < m; i++) {
  1108. var id = copy[i];
  1109. if (ignore[id] == false || (!skip[id] && !ignore[id] &&
  1110. defaults[id] == null && typeof(svg.element[id]) === "undefined")) {
  1111. svg.element.setAttribute(id,this.attr[id]);
  1112. svg.removeable = false;
  1113. }
  1114. }
  1115. }
  1116. },
  1117. SVGaddHref: function (svg) {
  1118. var a = SVG.Element("a",{"class":"mjx-svg-href"});
  1119. a.setAttributeNS(XLINKNS,"href",this.href);
  1120. a.onclick = this.SVGlink;
  1121. SVG.addElement(a,"rect",{width:svg.w, height:svg.h+svg.d, y:-svg.d,
  1122. fill:"none", stroke:"none", "pointer-events":"all"});
  1123. if (svg.type === "svg") {
  1124. // for svg element, put <a> inside the main <g> element
  1125. var g = svg.element.firstChild;
  1126. while (g.firstChild) {a.appendChild(g.firstChild)}
  1127. g.appendChild(a);
  1128. } else {
  1129. a.appendChild(svg.element); svg.element = a;
  1130. }
  1131. svg.removeable = false;
  1132. },
  1133. //
  1134. // WebKit currently scrolls to the BOTTOM of an svg element if it contains the
  1135. // target of the link, so implement link by hand, to the containing span element.
  1136. //
  1137. SVGlink: function () {
  1138. var href = this.href.animVal;
  1139. if (href.charAt(0) === "#") {
  1140. var target = SVG.hashCheck(document.getElementById(href.substr(1)));
  1141. if (target && target.scrollIntoView)
  1142. {setTimeout(function () {target.parentNode.scrollIntoView(true)},1)}
  1143. }
  1144. document.location = href;
  1145. },
  1146. SVGgetStyles: function () {
  1147. if (this.style) {
  1148. var span = HTML.Element("span");
  1149. span.style.cssText = this.style;
  1150. this.styles = this.SVGprocessStyles(span.style);
  1151. }
  1152. },
  1153. SVGprocessStyles: function (style) {
  1154. var styles = {border:SVG.getBorders(style), padding:SVG.getPadding(style)};
  1155. if (!styles.border) {delete styles.border}
  1156. if (!styles.padding) {delete styles.padding}
  1157. if (style.fontSize) {styles.fontSize = style.fontSize}
  1158. if (style.color) {styles.color = style.color}
  1159. if (style.backgroundColor) {styles.background = style.backgroundColor}
  1160. if (style.fontStyle) {styles.fontStyle = style.fontStyle}
  1161. if (style.fontWeight) {styles.fontWeight = style.fontWeight}
  1162. if (style.fontFamily) {styles.fontFamily = style.fontFamily}
  1163. if (styles.fontWeight && styles.fontWeight.match(/^\d+$/))
  1164. {styles.fontWeight = (parseInt(styles.fontWeight) > 600 ? "bold" : "normal")}
  1165. return styles;
  1166. },
  1167. SVGhandleSpace: function (svg) {
  1168. if (this.useMMLspacing) {
  1169. if (this.type !== "mo") return;
  1170. var values = this.getValues("scriptlevel","lspace","rspace");
  1171. if (values.scriptlevel <= 0 || this.hasValue("lspace") || this.hasValue("rspace")) {
  1172. var mu = this.SVGgetMu(svg);
  1173. values.lspace = Math.max(0,SVG.length2em(values.lspace,mu));
  1174. values.rspace = Math.max(0,SVG.length2em(values.rspace,mu));
  1175. var core = this, parent = this.Parent();
  1176. while (parent && parent.isEmbellished() && parent.Core() === core)
  1177. {core = parent; parent = parent.Parent()}
  1178. if (values.lspace) {svg.x += values.lspace}
  1179. if (values.rspace) {svg.X = values.rspace}
  1180. }
  1181. } else {
  1182. var space = this.texSpacing();
  1183. this.SVGgetScale();
  1184. if (space !== "") {svg.x += SVG.length2em(space,this.scale)*this.mscale}
  1185. }
  1186. },
  1187. SVGhandleColor: function (svg) {
  1188. var values = this.getValues("mathcolor","color");
  1189. if (this.styles && this.styles.color && !values.color) {values.color = this.styles.color}
  1190. if (values.color && !this.mathcolor) {values.mathcolor = values.color}
  1191. if (values.mathcolor) {
  1192. SVG.Element(svg.element,{fill:values.mathcolor,stroke:values.mathcolor})
  1193. svg.removeable = false;
  1194. }
  1195. var borders = (this.styles||{}).border, padding = (this.styles||{}).padding,
  1196. bleft = ((borders||{}).left||0), pleft = ((padding||{}).left||0), id;
  1197. values.background = (this.mathbackground || this.background ||
  1198. (this.styles||{}).background || MML.COLOR.TRANSPARENT);
  1199. if (bleft + pleft) {
  1200. //
  1201. // Make a box and move the contents of svg to it,
  1202. // then add it back into svg, but offset by the left amount
  1203. //
  1204. var dup = BBOX(); for (id in svg) {if (svg.hasOwnProperty(id)) {dup[id] = svg[id]}}
  1205. dup.x = 0; dup.y = 0;
  1206. svg.element = SVG.Element("g"); svg.removeable = true;
  1207. svg.Add(dup,bleft+pleft,0);
  1208. }
  1209. //
  1210. // Adjust size by padding and dashed borders (left is taken care of above)
  1211. //
  1212. if (padding) {svg.w += padding.right||0; svg.h += padding.top||0; svg.d += padding.bottom||0}
  1213. if (borders) {svg.w += borders.right||0; svg.h += borders.top||0; svg.d += borders.bottom||0}
  1214. //
  1215. // Add background color
  1216. //
  1217. if (values.background !== MML.COLOR.TRANSPARENT) {
  1218. var nodeName = svg.element.nodeName.toLowerCase();
  1219. if (nodeName !== "g" && nodeName !== "svg") {
  1220. var g = SVG.Element("g"); g.appendChild(svg.element);
  1221. svg.element = g; svg.removeable = true;
  1222. }
  1223. svg.Add(BBOX.RECT(svg.h,svg.d,svg.w,{fill:values.background,stroke:"none"}),0,0,false,true)
  1224. }
  1225. //
  1226. // Add borders
  1227. //
  1228. if (borders) {
  1229. var dd = 5; // fuzz factor to avoid anti-alias problems at edges
  1230. var sides = {
  1231. left: ["V",svg.h+svg.d,-dd,-svg.d],
  1232. right: ["V",svg.h+svg.d,svg.w-borders.right+dd,-svg.d],
  1233. top: ["H",svg.w,0,svg.h-borders.top+dd],
  1234. bottom:["H",svg.w,0,-svg.d-dd]
  1235. }
  1236. for (id in sides) {if (sides.hasOwnProperty(id)) {
  1237. if (borders[id]) {
  1238. var side = sides[id], box = BBOX[side[0]+"LINE"];
  1239. svg.Add(box(side[1],borders[id],borders[id+"Style"],borders[id+"Color"]),side[2],side[3]);
  1240. }
  1241. }}
  1242. }
  1243. },
  1244. SVGhandleVariant: function (variant,scale,text) {
  1245. return SVG.HandleVariant(variant,scale,text);
  1246. },
  1247. SVGgetVariant: function () {
  1248. var values = this.getValues("mathvariant","fontfamily","fontweight","fontstyle");
  1249. var variant = values.mathvariant;
  1250. if (this.variantForm) variant = "-"+SVG.fontInUse+"-variant";
  1251. values.hasVariant = this.Get("mathvariant",true); // null if not explicitly specified
  1252. if (!values.hasVariant) {
  1253. values.family = values.fontfamily;
  1254. values.weight = values.fontweight;
  1255. values.style = values.fontstyle;
  1256. }
  1257. if (this.styles) {
  1258. if (!values.style && this.styles.fontStyle) {values.style = this.styles.fontStyle}
  1259. if (!values.weight && this.styles.fontWeight) {values.weight = this.styles.fontWeight}
  1260. if (!values.family && this.styles.fontFamily) {values.family = this.styles.fontFamily}
  1261. }
  1262. if (values.family && !values.hasVariant) {
  1263. if (!values.weight && values.mathvariant.match(/bold/)) {values.weight = "bold"}
  1264. if (!values.style && values.mathvariant.match(/italic/)) {values.style = "italic"}
  1265. variant = {forceFamily: true, font: {"font-family":values.family}};
  1266. if (values.style) {variant.font["font-style"] = values.style}
  1267. if (values.weight) {variant.font["font-weight"] = values.weight}
  1268. return variant;
  1269. }
  1270. if (values.weight === "bold") {
  1271. variant = {
  1272. normal:MML.VARIANT.BOLD, italic:MML.VARIANT.BOLDITALIC,
  1273. fraktur:MML.VARIANT.BOLDFRAKTUR, script:MML.VARIANT.BOLDSCRIPT,
  1274. "sans-serif":MML.VARIANT.BOLDSANSSERIF,
  1275. "sans-serif-italic":MML.VARIANT.SANSSERIFBOLDITALIC
  1276. }[variant]||variant;
  1277. } else if (values.weight === "normal") {
  1278. variant = {
  1279. bold:MML.VARIANT.normal, "bold-italic":MML.VARIANT.ITALIC,
  1280. "bold-fraktur":MML.VARIANT.FRAKTUR, "bold-script":MML.VARIANT.SCRIPT,
  1281. "bold-sans-serif":MML.VARIANT.SANSSERIF,
  1282. "sans-serif-bold-italic":MML.VARIANT.SANSSERIFITALIC
  1283. }[variant]||variant;
  1284. }
  1285. if (values.style === "italic") {
  1286. variant = {
  1287. normal:MML.VARIANT.ITALIC, bold:MML.VARIANT.BOLDITALIC,
  1288. "sans-serif":MML.VARIANT.SANSSERIFITALIC,
  1289. "bold-sans-serif":MML.VARIANT.SANSSERIFBOLDITALIC
  1290. }[variant]||variant;
  1291. } else if (values.style === "normal") {
  1292. variant = {
  1293. italic:MML.VARIANT.NORMAL, "bold-italic":MML.VARIANT.BOLD,
  1294. "sans-serif-italic":MML.VARIANT.SANSSERIF,
  1295. "sans-serif-bold-italic":MML.VARIANT.BOLDSANSSERIF
  1296. }[variant]||variant;
  1297. }
  1298. if (!(variant in SVG.FONTDATA.VARIANT)) {
  1299. // If the mathvariant value is invalid or not supported by this
  1300. // font, fallback to normal. See issue 363.
  1301. variant = "normal";
  1302. }
  1303. return SVG.FONTDATA.VARIANT[variant];
  1304. },
  1305. SVGgetScale: function (svg) {
  1306. var scale = 1;
  1307. if (this.mscale) {
  1308. scale = this.scale;
  1309. } else {
  1310. var values = this.getValues("scriptlevel","fontsize");
  1311. values.mathsize = (this.isToken ? this : this.Parent()).Get("mathsize");
  1312. if ((this.styles||{}).fontSize && !values.fontsize) {values.fontsize = this.styles.fontSize}
  1313. if (values.fontsize && !this.mathsize) {values.mathsize = values.fontsize}
  1314. if (values.scriptlevel !== 0) {
  1315. if (values.scriptlevel > 2) {values.scriptlevel = 2}
  1316. scale = Math.pow(this.Get("scriptsizemultiplier"),values.scriptlevel);
  1317. values.scriptminsize = SVG.length2em(this.Get("scriptminsize"))/1000;
  1318. if (scale < values.scriptminsize) {scale = values.scriptminsize}
  1319. }
  1320. this.scale = scale; this.mscale = SVG.length2em(values.mathsize)/1000;
  1321. }
  1322. if (svg) {svg.scale = scale; if (this.isToken) {svg.scale *= this.mscale}}
  1323. return scale * this.mscale;
  1324. },
  1325. SVGgetMu: function (svg) {
  1326. var mu = 1, values = this.getValues("scriptlevel","scriptsizemultiplier");
  1327. if (svg.scale && svg.scale !== 1) {mu = 1/svg.scale}
  1328. if (values.scriptlevel !== 0) {
  1329. if (values.scriptlevel > 2) {values.scriptlevel = 2}
  1330. mu = Math.sqrt(Math.pow(values.scriptsizemultiplier,values.scriptlevel));
  1331. }
  1332. return mu;
  1333. },
  1334. SVGnotEmpty: function (data) {
  1335. while (data) {
  1336. if ((data.type !== "mrow" && data.type !== "texatom") ||
  1337. data.data.length > 1) {return true}
  1338. data = data.data[0];
  1339. }
  1340. return false;
  1341. },
  1342. SVGcanStretch: function (direction) {
  1343. var can = false;
  1344. if (this.isEmbellished()) {
  1345. var core = this.Core();
  1346. if (core && core !== this) {
  1347. can = core.SVGcanStretch(direction);
  1348. if (can && core.forceStretch) {this.forceStretch = true}
  1349. }
  1350. }
  1351. return can;
  1352. },
  1353. SVGstretchV: function (h,d) {return this.toSVG(h,d)},
  1354. SVGstretchH: function (w) {return this.toSVG(w)},
  1355. SVGlineBreaks: function () {return false}
  1356. },{
  1357. SVGemptySVG: function () {
  1358. var svg = this.SVG();
  1359. svg.Clean();
  1360. this.SVGsaveData(svg);
  1361. return svg;
  1362. },
  1363. SVGautoload: function () {
  1364. var file = SVG.autoloadDir+"/"+this.type+".js";
  1365. HUB.RestartAfter(AJAX.Require(file));
  1366. },
  1367. SVGautoloadFile: function (name) {
  1368. var file = SVG.autoloadDir+"/"+name+".js";
  1369. HUB.RestartAfter(AJAX.Require(file));
  1370. }
  1371. });
  1372. MML.chars.Augment({
  1373. toSVG: function (variant,scale,remap,chars) {
  1374. var text = this.data.join("").replace(/[\u2061-\u2064]/g,""); // remove invisibles
  1375. if (remap) {text = remap(text,chars)}
  1376. return this.SVGhandleVariant(variant,scale,text);
  1377. }
  1378. });
  1379. MML.entity.Augment({
  1380. toSVG: function (variant,scale,remap,chars) {
  1381. var text = this.toString().replace(/[\u2061-\u2064]/g,""); // remove invisibles
  1382. if (remap) {text = remap(text,chars)}
  1383. return this.SVGhandleVariant(variant,scale,text);
  1384. }
  1385. });
  1386. MML.mo.Augment({
  1387. toSVG: function (HW,D) {
  1388. this.SVGgetStyles();
  1389. var svg = this.svg = this.SVG();
  1390. var scale = this.SVGgetScale(svg);
  1391. this.SVGhandleSpace(svg);
  1392. if (this.data.length == 0) {svg.Clean(); this.SVGsaveData(svg); return svg}
  1393. //
  1394. // Stretch the operator, if that is requested
  1395. //
  1396. if (D != null) {return this.SVGstretchV(HW,D)}
  1397. else if (HW != null) {return this.SVG.strechH(HW)}
  1398. //
  1399. // Get the variant, and check for operator size
  1400. //
  1401. var variant = this.SVGgetVariant();
  1402. var values = this.getValues("largeop","displaystyle");
  1403. if (values.largeop)
  1404. {variant = SVG.FONTDATA.VARIANT[values.displaystyle ? "-largeOp" : "-smallOp"]}
  1405. //
  1406. // Get character translation for superscript and accents
  1407. //
  1408. var parent = this.CoreParent(),
  1409. isScript = (parent && parent.isa(MML.msubsup) && this !== parent.data[0]),
  1410. mapchars = (isScript?this.remapChars:null);
  1411. if (this.data.join("").length === 1 && parent && parent.isa(MML.munderover) &&
  1412. this.CoreText(parent.data[parent.base]).length === 1) {
  1413. var over = parent.data[parent.over], under = parent.data[parent.under];
  1414. if (over && this === over.CoreMO() && parent.Get("accent")) {mapchars = SVG.FONTDATA.REMAPACCENT}
  1415. else if (under && this === under.CoreMO() && parent.Get("accentunder")) {mapchars = SVG.FONTDATA.REMAPACCENTUNDER}
  1416. }
  1417. //
  1418. // Primes must come from another font
  1419. //
  1420. if (isScript && this.data.join("").match(/['`"\u00B4\u2032-\u2037\u2057]/))
  1421. {variant = SVG.FONTDATA.VARIANT["-"+SVG.fontInUse+"-variant"]}
  1422. //
  1423. // Typeset contents
  1424. //
  1425. for (var i = 0, m = this.data.length; i < m; i++) {
  1426. if (this.data[i]) {
  1427. var text = this.data[i].toSVG(variant,scale,this.remap,mapchars), x = svg.w;
  1428. if (x === 0 && -text.l > 10*text.w) {x += -text.l} // initial combining character doesn't combine
  1429. svg.Add(text,x,0,true);
  1430. if (text.skew) {svg.skew = text.skew}
  1431. }
  1432. }
  1433. svg.Clean();
  1434. if (this.data.join("").length !== 1) {delete svg.skew}
  1435. //
  1436. // Handle large operator centering
  1437. //
  1438. if (values.largeop) {
  1439. svg.y = SVG.TeX.axis_height - (svg.h - svg.d)/2/scale;
  1440. if (svg.r > svg.w) {svg.ic = svg.r - svg.w; svg.w = svg.r}
  1441. }
  1442. //
  1443. // Finish up
  1444. //
  1445. this.SVGhandleColor(svg);
  1446. this.SVGsaveData(svg);
  1447. return svg;
  1448. },
  1449. SVGcanStretch: function (direction) {
  1450. if (!this.Get("stretchy")) {return false}
  1451. var c = this.data.join("");
  1452. if (c.length > 1) {return false}
  1453. var parent = this.CoreParent();
  1454. if (parent && parent.isa(MML.munderover) &&
  1455. this.CoreText(parent.data[parent.base]).length === 1) {
  1456. var over = parent.data[parent.over], under = parent.data[parent.under];
  1457. if (over && this === over.CoreMO() && parent.Get("accent")) {c = SVG.FONTDATA.REMAPACCENT[c]||c}
  1458. else if (under && this === under.CoreMO() && parent.Get("accentunder")) {c = SVG.FONTDATA.REMAPACCENTUNDER[c]||c}
  1459. }
  1460. c = SVG.FONTDATA.DELIMITERS[c.charCodeAt(0)];
  1461. var can = (c && c.dir == direction.substr(0,1));
  1462. if (!can) {delete this.svg}
  1463. this.forceStretch = can && (this.Get("minsize",true) || this.Get("maxsize",true));
  1464. return can;
  1465. },
  1466. SVGstretchV: function (h,d) {
  1467. var svg = this.svg || this.toSVG();
  1468. var values = this.getValues("symmetric","maxsize","minsize");
  1469. var axis = SVG.TeX.axis_height*svg.scale, mu = this.SVGgetMu(svg), H;
  1470. if (values.symmetric) {H = 2*Math.max(h-axis,d+axis)} else {H = h + d}
  1471. values.maxsize = SVG.length2em(values.maxsize,mu,svg.h+svg.d);
  1472. values.minsize = SVG.length2em(values.minsize,mu,svg.h+svg.d);
  1473. H = Math.max(values.minsize,Math.min(values.maxsize,H));
  1474. if (H != values.minsize)
  1475. {H = [Math.max(H*SVG.TeX.delimiterfactor/1000,H-SVG.TeX.delimitershortfall),H]}
  1476. svg = SVG.createDelimiter(this.data.join("").charCodeAt(0),H,svg.scale);
  1477. if (values.symmetric) {H = (svg.h + svg.d)/2 + axis}
  1478. else {H = (svg.h + svg.d) * h/(h + d)}
  1479. svg.y = H - svg.h;
  1480. this.SVGhandleSpace(svg);
  1481. this.SVGhandleColor(svg);
  1482. delete this.svg.element;
  1483. this.SVGsaveData(svg);
  1484. svg.stretched = true;
  1485. return svg;
  1486. },
  1487. SVGstretchH: function (w) {
  1488. var svg = this.svg || this.toSVG(), mu = this.SVGgetMu(svg);
  1489. var values = this.getValues("maxsize","minsize","mathvariant","fontweight");
  1490. // FIXME: should take style="font-weight:bold" into account as well
  1491. if ((values.fontweight === "bold" || parseInt(values.fontweight) >= 600) &&
  1492. !this.Get("mathvariant",true)) {values.mathvariant = MML.VARIANT.BOLD}
  1493. values.maxsize = SVG.length2em(values.maxsize,mu,svg.w);
  1494. values.minsize = SVG.length2em(values.minsize,mu,svg.w);
  1495. w = Math.max(values.minsize,Math.min(values.maxsize,w));
  1496. svg = SVG.createDelimiter(this.data.join("").charCodeAt(0),w,svg.scale,values.mathvariant);
  1497. this.SVGhandleSpace(svg);
  1498. this.SVGhandleColor(svg);
  1499. delete this.svg.element;
  1500. this.SVGsaveData(svg);
  1501. svg.stretched = true;
  1502. return svg;
  1503. }
  1504. });
  1505. MML.mn.Augment({
  1506. SVGremapMinus: function (text) {return text.replace(/^-/,"\u2212")},
  1507. toSVG: function () {
  1508. this.SVGgetStyles();
  1509. var variant = this.SVGgetVariant();
  1510. var svg = this.SVG(); this.SVGgetScale(svg);
  1511. this.SVGhandleSpace(svg);
  1512. var remap = this.SVGremapMinus;
  1513. for (var i = 0, m = this.data.length; i < m; i++) {
  1514. if (this.data[i]) {
  1515. var child = svg.Add(this.data[i].toSVG(variant,svg.scale,remap),svg.w,0,true);
  1516. if (child.skew) {svg.skew = child.skew}
  1517. remap = null;
  1518. }
  1519. }
  1520. svg.Clean(); var text = this.data.join("");
  1521. if (svg.skew && text.length !== 1) {delete svg.skew}
  1522. if (svg.r > svg.w && text.length === 1 && !variant.noIC)
  1523. {svg.ic = svg.r - svg.w; svg.w = svg.r}
  1524. this.SVGhandleColor(svg);
  1525. this.SVGsaveData(svg);
  1526. return svg;
  1527. },
  1528. }),
  1529. MML.mtext.Augment({
  1530. toSVG: function () {
  1531. if (SVG.config.mtextFontInherit || this.Parent().type === "merror") {
  1532. this.SVGgetStyles();
  1533. var svg = this.SVG(), scale = this.SVGgetScale(svg);
  1534. this.SVGhandleSpace(svg);
  1535. var variant = this.SVGgetVariant(), def = {direction:this.Get("dir")};
  1536. if (variant.bold) {def["font-weight"] = "bold"}
  1537. if (variant.italic) {def["font-style"] = "italic"}
  1538. variant = this.Get("mathvariant");
  1539. if (variant === "monospace") {def["class"] = "MJX-monospace"}
  1540. else if (variant.match(/sans-serif/)) {def["class"] = "MJX-sans-serif"}
  1541. svg.Add(BBOX.TEXT(scale*100/SVG.config.scale,this.data.join(""),def)); svg.Clean();
  1542. this.SVGhandleColor(svg);
  1543. this.SVGsaveData(svg);
  1544. return svg;
  1545. } else {
  1546. return this.SUPER(arguments).toSVG.call(this);
  1547. }
  1548. }
  1549. });
  1550. MML.merror.Augment({
  1551. toSVG: function (HW,D) {
  1552. this.SVGgetStyles();
  1553. var svg = this.SVG(), scale = SVG.length2em(this.styles.fontSize||1)/1000;
  1554. this.SVGhandleSpace(svg);
  1555. var def = (scale !== 1 ? {transform:"scale("+SVG.Fixed(scale)+")"} : {});
  1556. var bbox = BBOX(def);
  1557. bbox.Add(this.SVGchildSVG(0)); bbox.Clean();
  1558. if (scale !== 1) {
  1559. bbox.removeable = false;
  1560. var adjust = ["w","h","d","l","r","D","H"];
  1561. for (var i = 0, m = adjust.length; i < m; i++) {bbox[adjust[i]] *= scale}
  1562. }
  1563. svg.Add(bbox); svg.Clean();
  1564. this.SVGhandleColor(svg);
  1565. this.SVGsaveData(svg);
  1566. return svg;
  1567. },
  1568. SVGgetStyles: function () {
  1569. var span = HTML.Element("span",{style: SVG.config.merrorStyle});
  1570. this.styles = this.SVGprocessStyles(span.style);
  1571. if (this.style) {
  1572. span.style.cssText = this.style;
  1573. HUB.Insert(this.styles,this.SVGprocessStyles(span.style));
  1574. }
  1575. }
  1576. });
  1577. MML.ms.Augment({toSVG: MML.mbase.SVGautoload});
  1578. MML.mglyph.Augment({toSVG: MML.mbase.SVGautoload});
  1579. MML.mspace.Augment({
  1580. toSVG: function () {
  1581. this.SVGgetStyles();
  1582. var values = this.getValues("height","depth","width");
  1583. values.mathbackground = this.mathbackground;
  1584. if (this.background && !this.mathbackground) {values.mathbackground = this.background}
  1585. var svg = this.SVG(); this.SVGgetScale(svg);
  1586. var scale = this.mscale, mu = this.SVGgetMu(svg);
  1587. svg.h = SVG.length2em(values.height,mu) * scale;
  1588. svg.d = SVG.length2em(values.depth,mu) * scale;
  1589. svg.w = svg.r = SVG.length2em(values.width,mu) * scale;
  1590. if (svg.w < 0) {svg.x = svg.w; svg.w = svg.r = 0}
  1591. if (svg.h < -svg.d) {svg.d = -svg.h}
  1592. svg.l = 0; svg.Clean();
  1593. this.SVGhandleColor(svg);
  1594. this.SVGsaveData(svg);
  1595. return svg;
  1596. }
  1597. });
  1598. MML.mphantom.Augment({
  1599. toSVG: function (HW,D) {
  1600. this.SVGgetStyles();
  1601. var svg = this.SVG(); this.SVGgetScale(svg);
  1602. if (this.data[0] != null) {
  1603. this.SVGhandleSpace(svg); svg.Add(this.SVGdataStretched(0,HW,D)); svg.Clean();
  1604. while (svg.element.firstChild) {svg.element.removeChild(svg.element.firstChild)}
  1605. }
  1606. this.SVGhandleColor(svg);
  1607. this.SVGsaveData(svg);
  1608. if (svg.removeable && !svg.element.firstChild) {delete svg.element}
  1609. return svg;
  1610. }
  1611. });
  1612. MML.mpadded.Augment({
  1613. toSVG: function (HW,D) {
  1614. this.SVGgetStyles();
  1615. var svg = this.SVG();
  1616. if (this.data[0] != null) {
  1617. this.SVGgetScale(svg); this.SVGhandleSpace(svg);
  1618. var pad = this.SVGdataStretched(0,HW,D), mu = this.SVGgetMu(svg);
  1619. var values = this.getValues("height","depth","width","lspace","voffset"), X = 0, Y = 0;
  1620. if (values.lspace) {X = this.SVGlength2em(pad,values.lspace,mu)}
  1621. if (values.voffset) {Y = this.SVGlength2em(pad,values.voffset,mu)}
  1622. var h = pad.h, d = pad.d, w = pad.w, y = pad.y; // these can change durring the Add()
  1623. svg.Add(pad,X,Y); svg.Clean();
  1624. svg.h = h+y; svg.d = d-y; svg.w = w; svg.removeable = false;
  1625. if (values.height !== "") {svg.h = this.SVGlength2em(svg,values.height,mu,"h",0)}
  1626. if (values.depth !== "") {svg.d = this.SVGlength2em(svg,values.depth,mu,"d",0)}
  1627. if (values.width !== "") {svg.w = this.SVGlength2em(svg,values.width,mu,"w",0)}
  1628. if (svg.h > svg.H) {svg.H = svg.h}; if (svg.d > svg.D) {svg.D = svg.d}
  1629. }
  1630. this.SVGhandleColor(svg);
  1631. this.SVGsaveData(svg);
  1632. return svg;
  1633. },
  1634. SVGlength2em: function (svg,length,mu,d,m) {
  1635. if (m == null) {m = -SVG.BIGDIMEN}
  1636. var match = String(length).match(/width|height|depth/);
  1637. var size = (match ? svg[match[0].charAt(0)] : (d ? svg[d] : 0));
  1638. var v = SVG.length2em(length,mu,size/this.mscale)*this.mscale;
  1639. if (d && String(length).match(/^\s*[-+]/))
  1640. {return Math.max(m,svg[d]+v)} else {return v}
  1641. }
  1642. });
  1643. MML.mrow.Augment({
  1644. SVG: BBOX.ROW,
  1645. toSVG: function (h,d) {
  1646. this.SVGgetStyles();
  1647. var svg = this.SVG();
  1648. this.SVGhandleSpace(svg);
  1649. if (d != null) {svg.sh = h; svg.sd = d}
  1650. for (var i = 0, m = this.data.length; i < m; i++)
  1651. {if (this.data[i]) {svg.Check(this.data[i])}}
  1652. svg.Stretch(); svg.Clean();
  1653. if (this.data.length === 1 && this.data[0]) {
  1654. var data = this.data[0].SVGdata;
  1655. if (data.skew) {svg.skew = data.skew}
  1656. }
  1657. if (this.SVGlineBreaks(svg)) {svg = this.SVGmultiline(svg)}
  1658. this.SVGhandleColor(svg);
  1659. this.SVGsaveData(svg);
  1660. return svg;
  1661. },
  1662. SVGlineBreaks: function (svg) {
  1663. if (!this.parent.linebreakContainer) {return false}
  1664. return (SVG.config.linebreaks.automatic &&
  1665. svg.w > SVG.linebreakWidth) || this.hasNewline();
  1666. },
  1667. SVGmultiline: function (span) {MML.mbase.SVGautoloadFile("multiline")},
  1668. SVGstretchH: function (w) {
  1669. var svg = this.SVG();
  1670. this.SVGhandleSpace(svg);
  1671. for (var i = 0, m = this.data.length; i < m; i++)
  1672. {svg.Add(this.SVGdataStretched(i,w),svg.w,0)}
  1673. svg.Clean();
  1674. this.SVGhandleColor(svg);
  1675. this.SVGsaveData(svg);
  1676. return svg;
  1677. }
  1678. });
  1679. MML.mstyle.Augment({
  1680. toSVG: function () {
  1681. this.SVGgetStyles();
  1682. var svg = this.SVG();
  1683. if (this.data[0] != null) {
  1684. this.SVGhandleSpace(svg);
  1685. var math = svg.Add(this.data[0].toSVG()); svg.Clean();
  1686. if (math.ic) {svg.ic = math.ic}
  1687. this.SVGhandleColor(svg);
  1688. }
  1689. this.SVGsaveData(svg);
  1690. return svg;
  1691. },
  1692. SVGstretchH: function (w) {
  1693. return (this.data[0] != null ? this.data[0].SVGstretchH(w) : BBOX.NULL());
  1694. },
  1695. SVGstretchV: function (h,d) {
  1696. return (this.data[0] != null ? this.data[0].SVGstretchV(h,d) : BBOX.NULL());
  1697. }
  1698. });
  1699. MML.mfrac.Augment({
  1700. toSVG: function () {
  1701. this.SVGgetStyles();
  1702. var svg = this.SVG(), scale = this.SVGgetScale(svg);
  1703. var frac = BBOX(); frac.scale = svg.scale; this.SVGhandleSpace(frac);
  1704. var num = this.SVGchildSVG(0), den = this.SVGchildSVG(1);
  1705. var values = this.getValues("displaystyle","linethickness","numalign","denomalign","bevelled");
  1706. var isDisplay = values.displaystyle;
  1707. var a = SVG.TeX.axis_height * scale;
  1708. if (values.bevelled) {
  1709. var delta = (isDisplay ? 400 : 150);
  1710. var H = Math.max(num.h+num.d,den.h+den.d)+2*delta;
  1711. var bevel = SVG.createDelimiter(0x2F,H);
  1712. frac.Add(num,0,(num.d-num.h)/2+a+delta);
  1713. frac.Add(bevel,num.w-delta/2,(bevel.d-bevel.h)/2+a);
  1714. frac.Add(den,num.w+bevel.w-delta,(den.d-den.h)/2+a-delta);
  1715. } else {
  1716. var W = Math.max(num.w,den.w);
  1717. var t = SVG.thickness2em(values.linethickness,this.scale)*this.mscale, p,q, u,v;
  1718. var mt = SVG.TeX.min_rule_thickness/SVG.em * 1000;
  1719. if (isDisplay) {u = SVG.TeX.num1; v = SVG.TeX.denom1}
  1720. else {u = (t === 0 ? SVG.TeX.num3 : SVG.TeX.num2); v = SVG.TeX.denom2}
  1721. u *= scale; v *= scale;
  1722. if (t === 0) {// \atop
  1723. p = Math.max((isDisplay ? 7 : 3) * SVG.TeX.rule_thickness, 2*mt); // force to at least 2 px
  1724. q = (u - num.d) - (den.h - v);
  1725. if (q < p) {u += (p - q)/2; v += (p - q)/2}
  1726. frac.w = W; t = 0;
  1727. } else {// \over
  1728. p = Math.max((isDisplay ? 2 : 0) * mt + t, t/2 + 1.5*mt); // force to be at least 1.5px
  1729. q = (u - num.d) - (a + t/2); if (q < p) {u += p - q}
  1730. q = (a - t/2) - (den.h - v); if (q < p) {v += p - q}
  1731. frac.Add(BBOX.RECT(t/2,t/2,W+2*t),0,a);
  1732. }
  1733. frac.Align(num,values.numalign,t,u);
  1734. frac.Align(den,values.denomalign,t,-v);
  1735. }
  1736. frac.Clean(); svg.Add(frac,0,0); svg.Clean();
  1737. this.SVGhandleColor(svg);
  1738. this.SVGsaveData(svg);
  1739. return svg;
  1740. },
  1741. SVGcanStretch: function (direction) {return false},
  1742. SVGhandleSpace: function (svg) {
  1743. if (!this.texWithDelims) {
  1744. //
  1745. // Add nulldelimiterspace around the fraction
  1746. // (TeXBook pg 150 and Appendix G rule 15e)
  1747. //
  1748. svg.x = svg.X = SVG.TeX.nulldelimiterspace * this.mscale;
  1749. }
  1750. this.SUPER(arguments).SVGhandleSpace.call(this,svg);
  1751. }
  1752. });
  1753. MML.msqrt.Augment({
  1754. toSVG: function () {
  1755. this.SVGgetStyles();
  1756. var svg = this.SVG(), scale = this.SVGgetScale(svg); this.SVGhandleSpace(svg);
  1757. var base = this.SVGchildSVG(0), rule, surd;
  1758. var t = SVG.TeX.rule_thickness * scale, p,q, H, x = 0;
  1759. if (this.Get("displaystyle")) {p = SVG.TeX.x_height * scale} else {p = t}
  1760. q = Math.max(t + p/4,1000*SVG.TeX.min_root_space/SVG.em);
  1761. H = base.h + base.d + q + t;
  1762. surd = SVG.createDelimiter(0x221A,H,scale);
  1763. if (surd.h + surd.d > H) {q = ((surd.h+surd.d) - (H-t)) / 2}
  1764. rule = BBOX.RECT(t,0,base.w);
  1765. H = base.h + q + t;
  1766. x = this.SVGaddRoot(svg,surd,x,surd.h+surd.d-H,scale);
  1767. svg.Add(surd,x,H-surd.h);
  1768. svg.Add(rule,x+surd.w,H-rule.h);
  1769. svg.Add(base,x+surd.w,0);
  1770. svg.Clean();
  1771. svg.h += t; svg.H += t;
  1772. this.SVGhandleColor(svg);
  1773. this.SVGsaveData(svg);
  1774. return svg;
  1775. },
  1776. SVGaddRoot: function (svg,surd,x,d,scale) {return x}
  1777. });
  1778. MML.mroot.Augment({
  1779. toSVG: MML.msqrt.prototype.toSVG,
  1780. SVGaddRoot: function (svg,surd,x,d,scale) {
  1781. var dx = (surd.isMultiChar ? .55 : .65) * surd.w;
  1782. if (this.data[1]) {
  1783. var root = this.data[1].toSVG(); root.x = 0;
  1784. var h = this.SVGrootHeight(surd.h+surd.d,scale,root)-d;
  1785. var w = Math.min(root.w,root.r); // remove extra right-hand padding, if any
  1786. x = Math.max(w,dx);
  1787. svg.Add(root,x-w,h);
  1788. } else {dx = x}
  1789. return x - dx;
  1790. },
  1791. SVGrootHeight: function (d,scale,root) {
  1792. return .45*(d-900*scale) + 600*scale + Math.max(0,root.d-75);
  1793. }
  1794. });
  1795. MML.mfenced.Augment({
  1796. SVG: BBOX.ROW,
  1797. toSVG: function () {
  1798. this.SVGgetStyles();
  1799. var svg = this.SVG();
  1800. this.SVGhandleSpace(svg);
  1801. if (this.data.open) {svg.Check(this.data.open)}
  1802. if (this.data[0] != null) {svg.Check(this.data[0])}
  1803. for (var i = 1, m = this.data.length; i < m; i++) {
  1804. if (this.data[i]) {
  1805. if (this.data["sep"+i]) {svg.Check(this.data["sep"+i])}
  1806. svg.Check(this.data[i]);
  1807. }
  1808. }
  1809. if (this.data.close) {svg.Check(this.data.close)}
  1810. svg.Stretch(); svg.Clean();
  1811. this.SVGhandleColor(svg);
  1812. this.SVGsaveData(svg);
  1813. return svg;
  1814. }
  1815. });
  1816. MML.menclose.Augment({toSVG: MML.mbase.SVGautoload});
  1817. MML.maction.Augment({toSVG: MML.mbase.SVGautoload});
  1818. MML.semantics.Augment({
  1819. toSVG: function () {
  1820. this.SVGgetStyles();
  1821. var svg = this.SVG();
  1822. if (this.data[0] != null) {
  1823. this.SVGhandleSpace(svg);
  1824. svg.Add(this.data[0].toSVG()); svg.Clean();
  1825. } else {svg.Clean()}
  1826. this.SVGsaveData(svg);
  1827. return svg;
  1828. },
  1829. SVGstretchH: function (w) {
  1830. return (this.data[0] != null ? this.data[0].SVGstretchH(w) : BBOX.NULL());
  1831. },
  1832. SVGstretchV: function (h,d) {
  1833. return (this.data[0] != null ? this.data[0].SVGstretchV(h,d) : BBOX.NULL());
  1834. }
  1835. });
  1836. MML.munderover.Augment({
  1837. toSVG: function (HW,D) {
  1838. this.SVGgetStyles();
  1839. var values = this.getValues("displaystyle","accent","accentunder","align");
  1840. var base = this.data[this.base];
  1841. if (!values.displaystyle && base != null &&
  1842. (base.movablelimits || base.CoreMO().Get("movablelimits")))
  1843. {return MML.msubsup.prototype.toSVG.call(this)}
  1844. var svg = this.SVG(), scale = this.SVGgetScale(svg); this.SVGhandleSpace(svg);
  1845. var boxes = [], stretch = [], box, i, m, W = -SVG.BIGDIMEN, WW = W;
  1846. for (i = 0, m = this.data.length; i < m; i++) {
  1847. if (this.data[i] != null) {
  1848. if (i == this.base) {
  1849. boxes[i] = this.SVGdataStretched(i,HW,D);
  1850. stretch[i] = (D != null || HW == null) && this.data[i].SVGcanStretch("Horizontal");
  1851. if (this.data[this.over] && values.accent) {
  1852. boxes[i].h = Math.max(boxes[i].h,SVG.TeX.x_height); // min height of 1ex (#1706)
  1853. }
  1854. } else {
  1855. boxes[i] = this.data[i].toSVG(); boxes[i].x = 0; delete boxes[i].X;
  1856. stretch[i] = this.data[i].SVGcanStretch("Horizontal");
  1857. }
  1858. if (boxes[i].w > WW) {WW = boxes[i].w}
  1859. if (!stretch[i] && WW > W) {W = WW}
  1860. }
  1861. }
  1862. if (D == null && HW != null) {W = HW} else if (W == -SVG.BIGDIMEN) {W = WW}
  1863. for (i = WW = 0, m = this.data.length; i < m; i++) {if (this.data[i]) {
  1864. if (stretch[i]) {
  1865. boxes[i] = this.data[i].SVGstretchH(W);
  1866. if (i !== this.base) {boxes[i].x = 0; delete boxes[i].X}
  1867. }
  1868. if (boxes[i].w > WW) {WW = boxes[i].w}
  1869. }}
  1870. var t = SVG.TeX.rule_thickness * this.mscale;
  1871. var x, y, z1, z2, z3, dw, k, delta = 0;
  1872. base = boxes[this.base] || {w:0, h:0, d:0, H:0, D:0, l:0, r:0, y:0, scale:scale};
  1873. if (base.ic) {delta = 1.3*base.ic + .05} // adjust faked IC to be more in line with expeted results
  1874. for (i = 0, m = this.data.length; i < m; i++) {
  1875. if (this.data[i] != null) {
  1876. box = boxes[i];
  1877. z3 = SVG.TeX.big_op_spacing5 * scale;
  1878. var accent = (i != this.base && values[this.ACCENTS[i]]);
  1879. if (accent && box.w <= 1) {
  1880. box.x = -box.l;
  1881. boxes[i] = BBOX.G().With({removeable: false});
  1882. boxes[i].Add(box); boxes[i].Clean();
  1883. boxes[i].w = -box.l; box = boxes[i];
  1884. }
  1885. dw = {left:0, center:(WW-box.w)/2, right:WW-box.w}[values.align];
  1886. x = dw; y = 0;
  1887. if (i == this.over) {
  1888. if (accent) {
  1889. k = t * scale; z3 = 0;
  1890. if (base.skew) {
  1891. x += base.skew; svg.skew = base.skew;
  1892. if (x+box.w > WW) {svg.skew += (WW-box.w-x)/2}
  1893. }
  1894. } else {
  1895. z1 = SVG.TeX.big_op_spacing1 * scale;
  1896. z2 = SVG.TeX.big_op_spacing3 * scale;
  1897. k = Math.max(z1,z2-Math.max(0,box.d));
  1898. }
  1899. k = Math.max(k,1500/SVG.em);
  1900. x += delta/2; y = base.y + base.h + box.d + k;
  1901. box.h += z3; if (box.h > box.H) {box.H = box.h}
  1902. } else if (i == this.under) {
  1903. if (accent) {
  1904. k = 3*t * scale; z3 = 0;
  1905. } else {
  1906. z1 = SVG.TeX.big_op_spacing2 * scale;
  1907. z2 = SVG.TeX.big_op_spacing4 * scale;
  1908. k = Math.max(z1,z2-box.h);
  1909. }
  1910. k = Math.max(k,1500/SVG.em);
  1911. x -= delta/2; y = base.y -(base.d + box.h + k);
  1912. box.d += z3; if (box.d > box.D) {box.D = box.d}
  1913. }
  1914. svg.Add(box,x,y);
  1915. }
  1916. }
  1917. svg.Clean();
  1918. this.SVGhandleColor(svg);
  1919. this.SVGsaveData(svg);
  1920. return svg;
  1921. }
  1922. });
  1923. MML.msubsup.Augment({
  1924. toSVG: function (HW,D) {
  1925. this.SVGgetStyles();
  1926. var svg = this.SVG(), scale = this.SVGgetScale(svg); this.SVGhandleSpace(svg);
  1927. var mu = this.SVGgetMu(svg);
  1928. var base = svg.Add(this.SVGdataStretched(this.base,HW,D));
  1929. var sscale = (this.data[this.sup] || this.data[this.sub] || this).SVGgetScale();
  1930. var x_height = SVG.TeX.x_height * scale, s = SVG.TeX.scriptspace * scale;
  1931. var sup, sub;
  1932. if (this.SVGnotEmpty(this.data[this.sup])) {
  1933. sup = this.data[this.sup].toSVG();
  1934. sup.w += s; sup.r = Math.max(sup.w,sup.r);
  1935. }
  1936. if (this.SVGnotEmpty(this.data[this.sub])) {
  1937. sub = this.data[this.sub].toSVG();
  1938. sub.w += s; sub.r = Math.max(sub.w,sub.r);
  1939. }
  1940. var q = SVG.TeX.sup_drop * sscale, r = SVG.TeX.sub_drop * sscale;
  1941. var u = base.h+(base.y||0) - q, v = base.d-(base.y||0) + r, delta = 0, p;
  1942. if (base.ic) {
  1943. base.w -= base.ic; // remove IC (added by mo and mi)
  1944. delta = 1.3*base.ic+.05; // adjust faked IC to be more in line with expeted results
  1945. }
  1946. if (this.data[this.base] &&
  1947. (this.data[this.base].type === "mi" || this.data[this.base].type === "mo")) {
  1948. if (this.data[this.base].data.join("").length === 1 && base.scale === 1 &&
  1949. !base.stretched && !this.data[this.base].Get("largeop")) {u = v = 0}
  1950. }
  1951. var min = this.getValues("subscriptshift","superscriptshift");
  1952. min.subscriptshift = (min.subscriptshift === "" ? 0 : SVG.length2em(min.subscriptshift,mu));
  1953. min.superscriptshift = (min.superscriptshift === "" ? 0 : SVG.length2em(min.superscriptshift,mu));
  1954. var x = base.w + base.x;
  1955. if (!sup) {
  1956. if (sub) {
  1957. v = Math.max(v,SVG.TeX.sub1*scale,sub.h-(4/5)*x_height,min.subscriptshift);
  1958. svg.Add(sub,x,-v); this.data[this.sub].SVGdata.dy = -v;
  1959. }
  1960. } else {
  1961. if (!sub) {
  1962. var values = this.getValues("displaystyle","texprimestyle");
  1963. p = SVG.TeX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
  1964. u = Math.max(u,p*scale,sup.d+(1/4)*x_height,min.superscriptshift);
  1965. svg.Add(sup,x+delta,u);
  1966. this.data[this.sup].SVGdata.dx = delta;
  1967. this.data[this.sup].SVGdata.dy = u;
  1968. } else {
  1969. v = Math.max(v,SVG.TeX.sub2*scale);
  1970. var t = SVG.TeX.rule_thickness * scale;
  1971. if ((u - sup.d) - (sub.h - v) < 3*t) {
  1972. v = 3*t - u + sup.d + sub.h;
  1973. q = (4/5)*x_height - (u - sup.d);
  1974. if (q > 0) {u += q; v -= q}
  1975. }
  1976. svg.Add(sup,x+delta,Math.max(u,min.superscriptshift));
  1977. svg.Add(sub,x,-Math.max(v,min.subscriptshift));
  1978. this.data[this.sup].SVGdata.dx = delta;
  1979. this.data[this.sup].SVGdata.dy = Math.max(u,min.superscriptshift);
  1980. this.data[this.sub].SVGdata.dy = -Math.max(v,min.subscriptshift);
  1981. }
  1982. }
  1983. svg.Clean();
  1984. this.SVGhandleColor(svg);
  1985. this.SVGsaveData(svg);
  1986. return svg;
  1987. }
  1988. });
  1989. MML.mmultiscripts.Augment({toSVG: MML.mbase.SVGautoload});
  1990. MML.mtable.Augment({toSVG: MML.mbase.SVGautoload});
  1991. MML["annotation-xml"].Augment({toSVG: MML.mbase.SVGautoload});
  1992. MML.math.Augment({
  1993. SVG: BBOX.Subclass({type:"svg", removeable: false}),
  1994. toSVG: function (span,div) {
  1995. var CONFIG = SVG.config;
  1996. //
  1997. // All the data should be in an inferrerd row
  1998. //
  1999. if (this.data[0]) {
  2000. this.SVGgetStyles();
  2001. MML.mbase.prototype.displayAlign = HUB.config.displayAlign;
  2002. MML.mbase.prototype.displayIndent = HUB.config.displayIndent;
  2003. if (String(HUB.config.displayIndent).match(/^0($|[a-z%])/i))
  2004. MML.mbase.prototype.displayIndent = "0";
  2005. //
  2006. // Put content in a <g> with defaults and matrix that flips y axis.
  2007. // Put that in an <svg> with xlink defined.
  2008. //
  2009. var box = BBOX.G(); box.Add(this.data[0].toSVG(),0,0,true); box.Clean();
  2010. this.SVGhandleColor(box);
  2011. SVG.Element(box.element,{
  2012. stroke:"currentColor", fill:"currentColor", "stroke-width":0,
  2013. transform: "matrix(1 0 0 -1 0 0)"
  2014. });
  2015. box.removeable = false;
  2016. var svg = this.SVG();
  2017. svg.element.setAttribute("xmlns:xlink",XLINKNS);
  2018. if (CONFIG.useFontCache && !CONFIG.useGlobalCache)
  2019. {svg.element.appendChild(BBOX.GLYPH.defs)}
  2020. svg.Add(box); svg.Clean();
  2021. this.SVGsaveData(svg);
  2022. //
  2023. // If this element is not the top-level math element
  2024. // remove the transform and return the svg object
  2025. // (issue #614).
  2026. //
  2027. if (!span) {
  2028. svg.element = svg.element.firstChild; // remove <svg> element
  2029. svg.element.removeAttribute("transform");
  2030. svg.removable = true;
  2031. return svg;
  2032. }
  2033. //
  2034. // Style the <svg> to get the right size and placement
  2035. //
  2036. var l = Math.max(-svg.l,0), r = Math.max(svg.r-svg.w,0);
  2037. var style = svg.element.style, px = SVG.TeX.x_height/SVG.ex;
  2038. var H = (Math.ceil(svg.H/px)+1)*px+SVG.HFUZZ, // round to pixels and add padding
  2039. D = (Math.ceil(svg.D/px)+1)*px+SVG.DFUZZ;
  2040. var w = l + svg.w + r;
  2041. svg.element.setAttribute("width",SVG.Ex(w));
  2042. svg.element.setAttribute("height",SVG.Ex(H+D));
  2043. style.verticalAlign = SVG.Ex(-D);
  2044. if (l) style.marginLeft = SVG.Ex(-l);
  2045. if (r) style.marginRight = SVG.Ex(-r);
  2046. svg.element.setAttribute("viewBox",SVG.Fixed(-l,1)+" "+SVG.Fixed(-H,1)+" "+
  2047. SVG.Fixed(w,1)+" "+SVG.Fixed(H+D,1));
  2048. //
  2049. // If there is extra height or depth, hide that
  2050. //
  2051. if (svg.H > svg.h) style.marginTop = SVG.Ex(svg.h-H);
  2052. if (svg.D > svg.d) {
  2053. style.marginBottom = SVG.Ex(svg.d-D);
  2054. style.verticalAlign = SVG.Ex(-svg.d);
  2055. }
  2056. //
  2057. // The approximate ex can cause full-width equations to be too wide,
  2058. // so if they are close to full width, make sure they aren't too big.
  2059. //
  2060. if (Math.abs(w-SVG.cwidth) < 10)
  2061. style.maxWidth = SVG.Fixed(SVG.cwidth*SVG.em/1000);
  2062. //
  2063. // Add it to the MathJax span
  2064. //
  2065. var alttext = this.Get("alttext");
  2066. if (alttext && !svg.element.getAttribute("aria-label")) svg.element.setAttribute("aria-label",alttext);
  2067. if (!svg.element.getAttribute("role")) svg.element.setAttribute("role","img");
  2068. svg.element.setAttribute("focusable","false");
  2069. span.appendChild(svg.element);
  2070. svg.element = null;
  2071. //
  2072. // Handle indentalign and indentshift for single-line displays
  2073. //
  2074. if (!this.isMultiline && this.Get("display") === "block" && !svg.hasIndent) {
  2075. var values = this.getValues("indentalignfirst","indentshiftfirst","indentalign","indentshift");
  2076. if (values.indentalignfirst !== MML.INDENTALIGN.INDENTALIGN) {values.indentalign = values.indentalignfirst}
  2077. if (values.indentalign === MML.INDENTALIGN.AUTO) {values.indentalign = this.displayAlign}
  2078. if (values.indentshiftfirst !== MML.INDENTSHIFT.INDENTSHIFT) {values.indentshift = values.indentshiftfirst}
  2079. if (values.indentshift === "auto") {values.indentshift = "0"}
  2080. var shift = SVG.length2em(values.indentshift,1,SVG.cwidth);
  2081. if (this.displayIndent !== "0") {
  2082. var indent = SVG.length2em(this.displayIndent,1,SVG.cwidth);
  2083. shift += (values.indentalign === MML.INDENTALIGN.RIGHT ? -indent : indent);
  2084. }
  2085. div.style.textAlign = values.indentalign;
  2086. if (shift) {
  2087. HUB.Insert(style,({
  2088. left: {marginLeft: SVG.Ex(shift)},
  2089. right: {marginRight: SVG.Ex(-shift), marginLeft: SVG.Ex(Math.max(0,shift-w))},
  2090. center: {marginLeft: SVG.Ex(shift), marginRight: SVG.Ex(-shift)}
  2091. })[values.indentalign]);
  2092. }
  2093. }
  2094. }
  2095. return span;
  2096. }
  2097. });
  2098. MML.TeXAtom.Augment({
  2099. toSVG: function (HW,D) {
  2100. this.SVGgetStyles();
  2101. var svg = this.SVG();
  2102. this.SVGhandleSpace(svg);
  2103. if (this.data[0] != null) {
  2104. var box = this.SVGdataStretched(0,HW,D), y = 0;
  2105. if (this.texClass === MML.TEXCLASS.VCENTER)
  2106. {y = SVG.TeX.axis_height - (box.h+box.d)/2 + box.d}
  2107. svg.Add(box,0,y);
  2108. svg.ic = box.ic; svg.skew = box.skew;
  2109. }
  2110. this.SVGhandleColor(svg);
  2111. this.SVGsaveData(svg);
  2112. return svg;
  2113. }
  2114. });
  2115. //
  2116. // Make sure these don't generate output
  2117. //
  2118. MML.maligngroup.Augment({toSVG: MML.mbase.SVGemptySVG});
  2119. MML.malignmark.Augment({toSVG: MML.mbase.SVGemptySVG});
  2120. MML.mprescripts.Augment({toSVG: MML.mbase.SVGemptySVG});
  2121. MML.none.Augment({toSVG: MML.mbase.SVGemptySVG});
  2122. //
  2123. // Loading isn't complete until the element jax is modified,
  2124. // but can't call loadComplete within the callback for "mml Jax Ready"
  2125. // (it would call SVG's Require routine, asking for the mml jax again)
  2126. // so wait until after the mml jax has finished processing.
  2127. //
  2128. // We also need to wait for the onload handler to run, since the loadComplete
  2129. // will call Config and Startup, which need to modify the body.
  2130. //
  2131. HUB.Register.StartupHook("onLoad",function () {
  2132. setTimeout(MathJax.Callback(["loadComplete",SVG,"jax.js"]),0);
  2133. });
  2134. });
  2135. HUB.Browser.Select({
  2136. Opera: function (browser) {
  2137. SVG.Augment({
  2138. operaZoomRefresh: true // Opera needs a kick to redraw zoomed equations
  2139. });
  2140. }
  2141. });
  2142. HUB.Register.StartupHook("End Cookie", function () {
  2143. if (HUB.config.menuSettings.zoom !== "None")
  2144. {AJAX.Require("[MathJax]/extensions/MathZoom.js")}
  2145. });
  2146. if (!document.createElementNS) {
  2147. //
  2148. // Try to handle SVG in IE8 and below, but fail
  2149. // (but don't crash on loading the file, so no delay for loadComplete)
  2150. //
  2151. if (!document.namespaces.svg) {document.namespaces.add("svg",SVGNS)}
  2152. SVG.Augment({
  2153. Element: function (type,def) {
  2154. var obj = (typeof(type) === "string" ? document.createElement("svg:"+type) : type);
  2155. obj.isMathJax = true;
  2156. if (def) {for (var id in def) {if (def.hasOwnProperty(id)) {obj.setAttribute(id,def[id].toString())}}}
  2157. return obj;
  2158. }
  2159. });
  2160. }
  2161. })(MathJax.Ajax, MathJax.Hub, MathJax.HTML, MathJax.OutputJax.SVG);