www

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

mglyph.js (4707B)


      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/HTML-CSS/autoload/mglyph.js
      7  *  
      8  *  Implements the HTML-CSS output for <mglyph> elements.
      9  *
     10  *  ---------------------------------------------------------------------
     11  *  
     12  *  Copyright (c) 2010-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("HTML-CSS Jax Ready",function () {
     28   var VERSION = "2.6.0";
     29   var MML = MathJax.ElementJax.mml,
     30       HTMLCSS = MathJax.OutputJax["HTML-CSS"],
     31       LOCALE = MathJax.Localization;
     32   
     33   MML.mglyph.Augment({
     34     toHTML: function (span,variant) {
     35       var SPAN = span, values = this.getValues("src","width","height","valign","alt"), err;
     36       span = this.HTMLcreateSpan(span);
     37       if (values.src === "") {
     38         var index = this.Get("index");
     39         if (index) {
     40           variant = this.HTMLgetVariant(); var font = variant.defaultFont;
     41           if (font) {
     42             font.noStyleChar = true; font.testString = String.fromCharCode(index) + 'ABCabc';
     43             if (HTMLCSS.Font.testFont(font)) {
     44               this.HTMLhandleVariant(span,variant,String.fromCharCode(index));
     45             } else {
     46               if (values.alt === "")
     47                 {values.alt = LOCALE._(["MathML","BadMglyphFont"],"Bad font: %1",font.family)}
     48               err = MML.Error(values.alt,{mathsize:"75%"});
     49               this.Append(err); err.toHTML(span); this.data.pop();
     50               span.bbox = err.HTMLspanElement().bbox;
     51             }
     52           }
     53         }
     54       } else {
     55         if (!this.img) {this.img = MML.mglyph.GLYPH[values.src]}
     56         if (!this.img) {
     57           this.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"};
     58           var img = this.img.img;
     59           img.onload = MathJax.Callback(["HTMLimgLoaded",this]);
     60           img.onerror = MathJax.Callback(["HTMLimgError",this]);
     61           img.src = values.src;
     62           MathJax.Hub.RestartAfter(img.onload);
     63         }
     64         if (this.img.status !== "OK") {
     65           err = MML.Error(
     66             LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src),
     67             {mathsize:"75%"});
     68           this.Append(err); err.toHTML(span); this.data.pop();
     69           span.bbox = err.HTMLspanElement().bbox;
     70         } else {
     71           var mu = this.HTMLgetMu(span);
     72           img = HTMLCSS.addElement(span,"img",{isMathJax:true, src:values.src, alt:values.alt, title:values.alt});
     73           if (values.width)  {
     74             img.style.width = HTMLCSS.Em(HTMLCSS.length2em(values.width,mu,this.img.img.width/HTMLCSS.em));
     75           }
     76           if (values.height) {
     77             img.style.height = HTMLCSS.Em(HTMLCSS.length2em(values.height,mu,this.img.img.height/HTMLCSS.em));
     78           }
     79           span.bbox.w = span.bbox.rw = img.offsetWidth/HTMLCSS.em;
     80           span.bbox.h = img.offsetHeight/HTMLCSS.em;
     81           if (values.valign) {
     82             span.bbox.d = -HTMLCSS.length2em(values.valign,mu,this.img.img.height/HTMLCSS.em);
     83             img.style.verticalAlign = HTMLCSS.Em(-span.bbox.d);
     84             span.bbox.h -= span.bbox.d;
     85           }
     86         }
     87       }
     88       if (!SPAN.bbox) {
     89         SPAN.bbox = {w: span.bbox.w, h: span.bbox.h, d: span.bbox.d,
     90                      rw: span.bbox.rw, lw: span.bbox.lw};
     91       } else if (span.bbox) {
     92         SPAN.bbox.w += span.bbox.w;
     93         if (SPAN.bbox.w > SPAN.bbox.rw) {SPAN.bbox.rw = SPAN.bbox.w}
     94         if (span.bbox.h > SPAN.bbox.h) {SPAN.bbox.h = span.bbox.h}
     95         if (span.bbox.d > SPAN.bbox.d) {SPAN.bbox.d = span.bbox.d}
     96       }
     97       this.HTMLhandleSpace(span);
     98       this.HTMLhandleColor(span);
     99       return span;
    100     },
    101     HTMLimgLoaded: function (event,status) {
    102       if (typeof(event) === "string") {status = event}
    103       this.img.status = (status || "OK")
    104     },
    105     HTMLimgError: function () {this.img.img.onload("error")}
    106   },{
    107     GLYPH: {}    // global list of all loaded glyphs
    108   });
    109   
    110   MathJax.Hub.Startup.signal.Post("HTML-CSS mglyph Ready");
    111   MathJax.Ajax.loadComplete(HTMLCSS.autoloadDir+"/mglyph.js");
    112   
    113 });
    114