imageFonts.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/HTML-CSS/imageFonts.js
  6. *
  7. * Implements the image fallback fonts for the HTML-CSS OutputJax.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2009-2013 The MathJax Consortium
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. (function (HUB,HTMLCSS,AJAX) {
  26. var VERSION = "2.2";
  27. HUB.Register.LoadHook(HTMLCSS.fontDir + "/fontdata.js",function () {
  28. HTMLCSS.Augment({
  29. allowWebFonts: false,
  30. imgDir: HTMLCSS.webfontDir+"/png",
  31. imgPacked: (MathJax.isPacked ? "" : "/unpacked"),
  32. imgSize: ['050','060','071','085',100,120,141,168,200,238,283,336,400,476],
  33. imgBaseIndex: 4, // set by initImg()
  34. imgSizeForEm: {}, // cache of indexes by em-size
  35. imgSizeForScale: {}, // cache of indexes by scale for a given em-size
  36. imgZoom: 1, // set by initImg for each equation
  37. handleImg: function (span,font,c,n,text) {
  38. if (text.length) {this.addText(span,text)}
  39. var orig = c[5].orig; if (!orig) {orig = c[5].orig = [c[0],c[1],c[2],c[3],c[4]]}
  40. var bscale = this.imgZoom; if (!span.scale) {span.scale = 1}
  41. var index = this.imgIndex(span.scale*bscale);
  42. if (index == this.imgEmWidth.length-1 &&
  43. this.em*span.scale*bscale/this.imgEmWidth[index] > 1.1)
  44. {bscale = this.imgEmWidth[index]/(this.em*span.scale)}
  45. var factor = this.imgEmWidth[index]/(this.em*(span.scale||1)*bscale);
  46. c[0] = orig[0]*factor; c[1] = orig[1]*factor; c[2] = orig[2]*factor;
  47. c[3] = orig[3]*factor; c[4] = orig[4]*factor;
  48. var dir = this.imgDir+"/"+font.directory+"/"+this.imgSize[index];
  49. var chr = n.toString(16).toUpperCase(); while (chr.length < 4) {chr = "0"+chr};
  50. var file = dir+"/"+chr+".png";
  51. var img = c[5].img[index];
  52. var style = {width:Math.floor(img[0]/bscale+.5)+"px", height:Math.floor(img[1]/bscale+.5)+"px"};
  53. if (img[2]) {style.verticalAlign = Math.floor(-img[2]/bscale+.5)+"px"}
  54. if (c[3] < 0) {style.marginLeft = this.Em(c[3]/1000)}
  55. if (c[4] != c[2]) {style.marginRight = this.Em((c[2]-c[4])/1000)}
  56. if (this.msieIE6) {
  57. style.filter = "progid:DXImageTransform.Microsoft." +
  58. "AlphaImageLoader(src='"+AJAX.fileURL(file)+"', sizingMethod='scale')";
  59. file = this.directory+"/blank.gif"
  60. }
  61. this.addElement(span,"img",{src:AJAX.fileURL(file), style:style, isMathJax:true});
  62. return "";
  63. },
  64. defineImageData: function (def) {
  65. for (var font in def) {if (def.hasOwnProperty(font)) {
  66. var FONT = HTMLCSS.FONTDATA.FONTS[font];
  67. if (FONT) {
  68. font = def[font];
  69. for (var n in font) {if (font.hasOwnProperty(n) && FONT[n]) {FONT[n][5] = {img: font[n]}}}
  70. }
  71. }}
  72. },
  73. initImg: function (span) {
  74. if (this.imgSizeForEm[this.em]) {this.imgBaseIndex = this.imgSizeForEm[this.em]}
  75. for (var i = 0, m = this.imgEmWidth.length-1; i < m; i++)
  76. {if (this.em <= this.imgEmWidth[i]) break}
  77. if (i && this.imgEmWidth[i] - this.em > this.em - this.imgEmWidth[i-1]) {i--}
  78. this.imgSizeForEm[this.em] = this.imgBaseIndex = i;
  79. this.imgZoom = this.imgBrowserZoom();
  80. },
  81. imgIndex: function (scale) {
  82. if (!scale) {return this.imgBaseIndex}
  83. if (!this.imgSizeForScale[this.em]) {this.imgSizeForScale[this.em] = {}}
  84. if (this.imgSizeForScale[this.em][scale]) {return this.imgSizeForScale[this.em][scale]}
  85. var em = this.em * scale;
  86. for (var i = 0, m = this.imgEmWidth.length-1; i < m; i++)
  87. {if (em <= this.imgEmWidth[i]) break}
  88. if (i && this.imgEmWidth[i] - em > em - this.imgEmWidth[i-1]) {i--}
  89. this.imgSizeForScale[this.em][scale] = i;
  90. return i;
  91. },
  92. imgBrowserZoom: function () {return 1}
  93. });
  94. HUB.Browser.Select({
  95. Firefox: function (browser) {
  96. var ZDIV = HTMLCSS.addElement(document.body,"div",{
  97. style: {
  98. display:"none", visibility:"hidden", overflow:"scroll",
  99. position:"absolute", top:0, left: 0, width:"200px", height:"200px",
  100. padding:0, border:0, margin:0
  101. }
  102. });
  103. var ZFRAME = HTMLCSS.addElement(ZDIV,"div",{
  104. style: {
  105. position:"absolute", left:0, top:0, right:0, bottom:0,
  106. padding:0, border:0, margin:0
  107. }
  108. });
  109. HTMLCSS.Augment({
  110. imgSpaceBug: true,
  111. imgSpace: "\u00A0",
  112. imgZoomLevel: (browser.isMac ?
  113. {50:.3, 30:.5, 22:.67, 19:.8, 16:.9, 15:1, 13:1.1, 12:1.2,
  114. 11:1.33, 10:1.5, 9:1.7, 7:2, 6:2.4, 5:3, 0:15} :
  115. {56:.3, 34:.5, 25:.67, 21:.8, 19:.9, 17:1, 15:1.1, 14:1.2,
  116. 13:1.33, 11:1.5, 10:1.7, 8:2, 7:2.4, 6:3, 0:17}
  117. ),
  118. imgZoomDiv: ZDIV,
  119. imgBrowserZoom: function () {
  120. var size = this.imgZoomLevel;
  121. ZDIV.style.display = "";
  122. var ratio = (ZDIV.offsetWidth-ZFRAME.offsetWidth);
  123. ratio = (size[ratio] ? size[ratio] : size[0]/ratio);
  124. ZDIV.style.display = "none";
  125. return ratio;
  126. }
  127. });
  128. },
  129. Safari: function (browser) {
  130. // for iPhone and iTouch
  131. // var webkit = (navigator.appVersion+"AppleWebKit/530").match(/AppleWebKit\/(\d+)/)[1];
  132. HTMLCSS.Augment({
  133. // imgHeightBug: (!browser.isMac || webkit > 525),
  134. // imgDepthBug: (!browser.isMac || webkit > 525),
  135. imgBrowserZoom: function () {return 3}
  136. });
  137. },
  138. Chrome: function (browser) {
  139. HTMLCSS.Augment({
  140. imgHeightBug: true,
  141. imgBrowserZoom: function () {return 3}
  142. });
  143. },
  144. Opera: function (browser) {
  145. HTMLCSS.Augment({
  146. imgSpaceBug: true,
  147. imgSpace: "\u00A0\u00A0",
  148. imgDoc: (document.compatMode == "BackCompat" ? document.body :
  149. document.documentElement),
  150. imgBrowserZoom: function () {
  151. if (browser.isMac) {return 3} // Mac Opera scales very nicely
  152. var H = this.imgDoc.clientHeight, d = Math.floor(15*H/window.innerHeight);
  153. if (this.imgDoc.clientWidth < this.imgDoc.scrollWidth-d) {H += d}
  154. return parseFloat((window.innerHeight/H).toFixed(1));
  155. }
  156. });
  157. }
  158. });
  159. var GETWIDTHS = function () {
  160. var img = HTMLCSS.FONTDATA.FONTS["MathJax_Main"][0x2014][5].img; // em-dash
  161. HTMLCSS.imgEmWidth = [];
  162. for (var i = 0, m = img.length; i < m; i++) {HTMLCSS.imgEmWidth[i] = img[i][0]}
  163. };
  164. var IMGDIR = HTMLCSS.imgDir + HTMLCSS.imgPacked;
  165. MathJax.Callback.Queue(
  166. ["Require",AJAX,IMGDIR+"/imagedata.js"],
  167. GETWIDTHS,
  168. ["loadComplete",AJAX,HTMLCSS.directory+"/imageFonts.js"]
  169. );
  170. });
  171. })(MathJax.Hub,MathJax.OutputJax["HTML-CSS"],MathJax.Ajax);