158 lines
4.2 KiB
JavaScript
158 lines
4.2 KiB
JavaScript
|
dojo.provide("dojox.html.metrics");
|
||
|
|
||
|
(function(){
|
||
|
var dhm = dojox.html.metrics;
|
||
|
|
||
|
// derived from Morris John's emResized measurer
|
||
|
dhm.getFontMeasurements = function(){
|
||
|
// summary
|
||
|
// Returns an object that has pixel equivilents of standard font size values.
|
||
|
var heights = {
|
||
|
'1em':0, '1ex':0, '100%':0, '12pt':0, '16px':0, 'xx-small':0, 'x-small':0,
|
||
|
'small':0, 'medium':0, 'large':0, 'x-large':0, 'xx-large':0
|
||
|
};
|
||
|
|
||
|
if(dojo.isIE){
|
||
|
// we do a font-size fix if and only if one isn't applied already.
|
||
|
// NOTE: If someone set the fontSize on the HTML Element, this will kill it.
|
||
|
dojo.doc.documentElement.style.fontSize="100%";
|
||
|
}
|
||
|
|
||
|
// set up the measuring node.
|
||
|
var div=dojo.doc.createElement("div");
|
||
|
var ds = div.style;
|
||
|
ds.position="absolute";
|
||
|
ds.left="-100px";
|
||
|
ds.top="0";
|
||
|
ds.width="30px";
|
||
|
ds.height="1000em";
|
||
|
ds.border="0";
|
||
|
ds.margin="0";
|
||
|
ds.padding="0";
|
||
|
ds.outline="0";
|
||
|
ds.lineHeight="1";
|
||
|
ds.overflow="hidden";
|
||
|
dojo.body().appendChild(div);
|
||
|
|
||
|
// do the measurements.
|
||
|
for(var p in heights){
|
||
|
ds.fontSize = p;
|
||
|
heights[p] = Math.round(div.offsetHeight * 12/16) * 16/12 / 1000;
|
||
|
}
|
||
|
|
||
|
dojo.body().removeChild(div);
|
||
|
div = null;
|
||
|
return heights; // object
|
||
|
};
|
||
|
|
||
|
var fontMeasurements = null;
|
||
|
|
||
|
dhm.getCachedFontMeasurements = function(recalculate){
|
||
|
if(recalculate || !fontMeasurements){
|
||
|
fontMeasurements = dhm.getFontMeasurements();
|
||
|
}
|
||
|
return fontMeasurements;
|
||
|
};
|
||
|
|
||
|
var measuringNode = null, empty = {};
|
||
|
dhm.getTextBox = function(/* String */ text, /* Object */ style, /* String? */ className){
|
||
|
var m;
|
||
|
if(!measuringNode){
|
||
|
m = measuringNode = dojo.doc.createElement("div");
|
||
|
m.style.position = "absolute";
|
||
|
m.style.left = "-10000px";
|
||
|
m.style.top = "0";
|
||
|
dojo.body().appendChild(m);
|
||
|
}else{
|
||
|
m = measuringNode;
|
||
|
}
|
||
|
// reset styles
|
||
|
m.className = "";
|
||
|
m.style.border = "0";
|
||
|
m.style.margin = "0";
|
||
|
m.style.padding = "0";
|
||
|
m.style.outline = "0";
|
||
|
// set new style
|
||
|
if(arguments.length > 1 && style){
|
||
|
for(var i in style){
|
||
|
if(i in empty){ continue; }
|
||
|
m.style[i] = style[i];
|
||
|
}
|
||
|
}
|
||
|
// set classes
|
||
|
if(arguments.length > 2 && className){
|
||
|
m.className = className;
|
||
|
}
|
||
|
// take a measure
|
||
|
m.innerHTML = text;
|
||
|
return dojo.marginBox(m);
|
||
|
};
|
||
|
|
||
|
// determine the scrollbar sizes on load.
|
||
|
var scroll={ w:16, h:16 };
|
||
|
dhm.getScrollbar=function(){ return { w:scroll.w, h:scroll.h }; };
|
||
|
|
||
|
dhm._fontResizeNode = null;
|
||
|
|
||
|
dhm.initOnFontResize = function(interval){
|
||
|
var f = dhm._fontResizeNode = dojo.doc.createElement("iframe");
|
||
|
var fs = f.style;
|
||
|
fs.position = "absolute";
|
||
|
fs.width = "5em";
|
||
|
fs.height = "10em";
|
||
|
fs.top = "-10000px";
|
||
|
if(dojo.isIE){
|
||
|
f.onreadystatechange = function(){
|
||
|
if(f.contentWindow.document.readyState == "complete"){
|
||
|
f.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize;
|
||
|
}
|
||
|
};
|
||
|
}else{
|
||
|
f.onload = function(){
|
||
|
f.contentWindow.onresize = f.contentWindow.parent[dojox._scopeName].html.metrics._fontresize;
|
||
|
};
|
||
|
}
|
||
|
//The script tag is to work around a known firebug race condition. See comments in bug #9046
|
||
|
f.setAttribute("src", "javascript:'<html><head><script>if(\"loadFirebugConsole\" in window){window.loadFirebugConsole();}</script></head><body></body></html>'");
|
||
|
dojo.body().appendChild(f);
|
||
|
dhm.initOnFontResize = function(){};
|
||
|
};
|
||
|
|
||
|
dhm.onFontResize = function(){};
|
||
|
dhm._fontresize = function(){
|
||
|
dhm.onFontResize();
|
||
|
}
|
||
|
|
||
|
dojo.addOnUnload(function(){
|
||
|
// destroy our font resize iframe if we have one
|
||
|
var f = dhm._fontResizeNode;
|
||
|
if(f){
|
||
|
if(dojo.isIE && f.onresize){
|
||
|
f.onresize = null;
|
||
|
}else if(f.contentWindow && f.contentWindow.onresize){
|
||
|
f.contentWindow.onresize = null;
|
||
|
}
|
||
|
dhm._fontResizeNode = null;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
dojo.addOnLoad(function(){
|
||
|
// getScrollbar metrics node
|
||
|
try{
|
||
|
var n=dojo.doc.createElement("div");
|
||
|
n.style.cssText = "top:0;left:0;width:100px;height:100px;overflow:scroll;position:absolute;visibility:hidden;";
|
||
|
dojo.body().appendChild(n);
|
||
|
scroll.w = n.offsetWidth - n.clientWidth;
|
||
|
scroll.h = n.offsetHeight - n.clientHeight;
|
||
|
dojo.body().removeChild(n);
|
||
|
//console.log("Scroll bar dimensions: ", scroll);
|
||
|
delete n;
|
||
|
}catch(e){}
|
||
|
|
||
|
// text size poll setup
|
||
|
if("fontSizeWatch" in dojo.config && !!dojo.config.fontSizeWatch){
|
||
|
dhm.initOnFontResize();
|
||
|
}
|
||
|
});
|
||
|
})();
|