You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.8 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-TextMetrics'>/**
  19. </span> * @class Ext.util.TextMetrics
  20. * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
  21. * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
  22. * should not contain any HTML, otherwise it may not be measured correctly.
  23. * @singleton
  24. */
  25. Ext.util.TextMetrics = function(){
  26. var shared;
  27. return {
  28. <span id='Ext-util-TextMetrics-method-measure'> /**
  29. </span> * Measures the size of the specified text
  30. * @param {String/HTMLElement} el The element, dom node or id from which to copy existing CSS styles
  31. * that can affect the size of the rendered text
  32. * @param {String} text The text to measure
  33. * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
  34. * in order to accurately measure the text height
  35. * @return {Object} An object containing the text's size {width: (width), height: (height)}
  36. */
  37. measure : function(el, text, fixedWidth){
  38. if(!shared){
  39. shared = Ext.util.TextMetrics.Instance(el, fixedWidth);
  40. }
  41. shared.bind(el);
  42. shared.setFixedWidth(fixedWidth || 'auto');
  43. return shared.getSize(text);
  44. },
  45. <span id='Ext-util-TextMetrics-method-createInstance'> /**
  46. </span> * Return a unique TextMetrics instance that can be bound directly to an element and reused. This reduces
  47. * the overhead of multiple calls to initialize the style properties on each measurement.
  48. * @param {String/HTMLElement} el The element, dom node or id that the instance will be bound to
  49. * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
  50. * in order to accurately measure the text height
  51. * @return {Ext.util.TextMetrics.Instance} instance The new instance
  52. */
  53. createInstance : function(el, fixedWidth){
  54. return Ext.util.TextMetrics.Instance(el, fixedWidth);
  55. }
  56. };
  57. }();
  58. Ext.util.TextMetrics.Instance = function(bindTo, fixedWidth){
  59. var ml = new Ext.Element(document.createElement('div'));
  60. document.body.appendChild(ml.dom);
  61. ml.position('absolute');
  62. ml.setLeftTop(-1000, -1000);
  63. ml.hide();
  64. if(fixedWidth){
  65. ml.setWidth(fixedWidth);
  66. }
  67. var instance = {
  68. <span id='Ext-util-TextMetrics-method-getSize'> /**
  69. </span> * &lt;p&gt;&lt;b&gt;Only available on the instance returned from {@link #createInstance}, &lt;u&gt;not&lt;/u&gt; on the singleton.&lt;/b&gt;&lt;/p&gt;
  70. * Returns the size of the specified text based on the internal element's style and width properties
  71. * @param {String} text The text to measure
  72. * @return {Object} An object containing the text's size {width: (width), height: (height)}
  73. */
  74. getSize : function(text){
  75. ml.update(text);
  76. var s = ml.getSize();
  77. ml.update('');
  78. return s;
  79. },
  80. <span id='Ext-util-TextMetrics-method-bind'> /**
  81. </span> * &lt;p&gt;&lt;b&gt;Only available on the instance returned from {@link #createInstance}, &lt;u&gt;not&lt;/u&gt; on the singleton.&lt;/b&gt;&lt;/p&gt;
  82. * Binds this TextMetrics instance to an element from which to copy existing CSS styles
  83. * that can affect the size of the rendered text
  84. * @param {String/HTMLElement} el The element, dom node or id
  85. */
  86. bind : function(el){
  87. ml.setStyle(
  88. Ext.fly(el).getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
  89. );
  90. },
  91. <span id='Ext-util-TextMetrics-method-setFixedWidth'> /**
  92. </span> * &lt;p&gt;&lt;b&gt;Only available on the instance returned from {@link #createInstance}, &lt;u&gt;not&lt;/u&gt; on the singleton.&lt;/b&gt;&lt;/p&gt;
  93. * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
  94. * to set a fixed width in order to accurately measure the text height.
  95. * @param {Number} width The width to set on the element
  96. */
  97. setFixedWidth : function(width){
  98. ml.setWidth(width);
  99. },
  100. <span id='Ext-util-TextMetrics-method-getWidth'> /**
  101. </span> * &lt;p&gt;&lt;b&gt;Only available on the instance returned from {@link #createInstance}, &lt;u&gt;not&lt;/u&gt; on the singleton.&lt;/b&gt;&lt;/p&gt;
  102. * Returns the measured width of the specified text
  103. * @param {String} text The text to measure
  104. * @return {Number} width The width in pixels
  105. */
  106. getWidth : function(text){
  107. ml.dom.style.width = 'auto';
  108. return this.getSize(text).width;
  109. },
  110. <span id='Ext-util-TextMetrics-method-getHeight'> /**
  111. </span> * &lt;p&gt;&lt;b&gt;Only available on the instance returned from {@link #createInstance}, &lt;u&gt;not&lt;/u&gt; on the singleton.&lt;/b&gt;&lt;/p&gt;
  112. * Returns the measured height of the specified text. For multiline text, be sure to call
  113. * {@link #setFixedWidth} if necessary.
  114. * @param {String} text The text to measure
  115. * @return {Number} height The height in pixels
  116. */
  117. getHeight : function(text){
  118. return this.getSize(text).height;
  119. }
  120. };
  121. instance.bind(bindTo);
  122. return instance;
  123. };
  124. Ext.Element.addMethods({
  125. <span id='Ext-Element-method-getTextWidth'> /**
  126. </span> * Returns the width in pixels of the passed text, or the width of the text in this Element.
  127. * @param {String} text The text to measure. Defaults to the innerHTML of the element.
  128. * @param {Number} min (Optional) The minumum value to return.
  129. * @param {Number} max (Optional) The maximum value to return.
  130. * @return {Number} The text width in pixels.
  131. * @member Ext.Element getTextWidth
  132. */
  133. getTextWidth : function(text, min, max){
  134. return (Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width).constrain(min || 0, max || 1000000);
  135. }
  136. });
  137. </pre>
  138. </body>
  139. </html>