metrics.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.html.metrics"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.html.metrics"] = true;
  8. dojo.provide("dojox.html.metrics");
  9. (function(){
  10. var dhm = dojox.html.metrics;
  11. // derived from Morris John's emResized measurer
  12. dhm.getFontMeasurements = function(){
  13. // summary
  14. // Returns an object that has pixel equivilents of standard font size values.
  15. var heights = {
  16. '1em':0, '1ex':0, '100%':0, '12pt':0, '16px':0, 'xx-small':0, 'x-small':0,
  17. 'small':0, 'medium':0, 'large':0, 'x-large':0, 'xx-large':0
  18. };
  19. if(dojo.isIE){
  20. // we do a font-size fix if and only if one isn't applied already.
  21. // NOTE: If someone set the fontSize on the HTML Element, this will kill it.
  22. dojo.doc.documentElement.style.fontSize="100%";
  23. }
  24. // set up the measuring node.
  25. var div=dojo.doc.createElement("div");
  26. var ds = div.style;
  27. ds.position="absolute";
  28. ds.left="-100px";
  29. ds.top="0";
  30. ds.width="30px";
  31. ds.height="1000em";
  32. ds.borderWidth="0";
  33. ds.margin="0";
  34. ds.padding="0";
  35. ds.outline="0";
  36. ds.lineHeight="1";
  37. ds.overflow="hidden";
  38. dojo.body().appendChild(div);
  39. // do the measurements.
  40. for(var p in heights){
  41. ds.fontSize = p;
  42. heights[p] = Math.round(div.offsetHeight * 12/16) * 16/12 / 1000;
  43. }
  44. dojo.body().removeChild(div);
  45. div = null;
  46. return heights; // object
  47. };
  48. var fontMeasurements = null;
  49. dhm.getCachedFontMeasurements = function(recalculate){
  50. if(recalculate || !fontMeasurements){
  51. fontMeasurements = dhm.getFontMeasurements();
  52. }
  53. return fontMeasurements;
  54. };
  55. var measuringNode = null, empty = {};
  56. dhm.getTextBox = function(/* String */ text, /* Object */ style, /* String? */ className){
  57. var m, s;
  58. if(!measuringNode){
  59. m = measuringNode = dojo.doc.createElement("div");
  60. // Container that we can set contraints on so that it doesn't
  61. // trigger a scrollbar.
  62. var c = dojo.doc.createElement("div");
  63. c.appendChild(m);
  64. s = c.style;
  65. s.overflow='scroll';
  66. s.position = "absolute";
  67. s.left = "0px";
  68. s.top = "-10000px";
  69. s.width = "1px";
  70. s.height = "1px";
  71. s.visibility = "hidden";
  72. s.borderWidth = "0";
  73. s.margin = "0";
  74. s.padding = "0";
  75. s.outline = "0";
  76. dojo.body().appendChild(c);
  77. }else{
  78. m = measuringNode;
  79. }
  80. // reset styles
  81. m.className = "";
  82. s = m.style;
  83. s.borderWidth = "0";
  84. s.margin = "0";
  85. s.padding = "0";
  86. s.outline = "0";
  87. // set new style
  88. if(arguments.length > 1 && style){
  89. for(var i in style){
  90. if(i in empty){ continue; }
  91. s[i] = style[i];
  92. }
  93. }
  94. // set classes
  95. if(arguments.length > 2 && className){
  96. m.className = className;
  97. }
  98. // take a measure
  99. m.innerHTML = text;
  100. var box = dojo.position(m);
  101. // position doesn't report right (reports 1, since parent is 1)
  102. // So we have to look at the scrollWidth to get the real width
  103. // Height is right.
  104. box.w = m.parentNode.scrollWidth;
  105. return box;
  106. };
  107. // determine the scrollbar sizes on load.
  108. var scroll={ w:16, h:16 };
  109. dhm.getScrollbar=function(){ return { w:scroll.w, h:scroll.h }; };
  110. dhm._fontResizeNode = null;
  111. dhm.initOnFontResize = function(interval){
  112. var f = dhm._fontResizeNode = dojo.doc.createElement("iframe");
  113. var fs = f.style;
  114. fs.position = "absolute";
  115. fs.width = "5em";
  116. fs.height = "10em";
  117. fs.top = "-10000px";
  118. if(dojo.isIE){
  119. f.onreadystatechange = function(){
  120. if(f.contentWindow.document.readyState == "complete"){
  121. f.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize;
  122. }
  123. };
  124. }else{
  125. f.onload = function(){
  126. f.contentWindow.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize;
  127. };
  128. }
  129. //The script tag is to work around a known firebug race condition. See comments in bug #9046
  130. f.setAttribute("src", "javascript:'<html><head><script>if(\"loadFirebugConsole\" in window){window.loadFirebugConsole();}</script></head><body></body></html>'");
  131. dojo.body().appendChild(f);
  132. dhm.initOnFontResize = function(){};
  133. };
  134. dhm.onFontResize = function(){};
  135. dhm._fontresize = function(){
  136. dhm.onFontResize();
  137. }
  138. dojo.addOnUnload(function(){
  139. // destroy our font resize iframe if we have one
  140. var f = dhm._fontResizeNode;
  141. if(f){
  142. if(dojo.isIE && f.onresize){
  143. f.onresize = null;
  144. }else if(f.contentWindow && f.contentWindow.onresize){
  145. f.contentWindow.onresize = null;
  146. }
  147. dhm._fontResizeNode = null;
  148. }
  149. });
  150. dojo.addOnLoad(function(){
  151. // getScrollbar metrics node
  152. try{
  153. var n=dojo.doc.createElement("div");
  154. n.style.cssText = "top:0;left:0;width:100px;height:100px;overflow:scroll;position:absolute;visibility:hidden;";
  155. dojo.body().appendChild(n);
  156. scroll.w = n.offsetWidth - n.clientWidth;
  157. scroll.h = n.offsetHeight - n.clientHeight;
  158. dojo.body().removeChild(n);
  159. //console.log("Scroll bar dimensions: ", scroll);
  160. delete n;
  161. }catch(e){}
  162. // text size poll setup
  163. if("fontSizeWatch" in dojo.config && !!dojo.config.fontSizeWatch){
  164. dhm.initOnFontResize();
  165. }
  166. });
  167. })();
  168. }