123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
-
- ; (function($) {
-
- $.fn.textfill = function(options) {
-
-
-
-
-
- var defaults = {
- debug : false,
- maxFontPixels : 40,
- minFontPixels : 4,
- innerTag : 'span',
- widthOnly : false,
- success : null,
- callback : null,
- fail : null,
- complete : null,
- explicitWidth : null,
- explicitHeight : null,
- changeLineHeight : false
- };
- var Opts = $.extend(defaults, options);
-
-
-
-
-
-
-
- function _debug() {
- if (!Opts.debug
- || typeof console == 'undefined'
- || typeof console.debug == 'undefined') {
- return;
- }
- console.debug.apply(console, arguments);
- }
-
- function _warn() {
- if (typeof console == 'undefined' ||
- typeof console.warn == 'undefined') {
- return;
- }
- console.warn.apply(console, arguments);
- }
-
-
- function _debug_sizing(prefix, ourText, maxHeight, maxWidth, minFontPixels, maxFontPixels) {
- function _m(v1, v2) {
- var marker = ' / ';
- if (v1 > v2)
- marker = ' > ';
- else if (v1 == v2)
- marker = ' = ';
- return marker;
- }
- _debug(
- '[TextFill] ' + prefix + ' { ' +
- 'font-size: ' + ourText.css('font-size') + ',' +
- 'Height: ' + ourText.height() + 'px ' + _m(ourText.height(), maxHeight) + maxHeight + 'px,' +
- 'Width: ' + ourText.width() + _m(ourText.width() , maxWidth) + maxWidth + ',' +
- 'minFontPixels: ' + minFontPixels + 'px, ' +
- 'maxFontPixels: ' + maxFontPixels + 'px }'
- );
- }
-
- function _sizing(prefix, ourText, func, max, maxHeight, maxWidth, minFontPixels, maxFontPixels) {
- _debug_sizing(
- prefix, ourText,
- maxHeight, maxWidth,
- minFontPixels, maxFontPixels
- );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- while (minFontPixels < (maxFontPixels - 1)) {
- var fontSize = Math.floor((minFontPixels + maxFontPixels) / 2);
- ourText.css('font-size', fontSize);
- if (func.call(ourText) <= max) {
- minFontPixels = fontSize;
- if (func.call(ourText) == max)
- break;
- }
- else
- maxFontPixels = fontSize;
- _debug_sizing(
- prefix, ourText,
- maxHeight, maxWidth,
- minFontPixels, maxFontPixels
- );
- }
- ourText.css('font-size', maxFontPixels);
- if (func.call(ourText) <= max) {
- minFontPixels = maxFontPixels;
- _debug_sizing(
- prefix + '* ', ourText,
- maxHeight, maxWidth,
- minFontPixels, maxFontPixels
- );
- }
- return minFontPixels;
- }
-
-
-
-
-
- _debug('[TextFill] Start Debug');
- this.each(function() {
-
-
- var ourText = $(Opts.innerTag + ':visible:first', this);
-
-
- var maxHeight = Opts.explicitHeight || $(this).height();
- var maxWidth = Opts.explicitWidth || $(this).width();
- var oldFontSize = ourText.css('font-size');
- var lineHeight = parseFloat(ourText.css('line-height')) / parseFloat(oldFontSize);
- _debug('[TextFill] Inner text: ' + ourText.text());
- _debug('[TextFill] All options: ', Opts);
- _debug('[TextFill] Maximum sizes: { ' +
- 'Height: ' + maxHeight + 'px, ' +
- 'Width: ' + maxWidth + 'px' + ' }'
- );
- var minFontPixels = Opts.minFontPixels;
-
-
-
- var maxFontPixels = (Opts.maxFontPixels <= 0 ?
- maxHeight :
- Opts.maxFontPixels);
-
-
-
- var fontSizeHeight = undefined;
- if (! Opts.widthOnly)
- fontSizeHeight = _sizing(
- 'Height', ourText,
- $.fn.height, maxHeight,
- maxHeight, maxWidth,
- minFontPixels, maxFontPixels
- );
-
-
- var fontSizeWidth = undefined;
- fontSizeWidth = _sizing(
- 'Width', ourText,
- $.fn.width, maxWidth,
- maxHeight, maxWidth,
- minFontPixels, maxFontPixels
- );
-
- if (Opts.widthOnly) {
- ourText.css({
- 'font-size' : fontSizeWidth,
- 'white-space': 'nowrap'
- });
- if (Opts.changeLineHeight)
- ourText.parent().css(
- 'line-height',
- (lineHeight * fontSizeWidth + 'px')
- );
- }
- else {
- var fontSizeFinal = Math.min(fontSizeHeight, fontSizeWidth);
- ourText.css('font-size', fontSizeFinal);
- if (Opts.changeLineHeight)
- ourText.parent().css(
- 'line-height',
- (lineHeight * fontSizeFinal) + 'px'
- );
- }
- _debug(
- '[TextFill] Finished { ' +
- 'Old font-size: ' + oldFontSize + ', ' +
- 'New font-size: ' + ourText.css('font-size') + ' }'
- );
-
-
- if ((ourText.width() > maxWidth) ||
- (ourText.height() > maxHeight && !Opts.widthOnly)) {
- ourText.css('font-size', oldFontSize);
-
- if (Opts.fail)
- Opts.fail(this);
- _debug(
- '[TextFill] Failure { ' +
- 'Current Width: ' + ourText.width() + ', ' +
- 'Maximum Width: ' + maxWidth + ', ' +
- 'Current Height: ' + ourText.height() + ', ' +
- 'Maximum Height: ' + maxHeight + ' }'
- );
- }
- else if (Opts.success) {
- Opts.success(this);
- }
- else if (Opts.callback) {
- _warn('callback is deprecated, use success, instead');
-
- Opts.callback(this);
- }
- });
-
- if (Opts.complete)
- Opts.complete(this);
- _debug('[TextFill] End Debug');
- return this;
- };
- })(window.jQuery);
|