polotno 2.38.0 → 2.38.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/fonts.js CHANGED
@@ -1 +1 @@
1
- import*as t from"mobx";import{triggerLoadError as e,getFontLoadTimeout as o}from"./loader.js";import{removeTags as n}from"./text.js";let r=t.observable(["Roboto","Amatic SC","Press Start 2P","Marck Script","Rubik Mono One"]),s=t.observable({value:!1});export function isGoogleFontChanged(){return s.value}export function setGoogleFonts(t){"default"!==t?(s.value=!0,r.splice(0,r.length),r.push(...t)):s.value=!1}export function getFontsList(){return r}export const globalFonts=t.observable([]);export function addGlobalFont(t){globalFonts.push(t)}export function removeGlobalFont(t){const e=globalFonts.findIndex(e=>e.fontFamily===t);-1!==e&&globalFonts.splice(e,1)}export function replaceGlobalFonts(t){globalFonts.replace(t)}const l="BESbswy 0123456789 Il1Ww";function a(t){const e=function(t=""){const e=n(t).replace(/\s+/g," ").trim();return e?Array.from(e).slice(0,10).join(""):""}(t);return e?`${l} ${e}`:l}export function _getFontProbeTextForTesting(t){return a(t)}let i;function c(t,e="sans-serif",o="normal",n="normal",r=l){i||(i=document.createElement("canvas"));var s=i.getContext("2d");return s.font=`${o} ${n} 40px '${t}', ${e}`,s.measureText(r).width}export function measureFontDom(t,e="sans-serif",o="normal",n="normal",r=l){if("undefined"==typeof document||!document.body){return 0}const s=document.createElement("span");s.textContent=r,s.style.cssText=`\n position:absolute;\n visibility:hidden;\n white-space:nowrap;\n top:-9999px;\n left:-9999px;\n font:${o} ${n} 90px '${t}', ${e};\n `,document.body.appendChild(s);const a=s.getBoundingClientRect().width;return s.remove(),a}const u={Arial:!0};export const isFontLoaded=(t,e,o)=>e&&o?!!u[`${t}_${e}_${o}`]:Object.keys(u).some(e=>e.startsWith(t+"_"))||!!u[t];export function _setFontLoadedForTesting(t,e){e?u[t]=!0:delete u[t]}export async function loadFont(t,n,r,s){var f;const m=`${t}_${n}_${r}`;if(u[m]){return}const d=!!(null===(f=document.fonts)||void 0===f?void 0:f.load),p=a(s),g=function(t="normal",e="normal",o=l){i||(i=document.createElement("canvas"));var n=i.getContext("2d");return n.font=`${t} ${e} 40px sans-serif`,n.measureText(o).width}(n,r,p);if(d){try{await document.fonts.load(`${n} ${r} 16px '${t}'`,p);if(g!==c(t,"sans-serif",n,r,p)){return void(u[m]=!0)}}catch(F){}}const x=function(t="normal",e="normal",o=l){i||(i=document.createElement("canvas"));var n=i.getContext("2d");return n.font=`${t} ${e} 40px serif`,n.measureText(o).width}(n,r,p),$=c(t,"sans-serif",n,r,p),y=Math.min(6e3,o())/60;for(let e=0;e<y;e++){const e=c(t,"sans-serif",n,r,p),o=c(t,"serif",n,r,p);if(e!==$||e!==g||o!==x){return await new Promise(t=>setTimeout(t,100)),void(u[m]=!0)}await new Promise(t=>setTimeout(t,60))}console.warn(`Timeout for loading font "${t}". Looks like polotno can't load it. Is it a correct font family?`),e(`Timeout for loading font "${t}"`)}const f={};let m="400,400italic,700,700italic";export function setGoogleFontsVariants(t){m=t}export function getGoogleFontsVariants(){return m}export function getGoogleFontsUrl(t){return`https://fonts.googleapis.com/css?family=${t.replace(/ /g,"+")}:${m}`}export function injectGoogleFont(t){if(f[t]){return}const e=getGoogleFontsUrl(t),o=document.createElement("link");o.type="text/css",o.href=e,o.rel="stylesheet",document.getElementsByTagName("head")[0].appendChild(o),f[t]=!0}const d={};let p;export function injectCustomFont(t){const e=t.fontFamily;if(d[e]){return}if(!t.url&&!t.styles){return}const o=t.styles||(t.url?[{src:`url("${t.url}")`}]:[]),n=(p||(p=document.getElementById("polotno-font-style"),p||(p=document.createElement("style"),p.id="polotno-font-style",document.head.appendChild(p)),p)).sheet;o.forEach(t=>{n.insertRule(`\n @font-face{\n font-family:'${e}';\n src:${t.src};\n font-style:${t.fontStyle||"normal"};\n font-weight:${t.fontWeight||"normal"};\n font-display:swap; /* optional but recommended */\n }`,n.cssRules.length)}),d[t.fontFamily]=!0}
1
+ import*as t from"mobx";import{triggerLoadError as n,getFontLoadTimeout as o}from"./loader.js";import{removeTags as e}from"./text.js";let a=t.observable(["Roboto","Amatic SC","Press Start 2P","Marck Script","Rubik Mono One"]),s=t.observable({value:!1});export function isGoogleFontChanged(){return s.value}export function setGoogleFonts(t){"default"!==t?(s.value=!0,a.splice(0,a.length),a.push(...t)):s.value=!1}export function getFontsList(){return a}export const globalFonts=t.observable([]);export function addGlobalFont(t){globalFonts.push(t)}export function removeGlobalFont(t){const n=globalFonts.findIndex(n=>n.fontFamily===t);-1!==n&&globalFonts.splice(n,1)}export function replaceGlobalFonts(t){globalFonts.replace(t)}const r="BESbswy 0123456789 Il1Ww",i=["sans-serif","serif","monospace"];function l(t){const n=function(t=""){const n=e(t).replace(/\s+/g," ").trim();return n?Array.from(n).slice(0,10).join(""):""}(t);return n?`${r} ${n}`:r}export function _getFontProbeTextForTesting(t){return l(t)}let c;function u(t){return{width:t.width,actualBoundingBoxLeft:t.actualBoundingBoxLeft||0,actualBoundingBoxRight:t.actualBoundingBoxRight||0,actualBoundingBoxAscent:t.actualBoundingBoxAscent||0,actualBoundingBoxDescent:t.actualBoundingBoxDescent||0}}function f(t="sans-serif",n="normal",o="normal",e=r){c||(c=document.createElement("canvas"));const a=c.getContext("2d");return a.font=`${n} ${o} 40px ${t}`,u(a.measureText(e))}function d(t,n,o,e,a){return i.some(s=>{const i=function(t,n="sans-serif",o="normal",e="normal",a=r){c||(c=document.createElement("canvas"));const s=c.getContext("2d");return s.font=`${o} ${e} 40px '${t}', ${n}`,u(s.measureText(a))}(t,s,n,o,e);return function(t,n,o=.01){return Math.abs(t.width-n.width)>o||Math.abs(t.actualBoundingBoxLeft-n.actualBoundingBoxLeft)>o||Math.abs(t.actualBoundingBoxRight-n.actualBoundingBoxRight)>o||Math.abs(t.actualBoundingBoxAscent-n.actualBoundingBoxAscent)>o||Math.abs(t.actualBoundingBoxDescent-n.actualBoundingBoxDescent)>o}(i,a[s])})}export function measureFontDom(t,n="sans-serif",o="normal",e="normal",a=r){if("undefined"==typeof document||!document.body){return 0}const s=document.createElement("span");s.textContent=a,s.style.cssText=`\n position:absolute;\n visibility:hidden;\n white-space:nowrap;\n top:-9999px;\n left:-9999px;\n font:${o} ${e} 90px '${t}', ${n};\n `,document.body.appendChild(s);const i=s.getBoundingClientRect().width;return s.remove(),i}const m={Arial:!0};export const isFontLoaded=(t,n,o)=>n&&o?!!m[`${t}_${n}_${o}`]:Object.keys(m).some(n=>n.startsWith(t+"_"))||!!m[t];export function _setFontLoadedForTesting(t,n){n?m[t]=!0:delete m[t]}export async function loadFont(t,e,a,s){var r;const i=`${t}_${e}_${a}`;if(m[i]){return}const c=!!(null===(r=document.fonts)||void 0===r?void 0:r.load),u=l(s),p={"sans-serif":f("sans-serif",e,a,u),serif:f("serif",e,a,u),monospace:f("monospace",e,a,u)};if(c){try{if(await document.fonts.load(`${e} ${a} 16px '${t}'`,u),d(t,e,a,u,p)){return void(m[i]=!0)}}catch(x){}}const g=Math.min(6e3,o())/60;for(let n=0;n<g;n++){if(d(t,e,a,u,p)){return void(m[i]=!0)}await new Promise(t=>setTimeout(t,60))}console.warn(`Timeout for loading font "${t}". Looks like polotno can't load it. Is it a correct font family?`),n(`Timeout for loading font "${t}"`)}const p={};let g="400,400italic,700,700italic";export function setGoogleFontsVariants(t){g=t}export function getGoogleFontsVariants(){return g}export function getGoogleFontsUrl(t){return`https://fonts.googleapis.com/css?family=${t.replace(/ /g,"+")}:${g}`}export function injectGoogleFont(t){if(p[t]){return}const n=getGoogleFontsUrl(t),o=document.createElement("link");o.type="text/css",o.href=n,o.rel="stylesheet",document.getElementsByTagName("head")[0].appendChild(o),p[t]=!0}const x={};let h;export function injectCustomFont(t){const n=t.fontFamily;if(x[n]){return}if(!t.url&&!t.styles){return}const o=t.styles||(t.url?[{src:`url("${t.url}")`}]:[]),e=(h||(h=document.getElementById("polotno-font-style"),h||(h=document.createElement("style"),h.id="polotno-font-style",document.head.appendChild(h)),h)).sheet;o.forEach(t=>{e.insertRule(`\n @font-face{\n font-family:'${n}';\n src:${t.src};\n font-style:${t.fontStyle||"normal"};\n font-weight:${t.fontWeight||"normal"};\n font-display:swap; /* optional but recommended */\n }`,e.cssRules.length)}),x[t.fontFamily]=!0}
@@ -1 +1 @@
1
- import*as e from"mobx";import{getAPI as o}from"./api.js";import{setRemoveBackgroundEnabled as t}from"./flags.js";const n=e.observable({value:!1}),a=e.observable({value:"v1"});export const ___=()=>a.value;const i=e.observable({value:0});export const ____=()=>i.value;export const isCreditVisible=()=>n.value;const r=e.action(()=>{n.value=!0});let s="";export const getKey=()=>s||"";let l="undefined"!=typeof window?window.location.origin:"";const c="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Headless")>-1,d="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Electron")>-1;"file://"===l&&c&&(l="headless"),"file://"===l&&d&&(l="electron");const p=`%cPolotno error! Current domain is not allowed. It may lead to unexpected behavior and stop working. Please add "${l}" here: https://polotno.com/cabinet`;let u=fetch;export const __=e=>{u=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await u(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"2.38.0"})});if(e.runInAction(()=>{a.value=r.headers.get("x-api-version")||""}),!n){return console.error("Polotno API is initialized without API key. It may lead to unexpected behavior and stop working. Please create API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1}),!1}if(200!==r.status){await new Promise(e=>setTimeout(e,3e3));continue}const s=await r.json();return s.is_valid||(console.error("Polotno API key is not valid. Please get new API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1})),s.is_paid||(console.log("%cPolotno Free Version. For development usage only. https://polotno.com/","background: rgb(0, 161, 255); color: white; padding: 5px; margin: 5px;"),e.runInAction(()=>{0===i.value&&(i.value=2)})),s.is_domain_valid||console.log(p,"background: rgba(247, 101, 68, 1); color: white; padding: 5px; margin: 5px;"),t(s.remove_background_enabled),s.is_paid||!1}catch(r){await new Promise(e=>setTimeout(e,3e3))}}return console.error("Can not validate Polotno API key. Please report to anton@polotno.com immediately."),!0}export async function validateKey(e,o){s=e,await isKeyPaid(e)&&!o||r()}
1
+ import*as e from"mobx";import{getAPI as o}from"./api.js";import{setRemoveBackgroundEnabled as t}from"./flags.js";const n=e.observable({value:!1}),a=e.observable({value:"v1"});export const ___=()=>a.value;const i=e.observable({value:0});export const ____=()=>i.value;export const isCreditVisible=()=>n.value;const r=e.action(()=>{n.value=!0});let s="";export const getKey=()=>s||"";let l="undefined"!=typeof window?window.location.origin:"";const c="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Headless")>-1,d="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Electron")>-1;"file://"===l&&c&&(l="headless"),"file://"===l&&d&&(l="electron");const p=`%cPolotno error! Current domain is not allowed. It may lead to unexpected behavior and stop working. Please add "${l}" here: https://polotno.com/cabinet`;let u=fetch;export const __=e=>{u=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await u(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"2.38.2"})});if(e.runInAction(()=>{a.value=r.headers.get("x-api-version")||""}),!n){return console.error("Polotno API is initialized without API key. It may lead to unexpected behavior and stop working. Please create API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1}),!1}if(200!==r.status){await new Promise(e=>setTimeout(e,3e3));continue}const s=await r.json();return s.is_valid||(console.error("Polotno API key is not valid. Please get new API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1})),s.is_paid||(console.log("%cPolotno Free Version. For development usage only. https://polotno.com/","background: rgb(0, 161, 255); color: white; padding: 5px; margin: 5px;"),e.runInAction(()=>{0===i.value&&(i.value=2)})),s.is_domain_valid||console.log(p,"background: rgba(247, 101, 68, 1); color: white; padding: 5px; margin: 5px;"),t(s.remove_background_enabled),s.is_paid||!1}catch(r){await new Promise(e=>setTimeout(e,3e3))}}return console.error("Can not validate Polotno API key. Please report to anton@polotno.com immediately."),!0}export async function validateKey(e,o){s=e,await isKeyPaid(e)&&!o||r()}