www

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

unicode.js (6436B)


      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/extensions/TeX/unicode.js
      7  *  
      8  *  Implements the \unicode extension to TeX to allow arbitrary unicode
      9  *  code points to be entered into the TeX file.  You can specify
     10  *  the height and depth of the character (the width is determined by
     11  *  the browser), and the default font from which to take the character.
     12  *  
     13  *  Examples:
     14  *      \unicode{65}                        % the character 'A'
     15  *      \unicode{x41}                       % the character 'A'
     16  *      \unicode[.55,0.05]{x22D6}           % less-than with dot, with height .55 and depth 0.05
     17  *      \unicode[.55,0.05][Geramond]{x22D6} % same taken from Geramond font
     18  *      \unicode[Garamond]{x22D6}           % same, but with default height, depth of .8,.2
     19  *
     20  *  Once a size and font are provided for a given code point, they need
     21  *  not be specified again in subsequent \unicode calls for that character.
     22  *  Note that a font list can be given, but Internet Explorer has a buggy
     23  *  implementation of font-family where it only looks in the first
     24  *  available font and if the glyph is not in that, it does not look at
     25  *  later fonts, but goes directly to the default font as set in the
     26  *  Internet-Options/Font panel.  For this reason, the default font list is
     27  *  "STIXGeneral,'Arial Unicode MS'", so if the user has STIX fonts, the
     28  *  symbol will be taken from that (almost all the symbols are in
     29  *  STIXGeneral), otherwise Arial Unicode MS is tried.
     30  *  
     31  *  To configure the default font list, use
     32  *  
     33  *      MathJax.Hub.Config({
     34  *        TeX: {
     35  *          unicode: {
     36  *            fonts: "STIXGeneral,'Arial Unicode MS'"
     37  *          }
     38  *        }
     39  *      });
     40  *
     41  *  The result of \unicode will have TeX class ORD (i.e., it will act like a
     42  *  variable).  Use \mathbin, \mathrel, etc, to specify a different class.
     43  *  
     44  *  ---------------------------------------------------------------------
     45  *  
     46  *  Copyright (c) 2009-2015 The MathJax Consortium
     47  * 
     48  *  Licensed under the Apache License, Version 2.0 (the "License");
     49  *  you may not use this file except in compliance with the License.
     50  *  You may obtain a copy of the License at
     51  * 
     52  *      http://www.apache.org/licenses/LICENSE-2.0
     53  * 
     54  *  Unless required by applicable law or agreed to in writing, software
     55  *  distributed under the License is distributed on an "AS IS" BASIS,
     56  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     57  *  See the License for the specific language governing permissions and
     58  *  limitations under the License.
     59  */
     60 
     61 //
     62 //  The configuration defaults, augmented by the user settings
     63 //  
     64 MathJax.Extension["TeX/unicode"] = {
     65   version: "2.6.0",
     66   unicode: {},
     67   config: MathJax.Hub.CombineConfig("TeX.unicode",{
     68     fonts: "STIXGeneral,'Arial Unicode MS'"
     69   })
     70 };
     71   
     72 MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
     73   var TEX = MathJax.InputJax.TeX;
     74   var MML = MathJax.ElementJax.mml;
     75   var UNICODE = MathJax.Extension["TeX/unicode"].unicode;
     76   
     77   //
     78   //  Add \unicode macro
     79   //
     80   TEX.Definitions.Add({macros: {unicode: 'Unicode'}},null,true);
     81   //
     82   //  Implementation of \unicode in parser
     83   //
     84   TEX.Parse.Augment({
     85     Unicode: function(name) {
     86       var HD = this.GetBrackets(name), font;
     87       if (HD) {
     88         if (HD.replace(/ /g,"").match(/^(\d+(\.\d*)?|\.\d+),(\d+(\.\d*)?|\.\d+)$/))
     89           {HD = HD.replace(/ /g,"").split(/,/); font = this.GetBrackets(name)}
     90             else {font = HD; HD = null}
     91       }
     92       var n = this.trimSpaces(this.GetArgument(name)),
     93           N = parseInt(n.match(/^x/) ? "0"+n : n);
     94       if (!UNICODE[N]) {UNICODE[N] = [800,200,font,N]}
     95       else if (!font) {font = UNICODE[N][2]}
     96       if (HD) {
     97         UNICODE[N][0] = Math.floor(HD[0]*1000);
     98         UNICODE[N][1] = Math.floor(HD[1]*1000);
     99       }
    100       var variant = this.stack.env.font, def = {};
    101       if (font) {
    102         UNICODE[N][2] = def.fontfamily = font.replace(/"/g,"'");
    103         if (variant) {
    104           if (variant.match(/bold/))   {def.fontweight = "bold"}
    105           if (variant.match(/italic|-mathit/)) {def.fontstyle = "italic"}
    106         }
    107       } else if (variant) {def.mathvariant = variant}
    108       def.unicode = [].concat(UNICODE[N]); // make a copy
    109       this.Push(MML.mtext(MML.entity("#"+n)).With(def));
    110     }
    111   });
    112 
    113   MathJax.Hub.Startup.signal.Post("TeX unicode Ready");
    114   
    115 });
    116     
    117 MathJax.Hub.Register.StartupHook("HTML-CSS Jax Ready",function () {
    118   var MML = MathJax.ElementJax.mml;
    119   var FONTS = MathJax.Extension["TeX/unicode"].config.fonts;
    120 
    121   //
    122   //  Override getVariant to make one that includes the font and size
    123   //
    124   var GETVARIANT = MML.mbase.prototype.HTMLgetVariant;
    125   MML.mbase.Augment({
    126     HTMLgetVariant: function () {
    127       var variant = GETVARIANT.apply(this,arguments);
    128       if (variant.unicode) {delete variant.unicode; delete variant.FONTS} // clear font cache in case of restart
    129       if (!this.unicode) {return variant}
    130       variant.unicode = true;
    131       if (!variant.defaultFont) {
    132         variant = MathJax.Hub.Insert({},variant); // make a copy
    133         variant.defaultFont = {family:FONTS};
    134       }
    135       var family = this.unicode[2]; if (family) {family += ","+FONTS} else {family = FONTS}
    136       variant.defaultFont[this.unicode[3]] = [
    137         this.unicode[0],this.unicode[1],500,0,500,
    138         {isUnknown:true, isUnicode:true, font:family}
    139       ];
    140       return variant;
    141     }
    142   });
    143 });
    144 
    145 MathJax.Hub.Register.StartupHook("SVG Jax Ready",function () {
    146   var MML = MathJax.ElementJax.mml;
    147   var FONTS = MathJax.Extension["TeX/unicode"].config.fonts;
    148 
    149   //
    150   //  Override getVariant to make one that includes the font and size
    151   //
    152   var GETVARIANT = MML.mbase.prototype.SVGgetVariant;
    153   MML.mbase.Augment({
    154     SVGgetVariant: function () {
    155       var variant = GETVARIANT.call(this);
    156       if (variant.unicode) {delete variant.unicode; delete variant.FONTS} // clear font cache in case of restart
    157       if (!this.unicode) {return variant}
    158       variant.unicode = true;
    159       if (!variant.forceFamily) {variant = MathJax.Hub.Insert({},variant)} // make a copy
    160       variant.defaultFamily = FONTS; variant.noRemap = true;
    161       variant.h = this.unicode[0]; variant.d = this.unicode[1];
    162       return variant;
    163     }
    164   });
    165 });
    166 
    167 MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/unicode.js");