{"version":3,"file":"cache.mjs","sources":["../../src/cache.ts"],"sourcesContent":["import { config } from './config';\nimport type { TRectBounds } from './typedefs';\n\ntype TextCouplesCache = Map</** char */ string, /** width */ number>;\n\ntype FamilyCache = Map</** fontStyleCacheKey */ string, TextCouplesCache>;\n\nexport class Cache {\n  /**\n   * Cache of widths of chars in text rendering.\n   */\n  declare charWidthsCache: Map</** fontFamily */ string, FamilyCache>;\n\n  constructor() {\n    this.charWidthsCache = new Map();\n  }\n\n  /**\n   * @return {Object} reference to cache\n   */\n  getFontCache({\n    fontFamily,\n    fontStyle,\n    fontWeight,\n  }: {\n    fontFamily: string;\n    fontStyle: string;\n    fontWeight: string | number;\n  }): TextCouplesCache {\n    fontFamily = fontFamily.toLowerCase();\n    const cache = this.charWidthsCache;\n    if (!cache.has(fontFamily)) {\n      cache.set(fontFamily, new Map<string, TextCouplesCache>());\n    }\n    const fontCache = cache.get(fontFamily)!;\n    const cacheKey = `${fontStyle.toLowerCase()}_${(\n      fontWeight + ''\n    ).toLowerCase()}`;\n    if (!fontCache.has(cacheKey)) {\n      fontCache.set(cacheKey, new Map<string, number>());\n    }\n    return fontCache.get(cacheKey)!;\n  }\n\n  /**\n   * Clear char widths cache for the given font family or all the cache if no\n   * fontFamily is specified.\n   * Use it if you know you are loading fonts in a lazy way and you are not waiting\n   * for custom fonts to load properly when adding text objects to the canvas.\n   * If a text object is added when its own font is not loaded yet, you will get wrong\n   * measurement and so wrong bounding boxes.\n   * After the font cache is cleared, either change the textObject text content or call\n   * initDimensions() to trigger a recalculation\n   * @param {String} [fontFamily] font family to clear\n   */\n  clearFontCache(fontFamily?: string) {\n    if (!fontFamily) {\n      this.charWidthsCache = new Map();\n    } else {\n      this.charWidthsCache.delete((fontFamily || '').toLowerCase());\n    }\n  }\n\n  /**\n   * Given current aspect ratio, determines the max width and height that can\n   * respect the total allowed area for the cache.\n   * @param {number} ar aspect ratio\n   * @return {number[]} Limited dimensions X and Y\n   */\n  limitDimsByArea(ar: number) {\n    const { perfLimitSizeTotal } = config;\n    const roughWidth = Math.sqrt(perfLimitSizeTotal * ar);\n    // we are not returning a point on purpose, to avoid circular dependencies\n    // this is an internal utility\n    return [\n      Math.floor(roughWidth),\n      Math.floor(perfLimitSizeTotal / roughWidth),\n    ];\n  }\n\n  /**\n   * This object keeps the results of the boundsOfCurve calculation mapped by the joined arguments necessary to calculate it.\n   * It does speed up calculation, if you parse and add always the same paths, but in case of heavy usage of freedrawing\n   * you do not get any speed benefit and you get a big object in memory.\n   * The object was a private variable before, while now is appended to the lib so that you have access to it and you\n   * can eventually clear it.\n   * It was an internal variable, is accessible since version 2.3.4\n   */\n  boundsOfCurveCache: Record<string, TRectBounds> = {};\n}\n\nexport const cache = new Cache();\n"],"names":["Cache","constructor","_defineProperty","charWidthsCache","Map","getFontCache","_ref","fontFamily","fontStyle","fontWeight","toLowerCase","cache","has","set","fontCache","get","cacheKey","clearFontCache","delete","limitDimsByArea","ar","perfLimitSizeTotal","config","roughWidth","Math","sqrt","floor"],"mappings":";;;AAOO,MAAMA,KAAK,CAAC;AACjB;AACF;AACA;;AAGEC,EAAAA,WAAWA,GAAG;AAmEd;AACF;AACA;AACA;AACA;AACA;AACA;AACA;IAPEC,eAAA,CAAA,IAAA,EAAA,oBAAA,EAQkD,EAAE,CAAA;AA1ElD,IAAA,IAAI,CAACC,eAAe,GAAG,IAAIC,GAAG,EAAE;AAClC,EAAA;;AAEA;AACF;AACA;EACEC,YAAYA,CAAAC,IAAA,EAQS;IAAA,IARR;MACXC,UAAU;MACVC,SAAS;AACTC,MAAAA;AAKF,KAAC,GAAAH,IAAA;AACCC,IAAAA,UAAU,GAAGA,UAAU,CAACG,WAAW,EAAE;AACrC,IAAA,MAAMC,KAAK,GAAG,IAAI,CAACR,eAAe;AAClC,IAAA,IAAI,CAACQ,KAAK,CAACC,GAAG,CAACL,UAAU,CAAC,EAAE;MAC1BI,KAAK,CAACE,GAAG,CAACN,UAAU,EAAE,IAAIH,GAAG,EAA4B,CAA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