UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

1 lines 4.15 kB
{"version":3,"file":"cache.min.mjs","sources":["../../src/cache.ts"],"sourcesContent":["import { config } from './config';\nimport type { TRectBounds } from './typedefs';\n\nexport class Cache {\n /**\n * Cache of widths of chars in text rendering.\n */\n charWidthsCache: Record<\n /** fontFamily */ string,\n Record<\n /** fontStyleCacheKey */ string,\n Record</** char */ string, /** width */ number>\n >\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 }) {\n fontFamily = fontFamily.toLowerCase();\n if (!this.charWidthsCache[fontFamily]) {\n this.charWidthsCache[fontFamily] = {};\n }\n const fontCache = this.charWidthsCache[fontFamily];\n const cacheKey = `${fontStyle.toLowerCase()}_${(\n fontWeight + ''\n ).toLowerCase()}`;\n if (!fontCache[cacheKey]) {\n fontCache[cacheKey] = {};\n }\n return fontCache[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 fontFamily = (fontFamily || '').toLowerCase();\n if (!fontFamily) {\n this.charWidthsCache = {};\n } else if (this.charWidthsCache[fontFamily]) {\n delete this.charWidthsCache[fontFamily];\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","this","getFontCache","_ref","fontFamily","fontStyle","fontWeight","toLowerCase","charWidthsCache","fontCache","cacheKey","concat","clearFontCache","limitDimsByArea","ar","perfLimitSizeTotal","config","roughWidth","Math","sqrt","floor","cache"],"mappings":"yHAGO,MAAMA,EAAMC,WAAAA,GACjBC,EAAAC,KAAA,kBASI,CAAA,GAiEJD,EAAAC,KAAA,qBAQkD,CAAA,EAAE,CApEpDC,YAAAA,CAAYC,GAQT,IARUC,WACXA,EAAUC,UACVA,EAASC,WACTA,GAKDH,EACCC,EAAaA,EAAWG,cACnBN,KAAKO,gBAAgBJ,KACxBH,KAAKO,gBAAgBJ,GAAc,IAErC,MAAMK,EAAYR,KAAKO,gBAAgBJ,GACjCM,KAAQC,OAAMN,EAAUE,cAAa,KAAAI,QACzCL,EAAa,IACbC,eAIF,OAHKE,EAAUC,KACbD,EAAUC,GAAY,IAEjBD,EAAUC,EACnB,CAaAE,cAAAA,CAAeR,IACbA,GAAcA,GAAc,IAAIG,eAGrBN,KAAKO,gBAAgBJ,WACvBH,KAAKO,gBAAgBJ,GAF5BH,KAAKO,gBAAkB,EAI3B,CAQAK,eAAAA,CAAgBC,GACd,MAAMC,mBAAEA,GAAuBC,EACzBC,EAAaC,KAAKC,KAAKJ,EAAqBD,GAGlD,MAAO,CACLI,KAAKE,MAAMH,GACXC,KAAKE,MAAML,EAAqBE,GAEpC,QAaWI,EAAQ,IAAIvB"}