jax.js 112 KB

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