1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
//>>built
define("dojox/html/metrics", ["dojo/_base/kernel","dojo/_base/lang", "dojo/_base/sniff", "dojo/ready", "dojo/_base/unload",
"dojo/_base/window", "dojo/dom-geometry"],
function(kernel,lang,has,ready,UnloadUtil,Window,DOMGeom){
var dhm = lang.getObject("dojox.html.metrics",true);
var dojox = lang.getObject("dojox");
// 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(has("ie")){
// 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.
Window.doc.documentElement.style.fontSize="100%";
}
// set up the measuring node.
var div=Window.doc.createElement("div");
var ds = div.style;
ds.position="absolute";
ds.left="-100px";
ds.top="0";
ds.width="30px";
ds.height="1000em";
ds.borderWidth="0";
ds.margin="0";
ds.padding="0";
ds.outline="0";
ds.lineHeight="1";
ds.overflow="hidden";
Window.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;
}
Window.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, s;
if(!measuringNode){
m = measuringNode = Window.doc.createElement("div");
// Container that we can set contraints on so that it doesn't
// trigger a scrollbar.
var c = Window.doc.createElement("div");
c.appendChild(m);
s = c.style;
s.overflow='scroll';
s.position = "absolute";
s.left = "0px";
s.top = "-10000px";
s.width = "1px";
s.height = "1px";
s.visibility = "hidden";
s.borderWidth = "0";
s.margin = "0";
s.padding = "0";
s.outline = "0";
Window.body().appendChild(c);
}else{
m = measuringNode;
}
// reset styles
m.className = "";
s = m.style;
s.borderWidth = "0";
s.margin = "0";
s.padding = "0";
s.outline = "0";
// set new style
if(arguments.length > 1 && style){
for(var i in style){
if(i in empty){ continue; }
s[i] = style[i];
}
}
// set classes
if(arguments.length > 2 && className){
m.className = className;
}
// take a measure
m.innerHTML = text;
var box = DOMGeom.position(m);
// position doesn't report right (reports 1, since parent is 1)
// So we have to look at the scrollWidth to get the real width
// Height is right.
box.w = m.parentNode.scrollWidth;
return box;
};
// 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 = Window.doc.createElement("iframe");
var fs = f.style;
fs.position = "absolute";
fs.width = "5em";
fs.height = "10em";
fs.top = "-10000px";
if(has("ie")){
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>'");
Window.body().appendChild(f);
dhm.initOnFontResize = function(){};
};
dhm.onFontResize = function(){};
dhm._fontresize = function(){
dhm.onFontResize();
}
UnloadUtil.addOnUnload(function(){
// destroy our font resize iframe if we have one
var f = dhm._fontResizeNode;
if(f){
if(has("ie") && f.onresize){
f.onresize = null;
}else if(f.contentWindow && f.contentWindow.onresize){
f.contentWindow.onresize = null;
}
dhm._fontResizeNode = null;
}
});
ready(function(){
// getScrollbar metrics node
try{
var n=Window.doc.createElement("div");
n.style.cssText = "top:0;left:0;width:100px;height:100px;overflow:scroll;position:absolute;visibility:hidden;";
Window.body().appendChild(n);
scroll.w = n.offsetWidth - n.clientWidth;
scroll.h = n.offsetHeight - n.clientHeight;
Window.body().removeChild(n);
//console.log("Scroll bar dimensions: ", scroll);
delete n;
}catch(e){}
// text size poll setup
if("fontSizeWatch" in kernel.config && !!kernel.config.fontSizeWatch){
dhm.initOnFontResize();
}
});
return dhm;
});
|