"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/registry.ts var registry = /* @__PURE__ */ new Map(); function list() { return [...registry.entries()].map((entry) => ({ family: entry[0], axisTupleList: entry[1].axisTupleList })); } // src/types.ts function isRange(numberOrRange) { return typeof numberOrRange === "object"; } // src/utils.ts var axisDefaults = { BNCE: 0, CASL: 0, CRSV: 0.5, EDPT: 100, EHLT: 12, ELGR: 1, ELSH: 0, FILL: 0, FLAR: 0, GRAD: 0, HEXP: 0, INFM: 0, ital: 0, MONO: 0, MUTA: 0, opsz: 14, ROND: 0, slnt: 0, SOFT: 0, SPAC: 0, wdth: 100, wght: 400, WONK: 0, XOPQ: 88, XROT: 0, XTRA: 400, YEAR: 2e3, YOPQ: 116, YROT: 0, YTAS: 750, YTDE: -250, YTFI: 600, YTLC: 500, YTUC: 725 }; function getQueryStringForAxisTupleList(axisTupleList) { const orderedTags = []; for (const tuple of axisTupleList) { for (const axis of tuple) { if (!orderedTags.includes(axis.tag)) { orderedTags.push(axis.tag); } } } if (orderedTags.length === 0) { return ""; } orderedTags.sort((a, b) => a.localeCompare(b, "en")); const axisTupleListStringBuilder = []; let index = 0; for (const tuple of axisTupleList) { axisTupleListStringBuilder[index] = []; for (const tag of orderedTags) { const axis = tuple.find((a) => a.tag === tag); axisTupleListStringBuilder[index].push(axis ? axis.value : axisDefaults[tag]); } index++; } axisTupleListStringBuilder.sort((a, b) => { for (const index2 in a) { const atA = numberOrRangeToNumber(a[index2]); const atB = numberOrRangeToNumber(b[index2]); if (atA !== atB) { return atA - atB; } } return 0; }); return `${orderedTags.join(",")}@${axisTupleListStringBuilder.map((a) => a.map((v) => isRange(v) ? `${v.min}..${v.max}` : v).join(",")).join(";")}`; } function generateFontUrl(font, options) { const axisQueryString = font.axisTupleList ? getQueryStringForAxisTupleList(font.axisTupleList) : ""; const searchParametersEntries = { family: font.family + (axisQueryString.length > 0 ? `:${axisQueryString}` : "") }; if ((options == null ? void 0 : options.text) !== void 0) { searchParametersEntries.text = options.text; } if ((options == null ? void 0 : options.display) !== void 0) { searchParametersEntries.display = options.display; } const searchParameters = new URLSearchParams(searchParametersEntries); return `https://fonts.googleapis.com/css2?${decodeURIComponent(searchParameters.toString())}`; } function mergeAxisTupleLists(a1, a2) { const newAxisTupleList = [...a1]; let changed = false; for (const axisTuple2 of a2) { if (!newAxisTupleList.some((newAxisTuple) => areAxisTuplesEquals(newAxisTuple, axisTuple2))) { newAxisTupleList.push(axisTuple2); changed = true; } } if (!changed) { return false; } return newAxisTupleList; } function areAxisTuplesEquals(axisTuple1, axisTuple2) { let longestAxisTuple = axisTuple1; let shortestAxisTuple = axisTuple2; if (axisTuple2.length > axisTuple1.length) { longestAxisTuple = axisTuple2; shortestAxisTuple = axisTuple1; } for (const axis1 of longestAxisTuple) { if (!shortestAxisTuple.some((axis2) => areAxisEquals(axis1, axis2) || areAxisEquals(axis1, { tag: axis1.tag, value: axisDefaults[axis1.tag] }))) { return false; } } return true; } function areAxisEquals(axis1, axis2) { if (axis1.tag !== axis2.tag) { return false; } if (isRange(axis1.value)) { if (!isRange(axis2.value)) { return false; } return axis1.value.min === axis2.value.min && axis1.value.max === axis2.value.max; } if (isRange(axis2.value)) { return false; } return axis1.value === axis2.value; } function numberOrRangeToNumber(v) { return isRange(v) ? v.min : v; } // src/listener.ts function awaitFontLoading(font, linkElement) { return __async(this, null, function* () { if (linkElement.sheet === null) { yield new Promise((resolve) => { linkElement.addEventListener("load", () => { resolve(); }); }); } yield font.axisTupleList === void 0 ? awaitAxisTupleLoading(font.family, []) : Promise.all(font.axisTupleList.map((axisTuple) => __async(this, null, function* () { return awaitAxisTupleLoading(font.family, axisTuple); }))); }); } function awaitAxisTupleLoading(family, tuple) { return __async(this, null, function* () { var _a; const isItalic = ((_a = tuple.find((axis) => axis.tag === "ital")) == null ? void 0 : _a.value) === 1; const weightAxis = tuple.find((axis) => axis.tag === "wght"); const weight = weightAxis === void 0 ? "normal" : numberOrRangeToNumber(weightAxis.value); const fontCssSpec = `${isItalic ? "italic" : "normal"} ${weight} 16px "${family}"`; const fontFaces = yield document.fonts.load(fontCssSpec); if (fontFaces.length > 0) { return; } return new Promise((resolve) => { const listenerCallback = () => { if (document.fonts.check(fontCssSpec)) { document.fonts.removeEventListener("loadingdone", listenerCallback); resolve(); } }; document.fonts.addEventListener("loadingdone", listenerCallback); }); }); } // src/load.ts function load(font, options) { initLoad(font, options); } function loadAwait(font, options) { return __async(this, null, function* () { const linkElement = initLoad(font, options); yield awaitFontLoading(font, linkElement); }); } function initLoad(font, options) { var _a, _b; if (registry.has(font.family)) { const currentFamilyRegistryItem = registry.get(font.family); if (!currentFamilyRegistryItem.element.isConnected) { console.warn("A google-fonts-loader-v2 link element was removed from the dom"); document.head.append(currentFamilyRegistryItem.element); } const mergeResult = mergeAxisTupleLists(currentFamilyRegistryItem.axisTupleList, (_a = font.axisTupleList) != null ? _a : []); if (!mergeResult) { return currentFamilyRegistryItem.element; } currentFamilyRegistryItem.element.href = generateFontUrl({ family: font.family, axisTupleList: mergeResult }, options); currentFamilyRegistryItem.axisTupleList = mergeResult; return currentFamilyRegistryItem.element; } const linkElement = document.createElement("link"); linkElement.rel = "stylesheet"; linkElement.href = generateFontUrl(font, options); linkElement.className = "google-fonts-loader-v2"; document.head.append(linkElement); registry.set(font.family, { element: linkElement, axisTupleList: (_b = font.axisTupleList) != null ? _b : [] }); return linkElement; } // src/unload.ts function unload(font) { if (registry.has(font.family)) { registry.delete(font.family); } } function unloadAll() { for (const [_, registryItem] of registry) { registryItem.element.remove(); } registry.clear(); } exports.list = list; exports.load = load; exports.loadAwait = loadAwait; exports.unload = unload; exports.unloadAll = unloadAll;