1/** 2 * @preserve textfill 3 * @name jquery.textfill.js 4 * @author Russ Painter (GeekyMonkey) 5 * @author Yu-Jie Lin 6 * @author Alexandre Dantas 7 * @version 0.6.2 8 * @date 2018-02-24 9 * @copyright (c) 2009 10 * @license MIT License 11 * @homepage https://github.com/jquery-textfill/jquery-textfill 12 * @example http://jquery-textfill.github.io/jquery-textfill/index.html 13 */ 14; (function($) { 15 16 /** 17 * Resizes an inner element's font so that the 18 * inner element completely fills the outer element. 19 * 20 * @param {Object} options User options that take 21 * higher precedence when 22 * merging with the default ones. 23 * 24 * @return All outer elements processed 25 */ 26 $.fn.textfill = function(options) { 27 28 // ______ _______ _______ _______ _ _ _______ _______ 29 // | \ |______ |______ |_____| | | | | |______ 30 // |_____/ |______ | | | |_____| |_____ | ______| 31 // 32 // Merging user options with the default values 33 34 var defaults = { 35 debug : false, 36 maxFontPixels : 40, 37 minFontPixels : 4, 38 innerTag : 'span', 39 widthOnly : false, 40 success : null, // callback when a resizing is done 41 fail : null, // callback when a resizing is failed 42 complete : null, // callback when all is done 43 explicitWidth : null, 44 explicitHeight : null, 45 changeLineHeight : false, 46 truncateOnFail : false, 47 allowOverflow : false // If true, text will stay at minFontPixels but overflow container w/out failing 48 }; 49 50 var Opts = $.extend(defaults, options); 51 52 // _______ _ _ __ _ _______ _______ _____ _____ __ _ _______ 53 // |______ | | | \ | | | | | | | \ | |______ 54 // | |_____| | \_| |_____ | __|__ |_____| | \_| ______| 55 // 56 // Predefining the awesomeness 57 58 // Output arguments to the Debug console 59 // if "Debug Mode" is enabled 60 function _debug() { 61 if (!Opts.debug 62 || typeof console == 'undefined' 63 || typeof console.debug == 'undefined') { 64 return; 65 } 66 console.debug.apply(console, arguments); 67 } 68 69 // Output arguments to the Warning console 70 function _warn() { 71 if (typeof console == 'undefined' || 72 typeof console.warn == 'undefined') { 73 return; 74 } 75 console.warn.apply(console, arguments); 76 } 77 78 // Outputs all information on the current sizing 79 // of the font. 80 function _debug_sizing(prefix, ourText, maxHeight, maxWidth, minFontPixels, maxFontPixels) { 81 82 function _m(v1, v2) { 83 84 var marker = ' / '; 85 86 if (v1 > v2) { 87 marker = ' > '; 88 } 89 else if (v1 == v2) { 90 marker = ' = '; 91 } 92 return marker; 93 } 94 95 _debug( 96 '[TextFill] ' + prefix + ' { ' + 97 'font-size: ' + ourText.css('font-size') + ',' +
98 'Height: ' + ourText.height() + 'px ' + _m(ourText.height(), maxHeight) + maxHeight + 'px,' + 99 'Width: ' + ourText.width() + _m(ourText.width() , maxWidth) + maxWidth + ',' + 100 'minFontPixels: ' + minFontPixels + 'px, ' + 101 'maxFontPixels: ' + maxFontPixels + 'px }' 102 ); 103 } 104 105 /** 106 * Calculates which size the font can get resized, 107 * according to constrains. 108 * 109 * @param {String} prefix Gets shown on the console before 110 * all the arguments, if debug mode is on. 111 * @param {Object} ourText The DOM element to resize, 112 * that contains the text. 113 * @param {function} func Function called on `ourText` that's 114 * used to compare with `max`. 115 * @param {number} max Maximum value, that gets compared with 116 * `func` called on `ourText`. 117 * @param {number} minFontPixels Minimum value the font can 118 * get resized to (in pixels). 119 * @param {number} maxFontPixels Maximum value the font can 120 * get resized to (in pixels). 121 * 122 * @return Size (in pixels) that the font can be resized. 123 */ 124 function _sizing(prefix, ourText, func, max, maxHeight, maxWidth, minFontPixels, maxFontPixels) { 125 126 _debug_sizing( 127 prefix, ourText, 128 maxHeight, maxWidth, 129 minFontPixels, maxFontPixels 130 ); 131 132 // The kernel of the whole plugin, take most attention 133 // on this part. 134 // 135 // This is a loop that keeps increasing the `font-size` 136 // until it fits the parent element. 137 // 138 // - Start from the minimal allowed value (`minFontPixels`) 139 // - Guesses an average font size (in pixels) for the font, 140 // - Resizes the text and sees if its size is within the 141 // boundaries (`minFontPixels` and `maxFontPixels`). 142 // - If so, keep guessing until we break. 143 // - If not, return the last calculated size. 144 // 145 // I understand this is not optimized and we should 146 // consider implementing something akin to 147 // Daniel Hoffmann's answer here: 148 // 149 // http://stackoverflow.com/a/17433451/1094964 150 // 151 152 while (minFontPixels < (Math.floor(maxFontPixels) - 1)) { 153 154 var fontSize = Math.floor((minFontPixels + maxFontPixels) / 2); 155 ourText.css('font-size', fontSize); 156 var curSize = func.call(ourText); 157 158 if (curSize <= max) { 159 minFontPixels = fontSize; 160 161 if (curSize == max) { 162 break; 163 } 164 } 165 else { 166 maxFontPixels = fontSize; 167 } 168 169 _debug_sizing( 170 prefix, ourText, 171 maxHeight, maxWidth, 172 minFontPixels, maxFontPixels 173 ); 174 } 175 176 ourText.css('font-size', maxFontPixels); 177 178 if (func.call(ourText) <= max) { 179 minFontPixels = maxFontPixels; 180 181 _debug_sizing( 182 prefix + '* ', ourText, 183 maxHeight, maxWidth, 184 minFontPixels, maxFontPixels 185 ); 186 } 187 return minFontPixels; 188 } 189 190 // _______ _______ _______ ______ _______ 191 // |______ | |_____| |_____/ | 192 // ______| | | | | \_ | 193 // 194 // Let's get it started (yeah)! 195 196 _debug('[TextFill] Start Debug'); 197 198 this.each(function() { 199 200 // Contains the child element we will resize. 201 // $(this) means the parent container 202 var ourText = $(Opts.innerTag + ':first', this); 203 204 _debug('[TextFill] Inner text: ' + ourText.text()); 205 _debug('[TextFill] All options: ', Opts); 206 _debug('[TextFill] Maximum sizes: { ' + 207 'Height: ' + maxHeight + 'px, ' + 208 'Width: ' + maxWidth + 'px' + ' }' 209 ); 210 211 if (!ourText.is(':visible')) { 212 // Failure callback 213 if (Opts.fail) 214 Opts.fail(this); 215 216 _debug( 217 '[TextFill] Failure { ' + 218 'Current Width: ' + ourText.width() + ', ' + 219 'Maximum Width: ' + maxWidth + ', ' + 220 'Current Height: ' + ourText.height() + ', ' + 221 'Maximum Height: ' + maxHeight + ' }' 222 ); 223 224 return; 225 } 226 227 // Will resize to this dimensions. 228 // Use explicit dimensions when specified 229 var maxHeight = Opts.explicitHeight || $(this).height(); 230 var maxWidth = Opts.explicitWidth || $(this).width(); 231 232 var oldFontSize = ourText.css('font-size'); 233 234 var lineHeight = parseFloat(ourText.css('line-height')) / parseFloat(oldFontSize); 235 236 var minFontPixels = Opts.minFontPixels; 237 238 // Remember, if this `maxFontPixels` is negative, 239 // the text will resize to as long as the container 240 // can accomodate 241 var maxFontPixels = (Opts.maxFontPixels <= 0 ? 242 maxHeight : 243 Opts.maxFontPixels); 244 245 // Let's start it all! 246 247 // 1. Calculate which `font-size` would 248 // be best for the Height 249 var fontSizeHeight = undefined; 250 251 if (Opts.widthOnly) { 252 // We need to measure with nowrap otherwise wrapping occurs and the measurement is
252wrong 253 ourText.css('white-space', 'nowrap' ); 254 } else { 255 fontSizeHeight = _sizing( 256 'Height', ourText, 257 $.fn.height, maxHeight, 258 maxHeight, maxWidth, 259 minFontPixels, maxFontPixels 260 ); 261 } 262 263 // 2. Calculate which `font-size` would 264 // be best for the Width 265 var fontSizeWidth = undefined; 266 267 fontSizeWidth = _sizing( 268 'Width', ourText, 269 $.fn.width, maxWidth, 270 maxHeight, maxWidth, 271 minFontPixels, maxFontPixels 272 ); 273 274 // 3. Actually resize the text! 275 276 if (Opts.widthOnly) { 277 ourText.css({ 278 'font-size' : fontSizeWidth 279 }); 280 281 if (Opts.changeLineHeight) { 282 ourText.parent().css( 283 'line-height', 284 (lineHeight * fontSizeWidth + 'px') 285 ); 286 } 287 } 288 else { 289 var fontSizeFinal = Math.min(fontSizeHeight, fontSizeWidth); 290 291 ourText.css('font-size', fontSizeFinal); 292 293 if (Opts.changeLineHeight) { 294 ourText.parent().css( 295 'line-height', 296 (lineHeight * fontSizeFinal) + 'px' 297 ); 298 } 299 } 300 301 _debug( 302 '[TextFill] Finished { ' + 303 'Old font-size: ' + oldFontSize + ', ' + 304 'New font-size: ' + ourText.css('font-size') + ' }' 305 ); 306 307 // Oops, something wrong happened! 308 // If font-size increasing, we weren't supposed to exceed the original size 309 // If font-size decreasing, we hit minFontPixels, and still won't fit 310 if ((ourText.width() > maxWidth && !Opts.allowOverflow) || 311 (ourText.height() > maxHeight && !Opts.widthOnly && !Opts.allowOverflow)) { 312 313 ourText.css('font-size', oldFontSize); 314 315 // Failure callback 316 if (Opts.fail) { 317 Opts.fail(this); 318 } 319 320 _debug( 321 '[TextFill] Failure { ' + 322 'Current Width: ' + ourText.width() + ', ' + 323 'Maximum Width: ' + maxWidth + ', ' + 324 'Current Height: ' + ourText.height() + ', ' + 325 'Maximum Height: ' + maxHeight + ' }' 326 ); 327 } 328 else if (Opts.success) { 329 Opts.success(this); 330 } 331 }); 332 333 // Complete callback 334 if (Opts.complete) { 335 Opts.complete(this); 336 } 337 338 _debug('[TextFill] End Debug'); 339 return this; 340 }; 341 342})(function() { 343 if (typeof module !== 'undefined' && module.exports) { 344 return require('jquery'); 345 } 346 return window.jQuery; 347}()); 348
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.