www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

mglyph.js (3866B)


      1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 
      4 /*************************************************************
      5  *
      6  *  MathJax/jax/output/CommonHTML/autoload/mglyph.js
      7  *  
      8  *  Implements the CommonHTML output for <mglyph> elements.
      9  *
     10  *  ---------------------------------------------------------------------
     11  *  
     12  *  Copyright (c) 2015 The MathJax Consortium
     13  * 
     14  *  Licensed under the Apache License, Version 2.0 (the "License");
     15  *  you may not use this file except in compliance with the License.
     16  *  You may obtain a copy of the License at
     17  * 
     18  *      http://www.apache.org/licenses/LICENSE-2.0
     19  * 
     20  *  Unless required by applicable law or agreed to in writing, software
     21  *  distributed under the License is distributed on an "AS IS" BASIS,
     22  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     23  *  See the License for the specific language governing permissions and
     24  *  limitations under the License.
     25  */
     26 
     27 MathJax.Hub.Register.StartupHook("CommonHTML Jax Ready",function () {
     28   var VERSION = "2.6.0";
     29   var MML = MathJax.ElementJax.mml,
     30       CHTML = MathJax.OutputJax.CommonHTML,
     31       LOCALE = MathJax.Localization;
     32   
     33   MML.mglyph.Augment({
     34     toCommonHTML: function (node,options) {
     35       var values = this.getValues("src","width","height","valign","alt");
     36       node = this.CHTMLcreateNode(node);
     37       this.CHTMLhandleStyle(node);
     38       this.CHTMLhandleScale(node);
     39       if (values.src === "") {
     40         var index = this.Get("index");
     41         this.CHTMLgetVariant();
     42         if (index && this.CHTMLvariant.style)
     43           this.CHTMLhandleText(node,String.fromCharCode(index),this.CHTMLvariant);
     44       } else {
     45         var bbox = this.CHTML;
     46         if (!bbox.img) bbox.img = MML.mglyph.GLYPH[values.src];
     47         if (!bbox.img) {
     48           bbox.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"};
     49           bbox.img.img.onload  = MathJax.Callback(["CHTMLimgLoaded",this]);
     50           bbox.img.img.onerror = MathJax.Callback(["CHTMLimgError",this]);
     51           bbox.img.img.src = values.src;
     52           MathJax.Hub.RestartAfter(bbox.img.img.onload);
     53         }
     54         if (bbox.img.status !== "OK") {
     55           var err = MML.Error(LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src));
     56           err.data[0].data[0].mathsize = "75%";
     57           this.Append(err); err.toCommonHTML(node); this.data.pop();
     58           bbox.combine(err.CHTML,0,0,1);
     59         } else {
     60           var img = CHTML.addElement(node,"img",{
     61             isMathJax:true, src:values.src, alt:values.alt, title:values.alt
     62           });
     63           var w = bbox.img.img.width/CHTML.em, h = bbox.img.img.height/CHTML.em;
     64           if (values.width !== "")  img.style.width  = CHTML.Em(this.CHTMLlength2em(values.width,w));
     65           if (values.height !== "") img.style.height = CHTML.Em(this.CHTMLlength2em(values.height,h));
     66           //
     67           //  Warning:  causes page reflows
     68           //
     69           bbox.w = bbox.r = img.offsetWidth/CHTML.em; bbox.h = bbox.t = img.offsetHeight/CHTML.em;
     70           if (values.valign) {
     71             bbox.d = bbox.b = -this.CHTMLlength2em(values.valign,h);
     72             img.style.verticalAlign = CHTML.Em(-bbox.d);
     73             bbox.h -= bbox.d; bbox.t = bbox.h;
     74           }
     75         }
     76       }
     77       this.CHTMLhandleSpace(node);
     78       this.CHTMLhandleBBox(node);
     79       this.CHTMLhandleColor(node);
     80       return node;
     81     },
     82     CHTMLimgLoaded: function (event,status) {
     83       if (typeof(event) === "string") status = event;
     84       this.CHTML.img.status = (status || "OK");
     85     },
     86     CHTMLimgError: function () {this.CHTML.img.img.onload("error")}
     87   },{
     88     GLYPH: {}    // global list of all loaded glyphs
     89   });
     90   
     91   MathJax.Hub.Startup.signal.Post("CommonHTML mglyph Ready");
     92   MathJax.Ajax.loadComplete(CHTML.autoloadDir+"/mglyph.js");
     93 });
     94