mglyph.js (4131B)
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/SVG/autoload/mglyph.js 7 * 8 * Implements the SVG output for <mglyph> elements. 9 * 10 * --------------------------------------------------------------------- 11 * 12 * Copyright (c) 2011-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("SVG Jax Ready",function () { 28 var VERSION = "2.6.0"; 29 var MML = MathJax.ElementJax.mml, 30 SVG = MathJax.OutputJax.SVG, 31 BBOX = SVG.BBOX, 32 LOCALE = MathJax.Localization; 33 34 var XLINKNS = "http://www.w3.org/1999/xlink"; 35 36 BBOX.MGLYPH = BBOX.Subclass({ 37 type: "image", removeable: false, 38 Init: function (img,w,h,align,mu,def) { 39 if (def == null) {def = {}} 40 var W = img.width*1000/SVG.em, H = img.height*1000/SVG.em; 41 var WW = W, HH = H, y = 0; 42 if (w !== "") {W = SVG.length2em(w,mu,WW); H = (WW ? W/WW * HH : 0)} 43 if (h !== "") {H = SVG.length2em(h,mu,HH); if (w === "") {W = (HH ? H/HH * WW : 0)}} 44 if (align !== "" && align.match(/\d/)) {y = SVG.length2em(align,mu); def.y = -y} 45 def.height = Math.floor(H); def.width = Math.floor(W); 46 def.transform = "translate(0,"+H+") matrix(1 0 0 -1 0 0)"; 47 def.preserveAspectRatio = "none"; 48 this.SUPER(arguments).Init.call(this,def); 49 this.element.setAttributeNS(XLINKNS,"href",img.src); 50 this.w = this.r = W; this.h = this.H = H + y; 51 this.d = this.D = -y; this.l = 0; 52 } 53 }); 54 55 MML.mglyph.Augment({ 56 toSVG: function (variant,scale) { 57 this.SVGgetStyles(); var svg = this.SVG(), img, err; 58 this.SVGhandleSpace(svg); 59 var values = this.getValues("src","width","height","valign","alt"); 60 if (values.src === "") { 61 values = this.getValues("index","fontfamily"); 62 if (values.index) { 63 if (!scale) {scale = this.SVGgetScale()} 64 var def = {}; if (values.fontfamily) {def["font-family"] = values.fontfamily} 65 svg.Add(BBOX.TEXT(scale,String.fromCharCode(values.index),def)); 66 } 67 } else { 68 if (!this.img) {this.img = MML.mglyph.GLYPH[values.src]} 69 if (!this.img) { 70 this.img = MML.mglyph.GLYPH[values.src] = {img: new Image(), status: "pending"}; 71 img = this.img.img; 72 img.onload = MathJax.Callback(["SVGimgLoaded",this]); 73 img.onerror = MathJax.Callback(["SVGimgError",this]); 74 img.src = values.src; 75 MathJax.Hub.RestartAfter(img.onload); 76 } 77 if (this.img.status !== "OK") { 78 err = MML.Error( 79 LOCALE._(["MathML","BadMglyph"],"Bad mglyph: %1",values.src), 80 {mathsize:"75%"}); 81 this.Append(err); svg = err.toSVG(); this.data.pop(); 82 } else { 83 var mu = this.SVGgetMu(svg); 84 svg.Add(BBOX.MGLYPH(this.img.img,values.width,values.height,values.valign,mu, 85 {src:values.src, alt:values.alt, title:values.alt})); 86 } 87 } 88 svg.Clean(); 89 this.SVGhandleColor(svg); 90 this.SVGsaveData(svg); 91 return svg; 92 }, 93 SVGimgLoaded: function (event,status) { 94 if (typeof(event) === "string") {status = event} 95 this.img.status = (status || "OK") 96 }, 97 SVGimgError: function () {this.img.img.onload("error")} 98 },{ 99 GLYPH: {} // global list of all loaded glyphs 100 }); 101 102 MathJax.Hub.Startup.signal.Post("SVG mglyph Ready"); 103 MathJax.Ajax.loadComplete(SVG.autoloadDir+"/mglyph.js"); 104 105 }); 106