UNPKG

@realfavicongenerator/generate-favicon

Version:
113 lines (112 loc) 4.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.scaleSvg = exports.makeFluidSvg = exports.dataUrlToSvg = exports.anyUrlToSvg = exports.httpUrlToDataUrl = exports.bitmapToSvg = exports.blobToDataUrl = exports.urlToSvg = exports.stringToSvg = void 0; const svg_js_1 = require("@svgdotjs/svg.js"); const helper_1 = require("../icon/helper"); const helper_2 = require("./helper"); const stringToSvg = (svg, imageAdapeter) => { // We use filterDoctypeOut because of // https://github.com/RealFaviconGenerator/realfavicongenerator/issues/507 // and https://github.com/svgdotjs/svgdom/issues/130 const s = imageAdapeter.createSvg().svg((0, helper_2.filterDoctypeOut)(svg)); const subSvg = s.find('svg')[0]; if (subSvg) { const viewBox = subSvg.viewbox(); s.width(subSvg.width() ? subSvg.width() : viewBox.width); s.height(subSvg.height() ? subSvg.height() : viewBox.height); } return s; }; exports.stringToSvg = stringToSvg; const urlToSvg = (url, imageAdapter) => __awaiter(void 0, void 0, void 0, function* () { const response = yield fetch(url); const text = yield response.text(); return (0, exports.stringToSvg)(text, imageAdapter); }); exports.urlToSvg = urlToSvg; const blobToDataUrl = (blob) => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = () => { if (typeof reader.result === 'string') { resolve(reader.result); } else { reject('Failed to read blob'); } }; reader.onerror = (e) => { reject(e); }; reader.readAsDataURL(blob); }); }; exports.blobToDataUrl = blobToDataUrl; const bitmapToSvg = (binaryData, mimeType, imageAdapter) => __awaiter(void 0, void 0, void 0, function* () { const blob = new Blob([binaryData], { type: mimeType }); const url = yield (0, exports.blobToDataUrl)(blob); return (0, exports.dataUrlToSvg)(url, imageAdapter); }); exports.bitmapToSvg = bitmapToSvg; const httpUrlToDataUrl = (url, imageAdapter) => __awaiter(void 0, void 0, void 0, function* () { const response = yield fetch(url); const buffer = yield response.arrayBuffer(); const contentType = response.headers.get('content-type') || 'application/octet-stream'; return `data:${contentType};base64,${Buffer.from(buffer).toString('base64')}`; }); exports.httpUrlToDataUrl = httpUrlToDataUrl; const anyUrlToSvg = (url, imageAdapter) => __awaiter(void 0, void 0, void 0, function* () { const dataUrl = url.startsWith('http') ? (0, exports.httpUrlToDataUrl)(url, imageAdapter) : url; return (0, exports.dataUrlToSvg)(url, imageAdapter); }); exports.anyUrlToSvg = anyUrlToSvg; const dataUrlToSvg = (dataUrl, imageAdapter) => __awaiter(void 0, void 0, void 0, function* () { const { width, height } = yield imageAdapter.getImageSize(dataUrl); const svg = imageAdapter.createSvg().size(width, height); const i = svg.image(dataUrl); i.width(width); i.height(height); svg.viewbox(0, 0, width, height); return svg; }); exports.dataUrlToSvg = dataUrlToSvg; const makeFluidSvg = (svg) => { const viewBox = svg.viewbox(); const w = viewBox.width || 100; const h = viewBox.height || 100; svg.width('100%'); svg.height('100%'); svg.viewbox(0, 0, w, h); const children = svg.children(); if (children.length === 1) { const child = children[0]; if (child instanceof svg_js_1.Svg) { const viewBox = child.viewbox(); const w = viewBox.width; const h = viewBox.height; child.width('100%'); child.height('100%'); child.viewbox(0, 0, w, h); } } return svg; }; exports.makeFluidSvg = makeFluidSvg; const scaleSvg = (svg, widthHeight, imageAdapeter) => { const newSvg = imageAdapeter.createSvg(); newSvg.size(widthHeight, widthHeight); const group = newSvg.group(); group.attr('transform', `scale(${widthHeight / (0, helper_1.numberAliasToNumber)(svg.width())})`); group.add(svg.clone()); return newSvg; }; exports.scaleSvg = scaleSvg;