forked from mkaminsky11/codeyourcloud
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext.js
More file actions
323 lines (267 loc) · 8.9 KB
/
Copy pathtext.js
File metadata and controls
323 lines (267 loc) · 8.9 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
erve textfill
* @name jquery.textfill.js
* @author Russ Painter
* @author Yu-Jie Lin
* @author Alexandre Dantas
* @version 0.6.0
* @date 2014-08-19
* @copyright (c) 2014 Alexandre Dantas
* @copyright (c) 2012-2013 Yu-Jie Lin
* @copyright (c) 2009 Russ Painter
* @license MIT License
* @homepage https://github.com/jquery-textfill/jquery-textfill
* @example http://jquery-textfill.github.io/jquery-textfill/index.html
*/
; (function($) {
/**
* Resizes an inner element's font so that the
* inner element completely fills the outer element.
*
* @param {Object} options User options that take
* higher precedence when
* merging with the default ones.
*
* @return All outer elements processed
*/
$.fn.textfill = function(options) {
// ______ _______ _______ _______ _ _ _______ _______
// | \ |______ |______ |_____| | | | | |______
// |_____/ |______ | | | |_____| |_____ | ______|
//
// Merging user options with the default values
var defaults = {
debug : false,
maxFontPixels : 40,
minFontPixels : 4,
innerTag : 'span',
widthOnly : false,
success : null, // callback when a resizing is done
callback : null, // callback when a resizing is done (deprecated, use success)
fail : null, // callback when a resizing is failed
complete : null, // callback when all is done
explicitWidth : null,
explicitHeight : null,
changeLineHeight : false
};
var Opts = $.extend(defaults, options);
// _______ _ _ __ _ _______ _______ _____ _____ __ _ _______
// |______ | | | \ | | | | | | | \ | |______
// | |_____| | \_| |_____ | __|__ |_____| | \_| ______|
//
// Predefining the awesomeness
// Output arguments to the Debug console
// if "Debug Mode" is enabled
function _debug() {
if (!Opts.debug
|| typeof console == 'undefined'
|| typeof console.debug == 'undefined') {
return;
}
console.debug.apply(console, arguments);
}
// Output arguments to the Warning console
function _warn() {
if (typeof console == 'undefined' ||
typeof console.warn == 'undefined') {
return;
}
console.warn.apply(console, arguments);
}
// Outputs all information on the current sizing
// of the font.
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 }'
);
}
/**
* Calculates which size the font can get resized,
* according to constrains.
*
* @param {String} prefix Gets shown on the console before
* all the arguments, if debug mode is on.
* @param {Object} ourText The DOM element to resize,
* that contains the text.
* @param {function} func Function called on `ourText` that's
* used to compare with `max`.
* @param {number} max Maximum value, that gets compared with
* `func` called on `ourText`.
* @param {number} minFontPixels Minimum value the font can
* get resized to (in pixels).
* @param {number} maxFontPixels Maximum value the font can
* get resized to (in pixels).
*
* @return Size (in pixels) that the font can be resized.
*/
function _sizing(prefix, ourText, func, max, maxHeight, maxWidth, minFontPixels, maxFontPixels) {
_debug_sizing(
prefix, ourText,
maxHeight, maxWidth,
minFontPixels, maxFontPixels
);
// The kernel of the whole plugin, take most attention
// on this part.
//
// This is a loop that keeps increasing the `font-size`
// until it fits the parent element.
//
// - Start from the minimal allowed value (`minFontPixels`)
// - Guesses an average font size (in pixels) for the font,
// - Resizes the text and sees if its size is within the
// boundaries (`minFontPixels` and `maxFontPixels`).
// - If so, keep guessing until we break.
// - If not, return the last calculated size.
//
// I understand this is not optimized and we should
// consider implementing something akin to
// Daniel Hoffmann's answer here:
//
// http://stackoverflow.com/a/17433451/1094964
//
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;
}
// _______ _______ _______ ______ _______
// |______ | |_____| |_____/ |
// ______| | | | | \_ |
//
// Let's get it started (yeah)!
_debug('[TextFill] Start Debug');
this.each(function() {
// Contains the child element we will resize.
// $(this) means the parent container
var ourText = $(Opts.innerTag + ':visible:first', this);
// Will resize to this dimensions.
// Use explicit dimensions when specified
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;
// Remember, if this `maxFontPixels` is negative,
// the text will resize to as long as the container
// can accomodate
var maxFontPixels = (Opts.maxFontPixels <= 0 ?
maxHeight :
Opts.maxFontPixels);
// Let's start it all!
// 1. Calculate which `font-size` would
// be best for the Height
var fontSizeHeight = undefined;
if (! Opts.widthOnly)
fontSizeHeight = _sizing(
'Height', ourText,
$.fn.height, maxHeight,
maxHeight, maxWidth,
minFontPixels, maxFontPixels
);
// 2. Calculate which `font-size` would
// be best for the Width
var fontSizeWidth = undefined;
fontSizeWidth = _sizing(
'Width', ourText,
$.fn.width, maxWidth,
maxHeight, maxWidth,
minFontPixels, maxFontPixels
);
// 3. Actually resize the text!
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') + ' }'
);
// Oops, something wrong happened!
// We weren't supposed to exceed the original size
if ((ourText.width() > maxWidth) ||
(ourText.height() > maxHeight && !Opts.widthOnly)) {
ourText.css('font-size', oldFontSize);
// Failure callback
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');
// Success callback
Opts.callback(this);
}
});
// Complete callback
if (Opts.complete)
Opts.complete(this);
_debug('[TextFill] End Debug');
return this;
};
})(window.jQuery);