| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.getTextMeasurementConfig = exports.getStringSize = exports.getStringCacheStats = exports.configureTextMeasurement = exports.clearStringCache = void 0;
|
|---|
| 7 | var _Global = require("./Global");
|
|---|
| 8 | var _LRUCache = require("./LRUCache");
|
|---|
| 9 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 10 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 11 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 12 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 13 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 14 | var defaultConfig = {
|
|---|
| 15 | cacheSize: 2000,
|
|---|
| 16 | enableCache: true
|
|---|
| 17 | };
|
|---|
| 18 | var currentConfig = _objectSpread({}, defaultConfig);
|
|---|
| 19 | var stringCache = new _LRUCache.LRUCache(currentConfig.cacheSize);
|
|---|
| 20 | var SPAN_STYLE = {
|
|---|
| 21 | position: 'absolute',
|
|---|
| 22 | top: '-20000px',
|
|---|
| 23 | left: 0,
|
|---|
| 24 | padding: 0,
|
|---|
| 25 | margin: 0,
|
|---|
| 26 | border: 'none',
|
|---|
| 27 | whiteSpace: 'pre'
|
|---|
| 28 | };
|
|---|
| 29 | var MEASUREMENT_SPAN_ID = 'recharts_measurement_span';
|
|---|
| 30 | function createCacheKey(text, style) {
|
|---|
| 31 | // Simple string concatenation for better performance than JSON.stringify
|
|---|
| 32 | var fontSize = style.fontSize || '';
|
|---|
| 33 | var fontFamily = style.fontFamily || '';
|
|---|
| 34 | var fontWeight = style.fontWeight || '';
|
|---|
| 35 | var fontStyle = style.fontStyle || '';
|
|---|
| 36 | var letterSpacing = style.letterSpacing || '';
|
|---|
| 37 | var textTransform = style.textTransform || '';
|
|---|
| 38 | return "".concat(text, "|").concat(fontSize, "|").concat(fontFamily, "|").concat(fontWeight, "|").concat(fontStyle, "|").concat(letterSpacing, "|").concat(textTransform);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Measure text using DOM (accurate but slower)
|
|---|
| 43 | * @param text - The text to measure
|
|---|
| 44 | * @param style - CSS style properties to apply
|
|---|
| 45 | * @returns The size of the text
|
|---|
| 46 | */
|
|---|
| 47 | var measureTextWithDOM = (text, style) => {
|
|---|
| 48 | try {
|
|---|
| 49 | var measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
|
|---|
| 50 | if (!measurementSpan) {
|
|---|
| 51 | measurementSpan = document.createElement('span');
|
|---|
| 52 | measurementSpan.setAttribute('id', MEASUREMENT_SPAN_ID);
|
|---|
| 53 | measurementSpan.setAttribute('aria-hidden', 'true');
|
|---|
| 54 | document.body.appendChild(measurementSpan);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | // Apply styles directly without unnecessary object creation
|
|---|
| 58 | Object.assign(measurementSpan.style, SPAN_STYLE, style);
|
|---|
| 59 | measurementSpan.textContent = "".concat(text);
|
|---|
| 60 | var rect = measurementSpan.getBoundingClientRect();
|
|---|
| 61 | return {
|
|---|
| 62 | width: rect.width,
|
|---|
| 63 | height: rect.height
|
|---|
| 64 | };
|
|---|
| 65 | } catch (_unused) {
|
|---|
| 66 | return {
|
|---|
| 67 | width: 0,
|
|---|
| 68 | height: 0
|
|---|
| 69 | };
|
|---|
| 70 | }
|
|---|
| 71 | };
|
|---|
| 72 | var getStringSize = exports.getStringSize = function getStringSize(text) {
|
|---|
| 73 | var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|---|
| 74 | if (text === undefined || text === null || _Global.Global.isSsr) {
|
|---|
| 75 | return {
|
|---|
| 76 | width: 0,
|
|---|
| 77 | height: 0
|
|---|
| 78 | };
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // If caching is disabled, measure directly
|
|---|
| 82 | if (!currentConfig.enableCache) {
|
|---|
| 83 | return measureTextWithDOM(text, style);
|
|---|
| 84 | }
|
|---|
| 85 | var cacheKey = createCacheKey(text, style);
|
|---|
| 86 | var cachedResult = stringCache.get(cacheKey);
|
|---|
| 87 | if (cachedResult) {
|
|---|
| 88 | return cachedResult;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | // Measure using DOM
|
|---|
| 92 | var result = measureTextWithDOM(text, style);
|
|---|
| 93 |
|
|---|
| 94 | // Store in LRU cache
|
|---|
| 95 | stringCache.set(cacheKey, result);
|
|---|
| 96 | return result;
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Configure text measurement behavior
|
|---|
| 101 | * @param config - Partial configuration to apply
|
|---|
| 102 | * @returns void
|
|---|
| 103 | */
|
|---|
| 104 | var configureTextMeasurement = config => {
|
|---|
| 105 | var newConfig = _objectSpread(_objectSpread({}, currentConfig), config);
|
|---|
| 106 | if (newConfig.cacheSize !== currentConfig.cacheSize) {
|
|---|
| 107 | stringCache = new _LRUCache.LRUCache(newConfig.cacheSize);
|
|---|
| 108 | }
|
|---|
| 109 | currentConfig = newConfig;
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Get current text measurement configuration
|
|---|
| 114 | * @returns Current configuration
|
|---|
| 115 | */
|
|---|
| 116 | exports.configureTextMeasurement = configureTextMeasurement;
|
|---|
| 117 | var getTextMeasurementConfig = () => _objectSpread({}, currentConfig);
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Clear the string size cache. Useful for testing or memory management.
|
|---|
| 121 | * @returns void
|
|---|
| 122 | */
|
|---|
| 123 | exports.getTextMeasurementConfig = getTextMeasurementConfig;
|
|---|
| 124 | var clearStringCache = () => {
|
|---|
| 125 | stringCache.clear();
|
|---|
| 126 | };
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Get cache statistics for debugging purposes.
|
|---|
| 130 | * @returns Cache statistics including size and max size
|
|---|
| 131 | */
|
|---|
| 132 | exports.clearStringCache = clearStringCache;
|
|---|
| 133 | var getStringCacheStats = () => ({
|
|---|
| 134 | size: stringCache.size(),
|
|---|
| 135 | maxSize: currentConfig.cacheSize
|
|---|
| 136 | });
|
|---|
| 137 | exports.getStringCacheStats = getStringCacheStats; |
|---|