svg-text-fit
Version:
SVG utility for fitting text inside a box by resizing or ellipsis
3 lines (2 loc) • 6.73 kB
JavaScript
// Version 1.2.3 svg-text-fit - https://github.com/vasturiano/svg-text-fit#readme
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).TextFit={})}(this,(function(t){"use strict";function n(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=Array(n);e<n;e++)r[e]=t[e];return r}function e(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t){var n=typeof t;return null!=t&&("object"==n||"function"==n)}var o="object"==typeof global&&global&&global.Object===Object&&global,i="object"==typeof self&&self&&self.Object===Object&&self,a=o||i||Function("return this")(),u=function(){return a.Date.now()},l=/\s/;var f=/^\s+/;function c(t){return t?t.slice(0,function(t){for(var n=t.length;n--&&l.test(t.charAt(n)););return n}(t)+1).replace(f,""):t}var s=a.Symbol,d=Object.prototype,v=d.hasOwnProperty,p=d.toString,h=s?s.toStringTag:void 0;var y=Object.prototype.toString;var b=s?s.toStringTag:void 0;function g(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":b&&b in Object(t)?function(t){var n=v.call(t,h),e=t[h];try{t[h]=void 0;var r=!0}catch(t){}var o=p.call(t);return r&&(n?t[h]=e:delete t[h]),o}(t):function(t){return y.call(t)}(t)}var m=/^[-+]0x[0-9a-f]+$/i,j=/^0b[01]+$/i,x=/^0o[0-7]+$/i,w=parseInt;function O(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return null!=t&&"object"==typeof t}(t)&&"[object Symbol]"==g(t)}(t))return NaN;if(r(t)){var n="function"==typeof t.valueOf?t.valueOf():t;t=r(n)?n+"":n}if("string"!=typeof t)return 0===t?t:+t;t=c(t);var e=j.test(t);return e||x.test(t)?w(t.slice(2),e?2:8):m.test(t)?NaN:+t}var A=Math.max,S=Math.min;function T(t,n,e){var o,i,a,l,f,c,s=0,d=!1,v=!1,p=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function h(n){var e=o,r=i;return o=i=void 0,s=n,l=t.apply(r,e)}function y(t){var e=t-c;return void 0===c||e>=n||e<0||v&&t-s>=a}function b(){var t=u();if(y(t))return g(t);f=setTimeout(b,function(t){var e=n-(t-c);return v?S(e,a-(t-s)):e}(t))}function g(t){return f=void 0,p&&o?h(t):(o=i=void 0,l)}function m(){var t=u(),e=y(t);if(o=arguments,i=this,c=t,e){if(void 0===f)return function(t){return s=t,f=setTimeout(b,n),d?h(t):l}(c);if(v)return clearTimeout(f),f=setTimeout(b,n),h(c)}return void 0===f&&(f=setTimeout(b,n)),l}return n=O(n)||0,r(e)&&(d=!!e.leading,a=(v="maxWait"in e)?A(O(e.maxWait)||0,n):a,p="trailing"in e?!!e.trailing:p),m.cancel=function(){void 0!==f&&clearTimeout(f),s=0,o=c=i=f=void 0},m.flush=function(){return void 0===f?l:g(u())},m}function E(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=Array(n);e<n;e++)r[e]=t[e];return r}function I(t,n,e){return Object.defineProperty(t,"prototype",{writable:!1}),t}function C(t,n){return function(t){if(Array.isArray(t))return t}(t)||function(t,n){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var r,o,i,a,u=[],l=!0,f=!1;try{if(i=(e=e.call(t)).next,0===n);else for(;!(l=(r=i.call(e)).done)&&(u.push(r.value),u.length!==n);l=!0);}catch(t){f=!0,o=t}finally{try{if(!l&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(f)throw o}}return u}}(t,n)||function(t,n){if(t){if("string"==typeof t)return E(t,n);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?E(t,n):void 0}}(t,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var B=I((function t(n,e){var r=e.default,o=void 0===r?null:r,i=e.triggerUpdate,a=void 0===i||i,u=e.onChange,l=void 0===u?function(t,n){}:u;!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,t),this.name=n,this.defaultVal=o,this.triggerUpdate=a,this.onChange=l}));function M(t){var n=t.stateInit,e=void 0===n?function(){return{}}:n,r=t.props,o=void 0===r?{}:r,i=t.methods,a=void 0===i?{}:i,u=t.aliases,l=void 0===u?{}:u,f=t.init,c=void 0===f?function(){}:f,s=t.update,d=void 0===s?function(){}:s,v=Object.keys(o).map((function(t){return new B(t,o[t])}));return function t(){for(var n=arguments.length,r=new Array(n),o=0;o<n;o++)r[o]=arguments[o];var i=!!(this instanceof t?this.constructor:void 0),u=i?r.shift():void 0,f=r[0],s=void 0===f?{}:f,p=Object.assign({},e instanceof Function?e(s):e,{initialised:!1}),h={};function y(t){return b(t,s),g(),y}var b=function(t,n){c.call(y,t,p,n),p.initialised=!0},g=T((function(){p.initialised&&(d.call(y,p,h),h={})}),1);return v.forEach((function(t){y[t.name]=function(t){var n=t.name,e=t.triggerUpdate,r=void 0!==e&&e,o=t.onChange,i=void 0===o?function(t,n){}:o,a=t.defaultVal,u=void 0===a?null:a;return function(t){var e=p[n];if(!arguments.length)return e;var o=void 0===t?u:t;return p[n]=o,i.call(y,o,p,e),!h.hasOwnProperty(n)&&(h[n]=e),r&&g(),y}}(t)})),Object.keys(a).forEach((function(t){y[t]=function(){for(var n,e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return(n=a[t]).call.apply(n,[y,p].concat(r))}})),Object.entries(l).forEach((function(t){var n=C(t,2),e=n[0],r=n[1];return y[e]=y[r]})),y.resetProps=function(){return v.forEach((function(t){y[t.name](t.defaultVal)})),y},y.resetProps(),p._rerender=g,i&&u&&y(u),y}}var U=M({props:{bbox:{default:{width:null,height:null}},passes:{default:3}},init:function(t,n){n.el=t},update:function(t){e(Array(t.passes).keys()).some((function(){var n=parseInt(t.el.style["font-size"].split("px")[0])||20,e=t.el.getBBox(),r=Math.floor(n*Math.min(t.bbox.width/e.width,t.bbox.height/e.height));if(r===n)return!0;t.el.style["font-size"]=r+"px"}))}}),N=M({props:{maxWidth:{default:1/0}},init:function(t,n){n.el=t},update:function(t){for(var n,e,r=t.el.textContent,o=Math.round(r.length*t.maxWidth/t.el.getBBox().width*1.2);--o&&t.maxWidth/t.el.getBBox().width<1;)t.el.textContent=(e=o,(n=r).length<=e?n:n.substring(0,2*e/3)+"..."+n.substring(n.length-e/3,n.length))}});t.ellipsis=N,t.fitToBox=U}));