jax.js 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810
  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/CommonHTML/jax.js
  6. *
  7. * Implements the CommonHTML OutputJax that displays mathematics
  8. * using HTML and CSS to position the characters from math fonts
  9. * in their proper locations. Unlike the HTML-CSS output jax,
  10. * this HTML is browser and OS independent.
  11. *
  12. * ---------------------------------------------------------------------
  13. *
  14. * Copyright (c) 2013-2017 The MathJax Consortium
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. (function (AJAX,HUB,HTML,CHTML) {
  29. var MML;
  30. var isArray = MathJax.Object.isArray;
  31. var EVENT, TOUCH, HOVER; // filled in later
  32. var STRUTHEIGHT = 1,
  33. EFUZZ = .1, // overlap needed for stretchy delimiters
  34. HFUZZ = .025, DFUZZ = .025; // adjustments to bounding box of character boxes
  35. var STYLES = {
  36. ".mjx-chtml": {
  37. display: "inline-block",
  38. "line-height": 0,
  39. "text-indent": 0,
  40. "text-align": "left",
  41. "text-transform": "none",
  42. "font-style": "normal",
  43. "font-weight": "normal",
  44. "font-size": "100%",
  45. "font-size-adjust":"none",
  46. "letter-spacing": "normal",
  47. "word-wrap": "normal",
  48. "word-spacing": "normal",
  49. "white-space": "nowrap",
  50. "float": "none",
  51. "direction": "ltr",
  52. "max-width": "none",
  53. "max-height": "none",
  54. "min-width": 0,
  55. "min-height": 0,
  56. border: 0,
  57. margin: 0,
  58. padding: "1px 0"
  59. },
  60. ".MJXc-display": {
  61. display: "block",
  62. "text-align": "center",
  63. "margin": "1em 0",
  64. padding: 0
  65. },
  66. ".mjx-chtml[tabindex]:focus, body :focus .mjx-chtml[tabindex]": {
  67. display: "inline-table" // see issues #1282 and #1338
  68. },
  69. ".mjx-full-width": {
  70. "text-align": "center",
  71. display: "table-cell!important",
  72. width: "10000em"
  73. },
  74. ".mjx-math": {
  75. "display": "inline-block",
  76. "border-collapse": "separate",
  77. "border-spacing": 0
  78. },
  79. ".mjx-math *": {
  80. display:"inline-block",
  81. "-webkit-box-sizing": "content-box!important",
  82. "-moz-box-sizing": "content-box!important",
  83. "box-sizing": "content-box!important", // override bootstrap settings
  84. "text-align":"left"
  85. },
  86. ".mjx-numerator": {display:"block", "text-align":"center"},
  87. ".mjx-denominator": {display:"block", "text-align":"center"},
  88. ".MJXc-stacked": {height:0, position:"relative"},
  89. ".MJXc-stacked > *": {position: "absolute"},
  90. ".MJXc-bevelled > *": {display:"inline-block"},
  91. ".mjx-stack": {display:"inline-block"},
  92. ".mjx-op": {display:"block"},
  93. ".mjx-under": {display:"table-cell"},
  94. ".mjx-over": {display:"block"},
  95. ".mjx-over > *": {"padding-left":"0px!important", "padding-right":"0px!important"},
  96. ".mjx-under > *": {"padding-left":"0px!important", "padding-right":"0px!important"},
  97. ".mjx-stack > .mjx-sup": {display:"block"},
  98. ".mjx-stack > .mjx-sub": {display:"block"},
  99. ".mjx-prestack > .mjx-presup": {display:"block"},
  100. ".mjx-prestack > .mjx-presub": {display:"block"},
  101. ".mjx-delim-h > .mjx-char": {display:"inline-block"},
  102. ".mjx-surd": {"vertical-align":"top"},
  103. ".mjx-mphantom *": {visibility:"hidden"},
  104. ".mjx-merror": {
  105. "background-color":"#FFFF88",
  106. color: "#CC0000",
  107. border: "1px solid #CC0000",
  108. padding: "2px 3px",
  109. "font-style": "normal",
  110. "font-size": "90%"
  111. },
  112. ".mjx-annotation-xml": {"line-height":"normal"},
  113. ".mjx-menclose > svg": {fill:"none", stroke:"currentColor"},
  114. ".mjx-mtr": {display:"table-row"},
  115. ".mjx-mlabeledtr": {display:"table-row"},
  116. ".mjx-mtd": {display:"table-cell", "text-align":"center"},
  117. ".mjx-label": {display:"table-row"},
  118. ".mjx-box": {display:"inline-block"},
  119. ".mjx-block": {display:"block"},
  120. ".mjx-span": {display:"inline"},
  121. ".mjx-char": {display:"block", "white-space":"pre"},
  122. ".mjx-itable": {display:"inline-table", width:"auto"},
  123. ".mjx-row": {display:"table-row"},
  124. ".mjx-cell": {display:"table-cell"},
  125. ".mjx-table": {display:"table", width:"100%"},
  126. ".mjx-line": {display:"block", height:0},
  127. ".mjx-strut": {width:0, "padding-top":STRUTHEIGHT+"em"},
  128. ".mjx-vsize": {width:0},
  129. ".MJXc-space1": {"margin-left":".167em"},
  130. ".MJXc-space2": {"margin-left":".222em"},
  131. ".MJXc-space3": {"margin-left":".278em"},
  132. ".mjx-chartest": {
  133. display:"block",
  134. visibility: "hidden",
  135. position:"absolute", top:0,
  136. "line-height":"normal",
  137. "font-size":"500%"
  138. },
  139. ".mjx-chartest .mjx-char": {display:"inline"},
  140. ".mjx-chartest .mjx-box": {"padding-top": "1000px"},
  141. ".MJXc-processing": {
  142. visibility: "hidden", position:"fixed",
  143. width: 0, height: 0, overflow:"hidden"
  144. },
  145. ".MJXc-processed": {display:"none"},
  146. ".mjx-test": {
  147. display: "block",
  148. "font-style": "normal",
  149. "font-weight": "normal",
  150. "font-size": "100%",
  151. "font-size-adjust":"none",
  152. "text-indent": 0,
  153. "text-transform": "none",
  154. "letter-spacing": "normal",
  155. "word-spacing": "normal",
  156. overflow: "hidden",
  157. height: "1px"
  158. },
  159. ".mjx-ex-box-test": {
  160. position: "absolute",
  161. overflow: "hidden",
  162. width:"1px", height:"60ex"
  163. },
  164. ".mjx-line-box-test": {display: "table!important"},
  165. ".mjx-line-box-test span": {
  166. display: "table-cell!important",
  167. width: "10000em!important",
  168. "min-width":0, "max-width":"none",
  169. padding:0, border:0, margin:0
  170. },
  171. "#MathJax_CHTML_Tooltip": {
  172. "background-color": "InfoBackground", color: "InfoText",
  173. border: "1px solid black",
  174. "box-shadow": "2px 2px 5px #AAAAAA", // Opera 10.5
  175. "-webkit-box-shadow": "2px 2px 5px #AAAAAA", // Safari 3 and Chrome
  176. "-moz-box-shadow": "2px 2px 5px #AAAAAA", // Firefox 3.5
  177. "-khtml-box-shadow": "2px 2px 5px #AAAAAA", // Konqueror
  178. padding: "3px 4px",
  179. "z-index": 401,
  180. position: "absolute", left: 0, top: 0,
  181. width: "auto", height: "auto",
  182. display: "none"
  183. }
  184. };
  185. /************************************************************/
  186. var BIGDIMEN = 1000000;
  187. var MAXREMAP = 5;
  188. var LINEBREAKS = {}, CONFIG = MathJax.Hub.config;
  189. CHTML.Augment({
  190. settings: HUB.config.menuSettings,
  191. config: {styles: STYLES},
  192. /********************************************/
  193. Config: function () {
  194. if (!this.require) {this.require = []}
  195. this.SUPER(arguments).Config.call(this); var settings = this.settings;
  196. if (settings.scale) {this.config.scale = settings.scale}
  197. this.require.push(this.fontDir+"/TeX/fontdata.js");
  198. this.require.push(MathJax.OutputJax.extensionDir+"/MathEvents.js");
  199. LINEBREAKS = this.config.linebreaks;
  200. },
  201. Startup: function () {
  202. //
  203. // Set up event handling
  204. //
  205. EVENT = MathJax.Extension.MathEvents.Event;
  206. TOUCH = MathJax.Extension.MathEvents.Touch;
  207. HOVER = MathJax.Extension.MathEvents.Hover;
  208. this.ContextMenu = EVENT.ContextMenu;
  209. this.Mousedown = EVENT.AltContextMenu;
  210. this.Mouseover = HOVER.Mouseover;
  211. this.Mouseout = HOVER.Mouseout;
  212. this.Mousemove = HOVER.Mousemove;
  213. //
  214. // Determine pixels per inch
  215. //
  216. var div = CHTML.addElement(document.body,"mjx-block",{style:{display:"block",width:"5in"}});
  217. this.pxPerInch = div.offsetWidth/5; div.parentNode.removeChild(div);
  218. //
  219. // Used in preTranslate to get scaling factors and line width
  220. //
  221. this.TestSpan = CHTML.Element("mjx-test",{style:{left:"1em"}},[["mjx-ex-box-test"]]);
  222. //
  223. // Used in preTranslate to get linebreak width
  224. //
  225. this.linebreakSpan = HTML.Element("span",{className:"mjx-line-box-test"},[["span"]]);
  226. //
  227. // Set up styles and preload web fonts
  228. //
  229. return AJAX.Styles(this.config.styles,["InitializeCHTML",this]);
  230. },
  231. InitializeCHTML: function () {
  232. this.getDefaultExEm();
  233. //
  234. // If the defaultEm size is zero, it might be that a web font hasn't
  235. // arrived yet, so try to wait for it, but don't wait too long.
  236. //
  237. if (this.defaultEm) return;
  238. var ready = MathJax.Callback();
  239. AJAX.timer.start(AJAX,function (check) {
  240. if (check.time(ready)) {HUB.signal.Post(["CommonHTML Jax - no default em size"]); return}
  241. CHTML.getDefaultExEm();
  242. if (CHTML.defaultEm) {ready()} else {setTimeout(check,check.delay)}
  243. },this.defaultEmDelay,this.defaultEmTimeout);
  244. return ready;
  245. },
  246. defaultEmDelay: 100, // initial delay when checking for defaultEm
  247. defaultEmTimeout: 1000, // when to stop looking for defaultEm
  248. getDefaultExEm: function () {
  249. //
  250. // Get the default sizes (need styles in place to do this)
  251. //
  252. document.body.appendChild(this.TestSpan);
  253. document.body.appendChild(this.linebreakSpan);
  254. this.defaultEm = this.getFontSize(this.TestSpan);
  255. this.defaultEx = this.TestSpan.firstChild.offsetHeight/60;
  256. this.defaultWidth = this.linebreakSpan.firstChild.offsetWidth;
  257. document.body.removeChild(this.linebreakSpan);
  258. document.body.removeChild(this.TestSpan);
  259. },
  260. getFontSize: (window.getComputedStyle ?
  261. function (node) {
  262. var style = window.getComputedStyle(node);
  263. return parseFloat(style.fontSize);
  264. } :
  265. //
  266. // IE 8 doesn't do getComputedStyle, so use
  267. // an alternative approach
  268. //
  269. function (node) {
  270. return node.style.pixelLeft;
  271. }
  272. ),
  273. getMaxWidth: (window.getComputedStyle ?
  274. function (node) {
  275. var style = window.getComputedStyle(node);
  276. if (style.maxWidth !== "none") return parseFloat(style.maxWidth);
  277. return 0;
  278. } :
  279. //
  280. // IE 8 doesn't do getComputedStyle, so use
  281. // currentStyle, and a hack to get the pixels for
  282. // a non-px max-width
  283. //
  284. function (node) {
  285. var max = node.currentStyle.maxWidth;
  286. if (max !== "none") {
  287. if (max.match(/\d*px/)) return parseFloat(max);
  288. var left = node.style.left;
  289. node.style.left = max; max = node.style.pixelLeft;
  290. node.style.left = left;
  291. return max;
  292. }
  293. return 0;
  294. }
  295. ),
  296. //
  297. // Load data for a font
  298. //
  299. loadFont: function (font) {
  300. HUB.RestartAfter(AJAX.Require(this.fontDir+"/"+font));
  301. },
  302. //
  303. // Signal that the font data are loaded
  304. //
  305. fontLoaded: function (font) {
  306. if (!font.match(/-|fontdata/)) font += "-Regular";
  307. if (!font.match(/\.js$/)) font += ".js"
  308. MathJax.Callback.Queue(
  309. ["Post",HUB.Startup.signal,["CommonHTML - font data loaded",font]],
  310. ["loadComplete",AJAX,this.fontDir+"/"+font]
  311. );
  312. },
  313. Element: function (type,def,content) {
  314. if (type.substr(0,4) === "mjx-") {
  315. if (!def) def = {};
  316. if (def.isMathJax == null) def.isMathJax = true;
  317. if (def.className) def.className = type+" "+def.className; else def.className = type;
  318. type = "span";
  319. }
  320. return this.HTMLElement(type,def,content);
  321. },
  322. addElement: function (node,type,def,content) {
  323. return node.appendChild(this.Element(type,def,content));
  324. },
  325. HTMLElement: HTML.Element,
  326. ucMatch: HTML.ucMatch,
  327. setScript: HTML.setScript,
  328. //
  329. // Look through the direct children of a node for one with the given
  330. // type (but if the node has intervening containers for its children,
  331. // step into them; note that elements corresponding to MathML nodes
  332. // will have id's so we don't step into them).
  333. //
  334. // This is used by munderover and msubsup to locate their child elements
  335. // when they are part of an embellished operator that is being stretched.
  336. // We don't use querySelector because we want to find only the direct child
  337. // nodes, not nodes that might be nested deeper in the tree (see issue #1447).
  338. //
  339. getNode: function (node,type) {
  340. var name = RegExp("\\b"+type+"\\b");
  341. var nodes = [];
  342. while (node) {
  343. for (var i = 0, m = node.childNodes.length; i < m; i++) {
  344. var child = node.childNodes[i];
  345. if (child) {
  346. if (name.test(child.className)) return child;
  347. if (child.id === "") nodes.push(child);
  348. }
  349. }
  350. node = nodes.shift();
  351. }
  352. return null;
  353. },
  354. /********************************************/
  355. preTranslate: function (state) {
  356. var scripts = state.jax[this.id], i, m = scripts.length,
  357. script, prev, node, test, span, jax, ex, em, scale;
  358. //
  359. // Get linebreaking information
  360. //
  361. var maxwidth = 100000, relwidth = false, cwidth = 0,
  362. linebreak = LINEBREAKS.automatic, width = LINEBREAKS.width;
  363. if (linebreak) {
  364. relwidth = !!width.match(/^\s*(\d+(\.\d*)?%\s*)?container\s*$/);
  365. if (relwidth) {width = width.replace(/\s*container\s*/,"")}
  366. else {maxwidth = this.defaultWidth}
  367. if (width === "") {width = "100%"}
  368. }
  369. //
  370. // Loop through the scripts
  371. //
  372. for (i = 0; i < m; i++) {
  373. script = scripts[i]; if (!script.parentNode) continue;
  374. //
  375. // Remove any existing output
  376. //
  377. prev = script.previousSibling;
  378. if (prev && prev.className && String(prev.className).substr(0,9) === "mjx-chtml")
  379. prev.parentNode.removeChild(prev);
  380. if (script.MathJax.preview) script.MathJax.preview.style.display = "none";
  381. //
  382. // Add the node for the math and mark it as being processed
  383. //
  384. jax = script.MathJax.elementJax; if (!jax) continue;
  385. jax.CHTML = {
  386. display: (jax.root.Get("display") === "block"),
  387. preview: (jax.CHTML||{}).preview // in case typeset calls are interleaved
  388. };
  389. node = CHTML.Element("mjx-chtml",{
  390. id:jax.inputID+"-Frame", className:"MathJax_CHTML", isMathJax:true, jaxID:this.id,
  391. oncontextmenu:EVENT.Menu, onmousedown: EVENT.Mousedown,
  392. onmouseover:EVENT.Mouseover, onmouseout:EVENT.Mouseout, onmousemove:EVENT.Mousemove,
  393. onclick:EVENT.Click, ondblclick:EVENT.DblClick,
  394. // Added for keyboard accessible menu.
  395. onkeydown: EVENT.Keydown, tabIndex: HUB.getTabOrder(jax)
  396. });
  397. if (jax.CHTML.display) {
  398. //
  399. // Zoom box requires an outer container to get the positioning right.
  400. //
  401. var NODE = CHTML.Element("mjx-chtml",{className:"MJXc-display",isMathJax:false});
  402. NODE.appendChild(node); node = NODE;
  403. }
  404. if (HUB.Browser.noContextMenu) {
  405. node.ontouchstart = TOUCH.start;
  406. node.ontouchend = TOUCH.end;
  407. }
  408. //
  409. node.className += " MJXc-processing";
  410. script.parentNode.insertBefore(node,script);
  411. //
  412. // Add test nodes for determineing scales and linebreak widths
  413. //
  414. script.parentNode.insertBefore(this.linebreakSpan.cloneNode(true),script);
  415. script.parentNode.insertBefore(this.TestSpan.cloneNode(true),script);
  416. }
  417. //
  418. // Determine the scaling factors for each script
  419. // (this only requires one reflow rather than a reflow for each equation)
  420. //
  421. for (i = 0; i < m; i++) {
  422. script = scripts[i]; if (!script.parentNode) continue;
  423. test = script.previousSibling;
  424. jax = script.MathJax.elementJax; if (!jax) continue;
  425. em = CHTML.getFontSize(test);
  426. ex = test.firstChild.offsetHeight/60;
  427. cwidth = Math.max(0,test.previousSibling.firstChild.offsetWidth-2);
  428. if (ex === 0 || ex === "NaN") {
  429. ex = this.defaultEx;
  430. cwidth = this.defaultWidth;
  431. }
  432. if (relwidth) maxwidth = cwidth;
  433. scale = (this.config.matchFontHeight ? ex/this.TEX.x_height/em : 1);
  434. scale = Math.floor(Math.max(this.config.minScaleAdjust/100,scale)*this.config.scale);
  435. jax.CHTML.scale = scale/100; jax.CHTML.fontSize = scale+"%";
  436. jax.CHTML.outerEm = em; jax.CHTML.em = this.em = em * scale/100;
  437. jax.CHTML.ex = ex; jax.CHTML.cwidth = cwidth/this.em;
  438. jax.CHTML.lineWidth = (linebreak ? this.length2em(width,maxwidth/this.em,1) : maxwidth);
  439. }
  440. //
  441. // Remove the test spans used for determining scales and linebreak widths
  442. //
  443. for (i = 0; i < m; i++) {
  444. script = scripts[i]; if (!script.parentNode) continue;
  445. test = script.previousSibling;
  446. span = test.previousSibling;
  447. jax = script.MathJax.elementJax; if (!jax) continue;
  448. span.parentNode.removeChild(span);
  449. test.parentNode.removeChild(test);
  450. if (script.MathJax.preview) script.MathJax.preview.style.display = "";
  451. }
  452. state.CHTMLeqn = state.CHTMLlast = 0; state.CHTMLi = -1;
  453. state.CHTMLchunk = this.config.EqnChunk;
  454. state.CHTMLdelay = false;
  455. },
  456. /********************************************/
  457. Translate: function (script,state) {
  458. if (!script.parentNode) return;
  459. //
  460. // If we are supposed to do a chunk delay, do it
  461. //
  462. if (state.CHTMLdelay) {
  463. state.CHTMLdelay = false;
  464. HUB.RestartAfter(MathJax.Callback.Delay(this.config.EqnChunkDelay));
  465. }
  466. //
  467. // Get the data about the math
  468. //
  469. var jax = script.MathJax.elementJax, math = jax.root,
  470. node = document.getElementById(jax.inputID+"-Frame");
  471. if (!node) return;
  472. this.getMetrics(jax);
  473. if (this.scale !== 1) node.style.fontSize = jax.CHTML.fontSize;
  474. //
  475. // Typeset the math
  476. //
  477. this.initCHTML(math,node);
  478. this.savePreview(script);
  479. this.CHTMLnode = node;
  480. try {
  481. math.setTeXclass();
  482. math.toCommonHTML(node);
  483. } catch (err) {
  484. while (node.firstChild) node.removeChild(node.firstChild);
  485. delete this.CHTMLnode;
  486. this.restorePreview(script);
  487. throw err;
  488. }
  489. delete this.CHTMLnode;
  490. this.restorePreview(script);
  491. //
  492. // Put it in place, and remove the processing marker
  493. //
  494. if (jax.CHTML.display) node = node.parentNode;
  495. node.className = node.className.replace(/ [^ ]+$/,"");
  496. //
  497. // Hide the math and don't let its preview be removed
  498. //
  499. node.className += " MJXc-processed";
  500. if (script.MathJax.preview) {
  501. jax.CHTML.preview = script.MathJax.preview;
  502. delete script.MathJax.preview;
  503. }
  504. //
  505. // Check if we should show this chunk of equations
  506. //
  507. state.CHTMLeqn += (state.i - state.CHTMLi); state.CHTMLi = state.i;
  508. if (state.CHTMLeqn >= state.CHTMLlast + state.CHTMLchunk) {
  509. this.postTranslate(state);
  510. state.CHTMLchunk = Math.floor(state.CHTMLchunk*this.config.EqnChunkFactor);
  511. state.CHTMLdelay = true; // delay if there are more scripts
  512. }
  513. },
  514. initCHTML: function (math,node) {},
  515. //
  516. // MathML previews can contain the same ID's as the HTML output,
  517. // which confuses CHTMLnodeElement(), so remove the preview temporarily
  518. // and restore it after typesetting the math.
  519. //
  520. savePreview: function (script) {
  521. var preview = script.MathJax.preview;
  522. if (preview && preview.parentNode) {
  523. script.MathJax.tmpPreview = document.createElement("span");
  524. preview.parentNode.replaceChild(script.MathJax.tmpPreview,preview);
  525. }
  526. },
  527. restorePreview: function (script) {
  528. var tmpPreview = script.MathJax.tmpPreview;
  529. if (tmpPreview) {
  530. tmpPreview.parentNode.replaceChild(script.MathJax.preview,tmpPreview);
  531. delete script.MathJax.tmpPreview;
  532. }
  533. },
  534. //
  535. // Get the jax metric information
  536. //
  537. getMetrics: function(jax) {
  538. var data = jax.CHTML;
  539. this.jax = jax;
  540. this.em = data.em;
  541. this.outerEm = data.outerEm;
  542. this.scale = data.scale;
  543. this.cwidth = data.cwidth;
  544. this.linebreakWidth = data.lineWidth;
  545. },
  546. /********************************************/
  547. postTranslate: function (state) {
  548. var scripts = state.jax[this.id];
  549. //
  550. // Reveal this chunk of math
  551. //
  552. for (var i = state.CHTMLlast, m = state.CHTMLeqn; i < m; i++) {
  553. var script = scripts[i];
  554. if (script && script.MathJax.elementJax) {
  555. //
  556. // Remove the processed marker
  557. //
  558. script.previousSibling.className = script.previousSibling.className.replace(/ [^ ]+$/,"");
  559. var data = script.MathJax.elementJax.CHTML;
  560. //
  561. // Remove the preview, if any
  562. //
  563. if (data.preview) {
  564. data.preview.innerHTML = "";
  565. data.preview.style.display = "none";
  566. script.MathJax.preview = data.preview;
  567. delete data.preview;
  568. }
  569. }
  570. }
  571. //
  572. // Save our place so we know what is revealed
  573. //
  574. state.CHTMLlast = state.CHTMLeqn;
  575. },
  576. /********************************************/
  577. getJaxFromMath: function (math) {
  578. if (math.parentNode.className.match(/MJXc-display/)) math = math.parentNode;
  579. do {math = math.nextSibling} while (math && math.nodeName.toLowerCase() !== "script");
  580. return HUB.getJaxFor(math);
  581. },
  582. getHoverSpan: function (jax,math) {return jax.root.CHTMLnodeElement()},
  583. getHoverBBox: function (jax,span,math) {
  584. var bbox = jax.root.CHTML, em = jax.CHTML.outerEm;
  585. var BBOX = {w:bbox.w*em, h:bbox.h*em, d:bbox.d*em};
  586. if (bbox.width) {BBOX.width = bbox.width}
  587. return BBOX;
  588. },
  589. Zoom: function (jax,span,math,Mw,Mh) {
  590. //
  591. // Re-render at larger size
  592. //
  593. this.getMetrics(jax);
  594. var node = CHTML.addElement(span,"mjx-chtml",{style:{"font-size":Math.floor(CHTML.scale*100)+"%"},isMathJax:false});
  595. CHTML.CHTMLnode = node;
  596. this.idPostfix = "-zoom"; jax.root.toCommonHTML(node); this.idPostfix = "";
  597. //
  598. // Adjust margins to prevent overlaps at the edges
  599. //
  600. var style = node.style, bbox = jax.root.CHTML;
  601. if (bbox.t > bbox.h) style.marginTop = CHTML.Em(bbox.t-bbox.h);
  602. if (bbox.b > bbox.d) style.marginBottom = CHTML.Em(bbox.b-bbox.d);
  603. if (bbox.l < 0) style.paddingLeft = CHTML.Em(-bbox.l);
  604. if (bbox.r > bbox.w) style.marginRight = CHTML.Em(bbox.r-bbox.w);
  605. //
  606. // Get height and width of zoomed math and original math
  607. //
  608. style.position = "absolute";
  609. var zW = node.offsetWidth, zH = node.offsetHeight,
  610. mH = math.firstChild.offsetHeight, mW = math.firstChild.offsetWidth;
  611. node.style.position = "";
  612. //
  613. return {Y:-EVENT.getBBox(span).h, mW:mW, mH:mH, zW:zW, zH:zH};
  614. },
  615. Remove: function (jax) {
  616. var node = document.getElementById(jax.inputID+"-Frame");
  617. if (node && jax.CHTML.display) node = node.parentNode;
  618. if (node) node.parentNode.removeChild(node);
  619. delete jax.CHTML;
  620. },
  621. /********************************************/
  622. ID: 0, idPostfix: "",
  623. GetID: function () {this.ID++; return this.ID},
  624. /********************************************/
  625. MATHSPACE: {
  626. veryverythinmathspace: 1/18,
  627. verythinmathspace: 2/18,
  628. thinmathspace: 3/18,
  629. mediummathspace: 4/18,
  630. thickmathspace: 5/18,
  631. verythickmathspace: 6/18,
  632. veryverythickmathspace: 7/18,
  633. negativeveryverythinmathspace: -1/18,
  634. negativeverythinmathspace: -2/18,
  635. negativethinmathspace: -3/18,
  636. negativemediummathspace: -4/18,
  637. negativethickmathspace: -5/18,
  638. negativeverythickmathspace: -6/18,
  639. negativeveryverythickmathspace: -7/18,
  640. thin: .04,
  641. medium: .06,
  642. thick: .1,
  643. infinity: BIGDIMEN
  644. },
  645. SPACECLASS: {
  646. thinmathspace: "MJXc-space1",
  647. mediummathspace: "MJXc-space2",
  648. thickmathspace: "MJXc-space3"
  649. },
  650. pxPerInch: 96,
  651. em: 16,
  652. maxStretchyParts: 1000, // limit the number of parts allowed for
  653. // stretchy operators. See issue 366.
  654. FONTDEF: {},
  655. TEXDEF: {
  656. x_height: .442,
  657. quad: 1,
  658. num1: .676508,
  659. num2: .393732,
  660. num3: .44373,
  661. denom1: .685951,
  662. denom2: .344841,
  663. sup1: .412892,
  664. sup2: .362892,
  665. sup3: .288888,
  666. sub1: .15,
  667. sub2: .247217,
  668. sup_drop: .386108,
  669. sub_drop: .05,
  670. delim1: 2.39,
  671. delim2: 1.0,
  672. axis_height: .25,
  673. rule_thickness: .06,
  674. big_op_spacing1: .111111,
  675. big_op_spacing2: .166666,
  676. big_op_spacing3: .2,
  677. big_op_spacing4: .45, //.6, // better spacing for under arrows and braces
  678. big_op_spacing5: .1,
  679. surd_height: .075,
  680. scriptspace: .05,
  681. nulldelimiterspace: .12,
  682. delimiterfactor: 901,
  683. delimitershortfall: .3,
  684. min_rule_thickness: 1.25 // in pixels
  685. },
  686. /********************************************************/
  687. //
  688. // Get a unicode character by number (even when it takes two character)
  689. //
  690. unicodeChar: function (n) {
  691. if (n < 0xFFFF) return String.fromCharCode(n);
  692. n -= 0x10000;
  693. return String.fromCharCode((n>>10)+0xD800) + String.fromCharCode((n&0x3FF)+0xDC00);
  694. },
  695. //
  696. // Get the unicode number of a (possibly multi-character) string
  697. //
  698. getUnicode: function (string) {
  699. var n = string.text.charCodeAt(string.i); string.i++;
  700. if (n >= 0xD800 && n < 0xDBFF) {
  701. n = (((n-0xD800)<<10)+(string.text.charCodeAt(string.i)-0xDC00))+0x10000;
  702. string.i++;
  703. }
  704. return n;
  705. },
  706. //
  707. // Get the list of actions for a given character in a given variant
  708. // (processing remaps, multi-character results, and so on). Results are
  709. // cached so that future lookups for the same variant/n pair will not
  710. // require looking through the data again.
  711. //
  712. getCharList: function (variant,n) {
  713. var id, M, cache = variant.cache, nn = n;
  714. if (cache[n]) return cache[n];
  715. if (n > 0xFFFF && this.FONTDATA.RemapPlane1) {
  716. var nv = this.FONTDATA.RemapPlane1(n,variant);
  717. n = nv.n; variant = nv.variant;
  718. }
  719. var RANGES = this.FONTDATA.RANGES, VARIANT = this.FONTDATA.VARIANT;
  720. if (n >= RANGES[0].low && n <= RANGES[RANGES.length-1].high) {
  721. for (id = 0, M = RANGES.length; id < M; id++) {
  722. if (RANGES[id].name === "alpha" && variant.noLowerCase) continue;
  723. var N = variant["offset"+RANGES[id].offset];
  724. if (N && n >= RANGES[id].low && n <= RANGES[id].high) {
  725. if (RANGES[id].remap && RANGES[id].remap[n]) {
  726. n = N + RANGES[id].remap[n];
  727. } else {
  728. n = n - RANGES[id].low + N;
  729. if (RANGES[id].add) {n += RANGES[id].add}
  730. }
  731. if (variant["variant"+RANGES[id].offset])
  732. variant = VARIANT[variant["variant"+RANGES[id].offset]];
  733. break;
  734. }
  735. }
  736. }
  737. cache[nn] = this.remapChar(variant,n,0);
  738. return cache[nn];
  739. },
  740. remapChar: function (variant,n,N) {
  741. var list = [], VARIANT = this.FONTDATA.VARIANT;
  742. if (variant.remap && variant.remap[n]) {
  743. n = variant.remap[n];
  744. if (variant.remap.variant) {variant = VARIANT[variant.remap.variant]}
  745. } else if (this.FONTDATA.REMAP[n] && !variant.noRemap) {
  746. n = this.FONTDATA.REMAP[n];
  747. }
  748. if (isArray(n)) {
  749. if (n[2]) N = MAXREMAP; // stop remapping
  750. variant = VARIANT[n[1]]; n = n[0];
  751. }
  752. if (typeof(n) === "string") {
  753. var string = {text:n, i:0, length:n.length};
  754. while (string.i < string.length) {
  755. n = this.getUnicode(string);
  756. var chars = this.getCharList(variant,n);
  757. if (chars) list.push.apply(list,chars);
  758. }
  759. } else {
  760. if (variant.cache[n]) {list = variant.cache[n]}
  761. else {variant.cache[n] = list = this.lookupChar(variant,n,N)}
  762. }
  763. return list;
  764. },
  765. //
  766. // After all remapping has been done, look up a character
  767. // in the fonts for a given variant, chaining to other
  768. // variants as needed. Return an undefined character if
  769. // it isnt' found in the given variant.
  770. //
  771. lookupChar: function (variant,n,N) {
  772. var VARIANT = variant;
  773. while (variant) {
  774. for (var i = 0, m = variant.fonts.length; i < m; i++) {
  775. var font = this.FONTDATA.FONTS[variant.fonts[i]];
  776. if (typeof(font) === "string") this.loadFont(font);
  777. var C = font[n];
  778. if (C) {
  779. this.fixChar(C,n);
  780. if (C[5].space) return [{type:"space", w:C[2], font:font}];
  781. return [{type:"char", font:font, n:n}];
  782. } else if (font.Extra) {
  783. this.findBlock(font,n);
  784. }
  785. }
  786. variant = this.FONTDATA.VARIANT[variant.chain];
  787. if (variant && variant.remap && variant.remap[n] && N++ < MAXREMAP) {
  788. return this.remapChar(variant,n,N);
  789. }
  790. }
  791. return [this.unknownChar(VARIANT,n)];
  792. },
  793. fixChar: function (C,n) {
  794. if (C.length === 5) C[5] = {};
  795. if (C.c == null) {
  796. C[0] /= 1000; C[1] /= 1000; C[2] /= 1000; C[3] /= 1000; C[4] /= 1000;
  797. C.c = this.unicodeChar(n);
  798. }
  799. return C;
  800. },
  801. findBlock: function (font,n) {
  802. var extra = font.Extra, name = font.file, file;
  803. for (var i = 0, m = extra.length; i < m; i++) {
  804. if (typeof(extra[i]) === "number") {
  805. if (n === extra[i]) {file = name; break}
  806. } else {
  807. if (n < extra[i][0]) return;
  808. if (n <= extra[i][1]) {file = name; break}
  809. }
  810. }
  811. //
  812. // Currently this only loads one extra file, but that
  813. // might need to be expanded in the future.
  814. //
  815. if (file) {delete font.Extra; this.loadFont(name)}
  816. },
  817. //
  818. // Create a fake font entry for an unknown character.
  819. //
  820. unknownChar: function (variant,n) {
  821. HUB.signal.Post(["CommonHTML Jax - unknown char",n,variant]);
  822. var id = ""; if (variant.bold) id += "B"; if (variant.italic) id += "I";
  823. var unknown = this.FONTDATA.UNKNOWN[id||"R"]; // cache of previously measured characters
  824. if (!unknown[n]) this.getUnknownChar(unknown,n);
  825. return {type:"unknown", n:n, font:unknown};
  826. },
  827. getUnknownChar: function (unknown,n) {
  828. var c = this.unicodeChar(n);
  829. var HDW = this.getHDW(c,unknown.className);
  830. // ### FIXME: provide a means of setting the height and depth for individual characters
  831. unknown[n] = [.8,.2,HDW.w,0,HDW.w,{a:Math.max(0,(HDW.h-HDW.d)/2), h:HDW.h, d:HDW.d}];
  832. unknown[n].c = c;
  833. },
  834. styledText: function (variant,text) {
  835. HUB.signal.Post(["CommonHTML Jax - styled text",text,variant]);
  836. var style = variant.style;
  837. var id = "_"+(style["font-family"]||variant.className||"");
  838. if (style["font-weight"]) id += "_"+style["font-weight"];
  839. if (style["font-style"]) id += "_"+style["font-style"];
  840. if (!this.STYLEDTEXT) this.STYLEDTEXT = {};
  841. if (!this.STYLEDTEXT[id]) this.STYLEDTEXT[id] = {className:variant.className||""};
  842. var unknown = this.STYLEDTEXT[id];
  843. if (!unknown["_"+text]) {
  844. var HDW = this.getHDW(text,variant.className||"",style);
  845. unknown["_"+text] = [.8,.2,HDW.w,0,HDW.w,{a:Math.max(0,(HDW.h-HDW.d)/2), h:HDW.h, d:HDW.d}];
  846. unknown["_"+text].c = text;
  847. }
  848. return {type:"unknown", n:"_"+text, font:unknown, style:style, rscale:variant.rscale};
  849. },
  850. //
  851. // Get the height, depth, and width of a character
  852. // (height and depth are of the font, not the character).
  853. // WARNING: causes reflow of the page!
  854. //
  855. getHDW: function (c,name,styles) {
  856. var test1 = CHTML.addElement(CHTML.CHTMLnode,"mjx-chartest",{className:name},[["mjx-char",{style:styles},[c]]]);
  857. var test2 = CHTML.addElement(CHTML.CHTMLnode,"mjx-chartest",{className:name},[["mjx-char",{style:styles},[c,["mjx-box"]]]]);
  858. test1.firstChild.style.fontSize = test2.firstChild.style.fontSize = "";
  859. var em = 5*CHTML.em;
  860. var H1 = test1.offsetHeight, H2 = test2.offsetHeight, W = test1.offsetWidth;
  861. CHTML.CHTMLnode.removeChild(test1);
  862. CHTML.CHTMLnode.removeChild(test2);
  863. if (H2 === 0) {
  864. em = 5*CHTML.defaultEm;
  865. var test = document.body.appendChild(document.createElement("div"));
  866. test.appendChild(test1); test.appendChild(test2);
  867. H1 = test1.offsetHeight, H2 = test2.offsetHeight, W = test1.offsetWidth;
  868. document.body.removeChild(test);
  869. }
  870. var d = (H2-1000)/em, w = W/em, h = H1/em - d;
  871. return {h:h, d:d, w:w}
  872. },
  873. /********************************************************/
  874. //
  875. // Process a character list into a given node and return
  876. // the updated bounding box.
  877. //
  878. addCharList: function (node,list,bbox) {
  879. var state = {text:"", className:null, a:0};
  880. for (var i = 0, m = list.length; i < m; i++) {
  881. var item = list[i];
  882. if (this.charList[item.type]) (this.charList[item.type])(item,node,bbox,state,m);
  883. }
  884. if (state.text !== "") {
  885. if (node.childNodes.length) {
  886. this.charList.flushText(node,state);
  887. } else {
  888. HTML.addText(node,state.text);
  889. if (node.className) node.className += " "+state.className;
  890. else node.className = state.className;
  891. }
  892. }
  893. bbox.b = (state.flushed ? 0 : bbox.a);
  894. },
  895. //
  896. // The various item types are processed by these
  897. // functions.
  898. //
  899. charList: {
  900. //
  901. // Character from the known fonts
  902. //
  903. "char": function (item,node,bbox,state,m) {
  904. var font = item.font, remap = (font.remapCombining||{})[item.n];
  905. if (font.className === state.className) {
  906. remap = null;
  907. } else if (state.className || (remap && state.text !== "")) {
  908. this.flushText(node,state);
  909. }
  910. if (!state.a) state.a = font.centerline/1000;
  911. if (state.a > (bbox.a||0)) bbox.a = state.a;
  912. state.className = font.className;
  913. var C = font[item.n];
  914. if (remap) {
  915. var FONT = font;
  916. if (isArray(remap)) {
  917. FONT = CHTML.FONTDATA.FONTS[remap[1]];
  918. remap = remap[0];
  919. if (typeof(FONT) === 'string') CHTML.loadFont(FONT);
  920. }
  921. if (FONT[item.n]) CHTML.fixChar(FONT[item.n],item.n);
  922. C = CHTML.fixChar(FONT[remap],remap);
  923. state.className = FONT.className;
  924. }
  925. state.text += C.c;
  926. if (bbox.h < C[0]+HFUZZ) bbox.t = bbox.h = C[0]+HFUZZ;
  927. if (bbox.d < C[1]+DFUZZ) bbox.b = bbox.d = C[1]+DFUZZ;
  928. if (bbox.l > bbox.w+C[3]) bbox.l = bbox.w+C[3];
  929. if (bbox.r < bbox.w+C[4]) bbox.r = bbox.w+C[4];
  930. bbox.w += C[2] * (item.rscale||1);
  931. if (m == 1 && font.skew && font.skew[item.n]) bbox.skew = font.skew[item.n];
  932. if (C[5] && C[5].rfix) this.flushText(node,state).style.marginRight = CHTML.Em(C[5].rfix/1000);
  933. if (remap) {
  934. //
  935. // Remap combining characters to non-combining versions since Safari
  936. // handles them differently from everyone else. (#1709)
  937. //
  938. var chr = this.flushText(node,state);
  939. var r = (FONT[item.n]||font[item.n])[4] - (C[4] - C[2]);
  940. chr.style.marginLeft = CHTML.Em(-C[2]-r);
  941. if (r < 0) chr.style.marginRight = CHTML.Em(-r);
  942. }
  943. },
  944. //
  945. // Space characters (not actually in the fonts)
  946. //
  947. space: function (item,node,bbox,state) {
  948. if (item.w) {
  949. if (state.text === "") state.className = item.font.className;
  950. this.flushText(node,state).style.marginRight = CHTML.Em(item.w);
  951. bbox.w += item.w;
  952. }
  953. },
  954. //
  955. // An unknown character (one not in the font data)
  956. //
  957. unknown: function (item,node,bbox,state) {
  958. (this["char"])(item,node,bbox,state,0);
  959. var C = item.font[item.n];
  960. if (C[5].a) {
  961. state.a = C[5].a;
  962. if (bbox.a == null || state.a > bbox.a) bbox.a = state.a;
  963. }
  964. node = this.flushText(node,state,item.style);
  965. if (C[2] < 3) node.style.width = CHTML.Em(C[2]); // only force width if not too large (#1718)
  966. },
  967. //
  968. // Put the pending text into a box of the class, and
  969. // reset the data about the text.
  970. //
  971. flushText: function (node,state,style) {
  972. node = CHTML.addElement(node,"mjx-charbox",
  973. {className:state.className,style:style},[state.text]);
  974. if (state.a) node.style.paddingBottom = CHTML.Em(state.a);
  975. state.text = ""; state.className = null; state.a = 0; state.flushed = true;
  976. return node;
  977. }
  978. },
  979. //
  980. // Add the given text (in the given variant) into the given node, and
  981. // update the bounding box of the result. Make sure the node's DOM
  982. // bounding box matches the contents.
  983. //
  984. handleText: function (node,text,variant,bbox) {
  985. if (node.childNodes.length === 0) {
  986. CHTML.addElement(node,"mjx-char");
  987. bbox = CHTML.BBOX.empty(bbox);
  988. }
  989. if (typeof(variant) === "string") variant = this.FONTDATA.VARIANT[variant];
  990. if (!variant) variant = this.FONTDATA.VARIANT[MML.VARIANT.NORMAL];
  991. var string = {text:text, i:0, length:text.length}, list = [];
  992. if (variant.style && string.length) {
  993. list.push(this.styledText(variant,text));
  994. } else {
  995. while (string.i < string.length) {
  996. var n = this.getUnicode(string);
  997. list.push.apply(list,this.getCharList(variant,n));
  998. }
  999. }
  1000. if (list.length) this.addCharList(node.firstChild,list,bbox);
  1001. bbox.clean();
  1002. if (bbox.d < 0) {bbox.D = bbox.d; bbox.d = 0}
  1003. if (bbox.h - bbox.a) node.firstChild.style[bbox.h - bbox.a < 0 ? "marginTop" : "paddingTop"] = this.EmRounded(bbox.h-bbox.a);
  1004. if (bbox.d > -bbox.b) node.firstChild.style.paddingBottom = this.EmRounded(bbox.d+bbox.b);
  1005. return bbox;
  1006. },
  1007. /********************************************************/
  1008. createDelimiter: function (node,code,HW,BBOX,font) {
  1009. if (!code) {
  1010. var bbox = this.BBOX.zero();
  1011. bbox.w = bbox.r = this.TEX.nulldelimiterspace;
  1012. CHTML.addElement(node,"mjx-box",{style:{width:bbox.w}});
  1013. return bbox;
  1014. }
  1015. if (!(HW instanceof Array)) HW = [HW,HW];
  1016. var hw = HW[1]; HW = HW[0];
  1017. var delim = {alias: code};
  1018. while (delim.alias) {
  1019. code = delim.alias; delim = this.FONTDATA.DELIMITERS[code];
  1020. if (!delim) {delim = {HW: [0,this.FONTDATA.VARIANT[MML.VARIANT.NORMAL]]}}
  1021. }
  1022. if (delim.load) HUB.RestartAfter(AJAX.Require(this.fontDir+"/TeX/fontdata-"+delim.load+".js"));
  1023. for (var i = 0, m = delim.HW.length; i < m; i++) {
  1024. if (delim.HW[i][0] >= HW-.01 || (i == m-1 && !delim.stretch)) {
  1025. if (delim.HW[i][3]) code = delim.HW[i][3];
  1026. bbox = this.createChar(node,[code,delim.HW[i][1]],(delim.HW[i][2]||1),font);
  1027. bbox.offset = .6 * bbox.w;
  1028. if (BBOX) {bbox.scale = BBOX.scale; BBOX.rscale = BBOX.rscale}
  1029. return bbox;
  1030. }
  1031. }
  1032. if (!delim.stretch) return bbox;
  1033. return this["extendDelimiter"+delim.dir](node,hw,delim.stretch,BBOX,font);
  1034. },
  1035. extendDelimiterV: function (node,H,delim,BBOX,font) {
  1036. node = CHTML.addElement(node,"mjx-delim-v"); var tmp = CHTML.Element("span");
  1037. var top, bot, mid, ext, tbox, bbox, mbox, ebox, k = 1, c;
  1038. tbox = this.createChar(tmp,(delim.top||delim.ext),1,font); top = tmp.removeChild(tmp.firstChild);
  1039. bbox = this.createChar(tmp,(delim.bot||delim.ext),1,font); bot = tmp.removeChild(tmp.firstChild);
  1040. mbox = ebox = CHTML.BBOX.zero();
  1041. var h = tbox.h + tbox.d + bbox.h + bbox.d - EFUZZ;
  1042. node.appendChild(top);
  1043. if (delim.mid) {
  1044. mbox = this.createChar(tmp,delim.mid,1,font); mid = tmp.removeChild(tmp.firstChild);
  1045. h += mbox.h + mbox.d; k = 2;
  1046. }
  1047. if (delim.min && H < h*delim.min) H = h*delim.min;
  1048. if (H > h) {
  1049. ebox = this.createChar(tmp,delim.ext,1,font); ext = tmp.removeChild(tmp.firstChild);
  1050. var eH = ebox.h + ebox.d, eh = eH - EFUZZ;
  1051. var n = Math.min(Math.ceil((H-h)/(k*eh)),this.maxStretchyParts);
  1052. if (delim.fullExtenders) H = n*k*eh + h; else eh = (H-h)/(k*n);
  1053. c = ebox.d + ebox.a - eH/2; // for centering of extenders
  1054. ext.style.margin = ext.style.padding = "";
  1055. ext.style.lineHeight = CHTML.Em(eh);
  1056. ext.style.marginBottom = CHTML.Em(c-EFUZZ/2/k);
  1057. ext.style.marginTop = CHTML.Em(-c-EFUZZ/2/k);
  1058. var TEXT = ext.textContent, text = "\n"+TEXT;
  1059. while (--n > 0) TEXT += text;
  1060. ext.textContent = TEXT;
  1061. node.appendChild(ext);
  1062. if (delim.mid) {
  1063. node.appendChild(mid);
  1064. node.appendChild(ext.cloneNode(true));
  1065. }
  1066. } else {
  1067. c = (H-h-EFUZZ) / k;
  1068. top.style.marginBottom = CHTML.Em(c+parseFloat(top.style.marginBottom||"0"));
  1069. if (delim.mid) node.appendChild(mid);
  1070. bot.style.marginTop = CHTML.Em(c+parseFloat(bot.style.marginTop||"0"));
  1071. }
  1072. node.appendChild(bot);
  1073. var vbox = CHTML.BBOX({
  1074. w: Math.max(tbox.w,ebox.w,bbox.w,mbox.w),
  1075. l: Math.min(tbox.l,ebox.l,bbox.l,mbox.l),
  1076. r: Math.max(tbox.r,ebox.r,bbox.r,mbox.r),
  1077. h: H-bbox.d, d: bbox.d, t: H-bbox.d, b: bbox.d
  1078. });
  1079. vbox.offset = .5 * vbox.w;
  1080. if (BBOX) {vbox.scale = BBOX.scale; vbox.rscale = BBOX.rscale}
  1081. return vbox;
  1082. },
  1083. extendDelimiterH: function (node,W,delim,BBOX,font) {
  1084. node = CHTML.addElement(node,"mjx-delim-h"); var tmp = CHTML.Element("span");
  1085. var left, right, mid, ext, ext2, lbox, rbox, mbox, ebox, k = 1;
  1086. lbox = this.createChar(tmp,(delim.left||delim.rep),1,font); left = tmp.removeChild(tmp.firstChild);
  1087. rbox = this.createChar(tmp,(delim.right||delim.rep),1,font); right = tmp.removeChild(tmp.firstChild);
  1088. ebox = this.createChar(tmp,delim.rep,1,font); ext = tmp.removeChild(tmp.firstChild);
  1089. left.style.marginLeft = CHTML.Em(-lbox.l);
  1090. right.style.marginRight = CHTML.Em(rbox.r-rbox.w);
  1091. node.appendChild(left);
  1092. var hbox = CHTML.BBOX.zero();
  1093. hbox.h = Math.max(lbox.h,rbox.h,ebox.h);
  1094. hbox.d = Math.max(lbox.D||lbox.d,rbox.D||rbox.d,ebox.D||ebox.d);
  1095. var w = (lbox.r - lbox.l) + (rbox.r - rbox.l) - EFUZZ;
  1096. if (delim.mid) {
  1097. mbox = this.createChar(tmp,delim.mid,1,font);
  1098. mid = tmp.removeChild(tmp.firstChild);
  1099. mid.style.marginleft = CHTML.Em(-mbox.l); mid.style.marginRight = CHTML.Em(mbox.r-mbox.w);
  1100. w += mbox.r - mbox.l + EFUZZ; k = 2;
  1101. if (mbox.h > hbox.h) hbox.h = mbox.h;
  1102. if (mbox.d > hbox.d) hbox.d = mbox.d;
  1103. }
  1104. if (delim.min && W < w*delim.min) W = w*delim.min;
  1105. hbox.w = hbox.r = W;
  1106. if (W > w) {
  1107. var eW = ebox.r-ebox.l, ew = eW - EFUZZ;
  1108. var n = Math.min(Math.ceil((W-w)/(k*ew)),this.maxStretchyParts);
  1109. if (delim.fullExtenders) W = n*k*ew + w; else ew = (W-w)/(k*n);
  1110. var c = (eW - ew + EFUZZ/k) / 2; // for centering of extenders
  1111. ext.style.marginLeft = CHTML.Em(-ebox.l-c);
  1112. ext.style.marginRight = CHTML.Em(ebox.r-ebox.w+c);
  1113. ext.style.letterSpacing = CHTML.Em(-(ebox.w-ew));
  1114. left.style.marginRight = CHTML.Em(lbox.r-lbox.w);
  1115. right.style.marginleft = CHTML.Em(-rbox.l);
  1116. var TEXT = ext.textContent, text = TEXT;
  1117. while (--n > 0) TEXT += text;
  1118. ext.textContent = TEXT;
  1119. node.appendChild(ext);
  1120. if (delim.mid) {
  1121. node.appendChild(mid);
  1122. ext2 = node.appendChild(ext.cloneNode(true));
  1123. }
  1124. } else {
  1125. c = (W-w-EFUZZ/k) / 2;
  1126. left.style.marginRight = CHTML.Em(lbox.r-lbox.w+c);
  1127. if (delim.mid) node.appendChild(mid);
  1128. right.style.marginLeft = CHTML.Em(-rbox.l+c);
  1129. }
  1130. node.appendChild(right);
  1131. this.adjustHeights([left,ext,mid,ext2,right],[lbox,ebox,mbox,ebox,rbox],hbox);
  1132. if (BBOX) {hbox.scale = BBOX.scale; hbox.rscale = BBOX.rscale}
  1133. return hbox;
  1134. },
  1135. adjustHeights: function (nodes,box,bbox) {
  1136. //
  1137. // To get alignment right in horizontal delimiters, we force all
  1138. // the elements to the same height and depth
  1139. //
  1140. var T = bbox.h, B = bbox.d;
  1141. if (bbox.d < 0) {B = -bbox.d; bbox.D = bbox.d; bbox.d = 0}
  1142. for (var i = 0, m = nodes.length; i < m; i++) if (nodes[i]) {
  1143. nodes[i].style.paddingTop = CHTML.Em(T-box[i].a);
  1144. nodes[i].style.paddingBottom = CHTML.Em(B+box[i].a);
  1145. nodes[i].style.marginTop = nodes[i].style.marginBottom = 0;
  1146. }
  1147. },
  1148. createChar: function (node,data,scale,font) {
  1149. // ### FIXME: handle cache better (by data[1] and font)
  1150. var text = "", variant = {fonts: [data[1]], noRemap:true, cache:{}};
  1151. if (font && font === MML.VARIANT.BOLD && this.FONTDATA.FONTS[data[1]+"-Bold"])
  1152. variant.fonts = [data[1]+"-Bold",data[1]];
  1153. if (typeof(data[1]) !== "string") variant = data[1];
  1154. if (data[0] instanceof Array) {
  1155. for (var i = 0, m = data[0].length; i < m; i++) text += String.fromCharCode(data[0][i]);
  1156. } else text = String.fromCharCode(data[0]);
  1157. if (data[4]) scale *= data[4];
  1158. var bbox = this.handleText(node,text,variant), style = node.firstChild.style;
  1159. if (scale !== 1) style.fontSize = this.Percent(scale);
  1160. if (data[2]) { // x offset
  1161. style.paddingLeft = this.Em(data[2]);
  1162. bbox.w += data[2]; bbox.r += data[2];
  1163. }
  1164. if (data[3]) { // y offset
  1165. style.verticalAlign = this.Em(data[3]);
  1166. bbox.h += data[3]; if (bbox.h < 0) bbox.h = 0;
  1167. }
  1168. if (data[5]) { // extra height
  1169. style.marginTop = this.Em(data[5]);
  1170. bbox.h += data[5]; bbox.t += data[5];
  1171. }
  1172. if (data[6]) { // extra depth
  1173. style.marginBottom = this.Em(data[6]);
  1174. bbox.d += data[6]; bbox.b += data[6];
  1175. }
  1176. return bbox;
  1177. },
  1178. /********************************************************/
  1179. //
  1180. // ### FIXME: Handle mu's
  1181. //
  1182. length2em: function (length,size,scale) {
  1183. if (typeof(length) !== "string") length = length.toString();
  1184. if (length === "") return "";
  1185. if (length === MML.SIZE.NORMAL) return 1;
  1186. if (length === MML.SIZE.BIG) return 2;
  1187. if (length === MML.SIZE.SMALL) return .71;
  1188. if (this.MATHSPACE[length]) return this.MATHSPACE[length];
  1189. var match = length.match(/^\s*([-+]?(?:\.\d+|\d+(?:\.\d*)?))?(pt|em|ex|mu|px|pc|in|mm|cm|%)?/);
  1190. var m = parseFloat(match[1]||"1"), unit = match[2];
  1191. if (size == null) size = 1; if (!scale) scale = 1;
  1192. scale = 1 /this.em / scale;
  1193. if (unit === "em") return m;
  1194. if (unit === "ex") return m * this.TEX.x_height;
  1195. if (unit === "%") return m / 100 * size;
  1196. if (unit === "px") return m * scale;
  1197. if (unit === "pt") return m / 10; // 10 pt to an em
  1198. if (unit === "pc") return m * 1.2; // 12 pt to a pc
  1199. scale *= this.pxPerInch;
  1200. if (unit === "in") return m * scale;
  1201. if (unit === "cm") return m * scale / 2.54; // 2.54 cm to an inch
  1202. if (unit === "mm") return m * scale / 25.4; // 10 mm to a cm
  1203. if (unit === "mu") return m / 18; // 18mu to an em for the scriptlevel
  1204. return m*size; // relative to given size (or 1em as default)
  1205. },
  1206. thickness2em: function (length,scale) {
  1207. var thick = CHTML.TEX.rule_thickness/(scale||1);
  1208. if (length === MML.LINETHICKNESS.MEDIUM) return thick;
  1209. if (length === MML.LINETHICKNESS.THIN) return .67*thick;
  1210. if (length === MML.LINETHICKNESS.THICK) return 1.67*thick;
  1211. return this.length2em(length,thick,scale);
  1212. },
  1213. Em: function (m) {
  1214. if (Math.abs(m) < .001) return "0";
  1215. return (m.toFixed(3).replace(/\.?0+$/,""))+"em";
  1216. },
  1217. EmRounded: function (m) {
  1218. m = (Math.round(m*CHTML.em)+.05)/CHTML.em;
  1219. if (Math.abs(m) < .0006) {return "0em"}
  1220. return m.toFixed(3).replace(/\.?0+$/,"") + "em";
  1221. },
  1222. unEm: function (m) {
  1223. return parseFloat(m);
  1224. },
  1225. Px: function (m,M) {
  1226. m *= this.em;
  1227. if (M && m < M) m = M;
  1228. if (Math.abs(m) < .1) return "0";
  1229. return m.toFixed(1).replace(/\.0$/,"")+"px";
  1230. },
  1231. Percent: function (m) {
  1232. return (100*m).toFixed(1).replace(/\.?0+$/,"") + "%";
  1233. },
  1234. Transform: function (node,trans,origin) {
  1235. var style = node.style;
  1236. style.transform = style.WebkitTransform = style.MozTransform = style["-ms-transform"] = trans;
  1237. if (origin)
  1238. style.transformOrigin = style.WebkitTransformOrigin =
  1239. style.MozTransformOrigin = style["-ms-transform-origin"] = origin;
  1240. },
  1241. /********************************************************/
  1242. arrayEntry: function (a,i) {return a[Math.max(0,Math.min(i,a.length-1))]},
  1243. //
  1244. // Styles to be removed from style="..." attributes
  1245. //
  1246. removeStyles: ["fontSize","fontFamily","fontWeight","fontStyle","fontVariant","font"]
  1247. });
  1248. /**********************************************************/
  1249. CHTML.BBOX = MathJax.Object.Subclass({
  1250. Init: function (def) {
  1251. for (var id in def) {
  1252. if (def.hasOwnProperty(id)) this[id] = def[id];
  1253. }
  1254. },
  1255. clean: function () {
  1256. if (this.h === -BIGDIMEN) this.h = 0;
  1257. if (this.d === -BIGDIMEN) this.d = 0;
  1258. if (this.l === BIGDIMEN) this.l = 0;
  1259. if (this.r === -BIGDIMEN) this.r = 0;
  1260. if (this.t === -BIGDIMEN) this.t = 0;
  1261. if (this.b === -BIGDIMEN) this.b = 0;
  1262. if (this.D && this.d > 0) delete this.D;
  1263. },
  1264. rescale: function (scale) {
  1265. this.w *= scale; this.h *= scale; this.d *= scale;
  1266. this.l *= scale; this.r *= scale; this.t *= scale; this.b *= scale;
  1267. if (this.L) this.L *= scale;
  1268. if (this.R) this.R *= scale;
  1269. if (this.D) this.D *= scale;
  1270. },
  1271. combine: function (cbox,x,y) {
  1272. cbox.X = x; cbox.Y = y; // save for use with line breaking
  1273. var scale = cbox.rscale;
  1274. if (x + scale*cbox.r > this.r) this.r = x + scale*cbox.r;
  1275. if (x + scale*cbox.l < this.l) this.l = x + scale*cbox.l;
  1276. if (x + scale*(cbox.w+(cbox.L||0)+(cbox.R||0)) > this.w)
  1277. this.w = x + scale*(cbox.w + (cbox.L||0) + (cbox.R||0));
  1278. if (y + scale*cbox.h > this.h) this.h = y + scale*cbox.h;
  1279. if (cbox.D && (this.D == null || scale*cbox.D - y > this.D) && scale*cbox.D > this.d) this.D = scale*cbox.D - y;
  1280. else if (cbox.D == null && this.D) delete this.D;
  1281. if (scale*cbox.d - y > this.d) this.d = scale*cbox.d - y;
  1282. if (y + scale*cbox.t > this.t) this.t = y + scale*cbox.t;
  1283. if (scale*cbox.b - y > this.b) this.b = scale*cbox.b - y;
  1284. },
  1285. append: function (cbox) {
  1286. var scale = cbox.rscale; var x = this.w;
  1287. if (x + scale*cbox.r > this.r) this.r = x + scale*cbox.r;
  1288. if (x + scale*cbox.l < this.l) this.l = x + scale*cbox.l;
  1289. this.w += scale*(cbox.w+(cbox.L||0)+(cbox.R||0)) ;
  1290. if (scale*cbox.h > this.h) this.h = scale*cbox.h;
  1291. if (cbox.D && (this.D == null || scale*cbox.D > this.D) && scale*cbox.D > this.d) this.D = scale*cbox.D;
  1292. else if (cbox.D == null && this.D) delete this.D;
  1293. if (scale*cbox.d > this.d) this.d = scale*cbox.d;
  1294. if (scale*cbox.t > this.t) this.t = scale*cbox.t;
  1295. if (scale*cbox.b > this.b) this.b = scale*cbox.b;
  1296. },
  1297. updateFrom: function (cbox) {
  1298. this.h = cbox.h; this.d = cbox.d; this.w = cbox.w; this.r = cbox.r; this.l = cbox.l;
  1299. this.t = cbox.t; this.b = cbox.b;
  1300. if (cbox.pwidth) this.pwidth = cbox.pwidth;
  1301. if (cbox.D) this.D = cbox.D; else delete this.D;
  1302. },
  1303. adjust: function (m,x,X,M) {
  1304. this[x] += CHTML.length2em(m,1,this.scale);
  1305. if (M == null) {
  1306. if (this[x] > this[X]) this[X] = this[x];
  1307. } else {
  1308. if (this[X] < M) this[X] = M;
  1309. }
  1310. }
  1311. },{
  1312. zero: function () {
  1313. return CHTML.BBOX({h:0, d:0, w:0, l:0, r:0, t:0, b:0, scale:1, rscale:1});
  1314. },
  1315. empty: function (bbox) {
  1316. if (!bbox) bbox = CHTML.BBOX.zero();
  1317. bbox.h = bbox.d = bbox.r = bbox.t = bbox.b = -BIGDIMEN;
  1318. bbox.w = 0; bbox.l = BIGDIMEN;
  1319. delete bbox.pwidth;
  1320. return bbox;
  1321. },
  1322. //
  1323. // CSS styles that affect BBOXes
  1324. //
  1325. styleAdjust: [
  1326. ["borderTopWidth","h","t"],
  1327. ["borderRightWidth","w","r"],
  1328. ["borderBottomWidth","d","b"],
  1329. ["borderLeftWidth","w","l",0],
  1330. ["paddingTop","h","t"],
  1331. ["paddingRight","w","r"],
  1332. ["paddingBottom","d","b"],
  1333. ["paddingLeft","w","l",0],
  1334. ]
  1335. });
  1336. /**********************************************************/
  1337. MathJax.Hub.Register.StartupHook("mml Jax Ready",function () {
  1338. MML = MathJax.ElementJax.mml;
  1339. /********************************************************/
  1340. MML.mbase.Augment({
  1341. toCommonHTML: function (node,options) {
  1342. return this.CHTMLdefaultNode(node,options);
  1343. },
  1344. CHTMLmultiline: function () {MML.mbase.CHTMLautoloadFile("multiline")},
  1345. CHTMLdefaultNode: function (node,options) {
  1346. if (!options) options = {};
  1347. node = this.CHTMLcreateNode(node); this.CHTML = CHTML.BBOX.empty();
  1348. this.CHTMLhandleStyle(node);
  1349. if (this.isToken) this.CHTMLgetVariant();
  1350. this.CHTMLhandleScale(node);
  1351. var m = Math.max((options.minChildren||0),this.data.length);
  1352. for (var i = 0; i < m; i++) this.CHTMLaddChild(node,i,options);
  1353. if (!options.noBBox) this.CHTML.clean();
  1354. this.CHTMLhandleSpace(node);
  1355. this.CHTMLhandleBBox(node);
  1356. this.CHTMLhandleColor(node);
  1357. return node;
  1358. },
  1359. CHTMLaddChild: function (node,i,options) {
  1360. var child = this.data[i], cnode;
  1361. var type = options.childNodes;
  1362. if (type instanceof Array) type = type[i]||"span";
  1363. if (child) {
  1364. if (type) node = CHTML.addElement(node,type);
  1365. cnode = child.toCommonHTML(node,options.childOptions);
  1366. if (type && child.CHTML.rscale !== 1) {
  1367. // move scale factor to outer container (which seems to be more accurate)
  1368. node.style.fontSize = node.firstChild.style.fontSize;
  1369. node.firstChild.style.fontSize = "";
  1370. }
  1371. if (!options.noBBox) {
  1372. var bbox = this.CHTML, cbox = child.CHTML;
  1373. bbox.append(cbox);
  1374. if (this.data.length === 1) {
  1375. if (cbox.ic) bbox.ic = cbox.ic;
  1376. if (cbox.skew) bbox.skew = cbox.skew;
  1377. } else {
  1378. delete bbox.ic;
  1379. delete bbox.skew;
  1380. }
  1381. if (cbox.pwidth) bbox.pwidth = cbox.pwidth;
  1382. }
  1383. } else if (options.forceChild) {
  1384. cnode = CHTML.addElement(node,(type||"mjx-box"));
  1385. }
  1386. return cnode;
  1387. },
  1388. CHTMLchildNode: function (node,i) {
  1389. node = node.childNodes[i];
  1390. if (node.nodeName.toLowerCase() === "a") node = node.firstChild;
  1391. return node;
  1392. },
  1393. CHTMLcoreNode: function (node) {
  1394. if (this.inferRow && this.data[0]) return this.data[0].CHTMLcoreNode(node.firstChild);
  1395. return this.CHTMLchildNode(node,this.CoreIndex());
  1396. },
  1397. CHTMLstretchChildV: function (i,H,D) {
  1398. var data = this.data[i];
  1399. if (data) {
  1400. var bbox = this.CHTML, dbox = data.CHTML;
  1401. if (dbox.stretch || (dbox.stretch == null && data.CHTMLcanStretch("Vertical",H,D))) {
  1402. var w = dbox.w;
  1403. dbox = data.CHTMLstretchV(H,D);
  1404. bbox.w += dbox.w - w;
  1405. if (bbox.w > bbox.r) bbox.r = bbox.w;
  1406. if (dbox.h > bbox.h) bbox.h = dbox.h;
  1407. if (dbox.d > bbox.d) bbox.d = dbox.d;
  1408. if (dbox.t > bbox.t) bbox.t = dbox.t;
  1409. if (dbox.b > bbox.b) bbox.b = dbox.b;
  1410. }
  1411. }
  1412. },
  1413. CHTMLstretchChildH: function (i,W,node) {
  1414. var data = this.data[i];
  1415. if (data) {
  1416. var bbox = this.CHTML, dbox = data.CHTML;
  1417. if (dbox.stretch || (dbox.stretch == null && data.CHTMLcanStretch("Horizontal",W))) {
  1418. var w = dbox.w;
  1419. dbox = data.CHTMLstretchH(this.CHTMLchildNode(node,i),W);
  1420. bbox.w += dbox.w - w;
  1421. if (bbox.w > bbox.r) bbox.r = bbox.w;
  1422. if (dbox.h > bbox.h) bbox.h = dbox.h;
  1423. if (dbox.d > bbox.d) bbox.d = dbox.d;
  1424. if (dbox.t > bbox.t) bbox.t = dbox.t;
  1425. if (dbox.b > bbox.b) bbox.b = dbox.b;
  1426. }
  1427. }
  1428. },
  1429. CHTMLupdateFrom: function (bbox) {
  1430. this.CHTML.updateFrom(bbox);
  1431. if (this.inferRow) this.data[0].CHTML.updateFrom(bbox);
  1432. },
  1433. CHTMLcanStretch: function (direction,H,D) {
  1434. var stretch = false;
  1435. if (this.isEmbellished()) {
  1436. var core = this.Core();
  1437. if (core && core !== this) stretch = core.CHTMLcanStretch(direction,H,D);
  1438. }
  1439. this.CHTML.stretch = stretch;
  1440. return stretch;
  1441. },
  1442. CHTMLstretchV: function (h,d) {
  1443. this.CHTMLupdateFrom(this.Core().CHTMLstretchV(h,d));
  1444. return this.CHTML;
  1445. },
  1446. CHTMLstretchH: function (node,w) {
  1447. this.CHTMLupdateFrom(this.CHTMLstretchCoreH(node,w));
  1448. return this.CHTML;
  1449. },
  1450. CHTMLstretchCoreH: function (node,w) {
  1451. return this.Core().CHTMLstretchH(this.CHTMLcoreNode(node),w);
  1452. },
  1453. CHTMLcreateNode: function (node) {
  1454. if (!this.CHTML) this.CHTML = {};
  1455. this.CHTML = CHTML.BBOX.zero();
  1456. if (this.href) node = CHTML.addElement(node,"a",{href:this.href, isMathJax:true});
  1457. if (!this.CHTMLnodeID) this.CHTMLnodeID = CHTML.GetID();
  1458. var id = (this.id || "MJXc-Node-"+this.CHTMLnodeID)+CHTML.idPostfix;
  1459. return this.CHTMLhandleAttributes(CHTML.addElement(node,"mjx-"+this.type,{id:id}));
  1460. },
  1461. CHTMLnodeElement: function () {
  1462. if (!this.CHTMLnodeID) {return null}
  1463. return document.getElementById((this.id||"MJXc-Node-"+this.CHTMLnodeID)+CHTML.idPostfix);
  1464. },
  1465. CHTMLlength2em: function (length,size) {
  1466. return CHTML.length2em(length,size,this.CHTML.scale);
  1467. },
  1468. CHTMLhandleAttributes: function (node) {
  1469. if (this["class"]) {
  1470. if (node.className) node.className += " "+this["class"];
  1471. else node.className = this["class"];
  1472. }
  1473. //
  1474. // Copy RDFa, aria, and other tags from the MathML to the CHTML
  1475. // output nodes. Don't copy those in the MML.nocopyAttributes list,
  1476. // the ignoreMMLattributes configuration list, or anything that
  1477. // already exists as a property of the node (e.g., no "onlick", etc.)
  1478. // If a name in the ignoreMMLattributes object is set to false, then
  1479. // the attribute WILL be copied.
  1480. //
  1481. if (this.attrNames) {
  1482. var copy = this.attrNames, skip = MML.nocopyAttributes, ignore = HUB.config.ignoreMMLattributes;
  1483. var defaults = (this.type === "mstyle" ? MML.math.prototype.defaults : this.defaults);
  1484. for (var i = 0, m = copy.length; i < m; i++) {
  1485. var id = copy[i];
  1486. if (ignore[id] == false || (!skip[id] && !ignore[id] &&
  1487. defaults[id] == null && typeof(node[id]) === "undefined")) {
  1488. node.setAttribute(id,this.attr[id])
  1489. }
  1490. }
  1491. }
  1492. return node;
  1493. },
  1494. CHTMLhandleScale: function (node) {
  1495. var scale = 1, parent = this.parent, pscale = (parent ? parent.CHTML.scale : 1);
  1496. var values = this.getValues("scriptlevel","fontsize");
  1497. values.mathsize = this.Get("mathsize",null,!this.isToken);
  1498. if (values.scriptlevel !== 0) {
  1499. if (values.scriptlevel > 2) values.scriptlevel = 2;
  1500. scale = Math.pow(this.Get("scriptsizemultiplier"),values.scriptlevel);
  1501. values.scriptminsize = CHTML.length2em(this.Get("scriptminsize"),.8,1);
  1502. if (scale < values.scriptminsize) scale = values.scriptminsize;
  1503. }
  1504. if (this.removedStyles && this.removedStyles.fontSize && !values.fontsize)
  1505. values.fontsize = this.removedStyles.fontSize;
  1506. if (values.fontsize && !this.mathsize) values.mathsize = values.fontsize;
  1507. if (values.mathsize !== 1) scale *= CHTML.length2em(values.mathsize,1,1);
  1508. var variant = this.CHTMLvariant;
  1509. if (variant && variant.style && variant.style["font-family"])
  1510. scale *= (CHTML.config.scale/100)/CHTML.scale;
  1511. this.CHTML.scale = scale; pscale = this.CHTML.rscale = scale/pscale;
  1512. if (Math.abs(pscale-1) < .001) pscale = 1;
  1513. if (node && pscale !== 1) node.style.fontSize = CHTML.Percent(pscale);
  1514. return scale;
  1515. },
  1516. CHTMLhandleStyle: function (node) {
  1517. if (!this.style) return;
  1518. var style = node.style;
  1519. style.cssText = this.style; this.removedStyles = {};
  1520. for (var i = 0, m = CHTML.removeStyles.length; i < m; i++) {
  1521. var id = CHTML.removeStyles[i];
  1522. if (style[id]) {
  1523. this.removedStyles[id] = style[id];
  1524. style[id] = "";
  1525. }
  1526. }
  1527. },
  1528. CHTMLhandleBBox: function (node) {
  1529. var BBOX = this.CHTML, style = node.style;
  1530. if (this.data.length === 1 && (this.data[0].CHTML||{}).pwidth) {
  1531. BBOX.pwidth = this.data[0].CHTML.pwidth;
  1532. BBOX.mwidth = this.data[0].CHTML.mwidth;
  1533. style.width = "100%";
  1534. } else if (BBOX.pwidth) {
  1535. BBOX.mwidth = CHTML.Em(BBOX.w);
  1536. style.width = "100%";
  1537. } else if (BBOX.w < 0) {
  1538. style.width = "0px";
  1539. style.marginRight = CHTML.Em(BBOX.w);
  1540. }
  1541. if (!this.style) return;
  1542. // ### FIXME: adjust for width, height, vertical-align?
  1543. for (var i = 0, m = CHTML.BBOX.styleAdjust.length; i < m; i++) {
  1544. var data = CHTML.BBOX.styleAdjust[i];
  1545. if (data && style[data[0]]) BBOX.adjust(style[data[0]],data[1],data[2],data[3]);
  1546. }
  1547. },
  1548. CHTMLhandleColor: function (node) {
  1549. if (this.mathcolor) {node.style.color = this.mathcolor}
  1550. else if (this.color) {node.style.color = this.color}
  1551. if (this.mathbackground) {node.style.backgroundColor = this.mathbackground}
  1552. else if (this.background) {node.style.backgroundColor = this.background}
  1553. },
  1554. CHTMLhandleSpace: function (node) {
  1555. if (!this.useMMLspacing) {
  1556. var space = this.texSpacing();
  1557. if (space !== "") {
  1558. this.CHTML.L = this.CHTMLlength2em(space);
  1559. node.className += " "+CHTML.SPACECLASS[space];
  1560. }
  1561. }
  1562. },
  1563. CHTMLhandleText: function (node,text,variant) {
  1564. if (node.firstChild && !this.CHTML) this.CHTML = CHTML.BBOX.empty();
  1565. this.CHTML = CHTML.handleText(node,text,variant,this.CHTML);
  1566. },
  1567. CHTMLgetVariant: function () {
  1568. var values = this.getValues("mathvariant","fontfamily","fontweight","fontstyle"), style;
  1569. values.hasVariant = this.Get("mathvariant",true); // null if not explicitly specified
  1570. if (this.removedStyles) {
  1571. style = this.removedStyles;
  1572. if (style.fontFamily) values.family = style.fontFamily;
  1573. if (style.fontWeight) values.weight = style.fontWeight;
  1574. if (style.fontStyle) values.style = style.fontStyle;
  1575. }
  1576. if (!values.hasVariant) {
  1577. if (values.fontfamily) values.family = values.fontfamily;
  1578. if (values.fontweight) values.weight = values.fontweight;
  1579. if (values.fontstyle) values.style = values.fontstyle;
  1580. }
  1581. if (values.weight && values.weight.match(/^\d+$/))
  1582. values.weight = (parseInt(values.weight) > 600 ? "bold" : "normal");
  1583. var variant = values.mathvariant; if (this.variantForm) variant = "-TeX-variant";
  1584. if (values.family && !values.hasVariant) {
  1585. if (!values.weight && values.mathvariant.match(/bold/)) values.weight = "bold";
  1586. if (!values.style && values.mathvariant.match(/italic/)) values.style = "italic";
  1587. this.CHTMLvariant = {fonts:[], noRemap:true, cache:{}, style: {
  1588. "font-family":values.family, "font-weight":values.weight||"normal", "font-style":values.style||"normal"
  1589. }};
  1590. return;
  1591. }
  1592. if (values.weight === "bold") {
  1593. variant = {
  1594. normal:MML.VARIANT.BOLD, italic:MML.VARIANT.BOLDITALIC,
  1595. fraktur:MML.VARIANT.BOLDFRAKTUR, script:MML.VARIANT.BOLDSCRIPT,
  1596. "sans-serif":MML.VARIANT.BOLDSANSSERIF,
  1597. "sans-serif-italic":MML.VARIANT.SANSSERIFBOLDITALIC
  1598. }[variant]||variant;
  1599. } else if (values.weight === "normal") {
  1600. variant = {
  1601. bold:MML.VARIANT.normal, "bold-italic":MML.VARIANT.ITALIC,
  1602. "bold-fraktur":MML.VARIANT.FRAKTUR, "bold-script":MML.VARIANT.SCRIPT,
  1603. "bold-sans-serif":MML.VARIANT.SANSSERIF,
  1604. "sans-serif-bold-italic":MML.VARIANT.SANSSERIFITALIC
  1605. }[variant]||variant;
  1606. }
  1607. if (values.style === "italic") {
  1608. variant = {
  1609. normal:MML.VARIANT.ITALIC, bold:MML.VARIANT.BOLDITALIC,
  1610. "sans-serif":MML.VARIANT.SANSSERIFITALIC,
  1611. "bold-sans-serif":MML.VARIANT.SANSSERIFBOLDITALIC
  1612. }[variant]||variant;
  1613. } else if (values.style === "normal") {
  1614. variant = {
  1615. italic:MML.VARIANT.NORMAL, "bold-italic":MML.VARIANT.BOLD,
  1616. "sans-serif-italic":MML.VARIANT.SANSSERIF,
  1617. "sans-serif-bold-italic":MML.VARIANT.BOLDSANSSERIF
  1618. }[variant]||variant;
  1619. }
  1620. this.CHTMLvariant = CHTML.FONTDATA.VARIANT[variant] ||
  1621. CHTML.FONTDATA.VARIANT[MML.VARIANT.NORMAL];
  1622. },
  1623. CHTMLbboxFor: function (n) {
  1624. if (this.data[n] && this.data[n].CHTML) return this.data[n].CHTML;
  1625. return CHTML.BBOX.zero();
  1626. },
  1627. //
  1628. // Debugging function to see if internal BBox matches actual bbox
  1629. //
  1630. CHTMLdrawBBox: function (node,bbox) {
  1631. if (!bbox) bbox = this.CHTML;
  1632. var box = CHTML.Element("mjx-box",
  1633. {style:{opacity:.25,"margin-left":CHTML.Em(-(bbox.w+(bbox.R||0)))}},[
  1634. ["mjx-box",{style:{
  1635. height:CHTML.Em(bbox.h),width:CHTML.Em(bbox.w),
  1636. "background-color":"red"
  1637. }}],
  1638. ["mjx-box",{style:{
  1639. height:CHTML.Em(bbox.d),width:CHTML.Em(bbox.w),
  1640. "margin-left":CHTML.Em(-bbox.w),"vertical-align":CHTML.Em(-bbox.d),
  1641. "background-color":"green"
  1642. }}]
  1643. ]);
  1644. if (node.nextSibling) {node.parentNode.insertBefore(box,node.nextSibling)}
  1645. else {node.parentNode.appendChild(box)}
  1646. },
  1647. CHTMLnotEmpty: function (mml) {
  1648. while (mml && mml.data.length < 2 && (mml.type === "mrow" || mml.type === "texatom"))
  1649. mml = mml.data[0];
  1650. return !!mml;
  1651. }
  1652. },{
  1653. //
  1654. // Autoload files based on node type or file name
  1655. //
  1656. CHTMLautoload: function () {
  1657. var file = CHTML.autoloadDir+"/"+this.type+".js";
  1658. HUB.RestartAfter(AJAX.Require(file));
  1659. },
  1660. CHTMLautoloadFile: function (name) {
  1661. var file = CHTML.autoloadDir+"/"+name+".js";
  1662. HUB.RestartAfter(AJAX.Require(file));
  1663. },
  1664. //
  1665. // For use with embellished operators
  1666. //
  1667. CHTMLstretchV: function (h,d) {
  1668. this.Core().CHTMLstretchV(h,d);
  1669. this.toCommonHTML(this.CHTMLnodeElement(),{stretch:true});
  1670. return this.CHTML;
  1671. },
  1672. CHTMLstretchH: function (node,w) {
  1673. this.CHTMLupdateFrom(this.CHTMLstretchCoreH(node,w));
  1674. this.toCommonHTML(node,{stretch:true});
  1675. return this.CHTML;
  1676. }
  1677. });
  1678. /********************************************************/
  1679. MML.chars.Augment({
  1680. toCommonHTML: function (node,options) {
  1681. if (options == null) options = {};
  1682. var text = this.toString();
  1683. if (options.remap) text = options.remap(text,options.remapchars);
  1684. this.CHTMLhandleText(node,text,options.variant||this.parent.CHTMLvariant);
  1685. }
  1686. });
  1687. MML.entity.Augment({
  1688. toCommonHTML: function (node,options) {
  1689. if (options == null) options = {};
  1690. var text = this.toString();
  1691. if (options.remapchars) text = options.remap(text,options.remapchars);
  1692. this.CHTMLhandleText(node,text,options.variant||this.parent.CHTMLvariant);
  1693. }
  1694. });
  1695. /********************************************************/
  1696. MML.math.Augment({
  1697. toCommonHTML: function (node) {
  1698. node = this.CHTMLdefaultNode(node);
  1699. if (this.CHTML.w < 0) {
  1700. node.parentNode.style.width = "0px";
  1701. node.parentNode.style.marginRight = CHTML.Em(this.CHTML.w);
  1702. }
  1703. var alttext = this.Get("alttext");
  1704. if (alttext && !node.getAttribute("aria-label")) node.setAttribute("aria-label",alttext);
  1705. if (this.CHTML.pwidth) {
  1706. node.parentNode.style.minWidth = this.CHTML.mwidth||CHTML.Em(this.CHTML.w);
  1707. node.parentNode.className = "mjx-full-width "+node.parentNode.className;
  1708. node.style.width = this.CHTML.pwidth;
  1709. } else if (!this.isMultiline && this.Get("display") === "block") {
  1710. var values = this.getValues("indentalignfirst","indentshiftfirst","indentalign","indentshift");
  1711. if (values.indentalignfirst !== MML.INDENTALIGN.INDENTALIGN) values.indentalign = values.indentalignfirst;
  1712. if (values.indentalign === MML.INDENTALIGN.AUTO) values.indentalign = CONFIG.displayAlign;
  1713. if (values.indentshiftfirst !== MML.INDENTSHIFT.INDENTSHIFT) values.indentshift = values.indentshiftfirst;
  1714. if (values.indentshift === "auto") values.indentshift = "0";
  1715. var shift = this.CHTMLlength2em(values.indentshift,CHTML.cwidth);
  1716. if (CONFIG.displayIndent !== "0") {
  1717. var indent = this.CHTMLlength2em(CONFIG.displayIndent,CHTML.cwidth);
  1718. shift += (values.indentalign === MML.INDENTALIGN.RIGHT ? -indent : indent);
  1719. }
  1720. var styles = node.parentNode.parentNode.style;
  1721. node.parentNode.style.textAlign = styles.textAlign = values.indentalign;
  1722. // ### FIXME: make percentage widths respond to changes in container
  1723. if (shift) {
  1724. shift *= CHTML.em/CHTML.outerEm;
  1725. HUB.Insert(styles,({
  1726. left: {marginLeft: CHTML.Em(shift)},
  1727. right: {marginRight: CHTML.Em(-shift)},
  1728. center: {marginLeft: CHTML.Em(shift), marginRight: CHTML.Em(-shift)}
  1729. })[values.indentalign]);
  1730. }
  1731. }
  1732. return node;
  1733. }
  1734. });
  1735. /********************************************************/
  1736. MML.mi.Augment({
  1737. toCommonHTML: function (node) {
  1738. node = this.CHTMLdefaultNode(node);
  1739. var bbox = this.CHTML, text = this.data.join("");
  1740. if (bbox.skew != null && text.length !== 1) delete bbox.skew;
  1741. if (bbox.r > bbox.w && text.length === 1 && !this.CHTMLvariant.noIC) {
  1742. bbox.ic = bbox.r - bbox.w; bbox.w = bbox.r;
  1743. node.lastChild.style.paddingRight = CHTML.Em(bbox.ic);
  1744. }
  1745. return node;
  1746. }
  1747. });
  1748. /********************************************************/
  1749. MML.mn.Augment({
  1750. CHTMLremapMinus: function (text) {return text.replace(/^-/,"\u2212")},
  1751. toCommonHTML: function (node) {
  1752. node = this.CHTMLdefaultNode(node,{childOptions:{remap:this.CHTMLremapMinus}});
  1753. var bbox = this.CHTML, text = this.data.join("");
  1754. if (bbox.skew != null && text.length !== 1) delete bbox.skew;
  1755. if (bbox.r > bbox.w && text.length === 1 && !this.CHTMLvariant.noIC) {
  1756. bbox.ic = bbox.r - bbox.w; bbox.w = bbox.r;
  1757. node.lastChild.style.paddingRight = CHTML.Em(bbox.ic);
  1758. }
  1759. return node;
  1760. }
  1761. });
  1762. /********************************************************/
  1763. MML.mo.Augment({
  1764. toCommonHTML: function (node) {
  1765. node = this.CHTMLcreateNode(node);
  1766. this.CHTMLhandleStyle(node);
  1767. this.CHTMLgetVariant();
  1768. this.CHTMLhandleScale(node);
  1769. CHTML.BBOX.empty(this.CHTML);
  1770. var values = this.getValues("displaystyle","largeop");
  1771. values.variant = this.CHTMLvariant;
  1772. values.text = this.data.join("");
  1773. if (values.text == "") {
  1774. if (this.fence) node.style.width = CHTML.Em(CHTML.TEX.nulldelimiterspace);
  1775. } else {
  1776. this.CHTMLadjustAccent(values);
  1777. this.CHTMLadjustVariant(values);
  1778. for (var i = 0, m = this.data.length; i < m; i++) {
  1779. this.CHTMLaddChild(node,i,{childOptions:{
  1780. variant: values.mathvariant,
  1781. remap: this.remap,
  1782. remapchars: values.remapchars
  1783. }});
  1784. }
  1785. if (values.text.length !== 1) delete this.CHTML.skew;
  1786. else if (this.CHTML.w === 0 && this.CHTML.l < 0) this.CHTMLfixCombiningChar(node);
  1787. if (values.largeop) this.CHTMLcenterOp(node);
  1788. }
  1789. this.CHTML.clean();
  1790. this.CHTMLhandleBBox(node);
  1791. this.CHTMLhandleSpace(node);
  1792. this.CHTMLhandleColor(node);
  1793. return node;
  1794. },
  1795. CHTMLhandleSpace: function (node) {
  1796. if (this.useMMLspacing) {
  1797. var values = this.getValues("scriptlevel","lspace","rspace");
  1798. values.lspace = Math.max(0,this.CHTMLlength2em(values.lspace));
  1799. values.rspace = Math.max(0,this.CHTMLlength2em(values.rspace));
  1800. if (values.scriptlevel > 0) {
  1801. if (!this.hasValue("lspace")) values.lspace = .15;
  1802. if (!this.hasValue("rspace")) values.rspace = .15;
  1803. }
  1804. var core = this, parent = this.Parent();
  1805. while (parent && parent.isEmbellished() && parent.Core() === core)
  1806. {core = parent; parent = parent.Parent(); node = core.CHTMLnodeElement()}
  1807. if (values.lspace) node.style.paddingLeft = CHTML.Em(values.lspace);
  1808. if (values.rspace) node.style.paddingRight = CHTML.Em(values.rspace);
  1809. this.CHTML.L = values.lspace; this.CHTML.R = values.rspace;
  1810. } else {
  1811. this.SUPER(arguments).CHTMLhandleSpace.apply(this,arguments);
  1812. }
  1813. },
  1814. CHTMLadjustAccent: function (data) {
  1815. var parent = this.CoreParent(); data.parent = parent;
  1816. if (data.text.length === 1 && parent && parent.isa(MML.munderover)) {
  1817. var over = parent.data[parent.over], under = parent.data[parent.under];
  1818. if (over && this === over.CoreMO() && parent.Get("accent")) {
  1819. data.remapchars = CHTML.FONTDATA.REMAPACCENT;
  1820. } else if (under && this === under.CoreMO() && parent.Get("accentunder")) {
  1821. data.remapchars = CHTML.FONTDATA.REMAPACCENTUNDER;
  1822. }
  1823. }
  1824. },
  1825. CHTMLadjustVariant: function (data) {
  1826. var parent = data.parent,
  1827. isScript = (parent && parent.isa(MML.msubsup) && this !== parent.data[parent.base]);
  1828. if (data.largeop) data.mathvariant = (data.displaystyle ? "-largeOp" : "-smallOp");
  1829. if (isScript) {
  1830. data.remapchars = this.remapChars;
  1831. if (data.text.match(/['`"\u00B4\u2032-\u2037\u2057]/))
  1832. data.mathvariant = "-TeX-variant"; // ### FIXME: handle other fonts
  1833. }
  1834. },
  1835. CHTMLfixCombiningChar: function (node) {
  1836. //
  1837. // IE doesn't display combining chararacters unless they combine with
  1838. // something, so put them over a space and remove the space's width
  1839. //
  1840. node = node.firstChild;
  1841. var space = CHTML.Element("mjx-box",{style:{width:".25em","margin-left":"-.25em"}});
  1842. node.insertBefore(space,node.firstChild);
  1843. },
  1844. CHTMLcenterOp: function (node) {
  1845. var bbox = this.CHTML;
  1846. var p = (bbox.h - bbox.d)/2 - CHTML.TEX.axis_height;
  1847. if (Math.abs(p) > .001) node.style.verticalAlign = CHTML.Em(-p);
  1848. bbox.h -= p; bbox.d += p;
  1849. if (bbox.r > bbox.w) {
  1850. bbox.ic = bbox.r - bbox.w; bbox.w = bbox.r;
  1851. node.style.paddingRight = CHTML.Em(bbox.ic);
  1852. }
  1853. },
  1854. CHTMLcanStretch: function (direction,H,D) {
  1855. if (!this.Get("stretchy")) return false;
  1856. var c = this.data.join(""); if (c.length !== 1) return false;
  1857. var values = {text: c};
  1858. this.CHTMLadjustAccent(values);
  1859. if (values.remapchars) c = values.remapchars[c]||c;
  1860. c = CHTML.FONTDATA.DELIMITERS[c.charCodeAt(0)];
  1861. var stretch = (c && c.dir === direction.substr(0,1));
  1862. if (stretch) {
  1863. stretch = (this.CHTML.h !== H || this.CHTML.d !== D ||
  1864. !!this.Get("minsize",true) || !!this.Get("maxsize",true));
  1865. if (stretch) this.CHTML.stretch = true;
  1866. }
  1867. return stretch;
  1868. },
  1869. CHTMLstretchV: function (h,d) {
  1870. var node = this.CHTMLnodeElement(), bbox = this.CHTML;
  1871. var values = this.getValues("symmetric","maxsize","minsize");
  1872. //
  1873. // Determine the height needed
  1874. //
  1875. var H, a = CHTML.TEX.axis_height;
  1876. if (values.symmetric) {H = 2*Math.max(h-a,d+a)} else {H = h + d}
  1877. values.maxsize = this.CHTMLlength2em(values.maxsize,bbox.h+bbox.d);
  1878. values.minsize = this.CHTMLlength2em(values.minsize,bbox.h+bbox.d);
  1879. H = Math.max(values.minsize,Math.min(values.maxsize,H));
  1880. //
  1881. // If we are not already stretched to this height
  1882. //
  1883. if (H !== bbox.sH) {
  1884. //
  1885. // Get a delimiter of the proper height and save the height
  1886. //
  1887. if (H != values.minsize)
  1888. {H = [Math.max(H*CHTML.TEX.delimiterfactor/1000,H-CHTML.TEX.delimitershortfall),H]}
  1889. while (node.firstChild) node.removeChild(node.firstChild);
  1890. this.CHTML = bbox = CHTML.createDelimiter(node,this.data.join("").charCodeAt(0),H,bbox);
  1891. bbox.sH = (H instanceof Array ? H[1] : H);
  1892. //
  1893. // Reposition as needed
  1894. //
  1895. if (values.symmetric) {H = (bbox.h + bbox.d)/2 + a}
  1896. else {H = (bbox.h + bbox.d) * h/(h + d)}
  1897. H -= bbox.h;
  1898. if (Math.abs(H) > .05) {
  1899. node.style.verticalAlign = CHTML.Em(H);
  1900. bbox.h += H; bbox.d -= H; bbox.t += H; bbox.b -= H;
  1901. }
  1902. }
  1903. return this.CHTML;
  1904. },
  1905. CHTMLstretchH: function (node,W) {
  1906. var bbox = this.CHTML;
  1907. var values = this.getValues("maxsize","minsize","mathvariant","fontweight");
  1908. if ((values.fontweight === "bold" || (this.removedStyles||{}).fontWeight === "bold" ||
  1909. parseInt(values.fontweight) >= 600) && !this.Get("mathvariant",true))
  1910. values.mathvariant = MML.VARIANT.BOLD;
  1911. values.maxsize = this.CHTMLlength2em(values.maxsize,bbox.w);
  1912. values.minsize = this.CHTMLlength2em(values.minsize,bbox.w);
  1913. W = Math.max(values.minsize,Math.min(values.maxsize,W));
  1914. if (W !== bbox.sW) {
  1915. while (node.firstChild) node.removeChild(node.firstChild);
  1916. this.CHTML = bbox = CHTML.createDelimiter(node,this.data.join("").charCodeAt(0),W,bbox,values.mathvariant);
  1917. bbox.sW = W;
  1918. }
  1919. return this.CHTML;
  1920. }
  1921. });
  1922. /********************************************************/
  1923. MML.mtext.Augment({
  1924. CHTMLgetVariant: function () {
  1925. if (CHTML.config.mtextFontInherit || this.Parent().type === "merror") {
  1926. var scale = (CHTML.config.scale/100)/CHTML.scale;
  1927. var variant = {cache:{}, fonts:[], className:"MJXc-font-inherit", rscale:scale,
  1928. style:{"font-size":CHTML.Percent(scale)}};
  1929. var name = this.Get("mathvariant");
  1930. if (name.match(/bold/)) variant.style["font-weight"] = "bold";
  1931. if (name.match(/italic|-tex-mathit/)) variant.style["font-style"] = "italic";
  1932. if (name === "monospace") variant.className += " MJXc-monospace-font";
  1933. if (name === "double-struck") variant.className += " MJXc-double-struck-font";
  1934. if (name.match(/fraktur/)) variant.className += " MJXc-fraktur-font";
  1935. if (name.match(/sans-serif/)) variant.className += " MJXc-sans-serif-font";
  1936. if (name.match(/script/)) variant.className += " MJXc-script-font";
  1937. this.CHTMLvariant = variant;
  1938. } else {
  1939. this.SUPER(arguments).CHTMLgetVariant.call(this);
  1940. }
  1941. }
  1942. });
  1943. /********************************************************/
  1944. MML.merror.Augment({
  1945. toCommonHTML: function (node) {
  1946. node = this.CHTMLdefaultNode(node);
  1947. var bbox = this.CHTML;
  1948. //
  1949. // Adjust for font-size: 90%
  1950. //
  1951. bbox.rescale(.9);
  1952. //
  1953. // Adjust for padding and border
  1954. //
  1955. bbox.h += 3/CHTML.em; if (bbox.h > bbox.t) bbox.t = bbox.h;
  1956. bbox.d += 3/CHTML.em; if (bbox.d > bbox.b) bbox.b = bbox.d;
  1957. bbox.w += 8/CHTML.em; bbox.r = bbox.w; bbox.l = 0;
  1958. return node;
  1959. }
  1960. });
  1961. /********************************************************/
  1962. MML.mspace.Augment({
  1963. toCommonHTML: function (node) {
  1964. node = this.CHTMLcreateNode(node);
  1965. this.CHTMLhandleStyle(node);
  1966. this.CHTMLhandleScale(node);
  1967. var values = this.getValues("height","depth","width");
  1968. var w = this.CHTMLlength2em(values.width),
  1969. h = this.CHTMLlength2em(values.height),
  1970. d = this.CHTMLlength2em(values.depth);
  1971. var bbox = this.CHTML;
  1972. bbox.w = bbox.r = w; bbox.h = bbox.t = h; bbox.d = bbox.b = d; bbox.l = 0;
  1973. if (w < 0) {node.style.marginRight = CHTML.Em(w); w = 0}
  1974. node.style.width = CHTML.Em(w);
  1975. node.style.height = CHTML.Em(Math.max(0,h+d));
  1976. if (d) node.style.verticalAlign = CHTML.Em(-d);
  1977. this.CHTMLhandleBBox(node);
  1978. this.CHTMLhandleColor(node);
  1979. return node;
  1980. }
  1981. });
  1982. /********************************************************/
  1983. MML.mpadded.Augment({
  1984. toCommonHTML: function (node,options) {
  1985. var child;
  1986. if (options && options.stretch) {
  1987. node = node.firstChild; child = node.firstChild;
  1988. } else {
  1989. node = this.CHTMLdefaultNode(node,{childNodes:"mjx-box", forceChild:true});
  1990. child = node.firstChild; node = CHTML.addElement(node,"mjx-block");
  1991. node.appendChild(child); CHTML.addElement(node,"mjx-strut"); // force proper alignment of short heights
  1992. }
  1993. var cbox = this.CHTMLbboxFor(0);
  1994. var values = this.getValues("width","height","depth","lspace","voffset");
  1995. var x = 0, y = 0, w = cbox.w, h = cbox.h, d = cbox.d;
  1996. child.style.width = 0; child.style.margin = CHTML.Em(-h)+" 0 "+CHTML.Em(-d);
  1997. if (values.width !== "") w = this.CHTMLdimen(values.width,"w",w,0);
  1998. if (values.height !== "") h = this.CHTMLdimen(values.height,"h",h,0);
  1999. if (values.depth !== "") d = this.CHTMLdimen(values.depth,"d",d,0);
  2000. if (values.voffset !== "") {
  2001. y = this.CHTMLdimen(values.voffset);
  2002. if (y) {
  2003. child.style.position = "relative";
  2004. child.style.top = CHTML.Em(-y);
  2005. }
  2006. }
  2007. if (values.lspace !== "") {
  2008. x = this.CHTMLdimen(values.lspace);
  2009. if (x) {
  2010. child.style.position = "relative";
  2011. child.style.left = CHTML.Em(x);
  2012. }
  2013. }
  2014. node.style.width = 0;
  2015. node.style.marginTop = CHTML.Em(h-STRUTHEIGHT);
  2016. node.style.padding = "0 "+CHTML.Em(w)+" "+CHTML.Em(d)+" 0";
  2017. var bbox = CHTML.BBOX({w:w, h:h, d:d, l:0, r:w, t:h, b:d,
  2018. scale:this.CHTML.scale, rscale:this.CHTML.rscale});
  2019. bbox.combine(cbox,x,y);
  2020. bbox.w = w; bbox.h = h; bbox.d = d;
  2021. this.CHTML = bbox;
  2022. return node.parentNode;
  2023. },
  2024. CHTMLstretchV: MML.mbase.CHTMLstretchV,
  2025. CHTMLstretchH: MML.mbase.CHTMLstretchH,
  2026. CHTMLdimen: function (length,d,D,m) {
  2027. if (m == null) {m = -BIGDIMEN}
  2028. length = String(length);
  2029. var match = length.match(/width|height|depth/);
  2030. var size = (match ? this.CHTML[match[0].charAt(0)] : (d ? this.CHTML[d] : 0));
  2031. var dimen = (this.CHTMLlength2em(length,size)||0);
  2032. if (length.match(/^[-+]/) && D != null) dimen += D;
  2033. if (m != null) dimen = Math.max(m,dimen);
  2034. return dimen;
  2035. }
  2036. });
  2037. /********************************************************/
  2038. MML.munderover.Augment({
  2039. toCommonHTML: function (node,options) {
  2040. var values = this.getValues("displaystyle","accent","accentunder","align");
  2041. var base = this.data[this.base];
  2042. if (!values.displaystyle && base != null &&
  2043. (base.movablelimits || base.CoreMO().Get("movablelimits")))
  2044. return MML.msubsup.prototype.toCommonHTML.call(this,node,stretch);
  2045. //
  2046. // Get the nodes for base and limits
  2047. //
  2048. var under, over, nodes = [], stretch = false;
  2049. if (options && options.stretch) {
  2050. if (this.data[this.base]) base = CHTML.getNode(node,"mjx-op");
  2051. if (this.data[this.under]) under = CHTML.getNode(node,"mjx-under");
  2052. if (this.data[this.over]) over = CHTML.getNode(node,"mjx-over");
  2053. nodes[0] = base; nodes[1] = under||over; nodes[2] = over;
  2054. stretch = true;
  2055. } else {
  2056. var types = ["mjx-op","mjx-under","mjx-over"];
  2057. if (this.over === 1) types[1] = types[2];
  2058. node = this.CHTMLdefaultNode(node,{
  2059. childNodes:types, noBBox:true, forceChild:true, minChildren: 2
  2060. });
  2061. nodes[0] = base = node.removeChild(node.firstChild);
  2062. nodes[1] = under = over = node.removeChild(node.firstChild);
  2063. if (node.firstChild) nodes[2] = over = node.removeChild(node.firstChild);
  2064. }
  2065. //
  2066. // Get the bounding boxes and the maximum width
  2067. //
  2068. var boxes = [], W = this.CHTMLgetBBoxes(boxes,nodes,values);
  2069. var bbox = boxes[this.base], BBOX = this.CHTML;
  2070. BBOX.w = W; BBOX.h = bbox.h; BBOX.d = bbox.d; // modified below
  2071. //
  2072. // Adjust for bases shorter than the center line (#1657)
  2073. // (the center line really depends on the surrounding font, so
  2074. // it should be measured along with ems and exs, but currently isn't.
  2075. // so this value is an approximation that is reasonable for most fonts.)
  2076. //
  2077. if (bbox.h < .35) base.style.marginTop = CHTML.Em(bbox.h - .35);
  2078. //
  2079. // Use a minimum height for accents (#1706)
  2080. // (same issues with the center line as above)
  2081. //
  2082. if (values.accent && bbox.h < CHTML.TEX.x_height) {
  2083. BBOX.h += CHTML.TEX.x_height - bbox.h;
  2084. base.style.marginTop = CHTML.Em(CHTML.TEX.x_height - Math.max(bbox.h,.35));
  2085. bbox.h = CHTML.TEX.x_height;
  2086. }
  2087. //
  2088. // Add over- and under-scripts
  2089. //
  2090. var stack = base, delta = 0;
  2091. if (bbox.ic) {delta = 1.3*bbox.ic + .05} // make faked IC be closer to expeted results
  2092. if (this.data[this.over]) stack = this.CHTMLaddOverscript(over,boxes,values,delta,base,stretch);
  2093. if (this.data[this.under]) this.CHTMLaddUnderscript(under,boxes,values,delta,node,stack,stretch);
  2094. else if (!stretch) node.appendChild(stack);
  2095. //
  2096. // Handle horizontal positions
  2097. //
  2098. this.CHTMLplaceBoxes(base,under,over,values,boxes);
  2099. return node;
  2100. },
  2101. //
  2102. // Get the bounding boxes for the children, stretch
  2103. // any stretchable elements, and compute the maximum width
  2104. //
  2105. CHTMLgetBBoxes: function (bbox,nodes,values) {
  2106. var i, m = this.data.length, scale,
  2107. w = -BIGDIMEN, // maximum width of non-stretchy items
  2108. W = w; // maximum width of all items
  2109. //
  2110. // Get the maximum width
  2111. //
  2112. for (i = 0; i < m; i++) {
  2113. bbox[i] = this.CHTMLbboxFor(i); bbox[i].x = bbox[i].y = 0;
  2114. if (this.data[i]) bbox[i].stretch = this.data[i].CHTMLcanStretch("Horizontal");
  2115. scale = (i === this.base ? 1 : bbox[i].rscale);
  2116. if (i !== this.base) {delete bbox[i].L; delete bbox[i].R} // these are overriden by CSS
  2117. W = Math.max(W,scale*(bbox[i].w + (bbox[i].L||0) + (bbox[i].R||0)));
  2118. if (!bbox[i].stretch && W > w) w = W;
  2119. }
  2120. if (w === -BIGDIMEN) w = W;
  2121. //
  2122. // Stretch those parts that need it
  2123. //
  2124. for (i = 0; i < m; i++) {
  2125. if (bbox[i].stretch) {
  2126. scale = (i === this.base ? 1 : bbox[i].rscale);
  2127. bbox[i] = this.data[i].CHTMLstretchH(nodes[i].firstChild,w/scale);
  2128. bbox[i].x = bbox[i].y = 0;
  2129. W = Math.max(W,scale*(bbox[i].w + (bbox[i].L||0) + (bbox[i].R||0)));
  2130. }
  2131. }
  2132. if (!bbox[this.base]) bbox[this.base] = CHTML.BBOX.empty();
  2133. return W;
  2134. },
  2135. //
  2136. // Add an overscript
  2137. //
  2138. CHTMLaddOverscript: function (over,boxes,values,delta,base,stretch) {
  2139. var BBOX = this.CHTML;
  2140. var z1, z2, z3 = CHTML.TEX.big_op_spacing5, k;
  2141. var obox = boxes[this.over], bbox = boxes[this.base], scale = obox.rscale;
  2142. //
  2143. // Put the base and script into a stack
  2144. //
  2145. if (!stretch) {
  2146. var stack = CHTML.Element("mjx-stack");
  2147. stack.appendChild(over); stack.appendChild(base);
  2148. }
  2149. if (obox.D) obox.d = obox.D;
  2150. if (obox.d < 0) {
  2151. //
  2152. // For negative depths, set the height and align to top
  2153. // in order to avoid extra baseline space
  2154. //
  2155. over.firstChild.style.verticalAlign = "top";
  2156. over.style.height = CHTML.Em(obox.h+obox.d);
  2157. }
  2158. //
  2159. // Determine the spacing
  2160. //
  2161. obox.x = 0;
  2162. if (values.accent) {
  2163. if (obox.w < .001) obox.x += (obox.r - obox.l)/2; // center combining accents
  2164. k = CHTML.TEX.rule_thickness; z3 = 0;
  2165. if (bbox.skew) {
  2166. obox.x += scale*bbox.skew; BBOX.skew = scale*bbox.skew;
  2167. if (obox.x+scale*obox.w > BBOX.w) BBOX.skew += (BBOX.w - (obox.x+scale*obox.w))/2;
  2168. }
  2169. } else {
  2170. z1 = CHTML.TEX.big_op_spacing1;
  2171. z2 = CHTML.TEX.big_op_spacing3;
  2172. k = Math.max(z1,z2-Math.max(0,scale*obox.d));
  2173. }
  2174. obox.x += delta/2; obox.y = BBOX.h + k + z3 + scale*obox.d;
  2175. //
  2176. // Position the overscript
  2177. //
  2178. if (k) over.style.paddingBottom = CHTML.Em(k/scale);
  2179. if (z3) over.style.paddingTop = CHTML.Em(z3/scale);
  2180. return stack;
  2181. },
  2182. //
  2183. // Add an underscript
  2184. //
  2185. CHTMLaddUnderscript: function (under,boxes,values,delta,node,stack,stretch) {
  2186. var BBOX = this.CHTML;
  2187. var z1, z2, z3 = CHTML.TEX.big_op_spacing5, k;
  2188. var ubox = boxes[this.under], scale = ubox.rscale;
  2189. //
  2190. // Create a table for the underscript
  2191. //
  2192. if (!stretch) {
  2193. CHTML.addElement(node,"mjx-itable",{},[
  2194. ["mjx-row",{},[["mjx-cell"]]],
  2195. ["mjx-row"]
  2196. ]);
  2197. node.firstChild.firstChild.firstChild.appendChild(stack);
  2198. node.firstChild.lastChild.appendChild(under);
  2199. }
  2200. if (ubox.D) ubox.d = ubox.D;
  2201. if (ubox.d < 0) {
  2202. //
  2203. // For negative depths, set the height and align to top
  2204. // in order to avoid extra baseline space
  2205. //
  2206. under.firstChild.style.verticalAlign = "top";
  2207. node.firstChild.style.marginBottom = CHTML.Em(ubox.d);
  2208. }
  2209. //
  2210. // determine the spacing
  2211. //
  2212. if (values.accentunder) {
  2213. k = 2*CHTML.TEX.rule_thickness; z3 = 0;
  2214. } else {
  2215. z1 = CHTML.TEX.big_op_spacing2;
  2216. z2 = CHTML.TEX.big_op_spacing4;
  2217. k = Math.max(z1,z2-scale*ubox.h);
  2218. }
  2219. ubox.x = -delta/2; ubox.y = -(BBOX.d + k + z3 + scale*ubox.h);
  2220. //
  2221. // Position the underscript
  2222. //
  2223. if (k) under.style.paddingTop = CHTML.Em(k/scale);
  2224. if (z3) under.style.paddingBottom = CHTML.Em(z3/scale);
  2225. },
  2226. //
  2227. // Center boxes horizontally, taking offsets into account
  2228. //
  2229. CHTMLplaceBoxes: function (base,under,over,values,boxes) {
  2230. var W = this.CHTML.w, i, m = boxes.length, scale;
  2231. var BBOX = CHTML.BBOX.zero();
  2232. BBOX.scale = this.CHTML.scale; BBOX.rscale = this.CHTML.rscale;
  2233. boxes[this.base].x = boxes[this.base].y = 0; var dx = BIGDIMEN;
  2234. for (i = 0; i < m; i++) {
  2235. scale = (i === this.base ? 1 : boxes[i].rscale);
  2236. var w = scale*(boxes[i].w + (boxes[i].L||0) + (boxes[i].R||0));
  2237. boxes[i].x += {left:0, center:(W-w)/2, right:W-w}[values.align];
  2238. if (boxes[i].x < dx) dx = boxes[i].x;
  2239. }
  2240. for (i = 0; i < m; i++) {
  2241. if (this.data[i]) {
  2242. scale = (i === this.base ? 1 : boxes[i].rscale);
  2243. if (boxes[i].x - dx) {
  2244. var node = (i === this.base ? base : i === this.over ? over : under);
  2245. node.style.paddingLeft = CHTML.Em((boxes[i].x-dx)/scale);
  2246. }
  2247. BBOX.combine(boxes[i],boxes[i].x-dx,boxes[i].y);
  2248. }
  2249. }
  2250. this.CHTML = BBOX;
  2251. },
  2252. CHTMLstretchV: MML.mbase.CHTMLstretchV,
  2253. CHTMLstretchH: MML.mbase.CHTMLstretchH,
  2254. CHTMLchildNode: function (node,i) {
  2255. var types = ["mjx-op","mjx-under","mjx-over"];
  2256. if (this.over === 1) types[1] = types[2];
  2257. return CHTML.getNode(node,types[i]);
  2258. }
  2259. });
  2260. /********************************************************/
  2261. MML.msubsup.Augment({
  2262. toCommonHTML: function (node,options) {
  2263. var values = this.getValues(
  2264. "displaystyle","subscriptshift","superscriptshift","texprimestyle"
  2265. );
  2266. //
  2267. // Get the nodes for base and limits
  2268. //
  2269. var base, sub, sup;
  2270. if (options && options.stretch) {
  2271. if (this.data[this.base]) base = CHTML.getNode(node,"mjx-base");
  2272. if (this.data[this.sub]) sub = CHTML.getNode(node,"mjx-sub");
  2273. if (this.data[this.sup]) sup = CHTML.getNode(node,"mjx-sup");
  2274. stack = CHTML.getNode(node,"mjx-stack");
  2275. } else {
  2276. var types = ["mjx-base","mjx-sub","mjx-sup"];
  2277. if (this.sup === 1) types[1] = types[2];
  2278. node = this.CHTMLdefaultNode(node,{
  2279. childNodes:types, noBBox:true, forceChild:true, minChildren: 3
  2280. });
  2281. base = node.childNodes[this.base];
  2282. sub = node.childNodes[this.sub]; sup = node.childNodes[this.sup];
  2283. if (!this.CHTMLnotEmpty(this.data[this.sub])) {node.removeChild(sub); sub = null}
  2284. if (!this.CHTMLnotEmpty(this.data[this.sup])) {node.removeChild(sup); sup = null}
  2285. if (node.childNodes.length === 3) {
  2286. var stack = CHTML.addElement(node,"mjx-stack");
  2287. stack.appendChild(sup); stack.appendChild(sub);
  2288. }
  2289. }
  2290. //
  2291. // Get the bounding boxes and maximum width of scripts
  2292. //
  2293. var boxes = [], BBOX = CHTML.BBOX.empty(this.CHTML);
  2294. for (var i = 0, m = this.data.length; i < m; i++) boxes[i] = this.CHTMLbboxFor(i);
  2295. var bbox = boxes[this.base] || CHTML.BBOX.empty(),
  2296. sbox = boxes[this.sub], Sbox = boxes[this.sup];
  2297. var sscale = (sub ? sbox.rscale : 1), Sscale = (sup ? Sbox.rscale : 1);
  2298. BBOX.combine(bbox,0,0);
  2299. //
  2300. // Get initial values for parameters
  2301. //
  2302. var ex = CHTML.TEX.x_height, s = CHTML.TEX.scriptspace;
  2303. var q = CHTML.TEX.sup_drop * Sscale, r = CHTML.TEX.sub_drop * sscale;
  2304. var u = bbox.h - q, v = bbox.d + r, delta = 0, p;
  2305. if (bbox.ic) {
  2306. BBOX.w -= bbox.ic; // remove IC (added by mo and mi)
  2307. base.style.marginRight = CHTML.Em(-bbox.ic);
  2308. delta = 1.3*bbox.ic + .05; // make faked IC be closer to expeted results
  2309. }
  2310. var bmml = this.data[this.base];
  2311. if (bmml) {
  2312. if ((bmml.type === "mrow" || bmml.type === "mstyle") && bmml.data.length === 1) bmml = bmml.data[0];
  2313. if (bmml.type === "mi" || bmml.type === "mo") {
  2314. if (bmml.data.join("").length === 1 && bbox.rscale === 1 && !bbox.sH &&
  2315. !bmml.Get("largeop")) {u = v = 0}
  2316. }
  2317. }
  2318. values.subscriptshift = (values.subscriptshift === "" ? 0 : this.CHTMLlength2em(values.subscriptshift));
  2319. values.superscriptshift = (values.superscriptshift === "" ? 0 : this.CHTMLlength2em(values.superscriptshift));
  2320. //
  2321. // Add the super- and subscripts
  2322. //
  2323. var x = BBOX.w; if (sub) sbox.w += s; if (sup) Sbox.w += s;
  2324. if (!sup) {
  2325. if (sub) {
  2326. v = Math.max(v,CHTML.TEX.sub1,sscale*sbox.h-(4/5)*ex,values.subscriptshift);
  2327. sub.style.verticalAlign = CHTML.Em(-v/sscale);
  2328. sub.style.paddingRight = CHTML.Em(s/sscale);
  2329. BBOX.combine(sbox,x,-v);
  2330. }
  2331. } else {
  2332. if (!sub) {
  2333. p = CHTML.TEX[(values.displaystyle ? "sup1" : (values.texprimestyle ? "sup3" : "sup2"))];
  2334. u = Math.max(u,p,Sscale*Sbox.d+(1/4)*ex,values.superscriptshift);
  2335. sup.style.verticalAlign = CHTML.Em(u/Sscale);
  2336. sup.style.paddingLeft = CHTML.Em(delta/Sscale);
  2337. sup.style.paddingRight = CHTML.Em(s/Sscale);
  2338. BBOX.combine(Sbox,x+delta,u);
  2339. } else {
  2340. v = Math.max(v,CHTML.TEX.sub2);
  2341. var t = CHTML.TEX.rule_thickness;
  2342. if ((u - Sscale*Sbox.d) - (sscale*sbox.h - v) < 3*t) {
  2343. v = 3*t - u + Sscale*Sbox.d + sscale*sbox.h;
  2344. q = (4/5)*ex - (u - Sscale*Sbox.d);
  2345. if (q > 0) {u += q; v -= q}
  2346. }
  2347. u = Math.max(u,values.superscriptshift);
  2348. v = Math.max(v,values.subscriptshift);
  2349. sub.style.paddingRight = CHTML.Em(s/sscale);
  2350. sup.style.paddingBottom = CHTML.Em(u/Sscale+v/sscale-Sbox.d-sbox.h/sscale*Sscale);
  2351. sup.style.paddingLeft = CHTML.Em(delta/Sscale);
  2352. sup.style.paddingRight = CHTML.Em(s/Sscale);
  2353. stack.style.verticalAlign = CHTML.Em(-v);
  2354. BBOX.combine(Sbox,x+delta,u);
  2355. BBOX.combine(sbox,x,-v);
  2356. }
  2357. }
  2358. BBOX.clean();
  2359. return node;
  2360. },
  2361. CHTMLstretchV: MML.mbase.CHTMLstretchV,
  2362. CHTMLstretchH: MML.mbase.CHTMLstretchH,
  2363. CHTMLchildNode: function (node,i) {
  2364. var types = ["mjx-base","mjx-sub","mjx-sup"];
  2365. if (this.over === 1) types[1] = types[2];
  2366. return CHTML.getNode(node,types[i]);
  2367. }
  2368. });
  2369. /********************************************************/
  2370. MML.mfrac.Augment({
  2371. toCommonHTML: function (node) {
  2372. node = this.CHTMLdefaultNode(node,{
  2373. childNodes:["mjx-numerator","mjx-denominator"],
  2374. childOptions: {autowidth: true},
  2375. forceChild:true, noBBox:true, minChildren:2
  2376. });
  2377. var values = this.getValues("linethickness","displaystyle",
  2378. "numalign","denomalign","bevelled");
  2379. var isDisplay = values.displaystyle;
  2380. //
  2381. // Create the table for the fraction and set the alignment
  2382. //
  2383. var num = node.firstChild, denom = node.lastChild;
  2384. var frac = CHTML.addElement(node,"mjx-box");
  2385. frac.appendChild(num); frac.appendChild(denom); node.appendChild(frac);
  2386. if (values.numalign !== "center") num.style.textAlign = values.numalign;
  2387. if (values.denomalign !== "center") denom.style.textAlign = values.denomalign;
  2388. //
  2389. // Get the bounding boxes for the parts, and determine the placement
  2390. // of the numerator and denominator
  2391. //
  2392. var nbox = this.CHTMLbboxFor(0), dbox = this.CHTMLbboxFor(1),
  2393. BBOX = CHTML.BBOX.empty(this.CHTML), nscale = nbox.rscale, dscale = dbox.rscale;
  2394. values.linethickness = Math.max(0,CHTML.thickness2em(values.linethickness||"0",BBOX.scale));
  2395. var mt = CHTML.TEX.min_rule_thickness/CHTML.em, a = CHTML.TEX.axis_height;
  2396. var t = values.linethickness, p,q, u,v;
  2397. if (values.bevelled) {
  2398. frac.className += " MJXc-bevelled";
  2399. var delta = (isDisplay ? .4 : .15);
  2400. var H = Math.max(nscale*(nbox.h+nbox.d),dscale*(dbox.h+dbox.d)) + 2*delta;
  2401. var bevel = CHTML.Element("mjx-bevel"); frac.insertBefore(bevel,denom);
  2402. var bbox = CHTML.createDelimiter(bevel,0x2F,H);
  2403. u = nscale*(nbox.d-nbox.h)/2+a+delta;
  2404. v = dscale*(dbox.d-dbox.h)/2+a-delta;
  2405. if (u) num.style.verticalAlign = CHTML.Em(u/nscale);
  2406. if (v) denom.style.verticalAlign = CHTML.Em(v/dscale);
  2407. bevel.style.marginLeft = bevel.style.marginRight = CHTML.Em(-delta/2);
  2408. BBOX.combine(nbox,0,u);
  2409. BBOX.combine(bbox,nscale*nbox.w-delta/2,0);
  2410. BBOX.combine(dbox,nscale*nbox.w+bbox.w-delta,v);
  2411. BBOX.clean();
  2412. } else {
  2413. frac.className += " MJXc-stacked";
  2414. if (isDisplay) {u = CHTML.TEX.num1; v = CHTML.TEX.denom1}
  2415. else {u = (t === 0 ? CHTML.TEX.num3 : CHTML.TEX.num2); v = CHTML.TEX.denom2}
  2416. if (t === 0) { // \atop
  2417. p = Math.max((isDisplay ? 7 : 3) * CHTML.TEX.rule_thickness, 2*mt); // force to at least 2 px
  2418. q = (u - nbox.d*nscale) - (dbox.h*dscale - v);
  2419. if (q < p) {u += (p - q)/2; v += (p - q)/2}
  2420. } else { // \over
  2421. p = Math.max((isDisplay ? 2 : 0) * mt + t, t/2 + 1.5*mt);
  2422. t = Math.max(t,mt);
  2423. q = (u - nbox.d*nscale) - (a + t/2); if (q < p) u += (p - q);
  2424. q = (a - t/2) - (dbox.h*dscale - v); if (q < p) v += (p - q);
  2425. nbox.L = nbox.R = dbox.L = dbox.R = .1; // account for padding in BBOX width
  2426. var rule = CHTML.addElement(frac,"mjx-line",{style: {
  2427. "border-bottom":CHTML.Px(t*BBOX.scale,1)+" solid", top: CHTML.Em(-t/2-a)
  2428. }});
  2429. }
  2430. //
  2431. // Determine the new bounding box and place the parts
  2432. //
  2433. BBOX.combine(nbox,0,u);
  2434. BBOX.combine(dbox,0,-v);
  2435. BBOX.clean();
  2436. //
  2437. // Force elements to the correct width
  2438. //
  2439. frac.style.width = CHTML.Em(BBOX.w);
  2440. num.style.width = CHTML.Em(BBOX.w/nscale);
  2441. denom.style.width = CHTML.Em(BBOX.w/dscale);
  2442. if (rule) rule.style.width = frac.style.width;
  2443. //
  2444. // Place the numerator and denominator in relation to the baseline
  2445. //
  2446. num.style.top = CHTML.Em(-BBOX.h/nscale);
  2447. denom.style.bottom = CHTML.Em(-BBOX.d/dscale);
  2448. //
  2449. // Force the size of the surrounding box, since everything is absolutely positioned
  2450. //
  2451. CHTML.addElement(node,"mjx-vsize",{style: {
  2452. height: CHTML.Em(BBOX.h+BBOX.d), verticalAlign: CHTML.Em(-BBOX.d)
  2453. }});
  2454. }
  2455. //
  2456. // Add nulldelimiterspace around the fraction
  2457. // (TeXBook pg 150 and Appendix G rule 15e)
  2458. //
  2459. if (!this.texWithDelims) {
  2460. var space = CHTML.TEX.nulldelimiterspace;
  2461. frac.style.padding = "0 "+CHTML.Em(space);
  2462. BBOX.l += space; BBOX.r += space; BBOX.w += 2*space;
  2463. }
  2464. //
  2465. // Return the completed fraction
  2466. //
  2467. return node;
  2468. },
  2469. CHTMLcanStretch: function (direction) {return false}
  2470. });
  2471. /********************************************************/
  2472. MML.msqrt.Augment({
  2473. toCommonHTML: function (node) {
  2474. node = this.CHTMLdefaultNode(node,{
  2475. childNodes:["mjx-box","mjx-root"], forceChild:true, noBBox:true
  2476. });
  2477. var base = node.firstChild || CHTML.Element("mjx-box");
  2478. var sqrt = CHTML.addElement(node,"mjx-box"); sqrt.appendChild(base);
  2479. var bbox = this.CHTMLbboxFor(0), BBOX = CHTML.BBOX.empty(this.CHTML);
  2480. var t = CHTML.TEX.rule_thickness, T = CHTML.TEX.surd_height, p = t, q, H;
  2481. if (this.Get("displaystyle")) p = CHTML.TEX.x_height;
  2482. q = t + p/4;
  2483. H = bbox.h + bbox.d + q + t;
  2484. var surd = CHTML.Element("mjx-surd"); sqrt.insertBefore(surd,base);
  2485. var sbox = CHTML.createDelimiter(surd,0x221A,[H-.04,H]);
  2486. if (sbox.h + sbox.d > H) q = ((sbox.h+sbox.d) - (H-t))/2;
  2487. H = bbox.h + q + t;
  2488. var x = this.CHTMLaddRoot(node,sbox,sbox.h+sbox.d-H);
  2489. base.style.paddingTop = CHTML.Em(q);
  2490. base.style.borderTop = CHTML.Px(T*bbox.scale,1)+" solid";
  2491. sqrt.style.paddingTop = CHTML.Em(2*t-T); // use wider line, but don't affect height
  2492. bbox.h += q + 2*t;
  2493. BBOX.combine(sbox,x,H-sbox.h);
  2494. BBOX.combine(bbox,x+sbox.w,0);
  2495. BBOX.clean();
  2496. return node;
  2497. },
  2498. CHTMLaddRoot: function () {return 0}
  2499. });
  2500. /********************************************************/
  2501. MML.mroot.Augment({
  2502. toCommonHTML: MML.msqrt.prototype.toCommonHTML,
  2503. CHTMLaddRoot: function (sqrt,sbox,d) {
  2504. if (!this.data[1]) return;
  2505. var BBOX = this.CHTML, bbox = this.data[1].CHTML, root = sqrt.firstChild;
  2506. var scale = bbox.rscale;
  2507. var h = this.CHTMLrootHeight(bbox,sbox,scale)-d;
  2508. var w = Math.min(bbox.w,bbox.r); // remove extra right-hand padding, if any
  2509. var dx = Math.max(w,sbox.offset/scale);
  2510. if (h) root.style.verticalAlign = CHTML.Em(h/scale);
  2511. if (dx > w) root.firstChild.style.paddingLeft = CHTML.Em(dx-w);
  2512. dx -= sbox.offset/scale;
  2513. root.style.width = CHTML.Em(dx);
  2514. BBOX.combine(bbox,0,h);
  2515. return dx*scale;
  2516. },
  2517. CHTMLrootHeight: function (bbox,sbox,scale) {
  2518. return .45*(sbox.h+sbox.d-.9)+sbox.offset + Math.max(0,bbox.d-.075);
  2519. }
  2520. });
  2521. /********************************************************/
  2522. MML.mfenced.Augment({
  2523. toCommonHTML: function (node) {
  2524. node = this.CHTMLcreateNode(node);
  2525. this.CHTMLhandleStyle(node);
  2526. this.CHTMLhandleScale(node);
  2527. //
  2528. // Make row of open, data, sep, ... data, close
  2529. //
  2530. this.CHTMLaddChild(node,"open",{});
  2531. for (var i = 0, m = this.data.length; i < m; i++) {
  2532. this.CHTMLaddChild(node,"sep"+i,{});
  2533. this.CHTMLaddChild(node,i,{});
  2534. }
  2535. this.CHTMLaddChild(node,"close",{});
  2536. //
  2537. // Check for stretching the elements
  2538. //
  2539. var H = this.CHTML.h, D = this.CHTML.d;
  2540. this.CHTMLstretchChildV("open",H,D);
  2541. for (i = 0, m = this.data.length; i < m; i++) {
  2542. this.CHTMLstretchChildV("sep"+i,H,D);
  2543. this.CHTMLstretchChildV(i,H,D);
  2544. }
  2545. this.CHTMLstretchChildV("close",H,D);
  2546. this.CHTMLhandleSpace(node);
  2547. this.CHTMLhandleBBox(node);
  2548. this.CHTMLhandleColor(node);
  2549. return node;
  2550. }
  2551. });
  2552. /********************************************************/
  2553. MML.mrow.Augment({
  2554. toCommonHTML: function (node,options) {
  2555. options = options || {};
  2556. node = this.CHTMLdefaultNode(node);
  2557. var bbox = this.CHTML, H = bbox.h, D = bbox.d, hasNegative;
  2558. for (var i = 0, m = this.data.length; i < m; i++) {
  2559. this.CHTMLstretchChildV(i,H,D);
  2560. if (this.data[i] && this.data[i].CHTML && this.data[i].CHTML.w < 0) hasNegative = true;
  2561. }
  2562. if (this.CHTMLlineBreaks()) {
  2563. this.CHTMLmultiline(node);
  2564. if (options.autowidth) node.style.width = "";
  2565. } else {
  2566. if (hasNegative && bbox.w) node.style.width = CHTML.Em(Math.max(0,bbox.w));
  2567. if (bbox.w < 0) node.style.marginRight = CHTML.Em(bbox.w);
  2568. }
  2569. return node;
  2570. },
  2571. CHTMLlineBreaks: function () {
  2572. if (!this.parent.linebreakContainer) return false;
  2573. return (LINEBREAKS.automatic && this.CHTML.w > CHTML.linebreakWidth) || this.hasNewline();
  2574. },
  2575. CHTMLstretchV: function (h,d) {
  2576. this.CHTMLstretchChildV(this.CoreIndex(),h,d);
  2577. return this.CHTML;
  2578. },
  2579. CHTMLstretchH: function (node,w) {
  2580. this.CHTMLstretchChildH(this.CoreIndex(),w,node);
  2581. return this.CHTML;
  2582. }
  2583. });
  2584. /********************************************************/
  2585. MML.mstyle.Augment({
  2586. toCommonHTML: function (node) {
  2587. node = this.CHTMLdefaultNode(node);
  2588. if (this.scriptlevel && this.data[0]) this.CHTML.rescale(this.data[0].CHTML.rscale);
  2589. return node;
  2590. }
  2591. });
  2592. /********************************************************/
  2593. MML.TeXAtom.Augment({
  2594. toCommonHTML: function (node,options) {
  2595. if (!options || !options.stretch) node = this.CHTMLdefaultNode(node);
  2596. if (this.texClass === MML.TEXCLASS.VCENTER) {
  2597. var a = CHTML.TEX.axis_height, BBOX = this.CHTML;
  2598. var v = a-(BBOX.h+BBOX.d)/2+BBOX.d;
  2599. if (Math.abs(v) > .001) {
  2600. node.style.verticalAlign = CHTML.Em(v);
  2601. BBOX.h += v; BBOX.t += v; BBOX.d -= v; BBOX.b -= v;
  2602. }
  2603. }
  2604. return node;
  2605. },
  2606. CHTMLstretchV: function (h,d) {
  2607. this.CHTMLupdateFrom(this.Core().CHTMLstretchV(h,d));
  2608. this.toCommonHTML(this.CHTMLnodeElement(),{stretch:true});
  2609. return this.CHTML;
  2610. },
  2611. CHTMLstretchH: function (node,w) {
  2612. this.CHTMLupdateFrom(this.CHTMLstretchCoreH(node,w));
  2613. this.toCommonHTML(node,{stretch:true});
  2614. return this.CHTML;
  2615. }
  2616. });
  2617. /********************************************************/
  2618. MML.semantics.Augment({
  2619. toCommonHTML: function (node) {
  2620. node = this.CHTMLcreateNode(node);
  2621. if (this.data[0]) {
  2622. this.data[0].toCommonHTML(node);
  2623. this.CHTMLupdateFrom(this.data[0].CHTML);
  2624. this.CHTMLhandleBBox(node);
  2625. }
  2626. return node;
  2627. }
  2628. });
  2629. MML.annotation.Augment({toCommonHTML: function(node) {return this.CHTMLcreateNode(node)}});
  2630. MML["annotation-xml"].Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2631. /********************************************************/
  2632. MML.ms.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2633. MML.mglyph.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2634. MML.menclose.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2635. MML.maction.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2636. MML.mmultiscripts.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2637. MML.mtable.Augment({toCommonHTML: MML.mbase.CHTMLautoload});
  2638. /********************************************************/
  2639. //
  2640. // Loading isn't complete until the element jax is modified,
  2641. // but can't call loadComplete within the callback for "mml Jax Ready"
  2642. // (it would call CommonHTML's Require routine, asking for the mml jax again)
  2643. // so wait until after the mml jax has finished processing.
  2644. //
  2645. // We also need to wait for the onload handler to run, since the loadComplete
  2646. // will call Config and Startup, which need to modify the body.
  2647. //
  2648. MathJax.Hub.Register.StartupHook("onLoad",function () {
  2649. setTimeout(MathJax.Callback(["loadComplete",CHTML,"jax.js"]),0);
  2650. });
  2651. });
  2652. MathJax.Hub.Register.StartupHook("End Cookie", function () {
  2653. if (HUB.config.menuSettings.zoom !== "None")
  2654. {AJAX.Require("[MathJax]/extensions/MathZoom.js")}
  2655. });
  2656. })(MathJax.Ajax,MathJax.Hub,MathJax.HTML,MathJax.OutputJax.CommonHTML);