metrics.js 5.3 KB

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